I want to add two numbers that are to be entered by the user in Lisp. How can I do that? I used the read command twice but it shows an error.
I recommend parsing numbers from user input with the parse-number library. Using read instead gives the user direct access to the Lisp reader, which you do not want.
Something like this should work:
(defun prompt-number (prompt &optional (prompt-stream *standard-output*)
(read-stream *standard-input*))
(format prompt-stream "~a: " prompt)
(parse-number:parse-number (read-line read-stream)))
Related
How can I let the user select multiple items from a list instead of just one? Like in the C-x b menu in helm.
Currently I can only select a single item instead of getting a complete list:
(defun test-main ()
(interactive)
(let ((choice (completing-read "Select: " '("item1 item2 item3"))))
(message choice)))
You can do that with completing-read-multiple:
(defun test-main ()
(interactive)
(let ((choice (completing-read-multiple "Select: " '("item1" "item2" "item3"))))
(message "%S" choice)))
It returns the selected items as a list, so if you type item2,item3 at the prompt, it returns ("item2" "item3").
Because that's what vanilla completing-read does. It reads and returns a single choice, providing completion to help you choose.
You can do what you are asking for with Icicles. It redefines function completing-read when Icicle mode is on.
Many Icicles commands are multi-commands, which means that you can make multiple input choices in a single command execution: a multiple-choice command. You can define your own multi-commands, using any actions.
And for any completion (not just for a multi-command), you can manipulate, save, and restore sets of completion candidates.
(You can also enter multiple inputs in the minibuffer. This is useful even for commands that read input without completion.)
Is there a nice and friendly set of searchable documentation for ANSI Common Lisp anywhere? Preferably one that can be downloaded for use offline. I've Google but can only find static HTML pages that basically mean you need to know exactly what you're looking for.
I'm after something like http://erldocs.com/, where I can type something like "string" or "list" and all the matching functions come up instantly, for me to click on and browse easily.
Man pages are no use, since you need to know the exact function you need, while the searchable style allows you to discover functions without knowing them beforehand.
CLHS: Symbol Index
l1sp.org
In Slime, type C-cC-dh, a few chars of your search term, and then Tab to get a completion list.
Just type (apropos "term") in repl.
A downloadable version of the CLHS is available in info format:
ftp://gnu.org/gnu/gcl/gcl.info.tgz
Here's a handy emacs function to lookup the symbol under the point in the using F1:
(defun clhs-info ()
(interactive)
(ignore-errors
(info (concatenate 'string "(gcl) " (thing-at-point 'symbol)))))
(add-hook 'lisp-mode-hook
(lambda ()
(define-key lisp-mode-map [f1] 'clhs-info)))
You can do partial matching using Info-index rather than concatenation as well.
I am dabbling in Emacs Lisp and I am trying to write the following function:
(defun buffer-file-name-body ()
(last (split-string (buffer-file-name) "/")))
What I am trying to achieve is to extract just the file name and extension from the full path given by (buffer-file-name). However, this implementation returns a list of one item ("scratch.el") ... I tried several things such as passing the result of (last) through (string) but that raises an error... Google did not return anything useful when I searched for Emacs List convert list to string. How to I do this?
It sounds to me like what you want is (file-name-nondirectory (buffer-file-name)) which returns the simple file name, sans any directory information, as a string.
I'm new to elisp. http://www.gnu.org/s/emacs/manual/html_node/elisp/Interactive-Codes.html#Interactive-Codes lists 'code characters' for interactive parameters, which AFAIK modifies the behaviour of the input mechanism when prompting the user for input (eg: if you specify that the input is a filename that exists, emacs' autocomplete functionality will look for file names that exists).
I'm trying to find a code for a bookmark name that already exists - ie: emacs will prompt the user for a bookmark name, and upon pressing tab emacs will show possible bookmark name completions.
Does such a code exist?
Use completing-read for that. You could write a function that prompts the user for a bookmark like so:
(defun my-function ()
(interactive)
(let ((bookmark (completing-read "Bookmark: " (bookmark-all-names))))
...))
If you prefer the prompting to be part of interactive (so that the result will be bound automatically to your function's arguments), you could use the following alternative:
(defun my-function (bookmark)
(interactive (list (completing-read "Bookmark: " (bookmark-all-names))))
...)
For Emacs to find the function bookmark-all-names you also have to add the following line to your .emacs file:
(require 'bookmark)
Function bookmark-completing-read is the standard way to complete a bookmark name. You do not need the lower-level function completing-read for this. Example:
(bookmark-completing-read "Bookmark" bookmark-current-bookmark)
If you use Bookmark+ then bookmark-completing-read accepts some optional arguments (similar to completing-read) that can help:
ALIST -- an alist of bookmarks to choose from (instead of all bookmarks: bookmark-alist)
PRED -- a predicate that filters the list of bookmark candidates
HIST -- an input history list
There is also a non-strict version of the function, bmkp-completing-read-lax, which is useful if you want to accept a new bookmark name or complete against existing names.
Emacs Lisp function often start like this:
(lambda () (interactive) ...
What does "(interactive)" do?
Just to clarify (it is in the quoted docs that Charlie cites) (interactive) is not just for key-bound functions, but for any function. Without (interactive), it can only be called programmatically, not from M-x (or via key-binding).
EDIT: Note that just adding "(interactive)" to a function won't necessarily make it work that way, either -- there could be many reasons functions are not interactive. Scoping, dependencies, parameters, etc.
I means that you're including some code for the things you need to make a function callable when bound to a key -- things like getting the argument from CTRL-u.
Have a look at CTRL-h f interactive for details:
interactive is a special form in `C source code'.
(interactive args)
Specify a way of parsing arguments for interactive use of a function.
For example, write
(defun foo (arg) "Doc string" (interactive "p") ...use arg...)
to make ARG be the prefix argument when `foo' is called as a command.
The "call" to `interactive' is actually a declaration rather than a function;
it tells `call-interactively' how to read arguments
to pass to the function.
When actually called, `interactive' just returns nil.
The argument of `interactive' is usually a string containing a code letter
followed by a prompt. (Some code letters do not use I/O to get
the argument and do not need prompts.) To prompt for multiple arguments,
give a code letter, its prompt, a newline, and another code letter, etc.
Prompts are passed to format, and may use % escapes to print the
arguments that have already been read.
Furthermore it’s worth mentioning that interactive's main purpose is, in an interactive context (e.g. when user calls function with key binding), let user specify function arguments that otherwise could be only given programmatically.
For instance, consider function sum returns sum of two numbers.
(defun sum (a b)
(+ a b))
You may call it by (sum 1 2) but you can do it only in a Lisp program (or in a REPL). If you use the interactive special form in your function, you can ask the user for the arguments.
(defun sum (a b)
(interactive
(list
(read-number "First num: ")
(read-number "Second num: ")))
(+ a b))
Now M-x sum will let you type two numbers in the minibuffer, and you can still do (sum 1 2) as well.
interactive should return a list that would be used as the argument list if function called interactively.
(interactive) is for functions meant to interact with the user, be it through M-x or through keybindings.
M-x describe-function RET interactive RET for detailed info on how to use it, including parameter to catch strings, integers, buffer names, etc.
One of the "gotchas" that this clarifies is that the argument to interactive is actually a kind of mini-formatting language (like for printf) that specifies the following (for the surrounding function's input):
schema (number of arguments and their type)
source (e.g., marked-region in buffer and/or user input, etc.)
For example,
'r'
Point and the mark, as two numeric arguments, smallest first.
means that the interactive-annotated function needs exactly two arguments.
e.g. this will work
(defun show-mark (start stop)
(interactive "r")
(print start)
(print stop))
This will break:
(defun show-mark (start)
(interactive "r")
(print start))
Wrong number of arguments: ((t) (start) (interactive "r") (print start)), 2