help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Visiting buffers programmatically


From: Jean Louis
Subject: Re: Visiting buffers programmatically
Date: Fri, 11 Jun 2021 03:47:03 +0300
User-agent: Mutt/2.0.7+183 (3d24855) (2021-05-28)

* Tim Johnson <tim@akwebsoft.com> [2021-06-11 00:18]:
> GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo
> version 1.16.0)
>  of 2021-06-04 ubuntu 20.04
> 
> I would like to write three simple elisp routines that would each visit one
> of:
> *scratch*, *messages* and *shell*
> 
> For *messages* I would only be reading or copying text.
> For *scratch* and *shell* I would be modifying those buffers.
> 
> which elisp function is best for each of
> these buffers?

Here is function that will cycle over the list of buffer names:

(defvar rcd-switch-to-buffer-list '("*scratch*" "*shell*" "*Messages*"))

(defun rcd-switch-to-buffer ()
  (interactive)
  (if (member (current-buffer) (mapcar (lambda (buffer) (get-buffer buffer))
                                       rcd-switch-to-buffer-list))
      (when (buffer-live-p (next-circular-list-item 
                            (mapcar (lambda (buffer) (get-buffer buffer))
                                    rcd-switch-to-buffer-list)
                            (current-buffer)))
        (switch-to-buffer 
         (next-circular-list-item 
          (mapcar (lambda (buffer) (get-buffer buffer))
                  rcd-switch-to-buffer-list)
          (current-buffer))))
    (when (buffer-live-p (get-buffer (nth 0 rcd-switch-to-buffer-list)))
      (switch-to-buffer (nth 0 rcd-switch-to-buffer-list)))))

and you need this one too:

(defun next-circular-list-item (list previous-item)
  "Return the next element in the LIST by looking at PREVIOUS-ITEM.

All elements in the LIST have to be different.

If last element of LIST is equal to PREVIOUS-ITEM then first element is 
returned."
  (let ((last-element (car (last list))))
    (cond ((eq last-element previous-item) (car list))
          (t (nth 1 (member previous-item list))))))


Then M-x rcd-switch-to-buffer will cycle over the list of buffers.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]