Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; electric.el --- window maker and Command loop for `electric' modes |
c0274f38 | 2 | |
e91081eb | 3 | ;; Copyright (C) 1985, 1986, 1995, 2001, 2002, 2003, 2004, |
409cc4a3 | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
9750e079 | 5 | |
e5167999 ER |
6 | ;; Author: K. Shane Hartman |
7 | ;; Maintainer: FSF | |
fd7fa35a | 8 | ;; Keywords: extensions |
e5167999 | 9 | |
0d20f9a0 JB |
10 | ;; This file is part of GNU Emacs. |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
0d20f9a0 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
0d20f9a0 JB |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
0d20f9a0 | 24 | |
c8472948 | 25 | ;;; Commentary: |
0d20f9a0 | 26 | |
49116ac0 | 27 | ; zaaaaaaap |
0d20f9a0 | 28 | |
c8472948 ER |
29 | ;;; Code: |
30 | ||
0d20f9a0 | 31 | ;; This loop is the guts for non-standard modes which retain control |
f135afd3 RS |
32 | ;; until some event occurs. It is a `do-forever', the only way out is |
33 | ;; to throw. It assumes that you have set up the keymap, window, and | |
0d20f9a0 JB |
34 | ;; everything else: all it does is read commands and execute them - |
35 | ;; providing error messages should one occur (if there is no loop | |
36 | ;; function - which see). The required argument is a tag which should | |
f135afd3 RS |
37 | ;; expect a value of nil if the user decides to punt. The second |
38 | ;; argument is the prompt to be used: if nil, use "->", if 'noprompt, | |
39 | ;; don't use a prompt, if a string, use that string as prompt, and if | |
40 | ;; a function of no variable, it will be evaluated in every iteration | |
41 | ;; of the loop and its return value, which can be nil, 'noprompt or a | |
42 | ;; string, will be used as prompt. Given third argument non-nil, it | |
43 | ;; INHIBITS quitting unless the user types C-g at toplevel. This is | |
44 | ;; so user can do things like C-u C-g and not get thrown out. Fourth | |
45 | ;; argument, if non-nil, should be a function of two arguments which | |
46 | ;; is called after every command is executed. The fifth argument, if | |
47 | ;; provided, is the state variable for the function. If the | |
0d20f9a0 JB |
48 | ;; loop-function gets an error, the loop will abort WITHOUT throwing |
49 | ;; (moral: use unwind-protect around call to this function for any | |
50 | ;; critical stuff). The second argument for the loop function is the | |
51 | ;; conditions for any error that occurred or nil if none. | |
52 | ||
53 | (defun Electric-command-loop (return-tag | |
54 | &optional prompt inhibit-quit | |
55 | loop-function loop-state) | |
f135afd3 | 56 | |
71296446 JB |
57 | (let (cmd |
58 | (err nil) | |
f135afd3 | 59 | (prompt-string prompt)) |
0d20f9a0 | 60 | (while t |
f135afd3 RS |
61 | (if (not (or (stringp prompt) (eq prompt nil) (eq prompt 'noprompt))) |
62 | (setq prompt-string (funcall prompt))) | |
63 | (if (not (stringp prompt-string)) | |
64 | (if (eq prompt-string 'noprompt) | |
65 | (setq prompt-string nil) | |
66 | (setq prompt-string "->"))) | |
67 | (setq cmd (read-key-sequence prompt-string)) | |
0d20f9a0 | 68 | (setq last-command-char (aref cmd (1- (length cmd))) |
f867d8d3 | 69 | this-command (key-binding cmd t) |
0d20f9a0 | 70 | cmd this-command) |
f867d8d3 RS |
71 | ;; This makes universal-argument-other-key work. |
72 | (setq universal-argument-num-events 0) | |
0d20f9a0 | 73 | (if (or (prog1 quit-flag (setq quit-flag nil)) |
7dc8eff9 | 74 | (eq last-input-char ?\C-g)) |
dbc4e1c1 | 75 | (progn (setq unread-command-events nil |
0d20f9a0 JB |
76 | prefix-arg nil) |
77 | ;; If it wasn't cancelling a prefix character, then quit. | |
78 | (if (or (= (length (this-command-keys)) 1) | |
79 | (not inhibit-quit)) ; safety | |
80 | (progn (ding) | |
81 | (message "Quit") | |
82 | (throw return-tag nil)) | |
83 | (setq cmd nil)))) | |
84 | (setq current-prefix-arg prefix-arg) | |
85 | (if cmd | |
86 | (condition-case conditions | |
87 | (progn (command-execute cmd) | |
17c17ec9 | 88 | (setq last-command this-command) |
0d20f9a0 | 89 | (if (or (prog1 quit-flag (setq quit-flag nil)) |
7dc8eff9 | 90 | (eq last-input-char ?\C-g)) |
dbc4e1c1 | 91 | (progn (setq unread-command-events nil) |
0d20f9a0 JB |
92 | (if (not inhibit-quit) |
93 | (progn (ding) | |
94 | (message "Quit") | |
95 | (throw return-tag nil)) | |
96 | (ding))))) | |
97 | (buffer-read-only (if loop-function | |
98 | (setq err conditions) | |
99 | (ding) | |
100 | (message "Buffer is read-only") | |
101 | (sit-for 2))) | |
102 | (beginning-of-buffer (if loop-function | |
103 | (setq err conditions) | |
104 | (ding) | |
105 | (message "Beginning of Buffer") | |
106 | (sit-for 2))) | |
107 | (end-of-buffer (if loop-function | |
108 | (setq err conditions) | |
109 | (ding) | |
110 | (message "End of Buffer") | |
111 | (sit-for 2))) | |
112 | (error (if loop-function | |
113 | (setq err conditions) | |
114 | (ding) | |
115 | (message "Error: %s" | |
116 | (if (eq (car conditions) 'error) | |
117 | (car (cdr conditions)) | |
118 | (prin1-to-string conditions))) | |
119 | (sit-for 2)))) | |
120 | (ding)) | |
121 | (if loop-function (funcall loop-function loop-state err)))) | |
122 | (ding) | |
123 | (throw return-tag nil)) | |
124 | ||
71296446 | 125 | ;; This function is like pop-to-buffer, sort of. |
0d20f9a0 JB |
126 | ;; The algorithm is |
127 | ;; If there is a window displaying buffer | |
128 | ;; Select it | |
129 | ;; Else if there is only one window | |
130 | ;; Split it, selecting the window on the bottom with height being | |
131 | ;; the lesser of max-height (if non-nil) and the number of lines in | |
132 | ;; the buffer to be displayed subject to window-min-height constraint. | |
133 | ;; Else | |
134 | ;; Switch to buffer in the current window. | |
135 | ;; | |
136 | ;; Then if max-height is nil, and not all of the lines in the buffer | |
f98955ea | 137 | ;; are displayed, grab the whole frame. |
0d20f9a0 JB |
138 | ;; |
139 | ;; Returns selected window on buffer positioned at point-min. | |
140 | ||
141 | (defun Electric-pop-up-window (buffer &optional max-height) | |
142 | (let* ((win (or (get-buffer-window buffer) (selected-window))) | |
143 | (buf (get-buffer buffer)) | |
144 | (one-window (one-window-p t)) | |
145 | (pop-up-windows t) | |
c45c149c | 146 | (pop-up-frames nil)) |
0d20f9a0 JB |
147 | (if (not buf) |
148 | (error "Buffer %s does not exist" buffer) | |
0d20f9a0 JB |
149 | (cond ((and (eq (window-buffer win) buf)) |
150 | (select-window win)) | |
151 | (one-window | |
0d20f9a0 | 152 | (pop-to-buffer buffer) |
c45c149c | 153 | (setq win (selected-window))) |
0d20f9a0 JB |
154 | (t |
155 | (switch-to-buffer buf))) | |
48238d1d | 156 | ;; Don't shrink the window, but expand it if necessary. |
0d20f9a0 | 157 | (goto-char (point-min)) |
48238d1d CY |
158 | (unless (= (point-max) (window-end win)) |
159 | (fit-window-to-buffer win max-height)) | |
0d20f9a0 | 160 | win))) |
49116ac0 | 161 | |
c0274f38 ER |
162 | (provide 'electric) |
163 | ||
cbee283d | 164 | ;; arch-tag: dae045eb-dc2d-4fb7-9f27-9cc2ce277be8 |
c8472948 | 165 | ;;; electric.el ends here |