Mercurial > emacs
annotate lisp/electric.el @ 680:f99c8512cd41
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Wed, 03 Jun 1992 20:43:49 +0000 |
parents | 8a533acedb77 |
children | e2780d9a814c |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; electric.el --- window maker and Command loop for `electric' modes. |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
35 | 3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc. |
4 ;; Principal author K. Shane Hartman | |
5 | |
6 ;; This file is part of GNU Emacs. | |
7 | |
8 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
9 ;; it under the terms of the GNU General Public License as published by | |
10 ;; the Free Software Foundation; either version 1, or (at your option) | |
11 ;; any later version. | |
12 | |
13 ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 ;; GNU General Public License for more details. | |
17 | |
18 ;; You should have received a copy of the GNU General Public License | |
19 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | |
22 | |
584 | 23 ; zaaaaaaap |
35 | 24 |
25 ;; perhaps this should be in subr.el... | |
26 (defun shrink-window-if-larger-than-buffer (window) | |
27 (save-excursion | |
28 (set-buffer (window-buffer window)) | |
29 (let ((w (selected-window)) ;save-window-excursion can't win | |
30 (buffer-file-name buffer-file-name) | |
31 (p (point)) | |
32 (n 0) | |
33 (window-min-height 0) | |
34 (buffer-read-only nil) | |
35 (modified (buffer-modified-p)) | |
36 (buffer (current-buffer))) | |
37 (unwind-protect | |
38 (progn | |
39 (select-window window) | |
40 (goto-char (point-min)) | |
41 (while (pos-visible-in-window-p (point-max)) | |
42 ;; defeat file locking... don't try this at home, kids! | |
43 (setq buffer-file-name nil) | |
44 (insert ?\n) (setq n (1+ n))) | |
45 (if (> n 0) (shrink-window (1- n)))) | |
46 (delete-region (point-min) (point)) | |
47 (set-buffer-modified-p modified) | |
48 (goto-char p) | |
49 (select-window w) | |
50 ;; Make sure we unbind buffer-read-only | |
51 ;; with the proper current buffer. | |
52 (set-buffer buffer))))) | |
53 | |
54 ;; This loop is the guts for non-standard modes which retain control | |
55 ;; until some event occurs. It is a `do-forever', the only way out is to | |
56 ;; throw. It assumes that you have set up the keymap, window, and | |
57 ;; everything else: all it does is read commands and execute them - | |
58 ;; providing error messages should one occur (if there is no loop | |
59 ;; function - which see). The required argument is a tag which should | |
60 ;; expect a value of nil if the user decides to punt. The | |
61 ;; second argument is a prompt string (defaults to "->"). Given third | |
62 ;; argument non-nil, it INHIBITS quitting unless the user types C-g at | |
63 ;; toplevel. This is so user can do things like C-u C-g and not get | |
64 ;; thrown out. Fourth argument, if non-nil, should be a function of two | |
65 ;; arguments which is called after every command is executed. The fifth | |
66 ;; argument, if provided, is the state variable for the function. If the | |
67 ;; loop-function gets an error, the loop will abort WITHOUT throwing | |
68 ;; (moral: use unwind-protect around call to this function for any | |
69 ;; critical stuff). The second argument for the loop function is the | |
70 ;; conditions for any error that occurred or nil if none. | |
71 | |
72 (defun Electric-command-loop (return-tag | |
73 &optional prompt inhibit-quit | |
74 loop-function loop-state) | |
75 (if (not prompt) (setq prompt "->")) | |
76 (let (cmd (err nil)) | |
77 (while t | |
78 (setq cmd (read-key-sequence (if (stringp prompt) | |
79 prompt (funcall prompt)))) | |
80 (setq last-command-char (aref cmd (1- (length cmd))) | |
81 this-command (key-binding cmd) | |
82 cmd this-command) | |
83 (if (or (prog1 quit-flag (setq quit-flag nil)) | |
84 (= last-input-char ?\C-g)) | |
85 (progn (setq unread-command-char -1 | |
86 prefix-arg nil) | |
87 ;; If it wasn't cancelling a prefix character, then quit. | |
88 (if (or (= (length (this-command-keys)) 1) | |
89 (not inhibit-quit)) ; safety | |
90 (progn (ding) | |
91 (message "Quit") | |
92 (throw return-tag nil)) | |
93 (setq cmd nil)))) | |
94 (setq current-prefix-arg prefix-arg) | |
95 (if cmd | |
96 (condition-case conditions | |
97 (progn (command-execute cmd) | |
98 (if (or (prog1 quit-flag (setq quit-flag nil)) | |
99 (= last-input-char ?\C-g)) | |
100 (progn (setq unread-command-char -1) | |
101 (if (not inhibit-quit) | |
102 (progn (ding) | |
103 (message "Quit") | |
104 (throw return-tag nil)) | |
105 (ding))))) | |
106 (buffer-read-only (if loop-function | |
107 (setq err conditions) | |
108 (ding) | |
109 (message "Buffer is read-only") | |
110 (sit-for 2))) | |
111 (beginning-of-buffer (if loop-function | |
112 (setq err conditions) | |
113 (ding) | |
114 (message "Beginning of Buffer") | |
115 (sit-for 2))) | |
116 (end-of-buffer (if loop-function | |
117 (setq err conditions) | |
118 (ding) | |
119 (message "End of Buffer") | |
120 (sit-for 2))) | |
121 (error (if loop-function | |
122 (setq err conditions) | |
123 (ding) | |
124 (message "Error: %s" | |
125 (if (eq (car conditions) 'error) | |
126 (car (cdr conditions)) | |
127 (prin1-to-string conditions))) | |
128 (sit-for 2)))) | |
129 (ding)) | |
130 (if loop-function (funcall loop-function loop-state err)))) | |
131 (ding) | |
132 (throw return-tag nil)) | |
133 | |
134 ;; This function is like pop-to-buffer, sort of. | |
135 ;; The algorithm is | |
136 ;; If there is a window displaying buffer | |
137 ;; Select it | |
138 ;; Else if there is only one window | |
139 ;; Split it, selecting the window on the bottom with height being | |
140 ;; the lesser of max-height (if non-nil) and the number of lines in | |
141 ;; the buffer to be displayed subject to window-min-height constraint. | |
142 ;; Else | |
143 ;; Switch to buffer in the current window. | |
144 ;; | |
145 ;; Then if max-height is nil, and not all of the lines in the buffer | |
146 ;; are displayed, grab the whole screen. | |
147 ;; | |
148 ;; Returns selected window on buffer positioned at point-min. | |
149 | |
150 (defun Electric-pop-up-window (buffer &optional max-height) | |
151 (let* ((win (or (get-buffer-window buffer) (selected-window))) | |
152 (buf (get-buffer buffer)) | |
153 (one-window (one-window-p t)) | |
154 (pop-up-windows t) | |
155 (target-height) | |
156 (lines)) | |
157 (if (not buf) | |
158 (error "Buffer %s does not exist" buffer) | |
159 (save-excursion | |
160 (set-buffer buf) | |
161 (setq lines (count-lines (point-min) (point-max))) | |
162 (setq target-height | |
163 (min (max (if max-height (min max-height (1+ lines)) (1+ lines)) | |
164 window-min-height) | |
165 (save-window-excursion | |
166 (delete-other-windows) | |
167 (1- (window-height (selected-window))))))) | |
168 (cond ((and (eq (window-buffer win) buf)) | |
169 (select-window win)) | |
170 (one-window | |
171 (goto-char (window-start win)) | |
172 (pop-to-buffer buffer) | |
173 (setq win (selected-window)) | |
174 (enlarge-window (- target-height (window-height win)))) | |
175 (t | |
176 (switch-to-buffer buf))) | |
177 (if (and (not max-height) | |
178 (> target-height (window-height (selected-window)))) | |
179 (progn (goto-char (window-start win)) | |
180 (enlarge-window (- target-height (window-height win))))) | |
181 (goto-char (point-min)) | |
182 win))) | |
584 | 183 |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
184 (provide 'electric) |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
185 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
186 ; electric.el ends here |