5

I thought this would be simple when I tried to do it, however I'm stuck.

When I add octave-mode to my ~/.emacs thus:

(add-to-list 'auto-mode-alist ("\\.m$" . octave-mode))

Opening an Octave file, .m, I instead end up in the OBJC major mode ... this is because auto-mode-alist contains:

(\.m\' . objc-mode)

which comes first in the A-list.

I've tried:

(setq auto-mode-alist (delete '( \.m\' . objc-mode) auto-mode-alist))

and I've even tried:

(setq ama '())
(setq objc '(\.m\' . objc-mode))
   (dolist (item auto-mode-alist)
      (if (not (eq (cdr (last objc)) (cdr (last item))))
          (setq ama (list ama item))))
 (setq auto-mode-alist ama)

Any suggestions on either removing the objc-mode from the alist or ensuring that octave-mode supercedes it would be great.

1
  • 2
    auto-mode-alist contains ("\\.m\\'" . objc-mode) by default. Where are you seeing (\.m\' . objc-mode) with not-double-quoted string syntax?
    – phils
    Jul 1, 2018 at 9:56

3 Answers 3

10

There are essentially two questions here. One is how to remove the element from the list. The other is how to automatically open *.m files in octave-mode. You shouldn't need to remove the element to override it. The provided form

(add-to-list 'auto-mode-alist ("\\.m$" . octave-mode))

causes an error. Instead you should use

(add-to-list 'auto-mode-alist '("\\.m$" . octave-mode))

or better yet:

(add-to-list 'auto-mode-alist '("\\.m\\'" . octave-mode))

These two forms will add the element to the beginning of the association list, meaning filenames will be checked against it first, never making it to the objc-mode element lower down the list.

If you really want to remove the element from the list, here are a couple of ways.

One way that only deletes the exact cons cell '("\\.m\\'" . objc-mode):

(setq auto-mode-alist (delete '("\\.m\\'" . objc-mode) auto-mode-alist))

Another way that will delete anything in the association list associated with "\\.m\\'":

(require 'cl-lib)
(cl-remove "\\.m\\'" auto-mode-alist :test 'equal :key 'car)
1
  • 1
    Apparently delete already delete the item from the original list, you may want to use remove in its place, or just the internal command (delete '("\\.m\\'" . objc-mode) auto-mode-alist)
    – campisano
    Apr 11, 2021 at 2:40
1

There's three obstacles here:

  1. Quoting and regexp syntax at the same time
  2. assq-delete-all (the obvious choice) uses eq to compare the keys, which only succeeds (and thus deletes) it finds the same object - it won't succeed if it sees only an identical string
  3. assq-delete-all does not guarantee to modify the list, so another setq is necessary

The solution is clear: find the car object in the alist. Like:

(setq auto-mode-alist (assq-delete-all (car (rassoc 'objc-mode auto-mode-alist)) auto-mode-alist))

After that, You can

(setq auto-mode-alist (add-to-list 'auto-mode-alist '("\.m\'" . octave-mode)))

0

To add to what was already said, for completion.

For some modes, the auto-mode-alist contains several different entries associating different file extensions to a mode. For example, the racket-mode uses 3 entries:

...
 ("\\.rktl\\'" . racket-mode)
 ("\\.rktd\\'" . racket-mode)
 ("\\.rkt\\'" . racket-mode)
...

If you want to remove those, the single assq-delete-all on the mode symbol is not sufficient, you must loop with something like this:

  (while (rassoc 'racket-mode auto-mode-alist)
    (setq auto-mode-alist
          (assq-delete-all (car (rassoc 'racket-mode auto-mode-alist))
                           auto-mode-alist))))

I ran on this issue with racket-mode because the built-in scheme-mode associate the .rtk files with scheme-mode and this association was closer to the head of the auto-mode-alist than the ones to racket-mode. Trying to add a new one failed because it was already present. To be able to have racket-mode used with .rkt files while scheme-mode still available for other scheme files, I had to remove the racket-mode entries first and them add them back, placing them in front of the association with the scheme-mode.

Your Answer

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

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