]> code.delx.au - gnu-emacs-elpa/blob - dired-async.el
Assign copyrights to the FSF, add autoloads file
[gnu-emacs-elpa] / dired-async.el
1 ;;; dired-async.el --- Copy/move/delete asynchronously in dired.
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 (defvar dired-async-operation nil)
48
49 (defgroup dired-async nil
50 "Copy rename files asynchronously from dired."
51 :group 'dired)
52
53 (defcustom dired-async-env-variables-regexp
54 "\\`\\(tramp-\\(default\\|connection\\|remote\\)\\|ange-ftp\\)-.*"
55 "Variables matching this regexp will be loaded on Child Emacs."
56 :type 'regexp
57 :group 'dired-async)
58
59 (defcustom dired-async-message-function 'dired-async-mode-line-message
60 "Function to use to notify result when operation finish.
61 Should take same args as `message'."
62 :group 'dired-async
63 :type 'function)
64
65 (defcustom dired-async-log-file "/tmp/dired-async.log"
66 "File use to communicate errors from Child Emacs to host Emacs."
67 :group 'dired-async
68 :type 'string)
69
70 (defface dired-async-message
71 '((t (:foreground "yellow")))
72 "Face used for mode-line message."
73 :group 'dired-async)
74
75 (defface dired-async-mode-message
76 '((t (:foreground "Gold")))
77 "Face used for `dired-async--modeline-mode' lighter."
78 :group 'dired-async)
79
80 (define-minor-mode dired-async--modeline-mode
81 "Notify mode-line that an async process run."
82 :group 'dired-async
83 :global t
84 :lighter (:eval (propertize (format " [%s Async job(s) running]"
85 (length (dired-async-processes)))
86 'face 'dired-async-mode-message))
87 (unless dired-async--modeline-mode
88 (let ((visible-bell t)) (ding))))
89
90 (defun dired-async-mode-line-message (text &rest args)
91 "Notify end of operation in `mode-line'."
92 (message nil)
93 (let ((mode-line-format (concat
94 " " (propertize
95 (if args
96 (apply #'format text args)
97 text)
98 'face 'dired-async-message))))
99 (force-mode-line-update)
100 (sit-for 3)
101 (force-mode-line-update)))
102
103 (defun dired-async-processes ()
104 (cl-loop for p in (process-list)
105 when (cl-loop for c in (process-command p) thereis
106 (string= "async-batch-invoke" c))
107 collect p))
108
109 (defun dired-async-kill-process ()
110 (interactive)
111 (let* ((processes (dired-async-processes))
112 (proc (car (last processes))))
113 (delete-process proc)
114 (unless (> (length processes) 1)
115 (dired-async--modeline-mode -1))))
116
117 (defun dired-async-after-file-create (len-flist)
118 "Callback function used for operation handled by `dired-create-file'."
119 (unless (dired-async-processes)
120 ;; Turn off mode-line notification
121 ;; only when last process end.
122 (dired-async--modeline-mode -1))
123 (when dired-async-operation
124 (if (file-exists-p dired-async-log-file)
125 (progn
126 (pop-to-buffer (get-buffer-create "*dired async*"))
127 (erase-buffer)
128 (insert "Error: ")
129 (insert-file-contents dired-async-log-file)
130 (delete-file dired-async-log-file))
131 (run-with-timer
132 0.1 nil
133 dired-async-message-function "Asynchronous %s of %s file(s) on %s file(s) done"
134 (car dired-async-operation) (cadr dired-async-operation) len-flist))))
135
136 (defun dired-async-maybe-kill-ftp ()
137 "Return a form to kill ftp process in child emacs."
138 (quote
139 (progn
140 (require 'cl-lib)
141 (let ((buf (cl-loop for b in (buffer-list)
142 thereis (and (string-match
143 "\\`\\*ftp.*"
144 (buffer-name b)) b))))
145 (when buf (kill-buffer buf))))))
146
147 (defun dired-async-create-files (file-creator operation fn-list name-constructor
148 &optional marker-char)
149 "Same as `dired-create-files' but asynchronous.
150
151 See `dired-create-files' for the behavior of arguments."
152 (setq dired-async-operation nil)
153 (let (dired-create-files-failures
154 failures async-fn-list
155 skipped (success-count 0)
156 (total (length fn-list))
157 callback)
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 (setq callback
220 `(lambda (&optional ignore)
221 (dired-async-after-file-create ,total)
222 (when (string= ,(downcase operation) "rename")
223 (cl-loop for (file . to) in ',async-fn-list
224 do (and (get-file-buffer file)
225 (with-current-buffer (get-file-buffer file)
226 (set-visited-file-name to nil t))))))))
227 ;; Handle error happening in host emacs.
228 (cond
229 (dired-create-files-failures
230 (setq failures (nconc failures dired-create-files-failures))
231 (dired-log-summary
232 (format "%s failed for %d file%s in %d requests"
233 operation (length failures)
234 (dired-plural-s (length failures))
235 total)
236 failures))
237 (failures
238 (dired-log-summary
239 (format "%s failed for %d of %d file%s"
240 operation (length failures)
241 total (dired-plural-s total))
242 failures))
243 (skipped
244 (dired-log-summary
245 (format "%s: %d of %d file%s skipped"
246 operation (length skipped) total
247 (dired-plural-s total))
248 skipped))
249 (t (message "%s: %s file%s"
250 operation success-count (dired-plural-s success-count))))
251 ;; Start async process.
252 (when async-fn-list
253 (async-start `(lambda ()
254 (require 'cl-lib) (require 'dired-aux) (require 'dired-x)
255 ,(async-inject-variables dired-async-env-variables-regexp)
256 (condition-case err
257 (let ((dired-recursive-copies (quote always)))
258 (cl-loop for (f . d) in (quote ,async-fn-list)
259 do (funcall (quote ,file-creator) f d t)))
260 (file-error
261 (with-temp-file ,dired-async-log-file
262 (insert (format "%S" err)))))
263 ,(dired-async-maybe-kill-ftp))
264 callback)
265 ;; Run mode-line notifications while process running.
266 (dired-async--modeline-mode 1)
267 (setq dired-async-operation (list operation (length async-fn-list)))
268 (message "%s proceeding asynchronously..." operation))))
269
270 (defadvice dired-create-files (around dired-async)
271 (dired-async-create-files file-creator operation fn-list
272 name-constructor marker-char))
273
274 ;;;###autoload
275 (define-minor-mode dired-async-mode
276 "Do dired actions asynchronously."
277 :group 'dired-async
278 :global t
279 (if dired-async-mode
280 (if (fboundp 'advice-add)
281 (advice-add 'dired-create-files :override #'dired-async-create-files)
282 (ad-activate 'dired-create-files))
283 (if (fboundp 'advice-remove)
284 (advice-remove 'dired-create-files #'dired-async-create-files)
285 (ad-deactivate 'dired-create-files))))
286
287
288 (provide 'dired-async)
289
290 ;;; dired-async.el ends here