Emacs: Interact with User

Q: In emacs, how can one get input from the user after a prompt? (modified from Xah Lee site)
A: “Interactive” turns a function into a command:

1
2
3
4
5
6
(defun irc-join (chan) 
  "join an IRC channel" 
  (interactive "sChannel to join: ") 
  (save-excursion 
    (setq irc-channel chan) 
    (irc-send-server (concat "JOIN " chan))))

Define a function via ‘defun’. ‘irc-join’, is the name
of the function. ‘chan’ is a local variable / parameter. Next is the description.
The string: “sChannel to join: ” –> The first character ‘s’ specifies
a string as the first variable of the parameter
list. The character ‘s’ means “string”. If you
wanted more parameters, it would be something like:

1
2
3
(defun function-name (stringparam numberparam) 
  (interactive "sPrompt here: \nnAnother prompt: ") 
        etc...

Note that each prompt string is separated by a newline character “n”.
Also, ‘n’ means you want to get a number from the user.

Comments are closed.