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