1 ;;; electric.el --- window maker and Command loop for `electric' modes.
3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
5 ;; Author: K. Shane Hartman
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)
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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31 ;; This loop is the guts for non-standard modes which retain control
32 ;; until some event occurs. It is a `do-forever', the only way out is to
33 ;; throw. It assumes that you have set up the keymap, window, and
34 ;; everything else: all it does is read commands and execute them -
35 ;; providing error messages should one occur (if there is no loop
36 ;; function - which see). The required argument is a tag which should
37 ;; expect a value of nil if the user decides to punt. The
38 ;; second argument is a prompt string (defaults to "->"). Given third
39 ;; argument non-nil, it INHIBITS quitting unless the user types C-g at
40 ;; toplevel. This is so user can do things like C-u C-g and not get
41 ;; thrown out. Fourth argument, if non-nil, should be a function of two
42 ;; arguments which is called after every command is executed. The fifth
43 ;; argument, if provided, is the state variable for the function. If the
44 ;; loop-function gets an error, the loop will abort WITHOUT throwing
45 ;; (moral: use unwind-protect around call to this function for any
46 ;; critical stuff). The second argument for the loop function is the
47 ;; conditions for any error that occurred or nil if none.
49 (defun Electric-command-loop (return-tag
50 &optional prompt inhibit-quit
51 loop-function loop-state
)
52 (if (not prompt
) (setq prompt
"->"))
55 (setq cmd
(read-key-sequence (if (stringp prompt
)
56 prompt
(funcall prompt
))))
57 (setq last-command-char
(aref cmd
(1- (length cmd
)))
58 this-command
(key-binding cmd
)
60 (if (or (prog1 quit-flag
(setq quit-flag nil
))
61 (= last-input-char ?\C-g
))
62 (progn (setq unread-command-events nil
64 ;; If it wasn't cancelling a prefix character, then quit.
65 (if (or (= (length (this-command-keys)) 1)
66 (not inhibit-quit
)) ; safety
69 (throw return-tag nil
))
71 (setq current-prefix-arg prefix-arg
)
73 (condition-case conditions
74 (progn (command-execute cmd
)
75 (setq last-command this-command
)
76 (if (or (prog1 quit-flag
(setq quit-flag nil
))
77 (= last-input-char ?\C-g
))
78 (progn (setq unread-command-events nil
)
79 (if (not inhibit-quit
)
82 (throw return-tag nil
))
84 (buffer-read-only (if loop-function
87 (message "Buffer is read-only")
89 (beginning-of-buffer (if loop-function
92 (message "Beginning of Buffer")
94 (end-of-buffer (if loop-function
97 (message "End of Buffer")
99 (error (if loop-function
100 (setq err conditions
)
103 (if (eq (car conditions
) 'error
)
104 (car (cdr conditions
))
105 (prin1-to-string conditions
)))
108 (if loop-function
(funcall loop-function loop-state err
))))
110 (throw return-tag nil
))
112 ;; This function is like pop-to-buffer, sort of.
114 ;; If there is a window displaying buffer
116 ;; Else if there is only one window
117 ;; Split it, selecting the window on the bottom with height being
118 ;; the lesser of max-height (if non-nil) and the number of lines in
119 ;; the buffer to be displayed subject to window-min-height constraint.
121 ;; Switch to buffer in the current window.
123 ;; Then if max-height is nil, and not all of the lines in the buffer
124 ;; are displayed, grab the whole frame.
126 ;; Returns selected window on buffer positioned at point-min.
128 (defun Electric-pop-up-window (buffer &optional max-height
)
129 (let* ((win (or (get-buffer-window buffer
) (selected-window)))
130 (buf (get-buffer buffer
))
131 (one-window (one-window-p t
))
136 (error "Buffer %s does not exist" buffer
)
139 (setq lines
(count-lines (point-min) (point-max)))
141 (min (max (if max-height
(min max-height
(1+ lines
)) (1+ lines
))
143 (save-window-excursion
144 (delete-other-windows)
145 (1- (window-height (selected-window)))))))
146 (cond ((and (eq (window-buffer win
) buf
))
149 (goto-char (window-start win
))
150 (pop-to-buffer buffer
)
151 (setq win
(selected-window))
152 (enlarge-window (- target-height
(window-height win
))))
154 (switch-to-buffer buf
)))
155 (if (and (not max-height
)
156 (> target-height
(window-height (selected-window))))
157 (progn (goto-char (window-start win
))
158 (enlarge-window (- target-height
(window-height win
)))))
159 (goto-char (point-min))
164 ;;; electric.el ends here