Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!snorkelwacker.mit.edu!mintaka!albert.ai.mit.edu!roland From: roland@ai.mit.edu (Roland McGrath) Newsgroups: comp.emacs Subject: Re: toggling key definitions Message-ID: Date: 29 Nov 90 23:58:48 GMT References: <9322@ncar.ucar.edu> Sender: daemon@mintaka.lcs.mit.edu (Lucifer Maleficius) Organization: Hackers Anonymous International, Ltd., Inc. (Applications welcome) Lines: 80 In-Reply-To: southern@neit.cgd.ucar.edu's message of 28 Nov 90 19:32:39 GMT In article <9322@ncar.ucar.edu> southern@neit.cgd.ucar.edu writes: First, there are a number of complimentary commands that I would like to define in such a manner that they are toggled by a single key. These include: 1. moving between the beginning and end-of-line, 2. moving between the beginning and end-of-buffer, These are pretty straightforward in Emacs Lisp. (defun line-boundary () "Go to the beginning of the line. If already there, go to the end." (interactive) (if (bolp) (end-of-line) (beginning-of-line))) (defun buffer-limit () "Go to the beginning of the line. If already there, go to the end." (interactive) (if (bobp) (end-of-buffer) (beginning-of-buffer))) 3. issuing start and end-kbd-macro commands. For example, I currently define my 7 and 8 keypad keys to begin and end a keyboard macro definition as such: (define-key function-keymap "7" 'start-kbd-macro) ;NCD [7] (define-key function-keymap "8" 'end-kbd-macro) ;NCD [8] I would like to use only one key to toggle between the start-kbd-macro and end-kbd-macro commands such that: if I currently am not defining a keyboard macro, 7 maps to start-kbd-macro if I currently am defining a keyboard macro, 7 maps to end-kbd-macro. This can be done the same way as the others: (defun toggle-kbd-macro () "Start or end a keyboard macro. Like \\[start-kbd-macro] when not defining a macro. Like \\[end-kbd-macro] when defining a macro." (if defining-kbd-macro (end-kbd-macro) (start-kbd-macro))) Can anyone show me a simple generic method to do this for the various cases that I'm currently working on as well as others which might crop up in the future. For all the things you have mentioned, which of the two alternatives can be a thing decided in the body of the command. This means, for example, that if C-c l is line-boundary, C-a C-c l C-a C-l will get you to the end of the line, which is where you want to be. Using the method of having two commands, which each rebind a key to the other, means that it always flip-flops, regardless of what other commands you execute in the meantime. The second definition I would like to make may be similar to the first. When I scroll-down through a buffer and reach the last full screen, the cursor is left a number of lines up from the bottom of the buffer and I get a beep and a "End of buffer" message. I would like to define the scroll-down such that when I reach the last full screen and issue another scroll-down, the cursor is placed at the end-of-buffer rather than being left a half screen up. (defun scroll-down-hard (arg) "Scroll down ARG number of lines, like \\[scroll-down]. If the end of the buffer is already on the screen, move point to it." (interactive "p") (if (or (numberp arg) (not (pos-visible-in-window-p (point-max)))) (scroll-down arg) (goto-char (point-max)))) -- Roland McGrath Free Software Foundation, Inc. roland@ai.mit.edu, uunet!ai.mit.edu!roland