unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Lennart Borgman <lennart.borgman@gmail.com>
To: Emacs-Devel devel <emacs-devel@gnu.org>
Subject: Why don't let bound values die?
Date: Fri, 3 Sep 2010 20:11:28 +0200	[thread overview]
Message-ID: <AANLkTikO7Pu77j7KOKMfyh1QRCbj7dsLhx+9O1M0py-G@mail.gmail.com> (raw)

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)))



             reply	other threads:[~2010-09-03 18:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-03 18:11 Lennart Borgman [this message]
2010-09-03 21:52 ` Why don't let bound values die? Stefan Monnier
2010-09-03 22:20   ` Lennart Borgman
2010-09-04  8:46     ` Stefan Monnier
2010-09-06  8:25       ` David Kastrup

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AANLkTikO7Pu77j7KOKMfyh1QRCbj7dsLhx+9O1M0py-G@mail.gmail.com \
    --to=lennart.borgman@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).