]> code.delx.au - gnu-emacs-elpa/blob - dired-async.el
Ensure dired-copy-preserve-time is passed to child with its current value.
[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 (and (null (eq file-creator 'backup-file))
170 (file-exists-p to)))
171 (dired-overwrite-confirmed ; for dired-handle-overwrite
172 (and overwrite
173 (let ((help-form '(format "\
174 Type SPC or `y' to overwrite file `%s',
175 DEL or `n' to skip to next,
176 ESC or `q' to not overwrite any of the remaining files,
177 `!' to overwrite all remaining files with no more questions." to)))
178 (dired-query 'overwrite-query
179 "Overwrite `%s'?" to))))
180 ;; must determine if FROM is marked before file-creator
181 ;; gets a chance to delete it (in case of a move).
182 (actual-marker-char
183 (cond ((integerp marker-char) marker-char)
184 (marker-char (dired-file-marker from)) ; slow
185 (t nil))))
186 ;; Handle the `dired-copy-file' file-creator specially
187 ;; When copying a directory to another directory or
188 ;; possibly to itself or one of its subdirectories.
189 ;; e.g "~/foo/" => "~/test/"
190 ;; or "~/foo/" =>"~/foo/"
191 ;; or "~/foo/ => ~/foo/bar/")
192 ;; In this case the 'name-constructor' have set the destination
193 ;; TO to "~/test/foo" because the old emacs23 behavior
194 ;; of `copy-directory' was to not create the subdirectory
195 ;; and instead copy the contents.
196 ;; With the new behavior of `copy-directory'
197 ;; (similar to the `cp' shell command) we don't
198 ;; need such a construction of the target directory,
199 ;; so modify the destination TO to "~/test/" instead of "~/test/foo/".
200 (let ((destname (file-name-directory to)))
201 (when (and (file-directory-p from)
202 (file-directory-p to)
203 (eq file-creator 'dired-copy-file))
204 (setq to destname))
205 ;; If DESTNAME is a subdirectory of FROM, not a symlink,
206 ;; and the method in use is copying, signal an error.
207 (and (eq t (car (file-attributes destname)))
208 (eq file-creator 'dired-copy-file)
209 (file-in-directory-p destname from)
210 (error "Cannot copy `%s' into its subdirectory `%s'"
211 from to)))
212 (if overwrite
213 (or (and dired-overwrite-confirmed
214 (push (cons from to) async-fn-list))
215 (progn
216 (push (dired-make-relative from) failures)
217 (dired-log "%s `%s' to `%s' failed"
218 operation from to)))
219 (push (cons from to) async-fn-list)))))
220 (setq callback
221 `(lambda (&optional ignore)
222 (dired-async-after-file-create ,total)
223 (when (string= ,(downcase operation) "rename")
224 (cl-loop for (file . to) in ',async-fn-list
225 do (and (get-file-buffer file)
226 (with-current-buffer (get-file-buffer file)
227 (set-visited-file-name to nil t))))))))
228 ;; Handle error happening in host emacs.
229 (cond
230 (dired-create-files-failures
231 (setq failures (nconc failures dired-create-files-failures))
232 (dired-log-summary
233 (format "%s failed for %d file%s in %d requests"
234 operation (length failures)
235 (dired-plural-s (length failures))
236 total)
237 failures))
238 (failures
239 (dired-log-summary
240 (format "%s failed for %d of %d file%s"
241 operation (length failures)
242 total (dired-plural-s total))
243 failures))
244 (skipped
245 (dired-log-summary
246 (format "%s: %d of %d file%s skipped"
247 operation (length skipped) total
248 (dired-plural-s total))
249 skipped))
250 (t (message "%s: %s file%s"
251 operation success-count (dired-plural-s success-count))))
252 ;; Start async process.
253 (when async-fn-list
254 (async-start `(lambda ()
255 (require 'cl-lib) (require 'dired-aux) (require 'dired-x)
256 ,(async-inject-variables dired-async-env-variables-regexp)
257 (condition-case err
258 (let ((dired-recursive-copies (quote always))
259 (dired-copy-preserve-time
260 ,dired-copy-preserve-time))
261 ;; Inline `backup-file' as long as it is not
262 ;; available in emacs.
263 (defalias 'backup-file
264 ;; Same feature as "cp --backup=numbered from to"
265 ;; Symlinks are copied as file from source unlike
266 ;; `dired-copy-file' which is same as cp -d.
267 ;; Directories are omitted.
268 (lambda (from to ok)
269 (cond ((file-directory-p from) (ignore))
270 (t (let ((count 0))
271 (while (let ((attrs (file-attributes to)))
272 (and attrs (null (nth 0 attrs))))
273 (cl-incf count)
274 (setq to (concat (file-name-sans-versions to)
275 (format ".~%s~" count)))))
276 (condition-case err
277 (copy-file from to ok dired-copy-preserve-time)
278 (file-date-error
279 (push (dired-make-relative from)
280 dired-create-files-failures)
281 (dired-log "Can't set date on %s:\n%s\n" from err)))))))
282 ;; Now run the FILE-CREATOR function on files.
283 (cl-loop with fn = (quote ,file-creator)
284 for (from . dest) in (quote ,async-fn-list)
285 do (funcall fn from dest t)))
286 (file-error
287 (with-temp-file ,dired-async-log-file
288 (insert (format "%S" err)))))
289 ,(dired-async-maybe-kill-ftp))
290 callback)
291 ;; Run mode-line notifications while process running.
292 (dired-async--modeline-mode 1)
293 (setq dired-async-operation (list operation (length async-fn-list)))
294 (message "%s proceeding asynchronously..." operation))))
295
296 (defadvice dired-create-files (around dired-async)
297 (dired-async-create-files file-creator operation fn-list
298 name-constructor marker-char))
299
300 ;;;###autoload
301 (define-minor-mode dired-async-mode
302 "Do dired actions asynchronously."
303 :group 'dired-async
304 :global t
305 (if dired-async-mode
306 (if (fboundp 'advice-add)
307 (advice-add 'dired-create-files :override #'dired-async-create-files)
308 (ad-activate 'dired-create-files))
309 (if (fboundp 'advice-remove)
310 (advice-remove 'dired-create-files #'dired-async-create-files)
311 (ad-deactivate 'dired-create-files))))
312
313
314 (provide 'dired-async)
315
316 ;;; dired-async.el ends here