0

I'd like to set up an alist (similar to auto-mode-alist), which is made up from regular expressions as key (AKA car) of the list. Next I'd like to be able, to match, say the major mode, against the regex in order to retrieve the value/cdr of the matching key/car.

(setq jb-string-mode-alist
     '(("\\(emacs-\\)?lisp\\(-interaction\\)?-mode" . "Combined")
        (lisp-interaction-mode . "Interaction") ; works
        (emacs-lisp-mode       . "Emacs")       ; works also
        (lisp-mode             . "Lisp")        ; works also
        (latex-mode            . "LaTeX")       ; works also 
        (".*$"                 . "Default")))

Afterwards I tried something like the following:

(assoc major-mode jb-tools-date-time-string-mode-alist) ; => "Interaction"

Evaluating the expression in the *scratch* buffer of emacs yields "Interaction" as the mode of the *scratch* buffer is lisp-interaction-mode. This matches the third entry in my alist, but it should match the first entry consisting of the regular expression.

Using assq instead of assoc makes no difference.

What is the trick, to evaluate the mode with respect to regular expressions?

1

1 Answer 1

2

assoc has the following documentation, which you can view by running C-h f assoc:

(assoc KEY ALIST &optional TESTFN)

Return non-nil if KEY is equal to the car of an element of ALIST.

The value is actually the first element of ALIST whose car equals KEY.

Equality is defined by the function TESTFN, defaulting to equal.
TESTFN is called with 2 arguments: a car of an alist element and KEY.

See that third argument? You can supply your own function there and it will be called instead of equal. If it returns true, the assoc be finished and will return the value associated with this key.

If you look in chapter 35.4 Regular Expression Searching of the Emacs Lisp manual, you will find that there is a function called string-match-p which returns t when the provided string matches the provided regexp. It is no coincidence that it takes the arguments in the same order that assoc provides them. Put the two together and call assoc like this:

(assoc major-mode jb-tools-date-time-string-mode-alist 'string-match-p)

Don’t forget that you can read the manuals inside of Emacs by running C-h i. Everything you need to know is in there.

6
  • Strange. I am using Aquamacs (Aquamacs 3.5 GNU Emacs 25.3.50.1) and assoc is described only with two arguments and no optional argument. Hence your solution doesn't work for me, as appealing as it seems to me :-(.
    – Jan
    Dec 13, 2021 at 14:47
  • :-( I get an error using your suggestion Debugger entered--Lisp error: (wrong-number-of-arguments assoc 3) Shit. Nevertheless big thanks!
    – Jan
    Dec 13, 2021 at 14:51
  • Also being the latest Aquamcs, it is by far not based on the latest GNU Emacs. So the solution seems to be clear to me.
    – Jan
    Dec 13, 2021 at 14:56
  • Yea, the solution is clearly to upgrade your Emacs. Just use the native version of Emacs, rather than some old alternate version.
    – db48x
    Dec 13, 2021 at 15:13
  • Upgraded, and it works. One problem solved, 10 new problems created :-D
    – Jan
    Dec 13, 2021 at 19:43

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.