]> code.delx.au - gnu-emacs-elpa/blob - dired-async.el
df8ea01d38cdaafbde7e7cee48cd34cecc743569
[gnu-emacs-elpa] / dired-async.el
1 ;;; dired-async.el --- Asynchronous dired actions -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2012-2016 Free Software Foundation, Inc.
4
5 ;; Authors: John Wiegley <jwiegley@gmail.com>
6 ;; Thierry Volpiatto <thierry.volpiatto@gmail.com>
7
8 ;; Keywords: dired async network
9 ;; X-URL: https://github.com/jwiegley/dired-async
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25 \f
26 ;;; Commentary:
27
28 ;; This file provide a redefinition of `dired-create-file' function,
29 ;; performs copies, moves and all what is handled by `dired-create-file'
30 ;; in the background using a slave Emacs process,
31 ;; by means of the async.el module.
32 ;; To use it, put this in your .emacs:
33
34 ;; (dired-async-mode 1)
35
36 ;; This will enable async copy/rename etc...
37 ;; in dired and helm.
38
39 ;;; Code:
40 \f
41 (require 'cl-lib)
42 (require 'dired-aux)
43 (require 'async)
44
45 (eval-when-compile
46 (defvar async-callback))
47
48 (defgroup dired-async nil
49 "Copy rename files asynchronously from dired."
50 :group 'dired)
51
52 (defcustom dired-async-env-variables-regexp
53 "\\`\\(tramp-\\(default\\|connection\\|remote\\)\\|ange-ftp\\)-.*"
54 "Variables matching this regexp will be loaded on Child Emacs."
55 :type 'regexp
56 :group 'dired-async)
57
58 (defcustom dired-async-message-function 'dired-async-mode-line-message
59 "Function to use to notify result when operation finish.
60 Should take same args as `message'."
61 :group 'dired-async
62 :type 'function)
63
64 (defcustom dired-async-log-file "/tmp/dired-async.log"
65 "File use to communicate errors from Child Emacs to host Emacs."
66 :group 'dired-async
67 :type 'string)
68
69 (defface dired-async-message
70 '((t (:foreground "yellow")))
71 "Face used for mode-line message."
72 :group 'dired-async)
73
74 (defface dired-async-failures
75 '((t (:foreground "red")))
76 "Face used for mode-line message."
77 :group 'dired-async)
78
79 (defface dired-async-mode-message
80 '((t (:foreground "Gold")))
81 "Face used for `dired-async--modeline-mode' lighter."
82 :group 'dired-async)
83
84 (define-minor-mode dired-async--modeline-mode
85 "Notify mode-line that an async process run."
86 :group 'dired-async
87 :global t
88 :lighter (:eval (propertize (format " [%s Async job(s) running]"
89 (length (dired-async-processes)))
90 'face 'dired-async-mode-message))
91 (unless dired-async--modeline-mode
92 (let ((visible-bell t)) (ding))))
93
94 (defun dired-async-mode-line-message (text face &rest args)
95 "Notify end of operation in `mode-line'."
96 (message nil)
97 (let ((mode-line-format (concat
98 " " (propertize
99 (if args
100 (apply #'format text args)
101 text)
102 'face face))))
103 (force-mode-line-update)
104 (sit-for 3)
105 (force-mode-line-update)))
106
107 (defun dired-async-processes ()
108 (cl-loop for p in (process-list)
109 when (cl-loop for c in (process-command p) thereis
110 (string= "async-batch-invoke" c))
111 collect p))
112
113 (defun dired-async-kill-process ()
114 (interactive)
115 (let* ((processes (dired-async-processes))
116 (proc (car (last processes))))
117 (and proc (delete-process proc))
118 (unless (> (length processes) 1)
119 (dired-async--modeline-mode -1))))
120
121 (defun dired-async-after-file-create (total operation failures skipped)
122 "Callback function used for operation handled by `dired-create-file'."
123 (unless (dired-async-processes)
124 ;; Turn off mode-line notification
125 ;; only when last process end.
126 (dired-async--modeline-mode -1))
127 (when operation
128 (if (file-exists-p dired-async-log-file)
129 (progn
130 (pop-to-buffer (get-buffer-create "*dired async*"))
131 (erase-buffer)
132 (insert "Error: ")
133 (insert-file-contents dired-async-log-file)
134 (delete-file dired-async-log-file))
135 (run-with-timer
136 0.1 nil
137 (lambda ()
138 ;; First send error messages.
139 (cond (failures
140 (funcall dired-async-message-function
141 "%s failed for %d of %d file%s"
142 'dired-async-failures
143 (car operation) (length failures)
144 total (dired-plural-s total)))
145 (skipped
146 (funcall dired-async-message-function
147 "%s: %d of %d file%s skipped"
148 'dired-async-failures
149 (car operation) (length skipped) total
150 (dired-plural-s total))))
151 ;; Finally send the success message.
152 (funcall dired-async-message-function
153 "Asynchronous %s of %s file(s) on %s file(s) done"
154 'dired-async-message
155 (car operation) (cadr operation) total))))))
156
157 (defun dired-async-maybe-kill-ftp ()
158 "Return a form to kill ftp process in child emacs."
159 (quote
160 (progn
161 (require 'cl-lib)
162 (let ((buf (cl-loop for b in (buffer-list)
163 thereis (and (string-match
164 "\\`\\*ftp.*"
165 (buffer-name b)) b))))
166 (when buf (kill-buffer buf))))))
167
168 (defvar overwrite-query)
169 (defun dired-async-create-files (file-creator operation fn-list name-constructor
170 &optional _marker-char)
171 "Same as `dired-create-files' but asynchronous.
172
173 See `dired-create-files' for the behavior of arguments."
174 (setq overwrite-query nil)
175 (let ((total (length fn-list))
176 failures async-fn-list skipped callback)
177 (let (to)
178 (dolist (from fn-list)
179 (setq to (funcall name-constructor from))
180 (if (equal to from)
181 (progn
182 (setq to nil)
183 (dired-log "Cannot %s to same file: %s\n"
184 (downcase operation) from)))
185 (if (not to)
186 (setq skipped (cons (dired-make-relative from) skipped))
187 (let* ((overwrite (and (null (eq file-creator 'backup-file))
188 (file-exists-p to)))
189 (dired-overwrite-confirmed ; for dired-handle-overwrite
190 (and overwrite
191 (let ((help-form `(format "\
192 Type SPC or `y' to overwrite file `%s',
193 DEL or `n' to skip to next,
194 ESC or `q' to not overwrite any of the remaining files,
195 `!' to overwrite all remaining files with no more questions." ,to)))
196 (dired-query 'overwrite-query "Overwrite `%s'?" to)))))
197 ;; Handle the `dired-copy-file' file-creator specially
198 ;; When copying a directory to another directory or
199 ;; possibly to itself or one of its subdirectories.
200 ;; e.g "~/foo/" => "~/test/"
201 ;; or "~/foo/" =>"~/foo/"
202 ;; or "~/foo/ => ~/foo/bar/")
203 ;; In this case the 'name-constructor' have set the destination
204 ;; TO to "~/test/foo" because the old emacs23 behavior
205 ;; of `copy-directory' was to not create the subdirectory
206 ;; and instead copy the contents.
207 ;; With the new behavior of `copy-directory'
208 ;; (similar to the `cp' shell command) we don't
209 ;; need such a construction of the target directory,
210 ;; so modify the destination TO to "~/test/" instead of "~/test/foo/".
211 (let ((destname (file-name-directory to)))
212 (when (and (file-directory-p from)
213 (file-directory-p to)
214 (eq file-creator 'dired-copy-file))
215 (setq to destname))
216 ;; If DESTNAME is a subdirectory of FROM, not a symlink,
217 ;; and the method in use is copying, signal an error.
218 (and (eq t (car (file-attributes destname)))
219 (eq file-creator 'dired-copy-file)
220 (file-in-directory-p destname from)
221 (error "Cannot copy `%s' into its subdirectory `%s'"
222 from to)))
223 (if overwrite
224 (or (and dired-overwrite-confirmed
225 (push (cons from to) async-fn-list))
226 (progn
227 (push (dired-make-relative from) failures)
228 (dired-log "%s `%s' to `%s' failed\n"
229 operation from to)))
230 (push (cons from to) async-fn-list)))))
231 ;; When failures have been printed to dired log add the date at bob.
232 (when (or failures skipped) (dired-log t))
233 ;; When async-fn-list is empty that's mean only one file
234 ;; had to be copied and user finally answer NO.
235 ;; In this case async process will never start and callback
236 ;; will have no chance to run, so notify failures here.
237 (unless async-fn-list
238 (cond (failures
239 (funcall dired-async-message-function
240 "%s failed for %d of %d file%s"
241 'dired-async-failures
242 operation (length failures)
243 total (dired-plural-s total)))
244 (skipped
245 (funcall dired-async-message-function
246 "%s: %d of %d file%s skipped"
247 'dired-async-failures
248 operation (length skipped) total
249 (dired-plural-s total)))))
250 ;; Setup callback.
251 (setq callback
252 (lambda (&optional _ignore)
253 (dired-async-after-file-create
254 total (list operation (length async-fn-list)) failures skipped)
255 (when (string= (downcase operation) "rename")
256 (cl-loop for (file . to) in async-fn-list
257 for bf = (get-file-buffer file)
258 do (and bf (with-current-buffer bf
259 (set-visited-file-name to nil t))))))))
260 ;; Start async process.
261 (when async-fn-list
262 (async-start `(lambda ()
263 (require 'cl-lib) (require 'dired-aux) (require 'dired-x)
264 ,(async-inject-variables dired-async-env-variables-regexp)
265 (condition-case err
266 (let ((dired-recursive-copies (quote always))
267 (dired-copy-preserve-time
268 ,dired-copy-preserve-time))
269 (setq overwrite-backup-query nil)
270 ;; Inline `backup-file' as long as it is not
271 ;; available in emacs.
272 (defalias 'backup-file
273 ;; Same feature as "cp --backup=numbered from to"
274 ;; Symlinks are copied as file from source unlike
275 ;; `dired-copy-file' which is same as cp -d.
276 ;; Directories are omitted.
277 (lambda (from to ok)
278 (cond ((file-directory-p from) (ignore))
279 (t (let ((count 0))
280 (while (let ((attrs (file-attributes to)))
281 (and attrs (null (nth 0 attrs))))
282 (cl-incf count)
283 (setq to (concat (file-name-sans-versions to)
284 (format ".~%s~" count)))))
285 (condition-case err
286 (copy-file from to ok dired-copy-preserve-time)
287 (file-date-error
288 (push (dired-make-relative from)
289 dired-create-files-failures)
290 (dired-log "Can't set date on %s:\n%s\n" from err)))))))
291 ;; Now run the FILE-CREATOR function on files.
292 (cl-loop with fn = (quote ,file-creator)
293 for (from . dest) in (quote ,async-fn-list)
294 do (funcall fn from dest t)))
295 (file-error
296 (with-temp-file ,dired-async-log-file
297 (insert (format "%S" err)))))
298 ,(dired-async-maybe-kill-ftp))
299 callback)
300 ;; Run mode-line notifications while process running.
301 (dired-async--modeline-mode 1)
302 (message "%s proceeding asynchronously..." operation))))
303
304 (defadvice dired-create-files (around dired-async)
305 (dired-async-create-files file-creator operation fn-list
306 name-constructor marker-char))
307
308 ;;;###autoload
309 (define-minor-mode dired-async-mode
310 "Do dired actions asynchronously."
311 :group 'dired-async
312 :global t
313 (if dired-async-mode
314 (if (fboundp 'advice-add)
315 (advice-add 'dired-create-files :override #'dired-async-create-files)
316 (ad-activate 'dired-create-files))
317 (if (fboundp 'advice-remove)
318 (advice-remove 'dired-create-files #'dired-async-create-files)
319 (ad-deactivate 'dired-create-files))))
320
321
322 (provide 'dired-async)
323
324 ;;; dired-async.el ends here