]> code.delx.au - gnu-emacs-elpa/blob - dired-async.el
Improve how error buffer is displayed.
[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
48 (defgroup dired-async nil
49 "Copy rename files asynchronously from dired."
50 :group 'dired)
51
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."
55 :type 'regexp
56 :group 'dired-async)
57
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'."
61 :group 'dired-async
62 :type 'function)
63
64 (defcustom dired-async-log-file "/tmp/dired-async.log"
65 "File use to communicate errors from Child Emacs to host Emacs."
66 :group 'dired-async
67 :type 'string)
68
69 (defface dired-async-message
70 '((t (:foreground "yellow")))
71 "Face used for mode-line message."
72 :group 'dired-async)
73
74 (defface dired-async-failures
75 '((t (:foreground "red")))
76 "Face used for mode-line message."
77 :group 'dired-async)
78
79 (defface dired-async-mode-message
80 '((t (:foreground "Gold")))
81 "Face used for `dired-async--modeline-mode' lighter."
82 :group 'dired-async)
83
84 (define-minor-mode dired-async--modeline-mode
85 "Notify mode-line that an async process run."
86 :group 'dired-async
87 :global t
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))))
93
94 (defun dired-async-mode-line-message (text face &rest args)
95 "Notify end of operation in `mode-line'."
96 (message nil)
97 (let ((mode-line-format (concat
98 " " (propertize
99 (if args
100 (apply #'format text args)
101 text)
102 'face face))))
103 (force-mode-line-update)
104 (sit-for 3)
105 (force-mode-line-update)))
106
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))
111 collect p))
112
113 (defun dired-async-kill-process ()
114 (interactive)
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))))
120
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))
127 (when operation
128 (if (file-exists-p dired-async-log-file)
129 (progn
130 (pop-to-buffer (get-buffer-create dired-log-buffer))
131 (goto-char (point-max))
132 (setq inhibit-read-only t)
133 (insert "Error: ")
134 (insert-file-contents dired-async-log-file)
135 (special-mode)
136 (shrink-window-if-larger-than-buffer)
137 (delete-file dired-async-log-file))
138 (run-with-timer
139 0.1 nil
140 (lambda ()
141 ;; First send error messages.
142 (cond (failures
143 (funcall dired-async-message-function
144 "%s failed for %d of %d file%s"
145 'dired-async-failures
146 (car operation) (length failures)
147 total (dired-plural-s total)))
148 (skipped
149 (funcall dired-async-message-function
150 "%s: %d of %d file%s skipped"
151 'dired-async-failures
152 (car operation) (length skipped) total
153 (dired-plural-s total))))
154 ;; Finally send the success message.
155 (funcall dired-async-message-function
156 "Asynchronous %s of %s file(s) on %s file(s) done"
157 'dired-async-message
158 (car operation) (cadr operation) total))))))
159
160 (defun dired-async-maybe-kill-ftp ()
161 "Return a form to kill ftp process in child emacs."
162 (quote
163 (progn
164 (require 'cl-lib)
165 (let ((buf (cl-loop for b in (buffer-list)
166 thereis (and (string-match
167 "\\`\\*ftp.*"
168 (buffer-name b)) b))))
169 (when buf (kill-buffer buf))))))
170
171 (defvar overwrite-query)
172 (defun dired-async-create-files (file-creator operation fn-list name-constructor
173 &optional _marker-char)
174 "Same as `dired-create-files' but asynchronous.
175
176 See `dired-create-files' for the behavior of arguments."
177 (setq overwrite-query nil)
178 (let ((total (length fn-list))
179 failures async-fn-list skipped callback)
180 (let (to)
181 (dolist (from fn-list)
182 (setq to (funcall name-constructor from))
183 (if (equal to from)
184 (progn
185 (setq to nil)
186 (dired-log "Cannot %s to same file: %s\n"
187 (downcase operation) from)))
188 (if (not to)
189 (setq skipped (cons (dired-make-relative from) skipped))
190 (let* ((overwrite (and (null (eq file-creator 'backup-file))
191 (file-exists-p to)))
192 (dired-overwrite-confirmed ; for dired-handle-overwrite
193 (and overwrite
194 (let ((help-form `(format "\
195 Type SPC or `y' to overwrite file `%s',
196 DEL or `n' to skip to next,
197 ESC or `q' to not overwrite any of the remaining files,
198 `!' to overwrite all remaining files with no more questions." ,to)))
199 (dired-query 'overwrite-query "Overwrite `%s'?" to)))))
200 ;; Handle the `dired-copy-file' file-creator specially
201 ;; When copying a directory to another directory or
202 ;; possibly to itself or one of its subdirectories.
203 ;; e.g "~/foo/" => "~/test/"
204 ;; or "~/foo/" =>"~/foo/"
205 ;; or "~/foo/ => ~/foo/bar/")
206 ;; In this case the 'name-constructor' have set the destination
207 ;; TO to "~/test/foo" because the old emacs23 behavior
208 ;; of `copy-directory' was to not create the subdirectory
209 ;; and instead copy the contents.
210 ;; With the new behavior of `copy-directory'
211 ;; (similar to the `cp' shell command) we don't
212 ;; need such a construction of the target directory,
213 ;; so modify the destination TO to "~/test/" instead of "~/test/foo/".
214 (let ((destname (file-name-directory to)))
215 (when (and (file-directory-p from)
216 (file-directory-p to)
217 (eq file-creator 'dired-copy-file))
218 (setq to destname))
219 ;; If DESTNAME is a subdirectory of FROM, not a symlink,
220 ;; and the method in use is copying, signal an error.
221 (and (eq t (car (file-attributes destname)))
222 (eq file-creator 'dired-copy-file)
223 (file-in-directory-p destname from)
224 (error "Cannot copy `%s' into its subdirectory `%s'"
225 from to)))
226 (if overwrite
227 (or (and dired-overwrite-confirmed
228 (push (cons from to) async-fn-list))
229 (progn
230 (push (dired-make-relative from) failures)
231 (dired-log "%s `%s' to `%s' failed\n"
232 operation from to)))
233 (push (cons from to) async-fn-list)))))
234 ;; When failures have been printed to dired log add the date at bob.
235 (when (or failures skipped) (dired-log t))
236 ;; When async-fn-list is empty that's mean only one file
237 ;; had to be copied and user finally answer NO.
238 ;; In this case async process will never start and callback
239 ;; will have no chance to run, so notify failures here.
240 (unless async-fn-list
241 (cond (failures
242 (funcall dired-async-message-function
243 "%s failed for %d of %d file%s"
244 'dired-async-failures
245 operation (length failures)
246 total (dired-plural-s total)))
247 (skipped
248 (funcall dired-async-message-function
249 "%s: %d of %d file%s skipped"
250 'dired-async-failures
251 operation (length skipped) total
252 (dired-plural-s total)))))
253 ;; Setup callback.
254 (setq callback
255 (lambda (&optional _ignore)
256 (dired-async-after-file-create
257 total (list operation (length async-fn-list)) failures skipped)
258 (when (string= (downcase operation) "rename")
259 (cl-loop for (file . to) in async-fn-list
260 for bf = (get-file-buffer file)
261 for destp = (file-exists-p to)
262 do (and bf destp
263 (with-current-buffer bf
264 (set-visited-file-name to nil t))))))))
265 ;; Start async process.
266 (when async-fn-list
267 (async-start `(lambda ()
268 (require 'cl-lib) (require 'dired-aux) (require 'dired-x)
269 ,(async-inject-variables dired-async-env-variables-regexp)
270 (condition-case err
271 (let ((dired-recursive-copies (quote always))
272 (dired-copy-preserve-time
273 ,dired-copy-preserve-time))
274 (setq overwrite-backup-query nil)
275 ;; Inline `backup-file' as long as it is not
276 ;; available in emacs.
277 (defalias 'backup-file
278 ;; Same feature as "cp --backup=numbered from to"
279 ;; Symlinks are copied as file from source unlike
280 ;; `dired-copy-file' which is same as cp -d.
281 ;; Directories are omitted.
282 (lambda (from to ok)
283 (cond ((file-directory-p from) (ignore))
284 (t (let ((count 0))
285 (while (let ((attrs (file-attributes to)))
286 (and attrs (null (nth 0 attrs))))
287 (cl-incf count)
288 (setq to (concat (file-name-sans-versions to)
289 (format ".~%s~" count)))))
290 (condition-case err
291 (copy-file from to ok dired-copy-preserve-time)
292 (file-date-error
293 (dired-log "Can't set date on %s:\n%s\n" from err)))))))
294 ;; Now run the FILE-CREATOR function on files.
295 (cl-loop with fn = (quote ,file-creator)
296 for (from . dest) in (quote ,async-fn-list)
297 do (funcall fn from dest t)))
298 (file-error
299 (dired-log "%s: %s\n" (car err) (cdr err))
300 (dired-log t)
301 (with-current-buffer dired-log-buffer
302 (write-region (point-min) (point-max)
303 ,dired-async-log-file))))
304 ,(dired-async-maybe-kill-ftp))
305 callback)
306 ;; Run mode-line notifications while process running.
307 (dired-async--modeline-mode 1)
308 (message "%s proceeding asynchronously..." operation))))
309
310 (defadvice dired-create-files (around dired-async)
311 (dired-async-create-files file-creator operation fn-list
312 name-constructor marker-char))
313
314 ;;;###autoload
315 (define-minor-mode dired-async-mode
316 "Do dired actions asynchronously."
317 :group 'dired-async
318 :global t
319 (if dired-async-mode
320 (if (fboundp 'advice-add)
321 (advice-add 'dired-create-files :override #'dired-async-create-files)
322 (ad-activate 'dired-create-files))
323 (if (fboundp 'advice-remove)
324 (advice-remove 'dired-create-files #'dired-async-create-files)
325 (ad-deactivate 'dired-create-files))))
326
327
328 (provide 'dired-async)
329
330 ;;; dired-async.el ends here