emacs-devel
[Top][All Lists]
Advanced

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

Re: Improve `replace-regexp-in-string' ergonomics?


From: Dmitry Gutov
Subject: Re: Improve `replace-regexp-in-string' ergonomics?
Date: Wed, 22 Sep 2021 13:59:54 +0300
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0

On 22.09.2021 07:36, Lars Ingebrigtsen wrote:
But I wonder whether we should
consider renaming the function to something more palatable, and since we
have `string-replace', why not `regexp-replace'?  The length of the name
of this common function is itself offputting.

        (org-babel-read
         (concat "'"
                (thread-last
                  results
                  (regexp-replace "'" "\"")
                  (regexp-replace ",[[:space:]]" " ")
                  (regexp-replace "\\]" ")")
                   (regexp-replace "\\[" "("))))

This way makes it impossible to use any optional arguments, right?

But if we target thread-first instead and make the new function accept STRING in the first position, all optional arguments would be still available.

We could also consider making `regexp-replace' take a series of pairs,
since this is so common.  Like:

        (org-babel-read
         (concat "'"
                (regexp-replace "'" "\""
                                ",[[:space:]]" " "
                                "\\]" ")"
                                "\\[" "("
                                results)))

Or some variation thereupon with some more ()s to group pairs.

I'm not sure how to also make it accept "normal" convention, and we probably don't want to always have to wrap the args in an alist, even when only one replacement is needed.

The most popular way to deal with the awkwardness is to just give up and
go all imperative:

(defun authors-canonical-author-name (author file pos)
[...]
   (when author
     (setq author (replace-regexp-in-string "[ \t]*[(<].*$" "" author))
     (setq author (replace-regexp-in-string "\\`[ \t]+" "" author))
     (setq author (replace-regexp-in-string "[ \t]+$" "" author))
     (setq author (replace-regexp-in-string "[ \t]+" " " author))

Which leads me to my other point -- about a quarter of the usages of the
function in Emacs core has "" as the replacement, so perhaps that should
have its own function?  `regexp-remove'?

Then that could be:

   (when author
     (setq author (regexp-remove "[ \t]*[(<].*$" author))
     (setq author (regexp-remove "\\`[ \t]+" author))
     (setq author (regexp-remove "[ \t]+$" author))
     (setq author (regexp-replace "[ \t]+" " " author))

IDK, if that leads to no increase in efficiency, then probably not? Replacing with "" is an established pattern by now.



reply via email to

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