I'm writing an interactive function that I'd like to have remember the last argument the user supplied and use it as the default.
(defun run-rake (param)
(interactive "sTask: ")
(shell-command (format "rake %s" task)))
The first time the function is invoked I want it to remember the argument the user supplied so that the next time they invoke the function they can just press enter and it will use the value they supplied the previous time.
I can't seem to find this in the documentation - how do you do this in elisp?
GNU Emacs supports command line arguments to request various actions when invoking Emacs. These are for compatibility with other editors and for sophisticated activities. We don't recommend using them for ordinary editing.
The normal actions of Emacs are to first load `site-start.el' if it exists, then your own init file `~/.emacs' if it exists, and finally `default.el' if it exists; certain options prevent loading of some of these files or substitute other files for them. Use device as the device for terminal input and output.
Emacs processes all the action arguments in the order they are written. Action Arguments: Arguments to visit files, load libraries, and call functions. Initial Options: Arguments that take effect while starting Emacs.
The last file name on your command line becomes the current buffer; the other files are also present in other buffers. You can use options to specify various other things, such as the size and position of the X window Emacs uses, its colors, and so on.
read-from-minibuffer
is what you want to use. It has a spot for a history variable.
Here is some sample code:
(defvar run-rake-history nil "History for run-rake")
(defun run-rake (cmd)
(interactive (list (read-from-minibuffer "Task: " (car run-rake-history) nil nil 'run-rake-history)))
(shell-command (format "rake %s " cmd)))
Obviously customize to your needs. The 'run-rake-history is simply a variable that is used to store the history for this invocation of 'read-from-minibuffer. Another option would be to use 'completing-read - but that assumes you've got a list of choices you want to restrict the user to using (which usually isn't the case for shell-like commands).
You can see how the compile
command does this. Bring up the help text for the compile command with C-h f compile
, move the cursor over the name of the file that contains the function, then hit RETURN
. This will bring up the source file for compile
.
Basically, there's a dynamic/global variable compile-command
that holds the last compile command. Emacs is a single-user, single-threaded system, so there's really no need for much more. Also keep in mind that Elisp is a very old school Lisp, and variables have dynamic (call stack), not lexical, scope. In this kind of system it is natural to:
(let ((compile-command "gcc -o foo foo.c frobnicate.c"))
...
(compile)
...)
Speaking of the compile
command, have you tried using it instead of your own run-rake
function?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With