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

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.

Related

Run a command after every input to minibuffer

I want to set up a sort of ISearch mode replacement in Emacs, where I can run a command after every input to the minibuffer. Is it possible?
EDIT 1:
The idea is to completely replace ISearch with my own mode. Ideally, I'd like it to have most of the functionality that ISearch has (like highlighting results as you type). To implement some, I'd need to run a command after every key is input. Is there a way to trigger a function when the minibuffer change, or should I use something that isn't the minibuffer?
EDIT 2:
To be more specific, basically I am looking to take a string from the minibuffer and highlight all matches in the buffer, much like in ISearch mode. So essentially, after each letter, symbol, or number inputted into the minibuffer, I'd like to be able to recognize that change and run some arbitrary elisp. Similar to how helm recognizes input and updates search results.
You wan to use minibuffer-with-setup-hook and in the setup hook, you'll want to either use post-command-hook or after-change-functions.
E.g.
(defun my-update-function (beg end len)
(let ((str (minibuffer-contents)))
<update-search-result>))
..(minibuffer-with-setup-hook
(lambda ()
(add-hook 'after-change-functions #'my-update-function))
...(read-string ...) ...)
See variable minibuffer-exit-hook.
In general, for this kind of question, look for a variable whose name ends in hook (or function or functions). Use commands such as apropos to find a variable (or function) whose name contains various substrings (e.g. -hook, minibuf).
Your question is, however, a bit unclear. Will you actually be using Isearch, or are you going to replace Isearch with something that uses the minibuffer? Please specify more precisely what you are doing.
FYI - Isearch does not use the minibuffer (except when you use M-e to edit the search string).
Update after your Edit 1:
Now I'm afraid your question is too broad, and risks being closed for that reason. You are essentially vaguely asking how to implement some kind of a replacement for Isearch.
If you want to do something after each command, see post-command-hook. But beware that nearly every key press is a command invocation.
Update after your Edit 2:
In that case, consider looking at what other libraries do in this regard. For example, highlight.el (e.g. hlt-highlight-symbol), highlight-symbol.el, color-moccur.el, Icicles (e.g. icicle-occur), and Helm (e.g. helm-swoop). Some of those, such as Icicles and Helm, provide incremental highlighting update as you describe. Others highlight a given name that is entered in the minibuffer (i.e., using RET, not just characters typed in the minibuffer).
But you might want to specify what you want that is different from what such libraries already do. Your question still seems overly broad, to me.
In general, things such as Isearch that do incremental updating read key presses and respond accordingly. They may do this from the minibuffer or at top level, but the point is that they generally do not require a user to enter something in the minibuffer, using RET. They respond after each key press (or each command invocation, e.g. via post-command-hook).

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

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

Emacs Comint History: Search Rather Than Navigate One By One

I would like to, after switching to the buffer where I usually run commands, navigate the history by searching it, rather than navigate one-command-at-a-time at the end of the buffer (e.g. C-p).
Basically I would like to "Reverse I-search" the command history at the end of the buffer, rather than search the buffer.
Did anyone code a working solution? Note that I noticed there is a Command History buffer available, but here it is just a bunch of text and it is not grouped well enough I think to use.
As in a terminal, you can use M-r to search backward. It works in comint-mode, but it also work elsewhere, like in M-x (M-xM-rpatternRET).
Yes, with Icicles.
In Icicle mode, command icicle-comint-search is bound to C-c ` in shell buffers. It gives you the behavior you are looking for. It is described here.
It uses only stuff that is in the currently visible history as candidates, however. If you want to access stuff from your history from previous sessions then use command comint-input-ring, bound to C-c TAB, instead. (This is explained in the same doc.)

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