[comp.emacs] User defined functions

dsill@NSWC-OAS.ARPA (Dave Sill) (08/12/87)

I've been toying around with the idea of extending the macro concept.
What I envision is a facility whereby one could define a function in
the same manner as a macro.  This function could be given an
associated name like any other Emacs command, e.g. daves-foo-command.
In addition, there should be a facility for saving these definitions
in a file.

This approach would bridge the gap between volatile macro definitions
and full-fledged elisp or mocklisp extensions.  I, for one, don't have
the time to learn elisp, especially since there is not currently any
documentation for it and I don't already know lisp.

I'd like to know what others think about this.  Does it sound useful?
Would it be hard to implement?  Has it been considered before?

-Dave Sill
 dsill@nswc-oas.arpa

barmar@think.COM (Barry Margolin) (08/12/87)

In article <8708121417.AA19618@ucbvax.Berkeley.EDU> dsill@NSWC-OAS.ARPA (Dave Sill) writes:
>I've been toying around with the idea of extending the macro concept.
>What I envision is a facility whereby one could define a function in
>the same manner as a macro.  This function could be given an
>associated name like any other Emacs command, e.g. daves-foo-command.
>In addition, there should be a facility for saving these definitions
>in a file.

Multics Emacs has two things along these lines.  One is the ability to
save keyboard macros into a text file and later load the macro back
into Emacs.  You can also manually edit a macro this way (don't you
hate it when you finish creating a keyboard macro and then realize
that you forgot to make it go to the next line at the end, and have to
redo it?).

The other facility it has is a keyboard macro compiler.  It takes a
keyboard macro in a buffer and converts it to Lisp.  It works by
simply replacing each key with the corresponding (function) call.  It
doesn't work really well, though; for example, it translates the macro
sequence

	^X^F "filename"

into
	(find-file)
	(insert-string "filename")

because the compiler doesn't realize that "filename" is actually a
parameter to find-file.  It knows about a few special case (I think it
knows about ^S, since that is used very frequently in macros).
Another bug in the macro compiler is that it uses the key bindings in
effect in the macro editing buffer, not the buffer where the macro was
created.

---
Barry Margolin
Thinking Machines Corp.

barmar@think.com
seismo!think!barmar

ron@topaz.rutgers.edu (Ron Natalie) (08/13/87)

How about using the function

    "name-last-kbd-macro"

which allows you to bind a function.

You can even do
    "insert-kbd-macro"
which will give you a piece of lisp you can put in your
init file.

-Ron

storm@ambush.UUCP (08/13/87)

In article <8708121417.AA19618@ucbvax.Berkeley.EDU> dsill@NSWC-OAS.ARPA (Dave Sill) writes:
>I've been toying around with the idea of extending the macro concept.
>What I envision is a facility whereby one could define a function in
>the same manner as a macro.  This function could be given an
>associated name like any other Emacs command, e.g. daves-foo-command.
>In addition, there should be a facility for saving these definitions
>in a file.

This is exactly what M-x name-last-kdb-macro and M-x insert-kbd-macro
in GNU Emacs can do for you.  The trick is that a string bound to a
function name is interpreted as if the characters in the string were
entered from the keyboard, e.g.

(fset 'insert-hello-world-in-beginning-of-line
   "^AHello world ")

However, there is a minor problem with this approach:  If you use
key bindings which are unique for the terminal on which the keyboard
macro is defined, you will not be able to use them on another
terminal.  If you want to be able to move functions defined via
keyboard macros between incompatible terminals, use only the standard
bindings and call other functions via M-x or eval even when you have
bound them to a key on YOUR keyboard.  For example,

(fset 'insert-hello-world-in-beginning-of-line
   "^[#Ghello world ")

MIGHT do what was intended on the terminal it was defined on, but not
on any other terminal!  The following version is always protable:

(fset 'insert-hello-world-in-beginning-of-line
   "^[xbeginning-of-line^Mhello world ")

--
Kim F. Storm, storm@ambush.uucp (or ..!mcvax!diku!ambush!storm)
AmbraSoft A/S, Rojelskaer 15, DK-2840 Holte, Denmark.  tel: +45 2424 111

ron@topaz.rutgers.edu (Ron Natalie) (08/14/87)

UniPress EMACS has the capability of translating keystrokes into mlisp
using the generate facility.  This involves hooks in the C code to
accomplish.  GNU EMACS can generate functions using the insert-kbd-macro
instruction, but it is pretty silly because it just reconsitutes the
named macro .i.e. a macro that inserts abc followed by ^B yields the
lisp code
    (fset 'macro "abc^B")

-Ron