Mercurial > emacs
annotate lisp/electric.el @ 2407:1c590cd84ab3
(Fexpand_file_name): Default DEFALT at beginning,
before expanding it. But avoid unneeded or infinite recursive expand.
(Fwrite_region): Set visit_file after expanding file arg.
Also expand VISIT arg if specified.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 28 Mar 1993 08:29:21 +0000 |
parents | 0d9cbf4b25c8 |
children | 0c20256f752d |
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 |
845 | 3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc. |
4 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
5 ;; Author: K. Shane Hartman |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
6 ;; Maintainer: FSF |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
7 ;; Keywords: extensions |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
8 |
35 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
13 ;; the Free Software Foundation; either version 2, or (at your option) |
35 | 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 | |
2229
bd3c525fa6fc
Added standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
25 ;;; Commentary: |
35 | 26 |
584 | 27 ; zaaaaaaap |
35 | 28 |
2229
bd3c525fa6fc
Added standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
29 ;;; Code: |
bd3c525fa6fc
Added standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
30 |
35 | 31 ;; perhaps this should be in subr.el... |
2398
0d9cbf4b25c8
(shrink-window-if-larger-than-buffer) Added doc string. Made argument
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2229
diff
changeset
|
32 (defun shrink-window-if-larger-than-buffer (&optional window) |
0d9cbf4b25c8
(shrink-window-if-larger-than-buffer) Added doc string. Made argument
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2229
diff
changeset
|
33 "Shrink the WINDOW to be as small as possible to display its contents. Do |
0d9cbf4b25c8
(shrink-window-if-larger-than-buffer) Added doc string. Made argument
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2229
diff
changeset
|
34 nothing if only one window is displayed or if the buffer contains more lines |
0d9cbf4b25c8
(shrink-window-if-larger-than-buffer) Added doc string. Made argument
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2229
diff
changeset
|
35 than the present window height." |
35 | 36 (save-excursion |
37 (set-buffer (window-buffer window)) | |
38 (let ((w (selected-window)) ;save-window-excursion can't win | |
39 (buffer-file-name buffer-file-name) | |
40 (p (point)) | |
41 (n 0) | |
42 (window-min-height 0) | |
43 (buffer-read-only nil) | |
44 (modified (buffer-modified-p)) | |
45 (buffer (current-buffer))) | |
46 (unwind-protect | |
47 (progn | |
48 (select-window window) | |
49 (goto-char (point-min)) | |
50 (while (pos-visible-in-window-p (point-max)) | |
51 ;; defeat file locking... don't try this at home, kids! | |
52 (setq buffer-file-name nil) | |
53 (insert ?\n) (setq n (1+ n))) | |
54 (if (> n 0) (shrink-window (1- n)))) | |
55 (delete-region (point-min) (point)) | |
56 (set-buffer-modified-p modified) | |
57 (goto-char p) | |
58 (select-window w) | |
59 ;; Make sure we unbind buffer-read-only | |
60 ;; with the proper current buffer. | |
61 (set-buffer buffer))))) | |
62 | |
63 ;; This loop is the guts for non-standard modes which retain control | |
64 ;; until some event occurs. It is a `do-forever', the only way out is to | |
65 ;; throw. It assumes that you have set up the keymap, window, and | |
66 ;; everything else: all it does is read commands and execute them - | |
67 ;; providing error messages should one occur (if there is no loop | |
68 ;; function - which see). The required argument is a tag which should | |
69 ;; expect a value of nil if the user decides to punt. The | |
70 ;; second argument is a prompt string (defaults to "->"). Given third | |
71 ;; argument non-nil, it INHIBITS quitting unless the user types C-g at | |
72 ;; toplevel. This is so user can do things like C-u C-g and not get | |
73 ;; thrown out. Fourth argument, if non-nil, should be a function of two | |
74 ;; arguments which is called after every command is executed. The fifth | |
75 ;; argument, if provided, is the state variable for the function. If the | |
76 ;; loop-function gets an error, the loop will abort WITHOUT throwing | |
77 ;; (moral: use unwind-protect around call to this function for any | |
78 ;; critical stuff). The second argument for the loop function is the | |
79 ;; conditions for any error that occurred or nil if none. | |
80 | |
81 (defun Electric-command-loop (return-tag | |
82 &optional prompt inhibit-quit | |
83 loop-function loop-state) | |
84 (if (not prompt) (setq prompt "->")) | |
85 (let (cmd (err nil)) | |
86 (while t | |
87 (setq cmd (read-key-sequence (if (stringp prompt) | |
88 prompt (funcall prompt)))) | |
89 (setq last-command-char (aref cmd (1- (length cmd))) | |
90 this-command (key-binding cmd) | |
91 cmd this-command) | |
92 (if (or (prog1 quit-flag (setq quit-flag nil)) | |
93 (= last-input-char ?\C-g)) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1609
diff
changeset
|
94 (progn (setq unread-command-events nil |
35 | 95 prefix-arg nil) |
96 ;; If it wasn't cancelling a prefix character, then quit. | |
97 (if (or (= (length (this-command-keys)) 1) | |
98 (not inhibit-quit)) ; safety | |
99 (progn (ding) | |
100 (message "Quit") | |
101 (throw return-tag nil)) | |
102 (setq cmd nil)))) | |
103 (setq current-prefix-arg prefix-arg) | |
104 (if cmd | |
105 (condition-case conditions | |
106 (progn (command-execute cmd) | |
703
e2780d9a814c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
107 (setq last-command this-command) |
35 | 108 (if (or (prog1 quit-flag (setq quit-flag nil)) |
109 (= last-input-char ?\C-g)) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1609
diff
changeset
|
110 (progn (setq unread-command-events nil) |
35 | 111 (if (not inhibit-quit) |
112 (progn (ding) | |
113 (message "Quit") | |
114 (throw return-tag nil)) | |
115 (ding))))) | |
116 (buffer-read-only (if loop-function | |
117 (setq err conditions) | |
118 (ding) | |
119 (message "Buffer is read-only") | |
120 (sit-for 2))) | |
121 (beginning-of-buffer (if loop-function | |
122 (setq err conditions) | |
123 (ding) | |
124 (message "Beginning of Buffer") | |
125 (sit-for 2))) | |
126 (end-of-buffer (if loop-function | |
127 (setq err conditions) | |
128 (ding) | |
129 (message "End of Buffer") | |
130 (sit-for 2))) | |
131 (error (if loop-function | |
132 (setq err conditions) | |
133 (ding) | |
134 (message "Error: %s" | |
135 (if (eq (car conditions) 'error) | |
136 (car (cdr conditions)) | |
137 (prin1-to-string conditions))) | |
138 (sit-for 2)))) | |
139 (ding)) | |
140 (if loop-function (funcall loop-function loop-state err)))) | |
141 (ding) | |
142 (throw return-tag nil)) | |
143 | |
144 ;; This function is like pop-to-buffer, sort of. | |
145 ;; The algorithm is | |
146 ;; If there is a window displaying buffer | |
147 ;; Select it | |
148 ;; Else if there is only one window | |
149 ;; Split it, selecting the window on the bottom with height being | |
150 ;; the lesser of max-height (if non-nil) and the number of lines in | |
151 ;; the buffer to be displayed subject to window-min-height constraint. | |
152 ;; Else | |
153 ;; Switch to buffer in the current window. | |
154 ;; | |
155 ;; Then if max-height is nil, and not all of the lines in the buffer | |
778 | 156 ;; are displayed, grab the whole frame. |
35 | 157 ;; |
158 ;; Returns selected window on buffer positioned at point-min. | |
159 | |
160 (defun Electric-pop-up-window (buffer &optional max-height) | |
161 (let* ((win (or (get-buffer-window buffer) (selected-window))) | |
162 (buf (get-buffer buffer)) | |
163 (one-window (one-window-p t)) | |
164 (pop-up-windows t) | |
165 (target-height) | |
166 (lines)) | |
167 (if (not buf) | |
168 (error "Buffer %s does not exist" buffer) | |
169 (save-excursion | |
170 (set-buffer buf) | |
171 (setq lines (count-lines (point-min) (point-max))) | |
172 (setq target-height | |
173 (min (max (if max-height (min max-height (1+ lines)) (1+ lines)) | |
174 window-min-height) | |
175 (save-window-excursion | |
176 (delete-other-windows) | |
177 (1- (window-height (selected-window))))))) | |
178 (cond ((and (eq (window-buffer win) buf)) | |
179 (select-window win)) | |
180 (one-window | |
181 (goto-char (window-start win)) | |
182 (pop-to-buffer buffer) | |
183 (setq win (selected-window)) | |
184 (enlarge-window (- target-height (window-height win)))) | |
185 (t | |
186 (switch-to-buffer buf))) | |
187 (if (and (not max-height) | |
188 (> target-height (window-height (selected-window)))) | |
189 (progn (goto-char (window-start win)) | |
190 (enlarge-window (- target-height (window-height win))))) | |
191 (goto-char (point-min)) | |
192 win))) | |
584 | 193 |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
194 (provide 'electric) |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
195 |
2229
bd3c525fa6fc
Added standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
196 ;;; electric.el ends here |