help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Print out my key-sequences


From: Kevin Rodgers
Subject: Re: Print out my key-sequences
Date: Tue, 25 May 2010 00:47:59 -0600
User-agent: Thunderbird 2.0.0.24 (Macintosh/20100228)

Andrea Crotti wrote:
Kevin Rodgers <kevin.d.rodgers@gmail.com> writes:

this-command-keys can return a vector containing elements other than
integers (characters) e.g. symbols for function keys and mouse events.  And
the vector can contain integers that cannot be put into a string.  See
the Strings of Events node of the Emacs Lisp manual.

So first you have to find a robust way of converting the result of
this-command-keys to a string, so it can incorporated into the COMMAND
string argument to shell-command.  As Lennart suggested, key-description
is probably the way to go.

And passing an arbitrary string as an argument to a program via
shell-command is itself risky, if it may contain a shell metacharacters
(which seems quite likely in this case).  So the string should be escaped
using shell-quote-argument, or you could use call-process instead of
shell-command to avoid the quoting issue.

So:

(defun growl-popup (msg)
  "Pop up a growl notification with MSG, or display an Emacs message.
The \"growlnotify\" program is used if `window-system' is non-nil and
the program is found in `exec-path'; otherwise `message' is used."
  (interactive)
  (if (and window-system (executable-find "growlnotify"))
      (shell-command (concat "growlnotify -a /Applications/Emacs.app/ -m "
                             (shell-quote-argument msg)))
    (message msg)))

Oops, that should be (message "%s" msg)

(defun popup-last ()
  (interactive)
  (growl-popup (key-description (this-command-keys))))


That's really nice thanks, I've added it to my configuration, with also
two functions to enable/disable it.

I have to use setq-default to disable it by the way, is that correct?
--8<---------------cut here---------------start------------->8---
  (defun growl-mode-off ()
    (interactive)
    (setq-default pre-command-hook (remq 'popup-last pre-command-hook)))
--8<---------------cut here---------------end--------------->8---

Only one more problem, here I display all the keys, but I don't really
care about stuff like
"C-n", "C-p", "m" (all the keys are inserted).

Is there any kind of distinction between simple and more complex
combinations?

How about:

(unless (= (length (this-command-keys-vector)) 1)
  ...)

or:

(unless (eq (lookup-key global-map (this-command-keys)) this-command)
  ...)

or both:

(unless (and (= (length (this-command-keys-vector)) 1)
             (eq (lookup-key global-map (this-command-keys)) this-command))
  ...)

--
Kevin Rodgers
Denver, Colorado, USA




reply via email to

[Prev in Thread] Current Thread [Next in Thread]