unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Why don't let bound values die?
@ 2010-09-03 18:11 Lennart Borgman
  2010-09-03 21:52 ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Lennart Borgman @ 2010-09-03 18:11 UTC (permalink / raw)
  To: Emacs-Devel devel

I have a small utility in nXhtml to save commands accessed through the
menus to M-x history list. (We discussed such a feature here a while
ago.) Recently I noticed problems with menu items defined by easymenu.
Org-mode uses this, for example to define the menu item "Org -
Hyperlinks - Literal links".

When accessing this menu item a temporary command symbol is set up (if
I understand this correctly), something like menu-function-21. This
symbol then shows up in this-command (in pre-command-hook).

(BTW, it is in my opinion a bit annoying that anonymous functions are
used in the menus. Should not at least easymenu discourage this?)

I tried to avoid adding such symbols like menu-function-21 to the
history list. I thought it would be gone if I looked for it in a
run-with-idle-timer timer, but it is not. I do not understand why and
would be glad for an explanation. (I fixed it by saving the symbol
name and checking for it with intern-soft.)

Here is the code in question:


(defvar ourcomments-M-x-menu-timer nil)
(defvar ourcomments-M-x-menu-this-command nil)
(defun ourcomments-M-x-menu-pre ()
  "Add menu command to M-x history."
  (let ((is-menu-command (equal '(menu-bar)
                                (when (< 0 (length (this-command-keys-vector)))
                                  (elt (this-command-keys-vector) 0)))))
    (when (and is-menu-command
               (not (memq this-command '(ourcomments-M-x-menu-mode))))
      (when (timerp ourcomments-M-x-menu-timer)
        (cancel-timer ourcomments-M-x-menu-timer))
      (message "this-command=%s" this-command)
      (setq ourcomments-M-x-menu-this-command (symbol-name this-command))
      (setq ourcomments-M-x-menu-timer
            (run-with-idle-timer 3 nil 'ourcomments-M-x-menu-in-timer)))))

(defun ourcomments-M-x-menu-in-timer ()
  "Add MAYBE-COMMAND to M-x history if it is a command."
  (condition-case err
      (ourcomments-M-x-menu-in-timer-1)
    (error (message "M-x-menu-in-timer ERROR: %s" (error-message-string err)))))

(defun ourcomments-M-x-menu-in-timer-1 ()
  (setq ourcomments-M-x-menu-timer nil)
  ;; Fix-me: temporary commands, like from "Org - Hyperlinks - Literal
  ;; links" are still defined here, why???
  ;; Using symbol-name + intern-soft helped, but why is it necessary?
  (let ((maybe-command (intern-soft ourcomments-M-x-menu-this-command)))
    (message "maybe-command=%s, %s" maybe-command (commandp maybe-command))
    ;; this-command could have been let bound so check it:
    (when (commandp maybe-command)
      (let ((pre-len (length extended-command-history)))
        (pushnew (symbol-name maybe-command) extended-command-history)
        (when (< pre-len (length extended-command-history))
          ;; Give a temporary message
          (let ((msg
                 (format "(Added %s to M-x history so you can run it
from there)"
                         maybe-command)))
            (with-temp-message (propertize msg 'face 'file-name-shadow)
              (sit-for 3))))))))

;;;###autoload
(define-minor-mode ourcomments-M-x-menu-mode
  "Add commands started from Emacs menus to M-x history.
The purpose of this is to make it easier to redo them and easier
to learn how to do them from the command line \(which is often
faster if you know how to do it).

Only commands that are not already in M-x history are added."
  :global t
  (if ourcomments-M-x-menu-mode
      (add-hook 'pre-command-hook 'ourcomments-M-x-menu-pre)
    (remove-hook 'pre-command-hook 'ourcomments-M-x-menu-pre)))



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Why don't let bound values die?
  2010-09-03 18:11 Why don't let bound values die? Lennart Borgman
@ 2010-09-03 21:52 ` Stefan Monnier
  2010-09-03 22:20   ` Lennart Borgman
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2010-09-03 21:52 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: Emacs-Devel devel

> When accessing this menu item a temporary command symbol is set up (if
> I understand this correctly), something like menu-function-21.

No, this symbol is setup when the menu is defined.

> I tried to avoid adding such symbols like menu-function-21 to the
> history list.

One way is to check whether they're interned in `obarray'.

> I thought it would be gone if I looked for it in a
> run-with-idle-timer timer, but it is not.

I have no clue whatsoever what you mean by "looked for it in
a run-with-idle-timer".  Then again, I have no idea why you say "let
bound" in the title, so I'm clearly missing something.

> I do not understand why and would be glad for an explanation.

Why what?

> (I fixed it by saving the symbol name and checking for it with
> intern-soft.)

That means checking that it's interned in `obarray', so it sounds like
a good solution.


        Stefan



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Why don't let bound values die?
  2010-09-03 21:52 ` Stefan Monnier
@ 2010-09-03 22:20   ` Lennart Borgman
  2010-09-04  8:46     ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Lennart Borgman @ 2010-09-03 22:20 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Emacs-Devel devel

On Fri, Sep 3, 2010 at 11:52 PM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
>> When accessing this menu item a temporary command symbol is set up (if
>> I understand this correctly), something like menu-function-21.
>
> No, this symbol is setup when the menu is defined.

Thanks. Learning. So that was a bad assumption by me. (Did not think
about uninterned symbols so I thought it was let bound.)

>> I tried to avoid adding such symbols like menu-function-21 to the
>> history list.
>
> One way is to check whether they're interned in `obarray'.
>
>> I thought it would be gone if I looked for it in a
>> run-with-idle-timer timer, but it is not.
>
> I have no clue whatsoever what you mean by "looked for it in
> a run-with-idle-timer".  Then again, I have no idea why you say "let
> bound" in the title, so I'm clearly missing something.

Yes, that is because you are not understanding my misunderstanding correctly ;-)

I just missed that it was defined by make-symbol. (Easy for me to do
since I have not fully understood how such symbols are handled.)

>> I do not understand why and would be glad for an explanation.
>
> Why what?
>
>> (I fixed it by saving the symbol name and checking for it with
>> intern-soft.)
>
> That means checking that it's interned in `obarray', so it sounds like
> a good solution.

Thanks, you have given me most of the explanation I needed to understand.

I think I still do not know exactly what an uninterned symbol is, but
I guess it is something that can not be looked up, just somehow
accessed directly. But it still has a name and I wonder why.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Why don't let bound values die?
  2010-09-03 22:20   ` Lennart Borgman
@ 2010-09-04  8:46     ` Stefan Monnier
  2010-09-06  8:25       ` David Kastrup
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2010-09-04  8:46 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: Emacs-Devel devel

> I think I still do not know exactly what an uninterned symbol is, but
> I guess it is something that can not be looked up, just somehow
> accessed directly. But it still has a name and I wonder why.

Fundamentally symbols are just special kinds of immutable strings.
Obarrays then are special kinds of hash-tables which only map strings
to symbols (whose name has to be the specified string).

A symbol may be placed in only one obarray at any given time (because
of details of the way the obarray-hash-tables are implemented, where the
linked list slots of each hash-bucket are actually stored directly in
the symbol themselves).


        Stefan



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Why don't let bound values die?
  2010-09-04  8:46     ` Stefan Monnier
@ 2010-09-06  8:25       ` David Kastrup
  0 siblings, 0 replies; 5+ messages in thread
From: David Kastrup @ 2010-09-06  8:25 UTC (permalink / raw)
  To: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> I think I still do not know exactly what an uninterned symbol is, but
>> I guess it is something that can not be looked up, just somehow
>> accessed directly. But it still has a name and I wonder why.
>
> Fundamentally symbols are just special kinds of immutable strings.
> Obarrays then are special kinds of hash-tables which only map strings
> to symbols (whose name has to be the specified string).
>
> A symbol may be placed in only one obarray

s/only/at most/

> at any given time (because of details of the way the
> obarray-hash-tables are implemented, where the linked list slots of
> each hash-bucket are actually stored directly in the symbol
> themselves).

Uninterned symbols are not in any obarray.

-- 
David Kastrup




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-09-06  8:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-03 18:11 Why don't let bound values die? Lennart Borgman
2010-09-03 21:52 ` Stefan Monnier
2010-09-03 22:20   ` Lennart Borgman
2010-09-04  8:46     ` Stefan Monnier
2010-09-06  8:25       ` David Kastrup

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).