This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In emacs, can I set up the *Messages* buffer so that it tails?
I am playing with Elisp, and I find convenient to have the *Messages* buffer always open in a window in my frame.
I discovered recently that sometimes the buffer stops following the last line in the file. If I want to see the last appended lines in this buffer, I need to go in the buffer and jump to the end manually, with M->. Which quite annoying and disruptive.
I am trying to reproduce the "tail -f" command line, in a buffer. Of course 'auto-revert-tail-mode complains that the *Messages* is not a visited file... As a consequence, this mode does not want to work. But it gave me the idea to add a function hook when the buffer is modified. That function would jump to (point-max) each time that buffer is modified.
Here is my own attempt, invoked from *Messages* buffer, with M-::
(add-hook 'after-change-functions (lambda (s e l) (goto-char (point-max)) nil) nil t)
But it does not work. The (point) remains in the same place while I see the buffer is growing... The lambda function does not produce any error, otherwise it would have been removed from the 'after-change-functions hook and C-h k 'after-change-functions shows it is present.
Any better suggestions?
Modifying the point position from after-change-functions is very dangerous anyway because it can break some types of edit to the buffer (for example, Emacs compresses multiple consecutive messages with the same content). However, for your purposes the post-command-hook is more than sufficient and much safer, so you can just use this:
(add-hook 'post-command-hook
(lambda ()
(let ((messages (get-buffer "*Messages*")))
(unless (eq (current-buffer) messages)
(with-current-buffer messages
(goto-char (point-max)))))))
The hook will make sure the point in *Messages* is at the end of buffer after every command, unless you're currently editing the *Messages buffer itself.
Well I've made my own one with set-window-point.
(defun tail-f-msgs ()
"Go to the end of Messages buffer."
(let ((msg-window (get-buffer-window "*Messages*")))
(if msg-window
(with-current-buffer (window-buffer msg-window)
(set-window-point msg-window (point-max))))))
;; Make the Messages buffer stick to the end.
(add-hook 'post-command-hook 'tail-f-msgs)
Related
when I type in emacs shell and tab to complete, a completion minibuffer shows up and list the possible completions like this:
Click <mouse-2> on a completion to select it.
In this buffer, type RET to select the completion near point.
Possible completions are:
CLUTTER_IM_MODULE DBUS_SESSION_BUS_ADDRESS DEFAULTS_PATH
I was able to remove first two lines by setting (setq completion-show-help nil). but is it possible to get rid of possible completions are:? I just want a little bit of cleanness.
One simple hack around that message not being customizable is to just erase that line from the output buffer after display-completion-list runs (assuming you have already set completion-show-help to nil),
(define-advice display-completion-list (:after (&rest r) "remove-msg")
(with-current-buffer standard-output
(goto-char (point-min))
(when (looking-at-p "Possible completions.*")
(delete-region (line-beginning-position) (line-beginning-position 2)))))
Sometimes I get multiple windows open for the same buffer (or a similar one) and I have to differentiate whether or not the buffer in the window is the same as another before deciding to either kill it or delete the window.
Is there a way in emacs to simply delete a window only if the buffer exists already in another? Ideally I would like the same function to also kill the buffer and the window if it is the only instance of the buffer in a window.
(defun delete-extra-windows ()
(interactive)
(let* ((selwin (selected-window))
(buf (window-buffer selwin)))
(walk-windows (lambda (ww)
(unless (eq ww selwin)
(when (eq (window-buffer ww) buf)
(delete-window ww))))
'NO-MINI 'THIS-FRAME)))
I added quit-window (normally bound to q in non-self-insert - AKA special - buffers) 15 years ago to solve a similar problem.
You can try it or its sibling quit-windows-on.
Your specification of what you wanted is not clear. You said "delete a window only if the buffer exists already in another". That means do not delete the window if the buffer does not exist in another window. Yet you also said "kill the buffer and the window if it is the only instance of the buffer in a window", which contradicts the first requirement.
I guess by "delete a window only if..." you really meant "delete only the window (not also the buffer) if...".
(defun delete-window-maybe-kill-buffer ()
"Delete selected window.
If no other window shows its buffer, kill the buffer too."
(interactive)
(let* ((selwin (selected-window))
(buf (window-buffer selwin)))
(delete-window selwin)
(unless (get-buffer-window buf 'visible) (kill-buffer buf))))
This is the behavior I was looking for. Thank you for helping with the basic function layour and logic. Elisp is still very confusing to work with and I appreciate help with getting through the rough spots.
See the code somment which explains the behavior. You should also be able to understand it directly from the source.
I've up-voted your previous answer which includes the bulk of the code I used.
;;; Delete the selected window without killing the buffer if the buffer is open
;;; in another; otherwise close the window and its buffer. If called on the
;;; last visible window then the buffer will simply be killed and replaced by
;;; the next available buffer.
(defun delete-window-maybe-kill-buffer ()
"Delete selected window.
If no other window shows its buffer, kill the buffer too."
(interactive)
(let* ((selwin (selected-window))
(buf (window-buffer selwin)))
(if (> (length (window-list)) 1)
(delete-window selwin)
(unless (get-buffer-window buf 'visible) (kill-buffer buf))
(kill-buffer buf))))
How can I make Emacs retain its undo history for my buffer after doing revert-buffer or using auto-revert-mode?
In Vim, if a file that is open in a buffer is changed on disc, Vim prompts me to reload the file. I can then simply click 'u' to undo the reload if I so wish and even go back further from then. Emacs seems to trash all the undo information the moment I revert the buffer.
Emacs allows you to set revert-buffer-function to override the behaviour. Here's a revert-buffer implementation that keeps the history.
;; emacs doesn't actually save undo history with revert-buffer
;; see http://lists.gnu.org/archive/html/bug-gnu-emacs/2011-04/msg00151.html
;; fix that.
(defun revert-buffer-keep-history (&optional IGNORE-AUTO NOCONFIRM PRESERVE-MODES)
(interactive)
;; tell Emacs the modtime is fine, so we can edit the buffer
(clear-visited-file-modtime)
;; insert the current contents of the file on disk
(widen)
(delete-region (point-min) (point-max))
(insert-file-contents (buffer-file-name))
;; mark the buffer as not modified
(not-modified)
(set-visited-file-modtime))
(setq revert-buffer-function 'revert-buffer-keep-history)
You could use the before-hook to save the previous buffer-content to the kill-ring:
(add-hook 'before-revert-hook (lambda () (kill-ring-save (point-min) (point-max))))
The upcoming Emacs-24.4 does what you want by default.
I guess the obvious approach would be a function which kills the current buffer content, and then calls insert-file to read in the current content from the file.
If the changes to the file included changes to the character encoding, there might be problems? I haven't tested that.
Here's my current attempt. It's a little hairy IMO, but it works okay.
;; Allow buffer reverts to be undone
(defun my-revert-buffer (&optional ignore-auto noconfirm preserve-modes)
"Revert buffer from file in an undo-able manner."
(interactive)
(when (buffer-file-name)
;; Based upon `delphi-save-state':
;; Ensure that any buffer modifications do not have any side
;; effects beyond the actual content changes.
(let ((buffer-read-only nil)
(inhibit-read-only t)
(before-change-functions nil)
(after-change-functions nil))
(unwind-protect
(progn
;; Prevent triggering `ask-user-about-supersession-threat'
(set-visited-file-modtime)
;; Kill buffer contents and insert from associated file.
(widen)
(kill-region (point-min) (point-max))
(insert-file-contents (buffer-file-name))
;; Mark buffer as unmodified.
(set-buffer-modified-p nil))))))
(defadvice ask-user-about-supersession-threat
(around my-supersession-revert-buffer)
"Use my-revert-buffer in place of revert-buffer."
(let ((real-revert-buffer (symbol-function 'revert-buffer)))
(fset 'revert-buffer 'my-revert-buffer)
;; Note that `ask-user-about-supersession-threat' calls
;; (signal 'file-supersession ...), so we need to handle
;; the error in order to restore revert-buffer.
(unwind-protect
ad-do-it
(fset 'revert-buffer real-revert-buffer))))
(ad-activate 'ask-user-about-supersession-threat)
Annoyingly, I've only just noticed all the relevant-looking information in the revert-buffer docs, so there's probably a much simpler way to do this.
If the value of revert-buffer-function is non-nil, it is called to
do all the work for this command. Otherwise, the hooks
before-revert-hook and after-revert-hook are run at the beginning
and the end, and if revert-buffer-insert-file-contents-function is
non-nil, it is called instead of rereading visited file contents.
What I must write in my .emacs file so that the *scratch* buffer is closed when I open Emacs?
(kill-buffer "*scratch*")
Not exactly the answer to your question, but you might like to know that you can choose to have a different buffer open on startup, or change the contents of the *scratch* buffer. For example:
;; Make *scratch* buffer blank.
(setq initial-scratch-message nil)
;; Make the buffer that opens on startup your init file ("~/.emacs" or
;; "~/.emacs.d/init.el").
(setq initial-buffer-choice user-init-file)
In the first example, the *scratch* buffer will be empty. In the second example, the *scratch* buffer will still exist, but user-init-file will be focused.
You can customize:
initial-buffer-choice
I set it to my homedir: "~/" to start in Dired mode.
I suspect from your question that you probably start emacs fairly often, perhaps even once for each file you want to edit. (If I'm wrong in this assumption, then the following comments don't apply to you.)
Emacs is designed to be started and then left running for weeks or months while you visit various files as you need to edit them. Emacs handles multiple files very well, so it's hardly even necessary to kill the associated buffers until you get 50 or 100 of them hanging around. I start emacs just after my window system starts, and it runs until my system shuts down or crashes. The initial scratch buffer is a non-problem in this mode, because I see it so infrequently.
I use this to kill the scratch buffer and open a new buffer in text mode called Untitled.
Found it on a newsgroup and modified it slightly.
(defun my-close-scratch ()
(kill-buffer "*scratch*")
(if (not (delq nil (mapcar 'buffer-file-name (buffer-list))))
(new-untitled-buffer)
))
(defun my-emacs-startup-hook ()
(my-close-scratch))
(add-hook 'emacs-startup-hook 'my-emacs-startup-hook)
(defun new-untitled-buffer ()
"Opens a new empty buffer."
(interactive)
(let ((buf (generate-new-buffer "Untitled")))
(switch-to-buffer buf)
(normal-mode)
(setq buffer-offer-save t))
(add-hook 'kill-buffer-query-functions
'ask-to-save-modified nil t)
)
To close Untitled when opening files from filemanager when emacs is not open I use this:
(defun my-close-untitled ()
(if (get-buffer "Untitled")
(kill-buffers-by-name "Untitled")))
(add-hook 'find-file-hook 'my-close-untitled)
The proper way is to add inhibit-startup-screen to the custom-set-variables section of your .emacs file.
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(inhibit-startup-screen t)
)
I get used to emacsclient for the speedy response like vim, by putting emacs into sever mode with command "emacs --daemon". But I found it quite annoying that lots of buffers kept alive when I viewed some files and then closed them by pressing Alt+F4. I have to kill the buffer explicitly before closing the frame.
I want to know, if there is a way to make emacsclient behave more like a lightweight GUI editor(e.g. vim) in this point?
I think you're asking for trouble, but you you could try this:
(add-hook 'delete-frame-functions
(lambda (frame)
(let* ((window (frame-selected-window frame))
(buffer (and window (window-buffer window))))
(when (and buffer (buffer-file-name buffer))
(kill-buffer buffer)))))
I suggest that you use the command quit-window which does precisely what you want (with the prefix argument); it is already the binding for q in special-mode (i.e., not self-insert) buffers. You can bind it to, say, C-f4, and it will kill the buffer and the frame when you type C-u C-f4.
Do something like the following:
(defun my-kill-buffer-and-frame ()
"kill the current buffer and the current frame"
(interactive)
(when (y-or-n-p "Are you sure you wish to delete the current frame?")
(kill-buffer)
(delete-frame)))
If you're sure you always wnt to do it, you can get rid of the prompt:
(defun my-kill-buffer-and-frame ()
"kill the current buffer and the current frame"
(interactive)
(kill-buffer)
(delete-frame))
Then bind it to a key of your choice, like so:
(global-set-key [(f5)] 'my-kill-buffer-and-frame)
Enjoy!