1 ;; electric -- Window maker and Command loop for `electric' modes.
2 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
3 ;; Principal author K. Shane Hartman
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 (provide 'electric
) ; zaaaaaaap
24 ;; perhaps this should be in subr.el...
25 (defun shrink-window-if-larger-than-buffer (window)
27 (set-buffer (window-buffer window
))
28 (let ((w (selected-window)) ;save-window-excursion can't win
29 (buffer-file-name buffer-file-name
)
33 (buffer-read-only nil
)
34 (modified (buffer-modified-p))
35 (buffer (current-buffer)))
38 (select-window window
)
39 (goto-char (point-min))
40 (while (pos-visible-in-window-p (point-max))
41 ;; defeat file locking... don't try this at home, kids!
42 (setq buffer-file-name nil
)
43 (insert ?
\n) (setq n
(1+ n
)))
44 (if (> n
0) (shrink-window (1- n
))))
45 (delete-region (point-min) (point))
46 (set-buffer-modified-p modified
)
49 ;; Make sure we unbind buffer-read-only
50 ;; with the proper current buffer.
51 (set-buffer buffer
)))))
53 ;; This loop is the guts for non-standard modes which retain control
54 ;; until some event occurs. It is a `do-forever', the only way out is to
55 ;; throw. It assumes that you have set up the keymap, window, and
56 ;; everything else: all it does is read commands and execute them -
57 ;; providing error messages should one occur (if there is no loop
58 ;; function - which see). The required argument is a tag which should
59 ;; expect a value of nil if the user decides to punt. The
60 ;; second argument is a prompt string (defaults to "->"). Given third
61 ;; argument non-nil, it INHIBITS quitting unless the user types C-g at
62 ;; toplevel. This is so user can do things like C-u C-g and not get
63 ;; thrown out. Fourth argument, if non-nil, should be a function of two
64 ;; arguments which is called after every command is executed. The fifth
65 ;; argument, if provided, is the state variable for the function. If the
66 ;; loop-function gets an error, the loop will abort WITHOUT throwing
67 ;; (moral: use unwind-protect around call to this function for any
68 ;; critical stuff). The second argument for the loop function is the
69 ;; conditions for any error that occurred or nil if none.
71 (defun Electric-command-loop (return-tag
72 &optional prompt inhibit-quit
73 loop-function loop-state
)
74 (if (not prompt
) (setq prompt
"->"))
77 (setq cmd
(read-key-sequence (if (stringp prompt
)
78 prompt
(funcall prompt
))))
79 (setq last-command-char
(aref cmd
(1- (length cmd
)))
80 this-command
(key-binding cmd
)
82 (if (or (prog1 quit-flag
(setq quit-flag nil
))
83 (= last-input-char ?\C-g
))
84 (progn (setq unread-command-char -
1
86 ;; If it wasn't cancelling a prefix character, then quit.
87 (if (or (= (length (this-command-keys)) 1)
88 (not inhibit-quit
)) ; safety
91 (throw return-tag nil
))
93 (setq current-prefix-arg prefix-arg
)
95 (condition-case conditions
96 (progn (command-execute cmd
)
97 (if (or (prog1 quit-flag
(setq quit-flag nil
))
98 (= last-input-char ?\C-g
))
99 (progn (setq unread-command-char -
1)
100 (if (not inhibit-quit
)
103 (throw return-tag nil
))
105 (buffer-read-only (if loop-function
106 (setq err conditions
)
108 (message "Buffer is read-only")
110 (beginning-of-buffer (if loop-function
111 (setq err conditions
)
113 (message "Beginning of Buffer")
115 (end-of-buffer (if loop-function
116 (setq err conditions
)
118 (message "End of Buffer")
120 (error (if loop-function
121 (setq err conditions
)
124 (if (eq (car conditions
) 'error
)
125 (car (cdr conditions
))
126 (prin1-to-string conditions
)))
129 (if loop-function
(funcall loop-function loop-state err
))))
131 (throw return-tag nil
))
133 ;; This function is like pop-to-buffer, sort of.
135 ;; If there is a window displaying buffer
137 ;; Else if there is only one window
138 ;; Split it, selecting the window on the bottom with height being
139 ;; the lesser of max-height (if non-nil) and the number of lines in
140 ;; the buffer to be displayed subject to window-min-height constraint.
142 ;; Switch to buffer in the current window.
144 ;; Then if max-height is nil, and not all of the lines in the buffer
145 ;; are displayed, grab the whole screen.
147 ;; Returns selected window on buffer positioned at point-min.
149 (defun Electric-pop-up-window (buffer &optional max-height
)
150 (let* ((win (or (get-buffer-window buffer
) (selected-window)))
151 (buf (get-buffer buffer
))
152 (one-window (one-window-p t
))
157 (error "Buffer %s does not exist" buffer
)
160 (setq lines
(count-lines (point-min) (point-max)))
162 (min (max (if max-height
(min max-height
(1+ lines
)) (1+ lines
))
164 (save-window-excursion
165 (delete-other-windows)
166 (1- (window-height (selected-window)))))))
167 (cond ((and (eq (window-buffer win
) buf
))
170 (goto-char (window-start win
))
171 (pop-to-buffer buffer
)
172 (setq win
(selected-window))
173 (enlarge-window (- target-height
(window-height win
))))
175 (switch-to-buffer buf
)))
176 (if (and (not max-height
)
177 (> target-height
(window-height (selected-window))))
178 (progn (goto-char (window-start win
))
179 (enlarge-window (- target-height
(window-height win
)))))
180 (goto-char (point-min))