emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Latest commit to dired-aux; maybe add string-multi-replace?


From: Tino Calancha
Subject: Re: Latest commit to dired-aux; maybe add string-multi-replace?
Date: Mon, 29 Aug 2016 21:16:49 +0900 (JST)
User-agent: Alpine 2.20 (DEB 67 2015-01-07)



On Mon, 29 Aug 2016, Oleh Krehel wrote:

In which file would the new function belong?
I guess this should go to subr.el (or subr-x.el).
In that case you should use:
(car (cddr pat))
instead of
(caddr pat)
Any further comments?
I would mention in the doc string that the returned string is
actually a copy of STR.
I would also mention the order of the replacements: only in case this
doesn't result obvious, but i guess it should be obvious.

How about passing the patterns just as a list of strings as follows?:

(defun string-multi-replace (str &rest patterns)
  "Replace PATTERNS in STR.
Return a new string containing the replacements.
PATTERNS is a list of patterns (FROM TO LITERAL ...).
Replace patterns calling `replace-regexp-in-string'
sequencially starting from (car PATTERNS)."
  (let (pat from to literal)
    (unless (zerop (% (length patterns) 3))
      (error "Length of patterns should be a multiple of 3"))
      (while (setq pat patterns)
        (setq from     (car pat)
              to       (cadr pat)
              literal  (car (cddr pat))
              str      (replace-regexp-in-string from to str nil literal)
              patterns (nthcdr 3 patterns)))
      str))

 (string-multi-replace
  "tar -c %i | xz -c9 > %o"
  "%o" (shell-quote-argument "foo bar.tar.gz") t
  "%i" (shell-quote-argument "foo bar") t)
;; => "tar -c foo\\ bar | xz -c9 > foo\\ bar.tar.gz"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]