]> code.delx.au - gnu-emacs-elpa/commitdiff
Allow reproducing "cp --backup=numbered from to".
authorThierry Volpiatto <thierry.volpiatto@gmail.com>
Sat, 23 Apr 2016 09:10:51 +0000 (11:10 +0200)
committerThierry Volpiatto <thierry.volpiatto@gmail.com>
Sat, 23 Apr 2016 09:10:51 +0000 (11:10 +0200)
* dired-async.el (dired-async-create-files):
When using a file-creator named 'backup-file, files are
copied to destination without overwriting existing files,
making a versionned copy of original files.

dired-async.el

index ecab9cb5b72a5d07fb1bf48a9a04ceba06a42b28..461e1ddb07eb751c22a8be36bed06b5229a24631 100644 (file)
@@ -166,7 +166,8 @@ See `dired-create-files' for the behavior of arguments."
                          (downcase operation) from)))
         (if (not to)
             (setq skipped (cons (dired-make-relative from) skipped))
-            (let* ((overwrite (file-exists-p to))
+            (let* ((overwrite (and (null (eq file-creator 'backup-file))
+                                   (file-exists-p to)))
                    (dired-overwrite-confirmed ; for dired-handle-overwrite
                     (and overwrite
                          (let ((help-form '(format "\
@@ -255,8 +256,21 @@ ESC or `q' to not overwrite any of the remaining files,
                       ,(async-inject-variables dired-async-env-variables-regexp)
                       (condition-case err
                           (let ((dired-recursive-copies (quote always)))
-                            (cl-loop for (f . d) in (quote ,async-fn-list)
-                                     do (funcall (quote ,file-creator) f d t)))
+                            (defalias 'backup-file
+                                ;; Same feature as "cp --backup=numbered from to"
+                                (lambda (from to ok)
+                                  (when (null (nth 0 (file-attributes from)))
+                                    (let ((count 0))
+                                      (while (let ((attrs (file-attributes to)))
+                                               (and attrs
+                                                    (null (nth 0 (file-attributes to)))))
+                                        (cl-incf count)
+                                        (setq to (concat (file-name-sans-versions to)
+                                                         (format ".~%s~" count)))))
+                                    (copy-file from to ok dired-copy-preserve-time))))
+                            (cl-loop with fn = (quote ,file-creator)
+                                     for (from . dest) in (quote ,async-fn-list)
+                                     do (funcall fn from dest t)))
                         (file-error
                          (with-temp-file ,dired-async-log-file
                            (insert (format "%S" err)))))