"Press" a key from inside an interactive function - emacs

I need to programmatically press a key from inside of an interactive function. Here's an outline of what I have so far:
(defun answer-to-life-the-universe-and-everything ()
(interactive)
(insert "(* 6 7)")
;; Need to automagically press the RETURN key here
)
My use case: in a REPL buffer, I need frequently execute a long command. I can use the above code to create an interactive function that inserts the required string, but I still have to hit RETURN manually for the REPL to read it. Terminating the string with \n or \r won't do what I need it to.
How can I do this inside of my interactive function definition?

A simpler way to do this is to find out what command the enter key is bound to in the REPL and then call that command in your interactive function. (To find out, go to the REPL buffer and hit C-h k <return>.)
For example, enter is bound to inferior-ess-send-input when using the R REPL via ess, so this command inserts the string and "hits enter":
(defun try-this ()
(interactive)
(insert "print(\"hi\")")
(inferior-ess-send-input))

Related

Emacs I want to call execute-kbd-macro from a lisp function

I want to call a keyboard macro from a Lisp function. I hope to layer in some custom error handling.
mykey is a keyboard macro stored in a text file in (fset ...) format.
I loaded it with load-file and it works fine when called with M-x mykey.
When I execute this function and plug in mykey I get only the name of the key displayed in current buffer, not it's execution. Is there a step I'm missing?
(defun gn-batch-search (key-name)
"Execute a keyboard macro that has already been loaded."
(interactive "sName of macro key:")
(execute-kbd-macro key-name))
Try
(defun gn-batch-search (key-name)
"Execute a keyboard macro that has already been loaded."
(interactive "sName of macro key:")
(execute-kbd-macro (intern key-name)))
The issue you bumped into is that the "sName of macro key:" interactive spec prompts the user and returns a string, and you want to run the command whose name is described by this string. That explains why it didn't do what you wanted and why you need intern.
As for why it did what it did: a keyboard macro is represented as a vector of events, where events can be things like mouse clicks or key presses. And as it turns out, a string is considered as a kind of vector (a vector characters) and a character is also an event (it represents the event that is sent when you press that character on your keyboard), so the string "hi" is a valid keyboard macro which represents the act of pressing h followed by pressing i, so when you run this macro, it will (usually) end up inserting "itself" in the current buffer (except in special buffers like dired, *Help*, ... where h and i are bound to other commands).
Also, rather than execute-kbd-macro you can use command-execute which will work with "any" command, whether it's defined as a keyboard macro or a normal function.

Binding a key to a command and automatically using the (interactive) default values for its interactive arguments

I would like F5 to switch to the most recently used buffer. This functionality is accomplished by running M-x icicle-buffer and then hitting enter to not specify the buffer I want to switch to -- (the default behavior of icicle is to switch to the most recent buffer.)
I have tried editing my .emacs thus:
(defun most-recent-buffer-please ()
(interactive)
(icicle-buffer " "))
(global-set-key [(f5)] 'most-recent-buffer-please)
but when I evaluate this lisp, and then hit F5, I get an error that starts with Wrong number of arguments followed by a lot of gibberish characters. What am I doing wrong?
A function can have mandatory and/or optional arguments, or no arguments at all. When writing elisp, it is usually a good idea to find out what arguments are available for certain functions by typing M-x describe-function RET [name of the function] RET. Drew (the author of Icicles) has indicated in his comment underneath the original question that the function icicle-buffer is not designed to be used in conjunction with any arguments -- therefore, adding " " causes the error message that the original poster experienced.
To switch to the previous buffer, Emacs already has a built-in function called previous-buffer. Since the original poster has indicated a preference for the f5 key, the following is an example of how to configure that keyboard shortcut so that it triggers previous-buffer -- brackets are sufficient and the parentheses used by the original poster around f5 can be omitted:
(global-set-key [f5] 'previous-buffer)

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

A basic function for emacs

I have never written an emacs function before and was wondering if anyone could help me get started. I would like to have a function that takes a highlighted region parses it (by ",") then evaluates each chunk with another function already built into emacs.
The highlighted code may look something like this: x <- function(w=NULL,y=1,z=20){} (r code), and I would like to scrape out w=NULL, y=1, and z=20 then pass each one a function already included with emacs. Any suggestions on how to get started?
A lisp function is defined using defun (you really should read the elisp intro, it will save you a lot of time - "a pint of sweat saves a gallon of blood").
To turn a mere function into an interactive command (which can be called using M-x or bound to a key), you use interactive.
To pass the region (selection) to the function, you use the "r" code:
(defun my-command (beg end)
"Operate on each word in the region."
(interactive "r")
(mapc #'the-emacs-function-you-want-to-call-on-each-arg
;; split the string on any sequence of spaces and commas
(split-string (buffer-substring-no-properties beg end) "[ ,]+")))
Now, copy the form above to the *scratch* emacs buffer, place the point (cursor) on a function, say, mapc or split-string, then hit C-h f RET and you will see a *Help* buffer explaining what the function does.
You can evaluate the function definition by hitting C-M-x while the point is on it (don't forget to replace the-emacs-function-you-want-to-call-on-each-arg with something meaningful), and then test is by selecting w=NULL,y=1,z=20 and hitting M-x my-command RET.
Incidentally, C-h f my-command RET will now show Operate on each word in the region in the *Help* buffer.

Emacs Org-Mode: how to fold block without going to block header?

I know I can go to a block header and fold/unfold by hitting the TAB key.
However suppose I'm inside a block that has hundreds of lines, and I just want to fold the current block, without having to go to the block header -- is there a keyboard short-cut that can do that? Or is there an elisp function that does that, so that I can bind some shortcut to that function?
Create a keybinding that performs the following function:
(defun zin/org-cycle-current-headline ()
(interactive)
(outline-previous-heading)
(org-cycle))
This will jump back to the previous headline and then cycle. Since the headline is already open, it will close it. It also places the point at the start of the headline.
If you wrap the two commands in (save-excursion ) it will retain the point, however that could lead to entering information inside the ellipsis without realizing it. Alternately you can change the command to call a non-interactive form:
(defun zin/org-cycle-current-headline ()
(interactive)
(org-cycle-internal-local))
This is equivalent to the above with (save-excursion ).
C-c C-p will get you to the heading, TAB will fold. You can create a keyboard macro for this, or the equivalent ELISP:
(defun up-n-fold ()
(interactive)
(progn
(outline-previous-visible-heading 1)
(org-cycle)))
Edit: corrected C-c p for C-c C-p as noted by many below. Thanks!
I am not sure whether such a function exists, but it is not hard to create one. Just replace the following keystrokes with functions: C-M-r^*Entertab.