1

Is there an easy way to use thing-at-point to initialize the contents of an interactive string reading?

I have a function the looks something like this:

(defun foo (string)
   (interactive "sString:")
   ;; does something with string
   )

But I'd really like it if when I did M-x foo, that it filled the minibuffer with the thing-at-point. In this case (thing-at-point 'word).

Is the only way to get that to use an explicit list argument to interactive, something like:

(defun foo (string)
   (interactive (list (read-from-minibuffer 
                        "String:" ;; prompt
                        (thing-at-point 'word) ;; initial value
                       ) ) ) ; interactive
   ;; does something with string
   )

And if I do so, do my own history list etc?

1 Answer 1

1

Yes, use interactive with a Lisp sexp, not a literal string. You can't specify particular defaulting with a literal string arg.

You can use read-from-minibuffer, read-string, or completing-read.

Provide the value returned by (thing-at-point 'word) as the DEFAULT-VALUE argument to one of those functions, not the INITIAL-CONTENTS argument.

(You can use the initial-contents/input arg if you like, but Emacs convention prefers that you use the default-value arg nowadays.)

As for the input history list: you can provide your own history variable, or you can just use the default history list, minibuffer-history. If you use your own then the only entries on it will be previous inputs to minibuffer-input reads that use your own history variable. That can mean less noise interactively, but it can also be handy to not be so specific. It's up to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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