42802

How to replace “(” with “\\(” in the regexp, Emacs/elisp flavor?

Question as title.

More specifically, I'm rather tired of having to type \(, etc. every time I want a parenthesis in Emacs's (interactive) regexp functions (not to mention the \\( in code). So I wrote something like

(defadvice query-replace-regexp (before my-query-replace-regexp activate) (ad-set-arg 0 (replace-regexp-in-string "(" "\\\\(" (ad-get-arg 0))) (ad-set-arg 0 (replace-regexp-in-string ")" "\\\\)" (ad-get-arg 0)))))

in hope that I can conveniently forget about emacs's idiosyncrasy in regexp during "interaction mode". Except I cannot get the regexp right...

(replace-regexp-in-string "(" "\\\\(" "(abc")

gives \\(abc instead of the wanted \(abc. Other variations on the number of slashes just gives errors. Thoughts?

Since I started questioning, might as well ask another one: since lisp code is not supposed to use interactive functions, advicing query-replace-regexp should be okay, am I correct?

Answer1:

The replacement you has works well for me.

Take the text:

hi there mom hi son!

and try query-replace-regexp with your advice:

M-x query-replace-regexp (hi).*(mom) RET \1 \2! RET

yields

hi mom! hi son!

I didn't have to put a backslash in front of the parentheses to get them to group. That said, this disables being able to match actual parentheses...

The reason the replace-regexp-in-string yields \\(abc, is that as a string, that is equivalent to an interactively typed \(abc. In a string \ is used to denote that the following character is special, e.g. "\t" is a string with a tab. So, in order to specify just a backslash, you need to use a backslash in front of it "\\" is a string containing a backslash.

Regarding advising interactive functions, lisp code can call interactive functions all it wants. A prime example is find-file - which is called all over the place. To make your advice a little safer, you can wrap the body with a check for interactive-p to avoid mussing with internal calls:

(defadvice query-replace-regexp (before my-query-replace-regexp activate) (when (interactive-p) (ad-set-arg 0 (replace-regexp-in-string "(" "\\\\(" (ad-get-arg 0))) (ad-set-arg 0 (replace-regexp-in-string ")" "\\\\)" (ad-get-arg 0)))))

Recommend

  • QTabWidget with multiple line?
  • Lat-Long processing in mongoDB
  • How to fix this error? emacs.d/starter-kit.el: `flet' is an obsolete macro (as of 24.3); use ei
  • Will this JavaScript code affect other keypress events too by disabling one key?
  • Convert Tiff data type in libTiff to nsdata ios
  • How to tell the browser to forget htdigest?
  • Test Angular resolve method
  • Check if trigger exists
  • Getting the name of the data set in an '.rda' file
  • make AppleScript program that listens systemwide for shortcuts
  • How can I distinguish scheme dialects in org-babel code blocks?
  • Identify C++ style comments with asm-mode in Emacs Lisp
  • Symfony 3 new project error
  • JQuery Mobile 1.4 How to Disable Hover Effect on Mobile Devices
  • staticfiles and STATIC_URL with virrtualenv - django
  • Convert adjacency matrix to a csv file
  • cast stl::vector containing pointers to stl::vector containing constant pointers
  • Create File Command in Batch Files (*.bat)
  • Installing apk from within application in android
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • C++/CLI Thread synchronization including managed and unmanaged code
  • Using extern @class in order to add a category?
  • Replace and retrieve placeholder value
  • Getting error java.io.FileNotFoundException (log4j log file) at the time of publish project on cloud
  • in batch how do i use taskkill properly
  • allocating memory to an array of string
  • Setting the run time properties on SpringApplicationBuilder()
  • Getting media player state in windows phone 7
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Clear activity stack before launching another activity
  • Alternative to overridePendingTransition() - Android
  • How do I get HTML corresponding to current DOM tree?
  • Refering to the class itself from within a class mehod in Objective C
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • How to show dropdown in excel using jrxml (jasper api)?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++