How to enter recursive edit mode and abort command inside it? - emacs

I used to be confident of returning to buffer from mini-buffer by just pressing C-g once, until I learned there is a "recursive-edit-mode" inside Emacs Here, which seems to make cancellation action inconsistent. Therefore, I am reading the entire section in hope of finding a more consistent / stateless command to return to mini-buffer.
However, I encountered below problems when following and trying to replicate what the manual says:
Entering Recursive-edit-mode: After "M-x query-replace RET C-r" (in Fundamental-mode), I could not replicate "a pair of square brackets" in mode-line. However, now I DO have to press C-g TWICE to return back to buffer.
Question: Does recursive-edit-mode still have square brackets now? (I am using ver 24.3)
Quitting: The manual directs me to the Quitting section for quitting command. It says Aborting with C-] (abort-recursive-edit) is used to get out of a recursive editing level and cancel the command which invoked it. So I tried it out within "M-x query-replace RET C-r", hoping one C-] will get me out from minibuffer to buffer, but it still takes 2 shots to do that.
Question: Is this normal? If yes, any other command to guarantee command abortion and return to buffer with 1 execution?
Just want to brush frequently used command such as C-g. If made consistent, it may help avoid inputs being eaten (sometimes unnoticed) by the minibuffer due to trapped inside recursive-edit-mode.
Visually confirming point each time inside the buffer makes keyboard input an closed-loop feedback process and thus consumes more attention, though.

You definitely will see square brackets in the mode line for a recursive edit -- but not for a recursive minibuffer.
C-g does act consistently, but it does different things in different contexts (always the same thing for a given context, however). That can be confusing. It can take some getting used to - a bit like it takes getting used to the various behaviors of C-g during Isearch, depending on the current search state.
If you use recursive edits or recursive minibuffers then you need to pay attention to clues wrt the current state/context. Again, this is like learning Isearch. minibuffer-depth-indicate-mode is a must, to provide feedback about what state you are in.
FWIW, beyond minibuffer-depth-indicate-mode, I use a standalone minibuffer frame, and with each recursive edit or recursive minibuffer the background hue is changed slightly, to better indicate the level etc. (The background color changes slightly also for (a) active minibuffer and (b) Isearch. I use library oneonone.el for these things.)
If you don't use a standalone minibuffer frame then you might consider highlighting the square brackets or something else in the mode line, to help you tell where you are at.
C-r during query-replace is a good example of when a recursive edit can be useful. There are a few others. But generally I do not make much use of recursive edits, FWIW. (I do, however, use recursive minibuffers a lot, because I use keys bound in minibuffer maps that do invoke commands that themselves might prompt for input etc.)
Wrt your question about query-replace with C-r: There is no minibuffer involved at all, here. And C-] after C-r does end not only the recursive edit but also the query-replace (whereas C-M-c ends the recursive edit and returns to the query-replace.
FWIW - In Icicles interactions, you might find yourself within any number of recursive minibuffers. (For example, you might use progressive completion to successively narrow the set of completion candidates, and each narrowing opens a recursive minibuffer.)
C-g always aborts the current command, or if there is none then the current minibuffer, and C-] always aborts the current minibuffer. Repeating C-g (or C-]) pops back up the minibuffer chain, a level at a time. But (in Icicles) you can use C-M-S-t (aka C-M-T) to pop directly back to the top level (exit all minibuffers).
Similarly, answering a minibuffer prompt (e.g. hitting RET or using mouse-2 on a candidate in *Completions*) exits the current minibuffer, popping up a level (to the parent minibuffer or to the top level if there is none). (In Icicles RET can optionally put you back at top level.)

Related

Emacs - How to enter recursive edit mode when searching (C-r) instead of query-replace (M-%)?

I know how to enter recursive edit mode when query-replace (M-%), but sometimes I really don't want to modify the code instead I'm just in searching. Such as when I was reading code, and from one point I saw a function call, then I want to jump to the declaration of the fucntion (C-r Enter PageList::Init), when I have read the declaration I want to jump back to the calling of the function.
How to enter recursive edit mode when searching (not only when query-replace)?
You can open a recursive edit while you are searching, if you use Isearch+. Then, C-x o is bound during Isearch to command isearchp-open-recursive-edit:
Invoke the editor command loop recursively, during Isearch.
Use C-M-c to end the recursive edit and resume searching from there.
Or use abort-recursive-edit to exit the recursive edit and cancel the previous search.
If you do not want to use Isearch+ then this is all you need:
(defun isearchp-open-recursive-edit ()
"Invoke the editor command loop recursively, during Isearch.
Use `\\[exit-recursive-edit]' to end the recursive edit and resume searching from there.
Or use `abort-recursive-edit' to exit the recursive edit and cancel the previous search."
(interactive)
(with-isearch-suspended (recursive-edit))))
(define-key isearch-mode-map "\C-xo" 'isearchp-open-recursive-edit)
If you are asking about recursive editing generally, and not just during search, then the answer is command recursive-edit. Just use M-x recursive-edit to open a recursive edit. You can also bind this command to a key, of course.
(If the minibuffer is active, then you normally cannot use M-x recursive-edit. In that case, bind a key in the minibuffer keymaps to recursive-edit. Or you can set option enable-recursive-minibuffers to non-nil, to be able to invoke M-x recursive-edit from a minibuffer.)
You enter a recursive edit by calling recursive-edit, but there is no default binding which invokes this anywhere (and doing so arbitrarily might be hazardous).
In the case of query-replace there is some special-case handling which goes on when you type C-r which saves the current match data/point/mark/buffer/window configuration before entering the recursive edit, to ensure that it can restore things when you exit from it.
This need to wrap some situation-specific custom handling around calls to recursive-edit is very common to the existing use-cases, so there might not be a safe way to invoke it generally.
My guess is that custom handling would certainly also be needed to support it during an isearch, and I see no such facility.
Do note that Emacs pushes point to the mark ring when you begin an isearch; so for your specific use-case you would simply jump to & pop the mark with C-uC-SPC
From your workflow description, I'm not sure you really need recursive edit mode.
When you do a search in emacs, the current point is saved. On my system C-r is reverse isearch. When I hit it, I'm prompted for the search string and hit enter. This takes me to the first 'hit'. Hitting C-r againi takes me the next hit etc. Once you find the text you want, provided you don't hit enter, you can read the code and then hit C-g to cancel the search. This will jump your cursor back to the point where you started the search. This is fine provided you can see all the code you want to read on screen.
When you can't read all the code on screen and you need to move around in the buffer, you have to hit enter. Once you do this, you have lost the saved point and I suspect this is where you thought of using a recursive edit mode so that you can jump back once you finish. However, this won't work quite as you want because as soon as you hit enter to select the searched for string, you will come out of recursive mode and lose the saved point.
There are a couple of ways to fix this workflow to achieve what you want. In fact, your workflow is quite a common requirement. Because of this, many programming modes already have this functionality built in. Therefore, the first thing to do would be to ensure your mode doesn't already have this - it is probably called something like jump to definition or similar.
If your mode doesn't have this support, then you can get what you want by saving the point and then jumping back to it later. This is a really handy technique. All you need to do is C-SPACE twice. Then later, after moving the point to a new locaiton, you can just do a C-u C-SPACE to jump back to that point. From the emacs manual
Instead of setting the mark in order to operate on a region, you
can also use it to “remember” a position in the buffer (by typing
‘C-SPC C-SPC’), and later jump back there (by typing ‘C-u
C-SPC’). *Note Mark Ring::, for details.

auto complete in command minibuffer of emacs

Which setting needs to be done in init.el file, which allows completing the rest of command if one hit M-x and initial letter of the command.
Infact in need something similar as ido-mode for minibuffer too
The ido-mode for the "M-x minifuffer" is called smex (smex use ido).
Available on the main package repos of Emacs. Homepage here
I think you are talking about incremental completion, i.e., having Emacs automatically complete what you type in the minibuffer, without your having to explicitly request completion (e.g., using TAB).
Incremental completion is available in Icicles, as well as Ido and IswitchB. And icomplete-mode shows you completion candidates in a similar way to Ido and IswitchB.
Icicles incremental completion has two aspects:
When buffer *Completions* is displayed and updated, showing you the candidates matching your input -- how soon that happens and what triggers updating
Whether and how much your minibuffer input is expanded (completed) to reflect the set of matching completions
Wrt *Completions* display (#1):
You can use C-# to cycle among the levels (normal, eager, off) at any time.
Normal means that *Completions* is not displayed until you ask for it, but thereafter it is automatically updated as you type/edit your input.
Eager means that *Completions* is displayed as soon as you type something that matches at least two candidates.
(There is also an option to show *Completions* from the outset, before you type anything -- useful as a kind of menu.) You can also specify how long to wait after you type or delete a character before updating *Completions*.
Wrt input expansion (#2):
Icicles is unique in expanding your input to (typically) the longest common match among all completions, even when completion uses apropos matching (that is, regexp or substring -- S-TAB), not just prefix matching (TAB).
There are 4 levels/behaviors for this expansion, plus off (no expansion):
Off -- this is like Ido and IswitchB: completions are shown, but your input is not completed
On request -- expand your input only when requested (TAB or S-TAB)
On request or sole candidate -- on request or when there is only one match
Always for TAB, on request for S-TAB -- TAB expands whenever possible; S-TAB is like previous
Always -- expand input whenever possible
C-" toggles between two of the input-expansion behaviors that you choose (a user option), and C-M-" cycles among all of the behaviors.

list of emacs commands executed

I am using emacs and the auto-newline feature is not working as expected. I have a pretty large number of customizations done to my emacs. So it would be no wonder if one of the other customizations is not what auto-line is expecting. I would like to know if there is a way to know the list of commands (list of emacs commands) executed by emacs at a particular point, for e.g. when ctrl-s ctrl-c or in my case when auto-line feature is called.
edit : I think you have misunderstood the question. I would like to know what command emacs calls 'internally'.
I believe view-lossage is what you're looking for -- M-x view-lossage, or C-h l.
If you want to know what a keystroke is bound to, consider using describe-key, which is usually bound to C-h k.
Basically at this point, you need to bite the bullet and learn some Emacs-lisp. The debugger is what you are looking for to dig further into your problem (I use edebug). It's not just about seeing what functions get called, you also need to see the values of the relevant variables when those functions are called.
If you feel you're not up to it, then you can bi-sect your init file until you find the culprit, but at that point you still need some Emacs-lisp to investigate further.
To add to what #event_jr said --
What you seem to be asking is the history of the functions called by the command you last invoked. (You speak of Emacs "internal commands", but it seems you just mean functions.)
To get that history for any given command you invoke (e.g., by a key), use M-x debug-on-entry and then enter the command name. The next time you use that command, you can walk through its execution in the Emacs debugger (hit d to step, c to continue past a step).

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

Emacs reselect region, as Vim shortcut 'gv' does

In vim, visual block can be recall by 'gv' command so that multiple commands can be applied easily. (such as, comment out, then indent, then do_something_fun).
In Emacs, how can this be achieved?
[C-xC-x] only works when current cursor position stays where previous block ended.
If previous block was changed, the closest is to go through 'point-to-register' and 'jump-to-register'.
Just I am curious if there is an Emacs built-in command making this in one shot.
If Transient Mark mode is off, the region is always active. If it's on (which it sounds like is your situation), you can set mark-even-if-inactive to non-nil to allow region commands to work while the region isn't highlighted.
However, note you also can cycle back through previous mark positions using C-u C-SPC -- this will pop the mark ring. Once you're back to where you want to be, C-x C-x will rehighlight the region you want. (It may take a little bit of playing with this feature to get a feel for it, but it's why I can't switch away from Emacs now.)
If I understand correctly what you are asking for, then you don't need to do anything. When you select a region in emacs, it stays selected until you select a new one. So you could select the region and then perform as many actions as you want.
Sounds like you're looking for the secondary selection, which stays put even as the region might change. (It stays put until you move it.)
See:
the Emacs manual, node Secondary Selection
Emacs wiki page Secondary Selection
library second-sel.el:
Also narrow-to-region (CTRL-x n n ) applies every command from then on just to that region- you can't hurt the rest of the buffer, it doesn't even show. After done editing , widen (CTRL-x n w )to get back the whole buffer.
CMM
If you use evil-mode, just press gv like in vim.
Since the answers here and for other similar SO questions didn't help for me (CUA-mode, Emacs 24, not only indent-rigidly), I continued searching and finally found a reselect-last-region defined in this collection of custom function (starting line 670). That worked like a charm for me - and hopefully does for others still arriving here.