How can I toggle the appearance of the *Compilation* buffer? - emacs

I've recently switched to using emacs.
One of the things that I want to be able to do is to toggle the appearance of the compilation buffer using Ctrl+, (i.e. when the compilation buffer is visible on the screen, I want to be able to press Ctrl+, to hide it, and then press Ctrl+, to bring it back up).

Here is a function modified from Spacemacs:
(defun toggle-compilation-window ()
"Show/Hide the window containing the '*compilation*' buffer."
(interactive)
(when-let ((buffer compilation-last-buffer))
(if (get-buffer-window buffer 'visible)
(delete-windows-on buffer)
(display-buffer buffer))))
(bind-key "C-," #'toggle-compilation-window)
I also suggest taking a look at this awesome package popper

Related

How to move an emacs buffer to a new window?

I have many buffers open in an Emacs window. I want to move one of the buffers to a new window. Is there a command which does that?
The command you are looking for is tear-off-window. Note that this command must be associated with a mouse click event.
For example, you can put the following code (from this reddit comment) in your init file (more about init file here):
(global-set-key [mode-line C-mouse-1] 'tear-off-window)
This will call tear-off-window when you Control-click on the buffer modeline.
If you want to use a keyboard binding, the modified version of tear-off-window is below. Put it into your init file to save it after restarting emacs.
(bind-key "C-x C-X" #'my/tear-off-window)
(defun my/tear-off-window ()
"Delete the selected window, and create a new frame displaying its buffer."
(interactive)
(let* ((window (selected-window))
(buf (window-buffer window))
(frame (make-frame)))
(select-frame frame)
(switch-to-buffer buf)
(delete-window window)))
In the code, the modified command is bind to "C-x C-X". You are free to change it to any other key sequence (more details here).
IIUC, you want to create a new WM window.
Emacs uses a slightly different terminology: what are usually called "windows" in a GUI environment, Emacs calls "frames". WIthin a single frame, Emacs subdivides its area into separate "windows" (IOW, even in a non-GUI environment, Emacs acts as a "tiling" window manager). So, you can create a new frame with C-x 5 2 (or equivalently M-x make-frame-command RET) and then in that frame, switch to the required buffer with C-x b <buffer-name> RET.
Note by the way, that you don't "move" the buffer to the new frame: the buffer exists independently of whether there is a (emacs) window in a frame that displays the buffer.

Is there a way to fix window buffer in emacs for cider error/repl

Using emacs 24.4, with clojure mode and cider-jack-in. Anytime I evaluate a wrong exception the error buffer randomly replaces buffers in any other screen splits. Now I am looking for some configuration in init.el which help me to configure something like this:
When clojure mode is selected/or I opened a .clj file, cider-jack-in started by default.
The screen should split in 4 parts 1 current buffer/file i opened, 1 more scratch buffer, repl and clojure error msg buffer.
I have two settings in my init files related to similar requirement:
(add-to-list 'same-window-buffer-names "<em>nrepl</em>")
same-window-buffer-names is a built-in feature of Emacs.
The other one is a helper function I use that harnesses the sticky-windows extension.
;; Toggle window dedication
(defun toggle-window-dedicated ()
"Toggle whether the current active window is dedicated or not"
(interactive)
(message
(if (let (window (get-buffer-window (current-buffer)))
(set-window-dedicated-p window
(not (window-dedicated-p window))))
"Window '%s' is dedicated"
"Window '%s' is normal")
(current-buffer)))
It's not a full answer to your question, but hopefully a good starting point :)

how to show buffer content in real time in other window when focus is in buffer list

I have 2 windows, one is the buffer list, how do I show the buffer in the buffer list in the other windows when I use n and p to naviage in the buffer list?
Thanks a lot.
If I understand the question right, the answer is simply to use C-o in the buffer menu. that displays the buffer of the line you are on in another window, but it does not select that buffer. That is, it does not move the input focus to that buffer's window. The buffer list retains the input focus, so you can continue to use C-o on different lines, to display different buffers.
If you mean an Emacs window when you say "frame", then see above. The same is true even if the buffers are on different frames, with this caveat: Some window managers, including MS Windows, change the input focus to a new frame that is created. So if, for example, you have non-nil pop-up-frames (so that a separate frame is used to display a buffer), and if the buffer you choose to display (using C-o) is not already shown in some other frame, then displaying it not only creates a new frame for it but also shifts the focus to that new frame. If the buffer is already visible in another frame then C-o simply raises that frame.
There is currently no key bound in the buffer-list display, to both (a) move the cursor down or up to the next or previous buffer line and (b) invoke the C-o behavior of displaying the buffer named on the target buffer line. But you could easily define such a command and bind it to a key:
(defun show-next (arg)
"Show next line's buffer in another window."
(interactive "p")
(next-line arg)
(Buffer-menu-switch-other-window))
(defun show-previous (arg)
"Show previous line's buffer in another window."
(interactive "p")
(previous-line arg)
(Buffer-menu-switch-other-window))
(define-key Buffer-menu-mode-map "\M-n" 'show-next)
(define-key Buffer-menu-mode-map "\M-p" 'show-previous)
Here's my prototype for this feature in a dired buffer. It kills a buffer after it got visited.
This is a feature I like from the ranger file manager, it is handy when you explore a directory.
(setq show-next-current-buffer nil)
(defun show-next ()
(interactive)
(next-line 1)
(dired-find-file-other-window)
(if show-next-current-buffer (kill-buffer show-next-current-buffer))
(setq show-next-current-buffer (current-buffer))
(other-window 1)
)
(defun show-previous ()
(interactive)
(previous-line 1)
(dired-find-file-other-window)
(if show-next-current-buffer (kill-buffer show-next-current-buffer))
(setq show-next-current-buffer (current-buffer))
(other-window 1)
)
(define-key dired-mode-map "n" 'show-next)
(define-key dired-mode-map "p" 'show-previous)
edit: I've written a minor mode to enable/disable this feature easily. See https://gitlab.com/emacs-stuff/my-elisp/blob/master/dired-show.el and meld it to your needs.

Emacs ido start with open buffers first in cycle list

When switching buffers with emacs ido mode enabled, a list of completions are displayed in the minibuffer. It appears there is a "feature" that buffers that are already open are put to the end of the list. I, however, often open the same buffer in multiple panes.
Is there a way to either turn this "feature" off, or alternatively do the opposite: have the buffers that are already open be at the front of the completion list?
The main point of ido mode is that you don't use arrows to navigate between buffers in the minibuffer. Instead you type the part of the buffer's name. In this case it doesn't matter where the buffer is in the list.
This is not possible unless you want to wade deep in ido's intestines.
As eGlyph already said: You're likely using ido wrongly (and there's also C-s for <right> and C-r for <left>; no need for arrow keys).
But you can define command for choosing among the already shown buffers (here only from the current frame, if you want all shown buffers you have to collect the windows first via `frame-list):
(defun choose-from-shown-buffers ()
(interactive)
(let ((buffers (mapcar (lambda (window)
(buffer-name (window-buffer window)))
(window-list))))
(pop-to-buffer (ido-completing-read "Buffer: " buffers))))

Dedicated window for dired mode in Emacs?

I have emacs behaving more or less how I want it to by using this common bit of elisp:
(defun toggle-current-window-dedication ()
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(global-set-key [pause] 'toggle-current-window-dedication)
Unfortunately, dired uses the directory for the buffer name, so dedicating a dired window only dedicates it to that directory. Once you navigate up or down, it opens a new buffer in a separate window. What I would like to do is dedicate a window to a major mode (dired in this case), and have all new buffers that default to that mode prefer that window. Is this possible?
Try using your code in combination with dired-single, which will cause all dired navigation to happen within a single buffer named *dired*. In the interests of full disclosure, I wrote dired-single.
set-window-dedicated-p forces Emacs to only show that window for that buffer, the other dired buffers cannot use the same window. See the *info* page for set-window-dedicated-p:
`display-buffer' (*note Choosing
Window::) never uses a dedicated
window for displaying another buffer
in it.
Perhaps one of the packages on the wiki page for DiredReuseDirectoryBuffer provides the functionality you're looking for...