Devon Ville
Devon Ville

Reputation: 2091

Check if major mode is equal one of several emacs

I found a snippet to close all dired buffers, which I want to use in sunrise commander:

(defun er/kill-all-dired-buffers()
      "Kill all dired buffers."
      (interactive)
      (save-excursion
        (let((count 0))
          (dolist(buffer (buffer-list))
            (set-buffer buffer)
            (when (equal major-mode 'sr-mode) 
              (or (equal major-mode 'dired-mode))
              (setq count (1+ count))
              (kill-buffer buffer)))
          (message "Killed %i dired buffer(s)." count ))))
(setq sr-quit-hook 'er/kill-all-dired-buffers)

Issue being, I can't make it work both for sr-mode and dired-mode together. How do I check "if major mode is sr-mode OR dired-mode"?


EDIT: Just a syntax error. Should be

(when (or (equal major-mode 'dired-mode) (equal major-mode 'sr-mode))

Have to admit it's not too intuitive.

Upvotes: 10

Views: 3326

Answers (3)

Medardo Rodriguez
Medardo Rodriguez

Reputation: 19

Maybe the correct check function is:

(derived-mode-p &rest MODES)

See 'subr.el'.

Upvotes: 0

alexpanter
alexpanter

Reputation: 1578

I tried some things and found this to work on my emacs-ielm - perhaps it might help also:

(if (member major-mode '(fsharp-mode c-mode java-mode inferior-emacs-lisp-mode))
(message "yeah right"))

Upvotes: 4

Stefan
Stefan

Reputation: 28531

The canonical way would be (when (derived-mode-p 'sr-mode 'dired-mode) ...).

Upvotes: 16

Related Questions