Function: dired-async-create-files

dired-async-create-files is a natively compiled function defined in dired-async.el.

Signature

(dired-async-create-files FILE-CREATOR OPERATION FN-LIST NAME-CONSTRUCTOR &optional MARKER-CHAR)

Documentation

Same as dired-create-files but asynchronous.

See dired-create-files for the behavior of arguments.

Source Code

;; Defined in /nix/store/jhxqyr2zk061dl3wd3vm38lw3zin47b9-emacs-packages-deps/share/emacs/site-lisp/elpa/async-20260729.1431/dired-async.el
(defun dired-async-create-files (file-creator operation fn-list name-constructor
                                              &optional _marker-char)
  "Same as `dired-create-files' but asynchronous.

See `dired-create-files' for the behavior of arguments."
  (setq overwrite-query nil)
  (let ((total (length fn-list))
        failures async-fn-list skipped callback
        async-quiet-switch create-dir)
    (let (to)
      (dolist (from fn-list)
        (setq to (funcall name-constructor from))
        (if (and (equal to from)
                 (null (eq file-creator 'backup-file)))
            (progn
              (setq to nil)
              (dired-log "Cannot %s to same file: %s\n"
                         (downcase operation) from)))
        (if (not to)
            (setq skipped (cons (dired-make-relative from) skipped))
          (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 "\
Type SPC or `y' to overwrite file `%s',
DEL or `n' to skip to next,
ESC or `q' to not overwrite any of the remaining files,
`!' to overwrite all remaining files with no more questions." ,to)))
                         (dired-query 'overwrite-query "Overwrite `%s'?" to)))))
            ;; Handle the `dired-copy-file' file-creator specially
            ;; When copying a directory to another directory or
            ;; possibly to itself or one of its subdirectories.
            ;; e.g "~/foo/" => "~/test/"
            ;; or "~/foo/" =>"~/foo/"
            ;; or "~/foo/ => ~/foo/bar/")
            ;; In this case the 'name-constructor' have set the destination
            ;; TO to "~/test/foo" because the old emacs23 behavior
            ;; of `copy-directory' was to not create the subdirectory
            ;; and instead copy the contents.
            ;; With the new behavior of `copy-directory'
            ;; (similar to the `cp' shell command) we don't
            ;; need such a construction of the target directory,
            ;; so modify the destination TO to "~/test/" instead of "~/test/foo/".
            (let ((destname (file-name-directory to)))
              (when (and (funcall dired-async-file-directory-p-fn from)
                         (file-directory-p to)
                         (eq file-creator 'dired-copy-file))
                (setq to destname))
              ;; If DESTNAME is a subdirectory of FROM, not a symlink,
              ;; and the method in use is copying, signal an error.
              (and (eq t (car (file-attributes destname)))
                   (eq file-creator 'dired-copy-file)
                   (file-in-directory-p destname from)
                   (error "Cannot copy `%s' into its subdirectory `%s'"
                          from to)))
            ;; Skip file if it is too large.
            (if (and (member operation '("Copy" "Rename"))
                     dired-async-large-file-warning-threshold
                     (eq (dired-async--abort-if-file-too-large
                          (file-attribute-size
                           (file-attributes (file-truename from)))
                          (downcase operation) from)
                         'abort))
                (push from skipped)
              (if overwrite
                  (or (and dired-overwrite-confirmed
                           (push (cons from to) async-fn-list))
                      (progn
                        (push (dired-make-relative from) failures)
                        (dired-log "%s `%s' to `%s' failed\n"
                                   operation from to)))
                (push (cons from to) async-fn-list))))))
      ;; Fix tramp issue #80 with emacs-26, use "-q" only when needed.
      (setq async-quiet-switch
            (if (and (boundp 'tramp-cache-read-persistent-data)
                     async-fn-list
                     (cl-loop for (_from . to) in async-fn-list
                              thereis (file-remote-p to)))
                "-q" "-Q"))
      ;; When failures have been printed to dired log add the date at bob.
      (when (or failures skipped) (dired-log t))
      ;; When async-fn-list is empty that's mean only one file
      ;; had to be copied and user finally answer NO.
      ;; In this case async process will never start and callback
      ;; will have no chance to run, so notify failures here.
      (unless async-fn-list
        (cond (failures
               (funcall dired-async-message-function
                        "%s failed for %d of %d file%s -- See *Dired log* buffer"
                        'dired-async-failures
                        operation (length failures)
                        total (dired-plural-s total)))
              (skipped
               (funcall dired-async-message-function
                        "%s: %d of %d file%s skipped -- See *Dired log* buffer"
                        'dired-async-failures
                        operation (length skipped) total
                        (dired-plural-s total)))))
      ;; Setup callback.
      (setq callback
            (lambda (&optional _ignore)
              (dired-async-after-file-create
               total (list operation (length async-fn-list)) failures skipped)
              (when (string= (downcase operation) "rename")
                (cl-loop for (file . to) in async-fn-list
                         for bf = (get-file-buffer file)
                         for destp = (file-exists-p to)
                         do (and bf destp
                                 (with-current-buffer bf
                                   (set-visited-file-name to t t)))))))
      (let ((dirp (file-directory-p to))
            (dest (file-name-directory to)))
        (when (boundp 'dired-create-destination-dirs)
          (setq create-dir
                (cl-case dired-create-destination-dirs
                  (always 'always)
                  (ask (and (null dirp)
                            (null (file-directory-p dest))
                            (y-or-n-p (format "Create directory `%s'? " dest)))
                       'always))))))
    ;; Start async process.
    (when async-fn-list
      (process-put
       (async-start `(lambda ()
                       (require 'cl-lib) (require 'dired-aux) (require 'dired-x)
                       ,(async-inject-variables dired-async-env-variables-regexp)
                       (advice-add #'files--ask-user-about-large-file
                                   :override (lambda (&rest args) nil))
                       (let ((dired-recursive-copies (quote always))
                             (dired-copy-preserve-time
                              ,dired-copy-preserve-time)
                             (dired-create-destination-dirs ',create-dir)
                             (dired-vc-rename-file ,dired-vc-rename-file)
                             auth-source-save-behavior)
                         (setq overwrite-backup-query nil)
                         ;; Inline `backup-file' as long as it is not
                         ;; available in emacs.
                         (defalias 'backup-file
                           ;; Same feature as "cp -f --backup=numbered from to"
                           ;; Symlinks are copied as file from source unlike
                           ;; `dired-copy-file' which is same as cp -d.
                           ;; Directories are omitted.
                           (lambda (from to ok)
                             (cond ((file-directory-p from) (ignore))
                                   (t (let ((count 0))
                                        (while (let ((attrs (file-attributes to)))
                                                 (and attrs (null (nth 0 attrs))))
                                          (cl-incf count)
                                          (setq to (concat (file-name-sans-versions to)
                                                           (format ".~%s~" count)))))
                                      (condition-case err
                                          (copy-file from to ok dired-copy-preserve-time)
                                        (file-date-error
                                         (dired-log "Can't set date on %s:\n%s\n" from err)))))))
                         ;; Now run the FILE-CREATOR function on files.
                         (cl-loop with fn = (quote ,file-creator)
                                  for (from . dest) in (quote ,async-fn-list)
                                  do (condition-case err
                                         (funcall fn from dest t)
                                       (file-error
                                        (dired-log "%s: %s\n" (car err) (cdr err))
                                        nil)))
                         (when (get-buffer dired-log-buffer)
                           (dired-log t)
                           (with-current-buffer dired-log-buffer
                             (write-region (point-min) (point-max)
                                           ,dired-async-log-file))))
                       ,(dired-async-maybe-kill-ftp))
                    callback)
       'dired-async-process t)
      ;; Run mode-line notifications while process running.
      (dired-async--modeline-mode 1)
      (message "%s proceeding asynchronously..." operation))))