* src/term.c: Remove dead code.
[emacs.git] / lisp / electric.el
blobfb3e462efbaf757c06555166ffd9caf67fb2405b
1 ;;; electric.el --- window maker and Command loop for `electric' modes
3 ;; Copyright (C) 1985, 1986, 1995, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: K. Shane Hartman
7 ;; Maintainer: FSF
8 ;; Keywords: extensions
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ; zaaaaaaap
29 ;;; Code:
31 ;; This loop is the guts for non-standard modes which retain control
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
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
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
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.
53 (defun Electric-command-loop (return-tag
54 &optional prompt inhibit-quit
55 loop-function loop-state)
57 (let (cmd
58 (err nil)
59 (prompt-string prompt))
60 (while t
61 (if (functionp prompt)
62 (setq prompt-string (funcall prompt)))
63 (if (not (stringp prompt-string))
64 (setq prompt-string (unless (eq prompt-string 'noprompt) "->")))
65 (setq cmd (read-key-sequence prompt-string))
66 (setq last-command-event (aref cmd (1- (length cmd)))
67 this-command (key-binding cmd t)
68 cmd this-command)
69 ;; This makes universal-argument-other-key work.
70 (setq universal-argument-num-events 0)
71 (if (or (prog1 quit-flag (setq quit-flag nil))
72 (eq last-input-event ?\C-g))
73 (progn (setq unread-command-events nil
74 prefix-arg nil)
75 ;; If it wasn't cancelling a prefix character, then quit.
76 (if (or (= (length (this-command-keys)) 1)
77 (not inhibit-quit)) ; safety
78 (progn (ding)
79 (message "Quit")
80 (throw return-tag nil))
81 (setq cmd nil))))
82 (setq current-prefix-arg prefix-arg)
83 (if cmd
84 (condition-case conditions
85 (progn (command-execute cmd)
86 (setq last-command this-command)
87 (if (or (prog1 quit-flag (setq quit-flag nil))
88 (eq last-input-event ?\C-g))
89 (progn (setq unread-command-events nil)
90 (if (not inhibit-quit)
91 (progn (ding)
92 (message "Quit")
93 (throw return-tag nil))
94 (ding)))))
95 (buffer-read-only (if loop-function
96 (setq err conditions)
97 (ding)
98 (message "Buffer is read-only")
99 (sit-for 2)))
100 (beginning-of-buffer (if loop-function
101 (setq err conditions)
102 (ding)
103 (message "Beginning of Buffer")
104 (sit-for 2)))
105 (end-of-buffer (if loop-function
106 (setq err conditions)
107 (ding)
108 (message "End of Buffer")
109 (sit-for 2)))
110 (error (if loop-function
111 (setq err conditions)
112 (ding)
113 (message "Error: %s"
114 (if (eq (car conditions) 'error)
115 (car (cdr conditions))
116 (prin1-to-string conditions)))
117 (sit-for 2))))
118 (ding))
119 (if loop-function (funcall loop-function loop-state err))))
120 (ding)
121 (throw return-tag nil))
123 ;; This function is like pop-to-buffer, sort of.
124 ;; The algorithm is
125 ;; If there is a window displaying buffer
126 ;; Select it
127 ;; Else if there is only one window
128 ;; Split it, selecting the window on the bottom with height being
129 ;; the lesser of max-height (if non-nil) and the number of lines in
130 ;; the buffer to be displayed subject to window-min-height constraint.
131 ;; Else
132 ;; Switch to buffer in the current window.
134 ;; Then if max-height is nil, and not all of the lines in the buffer
135 ;; are displayed, grab the whole frame.
137 ;; Returns selected window on buffer positioned at point-min.
139 (defun Electric-pop-up-window (buffer &optional max-height)
140 (let* ((win (or (get-buffer-window buffer) (selected-window)))
141 (buf (get-buffer buffer))
142 (one-window (one-window-p t))
143 (pop-up-windows t)
144 (pop-up-frames nil))
145 (if (not buf)
146 (error "Buffer %s does not exist" buffer)
147 (cond ((and (eq (window-buffer win) buf))
148 (select-window win))
149 (one-window
150 (pop-to-buffer buffer)
151 (setq win (selected-window)))
153 (switch-to-buffer buf)))
154 ;; Don't shrink the window, but expand it if necessary.
155 (goto-char (point-min))
156 (unless (= (point-max) (window-end win t))
157 (fit-window-to-buffer win max-height))
158 win)))
160 (provide 'electric)
162 ;; arch-tag: dae045eb-dc2d-4fb7-9f27-9cc2ce277be8
163 ;;; electric.el ends here