[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

Re: `g' and `G' do not provide current url as the initial minibuffer contents



Katsumi Yamaoka <yamaoka@xxxxxxx> writes:

> I tried to use `minibuffer-default-add-function' first, but
> failed.  The custom function set to it didn't seem to be called.
> What I did was:
>
> (defun w3m-input-url-default-add-completions ()
>   "Use the current url string (if any) as the next history by default.
> This function is used as `minibuffer-default-add-function'."
>   (w3m-static-when (fboundp 'minibuffer-default-add-completions)
>     (let ((minibuffer-default
> 	   (or (with-current-buffer
> 		   (window-buffer (minibuffer-selected-window))
> 		 (or (w3m-active-region-or-url-at-point) w3m-current-url))
> 	       minibuffer-default)))
>       (minibuffer-default-add-completions))))

It actually is called, but I think it is just written in an illegitimate
way and doesn't work correctly.

You must not shadow the variable `minibuffer-default', because it is
already bound to the DEFAULT-VALUE argument of `read-from-minibuffer,
e.g. "about:".  Rebinding this variable obviously confuses Emacs.  Here
is a version that works for me:

--8<---------------cut here---------------start------------->8---
(defun w3m-input-url-default-add-completions ()
  "Use the current url string (if any) as the next history by default.
This function is used as `minibuffer-default-add-function'."
  (w3m-static-when (fboundp 'minibuffer-default-add-completions)
    (let ((to-add (with-current-buffer
                      (window-buffer (minibuffer-selected-window))
                    (or (w3m-active-region-or-url-at-point) w3m-current-url)))
          (def minibuffer-default)
          (all (all-completions ""
                                minibuffer-completion-table
                                minibuffer-completion-predicate))
          (listify (lambda (thing) (if (listp thing) thing (list thing)))))
      (append (funcall listify def)
              (funcall listify to-add)
              all))))
--8<---------------cut here---------------end--------------->8---

There is probably no sane solution that calls
`minibuffer-default-add-completions' and avoids code duplication.

Note that in the list returned by
`w3m-input-url-default-add-completions' the current value of
`minibuffer-default' must be the first element; else, the thing doesn't
work.

So, if you want the current url to be the first candidate when hitting
M-n, you must make it the DEFAULT-VALUE of `read-from-minibuffer'.

> (defun w3m-input-url (...)

That part should be ok.


BTW, which Emacs versions do we need to support?  Because, starting with
Emacs 23, the DEFAULT-VALUE argument of `read-from-minibuffer' is
allowed to be a list of several defaults.  When we would use this
feature, everything would become trivially simple.


Regards,

Michael.