Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an item from auto-mode-alist (Emacs)

Tags:

list

emacs

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.

like image 867
Daniel Avatar asked Jan 02 '23 04:01

Daniel


1 Answers

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)
like image 57
Michael Hoffman Avatar answered Jan 09 '23 08:01

Michael Hoffman