Commit | Line | Data |
---|---|---|
c0274f38 ER |
1 | ;;; electric.el --- window maker and Command loop for `electric' modes. |
2 | ||
e5167999 ER |
3 | ;; Author: K. Shane Hartman |
4 | ;; Maintainer: FSF | |
5 | ;; Last-Modified: 09 Jun 1992 | |
6 | ||
0d20f9a0 | 7 | ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc. |
0d20f9a0 JB |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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 | |
e5167999 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
0d20f9a0 JB |
14 | ;; any later version. |
15 | ||
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. | |
20 | ||
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. | |
24 | ||
25 | ||
49116ac0 | 26 | ; zaaaaaaap |
0d20f9a0 JB |
27 | |
28 | ;; perhaps this should be in subr.el... | |
29 | (defun shrink-window-if-larger-than-buffer (window) | |
30 | (save-excursion | |
31 | (set-buffer (window-buffer window)) | |
32 | (let ((w (selected-window)) ;save-window-excursion can't win | |
33 | (buffer-file-name buffer-file-name) | |
34 | (p (point)) | |
35 | (n 0) | |
36 | (window-min-height 0) | |
37 | (buffer-read-only nil) | |
38 | (modified (buffer-modified-p)) | |
39 | (buffer (current-buffer))) | |
40 | (unwind-protect | |
41 | (progn | |
42 | (select-window window) | |
43 | (goto-char (point-min)) | |
44 | (while (pos-visible-in-window-p (point-max)) | |
45 | ;; defeat file locking... don't try this at home, kids! | |
46 | (setq buffer-file-name nil) | |
47 | (insert ?\n) (setq n (1+ n))) | |
48 | (if (> n 0) (shrink-window (1- n)))) | |
49 | (delete-region (point-min) (point)) | |
50 | (set-buffer-modified-p modified) | |
51 | (goto-char p) | |
52 | (select-window w) | |
53 | ;; Make sure we unbind buffer-read-only | |
54 | ;; with the proper current buffer. | |
55 | (set-buffer buffer))))) | |
56 | ||
57 | ;; This loop is the guts for non-standard modes which retain control | |
58 | ;; until some event occurs. It is a `do-forever', the only way out is to | |
59 | ;; throw. It assumes that you have set up the keymap, window, and | |
60 | ;; everything else: all it does is read commands and execute them - | |
61 | ;; providing error messages should one occur (if there is no loop | |
62 | ;; function - which see). The required argument is a tag which should | |
63 | ;; expect a value of nil if the user decides to punt. The | |
64 | ;; second argument is a prompt string (defaults to "->"). Given third | |
65 | ;; argument non-nil, it INHIBITS quitting unless the user types C-g at | |
66 | ;; toplevel. This is so user can do things like C-u C-g and not get | |
67 | ;; thrown out. Fourth argument, if non-nil, should be a function of two | |
68 | ;; arguments which is called after every command is executed. The fifth | |
69 | ;; argument, if provided, is the state variable for the function. If the | |
70 | ;; loop-function gets an error, the loop will abort WITHOUT throwing | |
71 | ;; (moral: use unwind-protect around call to this function for any | |
72 | ;; critical stuff). The second argument for the loop function is the | |
73 | ;; conditions for any error that occurred or nil if none. | |
74 | ||
75 | (defun Electric-command-loop (return-tag | |
76 | &optional prompt inhibit-quit | |
77 | loop-function loop-state) | |
78 | (if (not prompt) (setq prompt "->")) | |
79 | (let (cmd (err nil)) | |
80 | (while t | |
81 | (setq cmd (read-key-sequence (if (stringp prompt) | |
82 | prompt (funcall prompt)))) | |
83 | (setq last-command-char (aref cmd (1- (length cmd))) | |
84 | this-command (key-binding cmd) | |
85 | cmd this-command) | |
86 | (if (or (prog1 quit-flag (setq quit-flag nil)) | |
87 | (= last-input-char ?\C-g)) | |
88 | (progn (setq unread-command-char -1 | |
89 | prefix-arg nil) | |
90 | ;; If it wasn't cancelling a prefix character, then quit. | |
91 | (if (or (= (length (this-command-keys)) 1) | |
92 | (not inhibit-quit)) ; safety | |
93 | (progn (ding) | |
94 | (message "Quit") | |
95 | (throw return-tag nil)) | |
96 | (setq cmd nil)))) | |
97 | (setq current-prefix-arg prefix-arg) | |
98 | (if cmd | |
99 | (condition-case conditions | |
100 | (progn (command-execute cmd) | |
17c17ec9 | 101 | (setq last-command this-command) |
0d20f9a0 JB |
102 | (if (or (prog1 quit-flag (setq quit-flag nil)) |
103 | (= last-input-char ?\C-g)) | |
104 | (progn (setq unread-command-char -1) | |
105 | (if (not inhibit-quit) | |
106 | (progn (ding) | |
107 | (message "Quit") | |
108 | (throw return-tag nil)) | |
109 | (ding))))) | |
110 | (buffer-read-only (if loop-function | |
111 | (setq err conditions) | |
112 | (ding) | |
113 | (message "Buffer is read-only") | |
114 | (sit-for 2))) | |
115 | (beginning-of-buffer (if loop-function | |
116 | (setq err conditions) | |
117 | (ding) | |
118 | (message "Beginning of Buffer") | |
119 | (sit-for 2))) | |
120 | (end-of-buffer (if loop-function | |
121 | (setq err conditions) | |
122 | (ding) | |
123 | (message "End of Buffer") | |
124 | (sit-for 2))) | |
125 | (error (if loop-function | |
126 | (setq err conditions) | |
127 | (ding) | |
128 | (message "Error: %s" | |
129 | (if (eq (car conditions) 'error) | |
130 | (car (cdr conditions)) | |
131 | (prin1-to-string conditions))) | |
132 | (sit-for 2)))) | |
133 | (ding)) | |
134 | (if loop-function (funcall loop-function loop-state err)))) | |
135 | (ding) | |
136 | (throw return-tag nil)) | |
137 | ||
138 | ;; This function is like pop-to-buffer, sort of. | |
139 | ;; The algorithm is | |
140 | ;; If there is a window displaying buffer | |
141 | ;; Select it | |
142 | ;; Else if there is only one window | |
143 | ;; Split it, selecting the window on the bottom with height being | |
144 | ;; the lesser of max-height (if non-nil) and the number of lines in | |
145 | ;; the buffer to be displayed subject to window-min-height constraint. | |
146 | ;; Else | |
147 | ;; Switch to buffer in the current window. | |
148 | ;; | |
149 | ;; Then if max-height is nil, and not all of the lines in the buffer | |
f98955ea | 150 | ;; are displayed, grab the whole frame. |
0d20f9a0 JB |
151 | ;; |
152 | ;; Returns selected window on buffer positioned at point-min. | |
153 | ||
154 | (defun Electric-pop-up-window (buffer &optional max-height) | |
155 | (let* ((win (or (get-buffer-window buffer) (selected-window))) | |
156 | (buf (get-buffer buffer)) | |
157 | (one-window (one-window-p t)) | |
158 | (pop-up-windows t) | |
159 | (target-height) | |
160 | (lines)) | |
161 | (if (not buf) | |
162 | (error "Buffer %s does not exist" buffer) | |
163 | (save-excursion | |
164 | (set-buffer buf) | |
165 | (setq lines (count-lines (point-min) (point-max))) | |
166 | (setq target-height | |
167 | (min (max (if max-height (min max-height (1+ lines)) (1+ lines)) | |
168 | window-min-height) | |
169 | (save-window-excursion | |
170 | (delete-other-windows) | |
171 | (1- (window-height (selected-window))))))) | |
172 | (cond ((and (eq (window-buffer win) buf)) | |
173 | (select-window win)) | |
174 | (one-window | |
175 | (goto-char (window-start win)) | |
176 | (pop-to-buffer buffer) | |
177 | (setq win (selected-window)) | |
178 | (enlarge-window (- target-height (window-height win)))) | |
179 | (t | |
180 | (switch-to-buffer buf))) | |
181 | (if (and (not max-height) | |
182 | (> target-height (window-height (selected-window)))) | |
183 | (progn (goto-char (window-start win)) | |
184 | (enlarge-window (- target-height (window-height win))))) | |
185 | (goto-char (point-min)) | |
186 | win))) | |
49116ac0 | 187 | |
c0274f38 ER |
188 | (provide 'electric) |
189 | ||
190 | ; electric.el ends here |