[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Latest commit to dired-aux; maybe add string-multi-replace?
From: |
Oleh Krehel |
Subject: |
Latest commit to dired-aux; maybe add string-multi-replace? |
Date: |
Mon, 29 Aug 2016 10:44:43 +0200 |
Hi all,
I just fixed the `dired-do-compress' command to work with files and
directories that have spaces in them.
After the fix, I saw this pattern repeat:
(dired-shell-command
(replace-regexp-in-string
"%o" (shell-quote-argument out-name)
(replace-regexp-in-string
"%i" (shell-quote-argument (file-name-nondirectory file))
(cadr suffix)
nil t)
nil t))
I've seen the pattern of nested `replace-regexp-in-string' quite a few
times before. It doesn't look great.
I propose the following API:
(defun string-multi-replace (str &rest patterns)
"Return STR after replacing PATTERNS in it.
PATTERNS is a list of (FROM TO LITERAL)."
(let (pat from to literal)
(while (setq pat (pop patterns))
(setq from (car pat))
(setq to (cadr pat))
(setq literal (caddr pat))
(setq str (replace-regexp-in-string from to str nil literal)))
str))
(string-multi-replace
"tar -c %i | xz -c9 > %o"
(list "%o" (shell-quote-argument "foo bar.tar.gz") t)
(list "%i" (shell-quote-argument "foo bar") t))
;; => "tar -c foo\\ bar | xz -c9 > foo\\ bar.tar.gz"
Would this be OK? In which file would the new function belong? Any
further comments?
regards,
Oleh
- Latest commit to dired-aux; maybe add string-multi-replace?,
Oleh Krehel <=