how to stop Emacs from opening new buffers when following a link in customization - emacs

The problem occurs when customizing options in Emacs. Every time I click on a link a new buffer is created. How to force Emacs to use single buffer?

Try this:
(defadvice custom-buffer-create (before my-advice-custom-buffer-create)
"Exit the current Customize buffer before creating a new one, unless there are modified widgets."
(if (eq major-mode 'Custom-mode)
(let ((custom-buffer-done-kill t)
(custom-buffer-modified nil))
(mapc (lambda (widget)
(and (not custom-buffer-modified)
(eq (widget-get widget :custom-state) 'modified)
(setq custom-buffer-modified t)))
custom-options)
(if (not custom-buffer-modified)
(Custom-buffer-done)))))
(ad-activate 'custom-buffer-create)

As an alternative to my original answer (which I am not inclined to use myself), I thought I might suggest other ways in which you might deal with getting rid of lots of Customize buffers once you have finished customising things.
First, do note that simply pressing q will "Exit current Custom buffer according to `custom-buffer-done-kill'" (i.e. either bury it or kill it).
Second is to use M-x kill-matching-buffers RET \*Customize RET (and then confirm each one), but that's a bit tedious.
What I'd actually do is use ibuffer.
If you don't use it already, I recommend binding C-x C-b to ibuffer, which is a greatly enhanced alternative to the default list-buffers. I love it primarily for its filtering and grouping abilities, but it can do a great deal besides that.
(global-set-key (kbd "C-x C-b") 'ibuffer)
I also use the advice which can currently be found here at the Emacs Wiki so that ibuffer always opens with the buffer I came from selected.
That done, and from a Customize buffer, C-x C-b * M RET D y will kill all the Customize buffers. That is:
C-x C-b Open ibuffer (with point on the current Customize buffer entry)
* M Mark buffers by major mode...
RET ...select the mode (which defaulted to the major mode of the selected buffer, or otherwise type Custom-mode RET)
D y kill all marked buffers
Try it out; you'll probably like it. Searching for ibuffer will turn up other handy uses.
For instance, you could use / n ^\* RET / g tmp RET to separate out all buffers starting with * into a "tmp" group, so that they don't clutter up the group of buffers you are more likely to be interested in.
As with any major mode, use C-h m to read the built-in documentation.

Related

How can I get back the "natural" order in the Buffer List?

With C-x C-b the buffer list is displayed. First in it's natural order with most recently used buffers on top, and buried buffers at the bottom.
There, I can now sort the Buffer by name, size, mode and file. But once I click on such an option I cannot go back to the original ordering.
Also killing the buffer and recreating it does not change that order. (Using 25.2)
So how can I get that ordering back without restarting emacs?
There is another mode that's nowadays built-in to Emacs that can be used to display the buffer list: ibuffer-mode.
If you are not using it already, you can experiment with M-x ibuffer and find out its capabilities with C-h m. Note that in particular, it can sort the buffer list in various ways, one of which is by recency with s v, which is what the OP asked for; but note also that it has many other ways to sort that make it very flexible.
Once you are convinced that that's the way to go, you can redefine the C-x C-b keybinding in your init file with:
(global-set-key (kbd "C-x C-b") 'ibuffer)
I did that a long time ago and have never looked back. IMO, it should be the default: that may come to pass, but AFAIK that is still not the case.
Sorting order goes away as expected when I kill buffer named *Buffer List* and reopen using C-x C-b (Using emacs 27.1 & 28.1). From the EmacsWiki
Once sorted, there is no nice way to restore the default MRU sort. You
have to ‘kill-buffer’ the buffer menu buffer, and then re-open it.
(Sorting sets the variable ‘tabulated-list-sort-key’ in
tabulated-list.el. Its default is ‘nil’. No way to restore the nil
value is provided.)
May be worth adding a custom function which can disable the sort without closing the buffer.
(defun disable-buffer-sort()
(interactive)
(save-excursion
(set-buffer (get-buffer "*Buffer List*"))
(setq tabulated-list-sort-key 'nil)
(revert-buffer)
))
(disable-buffer-sort)
As #NickD mentioned in other answer ibuffer-mode is also a good alternative with more features.

How to enable auto-complete in Emacs Org-babel?

I would like to enable auto-complete for Babel code blocks in org-mode:
#+begin_src emacs-lisp
(setq ) <--- language-aware auto-completion here
#+end_src
What do I need to add to my .emacs file in order to configure auto-complete to do this?
Late to the party but today the default (and recommended way without other hacks) is to switch to the dedicated elisp buffer for that "hunk" using 'org-edit-special which today is mapped to
C-c- '
Hit the same to return to your org file editing.
You can switch to a dedicated session with the right mode and auto-completion
simply with C-c C-v z when you are in a code block.
C-c C-v z or C-c C-v org-babel-switch-to-session-with-code
Check the org-documentation 14.11 Key bindings and useful functions for more information.
The most robust (and entirely not org-mode specific) way to do this involves an indirect buffer. Here's a blog post that explains indirect buffers in depth. Basically an indirect buffer mirrors the contents of a section of another buffer.
(defun narrow-to-region-indirect (start end)
"Restrict editing in this buffer to the current region, indirectly."
(interactive "r")
(deactivate-mark)
(let ((buf (clone-indirect-buffer nil nil)))
(with-current-buffer buf
(narrow-to-region start end))
(switch-to-buffer buf)))
At this point, you will have a new buffer that contains the region you previously made. You can enable a major mode for that buffer and edit to your satisfaction--the changes you make are (like any good mirror should do) reflected in the original document.

How to clear the Emacs buffer history?

When I press C-x b (ido-switch-buffer) I get a lot of buffers that I don't want to see. I'd like to clear the buffer history.
I tried evaluating this expression (using M-x eval-buffer):
(setq ido-buffer-history '())
And it took effect; I can tell because I looked at the variable with C-h v ido-buffer-history. However, the change did not get reflected in the minibuffer when I press C-x b.
What else should I do? Are there other variables I should be clearing?
UPDATE : The 'extra' buffers that I'm seeing are not active. Interestingly, C-x C-b (ido-fallback-command) shows exactly what I would expect. It is the buffer history that I'm interested in. (See the buffer-name-history and ido-buffer-history variables for more context.)
Note: Perhaps it will help to mention that I'm using the emacs-starter-kit which has ido-ubiquitous installed.
Add the following to your init.el: (setq ido-use-virtual-buffers nil)
For posterity:
Those are all the active buffers in your session. You can clean them with the following commands:
M-x clean-buffer-list will close any clean buffers you haven't used in a while
M-x kill-some-buffers will visit each buffer and ask if you want to close it
M-x kill-matching-buffers will prompt for a regex over buffer names, which you can just leave blank
Edit:
There's also the boring old buffer menu you get with C-x C-b. In this menu, you can hold d until it marks each buffer for deletion, then press x to commit.
Thanks to Chris, I learned about ido's virtual buffers. I don't want to disable ido-use-virtual-buffers altogether. I want to clear the history as needed; these commands accomplish that goal:
(setq ido-virtual-buffers '())
(setq recentf-list '())
(Note that clearing ido-virtual-buffers was not sufficient -- recentf-list also must be cleared.)
I found this entry on emacswiki.
The variable that stores the buffer history you are referring to is buffer-name-history
If you run M-x describe-variable RET buffer-name-history RET you will see all of the dead buffers that no longer really exist. The wiki recommends creating a hook that removes the buffer name from the list whenever you kill a buffer.
I just did: M-x eval-expression RET (setq buffer-name-history '()) RET
This seems to have worked. The next time I ran C-x b I only cycled-through my real buffers.
That said, setting the variable to nil seems to completely disable the functionality (the variable doesn't seem to be re-populated once I open more buffers).

Close all temporary buffers automatically in emacs

How can we close temporary buffers which are enclosed with * automatically. For e.g. messages, completions buffer needs to be closed. Killing all these buffers manually after use is painful.
Is there a way to close temporary buffers created by emacs (not by us)?
Do you really need to close those buffers? If you use a proper buffer switching method like iswitchb then you don't have to care about temporary or other buffers, because you can go directly to any buffer you want.
I'd second the suggestion you use ido or iswitchb to avoid being bothered by temporary buffers. The presence of those buffers is a natural consequence of using emacs, so don't try to swim upstream!
On the other hand, if you're irritated by the growing list of open buffers, you can use midnight.el to automatically close inactive buffers after a period of time, or you can use ibuffer to easily select and close unwanted buffers en masse.
Personally, I leave buffers open for a long time, I tidy them up occasionally using ibuffer, and I rely on ido to switch buffers quickly. In Emacs 24, you can set ido-use-virtual-buffers to t, and then ido will let you switch to closed files, reopening them as necessary.
To avoid having those buffers in your way, you could define key-bindings to cycle through «user buffers» and «useless buffers» :
http://ergoemacs.org/emacs/effective_emacs.html , section «Switching Next/Previous User Buffers»
but some useful buffers start with a *, like shells, compilation buffer, ielm, etc.
As user said, it would be better to use a smart buffer-switching package such as iswitchb and ido. iswitchb's iswitchb-buffer-ignore and ido's ido-ignore-buffers variables allow us to specify what buffers to ignore using regular expressions.
However, if you really want to kill those buffers, a program like this will be helpful to you:
(require 'cl)
(defvar kill-star-buffers-except
'("\\`\\*scratch\\*\\'"
"\\`\\*Messages\\*\\'"
"\\` \\*Minibuf-[[:digit:]]+\\*\\'"
"\\` \\*Echo Area [[:digit:]]+\\*\\'")
"Exception list for `kill-star-buffers'")
(defun kill-star-buffers ()
"Kill all star buffers except those in `kill-star-buffers-except'"
(interactive)
(mapc (lambda (buf)
(let ((buf-name (buffer-name buf)))
(when (and
;; if a buffer's name is enclosed by * with optional leading
;; space characters
(string-match-p "\\` *\\*.*\\*\\'" buf-name)
;; and the buffer is not associated with a process
;; (suggested by "sanityinc")
(null (get-buffer-process buf))
;; and the buffer's name is not in `kill-star-buffers-except'
(notany (lambda (except) (string-match-p except buf-name))
kill-star-buffers-except))
(kill-buffer buf))))
(buffer-list)))

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))))