reddit / emacs

Advice on using Embark and Consult - search in other buffer

shryzr · 2022-02-09 20:32
Hi, I would appreciate advice about the following. I am currently using Embark, Consult, Orderless (and Marginalia). Desired action: I have a current buffer open that is 'A'. I want to switch to a different buffer 'B' (which may be anywhere), but I actually want to search buffer B and maybe copy some results over to the current buffer. This could be using consult-line or occur. How I am going about the above: 1. switch buffer (consult-buffer) 2. Hit embark-act on the desired buffer (bound to `C-.` as default) 3. Choose either C-s (embark-isearch) or C and l for consult-line 4. However - the search chosen above always takes place on the current buffer A. I want to act on Buffer B that I chose in the minibuffer. My config for these packages is pretty vanilla, mostly borrowed off the README's of these packages with some functions stolen from the authors of the packages.
shryzr · 2022-02-09 20:57
Okay I tried the following as a variation and it seems to .. work. 1. Switch buffer (consult-buffer) 2. Now call projectile switch project or switch buffer or any command while in minibuffer (my projectile switch project is connected to helm-projectile) 3. Choose a project, and file 4. Copy / Search / Occur - do whatever 5. The original minibuffer with the switch buffer command is still open. Navigate to this and Cancel or switch to some other buffer altogether. If I cancel I am back in buffer A after having navigated anything at all including buffer B. This appears fantastic though I suppose there are better ways to go about. I am starting to learn about embark-collect - but perhaps what I need to collect a single file/buffer and act on that?
wargxs · 2022-02-09 21:17
But why? Why not switch to the desired buffer, do isearch and whatever, then switch back - the pervious buffer should be the default choice, so just hit enter? I don’t understand what the advantage of using embark ist.
oantolin · 2022-02-09 21:30
First, let me explain what happens with the recipe in your post. The `embark-isearch` and `consult-line` commands, when used as Embark actions, will search in the buffer you started from (your buffer A) for the thing you are acting on, in this case the *name of buffer B*. Now, for your question what's wrong with the straightforward (embark-less!) method? 1. Use `consult-buffer` to switch to buffer B. 2. Now that you are in buffer B, run your search, maybe copy some results to the kill-ring. 3. Use `consult-buffer` again to switch back to buffer A. Here's an alternative that uses a little feature of `embark-insert`: 1. Use `consult-buffer-other-window` to open buffer B in another window. 2. Now use `read-only-mode` (bound by default to `C-x C-q`) to put buffer in B in read only mode. 3. Move around buffer B however you want, to look for the stuff you want to insert in buffer A. When you find the stuff, use `embark-act` + `embark-insert`. Since the current buffer is read only, this inserts it into buffer A instead! Or if you want buffer B only displayed temporarily: 1. Run `consult-buffer` and select buffer B, but don't press RET, so the minibuffer stays open. If you have not turned Consult preview off, buffer B will be displayed. 2. Use `other-window` to move from the minibuffer to buffer B. 3. Search for the stuff you want and copy it to the kill-ring. 4. Now quit the minibuffer by using `abort-recursive-edit` (bound by default to `C-]`), this should restore the previous window configuration showing buffer A again.
shryzr · 2022-02-09 22:40
Thanks very much u/oantolin for taking the time to respond and of course for your awesome packages :) I see what you mean now. It looks like my earlier comment describes a version of your alternate procedure via recursive edit for a temporary view (sort of), though I did not know what I was doing. Is this recursive edit enabled by.. vertico/embark or was always standard Emacs? The `embark-insert` tip is excellent - I think this is what I actually wanted to do. :) There is nothing wrong with the straightforward method and of course I use that. It's just that 'buffer B' might be 'a thing' somewhere in some project, or might require a search through a directory. It might not exist at all. Often, in the end - I cannot remember where I started and what I was doing. I suppose the recursive edit with the original command open is the key - to me it is new and very pleasant.
oantolin · 2022-02-09 22:51
> Is this recursive edit enabled by.. vertico/embark or was always standard Emacs? Standard Emacs! To make recursive editing even more useful you can try: (setq enable-recursive-minibuffers t) This way you can even use new minibuffers in the recursive edit without quitting the old minibuffer. If you do use recursive minibuffers you might also like an indication of how many minibuffers deep you are, for that you can use `(minibuffer-depth-indicate-mode)`, which puts a little indicator in square brackets to the left of the minibuffer prompt. I use recursive minibuffers all the time, but almost always only to level 2. It must take some pretty fancy workflow to need depth 3 or higher!
zhyang11 · 2022-02-09 23:07
I recommend to set up (single-stroke) key binding for `previous-buffer` and `next-buffer`. Really convenient for going back-and-forth between 2 buffers.
karthink · 2022-02-09 23:57
> Since the current buffer is read only, this inserts it into buffer A instead! Whoa, didn't know this. What's the reasoning behind this?
oantolin · 2022-02-10 00:06
Well, I wanted a behavior more useful than printing an error message, "Buffer is read-only". :) So what `embark-insert` does is that if the target buffer is read-only it uses the buffer displayed in `(other-window-for-scrolling)`, that is in the window that `C-M-v` would scroll. I find this pretty safe because (1) it's a buffer that's visible, (2) I think most users have a good sense of which window would be scrolled by `C-M-v` at any given time. I was mainly thinking of looking things up in the help or info manuals. Often, after reading the help to make sure that's the function I really want, the next step is to insert the function name into the buffer I was in before, so it's handy to use `embark-insert` for that. For what's it worth, no-one has ever complained about this unorthodox behavior. ;)
oantolin · 2022-02-10 00:22
My Chromebook has back and forward keys meant for a web browser. In Emacs bind them to previous-buffer and next-buffer. :)