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

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

Re: Execute as a command a yanked text


From: Mathias Dahl
Subject: Re: Execute as a command a yanked text
Date: Thu, 04 May 2006 10:03:30 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (windows-nt)

Stefan Horomnea <stefan@softexperience.ro> writes:

>    Indeed, the code
>    (command-execute (intern (car kill-ring-yank-pointer)))
>    works !

And thanks to your post I learned about `intern' :)

>    The complete code (which is very short) is:
>    ______________________
>    (defun your-fun ()
>    "helps execute a template"
>    (interactive)
>    (backward-kill-word 1)
>    (command-execute (intern (car kill-ring-yank-pointer)))
>    )
>    (global-set-key [(control return)] 'stefan-fun)
>    ______________________
>    now, if I have to enter a template, I do: htb C-RET or div C-RET etc.

I like this "trick" of yours very much! These are things which I am
very interested in, how to make your computer environment easier and
faster to use. For your information I experimented with abbrevs in a
way that you might find interesting:

Most people know how abbrevs work in Emacs. You define the abbrev (the
"short" word) and you define what is should expand to, and when you
are in abbrev-mode, each abbrev will be expanded to the expansion as
you type. Simple. Useful. One thing that I don't think all users know
about is that you can also make an abbrev execute code.

Example:

;; Simple function to demonstrate

(defun xtst-func ()
  (message "xtst-func was called")
  ;; Need to return non-`nil' in order to inhibit inserting
  ;; the character that triggered the expansion. See the elisp
  ;; manual for more information.
  t)

;; Needed in combination with the `t' above. Again, read the manual.
(put 'xtst-func 'no-self-insert t)

;; Let the expansion be an empty string, set the HOOK argument to our
;; new function. Did I say you should read the manual? ... :)

(define-abbrev lisp-mode-abbrev-table "xtst" "" 'xtst-func)

;; Enable abbrev mode

(abbrev-mode 1)

To test it:

 1. Go to the *scratch* buffer (should be in lisp-mode) and paste the above
 2. Evaluate all of the above
 3. Type xtst and then space (or return, both works)

The result should be that the message "xtst-func was called" is
printed in the echo area and the abbrev you wrote should be gone.

I actually don't *use* this for anything (yet) but I think it is
interesting functionality. It is also much more complex than you
clever hack.

When naming the abbrevs, makes sure you pick names that you will not
type by mistake, so to speak. Maybe use a prefix that is easy/quick to
type, before all your "commands".

Anyway, about your example: maybe you should use normal abbrevs
(create one called htb, for example) that expands into the text you
want to insert in your code. Or use skeletons or some other
expansion/template functionality that exists in Emacs and Emacs
add-ons.

/Mathias


reply via email to

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