]> code.delx.au - gnu-emacs-elpa/blob - dired-async.el
Add autoload cookie and make dired-async--modeline-mode internal.
[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 (defcustom dired-async-be-async t
72 "When non--nil make `dired-create-file' async.
73 This allow to turn off async features provided to this package."
74 :group 'dired-async
75 :type 'boolean)
76
77 (defface dired-async-message
78 '((t (:foreground "yellow")))
79 "Face used for mode-line message."
80 :group 'dired-async)
81
82 (defface dired-async-mode-message
83 '((t (:background "Firebrick1")))
84 "Face used for `dired-async--modeline-mode' lighter."
85 :group 'dired-async)
86
87 (define-minor-mode dired-async--modeline-mode
88 "Notify mode-line that an async process run."
89 :group 'dired-async
90 :global t
91 :lighter (:eval (propertize (format " [%s Async job(s) running]"
92 (length (dired-async-processes)))
93 'face 'dired-async-mode-message))
94 (unless dired-async--modeline-mode
95 (let ((visible-bell t)) (ding))))
96
97 (defun dired-async-mode-line-message (text &rest args)
98 "Notify end of operation in `mode-line'."
99 (message nil)
100 (let ((mode-line-format (concat
101 " " (propertize
102 (if args
103 (apply #'format text args)
104 text)
105 'face 'dired-async-message))))
106 (force-mode-line-update)
107 (sit-for 3)
108 (force-mode-line-update)))
109
110 (defun dired-async-processes ()
111 (cl-loop for p in (process-list)
112 when (cl-loop for c in (process-command p) thereis
113 (string= "async-batch-invoke" c))
114 collect p))
115
116 (defun dired-async-kill-process ()
117 (interactive)
118 (let* ((processes (dired-async-processes))
119 (proc (car (last processes))))
120 (delete-process proc)
121 (unless (> (length processes) 1)
122 (dired-async--modeline-mode -1))))
123
124 (defun dired-async-after-file-create (len-flist)
125 "Callback function used for operation handled by `dired-create-file'."
126 (unless (dired-async-processes)
127 ;; Turn off mode-line notification
128 ;; only when last process end.
129 (dired-async--modeline-mode -1))
130 (when dired-async-operation
131 (if (file-exists-p dired-async-log-file)
132 (progn
133 (pop-to-buffer (get-buffer-create "*dired async*"))
134 (erase-buffer)
135 (insert "Error: ")
136 (insert-file-contents dired-async-log-file)
137 (delete-file dired-async-log-file))
138 (run-with-timer
139 0.1 nil
140 dired-async-message-function "Asynchronous %s of %s file(s) on %s file(s) done"
141 (car dired-async-operation) (cadr dired-async-operation) len-flist))))
142
143 (defun dired-async-maybe-kill-ftp ()
144 "Return a form to kill ftp process in child emacs."
145 (quote
146 (progn
147 (require 'cl-lib)
148 (let ((buf (cl-loop for b in (buffer-list)
149 thereis (and (string-match
150 "\\`\\*ftp.*"
151 (buffer-name b)) b))))
152 (when buf (kill-buffer buf))))))
153
154 (defun dired-async-create-files (file-creator operation fn-list name-constructor
155 &optional marker-char)
156 "Same as `dired-create-files' but asynchronous.
157
158 See `dired-create-files' for the behavior of arguments."
159 (setq dired-async-operation nil)
160 (let (dired-create-files-failures failures async-fn-list
161 skipped (success-count 0) (total (length fn-list))
162 (callback `(lambda (&optional ignore)
163 (dired-async-after-file-create ,(length fn-list)))))
164 (let (to overwrite-query
165 overwrite-backup-query) ; for dired-handle-overwrite
166 (dolist (from fn-list)
167 (setq to (funcall name-constructor from))
168 (if (equal to from)
169 (progn
170 (setq to nil)
171 (dired-log "Cannot %s to same file: %s\n"
172 (downcase operation) from)))
173 (if (not to)
174 (setq skipped (cons (dired-make-relative from) skipped))
175 (let* ((overwrite (file-exists-p to))
176 (dired-overwrite-confirmed ; for dired-handle-overwrite
177 (and overwrite
178 (let ((help-form '(format "\
179 Type SPC or `y' to overwrite file `%s',
180 DEL or `n' to skip to next,
181 ESC or `q' to not overwrite any of the remaining files,
182 `!' to overwrite all remaining files with no more questions." to)))
183 (dired-query 'overwrite-query
184 "Overwrite `%s'?" to))))
185 ;; must determine if FROM is marked before file-creator
186 ;; gets a chance to delete it (in case of a move).
187 (actual-marker-char
188 (cond ((integerp marker-char) marker-char)
189 (marker-char (dired-file-marker from)) ; slow
190 (t nil))))
191 ;; Handle the `dired-copy-file' file-creator specially
192 ;; When copying a directory to another directory or
193 ;; possibly to itself or one of its subdirectories.
194 ;; e.g "~/foo/" => "~/test/"
195 ;; or "~/foo/" =>"~/foo/"
196 ;; or "~/foo/ => ~/foo/bar/")
197 ;; In this case the 'name-constructor' have set the destination
198 ;; TO to "~/test/foo" because the old emacs23 behavior
199 ;; of `copy-directory' was to not create the subdirectory
200 ;; and instead copy the contents.
201 ;; With the new behavior of `copy-directory'
202 ;; (similar to the `cp' shell command) we don't
203 ;; need such a construction of the target directory,
204 ;; so modify the destination TO to "~/test/" instead of "~/test/foo/".
205 (let ((destname (file-name-directory to)))
206 (when (and (file-directory-p from)
207 (file-directory-p to)
208 (eq file-creator 'dired-copy-file))
209 (setq to destname))
210 ;; If DESTNAME is a subdirectory of FROM, not a symlink,
211 ;; and the method in use is copying, signal an error.
212 (and (eq t (car (file-attributes destname)))
213 (eq file-creator 'dired-copy-file)
214 (file-in-directory-p destname from)
215 (error "Cannot copy `%s' into its subdirectory `%s'"
216 from to)))
217 (if overwrite
218 (or (and dired-overwrite-confirmed
219 (push (cons from to) async-fn-list))
220 (progn
221 (push (dired-make-relative from) failures)
222 (dired-log "%s `%s' to `%s' failed"
223 operation from to)))
224 (push (cons from to) async-fn-list))))))
225 ;; Handle error happening in host emacs.
226 (cond
227 (dired-create-files-failures
228 (setq failures (nconc failures dired-create-files-failures))
229 (dired-log-summary
230 (format "%s failed for %d file%s in %d requests"
231 operation (length failures)
232 (dired-plural-s (length failures))
233 total)
234 failures))
235 (failures
236 (dired-log-summary
237 (format "%s failed for %d of %d file%s"
238 operation (length failures)
239 total (dired-plural-s total))
240 failures))
241 (skipped
242 (dired-log-summary
243 (format "%s: %d of %d file%s skipped"
244 operation (length skipped) total
245 (dired-plural-s total))
246 skipped))
247 (t (message "%s: %s file%s"
248 operation success-count (dired-plural-s success-count))))
249 ;; Start async process.
250 (when async-fn-list
251 (async-start `(lambda ()
252 (require 'cl-lib) (require 'dired-aux) (require 'dired-x)
253 ,(async-inject-variables dired-async-env-variables-regexp)
254 (condition-case err
255 (let ((dired-recursive-copies (quote always)))
256 (cl-loop for (f . d) in (quote ,async-fn-list)
257 do (funcall (quote ,file-creator) f d t)))
258 (file-error
259 (with-temp-file ,dired-async-log-file
260 (insert (format "%S" err)))))
261 ,(dired-async-maybe-kill-ftp))
262 callback)
263 ;; Run mode-line notifications while process running.
264 (dired-async--modeline-mode 1)
265 (setq dired-async-operation (list operation (length async-fn-list)))
266 (message "%s proceeding asynchronously..." operation))))
267
268 ;;;###autoload
269 (define-minor-mode dired-async-mode
270 "Do dired actions asynchronously."
271 :group 'helm
272 :global t
273 (if dired-async-mode
274 (advice-add 'dired-create-files :override #'dired-async-create-files)
275 (advice-remove 'dired-create-files #'dired-async-create-files)))
276
277
278 (provide 'dired-async)
279
280 ;;; dired-async.el ends here