Repeat-like copycat function for Emacs - emacs

I would like some copycat function, that takes the previous input and repeats it (like repeat), but does not get written over when something else is done, and thus remains repeatable. Anyone has any ideas?
EDIT: The way I intend this is to have some mode in which D keypress will act exactly like repeat (if some other input has been done, repeat that), while d will repeat the last thing assigned to the last D key press.
EDIT2: If I would yank, and then press C-x z (in my mode also bound to D), then it will repeat the yank. However, when I would move the cursor down, and I try to press D, it then repeats the down cursor. In this case, I would like the small d to do the behavior of the last repeat (that is, yank) while D would repeat the down cursor command. So, d would store the last repeated command, while D would repeat the last command.

This was just too long for a comment:
It feels like you essentially want a shorter version of keyboard macros? I'll try to explain briefly, and see if it is close:
C-x ( - start recording the macro.
Do whatever you want (may be just a single command). For example, yank something, i.e. M-d
C-x ) - finish recording the macro.
Now you can C-x e to replay the macro (you can do other stuff after you've recorded the macro, C-x e will do what you have previously recorded (i.e. M-d in this case).
Maybe you can create a shorthand version of start-macro end-macro recording, if you are sure there will be only one command, but these are really minor improvements. Once you get used to macros, you'll do it unconsciously, so that one keystroke saved won't matter really.
Also, if I didn't guess what you were after, this may prove to be interesting to you: http://www.gnu.org/software/emacs/manual/html_node/elisp/Command-History.html

My best attempt. It works, though it didn't incorporate all the error handling that repeat has.
(defun Navi-repeat ()
;; Checks whether the last repeatable command is the same as repeat var.
;; If yes, set repeat-navi to that command, and call it.
;; If no, check whether the Navi-repeat variable has been set before:
;; If bound, call it.
;; If not bound,
;; give it the value of the last-repeatable command, and call it.
(interactive)
(if (eq last-repeatable-command 'repeat)
(progn (setq repeat-navi repeat-previous-repeated-command)
(call-interactively repeat-navi))
(if (boundp 'repeat-navi)
(call-interactively repeat-navi)
(progn (setq repeat-navi last-repeatable-command)
(call-interactively repeat-navi))
)
)
)

Related

Search-forward typo aborts macros

Search-forward seems to not work well inside emacs macros. For example, say I want to use a macro to help replace FOO with BAR in the following string:
aoeuFOOsnutehaFOOsanotehuFOO
I might begin recording a macro, search-forward for FOO, and then hit backspace a few times and type BAR. Then I can replay this macro to replace the rest of the occurrences. Pretty simple.
Suppose I hit the wrong key and search-forward for FOOO. I hit Backspace to remove the extra O, and finish recording the macro. But when I replay it, nothing happens. FOOO is not in the document, so the macro replay is immediately aborted when the search fails.
This gets annoying in longer macros. As it is, whenever I record a macro, I have to make sure I type in the search text for my search-forwards perfectly. If I make even one mistake, I have to cancel recording my macro and start over; otherwise, the macro will just abort when I replay it.
To sum up, if you use search-forward and commit a typo while recording a macro, the macro will not replay properly because it will abort as soon as it replays your typo.
Any workarounds or solutions to this problem?
You don't have to abort the macro: The easiest thing to do is to simply finish recording the macro (typos and all) and then edit it via C-x C-k C-e (kmacro-edit-macro-repeat) to remove the typo(s).
For instance, when you call this command after defining a macro that is supposed to simply search for occurrences of foo in the current buffer but contains a typo in the search (you typed fooo instead of foo before fixing the typo), the buffer for editing it would look like this:
C-s ;; isearch-forward
f ;; self-insert-command
ooo ;; self-insert-command * 3
DEL ;; delete-backward-char
RET ;; newline
To fix the macro, delete one of the os from the third line and remove the fourth line:
C-s ;; isearch-forward
f ;; self-insert-command
oo ;; self-insert-command * 2
RET ;; newline
Note that you don't have to change self-insert-command * 3 to self-insert-command * 2, I just did that to avoid confusion.
When you're done, hit C-c C-c to recompile the macro and close the *Edit macro* buffer.
The documentation of the ding function says (emphasis mine):
(ding &optional ARG)
Beep, or flash the screen.
Also, unless an argument is given, terminate any keyboard macro currently executing.
Digging into the source we can see that all isearch-* functions that call ding do so without passing a non-nil ARG. You could redefine these functions to change the way they call ding (not sure if it would be good idea to remove the calls to ding entirely), but there is an easier way to achieve what you want:
You can make sure ding is always called with a non-nil ARG by advising it as follows:
(defadvice ding (before be-nice activate compile)
(ad-set-arg 0 t))
As documented here, the ad-set-arg macro
sets the value of the actual argument at position to value.
So what the advice does is tell Emacs to set ARG to t before running the body of the original ding function, causing ding to be nice and not terminate keyboard macros anymore.
With the advice in place, you can now transform
aoeuFOOsnutehaFOOsanotehuFOO
into
aoeuBARsnutehaBARsanotehuBAR
with a "faulty" macro that contains a corrected typo:
C-s ;; isearch-forward
F ;; self-insert-command
OOO ;; self-insert-command * 3
DEL ;; delete-backward-char
RET ;; newline
3*DEL ;; delete-backward-char
BAR ;; self-insert-command * 3
EDIT
As #phils mentions in the comments below, allowing macro execution to continue regardless of errors can lead to unwanted consequences, so use this solution with care. If you want to be able to quickly enable or disable the advice so that you can use it selectively (i.e., in situations where you are sure it won't mess things up), define it like this:
(defadvice ding (before be-nice) ; advice not activated by default
(ad-set-arg 0 t))
and add a command for turning it on and off to your .emacs:
(defun toggle-ding-advice ()
(interactive)
(if (ad-is-active 'ding)
(ad-disable-advice 'ding 'before 'be-nice)
(ad-enable-advice 'ding 'before 'be-nice))
(ad-activate 'ding))
(global-set-key (kbd "C-c a") 'toggle-ding-advice)
You can then toggle the advice by simply pressing C-c a.

repeat macro after error stops execution

My objective is to remove duplicated stanzas in xml files. I am sure I have a duplicated stanza if the first line of the stanza is found multiple times in the file.
I have created a macro that finds the first line of my stanzas through interactive search, then Ctrl-S again to go to the next occurrence. If found, I then mark the section I want to delete and delete it. My macro terminates here.
If my Ctrl-s does not find the next occurrence, my macro stops, which is exacly what I want it to do. However, when I Esc 1000 Ctrl-x e to execute my macro multiple times, when the error is found also the 1000 cycle stops. I am happy for the macro to stop, but I want to execute it again after the error. Is this possible? Or is there already a macro somewhere to remove duplicates stanzas or groups of lines from a file?
My macro:
C-s ;; isearch-forward
<Conduit ;; self-insert-command * 8
SPC ;; self-insert-command
6*C-w ;; kill-region
C-s ;; isearch-forward
C-a ;; beginning-of-line
C-SPC ;; set-mark-command
C-s ;; isearch-forward
< ;; self-insert-command
/ ;; nxml-electric-slash
Conduit> ;; self-insert-command * 8
<right> ;; forward-char
C-w ;; kill-region
Thanks
Joe
Hmm. It would be nice if Emacs provided a convenient interactive way to handle error conditions inside keyboard macros, but I suspect juanleon's answer might be your best bet at present.
A workaround in general is to not use C-s, but instead use something like M-: (search-forward "foo" nil t) RET to search for "foo" without triggering an error if it's not there. In this example there would be more to it, though.
For one-off processing, what I tend to do in these kinds of situation is generate a buffer of the results I'm interested in, and then process that with the keyboard macro.
This example is actually a bit tricky, but you could occur all the lines matching the pattern, pipe through sort | uniq --all-repeated=separate, and then eliminate the first line of each group. That leaves you with a the exact number of instances you wish to remove for each duplicate, so your keyboard macro could grab a line from that list, find the last instance of it in the original buffer, delete it, and move to the next line in the list.
If this is a common activity, a custom elisp function would seem like the way to go.
If what you want to achieve is to run a macro repeatedly regardless of errors, this would be a way to do it:
(defun repeat-macro-until-abort ()
(interactive)
(while t
(ignore-errors
(kmacro-call-macro 0))))
It will run your last macro until you hit C-g. Please notice that it won't be stopped even by reaching end-of-buffer.
There is a easy way to remove lines that match a regexp: M-x flush-lines
If you know the regexp for the stanza you want to remove, you can type C-M-% <your-regexp> RET RET and you witll iterate over the occurences chosing what to remove (type ! for removing every of them). C-M-% is the default keybinding for query-replace-regexp.

A quick way to repeatedly enter a variable name in Emacs?

I was just typing in this sort of code for Nth time:
menu.add_item(spamspamspam, "spamspamspam");
And I'm wondering if there's a faster way to do it.
I'd like a behavior similar to yasnippet's mirrors, except
I don't want to create a snippet: the argument order varies from
project to project and from language to language.
The only thing that's constant is the variable name that needs to be
repeated several times on the same line.
I'd like to type in
menu.add_item($,"")
and with the point between the quotes, call the shortcut and start typing,
and finally exit with C-e.
This seems advantageous to me, since there's zero extra cursor movement.
I have an idea of how to do this, but I'm wondering if it's already done,
or if something better/faster can be done.
UPD The yasnippet way after all.
Thanks to thisirs for the answer. This is indeed the yasnippet code I had initially in mind:
(defun yas-one-line ()
(interactive)
(insert "$")
(let ((snippet
(replace-regexp-in-string
"\\$" "$1"
(substring-no-properties
(delete-and-extract-region
(line-beginning-position)
(line-end-position))))))
(yas/expand-snippet snippet)))
But I'm still hoping to see something better/faster.
yasnippet can actually be used to create a snippet on-the-fly:
(defun yas-one-line ()
(interactive)
(let ((snippet (delete-and-extract-region
(line-beginning-position)
(line-end-position))))
(yas-expand-snippet snippet)))
Now just type:
menu.add_item($1,"$1")
and call yas-one-line. The above snippet is expanded by yasnippet!
You could try
(defvar sm-push-id-last nil)
(defun sm-push-id ()
(interactive)
(if (not sm-push-id-last)
(setq sm-push-id-last (point))
(text-clone-create sm-push-id-last sm-push-id-last
t "\\(?:\\sw\\|\\s_\\)*")
(setq sm-push-id-last nil)))
after which you can do M-x sm-push-id RET , SPC M-x sm-push-id RET toto and that will insert toto, toto. Obviously, this would make more sense if you bind sm-push-id to a convenient key-combo. Also this only works to insert a duplicate pair of identifiers. If you need to insert something else, you'll have to adjust the regexp. Using too lax a regexp means that the clones will tend to overgrow their intended use, so they may become annoying (e.g. you type foo") and not only foo but also ") gets mirrored on the previous copy).
Record a macro. Hit F3 (or possibly C-x (, it depends) to begin recording. Type whatever you want and run whatever commands you need, then hit F4 (or C-x )) to finish. Then hit F4 again the next time you want to run the macro. See chapter 17 of the Emacs manual for more information (C-h i opens the info browser, the Emacs manual is right at the top of the list).
So, for example, you could type the beginning of the line:
menu.add_item(spamspamspam
Then, with point at the end of that line, record this macro:
F3 C-SPC C-left M-w C-e , SPC " C-y " ) ; RET F4
This copies the last word on the line and pastes it back in, but inside of the quotes.

emacs multi-keystroke binding [duplicate]

This question already has answers here:
How to write a key bindings in emacs for easy repeat?
(5 answers)
Closed 8 years ago.
I'm still very new to EMACS, but are getting familiar when i'm going through the emacs and elisp manual. But right now i'm stuck on this:
Is there a simple way to bind input sequences in regexp style?
eg: the default binding for function enlarge-window-horizontally is "C-x {", is it possible to rebind it to something like "C-x ({)+" so that enlarge-window-horizontally can be called repeatedly by repeating "{" character, instead of release Ctrl key multiple times?
There is another way to archive what you desire:
The first time you want to repeat the last command, press C-x z, afterwards you may repeat your command as often as desired by just pressing z.
The advantage of this approach is that it works with every command you use and not just for a specific one.
For additional reference here is the output of C-h f
8.11 Repeating a Command
Many simple commands, such as those invoked with a single key or with
M-x COMMAND-NAME , can be repeated by invoking them with a
numeric argument that serves as a repeat count (*note Arguments::).
However, if the command you want to repeat prompts for input, or uses
a numeric argument in another way, that method won't work.
The command C-x z (`repeat') provides another way to repeat an
Emacs command many times. This command repeats the previous Emacs
command, whatever that was. Repeating a command uses the same
arguments that were used before; it does not read new arguments each
time.
To repeat the command more than once, type additional z's: each
z repeats the command one more time. Repetition ends when you type
a character other than z, or press a mouse button.
For example, suppose you type C-u 2 0 C-d to delete 20
characters. You can repeat that command (including its argument) three
additional times, to delete a total of 80 characters, by typing C-x z
z z. The first C-x z repeats the command once, and each subsequent
z repeats it once again.
The "Emacs way" is to use C-u as a prefix key. E.g. C-u20C-x{.
Having said that, it's possible to do what you ask for. However, it would require you to bind C-x { and { separately. The former would be defined like it is today, but the latter would have to look something like:
(defun my-open-brace ()
(interactive)
(if (eq last-command 'shrink-window-horizontally)
(progn
(setq this-command 'shrink-window-horizontally)
(call-interactively 'shrink-window-horizontally))
(call-interactively 'self-insert-command)))
Unfortunately, if you have many sequences ending in {, you would have to write one function to handle them all.
You can also define your own repeatable command and bind it to C-x {. You can then use it exactly as you requested: C-x { { { {..., instead of having to use C-x { C-x z z z z...
Here is what you do:
(defun your-repeat-command (command)
"Repeat COMMAND."
(let ((repeat-message-function 'ignore))
(setq last-repeatable-command command)
(repeat nil)))
(defun your-shrink-window-horizontally ()
"Shrink window horizontally.
You can repeat this by hitting the last key again..."
(interactive)
(require 'repeat nil t)
(my-repeat-command 'shrink-window-horizontally))
(define-key ctl-x-map "{" 'your-shrink-window-horizontally)
You can do this with any command you like --- use my-repeat-command to make a repeatable version of it. I do this all the time, in several of my libraries.
Write a multi repeat command for emacs by using minor mode. I name it smart-repeat-mode
https://github.com/zhsfei/emacs-ext

Emacs minibuffer message when typing C-x

Well. When I type some first keys of the key series, emacs write those keys in minibuffer after some interval of time. Like that: Typing C-x 4 will make C-x 4- visible in minibuffer.
The question is: can this be modified? I was thinking about making something like combining part of key-help (generated by C-h when type some keys) with this string.
Can interval for waiting this message be shorten too?
Is it subroutine?
Edited, new question
There is a message when I quit emacs with C-x C-c and have modified buffers, that ask me if I want to save them. How can I know that this message is here? I tried to look in (minibuffer-prompt) (minibuffer-contents) (buffer-substring (point-min) (point-max)), selecting (select-window (minibuffer-window)). Nothing gives me results.
Yes, the user option echo-keystrokes controls how much time elapses before the prefix key is shown in the minibuffer. From (emacs) Echo Area Customization:
User Option: echo-keystrokes
This variable determines how much time should elapse before command
characters echo. Its value must be an integer or floating point
number, which specifies the number of seconds to wait before
echoing. If the user types a prefix key (such as `C-x') and then
delays this many seconds before continuing, the prefix key is
echoed in the echo area. (Once echoing begins in a key sequence,
all subsequent characters in the same key sequence are echoed
immediately.)
If the value is zero, then command input is not echoed.
You can control the timing of this help message by setting suggest-key-bindings to a larger/smaller number.
(setq suggest-key-bindings 5) ; wait 5 seconds
There is no easy way to customize the behavior, you'd have to edit the C code for execute-extended-command, or use a replacement for it which also provides the help. One possibility for a replacement is the anything-complete library which has a replacement for execute-extended-command (note: I haven't tried it). It builds on top of the package anything, which is a different experience than the standard Emacs.
I wrote working version of what I wanted to implement.
To use, (require 'keylist), copy one or two last lines to .emacs and uncomment them.
As you can see through this code, I used this
(not cursor-in-echo-area)
(not (minibufferp))
(not (= 13 (aref (this-command-keys-vector) 0)))
to find out, if my minibuffer, or echo area is in use.
The difference between them is that minibuffer is used to read, and echo area is used to message something.
When you type C-x C-c cursor is placed in echo area, and value of cursor-in-echo-area is changed.
The last string (= 13 (aref (this-command-keys-vector) 0)) is the most funny. It is used to catch things like query-replace. When making raplacements, (this-command-keys-vector) shows that RET is the first key pressed, then keys of your choise(y,n). As far as I don't have key-sequences starting with RET, i am okay with this.