all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Latest commit to dired-aux; maybe add string-multi-replace?
@ 2016-08-29  8:44 Oleh Krehel
  2016-08-29 11:52 ` Noam Postavsky
  2016-08-29 12:16 ` Tino Calancha
  0 siblings, 2 replies; 4+ messages in thread
From: Oleh Krehel @ 2016-08-29  8:44 UTC (permalink / raw)
  To: emacs-devel

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
    
    



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Latest commit to dired-aux; maybe add string-multi-replace?
  2016-08-29  8:44 Latest commit to dired-aux; maybe add string-multi-replace? Oleh Krehel
@ 2016-08-29 11:52 ` Noam Postavsky
  2016-08-29 12:04   ` Oleh Krehel
  2016-08-29 12:16 ` Tino Calancha
  1 sibling, 1 reply; 4+ messages in thread
From: Noam Postavsky @ 2016-08-29 11:52 UTC (permalink / raw)
  To: Oleh Krehel; +Cc: Emacs developers

On Mon, Aug 29, 2016 at 4:44 AM, Oleh Krehel <oleh@oremacs.com> wrote:
> 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.

What about using `format-spec'? I think that would be:

(dired-shell-command
     (format-spec (cadr suffix)
                  `((?o . ,(shell-quote-argument out-name))
                    (?i . ,(shell-quote-argument
                            (file-name-nondirectory file))))))



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Latest commit to dired-aux; maybe add string-multi-replace?
  2016-08-29 11:52 ` Noam Postavsky
@ 2016-08-29 12:04   ` Oleh Krehel
  0 siblings, 0 replies; 4+ messages in thread
From: Oleh Krehel @ 2016-08-29 12:04 UTC (permalink / raw)
  To: Noam Postavsky; +Cc: Emacs developers

Noam Postavsky <npostavs@users.sourceforge.net> writes:

> On Mon, Aug 29, 2016 at 4:44 AM, Oleh Krehel <oleh@oremacs.com> wrote:
>> 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.
>
> What about using `format-spec'? I think that would be:
>
> (dired-shell-command
>      (format-spec (cadr suffix)
>                   `((?o . ,(shell-quote-argument out-name))
>                     (?i . ,(shell-quote-argument
>                             (file-name-nondirectory file))))))

Thanks, Noam. `format-spec' is exactly what I needed.

Oleh



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Latest commit to dired-aux; maybe add string-multi-replace?
  2016-08-29  8:44 Latest commit to dired-aux; maybe add string-multi-replace? Oleh Krehel
  2016-08-29 11:52 ` Noam Postavsky
@ 2016-08-29 12:16 ` Tino Calancha
  1 sibling, 0 replies; 4+ messages in thread
From: Tino Calancha @ 2016-08-29 12:16 UTC (permalink / raw)
  To: Oleh Krehel; +Cc: tino.calancha, Emacs developers



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"



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-08-29 12:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-29  8:44 Latest commit to dired-aux; maybe add string-multi-replace? Oleh Krehel
2016-08-29 11:52 ` Noam Postavsky
2016-08-29 12:04   ` Oleh Krehel
2016-08-29 12:16 ` Tino Calancha

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.