Elisp: Buffer Menu - emacs

Every time I hit CTRL + Left click in Emacs I get a list of all opened buffers.
How can I change the default output in order to pop up a menu customized by myself, I have something like this in mind:
Buffer Menu
-----------
dot.C > ex1.c
dot.java ex2.c
dot.lisp ex3.c
dot.?
...
-----------
For instance, every .c file would be stored inside dot.c entry as showed above.

Are you saying that you'd have one menu for .c files, and another for .h files, even though they're both using the same major mode? The standard menu categorizes based on major mode, so I think you'd need custom menu code to achieve that.
A hacky approach which springs to mind would be to define minimal derived major modes in order to associate each with a different filename extension, and then you could use the existing menus to achieve your goal.
If you didn't mean that, and grouping by major mode is fine, then the current system should already provide a solution.
In either case, the following may prove useful:
How do I set the number of items in the mouse buffer menu in Emacs?
M-x apropos-variable RET mouse-buffer-menu RET

Related

Is there an Emacs keyboard shortcut for selecting between ambiguous buffer names when switching buffers

In emacs I'm already using iswitchbuffer, but I would still like to be more productive with my buffer switching.
One common case which arises a lot is that I have 6 or 7 identical or near-identical file names. For example:
server.py<proj1> server.py<proj2> server.py<proj3>... foo_server.py<proj4>
The problem is that I have to type out most of the directory context when I'm switching buffers.
An existing alternative to typing the directory context is a completion window which pops up saying:e
Click on a completion to select it.
In this buffer, type RET to select the completion near point.
Possible completions are:
... ....
but this is unsatisfactory as well since I have to the switch to the window and use the mouse to pick one.
What I'd prefer is to use tab or some other key to highlight alternative buffer selections, then enter to select one. Is there a way to do this?
In iswitchb-mode (and its successor1 ido-mode) the default binding for cycling through candidates are Ctrl+s and Ctrl+r.
You should be able to limit your candidates by typing the common bit of the filename then cycle through those candidates with those bindings.
1 See the header text in iswitchb.el and ido.el.
If you use icomplete-mode (which is a bit like iswitchb-mode but applies to all completions rather than only for buffers), you can cycle to the next/previous matching candidate with C-, and
C-..
Another option, if you use the default completion system (with or without icomplete-mode), is to type serv*1 (for example) to select server.py<proj1>.

How to open a code block using emacs org-mode in a specific window

When working with org-mode I would like to edit a code block in a specific window.
While org-mode does allow some customization with respect to which window a code block will open in, it is quite limited. The four options currently available for the org-src-window-setup variable include current-window, other-window, other-frame, and reorganize-frame. However, these do not accomplish my goal.
I would like to either be able to specify a specific window and/or use windmove-right, for instance, from the windmove package. At the moment, I can only specify one of the four options above. Also, other-window, does not actually go to the other window. Instead it opens a vertical split and creates a new window. At least in my setup.
Is there a way to alter what options the org-src-window-setup variable accepts? Or perhaps there is another solution?
You are looking for:
C-c '
which opens a new window for code editing. You insert the code in your file by saving the buffer
C-x C-s
or by closing the code buffer with
C-c '
For code block there is a shortcut (yasnipet)
<s [tab]
More: orgmode documentation

Emacs: help me understand file/buffer management

I've been using emacs for all of my text editing needs for the past three years now. When I'm in a single file, working on code or whatnot, I'm fairly efficient. With two files, I can "C-x b RET" between them and I do fine. However, whenever I need to be working on more than two files at a time, I tend to get lost.
Here are some of the problems that I'd like to work on:
I forget what some of my buffers are called, but I don't understand why C-x C-b splits my window into two buffers and exits the mini buffer. Sure I can switch buffers and choose a buffer to visit, but this feels unintuitive, and leaves me with two buffers open.
When I visit a directory rather than a file, I have a convenient list of all of the files and directories. I usually want to do one of two things with this: 1) Open a single file and never see this buffer again OR 2) Open a bunch of files and never see this buffer again. I don't really know how to do this, as moving the point to a file and hitting return doesn't do either of these things.
I know that my buffers aren't like tabs, but I have an inclination to want to scroll through them to find what I want. I don't know of any key-bindings for this, but I'd like it to be M-n / M-p or the like. Then again, this may be a horribly inefficient way to switch buffers.
When I open interactive help of any kind (for example in ESS), I have a habit of switching back to the buffer I was working in and using C-x 1 to get back to a single buffer. When I do this, however, the help buffer hangs around in my buffer list, further confusing me. I know I can switch to that buffer, kill it, switch back, and then go back to a single buffer, but this seems wrong.
The way I've dealt with this so far involves using a tiling window manager and a few emacs windows in different work-spaces, rather than actually learn the best way to manage a number of files in emacs. I don't necessarily want to change emacs to better fit my needs (although I am open to that if it fits in with what I'm about to say), instead, I'd like to grok the thought process behind handling files/buffers the way that emacs does, and how I can be more efficient with it.
Any answer that would help me understand the correct way, or a more efficient way to manage my buffers or files would be greatly appreciated.
Bind C-x C-b to ibuffer. This is a better buffer listing facility with many advanced features, and its default behaviour is to replace the current buffer with the buffer listing, and then bury the listing when you select a buffer (leaving you with the newly-selected buffer in place of the original one).
You can simply use C-x b to enter your selection in the mini-buffer, of course; however the tab-completion (which is needed to make this a viable option, IMO) does open a new window temporarily, at which point I think you might as well familiarise yourself with something with more features.
Use a instead of RET when selecting from dired. This kills the dired buffer instead of leaving it behind. C-h m in any buffer will show you the help for its major mode (followed by help for the minor modes), and you can read about all the available dired key bindings there.
http://www.emacswiki.org/cgi-bin/wiki/TabBarMode ? (edit: I prefer Rémi's answer for this one, but TabBarMode would give you the visual tab element if you were particularly keen on that.)
q is bound to a 'quit' function in a great many major modes. Generally it buries the buffer rather than killing it, but I certainly find that fine.
To elaborate a little on #1, ibuffer has lots of nice features, and M-x customize-group ibuffer RET will give you some idea of how you can customise it to your liking.
Furthermore, you can filter the buffer list by many criteria (again, use C-h m to see its help page), and then generate a 'group' definition from the current filters, and save your custom filters and groups for future usage.
For example:
/ f ^/var/www/ RET: filter buffer list to show only filenames starting with /var/www/.
/ s Web filters RET: name and save active filter set to your init file.
/ g Web development RET: create a named group from the active filters.
/ S My groups RET: name and save group definitions to your init file.
/ r Web filters RET: invoke the "Web filters" filters.
/ R My groups RET: invoke the "My groups" groups.
RET on a group name to collapse or expand it.
C-k and C-y to kill and yank groups, to re-arrange them.
C-h m for more information...
This way you can have a single Emacs instance running, and create filters and groups for different types of task, and easily switch between them.
I think you will really enjoy Ido for dealing with multiple buffers who's names you can't exactly remember. When you type C-x b it shows a list of open buffers in most used order. As you type some of the characters in a buffer name the list is filtered. The characters you type don't have to be at the begging of the name or contiguous. Using C-f, C-b or left/right arrow keys cycles through the buffer choices.
Also see Smex for Ido like functionality for M-x
Closing windows is done with C-x 0. Intentionally splitting the window is done with C-x 2 for horizontal, C-x 3 for vertical. I love this feature, since it allows me to have test and production code visible at the same time. C-x o takes me to the other window.
I use C-x right (or C-x C-right) and C-x left (or C-x C-right) to go to the next and previous buffer. I don't mind anymore off the few buffer that lay around in Emacs but you could use k in the buffer list to kill the buffer you don't use anymore.
You can also try Iswitchb mode which provides auto-completion for buffer names when you switch buffers via C-x b.
To activate:
M-x iswitchb-mode
Or add to your .emacs file:
(iswitchb-mode)
It is similar to Ido mode for buffer switching but a bit more lightweight.
Also, if you want a more customizable listing of your buffers then use M-x bs-show as an alternative to C-x C-b. In that buffer type ? to get a list of actions you can perform.
I think the number one most useful extension for flipping through buffers is Anything. It lets you start typing part of a buffer (or file!) name and it will figure out what you want. I've rebound C-x b to anything-for-buffers. It makes life so much better.
As always, there are many ways to help you with this; it depends a bit on personal preference what works best, here are some links with explanations:
ibuffer; which is an updated buffer menu (C-x C-b)
ido, which let's you have more powerful autocompletion to switch through buffers. It's a kind-of 'better iswitchb'.
These two are enough for me; but you may also be interested in the tabbar-mode, which gives you rudimentary tabs (like firefox has them).

In Emacs, how can I jump between functions in the current file?

I'd like to quickly move point to a function in my Emacs buffer. I'd like to run some function and get a prompt asking me for the function name, with completion provided for every function defined in the current buffer.
I generally use etags to navigate around, but sometimes I'm looking for a framework method that's been overridden in several files. In these cases, I can find the file I need but then I'd like to quickly jump to the function there. There is a similar feature in TextMate where you can select a definition from a list in the bottom right of the editor.
Just to jump around functions in the current file? Use imenu. It's the simplest and lightest of all the alternatives listed so far and might be enough for what you want. It's also built into Emacs and has minimum setup hassle. It features graphical and textual interfaces. Anything extra and you'll be better off using one of the other excellent suggestions made here.
speedbar comes standard, and gives you a collapsible menu for each file in the current directory, by default middle clicking on an entry for a function definition jumps to that def. With emacs23 this was changed to the more normal leftclick.
You can use etags-select to select from multiple matching tags. But the answer to what you asked is imenu.
Icicles is probably closer to what you are looking for:
http://www.emacswiki.org/emacs/Icicles_-_Tags_Enhancements
It's an enhancement to etags and includes (among other things) the file name with the tag so you can tell if it's the one you are looking for.
try CEDET. It is a bit difficult to set up the first, but here is an excellent tutorial: by Alex ott
And when he gets installed, you can use semantic-complete-jump. pressed tab couple times, and it is also brings up symbol definitions.
If M-. brings up the wrong method, you can type C-u M-. to find the next one with the same name.
global gtags is very good
To navigate within the current file or a set of files that you select, you do not need a TAGS file. You can use Imenu. But it is better to use Icicles imenu commands.
Why? Because they let you use completion. Substring, regexp, prefix, or fuzzy completion. Combine simple patterns to match, or subtract them.
Command icicle-imenu is bound in Icicle mode to C-c =. Butyou can also look up just a command or just a non-command function (non-interactive), using command icicle-imenu-command or icicle-imenu-non-interactive-function.
These commands are multi-commands, meaning that they are actually browsers: you can trip among function definitions using keys C-RET or C-mouse-2 (direct jumps) and C-down (cycle). Hit RET or click mouse-2 to settle down at a final destination.
I use C-M-a and C-M-e to jump between the beginning and end of functions.
Otherwise, open up Speedbar and click the + icon next to a file name to view a list of functions contained in the file. Then click on the function names to jump to them directly.

Emacs - Multiple columns one buffer

I'm trying to edit some assembly code which tends to be formatted in long but thin listings. I'd like to be able to use some of the acres of horizontal space I have and see more code on-screen at one time. Is there a method for getting Emacs (or indeed another editor) to show me multiple columns all pointing to the same buffer?
C-x 3 (emacs) and :vsplit (vim) are great for multiple separate views into the code, but I'd like it to flow from one column to the other (like text in a newspaper).
See follow-mode.
Excerpt:
Follow mode is a minor mode that makes two windows, both showing the same buffer, scroll as a single tall “virtual window.” To use Follow mode, go to a frame with just one window, split it into two side-by-side windows using C-x 3, and then type M-x follow-mode. From then on, you can edit the buffer in either of the two windows, or scroll either one; the other window follows it.
In Follow mode, if you move point outside the portion visible in one window and into the portion visible in the other window, that selects the other window—again, treating the two as if they were parts of one large window.
I use this function to invoke follow-mode, although it would need customization for a different screen size:
;;; I want a key to open the current buffer all over the screen.
(defun all-over-the-screen ()
(interactive)
(delete-other-windows)
(split-window-horizontally)
(split-window-horizontally)
(balance-windows)
(follow-mode t))
The "Multipager" plugin for Vim can do this with VIM splits for people who want to get this behavior in Vim.
Get it from Dr. Chip's page: http://mysite.verizon.net/astronaut/vim/index.html#MPAGE
Docs: http://mysite.verizon.net/astronaut/vim/doc/mpage.txt.html
Vim can do this using :vsplit - and you can have the same buffer open in multiple "windows" (which are actually sections within a single "window").
Documentation here
A quick look at the emacs wiki doesn't show a mode like you describe. However, it shouldn't be too hard to write one... You just need to split the window with C-x 3 and move the text in the other window down, and whenever you move the text, do the same to the other window...
Problems may occur when you get to the bottom of the buffer, do you want the cursor to immediately go to the other window at the top?
Hmm, maybe its not that easy. But it should still be doable...
this is the default behaviour of emacs when splitting the window (C-x 3 for vertical split)
you get two columns which both have the current buffer open
Use vertical-split with C-x 3. This will split the current buffer into two columns that you can switch between with C-x o.