Do this:
(defvar my-hist nil)
(make-variable-buffer-local 'my-hist)
(defun my-function (hist)
(read-from-minibuffer "> " nil nil nil hist))
(defun my-command ()
(interactive)
(add-to-history 'my-hist (my-function 'my-hist)))
Don't quote hist
when you pass it to read-from-minibuffer
. You want to pass its value, e.g. the symbol my-hist
, and not the result of evaluating 'hist
, which is the symbol hist
.
But I agree with you about using M-p
etc. Function read-from-minibuffer
apparently doesn't use the buffer-local value of the history variable - either for access or for updating (adding to it).
That would also explain why, to add to the buffer-local value you need to explicitly use add-to-history
, passing the value of the input that was read.
Normally (i.e., if the history var is not buffer-local), you would just invoke (my-function 'my-hist)
in my-command
- read-from-minibuffer
automatically adds the read input to the history variable. But doing that when the var is buffer-local doesn't update it.
Seems like a bug. But this is longstanding behavior (I see it back to Emacs 22, and I see the fact that it doesn't work without add-to-history
even back to Emacs 20, which doesn't have add-to-history
). So I'm probably missing something. Hopefully someone else will enlighten us.
(completing-read
behaves the same way.)
Good question.