comparison lisp/electric/electric.el @ 0:376386a54a3c r19-14

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