0

Action 1: evaluate the lisp expression: (search-forward-regexp "\\s'") in a buffer in lisp-interaction-mode containing the text:

The pandemic market was cooling, but homes in north Seattle were still going for over asking price. 'A few homes they were interested in received multiple offers,' she said, 'and sold for 10 to 15 percent over list price.'

Result 1: the comma after "cooling" will be found.


Action 2: evaluate the exact same expression above in another buffer, containing the exact same text, except that this time the bufer is in text-mode.

Result 2: nothing will be found and a search-failed error is issued.


The question is thus: what justifies this weird behavior?

I cannot understand why a buffer's mode would affect the behavior of search-forward-regexp. It totally blows my mind that the "comma" is found in the first case. Regarding the second case, things are still incomprehensible since "\\s" is described in the manual as

Match whitespace, it is a synonym for ‘[[:space:]]’

so the second search should match the space before "A few homes" followed by the single quote.

Could I possibly be interpreting things in the wrong way?

2
  • 1
    Which manual are you looking at? Please direct us to the phrase "Match whitespace, it is a synonym for ‘[[:space:]]’", because that isn't true.
    – phils
    Nov 10, 2022 at 20:53
  • 1
    It turns out that was a quote from the GNU grep manual, which explains the confusion.
    – phils
    Nov 11, 2022 at 2:31

1 Answer 1

2

I don't know which manual you're looking at, but \\s is not a synonym for [[:space:]].

In the double-quoted read syntax for strings, "\\s'" is the regexp \s' which means this:

‘\sCODE’
     matches any character whose syntax is CODE.  Here CODE is a
     character that represents a syntax code: thus, ‘w’ for word
     constituent, ‘-’ for whitespace, ‘(’ for open parenthesis, etc.  To
     represent whitespace syntax, use either ‘-’ or a space character.
     *Note Syntax Class Table::, for a list of syntax codes and the
     characters that stand for them.

Syntax tables are buffer-local and therefore different modes may indeed set different syntax for a given character.

\s' matches any character which is an "expression prefix":

Expression prefixes: ‘'’
     Characters used for syntactic operators that are considered as part
     of an expression if they appear next to one.  In Lisp modes, these
     characters include the apostrophe, ‘'’ (used for quoting), the
     comma, ‘,’ (used in macros), and ‘#’ (used in the read syntax for
     certain data types).

Refer to:

  • C-hig (elisp)Regexp Backslash
  • C-hig (elisp)Syntax Class Table
5
  • OK, so I am indeed interpreting things in the wrong way. The info node (grep)The Backslash Character and Special Expressions states that ‘\s’ matches whitespace and I erroneously understood that I should double the backslash as it is usually required by the read syntax for strings. Now I see that there is a difference between "the escaped character s" and "the string formed by the characters backslash plus s" and clearly the manual adopts the previous interpretation. Thanks very much for pointing it out!
    – Ruy
    Nov 11, 2022 at 1:38
  • 2
    You were reading the manual for GNU grep, which is a command-line program that is fully independent of Emacs. The info reader allows you to access manuals for many things besides Emacs. You need to read the Emacs manuals -- primarily (elisp), the Emacs Lisp Reference Manual, or (emacs) for end-user documentation. There are numerous others, but they should all be grouped under an "Emacs" heading in the info directory.
    – phils
    Nov 11, 2022 at 2:23
  • Note that M-x elisp-index-search is often a great entry point.
    – phils
    Nov 11, 2022 at 2:33
  • 2
    And for total clarity: Although both grep and Emacs support regular expressions, there's no reason to expect that they (or indeed any other program) use the same syntax to represent regexps, nor that they support the same set of regexp features. Emacs regexps are in fact fairly similar to the default BRE regexps used by grep, but they are definitely not the same; and in general reading the manual for program A in order to learn how to use program B is going to be a mistake.
    – phils
    Nov 11, 2022 at 2:39
  • Phils, you are right, besides messing up with the interpretation of the backlash character I was using the wrong manual. I am from another area of science, where concepts are pretty much universaly defined, and I erroneously imagined that this principle applies to computer science!
    – Ruy
    Nov 11, 2022 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.