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