In emacs, can I set up the *Messages* buffer so that it tails?

前端 未结 6 714
借酒劲吻你
借酒劲吻你 2021-01-07 22:42

Basically I want the *Messages* buffer to always scroll to the bottom when a new message arrives.

Can I do that?

6条回答
  •  感情败类
    2021-01-07 23:15

    i run 23.3 and there were still way too many occasions where the built-in 'solution' and the orginal defadvice on the message function just didn't cut it, so i wrapped that code in a list / toggle / timer set up and it's working beautifully - no more frustration when debugging!

    it's generic, so works on any buffer, although i only really use it for..

    (toggle-buffer-tail "*Messages*" "on")
    

    ..hope it's useful to someone.

    ;alist of 'buffer-name / timer' items
    (defvar buffer-tail-alist nil)
    (defun buffer-tail (name)
      "follow buffer tails"
      (cond ((or (equal (buffer-name (current-buffer)) name)
             (string-match "^ \\*Minibuf.*?\\*$" (buffer-name (current-buffer)))))
            ((get-buffer name)
          (with-current-buffer (get-buffer name)
            (goto-char (point-max))
            (let ((windows (get-buffer-window-list (current-buffer) nil t)))
              (while windows (set-window-point (car windows) (point-max))
             (with-selected-window (car windows) (recenter -3)) (setq windows (cdr windows))))))))
    
    (defun toggle-buffer-tail (name &optional force)
      "toggle tailing of buffer NAME. when called non-interactively, a FORCE arg of 'on' or 'off' can be used to to ensure a given state for buffer NAME"
      (interactive (list (cond ((if name name) (read-from-minibuffer 
          (concat "buffer name to tail" 
            (if buffer-tail-alist (concat " (" (caar buffer-tail-alist) ")") "") ": ")
        (if buffer-tail-alist (caar buffer-tail-alist)) nil nil
               (mapcar '(lambda (x) (car x)) buffer-tail-alist)
            (if buffer-tail-alist (caar buffer-tail-alist)))) nil)))
      (let ((toggle (cond (force force) ((assoc name buffer-tail-alist) "off") (t "on")) ))
        (if (not (or (equal toggle "on") (equal toggle "off"))) 
          (error "invalid 'force' arg. required 'on'/'off'") 
          (progn 
            (while (assoc name buffer-tail-alist) 
               (cancel-timer (cdr (assoc name buffer-tail-alist)))
               (setq buffer-tail-alist (remove* name buffer-tail-alist :key 'car :test 'equal)))
            (if (equal toggle "on")
                (add-to-list 'buffer-tail-alist (cons name (run-at-time t 1 'buffer-tail name))))
            (message "toggled 'tail buffer' for '%s' %s" name toggle)))))
    

    edit: changed functionality to display tail at the bottom of the window

提交回复
热议问题