2

Is this the way to use add-to-list with multiple entries?

      (add-to-list 'auto-mode-alist 
         '(("\\.f\\'" . fortran-mode)
           ("\\.F\\'" . fortran-mode)
           ("\\.f90\\'" . f90-mode)
           ("\\.f95\\'" . f90-mode)
           ("\\.f03\\'" . f90-mode)
           ("\\.f08\\'" . f90-mode)))

If not, how can I do so?

1
  • 1
    When you try it in your *scratch* buffer, what does auto-mode-alist look like afterwards?
    – db48x
    Commented Jul 26, 2022 at 15:11

2 Answers 2

2

No. The doc tells you that add-to-list adds a single element.

You can do what you want with this, for example:

(defun add-list-to-list-var (list-var new-elements &optional append compare-fn)
  "Like `add-to-list', but add multiple elements NEW-ELEMENTS, in order."
  (interactive)
  (setq new-elements  (if append new-elements (reverse new-elements)))
  (let* ((val  (symbol-value list-var))
         (lst  (if append (reverse val) val)))
    (dolist (elt new-elements)
      (cl-pushnew elt lst :test compare-fn))
    (set list-var (if append (nreverse lst) lst)))
  (symbol-value list-var))
    
;; Check it:
(setq bar '("a" "b"))

(setq foo '("1" "2" "b" "4" "5"))
(add-list-to-list-var 'foo bar)                   ; ("a" "b" "1" "2" "b" "4" "5")

(setq foo '("1" "2" "b" "4" "5"))
(add-list-to-list-var 'foo bar 'APPEND)           ; ("1" "2" "b" "4" "5" "a" "b")

(setq foo '("1" "2" "b" "4" "5"))
(add-list-to-list-var 'foo bar nil #'string=)     ; ("a" "1" "2" "b" "4" "5")

(setq foo '("1" "2" "b" "4" "5"))
(add-list-to-list-var 'foo bar 'APPEND #'string=) ; ("1" "2" "b" "4" "5" "a")
1

The simplest way is using dolist:

(dolist (el '(("\\.f\\'" . fortran-mode)
              ("\\.F\\'" . fortran-mode)
              ("\\.f90\\'" . f90-mode)
              ("\\.f95\\'" . f90-mode)
              ("\\.f03\\'" . f90-mode)
              ("\\.f08\\'" . f90-mode)))
  (add-to-list 'auto-mode-alist el))

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.