reddit / emacs
Can I collect the text I wrote last in a buffer via Elisp?
AndreaSomePostfix · 2022-02-06 23:49
Say that I left my computer inactive for a minute, then I write something like "What a fantastic day!" in the middle of the buffer. Is there a function in Emacs that returns just my last typed text (in the example "What a fantastic day!")?
I was hoping that `undo` relies on some sort of recording of user action, but I have not found much so far! Do I need to write my own code for this feature?
ztest123 · 2022-02-07 01:49
You could make it so that after a bit of inactivity it will set the mark at the cursor position, then when you type "what a fantastic day" and hit C-w it will kill the text (and you can extract it from the kill ring).
karthink · 2022-02-07 02:16
Needs some work but here's a start:
(defun replay-recent-keys ()
(interactive)
(kmacro-push-ring)
(setq last-kbd-macro (recent-keys))
(kmacro-call-macro 1))
This plays back all keystrokes, though, not just `self-insert-command`, so I'd be careful. You could also limit how many recent keys it plays back to be safer.
EDIT: Just realized there's a more developed version of this available in [dot-mode](https://melpa.org/#/dot-mode).
7890yuiop · 2022-02-07 08:33
This requirement is under-specified, IMO.
For example, what is "the last typed text" if the user types more than just self-inserting characters? What if they delete something? What if they type something in one part of the buffer and then immediately jump to another part of the buffer and type something else? What if they do that across multiple buffers? Specifically what are you trying to capture?
AndreaSomePostfix · 2022-02-07 21:21
Sorry, I would like to type something at a random place in a buffer and then be able to kill that text and use it somewhere.
Think like: I am programming, suddenly an idea pops in, I pause a moment, I start writing the idea; once finished THEN I collect that last text and make a note of it
Does this example help?
AndreaSomePostfix · 2022-02-07 21:23
That is a cool approach indeed, I think I will go this way :)
AndreaSomePostfix · 2022-02-07 21:24
Thanks for sharing this: I really was thinking just about capturing some text, not running past commands. Although this is quite cool and I think I can use for something else: thanks!
karthink · 2022-02-07 21:30
If you want to just capture text, you can modify `(recent-keys)` to `(recent-keys 'include-cmds)`, then filter the list so only `self-insert-command`s remain. Concat that into a string and return it.
7890yuiop · 2022-02-07 22:54
It does clarify things, but I don't think your desired solution is the right one. What you really want (IMO) is a command (which you can bind to a key sequence) which will immediately create a new buffer for you to type your note into, and then file it somewhere when you're done, and dismiss the temporary buffer.
That's also what `org-capture` does, so I suggest you try that.
AndreaSomePostfix · 2022-02-08 15:04
Thanks, I already use that. I was just recently pointed to embark and I wondered if we could turn around things: usually you run org-capture and then write text, but this is an obstacle to your idea. Imagine you messed up your config and org-capture doesn't work: isn't your precious idea going to be lost due to the command not working?
The embark way is reasonable to me: input first and then command. I just wanted to see if it works for notes too :)
7890yuiop · 2022-02-08 21:42
If org-capture doesn't work because you broke your config, why would you expect any other custom code to work any better? But in any case you don't *have* to use org-capture; you could write your own trivial command that does the basics without involving any of org-mode. I just think the idea that Emacs should *intelligently determine which parts of your recent input actually constitute a note that has nothing to do with the rest of the text in the buffer* is in general an incredibly difficult and 100% unnecessary complication.
AndreaSomePostfix · 2022-02-08 21:53
Thanks for sharing your opinion. I am not sure it makes sense myself: I just want to try out.
In the meanwhile I learned a couple of tricks thanks to this amazing community, so the head scratching was still worth :D
7890yuiop · 2022-02-08 22:17
Fair enough :)