1

Some existing functions have ability to remember last input, which you can use in the next command call - for example, replace-string will suggest the last replacement by default, while isearch will look for the thing you searched for last, after pressing the combination once again. How can I achieve the same effect as in replace-string?

2

You could use read-string and give they default value def-say-word and save in later.

(setq def-say-word "default value")

(defun say-word (word)
  (interactive
   (list (read-string (format "Say word [%s]: " def-say-word) nil nil def-say-word)))
   (setq def-say-word word)
   (message "The word is %s" word))
2

Commands that read input from the minibuffer use functions such as completing-read, read-string, read-regexp, etc. The most general of these is read-from-minibuffer.

All such functions take a HISTORY argument, which is a variable whose value is a list of previous inputs read by the function. If HISTORY is absent or nil then usually the variable is understood to be minibuffer-history.

So you just provide the variable you want, to get the behavior you want.

As a user, to get to previous inputs when reading a new input, you use M-p (or M-n), which you can repeat to go farther back in the given history list. Or you can use M-r (or M-s) to search the history list for a match to a regular expression that you type at the prompt.

See the Emacs manual, node Minibuffer History and the Elisp manual, node Minibuffer History.

  • Thanks for the useful information. However, I wanted to provide default value, and I wanted to explicitly tell user about it. Konstantin's answer does that. – MatthewRock Jul 12 '16 at 15:25
  • All of the functions I mentioned accept a DEFAULT-VALUE argument. And there is no need to "save" it in a global variable, for the reason I already gave: it is available in the history list (variable) that you also pass to the function. (And if you do use a global variable for that, for some reason, you should use defvar, not just setq.) – Drew Jul 12 '16 at 17:23
  • Example then, please? – MatthewRock Jul 12 '16 at 19:29
  • Example of what? Passing a DEFAULT argument to read-string or completing-read or whatever? Or getting values from a given HISTORY argument list value? Or using defvar instead of setq? What is your remaining question, exactly? – Drew Jul 12 '16 at 21:56
  • Example of applying your solution to solve my problem. I tried what you're suggesting and none of the behaviours is the one I want; I'd like to present the user with option to just press enter to get the default value used(and inform about the default value somehow in the prompt), instead of providing history. I asked for the same behaviour as in replace-string. None of your suggestions provide that behaviour. – MatthewRock Jul 13 '16 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.