Q: I have a list of strings: '("apple" "pear" "peach" "nectarine" "watermelon")
. How can I replace "peach"
with "plum"
and maintain the same order of fruits? [CAVEAT: The list may be very long and the nth position of the element to be replaced will not be known at the outset.]
BACKBROUND:
I am working on implementing wdired
compatibility with a dired-mode
listing of files created by calling:
(dired (directory-files-recursively "/path/to/directory" "" 'include-directories))
This creates a listing of absolute paths to files and directories. After modifying the wdired
buffer, and pressing the command to finalize/commit the edits, here is what happens:
The files get renamed correctly using a revised version of
wdired-get-filename
, thanks to the assistance of @Drew in a related thread: Dired / Wdired: How to test whether a filename is relative / absolute . [The renaming of directories (if applicable) will be dealt with by me on another day.]dired-readin-insert
does not work correctly to populate the buffer anew because the variabledired-directory
was not updated with the new absolute path of the files that got renamed./path/to/bin/gls: cannot access '/path/to/filename': No such file or directory
There will undoubtedly be additional hurdles to implement this new wdired
feature, so I understand this will be one baby step at a time. To resolve this particular question, I am looking for something a little fancier than delete
and push/add-to-list
. Specifically, I would like to programmatically replace the old filename with the new filename and maintain the same list order.
EDIT: Here is a link to the bug-report seeking an enhancement of wdired and dired: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=31601
default-directory
at all.dired-mode
does not presently have the ability to update thedired-directory
variable when a user renames a file in a buffer containing a recursive list of files in various subdirectories. I tried to rename a file in adired-mode
buffer also in Emacs 26 using the recursive listing of files as indicated in the question, and the variable is not updated.(when (and (listp dired-directory) (member file dired-directory)) (setq dired-directory (ar-replace--in-list file newname dired-directory)))
todired-rename-file
fixes the issue desribed in #2 of the question above. I would imagine that this will need to be added to other aspects ofdired
such as deleting a file; and, there may be an issue with speed/performance. Perhaps a hash-table system would be better suited to this type of job. I'll submit an enhancement request to the Emacs team.