1 ;;; dired-async.el --- Asynchronous dired actions -*- lexical-binding: t -*-
3 ;; Copyright (C) 2012-2016 Free Software Foundation, Inc.
5 ;; Authors: John Wiegley <jwiegley@gmail.com>
6 ;; Thierry Volpiatto <thierry.volpiatto@gmail.com>
8 ;; Keywords: dired async network
9 ;; X-URL: https://github.com/jwiegley/dired-async
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.
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.
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.
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:
34 ;; (dired-async-mode 1)
36 ;; This will enable async copy/rename etc...
46 (defvar async-callback))
48 (defgroup dired-async nil
49 "Copy rename files asynchronously from dired."
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."
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'."
64 (defcustom dired-async-log-file "/tmp/dired-async.log"
65 "File use to communicate errors from Child Emacs to host Emacs."
69 (defface dired-async-message
70 '((t (:foreground "yellow")))
71 "Face used for mode-line message."
74 (defface dired-async-failures
75 '((t (:foreground "red")))
76 "Face used for mode-line message."
79 (defface dired-async-mode-message
80 '((t (:foreground "Gold")))
81 "Face used for `dired-async--modeline-mode' lighter."
84 (define-minor-mode dired-async--modeline-mode
85 "Notify mode-line that an async process run."
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))))
94 (defun dired-async-mode-line-message (text face &rest args)
95 "Notify end of operation in `mode-line'."
97 (let ((mode-line-format (concat
100 (apply #'format text args)
103 (force-mode-line-update)
105 (force-mode-line-update)))
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))
113 (defun dired-async-kill-process ()
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))))
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))
128 (if (file-exists-p dired-async-log-file)
130 (pop-to-buffer (get-buffer-create "*dired async*"))
133 (insert-file-contents dired-async-log-file)
134 (delete-file dired-async-log-file))
138 ;; First send error messages.
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)))
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"
155 (car operation) (cadr operation) total))))))
157 (defun dired-async-maybe-kill-ftp ()
158 "Return a form to kill ftp process in child emacs."
162 (let ((buf (cl-loop for b in (buffer-list)
163 thereis (and (string-match
165 (buffer-name b)) b))))
166 (when buf (kill-buffer buf))))))
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.
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)
178 (dolist (from fn-list)
179 (setq to (funcall name-constructor from))
183 (dired-log "Cannot %s to same file: %s\n"
184 (downcase operation) from)))
186 (setq skipped (cons (dired-make-relative from) skipped))
187 (let* ((overwrite (and (null (eq file-creator 'backup-file))
189 (dired-overwrite-confirmed ; for dired-handle-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))
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'"
224 (or (and dired-overwrite-confirmed
225 (push (cons from to) async-fn-list))
227 (push (dired-make-relative from) failures)
228 (dired-log "%s `%s' to `%s' failed\n"
230 (push (cons from to) async-fn-list)))))
231 ;; When async-fn-list is empty that's mean only one file
232 ;; had to be copied and user finally answer NO.
233 ;; In this case async process will never start and callback
234 ;; will have no chance to run, so notify failures here.
235 (unless async-fn-list
237 (funcall dired-async-message-function
238 "%s failed for %d of %d file%s"
239 'dired-async-failures
240 operation (length failures)
241 total (dired-plural-s total)))
243 (funcall dired-async-message-function
244 "%s: %d of %d file%s skipped"
245 'dired-async-failures
246 operation (length skipped) total
247 (dired-plural-s total)))))
250 (lambda (&optional _ignore)
251 (dired-async-after-file-create
252 total (list operation (length async-fn-list)) failures skipped)
253 (when (string= (downcase operation) "rename")
254 (cl-loop for (file . to) in async-fn-list
255 for bf = (get-file-buffer file)
256 do (and bf (with-current-buffer bf
257 (set-visited-file-name to nil t))))))))
258 ;; Start async process.
260 (async-start `(lambda ()
261 (require 'cl-lib) (require 'dired-aux) (require 'dired-x)
262 ,(async-inject-variables dired-async-env-variables-regexp)
264 (let ((dired-recursive-copies (quote always))
265 (dired-copy-preserve-time
266 ,dired-copy-preserve-time))
267 (setq overwrite-backup-query nil)
268 ;; Inline `backup-file' as long as it is not
269 ;; available in emacs.
270 (defalias 'backup-file
271 ;; Same feature as "cp --backup=numbered from to"
272 ;; Symlinks are copied as file from source unlike
273 ;; `dired-copy-file' which is same as cp -d.
274 ;; Directories are omitted.
276 (cond ((file-directory-p from) (ignore))
278 (while (let ((attrs (file-attributes to)))
279 (and attrs (null (nth 0 attrs))))
281 (setq to (concat (file-name-sans-versions to)
282 (format ".~%s~" count)))))
284 (copy-file from to ok dired-copy-preserve-time)
286 (push (dired-make-relative from)
287 dired-create-files-failures)
288 (dired-log "Can't set date on %s:\n%s\n" from err)))))))
289 ;; Now run the FILE-CREATOR function on files.
290 (cl-loop with fn = (quote ,file-creator)
291 for (from . dest) in (quote ,async-fn-list)
292 do (funcall fn from dest t)))
294 (with-temp-file ,dired-async-log-file
295 (insert (format "%S" err)))))
296 ,(dired-async-maybe-kill-ftp))
298 ;; Run mode-line notifications while process running.
299 (dired-async--modeline-mode 1)
300 (message "%s proceeding asynchronously..." operation))))
302 (defadvice dired-create-files (around dired-async)
303 (dired-async-create-files file-creator operation fn-list
304 name-constructor marker-char))
307 (define-minor-mode dired-async-mode
308 "Do dired actions asynchronously."
312 (if (fboundp 'advice-add)
313 (advice-add 'dired-create-files :override #'dired-async-create-files)
314 (ad-activate 'dired-create-files))
315 (if (fboundp 'advice-remove)
316 (advice-remove 'dired-create-files #'dired-async-create-files)
317 (ad-deactivate 'dired-create-files))))
320 (provide 'dired-async)
322 ;;; dired-async.el ends here