Require cl only when compiling.
[emacs.git] / lisp / electric.el
blob2b95a1c2a3ada0d7e0ac8b75dc6b267c6028a9d3
1 ;;; electric.el --- window maker and Command loop for `electric' modes.
3 ;; Copyright (C) 1985, 1986, 1995 Free Software Foundation, Inc.
5 ;; Author: K. Shane Hartman
6 ;; Maintainer: FSF
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it 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.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ; zaaaaaaap
30 ;;; Code:
32 ;; This loop is the guts for non-standard modes which retain control
33 ;; until some event occurs. It is a `do-forever', the only way out is
34 ;; to throw. It assumes that you have set up the keymap, window, and
35 ;; everything else: all it does is read commands and execute them -
36 ;; providing error messages should one occur (if there is no loop
37 ;; function - which see). The required argument is a tag which should
38 ;; expect a value of nil if the user decides to punt. The second
39 ;; argument is the prompt to be used: if nil, use "->", if 'noprompt,
40 ;; don't use a prompt, if a string, use that string as prompt, and if
41 ;; a function of no variable, it will be evaluated in every iteration
42 ;; of the loop and its return value, which can be nil, 'noprompt or a
43 ;; string, will be used as prompt. Given third argument non-nil, it
44 ;; INHIBITS quitting unless the user types C-g at toplevel. This is
45 ;; so user can do things like C-u C-g and not get thrown out. Fourth
46 ;; argument, if non-nil, should be a function of two arguments which
47 ;; is called after every command is executed. The fifth argument, if
48 ;; provided, is the state variable for the function. If the
49 ;; loop-function gets an error, the loop will abort WITHOUT throwing
50 ;; (moral: use unwind-protect around call to this function for any
51 ;; critical stuff). The second argument for the loop function is the
52 ;; conditions for any error that occurred or nil if none.
54 (defun Electric-command-loop (return-tag
55 &optional prompt inhibit-quit
56 loop-function loop-state)
58 (let (cmd
59 (err nil)
60 (prompt-string prompt))
61 (while t
62 (if (not (or (stringp prompt) (eq prompt nil) (eq prompt 'noprompt)))
63 (setq prompt-string (funcall prompt)))
64 (if (not (stringp prompt-string))
65 (if (eq prompt-string 'noprompt)
66 (setq prompt-string nil)
67 (setq prompt-string "->")))
68 (setq cmd (read-key-sequence prompt-string))
69 (setq last-command-char (aref cmd (1- (length cmd)))
70 this-command (key-binding cmd)
71 cmd this-command)
72 (if (or (prog1 quit-flag (setq quit-flag nil))
73 (eq last-input-char ?\C-g))
74 (progn (setq unread-command-events nil
75 prefix-arg nil)
76 ;; If it wasn't cancelling a prefix character, then quit.
77 (if (or (= (length (this-command-keys)) 1)
78 (not inhibit-quit)) ; safety
79 (progn (ding)
80 (message "Quit")
81 (throw return-tag nil))
82 (setq cmd nil))))
83 (setq current-prefix-arg prefix-arg)
84 (if cmd
85 (condition-case conditions
86 (progn (command-execute cmd)
87 (setq last-command this-command)
88 (if (or (prog1 quit-flag (setq quit-flag nil))
89 (eq last-input-char ?\C-g))
90 (progn (setq unread-command-events nil)
91 (if (not inhibit-quit)
92 (progn (ding)
93 (message "Quit")
94 (throw return-tag nil))
95 (ding)))))
96 (buffer-read-only (if loop-function
97 (setq err conditions)
98 (ding)
99 (message "Buffer is read-only")
100 (sit-for 2)))
101 (beginning-of-buffer (if loop-function
102 (setq err conditions)
103 (ding)
104 (message "Beginning of Buffer")
105 (sit-for 2)))
106 (end-of-buffer (if loop-function
107 (setq err conditions)
108 (ding)
109 (message "End of Buffer")
110 (sit-for 2)))
111 (error (if loop-function
112 (setq err conditions)
113 (ding)
114 (message "Error: %s"
115 (if (eq (car conditions) 'error)
116 (car (cdr conditions))
117 (prin1-to-string conditions)))
118 (sit-for 2))))
119 (ding))
120 (if loop-function (funcall loop-function loop-state err))))
121 (ding)
122 (throw return-tag nil))
124 ;; This function is like pop-to-buffer, sort of.
125 ;; The algorithm is
126 ;; If there is a window displaying buffer
127 ;; Select it
128 ;; Else if there is only one window
129 ;; Split it, selecting the window on the bottom with height being
130 ;; the lesser of max-height (if non-nil) and the number of lines in
131 ;; the buffer to be displayed subject to window-min-height constraint.
132 ;; Else
133 ;; Switch to buffer in the current window.
135 ;; Then if max-height is nil, and not all of the lines in the buffer
136 ;; are displayed, grab the whole frame.
138 ;; Returns selected window on buffer positioned at point-min.
140 (defun Electric-pop-up-window (buffer &optional max-height)
141 (let* ((win (or (get-buffer-window buffer) (selected-window)))
142 (buf (get-buffer buffer))
143 (one-window (one-window-p t))
144 (pop-up-windows t)
145 (target-height)
146 (lines))
147 (if (not buf)
148 (error "Buffer %s does not exist" buffer)
149 (save-excursion
150 (set-buffer buf)
151 (setq lines (count-lines (point-min) (point-max)))
152 (setq target-height
153 (min (max (if max-height (min max-height (1+ lines)) (1+ lines))
154 window-min-height)
155 (save-window-excursion
156 (delete-other-windows)
157 (1- (window-height (selected-window)))))))
158 (cond ((and (eq (window-buffer win) buf))
159 (select-window win))
160 (one-window
161 (goto-char (window-start win))
162 (pop-to-buffer buffer)
163 (setq win (selected-window))
164 (enlarge-window (- target-height (window-height win))))
166 (switch-to-buffer buf)))
167 (if (and (not max-height)
168 (> target-height (window-height (selected-window))))
169 (progn (goto-char (window-start win))
170 (enlarge-window (- target-height (window-height win)))))
171 (goto-char (point-min))
172 win)))
174 (provide 'electric)
176 ;;; electric.el ends here