Info on the terminal-package-finding algorithm.
[emacs.git] / lisp / electric.el
blob5272d7068451cbf580a5b864fd0b046f1837eb46
1 ;;; electric.el --- window maker and Command loop for `electric' modes.
3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
5 ;; Author: K. Shane Hartman
6 ;; Maintainer: FSF
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
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.
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.
25 ;;; Commentary:
27 ; zaaaaaaap
29 ;;; Code:
31 ;; perhaps this should be in subr.el...
32 (defun shrink-window-if-larger-than-buffer (window)
33 (save-excursion
34 (set-buffer (window-buffer window))
35 (let ((w (selected-window)) ;save-window-excursion can't win
36 (buffer-file-name buffer-file-name)
37 (p (point))
38 (n 0)
39 (window-min-height 0)
40 (buffer-read-only nil)
41 (modified (buffer-modified-p))
42 (buffer (current-buffer)))
43 (unwind-protect
44 (progn
45 (select-window window)
46 (goto-char (point-min))
47 (while (pos-visible-in-window-p (point-max))
48 ;; defeat file locking... don't try this at home, kids!
49 (setq buffer-file-name nil)
50 (insert ?\n) (setq n (1+ n)))
51 (if (> n 0) (shrink-window (1- n))))
52 (delete-region (point-min) (point))
53 (set-buffer-modified-p modified)
54 (goto-char p)
55 (select-window w)
56 ;; Make sure we unbind buffer-read-only
57 ;; with the proper current buffer.
58 (set-buffer buffer)))))
60 ;; This loop is the guts for non-standard modes which retain control
61 ;; until some event occurs. It is a `do-forever', the only way out is to
62 ;; throw. It assumes that you have set up the keymap, window, and
63 ;; everything else: all it does is read commands and execute them -
64 ;; providing error messages should one occur (if there is no loop
65 ;; function - which see). The required argument is a tag which should
66 ;; expect a value of nil if the user decides to punt. The
67 ;; second argument is a prompt string (defaults to "->"). Given third
68 ;; argument non-nil, it INHIBITS quitting unless the user types C-g at
69 ;; toplevel. This is so user can do things like C-u C-g and not get
70 ;; thrown out. Fourth argument, if non-nil, should be a function of two
71 ;; arguments which is called after every command is executed. The fifth
72 ;; argument, if provided, is the state variable for the function. If the
73 ;; loop-function gets an error, the loop will abort WITHOUT throwing
74 ;; (moral: use unwind-protect around call to this function for any
75 ;; critical stuff). The second argument for the loop function is the
76 ;; conditions for any error that occurred or nil if none.
78 (defun Electric-command-loop (return-tag
79 &optional prompt inhibit-quit
80 loop-function loop-state)
81 (if (not prompt) (setq prompt "->"))
82 (let (cmd (err nil))
83 (while t
84 (setq cmd (read-key-sequence (if (stringp prompt)
85 prompt (funcall prompt))))
86 (setq last-command-char (aref cmd (1- (length cmd)))
87 this-command (key-binding cmd)
88 cmd this-command)
89 (if (or (prog1 quit-flag (setq quit-flag nil))
90 (= last-input-char ?\C-g))
91 (progn (setq unread-command-events nil
92 prefix-arg nil)
93 ;; If it wasn't cancelling a prefix character, then quit.
94 (if (or (= (length (this-command-keys)) 1)
95 (not inhibit-quit)) ; safety
96 (progn (ding)
97 (message "Quit")
98 (throw return-tag nil))
99 (setq cmd nil))))
100 (setq current-prefix-arg prefix-arg)
101 (if cmd
102 (condition-case conditions
103 (progn (command-execute cmd)
104 (setq last-command this-command)
105 (if (or (prog1 quit-flag (setq quit-flag nil))
106 (= last-input-char ?\C-g))
107 (progn (setq unread-command-events nil)
108 (if (not inhibit-quit)
109 (progn (ding)
110 (message "Quit")
111 (throw return-tag nil))
112 (ding)))))
113 (buffer-read-only (if loop-function
114 (setq err conditions)
115 (ding)
116 (message "Buffer is read-only")
117 (sit-for 2)))
118 (beginning-of-buffer (if loop-function
119 (setq err conditions)
120 (ding)
121 (message "Beginning of Buffer")
122 (sit-for 2)))
123 (end-of-buffer (if loop-function
124 (setq err conditions)
125 (ding)
126 (message "End of Buffer")
127 (sit-for 2)))
128 (error (if loop-function
129 (setq err conditions)
130 (ding)
131 (message "Error: %s"
132 (if (eq (car conditions) 'error)
133 (car (cdr conditions))
134 (prin1-to-string conditions)))
135 (sit-for 2))))
136 (ding))
137 (if loop-function (funcall loop-function loop-state err))))
138 (ding)
139 (throw return-tag nil))
141 ;; This function is like pop-to-buffer, sort of.
142 ;; The algorithm is
143 ;; If there is a window displaying buffer
144 ;; Select it
145 ;; Else if there is only one window
146 ;; Split it, selecting the window on the bottom with height being
147 ;; the lesser of max-height (if non-nil) and the number of lines in
148 ;; the buffer to be displayed subject to window-min-height constraint.
149 ;; Else
150 ;; Switch to buffer in the current window.
152 ;; Then if max-height is nil, and not all of the lines in the buffer
153 ;; are displayed, grab the whole frame.
155 ;; Returns selected window on buffer positioned at point-min.
157 (defun Electric-pop-up-window (buffer &optional max-height)
158 (let* ((win (or (get-buffer-window buffer) (selected-window)))
159 (buf (get-buffer buffer))
160 (one-window (one-window-p t))
161 (pop-up-windows t)
162 (target-height)
163 (lines))
164 (if (not buf)
165 (error "Buffer %s does not exist" buffer)
166 (save-excursion
167 (set-buffer buf)
168 (setq lines (count-lines (point-min) (point-max)))
169 (setq target-height
170 (min (max (if max-height (min max-height (1+ lines)) (1+ lines))
171 window-min-height)
172 (save-window-excursion
173 (delete-other-windows)
174 (1- (window-height (selected-window)))))))
175 (cond ((and (eq (window-buffer win) buf))
176 (select-window win))
177 (one-window
178 (goto-char (window-start win))
179 (pop-to-buffer buffer)
180 (setq win (selected-window))
181 (enlarge-window (- target-height (window-height win))))
183 (switch-to-buffer buf)))
184 (if (and (not max-height)
185 (> target-height (window-height (selected-window))))
186 (progn (goto-char (window-start win))
187 (enlarge-window (- target-height (window-height win)))))
188 (goto-char (point-min))
189 win)))
191 (provide 'electric)
193 ;;; electric.el ends here