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