problem in `delete-directory` with enabled `delete-by-removing-to-trash` - emacs

There is a strange behavior of delete-directory function with enabled flag delete-by-removing-to-trash. It deletes files one by one instead of applying move-file-to-trash to the directory. As a result emacs deletes big directories slowly and there
are many files in the trash after deleting, so it is impossible to restore the directory.
Example:
Directory structure:
ddd/
ccc/
1.txt
There are three files in the trash after deleting ddd:
trash/
ddd/
ccc/
1.txt
instead of one:
trash/
ddd/
It is very slow, because emacs traverse directory recursively.
I can't restore deleted directory.
What i need is exactly the same behavior as of move-file-to-trash. But it should be transparent (i.e. 'D x' in dired mode). How to solve the problem? As a temporary solution i see the making advice function for `delete-directory'.

As a temporary workaround i use:
(setq delete-by-moving-to-trash t)
(defadvice dired-delete-file (around
activate-move-to-trash
activate
compile)
(if delete-by-moving-to-trash
(move-file-to-trash (ad-get-arg 0))
ad-do-it))

Here's my whole system. If you look at the bottom you'll see a nearly identical defadvice to Andreo's.
;;; trash-settings.el - Intelligent integration with system trash
;; Use the system trash, except for temp files and stuff
(require 'cl)
(defcustom system-trash-exclude-names
nil
"List of file names to exclude from system trash.
The names in this variable are matched only against the basename
of the file to be deleted."
:type '(repeat string)
:group 'trash)
(defcustom system-trash-exclude-paths
nil
"List of absolute paths to exclude from system trash.
If a path to a directory is excluded, then all the contents of that directory are also excluded."
:type '(repeat string)
:group 'trash)
(defcustom system-trash-exclude-matches
nil
"List of regexps or functions matching file names to exclude from system trash.
The matches are only applied against the file name, not the path."
:type '(repeat (choice regexp function))
:group 'trash)
(defcustom system-trash-exclude-path-matches
nil
"List of regexps or functions matching paths to exclude from system trash.
The matches are applied against the full path."
:type '(repeat (choice regexp function))
:group 'trash)
(defun call-process-discard-output (program &rest args)
"Execute program with args without saving any output.
In particular, no temp files are created."
(eval (append `(call-process ,program nil nil nil) args)))
(defun string-begins-with-p (string beginning)
"Return t if and only if string begins with beginning"
(string-match-p (concat "^" (regexp-quote beginning)) string))
(defun file-excluded-from-system-trash-p (path)
"Returns non-nil if file name is excluded from trash."
(let ((basename (file-name-nondirectory path)))
(or
(some (apply-partially 'string= basename)
system-trash-exclude-names)
(some (apply-partially 'string-begins-with-p path)
system-trash-exclude-paths)
(some (lambda (match)
(funcall
(cond ((stringp match) 'string-match-p)
((functionp protected-match) 'funcall)
(t 'ignore))
match
basename))
system-trash-exclude-matches)
(some (lambda (match)
(funcall
(cond ((stringp match) 'string-match-p)
((functionp protected-match) 'funcall)
(t 'ignore))
match
path))
system-trash-exclude-path-matches))))
(defun trash-or-rm (filename)
"Attempt to move a file to the trash. If this fails, simply delete it.
This guarantees that any deletable file will either be trashed or deleted.
If the file is excluded from the trash, it is simply deleted."
(unless (file-excluded-from-system-trash-p filename)
(ignore-errors
(call-process-discard-output "gvfs-trash" filename)))
(when (file-exists-p filename)
(call-process-discard-output "rm" "-rf" filename)))
(defalias 'system-move-file-to-trash 'trash-or-rm)
(defadvice delete-directory (around no-recursive-trash activate)
"When trashing a directory, there's no need to trash its contents first."
(if delete-by-moving-to-trash
(move-file-to-trash directory)
ad-do-it))
(defadvice dired-delete-file (around no-recursive-trash activate)
"When trashing a directory, there's no need to trash its contents first.
There's also no need to ask, because it's undoable."
(if delete-by-moving-to-trash
(move-file-to-trash file)
ad-do-it))
Edit: I should mention that my solution above requires the gvfs-trash command-line program, which is associated with GNOME. You can replace this with trash from the trash-cli package if you don't use GNOME.

My "answer" would be: M-x report-emacs-bug. Bug reports help everyone. Emacs Dev will determine whether there is actually a bug, and everyone involved will learn from the report.

Related

Emacs dired - using predefined variable

In emacs dired I want to do something I do quite often in Microsoft PowerShell.
In PowerShell, I have a set of folders that I always use, and I assign their full path to global variables in my profile script (similar to init.el in the emacs world) e.g:
$standardTemp = "C:\Long\Path\To\Folder"
If I am in another folder and I want to copy something to the above folder, I do:
copy myFile $standardTemp
Even more useful as a feature, is if I put a backslash after $standardTemp, it will expand it out, so I can go into subfolders if I need to. This is a very awesome feature and saves me a lot of time.
With the dired copy command can I do something similar, if I define variables with e.g. setq in my init.el file?
How about something like this?
;; Use ido
(require 'ido)
(ido-mode t)
;; Make a hash table to hold the paths
(setq my-target-dirs (make-hash-table :test 'equal))
;; Put some paths in the hash (sorry for Unix pathnames)
(puthash "home" "/home/jhrr/" my-target-dirs)
(puthash "target" "/home/jhrr/target/" my-target-dirs)
;; A function to return all the keys from a hash.
(defun get-keys-from-hash (hash)
(let ((keys ()))
(maphash (lambda (k v) (push k keys)) hash)
keys))
;; And the function to prompt for a directory by keyword that is looked
;; up in the hash-table and used to build the target path from the
;; value of the lookup.
(defun my-dired-expand-copy ()
(interactive)
(let* ((my-hash my-target-dirs)
(files (dired-get-marked-files))
(keys (get-keys-from-hash my-hash)))
(mapc (lambda (file)
(copy-file file
(concat
(gethash
(ido-completing-read
(concat "copy " file " to: ") keys) my-hash)
(file-name-nondirectory file))))
files)))
It's not exhaustively tested as I just whipped it up in 10 minutes, but it does the job and it can handle multiple files.
You will need to open the dired buffer in the directory the files are in and mark each file you want to copy with 'm', then invoke my-dired-expand-copy and it will prompt you for a target destination (in the form of a keyword from the hash-table we set-up) for the file before, finally, copying the file over to the directory that maps to the target keyword.
It doesn't quite cover the sub-directories use-case you mention, but it shouldn't be too hard to get there given a bit more hacking.
UPDATE:
This should now prompt you to be able to descend into subdirectories from an original target; maybe not the most mind-shatteringly wonderful UX on the whole, but, it works:
(defun my-dired-expand-copy-2 ()
(interactive)
(let* ((my-hash my-target-dirs)
(files (dired-get-marked-files))
(keys (get-keys-from-hash my-hash)))
(mapc (lambda (file)
(let ((target (gethash
(ido-completing-read
(concat "copy " file " to: ") keys) my-hash)))
(if (y-or-n-p "Descend?")
;; Descend into subdirectories relative to target dir
(let ((new-target (ido-read-directory-name "new dir: " target)))
(copy-file file (concat new-target
(file-name-nondirectory file)))
(message (concat "File: " file " was copied to " new-target)))
;; Else copy to root of originally selected directory
(copy-file file (concat target (file-name-nondirectory file)))
(message (concat "File: " file " was copied to " target)))))
files)))
When I need to use dired to get to frequently-used directories, I use the standard emacs bookmarking capabilities.
I manually navigate to the directory, and press
C-x r m
to execute the command
bookmark-set
You'll be prompted for a name for the bookmark. Enter a shortcut that you can remember.
At this point, anytime you want to open that directory within dired, simply execute the command
bookmark-jump
with the keys
C-x r b
Enter your shortcut to the directory, and dired will open to that location.
To copy from one directory to another, ensure you have the following set in your init file
(setq dired-dwim-target t)
Then you can open a dired window for the source directory, and another window for the target directory within in the same frame, and dired will automatically assign the source and target location to the appropriate directories.
Note this is just a subset of what emacs bookmarks can do for you!
Chris
In addition to using bookmarks, consider using directory-name aliases (e.g. symlinks) or directory-abbrev-alist. See the Emacs manual, node File Aliases.
If you want to insert the value of an environment variable into the minibuffer, you can do it this way:
C-u M-: (getenv "THE-VARIABLE")
where THE-VARIABLE is the variable name. Using C-u inserts the value of evaluating the sexp into the current buffer (in this case the minibuffer).
So you would, say, use C to copy the marked files in Dired, and then use C-u with a getenv sexp for the existing variable you have, to insert its value into the minibuffer when prompted for the directory to copy to.
(Depending on your Emacs setup, you might need to set enable-recursive-minibuffers to non-nil, to be able to use M-: from the minibuffer.)

Dired: Duplicate Files (the operation, not how to get rid of 'em)

Sometimes I want to create a duplicate of a number of files (say, config files), which initially should have the same content as the initial files. Therefore I'd like to be able mark some files in dired and "duplicate" them, this duplication procedure could work similar like the duplication procedure utilised by most file managers, when pasting to the original directory: The file names of the duplicated get "(Copy)" appended (just before the file extension).
I can't seem to find a built-in dired function that does this, maybe someone can help/has already created a function like this?
Help is much appreciated!
There is one function that does what you want: dired-do-copy-regexp
Example of use:
mark the files
M-x dired-do-copy-regexp
\(.*\)\.\(.*\)
\1 (copy).\2
For a file named foo.txt you will be creating another named foo (copy).txt
Note that my first regexp has two groups, and the second regexp references them. You can do much more complicated things, if needed.
Maybe you will want to rename the functions (I didn't come up with better names), maybe some more elaborate formatting, if you wish...
(defcustom dired-keep-marker-version ?V
"Controls marking of versioned files.
If t, versioned files are marked if and as the corresponding original files were.
If a character, copied files are unconditionally marked with that character."
:type '(choice (const :tag "Keep" t)
(character :tag "Mark"))
:group 'dired-mark)
(defun dired-version-file (from to ok-flag)
(dired-handle-overwrite to)
(dired-copy-file-recursive from to ok-flag dired-copy-preserve-time t
dired-recursive-copies))
(defun dired-do-version (&optional arg)
"Search for numeric pattern in file name and create a version of that file
with that number incremented by one, or, in case such file already exists,
will search for a file with the similar name, incrementing the counter each
time by one.
Additionally, if called with prefix argument, will prompt for number format.
The formatting is the same as is used with `format' function."
(interactive "P")
(let ((fn-list (dired-get-marked-files nil nil)))
(dired-create-files
(function dired-version-file) "Version" fn-list
(function
(lambda (from)
(let (new-name (i 0) (fmt (if arg (read-string "Version format: " "%d") "%d")))
(while (or (null new-name) (file-exists-p new-name))
(setq new-name
(if (string-match "^\\([^0-9]*\\)\\([0-9]+\\)\\(.*\\)$" from)
(concat (match-string 1 from)
(format fmt
(+ (string-to-number (match-string 2 from)) (1+ i)))
(match-string 3 from))
(concat from (format (concat "." fmt) i)))
i (1+ i))) new-name)))
dired-keep-marker-version)))
(define-key dired-mode-map (kbd "c") 'dired-do-version)
Also, I've originally used v to bind this function because I don't use dired-view, but you would need to bind that inside direds hook. c just was the first undefined key, so I used it.
In the Dired mode, put cursor on the file you want to duplicate or mark that file, then press "C". You will be prompted for new name.
You can use this feature to copy files between Dired buffers as well. To make it possible put into your init file:
(setq dired-dwim-target t)

Recursively adding .org files in a top-level directory for org-agenda-files takes a long time

I'm trying to find a way to quickly recurse through every subdirectory searching for org files. I've found several solutions (Elisp Cookbook, and several solutions on github), but they don't handle my real world usage (hundreds of directories (and subdirectories) and hundreds of org files). They seem to run forever on my system (Windows 7, with max-lisp-eval-depth = 10000). My work around is to add each directory manually to my org-agenda-list, but it's annoying and I know I've probably forgotten some. Any suggestions would be appreciated.
Haven't seen other people post this, so I will do.
Have you tried load "find-list" library and use its "find-lisp-find-files" function?
I added these lines in my org config and it works, but it may not fit your performance requirement:
(load-library "find-lisp")
(setq org-agenda-files
(find-lisp-find-files "FOLDERNAME" "\.org$"))
source: http://emacs-orgmode.gnu.narkive.com/n5bQRs5t/o-multiple-recursive-directories-with-org-agenda-files
The following code works well in emacs 24.3+:
;; Collect all .org from my Org directory and subdirs
(setq org-agenda-file-regexp "\\`[^.].*\\.org\\'") ; default value
(defun load-org-agenda-files-recursively (dir) "Find all directories in DIR."
(unless (file-directory-p dir) (error "Not a directory `%s'" dir))
(unless (equal (directory-files dir nil org-agenda-file-regexp t) nil)
(add-to-list 'org-agenda-files dir)
)
(dolist (file (directory-files dir nil nil t))
(unless (member file '("." ".."))
(let ((file (concat dir file "/")))
(when (file-directory-p file)
(load-org-agenda-files-recursively file)
)
)
)
)
)
(load-org-agenda-files-recursively "/path/to/your/org/dir/" ) ; trailing slash required
It does not require intermediate files creation and you can put it on a shortcut as well.
To be able to refile to any file found add this:
(setq org-refile-targets
'((nil :maxlevel . 3)
(org-agenda-files :maxlevel . 1)))
How about storing the list of org-agenda directories in a file that you (automatically) update every once in a while, when you know the direcory structure has changed and you have some time.
You could for example use something like this:
;; From http://www.emacswiki.org/emacs/ElispCookbook#toc58
(defun directory-dirs (dir)
"Find all directories in DIR."
(unless (file-directory-p dir)
(error "Not a directory `%s'" dir))
(let ((dir (directory-file-name dir))
(dirs '())
(files (directory-files dir nil nil t)))
(dolist (file files)
(unless (member file '("." ".."))
(let ((file (concat dir "/" file)))
(when (file-directory-p file)
(setq dirs (append (cons file
(directory-dirs file))
dirs))))))
dirs))
(setq my-org-agenda-root "~/org")
(setq my-org-agenda-files-list "~/.emacs.d/org-agenda-list.el")
(defun my-update-org-agenda-files ()
"Create or update the `my-org-agenda-files-list' file.
This file contains elisp code to set `org-agenda-files' to a
recursive list of all children under `my-org-agenda-root'. "
(interactive)
(with-temp-buffer
(insert
";; Warning: this file has been automatically generated\n"
";; by `my-update-org-agenda-files'\n")
(let ((dir-list (directory-dirs my-org-agenda-root))
(print-level nil)
(print-length nil))
(cl-prettyprint `(setq org-agenda-files (quote ,dir-list))))
(write-file my-org-agenda-files-list)))
(load my-org-agenda-files-list)
Every once in a while, run M-xmy-update-org-agenda-files to update the list.
As of Emacs 25, you can use directory-files-recursively which returns all files matching a regex from a root directory.
(defun org-get-agenda-files-recursively (dir)
"Get org agenda files from root DIR."
(directory-files-recursively dir "\.org$"))
(defun org-set-agenda-files-recursively (dir)
"Set org-agenda files from root DIR."
(setq org-agenda-files
(org-get-agenda-files-recursively dir)))
(defun org-add-agenda-files-recursively (dir)
"Add org-agenda files from root DIR."
(nconc org-agenda-files
(org-get-agenda-files-recursively dir)))
(setq org-agenda-files nil) ; zero out for testing
(org-set-agenda-files-recursively "~/Github") ; test set
(org-add-agenda-files-recursively "~/Dropbox") ; test add
You can view the contents of org-agenda-files by typing C-hv org-agenda-files.
Little late to the party, and probably not the answer you were hoping for, but the only way I found to speed up my agenda load times was by not including some directories which had thousands of org files.
(setq org-directory "~/org/"
my-agenda-dirs '("personal" "projects" "todos" "work")
org-agenda-files (mapcan (lambda (x) (directory-files-recursively
(expand-file-name x org-directory)
"\.org$"))
my-agenda-dirs))
Essentially rather than recursing over my entire org dir I only recurse through a select few of it's subdirs.
I also tried "recursive directory listing" at Emacs startup. It simply is way to loooong to be usable. So, try to stick with a limited number of "root" directories where you put your agenda files.
Even better is sticking with "~/org", and possibly a few subdirs like "work" and "personal", and that's it. You can put there your agenda files, and have links Inside them to your real project root dirs.
I know, this is not optimal, but I don't have anything better right now.

Allow dired-do-copy and dired-do-rename to create new dir on the fly

Does anyone have an emacs lisp hack that would allow the creation of a new directory on the fly during dired-do-copy or dired-do-rename? I understand that it can be created prior to running one of these two commands. Extra points for some type of "Are you sure..." prompt.
It look like a case of applying an advice. The question being: what to
advice. Looking at the dired code, it seem that the correct target is
dired-mark-read-file-name that is used to read the destination
file-name. This will work:
(defadvice dired-mark-read-file-name (after rv:dired-create-dir-when-needed (prompt dir op-symbol arg files &optional default) activate)
(when (member op-symbol '(copy move))
(let ((directory-name (if (< 1 (length files))
ad-return-value
(file-name-directory ad-return-value))))
(when (and (not (file-directory-p directory-name))
(y-or-n-p (format "directory %s doesn't exist, create it?" directory-name)))
(make-directory directory-name t)))))
Note that maybe the first when (when (member op-symbol '(copy move))) could be removed for this to apply to more case of file creation in dired. But I'm not sure of when dired-mark-read-file-name is called, So I let this test there to reduce potential unwanted side-effect

What is the canonical way to list numbered backup files emacs has created?

I know how to configure emacs to keep numbered backups. I don't know the most canonical way to find those numbered backups.
The emacs function "find-backup-file-name" seems like it is the closest. Its documentation states:
This function computes the file name for a new backup file for filename. It may also propose certain existing backup files for deletion. find-backup-file-name returns a list whose CAR is the name for the new backup file and whose CDR is a list of backup files whose deletion is proposed.
However, this is not what I am looking for. I'm looking for a list of ALL previously created backup files. Here's the code (paraphrased) I have written to accomplish this:
(defvar backup-directory "~/emacs.d/backups/")
(defun get-backup-pattern (file-name)
(concat "*" (replace-regexp-in-string "\/" "\\!" file-name t t) ".~*"))
(butlast
(split-string
(shell-command-to-string
(concat "find "
backup-directory
" -name \""
(get-backup-pattern (buffer-file-name))
"\""))
"\n"))
This method works fine. However, shelling out to "find" seems a like a hack to me; Especially since this method is platform specific.
Is there a built-in method I should use or at least something more idiomatic?
Personally, I don't save backup files in a central folder so I can't provide working code, but if you want to search the contents of a directory, use directory-files.
So here is the solution I've decided on. I went away from using the *nix find command and am using directory-files as suggested.
(defun get-filter-pattern (file-name)
(concat (replace-regexp-in-string "\/" "!" file-name t t)
".~[0-9]*~*$"))
(defun filter (condp lst)
(delq nil
(mapcar (lambda (x) (and (funcall condp x) x)) lst)))
(defun filter-files (backup-directory buffer-file-name)
(mapcar (lambda (backup-name) (concat backup-directory backup-name))
(filter (lambda (backup-name)
(string-match (get-filter-pattern buffer-file-name) backup-name))
(directory-files backup-directory))))
Perhaps this isn't quite as optimized as using find. However, it should be platform independent (ie can use on Windows).