Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ncar!tank!oddjob!matt From: matt@oddjob.uchicago.edu (Matt Crawford) Newsgroups: comp.emacs Subject: Re: Help with hooks, sun.el, and .emacs Keywords: hooks sun.el Message-ID: <440@tank.uchicago.edu> Date: 20 Oct 88 15:26:19 GMT References: <25180@tut.cis.ohio-state.edu> Sender: news@tank.uchicago.edu Reply-To: matt@oddjob.uchicago.edu (Matt Crawford) Organization: Chicago Superconductivity Center - "Resistance is useless!" Lines: 100 Since this may be of general interest, I'm posting the answer here. My answer is kind of tutorial, so experts probably want to skip or skim it. phil@tut (Phil Ritzenthaler) writes: ) Could someone with a better grasp of emacs-lisp please help me. I ) want to re-bind the function keys on our suns when using either emacs ) or emacstool, but I've run into a snag . . . Simplest solution, ) probably, is to put my new key bindings in the sun.el file. But, the ) author of that file had a more elegant solution by using 'hooks'. ) Now, comes my problem, just how do I use the dang blasted hooks???? A "hook" is a variable you can set to get some of your code executed at an appropriate time. To know when the hook will be invoked and what value to give the hook, you either have to read and understand the code that invokes it, or that code has to be well-documented. Most hooks I've seen for emacs should be set to a function (or the name of a function) with no arguments. For example, I have defined a function called my-c-comment-indent by using "defun". Then I do: (setq comment-indent-hook 'my-c-comment-indent) You can define the function on the spot, without giving it a name, as in: (setq TeX-mode-hook (function (lambda nil (setq TeX-dvi-print-command "dviimp -p")))) ("function" is the way you quote the function definition, so it is not evaluated right away, and "lambda nil" is the way you begin a function of no arguments.) Some hook variables should be set to a list of functions which are to be run in order. You can add one to the front of the list like this: (setq find-file-not-found-hooks (nconc '(my-RCS-file) find-file-not-found-hooks)) Now for your particular problem. The answer is different depending on whether you want to set up for emacs or emacstool. Everyone who is reading this from within emacs please find .../lisp/term/sun.el in another window. Ready? Skip down to the lines that read ;;; handle sun's extra function keys ;;; this version for those who run with standard .ttyswrc and no emacstool After that you see that your .emacs file must have set the variable sun-esc-bracket to a non-nil value in order for the keymap called sun-raw-map to be bound to the key sequence ESC [. In sun.el, after this variable is tested and the keymap is bound to ESC [ and the arrow keys bound to the natural functions, you find the following: ;;; Since .emacs gets loaded before this file, a hook is supplied ;;; for you to put your own bindings in. (defvar sun-raw-map-hooks nil "List of forms to evaluate after setting sun-raw-map.") (let ((hooks sun-raw-map-hooks)) (while hooks (eval (car hooks)) (setq hooks (cdr hooks)) )) Thus you discover that the variable sun-raw-hooks should be set not to a function or list of functions, but to a list of lisp expressions. So you would put something like the following into your .emacs file: (setq sun-raw-map-hooks '((define-key sun-raw-map "210z" 'backward-page) ; R3 (define-key sun-raw-map "213z" 'forward-page) ; R6 (define-key sun-raw-map "214z" 'beginning-of-buffer) ; R7 (define-key sun-raw-map "216z" 'scroll-down) ; R9 (define-key sun-raw-map "218z" 'recenter) ; R11 )) (The strings like "210z" have to be found in the sun documentation. The key "R3", for example, sends the six bytes ESC [ 2 1 0 z. The keymap sun-raw-map has been bound to ESC [.) To set up for emacstool, read down a little further in sun.el, to the part that begins with ;;; This section adds defintions for the emacstool users ;;; emacstool event filter converts function keys to C-x*{c}{lrt} Reading further, you discover that the keymap used is called suntool-map, it is bound to C-X *, and the hook variable suntool-map-hooks is evaluated just as sun-raw-map-hooks was. See sun.el for example keybindings. Is that a question in the back? ... Yes, this will be on the midterm. Matt Crawford Excuse me, but didn't I tell you there's NO HOPE for the survival of OFFSET PRINTING?