Commit | Line | Data |
---|---|---|
c88ab9ce ER |
1 | ;;; simple.el --- basic editing commands for Emacs |
2 | ||
c6db81aa | 3 | ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
0a487199 | 4 | ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. |
2076c87c | 5 | |
30764597 PJ |
6 | ;; Maintainer: FSF |
7 | ;; Keywords: internal | |
8 | ||
2076c87c JB |
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 | |
3a801d0c | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
2076c87c JB |
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 | |
b578f267 | 22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
2076c87c | 25 | |
d9ecc911 ER |
26 | ;;; Commentary: |
27 | ||
28 | ;; A grab-bag of basic Emacs commands not specifically related to some | |
29 | ;; major mode or to file-handling. | |
30 | ||
3a801d0c | 31 | ;;; Code: |
2076c87c | 32 | |
d01a33cf | 33 | (eval-when-compile |
a339f909 | 34 | (autoload 'widget-convert "wid-edit") |
cc76a58c | 35 | (autoload 'shell-mode "shell")) |
d01a33cf | 36 | |
ca60ee11 JB |
37 | (defvar compilation-current-error) |
38 | ||
7fcce20f RS |
39 | (defcustom idle-update-delay 0.5 |
40 | "*Idle time delay before updating various things on the screen. | |
41 | Various Emacs features that update auxiliary information when point moves | |
42 | wait this many seconds after Emacs becomes idle before doing an update." | |
43 | :type 'number | |
44 | :group 'display | |
45 | :version "22.1") | |
d01a33cf | 46 | |
69c1dd37 | 47 | (defgroup killing nil |
c9f0110e | 48 | "Killing and yanking commands." |
69c1dd37 RS |
49 | :group 'editing) |
50 | ||
69c1dd37 RS |
51 | (defgroup paren-matching nil |
52 | "Highlight (un)matching of parens and expressions." | |
69c1dd37 RS |
53 | :group 'matching) |
54 | ||
7979163c JL |
55 | (defun get-next-valid-buffer (list &optional buffer visible-ok frame) |
56 | "Search LIST for a valid buffer to display in FRAME. | |
a74f9094 KL |
57 | Return nil when all buffers in LIST are undesirable for display, |
58 | otherwise return the first suitable buffer in LIST. | |
59 | ||
60 | Buffers not visible in windows are preferred to visible buffers, | |
61 | unless VISIBLE-OK is non-nil. | |
62 | If the optional argument FRAME is nil, it defaults to the selected frame. | |
7979163c | 63 | If BUFFER is non-nil, ignore occurrences of that buffer in LIST." |
a74f9094 KL |
64 | ;; This logic is more or less copied from other-buffer. |
65 | (setq frame (or frame (selected-frame))) | |
66 | (let ((pred (frame-parameter frame 'buffer-predicate)) | |
67 | found buf) | |
68 | (while (and (not found) list) | |
69 | (setq buf (car list)) | |
70 | (if (and (not (eq buffer buf)) | |
71 | (buffer-live-p buf) | |
72 | (or (null pred) (funcall pred buf)) | |
73 | (not (eq (aref (buffer-name buf) 0) ?\s)) | |
74 | (or visible-ok (null (get-buffer-window buf 'visible)))) | |
75 | (setq found buf) | |
76 | (setq list (cdr list)))) | |
77 | (car list))) | |
78 | ||
7979163c JL |
79 | (defun last-buffer (&optional buffer visible-ok frame) |
80 | "Return the last non-hidden displayable buffer in the buffer list. | |
a74f9094 KL |
81 | If BUFFER is non-nil, last-buffer will ignore that buffer. |
82 | Buffers not visible in windows are preferred to visible buffers, | |
83 | unless optional argument VISIBLE-OK is non-nil. | |
84 | If the optional third argument FRAME is non-nil, use that frame's | |
85 | buffer list instead of the selected frame's buffer list. | |
86 | If no other buffer exists, the buffer `*scratch*' is returned." | |
87 | (setq frame (or frame (selected-frame))) | |
88 | (or (get-next-valid-buffer (frame-parameter frame 'buried-buffer-list) | |
89 | buffer visible-ok frame) | |
90 | (get-next-valid-buffer (nreverse (buffer-list frame)) | |
91 | buffer visible-ok frame) | |
92 | (progn | |
93 | (set-buffer-major-mode (get-buffer-create "*scratch*")) | |
94 | (get-buffer "*scratch*")))) | |
95 | ||
f54b0d85 RS |
96 | (defun next-buffer () |
97 | "Switch to the next buffer in cyclic order." | |
98 | (interactive) | |
a74f9094 KL |
99 | (let ((buffer (current-buffer)) |
100 | (bbl (frame-parameter nil 'buried-buffer-list))) | |
101 | (switch-to-buffer (other-buffer buffer t)) | |
102 | (bury-buffer buffer) | |
103 | (set-frame-parameter nil 'buried-buffer-list | |
104 | (cons buffer (delq buffer bbl))))) | |
105 | ||
106 | (defun previous-buffer () | |
f54b0d85 RS |
107 | "Switch to the previous buffer in cyclic order." |
108 | (interactive) | |
a74f9094 KL |
109 | (let ((buffer (last-buffer (current-buffer) t)) |
110 | (bbl (frame-parameter nil 'buried-buffer-list))) | |
111 | (switch-to-buffer buffer) | |
112 | ;; Clean up buried-buffer-list up to and including the chosen buffer. | |
113 | (while (and bbl (not (eq (car bbl) buffer))) | |
114 | (setq bbl (cdr bbl))) | |
115 | (set-frame-parameter nil 'buried-buffer-list bbl))) | |
116 | ||
ee9c5954 | 117 | \f |
50f007fb | 118 | ;;; next-error support framework |
bbf41690 RS |
119 | |
120 | (defgroup next-error nil | |
f33321ad | 121 | "`next-error' support framework." |
bbf41690 | 122 | :group 'compilation |
bf247b6e | 123 | :version "22.1") |
bbf41690 RS |
124 | |
125 | (defface next-error | |
126 | '((t (:inherit region))) | |
127 | "Face used to highlight next error locus." | |
128 | :group 'next-error | |
bf247b6e | 129 | :version "22.1") |
bbf41690 | 130 | |
7408ee97 | 131 | (defcustom next-error-highlight 0.5 |
bbf41690 | 132 | "*Highlighting of locations in selected source buffers. |
f33321ad | 133 | If number, highlight the locus in `next-error' face for given time in seconds. |
6d3c944b | 134 | If t, highlight the locus indefinitely until some other locus replaces it. |
bbf41690 RS |
135 | If nil, don't highlight the locus in the source buffer. |
136 | If `fringe-arrow', indicate the locus by the fringe arrow." | |
6d3c944b | 137 | :type '(choice (number :tag "Highlight for specified time") |
c81b29e6 | 138 | (const :tag "Semipermanent highlighting" t) |
bbf41690 | 139 | (const :tag "No highlighting" nil) |
6d3c944b | 140 | (const :tag "Fringe arrow" fringe-arrow)) |
bbf41690 | 141 | :group 'next-error |
bf247b6e | 142 | :version "22.1") |
bbf41690 | 143 | |
7408ee97 RS |
144 | (defcustom next-error-highlight-no-select 0.5 |
145 | "*Highlighting of locations in `next-error-no-select'. | |
f33321ad | 146 | If number, highlight the locus in `next-error' face for given time in seconds. |
6d3c944b | 147 | If t, highlight the locus indefinitely until some other locus replaces it. |
bbf41690 RS |
148 | If nil, don't highlight the locus in the source buffer. |
149 | If `fringe-arrow', indicate the locus by the fringe arrow." | |
6d3c944b | 150 | :type '(choice (number :tag "Highlight for specified time") |
c81b29e6 | 151 | (const :tag "Semipermanent highlighting" t) |
bbf41690 | 152 | (const :tag "No highlighting" nil) |
6d3c944b | 153 | (const :tag "Fringe arrow" fringe-arrow)) |
bbf41690 | 154 | :group 'next-error |
bf247b6e | 155 | :version "22.1") |
bbf41690 | 156 | |
d634a3a2 JL |
157 | (defcustom next-error-hook nil |
158 | "*List of hook functions run by `next-error' after visiting source file." | |
159 | :type 'hook | |
160 | :group 'next-error) | |
161 | ||
814c3037 JL |
162 | (defvar next-error-highlight-timer nil) |
163 | ||
9c9b00d6 | 164 | (defvar next-error-overlay-arrow-position nil) |
c29d24ea | 165 | (put 'next-error-overlay-arrow-position 'overlay-arrow-string "=>") |
9c9b00d6 JL |
166 | (add-to-list 'overlay-arrow-variable-list 'next-error-overlay-arrow-position) |
167 | ||
50f007fb | 168 | (defvar next-error-last-buffer nil |
f33321ad | 169 | "The most recent `next-error' buffer. |
50f007fb KS |
170 | A buffer becomes most recent when its compilation, grep, or |
171 | similar mode is started, or when it is used with \\[next-error] | |
172 | or \\[compile-goto-error].") | |
173 | ||
174 | (defvar next-error-function nil | |
e462ab77 SM |
175 | "Function to use to find the next error in the current buffer. |
176 | The function is called with 2 parameters: | |
177 | ARG is an integer specifying by how many errors to move. | |
178 | RESET is a boolean which, if non-nil, says to go back to the beginning | |
179 | of the errors before moving. | |
180 | Major modes providing compile-like functionality should set this variable | |
181 | to indicate to `next-error' that this is a candidate buffer and how | |
182 | to navigate in it.") | |
50f007fb KS |
183 | |
184 | (make-variable-buffer-local 'next-error-function) | |
185 | ||
f1e2a033 | 186 | (defsubst next-error-buffer-p (buffer |
e967cd11 | 187 | &optional avoid-current |
f1e2a033 | 188 | extra-test-inclusive |
5f9e0ca5 | 189 | extra-test-exclusive) |
f33321ad | 190 | "Test if BUFFER is a `next-error' capable buffer. |
e967cd11 RS |
191 | |
192 | If AVOID-CURRENT is non-nil, treat the current buffer | |
193 | as an absolute last resort only. | |
194 | ||
195 | The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer | |
196 | that normally would not qualify. If it returns t, the buffer | |
197 | in question is treated as usable. | |
198 | ||
7979163c | 199 | The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer |
01ba9662 | 200 | that would normally be considered usable. If it returns nil, |
e967cd11 RS |
201 | that buffer is rejected." |
202 | (and (buffer-name buffer) ;First make sure it's live. | |
203 | (not (and avoid-current (eq buffer (current-buffer)))) | |
204 | (with-current-buffer buffer | |
205 | (if next-error-function ; This is the normal test. | |
206 | ;; Optionally reject some buffers. | |
207 | (if extra-test-exclusive | |
208 | (funcall extra-test-exclusive) | |
209 | t) | |
210 | ;; Optionally accept some other buffers. | |
211 | (and extra-test-inclusive | |
212 | (funcall extra-test-inclusive)))))) | |
213 | ||
214 | (defun next-error-find-buffer (&optional avoid-current | |
f1e2a033 | 215 | extra-test-inclusive |
5f9e0ca5 | 216 | extra-test-exclusive) |
f33321ad | 217 | "Return a `next-error' capable buffer. |
7979163c | 218 | |
e967cd11 RS |
219 | If AVOID-CURRENT is non-nil, treat the current buffer |
220 | as an absolute last resort only. | |
221 | ||
01ba9662 | 222 | The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer |
e967cd11 RS |
223 | that normally would not qualify. If it returns t, the buffer |
224 | in question is treated as usable. | |
225 | ||
7979163c | 226 | The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer |
e967cd11 RS |
227 | that would normally be considered usable. If it returns nil, |
228 | that buffer is rejected." | |
03e75c7e JL |
229 | (or |
230 | ;; 1. If one window on the selected frame displays such buffer, return it. | |
231 | (let ((window-buffers | |
232 | (delete-dups | |
233 | (delq nil (mapcar (lambda (w) | |
234 | (if (next-error-buffer-p | |
e967cd11 RS |
235 | (window-buffer w) |
236 | avoid-current | |
f1e2a033 | 237 | extra-test-inclusive extra-test-exclusive) |
03e75c7e JL |
238 | (window-buffer w))) |
239 | (window-list)))))) | |
03e75c7e JL |
240 | (if (eq (length window-buffers) 1) |
241 | (car window-buffers))) | |
e967cd11 | 242 | ;; 2. If next-error-last-buffer is an acceptable buffer, use that. |
03e75c7e | 243 | (if (and next-error-last-buffer |
e967cd11 | 244 | (next-error-buffer-p next-error-last-buffer avoid-current |
f1e2a033 | 245 | extra-test-inclusive extra-test-exclusive)) |
e967cd11 RS |
246 | next-error-last-buffer) |
247 | ;; 3. If the current buffer is acceptable, choose it. | |
248 | (if (next-error-buffer-p (current-buffer) avoid-current | |
249 | extra-test-inclusive extra-test-exclusive) | |
03e75c7e | 250 | (current-buffer)) |
e967cd11 | 251 | ;; 4. Look for any acceptable buffer. |
03e75c7e JL |
252 | (let ((buffers (buffer-list))) |
253 | (while (and buffers | |
e967cd11 RS |
254 | (not (next-error-buffer-p |
255 | (car buffers) avoid-current | |
256 | extra-test-inclusive extra-test-exclusive))) | |
03e75c7e | 257 | (setq buffers (cdr buffers))) |
e967cd11 RS |
258 | (car buffers)) |
259 | ;; 5. Use the current buffer as a last resort if it qualifies, | |
260 | ;; even despite AVOID-CURRENT. | |
261 | (and avoid-current | |
262 | (next-error-buffer-p (current-buffer) nil | |
263 | extra-test-inclusive extra-test-exclusive) | |
264 | (progn | |
265 | (message "This is the only next-error capable buffer") | |
266 | (current-buffer))) | |
267 | ;; 6. Give up. | |
268 | (error "No next-error capable buffer found"))) | |
50f007fb | 269 | |
310abb0b | 270 | (defun next-error (&optional arg reset) |
f33321ad | 271 | "Visit next `next-error' message and corresponding source code. |
50f007fb KS |
272 | |
273 | If all the error messages parsed so far have been processed already, | |
274 | the message buffer is checked for new ones. | |
275 | ||
e462ab77 | 276 | A prefix ARG specifies how many error messages to move; |
50f007fb KS |
277 | negative means move back to previous error messages. |
278 | Just \\[universal-argument] as a prefix means reparse the error message buffer | |
279 | and start at the first error. | |
280 | ||
e249a6d8 | 281 | The RESET argument specifies that we should restart from the beginning. |
50f007fb KS |
282 | |
283 | \\[next-error] normally uses the most recently started | |
284 | compilation, grep, or occur buffer. It can also operate on any | |
285 | buffer with output from the \\[compile], \\[grep] commands, or, | |
286 | more generally, on any buffer in Compilation mode or with | |
287 | Compilation Minor mode enabled, or any buffer in which | |
03e75c7e JL |
288 | `next-error-function' is bound to an appropriate function. |
289 | To specify use of a particular buffer for error messages, type | |
290 | \\[next-error] in that buffer when it is the only one displayed | |
291 | in the current frame. | |
50f007fb | 292 | |
d634a3a2 JL |
293 | Once \\[next-error] has chosen the buffer for error messages, it |
294 | runs `next-error-hook' with `run-hooks', and stays with that buffer | |
295 | until you use it in some other buffer which uses Compilation mode | |
296 | or Compilation Minor mode. | |
50f007fb KS |
297 | |
298 | See variables `compilation-parse-errors-function' and | |
299 | \`compilation-error-regexp-alist' for customization ideas." | |
300 | (interactive "P") | |
e462ab77 | 301 | (if (consp arg) (setq reset t arg nil)) |
50f007fb KS |
302 | (when (setq next-error-last-buffer (next-error-find-buffer)) |
303 | ;; we know here that next-error-function is a valid symbol we can funcall | |
304 | (with-current-buffer next-error-last-buffer | |
d634a3a2 JL |
305 | (funcall next-error-function (prefix-numeric-value arg) reset) |
306 | (run-hooks 'next-error-hook)))) | |
50f007fb | 307 | |
56ab610b RS |
308 | (defun next-error-internal () |
309 | "Visit the source code corresponding to the `next-error' message at point." | |
310 | (setq next-error-last-buffer (current-buffer)) | |
311 | ;; we know here that next-error-function is a valid symbol we can funcall | |
312 | (with-current-buffer next-error-last-buffer | |
313 | (funcall next-error-function 0 nil) | |
314 | (run-hooks 'next-error-hook))) | |
315 | ||
50f007fb KS |
316 | (defalias 'goto-next-locus 'next-error) |
317 | (defalias 'next-match 'next-error) | |
318 | ||
310abb0b | 319 | (defun previous-error (&optional n) |
f33321ad | 320 | "Visit previous `next-error' message and corresponding source code. |
50f007fb KS |
321 | |
322 | Prefix arg N says how many error messages to move backwards (or | |
323 | forwards, if negative). | |
324 | ||
325 | This operates on the output from the \\[compile] and \\[grep] commands." | |
326 | (interactive "p") | |
310abb0b | 327 | (next-error (- (or n 1)))) |
50f007fb | 328 | |
310abb0b | 329 | (defun first-error (&optional n) |
50f007fb KS |
330 | "Restart at the first error. |
331 | Visit corresponding source code. | |
332 | With prefix arg N, visit the source code of the Nth error. | |
333 | This operates on the output from the \\[compile] command, for instance." | |
334 | (interactive "p") | |
335 | (next-error n t)) | |
336 | ||
310abb0b | 337 | (defun next-error-no-select (&optional n) |
f33321ad | 338 | "Move point to the next error in the `next-error' buffer and highlight match. |
50f007fb KS |
339 | Prefix arg N says how many error messages to move forwards (or |
340 | backwards, if negative). | |
341 | Finds and highlights the source line like \\[next-error], but does not | |
342 | select the source buffer." | |
343 | (interactive "p") | |
ee9c5954 JL |
344 | (let ((next-error-highlight next-error-highlight-no-select)) |
345 | (next-error n)) | |
50f007fb KS |
346 | (pop-to-buffer next-error-last-buffer)) |
347 | ||
310abb0b | 348 | (defun previous-error-no-select (&optional n) |
f33321ad | 349 | "Move point to the previous error in the `next-error' buffer and highlight match. |
50f007fb KS |
350 | Prefix arg N says how many error messages to move backwards (or |
351 | forwards, if negative). | |
352 | Finds and highlights the source line like \\[previous-error], but does not | |
353 | select the source buffer." | |
354 | (interactive "p") | |
310abb0b | 355 | (next-error-no-select (- (or n 1)))) |
50f007fb | 356 | |
282d6eae EZ |
357 | ;;; Internal variable for `next-error-follow-mode-post-command-hook'. |
358 | (defvar next-error-follow-last-line nil) | |
359 | ||
2a223f35 | 360 | (define-minor-mode next-error-follow-minor-mode |
282d6eae | 361 | "Minor mode for compilation, occur and diff modes. |
2a223f35 EZ |
362 | When turned on, cursor motion in the compilation, grep, occur or diff |
363 | buffer causes automatic display of the corresponding source code | |
364 | location." | |
ed8e0f0a | 365 | :group 'next-error :init-value nil :lighter " Fol" |
8a98a6c2 | 366 | (if (not next-error-follow-minor-mode) |
282d6eae EZ |
367 | (remove-hook 'post-command-hook 'next-error-follow-mode-post-command-hook t) |
368 | (add-hook 'post-command-hook 'next-error-follow-mode-post-command-hook nil t) | |
e56dd5c6 | 369 | (make-local-variable 'next-error-follow-last-line))) |
282d6eae EZ |
370 | |
371 | ;;; Used as a `post-command-hook' by `next-error-follow-mode' | |
372 | ;;; for the *Compilation* *grep* and *Occur* buffers. | |
373 | (defun next-error-follow-mode-post-command-hook () | |
374 | (unless (equal next-error-follow-last-line (line-number-at-pos)) | |
375 | (setq next-error-follow-last-line (line-number-at-pos)) | |
376 | (condition-case nil | |
377 | (let ((compilation-context-lines nil)) | |
378 | (setq compilation-current-error (point)) | |
379 | (next-error-no-select 0)) | |
380 | (error t)))) | |
381 | ||
ee9c5954 | 382 | \f |
50f007fb KS |
383 | ;;; |
384 | ||
93be67de KH |
385 | (defun fundamental-mode () |
386 | "Major mode not specialized for anything in particular. | |
387 | Other major modes are defined by comparison with this one." | |
388 | (interactive) | |
e174f8db | 389 | (kill-all-local-variables) |
52167727 LK |
390 | (unless delay-mode-hooks |
391 | (run-hooks 'after-change-major-mode-hook))) | |
eaae8106 | 392 | |
93be67de KH |
393 | ;; Making and deleting lines. |
394 | ||
4ea0018b CY |
395 | (defvar hard-newline (propertize "\n" 'hard t 'rear-nonsticky '(hard))) |
396 | ||
30bb9754 | 397 | (defun newline (&optional arg) |
d133d835 | 398 | "Insert a newline, and move to left margin of the new line if it's blank. |
058d4999 DL |
399 | If `use-hard-newlines' is non-nil, the newline is marked with the |
400 | text-property `hard'. | |
76c64e24 | 401 | With ARG, insert that many newlines. |
058d4999 | 402 | Call `auto-fill-function' if the current column number is greater |
6688f85f | 403 | than the value of `fill-column' and ARG is nil." |
30bb9754 | 404 | (interactive "*P") |
4c4cbf11 | 405 | (barf-if-buffer-read-only) |
30bb9754 BG |
406 | ;; Inserting a newline at the end of a line produces better redisplay in |
407 | ;; try_window_id than inserting at the beginning of a line, and the textual | |
408 | ;; result is the same. So, if we're at beginning of line, pretend to be at | |
409 | ;; the end of the previous line. | |
1e722f9f | 410 | (let ((flag (and (not (bobp)) |
30bb9754 | 411 | (bolp) |
1cd24721 RS |
412 | ;; Make sure no functions want to be told about |
413 | ;; the range of the changes. | |
1cd24721 RS |
414 | (not after-change-functions) |
415 | (not before-change-functions) | |
fd977703 RS |
416 | ;; Make sure there are no markers here. |
417 | (not (buffer-has-markers-at (1- (point)))) | |
2f047f6c | 418 | (not (buffer-has-markers-at (point))) |
1cd24721 RS |
419 | ;; Make sure no text properties want to know |
420 | ;; where the change was. | |
421 | (not (get-char-property (1- (point)) 'modification-hooks)) | |
422 | (not (get-char-property (1- (point)) 'insert-behind-hooks)) | |
423 | (or (eobp) | |
424 | (not (get-char-property (point) 'insert-in-front-hooks))) | |
31a5333f MB |
425 | ;; Make sure the newline before point isn't intangible. |
426 | (not (get-char-property (1- (point)) 'intangible)) | |
427 | ;; Make sure the newline before point isn't read-only. | |
428 | (not (get-char-property (1- (point)) 'read-only)) | |
429 | ;; Make sure the newline before point isn't invisible. | |
430 | (not (get-char-property (1- (point)) 'invisible)) | |
431 | ;; Make sure the newline before point has the same | |
432 | ;; properties as the char before it (if any). | |
1e722f9f | 433 | (< (or (previous-property-change (point)) -2) |
d133d835 RS |
434 | (- (point) 2)))) |
435 | (was-page-start (and (bolp) | |
436 | (looking-at page-delimiter))) | |
437 | (beforepos (point))) | |
30bb9754 BG |
438 | (if flag (backward-char 1)) |
439 | ;; Call self-insert so that auto-fill, abbrev expansion etc. happens. | |
440 | ;; Set last-command-char to tell self-insert what to insert. | |
441 | (let ((last-command-char ?\n) | |
442 | ;; Don't auto-fill if we have a numeric argument. | |
3954fff9 RS |
443 | ;; Also not if flag is true (it would fill wrong line); |
444 | ;; there is no need to since we're at BOL. | |
445 | (auto-fill-function (if (or arg flag) nil auto-fill-function))) | |
4cc9d0dc RS |
446 | (unwind-protect |
447 | (self-insert-command (prefix-numeric-value arg)) | |
448 | ;; If we get an error in self-insert-command, put point at right place. | |
449 | (if flag (forward-char 1)))) | |
2f047f6c KH |
450 | ;; Even if we did *not* get an error, keep that forward-char; |
451 | ;; all further processing should apply to the newline that the user | |
452 | ;; thinks he inserted. | |
453 | ||
30bb9754 BG |
454 | ;; Mark the newline(s) `hard'. |
455 | (if use-hard-newlines | |
2f047f6c | 456 | (set-hard-newline-properties |
55741b46 | 457 | (- (point) (if arg (prefix-numeric-value arg) 1)) (point))) |
d133d835 RS |
458 | ;; If the newline leaves the previous line blank, |
459 | ;; and we have a left margin, delete that from the blank line. | |
460 | (or flag | |
461 | (save-excursion | |
462 | (goto-char beforepos) | |
463 | (beginning-of-line) | |
464 | (and (looking-at "[ \t]$") | |
465 | (> (current-left-margin) 0) | |
466 | (delete-region (point) (progn (end-of-line) (point)))))) | |
d133d835 RS |
467 | ;; Indent the line after the newline, except in one case: |
468 | ;; when we added the newline at the beginning of a line | |
469 | ;; which starts a page. | |
470 | (or was-page-start | |
471 | (move-to-left-margin nil t))) | |
30bb9754 BG |
472 | nil) |
473 | ||
55741b46 RS |
474 | (defun set-hard-newline-properties (from to) |
475 | (let ((sticky (get-text-property from 'rear-nonsticky))) | |
476 | (put-text-property from to 'hard 't) | |
477 | ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list | |
478 | (if (and (listp sticky) (not (memq 'hard sticky))) | |
479 | (put-text-property from (point) 'rear-nonsticky | |
480 | (cons 'hard sticky))))) | |
eaae8106 | 481 | |
e249a6d8 | 482 | (defun open-line (n) |
ff1fbe3e | 483 | "Insert a newline and leave point before it. |
f33321ad JB |
484 | If there is a fill prefix and/or a `left-margin', insert them |
485 | on the new line if the line would have been blank. | |
616ed245 | 486 | With arg N, insert N newlines." |
2076c87c | 487 | (interactive "*p") |
616ed245 | 488 | (let* ((do-fill-prefix (and fill-prefix (bolp))) |
3db1e3b5 | 489 | (do-left-margin (and (bolp) (> (current-left-margin) 0))) |
207d7545 GM |
490 | (loc (point)) |
491 | ;; Don't expand an abbrev before point. | |
492 | (abbrev-mode nil)) | |
e249a6d8 | 493 | (newline n) |
d133d835 | 494 | (goto-char loc) |
e249a6d8 | 495 | (while (> n 0) |
d133d835 RS |
496 | (cond ((bolp) |
497 | (if do-left-margin (indent-to (current-left-margin))) | |
498 | (if do-fill-prefix (insert-and-inherit fill-prefix)))) | |
499 | (forward-line 1) | |
e249a6d8 | 500 | (setq n (1- n))) |
d133d835 RS |
501 | (goto-char loc) |
502 | (end-of-line))) | |
2076c87c | 503 | |
da7d231b KS |
504 | (defun split-line (&optional arg) |
505 | "Split current line, moving portion beyond point vertically down. | |
506 | If the current line starts with `fill-prefix', insert it on the new | |
f33321ad | 507 | line as well. With prefix ARG, don't insert `fill-prefix' on new line. |
da7d231b | 508 | |
e249a6d8 | 509 | When called from Lisp code, ARG may be a prefix string to copy." |
da7d231b | 510 | (interactive "*P") |
2076c87c | 511 | (skip-chars-forward " \t") |
d77bbdc9 RS |
512 | (let* ((col (current-column)) |
513 | (pos (point)) | |
514 | ;; What prefix should we check for (nil means don't). | |
515 | (prefix (cond ((stringp arg) arg) | |
516 | (arg nil) | |
517 | (t fill-prefix))) | |
518 | ;; Does this line start with it? | |
519 | (have-prfx (and prefix | |
520 | (save-excursion | |
521 | (beginning-of-line) | |
522 | (looking-at (regexp-quote prefix)))))) | |
28191e20 | 523 | (newline 1) |
d77bbdc9 | 524 | (if have-prfx (insert-and-inherit prefix)) |
2076c87c JB |
525 | (indent-to col 0) |
526 | (goto-char pos))) | |
527 | ||
2076c87c JB |
528 | (defun delete-indentation (&optional arg) |
529 | "Join this line to previous and fix up whitespace at join. | |
ccc58657 | 530 | If there is a fill prefix, delete it from the beginning of this line. |
2076c87c JB |
531 | With argument, join this line to following line." |
532 | (interactive "*P") | |
533 | (beginning-of-line) | |
534 | (if arg (forward-line 1)) | |
535 | (if (eq (preceding-char) ?\n) | |
536 | (progn | |
537 | (delete-region (point) (1- (point))) | |
ccc58657 RS |
538 | ;; If the second line started with the fill prefix, |
539 | ;; delete the prefix. | |
540 | (if (and fill-prefix | |
01b8e020 | 541 | (<= (+ (point) (length fill-prefix)) (point-max)) |
ccc58657 RS |
542 | (string= fill-prefix |
543 | (buffer-substring (point) | |
544 | (+ (point) (length fill-prefix))))) | |
545 | (delete-region (point) (+ (point) (length fill-prefix)))) | |
2076c87c JB |
546 | (fixup-whitespace)))) |
547 | ||
fc025090 | 548 | (defalias 'join-line #'delete-indentation) ; easier to find |
eaae8106 | 549 | |
2076c87c JB |
550 | (defun delete-blank-lines () |
551 | "On blank line, delete all surrounding blank lines, leaving just one. | |
552 | On isolated blank line, delete that one. | |
6d30d416 | 553 | On nonblank line, delete any immediately following blank lines." |
2076c87c JB |
554 | (interactive "*") |
555 | (let (thisblank singleblank) | |
556 | (save-excursion | |
557 | (beginning-of-line) | |
558 | (setq thisblank (looking-at "[ \t]*$")) | |
70e14c01 | 559 | ;; Set singleblank if there is just one blank line here. |
2076c87c JB |
560 | (setq singleblank |
561 | (and thisblank | |
562 | (not (looking-at "[ \t]*\n[ \t]*$")) | |
563 | (or (bobp) | |
564 | (progn (forward-line -1) | |
565 | (not (looking-at "[ \t]*$"))))))) | |
70e14c01 | 566 | ;; Delete preceding blank lines, and this one too if it's the only one. |
2076c87c JB |
567 | (if thisblank |
568 | (progn | |
569 | (beginning-of-line) | |
570 | (if singleblank (forward-line 1)) | |
571 | (delete-region (point) | |
572 | (if (re-search-backward "[^ \t\n]" nil t) | |
573 | (progn (forward-line 1) (point)) | |
574 | (point-min))))) | |
70e14c01 JB |
575 | ;; Delete following blank lines, unless the current line is blank |
576 | ;; and there are no following blank lines. | |
2076c87c JB |
577 | (if (not (and thisblank singleblank)) |
578 | (save-excursion | |
579 | (end-of-line) | |
580 | (forward-line 1) | |
581 | (delete-region (point) | |
582 | (if (re-search-forward "[^ \t\n]" nil t) | |
583 | (progn (beginning-of-line) (point)) | |
70e14c01 JB |
584 | (point-max))))) |
585 | ;; Handle the special case where point is followed by newline and eob. | |
586 | ;; Delete the line, leaving point at eob. | |
587 | (if (looking-at "^[ \t]*\n\\'") | |
588 | (delete-region (point) (point-max))))) | |
2076c87c | 589 | |
eaae8106 SS |
590 | (defun delete-trailing-whitespace () |
591 | "Delete all the trailing whitespace across the current buffer. | |
592 | All whitespace after the last non-whitespace character in a line is deleted. | |
103db06c RS |
593 | This respects narrowing, created by \\[narrow-to-region] and friends. |
594 | A formfeed is not considered whitespace by this function." | |
eaae8106 SS |
595 | (interactive "*") |
596 | (save-match-data | |
597 | (save-excursion | |
598 | (goto-char (point-min)) | |
5c9b3fac MB |
599 | (while (re-search-forward "\\s-$" nil t) |
600 | (skip-syntax-backward "-" (save-excursion (forward-line 0) (point))) | |
3a768251 | 601 | ;; Don't delete formfeeds, even if they are considered whitespace. |
661aa5c7 GM |
602 | (save-match-data |
603 | (if (looking-at ".*\f") | |
604 | (goto-char (match-end 0)))) | |
7981d89f | 605 | (delete-region (point) (match-end 0)))))) |
eaae8106 | 606 | |
2076c87c JB |
607 | (defun newline-and-indent () |
608 | "Insert a newline, then indent according to major mode. | |
ff1fbe3e | 609 | Indentation is done using the value of `indent-line-function'. |
2076c87c | 610 | In programming language modes, this is the same as TAB. |
ff1fbe3e | 611 | In some text modes, where TAB inserts a tab, this command indents to the |
eed5698b | 612 | column specified by the function `current-left-margin'." |
2076c87c | 613 | (interactive "*") |
5ff4ba3d | 614 | (delete-horizontal-space t) |
46947372 | 615 | (newline) |
2076c87c JB |
616 | (indent-according-to-mode)) |
617 | ||
618 | (defun reindent-then-newline-and-indent () | |
619 | "Reindent current line, insert newline, then indent the new line. | |
620 | Indentation of both lines is done according to the current major mode, | |
ff1fbe3e | 621 | which means calling the current value of `indent-line-function'. |
2076c87c JB |
622 | In programming language modes, this is the same as TAB. |
623 | In some text modes, where TAB inserts a tab, this indents to the | |
eed5698b | 624 | column specified by the function `current-left-margin'." |
2076c87c | 625 | (interactive "*") |
e1e04350 SM |
626 | (let ((pos (point))) |
627 | ;; Be careful to insert the newline before indenting the line. | |
628 | ;; Otherwise, the indentation might be wrong. | |
629 | (newline) | |
630 | (save-excursion | |
631 | (goto-char pos) | |
fd2c4cd8 SM |
632 | (indent-according-to-mode) |
633 | (delete-horizontal-space t)) | |
e1e04350 | 634 | (indent-according-to-mode))) |
eaae8106 | 635 | |
93be67de KH |
636 | (defun quoted-insert (arg) |
637 | "Read next input character and insert it. | |
638 | This is useful for inserting control characters. | |
2076c87c | 639 | |
93be67de KH |
640 | If the first character you type after this command is an octal digit, |
641 | you should type a sequence of octal digits which specify a character code. | |
642 | Any nondigit terminates the sequence. If the terminator is a RET, | |
643 | it is discarded; any other terminator is used itself as input. | |
644 | The variable `read-quoted-char-radix' specifies the radix for this feature; | |
645 | set it to 10 or 16 to use decimal or hex instead of octal. | |
dff7d67f | 646 | |
93be67de KH |
647 | In overwrite mode, this function inserts the character anyway, and |
648 | does not handle octal digits specially. This means that if you use | |
649 | overwrite as your normal editing mode, you can use this function to | |
650 | insert characters when necessary. | |
dff7d67f | 651 | |
93be67de KH |
652 | In binary overwrite mode, this function does overwrite, and octal |
653 | digits are interpreted as a character code. This is intended to be | |
654 | useful for editing binary files." | |
655 | (interactive "*p") | |
1f57934c | 656 | (let* ((char (let (translation-table-for-input input-method-function) |
940fe486 DL |
657 | (if (or (not overwrite-mode) |
658 | (eq overwrite-mode 'overwrite-mode-binary)) | |
659 | (read-quoted-char) | |
660 | (read-char))))) | |
93be67de KH |
661 | ;; Assume character codes 0240 - 0377 stand for characters in some |
662 | ;; single-byte character set, and convert them to Emacs | |
663 | ;; characters. | |
664 | (if (and enable-multibyte-characters | |
665 | (>= char ?\240) | |
666 | (<= char ?\377)) | |
667 | (setq char (unibyte-char-to-multibyte char))) | |
668 | (if (> arg 0) | |
669 | (if (eq overwrite-mode 'overwrite-mode-binary) | |
670 | (delete-char arg))) | |
671 | (while (> arg 0) | |
672 | (insert-and-inherit char) | |
673 | (setq arg (1- arg))))) | |
eaae8106 | 674 | |
b82d844f | 675 | (defun forward-to-indentation (&optional arg) |
93be67de KH |
676 | "Move forward ARG lines and position at first nonblank character." |
677 | (interactive "p") | |
b82d844f | 678 | (forward-line (or arg 1)) |
93be67de | 679 | (skip-chars-forward " \t")) |
cc2b2b6c | 680 | |
b82d844f | 681 | (defun backward-to-indentation (&optional arg) |
93be67de KH |
682 | "Move backward ARG lines and position at first nonblank character." |
683 | (interactive "p") | |
b82d844f | 684 | (forward-line (- (or arg 1))) |
93be67de | 685 | (skip-chars-forward " \t")) |
2076c87c | 686 | |
93be67de KH |
687 | (defun back-to-indentation () |
688 | "Move point to the first non-whitespace character on this line." | |
689 | (interactive) | |
690 | (beginning-of-line 1) | |
1e96c007 | 691 | (skip-syntax-forward " " (line-end-position)) |
b9863466 RS |
692 | ;; Move back over chars that have whitespace syntax but have the p flag. |
693 | (backward-prefix-chars)) | |
93be67de KH |
694 | |
695 | (defun fixup-whitespace () | |
696 | "Fixup white space between objects around point. | |
697 | Leave one space or none, according to the context." | |
698 | (interactive "*") | |
699 | (save-excursion | |
700 | (delete-horizontal-space) | |
701 | (if (or (looking-at "^\\|\\s)") | |
702 | (save-excursion (forward-char -1) | |
703 | (looking-at "$\\|\\s(\\|\\s'"))) | |
704 | nil | |
f33321ad | 705 | (insert ?\s)))) |
93be67de | 706 | |
5ff4ba3d MB |
707 | (defun delete-horizontal-space (&optional backward-only) |
708 | "Delete all spaces and tabs around point. | |
709 | If BACKWARD-ONLY is non-nil, only delete spaces before point." | |
93be67de | 710 | (interactive "*") |
9ab59a1a MB |
711 | (let ((orig-pos (point))) |
712 | (delete-region | |
713 | (if backward-only | |
714 | orig-pos | |
715 | (progn | |
716 | (skip-chars-forward " \t") | |
717 | (constrain-to-field nil orig-pos t))) | |
5ff4ba3d | 718 | (progn |
9ab59a1a MB |
719 | (skip-chars-backward " \t") |
720 | (constrain-to-field nil orig-pos))))) | |
93be67de | 721 | |
68c16b59 | 722 | (defun just-one-space (&optional n) |
56abefac RS |
723 | "Delete all spaces and tabs around point, leaving one space (or N spaces)." |
724 | (interactive "*p") | |
9ab59a1a MB |
725 | (let ((orig-pos (point))) |
726 | (skip-chars-backward " \t") | |
727 | (constrain-to-field nil orig-pos) | |
68c16b59 | 728 | (dotimes (i (or n 1)) |
f33321ad | 729 | (if (= (following-char) ?\s) |
56abefac | 730 | (forward-char 1) |
f33321ad | 731 | (insert ?\s))) |
9ab59a1a MB |
732 | (delete-region |
733 | (point) | |
734 | (progn | |
735 | (skip-chars-forward " \t") | |
736 | (constrain-to-field nil orig-pos t))))) | |
2d88b556 | 737 | \f |
2076c87c JB |
738 | (defun beginning-of-buffer (&optional arg) |
739 | "Move point to the beginning of the buffer; leave mark at previous position. | |
a416e7ef KS |
740 | With \\[universal-argument] prefix, do not set mark at previous position. |
741 | With numeric arg N, put point N/10 of the way from the beginning. | |
c66587fe RS |
742 | |
743 | If the buffer is narrowed, this command uses the beginning and size | |
744 | of the accessible part of the buffer. | |
ff1fbe3e RS |
745 | |
746 | Don't use this command in Lisp programs! | |
2076c87c JB |
747 | \(goto-char (point-min)) is faster and avoids clobbering the mark." |
748 | (interactive "P") | |
24199fe7 | 749 | (or (consp arg) |
705a5933 JL |
750 | (and transient-mark-mode mark-active) |
751 | (push-mark)) | |
c66587fe | 752 | (let ((size (- (point-max) (point-min)))) |
a416e7ef | 753 | (goto-char (if (and arg (not (consp arg))) |
c66587fe RS |
754 | (+ (point-min) |
755 | (if (> size 10000) | |
756 | ;; Avoid overflow for large buffer sizes! | |
757 | (* (prefix-numeric-value arg) | |
758 | (/ size 10)) | |
759 | (/ (+ 10 (* size (prefix-numeric-value arg))) 10))) | |
760 | (point-min)))) | |
2076c87c JB |
761 | (if arg (forward-line 1))) |
762 | ||
763 | (defun end-of-buffer (&optional arg) | |
764 | "Move point to the end of the buffer; leave mark at previous position. | |
a416e7ef KS |
765 | With \\[universal-argument] prefix, do not set mark at previous position. |
766 | With numeric arg N, put point N/10 of the way from the end. | |
c66587fe RS |
767 | |
768 | If the buffer is narrowed, this command uses the beginning and size | |
769 | of the accessible part of the buffer. | |
ff1fbe3e RS |
770 | |
771 | Don't use this command in Lisp programs! | |
2076c87c JB |
772 | \(goto-char (point-max)) is faster and avoids clobbering the mark." |
773 | (interactive "P") | |
24199fe7 | 774 | (or (consp arg) |
705a5933 JL |
775 | (and transient-mark-mode mark-active) |
776 | (push-mark)) | |
c66587fe | 777 | (let ((size (- (point-max) (point-min)))) |
a416e7ef | 778 | (goto-char (if (and arg (not (consp arg))) |
c66587fe RS |
779 | (- (point-max) |
780 | (if (> size 10000) | |
781 | ;; Avoid overflow for large buffer sizes! | |
782 | (* (prefix-numeric-value arg) | |
783 | (/ size 10)) | |
784 | (/ (* size (prefix-numeric-value arg)) 10))) | |
785 | (point-max)))) | |
3a801d0c ER |
786 | ;; If we went to a place in the middle of the buffer, |
787 | ;; adjust it to the beginning of a line. | |
314808dc | 788 | (cond (arg (forward-line 1)) |
919f2812 | 789 | ((> (point) (window-end nil t)) |
314808dc GM |
790 | ;; If the end of the buffer is not already on the screen, |
791 | ;; then scroll specially to put it near, but not at, the bottom. | |
792 | (overlay-recenter (point)) | |
793 | (recenter -3)))) | |
2076c87c JB |
794 | |
795 | (defun mark-whole-buffer () | |
70e14c01 JB |
796 | "Put point at beginning and mark at end of buffer. |
797 | You probably should not use this function in Lisp programs; | |
798 | it is usually a mistake for a Lisp function to use any subroutine | |
799 | that uses or sets the mark." | |
2076c87c JB |
800 | (interactive) |
801 | (push-mark (point)) | |
fd0f4056 | 802 | (push-mark (point-max) nil t) |
2076c87c | 803 | (goto-char (point-min))) |
2d88b556 | 804 | \f |
eaae8106 | 805 | |
93be67de KH |
806 | ;; Counting lines, one way or another. |
807 | ||
00a369ac RS |
808 | (defun goto-line (arg &optional buffer) |
809 | "Goto line ARG, counting from line 1 at beginning of buffer. | |
09ad1a5b EZ |
810 | Normally, move point in the current buffer. |
811 | With just \\[universal-argument] as argument, move point in the most recently | |
812 | displayed other buffer, and switch to it. When called from Lisp code, | |
813 | the optional argument BUFFER specifies a buffer to switch to. | |
00a369ac RS |
814 | |
815 | If there's a number in the buffer at point, it is the default for ARG." | |
816 | (interactive | |
817 | (if (and current-prefix-arg (not (consp current-prefix-arg))) | |
818 | (list (prefix-numeric-value current-prefix-arg)) | |
819 | ;; Look for a default, a number in the buffer at point. | |
820 | (let* ((default | |
821 | (save-excursion | |
822 | (skip-chars-backward "0-9") | |
823 | (if (looking-at "[0-9]") | |
824 | (buffer-substring-no-properties | |
825 | (point) | |
826 | (progn (skip-chars-forward "0-9") | |
827 | (point)))))) | |
828 | ;; Decide if we're switching buffers. | |
829 | (buffer | |
830 | (if (consp current-prefix-arg) | |
831 | (other-buffer (current-buffer) t))) | |
832 | (buffer-prompt | |
833 | (if buffer | |
834 | (concat " in " (buffer-name buffer)) | |
835 | ""))) | |
836 | ;; Read the argument, offering that number (if any) as default. | |
837 | (list (read-from-minibuffer (format (if default "Goto line%s (%s): " | |
838 | "Goto line%s: ") | |
839 | buffer-prompt | |
840 | default) | |
841 | nil nil t | |
842 | 'minibuffer-history | |
843 | default) | |
844 | buffer)))) | |
845 | ;; Switch to the desired buffer, one way or another. | |
846 | (if buffer | |
847 | (let ((window (get-buffer-window buffer))) | |
848 | (if window (select-window window) | |
849 | (switch-to-buffer-other-window buffer)))) | |
850 | ;; Move to the specified line number in that buffer. | |
93be67de KH |
851 | (save-restriction |
852 | (widen) | |
853 | (goto-char 1) | |
854 | (if (eq selective-display t) | |
855 | (re-search-forward "[\n\C-m]" nil 'end (1- arg)) | |
f38c52df | 856 | (forward-line (1- arg))))) |
2076c87c JB |
857 | |
858 | (defun count-lines-region (start end) | |
eb8c3be9 | 859 | "Print number of lines and characters in the region." |
2076c87c JB |
860 | (interactive "r") |
861 | (message "Region has %d lines, %d characters" | |
862 | (count-lines start end) (- end start))) | |
863 | ||
864 | (defun what-line () | |
2578be76 | 865 | "Print the current buffer line number and narrowed line number of point." |
2076c87c | 866 | (interactive) |
c6db81aa | 867 | (let ((start (point-min)) |
4109ce02 | 868 | (n (line-number-at-pos))) |
f076870a KS |
869 | (if (= start 1) |
870 | (message "Line %d" n) | |
871 | (save-excursion | |
872 | (save-restriction | |
873 | (widen) | |
8e5d85ff | 874 | (message "line %d (narrowed line %d)" |
4109ce02 | 875 | (+ n (line-number-at-pos start) -1) n)))))) |
2578be76 | 876 | |
2076c87c JB |
877 | (defun count-lines (start end) |
878 | "Return number of lines between START and END. | |
879 | This is usually the number of newlines between them, | |
ff1fbe3e | 880 | but can be one more if START is not equal to END |
2076c87c | 881 | and the greater of them is not at the start of a line." |
e406700d RS |
882 | (save-excursion |
883 | (save-restriction | |
884 | (narrow-to-region start end) | |
885 | (goto-char (point-min)) | |
886 | (if (eq selective-display t) | |
887 | (save-match-data | |
dde92ca6 RS |
888 | (let ((done 0)) |
889 | (while (re-search-forward "[\n\C-m]" nil t 40) | |
890 | (setq done (+ 40 done))) | |
891 | (while (re-search-forward "[\n\C-m]" nil t 1) | |
892 | (setq done (+ 1 done))) | |
043efc41 RS |
893 | (goto-char (point-max)) |
894 | (if (and (/= start end) | |
895 | (not (bolp))) | |
896 | (1+ done) | |
e406700d RS |
897 | done))) |
898 | (- (buffer-size) (forward-line (buffer-size))))))) | |
eaae8106 | 899 | |
4109ce02 | 900 | (defun line-number-at-pos (&optional pos) |
f076870a | 901 | "Return (narrowed) buffer line number at position POS. |
79ffb765 RS |
902 | If POS is nil, use current buffer location. |
903 | Counting starts at (point-min), so the value refers | |
904 | to the contents of the accessible portion of the buffer." | |
f076870a KS |
905 | (let ((opoint (or pos (point))) start) |
906 | (save-excursion | |
907 | (goto-char (point-min)) | |
908 | (setq start (point)) | |
909 | (goto-char opoint) | |
910 | (forward-line 0) | |
911 | (1+ (count-lines start (point)))))) | |
912 | ||
d5d99b80 KH |
913 | (defun what-cursor-position (&optional detail) |
914 | "Print info on cursor position (on screen and within buffer). | |
e38dff0c | 915 | Also describe the character after point, and give its character code |
c6fcc518 KH |
916 | in octal, decimal and hex. |
917 | ||
918 | For a non-ASCII multibyte character, also give its encoding in the | |
919 | buffer's selected coding system if the coding system encodes the | |
920 | character safely. If the character is encoded into one byte, that | |
921 | code is shown in hex. If the character is encoded into more than one | |
922 | byte, just \"...\" is shown. | |
e5a902cf | 923 | |
24dad5d5 | 924 | In addition, with prefix argument, show details about that character |
0b69eec5 | 925 | in *Help* buffer. See also the command `describe-char'." |
d5d99b80 | 926 | (interactive "P") |
2076c87c JB |
927 | (let* ((char (following-char)) |
928 | (beg (point-min)) | |
929 | (end (point-max)) | |
930 | (pos (point)) | |
931 | (total (buffer-size)) | |
932 | (percent (if (> total 50000) | |
933 | ;; Avoid overflow from multiplying by 100! | |
934 | (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1)) | |
935 | (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1)))) | |
936 | (hscroll (if (= (window-hscroll) 0) | |
937 | "" | |
938 | (format " Hscroll=%d" (window-hscroll)))) | |
939 | (col (current-column))) | |
940 | (if (= pos end) | |
941 | (if (or (/= beg 1) (/= end (1+ total))) | |
a17a79c0 | 942 | (message "point=%d of %d (%d%%) <%d-%d> column=%d%s" |
2076c87c | 943 | pos total percent beg end col hscroll) |
a17a79c0 | 944 | (message "point=%d of %d (EOB) column=%d%s" |
63219d53 | 945 | pos total col hscroll)) |
c6fcc518 | 946 | (let ((coding buffer-file-coding-system) |
a41b50ca | 947 | encoded encoding-msg display-prop under-display) |
c6fcc518 KH |
948 | (if (or (not coding) |
949 | (eq (coding-system-type coding) t)) | |
950 | (setq coding default-buffer-file-coding-system)) | |
28fd4883 KH |
951 | (if (not (char-valid-p char)) |
952 | (setq encoding-msg | |
a17a79c0 | 953 | (format "(%d, #o%o, #x%x, invalid)" char char char)) |
a41b50ca KH |
954 | ;; Check if the character is displayed with some `display' |
955 | ;; text property. In that case, set under-display to the | |
956 | ;; buffer substring covered by that property. | |
957 | (setq display-prop (get-text-property pos 'display)) | |
958 | (if display-prop | |
959 | (let ((to (or (next-single-property-change pos 'display) | |
960 | (point-max)))) | |
961 | (if (< to (+ pos 4)) | |
962 | (setq under-display "") | |
963 | (setq under-display "..." | |
964 | to (+ pos 4))) | |
965 | (setq under-display | |
966 | (concat (buffer-substring-no-properties pos to) | |
967 | under-display))) | |
968 | (setq encoded (and (>= char 128) (encode-coding-char char coding)))) | |
28fd4883 | 969 | (setq encoding-msg |
a41b50ca KH |
970 | (if display-prop |
971 | (if (not (stringp display-prop)) | |
a17a79c0 | 972 | (format "(%d, #o%o, #x%x, part of display \"%s\")" |
a41b50ca | 973 | char char char under-display) |
a17a79c0 | 974 | (format "(%d, #o%o, #x%x, part of display \"%s\"->\"%s\")" |
a41b50ca KH |
975 | char char char under-display display-prop)) |
976 | (if encoded | |
a17a79c0 | 977 | (format "(%d, #o%o, #x%x, file %s)" |
a41b50ca KH |
978 | char char char |
979 | (if (> (length encoded) 1) | |
980 | "..." | |
981 | (encoded-string-description encoded coding))) | |
a17a79c0 | 982 | (format "(%d, #o%o, #x%x)" char char char))))) |
e5e89e48 | 983 | (if detail |
24dad5d5 | 984 | ;; We show the detailed information about CHAR. |
0b69eec5 | 985 | (describe-char (point))) |
24dad5d5 | 986 | (if (or (/= beg 1) (/= end (1+ total))) |
a17a79c0 | 987 | (message "Char: %s %s point=%d of %d (%d%%) <%d-%d> column=%d%s" |
e5a902cf KH |
988 | (if (< char 256) |
989 | (single-key-description char) | |
f0d16a7f | 990 | (buffer-substring-no-properties (point) (1+ (point)))) |
24dad5d5 | 991 | encoding-msg pos total percent beg end col hscroll) |
a17a79c0 | 992 | (message "Char: %s %s point=%d of %d (%d%%) column=%d%s" |
a41b50ca KH |
993 | (if enable-multibyte-characters |
994 | (if (< char 128) | |
995 | (single-key-description char) | |
996 | (buffer-substring-no-properties (point) (1+ (point)))) | |
997 | (single-key-description char)) | |
24dad5d5 | 998 | encoding-msg pos total percent col hscroll)))))) |
2d88b556 | 999 | \f |
76c64e24 SM |
1000 | (defvar read-expression-map |
1001 | (let ((m (make-sparse-keymap))) | |
1002 | (define-key m "\M-\t" 'lisp-complete-symbol) | |
1003 | (set-keymap-parent m minibuffer-local-map) | |
1004 | m) | |
854c16c5 | 1005 | "Minibuffer keymap used for reading Lisp expressions.") |
854c16c5 | 1006 | |
8570b0ca RM |
1007 | (defvar read-expression-history nil) |
1008 | ||
b49df39d | 1009 | (defcustom eval-expression-print-level 4 |
2f7e1f5a | 1010 | "Value for `print-level' while printing value in `eval-expression'. |
d26b26dc | 1011 | A value of nil means no limit." |
b49df39d | 1012 | :group 'lisp |
058d4999 | 1013 | :type '(choice (const :tag "No Limit" nil) integer) |
b49df39d RS |
1014 | :version "21.1") |
1015 | ||
1016 | (defcustom eval-expression-print-length 12 | |
2f7e1f5a | 1017 | "Value for `print-length' while printing value in `eval-expression'. |
d26b26dc | 1018 | A value of nil means no limit." |
b49df39d | 1019 | :group 'lisp |
058d4999 | 1020 | :type '(choice (const :tag "No Limit" nil) integer) |
b49df39d RS |
1021 | :version "21.1") |
1022 | ||
1023 | (defcustom eval-expression-debug-on-error t | |
2f7e1f5a | 1024 | "If non-nil set `debug-on-error' to t in `eval-expression'. |
ed8bcabe | 1025 | If nil, don't change the value of `debug-on-error'." |
b49df39d RS |
1026 | :group 'lisp |
1027 | :type 'boolean | |
1028 | :version "21.1") | |
1029 | ||
fa219ebd JL |
1030 | (defun eval-expression-print-format (value) |
1031 | "Format VALUE as a result of evaluated expression. | |
1032 | Return a formatted string which is displayed in the echo area | |
1033 | in addition to the value printed by prin1 in functions which | |
1034 | display the result of expression evaluation." | |
1035 | (if (and (integerp value) | |
c9f0110e | 1036 | (or (not (memq this-command '(eval-last-sexp eval-print-last-sexp))) |
fa219ebd | 1037 | (eq this-command last-command) |
56abefac | 1038 | (if (boundp 'edebug-active) edebug-active))) |
fa219ebd | 1039 | (let ((char-string |
9bb25ed3 | 1040 | (if (or (if (boundp 'edebug-active) edebug-active) |
c9f0110e | 1041 | (memq this-command '(eval-last-sexp eval-print-last-sexp))) |
fa219ebd JL |
1042 | (prin1-char value)))) |
1043 | (if char-string | |
1b5fd09e SM |
1044 | (format " (#o%o, #x%x, %s)" value value char-string) |
1045 | (format " (#o%o, #x%x)" value value))))) | |
fa219ebd | 1046 | |
8570b0ca | 1047 | ;; We define this, rather than making `eval' interactive, |
ac052b48 | 1048 | ;; for the sake of completion of names like eval-region, eval-buffer. |
ecb7ad00 RS |
1049 | (defun eval-expression (eval-expression-arg |
1050 | &optional eval-expression-insert-value) | |
a6a1ee53 EZ |
1051 | "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area. |
1052 | Value is also consed on to front of the variable `values'. | |
1053 | Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means | |
1054 | insert the result into the current buffer instead of printing it in | |
b4f73994 RS |
1055 | the echo area. |
1056 | ||
1057 | If `eval-expression-debug-on-error' is non-nil, which is the default, | |
1058 | this command arranges for all errors to enter the debugger." | |
adca5fa6 | 1059 | (interactive |
b387ef9a RS |
1060 | (list (read-from-minibuffer "Eval: " |
1061 | nil read-expression-map t | |
ecb7ad00 RS |
1062 | 'read-expression-history) |
1063 | current-prefix-arg)) | |
eaae8106 | 1064 | |
ed8bcabe GM |
1065 | (if (null eval-expression-debug-on-error) |
1066 | (setq values (cons (eval eval-expression-arg) values)) | |
1067 | (let ((old-value (make-symbol "t")) new-value) | |
1068 | ;; Bind debug-on-error to something unique so that we can | |
1069 | ;; detect when evaled code changes it. | |
1070 | (let ((debug-on-error old-value)) | |
1071 | (setq values (cons (eval eval-expression-arg) values)) | |
1072 | (setq new-value debug-on-error)) | |
1073 | ;; If evaled code has changed the value of debug-on-error, | |
1074 | ;; propagate that change to the global binding. | |
1075 | (unless (eq old-value new-value) | |
1076 | (setq debug-on-error new-value)))) | |
eaae8106 | 1077 | |
b49df39d RS |
1078 | (let ((print-length eval-expression-print-length) |
1079 | (print-level eval-expression-print-level)) | |
b82d844f RS |
1080 | (if eval-expression-insert-value |
1081 | (with-no-warnings | |
683e7415 RS |
1082 | (let ((standard-output (current-buffer))) |
1083 | (eval-last-sexp-print-value (car values)))) | |
fa219ebd JL |
1084 | (prog1 |
1085 | (prin1 (car values) t) | |
1086 | (let ((str (eval-expression-print-format (car values)))) | |
1087 | (if str (princ str t))))))) | |
2076c87c JB |
1088 | |
1089 | (defun edit-and-eval-command (prompt command) | |
1090 | "Prompting with PROMPT, let user edit COMMAND and eval result. | |
1091 | COMMAND is a Lisp expression. Let user edit that expression in | |
1092 | the minibuffer, then read and evaluate the result." | |
9f4b6084 | 1093 | (let ((command |
11178a06 LT |
1094 | (let ((print-level nil) |
1095 | (minibuffer-history-sexp-flag (1+ (minibuffer-depth)))) | |
1096 | (unwind-protect | |
1097 | (read-from-minibuffer prompt | |
1098 | (prin1-to-string command) | |
1099 | read-expression-map t | |
1100 | 'command-history) | |
1101 | ;; If command was added to command-history as a string, | |
1102 | ;; get rid of that. We want only evaluable expressions there. | |
1103 | (if (stringp (car command-history)) | |
1104 | (setq command-history (cdr command-history))))))) | |
5d6c83ae KH |
1105 | |
1106 | ;; If command to be redone does not match front of history, | |
1107 | ;; add it to the history. | |
1108 | (or (equal command (car command-history)) | |
1109 | (setq command-history (cons command command-history))) | |
2076c87c JB |
1110 | (eval command))) |
1111 | ||
ebb61177 | 1112 | (defun repeat-complex-command (arg) |
2076c87c JB |
1113 | "Edit and re-evaluate last complex command, or ARGth from last. |
1114 | A complex command is one which used the minibuffer. | |
1115 | The command is placed in the minibuffer as a Lisp form for editing. | |
1116 | The result is executed, repeating the command as changed. | |
1117 | If the command has been changed or is not the most recent previous command | |
1118 | it is added to the front of the command history. | |
eb6e9899 RS |
1119 | You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element] |
1120 | to get different commands to edit and resubmit." | |
2076c87c | 1121 | (interactive "p") |
ba343182 | 1122 | (let ((elt (nth (1- arg) command-history)) |
2076c87c JB |
1123 | newcmd) |
1124 | (if elt | |
854c16c5 | 1125 | (progn |
eab22e27 | 1126 | (setq newcmd |
74ae5fab RS |
1127 | (let ((print-level nil) |
1128 | (minibuffer-history-position arg) | |
99ea24de | 1129 | (minibuffer-history-sexp-flag (1+ (minibuffer-depth)))) |
9f4b6084 MR |
1130 | (unwind-protect |
1131 | (read-from-minibuffer | |
1132 | "Redo: " (prin1-to-string elt) read-expression-map t | |
1133 | (cons 'command-history arg)) | |
1134 | ||
1135 | ;; If command was added to command-history as a | |
1136 | ;; string, get rid of that. We want only | |
1137 | ;; evaluable expressions there. | |
1138 | (if (stringp (car command-history)) | |
1139 | (setq command-history (cdr command-history)))))) | |
db16f109 RS |
1140 | |
1141 | ;; If command to be redone does not match front of history, | |
1142 | ;; add it to the history. | |
1143 | (or (equal newcmd (car command-history)) | |
1144 | (setq command-history (cons newcmd command-history))) | |
2076c87c | 1145 | (eval newcmd)) |
536b728a RS |
1146 | (if command-history |
1147 | (error "Argument %d is beyond length of command history" arg) | |
1148 | (error "There are no previous complex commands to repeat"))))) | |
2d88b556 | 1149 | \f |
854c16c5 RS |
1150 | (defvar minibuffer-history nil |
1151 | "Default minibuffer history list. | |
1152 | This is used for all minibuffer input | |
1153 | except when an alternate history list is specified.") | |
1154 | (defvar minibuffer-history-sexp-flag nil | |
2c6a2254 RS |
1155 | "Control whether history list elements are expressions or strings. |
1156 | If the value of this variable equals current minibuffer depth, | |
1157 | they are expressions; otherwise they are strings. | |
7979163c | 1158 | \(That convention is designed to do the right thing for |
2c6a2254 | 1159 | recursive uses of the minibuffer.)") |
e91f80c4 | 1160 | (setq minibuffer-history-variable 'minibuffer-history) |
535c8bdb | 1161 | (setq minibuffer-history-position nil) ;; Defvar is in C code. |
854c16c5 | 1162 | (defvar minibuffer-history-search-history nil) |
e91f80c4 | 1163 | |
93cee14b RS |
1164 | (defvar minibuffer-text-before-history nil |
1165 | "Text that was in this minibuffer before any history commands. | |
1166 | This is nil if there have not yet been any history commands | |
1167 | in this use of the minibuffer.") | |
1168 | ||
1169 | (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize) | |
1170 | ||
1171 | (defun minibuffer-history-initialize () | |
1172 | (setq minibuffer-text-before-history nil)) | |
1173 | ||
6e7d0ff7 MB |
1174 | (defun minibuffer-avoid-prompt (new old) |
1175 | "A point-motion hook for the minibuffer, that moves point out of the prompt." | |
1176 | (constrain-to-field nil (point-max))) | |
1177 | ||
6e30a99a RS |
1178 | (defcustom minibuffer-history-case-insensitive-variables nil |
1179 | "*Minibuffer history variables for which matching should ignore case. | |
1180 | If a history variable is a member of this list, then the | |
1181 | \\[previous-matching-history-element] and \\[next-matching-history-element]\ | |
1182 | commands ignore case when searching it, regardless of `case-fold-search'." | |
1183 | :type '(repeat variable) | |
1184 | :group 'minibuffer) | |
1185 | ||
e91f80c4 | 1186 | (defun previous-matching-history-element (regexp n) |
854c16c5 RS |
1187 | "Find the previous history element that matches REGEXP. |
1188 | \(Previous history elements refer to earlier actions.) | |
1189 | With prefix argument N, search for Nth previous match. | |
5c2010f0 | 1190 | If N is negative, find the next or Nth next match. |
9889af08 EZ |
1191 | Normally, history elements are matched case-insensitively if |
1192 | `case-fold-search' is non-nil, but an uppercase letter in REGEXP | |
1193 | makes the search case-sensitive. | |
6e30a99a | 1194 | See also `minibuffer-history-case-insensitive-variables'." |
854c16c5 | 1195 | (interactive |
c1172a19 | 1196 | (let* ((enable-recursive-minibuffers t) |
c1172a19 RS |
1197 | (regexp (read-from-minibuffer "Previous element matching (regexp): " |
1198 | nil | |
1199 | minibuffer-local-map | |
1200 | nil | |
5794c45d RS |
1201 | 'minibuffer-history-search-history |
1202 | (car minibuffer-history-search-history)))) | |
c1172a19 RS |
1203 | ;; Use the last regexp specified, by default, if input is empty. |
1204 | (list (if (string= regexp "") | |
a8e96cea KH |
1205 | (if minibuffer-history-search-history |
1206 | (car minibuffer-history-search-history) | |
1207 | (error "No previous history search regexp")) | |
c1172a19 | 1208 | regexp) |
854c16c5 | 1209 | (prefix-numeric-value current-prefix-arg)))) |
e276a14a MB |
1210 | (unless (zerop n) |
1211 | (if (and (zerop minibuffer-history-position) | |
1212 | (null minibuffer-text-before-history)) | |
efaac2e6 | 1213 | (setq minibuffer-text-before-history |
6d74d713 | 1214 | (minibuffer-contents-no-properties))) |
e276a14a MB |
1215 | (let ((history (symbol-value minibuffer-history-variable)) |
1216 | (case-fold-search | |
1217 | (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped | |
1218 | ;; On some systems, ignore case for file names. | |
1219 | (if (memq minibuffer-history-variable | |
1220 | minibuffer-history-case-insensitive-variables) | |
1221 | t | |
1222 | ;; Respect the user's setting for case-fold-search: | |
1223 | case-fold-search) | |
1224 | nil)) | |
1225 | prevpos | |
1226 | match-string | |
1227 | match-offset | |
1228 | (pos minibuffer-history-position)) | |
1229 | (while (/= n 0) | |
1230 | (setq prevpos pos) | |
1231 | (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history))) | |
1232 | (when (= pos prevpos) | |
e91f80c4 | 1233 | (error (if (= pos 1) |
ccc58657 RS |
1234 | "No later matching history item" |
1235 | "No earlier matching history item"))) | |
e276a14a MB |
1236 | (setq match-string |
1237 | (if (eq minibuffer-history-sexp-flag (minibuffer-depth)) | |
7908d27c | 1238 | (let ((print-level nil)) |
e276a14a MB |
1239 | (prin1-to-string (nth (1- pos) history))) |
1240 | (nth (1- pos) history))) | |
1241 | (setq match-offset | |
1242 | (if (< n 0) | |
1243 | (and (string-match regexp match-string) | |
1244 | (match-end 0)) | |
1245 | (and (string-match (concat ".*\\(" regexp "\\)") match-string) | |
1246 | (match-beginning 1)))) | |
1247 | (when match-offset | |
1248 | (setq n (+ n (if (< n 0) 1 -1))))) | |
1249 | (setq minibuffer-history-position pos) | |
1250 | (goto-char (point-max)) | |
efaac2e6 | 1251 | (delete-minibuffer-contents) |
e276a14a | 1252 | (insert match-string) |
6d74d713 | 1253 | (goto-char (+ (minibuffer-prompt-end) match-offset)))) |
e1e04350 SM |
1254 | (if (memq (car (car command-history)) '(previous-matching-history-element |
1255 | next-matching-history-element)) | |
854c16c5 | 1256 | (setq command-history (cdr command-history)))) |
e91f80c4 | 1257 | |
e91f80c4 | 1258 | (defun next-matching-history-element (regexp n) |
854c16c5 RS |
1259 | "Find the next history element that matches REGEXP. |
1260 | \(The next history element refers to a more recent action.) | |
1261 | With prefix argument N, search for Nth next match. | |
5c2010f0 | 1262 | If N is negative, find the previous or Nth previous match. |
9889af08 EZ |
1263 | Normally, history elements are matched case-insensitively if |
1264 | `case-fold-search' is non-nil, but an uppercase letter in REGEXP | |
1265 | makes the search case-sensitive." | |
854c16c5 | 1266 | (interactive |
c1172a19 | 1267 | (let* ((enable-recursive-minibuffers t) |
c1172a19 RS |
1268 | (regexp (read-from-minibuffer "Next element matching (regexp): " |
1269 | nil | |
1270 | minibuffer-local-map | |
1271 | nil | |
e967cd11 RS |
1272 | 'minibuffer-history-search-history |
1273 | (car minibuffer-history-search-history)))) | |
c1172a19 RS |
1274 | ;; Use the last regexp specified, by default, if input is empty. |
1275 | (list (if (string= regexp "") | |
e967cd11 RS |
1276 | (if minibuffer-history-search-history |
1277 | (car minibuffer-history-search-history) | |
1278 | (error "No previous history search regexp")) | |
c1172a19 | 1279 | regexp) |
854c16c5 | 1280 | (prefix-numeric-value current-prefix-arg)))) |
e91f80c4 | 1281 | (previous-matching-history-element regexp (- n))) |
2076c87c | 1282 | |
8dc3ba7d MB |
1283 | (defvar minibuffer-temporary-goal-position nil) |
1284 | ||
ebb61177 | 1285 | (defun next-history-element (n) |
1459a43b RS |
1286 | "Puts next element of the minibuffer history in the minibuffer. |
1287 | With argument N, it uses the Nth following element." | |
2076c87c | 1288 | (interactive "p") |
0818b15e | 1289 | (or (zerop n) |
93cee14b RS |
1290 | (let ((narg (- minibuffer-history-position n)) |
1291 | (minimum (if minibuffer-default -1 0)) | |
068c712c | 1292 | elt minibuffer-returned-to-present) |
93cee14b RS |
1293 | (if (and (zerop minibuffer-history-position) |
1294 | (null minibuffer-text-before-history)) | |
efaac2e6 MB |
1295 | (setq minibuffer-text-before-history |
1296 | (minibuffer-contents-no-properties))) | |
93cee14b | 1297 | (if (< narg minimum) |
81091543 RS |
1298 | (if minibuffer-default |
1299 | (error "End of history; no next item") | |
1300 | (error "End of history; no default available"))) | |
93cee14b RS |
1301 | (if (> narg (length (symbol-value minibuffer-history-variable))) |
1302 | (error "Beginning of history; no preceding item")) | |
e1e04350 SM |
1303 | (unless (memq last-command '(next-history-element |
1304 | previous-history-element)) | |
efaac2e6 | 1305 | (let ((prompt-end (minibuffer-prompt-end))) |
8dc3ba7d MB |
1306 | (set (make-local-variable 'minibuffer-temporary-goal-position) |
1307 | (cond ((<= (point) prompt-end) prompt-end) | |
1308 | ((eobp) nil) | |
1309 | (t (point)))))) | |
a4d1159b | 1310 | (goto-char (point-max)) |
efaac2e6 | 1311 | (delete-minibuffer-contents) |
93cee14b RS |
1312 | (setq minibuffer-history-position narg) |
1313 | (cond ((= narg -1) | |
1314 | (setq elt minibuffer-default)) | |
1315 | ((= narg 0) | |
54c548db | 1316 | (setq elt (or minibuffer-text-before-history "")) |
068c712c | 1317 | (setq minibuffer-returned-to-present t) |
93cee14b RS |
1318 | (setq minibuffer-text-before-history nil)) |
1319 | (t (setq elt (nth (1- minibuffer-history-position) | |
1320 | (symbol-value minibuffer-history-variable))))) | |
1321 | (insert | |
068c712c KH |
1322 | (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth)) |
1323 | (not minibuffer-returned-to-present)) | |
93cee14b RS |
1324 | (let ((print-level nil)) |
1325 | (prin1-to-string elt)) | |
1326 | elt)) | |
8dc3ba7d | 1327 | (goto-char (or minibuffer-temporary-goal-position (point-max)))))) |
2076c87c | 1328 | |
ebb61177 | 1329 | (defun previous-history-element (n) |
1459a43b RS |
1330 | "Puts previous element of the minibuffer history in the minibuffer. |
1331 | With argument N, it uses the Nth previous element." | |
2076c87c | 1332 | (interactive "p") |
2c5e21c1 | 1333 | (next-history-element (- n))) |
d0678801 RM |
1334 | |
1335 | (defun next-complete-history-element (n) | |
a4d1159b GM |
1336 | "Get next history element which completes the minibuffer before the point. |
1337 | The contents of the minibuffer after the point are deleted, and replaced | |
1338 | by the new completion." | |
d0678801 | 1339 | (interactive "p") |
b5e6f936 RM |
1340 | (let ((point-at-start (point))) |
1341 | (next-matching-history-element | |
a4d1159b | 1342 | (concat |
efaac2e6 | 1343 | "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point)))) |
a4d1159b | 1344 | n) |
b5e6f936 RM |
1345 | ;; next-matching-history-element always puts us at (point-min). |
1346 | ;; Move to the position we were at before changing the buffer contents. | |
1347 | ;; This is still sensical, because the text before point has not changed. | |
1348 | (goto-char point-at-start))) | |
d0678801 RM |
1349 | |
1350 | (defun previous-complete-history-element (n) | |
1f6fcec3 | 1351 | "\ |
a4d1159b GM |
1352 | Get previous history element which completes the minibuffer before the point. |
1353 | The contents of the minibuffer after the point are deleted, and replaced | |
1354 | by the new completion." | |
d0678801 RM |
1355 | (interactive "p") |
1356 | (next-complete-history-element (- n))) | |
a4d1159b | 1357 | |
efaac2e6 | 1358 | ;; For compatibility with the old subr of the same name. |
a4d1159b GM |
1359 | (defun minibuffer-prompt-width () |
1360 | "Return the display width of the minibuffer prompt. | |
f33321ad | 1361 | Return 0 if current buffer is not a minibuffer." |
a4d1159b GM |
1362 | ;; Return the width of everything before the field at the end of |
1363 | ;; the buffer; this should be 0 for normal buffers. | |
efaac2e6 | 1364 | (1- (minibuffer-prompt-end))) |
2d88b556 | 1365 | \f |
2076c87c | 1366 | ;Put this on C-x u, so we can force that rather than C-_ into startup msg |
e462e42f | 1367 | (defalias 'advertised-undo 'undo) |
2076c87c | 1368 | |
1e96c007 | 1369 | (defconst undo-equiv-table (make-hash-table :test 'eq :weakness t) |
713c9020 RS |
1370 | "Table mapping redo records to the corresponding undo one. |
1371 | A redo record for undo-in-region maps to t. | |
1372 | A redo record for ordinary undo maps to the following (earlier) undo.") | |
1e96c007 SM |
1373 | |
1374 | (defvar undo-in-region nil | |
1375 | "Non-nil if `pending-undo-list' is not just a tail of `buffer-undo-list'.") | |
1376 | ||
1377 | (defvar undo-no-redo nil | |
1378 | "If t, `undo' doesn't go through redo entries.") | |
1379 | ||
a7fe694c RS |
1380 | (defvar pending-undo-list nil |
1381 | "Within a run of consecutive undo commands, list remaining to be undone. | |
8ac28be5 | 1382 | If t, we undid all the way to the end of it.") |
a7fe694c | 1383 | |
2076c87c JB |
1384 | (defun undo (&optional arg) |
1385 | "Undo some previous changes. | |
1386 | Repeat this command to undo more changes. | |
65627aad RS |
1387 | A numeric argument serves as a repeat count. |
1388 | ||
3c1b77ca | 1389 | In Transient Mark mode when the mark is active, only undo changes within |
1e96c007 | 1390 | the current region. Similarly, when not in Transient Mark mode, just \\[universal-argument] |
3c1b77ca | 1391 | as an argument limits undo to changes within the current region." |
65627aad | 1392 | (interactive "*P") |
2e033693 RS |
1393 | ;; Make last-command indicate for the next command that this was an undo. |
1394 | ;; That way, another undo will undo more. | |
1395 | ;; If we get to the end of the undo history and get an error, | |
1396 | ;; another undo command will find the undo history empty | |
1397 | ;; and will get another error. To begin undoing the undos, | |
1398 | ;; you must type some other command. | |
b553cffa | 1399 | (let ((modified (buffer-modified-p)) |
cb3b2ec0 RS |
1400 | (recent-save (recent-auto-save-p)) |
1401 | message) | |
2c6a2254 RS |
1402 | ;; If we get an error in undo-start, |
1403 | ;; the next command should not be a "consecutive undo". | |
1404 | ;; So set `this-command' to something other than `undo'. | |
1405 | (setq this-command 'undo-start) | |
1406 | ||
e967cd11 | 1407 | (unless (and (eq last-command 'undo) |
a7fe694c RS |
1408 | (or (eq pending-undo-list t) |
1409 | ;; If something (a timer or filter?) changed the buffer | |
1410 | ;; since the previous command, don't continue the undo seq. | |
1411 | (let ((list buffer-undo-list)) | |
1412 | (while (eq (car list) nil) | |
1413 | (setq list (cdr list))) | |
1414 | ;; If the last undo record made was made by undo | |
1415 | ;; it shows nothing else happened in between. | |
1416 | (gethash list undo-equiv-table)))) | |
1e96c007 SM |
1417 | (setq undo-in-region |
1418 | (if transient-mark-mode mark-active (and arg (not (numberp arg))))) | |
1419 | (if undo-in-region | |
3c1b77ca MB |
1420 | (undo-start (region-beginning) (region-end)) |
1421 | (undo-start)) | |
1422 | ;; get rid of initial undo boundary | |
1423 | (undo-more 1)) | |
9a1120ea | 1424 | ;; If we got this far, the next command should be a consecutive undo. |
2c6a2254 | 1425 | (setq this-command 'undo) |
1e96c007 SM |
1426 | ;; Check to see whether we're hitting a redo record, and if |
1427 | ;; so, ask the user whether she wants to skip the redo/undo pair. | |
1428 | (let ((equiv (gethash pending-undo-list undo-equiv-table))) | |
1429 | (or (eq (selected-window) (minibuffer-window)) | |
cb3b2ec0 RS |
1430 | (setq message (if undo-in-region |
1431 | (if equiv "Redo in region!" "Undo in region!") | |
1432 | (if equiv "Redo!" "Undo!")))) | |
0047373b | 1433 | (when (and (consp equiv) undo-no-redo) |
1e96c007 SM |
1434 | ;; The equiv entry might point to another redo record if we have done |
1435 | ;; undo-redo-undo-redo-... so skip to the very last equiv. | |
1436 | (while (let ((next (gethash equiv undo-equiv-table))) | |
1437 | (if next (setq equiv next)))) | |
1438 | (setq pending-undo-list equiv))) | |
3c1b77ca MB |
1439 | (undo-more |
1440 | (if (or transient-mark-mode (numberp arg)) | |
1441 | (prefix-numeric-value arg) | |
1442 | 1)) | |
1e96c007 | 1443 | ;; Record the fact that the just-generated undo records come from an |
713c9020 RS |
1444 | ;; undo operation--that is, they are redo records. |
1445 | ;; In the ordinary case (not within a region), map the redo | |
1446 | ;; record to the following undos. | |
1e96c007 | 1447 | ;; I don't know how to do that in the undo-in-region case. |
713c9020 RS |
1448 | (puthash buffer-undo-list |
1449 | (if undo-in-region t pending-undo-list) | |
1450 | undo-equiv-table) | |
2512c9f0 RS |
1451 | ;; Don't specify a position in the undo record for the undo command. |
1452 | ;; Instead, undoing this should move point to where the change is. | |
1453 | (let ((tail buffer-undo-list) | |
003550c5 GM |
1454 | (prev nil)) |
1455 | (while (car tail) | |
1456 | (when (integerp (car tail)) | |
1457 | (let ((pos (car tail))) | |
1e96c007 SM |
1458 | (if prev |
1459 | (setcdr prev (cdr tail)) | |
1460 | (setq buffer-undo-list (cdr tail))) | |
003550c5 GM |
1461 | (setq tail (cdr tail)) |
1462 | (while (car tail) | |
1463 | (if (eq pos (car tail)) | |
1464 | (if prev | |
1465 | (setcdr prev (cdr tail)) | |
1466 | (setq buffer-undo-list (cdr tail))) | |
1467 | (setq prev tail)) | |
1468 | (setq tail (cdr tail))) | |
1469 | (setq tail nil))) | |
1470 | (setq prev tail tail (cdr tail)))) | |
e967cd11 RS |
1471 | ;; Record what the current undo list says, |
1472 | ;; so the next command can tell if the buffer was modified in between. | |
2076c87c | 1473 | (and modified (not (buffer-modified-p)) |
cb3b2ec0 RS |
1474 | (delete-auto-save-file-if-necessary recent-save)) |
1475 | ;; Display a message announcing success. | |
1476 | (if message | |
1477 | (message message)))) | |
2076c87c | 1478 | |
e967cd11 RS |
1479 | (defun buffer-disable-undo (&optional buffer) |
1480 | "Make BUFFER stop keeping undo information. | |
1481 | No argument or nil as argument means do this for the current buffer." | |
1482 | (interactive) | |
0d808a63 | 1483 | (with-current-buffer (if buffer (get-buffer buffer) (current-buffer)) |
d020fce0 | 1484 | (setq buffer-undo-list t))) |
e967cd11 | 1485 | |
1e96c007 SM |
1486 | (defun undo-only (&optional arg) |
1487 | "Undo some previous changes. | |
1488 | Repeat this command to undo more changes. | |
1489 | A numeric argument serves as a repeat count. | |
1490 | Contrary to `undo', this will not redo a previous undo." | |
1491 | (interactive "*p") | |
1492 | (let ((undo-no-redo t)) (undo arg))) | |
1e96c007 | 1493 | |
52d1110d RS |
1494 | (defvar undo-in-progress nil |
1495 | "Non-nil while performing an undo. | |
1496 | Some change-hooks test this variable to do something different.") | |
1497 | ||
8ac28be5 | 1498 | (defun undo-more (n) |
2076c87c | 1499 | "Undo back N undo-boundaries beyond what was already undone recently. |
ff1fbe3e RS |
1500 | Call `undo-start' to get ready to undo recent changes, |
1501 | then call `undo-more' one or more times to undo them." | |
a7fe694c | 1502 | (or (listp pending-undo-list) |
8ac28be5 JB |
1503 | (error (concat "No further undo information" |
1504 | (and transient-mark-mode mark-active | |
1505 | " for region")))) | |
52d1110d | 1506 | (let ((undo-in-progress t)) |
8ac28be5 | 1507 | (setq pending-undo-list (primitive-undo n pending-undo-list)) |
a7fe694c RS |
1508 | (if (null pending-undo-list) |
1509 | (setq pending-undo-list t)))) | |
2076c87c | 1510 | |
65627aad RS |
1511 | ;; Deep copy of a list |
1512 | (defun undo-copy-list (list) | |
1513 | "Make a copy of undo list LIST." | |
1514 | (mapcar 'undo-copy-list-1 list)) | |
1515 | ||
1516 | (defun undo-copy-list-1 (elt) | |
1517 | (if (consp elt) | |
1518 | (cons (car elt) (undo-copy-list-1 (cdr elt))) | |
1519 | elt)) | |
1520 | ||
1521 | (defun undo-start (&optional beg end) | |
1522 | "Set `pending-undo-list' to the front of the undo list. | |
1523 | The next call to `undo-more' will undo the most recently made change. | |
1524 | If BEG and END are specified, then only undo elements | |
1525 | that apply to text between BEG and END are used; other undo elements | |
1526 | are ignored. If BEG and END are nil, all undo elements are used." | |
1527 | (if (eq buffer-undo-list t) | |
1528 | (error "No undo information in this buffer")) | |
1e722f9f | 1529 | (setq pending-undo-list |
65627aad RS |
1530 | (if (and beg end (not (= beg end))) |
1531 | (undo-make-selective-list (min beg end) (max beg end)) | |
1532 | buffer-undo-list))) | |
1533 | ||
1534 | (defvar undo-adjusted-markers) | |
1535 | ||
1536 | (defun undo-make-selective-list (start end) | |
1537 | "Return a list of undo elements for the region START to END. | |
1538 | The elements come from `buffer-undo-list', but we keep only | |
1539 | the elements inside this region, and discard those outside this region. | |
1540 | If we find an element that crosses an edge of this region, | |
1541 | we stop and ignore all further elements." | |
1542 | (let ((undo-list-copy (undo-copy-list buffer-undo-list)) | |
1543 | (undo-list (list nil)) | |
1544 | undo-adjusted-markers | |
1545 | some-rejected | |
1546 | undo-elt undo-elt temp-undo-list delta) | |
1547 | (while undo-list-copy | |
1548 | (setq undo-elt (car undo-list-copy)) | |
1549 | (let ((keep-this | |
1550 | (cond ((and (consp undo-elt) (eq (car undo-elt) t)) | |
1551 | ;; This is a "was unmodified" element. | |
1552 | ;; Keep it if we have kept everything thus far. | |
1553 | (not some-rejected)) | |
1554 | (t | |
1555 | (undo-elt-in-region undo-elt start end))))) | |
1556 | (if keep-this | |
1557 | (progn | |
1558 | (setq end (+ end (cdr (undo-delta undo-elt)))) | |
1559 | ;; Don't put two nils together in the list | |
1560 | (if (not (and (eq (car undo-list) nil) | |
1561 | (eq undo-elt nil))) | |
1562 | (setq undo-list (cons undo-elt undo-list)))) | |
1563 | (if (undo-elt-crosses-region undo-elt start end) | |
1564 | (setq undo-list-copy nil) | |
1565 | (setq some-rejected t) | |
1566 | (setq temp-undo-list (cdr undo-list-copy)) | |
1567 | (setq delta (undo-delta undo-elt)) | |
1568 | ||
1569 | (when (/= (cdr delta) 0) | |
1570 | (let ((position (car delta)) | |
1571 | (offset (cdr delta))) | |
1572 | ||
e1e04350 SM |
1573 | ;; Loop down the earlier events adjusting their buffer |
1574 | ;; positions to reflect the fact that a change to the buffer | |
1575 | ;; isn't being undone. We only need to process those element | |
1576 | ;; types which undo-elt-in-region will return as being in | |
1577 | ;; the region since only those types can ever get into the | |
1578 | ;; output | |
65627aad RS |
1579 | |
1580 | (while temp-undo-list | |
1581 | (setq undo-elt (car temp-undo-list)) | |
1582 | (cond ((integerp undo-elt) | |
1583 | (if (>= undo-elt position) | |
1584 | (setcar temp-undo-list (- undo-elt offset)))) | |
1585 | ((atom undo-elt) nil) | |
1586 | ((stringp (car undo-elt)) | |
1587 | ;; (TEXT . POSITION) | |
1588 | (let ((text-pos (abs (cdr undo-elt))) | |
1589 | (point-at-end (< (cdr undo-elt) 0 ))) | |
1590 | (if (>= text-pos position) | |
1e722f9f | 1591 | (setcdr undo-elt (* (if point-at-end -1 1) |
65627aad RS |
1592 | (- text-pos offset)))))) |
1593 | ((integerp (car undo-elt)) | |
1594 | ;; (BEGIN . END) | |
1595 | (when (>= (car undo-elt) position) | |
1596 | (setcar undo-elt (- (car undo-elt) offset)) | |
1597 | (setcdr undo-elt (- (cdr undo-elt) offset)))) | |
1598 | ((null (car undo-elt)) | |
1599 | ;; (nil PROPERTY VALUE BEG . END) | |
1600 | (let ((tail (nthcdr 3 undo-elt))) | |
1601 | (when (>= (car tail) position) | |
1602 | (setcar tail (- (car tail) offset)) | |
1603 | (setcdr tail (- (cdr tail) offset)))))) | |
1604 | (setq temp-undo-list (cdr temp-undo-list)))))))) | |
1605 | (setq undo-list-copy (cdr undo-list-copy))) | |
1606 | (nreverse undo-list))) | |
1607 | ||
1608 | (defun undo-elt-in-region (undo-elt start end) | |
1609 | "Determine whether UNDO-ELT falls inside the region START ... END. | |
1610 | If it crosses the edge, we return nil." | |
1611 | (cond ((integerp undo-elt) | |
1612 | (and (>= undo-elt start) | |
12a93712 | 1613 | (<= undo-elt end))) |
65627aad RS |
1614 | ((eq undo-elt nil) |
1615 | t) | |
1616 | ((atom undo-elt) | |
1617 | nil) | |
1618 | ((stringp (car undo-elt)) | |
1619 | ;; (TEXT . POSITION) | |
1620 | (and (>= (abs (cdr undo-elt)) start) | |
1621 | (< (abs (cdr undo-elt)) end))) | |
1622 | ((and (consp undo-elt) (markerp (car undo-elt))) | |
1623 | ;; This is a marker-adjustment element (MARKER . ADJUSTMENT). | |
1624 | ;; See if MARKER is inside the region. | |
1625 | (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers))) | |
1626 | (unless alist-elt | |
1627 | (setq alist-elt (cons (car undo-elt) | |
1628 | (marker-position (car undo-elt)))) | |
1629 | (setq undo-adjusted-markers | |
1630 | (cons alist-elt undo-adjusted-markers))) | |
1631 | (and (cdr alist-elt) | |
1632 | (>= (cdr alist-elt) start) | |
12a93712 | 1633 | (<= (cdr alist-elt) end)))) |
65627aad RS |
1634 | ((null (car undo-elt)) |
1635 | ;; (nil PROPERTY VALUE BEG . END) | |
1636 | (let ((tail (nthcdr 3 undo-elt))) | |
1637 | (and (>= (car tail) start) | |
12a93712 | 1638 | (<= (cdr tail) end)))) |
65627aad RS |
1639 | ((integerp (car undo-elt)) |
1640 | ;; (BEGIN . END) | |
1641 | (and (>= (car undo-elt) start) | |
12a93712 | 1642 | (<= (cdr undo-elt) end))))) |
65627aad RS |
1643 | |
1644 | (defun undo-elt-crosses-region (undo-elt start end) | |
1645 | "Test whether UNDO-ELT crosses one edge of that region START ... END. | |
1646 | This assumes we have already decided that UNDO-ELT | |
1647 | is not *inside* the region START...END." | |
1648 | (cond ((atom undo-elt) nil) | |
1649 | ((null (car undo-elt)) | |
1650 | ;; (nil PROPERTY VALUE BEG . END) | |
1651 | (let ((tail (nthcdr 3 undo-elt))) | |
1652 | (not (or (< (car tail) end) | |
1653 | (> (cdr tail) start))))) | |
1654 | ((integerp (car undo-elt)) | |
1655 | ;; (BEGIN . END) | |
1656 | (not (or (< (car undo-elt) end) | |
1657 | (> (cdr undo-elt) start)))))) | |
1658 | ||
1659 | ;; Return the first affected buffer position and the delta for an undo element | |
1660 | ;; delta is defined as the change in subsequent buffer positions if we *did* | |
1661 | ;; the undo. | |
1662 | (defun undo-delta (undo-elt) | |
1663 | (if (consp undo-elt) | |
1664 | (cond ((stringp (car undo-elt)) | |
1665 | ;; (TEXT . POSITION) | |
1666 | (cons (abs (cdr undo-elt)) (length (car undo-elt)))) | |
1667 | ((integerp (car undo-elt)) | |
1668 | ;; (BEGIN . END) | |
1669 | (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt)))) | |
1670 | (t | |
1671 | '(0 . 0))) | |
1672 | '(0 . 0))) | |
b6e8e8e5 | 1673 | |
1223933d | 1674 | (defcustom undo-ask-before-discard nil |
28cb725d LT |
1675 | "If non-nil ask about discarding undo info for the current command. |
1676 | Normally, Emacs discards the undo info for the current command if | |
1677 | it exceeds `undo-outer-limit'. But if you set this option | |
1678 | non-nil, it asks in the echo area whether to discard the info. | |
1679 | If you answer no, there a slight risk that Emacs might crash, so | |
1680 | only do it if you really want to undo the command. | |
1681 | ||
1682 | This option is mainly intended for debugging. You have to be | |
1683 | careful if you use it for other purposes. Garbage collection is | |
1684 | inhibited while the question is asked, meaning that Emacs might | |
1685 | leak memory. So you should make sure that you do not wait | |
1686 | excessively long before answering the question." | |
1687 | :type 'boolean | |
1688 | :group 'undo | |
bf247b6e | 1689 | :version "22.1") |
28cb725d | 1690 | |
a1a801de RS |
1691 | (defvar undo-extra-outer-limit nil |
1692 | "If non-nil, an extra level of size that's ok in an undo item. | |
1693 | We don't ask the user about truncating the undo list until the | |
28cb725d LT |
1694 | current item gets bigger than this amount. |
1695 | ||
1696 | This variable only matters if `undo-ask-before-discard' is non-nil.") | |
a1a801de RS |
1697 | (make-variable-buffer-local 'undo-extra-outer-limit) |
1698 | ||
28cb725d LT |
1699 | ;; When the first undo batch in an undo list is longer than |
1700 | ;; undo-outer-limit, this function gets called to warn the user that | |
1701 | ;; the undo info for the current command was discarded. Garbage | |
1702 | ;; collection is inhibited around the call, so it had better not do a | |
1703 | ;; lot of consing. | |
b6e8e8e5 RS |
1704 | (setq undo-outer-limit-function 'undo-outer-limit-truncate) |
1705 | (defun undo-outer-limit-truncate (size) | |
28cb725d LT |
1706 | (if undo-ask-before-discard |
1707 | (when (or (null undo-extra-outer-limit) | |
1708 | (> size undo-extra-outer-limit)) | |
1709 | ;; Don't ask the question again unless it gets even bigger. | |
1710 | ;; This applies, in particular, if the user quits from the question. | |
1711 | ;; Such a quit quits out of GC, but something else will call GC | |
1712 | ;; again momentarily. It will call this function again, | |
1713 | ;; but we don't want to ask the question again. | |
1714 | (setq undo-extra-outer-limit (+ size 50000)) | |
1715 | (if (let (use-dialog-box track-mouse executing-kbd-macro ) | |
d5aa078b | 1716 | (yes-or-no-p (format "Buffer `%s' undo info is %d bytes long; discard it? " |
28cb725d LT |
1717 | (buffer-name) size))) |
1718 | (progn (setq buffer-undo-list nil) | |
1719 | (setq undo-extra-outer-limit nil) | |
1720 | t) | |
1721 | nil)) | |
1722 | (display-warning '(undo discard-info) | |
1723 | (concat | |
d5aa078b | 1724 | (format "Buffer `%s' undo info was %d bytes long.\n" |
28cb725d LT |
1725 | (buffer-name) size) |
1726 | "The undo info was discarded because it exceeded \ | |
1727 | `undo-outer-limit'. | |
1728 | ||
1729 | This is normal if you executed a command that made a huge change | |
1730 | to the buffer. In that case, to prevent similar problems in the | |
1731 | future, set `undo-outer-limit' to a value that is large enough to | |
1732 | cover the maximum size of normal changes you expect a single | |
1733 | command to make, but not so large that it might exceed the | |
1734 | maximum memory allotted to Emacs. | |
1735 | ||
1736 | If you did not execute any such command, the situation is | |
1737 | probably due to a bug and you should report it. | |
1738 | ||
1739 | You can disable the popping up of this buffer by adding the entry | |
1740 | \(undo discard-info) to the user option `warning-suppress-types'.\n") | |
1741 | :warning) | |
1742 | (setq buffer-undo-list nil) | |
1743 | t)) | |
e1e04350 | 1744 | \f |
009ef402 RS |
1745 | (defvar shell-command-history nil |
1746 | "History list for some commands that read shell commands.") | |
1747 | ||
59fc41e5 RS |
1748 | (defvar shell-command-switch "-c" |
1749 | "Switch used to have the shell execute its command line argument.") | |
1750 | ||
cc039f78 KH |
1751 | (defvar shell-command-default-error-buffer nil |
1752 | "*Buffer name for `shell-command' and `shell-command-on-region' error output. | |
637fff82 | 1753 | This buffer is used when `shell-command' or `shell-command-on-region' |
cc039f78 KH |
1754 | is run interactively. A value of nil means that output to stderr and |
1755 | stdout will be intermixed in the output stream.") | |
1756 | ||
1757 | (defun shell-command (command &optional output-buffer error-buffer) | |
2076c87c | 1758 | "Execute string COMMAND in inferior shell; display output, if any. |
0b3f96d4 | 1759 | With prefix argument, insert the COMMAND's output at point. |
d382f610 | 1760 | |
2076c87c | 1761 | If COMMAND ends in ampersand, execute it asynchronously. |
d382f610 | 1762 | The output appears in the buffer `*Async Shell Command*'. |
bcad4985 | 1763 | That buffer is in shell mode. |
d382f610 | 1764 | |
939ac10c GM |
1765 | Otherwise, COMMAND is executed synchronously. The output appears in |
1766 | the buffer `*Shell Command Output*'. If the output is short enough to | |
1767 | display in the echo area (which is determined by the variables | |
1768 | `resize-mini-windows' and `max-mini-window-height'), it is shown | |
1769 | there, but it is nonetheless available in buffer `*Shell Command | |
e1e04350 | 1770 | Output*' even though that buffer is not automatically displayed. |
d0d74413 | 1771 | |
07f458c1 RS |
1772 | To specify a coding system for converting non-ASCII characters |
1773 | in the shell command output, use \\[universal-coding-system-argument] | |
1774 | before this command. | |
1775 | ||
1776 | Noninteractive callers can specify coding systems by binding | |
1777 | `coding-system-for-read' and `coding-system-for-write'. | |
1778 | ||
d0d74413 RS |
1779 | The optional second argument OUTPUT-BUFFER, if non-nil, |
1780 | says to put the output in some other buffer. | |
1781 | If OUTPUT-BUFFER is a buffer or buffer name, put the output there. | |
1782 | If OUTPUT-BUFFER is not a buffer and not nil, | |
1783 | insert output in current buffer. (This cannot be done asynchronously.) | |
cc039f78 KH |
1784 | In either case, the output is inserted after point (leaving mark after it). |
1785 | ||
2e033693 RS |
1786 | If the command terminates without error, but generates output, |
1787 | and you did not specify \"insert it in the current buffer\", | |
1788 | the output can be displayed in the echo area or in its buffer. | |
1789 | If the output is short enough to display in the echo area | |
1790 | \(determined by the variable `max-mini-window-height' if | |
1791 | `resize-mini-windows' is non-nil), it is shown there. Otherwise, | |
1792 | the buffer containing the output is displayed. | |
1793 | ||
1794 | If there is output and an error, and you did not specify \"insert it | |
1795 | in the current buffer\", a message about the error goes at the end | |
1796 | of the output. | |
1797 | ||
1798 | If there is no output, or if output is inserted in the current buffer, | |
1799 | then `*Shell Command Output*' is deleted. | |
1800 | ||
cc039f78 KH |
1801 | If the optional third argument ERROR-BUFFER is non-nil, it is a buffer |
1802 | or buffer name to which to direct the command's standard error output. | |
1803 | If it is nil, error output is mingled with regular output. | |
1804 | In an interactive call, the variable `shell-command-default-error-buffer' | |
1805 | specifies the value of ERROR-BUFFER." | |
1806 | ||
aa00b92d RS |
1807 | (interactive (list (read-from-minibuffer "Shell command: " |
1808 | nil nil nil 'shell-command-history) | |
cc039f78 KH |
1809 | current-prefix-arg |
1810 | shell-command-default-error-buffer)) | |
c7edd03c KH |
1811 | ;; Look for a handler in case default-directory is a remote file name. |
1812 | (let ((handler | |
1813 | (find-file-name-handler (directory-file-name default-directory) | |
1814 | 'shell-command))) | |
1815 | (if handler | |
cc039f78 | 1816 | (funcall handler 'shell-command command output-buffer error-buffer) |
c7edd03c KH |
1817 | (if (and output-buffer |
1818 | (not (or (bufferp output-buffer) (stringp output-buffer)))) | |
2e033693 | 1819 | ;; Output goes in current buffer. |
cc039f78 | 1820 | (let ((error-file |
1e722f9f | 1821 | (if error-buffer |
b005abd5 | 1822 | (make-temp-file |
171a45d9 EZ |
1823 | (expand-file-name "scor" |
1824 | (or small-temporary-file-directory | |
1825 | temporary-file-directory))) | |
cc039f78 KH |
1826 | nil))) |
1827 | (barf-if-buffer-read-only) | |
63437623 | 1828 | (push-mark nil t) |
cc039f78 KH |
1829 | ;; We do not use -f for csh; we will not support broken use of |
1830 | ;; .cshrcs. Even the BSD csh manual says to use | |
1831 | ;; "if ($?prompt) exit" before things which are not useful | |
1832 | ;; non-interactively. Besides, if someone wants their other | |
1833 | ;; aliases for shell commands then they can still have them. | |
1e722f9f | 1834 | (call-process shell-file-name nil |
cc039f78 KH |
1835 | (if error-file |
1836 | (list t error-file) | |
1837 | t) | |
1838 | nil shell-command-switch command) | |
1839 | (when (and error-file (file-exists-p error-file)) | |
1840 | (if (< 0 (nth 7 (file-attributes error-file))) | |
1841 | (with-current-buffer (get-buffer-create error-buffer) | |
1842 | (let ((pos-from-end (- (point-max) (point)))) | |
1843 | (or (bobp) | |
1844 | (insert "\f\n")) | |
1845 | ;; Do no formatting while reading error file, | |
1846 | ;; because that can run a shell command, and we | |
1847 | ;; don't want that to cause an infinite recursion. | |
1848 | (format-insert-file error-file nil) | |
1849 | ;; Put point after the inserted errors. | |
1850 | (goto-char (- (point-max) pos-from-end))) | |
1851 | (display-buffer (current-buffer)))) | |
1852 | (delete-file error-file)) | |
1853 | ;; This is like exchange-point-and-mark, but doesn't | |
1854 | ;; activate the mark. It is cleaner to avoid activation, | |
1855 | ;; even though the command loop would deactivate the mark | |
1856 | ;; because we inserted text. | |
1857 | (goto-char (prog1 (mark t) | |
1858 | (set-marker (mark-marker) (point) | |
1859 | (current-buffer))))) | |
2e033693 | 1860 | ;; Output goes in a separate buffer. |
c7edd03c KH |
1861 | ;; Preserve the match data in case called from a program. |
1862 | (save-match-data | |
aab5d2c5 | 1863 | (if (string-match "[ \t]*&[ \t]*\\'" command) |
c7edd03c KH |
1864 | ;; Command ending with ampersand means asynchronous. |
1865 | (let ((buffer (get-buffer-create | |
1866 | (or output-buffer "*Async Shell Command*"))) | |
1867 | (directory default-directory) | |
1868 | proc) | |
1869 | ;; Remove the ampersand. | |
1870 | (setq command (substring command 0 (match-beginning 0))) | |
1871 | ;; If will kill a process, query first. | |
1872 | (setq proc (get-buffer-process buffer)) | |
1873 | (if proc | |
1874 | (if (yes-or-no-p "A command is running. Kill it? ") | |
1875 | (kill-process proc) | |
1876 | (error "Shell command in progress"))) | |
1e96c007 | 1877 | (with-current-buffer buffer |
c7edd03c KH |
1878 | (setq buffer-read-only nil) |
1879 | (erase-buffer) | |
1880 | (display-buffer buffer) | |
1881 | (setq default-directory directory) | |
1e722f9f | 1882 | (setq proc (start-process "Shell" buffer shell-file-name |
c7edd03c KH |
1883 | shell-command-switch command)) |
1884 | (setq mode-line-process '(":%s")) | |
c2020c27 | 1885 | (require 'shell) (shell-mode) |
c7edd03c KH |
1886 | (set-process-sentinel proc 'shell-command-sentinel) |
1887 | )) | |
cc039f78 KH |
1888 | (shell-command-on-region (point) (point) command |
1889 | output-buffer nil error-buffer))))))) | |
eaae8106 | 1890 | |
f69aad2b MB |
1891 | (defun display-message-or-buffer (message |
1892 | &optional buffer-name not-this-window frame) | |
1893 | "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer. | |
1894 | MESSAGE may be either a string or a buffer. | |
1895 | ||
1896 | A buffer is displayed using `display-buffer' if MESSAGE is too long for | |
939ac10c GM |
1897 | the maximum height of the echo area, as defined by `max-mini-window-height' |
1898 | if `resize-mini-windows' is non-nil. | |
f69aad2b | 1899 | |
2a3f00bf MB |
1900 | Returns either the string shown in the echo area, or when a pop-up |
1901 | buffer is used, the window used to display it. | |
1902 | ||
f69aad2b MB |
1903 | If MESSAGE is a string, then the optional argument BUFFER-NAME is the |
1904 | name of the buffer used to display it in the case where a pop-up buffer | |
1905 | is used, defaulting to `*Message*'. In the case where MESSAGE is a | |
1906 | string and it is displayed in the echo area, it is not specified whether | |
1907 | the contents are inserted into the buffer anyway. | |
1908 | ||
1909 | Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer', | |
1910 | and only used if a buffer is displayed." | |
aca2d9bb EZ |
1911 | (cond ((and (stringp message) |
1912 | (not (string-match "\n" message)) | |
1913 | (<= (length message) (frame-width))) | |
f69aad2b MB |
1914 | ;; Trivial case where we can use the echo area |
1915 | (message "%s" message)) | |
1916 | ((and (stringp message) | |
aca2d9bb EZ |
1917 | (= (string-match "\n" message) (1- (length message))) |
1918 | (<= (1- (length message)) (frame-width))) | |
f69aad2b MB |
1919 | ;; Trivial case where we can just remove single trailing newline |
1920 | (message "%s" (substring message 0 (1- (length message))))) | |
1921 | (t | |
1922 | ;; General case | |
1923 | (with-current-buffer | |
1924 | (if (bufferp message) | |
1925 | message | |
1926 | (get-buffer-create (or buffer-name "*Message*"))) | |
1927 | ||
1928 | (unless (bufferp message) | |
1929 | (erase-buffer) | |
1930 | (insert message)) | |
1931 | ||
1932 | (let ((lines | |
1933 | (if (= (buffer-size) 0) | |
1934 | 0 | |
62ffcd76 | 1935 | (count-screen-lines nil nil nil (minibuffer-window))))) |
4f017185 RS |
1936 | (cond ((= lines 0)) |
1937 | ((and (or (<= lines 1) | |
aab5d2c5 RS |
1938 | (<= lines |
1939 | (if resize-mini-windows | |
1940 | (cond ((floatp max-mini-window-height) | |
1941 | (* (frame-height) | |
1942 | max-mini-window-height)) | |
1943 | ((integerp max-mini-window-height) | |
1944 | max-mini-window-height) | |
1945 | (t | |
1946 | 1)) | |
1947 | 1))) | |
1948 | ;; Don't use the echo area if the output buffer is | |
1949 | ;; already dispayed in the selected frame. | |
61b80ebf | 1950 | (not (get-buffer-window (current-buffer)))) |
f69aad2b MB |
1951 | ;; Echo area |
1952 | (goto-char (point-max)) | |
1953 | (when (bolp) | |
1954 | (backward-char 1)) | |
1955 | (message "%s" (buffer-substring (point-min) (point)))) | |
1956 | (t | |
1957 | ;; Buffer | |
1958 | (goto-char (point-min)) | |
31252c00 MB |
1959 | (display-buffer (current-buffer) |
1960 | not-this-window frame)))))))) | |
f69aad2b MB |
1961 | |
1962 | ||
2076c87c JB |
1963 | ;; We have a sentinel to prevent insertion of a termination message |
1964 | ;; in the buffer itself. | |
1965 | (defun shell-command-sentinel (process signal) | |
bcad4985 | 1966 | (if (memq (process-status process) '(exit signal)) |
1e722f9f | 1967 | (message "%s: %s." |
bcad4985 KH |
1968 | (car (cdr (cdr (process-command process)))) |
1969 | (substring signal 0 -1)))) | |
2076c87c | 1970 | |
d0d74413 | 1971 | (defun shell-command-on-region (start end command |
cce1c318 | 1972 | &optional output-buffer replace |
63619f42 | 1973 | error-buffer display-error-buffer) |
2076c87c JB |
1974 | "Execute string COMMAND in inferior shell with region as input. |
1975 | Normally display output (if any) in temp buffer `*Shell Command Output*'; | |
a0184aeb DL |
1976 | Prefix arg means replace the region with it. Return the exit code of |
1977 | COMMAND. | |
56c0450e | 1978 | |
07f458c1 RS |
1979 | To specify a coding system for converting non-ASCII characters |
1980 | in the input and output to the shell command, use \\[universal-coding-system-argument] | |
1981 | before this command. By default, the input (from the current buffer) | |
1982 | is encoded in the same coding system that will be used to save the file, | |
1983 | `buffer-file-coding-system'. If the output is going to replace the region, | |
1984 | then it is decoded from that same coding system. | |
1985 | ||
63619f42 RS |
1986 | The noninteractive arguments are START, END, COMMAND, |
1987 | OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER. | |
1988 | Noninteractive callers can specify coding systems by binding | |
1989 | `coding-system-for-read' and `coding-system-for-write'. | |
2076c87c | 1990 | |
2e033693 RS |
1991 | If the command generates output, the output may be displayed |
1992 | in the echo area or in a buffer. | |
1993 | If the output is short enough to display in the echo area | |
1994 | \(determined by the variable `max-mini-window-height' if | |
1995 | `resize-mini-windows' is non-nil), it is shown there. Otherwise | |
1996 | it is displayed in the buffer `*Shell Command Output*'. The output | |
1997 | is available in that buffer in both cases. | |
1998 | ||
1999 | If there is output and an error, a message about the error | |
2000 | appears at the end of the output. | |
2001 | ||
2002 | If there is no output, or if output is inserted in the current buffer, | |
2003 | then `*Shell Command Output*' is deleted. | |
d0d74413 | 2004 | |
56c0450e RS |
2005 | If the optional fourth argument OUTPUT-BUFFER is non-nil, |
2006 | that says to put the output in some other buffer. | |
d0d74413 RS |
2007 | If OUTPUT-BUFFER is a buffer or buffer name, put the output there. |
2008 | If OUTPUT-BUFFER is not a buffer and not nil, | |
2009 | insert output in the current buffer. | |
cce1c318 RS |
2010 | In either case, the output is inserted after point (leaving mark after it). |
2011 | ||
8923a211 RS |
2012 | If REPLACE, the optional fifth argument, is non-nil, that means insert |
2013 | the output in place of text from START to END, putting point and mark | |
2014 | around it. | |
2015 | ||
b735c991 | 2016 | If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer |
cce1c318 | 2017 | or buffer name to which to direct the command's standard error output. |
7fd47839 | 2018 | If it is nil, error output is mingled with regular output. |
63619f42 RS |
2019 | If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there |
2020 | were any errors. (This is always t, interactively.) | |
cc039f78 KH |
2021 | In an interactive call, the variable `shell-command-default-error-buffer' |
2022 | specifies the value of ERROR-BUFFER." | |
195ce311 RS |
2023 | (interactive (let (string) |
2024 | (unless (mark) | |
2025 | (error "The mark is not set now, so there is no region")) | |
2026 | ;; Do this before calling region-beginning | |
2027 | ;; and region-end, in case subprocess output | |
2028 | ;; relocates them while we are in the minibuffer. | |
2029 | (setq string (read-from-minibuffer "Shell command on region: " | |
2030 | nil nil nil | |
2031 | 'shell-command-history)) | |
2b03c506 RS |
2032 | ;; call-interactively recognizes region-beginning and |
2033 | ;; region-end specially, leaving them in the history. | |
2034 | (list (region-beginning) (region-end) | |
cae49185 RS |
2035 | string |
2036 | current-prefix-arg | |
7fd47839 | 2037 | current-prefix-arg |
63619f42 RS |
2038 | shell-command-default-error-buffer |
2039 | t))) | |
cce1c318 | 2040 | (let ((error-file |
171a45d9 | 2041 | (if error-buffer |
b005abd5 | 2042 | (make-temp-file |
171a45d9 EZ |
2043 | (expand-file-name "scor" |
2044 | (or small-temporary-file-directory | |
2045 | temporary-file-directory))) | |
a0184aeb DL |
2046 | nil)) |
2047 | exit-status) | |
7fd47839 RS |
2048 | (if (or replace |
2049 | (and output-buffer | |
748d6ca4 | 2050 | (not (or (bufferp output-buffer) (stringp output-buffer))))) |
7fd47839 RS |
2051 | ;; Replace specified region with output from command. |
2052 | (let ((swap (and replace (< start end)))) | |
2053 | ;; Don't muck with mark unless REPLACE says we should. | |
2054 | (goto-char start) | |
30883773 | 2055 | (and replace (push-mark (point) 'nomsg)) |
a0184aeb DL |
2056 | (setq exit-status |
2057 | (call-process-region start end shell-file-name t | |
2058 | (if error-file | |
2059 | (list t error-file) | |
2060 | t) | |
2061 | nil shell-command-switch command)) | |
e1e04350 SM |
2062 | ;; It is rude to delete a buffer which the command is not using. |
2063 | ;; (let ((shell-buffer (get-buffer "*Shell Command Output*"))) | |
2064 | ;; (and shell-buffer (not (eq shell-buffer (current-buffer))) | |
2065 | ;; (kill-buffer shell-buffer))) | |
7fd47839 RS |
2066 | ;; Don't muck with mark unless REPLACE says we should. |
2067 | (and replace swap (exchange-point-and-mark))) | |
2068 | ;; No prefix argument: put the output in a temp buffer, | |
2069 | ;; replacing its entire contents. | |
2070 | (let ((buffer (get-buffer-create | |
d4bbcbb4 | 2071 | (or output-buffer "*Shell Command Output*")))) |
7fd47839 RS |
2072 | (unwind-protect |
2073 | (if (eq buffer (current-buffer)) | |
2074 | ;; If the input is the same buffer as the output, | |
2075 | ;; delete everything but the specified region, | |
2076 | ;; then replace that region with the output. | |
2077 | (progn (setq buffer-read-only nil) | |
2078 | (delete-region (max start end) (point-max)) | |
2079 | (delete-region (point-min) (min start end)) | |
2080 | (setq exit-status | |
2081 | (call-process-region (point-min) (point-max) | |
1e722f9f | 2082 | shell-file-name t |
7fd47839 RS |
2083 | (if error-file |
2084 | (list t error-file) | |
2085 | t) | |
a0184aeb DL |
2086 | nil shell-command-switch |
2087 | command))) | |
2088 | ;; Clear the output buffer, then run the command with | |
2089 | ;; output there. | |
c2e303c8 GM |
2090 | (let ((directory default-directory)) |
2091 | (save-excursion | |
2092 | (set-buffer buffer) | |
2093 | (setq buffer-read-only nil) | |
2094 | (if (not output-buffer) | |
2095 | (setq default-directory directory)) | |
2096 | (erase-buffer))) | |
7fd47839 RS |
2097 | (setq exit-status |
2098 | (call-process-region start end shell-file-name nil | |
2099 | (if error-file | |
2100 | (list buffer error-file) | |
2101 | buffer) | |
a0184aeb | 2102 | nil shell-command-switch command))) |
2e033693 | 2103 | ;; Report the output. |
9a98fa64 | 2104 | (with-current-buffer buffer |
f1180544 | 2105 | (setq mode-line-process |
d4bbcbb4 AS |
2106 | (cond ((null exit-status) |
2107 | " - Error") | |
2108 | ((stringp exit-status) | |
2109 | (format " - Signal [%s]" exit-status)) | |
2110 | ((not (equal 0 exit-status)) | |
2111 | (format " - Exit [%d]" exit-status))))) | |
f69aad2b MB |
2112 | (if (with-current-buffer buffer (> (point-max) (point-min))) |
2113 | ;; There's some output, display it | |
9a98fa64 | 2114 | (display-message-or-buffer buffer) |
f69aad2b | 2115 | ;; No output; error? |
94ddbe6d RS |
2116 | (let ((output |
2117 | (if (and error-file | |
2118 | (< 0 (nth 7 (file-attributes error-file)))) | |
2119 | "some error output" | |
2120 | "no output"))) | |
d4bbcbb4 AS |
2121 | (cond ((null exit-status) |
2122 | (message "(Shell command failed with error)")) | |
2123 | ((equal 0 exit-status) | |
2124 | (message "(Shell command succeeded with %s)" | |
2125 | output)) | |
2126 | ((stringp exit-status) | |
2127 | (message "(Shell command killed by signal %s)" | |
2128 | exit-status)) | |
2129 | (t | |
2130 | (message "(Shell command failed with code %d and %s)" | |
2131 | exit-status output)))) | |
e1e04350 SM |
2132 | ;; Don't kill: there might be useful info in the undo-log. |
2133 | ;; (kill-buffer buffer) | |
2134 | )))) | |
f69aad2b | 2135 | |
cc039f78 KH |
2136 | (when (and error-file (file-exists-p error-file)) |
2137 | (if (< 0 (nth 7 (file-attributes error-file))) | |
2138 | (with-current-buffer (get-buffer-create error-buffer) | |
2139 | (let ((pos-from-end (- (point-max) (point)))) | |
2140 | (or (bobp) | |
2141 | (insert "\f\n")) | |
2142 | ;; Do no formatting while reading error file, | |
2143 | ;; because that can run a shell command, and we | |
2144 | ;; don't want that to cause an infinite recursion. | |
2145 | (format-insert-file error-file nil) | |
2146 | ;; Put point after the inserted errors. | |
2147 | (goto-char (- (point-max) pos-from-end))) | |
63619f42 RS |
2148 | (and display-error-buffer |
2149 | (display-buffer (current-buffer))))) | |
cc039f78 | 2150 | (delete-file error-file)) |
a0184aeb | 2151 | exit-status)) |
1e722f9f | 2152 | |
d589bd99 RS |
2153 | (defun shell-command-to-string (command) |
2154 | "Execute shell command COMMAND and return its output as a string." | |
2155 | (with-output-to-string | |
17cc9013 RS |
2156 | (with-current-buffer |
2157 | standard-output | |
2158 | (call-process shell-file-name nil t nil shell-command-switch command)))) | |
0457dd55 KG |
2159 | |
2160 | (defun process-file (program &optional infile buffer display &rest args) | |
2161 | "Process files synchronously in a separate process. | |
2162 | Similar to `call-process', but may invoke a file handler based on | |
2163 | `default-directory'. The current working directory of the | |
2164 | subprocess is `default-directory'. | |
2165 | ||
2166 | File names in INFILE and BUFFER are handled normally, but file | |
2167 | names in ARGS should be relative to `default-directory', as they | |
2168 | are passed to the process verbatim. \(This is a difference to | |
2169 | `call-process' which does not support file handlers for INFILE | |
2170 | and BUFFER.\) | |
2171 | ||
2172 | Some file handlers might not support all variants, for example | |
2173 | they might behave as if DISPLAY was nil, regardless of the actual | |
2174 | value passed." | |
2175 | (let ((fh (find-file-name-handler default-directory 'process-file)) | |
2176 | lc stderr-file) | |
2177 | (unwind-protect | |
2178 | (if fh (apply fh 'process-file program infile buffer display args) | |
8de40f9f | 2179 | (when infile (setq lc (file-local-copy infile))) |
0457dd55 | 2180 | (setq stderr-file (when (and (consp buffer) (stringp (cadr buffer))) |
85af630d KG |
2181 | (make-temp-file "emacs"))) |
2182 | (prog1 | |
2183 | (apply 'call-process program | |
2184 | (or lc infile) | |
2185 | (if stderr-file (list (car buffer) stderr-file) buffer) | |
2186 | display args) | |
2187 | (when stderr-file (copy-file stderr-file (cadr buffer))))) | |
0457dd55 KG |
2188 | (when stderr-file (delete-file stderr-file)) |
2189 | (when lc (delete-file lc))))) | |
2190 | ||
2191 | ||
2d88b556 | 2192 | \f |
1b43f83f | 2193 | (defvar universal-argument-map |
69d4c3c4 KH |
2194 | (let ((map (make-sparse-keymap))) |
2195 | (define-key map [t] 'universal-argument-other-key) | |
b9ff190d | 2196 | (define-key map (vector meta-prefix-char t) 'universal-argument-other-key) |
69d4c3c4 KH |
2197 | (define-key map [switch-frame] nil) |
2198 | (define-key map [?\C-u] 'universal-argument-more) | |
2199 | (define-key map [?-] 'universal-argument-minus) | |
2200 | (define-key map [?0] 'digit-argument) | |
2201 | (define-key map [?1] 'digit-argument) | |
2202 | (define-key map [?2] 'digit-argument) | |
2203 | (define-key map [?3] 'digit-argument) | |
2204 | (define-key map [?4] 'digit-argument) | |
2205 | (define-key map [?5] 'digit-argument) | |
2206 | (define-key map [?6] 'digit-argument) | |
2207 | (define-key map [?7] 'digit-argument) | |
2208 | (define-key map [?8] 'digit-argument) | |
2209 | (define-key map [?9] 'digit-argument) | |
bd7acc8d GM |
2210 | (define-key map [kp-0] 'digit-argument) |
2211 | (define-key map [kp-1] 'digit-argument) | |
2212 | (define-key map [kp-2] 'digit-argument) | |
2213 | (define-key map [kp-3] 'digit-argument) | |
2214 | (define-key map [kp-4] 'digit-argument) | |
2215 | (define-key map [kp-5] 'digit-argument) | |
2216 | (define-key map [kp-6] 'digit-argument) | |
2217 | (define-key map [kp-7] 'digit-argument) | |
2218 | (define-key map [kp-8] 'digit-argument) | |
2219 | (define-key map [kp-9] 'digit-argument) | |
2220 | (define-key map [kp-subtract] 'universal-argument-minus) | |
69d4c3c4 KH |
2221 | map) |
2222 | "Keymap used while processing \\[universal-argument].") | |
2223 | ||
0de84e16 RS |
2224 | (defvar universal-argument-num-events nil |
2225 | "Number of argument-specifying events read by `universal-argument'. | |
2226 | `universal-argument-other-key' uses this to discard those events | |
2227 | from (this-command-keys), and reread only the final command.") | |
2228 | ||
6904b34b EZ |
2229 | (defvar overriding-map-is-bound nil |
2230 | "Non-nil when `overriding-terminal-local-map' is `universal-argument-map'.") | |
2231 | ||
2232 | (defvar saved-overriding-map nil | |
2233 | "The saved value of `overriding-terminal-local-map'. | |
2234 | That variable gets restored to this value on exiting \"universal | |
2235 | argument mode\".") | |
2236 | ||
2237 | (defun ensure-overriding-map-is-bound () | |
2238 | "Check `overriding-terminal-local-map' is `universal-argument-map'." | |
2239 | (unless overriding-map-is-bound | |
2240 | (setq saved-overriding-map overriding-terminal-local-map) | |
2241 | (setq overriding-terminal-local-map universal-argument-map) | |
2242 | (setq overriding-map-is-bound t))) | |
2243 | ||
2244 | (defun restore-overriding-map () | |
2245 | "Restore `overriding-terminal-local-map' to its saved value." | |
2246 | (setq overriding-terminal-local-map saved-overriding-map) | |
2247 | (setq overriding-map-is-bound nil)) | |
2248 | ||
e8d1a377 KH |
2249 | (defun universal-argument () |
2250 | "Begin a numeric argument for the following command. | |
2251 | Digits or minus sign following \\[universal-argument] make up the numeric argument. | |
2252 | \\[universal-argument] following the digits or minus sign ends the argument. | |
2253 | \\[universal-argument] without digits or minus sign provides 4 as argument. | |
2254 | Repeating \\[universal-argument] without digits or minus sign | |
0565d307 RS |
2255 | multiplies the argument by 4 each time. |
2256 | For some commands, just \\[universal-argument] by itself serves as a flag | |
a697fc62 RS |
2257 | which is different in effect from any particular numeric argument. |
2258 | These commands include \\[set-mark-command] and \\[start-kbd-macro]." | |
69d4c3c4 KH |
2259 | (interactive) |
2260 | (setq prefix-arg (list 4)) | |
0de84e16 | 2261 | (setq universal-argument-num-events (length (this-command-keys))) |
6904b34b | 2262 | (ensure-overriding-map-is-bound)) |
e8d1a377 | 2263 | |
69d4c3c4 KH |
2264 | ;; A subsequent C-u means to multiply the factor by 4 if we've typed |
2265 | ;; nothing but C-u's; otherwise it means to terminate the prefix arg. | |
2266 | (defun universal-argument-more (arg) | |
e8d1a377 | 2267 | (interactive "P") |
69d4c3c4 KH |
2268 | (if (consp arg) |
2269 | (setq prefix-arg (list (* 4 (car arg)))) | |
1cd24721 RS |
2270 | (if (eq arg '-) |
2271 | (setq prefix-arg (list -4)) | |
2272 | (setq prefix-arg arg) | |
6904b34b | 2273 | (restore-overriding-map))) |
0de84e16 | 2274 | (setq universal-argument-num-events (length (this-command-keys)))) |
e8d1a377 KH |
2275 | |
2276 | (defun negative-argument (arg) | |
2277 | "Begin a negative numeric argument for the next command. | |
2278 | \\[universal-argument] following digits or minus sign ends the argument." | |
2279 | (interactive "P") | |
69d4c3c4 KH |
2280 | (cond ((integerp arg) |
2281 | (setq prefix-arg (- arg))) | |
2282 | ((eq arg '-) | |
2283 | (setq prefix-arg nil)) | |
2284 | (t | |
b9ff190d | 2285 | (setq prefix-arg '-))) |
0de84e16 | 2286 | (setq universal-argument-num-events (length (this-command-keys))) |
6904b34b | 2287 | (ensure-overriding-map-is-bound)) |
69d4c3c4 KH |
2288 | |
2289 | (defun digit-argument (arg) | |
2290 | "Part of the numeric argument for the next command. | |
2291 | \\[universal-argument] following digits or minus sign ends the argument." | |
2292 | (interactive "P") | |
bd7acc8d GM |
2293 | (let* ((char (if (integerp last-command-char) |
2294 | last-command-char | |
2295 | (get last-command-char 'ascii-character))) | |
2296 | (digit (- (logand char ?\177) ?0))) | |
69d4c3c4 KH |
2297 | (cond ((integerp arg) |
2298 | (setq prefix-arg (+ (* arg 10) | |
2299 | (if (< arg 0) (- digit) digit)))) | |
2300 | ((eq arg '-) | |
2301 | ;; Treat -0 as just -, so that -01 will work. | |
2302 | (setq prefix-arg (if (zerop digit) '- (- digit)))) | |
2303 | (t | |
b9ff190d | 2304 | (setq prefix-arg digit)))) |
0de84e16 | 2305 | (setq universal-argument-num-events (length (this-command-keys))) |
6904b34b | 2306 | (ensure-overriding-map-is-bound)) |
69d4c3c4 KH |
2307 | |
2308 | ;; For backward compatibility, minus with no modifiers is an ordinary | |
2309 | ;; command if digits have already been entered. | |
2310 | (defun universal-argument-minus (arg) | |
2311 | (interactive "P") | |
2312 | (if (integerp arg) | |
2313 | (universal-argument-other-key arg) | |
2314 | (negative-argument arg))) | |
2315 | ||
2316 | ;; Anything else terminates the argument and is left in the queue to be | |
2317 | ;; executed as a command. | |
2318 | (defun universal-argument-other-key (arg) | |
2319 | (interactive "P") | |
2320 | (setq prefix-arg arg) | |
0de84e16 RS |
2321 | (let* ((key (this-command-keys)) |
2322 | (keylist (listify-key-sequence key))) | |
2323 | (setq unread-command-events | |
06697cdb RS |
2324 | (append (nthcdr universal-argument-num-events keylist) |
2325 | unread-command-events))) | |
f0ef2555 | 2326 | (reset-this-command-lengths) |
6904b34b | 2327 | (restore-overriding-map)) |
2d88b556 | 2328 | \f |
7fcce20f RS |
2329 | (defvar buffer-substring-filters nil |
2330 | "List of filter functions for `filter-buffer-substring'. | |
2331 | Each function must accept a single argument, a string, and return | |
2332 | a string. The buffer substring is passed to the first function | |
2333 | in the list, and the return value of each function is passed to | |
2334 | the next. The return value of the last function is used as the | |
2335 | return value of `filter-buffer-substring'. | |
2336 | ||
2337 | If this variable is nil, no filtering is performed.") | |
2338 | ||
398c9ffb | 2339 | (defun filter-buffer-substring (beg end &optional delete noprops) |
7fcce20f RS |
2340 | "Return the buffer substring between BEG and END, after filtering. |
2341 | The buffer substring is passed through each of the filter | |
2342 | functions in `buffer-substring-filters', and the value from the | |
2343 | last filter function is returned. If `buffer-substring-filters' | |
2344 | is nil, the buffer substring is returned unaltered. | |
2345 | ||
2346 | If DELETE is non-nil, the text between BEG and END is deleted | |
2347 | from the buffer. | |
2348 | ||
398c9ffb KS |
2349 | If NOPROPS is non-nil, final string returned does not include |
2350 | text properties, while the string passed to the filters still | |
2351 | includes text properties from the buffer text. | |
2352 | ||
2cd16d74 | 2353 | Point is temporarily set to BEG before calling |
7fcce20f RS |
2354 | `buffer-substring-filters', in case the functions need to know |
2355 | where the text came from. | |
2356 | ||
398c9ffb KS |
2357 | This function should be used instead of `buffer-substring', |
2358 | `buffer-substring-no-properties', or `delete-and-extract-region' | |
2359 | when you want to allow filtering to take place. For example, | |
2360 | major or minor modes can use `buffer-substring-filters' to | |
2361 | extract characters that are special to a buffer, and should not | |
2362 | be copied into other buffers." | |
2363 | (cond | |
2364 | ((or delete buffer-substring-filters) | |
2365 | (save-excursion | |
2366 | (goto-char beg) | |
2367 | (let ((string (if delete (delete-and-extract-region beg end) | |
2368 | (buffer-substring beg end)))) | |
2369 | (dolist (filter buffer-substring-filters) | |
2370 | (setq string (funcall filter string))) | |
2371 | (if noprops | |
2372 | (set-text-properties 0 (length string) nil string)) | |
2373 | string))) | |
2374 | (noprops | |
2375 | (buffer-substring-no-properties beg end)) | |
2376 | (t | |
2377 | (buffer-substring beg end)))) | |
2378 | ||
7fcce20f | 2379 | |
93be67de | 2380 | ;;;; Window system cut and paste hooks. |
70e14c01 JB |
2381 | |
2382 | (defvar interprogram-cut-function nil | |
2383 | "Function to call to make a killed region available to other programs. | |
2384 | ||
2385 | Most window systems provide some sort of facility for cutting and | |
9f112a3d RS |
2386 | pasting text between the windows of different programs. |
2387 | This variable holds a function that Emacs calls whenever text | |
2388 | is put in the kill ring, to make the new kill available to other | |
70e14c01 JB |
2389 | programs. |
2390 | ||
9f112a3d RS |
2391 | The function takes one or two arguments. |
2392 | The first argument, TEXT, is a string containing | |
2393 | the text which should be made available. | |
8e5d85ff LT |
2394 | The second, optional, argument PUSH, has the same meaning as the |
2395 | similar argument to `x-set-cut-buffer', which see.") | |
70e14c01 JB |
2396 | |
2397 | (defvar interprogram-paste-function nil | |
2398 | "Function to call to get text cut from other programs. | |
2399 | ||
2400 | Most window systems provide some sort of facility for cutting and | |
9f112a3d RS |
2401 | pasting text between the windows of different programs. |
2402 | This variable holds a function that Emacs calls to obtain | |
70e14c01 JB |
2403 | text that other programs have provided for pasting. |
2404 | ||
2405 | The function should be called with no arguments. If the function | |
2406 | returns nil, then no other program has provided such text, and the top | |
2407 | of the Emacs kill ring should be used. If the function returns a | |
8e5d85ff LT |
2408 | string, then the caller of the function \(usually `current-kill') |
2409 | should put this string in the kill ring as the latest kill. | |
daa37602 JB |
2410 | |
2411 | Note that the function should return a string only if a program other | |
2412 | than Emacs has provided a string for pasting; if Emacs provided the | |
2413 | most recent string, the function should return nil. If it is | |
2414 | difficult to tell whether Emacs or some other program provided the | |
2415 | current string, it is probably good enough to return nil if the string | |
2416 | is equal (according to `string=') to the last text Emacs provided.") | |
2d88b556 | 2417 | \f |
70e14c01 | 2418 | |
eaae8106 | 2419 | |
70e14c01 | 2420 | ;;;; The kill ring data structure. |
2076c87c JB |
2421 | |
2422 | (defvar kill-ring nil | |
70e14c01 JB |
2423 | "List of killed text sequences. |
2424 | Since the kill ring is supposed to interact nicely with cut-and-paste | |
2425 | facilities offered by window systems, use of this variable should | |
2426 | interact nicely with `interprogram-cut-function' and | |
2427 | `interprogram-paste-function'. The functions `kill-new', | |
2428 | `kill-append', and `current-kill' are supposed to implement this | |
2429 | interaction; you may want to use them instead of manipulating the kill | |
2430 | ring directly.") | |
2076c87c | 2431 | |
bffa4d92 | 2432 | (defcustom kill-ring-max 60 |
69c1dd37 RS |
2433 | "*Maximum length of kill ring before oldest elements are thrown away." |
2434 | :type 'integer | |
2435 | :group 'killing) | |
2076c87c JB |
2436 | |
2437 | (defvar kill-ring-yank-pointer nil | |
2438 | "The tail of the kill ring whose car is the last thing yanked.") | |
2439 | ||
be5936a7 | 2440 | (defun kill-new (string &optional replace yank-handler) |
70e14c01 | 2441 | "Make STRING the latest kill in the kill ring. |
3e505153 | 2442 | Set `kill-ring-yank-pointer' to point to it. |
f914dc91 KH |
2443 | If `interprogram-cut-function' is non-nil, apply it to STRING. |
2444 | Optional second argument REPLACE non-nil means that STRING will replace | |
be5936a7 KS |
2445 | the front of the kill ring, rather than being added to the list. |
2446 | ||
2447 | Optional third arguments YANK-HANDLER controls how the STRING is later | |
f1180544 | 2448 | inserted into a buffer; see `insert-for-yank' for details. |
2a262563 | 2449 | When a yank handler is specified, STRING must be non-empty (the yank |
8e5d85ff | 2450 | handler, if non-nil, is stored as a `yank-handler' text property on STRING). |
2a262563 KS |
2451 | |
2452 | When the yank handler has a non-nil PARAM element, the original STRING | |
2453 | argument is not used by `insert-for-yank'. However, since Lisp code | |
f33321ad | 2454 | may access and use elements from the kill ring directly, the STRING |
2a262563 KS |
2455 | argument should still be a \"useful\" string for such uses." |
2456 | (if (> (length string) 0) | |
f1180544 | 2457 | (if yank-handler |
7e46b7bf LT |
2458 | (put-text-property 0 (length string) |
2459 | 'yank-handler yank-handler string)) | |
2a262563 | 2460 | (if yank-handler |
f1180544 | 2461 | (signal 'args-out-of-range |
2a262563 KS |
2462 | (list string "yank-handler specified for empty string")))) |
2463 | (if (fboundp 'menu-bar-update-yank-menu) | |
2464 | (menu-bar-update-yank-menu string (and replace (car kill-ring)))) | |
ab7e20d5 | 2465 | (if (and replace kill-ring) |
f914dc91 | 2466 | (setcar kill-ring string) |
1b5fd09e | 2467 | (push string kill-ring) |
f914dc91 KH |
2468 | (if (> (length kill-ring) kill-ring-max) |
2469 | (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))) | |
70e14c01 JB |
2470 | (setq kill-ring-yank-pointer kill-ring) |
2471 | (if interprogram-cut-function | |
657a33ab | 2472 | (funcall interprogram-cut-function string (not replace)))) |
70e14c01 | 2473 | |
be5936a7 | 2474 | (defun kill-append (string before-p &optional yank-handler) |
70e14c01 JB |
2475 | "Append STRING to the end of the latest kill in the kill ring. |
2476 | If BEFORE-P is non-nil, prepend STRING to the kill. | |
8e5d85ff LT |
2477 | Optional third argument YANK-HANDLER, if non-nil, specifies the |
2478 | yank-handler text property to be set on the combined kill ring | |
2479 | string. If the specified yank-handler arg differs from the | |
2480 | yank-handler property of the latest kill string, this function | |
2481 | adds the combined string to the kill ring as a new element, | |
2482 | instead of replacing the last kill with it. | |
be5936a7 KS |
2483 | If `interprogram-cut-function' is set, pass the resulting kill to it." |
2484 | (let* ((cur (car kill-ring))) | |
2485 | (kill-new (if before-p (concat string cur) (concat cur string)) | |
2486 | (or (= (length cur) 0) | |
2487 | (equal yank-handler (get-text-property 0 'yank-handler cur))) | |
2488 | yank-handler))) | |
70e14c01 JB |
2489 | |
2490 | (defun current-kill (n &optional do-not-move) | |
2491 | "Rotate the yanking point by N places, and then return that kill. | |
2492 | If N is zero, `interprogram-paste-function' is set, and calling it | |
2493 | returns a string, then that string is added to the front of the | |
2494 | kill ring and returned as the latest kill. | |
1e722f9f | 2495 | If optional arg DO-NOT-MOVE is non-nil, then don't actually move the |
70e14c01 JB |
2496 | yanking point; just return the Nth kill forward." |
2497 | (let ((interprogram-paste (and (= n 0) | |
2498 | interprogram-paste-function | |
2499 | (funcall interprogram-paste-function)))) | |
2500 | (if interprogram-paste | |
2501 | (progn | |
2502 | ;; Disable the interprogram cut function when we add the new | |
2503 | ;; text to the kill ring, so Emacs doesn't try to own the | |
2504 | ;; selection, with identical text. | |
2505 | (let ((interprogram-cut-function nil)) | |
2506 | (kill-new interprogram-paste)) | |
2507 | interprogram-paste) | |
2508 | (or kill-ring (error "Kill ring is empty")) | |
47096a67 PE |
2509 | (let ((ARGth-kill-element |
2510 | (nthcdr (mod (- n (length kill-ring-yank-pointer)) | |
2511 | (length kill-ring)) | |
2512 | kill-ring))) | |
70e14c01 JB |
2513 | (or do-not-move |
2514 | (setq kill-ring-yank-pointer ARGth-kill-element)) | |
2515 | (car ARGth-kill-element))))) | |
c88ab9ce | 2516 | |
c88ab9ce | 2517 | |
eaae8106 | 2518 | |
70e14c01 | 2519 | ;;;; Commands for manipulating the kill ring. |
c88ab9ce | 2520 | |
69c1dd37 RS |
2521 | (defcustom kill-read-only-ok nil |
2522 | "*Non-nil means don't signal an error for killing read-only text." | |
2523 | :type 'boolean | |
2524 | :group 'killing) | |
e6291fe1 | 2525 | |
3a5da8a8 RS |
2526 | (put 'text-read-only 'error-conditions |
2527 | '(text-read-only buffer-read-only error)) | |
2528 | (put 'text-read-only 'error-message "Text is read-only") | |
2529 | ||
be5936a7 | 2530 | (defun kill-region (beg end &optional yank-handler) |
66e9b2b2 RS |
2531 | "Kill (\"cut\") text between point and mark. |
2532 | This deletes the text from the buffer and saves it in the kill ring. | |
2076c87c | 2533 | The command \\[yank] can retrieve it from there. |
81558867 EZ |
2534 | \(If you want to kill and then yank immediately, use \\[kill-ring-save].) |
2535 | ||
2536 | If you want to append the killed region to the last killed text, | |
2537 | use \\[append-next-kill] before \\[kill-region]. | |
2538 | ||
2aa7a8bf JB |
2539 | If the buffer is read-only, Emacs will beep and refrain from deleting |
2540 | the text, but put the text in the kill ring anyway. This means that | |
2541 | you can use the killing commands to copy text from a read-only buffer. | |
2076c87c JB |
2542 | |
2543 | This is the primitive for programs to kill text (as opposed to deleting it). | |
c15dc81f | 2544 | Supply two arguments, character positions indicating the stretch of text |
2076c87c JB |
2545 | to be killed. |
2546 | Any command that calls this function is a \"kill command\". | |
2547 | If the previous command was also a kill command, | |
2548 | the text killed this time appends to the text killed last time | |
be5936a7 KS |
2549 | to make one entry in the kill ring. |
2550 | ||
8e5d85ff LT |
2551 | In Lisp code, optional third arg YANK-HANDLER, if non-nil, |
2552 | specifies the yank-handler text property to be set on the killed | |
2553 | text. See `insert-for-yank'." | |
214a3db0 RS |
2554 | ;; Pass point first, then mark, because the order matters |
2555 | ;; when calling kill-append. | |
2556 | (interactive (list (point) (mark))) | |
f39d6be0 RS |
2557 | (unless (and beg end) |
2558 | (error "The mark is not set now, so there is no region")) | |
ccd19b9f | 2559 | (condition-case nil |
7fcce20f | 2560 | (let ((string (filter-buffer-substring beg end t))) |
a1eb02bd SM |
2561 | (when string ;STRING is nil if BEG = END |
2562 | ;; Add that string to the kill ring, one way or another. | |
2563 | (if (eq last-command 'kill-region) | |
be5936a7 KS |
2564 | (kill-append string (< end beg) yank-handler) |
2565 | (kill-new string nil yank-handler))) | |
8a7cda9b | 2566 | (when (or string (eq last-command 'kill-region)) |
8e5d85ff LT |
2567 | (setq this-command 'kill-region)) |
2568 | nil) | |
ccd19b9f KH |
2569 | ((buffer-read-only text-read-only) |
2570 | ;; The code above failed because the buffer, or some of the characters | |
2571 | ;; in the region, are read-only. | |
2572 | ;; We should beep, in case the user just isn't aware of this. | |
2573 | ;; However, there's no harm in putting | |
2574 | ;; the region's text in the kill ring, anyway. | |
2575 | (copy-region-as-kill beg end) | |
cb3e1b4c RS |
2576 | ;; Set this-command now, so it will be set even if we get an error. |
2577 | (setq this-command 'kill-region) | |
2578 | ;; This should barf, if appropriate, and give us the correct error. | |
ccd19b9f | 2579 | (if kill-read-only-ok |
8e5d85ff | 2580 | (progn (message "Read only text copied to kill ring") nil) |
ccd19b9f KH |
2581 | ;; Signal an error if the buffer is read-only. |
2582 | (barf-if-buffer-read-only) | |
2583 | ;; If the buffer isn't read-only, the text is. | |
2584 | (signal 'text-read-only (list (current-buffer))))))) | |
2076c87c | 2585 | |
a382890a KH |
2586 | ;; copy-region-as-kill no longer sets this-command, because it's confusing |
2587 | ;; to get two copies of the text when the user accidentally types M-w and | |
2588 | ;; then corrects it with the intended C-w. | |
2076c87c JB |
2589 | (defun copy-region-as-kill (beg end) |
2590 | "Save the region as if killed, but don't kill it. | |
0e264847 | 2591 | In Transient Mark mode, deactivate the mark. |
46947372 JB |
2592 | If `interprogram-cut-function' is non-nil, also save the text for a window |
2593 | system cut and paste." | |
2076c87c JB |
2594 | (interactive "r") |
2595 | (if (eq last-command 'kill-region) | |
7fcce20f RS |
2596 | (kill-append (filter-buffer-substring beg end) (< end beg)) |
2597 | (kill-new (filter-buffer-substring beg end))) | |
1e722f9f | 2598 | (if transient-mark-mode |
5c7319b6 | 2599 | (setq deactivate-mark t)) |
2076c87c JB |
2600 | nil) |
2601 | ||
2602 | (defun kill-ring-save (beg end) | |
0964e562 | 2603 | "Save the region as if killed, but don't kill it. |
0e264847 | 2604 | In Transient Mark mode, deactivate the mark. |
0964e562 | 2605 | If `interprogram-cut-function' is non-nil, also save the text for a window |
0e264847 RS |
2606 | system cut and paste. |
2607 | ||
81558867 EZ |
2608 | If you want to append the killed line to the last killed text, |
2609 | use \\[append-next-kill] before \\[kill-ring-save]. | |
2610 | ||
0e264847 RS |
2611 | This command is similar to `copy-region-as-kill', except that it gives |
2612 | visual feedback indicating the extent of the region being copied." | |
2076c87c JB |
2613 | (interactive "r") |
2614 | (copy-region-as-kill beg end) | |
bbf41690 RS |
2615 | ;; This use of interactive-p is correct |
2616 | ;; because the code it controls just gives the user visual feedback. | |
3a801d0c | 2617 | (if (interactive-p) |
66050f10 RS |
2618 | (let ((other-end (if (= (point) beg) end beg)) |
2619 | (opoint (point)) | |
2620 | ;; Inhibit quitting so we can make a quit here | |
2621 | ;; look like a C-g typed as a command. | |
2622 | (inhibit-quit t)) | |
2623 | (if (pos-visible-in-window-p other-end (selected-window)) | |
977e2654 KS |
2624 | (unless (and transient-mark-mode |
2625 | (face-background 'region)) | |
66050f10 RS |
2626 | ;; Swap point and mark. |
2627 | (set-marker (mark-marker) (point) (current-buffer)) | |
2628 | (goto-char other-end) | |
e4ef3e92 | 2629 | (sit-for blink-matching-delay) |
66050f10 RS |
2630 | ;; Swap back. |
2631 | (set-marker (mark-marker) other-end (current-buffer)) | |
2632 | (goto-char opoint) | |
2633 | ;; If user quit, deactivate the mark | |
2634 | ;; as C-g would as a command. | |
e4e593ae | 2635 | (and quit-flag mark-active |
fcadf1c7 | 2636 | (deactivate-mark))) |
66050f10 RS |
2637 | (let* ((killed-text (current-kill 0)) |
2638 | (message-len (min (length killed-text) 40))) | |
2639 | (if (= (point) beg) | |
2640 | ;; Don't say "killed"; that is misleading. | |
2641 | (message "Saved text until \"%s\"" | |
2642 | (substring killed-text (- message-len))) | |
2643 | (message "Saved text from \"%s\"" | |
2644 | (substring killed-text 0 message-len)))))))) | |
2076c87c | 2645 | |
c75d4986 KH |
2646 | (defun append-next-kill (&optional interactive) |
2647 | "Cause following command, if it kills, to append to previous kill. | |
2648 | The argument is used for internal purposes; do not supply one." | |
2649 | (interactive "p") | |
2650 | ;; We don't use (interactive-p), since that breaks kbd macros. | |
2651 | (if interactive | |
2076c87c JB |
2652 | (progn |
2653 | (setq this-command 'kill-region) | |
2654 | (message "If the next command is a kill, it will append")) | |
2655 | (setq last-command 'kill-region))) | |
cfb4f123 | 2656 | \f |
93be67de | 2657 | ;; Yanking. |
2076c87c | 2658 | |
cfb4f123 RS |
2659 | ;; This is actually used in subr.el but defcustom does not work there. |
2660 | (defcustom yank-excluded-properties | |
be5936a7 | 2661 | '(read-only invisible intangible field mouse-face help-echo local-map keymap |
7408ee97 | 2662 | yank-handler follow-link fontified) |
c6ff5a4c LT |
2663 | "*Text properties to discard when yanking. |
2664 | The value should be a list of text properties to discard or t, | |
2665 | which means to discard all text properties." | |
cfb4f123 | 2666 | :type '(choice (const :tag "All" t) (repeat symbol)) |
c9f0110e | 2667 | :group 'killing |
bf247b6e | 2668 | :version "22.1") |
cfb4f123 | 2669 | |
120de5bd | 2670 | (defvar yank-window-start nil) |
be5936a7 | 2671 | (defvar yank-undo-function nil |
44f5a7b2 KS |
2672 | "If non-nil, function used by `yank-pop' to delete last stretch of yanked text. |
2673 | Function is called with two parameters, START and END corresponding to | |
2674 | the value of the mark and point; it is guaranteed that START <= END. | |
2675 | Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.") | |
120de5bd | 2676 | |
8e5d85ff | 2677 | (defun yank-pop (&optional arg) |
ff1fbe3e RS |
2678 | "Replace just-yanked stretch of killed text with a different stretch. |
2679 | This command is allowed only immediately after a `yank' or a `yank-pop'. | |
2076c87c | 2680 | At such a time, the region contains a stretch of reinserted |
ff1fbe3e | 2681 | previously-killed text. `yank-pop' deletes that text and inserts in its |
2076c87c JB |
2682 | place a different stretch of killed text. |
2683 | ||
2684 | With no argument, the previous kill is inserted. | |
ff1fbe3e RS |
2685 | With argument N, insert the Nth previous kill. |
2686 | If N is negative, this is a more recent kill. | |
2076c87c JB |
2687 | |
2688 | The sequence of kills wraps around, so that after the oldest one | |
a0e8eaa3 EZ |
2689 | comes the newest one. |
2690 | ||
2691 | When this command inserts killed text into the buffer, it honors | |
2692 | `yank-excluded-properties' and `yank-handler' as described in the | |
2693 | doc string for `insert-for-yank-1', which see." | |
2076c87c JB |
2694 | (interactive "*p") |
2695 | (if (not (eq last-command 'yank)) | |
2696 | (error "Previous command was not a yank")) | |
2697 | (setq this-command 'yank) | |
8e5d85ff | 2698 | (unless arg (setq arg 1)) |
3a5da8a8 RS |
2699 | (let ((inhibit-read-only t) |
2700 | (before (< (point) (mark t)))) | |
8254897f KS |
2701 | (if before |
2702 | (funcall (or yank-undo-function 'delete-region) (point) (mark t)) | |
2703 | (funcall (or yank-undo-function 'delete-region) (mark t) (point))) | |
be5936a7 | 2704 | (setq yank-undo-function nil) |
fd0f4056 | 2705 | (set-marker (mark-marker) (point) (current-buffer)) |
cfb4f123 | 2706 | (insert-for-yank (current-kill arg)) |
120de5bd RS |
2707 | ;; Set the window start back where it was in the yank command, |
2708 | ;; if possible. | |
2709 | (set-window-start (selected-window) yank-window-start t) | |
fd0f4056 RS |
2710 | (if before |
2711 | ;; This is like exchange-point-and-mark, but doesn't activate the mark. | |
2712 | ;; It is cleaner to avoid activation, even though the command | |
2713 | ;; loop would deactivate the mark because we inserted text. | |
2714 | (goto-char (prog1 (mark t) | |
2715 | (set-marker (mark-marker) (point) (current-buffer)))))) | |
0964e562 | 2716 | nil) |
2076c87c JB |
2717 | |
2718 | (defun yank (&optional arg) | |
f894e671 | 2719 | "Reinsert (\"paste\") the last stretch of killed text. |
2076c87c | 2720 | More precisely, reinsert the stretch of killed text most recently |
ff1fbe3e | 2721 | killed OR yanked. Put point at end, and set mark at beginning. |
d99f8496 | 2722 | With just \\[universal-argument] as argument, same but put point at beginning (and mark at end). |
ff1fbe3e | 2723 | With argument N, reinsert the Nth most recently killed stretch of killed |
2076c87c | 2724 | text. |
a0e8eaa3 EZ |
2725 | |
2726 | When this command inserts killed text into the buffer, it honors | |
2727 | `yank-excluded-properties' and `yank-handler' as described in the | |
2728 | doc string for `insert-for-yank-1', which see. | |
2729 | ||
2076c87c JB |
2730 | See also the command \\[yank-pop]." |
2731 | (interactive "*P") | |
120de5bd | 2732 | (setq yank-window-start (window-start)) |
456c617c RS |
2733 | ;; If we don't get all the way thru, make last-command indicate that |
2734 | ;; for the following command. | |
2735 | (setq this-command t) | |
2076c87c | 2736 | (push-mark (point)) |
cfb4f123 RS |
2737 | (insert-for-yank (current-kill (cond |
2738 | ((listp arg) 0) | |
8e5d85ff | 2739 | ((eq arg '-) -2) |
cfb4f123 | 2740 | (t (1- arg))))) |
2076c87c | 2741 | (if (consp arg) |
fd0f4056 RS |
2742 | ;; This is like exchange-point-and-mark, but doesn't activate the mark. |
2743 | ;; It is cleaner to avoid activation, even though the command | |
2744 | ;; loop would deactivate the mark because we inserted text. | |
2745 | (goto-char (prog1 (mark t) | |
2746 | (set-marker (mark-marker) (point) (current-buffer))))) | |
456c617c | 2747 | ;; If we do get all the way thru, make this-command indicate that. |
be5936a7 KS |
2748 | (if (eq this-command t) |
2749 | (setq this-command 'yank)) | |
0964e562 | 2750 | nil) |
70e14c01 JB |
2751 | |
2752 | (defun rotate-yank-pointer (arg) | |
2753 | "Rotate the yanking point in the kill ring. | |
2754 | With argument, rotate that many kills forward (or backward, if negative)." | |
2755 | (interactive "p") | |
2756 | (current-kill arg)) | |
2d88b556 | 2757 | \f |
93be67de KH |
2758 | ;; Some kill commands. |
2759 | ||
2760 | ;; Internal subroutine of delete-char | |
2761 | (defun kill-forward-chars (arg) | |
2762 | (if (listp arg) (setq arg (car arg))) | |
2763 | (if (eq arg '-) (setq arg -1)) | |
2764 | (kill-region (point) (forward-point arg))) | |
2765 | ||
2766 | ;; Internal subroutine of backward-delete-char | |
2767 | (defun kill-backward-chars (arg) | |
2768 | (if (listp arg) (setq arg (car arg))) | |
2769 | (if (eq arg '-) (setq arg -1)) | |
2770 | (kill-region (point) (forward-point (- arg)))) | |
2771 | ||
2772 | (defcustom backward-delete-char-untabify-method 'untabify | |
2773 | "*The method for untabifying when deleting backward. | |
1e722f9f SS |
2774 | Can be `untabify' -- turn a tab to many spaces, then delete one space; |
2775 | `hungry' -- delete all whitespace, both tabs and spaces; | |
2776 | `all' -- delete all whitespace, including tabs, spaces and newlines; | |
93be67de | 2777 | nil -- just delete one character." |
1e722f9f | 2778 | :type '(choice (const untabify) (const hungry) (const all) (const nil)) |
03167a34 | 2779 | :version "20.3" |
93be67de KH |
2780 | :group 'killing) |
2781 | ||
2782 | (defun backward-delete-char-untabify (arg &optional killp) | |
2783 | "Delete characters backward, changing tabs into spaces. | |
2784 | The exact behavior depends on `backward-delete-char-untabify-method'. | |
2785 | Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil. | |
2786 | Interactively, ARG is the prefix arg (default 1) | |
2787 | and KILLP is t if a prefix arg was specified." | |
2788 | (interactive "*p\nP") | |
2789 | (when (eq backward-delete-char-untabify-method 'untabify) | |
2790 | (let ((count arg)) | |
2791 | (save-excursion | |
2792 | (while (and (> count 0) (not (bobp))) | |
2793 | (if (= (preceding-char) ?\t) | |
2794 | (let ((col (current-column))) | |
2795 | (forward-char -1) | |
2796 | (setq col (- col (current-column))) | |
f33321ad | 2797 | (insert-char ?\s col) |
93be67de KH |
2798 | (delete-char 1))) |
2799 | (forward-char -1) | |
2800 | (setq count (1- count)))))) | |
2801 | (delete-backward-char | |
1e722f9f SS |
2802 | (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t") |
2803 | ((eq backward-delete-char-untabify-method 'all) | |
2804 | " \t\n\r")))) | |
2805 | (if skip | |
2806 | (let ((wh (- (point) (save-excursion (skip-chars-backward skip) | |
93be67de KH |
2807 | (point))))) |
2808 | (+ arg (if (zerop wh) 0 (1- wh)))) | |
1e722f9f | 2809 | arg)) |
93be67de KH |
2810 | killp)) |
2811 | ||
2812 | (defun zap-to-char (arg char) | |
2813 | "Kill up to and including ARG'th occurrence of CHAR. | |
2814 | Case is ignored if `case-fold-search' is non-nil in the current buffer. | |
2815 | Goes backward if ARG is negative; error if CHAR not found." | |
e761e42c | 2816 | (interactive "p\ncZap to char: ") |
ea9fc1bd KH |
2817 | (if (char-table-p translation-table-for-input) |
2818 | (setq char (or (aref translation-table-for-input char) char))) | |
93be67de KH |
2819 | (kill-region (point) (progn |
2820 | (search-forward (char-to-string char) nil nil arg) | |
2821 | ; (goto-char (if (> arg 0) (1- (point)) (1+ (point)))) | |
2822 | (point)))) | |
eaae8106 | 2823 | |
93be67de KH |
2824 | ;; kill-line and its subroutines. |
2825 | ||
2826 | (defcustom kill-whole-line nil | |
2827 | "*If non-nil, `kill-line' with no arg at beg of line kills the whole line." | |
2828 | :type 'boolean | |
2829 | :group 'killing) | |
2830 | ||
2831 | (defun kill-line (&optional arg) | |
2832 | "Kill the rest of the current line; if no nonblanks there, kill thru newline. | |
2833 | With prefix argument, kill that many lines from point. | |
2834 | Negative arguments kill lines backward. | |
8be7408c | 2835 | With zero argument, kills the text before point on the current line. |
93be67de KH |
2836 | |
2837 | When calling from a program, nil means \"no arg\", | |
2838 | a number counts as a prefix arg. | |
2839 | ||
2840 | To kill a whole line, when point is not at the beginning, type \ | |
2841 | \\[beginning-of-line] \\[kill-line] \\[kill-line]. | |
2842 | ||
2843 | If `kill-whole-line' is non-nil, then this command kills the whole line | |
2844 | including its terminating newline, when used at the beginning of a line | |
2845 | with no argument. As a consequence, you can always kill a whole line | |
d3f22784 EZ |
2846 | by typing \\[beginning-of-line] \\[kill-line]. |
2847 | ||
81558867 EZ |
2848 | If you want to append the killed line to the last killed text, |
2849 | use \\[append-next-kill] before \\[kill-line]. | |
2850 | ||
d3f22784 EZ |
2851 | If the buffer is read-only, Emacs will beep and refrain from deleting |
2852 | the line, but put the line in the kill ring anyway. This means that | |
1a534b89 RS |
2853 | you can use this command to copy text from a read-only buffer. |
2854 | \(If the variable `kill-read-only-ok' is non-nil, then this won't | |
2855 | even beep.)" | |
e761e42c | 2856 | (interactive "P") |
93be67de KH |
2857 | (kill-region (point) |
2858 | ;; It is better to move point to the other end of the kill | |
2859 | ;; before killing. That way, in a read-only buffer, point | |
2860 | ;; moves across the text that is copied to the kill ring. | |
2861 | ;; The choice has no effect on undo now that undo records | |
2862 | ;; the value of point from before the command was run. | |
2863 | (progn | |
2864 | (if arg | |
2865 | (forward-visible-line (prefix-numeric-value arg)) | |
2866 | (if (eobp) | |
2867 | (signal 'end-of-buffer nil)) | |
5560dc5d RS |
2868 | (let ((end |
2869 | (save-excursion | |
2870 | (end-of-visible-line) (point)))) | |
2871 | (if (or (save-excursion | |
2c6a2254 RS |
2872 | ;; If trailing whitespace is visible, |
2873 | ;; don't treat it as nothing. | |
2874 | (unless show-trailing-whitespace | |
2875 | (skip-chars-forward " \t" end)) | |
5560dc5d RS |
2876 | (= (point) end)) |
2877 | (and kill-whole-line (bolp))) | |
2878 | (forward-visible-line 1) | |
2879 | (goto-char end)))) | |
93be67de KH |
2880 | (point)))) |
2881 | ||
348de80b KG |
2882 | (defun kill-whole-line (&optional arg) |
2883 | "Kill current line. | |
6c770e38 LT |
2884 | With prefix arg, kill that many lines starting from the current line. |
2885 | If arg is negative, kill backward. Also kill the preceding newline. | |
01ba9662 | 2886 | \(This is meant to make \\[repeat] work well with negative arguments.\) |
348de80b | 2887 | If arg is zero, kill current line but exclude the trailing newline." |
f8b0f284 | 2888 | (interactive "p") |
6c770e38 LT |
2889 | (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp))) |
2890 | (signal 'end-of-buffer nil)) | |
2891 | (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp))) | |
2892 | (signal 'beginning-of-buffer nil)) | |
2893 | (unless (eq last-command 'kill-region) | |
2894 | (kill-new "") | |
2895 | (setq last-command 'kill-region)) | |
348de80b | 2896 | (cond ((zerop arg) |
6c770e38 LT |
2897 | ;; We need to kill in two steps, because the previous command |
2898 | ;; could have been a kill command, in which case the text | |
2899 | ;; before point needs to be prepended to the current kill | |
2900 | ;; ring entry and the text after point appended. Also, we | |
2901 | ;; need to use save-excursion to avoid copying the same text | |
2902 | ;; twice to the kill ring in read-only buffers. | |
2903 | (save-excursion | |
2904 | (kill-region (point) (progn (forward-visible-line 0) (point)))) | |
348de80b KG |
2905 | (kill-region (point) (progn (end-of-visible-line) (point)))) |
2906 | ((< arg 0) | |
6c770e38 LT |
2907 | (save-excursion |
2908 | (kill-region (point) (progn (end-of-visible-line) (point)))) | |
2909 | (kill-region (point) | |
2910 | (progn (forward-visible-line (1+ arg)) | |
2911 | (unless (bobp) (backward-char)) | |
2912 | (point)))) | |
348de80b | 2913 | (t |
6c770e38 LT |
2914 | (save-excursion |
2915 | (kill-region (point) (progn (forward-visible-line 0) (point)))) | |
2916 | (kill-region (point) | |
2917 | (progn (forward-visible-line arg) (point)))))) | |
12a93712 | 2918 | |
93be67de KH |
2919 | (defun forward-visible-line (arg) |
2920 | "Move forward by ARG lines, ignoring currently invisible newlines only. | |
2921 | If ARG is negative, move backward -ARG lines. | |
2922 | If ARG is zero, move to the beginning of the current line." | |
2923 | (condition-case nil | |
2924 | (if (> arg 0) | |
12a93712 RS |
2925 | (progn |
2926 | (while (> arg 0) | |
93be67de | 2927 | (or (zerop (forward-line 1)) |
12a93712 RS |
2928 | (signal 'end-of-buffer nil)) |
2929 | ;; If the newline we just skipped is invisible, | |
2930 | ;; don't count it. | |
2931 | (let ((prop | |
2932 | (get-char-property (1- (point)) 'invisible))) | |
2933 | (if (if (eq buffer-invisibility-spec t) | |
2934 | prop | |
2935 | (or (memq prop buffer-invisibility-spec) | |
2936 | (assq prop buffer-invisibility-spec))) | |
2937 | (setq arg (1+ arg)))) | |
2938 | (setq arg (1- arg))) | |
2939 | ;; If invisible text follows, and it is a number of complete lines, | |
2940 | ;; skip it. | |
2941 | (let ((opoint (point))) | |
2942 | (while (and (not (eobp)) | |
2943 | (let ((prop | |
2944 | (get-char-property (point) 'invisible))) | |
2945 | (if (eq buffer-invisibility-spec t) | |
2946 | prop | |
2947 | (or (memq prop buffer-invisibility-spec) | |
2948 | (assq prop buffer-invisibility-spec))))) | |
2949 | (goto-char | |
2950 | (if (get-text-property (point) 'invisible) | |
2951 | (or (next-single-property-change (point) 'invisible) | |
2952 | (point-max)) | |
2953 | (next-overlay-change (point))))) | |
2954 | (unless (bolp) | |
2955 | (goto-char opoint)))) | |
93be67de | 2956 | (let ((first t)) |
f5fd8833 JB |
2957 | (while (or first (<= arg 0)) |
2958 | (if first | |
93be67de KH |
2959 | (beginning-of-line) |
2960 | (or (zerop (forward-line -1)) | |
2961 | (signal 'beginning-of-buffer nil))) | |
12a93712 RS |
2962 | ;; If the newline we just moved to is invisible, |
2963 | ;; don't count it. | |
2964 | (unless (bobp) | |
2965 | (let ((prop | |
2966 | (get-char-property (1- (point)) 'invisible))) | |
f5fd8833 JB |
2967 | (unless (if (eq buffer-invisibility-spec t) |
2968 | prop | |
2969 | (or (memq prop buffer-invisibility-spec) | |
2970 | (assq prop buffer-invisibility-spec))) | |
2971 | (setq arg (1+ arg))))) | |
2972 | (setq first nil)) | |
12a93712 RS |
2973 | ;; If invisible text follows, and it is a number of complete lines, |
2974 | ;; skip it. | |
2975 | (let ((opoint (point))) | |
93be67de KH |
2976 | (while (and (not (bobp)) |
2977 | (let ((prop | |
2978 | (get-char-property (1- (point)) 'invisible))) | |
2979 | (if (eq buffer-invisibility-spec t) | |
2980 | prop | |
2981 | (or (memq prop buffer-invisibility-spec) | |
2982 | (assq prop buffer-invisibility-spec))))) | |
2983 | (goto-char | |
2984 | (if (get-text-property (1- (point)) 'invisible) | |
2985 | (or (previous-single-property-change (point) 'invisible) | |
2986 | (point-min)) | |
12a93712 RS |
2987 | (previous-overlay-change (point))))) |
2988 | (unless (bolp) | |
2989 | (goto-char opoint))))) | |
93be67de KH |
2990 | ((beginning-of-buffer end-of-buffer) |
2991 | nil))) | |
70e14c01 | 2992 | |
93be67de KH |
2993 | (defun end-of-visible-line () |
2994 | "Move to end of current visible line." | |
2995 | (end-of-line) | |
2996 | ;; If the following character is currently invisible, | |
2997 | ;; skip all characters with that same `invisible' property value, | |
2998 | ;; then find the next newline. | |
2999 | (while (and (not (eobp)) | |
5560dc5d RS |
3000 | (save-excursion |
3001 | (skip-chars-forward "^\n") | |
3002 | (let ((prop | |
3003 | (get-char-property (point) 'invisible))) | |
3004 | (if (eq buffer-invisibility-spec t) | |
3005 | prop | |
3006 | (or (memq prop buffer-invisibility-spec) | |
3007 | (assq prop buffer-invisibility-spec)))))) | |
3008 | (skip-chars-forward "^\n") | |
93be67de KH |
3009 | (if (get-text-property (point) 'invisible) |
3010 | (goto-char (next-single-property-change (point) 'invisible)) | |
3011 | (goto-char (next-overlay-change (point)))) | |
3012 | (end-of-line))) | |
2d88b556 | 3013 | \f |
2076c87c JB |
3014 | (defun insert-buffer (buffer) |
3015 | "Insert after point the contents of BUFFER. | |
3016 | Puts mark after the inserted text. | |
6cb6e7a2 GM |
3017 | BUFFER may be a buffer or a buffer name. |
3018 | ||
3019 | This function is meant for the user to run interactively. | |
1e96c007 | 3020 | Don't call it from programs: use `insert-buffer-substring' instead!" |
c3d4f949 | 3021 | (interactive |
a3e7c391 FP |
3022 | (list |
3023 | (progn | |
3024 | (barf-if-buffer-read-only) | |
3025 | (read-buffer "Insert buffer: " | |
3026 | (if (eq (selected-window) (next-window (selected-window))) | |
3027 | (other-buffer (current-buffer)) | |
3028 | (window-buffer (next-window (selected-window)))) | |
3029 | t)))) | |
1e96c007 SM |
3030 | (push-mark |
3031 | (save-excursion | |
3032 | (insert-buffer-substring (get-buffer buffer)) | |
3033 | (point))) | |
1537a263 | 3034 | nil) |
2076c87c JB |
3035 | |
3036 | (defun append-to-buffer (buffer start end) | |
3037 | "Append to specified buffer the text of the region. | |
3038 | It is inserted into that buffer before its point. | |
3039 | ||
3040 | When calling from a program, give three arguments: | |
3041 | BUFFER (or buffer name), START and END. | |
3042 | START and END specify the portion of the current buffer to be copied." | |
70e14c01 | 3043 | (interactive |
5d771766 | 3044 | (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t)) |
23efee2c | 3045 | (region-beginning) (region-end))) |
2076c87c JB |
3046 | (let ((oldbuf (current-buffer))) |
3047 | (save-excursion | |
c069a480 GM |
3048 | (let* ((append-to (get-buffer-create buffer)) |
3049 | (windows (get-buffer-window-list append-to t t)) | |
3050 | point) | |
3051 | (set-buffer append-to) | |
3052 | (setq point (point)) | |
3053 | (barf-if-buffer-read-only) | |
3054 | (insert-buffer-substring oldbuf start end) | |
3055 | (dolist (window windows) | |
3056 | (when (= (window-point window) point) | |
3057 | (set-window-point window (point)))))))) | |
2076c87c JB |
3058 | |
3059 | (defun prepend-to-buffer (buffer start end) | |
3060 | "Prepend to specified buffer the text of the region. | |
3061 | It is inserted into that buffer after its point. | |
3062 | ||
3063 | When calling from a program, give three arguments: | |
3064 | BUFFER (or buffer name), START and END. | |
3065 | START and END specify the portion of the current buffer to be copied." | |
3066 | (interactive "BPrepend to buffer: \nr") | |
3067 | (let ((oldbuf (current-buffer))) | |
3068 | (save-excursion | |
3069 | (set-buffer (get-buffer-create buffer)) | |
74399eac | 3070 | (barf-if-buffer-read-only) |
2076c87c JB |
3071 | (save-excursion |
3072 | (insert-buffer-substring oldbuf start end))))) | |
3073 | ||
3074 | (defun copy-to-buffer (buffer start end) | |
3075 | "Copy to specified buffer the text of the region. | |
3076 | It is inserted into that buffer, replacing existing text there. | |
3077 | ||
3078 | When calling from a program, give three arguments: | |
3079 | BUFFER (or buffer name), START and END. | |
3080 | START and END specify the portion of the current buffer to be copied." | |
3081 | (interactive "BCopy to buffer: \nr") | |
3082 | (let ((oldbuf (current-buffer))) | |
1b5fd09e | 3083 | (with-current-buffer (get-buffer-create buffer) |
74399eac | 3084 | (barf-if-buffer-read-only) |
2076c87c JB |
3085 | (erase-buffer) |
3086 | (save-excursion | |
3087 | (insert-buffer-substring oldbuf start end))))) | |
2d88b556 | 3088 | \f |
62d1c1fc RM |
3089 | (put 'mark-inactive 'error-conditions '(mark-inactive error)) |
3090 | (put 'mark-inactive 'error-message "The mark is not active now") | |
3091 | ||
0251bafb RS |
3092 | (defvar activate-mark-hook nil |
3093 | "Hook run when the mark becomes active. | |
3094 | It is also run at the end of a command, if the mark is active and | |
3095 | it is possible that the region may have changed") | |
3096 | ||
3097 | (defvar deactivate-mark-hook nil | |
3098 | "Hook run when the mark becomes inactive.") | |
3099 | ||
af39530e | 3100 | (defun mark (&optional force) |
f00239cf RS |
3101 | "Return this buffer's mark value as integer, or nil if never set. |
3102 | ||
3103 | In Transient Mark mode, this function signals an error if | |
3104 | the mark is not active. However, if `mark-even-if-inactive' is non-nil, | |
3105 | or the argument FORCE is non-nil, it disregards whether the mark | |
3106 | is active, and returns an integer or nil in the usual way. | |
af39530e | 3107 | |
2076c87c JB |
3108 | If you are using this in an editing command, you are most likely making |
3109 | a mistake; see the documentation of `set-mark'." | |
0e3a7b14 | 3110 | (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive) |
af39530e | 3111 | (marker-position (mark-marker)) |
62d1c1fc | 3112 | (signal 'mark-inactive nil))) |
2076c87c | 3113 | |
19d35374 RM |
3114 | ;; Many places set mark-active directly, and several of them failed to also |
3115 | ;; run deactivate-mark-hook. This shorthand should simplify. | |
3116 | (defsubst deactivate-mark () | |
3117 | "Deactivate the mark by setting `mark-active' to nil. | |
fcadf1c7 | 3118 | \(That makes a difference only in Transient Mark mode.) |
19d35374 | 3119 | Also runs the hook `deactivate-mark-hook'." |
868c2f49 KS |
3120 | (cond |
3121 | ((eq transient-mark-mode 'lambda) | |
3122 | (setq transient-mark-mode nil)) | |
3123 | (transient-mark-mode | |
3124 | (setq mark-active nil) | |
3125 | (run-hooks 'deactivate-mark-hook)))) | |
19d35374 | 3126 | |
2076c87c JB |
3127 | (defun set-mark (pos) |
3128 | "Set this buffer's mark to POS. Don't use this function! | |
3129 | That is to say, don't use this function unless you want | |
3130 | the user to see that the mark has moved, and you want the previous | |
3131 | mark position to be lost. | |
3132 | ||
3133 | Normally, when a new mark is set, the old one should go on the stack. | |
f59006cb | 3134 | This is why most applications should use `push-mark', not `set-mark'. |
2076c87c | 3135 | |
ff1fbe3e | 3136 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
3137 | purposes. The mark saves a location for the user's convenience. |
3138 | Most editing commands should not alter the mark. | |
3139 | To remember a location for internal use in the Lisp program, | |
3140 | store it in a Lisp variable. Example: | |
3141 | ||
3142 | (let ((beg (point))) (forward-line 1) (delete-region beg (point)))." | |
3143 | ||
fcadf1c7 RS |
3144 | (if pos |
3145 | (progn | |
3146 | (setq mark-active t) | |
3147 | (run-hooks 'activate-mark-hook) | |
3148 | (set-marker (mark-marker) pos (current-buffer))) | |
24c22852 RS |
3149 | ;; Normally we never clear mark-active except in Transient Mark mode. |
3150 | ;; But when we actually clear out the mark value too, | |
3151 | ;; we must clear mark-active in any mode. | |
3152 | (setq mark-active nil) | |
3153 | (run-hooks 'deactivate-mark-hook) | |
3154 | (set-marker (mark-marker) nil))) | |
2076c87c JB |
3155 | |
3156 | (defvar mark-ring nil | |
e55e2267 | 3157 | "The list of former marks of the current buffer, most recent first.") |
2076c87c | 3158 | (make-variable-buffer-local 'mark-ring) |
e55e2267 | 3159 | (put 'mark-ring 'permanent-local t) |
2076c87c | 3160 | |
69c1dd37 RS |
3161 | (defcustom mark-ring-max 16 |
3162 | "*Maximum size of mark ring. Start discarding off end if gets this big." | |
3163 | :type 'integer | |
3164 | :group 'editing-basics) | |
2076c87c | 3165 | |
dc029f0b RM |
3166 | (defvar global-mark-ring nil |
3167 | "The list of saved global marks, most recent first.") | |
3168 | ||
69c1dd37 | 3169 | (defcustom global-mark-ring-max 16 |
dc029f0b | 3170 | "*Maximum size of global mark ring. \ |
69c1dd37 RS |
3171 | Start discarding off end if gets this big." |
3172 | :type 'integer | |
3173 | :group 'editing-basics) | |
dc029f0b | 3174 | |
868c2f49 KS |
3175 | (defun pop-to-mark-command () |
3176 | "Jump to mark, and pop a new position for mark off the ring | |
3177 | \(does not affect global mark ring\)." | |
3178 | (interactive) | |
3179 | (if (null (mark t)) | |
3180 | (error "No mark set in this buffer") | |
868c2f49 KS |
3181 | (goto-char (mark t)) |
3182 | (pop-mark))) | |
3183 | ||
d00ffe21 | 3184 | (defun push-mark-command (arg &optional nomsg) |
868c2f49 | 3185 | "Set mark at where point is. |
d00ffe21 KS |
3186 | If no prefix arg and mark is already set there, just activate it. |
3187 | Display `Mark set' unless the optional second arg NOMSG is non-nil." | |
868c2f49 KS |
3188 | (interactive "P") |
3189 | (let ((mark (marker-position (mark-marker)))) | |
3190 | (if (or arg (null mark) (/= mark (point))) | |
d00ffe21 | 3191 | (push-mark nil nomsg t) |
868c2f49 | 3192 | (setq mark-active t) |
0251bafb | 3193 | (run-hooks 'activate-mark-hook) |
d00ffe21 KS |
3194 | (unless nomsg |
3195 | (message "Mark activated"))))) | |
868c2f49 | 3196 | |
6a936796 RS |
3197 | (defcustom set-mark-command-repeat-pop nil |
3198 | "*Non-nil means that repeating \\[set-mark-command] after popping will pop. | |
3199 | This means that if you type C-u \\[set-mark-command] \\[set-mark-command] | |
3200 | will pop twice." | |
3201 | :type 'boolean | |
3202 | :group 'editing) | |
3203 | ||
2076c87c JB |
3204 | (defun set-mark-command (arg) |
3205 | "Set mark at where point is, or jump to mark. | |
66ef2df9 KS |
3206 | With no prefix argument, set mark, and push old mark position on local |
3207 | mark ring; also push mark on global mark ring if last mark was set in | |
3208 | another buffer. Immediately repeating the command activates | |
3209 | `transient-mark-mode' temporarily. | |
3210 | ||
3211 | With argument, e.g. \\[universal-argument] \\[set-mark-command], \ | |
3212 | jump to mark, and pop a new position | |
3213 | for mark off the local mark ring \(this does not affect the global | |
3214 | mark ring\). Use \\[pop-global-mark] to jump to a mark off the global | |
3215 | mark ring \(see `pop-global-mark'\). | |
18c5df40 | 3216 | |
2ef0a47e RS |
3217 | If `set-mark-command-repeat-pop' is non-nil, repeating |
3218 | the \\[set-mark-command] command with no prefix pops the next position | |
3219 | off the local (or global) mark ring and jumps there. | |
66ef2df9 KS |
3220 | |
3221 | With a double \\[universal-argument] prefix argument, e.g. \\[universal-argument] \ | |
3222 | \\[universal-argument] \\[set-mark-command], unconditionally | |
3223 | set mark where point is. | |
2076c87c | 3224 | |
7cb42362 RS |
3225 | Setting the mark also sets the \"region\", which is the closest |
3226 | equivalent in Emacs to what some editors call the \"selection\". | |
3227 | ||
ff1fbe3e | 3228 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
3229 | purposes. See the documentation of `set-mark' for more information." |
3230 | (interactive "P") | |
868c2f49 KS |
3231 | (if (eq transient-mark-mode 'lambda) |
3232 | (setq transient-mark-mode nil)) | |
3233 | (cond | |
18c5df40 KS |
3234 | ((and (consp arg) (> (prefix-numeric-value arg) 4)) |
3235 | (push-mark-command nil)) | |
868c2f49 | 3236 | ((not (eq this-command 'set-mark-command)) |
1841f9e3 KS |
3237 | (if arg |
3238 | (pop-to-mark-command) | |
3239 | (push-mark-command t))) | |
6a936796 RS |
3240 | ((and set-mark-command-repeat-pop |
3241 | (eq last-command 'pop-to-mark-command)) | |
66ef2df9 KS |
3242 | (setq this-command 'pop-to-mark-command) |
3243 | (pop-to-mark-command)) | |
6a936796 RS |
3244 | ((and set-mark-command-repeat-pop |
3245 | (eq last-command 'pop-global-mark) | |
3246 | (not arg)) | |
66ef2df9 KS |
3247 | (setq this-command 'pop-global-mark) |
3248 | (pop-global-mark)) | |
868c2f49 | 3249 | (arg |
1841f9e3 | 3250 | (setq this-command 'pop-to-mark-command) |
868c2f49 KS |
3251 | (pop-to-mark-command)) |
3252 | ((and (eq last-command 'set-mark-command) | |
3253 | mark-active (null transient-mark-mode)) | |
3254 | (setq transient-mark-mode 'lambda) | |
3255 | (message "Transient-mark-mode temporarily enabled")) | |
3256 | (t | |
3257 | (push-mark-command nil)))) | |
2076c87c | 3258 | |
fd0f4056 | 3259 | (defun push-mark (&optional location nomsg activate) |
2076c87c | 3260 | "Set mark at LOCATION (point, by default) and push old mark on mark ring. |
f1382a3d RM |
3261 | If the last global mark pushed was not in the current buffer, |
3262 | also push LOCATION on the global mark ring. | |
fd0f4056 | 3263 | Display `Mark set' unless the optional second arg NOMSG is non-nil. |
8cdc660f | 3264 | In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil. |
2076c87c | 3265 | |
ff1fbe3e | 3266 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
9a1277dd RS |
3267 | purposes. See the documentation of `set-mark' for more information. |
3268 | ||
3269 | In Transient Mark mode, this does not activate the mark." | |
1a0d0b6a | 3270 | (unless (null (mark t)) |
2076c87c | 3271 | (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring)) |
1a0d0b6a JPW |
3272 | (when (> (length mark-ring) mark-ring-max) |
3273 | (move-marker (car (nthcdr mark-ring-max mark-ring)) nil) | |
3274 | (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))) | |
9a1277dd | 3275 | (set-marker (mark-marker) (or location (point)) (current-buffer)) |
dc029f0b | 3276 | ;; Now push the mark on the global mark ring. |
f1382a3d | 3277 | (if (and global-mark-ring |
e08d3f7c | 3278 | (eq (marker-buffer (car global-mark-ring)) (current-buffer))) |
f1382a3d RM |
3279 | ;; The last global mark pushed was in this same buffer. |
3280 | ;; Don't push another one. | |
3281 | nil | |
3282 | (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring)) | |
1a0d0b6a JPW |
3283 | (when (> (length global-mark-ring) global-mark-ring-max) |
3284 | (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil) | |
3285 | (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))) | |
efcf38c7 | 3286 | (or nomsg executing-kbd-macro (> (minibuffer-depth) 0) |
2076c87c | 3287 | (message "Mark set")) |
8cdc660f RS |
3288 | (if (or activate (not transient-mark-mode)) |
3289 | (set-mark (mark t))) | |
2076c87c JB |
3290 | nil) |
3291 | ||
3292 | (defun pop-mark () | |
3293 | "Pop off mark ring into the buffer's actual mark. | |
3294 | Does not set point. Does nothing if mark ring is empty." | |
1a0d0b6a JPW |
3295 | (when mark-ring |
3296 | (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker))))) | |
3297 | (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer)) | |
1a0d0b6a JPW |
3298 | (move-marker (car mark-ring) nil) |
3299 | (if (null (mark t)) (ding)) | |
0137bae6 JL |
3300 | (setq mark-ring (cdr mark-ring))) |
3301 | (deactivate-mark)) | |
2076c87c | 3302 | |
e462e42f | 3303 | (defalias 'exchange-dot-and-mark 'exchange-point-and-mark) |
868c2f49 | 3304 | (defun exchange-point-and-mark (&optional arg) |
af39530e RS |
3305 | "Put the mark where point is now, and point where the mark is now. |
3306 | This command works even when the mark is not active, | |
868c2f49 KS |
3307 | and it reactivates the mark. |
3308 | With prefix arg, `transient-mark-mode' is enabled temporarily." | |
3309 | (interactive "P") | |
3310 | (if arg | |
f1180544 | 3311 | (if mark-active |
868c2f49 KS |
3312 | (if (null transient-mark-mode) |
3313 | (setq transient-mark-mode 'lambda)) | |
3314 | (setq arg nil))) | |
3315 | (unless arg | |
3316 | (let ((omark (mark t))) | |
3317 | (if (null omark) | |
3318 | (error "No mark set in this buffer")) | |
3319 | (set-mark (point)) | |
3320 | (goto-char omark) | |
3321 | nil))) | |
e23c2c21 | 3322 | |
6710df48 | 3323 | (define-minor-mode transient-mark-mode |
e23c2c21 | 3324 | "Toggle Transient Mark mode. |
b411b5fa | 3325 | With arg, turn Transient Mark mode on if arg is positive, off otherwise. |
e23c2c21 | 3326 | |
5dd1220d RS |
3327 | In Transient Mark mode, when the mark is active, the region is highlighted. |
3328 | Changing the buffer \"deactivates\" the mark. | |
3329 | So do certain other operations that set the mark | |
3330 | but whose main purpose is something else--for example, | |
cfa70244 EZ |
3331 | incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]. |
3332 | ||
8e843bc4 EZ |
3333 | You can also deactivate the mark by typing \\[keyboard-quit] or |
3334 | \\[keyboard-escape-quit]. | |
1465c66b | 3335 | |
cfa70244 EZ |
3336 | Many commands change their behavior when Transient Mark mode is in effect |
3337 | and the mark is active, by acting on the region instead of their usual | |
4c5f7215 | 3338 | default part of the buffer's text. Examples of such commands include |
705a5933 JL |
3339 | \\[comment-dwim], \\[flush-lines], \\[keep-lines], \ |
3340 | \\[query-replace], \\[query-replace-regexp], \\[ispell], and \\[undo]. | |
3341 | Invoke \\[apropos-documentation] and type \"transient\" or | |
3342 | \"mark.*active\" at the prompt, to see the documentation of | |
3343 | commands which are sensitive to the Transient Mark mode." | |
dfd8ca69 | 3344 | :global t :group 'editing-basics) |
dc029f0b | 3345 | |
d0c4882d RS |
3346 | (defvar widen-automatically t |
3347 | "Non-nil means it is ok for commands to call `widen' when they want to. | |
3348 | Some commands will do this in order to go to positions outside | |
3349 | the current accessible part of the buffer. | |
3350 | ||
3351 | If `widen-automatically' is nil, these commands will do something else | |
3352 | as a fallback, and won't change the buffer bounds.") | |
3353 | ||
dc029f0b RM |
3354 | (defun pop-global-mark () |
3355 | "Pop off global mark ring and jump to the top location." | |
3356 | (interactive) | |
52b6d445 RS |
3357 | ;; Pop entries which refer to non-existent buffers. |
3358 | (while (and global-mark-ring (not (marker-buffer (car global-mark-ring)))) | |
3359 | (setq global-mark-ring (cdr global-mark-ring))) | |
dc029f0b RM |
3360 | (or global-mark-ring |
3361 | (error "No global mark set")) | |
3362 | (let* ((marker (car global-mark-ring)) | |
3363 | (buffer (marker-buffer marker)) | |
3364 | (position (marker-position marker))) | |
34c31301 RS |
3365 | (setq global-mark-ring (nconc (cdr global-mark-ring) |
3366 | (list (car global-mark-ring)))) | |
dc029f0b RM |
3367 | (set-buffer buffer) |
3368 | (or (and (>= position (point-min)) | |
3369 | (<= position (point-max))) | |
d0c4882d | 3370 | (if widen-automatically |
60aee8b2 RS |
3371 | (widen) |
3372 | (error "Global mark position is outside accessible part of buffer"))) | |
dc029f0b RM |
3373 | (goto-char position) |
3374 | (switch-to-buffer buffer))) | |
2d88b556 | 3375 | \f |
95791033 | 3376 | (defcustom next-line-add-newlines nil |
69c1dd37 RS |
3377 | "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error." |
3378 | :type 'boolean | |
e1d6e383 | 3379 | :version "21.1" |
69c1dd37 | 3380 | :group 'editing-basics) |
38ebcf29 | 3381 | |
295f6616 | 3382 | (defun next-line (&optional arg try-vscroll) |
2076c87c | 3383 | "Move cursor vertically down ARG lines. |
295f6616 | 3384 | Interactively, vscroll tall lines if `auto-window-vscroll' is enabled. |
2076c87c JB |
3385 | If there is no character in the target line exactly under the current column, |
3386 | the cursor is positioned after the character in that line which spans this | |
3387 | column, or at the end of the line if it is not long enough. | |
38ebcf29 | 3388 | If there is no line in the buffer after this one, behavior depends on the |
1a2c3941 RS |
3389 | value of `next-line-add-newlines'. If non-nil, it inserts a newline character |
3390 | to create a line, and moves the cursor to that line. Otherwise it moves the | |
e47d38f6 | 3391 | cursor to the end of the buffer. |
2076c87c JB |
3392 | |
3393 | The command \\[set-goal-column] can be used to create | |
85969cb1 RS |
3394 | a semipermanent goal column for this command. |
3395 | Then instead of trying to move exactly vertically (or as close as possible), | |
3396 | this command moves to the specified goal column (or as close as possible). | |
3397 | The goal column is stored in the variable `goal-column', which is nil | |
3398 | when there is no goal column. | |
2076c87c JB |
3399 | |
3400 | If you are thinking of using this in a Lisp program, consider | |
3401 | using `forward-line' instead. It is usually easier to use | |
3402 | and more reliable (no dependence on goal column, etc.)." | |
295f6616 | 3403 | (interactive "p\np") |
b82d844f | 3404 | (or arg (setq arg 1)) |
028922cf | 3405 | (if (and next-line-add-newlines (= arg 1)) |
207d7545 GM |
3406 | (if (save-excursion (end-of-line) (eobp)) |
3407 | ;; When adding a newline, don't expand an abbrev. | |
3408 | (let ((abbrev-mode nil)) | |
24886813 | 3409 | (end-of-line) |
15575807 | 3410 | (insert (if use-hard-newlines hard-newline "\n"))) |
295f6616 | 3411 | (line-move arg nil nil try-vscroll)) |
1a2c3941 RS |
3412 | (if (interactive-p) |
3413 | (condition-case nil | |
295f6616 | 3414 | (line-move arg nil nil try-vscroll) |
1a2c3941 | 3415 | ((beginning-of-buffer end-of-buffer) (ding))) |
295f6616 | 3416 | (line-move arg nil nil try-vscroll))) |
2076c87c JB |
3417 | nil) |
3418 | ||
295f6616 | 3419 | (defun previous-line (&optional arg try-vscroll) |
2076c87c | 3420 | "Move cursor vertically up ARG lines. |
295f6616 | 3421 | Interactively, vscroll tall lines if `auto-window-vscroll' is enabled. |
2076c87c JB |
3422 | If there is no character in the target line exactly over the current column, |
3423 | the cursor is positioned after the character in that line which spans this | |
3424 | column, or at the end of the line if it is not long enough. | |
3425 | ||
3426 | The command \\[set-goal-column] can be used to create | |
85969cb1 RS |
3427 | a semipermanent goal column for this command. |
3428 | Then instead of trying to move exactly vertically (or as close as possible), | |
3429 | this command moves to the specified goal column (or as close as possible). | |
3430 | The goal column is stored in the variable `goal-column', which is nil | |
3431 | when there is no goal column. | |
2076c87c JB |
3432 | |
3433 | If you are thinking of using this in a Lisp program, consider using | |
c2e8a012 | 3434 | `forward-line' with a negative argument instead. It is usually easier |
2076c87c | 3435 | to use and more reliable (no dependence on goal column, etc.)." |
295f6616 | 3436 | (interactive "p\np") |
b82d844f | 3437 | (or arg (setq arg 1)) |
1a2c3941 RS |
3438 | (if (interactive-p) |
3439 | (condition-case nil | |
295f6616 | 3440 | (line-move (- arg) nil nil try-vscroll) |
1a2c3941 | 3441 | ((beginning-of-buffer end-of-buffer) (ding))) |
295f6616 | 3442 | (line-move (- arg) nil nil try-vscroll)) |
2076c87c | 3443 | nil) |
eaae8106 | 3444 | |
69c1dd37 | 3445 | (defcustom track-eol nil |
2076c87c JB |
3446 | "*Non-nil means vertical motion starting at end of line keeps to ends of lines. |
3447 | This means moving to the end of each line moved onto. | |
69c1dd37 RS |
3448 | The beginning of a blank line does not count as the end of a line." |
3449 | :type 'boolean | |
3450 | :group 'editing-basics) | |
3451 | ||
3452 | (defcustom goal-column nil | |
3453 | "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil." | |
3454 | :type '(choice integer | |
3455 | (const :tag "None" nil)) | |
3456 | :group 'editing-basics) | |
912c6728 | 3457 | (make-variable-buffer-local 'goal-column) |
2076c87c JB |
3458 | |
3459 | (defvar temporary-goal-column 0 | |
3460 | "Current goal column for vertical motion. | |
3461 | It is the column where point was | |
3462 | at the start of current run of vertical motion commands. | |
c637ae6f | 3463 | When the `track-eol' feature is doing its job, the value is 9999.") |
2076c87c | 3464 | |
bbf41690 | 3465 | (defcustom line-move-ignore-invisible t |
098fc1fb | 3466 | "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines. |
69c1dd37 RS |
3467 | Outline mode sets this." |
3468 | :type 'boolean | |
3469 | :group 'editing-basics) | |
098fc1fb | 3470 | |
bbf41690 | 3471 | (defun line-move-invisible-p (pos) |
af894fc9 RS |
3472 | "Return non-nil if the character after POS is currently invisible." |
3473 | (let ((prop | |
3474 | (get-char-property pos 'invisible))) | |
3475 | (if (eq buffer-invisibility-spec t) | |
3476 | prop | |
3477 | (or (memq prop buffer-invisibility-spec) | |
3478 | (assq prop buffer-invisibility-spec))))) | |
3479 | ||
b704b1f0 KS |
3480 | ;; Returns non-nil if partial move was done. |
3481 | (defun line-move-partial (arg noerror to-end) | |
3482 | (if (< arg 0) | |
3483 | ;; Move backward (up). | |
3484 | ;; If already vscrolled, reduce vscroll | |
3485 | (let ((vs (window-vscroll nil t))) | |
3486 | (when (> vs (frame-char-height)) | |
3487 | (set-window-vscroll nil (- vs (frame-char-height)) t))) | |
3488 | ||
3489 | ;; Move forward (down). | |
e437f99a KS |
3490 | (let* ((lh (window-line-height -1)) |
3491 | (vpos (nth 1 lh)) | |
3492 | (ypos (nth 2 lh)) | |
3493 | (rbot (nth 3 lh)) | |
3494 | ppos py vs) | |
3495 | (when (or (null lh) | |
3496 | (>= rbot (frame-char-height)) | |
3497 | (<= ypos (- (frame-char-height)))) | |
3498 | (unless lh | |
0e7a5039 KS |
3499 | (let ((wend (pos-visible-in-window-p t nil t))) |
3500 | (setq rbot (nth 3 wend) | |
3501 | vpos (nth 5 wend)))) | |
e437f99a KS |
3502 | (cond |
3503 | ;; If last line of window is fully visible, move forward. | |
3504 | ((or (null rbot) (= rbot 0)) | |
3505 | nil) | |
3506 | ;; If cursor is not in the bottom scroll margin, move forward. | |
3507 | ((and (> vpos 0) | |
95f5a37f KS |
3508 | (< (setq py |
3509 | (or (nth 1 (window-line-height)) | |
3510 | (let ((ppos (posn-at-point))) | |
3511 | (cdr (or (posn-actual-col-row ppos) | |
3512 | (posn-col-row ppos)))))) | |
e437f99a KS |
3513 | (min (- (window-text-height) scroll-margin 1) (1- vpos)))) |
3514 | nil) | |
3515 | ;; When already vscrolled, we vscroll some more if we can, | |
3516 | ;; or clear vscroll and move forward at end of tall image. | |
3517 | ((> (setq vs (window-vscroll nil t)) 0) | |
3518 | (when (> rbot 0) | |
3519 | (set-window-vscroll nil (+ vs (min rbot (frame-char-height))) t))) | |
3520 | ;; If cursor just entered the bottom scroll margin, move forward, | |
3521 | ;; but also vscroll one line so redisplay wont recenter. | |
3522 | ((and (> vpos 0) | |
3523 | (= py (min (- (window-text-height) scroll-margin 1) | |
3524 | (1- vpos)))) | |
3525 | (set-window-vscroll nil (frame-char-height) t) | |
3526 | (line-move-1 arg noerror to-end) | |
3527 | t) | |
3528 | ;; If there are lines above the last line, scroll-up one line. | |
3529 | ((> vpos 0) | |
3530 | (scroll-up 1) | |
3531 | t) | |
3532 | ;; Finally, start vscroll. | |
3533 | (t | |
3534 | (set-window-vscroll nil (frame-char-height) t))))))) | |
b704b1f0 KS |
3535 | |
3536 | ||
03ceda9e RS |
3537 | ;; This is like line-move-1 except that it also performs |
3538 | ;; vertical scrolling of tall images if appropriate. | |
3539 | ;; That is not really a clean thing to do, since it mixes | |
3540 | ;; scrolling with cursor motion. But so far we don't have | |
3541 | ;; a cleaner solution to the problem of making C-n do something | |
3542 | ;; useful given a tall image. | |
ed02c1db | 3543 | (defun line-move (arg &optional noerror to-end try-vscroll) |
b704b1f0 KS |
3544 | (unless (and auto-window-vscroll try-vscroll |
3545 | ;; Only vscroll for single line moves | |
3546 | (= (abs arg) 1) | |
3547 | ;; But don't vscroll in a keyboard macro. | |
3548 | (not defining-kbd-macro) | |
3549 | (not executing-kbd-macro) | |
3550 | (line-move-partial arg noerror to-end)) | |
3551 | (set-window-vscroll nil 0 t) | |
16c2f92f KS |
3552 | (line-move-1 arg noerror to-end))) |
3553 | ||
8c745744 RS |
3554 | ;; This is the guts of next-line and previous-line. |
3555 | ;; Arg says how many lines to move. | |
bbf41690 | 3556 | ;; The value is t if we can move the specified number of lines. |
16c2f92f | 3557 | (defun line-move-1 (arg &optional noerror to-end) |
2596511d RS |
3558 | ;; Don't run any point-motion hooks, and disregard intangibility, |
3559 | ;; for intermediate positions. | |
3560 | (let ((inhibit-point-motion-hooks t) | |
3561 | (opoint (point)) | |
fef11f15 | 3562 | (orig-arg arg)) |
2596511d RS |
3563 | (unwind-protect |
3564 | (progn | |
41d22ee0 | 3565 | (if (not (memq last-command '(next-line previous-line))) |
2596511d RS |
3566 | (setq temporary-goal-column |
3567 | (if (and track-eol (eolp) | |
3568 | ;; Don't count beg of empty line as end of line | |
3569 | ;; unless we just did explicit end-of-line. | |
ab9623c2 | 3570 | (or (not (bolp)) (eq last-command 'move-end-of-line))) |
2596511d RS |
3571 | 9999 |
3572 | (current-column)))) | |
bbf41690 | 3573 | |
2596511d RS |
3574 | (if (and (not (integerp selective-display)) |
3575 | (not line-move-ignore-invisible)) | |
3576 | ;; Use just newline characters. | |
e9cd25fe | 3577 | ;; Set ARG to 0 if we move as many lines as requested. |
2596511d RS |
3578 | (or (if (> arg 0) |
3579 | (progn (if (> arg 1) (forward-line (1- arg))) | |
3580 | ;; This way of moving forward ARG lines | |
3581 | ;; verifies that we have a newline after the last one. | |
3582 | ;; It doesn't get confused by intangible text. | |
3583 | (end-of-line) | |
e9cd25fe RS |
3584 | (if (zerop (forward-line 1)) |
3585 | (setq arg 0))) | |
2596511d | 3586 | (and (zerop (forward-line arg)) |
e9cd25fe RS |
3587 | (bolp) |
3588 | (setq arg 0))) | |
bbf41690 RS |
3589 | (unless noerror |
3590 | (signal (if (< arg 0) | |
3591 | 'beginning-of-buffer | |
3592 | 'end-of-buffer) | |
3593 | nil))) | |
2596511d | 3594 | ;; Move by arg lines, but ignore invisible ones. |
fef11f15 | 3595 | (let (done line-end) |
bbf41690 RS |
3596 | (while (and (> arg 0) (not done)) |
3597 | ;; If the following character is currently invisible, | |
3598 | ;; skip all characters with that same `invisible' property value. | |
3599 | (while (and (not (eobp)) (line-move-invisible-p (point))) | |
3600 | (goto-char (next-char-property-change (point)))) | |
fef11f15 CY |
3601 | ;; Move a line. |
3602 | ;; We don't use `end-of-line', since we want to escape | |
3603 | ;; from field boundaries ocurring exactly at point. | |
3604 | (let ((inhibit-field-text-motion t)) | |
3605 | (setq line-end (line-end-position))) | |
3606 | (goto-char (constrain-to-field line-end (point) t t)) | |
3607 | ;; When moving a single line, update the goal-column | |
3608 | ;; if we couldn't move to the end of line due to a | |
3609 | ;; field boundary. Otherwise we'll get stuck at the | |
3610 | ;; original position during the column motion in | |
3611 | ;; line-move-finish. | |
3612 | (and (/= line-end (point)) | |
3613 | (= orig-arg 1) | |
3614 | (setq temporary-goal-column | |
3615 | (max temporary-goal-column (current-column)))) | |
e9ab825f | 3616 | ;; If there's no invisibility here, move over the newline. |
3e43ae87 KS |
3617 | (cond |
3618 | ((eobp) | |
3619 | (if (not noerror) | |
3620 | (signal 'end-of-buffer nil) | |
3621 | (setq done t))) | |
3622 | ((and (> arg 1) ;; Use vertical-motion for last move | |
3623 | (not (integerp selective-display)) | |
3624 | (not (line-move-invisible-p (point)))) | |
3625 | ;; We avoid vertical-motion when possible | |
3626 | ;; because that has to fontify. | |
3627 | (forward-line 1)) | |
3628 | ;; Otherwise move a more sophisticated way. | |
3629 | ((zerop (vertical-motion 1)) | |
3630 | (if (not noerror) | |
3631 | (signal 'end-of-buffer nil) | |
3632 | (setq done t)))) | |
bbf41690 RS |
3633 | (unless done |
3634 | (setq arg (1- arg)))) | |
22c8bff1 | 3635 | ;; The logic of this is the same as the loop above, |
e9ab825f | 3636 | ;; it just goes in the other direction. |
bbf41690 RS |
3637 | (while (and (< arg 0) (not done)) |
3638 | (beginning-of-line) | |
3e43ae87 KS |
3639 | (cond |
3640 | ((bobp) | |
3641 | (if (not noerror) | |
3642 | (signal 'beginning-of-buffer nil) | |
3643 | (setq done t))) | |
3644 | ((and (< arg -1) ;; Use vertical-motion for last move | |
3645 | (not (integerp selective-display)) | |
3646 | (not (line-move-invisible-p (1- (point))))) | |
3647 | (forward-line -1)) | |
3648 | ((zerop (vertical-motion -1)) | |
3649 | (if (not noerror) | |
3650 | (signal 'beginning-of-buffer nil) | |
3651 | (setq done t)))) | |
bbf41690 RS |
3652 | (unless done |
3653 | (setq arg (1+ arg)) | |
3654 | (while (and ;; Don't move over previous invis lines | |
3655 | ;; if our target is the middle of this line. | |
3656 | (or (zerop (or goal-column temporary-goal-column)) | |
3657 | (< arg 0)) | |
3658 | (not (bobp)) (line-move-invisible-p (1- (point)))) | |
3659 | (goto-char (previous-char-property-change (point)))))))) | |
3660 | ;; This is the value the function returns. | |
3661 | (= arg 0)) | |
af894fc9 | 3662 | |
e9cd25fe RS |
3663 | (cond ((> arg 0) |
3664 | ;; If we did not move down as far as desired, | |
3665 | ;; at least go to end of line. | |
3666 | (end-of-line)) | |
3667 | ((< arg 0) | |
f9872a6b JL |
3668 | ;; If we did not move up as far as desired, |
3669 | ;; at least go to beginning of line. | |
e9cd25fe RS |
3670 | (beginning-of-line)) |
3671 | (t | |
20782abb | 3672 | (line-move-finish (or goal-column temporary-goal-column) |
fef11f15 | 3673 | opoint (> orig-arg 0))))))) |
2076c87c | 3674 | |
20782abb | 3675 | (defun line-move-finish (column opoint forward) |
af894fc9 RS |
3676 | (let ((repeat t)) |
3677 | (while repeat | |
3678 | ;; Set REPEAT to t to repeat the whole thing. | |
3679 | (setq repeat nil) | |
3680 | ||
1f980920 | 3681 | (let (new |
af894fc9 | 3682 | (line-beg (save-excursion (beginning-of-line) (point))) |
1f980920 RS |
3683 | (line-end |
3684 | ;; Compute the end of the line | |
20782abb | 3685 | ;; ignoring effectively invisible newlines. |
bbf41690 | 3686 | (save-excursion |
a5b4a6a0 RS |
3687 | ;; Like end-of-line but ignores fields. |
3688 | (skip-chars-forward "^\n") | |
20782abb RS |
3689 | (while (and (not (eobp)) (line-move-invisible-p (point))) |
3690 | (goto-char (next-char-property-change (point))) | |
a5b4a6a0 | 3691 | (skip-chars-forward "^\n")) |
bbf41690 | 3692 | (point)))) |
1f980920 RS |
3693 | |
3694 | ;; Move to the desired column. | |
3695 | (line-move-to-column column) | |
3696 | (setq new (point)) | |
af894fc9 RS |
3697 | |
3698 | ;; Process intangibility within a line. | |
594a1605 CY |
3699 | ;; With inhibit-point-motion-hooks bound to nil, a call to |
3700 | ;; goto-char moves point past intangible text. | |
3701 | ||
3702 | ;; However, inhibit-point-motion-hooks controls both the | |
3703 | ;; intangibility and the point-entered/point-left hooks. The | |
3704 | ;; following hack avoids calling the point-* hooks | |
3705 | ;; unnecessarily. Note that we move *forward* past intangible | |
3706 | ;; text when the initial and final points are the same. | |
d584e29d | 3707 | (goto-char new) |
9036363b CY |
3708 | (let ((inhibit-point-motion-hooks nil)) |
3709 | (goto-char new) | |
3710 | ||
3711 | ;; If intangibility moves us to a different (later) place | |
3712 | ;; in the same line, use that as the destination. | |
3713 | (if (<= (point) line-end) | |
1f980920 | 3714 | (setq new (point)) |
9036363b CY |
3715 | ;; If that position is "too late", |
3716 | ;; try the previous allowable position. | |
3717 | ;; See if it is ok. | |
3718 | (backward-char) | |
3719 | (if (if forward | |
3720 | ;; If going forward, don't accept the previous | |
3721 | ;; allowable position if it is before the target line. | |
3722 | (< line-beg (point)) | |
3723 | ;; If going backward, don't accept the previous | |
3724 | ;; allowable position if it is still after the target line. | |
3725 | (<= (point) line-end)) | |
3726 | (setq new (point)) | |
3727 | ;; As a last resort, use the end of the line. | |
3728 | (setq new line-end)))) | |
af894fc9 RS |
3729 | |
3730 | ;; Now move to the updated destination, processing fields | |
3731 | ;; as well as intangibility. | |
3732 | (goto-char opoint) | |
3733 | (let ((inhibit-point-motion-hooks nil)) | |
3734 | (goto-char | |
fef11f15 | 3735 | (constrain-to-field new opoint t t |
af894fc9 RS |
3736 | 'inhibit-line-move-field-capture))) |
3737 | ||
1f980920 | 3738 | ;; If all this moved us to a different line, |
af894fc9 RS |
3739 | ;; retry everything within that new line. |
3740 | (when (or (< (point) line-beg) (> (point) line-end)) | |
3741 | ;; Repeat the intangibility and field processing. | |
3742 | (setq repeat t)))))) | |
3743 | ||
3744 | (defun line-move-to-column (col) | |
3745 | "Try to find column COL, considering invisibility. | |
3746 | This function works only in certain cases, | |
3747 | because what we really need is for `move-to-column' | |
3748 | and `current-column' to be able to ignore invisible text." | |
a615252b RS |
3749 | (if (zerop col) |
3750 | (beginning-of-line) | |
095f9ae4 | 3751 | (move-to-column col)) |
af894fc9 RS |
3752 | |
3753 | (when (and line-move-ignore-invisible | |
bbf41690 | 3754 | (not (bolp)) (line-move-invisible-p (1- (point)))) |
af894fc9 RS |
3755 | (let ((normal-location (point)) |
3756 | (normal-column (current-column))) | |
3757 | ;; If the following character is currently invisible, | |
3758 | ;; skip all characters with that same `invisible' property value. | |
3759 | (while (and (not (eobp)) | |
bbf41690 | 3760 | (line-move-invisible-p (point))) |
af894fc9 RS |
3761 | (goto-char (next-char-property-change (point)))) |
3762 | ;; Have we advanced to a larger column position? | |
3763 | (if (> (current-column) normal-column) | |
3764 | ;; We have made some progress towards the desired column. | |
3765 | ;; See if we can make any further progress. | |
3766 | (line-move-to-column (+ (current-column) (- col normal-column))) | |
3767 | ;; Otherwise, go to the place we originally found | |
3768 | ;; and move back over invisible text. | |
3769 | ;; that will get us to the same place on the screen | |
3770 | ;; but with a more reasonable buffer position. | |
3771 | (goto-char normal-location) | |
3772 | (let ((line-beg (save-excursion (beginning-of-line) (point)))) | |
bbf41690 | 3773 | (while (and (not (bolp)) (line-move-invisible-p (1- (point)))) |
af894fc9 RS |
3774 | (goto-char (previous-char-property-change (point) line-beg)))))))) |
3775 | ||
bbf41690 | 3776 | (defun move-end-of-line (arg) |
f00239cf RS |
3777 | "Move point to end of current line as displayed. |
3778 | \(If there's an image in the line, this disregards newlines | |
3779 | which are part of the text that the image rests on.) | |
3780 | ||
bbf41690 RS |
3781 | With argument ARG not nil or 1, move forward ARG - 1 lines first. |
3782 | If point reaches the beginning or end of buffer, it stops there. | |
f00239cf | 3783 | To ignore intangibility, bind `inhibit-point-motion-hooks' to t." |
bbf41690 RS |
3784 | (interactive "p") |
3785 | (or arg (setq arg 1)) | |
3786 | (let (done) | |
3787 | (while (not done) | |
3788 | (let ((newpos | |
3789 | (save-excursion | |
3790 | (let ((goal-column 0)) | |
3791 | (and (line-move arg t) | |
3792 | (not (bobp)) | |
3793 | (progn | |
3794 | (while (and (not (bobp)) (line-move-invisible-p (1- (point)))) | |
3795 | (goto-char (previous-char-property-change (point)))) | |
3796 | (backward-char 1))) | |
3797 | (point))))) | |
3798 | (goto-char newpos) | |
3799 | (if (and (> (point) newpos) | |
3800 | (eq (preceding-char) ?\n)) | |
3801 | (backward-char 1) | |
3802 | (if (and (> (point) newpos) (not (eobp)) | |
3803 | (not (eq (following-char) ?\n))) | |
3804 | ;; If we skipped something intangible | |
3805 | ;; and now we're not really at eol, | |
3806 | ;; keep going. | |
3807 | (setq arg 1) | |
3808 | (setq done t))))))) | |
3809 | ||
0cbb497c | 3810 | (defun move-beginning-of-line (arg) |
f00239cf RS |
3811 | "Move point to beginning of current line as displayed. |
3812 | \(If there's an image in the line, this disregards newlines | |
3813 | which are part of the text that the image rests on.) | |
3814 | ||
0cbb497c KS |
3815 | With argument ARG not nil or 1, move forward ARG - 1 lines first. |
3816 | If point reaches the beginning or end of buffer, it stops there. | |
f00239cf | 3817 | To ignore intangibility, bind `inhibit-point-motion-hooks' to t." |
0cbb497c KS |
3818 | (interactive "p") |
3819 | (or arg (setq arg 1)) | |
398c9ffb | 3820 | |
ad47c4a0 RS |
3821 | (let ((orig (point)) |
3822 | start first-vis first-vis-field-value) | |
1fffd65f RS |
3823 | |
3824 | ;; Move by lines, if ARG is not 1 (the default). | |
3825 | (if (/= arg 1) | |
3826 | (line-move (1- arg) t)) | |
3827 | ||
3828 | ;; Move to beginning-of-line, ignoring fields and invisibles. | |
3829 | (skip-chars-backward "^\n") | |
3830 | (while (and (not (bobp)) (line-move-invisible-p (1- (point)))) | |
621a4cc8 | 3831 | (goto-char (previous-char-property-change (point))) |
1fffd65f | 3832 | (skip-chars-backward "^\n")) |
ad47c4a0 RS |
3833 | (setq start (point)) |
3834 | ||
3835 | ;; Now find first visible char in the line | |
3836 | (while (and (not (eobp)) (line-move-invisible-p (point))) | |
3837 | (goto-char (next-char-property-change (point)))) | |
3838 | (setq first-vis (point)) | |
3839 | ||
3840 | ;; See if fields would stop us from reaching FIRST-VIS. | |
3841 | (setq first-vis-field-value | |
3842 | (constrain-to-field first-vis orig (/= arg 1) t nil)) | |
3843 | ||
3844 | (goto-char (if (/= first-vis-field-value first-vis) | |
3845 | ;; If yes, obey them. | |
3846 | first-vis-field-value | |
3847 | ;; Otherwise, move to START with attention to fields. | |
3848 | ;; (It is possible that fields never matter in this case.) | |
3849 | (constrain-to-field (point) orig | |
3850 | (/= arg 1) t nil))))) | |
0cbb497c KS |
3851 | |
3852 | ||
d5ab2033 JB |
3853 | ;;; Many people have said they rarely use this feature, and often type |
3854 | ;;; it by accident. Maybe it shouldn't even be on a key. | |
3855 | (put 'set-goal-column 'disabled t) | |
2076c87c JB |
3856 | |
3857 | (defun set-goal-column (arg) | |
3858 | "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line]. | |
3859 | Those commands will move to this position in the line moved to | |
3860 | rather than trying to keep the same horizontal position. | |
3861 | With a non-nil argument, clears out the goal column | |
912c6728 RS |
3862 | so that \\[next-line] and \\[previous-line] resume vertical motion. |
3863 | The goal column is stored in the variable `goal-column'." | |
2076c87c JB |
3864 | (interactive "P") |
3865 | (if arg | |
3866 | (progn | |
3867 | (setq goal-column nil) | |
3868 | (message "No goal column")) | |
3869 | (setq goal-column (current-column)) | |
8a26c165 DG |
3870 | ;; The older method below can be erroneous if `set-goal-column' is bound |
3871 | ;; to a sequence containing % | |
3872 | ;;(message (substitute-command-keys | |
3873 | ;;"Goal column %d (use \\[set-goal-column] with an arg to unset it)") | |
3874 | ;;goal-column) | |
3875 | (message "%s" | |
63219d53 | 3876 | (concat |
8a26c165 DG |
3877 | (format "Goal column %d " goal-column) |
3878 | (substitute-command-keys | |
3879 | "(use \\[set-goal-column] with an arg to unset it)"))) | |
63219d53 | 3880 | |
8a26c165 | 3881 | ) |
2076c87c | 3882 | nil) |
2d88b556 | 3883 | \f |
7492f5a6 RS |
3884 | |
3885 | (defun scroll-other-window-down (lines) | |
e47d38f6 RS |
3886 | "Scroll the \"other window\" down. |
3887 | For more details, see the documentation for `scroll-other-window'." | |
7492f5a6 RS |
3888 | (interactive "P") |
3889 | (scroll-other-window | |
3890 | ;; Just invert the argument's meaning. | |
3891 | ;; We can do that without knowing which window it will be. | |
3892 | (if (eq lines '-) nil | |
3893 | (if (null lines) '- | |
3894 | (- (prefix-numeric-value lines)))))) | |
3aef9604 RS |
3895 | |
3896 | (defun beginning-of-buffer-other-window (arg) | |
3897 | "Move point to the beginning of the buffer in the other window. | |
3898 | Leave mark at previous position. | |
3899 | With arg N, put point N/10 of the way from the true beginning." | |
3900 | (interactive "P") | |
3901 | (let ((orig-window (selected-window)) | |
3902 | (window (other-window-for-scrolling))) | |
3903 | ;; We use unwind-protect rather than save-window-excursion | |
3904 | ;; because the latter would preserve the things we want to change. | |
3905 | (unwind-protect | |
3906 | (progn | |
3907 | (select-window window) | |
3908 | ;; Set point and mark in that window's buffer. | |
bbf41690 RS |
3909 | (with-no-warnings |
3910 | (beginning-of-buffer arg)) | |
3aef9604 RS |
3911 | ;; Set point accordingly. |
3912 | (recenter '(t))) | |
3913 | (select-window orig-window)))) | |
3914 | ||
3915 | (defun end-of-buffer-other-window (arg) | |
3916 | "Move point to the end of the buffer in the other window. | |
3917 | Leave mark at previous position. | |
3918 | With arg N, put point N/10 of the way from the true end." | |
3919 | (interactive "P") | |
3920 | ;; See beginning-of-buffer-other-window for comments. | |
3921 | (let ((orig-window (selected-window)) | |
3922 | (window (other-window-for-scrolling))) | |
3923 | (unwind-protect | |
3924 | (progn | |
3925 | (select-window window) | |
bbf41690 RS |
3926 | (with-no-warnings |
3927 | (end-of-buffer arg)) | |
3aef9604 RS |
3928 | (recenter '(t))) |
3929 | (select-window orig-window)))) | |
2d88b556 | 3930 | \f |
2076c87c JB |
3931 | (defun transpose-chars (arg) |
3932 | "Interchange characters around point, moving forward one character. | |
3933 | With prefix arg ARG, effect is to take character before point | |
3934 | and drag it forward past ARG other characters (backward if ARG negative). | |
3935 | If no argument and at end of line, the previous two chars are exchanged." | |
3936 | (interactive "*P") | |
3937 | (and (null arg) (eolp) (forward-char -1)) | |
3938 | (transpose-subr 'forward-char (prefix-numeric-value arg))) | |
3939 | ||
3940 | (defun transpose-words (arg) | |
3941 | "Interchange words around point, leaving point at end of them. | |
3942 | With prefix arg ARG, effect is to take word before or around point | |
3943 | and drag it forward past ARG other words (backward if ARG negative). | |
3944 | If ARG is zero, the words around or after point and around or after mark | |
3945 | are interchanged." | |
41d22ee0 | 3946 | ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'. |
2076c87c JB |
3947 | (interactive "*p") |
3948 | (transpose-subr 'forward-word arg)) | |
3949 | ||
3950 | (defun transpose-sexps (arg) | |
3951 | "Like \\[transpose-words] but applies to sexps. | |
3952 | Does not work on a sexp that point is in the middle of | |
3953 | if it is a list or string." | |
3954 | (interactive "*p") | |
41d22ee0 SM |
3955 | (transpose-subr |
3956 | (lambda (arg) | |
3957 | ;; Here we should try to simulate the behavior of | |
3958 | ;; (cons (progn (forward-sexp x) (point)) | |
3959 | ;; (progn (forward-sexp (- x)) (point))) | |
3960 | ;; Except that we don't want to rely on the second forward-sexp | |
3961 | ;; putting us back to where we want to be, since forward-sexp-function | |
3962 | ;; might do funny things like infix-precedence. | |
3963 | (if (if (> arg 0) | |
3964 | (looking-at "\\sw\\|\\s_") | |
3965 | (and (not (bobp)) | |
3966 | (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_")))) | |
3967 | ;; Jumping over a symbol. We might be inside it, mind you. | |
3968 | (progn (funcall (if (> arg 0) | |
3969 | 'skip-syntax-backward 'skip-syntax-forward) | |
3970 | "w_") | |
3971 | (cons (save-excursion (forward-sexp arg) (point)) (point))) | |
3972 | ;; Otherwise, we're between sexps. Take a step back before jumping | |
3973 | ;; to make sure we'll obey the same precedence no matter which direction | |
3974 | ;; we're going. | |
3975 | (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .") | |
3976 | (cons (save-excursion (forward-sexp arg) (point)) | |
3977 | (progn (while (or (forward-comment (if (> arg 0) 1 -1)) | |
3978 | (not (zerop (funcall (if (> arg 0) | |
3979 | 'skip-syntax-forward | |
3980 | 'skip-syntax-backward) | |
3981 | "."))))) | |
3982 | (point))))) | |
3983 | arg 'special)) | |
2076c87c JB |
3984 | |
3985 | (defun transpose-lines (arg) | |
3986 | "Exchange current line and previous line, leaving point after both. | |
3987 | With argument ARG, takes previous line and moves it past ARG lines. | |
3988 | With argument 0, interchanges line point is in with line mark is in." | |
3989 | (interactive "*p") | |
3990 | (transpose-subr (function | |
3991 | (lambda (arg) | |
d3f4ef3f | 3992 | (if (> arg 0) |
2076c87c | 3993 | (progn |
d3f4ef3f AS |
3994 | ;; Move forward over ARG lines, |
3995 | ;; but create newlines if necessary. | |
3996 | (setq arg (forward-line arg)) | |
3997 | (if (/= (preceding-char) ?\n) | |
3998 | (setq arg (1+ arg))) | |
3999 | (if (> arg 0) | |
4000 | (newline arg))) | |
2076c87c JB |
4001 | (forward-line arg)))) |
4002 | arg)) | |
4003 | ||
e1e04350 SM |
4004 | (defun transpose-subr (mover arg &optional special) |
4005 | (let ((aux (if special mover | |
4006 | (lambda (x) | |
4007 | (cons (progn (funcall mover x) (point)) | |
4008 | (progn (funcall mover (- x)) (point)))))) | |
4009 | pos1 pos2) | |
4010 | (cond | |
4011 | ((= arg 0) | |
4012 | (save-excursion | |
4013 | (setq pos1 (funcall aux 1)) | |
4014 | (goto-char (mark)) | |
4015 | (setq pos2 (funcall aux 1)) | |
4016 | (transpose-subr-1 pos1 pos2)) | |
4017 | (exchange-point-and-mark)) | |
4018 | ((> arg 0) | |
4019 | (setq pos1 (funcall aux -1)) | |
4020 | (setq pos2 (funcall aux arg)) | |
4021 | (transpose-subr-1 pos1 pos2) | |
4022 | (goto-char (car pos2))) | |
4023 | (t | |
4024 | (setq pos1 (funcall aux -1)) | |
4025 | (goto-char (car pos1)) | |
4026 | (setq pos2 (funcall aux arg)) | |
4027 | (transpose-subr-1 pos1 pos2))))) | |
4028 | ||
4029 | (defun transpose-subr-1 (pos1 pos2) | |
4030 | (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1)))) | |
4031 | (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2)))) | |
4032 | (when (> (car pos1) (car pos2)) | |
4033 | (let ((swap pos1)) | |
4034 | (setq pos1 pos2 pos2 swap))) | |
4035 | (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose")) | |
dc7d7552 RS |
4036 | (atomic-change-group |
4037 | (let (word2) | |
1e96c007 SM |
4038 | ;; FIXME: We first delete the two pieces of text, so markers that |
4039 | ;; used to point to after the text end up pointing to before it :-( | |
dc7d7552 RS |
4040 | (setq word2 (delete-and-extract-region (car pos2) (cdr pos2))) |
4041 | (goto-char (car pos2)) | |
4042 | (insert (delete-and-extract-region (car pos1) (cdr pos1))) | |
4043 | (goto-char (car pos1)) | |
4044 | (insert word2)))) | |
2d88b556 | 4045 | \f |
b82d844f | 4046 | (defun backward-word (&optional arg) |
b7e91b0c | 4047 | "Move backward until encountering the beginning of a word. |
20ecc110 | 4048 | With argument, do this that many times." |
9e50756b | 4049 | (interactive "p") |
b82d844f | 4050 | (forward-word (- (or arg 1)))) |
2076c87c | 4051 | |
a1a801de | 4052 | (defun mark-word (&optional arg allow-extend) |
705a5933 JL |
4053 | "Set mark ARG words away from point. |
4054 | The place mark goes is the same place \\[forward-word] would | |
4055 | move to with the same argument. | |
a1a801de | 4056 | Interactively, if this command is repeated |
771069f8 | 4057 | or (in Transient Mark mode) if the mark is active, |
705a5933 | 4058 | it marks the next ARG words after the ones already marked." |
a1a801de RS |
4059 | (interactive "P\np") |
4060 | (cond ((and allow-extend | |
4061 | (or (and (eq last-command this-command) (mark t)) | |
4062 | (and transient-mark-mode mark-active))) | |
705a5933 JL |
4063 | (setq arg (if arg (prefix-numeric-value arg) |
4064 | (if (< (mark) (point)) -1 1))) | |
cad113ae KG |
4065 | (set-mark |
4066 | (save-excursion | |
4067 | (goto-char (mark)) | |
4068 | (forward-word arg) | |
4069 | (point)))) | |
4070 | (t | |
4071 | (push-mark | |
4072 | (save-excursion | |
705a5933 | 4073 | (forward-word (prefix-numeric-value arg)) |
cad113ae KG |
4074 | (point)) |
4075 | nil t)))) | |
2076c87c JB |
4076 | |
4077 | (defun kill-word (arg) | |
4078 | "Kill characters forward until encountering the end of a word. | |
4079 | With argument, do this that many times." | |
e761e42c | 4080 | (interactive "p") |
89ee2bf6 | 4081 | (kill-region (point) (progn (forward-word arg) (point)))) |
2076c87c JB |
4082 | |
4083 | (defun backward-kill-word (arg) | |
4084 | "Kill characters backward until encountering the end of a word. | |
4085 | With argument, do this that many times." | |
e761e42c | 4086 | (interactive "p") |
2076c87c | 4087 | (kill-word (- arg))) |
d7c64071 | 4088 | |
0f7df535 RS |
4089 | (defun current-word (&optional strict really-word) |
4090 | "Return the symbol or word that point is on (or a nearby one) as a string. | |
4091 | The return value includes no text properties. | |
1e8c5ac4 | 4092 | If optional arg STRICT is non-nil, return nil unless point is within |
0fa19a57 RS |
4093 | or adjacent to a symbol or word. In all cases the value can be nil |
4094 | if there is no word nearby. | |
0f7df535 RS |
4095 | The function, belying its name, normally finds a symbol. |
4096 | If optional arg REALLY-WORD is non-nil, it finds just a word." | |
d7c64071 | 4097 | (save-excursion |
0f7df535 | 4098 | (let* ((oldpoint (point)) (start (point)) (end (point)) |
81d17173 | 4099 | (syntaxes (if really-word "w" "w_")) |
0f7df535 RS |
4100 | (not-syntaxes (concat "^" syntaxes))) |
4101 | (skip-syntax-backward syntaxes) (setq start (point)) | |
d7c64071 | 4102 | (goto-char oldpoint) |
0f7df535 RS |
4103 | (skip-syntax-forward syntaxes) (setq end (point)) |
4104 | (when (and (eq start oldpoint) (eq end oldpoint) | |
4105 | ;; Point is neither within nor adjacent to a word. | |
4106 | (not strict)) | |
4107 | ;; Look for preceding word in same line. | |
4108 | (skip-syntax-backward not-syntaxes | |
4109 | (save-excursion (beginning-of-line) | |
4110 | (point))) | |
4111 | (if (bolp) | |
4112 | ;; No preceding word in same line. | |
4113 | ;; Look for following word in same line. | |
4114 | (progn | |
4115 | (skip-syntax-forward not-syntaxes | |
4116 | (save-excursion (end-of-line) | |
4117 | (point))) | |
4118 | (setq start (point)) | |
4119 | (skip-syntax-forward syntaxes) | |
4120 | (setq end (point))) | |
4121 | (setq end (point)) | |
4122 | (skip-syntax-backward syntaxes) | |
4123 | (setq start (point)))) | |
4124 | ;; If we found something nonempty, return it as a string. | |
4125 | (unless (= start end) | |
020db25f | 4126 | (buffer-substring-no-properties start end))))) |
2d88b556 | 4127 | \f |
69c1dd37 | 4128 | (defcustom fill-prefix nil |
e1e04350 | 4129 | "*String for filling to insert at front of new line, or nil for none." |
69c1dd37 RS |
4130 | :type '(choice (const :tag "None" nil) |
4131 | string) | |
4132 | :group 'fill) | |
2076c87c | 4133 | (make-variable-buffer-local 'fill-prefix) |
631c8020 | 4134 | ;;;###autoload(put 'fill-prefix 'safe-local-variable 'string-or-null-p) |
2076c87c | 4135 | |
69c1dd37 RS |
4136 | (defcustom auto-fill-inhibit-regexp nil |
4137 | "*Regexp to match lines which should not be auto-filled." | |
4138 | :type '(choice (const :tag "None" nil) | |
4139 | regexp) | |
4140 | :group 'fill) | |
2076c87c | 4141 | |
58dd38f1 | 4142 | (defvar comment-line-break-function 'comment-indent-new-line |
b3ac9fa9 RS |
4143 | "*Mode-specific function which line breaks and continues a comment. |
4144 | ||
4145 | This function is only called during auto-filling of a comment section. | |
4146 | The function should take a single optional argument, which is a flag | |
cc4cb0f5 | 4147 | indicating whether it should use soft newlines.") |
b3ac9fa9 | 4148 | |
dbe524b6 | 4149 | ;; This function is used as the auto-fill-function of a buffer |
e2504204 KH |
4150 | ;; when Auto-Fill mode is enabled. |
4151 | ;; It returns t if it really did any work. | |
dbe524b6 RS |
4152 | ;; (Actually some major modes use a different auto-fill function, |
4153 | ;; but this one is the default one.) | |
2076c87c | 4154 | (defun do-auto-fill () |
621a3f62 | 4155 | (let (fc justify give-up |
a0170800 | 4156 | (fill-prefix fill-prefix)) |
c18465c4 | 4157 | (if (or (not (setq justify (current-justification))) |
8f066a20 RS |
4158 | (null (setq fc (current-fill-column))) |
4159 | (and (eq justify 'left) | |
4160 | (<= (current-column) fc)) | |
621a3f62 SM |
4161 | (and auto-fill-inhibit-regexp |
4162 | (save-excursion (beginning-of-line) | |
eed5698b RS |
4163 | (looking-at auto-fill-inhibit-regexp)))) |
4164 | nil ;; Auto-filling not required | |
3db1e3b5 BG |
4165 | (if (memq justify '(full center right)) |
4166 | (save-excursion (unjustify-current-line))) | |
a0170800 RS |
4167 | |
4168 | ;; Choose a fill-prefix automatically. | |
e1e04350 SM |
4169 | (when (and adaptive-fill-mode |
4170 | (or (null fill-prefix) (string= fill-prefix ""))) | |
4171 | (let ((prefix | |
4172 | (fill-context-prefix | |
4173 | (save-excursion (backward-paragraph 1) (point)) | |
4174 | (save-excursion (forward-paragraph 1) (point))))) | |
4175 | (and prefix (not (equal prefix "")) | |
4176 | ;; Use auto-indentation rather than a guessed empty prefix. | |
0e53a373 | 4177 | (not (and fill-indent-according-to-mode |
d99f8496 | 4178 | (string-match "\\`[ \t]*\\'" prefix))) |
e1e04350 | 4179 | (setq fill-prefix prefix)))) |
f1180544 | 4180 | |
eed5698b | 4181 | (while (and (not give-up) (> (current-column) fc)) |
e47d38f6 | 4182 | ;; Determine where to split the line. |
db893d00 RS |
4183 | (let* (after-prefix |
4184 | (fill-point | |
621a3f62 SM |
4185 | (save-excursion |
4186 | (beginning-of-line) | |
4187 | (setq after-prefix (point)) | |
4188 | (and fill-prefix | |
4189 | (looking-at (regexp-quote fill-prefix)) | |
4190 | (setq after-prefix (match-end 0))) | |
4191 | (move-to-column (1+ fc)) | |
4192 | (fill-move-to-break-point after-prefix) | |
4193 | (point)))) | |
db893d00 RS |
4194 | |
4195 | ;; See whether the place we found is any good. | |
e47d38f6 RS |
4196 | (if (save-excursion |
4197 | (goto-char fill-point) | |
41d22ee0 SM |
4198 | (or (bolp) |
4199 | ;; There is no use breaking at end of line. | |
4200 | (save-excursion (skip-chars-forward " ") (eolp)) | |
4201 | ;; It is futile to split at the end of the prefix | |
4202 | ;; since we would just insert the prefix again. | |
4203 | (and after-prefix (<= (point) after-prefix)) | |
4204 | ;; Don't split right after a comment starter | |
4205 | ;; since we would just make another comment starter. | |
4206 | (and comment-start-skip | |
4207 | (let ((limit (point))) | |
4208 | (beginning-of-line) | |
4209 | (and (re-search-forward comment-start-skip | |
4210 | limit t) | |
4211 | (eq (point) limit)))))) | |
4212 | ;; No good place to break => stop trying. | |
4213 | (setq give-up t) | |
4214 | ;; Ok, we have a useful place to break the line. Do it. | |
4215 | (let ((prev-column (current-column))) | |
4216 | ;; If point is at the fill-point, do not `save-excursion'. | |
4217 | ;; Otherwise, if a comment prefix or fill-prefix is inserted, | |
4218 | ;; point will end up before it rather than after it. | |
4219 | (if (save-excursion | |
4220 | (skip-chars-backward " \t") | |
4221 | (= (point) fill-point)) | |
4222 | (funcall comment-line-break-function t) | |
4223 | (save-excursion | |
4224 | (goto-char fill-point) | |
4225 | (funcall comment-line-break-function t))) | |
4226 | ;; Now do justification, if required | |
4227 | (if (not (eq justify 'left)) | |
e47d38f6 | 4228 | (save-excursion |
e1e04350 SM |
4229 | (end-of-line 0) |
4230 | (justify-current-line justify nil t))) | |
41d22ee0 SM |
4231 | ;; If making the new line didn't reduce the hpos of |
4232 | ;; the end of the line, then give up now; | |
4233 | ;; trying again will not help. | |
4234 | (if (>= (current-column) prev-column) | |
4235 | (setq give-up t)))))) | |
24ebf92e | 4236 | ;; Justify last line. |
e2504204 | 4237 | (justify-current-line justify t t) |
1e722f9f | 4238 | t))) |
2076c87c | 4239 | |
24ebf92e RS |
4240 | (defvar normal-auto-fill-function 'do-auto-fill |
4241 | "The function to use for `auto-fill-function' if Auto Fill mode is turned on. | |
4242 | Some major modes set this.") | |
4243 | ||
c75505b4 | 4244 | (put 'auto-fill-function :minor-mode-function 'auto-fill-mode) |
d99f8496 SM |
4245 | ;; FIXME: turn into a proper minor mode. |
4246 | ;; Add a global minor mode version of it. | |
d7465b15 | 4247 | (defun auto-fill-mode (&optional arg) |
24ebf92e RS |
4248 | "Toggle Auto Fill mode. |
4249 | With arg, turn Auto Fill mode on if and only if arg is positive. | |
4250 | In Auto Fill mode, inserting a space at a column beyond `current-fill-column' | |
4251 | automatically breaks the line at a previous space. | |
4252 | ||
4253 | The value of `normal-auto-fill-function' specifies the function to use | |
4254 | for `auto-fill-function' when turning Auto Fill mode on." | |
d7465b15 RS |
4255 | (interactive "P") |
4256 | (prog1 (setq auto-fill-function | |
4257 | (if (if (null arg) | |
4258 | (not auto-fill-function) | |
4259 | (> (prefix-numeric-value arg) 0)) | |
24ebf92e | 4260 | normal-auto-fill-function |
d7465b15 | 4261 | nil)) |
7911ecc8 | 4262 | (force-mode-line-update))) |
d7465b15 RS |
4263 | |
4264 | ;; This holds a document string used to document auto-fill-mode. | |
4265 | (defun auto-fill-function () | |
4266 | "Automatically break line at a previous space, in insertion of text." | |
4267 | nil) | |
4268 | ||
4269 | (defun turn-on-auto-fill () | |
4270 | "Unconditionally turn on Auto Fill mode." | |
4271 | (auto-fill-mode 1)) | |
3a99c819 GM |
4272 | |
4273 | (defun turn-off-auto-fill () | |
4274 | "Unconditionally turn off Auto Fill mode." | |
4275 | (auto-fill-mode -1)) | |
4276 | ||
7cbf1dc1 | 4277 | (custom-add-option 'text-mode-hook 'turn-on-auto-fill) |
d7465b15 RS |
4278 | |
4279 | (defun set-fill-column (arg) | |
4cc0ea11 | 4280 | "Set `fill-column' to specified argument. |
923efb99 | 4281 | Use \\[universal-argument] followed by a number to specify a column. |
4cc0ea11 | 4282 | Just \\[universal-argument] as argument means to use the current column." |
d7465b15 | 4283 | (interactive "P") |
f4520363 RS |
4284 | (if (consp arg) |
4285 | (setq arg (current-column))) | |
4286 | (if (not (integerp arg)) | |
4287 | ;; Disallow missing argument; it's probably a typo for C-x C-f. | |
f33321ad | 4288 | (error "set-fill-column requires an explicit argument") |
f4520363 RS |
4289 | (message "Fill column set to %d (was %d)" arg fill-column) |
4290 | (setq fill-column arg))) | |
2d88b556 | 4291 | \f |
2076c87c | 4292 | (defun set-selective-display (arg) |
ff1fbe3e RS |
4293 | "Set `selective-display' to ARG; clear it if no arg. |
4294 | When the value of `selective-display' is a number > 0, | |
4295 | lines whose indentation is >= that value are not displayed. | |
4296 | The variable `selective-display' has a separate value for each buffer." | |
2076c87c JB |
4297 | (interactive "P") |
4298 | (if (eq selective-display t) | |
4299 | (error "selective-display already in use for marked lines")) | |
c88ab9ce ER |
4300 | (let ((current-vpos |
4301 | (save-restriction | |
4302 | (narrow-to-region (point-min) (point)) | |
4303 | (goto-char (window-start)) | |
4304 | (vertical-motion (window-height))))) | |
4305 | (setq selective-display | |
4306 | (and arg (prefix-numeric-value arg))) | |
4307 | (recenter current-vpos)) | |
2076c87c JB |
4308 | (set-window-start (selected-window) (window-start (selected-window))) |
4309 | (princ "selective-display set to " t) | |
4310 | (prin1 selective-display t) | |
4311 | (princ "." t)) | |
4312 | ||
40a64816 RS |
4313 | (defvaralias 'indicate-unused-lines 'indicate-empty-lines) |
4314 | (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines) | |
4315 | ||
b3228584 | 4316 | (defun toggle-truncate-lines (&optional arg) |
0bb64d76 | 4317 | "Toggle whether to fold or truncate long lines on the screen. |
46cdfe8f RS |
4318 | With arg, truncate long lines iff arg is positive. |
4319 | Note that in side-by-side windows, truncation is always enabled." | |
0bb64d76 PA |
4320 | (interactive "P") |
4321 | (setq truncate-lines | |
4322 | (if (null arg) | |
4323 | (not truncate-lines) | |
46cdfe8f RS |
4324 | (> (prefix-numeric-value arg) 0))) |
4325 | (force-mode-line-update) | |
4f017185 RS |
4326 | (unless truncate-lines |
4327 | (let ((buffer (current-buffer))) | |
4328 | (walk-windows (lambda (window) | |
4329 | (if (eq buffer (window-buffer window)) | |
4330 | (set-window-hscroll window 0))) | |
4331 | nil t))) | |
46cdfe8f RS |
4332 | (message "Truncate long lines %s" |
4333 | (if truncate-lines "enabled" "disabled"))) | |
0bb64d76 | 4334 | |
4f8f7f9f | 4335 | (defvar overwrite-mode-textual " Ovwrt" |
b6a22db0 | 4336 | "The string displayed in the mode line when in overwrite mode.") |
4f8f7f9f | 4337 | (defvar overwrite-mode-binary " Bin Ovwrt" |
b6a22db0 JB |
4338 | "The string displayed in the mode line when in binary overwrite mode.") |
4339 | ||
2076c87c JB |
4340 | (defun overwrite-mode (arg) |
4341 | "Toggle overwrite mode. | |
4342 | With arg, turn overwrite mode on iff arg is positive. | |
4343 | In overwrite mode, printing characters typed in replace existing text | |
b6a22db0 JB |
4344 | on a one-for-one basis, rather than pushing it to the right. At the |
4345 | end of a line, such characters extend the line. Before a tab, | |
4346 | such characters insert until the tab is filled in. | |
4347 | \\[quoted-insert] still inserts characters in overwrite mode; this | |
4348 | is supposed to make it easier to insert characters when necessary." | |
4349 | (interactive "P") | |
4350 | (setq overwrite-mode | |
4351 | (if (if (null arg) (not overwrite-mode) | |
4352 | (> (prefix-numeric-value arg) 0)) | |
4353 | 'overwrite-mode-textual)) | |
4354 | (force-mode-line-update)) | |
4355 | ||
4356 | (defun binary-overwrite-mode (arg) | |
4357 | "Toggle binary overwrite mode. | |
4358 | With arg, turn binary overwrite mode on iff arg is positive. | |
4359 | In binary overwrite mode, printing characters typed in replace | |
4360 | existing text. Newlines are not treated specially, so typing at the | |
4361 | end of a line joins the line to the next, with the typed character | |
4362 | between them. Typing before a tab character simply replaces the tab | |
4363 | with the character typed. | |
4364 | \\[quoted-insert] replaces the text at the cursor, just as ordinary | |
4365 | typing characters do. | |
4366 | ||
4367 | Note that binary overwrite mode is not its own minor mode; it is a | |
f33321ad | 4368 | specialization of overwrite mode, entered by setting the |
b6a22db0 | 4369 | `overwrite-mode' variable to `overwrite-mode-binary'." |
2076c87c JB |
4370 | (interactive "P") |
4371 | (setq overwrite-mode | |
b6a22db0 | 4372 | (if (if (null arg) |
a61099dd | 4373 | (not (eq overwrite-mode 'overwrite-mode-binary)) |
b6a22db0 JB |
4374 | (> (prefix-numeric-value arg) 0)) |
4375 | 'overwrite-mode-binary)) | |
4376 | (force-mode-line-update)) | |
eaae8106 | 4377 | |
6710df48 | 4378 | (define-minor-mode line-number-mode |
a61099dd RS |
4379 | "Toggle Line Number mode. |
4380 | With arg, turn Line Number mode on iff arg is positive. | |
4381 | When Line Number mode is enabled, the line number appears | |
8dc9e2ef KH |
4382 | in the mode line. |
4383 | ||
32f2f98e EZ |
4384 | Line numbers do not appear for very large buffers and buffers |
4385 | with very long lines; see variables `line-number-display-limit' | |
4386 | and `line-number-display-limit-width'." | |
efeb22bf | 4387 | :init-value t :global t :group 'mode-line) |
bcad4985 | 4388 | |
6710df48 | 4389 | (define-minor-mode column-number-mode |
bcad4985 KH |
4390 | "Toggle Column Number mode. |
4391 | With arg, turn Column Number mode on iff arg is positive. | |
4392 | When Column Number mode is enabled, the column number appears | |
4393 | in the mode line." | |
efeb22bf | 4394 | :global t :group 'mode-line) |
cf045f9a LK |
4395 | |
4396 | (define-minor-mode size-indication-mode | |
4397 | "Toggle Size Indication mode. | |
4398 | With arg, turn Size Indication mode on iff arg is positive. When | |
4399 | Size Indication mode is enabled, the size of the accessible part | |
4400 | of the buffer appears in the mode line." | |
efeb22bf | 4401 | :global t :group 'mode-line) |
2d88b556 | 4402 | \f |
4b384a8f | 4403 | (defgroup paren-blinking nil |
020db25f | 4404 | "Blinking matching of parens and expressions." |
4b384a8f SM |
4405 | :prefix "blink-matching-" |
4406 | :group 'paren-matching) | |
4407 | ||
69c1dd37 RS |
4408 | (defcustom blink-matching-paren t |
4409 | "*Non-nil means show matching open-paren when close-paren is inserted." | |
4410 | :type 'boolean | |
4b384a8f | 4411 | :group 'paren-blinking) |
2076c87c | 4412 | |
69c1dd37 | 4413 | (defcustom blink-matching-paren-on-screen t |
29fc44dd | 4414 | "*Non-nil means show matching open-paren when it is on screen. |
4b384a8f | 4415 | If nil, means don't show it (but the open-paren can still be shown |
92aa8a33 LT |
4416 | when it is off screen). |
4417 | ||
9cb370a9 | 4418 | This variable has no effect if `blink-matching-paren' is nil. |
a9f72e5f | 4419 | \(In that case, the open-paren is never shown.) |
9cb370a9 | 4420 | It is also ignored if `show-paren-mode' is enabled." |
69c1dd37 | 4421 | :type 'boolean |
4b384a8f | 4422 | :group 'paren-blinking) |
29fc44dd | 4423 | |
4b384a8f | 4424 | (defcustom blink-matching-paren-distance (* 25 1024) |
66d44a36 EZ |
4425 | "*If non-nil, maximum distance to search backwards for matching open-paren. |
4426 | If nil, search stops at the beginning of the accessible portion of the buffer." | |
4427 | :type '(choice (const nil) integer) | |
4b384a8f | 4428 | :group 'paren-blinking) |
2076c87c | 4429 | |
69c1dd37 | 4430 | (defcustom blink-matching-delay 1 |
4b384a8f SM |
4431 | "*Time in seconds to delay after showing a matching paren." |
4432 | :type 'number | |
4433 | :group 'paren-blinking) | |
72dddf8b | 4434 | |
69c1dd37 | 4435 | (defcustom blink-matching-paren-dont-ignore-comments nil |
ab6b3b16 RS |
4436 | "*nil means `blink-matching-paren' ignores comments. |
4437 | More precisely, when looking for the matching parenthesis, | |
4438 | it skips the contents of comments that end before point." | |
69c1dd37 | 4439 | :type 'boolean |
4b384a8f | 4440 | :group 'paren-blinking) |
903b7f65 | 4441 | |
2076c87c JB |
4442 | (defun blink-matching-open () |
4443 | "Move cursor momentarily to the beginning of the sexp before point." | |
4444 | (interactive) | |
c448d316 | 4445 | (when (and (> (point) (point-min)) |
1d0e3fc8 RS |
4446 | blink-matching-paren |
4447 | ;; Verify an even number of quoting characters precede the close. | |
4448 | (= 1 (logand 1 (- (point) | |
4449 | (save-excursion | |
4450 | (forward-char -1) | |
4451 | (skip-syntax-backward "/\\") | |
4452 | (point)))))) | |
4453 | (let* ((oldpos (point)) | |
4454 | blinkpos | |
4455 | message-log-max ; Don't log messages about paren matching. | |
4456 | matching-paren | |
4457 | open-paren-line-string) | |
4458 | (save-excursion | |
4459 | (save-restriction | |
4460 | (if blink-matching-paren-distance | |
4461 | (narrow-to-region (max (point-min) | |
4462 | (- (point) blink-matching-paren-distance)) | |
4463 | oldpos)) | |
4464 | (condition-case () | |
4465 | (let ((parse-sexp-ignore-comments | |
4466 | (and parse-sexp-ignore-comments | |
4467 | (not blink-matching-paren-dont-ignore-comments)))) | |
4468 | (setq blinkpos (scan-sexps oldpos -1))) | |
4469 | (error nil))) | |
4470 | (and blinkpos | |
4471 | ;; Not syntax '$'. | |
4472 | (not (eq (syntax-class (syntax-after blinkpos)) 8)) | |
4473 | (setq matching-paren | |
4474 | (let ((syntax (syntax-after blinkpos))) | |
4475 | (and (consp syntax) | |
4476 | (eq (syntax-class syntax) 4) | |
4477 | (cdr syntax))))) | |
4478 | (cond | |
1c4b7278 SM |
4479 | ((not (or (eq matching-paren (char-before oldpos)) |
4480 | ;; The cdr might hold a new paren-class info rather than | |
4481 | ;; a matching-char info, in which case the two CDRs | |
4482 | ;; should match. | |
9c03d478 | 4483 | (eq matching-paren (cdr (syntax-after (1- oldpos)))))) |
1d0e3fc8 RS |
4484 | (message "Mismatched parentheses")) |
4485 | ((not blinkpos) | |
4486 | (if (not blink-matching-paren-distance) | |
4487 | (message "Unmatched parenthesis"))) | |
4488 | ((pos-visible-in-window-p blinkpos) | |
4489 | ;; Matching open within window, temporarily move to blinkpos but only | |
4490 | ;; if `blink-matching-paren-on-screen' is non-nil. | |
92aa8a33 LT |
4491 | (and blink-matching-paren-on-screen |
4492 | (not show-paren-mode) | |
4493 | (save-excursion | |
4494 | (goto-char blinkpos) | |
4495 | (sit-for blink-matching-delay)))) | |
1d0e3fc8 RS |
4496 | (t |
4497 | (save-excursion | |
4498 | (goto-char blinkpos) | |
4499 | (setq open-paren-line-string | |
4500 | ;; Show what precedes the open in its line, if anything. | |
4501 | (if (save-excursion | |
4502 | (skip-chars-backward " \t") | |
4503 | (not (bolp))) | |
4504 | (buffer-substring (line-beginning-position) | |
4505 | (1+ blinkpos)) | |
4506 | ;; Show what follows the open in its line, if anything. | |
4507 | (if (save-excursion | |
4508 | (forward-char 1) | |
4509 | (skip-chars-forward " \t") | |
4510 | (not (eolp))) | |
4511 | (buffer-substring blinkpos | |
4512 | (line-end-position)) | |
4513 | ;; Otherwise show the previous nonblank line, | |
4514 | ;; if there is one. | |
4515 | (if (save-excursion | |
4516 | (skip-chars-backward "\n \t") | |
4517 | (not (bobp))) | |
4518 | (concat | |
4519 | (buffer-substring (progn | |
267935b9 | 4520 | (skip-chars-backward "\n \t") |
1d0e3fc8 RS |
4521 | (line-beginning-position)) |
4522 | (progn (end-of-line) | |
4523 | (skip-chars-backward " \t") | |
4524 | (point))) | |
4525 | ;; Replace the newline and other whitespace with `...'. | |
4526 | "..." | |
4527 | (buffer-substring blinkpos (1+ blinkpos))) | |
4528 | ;; There is nothing to show except the char itself. | |
4529 | (buffer-substring blinkpos (1+ blinkpos))))))) | |
4530 | (message "Matches %s" | |
4531 | (substring-no-properties open-paren-line-string)))))))) | |
2076c87c JB |
4532 | |
4533 | ;Turned off because it makes dbx bomb out. | |
4534 | (setq blink-paren-function 'blink-matching-open) | |
2d88b556 | 4535 | \f |
9a1277dd RS |
4536 | ;; This executes C-g typed while Emacs is waiting for a command. |
4537 | ;; Quitting out of a program does not go through here; | |
4538 | ;; that happens in the QUIT macro at the C code level. | |
2076c87c | 4539 | (defun keyboard-quit () |
d5dae4e1 | 4540 | "Signal a `quit' condition. |
af39530e RS |
4541 | During execution of Lisp code, this character causes a quit directly. |
4542 | At top-level, as an editor command, this simply beeps." | |
2076c87c | 4543 | (interactive) |
19d35374 | 4544 | (deactivate-mark) |
8a7644e9 KS |
4545 | (if (fboundp 'kmacro-keyboard-quit) |
4546 | (kmacro-keyboard-quit)) | |
f5e13057 | 4547 | (setq defining-kbd-macro nil) |
2076c87c JB |
4548 | (signal 'quit nil)) |
4549 | ||
1c6c6fde RS |
4550 | (defvar buffer-quit-function nil |
4551 | "Function to call to \"quit\" the current buffer, or nil if none. | |
4552 | \\[keyboard-escape-quit] calls this function when its more local actions | |
4553 | \(such as cancelling a prefix argument, minibuffer or region) do not apply.") | |
4554 | ||
c66587fe RS |
4555 | (defun keyboard-escape-quit () |
4556 | "Exit the current \"mode\" (in a generalized sense of the word). | |
4557 | This command can exit an interactive command such as `query-replace', | |
4558 | can clear out a prefix argument or a region, | |
4559 | can get out of the minibuffer or other recursive edit, | |
1c6c6fde RS |
4560 | cancel the use of the current buffer (for special-purpose buffers), |
4561 | or go back to just one window (by deleting all but the selected window)." | |
c66587fe RS |
4562 | (interactive) |
4563 | (cond ((eq last-command 'mode-exited) nil) | |
4564 | ((> (minibuffer-depth) 0) | |
4565 | (abort-recursive-edit)) | |
4566 | (current-prefix-arg | |
4567 | nil) | |
705a5933 | 4568 | ((and transient-mark-mode mark-active) |
c66587fe | 4569 | (deactivate-mark)) |
1b657835 RS |
4570 | ((> (recursion-depth) 0) |
4571 | (exit-recursive-edit)) | |
1c6c6fde RS |
4572 | (buffer-quit-function |
4573 | (funcall buffer-quit-function)) | |
c66587fe | 4574 | ((not (one-window-p t)) |
1b657835 RS |
4575 | (delete-other-windows)) |
4576 | ((string-match "^ \\*" (buffer-name (current-buffer))) | |
4577 | (bury-buffer)))) | |
c66587fe | 4578 | |
2d88b556 RS |
4579 | (defun play-sound-file (file &optional volume device) |
4580 | "Play sound stored in FILE. | |
4581 | VOLUME and DEVICE correspond to the keywords of the sound | |
4582 | specification for `play-sound'." | |
4583 | (interactive "fPlay sound file: ") | |
4584 | (let ((sound (list :file file))) | |
4585 | (if volume | |
4586 | (plist-put sound :volume volume)) | |
4587 | (if device | |
4588 | (plist-put sound :device device)) | |
4589 | (push 'sound sound) | |
4590 | (play-sound sound))) | |
4591 | ||
56abefac | 4592 | \f |
7683b5c2 DL |
4593 | (defcustom read-mail-command 'rmail |
4594 | "*Your preference for a mail reading package. | |
9023837e DL |
4595 | This is used by some keybindings which support reading mail. |
4596 | See also `mail-user-agent' concerning sending mail." | |
7683b5c2 DL |
4597 | :type '(choice (function-item rmail) |
4598 | (function-item gnus) | |
4599 | (function-item mh-rmail) | |
4600 | (function :tag "Other")) | |
4601 | :version "21.1" | |
4602 | :group 'mail) | |
4603 | ||
69c1dd37 | 4604 | (defcustom mail-user-agent 'sendmail-user-agent |
a31ca314 | 4605 | "*Your preference for a mail composition package. |
9023837e | 4606 | Various Emacs Lisp packages (e.g. Reporter) require you to compose an |
a31ca314 RS |
4607 | outgoing email message. This variable lets you specify which |
4608 | mail-sending package you prefer. | |
4609 | ||
4610 | Valid values include: | |
4611 | ||
9023837e DL |
4612 | `sendmail-user-agent' -- use the default Emacs Mail package. |
4613 | See Info node `(emacs)Sending Mail'. | |
4614 | `mh-e-user-agent' -- use the Emacs interface to the MH mail system. | |
4615 | See Info node `(mh-e)'. | |
4616 | `message-user-agent' -- use the Gnus Message package. | |
4617 | See Info node `(message)'. | |
4618 | `gnus-user-agent' -- like `message-user-agent', but with Gnus | |
4619 | paraphernalia, particularly the Gcc: header for | |
4620 | archiving. | |
a31ca314 RS |
4621 | |
4622 | Additional valid symbols may be available; check with the author of | |
15d0c9b1 DL |
4623 | your package for details. The function should return non-nil if it |
4624 | succeeds. | |
9023837e DL |
4625 | |
4626 | See also `read-mail-command' concerning reading mail." | |
69c1dd37 RS |
4627 | :type '(radio (function-item :tag "Default Emacs mail" |
4628 | :format "%t\n" | |
4629 | sendmail-user-agent) | |
4630 | (function-item :tag "Emacs interface to MH" | |
4631 | :format "%t\n" | |
4632 | mh-e-user-agent) | |
9023837e | 4633 | (function-item :tag "Gnus Message package" |
69c1dd37 RS |
4634 | :format "%t\n" |
4635 | message-user-agent) | |
9023837e DL |
4636 | (function-item :tag "Gnus Message with full Gnus features" |
4637 | :format "%t\n" | |
4638 | gnus-user-agent) | |
69c1dd37 RS |
4639 | (function :tag "Other")) |
4640 | :group 'mail) | |
a31ca314 | 4641 | |
a31ca314 | 4642 | (define-mail-user-agent 'sendmail-user-agent |
34fbcdf3 | 4643 | 'sendmail-user-agent-compose |
a31ca314 RS |
4644 | 'mail-send-and-exit) |
4645 | ||
360b5483 RS |
4646 | (defun rfc822-goto-eoh () |
4647 | ;; Go to header delimiter line in a mail message, following RFC822 rules | |
4648 | (goto-char (point-min)) | |
e1e04350 SM |
4649 | (when (re-search-forward |
4650 | "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move) | |
4651 | (goto-char (match-beginning 0)))) | |
360b5483 | 4652 | |
34fbcdf3 RS |
4653 | (defun sendmail-user-agent-compose (&optional to subject other-headers continue |
4654 | switch-function yank-action | |
4655 | send-actions) | |
4656 | (if switch-function | |
4657 | (let ((special-display-buffer-names nil) | |
4658 | (special-display-regexps nil) | |
4659 | (same-window-buffer-names nil) | |
4660 | (same-window-regexps nil)) | |
4661 | (funcall switch-function "*mail*"))) | |
9462bf2c RS |
4662 | (let ((cc (cdr (assoc-string "cc" other-headers t))) |
4663 | (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t))) | |
4664 | (body (cdr (assoc-string "body" other-headers t)))) | |
a9267c2c CY |
4665 | (or (mail continue to subject in-reply-to cc yank-action send-actions) |
4666 | continue | |
4667 | (error "Message aborted")) | |
34fbcdf3 | 4668 | (save-excursion |
360b5483 | 4669 | (rfc822-goto-eoh) |
34fbcdf3 | 4670 | (while other-headers |
0740c738 GM |
4671 | (unless (member-ignore-case (car (car other-headers)) |
4672 | '("in-reply-to" "cc" "body")) | |
34fbcdf3 | 4673 | (insert (car (car other-headers)) ": " |
15575807 CY |
4674 | (cdr (car other-headers)) |
4675 | (if use-hard-newlines hard-newline "\n"))) | |
34fbcdf3 | 4676 | (setq other-headers (cdr other-headers))) |
0740c738 GM |
4677 | (when body |
4678 | (forward-line 1) | |
4679 | (insert body)) | |
34fbcdf3 RS |
4680 | t))) |
4681 | ||
d0008a00 RS |
4682 | (defun compose-mail (&optional to subject other-headers continue |
4683 | switch-function yank-action send-actions) | |
4684 | "Start composing a mail message to send. | |
4685 | This uses the user's chosen mail composition package | |
4686 | as selected with the variable `mail-user-agent'. | |
4687 | The optional arguments TO and SUBJECT specify recipients | |
4688 | and the initial Subject field, respectively. | |
4689 | ||
4690 | OTHER-HEADERS is an alist specifying additional | |
4691 | header fields. Elements look like (HEADER . VALUE) where both | |
4692 | HEADER and VALUE are strings. | |
4693 | ||
4694 | CONTINUE, if non-nil, says to continue editing a message already | |
4695 | being composed. | |
4696 | ||
4697 | SWITCH-FUNCTION, if non-nil, is a function to use to | |
4698 | switch to and display the buffer used for mail composition. | |
4699 | ||
4700 | YANK-ACTION, if non-nil, is an action to perform, if and when necessary, | |
06720de2 RS |
4701 | to insert the raw text of the message being replied to. |
4702 | It has the form (FUNCTION . ARGS). The user agent will apply | |
4703 | FUNCTION to ARGS, to insert the raw text of the original message. | |
4704 | \(The user agent will also run `mail-citation-hook', *after* the | |
4705 | original text has been inserted in this way.) | |
d0008a00 RS |
4706 | |
4707 | SEND-ACTIONS is a list of actions to call when the message is sent. | |
4708 | Each action has the form (FUNCTION . ARGS)." | |
b5f019be RS |
4709 | (interactive |
4710 | (list nil nil nil current-prefix-arg)) | |
d0008a00 RS |
4711 | (let ((function (get mail-user-agent 'composefunc))) |
4712 | (funcall function to subject other-headers continue | |
4713 | switch-function yank-action send-actions))) | |
b5f019be RS |
4714 | |
4715 | (defun compose-mail-other-window (&optional to subject other-headers continue | |
4716 | yank-action send-actions) | |
4717 | "Like \\[compose-mail], but edit the outgoing message in another window." | |
4718 | (interactive | |
4719 | (list nil nil nil current-prefix-arg)) | |
4720 | (compose-mail to subject other-headers continue | |
4721 | 'switch-to-buffer-other-window yank-action send-actions)) | |
4722 | ||
4723 | ||
4724 | (defun compose-mail-other-frame (&optional to subject other-headers continue | |
4725 | yank-action send-actions) | |
4726 | "Like \\[compose-mail], but edit the outgoing message in another frame." | |
4727 | (interactive | |
4728 | (list nil nil nil current-prefix-arg)) | |
4729 | (compose-mail to subject other-headers continue | |
4730 | 'switch-to-buffer-other-frame yank-action send-actions)) | |
56abefac | 4731 | \f |
610c1c68 RS |
4732 | (defvar set-variable-value-history nil |
4733 | "History of values entered with `set-variable'.") | |
4734 | ||
d6281b4e | 4735 | (defun set-variable (variable value &optional make-local) |
610c1c68 | 4736 | "Set VARIABLE to VALUE. VALUE is a Lisp object. |
d6281b4e RS |
4737 | VARIABLE should be a user option variable name, a Lisp variable |
4738 | meant to be customized by users. You should enter VALUE in Lisp syntax, | |
4739 | so if you want VALUE to be a string, you must surround it with doublequotes. | |
610c1c68 RS |
4740 | VALUE is used literally, not evaluated. |
4741 | ||
4742 | If VARIABLE has a `variable-interactive' property, that is used as if | |
4743 | it were the arg to `interactive' (which see) to interactively read VALUE. | |
4744 | ||
4745 | If VARIABLE has been defined with `defcustom', then the type information | |
16236388 RS |
4746 | in the definition is used to check that VALUE is valid. |
4747 | ||
4748 | With a prefix argument, set VARIABLE to VALUE buffer-locally." | |
e9dfb72e RS |
4749 | (interactive |
4750 | (let* ((default-var (variable-at-point)) | |
7fd0ef0d JL |
4751 | (var (if (user-variable-p default-var) |
4752 | (read-variable (format "Set variable (default %s): " default-var) | |
4753 | default-var) | |
4754 | (read-variable "Set variable: "))) | |
7dcd2d16 AS |
4755 | (minibuffer-help-form '(describe-variable var)) |
4756 | (prop (get var 'variable-interactive)) | |
0684376b JB |
4757 | (obsolete (car (get var 'byte-obsolete-variable))) |
4758 | (prompt (format "Set %s %s to value: " var | |
7dcd2d16 | 4759 | (cond ((local-variable-p var) |
0684376b | 4760 | "(buffer-local)") |
7dcd2d16 AS |
4761 | ((or current-prefix-arg |
4762 | (local-variable-if-set-p var)) | |
0684376b JB |
4763 | "buffer-locally") |
4764 | (t "globally")))) | |
4765 | (val (progn | |
4766 | (when obsolete | |
4767 | (message (concat "`%S' is obsolete; " | |
4768 | (if (symbolp obsolete) "use `%S' instead" "%s")) | |
4769 | var obsolete) | |
4770 | (sit-for 3)) | |
4771 | (if prop | |
4772 | ;; Use VAR's `variable-interactive' property | |
4773 | ;; as an interactive spec for prompting. | |
4774 | (call-interactively `(lambda (arg) | |
4775 | (interactive ,prop) | |
4776 | arg)) | |
4777 | (read | |
4778 | (read-string prompt nil | |
7fd0ef0d JL |
4779 | 'set-variable-value-history |
4780 | (format "%S" (symbol-value var)))))))) | |
7dcd2d16 | 4781 | (list var val current-prefix-arg))) |
610c1c68 | 4782 | |
d6281b4e RS |
4783 | (and (custom-variable-p variable) |
4784 | (not (get variable 'custom-type)) | |
4785 | (custom-load-symbol variable)) | |
4786 | (let ((type (get variable 'custom-type))) | |
610c1c68 RS |
4787 | (when type |
4788 | ;; Match with custom type. | |
36755dd9 | 4789 | (require 'cus-edit) |
610c1c68 | 4790 | (setq type (widget-convert type)) |
d6281b4e | 4791 | (unless (widget-apply type :match value) |
1e722f9f | 4792 | (error "Value `%S' does not match type %S of %S" |
d6281b4e | 4793 | value (car type) variable)))) |
16236388 RS |
4794 | |
4795 | (if make-local | |
d6281b4e | 4796 | (make-local-variable variable)) |
f1180544 | 4797 | |
d6281b4e | 4798 | (set variable value) |
a2aef080 GM |
4799 | |
4800 | ;; Force a thorough redisplay for the case that the variable | |
4801 | ;; has an effect on the display, like `tab-width' has. | |
4802 | (force-mode-line-update)) | |
56abefac | 4803 | \f |
e8a700bf RS |
4804 | ;; Define the major mode for lists of completions. |
4805 | ||
98b45886 RS |
4806 | (defvar completion-list-mode-map nil |
4807 | "Local map for completion list buffers.") | |
ac29eb79 | 4808 | (or completion-list-mode-map |
e8a700bf RS |
4809 | (let ((map (make-sparse-keymap))) |
4810 | (define-key map [mouse-2] 'mouse-choose-completion) | |
771069f8 | 4811 | (define-key map [follow-link] 'mouse-face) |
eaf76065 | 4812 | (define-key map [down-mouse-2] nil) |
80298193 | 4813 | (define-key map "\C-m" 'choose-completion) |
1c6c6fde | 4814 | (define-key map "\e\e\e" 'delete-completion-window) |
dde69dbe RS |
4815 | (define-key map [left] 'previous-completion) |
4816 | (define-key map [right] 'next-completion) | |
ac29eb79 | 4817 | (setq completion-list-mode-map map))) |
e8a700bf RS |
4818 | |
4819 | ;; Completion mode is suitable only for specially formatted data. | |
ac29eb79 | 4820 | (put 'completion-list-mode 'mode-class 'special) |
e8a700bf | 4821 | |
98b45886 RS |
4822 | (defvar completion-reference-buffer nil |
4823 | "Record the buffer that was current when the completion list was requested. | |
4824 | This is a local variable in the completion list buffer. | |
ec39964e | 4825 | Initial value is nil to avoid some compiler warnings.") |
3819736b | 4826 | |
83434bda RS |
4827 | (defvar completion-no-auto-exit nil |
4828 | "Non-nil means `choose-completion-string' should never exit the minibuffer. | |
4829 | This also applies to other functions such as `choose-completion' | |
4830 | and `mouse-choose-completion'.") | |
4831 | ||
98b45886 RS |
4832 | (defvar completion-base-size nil |
4833 | "Number of chars at beginning of minibuffer not involved in completion. | |
4834 | This is a local variable in the completion list buffer | |
4835 | but it talks about the buffer in `completion-reference-buffer'. | |
4836 | If this is nil, it means to compare text to determine which part | |
4837 | of the tail end of the buffer's text is involved in completion.") | |
f6b293e3 | 4838 | |
1c6c6fde RS |
4839 | (defun delete-completion-window () |
4840 | "Delete the completion list window. | |
4841 | Go to the window from which completion was requested." | |
4842 | (interactive) | |
4843 | (let ((buf completion-reference-buffer)) | |
ddb2b181 RS |
4844 | (if (one-window-p t) |
4845 | (if (window-dedicated-p (selected-window)) | |
4846 | (delete-frame (selected-frame))) | |
4847 | (delete-window (selected-window)) | |
4848 | (if (get-buffer-window buf) | |
4849 | (select-window (get-buffer-window buf)))))) | |
1c6c6fde | 4850 | |
dde69dbe RS |
4851 | (defun previous-completion (n) |
4852 | "Move to the previous item in the completion list." | |
4853 | (interactive "p") | |
4854 | (next-completion (- n))) | |
4855 | ||
4856 | (defun next-completion (n) | |
4857 | "Move to the next item in the completion list. | |
1f238ac2 | 4858 | With prefix argument N, move N items (negative N means move backward)." |
dde69dbe | 4859 | (interactive "p") |
58dd38f1 SM |
4860 | (let ((beg (point-min)) (end (point-max))) |
4861 | (while (and (> n 0) (not (eobp))) | |
dde69dbe | 4862 | ;; If in a completion, move to the end of it. |
58dd38f1 SM |
4863 | (when (get-text-property (point) 'mouse-face) |
4864 | (goto-char (next-single-property-change (point) 'mouse-face nil end))) | |
dde69dbe | 4865 | ;; Move to start of next one. |
58dd38f1 SM |
4866 | (unless (get-text-property (point) 'mouse-face) |
4867 | (goto-char (next-single-property-change (point) 'mouse-face nil end))) | |
4868 | (setq n (1- n))) | |
4869 | (while (and (< n 0) (not (bobp))) | |
4870 | (let ((prop (get-text-property (1- (point)) 'mouse-face))) | |
4871 | ;; If in a completion, move to the start of it. | |
4872 | (when (and prop (eq prop (get-text-property (point) 'mouse-face))) | |
b61a81c2 | 4873 | (goto-char (previous-single-property-change |
58dd38f1 SM |
4874 | (point) 'mouse-face nil beg))) |
4875 | ;; Move to end of the previous completion. | |
4876 | (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face)) | |
4877 | (goto-char (previous-single-property-change | |
4878 | (point) 'mouse-face nil beg))) | |
4879 | ;; Move to the start of that one. | |
4880 | (goto-char (previous-single-property-change | |
4881 | (point) 'mouse-face nil beg)) | |
4882 | (setq n (1+ n)))))) | |
dde69dbe | 4883 | |
80298193 RS |
4884 | (defun choose-completion () |
4885 | "Choose the completion that point is in or next to." | |
4886 | (interactive) | |
f6b293e3 RS |
4887 | (let (beg end completion (buffer completion-reference-buffer) |
4888 | (base-size completion-base-size)) | |
6096f362 RS |
4889 | (if (and (not (eobp)) (get-text-property (point) 'mouse-face)) |
4890 | (setq end (point) beg (1+ (point)))) | |
4891 | (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face)) | |
3f299281 | 4892 | (setq end (1- (point)) beg (point))) |
6096f362 RS |
4893 | (if (null beg) |
4894 | (error "No completion here")) | |
4895 | (setq beg (previous-single-property-change beg 'mouse-face)) | |
88dd3c24 | 4896 | (setq end (or (next-single-property-change end 'mouse-face) (point-max))) |
6cdd0211 | 4897 | (setq completion (buffer-substring-no-properties beg end)) |
ab63960f RS |
4898 | (let ((owindow (selected-window))) |
4899 | (if (and (one-window-p t 'selected-frame) | |
4900 | (window-dedicated-p (selected-window))) | |
4901 | ;; This is a special buffer's frame | |
4902 | (iconify-frame (selected-frame)) | |
4903 | (or (window-dedicated-p (selected-window)) | |
4904 | (bury-buffer))) | |
4905 | (select-window owindow)) | |
f6b293e3 | 4906 | (choose-completion-string completion buffer base-size))) |
80298193 RS |
4907 | |
4908 | ;; Delete the longest partial match for STRING | |
4909 | ;; that can be found before POINT. | |
4910 | (defun choose-completion-delete-max-match (string) | |
4911 | (let ((opoint (point)) | |
f0bfada7 RS |
4912 | len) |
4913 | ;; Try moving back by the length of the string. | |
4914 | (goto-char (max (- (point) (length string)) | |
4915 | (minibuffer-prompt-end))) | |
4916 | ;; See how far back we were actually able to move. That is the | |
4917 | ;; upper bound on how much we can match and delete. | |
4918 | (setq len (- opoint (point))) | |
61bbf6fe RS |
4919 | (if completion-ignore-case |
4920 | (setq string (downcase string))) | |
80298193 | 4921 | (while (and (> len 0) |
f0bfada7 | 4922 | (let ((tail (buffer-substring (point) opoint))) |
61bbf6fe RS |
4923 | (if completion-ignore-case |
4924 | (setq tail (downcase tail))) | |
80298193 RS |
4925 | (not (string= tail (substring string 0 len))))) |
4926 | (setq len (1- len)) | |
4927 | (forward-char 1)) | |
4928 | (delete-char len))) | |
4929 | ||
ba36181b | 4930 | (defvar choose-completion-string-functions nil |
bbbbb15b KS |
4931 | "Functions that may override the normal insertion of a completion choice. |
4932 | These functions are called in order with four arguments: | |
4933 | CHOICE - the string to insert in the buffer, | |
4934 | BUFFER - the buffer in which the choice should be inserted, | |
89a6cfe3 | 4935 | MINI-P - non-nil iff BUFFER is a minibuffer, and |
12829a07 RS |
4936 | BASE-SIZE - the number of characters in BUFFER before |
4937 | the string being completed. | |
4938 | ||
bbbbb15b KS |
4939 | If a function in the list returns non-nil, that function is supposed |
4940 | to have inserted the CHOICE in the BUFFER, and possibly exited | |
12829a07 | 4941 | the minibuffer; no further functions will be called. |
ba36181b | 4942 | |
12829a07 RS |
4943 | If all functions in the list return nil, that means to use |
4944 | the default method of inserting the completion in BUFFER.") | |
74d0290b | 4945 | |
f6b293e3 | 4946 | (defun choose-completion-string (choice &optional buffer base-size) |
12829a07 RS |
4947 | "Switch to BUFFER and insert the completion choice CHOICE. |
4948 | BASE-SIZE, if non-nil, says how many characters of BUFFER's text | |
e36aeef9 RS |
4949 | to keep. If it is nil, we call `choose-completion-delete-max-match' |
4950 | to decide what to delete." | |
12829a07 RS |
4951 | |
4952 | ;; If BUFFER is the minibuffer, exit the minibuffer | |
4953 | ;; unless it is reading a file name and CHOICE is a directory, | |
4954 | ;; or completion-no-auto-exit is non-nil. | |
4955 | ||
1a0d0b6a JPW |
4956 | (let* ((buffer (or buffer completion-reference-buffer)) |
4957 | (mini-p (minibufferp buffer))) | |
cf52ad58 RS |
4958 | ;; If BUFFER is a minibuffer, barf unless it's the currently |
4959 | ;; active minibuffer. | |
f436a90a | 4960 | (if (and mini-p |
45486731 RS |
4961 | (or (not (active-minibuffer-window)) |
4962 | (not (equal buffer | |
4963 | (window-buffer (active-minibuffer-window)))))) | |
cf52ad58 | 4964 | (error "Minibuffer is not active for completion") |
17aa3385 KS |
4965 | ;; Set buffer so buffer-local choose-completion-string-functions works. |
4966 | (set-buffer buffer) | |
f1180544 | 4967 | (unless (run-hook-with-args-until-success |
d99f8496 SM |
4968 | 'choose-completion-string-functions |
4969 | choice buffer mini-p base-size) | |
4970 | ;; Insert the completion into the buffer where it was requested. | |
bbbbb15b KS |
4971 | (if base-size |
4972 | (delete-region (+ base-size (if mini-p | |
4973 | (minibuffer-prompt-end) | |
4974 | (point-min))) | |
4975 | (point)) | |
4976 | (choose-completion-delete-max-match choice)) | |
4977 | (insert choice) | |
4978 | (remove-text-properties (- (point) (length choice)) (point) | |
4979 | '(mouse-face nil)) | |
4980 | ;; Update point in the window that BUFFER is showing in. | |
4981 | (let ((window (get-buffer-window buffer t))) | |
4982 | (set-window-point window (point))) | |
4983 | ;; If completing for the minibuffer, exit it with this choice. | |
4984 | (and (not completion-no-auto-exit) | |
4985 | (equal buffer (window-buffer (minibuffer-window))) | |
4986 | minibuffer-completion-table | |
4987 | ;; If this is reading a file name, and the file name chosen | |
4988 | ;; is a directory, don't exit the minibuffer. | |
4989 | (if (and (eq minibuffer-completion-table 'read-file-name-internal) | |
4990 | (file-directory-p (field-string (point-max)))) | |
4991 | (let ((mini (active-minibuffer-window))) | |
4992 | (select-window mini) | |
4993 | (when minibuffer-auto-raise | |
4994 | (raise-frame (window-frame mini)))) | |
4995 | (exit-minibuffer))))))) | |
80298193 | 4996 | |
ac29eb79 | 4997 | (defun completion-list-mode () |
e8a700bf | 4998 | "Major mode for buffers showing lists of possible completions. |
80298193 RS |
4999 | Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\ |
5000 | to select the completion near point. | |
5001 | Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\ | |
5002 | with the mouse." | |
e8a700bf RS |
5003 | (interactive) |
5004 | (kill-all-local-variables) | |
ac29eb79 RS |
5005 | (use-local-map completion-list-mode-map) |
5006 | (setq mode-name "Completion List") | |
5007 | (setq major-mode 'completion-list-mode) | |
f6b293e3 RS |
5008 | (make-local-variable 'completion-base-size) |
5009 | (setq completion-base-size nil) | |
b2777913 | 5010 | (run-mode-hooks 'completion-list-mode-hook)) |
e8a700bf | 5011 | |
c8d6d636 GM |
5012 | (defun completion-list-mode-finish () |
5013 | "Finish setup of the completions buffer. | |
5014 | Called from `temp-buffer-show-hook'." | |
5015 | (when (eq major-mode 'completion-list-mode) | |
5016 | (toggle-read-only 1))) | |
5017 | ||
5018 | (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish) | |
5019 | ||
747a0e2f RS |
5020 | (defvar completion-setup-hook nil |
5021 | "Normal hook run at the end of setting up a completion list buffer. | |
5022 | When this hook is run, the current buffer is the one in which the | |
5023 | command to display the completion list buffer was run. | |
f5fab556 | 5024 | The completion list buffer is available as the value of `standard-output'. |
63219d53 | 5025 | The common prefix substring for completion may be available as the |
f5fab556 MY |
5026 | value of `completion-common-substring'. See also `display-completion-list'.") |
5027 | ||
5028 | ||
5029 | ;; Variables and faces used in `completion-setup-function'. | |
747a0e2f | 5030 | |
d0fd0916 JPW |
5031 | (defcustom completion-show-help t |
5032 | "Non-nil means show help message in *Completions* buffer." | |
5033 | :type 'boolean | |
5034 | :version "22.1" | |
5035 | :group 'completion) | |
5036 | ||
6a4940b2 | 5037 | (defface completions-first-difference |
abcdd45a MY |
5038 | '((t (:inherit bold))) |
5039 | "Face put on the first uncommon character in completions in *Completions* buffer." | |
5040 | :group 'completion) | |
5041 | ||
6a4940b2 | 5042 | (defface completions-common-part |
abcdd45a | 5043 | '((t (:inherit default))) |
e4ef3e92 JL |
5044 | "Face put on the common prefix substring in completions in *Completions* buffer. |
5045 | The idea of `completions-common-part' is that you can use it to | |
5046 | make the common parts less visible than normal, so that the rest | |
5047 | of the differing parts is, by contrast, slightly highlighted." | |
abcdd45a | 5048 | :group 'completion) |
6096f362 | 5049 | |
abaf2e77 EZ |
5050 | ;; This is for packages that need to bind it to a non-default regexp |
5051 | ;; in order to make the first-differing character highlight work | |
5052 | ;; to their liking | |
5053 | (defvar completion-root-regexp "^/" | |
5054 | "Regexp to use in `completion-setup-function' to find the root directory.") | |
5055 | ||
f5fab556 MY |
5056 | (defvar completion-common-substring nil |
5057 | "Common prefix substring to use in `completion-setup-function' to put faces. | |
5058 | The value is set by `display-completion-list' during running `completion-setup-hook'. | |
5059 | ||
6cdd0211 JL |
5060 | To put faces `completions-first-difference' and `completions-common-part' |
5061 | in the `*Completions*' buffer, the common prefix substring in completions | |
5062 | is needed as a hint. (The minibuffer is a special case. The content | |
5063 | of the minibuffer before point is always the common substring.)") | |
f5fab556 MY |
5064 | |
5065 | ;; This function goes in completion-setup-hook, so that it is called | |
5066 | ;; after the text of the completion list buffer is written. | |
e8a700bf | 5067 | (defun completion-setup-function () |
1b5fd09e | 5068 | (let* ((mainbuf (current-buffer)) |
6cdd0211 JL |
5069 | (mbuf-contents (minibuffer-completion-contents)) |
5070 | common-string-length) | |
621a3f62 SM |
5071 | ;; When reading a file name in the minibuffer, |
5072 | ;; set default-directory in the minibuffer | |
5073 | ;; so it will get copied into the completion list buffer. | |
5074 | (if minibuffer-completing-file-name | |
5075 | (with-current-buffer mainbuf | |
5076 | (setq default-directory (file-name-directory mbuf-contents)))) | |
5077 | (with-current-buffer standard-output | |
3819736b | 5078 | (completion-list-mode) |
1b5fd09e | 5079 | (set (make-local-variable 'completion-reference-buffer) mainbuf) |
b8386753 | 5080 | (setq completion-base-size |
6cdd0211 JL |
5081 | (cond |
5082 | ((and (symbolp minibuffer-completion-table) | |
5083 | (get minibuffer-completion-table 'completion-base-size-function)) | |
5084 | ;; To compute base size, a function can use the global value of | |
5085 | ;; completion-common-substring or minibuffer-completion-contents. | |
5086 | (with-current-buffer mainbuf | |
5087 | (funcall (get minibuffer-completion-table | |
5088 | 'completion-base-size-function)))) | |
5089 | (minibuffer-completing-file-name | |
5090 | ;; For file name completion, use the number of chars before | |
5091 | ;; the start of the file name component at point. | |
5092 | (with-current-buffer mainbuf | |
5093 | (save-excursion | |
5094 | (skip-chars-backward completion-root-regexp) | |
5095 | (- (point) (minibuffer-prompt-end))))) | |
5096 | ;; Otherwise, in minibuffer, the base size is 0. | |
5097 | ((minibufferp mainbuf) 0))) | |
5098 | (setq common-string-length | |
5099 | (cond | |
5100 | (completion-common-substring | |
5101 | (length completion-common-substring)) | |
5102 | (completion-base-size | |
5103 | (- (length mbuf-contents) completion-base-size)))) | |
621a3f62 | 5104 | ;; Put faces on first uncommon characters and common parts. |
6cdd0211 | 5105 | (when (and (integerp common-string-length) (>= common-string-length 0)) |
1b5fd09e SM |
5106 | (let ((element-start (point-min)) |
5107 | (maxp (point-max)) | |
5108 | element-common-end) | |
5109 | (while (and (setq element-start | |
5110 | (next-single-property-change | |
5111 | element-start 'mouse-face)) | |
5112 | (< (setq element-common-end | |
5113 | (+ element-start common-string-length)) | |
5114 | maxp)) | |
a0bd072b | 5115 | (when (get-char-property element-start 'mouse-face) |
6cdd0211 JL |
5116 | (if (and (> common-string-length 0) |
5117 | (get-char-property (1- element-common-end) 'mouse-face)) | |
a0bd072b JL |
5118 | (put-text-property element-start element-common-end |
5119 | 'font-lock-face 'completions-common-part)) | |
5120 | (if (get-char-property element-common-end 'mouse-face) | |
5121 | (put-text-property element-common-end (1+ element-common-end) | |
5122 | 'font-lock-face 'completions-first-difference)))))) | |
d0fd0916 JPW |
5123 | ;; Maybe insert help string. |
5124 | (when completion-show-help | |
5125 | (goto-char (point-min)) | |
5126 | (if (display-mouse-p) | |
5127 | (insert (substitute-command-keys | |
5128 | "Click \\[mouse-choose-completion] on a completion to select it.\n"))) | |
5129 | (insert (substitute-command-keys | |
5130 | "In this buffer, type \\[choose-completion] to \ | |
5131 | select the completion near point.\n\n")))))) | |
c88ab9ce | 5132 | |
e8a700bf | 5133 | (add-hook 'completion-setup-hook 'completion-setup-function) |
dde69dbe | 5134 | |
1b5fd09e SM |
5135 | (define-key minibuffer-local-completion-map [prior] 'switch-to-completions) |
5136 | (define-key minibuffer-local-completion-map "\M-v" 'switch-to-completions) | |
dde69dbe RS |
5137 | |
5138 | (defun switch-to-completions () | |
5139 | "Select the completion list window." | |
5140 | (interactive) | |
9595fbdb RS |
5141 | ;; Make sure we have a completions window. |
5142 | (or (get-buffer-window "*Completions*") | |
5143 | (minibuffer-completion-help)) | |
fdbd7c4d KH |
5144 | (let ((window (get-buffer-window "*Completions*"))) |
5145 | (when window | |
5146 | (select-window window) | |
5147 | (goto-char (point-min)) | |
5148 | (search-forward "\n\n") | |
5149 | (forward-line 1)))) | |
f6039de6 JL |
5150 | \f |
5151 | ;;; Support keyboard commands to turn on various modifiers. | |
82072f33 RS |
5152 | |
5153 | ;; These functions -- which are not commands -- each add one modifier | |
5154 | ;; to the following event. | |
5155 | ||
5156 | (defun event-apply-alt-modifier (ignore-prompt) | |
1e96c007 | 5157 | "\\<function-key-map>Add the Alt modifier to the following event. |
70cf9f08 | 5158 | For example, type \\[event-apply-alt-modifier] & to enter Alt-&." |
82072f33 RS |
5159 | (vector (event-apply-modifier (read-event) 'alt 22 "A-"))) |
5160 | (defun event-apply-super-modifier (ignore-prompt) | |
1e96c007 | 5161 | "\\<function-key-map>Add the Super modifier to the following event. |
70cf9f08 | 5162 | For example, type \\[event-apply-super-modifier] & to enter Super-&." |
82072f33 RS |
5163 | (vector (event-apply-modifier (read-event) 'super 23 "s-"))) |
5164 | (defun event-apply-hyper-modifier (ignore-prompt) | |
1e96c007 | 5165 | "\\<function-key-map>Add the Hyper modifier to the following event. |
70cf9f08 | 5166 | For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&." |
82072f33 RS |
5167 | (vector (event-apply-modifier (read-event) 'hyper 24 "H-"))) |
5168 | (defun event-apply-shift-modifier (ignore-prompt) | |
1e96c007 | 5169 | "\\<function-key-map>Add the Shift modifier to the following event. |
70cf9f08 | 5170 | For example, type \\[event-apply-shift-modifier] & to enter Shift-&." |
82072f33 RS |
5171 | (vector (event-apply-modifier (read-event) 'shift 25 "S-"))) |
5172 | (defun event-apply-control-modifier (ignore-prompt) | |
1e96c007 | 5173 | "\\<function-key-map>Add the Ctrl modifier to the following event. |
70cf9f08 | 5174 | For example, type \\[event-apply-control-modifier] & to enter Ctrl-&." |
82072f33 RS |
5175 | (vector (event-apply-modifier (read-event) 'control 26 "C-"))) |
5176 | (defun event-apply-meta-modifier (ignore-prompt) | |
1e96c007 | 5177 | "\\<function-key-map>Add the Meta modifier to the following event. |
70cf9f08 | 5178 | For example, type \\[event-apply-meta-modifier] & to enter Meta-&." |
82072f33 RS |
5179 | (vector (event-apply-modifier (read-event) 'meta 27 "M-"))) |
5180 | ||
5181 | (defun event-apply-modifier (event symbol lshiftby prefix) | |
5182 | "Apply a modifier flag to event EVENT. | |
5183 | SYMBOL is the name of this modifier, as a symbol. | |
5184 | LSHIFTBY is the numeric value of this modifier, in keyboard events. | |
5185 | PREFIX is the string that represents this modifier in an event type symbol." | |
5186 | (if (numberp event) | |
5187 | (cond ((eq symbol 'control) | |
90bebcb0 KH |
5188 | (if (and (<= (downcase event) ?z) |
5189 | (>= (downcase event) ?a)) | |
82072f33 | 5190 | (- (downcase event) ?a -1) |
90bebcb0 KH |
5191 | (if (and (<= (downcase event) ?Z) |
5192 | (>= (downcase event) ?A)) | |
82072f33 RS |
5193 | (- (downcase event) ?A -1) |
5194 | (logior (lsh 1 lshiftby) event)))) | |
5195 | ((eq symbol 'shift) | |
5196 | (if (and (<= (downcase event) ?z) | |
5197 | (>= (downcase event) ?a)) | |
5198 | (upcase event) | |
5199 | (logior (lsh 1 lshiftby) event))) | |
5200 | (t | |
5201 | (logior (lsh 1 lshiftby) event))) | |
5202 | (if (memq symbol (event-modifiers event)) | |
5203 | event | |
5204 | (let ((event-type (if (symbolp event) event (car event)))) | |
5205 | (setq event-type (intern (concat prefix (symbol-name event-type)))) | |
5206 | (if (symbolp event) | |
5207 | event-type | |
5208 | (cons event-type (cdr event))))))) | |
5209 | ||
e5fff738 KH |
5210 | (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier) |
5211 | (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier) | |
5212 | (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier) | |
5213 | (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier) | |
5214 | (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier) | |
5215 | (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier) | |
f6039de6 | 5216 | \f |
a3d1480b JB |
5217 | ;;;; Keypad support. |
5218 | ||
5219 | ;;; Make the keypad keys act like ordinary typing keys. If people add | |
5220 | ;;; bindings for the function key symbols, then those bindings will | |
5221 | ;;; override these, so this shouldn't interfere with any existing | |
5222 | ;;; bindings. | |
5223 | ||
0d173134 | 5224 | ;; Also tell read-char how to handle these keys. |
e1e04350 | 5225 | (mapc |
a3d1480b JB |
5226 | (lambda (keypad-normal) |
5227 | (let ((keypad (nth 0 keypad-normal)) | |
5228 | (normal (nth 1 keypad-normal))) | |
0d173134 | 5229 | (put keypad 'ascii-character normal) |
a3d1480b JB |
5230 | (define-key function-key-map (vector keypad) (vector normal)))) |
5231 | '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4) | |
5232 | (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9) | |
f33321ad | 5233 | (kp-space ?\s) |
a3d1480b JB |
5234 | (kp-tab ?\t) |
5235 | (kp-enter ?\r) | |
5236 | (kp-multiply ?*) | |
5237 | (kp-add ?+) | |
5238 | (kp-separator ?,) | |
5239 | (kp-subtract ?-) | |
5240 | (kp-decimal ?.) | |
5241 | (kp-divide ?/) | |
5242 | (kp-equal ?=))) | |
f54b0d85 | 5243 | \f |
1e722f9f | 5244 | ;;;; |
b005abd5 | 5245 | ;;;; forking a twin copy of a buffer. |
1e722f9f | 5246 | ;;;; |
b005abd5 SM |
5247 | |
5248 | (defvar clone-buffer-hook nil | |
5249 | "Normal hook to run in the new buffer at the end of `clone-buffer'.") | |
5250 | ||
5251 | (defun clone-process (process &optional newname) | |
5252 | "Create a twin copy of PROCESS. | |
5253 | If NEWNAME is nil, it defaults to PROCESS' name; | |
5254 | NEWNAME is modified by adding or incrementing <N> at the end as necessary. | |
5255 | If PROCESS is associated with a buffer, the new process will be associated | |
5256 | with the current buffer instead. | |
5257 | Returns nil if PROCESS has already terminated." | |
5258 | (setq newname (or newname (process-name process))) | |
5259 | (if (string-match "<[0-9]+>\\'" newname) | |
5260 | (setq newname (substring newname 0 (match-beginning 0)))) | |
5261 | (when (memq (process-status process) '(run stop open)) | |
5262 | (let* ((process-connection-type (process-tty-name process)) | |
b005abd5 SM |
5263 | (new-process |
5264 | (if (memq (process-status process) '(open)) | |
ed7069af KS |
5265 | (let ((args (process-contact process t))) |
5266 | (setq args (plist-put args :name newname)) | |
5267 | (setq args (plist-put args :buffer | |
403ca8d9 KS |
5268 | (if (process-buffer process) |
5269 | (current-buffer)))) | |
ed7069af | 5270 | (apply 'make-network-process args)) |
b005abd5 SM |
5271 | (apply 'start-process newname |
5272 | (if (process-buffer process) (current-buffer)) | |
5273 | (process-command process))))) | |
ed7069af KS |
5274 | (set-process-query-on-exit-flag |
5275 | new-process (process-query-on-exit-flag process)) | |
b005abd5 SM |
5276 | (set-process-inherit-coding-system-flag |
5277 | new-process (process-inherit-coding-system-flag process)) | |
5278 | (set-process-filter new-process (process-filter process)) | |
5279 | (set-process-sentinel new-process (process-sentinel process)) | |
403ca8d9 | 5280 | (set-process-plist new-process (copy-sequence (process-plist process))) |
b005abd5 SM |
5281 | new-process))) |
5282 | ||
b75b82ab | 5283 | ;; things to maybe add (currently partly covered by `funcall mode'): |
b005abd5 SM |
5284 | ;; - syntax-table |
5285 | ;; - overlays | |
5286 | (defun clone-buffer (&optional newname display-flag) | |
186f9ad1 LT |
5287 | "Create and return a twin copy of the current buffer. |
5288 | Unlike an indirect buffer, the new buffer can be edited | |
5289 | independently of the old one (if it is not read-only). | |
5290 | NEWNAME is the name of the new buffer. It may be modified by | |
5291 | adding or incrementing <N> at the end as necessary to create a | |
5292 | unique buffer name. If nil, it defaults to the name of the | |
5293 | current buffer, with the proper suffix. If DISPLAY-FLAG is | |
5294 | non-nil, the new buffer is shown with `pop-to-buffer'. Trying to | |
5295 | clone a file-visiting buffer, or a buffer whose major mode symbol | |
5296 | has a non-nil `no-clone' property, results in an error. | |
5297 | ||
5298 | Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the | |
5299 | current buffer with appropriate suffix. However, if a prefix | |
5300 | argument is given, then the command prompts for NEWNAME in the | |
5301 | minibuffer. | |
b005abd5 | 5302 | |
b005abd5 SM |
5303 | This runs the normal hook `clone-buffer-hook' in the new buffer |
5304 | after it has been set up properly in other respects." | |
61acfe7f RS |
5305 | (interactive |
5306 | (progn | |
5307 | (if buffer-file-name | |
5308 | (error "Cannot clone a file-visiting buffer")) | |
5309 | (if (get major-mode 'no-clone) | |
5310 | (error "Cannot clone a buffer in %s mode" mode-name)) | |
f6039de6 JL |
5311 | (list (if current-prefix-arg |
5312 | (read-buffer "Name of new cloned buffer: " (current-buffer))) | |
61acfe7f | 5313 | t))) |
b005abd5 SM |
5314 | (if buffer-file-name |
5315 | (error "Cannot clone a file-visiting buffer")) | |
5316 | (if (get major-mode 'no-clone) | |
5317 | (error "Cannot clone a buffer in %s mode" mode-name)) | |
5318 | (setq newname (or newname (buffer-name))) | |
5319 | (if (string-match "<[0-9]+>\\'" newname) | |
5320 | (setq newname (substring newname 0 (match-beginning 0)))) | |
5321 | (let ((buf (current-buffer)) | |
5322 | (ptmin (point-min)) | |
5323 | (ptmax (point-max)) | |
5324 | (pt (point)) | |
5325 | (mk (if mark-active (mark t))) | |
5326 | (modified (buffer-modified-p)) | |
5327 | (mode major-mode) | |
5328 | (lvars (buffer-local-variables)) | |
5329 | (process (get-buffer-process (current-buffer))) | |
5330 | (new (generate-new-buffer (or newname (buffer-name))))) | |
5331 | (save-restriction | |
5332 | (widen) | |
5333 | (with-current-buffer new | |
5334 | (insert-buffer-substring buf))) | |
5335 | (with-current-buffer new | |
5336 | (narrow-to-region ptmin ptmax) | |
5337 | (goto-char pt) | |
5338 | (if mk (set-mark mk)) | |
5339 | (set-buffer-modified-p modified) | |
5340 | ||
5341 | ;; Clone the old buffer's process, if any. | |
5342 | (when process (clone-process process)) | |
5343 | ||
5344 | ;; Now set up the major mode. | |
5345 | (funcall mode) | |
5346 | ||
5347 | ;; Set up other local variables. | |
5348 | (mapcar (lambda (v) | |
5349 | (condition-case () ;in case var is read-only | |
5350 | (if (symbolp v) | |
5351 | (makunbound v) | |
5352 | (set (make-local-variable (car v)) (cdr v))) | |
5353 | (error nil))) | |
5354 | lvars) | |
5355 | ||
5356 | ;; Run any hooks (typically set up by the major mode | |
5357 | ;; for cloning to work properly). | |
5358 | (run-hooks 'clone-buffer-hook)) | |
0a487199 SM |
5359 | (if display-flag |
5360 | ;; Presumably the current buffer is shown in the selected frame, so | |
5361 | ;; we want to display the clone elsewhere. | |
5362 | (let ((same-window-regexps nil) | |
5363 | (same-window-buffer-names)) | |
5364 | (pop-to-buffer new))) | |
b005abd5 SM |
5365 | new)) |
5366 | ||
fa65f20b | 5367 | |
7e3afb04 | 5368 | (defun clone-indirect-buffer (newname display-flag &optional norecord) |
fa65f20b GM |
5369 | "Create an indirect buffer that is a twin copy of the current buffer. |
5370 | ||
01ba9662 | 5371 | Give the indirect buffer name NEWNAME. Interactively, read NEWNAME |
fa65f20b GM |
5372 | from the minibuffer when invoked with a prefix arg. If NEWNAME is nil |
5373 | or if not called with a prefix arg, NEWNAME defaults to the current | |
5374 | buffer's name. The name is modified by adding a `<N>' suffix to it | |
5375 | or by incrementing the N in an existing suffix. | |
5376 | ||
5377 | DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'. | |
7e3afb04 GM |
5378 | This is always done when called interactively. |
5379 | ||
f33321ad | 5380 | Optional third arg NORECORD non-nil means do not put this buffer at the |
7e3afb04 | 5381 | front of the list of recently selected ones." |
61acfe7f RS |
5382 | (interactive |
5383 | (progn | |
5384 | (if (get major-mode 'no-clone-indirect) | |
5385 | (error "Cannot indirectly clone a buffer in %s mode" mode-name)) | |
5386 | (list (if current-prefix-arg | |
f6039de6 | 5387 | (read-buffer "Name of indirect buffer: " (current-buffer))) |
61acfe7f RS |
5388 | t))) |
5389 | (if (get major-mode 'no-clone-indirect) | |
5390 | (error "Cannot indirectly clone a buffer in %s mode" mode-name)) | |
fa65f20b GM |
5391 | (setq newname (or newname (buffer-name))) |
5392 | (if (string-match "<[0-9]+>\\'" newname) | |
5393 | (setq newname (substring newname 0 (match-beginning 0)))) | |
5394 | (let* ((name (generate-new-buffer-name newname)) | |
5395 | (buffer (make-indirect-buffer (current-buffer) name t))) | |
5396 | (when display-flag | |
58dd38f1 | 5397 | (pop-to-buffer buffer norecord)) |
fa65f20b GM |
5398 | buffer)) |
5399 | ||
5400 | ||
1fffd65f RS |
5401 | (defun clone-indirect-buffer-other-window (newname display-flag &optional norecord) |
5402 | "Like `clone-indirect-buffer' but display in another window." | |
2ef0a47e RS |
5403 | (interactive |
5404 | (progn | |
5405 | (if (get major-mode 'no-clone-indirect) | |
5406 | (error "Cannot indirectly clone a buffer in %s mode" mode-name)) | |
5407 | (list (if current-prefix-arg | |
f6039de6 | 5408 | (read-buffer "Name of indirect buffer: " (current-buffer))) |
2ef0a47e | 5409 | t))) |
acd39eb6 | 5410 | (let ((pop-up-windows t)) |
1fffd65f | 5411 | (clone-indirect-buffer newname display-flag norecord))) |
7e3afb04 | 5412 | |
f54b0d85 | 5413 | \f |
1d4b11bf GM |
5414 | ;;; Handling of Backspace and Delete keys. |
5415 | ||
e5212545 SM |
5416 | (defcustom normal-erase-is-backspace |
5417 | (and (not noninteractive) | |
5418 | (or (memq system-type '(ms-dos windows-nt)) | |
267e1dbe | 5419 | (eq window-system 'mac) |
e5212545 SM |
5420 | (and (memq window-system '(x)) |
5421 | (fboundp 'x-backspace-delete-keys-p) | |
5422 | (x-backspace-delete-keys-p)) | |
5423 | ;; If the terminal Emacs is running on has erase char | |
5424 | ;; set to ^H, use the Backspace key for deleting | |
5425 | ;; backward and, and the Delete key for deleting forward. | |
5426 | (and (null window-system) | |
5427 | (eq tty-erase-char ?\^H)))) | |
7f62656b | 5428 | "If non-nil, Delete key deletes forward and Backspace key deletes backward. |
1d4b11bf GM |
5429 | |
5430 | On window systems, the default value of this option is chosen | |
5431 | according to the keyboard used. If the keyboard has both a Backspace | |
5432 | key and a Delete key, and both are mapped to their usual meanings, the | |
5433 | option's default value is set to t, so that Backspace can be used to | |
7f62656b | 5434 | delete backward, and Delete can be used to delete forward. |
1d4b11bf | 5435 | |
7f62656b | 5436 | If not running under a window system, customizing this option accomplishes |
1d4b11bf GM |
5437 | a similar effect by mapping C-h, which is usually generated by the |
5438 | Backspace key, to DEL, and by mapping DEL to C-d via | |
5439 | `keyboard-translate'. The former functionality of C-h is available on | |
5440 | the F1 key. You should probably not use this setting if you don't | |
f060b834 GM |
5441 | have both Backspace, Delete and F1 keys. |
5442 | ||
5443 | Setting this variable with setq doesn't take effect. Programmatically, | |
7f62656b | 5444 | call `normal-erase-is-backspace-mode' (which see) instead." |
1d4b11bf GM |
5445 | :type 'boolean |
5446 | :group 'editing-basics | |
5447 | :version "21.1" | |
5448 | :set (lambda (symbol value) | |
5449 | ;; The fboundp is because of a problem with :set when | |
5450 | ;; dumping Emacs. It doesn't really matter. | |
7f62656b EZ |
5451 | (if (fboundp 'normal-erase-is-backspace-mode) |
5452 | (normal-erase-is-backspace-mode (or value 0)) | |
1d4b11bf GM |
5453 | (set-default symbol value)))) |
5454 | ||
5455 | ||
7f62656b EZ |
5456 | (defun normal-erase-is-backspace-mode (&optional arg) |
5457 | "Toggle the Erase and Delete mode of the Backspace and Delete keys. | |
5458 | ||
e02160a3 | 5459 | With numeric arg, turn the mode on if and only if ARG is positive. |
7f62656b EZ |
5460 | |
5461 | On window systems, when this mode is on, Delete is mapped to C-d and | |
5462 | Backspace is mapped to DEL; when this mode is off, both Delete and | |
5463 | Backspace are mapped to DEL. (The remapping goes via | |
5464 | `function-key-map', so binding Delete or Backspace in the global or | |
5465 | local keymap will override that.) | |
5466 | ||
5467 | In addition, on window systems, the bindings of C-Delete, M-Delete, | |
5468 | C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in | |
5469 | the global keymap in accordance with the functionality of Delete and | |
5470 | Backspace. For example, if Delete is remapped to C-d, which deletes | |
5471 | forward, C-Delete is bound to `kill-word', but if Delete is remapped | |
5472 | to DEL, which deletes backward, C-Delete is bound to | |
5473 | `backward-kill-word'. | |
5474 | ||
5475 | If not running on a window system, a similar effect is accomplished by | |
5476 | remapping C-h (normally produced by the Backspace key) and DEL via | |
5477 | `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL | |
5478 | to C-d; if it's off, the keys are not remapped. | |
5479 | ||
5480 | When not running on a window system, and this mode is turned on, the | |
5481 | former functionality of C-h is available on the F1 key. You should | |
5482 | probably not turn on this mode on a text-only terminal if you don't | |
5483 | have both Backspace, Delete and F1 keys. | |
5484 | ||
5485 | See also `normal-erase-is-backspace'." | |
1d4b11bf | 5486 | (interactive "P") |
7f62656b | 5487 | (setq normal-erase-is-backspace |
1d4b11bf GM |
5488 | (if arg |
5489 | (> (prefix-numeric-value arg) 0) | |
7f62656b | 5490 | (not normal-erase-is-backspace))) |
1d4b11bf | 5491 | |
13a9eed7 EZ |
5492 | (cond ((or (memq window-system '(x w32 mac pc)) |
5493 | (memq system-type '(ms-dos windows-nt))) | |
7f62656b | 5494 | (let ((bindings |
103db06c RS |
5495 | `(([C-delete] [C-backspace]) |
5496 | ([M-delete] [M-backspace]) | |
5497 | ([C-M-delete] [C-M-backspace]) | |
ec9f4754 | 5498 | (,esc-map |
103db06c RS |
5499 | [C-delete] [C-backspace]))) |
5500 | (old-state (lookup-key function-key-map [delete]))) | |
ec9f4754 | 5501 | |
7f62656b | 5502 | (if normal-erase-is-backspace |
ec9f4754 GM |
5503 | (progn |
5504 | (define-key function-key-map [delete] [?\C-d]) | |
be4f1e41 | 5505 | (define-key function-key-map [kp-delete] [?\C-d]) |
ec9f4754 GM |
5506 | (define-key function-key-map [backspace] [?\C-?])) |
5507 | (define-key function-key-map [delete] [?\C-?]) | |
be4f1e41 | 5508 | (define-key function-key-map [kp-delete] [?\C-?]) |
ec9f4754 GM |
5509 | (define-key function-key-map [backspace] [?\C-?])) |
5510 | ||
103db06c RS |
5511 | ;; Maybe swap bindings of C-delete and C-backspace, etc. |
5512 | (unless (equal old-state (lookup-key function-key-map [delete])) | |
5513 | (dolist (binding bindings) | |
5514 | (let ((map global-map)) | |
5515 | (when (keymapp (car binding)) | |
5516 | (setq map (car binding) binding (cdr binding))) | |
5517 | (let* ((key1 (nth 0 binding)) | |
5518 | (key2 (nth 1 binding)) | |
5519 | (binding1 (lookup-key map key1)) | |
5520 | (binding2 (lookup-key map key2))) | |
5521 | (define-key map key1 binding2) | |
5522 | (define-key map key2 binding1))))))) | |
1d4b11bf | 5523 | (t |
7f62656b | 5524 | (if normal-erase-is-backspace |
1d4b11bf GM |
5525 | (progn |
5526 | (keyboard-translate ?\C-h ?\C-?) | |
5527 | (keyboard-translate ?\C-? ?\C-d)) | |
5528 | (keyboard-translate ?\C-h ?\C-h) | |
5529 | (keyboard-translate ?\C-? ?\C-?)))) | |
5530 | ||
7f62656b | 5531 | (run-hooks 'normal-erase-is-backspace-hook) |
1d4b11bf | 5532 | (if (interactive-p) |
7f62656b EZ |
5533 | (message "Delete key deletes %s" |
5534 | (if normal-erase-is-backspace "forward" "backward")))) | |
ea82f0df | 5535 | \f |
aca8bee5 | 5536 | (defvar vis-mode-saved-buffer-invisibility-spec nil |
0f7df535 | 5537 | "Saved value of `buffer-invisibility-spec' when Visible mode is on.") |
7f62656b | 5538 | |
0f7df535 RS |
5539 | (define-minor-mode visible-mode |
5540 | "Toggle Visible mode. | |
5541 | With argument ARG turn Visible mode on iff ARG is positive. | |
1d4b11bf | 5542 | |
0f7df535 RS |
5543 | Enabling Visible mode makes all invisible text temporarily visible. |
5544 | Disabling Visible mode turns off that effect. Visible mode | |
5545 | works by saving the value of `buffer-invisibility-spec' and setting it to nil." | |
4e57881d | 5546 | :lighter " Vis" |
ab77efd0 | 5547 | :group 'editing-basics |
aca8bee5 SM |
5548 | (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec) |
5549 | (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec) | |
5550 | (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec)) | |
0f7df535 | 5551 | (when visible-mode |
aca8bee5 SM |
5552 | (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec) |
5553 | buffer-invisibility-spec) | |
5554 | (setq buffer-invisibility-spec nil))) | |
4e57881d | 5555 | \f |
e1e04350 | 5556 | ;; Minibuffer prompt stuff. |
9b350152 | 5557 | |
49c14a05 GM |
5558 | ;(defun minibuffer-prompt-modification (start end) |
5559 | ; (error "You cannot modify the prompt")) | |
5560 | ; | |
5561 | ; | |
5562 | ;(defun minibuffer-prompt-insertion (start end) | |
5563 | ; (let ((inhibit-modification-hooks t)) | |
5564 | ; (delete-region start end) | |
5565 | ; ;; Discard undo information for the text insertion itself | |
5566 | ; ;; and for the text deletion.above. | |
5567 | ; (when (consp buffer-undo-list) | |
5568 | ; (setq buffer-undo-list (cddr buffer-undo-list))) | |
5569 | ; (message "You cannot modify the prompt"))) | |
5570 | ; | |
5571 | ; | |
f1180544 | 5572 | ;(setq minibuffer-prompt-properties |
49c14a05 GM |
5573 | ; (list 'modification-hooks '(minibuffer-prompt-modification) |
5574 | ; 'insert-in-front-hooks '(minibuffer-prompt-insertion))) | |
f1180544 | 5575 | ; |
9b350152 | 5576 | |
00398e3b | 5577 | (provide 'simple) |
ab5796a9 | 5578 | |
621a3f62 | 5579 | ;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd |
c88ab9ce | 5580 | ;;; simple.el ends here |