Commit | Line | Data |
---|---|---|
0d20f9a0 JB |
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 | |
4 | ||
5 | ;; This file is part of GNU Emacs. | |
6 | ||
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) | |
10 | ;; any later version. | |
11 | ||
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. | |
16 | ||
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. | |
20 | ||
21 | ||
22 | (provide 'electric) ; zaaaaaaap | |
23 | ||
24 | ;; perhaps this should be in subr.el... | |
25 | (defun shrink-window-if-larger-than-buffer (window) | |
26 | (save-excursion | |
27 | (set-buffer (window-buffer window)) | |
28 | (let ((w (selected-window)) ;save-window-excursion can't win | |
29 | (buffer-file-name buffer-file-name) | |
30 | (p (point)) | |
31 | (n 0) | |
32 | (window-min-height 0) | |
33 | (buffer-read-only nil) | |
34 | (modified (buffer-modified-p)) | |
35 | (buffer (current-buffer))) | |
36 | (unwind-protect | |
37 | (progn | |
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) | |
47 | (goto-char p) | |
48 | (select-window w) | |
49 | ;; Make sure we unbind buffer-read-only | |
50 | ;; with the proper current buffer. | |
51 | (set-buffer buffer))))) | |
52 | ||
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. | |
70 | ||
71 | (defun Electric-command-loop (return-tag | |
72 | &optional prompt inhibit-quit | |
73 | loop-function loop-state) | |
74 | (if (not prompt) (setq prompt "->")) | |
75 | (let (cmd (err nil)) | |
76 | (while t | |
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) | |
81 | cmd this-command) | |
82 | (if (or (prog1 quit-flag (setq quit-flag nil)) | |
83 | (= last-input-char ?\C-g)) | |
84 | (progn (setq unread-command-char -1 | |
85 | prefix-arg nil) | |
86 | ;; If it wasn't cancelling a prefix character, then quit. | |
87 | (if (or (= (length (this-command-keys)) 1) | |
88 | (not inhibit-quit)) ; safety | |
89 | (progn (ding) | |
90 | (message "Quit") | |
91 | (throw return-tag nil)) | |
92 | (setq cmd nil)))) | |
93 | (setq current-prefix-arg prefix-arg) | |
94 | (if cmd | |
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) | |
101 | (progn (ding) | |
102 | (message "Quit") | |
103 | (throw return-tag nil)) | |
104 | (ding))))) | |
105 | (buffer-read-only (if loop-function | |
106 | (setq err conditions) | |
107 | (ding) | |
108 | (message "Buffer is read-only") | |
109 | (sit-for 2))) | |
110 | (beginning-of-buffer (if loop-function | |
111 | (setq err conditions) | |
112 | (ding) | |
113 | (message "Beginning of Buffer") | |
114 | (sit-for 2))) | |
115 | (end-of-buffer (if loop-function | |
116 | (setq err conditions) | |
117 | (ding) | |
118 | (message "End of Buffer") | |
119 | (sit-for 2))) | |
120 | (error (if loop-function | |
121 | (setq err conditions) | |
122 | (ding) | |
123 | (message "Error: %s" | |
124 | (if (eq (car conditions) 'error) | |
125 | (car (cdr conditions)) | |
126 | (prin1-to-string conditions))) | |
127 | (sit-for 2)))) | |
128 | (ding)) | |
129 | (if loop-function (funcall loop-function loop-state err)))) | |
130 | (ding) | |
131 | (throw return-tag nil)) | |
132 | ||
133 | ;; This function is like pop-to-buffer, sort of. | |
134 | ;; The algorithm is | |
135 | ;; If there is a window displaying buffer | |
136 | ;; Select it | |
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. | |
141 | ;; Else | |
142 | ;; Switch to buffer in the current window. | |
143 | ;; | |
144 | ;; Then if max-height is nil, and not all of the lines in the buffer | |
145 | ;; are displayed, grab the whole screen. | |
146 | ;; | |
147 | ;; Returns selected window on buffer positioned at point-min. | |
148 | ||
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)) | |
153 | (pop-up-windows t) | |
154 | (target-height) | |
155 | (lines)) | |
156 | (if (not buf) | |
157 | (error "Buffer %s does not exist" buffer) | |
158 | (save-excursion | |
159 | (set-buffer buf) | |
160 | (setq lines (count-lines (point-min) (point-max))) | |
161 | (setq target-height | |
162 | (min (max (if max-height (min max-height (1+ lines)) (1+ lines)) | |
163 | window-min-height) | |
164 | (save-window-excursion | |
165 | (delete-other-windows) | |
166 | (1- (window-height (selected-window))))))) | |
167 | (cond ((and (eq (window-buffer win) buf)) | |
168 | (select-window win)) | |
169 | (one-window | |
170 | (goto-char (window-start win)) | |
171 | (pop-to-buffer buffer) | |
172 | (setq win (selected-window)) | |
173 | (enlarge-window (- target-height (window-height win)))) | |
174 | (t | |
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)) | |
181 | win))) |