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