Subject: RE: bug#12504: `bookmark-rename' and `bookmark-maybe-historicize-string'

From: "Drew Adams" <[email-masked]>
Date: Sun, 30 Sep 2012 21:29:13 -0700

> I agree there is a bug, or maybe a buglet, here, for the reasons you
> describe, but I'm not sure how to solve it.
> 
> Does invoking functions through a menu result in an environment where
> `called-interactively-p' returns non-nil?  In that case, the premise
> behind `bookmark-maybe-historicize-string' is all wrong 
> anyway, and the macro should be rewritten to:
> 
>   `(when (called-interactively-p 'interactive)
>      (setq bookmark-history (cons ,string bookmark-history))))

I think the problem that the code comment refers to, namely that invoking it
from a menu will not add it to the history, is a real problem, albeit a minor
one.  And as you say, it has nothing to do with bookmarks in particular.

I would suggest removing this workaround to try to make it DTRT for bookmark.el
menu commands, and just kick the question up to emacs-devel.  There might be a
good general answer.  In any case, it is a general question.  And I don't see a
crying need for bookmark renaming etc. to handle this specially.

There was some discussion on emacs-devel a few years back about optionally
(i.e., a user could choose) adding commands invoked using menus to the command
history (which is a bit different, but which could cover this case as well).

I implemented that in Icicles, with user option
`icicle-menu-items-to-history-flag':

 "Non-nil means to add menu-item commands to the command history.
  This history is `extended-command-history'."

FWIW, I do that by adding/removing this function to/from `pre-command-hook',
according to the option value:

(defun icicle-add-menu-item-to-cmd-history ()
  "Add `this-command' to command history, if it is a menu item.
Menu items that are not associated with a command symbol are ignored.
Used on `pre-command-hook'."
  (condition-case nil ; Just in case, since this is on `pre-command-hook'.
      (when (and (> (length (this-command-keys-vector)) 0)
                 (equal '(menu-bar) (elt (this-command-keys-vector) 0))
                 ;; Exclude uninterned symbols such as `menu-function-356'.
                 (symbolp this-command)
                 (intern-soft this-command))
        (pushnew (symbol-name this-command) extended-command-history))
    (error nil)))

> The issue is larger than just `bookmark-rename', obviously.

Yup.  I don't think bookmark.el should try to deal with it.  How important is it
for menu access to add such stuff to the history?

> By the way, your guess is right: it's useful (I think) to have the old
> name in the history for `bookmark-rename', because someone may want to
> use it or a variant of it in another bookmark soon.  History is cheap
> that way: it's better to have a little junk than to *not* 
> have the thing the user needs when they need it.

I agree about the usefulness, but I think it should be done only when
`bookmark-rename' is invoked interactively.

(In my code, there is, in some contexts, some automatic bookmark renaming, and
it makes no sense in such non-interactive calls to add the old names to the
history.  Anyway, that's my problem, but for bookmark.el I still don't think it
helps to add to the history unless invoked interactively.)

> Let's tackle the larger issue with 
> `bookmark-maybe-historicize-string',

See above.  Any solution to satisfy this need should, in any case, not be adding
to the history "if caller non-interactive".  That's a bad workaround if the goal
is to add to the history when the user uses the menu: non-interactively is not
the same thing as interactively-using-a-menu.

> and then figure out whether `bookmark-rename' is doing the 
> right thing.

See above for my opinion.

HTH - Drew