]> code.delx.au - gnu-emacs-elpa/blob - dired-async.el
DRY use apply.
[gnu-emacs-elpa] / dired-async.el
1 ;;; dired-async.el --- Asynchronous dired actions -*- lexical-binding: t -*-
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-failures
76 '((t (:foreground "red")))
77 "Face used for mode-line message."
78 :group 'dired-async)
79
80 (defface dired-async-mode-message
81 '((t (:foreground "Gold")))
82 "Face used for `dired-async--modeline-mode' lighter."
83 :group 'dired-async)
84
85 (define-minor-mode dired-async--modeline-mode
86 "Notify mode-line that an async process run."
87 :group 'dired-async
88 :global t
89 :lighter (:eval (propertize (format " [%s Async job(s) running]"
90 (length (dired-async-processes)))
91 'face 'dired-async-mode-message))
92 (unless dired-async--modeline-mode
93 (let ((visible-bell t)) (ding))))
94
95 (defun dired-async-mode-line-message (text face &rest args)
96 "Notify end of operation in `mode-line'."
97 (message nil)
98 (let ((mode-line-format (concat
99 " " (propertize
100 (if args
101 (apply #'format text args)
102 text)
103 'face face))))
104 (force-mode-line-update)
105 (sit-for 3)
106 (force-mode-line-update)))
107
108 (defun dired-async-processes ()
109 (cl-loop for p in (process-list)
110 when (cl-loop for c in (process-command p) thereis
111 (string= "async-batch-invoke" c))
112 collect p))
113
114 (defun dired-async-kill-process ()
115 (interactive)
116 (let* ((processes (dired-async-processes))
117 (proc (car (last processes))))
118 (and proc (delete-process proc))
119 (unless (> (length processes) 1)
120 (dired-async--modeline-mode -1))))
121
122 (defun dired-async-after-file-create (total operation failures skipped)
123 "Callback function used for operation handled by `dired-create-file'."
124 (unless (dired-async-processes)
125 ;; Turn off mode-line notification
126 ;; only when last process end.
127 (dired-async--modeline-mode -1))
128 (when operation
129 (if (file-exists-p dired-async-log-file)
130 (progn
131 (pop-to-buffer (get-buffer-create "*dired async*"))
132 (erase-buffer)
133 (insert "Error: ")
134 (insert-file-contents dired-async-log-file)
135 (delete-file dired-async-log-file))
136 (run-with-timer
137 0.1 nil
138 (lambda ()
139 ;; First send error messages.
140 (apply dired-async-message-function
141 (cond (failures
142 (list "%s failed for %d of %d file%s"
143 'dired-async-failures
144 (car operation) (length failures)
145 total (dired-plural-s total)))
146 (skipped
147 (list "%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"
154 'dired-async-message
155 (car operation) (cadr operation) total))))))
156
157 (defun dired-async-maybe-kill-ftp ()
158 "Return a form to kill ftp process in child emacs."
159 (quote
160 (progn
161 (require 'cl-lib)
162 (let ((buf (cl-loop for b in (buffer-list)
163 thereis (and (string-match
164 "\\`\\*ftp.*"
165 (buffer-name b)) b))))
166 (when buf (kill-buffer buf))))))
167
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.
172
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)
177 (let (to)
178 (dolist (from fn-list)
179 (setq to (funcall name-constructor from))
180 (if (equal to from)
181 (progn
182 (setq to nil)
183 (dired-log "Cannot %s to same file: %s\n"
184 (downcase operation) from)))
185 (if (not to)
186 (setq skipped (cons (dired-make-relative from) skipped))
187 (let* ((overwrite (and (null (eq file-creator 'backup-file))
188 (file-exists-p to)))
189 (dired-overwrite-confirmed ; for dired-handle-overwrite
190 (and 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))
215 (setq to destname))
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'"
222 from to)))
223 (if overwrite
224 (or (and dired-overwrite-confirmed
225 (push (cons from to) async-fn-list))
226 (progn
227 (push (dired-make-relative from) failures)
228 (dired-log "%s `%s' to `%s' failed"
229 operation from to)))
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
236 (apply dired-async-message-function
237 (cond (failures
238 (list "%s failed for %d of %d file%s"
239 'dired-async-failures
240 operation (length failures)
241 total (dired-plural-s total)))
242 (skipped
243 (list "%s: %d of %d file%s skipped"
244 'dired-async-failures
245 operation (length skipped) total
246 (dired-plural-s total))))))
247 ;; Setup callback.
248 (setq callback
249 (lambda (&optional _ignore)
250 (dired-async-after-file-create
251 total (list operation (length async-fn-list)) failures skipped)
252 (when (string= (downcase operation) "rename")
253 (cl-loop for (file . to) in async-fn-list
254 for bf = (get-file-buffer file)
255 do (and bf (with-current-buffer bf
256 (set-visited-file-name to nil t))))))))
257 ;; Start async process.
258 (when async-fn-list
259 (async-start `(lambda ()
260 (require 'cl-lib) (require 'dired-aux) (require 'dired-x)
261 ,(async-inject-variables dired-async-env-variables-regexp)
262 (condition-case err
263 (let ((dired-recursive-copies (quote always))
264 (dired-copy-preserve-time
265 ,dired-copy-preserve-time))
266 (setq overwrite-backup-query nil)
267 ;; Inline `backup-file' as long as it is not
268 ;; available in emacs.
269 (defalias 'backup-file
270 ;; Same feature as "cp --backup=numbered from to"
271 ;; Symlinks are copied as file from source unlike
272 ;; `dired-copy-file' which is same as cp -d.
273 ;; Directories are omitted.
274 (lambda (from to ok)
275 (cond ((file-directory-p from) (ignore))
276 (t (let ((count 0))
277 (while (let ((attrs (file-attributes to)))
278 (and attrs (null (nth 0 attrs))))
279 (cl-incf count)
280 (setq to (concat (file-name-sans-versions to)
281 (format ".~%s~" count)))))
282 (condition-case err
283 (copy-file from to ok dired-copy-preserve-time)
284 (file-date-error
285 (push (dired-make-relative from)
286 dired-create-files-failures)
287 (dired-log "Can't set date on %s:\n%s\n" from err)))))))
288 ;; Now run the FILE-CREATOR function on files.
289 (cl-loop with fn = (quote ,file-creator)
290 for (from . dest) in (quote ,async-fn-list)
291 do (funcall fn from dest t)))
292 (file-error
293 (with-temp-file ,dired-async-log-file
294 (insert (format "%S" err)))))
295 ,(dired-async-maybe-kill-ftp))
296 callback)
297 ;; Run mode-line notifications while process running.
298 (dired-async--modeline-mode 1)
299 (message "%s proceeding asynchronously..." operation))))
300
301 (defadvice dired-create-files (around dired-async)
302 (dired-async-create-files file-creator operation fn-list
303 name-constructor marker-char))
304
305 ;;;###autoload
306 (define-minor-mode dired-async-mode
307 "Do dired actions asynchronously."
308 :group 'dired-async
309 :global t
310 (if dired-async-mode
311 (if (fboundp 'advice-add)
312 (advice-add 'dired-create-files :override #'dired-async-create-files)
313 (ad-activate 'dired-create-files))
314 (if (fboundp 'advice-remove)
315 (advice-remove 'dired-create-files #'dired-async-create-files)
316 (ad-deactivate 'dired-create-files))))
317
318
319 (provide 'dired-async)
320
321 ;;; dired-async.el ends here