I got a problem for an advice-add
inside Dired's copy function (dired-do-copy
).
My object is it to filter all marked files/directories in Dired, which should be copied to an other location within Dired. The filter is simply finding all files including a recursive search in marked directories which are greater in files size than a given treasure/limit value. Only the files, which are smaller in file size than this value should be copied by Dired in the normal way. The other greater one's I would like to copy by a separate function (not mentioned here) in an asynchronous process.
My problem is that the ordinary list, where all files are mentioned with its absolute path can not be given to the dired-do-copy
function. Every time I copy within Dired with activated following advice-add
I get this error:
apply: Wrong number of arguments: (0 . 1), 2
Please see my advice-add
function:
(defun filter-files-*-marked-greater-than-*LIMIT-VALUE-COPYING-TRASH* (old-function &rest arguments)
;; for testing and debugging
(message "Here are all given arguments %S" arguments )
(let* (
(list-of-marked-files-first (dired-get-marked-files))
(list-of-marked-files-second list-of-marked-files-first)
(list-of-all-files-within-all-marked-directories nil)
list-of-files-smaller-than-*LIMIT-VALUE-COPYING-TRASH*
list-of-all-marked-files-greater-than*LIMIT-VALUE-COPYING-TRASH*
number-of-marked-files
singleFile
singleFileOrDirectory
fileSize
fileSizeKB
fileSizeMB
numberOfFilesToCopy
numberOfFilesToCopy-greater-than-*LIMIT-VALUE-COPYING-TRASH*
newArgsList
)
;; filter all directories from marked list
(while list-of-marked-files-first
(setq singleFileOrDirectory (pop list-of-marked-files-first))
;; if a directory then recursivly collect all files inside this directory
;; and subdirectories and add them to
;; list-of-all-files-within-all-marked-directories
;; directory check follows --> first condition is also true if it is
;; a symlink (see file-attributes)
(if (and (nth 0 (file-attributes singleFileOrDirectory)) (booleanp (nth 0 (file-attributes singleFileOrDirectory))))
(progn
(setq list-of-all-files-within-all-marked-directories (append list-of-all-files-within-all-marked-directories (directory-files-recursively singleFileOrDirectory ".*" t)))
;; delete the directory from the list-of-marked-files-second
(delete singleFileOrDirectory list-of-marked-files-second))))
;; put these to lists together, where both have only files listed inside
(setq list-of-marked-files-second (append list-of-marked-files-second list-of-all-files-within-all-marked-directories))
;; determine elements in list-of-marked-files-second, where only files and
;; no directories reside inside
(setq number-of-marked-files (length list-of-marked-files-second))
;; true as long any marked files exist AND not more than one file was found,
;; which is larger than *LIMIT-VALUE-COPYING-TRASH*
(while list-of-marked-files-second
(setq singleFile (pop list-of-marked-files-second))
;; check for directory is obsolete, because there shouldn't be any
;; directories inside
(if (not (nth 0 (file-attributes singleFile)))
(progn
(setq fileSize (nth 7 (file-attributes singleFile)))
(setq fileSizeKB (/ fileSize 1024))
(setq fileSizeMB (/ fileSizeKB 1024))
(if (> fileSizeMB *LIMIT-VALUE-COPYING-TRASH*)
(progn
(add-to-list 'list-of-all-marked-files-greater-than*LIMIT-VALUE-COPYING-TRASH* singleFile)
;; remove file greater than *LIMIT-VALUE-COPYING-TRASH* from
;; given arguments in ordwer to copy it with rsync
;; (delete singleFile arguments)
)
(add-to-list 'list-of-files-smaller-than-*LIMIT-VALUE-COPYING-TRASH* singleFile)
)
)
)
)
;; for testing and debugging
(setq numberOfFilesToCopy-greater-than-*LIMIT-VALUE-COPYING-TRASH* (length list-of-all-marked-files-greater-than*LIMIT-VALUE-COPYING-TRASH*))
;; for testing and debugging
(setq numberOfFilesToCopy (length list-of-files-smaller-than-*LIMIT-VALUE-COPYING-TRASH*))
;; for testing and debugging
(setq numberOfArgsElements (length arguments))
;; for testing and debugging
(if (sequencep arguments)
(message (propertize (concat "arguments is sequencep" ".") 'face '(:foreground "cyan"))))
;; for testing and debugging
(if (sequencep list-of-files-smaller-than-*LIMIT-VALUE-COPYING-TRASH*)
(message (propertize (concat "list-of-files-smaller-than-*LIMIT-VALUE-COPYING-TRASH* is sequencep" ".") 'face '(:foreground "cyan"))))
;; (while list-of-files-smaller-than-*LIMIT-VALUE-COPYING-TRASH*
;; (setq runner (pop list-of-files-smaller-than-*LIMIT-VALUE-COPYING-TRASH*))
;; (message (propertize runner 'face '(:foreground "cyan"))))
;; for testing and debugging
(message "Here are all arguments end the end of advice-add function %S" arguments)
;; for testing and debugging
(print arguments)
;; for testing and debugging
(add-to-list 'newArgsList list-of-files-smaller-than-*LIMIT-VALUE-COPYING-TRASH*)
;; testing: when directory is given for copying
;; for testing and debugging
(message (propertize (concat (number-to-string numberOfFilesToCopy) " Number of arguments given to dired-do-copy ") 'face '(:foreground "cyan")))
(message (propertize (concat (number-to-string numberOfFilesToCopy-greater-than-*LIMIT-VALUE-COPYING-TRASH*) " Number of found files greater than 10MB.") 'face '(:foreground "cyan")))
(message (propertize (concat (number-to-string numberOfArgsElements) " Number of elements in Args.") 'face '(:foreground "cyan")))
;; (setq arguments list-of-files-smaller-than-*LIMIT-VALUE-COPYING-TRASH*)
;; !!!!!!!replacing
;; (apply old-function newArgsList)
;; (apply old-function )
;; with
;; (apply dired-do-copy newArgsList)
;; (apply dired-do-copy 'list-of-files-smaller-than-*LIMIT-VALUE-COPYING-TRASH*)
;; !!!!!!!with following line do not work
(apply old-function list-of-files-smaller-than-*LIMIT-VALUE-COPYING-TRASH*)
;; !!!!!!!
)
)
And here my final advice-add
declaration.
(advice-add 'dired-do-copy :around #'filter-files-*-marked-greater-than-*LIMIT-VALUE-COPYING-TRASH*)