Why is (string-match "lisp" major-mode) not working, while (string-equal "lisp-interaction-mode" major-mode) is working?

string-match results in debugger message: (wrong-type-argument stringp lisp-interaction-mode)

But help states:

(string-equal S1 S2)

(string-match REGEXP STRING &optional START)

(S1 and S2 is a placeholder for STRING, right?)

I already know that (string-match "lisp" (prin1-to-string major-mode)) can be used to get the expected result.

  • 2
    (string-match "lisp" (symbol-name major-mode)) would be more precise, depending on what you want (derived-mode-p 'emacs-lisp-mode) might make sense. – npostavs May 30 '17 at 14:17
  • In addition, if you only want to check whether a pattern matches a string, so you don't care what the matched part is, then consider using string-match-p instead of string-match. – Drew May 30 '17 at 15:33

string-match doesn't allow symbols but string-equal does.

(string-equal S1 S2)

Return t if two strings have identical contents. Case is significant, but text properties are ignored. Symbols are also allowed; their print names are used instead.

Note that result of evaluating major-mode is not a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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