I am trying to create a function that switches to the most recent buffer in major mode major-mode
. An answer to a related question supplies a function that should require the smallest of tweaks to serve my purpose, but I can't make it work. Here is the original function in that answer:
(defun switch-to-most-recent-org-buffer ()
(interactive)
(let (found)
(catch 'done
(mapc (lambda (x)
(when (with-current-buffer x (eq major-mode 'org-mode))
(switch-to-buffer x)
(setq found t)
(throw 'done nil)))
(buffer-list))
(unless found (message "not found")))))
I thought I could just replace the first and sixth lines, respectively, with
(defun switch-to-most-recent-buffer-in-mode (major-mode)
and
(when (with-current-buffer x (eq major-mode major-mode))
Which gives me this:
(defun switch-to-most-recent-buffer-in-mode (major-mode)
(interactive)
(let (found)
(catch 'done
(mapc (lambda (x)
(when (with-current-buffer x (eq major-mode major-mode))
(switch-to-buffer x)
(setq found t)
(throw 'done nil)))
(buffer-list))
(unless found (message "not found")))))
but it doesn't work: while the original function switches to my most recent org buffer, evaluating (switch-to-most-recent-buffer-in-mode org-mode)
returns a "not found" message. It seems I am failing to grasp something very basic about Elisp, so please excuse my ignorance: I'm not a programmer and am new to Emacs.
--
[edited to add:] The amended code below works (taken from phils's answer).
(defun switch-to-most-recent-buffer-in-mode (mode)
(interactive "C")
(let (found)
(catch 'done
(mapc (lambda (x)
(when (with-current-buffer x (eq major-mode mode))
(switch-to-buffer x)
(setq found t)
(throw 'done nil)))
(buffer-list))
(unless found (message "not found")))))