annotate lisp/electric/electric.el @ 71:bae944334fa4

Added tag r20-0b30 for changeset 131b0175ea99
author cvs
date Mon, 13 Aug 2007 09:03:07 +0200
parents 131b0175ea99
children b9518feda344
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; electric.el --- window maker and Command loop for `electric' modes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
3 ;; Copyright (C) 1985, 1986, 1992, 1995 Free Software Foundation, Inc.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Author: K. Shane Hartman
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Maintainer: FSF
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; Keywords: extensions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
23 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
25 ;;; Synched up with: FSF 19.30.97.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ; zaaaaaaap
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;; This loop is the guts for non-standard modes which retain control
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;; until some event occurs. It is a `do-forever', the only way out is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;; to throw. It assumes that you have set up the keymap, window, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;; everything else: all it does is read commands and execute them -
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;; providing error messages should one occur (if there is no loop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;; function - which see). The required argument is a tag which should
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;; expect a value of nil if the user decides to punt. The second
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;; argument is the prompt to be used: if nil, use "->", if 'noprompt,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;; don't use a prompt, if a string, use that string as prompt, and if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;; a function of no variable, it will be evaluated in every iteration
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;; of the loop and its return value, which can be nil, 'noprompt or a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;; string, will be used as prompt. Given third argument non-nil, it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;; INHIBITS quitting unless the user types C-g at toplevel. This is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;; so user can do things like C-u C-g and not get thrown out. Fourth
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;; argument, if non-nil, should be a function of two arguments which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;; is called after every command is executed. The fifth argument, if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ;; provided, is the state variable for the function. If the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; loop-function gets an error, the loop will abort WITHOUT throwing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;; (moral: use unwind-protect around call to this function for any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ;; critical stuff). The second argument for the loop function is the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;; conditions for any error that occurred or nil if none.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (defun Electric-command-loop (return-tag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 &optional prompt inhibit-quit
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 loop-function loop-state)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 (let (cmd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (err nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (electrified-buffer (current-buffer)) ; XEmacs -
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (prompt-string prompt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 (while t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (if (not (or (stringp prompt) (eq prompt nil) (eq prompt 'noprompt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (setq prompt-string (funcall prompt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (if (not (stringp prompt-string))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (if (eq prompt-string 'noprompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (setq prompt-string nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (setq prompt-string "->")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (setq cmd (read-key-sequence prompt-string))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (or prefix-arg (setq last-command this-command))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (setq last-command-event (aref cmd (1- (length cmd)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 current-mouse-event
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (and (or (button-press-event-p last-command-event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (button-release-event-p last-command-event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (menu-event-p last-command-event))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 last-command-event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 this-command (if (menu-event-p last-command-event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 last-command-event
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (key-binding cmd t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 cmd this-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 ;; This makes universal-argument-other-key work.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (setq universal-argument-num-events 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (if (or (prog1 quit-flag (setq quit-flag nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (eq (event-to-character last-input-event) (quit-char)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (progn (setq unread-command-events nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 prefix-arg nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 ;; If it wasn't cancelling a prefix character, then quit.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (if (or (= (length (this-command-keys)) 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (not inhibit-quit)) ; safety
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (progn (ding nil 'quit) ; XEmacs -
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (message "Quit")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (throw return-tag nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (setq cmd nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (setq current-prefix-arg prefix-arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (if cmd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (condition-case conditions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (progn (if (eventp cmd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (let ((b (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (dispatch-event cmd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (if (not (eq b (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (throw return-tag (current-buffer)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (command-execute cmd))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (setq last-command this-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (if (or (prog1 quit-flag (setq quit-flag nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (eq (event-to-character last-input-event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (quit-char)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (progn (setq unread-command-events nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (if (not inhibit-quit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (progn (ding nil 'quit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (message "Quit")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (throw return-tag nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (message "Quit inhibited")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (ding)))))
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
116 (error (command-error conditions) ; XEmacs
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
117 (sit-for 2)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
118 (ding nil 'undefined-key))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
119 (and (not (eq (current-buffer) electrified-buffer)) ; XEmacs -
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
120 (not (eq (selected-window) (minibuffer-window)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
121 (progn (ding nil 'quit)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
122 (message "Leaving electric command loop %s."
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
123 "because buffer has changed")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
124 (sit-for 2)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
125 (throw return-tag nil)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (if loop-function (funcall loop-function loop-state err))))
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
127 ;; ####> - huh? It should be impossible to ever get here...
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (ding nil 'alarm)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (throw return-tag nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 ;; This function is like pop-to-buffer, sort of.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 ;; The algorithm is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 ;; If there is a window displaying buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 ;; Select it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 ;; Else if there is only one window
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 ;; Split it, selecting the window on the bottom with height being
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 ;; the lesser of max-height (if non-nil) and the number of lines in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 ;; the buffer to be displayed subject to window-min-height constraint.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 ;; Else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 ;; Switch to buffer in the current window.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 ;; Then if max-height is nil, and not all of the lines in the buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 ;; are displayed, grab the whole frame.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 ;; Returns selected window on buffer positioned at point-min.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (defun Electric-pop-up-window (buffer &optional max-height)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (let* ((win (or (get-buffer-window buffer) (selected-window)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (buf (get-buffer buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (one-window (one-window-p t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (pop-up-windows t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (target-height)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (lines))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (if (not buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (error "Buffer %s does not exist" buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (set-buffer buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (setq lines (count-lines (point-min) (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (setq target-height
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (min (max (if max-height (min max-height (1+ lines)) (1+ lines))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 window-min-height)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (save-window-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (delete-other-windows)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (1- (window-height (selected-window)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (cond ((and (eq (window-buffer win) buf))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (select-window win))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (one-window
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (goto-char (window-start win))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (pop-to-buffer buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (setq win (selected-window))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (enlarge-window (- target-height (window-height win))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (switch-to-buffer buf)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (if (and (not max-height)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (> target-height (window-height (selected-window))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (progn (goto-char (window-start win))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (enlarge-window (- target-height (window-height win)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 win)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 4
diff changeset
181 (provide 'electric) ; zaaaaaaap
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 ;;; electric.el ends here