Commit | Line | Data |
---|---|---|
a4648137 | 1 | ;;; simple.el --- basic editing commands for Emacs -*- lexical-binding: t -*- |
c88ab9ce | 2 | |
ba318903 | 3 | ;; Copyright (C) 1985-1987, 1993-2014 Free Software Foundation, Inc. |
2076c87c | 4 | |
30764597 PJ |
5 | ;; Maintainer: FSF |
6 | ;; Keywords: internal | |
bd78fa1d | 7 | ;; Package: emacs |
30764597 | 8 | |
2076c87c JB |
9 | ;; This file is part of GNU Emacs. |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
2076c87c | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
2076c87c JB |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
2076c87c | 23 | |
d9ecc911 ER |
24 | ;;; Commentary: |
25 | ||
26 | ;; A grab-bag of basic Emacs commands not specifically related to some | |
27 | ;; major mode or to file-handling. | |
28 | ||
3a801d0c | 29 | ;;; Code: |
2076c87c | 30 | |
f31b1257 DN |
31 | (declare-function widget-convert "wid-edit" (type &rest args)) |
32 | (declare-function shell-mode "shell" ()) | |
d01a33cf | 33 | |
06b60517 | 34 | ;;; From compile.el |
ca60ee11 | 35 | (defvar compilation-current-error) |
06b60517 | 36 | (defvar compilation-context-lines) |
ca60ee11 | 37 | |
7fcce20f | 38 | (defcustom idle-update-delay 0.5 |
1d2b0303 | 39 | "Idle time delay before updating various things on the screen. |
7fcce20f RS |
40 | Various Emacs features that update auxiliary information when point moves |
41 | wait this many seconds after Emacs becomes idle before doing an update." | |
42 | :type 'number | |
43 | :group 'display | |
44 | :version "22.1") | |
d01a33cf | 45 | |
69c1dd37 | 46 | (defgroup killing nil |
c9f0110e | 47 | "Killing and yanking commands." |
69c1dd37 RS |
48 | :group 'editing) |
49 | ||
69c1dd37 RS |
50 | (defgroup paren-matching nil |
51 | "Highlight (un)matching of parens and expressions." | |
69c1dd37 | 52 | :group 'matching) |
ee9c5954 | 53 | \f |
50f007fb | 54 | ;;; next-error support framework |
bbf41690 RS |
55 | |
56 | (defgroup next-error nil | |
f33321ad | 57 | "`next-error' support framework." |
bbf41690 | 58 | :group 'compilation |
bf247b6e | 59 | :version "22.1") |
bbf41690 RS |
60 | |
61 | (defface next-error | |
62 | '((t (:inherit region))) | |
63 | "Face used to highlight next error locus." | |
64 | :group 'next-error | |
bf247b6e | 65 | :version "22.1") |
bbf41690 | 66 | |
7408ee97 | 67 | (defcustom next-error-highlight 0.5 |
1d2b0303 | 68 | "Highlighting of locations in selected source buffers. |
676b1a74 CY |
69 | If a number, highlight the locus in `next-error' face for the given time |
70 | in seconds, or until the next command is executed. | |
71 | If t, highlight the locus until the next command is executed, or until | |
72 | some other locus replaces it. | |
bbf41690 | 73 | If nil, don't highlight the locus in the source buffer. |
249f792c JL |
74 | If `fringe-arrow', indicate the locus by the fringe arrow |
75 | indefinitely until some other locus replaces it." | |
6d3c944b | 76 | :type '(choice (number :tag "Highlight for specified time") |
c81b29e6 | 77 | (const :tag "Semipermanent highlighting" t) |
bbf41690 | 78 | (const :tag "No highlighting" nil) |
6d3c944b | 79 | (const :tag "Fringe arrow" fringe-arrow)) |
bbf41690 | 80 | :group 'next-error |
bf247b6e | 81 | :version "22.1") |
bbf41690 | 82 | |
7408ee97 | 83 | (defcustom next-error-highlight-no-select 0.5 |
1d2b0303 | 84 | "Highlighting of locations in `next-error-no-select'. |
f33321ad | 85 | If number, highlight the locus in `next-error' face for given time in seconds. |
6d3c944b | 86 | If t, highlight the locus indefinitely until some other locus replaces it. |
bbf41690 | 87 | If nil, don't highlight the locus in the source buffer. |
249f792c JL |
88 | If `fringe-arrow', indicate the locus by the fringe arrow |
89 | indefinitely until some other locus replaces it." | |
6d3c944b | 90 | :type '(choice (number :tag "Highlight for specified time") |
c81b29e6 | 91 | (const :tag "Semipermanent highlighting" t) |
bbf41690 | 92 | (const :tag "No highlighting" nil) |
6d3c944b | 93 | (const :tag "Fringe arrow" fringe-arrow)) |
bbf41690 | 94 | :group 'next-error |
bf247b6e | 95 | :version "22.1") |
bbf41690 | 96 | |
446b609e | 97 | (defcustom next-error-recenter nil |
1d2b0303 | 98 | "Display the line in the visited source file recentered as specified. |
28adf31c TTN |
99 | If non-nil, the value is passed directly to `recenter'." |
100 | :type '(choice (integer :tag "Line to recenter to") | |
101 | (const :tag "Center of window" (4)) | |
446b609e TTN |
102 | (const :tag "No recentering" nil)) |
103 | :group 'next-error | |
104 | :version "23.1") | |
105 | ||
d634a3a2 | 106 | (defcustom next-error-hook nil |
1d2b0303 | 107 | "List of hook functions run by `next-error' after visiting source file." |
d634a3a2 JL |
108 | :type 'hook |
109 | :group 'next-error) | |
110 | ||
814c3037 JL |
111 | (defvar next-error-highlight-timer nil) |
112 | ||
9c9b00d6 | 113 | (defvar next-error-overlay-arrow-position nil) |
6bdad9ae | 114 | (put 'next-error-overlay-arrow-position 'overlay-arrow-string (purecopy "=>")) |
9c9b00d6 JL |
115 | (add-to-list 'overlay-arrow-variable-list 'next-error-overlay-arrow-position) |
116 | ||
50f007fb | 117 | (defvar next-error-last-buffer nil |
f33321ad | 118 | "The most recent `next-error' buffer. |
50f007fb KS |
119 | A buffer becomes most recent when its compilation, grep, or |
120 | similar mode is started, or when it is used with \\[next-error] | |
121 | or \\[compile-goto-error].") | |
122 | ||
123 | (defvar next-error-function nil | |
e462ab77 SM |
124 | "Function to use to find the next error in the current buffer. |
125 | The function is called with 2 parameters: | |
126 | ARG is an integer specifying by how many errors to move. | |
127 | RESET is a boolean which, if non-nil, says to go back to the beginning | |
128 | of the errors before moving. | |
129 | Major modes providing compile-like functionality should set this variable | |
130 | to indicate to `next-error' that this is a candidate buffer and how | |
131 | to navigate in it.") | |
50f007fb KS |
132 | (make-variable-buffer-local 'next-error-function) |
133 | ||
8cdba32b RS |
134 | (defvar next-error-move-function nil |
135 | "Function to use to move to an error locus. | |
136 | It takes two arguments, a buffer position in the error buffer | |
137 | and a buffer position in the error locus buffer. | |
138 | The buffer for the error locus should already be current. | |
139 | nil means use goto-char using the second argument position.") | |
140 | (make-variable-buffer-local 'next-error-move-function) | |
141 | ||
f1e2a033 | 142 | (defsubst next-error-buffer-p (buffer |
e967cd11 | 143 | &optional avoid-current |
f1e2a033 | 144 | extra-test-inclusive |
5f9e0ca5 | 145 | extra-test-exclusive) |
f33321ad | 146 | "Test if BUFFER is a `next-error' capable buffer. |
e967cd11 RS |
147 | |
148 | If AVOID-CURRENT is non-nil, treat the current buffer | |
149 | as an absolute last resort only. | |
150 | ||
151 | The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer | |
152 | that normally would not qualify. If it returns t, the buffer | |
153 | in question is treated as usable. | |
154 | ||
7979163c | 155 | The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer |
01ba9662 | 156 | that would normally be considered usable. If it returns nil, |
e967cd11 RS |
157 | that buffer is rejected." |
158 | (and (buffer-name buffer) ;First make sure it's live. | |
159 | (not (and avoid-current (eq buffer (current-buffer)))) | |
160 | (with-current-buffer buffer | |
161 | (if next-error-function ; This is the normal test. | |
162 | ;; Optionally reject some buffers. | |
163 | (if extra-test-exclusive | |
164 | (funcall extra-test-exclusive) | |
165 | t) | |
166 | ;; Optionally accept some other buffers. | |
167 | (and extra-test-inclusive | |
168 | (funcall extra-test-inclusive)))))) | |
169 | ||
170 | (defun next-error-find-buffer (&optional avoid-current | |
f1e2a033 | 171 | extra-test-inclusive |
5f9e0ca5 | 172 | extra-test-exclusive) |
f33321ad | 173 | "Return a `next-error' capable buffer. |
7979163c | 174 | |
e967cd11 RS |
175 | If AVOID-CURRENT is non-nil, treat the current buffer |
176 | as an absolute last resort only. | |
177 | ||
01ba9662 | 178 | The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer |
e967cd11 RS |
179 | that normally would not qualify. If it returns t, the buffer |
180 | in question is treated as usable. | |
181 | ||
7979163c | 182 | The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer |
e967cd11 RS |
183 | that would normally be considered usable. If it returns nil, |
184 | that buffer is rejected." | |
03e75c7e JL |
185 | (or |
186 | ;; 1. If one window on the selected frame displays such buffer, return it. | |
187 | (let ((window-buffers | |
188 | (delete-dups | |
189 | (delq nil (mapcar (lambda (w) | |
190 | (if (next-error-buffer-p | |
e967cd11 RS |
191 | (window-buffer w) |
192 | avoid-current | |
f1e2a033 | 193 | extra-test-inclusive extra-test-exclusive) |
03e75c7e JL |
194 | (window-buffer w))) |
195 | (window-list)))))) | |
03e75c7e JL |
196 | (if (eq (length window-buffers) 1) |
197 | (car window-buffers))) | |
e967cd11 | 198 | ;; 2. If next-error-last-buffer is an acceptable buffer, use that. |
03e75c7e | 199 | (if (and next-error-last-buffer |
e967cd11 | 200 | (next-error-buffer-p next-error-last-buffer avoid-current |
f1e2a033 | 201 | extra-test-inclusive extra-test-exclusive)) |
e967cd11 RS |
202 | next-error-last-buffer) |
203 | ;; 3. If the current buffer is acceptable, choose it. | |
204 | (if (next-error-buffer-p (current-buffer) avoid-current | |
205 | extra-test-inclusive extra-test-exclusive) | |
03e75c7e | 206 | (current-buffer)) |
e967cd11 | 207 | ;; 4. Look for any acceptable buffer. |
03e75c7e JL |
208 | (let ((buffers (buffer-list))) |
209 | (while (and buffers | |
e967cd11 RS |
210 | (not (next-error-buffer-p |
211 | (car buffers) avoid-current | |
212 | extra-test-inclusive extra-test-exclusive))) | |
03e75c7e | 213 | (setq buffers (cdr buffers))) |
e967cd11 RS |
214 | (car buffers)) |
215 | ;; 5. Use the current buffer as a last resort if it qualifies, | |
216 | ;; even despite AVOID-CURRENT. | |
217 | (and avoid-current | |
218 | (next-error-buffer-p (current-buffer) nil | |
219 | extra-test-inclusive extra-test-exclusive) | |
220 | (progn | |
ee4dc5d9 | 221 | (message "This is the only buffer with error message locations") |
e967cd11 RS |
222 | (current-buffer))) |
223 | ;; 6. Give up. | |
ee4dc5d9 | 224 | (error "No buffers contain error message locations"))) |
50f007fb | 225 | |
310abb0b | 226 | (defun next-error (&optional arg reset) |
f33321ad | 227 | "Visit next `next-error' message and corresponding source code. |
50f007fb KS |
228 | |
229 | If all the error messages parsed so far have been processed already, | |
230 | the message buffer is checked for new ones. | |
231 | ||
e462ab77 | 232 | A prefix ARG specifies how many error messages to move; |
50f007fb KS |
233 | negative means move back to previous error messages. |
234 | Just \\[universal-argument] as a prefix means reparse the error message buffer | |
235 | and start at the first error. | |
236 | ||
e249a6d8 | 237 | The RESET argument specifies that we should restart from the beginning. |
50f007fb KS |
238 | |
239 | \\[next-error] normally uses the most recently started | |
240 | compilation, grep, or occur buffer. It can also operate on any | |
241 | buffer with output from the \\[compile], \\[grep] commands, or, | |
242 | more generally, on any buffer in Compilation mode or with | |
243 | Compilation Minor mode enabled, or any buffer in which | |
03e75c7e JL |
244 | `next-error-function' is bound to an appropriate function. |
245 | To specify use of a particular buffer for error messages, type | |
246 | \\[next-error] in that buffer when it is the only one displayed | |
247 | in the current frame. | |
50f007fb | 248 | |
d634a3a2 JL |
249 | Once \\[next-error] has chosen the buffer for error messages, it |
250 | runs `next-error-hook' with `run-hooks', and stays with that buffer | |
251 | until you use it in some other buffer which uses Compilation mode | |
252 | or Compilation Minor mode. | |
50f007fb | 253 | |
3caced0b GM |
254 | To control which errors are matched, customize the variable |
255 | `compilation-error-regexp-alist'." | |
50f007fb | 256 | (interactive "P") |
e462ab77 | 257 | (if (consp arg) (setq reset t arg nil)) |
50f007fb KS |
258 | (when (setq next-error-last-buffer (next-error-find-buffer)) |
259 | ;; we know here that next-error-function is a valid symbol we can funcall | |
260 | (with-current-buffer next-error-last-buffer | |
d634a3a2 | 261 | (funcall next-error-function (prefix-numeric-value arg) reset) |
446b609e TTN |
262 | (when next-error-recenter |
263 | (recenter next-error-recenter)) | |
d634a3a2 | 264 | (run-hooks 'next-error-hook)))) |
50f007fb | 265 | |
56ab610b RS |
266 | (defun next-error-internal () |
267 | "Visit the source code corresponding to the `next-error' message at point." | |
268 | (setq next-error-last-buffer (current-buffer)) | |
269 | ;; we know here that next-error-function is a valid symbol we can funcall | |
270 | (with-current-buffer next-error-last-buffer | |
271 | (funcall next-error-function 0 nil) | |
446b609e TTN |
272 | (when next-error-recenter |
273 | (recenter next-error-recenter)) | |
56ab610b RS |
274 | (run-hooks 'next-error-hook))) |
275 | ||
50f007fb KS |
276 | (defalias 'goto-next-locus 'next-error) |
277 | (defalias 'next-match 'next-error) | |
278 | ||
310abb0b | 279 | (defun previous-error (&optional n) |
f33321ad | 280 | "Visit previous `next-error' message and corresponding source code. |
50f007fb KS |
281 | |
282 | Prefix arg N says how many error messages to move backwards (or | |
283 | forwards, if negative). | |
284 | ||
285 | This operates on the output from the \\[compile] and \\[grep] commands." | |
286 | (interactive "p") | |
310abb0b | 287 | (next-error (- (or n 1)))) |
50f007fb | 288 | |
310abb0b | 289 | (defun first-error (&optional n) |
50f007fb KS |
290 | "Restart at the first error. |
291 | Visit corresponding source code. | |
292 | With prefix arg N, visit the source code of the Nth error. | |
293 | This operates on the output from the \\[compile] command, for instance." | |
294 | (interactive "p") | |
295 | (next-error n t)) | |
296 | ||
310abb0b | 297 | (defun next-error-no-select (&optional n) |
f33321ad | 298 | "Move point to the next error in the `next-error' buffer and highlight match. |
50f007fb KS |
299 | Prefix arg N says how many error messages to move forwards (or |
300 | backwards, if negative). | |
301 | Finds and highlights the source line like \\[next-error], but does not | |
302 | select the source buffer." | |
303 | (interactive "p") | |
ee9c5954 JL |
304 | (let ((next-error-highlight next-error-highlight-no-select)) |
305 | (next-error n)) | |
50f007fb KS |
306 | (pop-to-buffer next-error-last-buffer)) |
307 | ||
310abb0b | 308 | (defun previous-error-no-select (&optional n) |
f33321ad | 309 | "Move point to the previous error in the `next-error' buffer and highlight match. |
50f007fb KS |
310 | Prefix arg N says how many error messages to move backwards (or |
311 | forwards, if negative). | |
312 | Finds and highlights the source line like \\[previous-error], but does not | |
313 | select the source buffer." | |
314 | (interactive "p") | |
310abb0b | 315 | (next-error-no-select (- (or n 1)))) |
50f007fb | 316 | |
85be9ec4 | 317 | ;; Internal variable for `next-error-follow-mode-post-command-hook'. |
282d6eae EZ |
318 | (defvar next-error-follow-last-line nil) |
319 | ||
2a223f35 | 320 | (define-minor-mode next-error-follow-minor-mode |
282d6eae | 321 | "Minor mode for compilation, occur and diff modes. |
e1ac4066 GM |
322 | With a prefix argument ARG, enable mode if ARG is positive, and |
323 | disable it otherwise. If called from Lisp, enable mode if ARG is | |
324 | omitted or nil. | |
2a223f35 | 325 | When turned on, cursor motion in the compilation, grep, occur or diff |
e1ac4066 | 326 | buffer causes automatic display of the corresponding source code location." |
ed8e0f0a | 327 | :group 'next-error :init-value nil :lighter " Fol" |
8a98a6c2 | 328 | (if (not next-error-follow-minor-mode) |
282d6eae EZ |
329 | (remove-hook 'post-command-hook 'next-error-follow-mode-post-command-hook t) |
330 | (add-hook 'post-command-hook 'next-error-follow-mode-post-command-hook nil t) | |
e56dd5c6 | 331 | (make-local-variable 'next-error-follow-last-line))) |
282d6eae | 332 | |
85be9ec4 SM |
333 | ;; Used as a `post-command-hook' by `next-error-follow-mode' |
334 | ;; for the *Compilation* *grep* and *Occur* buffers. | |
282d6eae EZ |
335 | (defun next-error-follow-mode-post-command-hook () |
336 | (unless (equal next-error-follow-last-line (line-number-at-pos)) | |
337 | (setq next-error-follow-last-line (line-number-at-pos)) | |
338 | (condition-case nil | |
339 | (let ((compilation-context-lines nil)) | |
340 | (setq compilation-current-error (point)) | |
341 | (next-error-no-select 0)) | |
342 | (error t)))) | |
343 | ||
ee9c5954 | 344 | \f |
50f007fb KS |
345 | ;;; |
346 | ||
93be67de KH |
347 | (defun fundamental-mode () |
348 | "Major mode not specialized for anything in particular. | |
349 | Other major modes are defined by comparison with this one." | |
350 | (interactive) | |
e174f8db | 351 | (kill-all-local-variables) |
c9586acc | 352 | (run-mode-hooks)) |
eaae8106 | 353 | |
d445b3f8 SM |
354 | ;; Special major modes to view specially formatted data rather than files. |
355 | ||
356 | (defvar special-mode-map | |
357 | (let ((map (make-sparse-keymap))) | |
358 | (suppress-keymap map) | |
359 | (define-key map "q" 'quit-window) | |
ce3cefcc | 360 | (define-key map " " 'scroll-up-command) |
958614cf | 361 | (define-key map [?\S-\ ] 'scroll-down-command) |
ce3cefcc | 362 | (define-key map "\C-?" 'scroll-down-command) |
d445b3f8 | 363 | (define-key map "?" 'describe-mode) |
abef340a | 364 | (define-key map "h" 'describe-mode) |
d445b3f8 SM |
365 | (define-key map ">" 'end-of-buffer) |
366 | (define-key map "<" 'beginning-of-buffer) | |
367 | (define-key map "g" 'revert-buffer) | |
368 | map)) | |
1d2b0303 | 369 | |
d445b3f8 SM |
370 | (put 'special-mode 'mode-class 'special) |
371 | (define-derived-mode special-mode nil "Special" | |
372 | "Parent major mode from which special major modes should inherit." | |
373 | (setq buffer-read-only t)) | |
374 | ||
93be67de KH |
375 | ;; Making and deleting lines. |
376 | ||
28fab7b5 GM |
377 | (defvar hard-newline (propertize "\n" 'hard t 'rear-nonsticky '(hard)) |
378 | "Propertized string representing a hard newline character.") | |
4ea0018b | 379 | |
914b7f98 | 380 | (defun newline (&optional arg interactive) |
d133d835 | 381 | "Insert a newline, and move to left margin of the new line if it's blank. |
9fc9a531 | 382 | If option `use-hard-newlines' is non-nil, the newline is marked with the |
058d4999 | 383 | text-property `hard'. |
76c64e24 | 384 | With ARG, insert that many newlines. |
b51ad4cf RS |
385 | |
386 | To turn off indentation by this command, disable Electric Indent mode | |
387 | \(see \\[electric-indent-mode]). | |
388 | ||
389 | Calls `auto-fill-function' if the current column number is greater | |
914b7f98 | 390 | than the value of `fill-column' and ARG is nil. |
b51ad4cf | 391 | A non-nil INTERACTIVE argument means to run the `post-self-insert-hook'." |
914b7f98 | 392 | (interactive "*P\np") |
4c4cbf11 | 393 | (barf-if-buffer-read-only) |
e5eddfd1 SM |
394 | ;; Call self-insert so that auto-fill, abbrev expansion etc. happens. |
395 | ;; Set last-command-event to tell self-insert what to insert. | |
396 | (let* ((was-page-start (and (bolp) (looking-at page-delimiter))) | |
397 | (beforepos (point)) | |
398 | (last-command-event ?\n) | |
399 | ;; Don't auto-fill if we have a numeric argument. | |
400 | (auto-fill-function (if arg nil auto-fill-function)) | |
401 | (postproc | |
402 | ;; Do the rest in post-self-insert-hook, because we want to do it | |
403 | ;; *before* other functions on that hook. | |
404 | (lambda () | |
405 | ;; Mark the newline(s) `hard'. | |
406 | (if use-hard-newlines | |
407 | (set-hard-newline-properties | |
408 | (- (point) (prefix-numeric-value arg)) (point))) | |
409 | ;; If the newline leaves the previous line blank, and we | |
410 | ;; have a left margin, delete that from the blank line. | |
411 | (save-excursion | |
412 | (goto-char beforepos) | |
413 | (beginning-of-line) | |
414 | (and (looking-at "[ \t]$") | |
415 | (> (current-left-margin) 0) | |
416 | (delete-region (point) | |
417 | (line-end-position)))) | |
418 | ;; Indent the line after the newline, except in one case: | |
419 | ;; when we added the newline at the beginning of a line which | |
420 | ;; starts a page. | |
421 | (or was-page-start | |
422 | (move-to-left-margin nil t))))) | |
914b7f98 SM |
423 | (if (not interactive) |
424 | ;; FIXME: For non-interactive uses, many calls actually just want | |
425 | ;; (insert "\n"), so maybe we should do just that, so as to avoid | |
426 | ;; the risk of filling or running abbrevs unexpectedly. | |
427 | (let ((post-self-insert-hook (list postproc))) | |
e5eddfd1 | 428 | (self-insert-command (prefix-numeric-value arg))) |
914b7f98 SM |
429 | (unwind-protect |
430 | (progn | |
431 | (add-hook 'post-self-insert-hook postproc) | |
432 | (self-insert-command (prefix-numeric-value arg))) | |
433 | ;; We first used let-binding to protect the hook, but that was naive | |
434 | ;; since add-hook affects the symbol-default value of the variable, | |
435 | ;; whereas the let-binding might only protect the buffer-local value. | |
436 | (remove-hook 'post-self-insert-hook postproc)))) | |
30bb9754 BG |
437 | nil) |
438 | ||
55741b46 RS |
439 | (defun set-hard-newline-properties (from to) |
440 | (let ((sticky (get-text-property from 'rear-nonsticky))) | |
441 | (put-text-property from to 'hard 't) | |
442 | ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list | |
443 | (if (and (listp sticky) (not (memq 'hard sticky))) | |
444 | (put-text-property from (point) 'rear-nonsticky | |
445 | (cons 'hard sticky))))) | |
eaae8106 | 446 | |
e249a6d8 | 447 | (defun open-line (n) |
ff1fbe3e | 448 | "Insert a newline and leave point before it. |
f33321ad JB |
449 | If there is a fill prefix and/or a `left-margin', insert them |
450 | on the new line if the line would have been blank. | |
616ed245 | 451 | With arg N, insert N newlines." |
2076c87c | 452 | (interactive "*p") |
616ed245 | 453 | (let* ((do-fill-prefix (and fill-prefix (bolp))) |
3db1e3b5 | 454 | (do-left-margin (and (bolp) (> (current-left-margin) 0))) |
c156a63b | 455 | (loc (point-marker)) |
207d7545 GM |
456 | ;; Don't expand an abbrev before point. |
457 | (abbrev-mode nil)) | |
e249a6d8 | 458 | (newline n) |
d133d835 | 459 | (goto-char loc) |
e249a6d8 | 460 | (while (> n 0) |
d133d835 RS |
461 | (cond ((bolp) |
462 | (if do-left-margin (indent-to (current-left-margin))) | |
463 | (if do-fill-prefix (insert-and-inherit fill-prefix)))) | |
464 | (forward-line 1) | |
e249a6d8 | 465 | (setq n (1- n))) |
d133d835 RS |
466 | (goto-char loc) |
467 | (end-of-line))) | |
2076c87c | 468 | |
da7d231b KS |
469 | (defun split-line (&optional arg) |
470 | "Split current line, moving portion beyond point vertically down. | |
471 | If the current line starts with `fill-prefix', insert it on the new | |
f33321ad | 472 | line as well. With prefix ARG, don't insert `fill-prefix' on new line. |
da7d231b | 473 | |
e249a6d8 | 474 | When called from Lisp code, ARG may be a prefix string to copy." |
da7d231b | 475 | (interactive "*P") |
2076c87c | 476 | (skip-chars-forward " \t") |
d77bbdc9 RS |
477 | (let* ((col (current-column)) |
478 | (pos (point)) | |
479 | ;; What prefix should we check for (nil means don't). | |
480 | (prefix (cond ((stringp arg) arg) | |
481 | (arg nil) | |
482 | (t fill-prefix))) | |
483 | ;; Does this line start with it? | |
484 | (have-prfx (and prefix | |
485 | (save-excursion | |
486 | (beginning-of-line) | |
487 | (looking-at (regexp-quote prefix)))))) | |
28191e20 | 488 | (newline 1) |
d77bbdc9 | 489 | (if have-prfx (insert-and-inherit prefix)) |
2076c87c JB |
490 | (indent-to col 0) |
491 | (goto-char pos))) | |
492 | ||
2076c87c JB |
493 | (defun delete-indentation (&optional arg) |
494 | "Join this line to previous and fix up whitespace at join. | |
ccc58657 | 495 | If there is a fill prefix, delete it from the beginning of this line. |
2076c87c JB |
496 | With argument, join this line to following line." |
497 | (interactive "*P") | |
498 | (beginning-of-line) | |
499 | (if arg (forward-line 1)) | |
500 | (if (eq (preceding-char) ?\n) | |
501 | (progn | |
502 | (delete-region (point) (1- (point))) | |
ccc58657 RS |
503 | ;; If the second line started with the fill prefix, |
504 | ;; delete the prefix. | |
505 | (if (and fill-prefix | |
01b8e020 | 506 | (<= (+ (point) (length fill-prefix)) (point-max)) |
ccc58657 RS |
507 | (string= fill-prefix |
508 | (buffer-substring (point) | |
509 | (+ (point) (length fill-prefix))))) | |
510 | (delete-region (point) (+ (point) (length fill-prefix)))) | |
2076c87c JB |
511 | (fixup-whitespace)))) |
512 | ||
fc025090 | 513 | (defalias 'join-line #'delete-indentation) ; easier to find |
eaae8106 | 514 | |
2076c87c JB |
515 | (defun delete-blank-lines () |
516 | "On blank line, delete all surrounding blank lines, leaving just one. | |
517 | On isolated blank line, delete that one. | |
6d30d416 | 518 | On nonblank line, delete any immediately following blank lines." |
2076c87c JB |
519 | (interactive "*") |
520 | (let (thisblank singleblank) | |
521 | (save-excursion | |
522 | (beginning-of-line) | |
523 | (setq thisblank (looking-at "[ \t]*$")) | |
70e14c01 | 524 | ;; Set singleblank if there is just one blank line here. |
2076c87c JB |
525 | (setq singleblank |
526 | (and thisblank | |
527 | (not (looking-at "[ \t]*\n[ \t]*$")) | |
528 | (or (bobp) | |
529 | (progn (forward-line -1) | |
530 | (not (looking-at "[ \t]*$"))))))) | |
70e14c01 | 531 | ;; Delete preceding blank lines, and this one too if it's the only one. |
2076c87c JB |
532 | (if thisblank |
533 | (progn | |
534 | (beginning-of-line) | |
535 | (if singleblank (forward-line 1)) | |
536 | (delete-region (point) | |
537 | (if (re-search-backward "[^ \t\n]" nil t) | |
538 | (progn (forward-line 1) (point)) | |
539 | (point-min))))) | |
70e14c01 JB |
540 | ;; Delete following blank lines, unless the current line is blank |
541 | ;; and there are no following blank lines. | |
2076c87c JB |
542 | (if (not (and thisblank singleblank)) |
543 | (save-excursion | |
544 | (end-of-line) | |
545 | (forward-line 1) | |
546 | (delete-region (point) | |
547 | (if (re-search-forward "[^ \t\n]" nil t) | |
548 | (progn (beginning-of-line) (point)) | |
70e14c01 JB |
549 | (point-max))))) |
550 | ;; Handle the special case where point is followed by newline and eob. | |
551 | ;; Delete the line, leaving point at eob. | |
552 | (if (looking-at "^[ \t]*\n\\'") | |
553 | (delete-region (point) (point-max))))) | |
2076c87c | 554 | |
345a2258 CY |
555 | (defcustom delete-trailing-lines t |
556 | "If non-nil, \\[delete-trailing-whitespace] deletes trailing lines. | |
557 | Trailing lines are deleted only if `delete-trailing-whitespace' | |
558 | is called on the entire buffer (rather than an active region)." | |
559 | :type 'boolean | |
560 | :group 'editing | |
2a1e2476 | 561 | :version "24.3") |
345a2258 | 562 | |
25833f5e | 563 | (defun delete-trailing-whitespace (&optional start end) |
345a2258 CY |
564 | "Delete trailing whitespace between START and END. |
565 | If called interactively, START and END are the start/end of the | |
566 | region if the mark is active, or of the buffer's accessible | |
567 | portion if the mark is inactive. | |
568 | ||
569 | This command deletes whitespace characters after the last | |
570 | non-whitespace character in each line between START and END. It | |
571 | does not consider formfeed characters to be whitespace. | |
572 | ||
573 | If this command acts on the entire buffer (i.e. if called | |
574 | interactively with the mark inactive, or called from Lisp with | |
575 | END nil), it also deletes all trailing lines at the end of the | |
576 | buffer if the variable `delete-trailing-lines' is non-nil." | |
25833f5e DD |
577 | (interactive (progn |
578 | (barf-if-buffer-read-only) | |
579 | (if (use-region-p) | |
580 | (list (region-beginning) (region-end)) | |
581 | (list nil nil)))) | |
eaae8106 SS |
582 | (save-match-data |
583 | (save-excursion | |
25833f5e DD |
584 | (let ((end-marker (copy-marker (or end (point-max)))) |
585 | (start (or start (point-min)))) | |
586 | (goto-char start) | |
587 | (while (re-search-forward "\\s-$" end-marker t) | |
db4e950d | 588 | (skip-syntax-backward "-" (line-beginning-position)) |
25833f5e | 589 | ;; Don't delete formfeeds, even if they are considered whitespace. |
db4e950d SM |
590 | (if (looking-at-p ".*\f") |
591 | (goto-char (match-end 0))) | |
25833f5e | 592 | (delete-region (point) (match-end 0))) |
db4e950d SM |
593 | ;; Delete trailing empty lines. |
594 | (goto-char end-marker) | |
595 | (when (and (not end) | |
345a2258 | 596 | delete-trailing-lines |
db4e950d | 597 | ;; Really the end of buffer. |
a97dc380 | 598 | (= (point-max) (1+ (buffer-size))) |
88d9610c | 599 | (<= (skip-chars-backward "\n") -2)) |
db4e950d | 600 | (delete-region (1+ (point)) end-marker)) |
f346fd6b MA |
601 | (set-marker end-marker nil)))) |
602 | ;; Return nil for the benefit of `write-file-functions'. | |
603 | nil) | |
eaae8106 | 604 | |
2076c87c JB |
605 | (defun newline-and-indent () |
606 | "Insert a newline, then indent according to major mode. | |
ff1fbe3e | 607 | Indentation is done using the value of `indent-line-function'. |
2076c87c | 608 | In programming language modes, this is the same as TAB. |
ff1fbe3e | 609 | In some text modes, where TAB inserts a tab, this command indents to the |
eed5698b | 610 | column specified by the function `current-left-margin'." |
2076c87c | 611 | (interactive "*") |
5ff4ba3d | 612 | (delete-horizontal-space t) |
3b8d5131 | 613 | (newline 1 t) |
2076c87c JB |
614 | (indent-according-to-mode)) |
615 | ||
616 | (defun reindent-then-newline-and-indent () | |
617 | "Reindent current line, insert newline, then indent the new line. | |
618 | Indentation of both lines is done according to the current major mode, | |
ff1fbe3e | 619 | which means calling the current value of `indent-line-function'. |
2076c87c JB |
620 | In programming language modes, this is the same as TAB. |
621 | In some text modes, where TAB inserts a tab, this indents to the | |
eed5698b | 622 | column specified by the function `current-left-margin'." |
2076c87c | 623 | (interactive "*") |
e1e04350 SM |
624 | (let ((pos (point))) |
625 | ;; Be careful to insert the newline before indenting the line. | |
626 | ;; Otherwise, the indentation might be wrong. | |
627 | (newline) | |
628 | (save-excursion | |
629 | (goto-char pos) | |
eb3d6c67 SM |
630 | ;; We are at EOL before the call to indent-according-to-mode, and |
631 | ;; after it we usually are as well, but not always. We tried to | |
632 | ;; address it with `save-excursion' but that uses a normal marker | |
633 | ;; whereas we need `move after insertion', so we do the save/restore | |
634 | ;; by hand. | |
635 | (setq pos (copy-marker pos t)) | |
636 | (indent-according-to-mode) | |
637 | (goto-char pos) | |
638 | ;; Remove the trailing white-space after indentation because | |
639 | ;; indentation may introduce the whitespace. | |
6b61353c | 640 | (delete-horizontal-space t)) |
e1e04350 | 641 | (indent-according-to-mode))) |
eaae8106 | 642 | |
53b39e89 SM |
643 | (defcustom read-quoted-char-radix 8 |
644 | "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'. | |
645 | Legitimate radix values are 8, 10 and 16." | |
646 | :type '(choice (const 8) (const 10) (const 16)) | |
647 | :group 'editing-basics) | |
648 | ||
649 | (defun read-quoted-char (&optional prompt) | |
650 | "Like `read-char', but do not allow quitting. | |
651 | Also, if the first character read is an octal digit, | |
652 | we read any number of octal digits and return the | |
653 | specified character code. Any nondigit terminates the sequence. | |
654 | If the terminator is RET, it is discarded; | |
655 | any other terminator is used itself as input. | |
656 | ||
657 | The optional argument PROMPT specifies a string to use to prompt the user. | |
658 | The variable `read-quoted-char-radix' controls which radix to use | |
659 | for numeric input." | |
660 | (let ((message-log-max nil) done (first t) (code 0) translated) | |
661 | (while (not done) | |
662 | (let ((inhibit-quit first) | |
663 | ;; Don't let C-h get the help message--only help function keys. | |
664 | (help-char nil) | |
665 | (help-form | |
666 | "Type the special character you want to use, | |
667 | or the octal character code. | |
668 | RET terminates the character code and is discarded; | |
669 | any other non-digit terminates the character code and is then used as input.")) | |
670 | (setq translated (read-key (and prompt (format "%s-" prompt)))) | |
671 | (if inhibit-quit (setq quit-flag nil))) | |
672 | (if (integerp translated) | |
673 | (setq translated (char-resolve-modifiers translated))) | |
674 | (cond ((null translated)) | |
675 | ((not (integerp translated)) | |
676 | (setq unread-command-events | |
677 | (listify-key-sequence (this-single-command-raw-keys)) | |
678 | done t)) | |
679 | ((/= (logand translated ?\M-\^@) 0) | |
680 | ;; Turn a meta-character into a character with the 0200 bit set. | |
681 | (setq code (logior (logand translated (lognot ?\M-\^@)) 128) | |
682 | done t)) | |
683 | ((and (<= ?0 translated) | |
684 | (< translated (+ ?0 (min 10 read-quoted-char-radix)))) | |
685 | (setq code (+ (* code read-quoted-char-radix) (- translated ?0))) | |
686 | (and prompt (setq prompt (message "%s %c" prompt translated)))) | |
687 | ((and (<= ?a (downcase translated)) | |
688 | (< (downcase translated) | |
689 | (+ ?a -10 (min 36 read-quoted-char-radix)))) | |
690 | (setq code (+ (* code read-quoted-char-radix) | |
691 | (+ 10 (- (downcase translated) ?a)))) | |
692 | (and prompt (setq prompt (message "%s %c" prompt translated)))) | |
693 | ((and (not first) (eq translated ?\C-m)) | |
694 | (setq done t)) | |
695 | ((not first) | |
696 | (setq unread-command-events | |
697 | (listify-key-sequence (this-single-command-raw-keys)) | |
698 | done t)) | |
699 | (t (setq code translated | |
700 | done t))) | |
701 | (setq first nil)) | |
702 | code)) | |
703 | ||
93be67de KH |
704 | (defun quoted-insert (arg) |
705 | "Read next input character and insert it. | |
706 | This is useful for inserting control characters. | |
5626c14e | 707 | With argument, insert ARG copies of the character. |
2076c87c | 708 | |
93be67de KH |
709 | If the first character you type after this command is an octal digit, |
710 | you should type a sequence of octal digits which specify a character code. | |
711 | Any nondigit terminates the sequence. If the terminator is a RET, | |
712 | it is discarded; any other terminator is used itself as input. | |
713 | The variable `read-quoted-char-radix' specifies the radix for this feature; | |
714 | set it to 10 or 16 to use decimal or hex instead of octal. | |
dff7d67f | 715 | |
93be67de KH |
716 | In overwrite mode, this function inserts the character anyway, and |
717 | does not handle octal digits specially. This means that if you use | |
718 | overwrite as your normal editing mode, you can use this function to | |
719 | insert characters when necessary. | |
dff7d67f | 720 | |
93be67de KH |
721 | In binary overwrite mode, this function does overwrite, and octal |
722 | digits are interpreted as a character code. This is intended to be | |
723 | useful for editing binary files." | |
724 | (interactive "*p") | |
a6c39c14 EZ |
725 | (let* ((char |
726 | ;; Avoid "obsolete" warnings for translation-table-for-input. | |
727 | (with-no-warnings | |
728 | (let (translation-table-for-input input-method-function) | |
729 | (if (or (not overwrite-mode) | |
730 | (eq overwrite-mode 'overwrite-mode-binary)) | |
731 | (read-quoted-char) | |
732 | (read-char)))))) | |
0e3269e5 JL |
733 | ;; This used to assume character codes 0240 - 0377 stand for |
734 | ;; characters in some single-byte character set, and converted them | |
735 | ;; to Emacs characters. But in 23.1 this feature is deprecated | |
736 | ;; in favor of inserting the corresponding Unicode characters. | |
737 | ;; (if (and enable-multibyte-characters | |
738 | ;; (>= char ?\240) | |
739 | ;; (<= char ?\377)) | |
740 | ;; (setq char (unibyte-char-to-multibyte char))) | |
93be67de KH |
741 | (if (> arg 0) |
742 | (if (eq overwrite-mode 'overwrite-mode-binary) | |
743 | (delete-char arg))) | |
744 | (while (> arg 0) | |
745 | (insert-and-inherit char) | |
746 | (setq arg (1- arg))))) | |
eaae8106 | 747 | |
6b61353c | 748 | (defun forward-to-indentation (&optional arg) |
93be67de | 749 | "Move forward ARG lines and position at first nonblank character." |
109cfe4e | 750 | (interactive "^p") |
6b61353c | 751 | (forward-line (or arg 1)) |
93be67de | 752 | (skip-chars-forward " \t")) |
cc2b2b6c | 753 | |
6b61353c | 754 | (defun backward-to-indentation (&optional arg) |
93be67de | 755 | "Move backward ARG lines and position at first nonblank character." |
109cfe4e | 756 | (interactive "^p") |
6b61353c | 757 | (forward-line (- (or arg 1))) |
93be67de | 758 | (skip-chars-forward " \t")) |
2076c87c | 759 | |
93be67de KH |
760 | (defun back-to-indentation () |
761 | "Move point to the first non-whitespace character on this line." | |
109cfe4e | 762 | (interactive "^") |
93be67de | 763 | (beginning-of-line 1) |
1e96c007 | 764 | (skip-syntax-forward " " (line-end-position)) |
b9863466 RS |
765 | ;; Move back over chars that have whitespace syntax but have the p flag. |
766 | (backward-prefix-chars)) | |
93be67de KH |
767 | |
768 | (defun fixup-whitespace () | |
769 | "Fixup white space between objects around point. | |
770 | Leave one space or none, according to the context." | |
771 | (interactive "*") | |
772 | (save-excursion | |
773 | (delete-horizontal-space) | |
774 | (if (or (looking-at "^\\|\\s)") | |
775 | (save-excursion (forward-char -1) | |
776 | (looking-at "$\\|\\s(\\|\\s'"))) | |
777 | nil | |
f33321ad | 778 | (insert ?\s)))) |
93be67de | 779 | |
5ff4ba3d MB |
780 | (defun delete-horizontal-space (&optional backward-only) |
781 | "Delete all spaces and tabs around point. | |
1cfcd2db | 782 | If BACKWARD-ONLY is non-nil, only delete them before point." |
a168699d | 783 | (interactive "*P") |
9ab59a1a MB |
784 | (let ((orig-pos (point))) |
785 | (delete-region | |
786 | (if backward-only | |
787 | orig-pos | |
788 | (progn | |
789 | (skip-chars-forward " \t") | |
790 | (constrain-to-field nil orig-pos t))) | |
5ff4ba3d | 791 | (progn |
9ab59a1a MB |
792 | (skip-chars-backward " \t") |
793 | (constrain-to-field nil orig-pos))))) | |
93be67de | 794 | |
68c16b59 | 795 | (defun just-one-space (&optional n) |
88b5a757 | 796 | "Delete all spaces and tabs around point, leaving one space (or N spaces). |
6ce49f24 | 797 | If N is negative, delete newlines as well, leaving -N spaces." |
56abefac | 798 | (interactive "*p") |
ad4de702 MN |
799 | (cycle-spacing n nil t)) |
800 | ||
801 | (defvar cycle-spacing--context nil | |
802 | "Store context used in consecutive calls to `cycle-spacing' command. | |
803 | The first time this function is run, it saves the original point | |
804 | position and original spacing around the point in this | |
805 | variable.") | |
806 | ||
807 | (defun cycle-spacing (&optional n preserve-nl-back single-shot) | |
808 | "Manipulate spaces around the point in a smart way. | |
809 | ||
810 | When run as an interactive command, the first time it's called | |
811 | in a sequence, deletes all spaces and tabs around point leaving | |
812 | one (or N spaces). If this does not change content of the | |
813 | buffer, skips to the second step: | |
814 | ||
815 | When run for the second time in a sequence, deletes all the | |
816 | spaces it has previously inserted. | |
817 | ||
818 | When run for the third time, returns the whitespace and point in | |
819 | a state encountered when it had been run for the first time. | |
820 | ||
821 | For example, if buffer contains \"foo ^ bar\" with \"^\" denoting the | |
822 | point, calling `cycle-spacing' command will replace two spaces with | |
823 | a single space, calling it again immediately after, will remove all | |
824 | spaces, and calling it for the third time will bring two spaces back | |
825 | together. | |
826 | ||
827 | If N is negative, delete newlines as well. However, if | |
828 | PRESERVE-NL-BACK is t new line characters prior to the point | |
829 | won't be removed. | |
830 | ||
831 | If SINGLE-SHOT is non-nil, will only perform the first step. In | |
832 | other words, it will work just like `just-one-space' command." | |
833 | (interactive "*p") | |
834 | (let ((orig-pos (point)) | |
835 | (skip-characters (if (and n (< n 0)) " \t\n\r" " \t")) | |
836 | (n (abs (or n 1)))) | |
837 | (skip-chars-backward (if preserve-nl-back " \t" skip-characters)) | |
9ab59a1a | 838 | (constrain-to-field nil orig-pos) |
ad4de702 MN |
839 | (cond |
840 | ;; Command run for the first time or single-shot is non-nil. | |
841 | ((or single-shot | |
842 | (not (equal last-command this-command)) | |
843 | (not cycle-spacing--context)) | |
844 | (let* ((start (point)) | |
845 | (n (- n (skip-chars-forward " " (+ n (point))))) | |
846 | (mid (point)) | |
847 | (end (progn | |
848 | (skip-chars-forward skip-characters) | |
849 | (constrain-to-field nil orig-pos t)))) | |
850 | (setq cycle-spacing--context ;; Save for later. | |
851 | ;; Special handling for case where there was no space at all. | |
852 | (unless (= start end) | |
853 | (cons orig-pos (buffer-substring start (point))))) | |
854 | ;; If this run causes no change in buffer content, delete all spaces, | |
d9c287e5 | 855 | ;; otherwise delete all excess spaces. |
ad4de702 MN |
856 | (delete-region (if (and (not single-shot) (zerop n) (= mid end)) |
857 | start mid) end) | |
1e77b18e | 858 | (insert (make-string n ?\s)))) |
ad4de702 MN |
859 | |
860 | ;; Command run for the second time. | |
861 | ((not (equal orig-pos (point))) | |
862 | (delete-region (point) orig-pos)) | |
863 | ||
864 | ;; Command run for the third time. | |
865 | (t | |
866 | (insert (cdr cycle-spacing--context)) | |
867 | (goto-char (car cycle-spacing--context)) | |
868 | (setq cycle-spacing--context nil))))) | |
2d88b556 | 869 | \f |
2076c87c | 870 | (defun beginning-of-buffer (&optional arg) |
8d9f4291 | 871 | "Move point to the beginning of the buffer. |
a416e7ef | 872 | With numeric arg N, put point N/10 of the way from the beginning. |
8d9f4291 CY |
873 | If the buffer is narrowed, this command uses the beginning of the |
874 | accessible part of the buffer. | |
c66587fe | 875 | |
8d9f4291 CY |
876 | If Transient Mark mode is disabled, leave mark at previous |
877 | position, unless a \\[universal-argument] prefix is supplied. | |
ff1fbe3e RS |
878 | |
879 | Don't use this command in Lisp programs! | |
8d9f4291 | 880 | \(goto-char (point-min)) is faster." |
109cfe4e | 881 | (interactive "^P") |
24199fe7 | 882 | (or (consp arg) |
d34c311a | 883 | (region-active-p) |
705a5933 | 884 | (push-mark)) |
c66587fe | 885 | (let ((size (- (point-max) (point-min)))) |
a416e7ef | 886 | (goto-char (if (and arg (not (consp arg))) |
c66587fe RS |
887 | (+ (point-min) |
888 | (if (> size 10000) | |
889 | ;; Avoid overflow for large buffer sizes! | |
890 | (* (prefix-numeric-value arg) | |
891 | (/ size 10)) | |
892 | (/ (+ 10 (* size (prefix-numeric-value arg))) 10))) | |
893 | (point-min)))) | |
d7e7ecd7 | 894 | (if (and arg (not (consp arg))) (forward-line 1))) |
3e2fb4db GM |
895 | (put 'beginning-of-buffer 'interactive-only |
896 | "use `(goto-char (point-min))' instead.") | |
2076c87c JB |
897 | |
898 | (defun end-of-buffer (&optional arg) | |
8d9f4291 | 899 | "Move point to the end of the buffer. |
a416e7ef | 900 | With numeric arg N, put point N/10 of the way from the end. |
8d9f4291 CY |
901 | If the buffer is narrowed, this command uses the end of the |
902 | accessible part of the buffer. | |
c66587fe | 903 | |
8d9f4291 CY |
904 | If Transient Mark mode is disabled, leave mark at previous |
905 | position, unless a \\[universal-argument] prefix is supplied. | |
ff1fbe3e RS |
906 | |
907 | Don't use this command in Lisp programs! | |
8d9f4291 | 908 | \(goto-char (point-max)) is faster." |
109cfe4e | 909 | (interactive "^P") |
d34c311a | 910 | (or (consp arg) (region-active-p) (push-mark)) |
c66587fe | 911 | (let ((size (- (point-max) (point-min)))) |
a416e7ef | 912 | (goto-char (if (and arg (not (consp arg))) |
c66587fe RS |
913 | (- (point-max) |
914 | (if (> size 10000) | |
915 | ;; Avoid overflow for large buffer sizes! | |
916 | (* (prefix-numeric-value arg) | |
917 | (/ size 10)) | |
918 | (/ (* size (prefix-numeric-value arg)) 10))) | |
919 | (point-max)))) | |
3a801d0c ER |
920 | ;; If we went to a place in the middle of the buffer, |
921 | ;; adjust it to the beginning of a line. | |
d7e7ecd7 | 922 | (cond ((and arg (not (consp arg))) (forward-line 1)) |
c9586acc SM |
923 | ((and (eq (current-buffer) (window-buffer)) |
924 | (> (point) (window-end nil t))) | |
314808dc GM |
925 | ;; If the end of the buffer is not already on the screen, |
926 | ;; then scroll specially to put it near, but not at, the bottom. | |
927 | (overlay-recenter (point)) | |
928 | (recenter -3)))) | |
3e2fb4db | 929 | (put 'end-of-buffer 'interactive-only "use `(goto-char (point-max))' instead.") |
2076c87c | 930 | |
b9229673 CY |
931 | (defcustom delete-active-region t |
932 | "Whether single-char deletion commands delete an active region. | |
933 | This has an effect only if Transient Mark mode is enabled, and | |
934 | affects `delete-forward-char' and `delete-backward-char', though | |
935 | not `delete-char'. | |
936 | ||
937 | If the value is the symbol `kill', the active region is killed | |
938 | instead of deleted." | |
939 | :type '(choice (const :tag "Delete active region" t) | |
940 | (const :tag "Kill active region" kill) | |
941 | (const :tag "Do ordinary deletion" nil)) | |
d55486c7 | 942 | :group 'killing |
b9229673 CY |
943 | :version "24.1") |
944 | ||
3472b6c6 SM |
945 | (defvar region-extract-function |
946 | (lambda (delete) | |
947 | (when (region-beginning) | |
948 | (if (eq delete 'delete-only) | |
949 | (delete-region (region-beginning) (region-end)) | |
950 | (filter-buffer-substring (region-beginning) (region-end) delete)))) | |
951 | "Function to get the region's content. | |
952 | Called with one argument DELETE. | |
953 | If DELETE is `delete-only', then only delete the region and the return value | |
954 | is undefined. If DELETE is nil, just return the content as a string. | |
955 | If anything else, delete the region and return its content as a string.") | |
956 | ||
b9229673 CY |
957 | (defun delete-backward-char (n &optional killflag) |
958 | "Delete the previous N characters (following if N is negative). | |
959 | If Transient Mark mode is enabled, the mark is active, and N is 1, | |
960 | delete the text in the region and deactivate the mark instead. | |
9fc9a531 | 961 | To disable this, set option `delete-active-region' to nil. |
b9229673 CY |
962 | |
963 | Optional second arg KILLFLAG, if non-nil, means to kill (save in | |
964 | kill ring) instead of delete. Interactively, N is the prefix | |
965 | arg, and KILLFLAG is set if N is explicitly specified. | |
966 | ||
967 | In Overwrite mode, single character backward deletion may replace | |
968 | tabs with spaces so as to back over columns, unless point is at | |
969 | the end of the line." | |
970 | (interactive "p\nP") | |
971 | (unless (integerp n) | |
972 | (signal 'wrong-type-argument (list 'integerp n))) | |
973 | (cond ((and (use-region-p) | |
974 | delete-active-region | |
975 | (= n 1)) | |
976 | ;; If a region is active, kill or delete it. | |
977 | (if (eq delete-active-region 'kill) | |
3472b6c6 SM |
978 | (kill-region (region-beginning) (region-end) 'region) |
979 | (funcall region-extract-function 'delete-only))) | |
b9229673 CY |
980 | ;; In Overwrite mode, maybe untabify while deleting |
981 | ((null (or (null overwrite-mode) | |
982 | (<= n 0) | |
983 | (memq (char-before) '(?\t ?\n)) | |
984 | (eobp) | |
985 | (eq (char-after) ?\n))) | |
06b60517 JB |
986 | (let ((ocol (current-column))) |
987 | (delete-char (- n) killflag) | |
b9229673 CY |
988 | (save-excursion |
989 | (insert-char ?\s (- ocol (current-column)) nil)))) | |
990 | ;; Otherwise, do simple deletion. | |
991 | (t (delete-char (- n) killflag)))) | |
3e2fb4db | 992 | (put 'delete-backward-char 'interactive-only 'delete-char) |
b9229673 CY |
993 | |
994 | (defun delete-forward-char (n &optional killflag) | |
4c5130d6 | 995 | "Delete the following N characters (previous if N is negative). |
b9229673 CY |
996 | If Transient Mark mode is enabled, the mark is active, and N is 1, |
997 | delete the text in the region and deactivate the mark instead. | |
9fc9a531 | 998 | To disable this, set variable `delete-active-region' to nil. |
b9229673 CY |
999 | |
1000 | Optional second arg KILLFLAG non-nil means to kill (save in kill | |
1001 | ring) instead of delete. Interactively, N is the prefix arg, and | |
1002 | KILLFLAG is set if N was explicitly specified." | |
1003 | (interactive "p\nP") | |
1004 | (unless (integerp n) | |
1005 | (signal 'wrong-type-argument (list 'integerp n))) | |
1006 | (cond ((and (use-region-p) | |
1007 | delete-active-region | |
1008 | (= n 1)) | |
1009 | ;; If a region is active, kill or delete it. | |
1010 | (if (eq delete-active-region 'kill) | |
3472b6c6 SM |
1011 | (kill-region (region-beginning) (region-end) 'region) |
1012 | (funcall region-extract-function 'delete-only))) | |
1013 | ||
b9229673 CY |
1014 | ;; Otherwise, do simple deletion. |
1015 | (t (delete-char n killflag)))) | |
1016 | ||
2076c87c | 1017 | (defun mark-whole-buffer () |
70e14c01 | 1018 | "Put point at beginning and mark at end of buffer. |
947cd66b | 1019 | If narrowing is in effect, only uses the accessible part of the buffer. |
70e14c01 JB |
1020 | You probably should not use this function in Lisp programs; |
1021 | it is usually a mistake for a Lisp function to use any subroutine | |
1022 | that uses or sets the mark." | |
2076c87c JB |
1023 | (interactive) |
1024 | (push-mark (point)) | |
fd0f4056 | 1025 | (push-mark (point-max) nil t) |
2076c87c | 1026 | (goto-char (point-min))) |
2d88b556 | 1027 | \f |
eaae8106 | 1028 | |
93be67de KH |
1029 | ;; Counting lines, one way or another. |
1030 | ||
9af967bd | 1031 | (defun goto-line (line &optional buffer) |
397a688f CY |
1032 | "Go to LINE, counting from line 1 at beginning of buffer. |
1033 | If called interactively, a numeric prefix argument specifies | |
1034 | LINE; without a numeric prefix argument, read LINE from the | |
1035 | minibuffer. | |
9af967bd | 1036 | |
397a688f CY |
1037 | If optional argument BUFFER is non-nil, switch to that buffer and |
1038 | move to line LINE there. If called interactively with \\[universal-argument] | |
1039 | as argument, BUFFER is the most recently selected other buffer. | |
1040 | ||
1041 | Prior to moving point, this function sets the mark (without | |
1042 | activating it), unless Transient Mark mode is enabled and the | |
1043 | mark is already active. | |
a5785534 SM |
1044 | |
1045 | This function is usually the wrong thing to use in a Lisp program. | |
1046 | What you probably want instead is something like: | |
397a688f CY |
1047 | (goto-char (point-min)) |
1048 | (forward-line (1- N)) | |
a5785534 SM |
1049 | If at all possible, an even better solution is to use char counts |
1050 | rather than line counts." | |
00a369ac RS |
1051 | (interactive |
1052 | (if (and current-prefix-arg (not (consp current-prefix-arg))) | |
1053 | (list (prefix-numeric-value current-prefix-arg)) | |
1054 | ;; Look for a default, a number in the buffer at point. | |
1055 | (let* ((default | |
1056 | (save-excursion | |
1057 | (skip-chars-backward "0-9") | |
1058 | (if (looking-at "[0-9]") | |
a514d856 JM |
1059 | (string-to-number |
1060 | (buffer-substring-no-properties | |
1061 | (point) | |
1062 | (progn (skip-chars-forward "0-9") | |
1063 | (point))))))) | |
00a369ac RS |
1064 | ;; Decide if we're switching buffers. |
1065 | (buffer | |
1066 | (if (consp current-prefix-arg) | |
1067 | (other-buffer (current-buffer) t))) | |
1068 | (buffer-prompt | |
1069 | (if buffer | |
1070 | (concat " in " (buffer-name buffer)) | |
1071 | ""))) | |
1072 | ;; Read the argument, offering that number (if any) as default. | |
a5dcc929 JL |
1073 | (list (read-number (format "Goto line%s: " buffer-prompt) |
1074 | (list default (line-number-at-pos))) | |
00a369ac RS |
1075 | buffer)))) |
1076 | ;; Switch to the desired buffer, one way or another. | |
1077 | (if buffer | |
1078 | (let ((window (get-buffer-window buffer))) | |
1079 | (if window (select-window window) | |
1080 | (switch-to-buffer-other-window buffer)))) | |
f564644b | 1081 | ;; Leave mark at previous position |
d34c311a | 1082 | (or (region-active-p) (push-mark)) |
00a369ac | 1083 | ;; Move to the specified line number in that buffer. |
93be67de KH |
1084 | (save-restriction |
1085 | (widen) | |
a5785534 | 1086 | (goto-char (point-min)) |
93be67de | 1087 | (if (eq selective-display t) |
9af967bd LK |
1088 | (re-search-forward "[\n\C-m]" nil 'end (1- line)) |
1089 | (forward-line (1- line))))) | |
3e2fb4db | 1090 | (put 'goto-line 'interactive-only 'forward-line) |
2076c87c | 1091 | |
e1293765 | 1092 | (defun count-words-region (start end &optional arg) |
10607bea | 1093 | "Count the number of words in the region. |
b2b0776e | 1094 | If called interactively, print a message reporting the number of |
e1293765 CY |
1095 | lines, words, and characters in the region (whether or not the |
1096 | region is active); with prefix ARG, report for the entire buffer | |
1097 | rather than the region. | |
1098 | ||
10607bea CY |
1099 | If called from Lisp, return the number of words between positions |
1100 | START and END." | |
e5c2edf7 CY |
1101 | (interactive (if current-prefix-arg |
1102 | (list nil nil current-prefix-arg) | |
1103 | (list (region-beginning) (region-end) nil))) | |
e1293765 CY |
1104 | (cond ((not (called-interactively-p 'any)) |
1105 | (count-words start end)) | |
1106 | (arg | |
1107 | (count-words--buffer-message)) | |
1108 | (t | |
1109 | (count-words--message "Region" start end)))) | |
10607bea CY |
1110 | |
1111 | (defun count-words (start end) | |
1112 | "Count words between START and END. | |
1113 | If called interactively, START and END are normally the start and | |
1114 | end of the buffer; but if the region is active, START and END are | |
1115 | the start and end of the region. Print a message reporting the | |
1116 | number of lines, words, and chars. | |
1117 | ||
1118 | If called from Lisp, return the number of words between START and | |
1119 | END, without printing any message." | |
1120 | (interactive (list nil nil)) | |
1121 | (cond ((not (called-interactively-p 'any)) | |
1122 | (let ((words 0)) | |
1123 | (save-excursion | |
1124 | (save-restriction | |
1125 | (narrow-to-region start end) | |
1126 | (goto-char (point-min)) | |
1127 | (while (forward-word 1) | |
1128 | (setq words (1+ words))))) | |
1129 | words)) | |
1130 | ((use-region-p) | |
1131 | (call-interactively 'count-words-region)) | |
1132 | (t | |
e1293765 CY |
1133 | (count-words--buffer-message)))) |
1134 | ||
1135 | (defun count-words--buffer-message () | |
1136 | (count-words--message | |
e5c2edf7 | 1137 | (if (buffer-narrowed-p) "Narrowed part of buffer" "Buffer") |
e1293765 | 1138 | (point-min) (point-max))) |
10607bea CY |
1139 | |
1140 | (defun count-words--message (str start end) | |
1141 | (let ((lines (count-lines start end)) | |
1142 | (words (count-words start end)) | |
1143 | (chars (- end start))) | |
1144 | (message "%s has %d line%s, %d word%s, and %d character%s." | |
1145 | str | |
1146 | lines (if (= lines 1) "" "s") | |
1147 | words (if (= words 1) "" "s") | |
1148 | chars (if (= chars 1) "" "s")))) | |
1149 | ||
1150 | (define-obsolete-function-alias 'count-lines-region 'count-words-region "24.1") | |
2076c87c JB |
1151 | |
1152 | (defun what-line () | |
2578be76 | 1153 | "Print the current buffer line number and narrowed line number of point." |
2076c87c | 1154 | (interactive) |
c6db81aa | 1155 | (let ((start (point-min)) |
6b61353c KH |
1156 | (n (line-number-at-pos))) |
1157 | (if (= start 1) | |
1158 | (message "Line %d" n) | |
1159 | (save-excursion | |
1160 | (save-restriction | |
1161 | (widen) | |
1162 | (message "line %d (narrowed line %d)" | |
1163 | (+ n (line-number-at-pos start) -1) n)))))) | |
2578be76 | 1164 | |
2076c87c JB |
1165 | (defun count-lines (start end) |
1166 | "Return number of lines between START and END. | |
1167 | This is usually the number of newlines between them, | |
ff1fbe3e | 1168 | but can be one more if START is not equal to END |
2076c87c | 1169 | and the greater of them is not at the start of a line." |
e406700d RS |
1170 | (save-excursion |
1171 | (save-restriction | |
1172 | (narrow-to-region start end) | |
1173 | (goto-char (point-min)) | |
1174 | (if (eq selective-display t) | |
1175 | (save-match-data | |
dde92ca6 | 1176 | (let ((done 0)) |
fece895e RT |
1177 | (while (re-search-forward "[\n\C-m]" nil t 40) |
1178 | (setq done (+ 40 done))) | |
1179 | (while (re-search-forward "[\n\C-m]" nil t 1) | |
1180 | (setq done (+ 1 done))) | |
1181 | (goto-char (point-max)) | |
1182 | (if (and (/= start end) | |
043efc41 RS |
1183 | (not (bolp))) |
1184 | (1+ done) | |
e406700d RS |
1185 | done))) |
1186 | (- (buffer-size) (forward-line (buffer-size))))))) | |
eaae8106 | 1187 | |
6b61353c KH |
1188 | (defun line-number-at-pos (&optional pos) |
1189 | "Return (narrowed) buffer line number at position POS. | |
79ffb765 RS |
1190 | If POS is nil, use current buffer location. |
1191 | Counting starts at (point-min), so the value refers | |
1192 | to the contents of the accessible portion of the buffer." | |
6b61353c KH |
1193 | (let ((opoint (or pos (point))) start) |
1194 | (save-excursion | |
1195 | (goto-char (point-min)) | |
1196 | (setq start (point)) | |
1197 | (goto-char opoint) | |
1198 | (forward-line 0) | |
1199 | (1+ (count-lines start (point)))))) | |
1200 | ||
d5d99b80 KH |
1201 | (defun what-cursor-position (&optional detail) |
1202 | "Print info on cursor position (on screen and within buffer). | |
e38dff0c | 1203 | Also describe the character after point, and give its character code |
c6fcc518 KH |
1204 | in octal, decimal and hex. |
1205 | ||
1206 | For a non-ASCII multibyte character, also give its encoding in the | |
1207 | buffer's selected coding system if the coding system encodes the | |
1208 | character safely. If the character is encoded into one byte, that | |
1209 | code is shown in hex. If the character is encoded into more than one | |
1210 | byte, just \"...\" is shown. | |
e5a902cf | 1211 | |
24dad5d5 | 1212 | In addition, with prefix argument, show details about that character |
0b69eec5 | 1213 | in *Help* buffer. See also the command `describe-char'." |
d5d99b80 | 1214 | (interactive "P") |
2076c87c | 1215 | (let* ((char (following-char)) |
71cc0b74 EZ |
1216 | (bidi-fixer |
1217 | (cond ((memq char '(?\x202a ?\x202b ?\x202d ?\x202e)) | |
1218 | ;; If the character is one of LRE, LRO, RLE, RLO, it | |
1219 | ;; will start a directional embedding, which could | |
1220 | ;; completely disrupt the rest of the line (e.g., RLO | |
1221 | ;; will display the rest of the line right-to-left). | |
1222 | ;; So we put an invisible PDF character after these | |
1223 | ;; characters, to end the embedding, which eliminates | |
1224 | ;; any effects on the rest of the line. | |
1225 | (propertize (string ?\x202c) 'invisible t)) | |
1226 | ;; Strong right-to-left characters cause reordering of | |
1227 | ;; the following numerical characters which show the | |
1228 | ;; codepoint, so append LRM to countermand that. | |
1229 | ((memq (get-char-code-property char 'bidi-class) '(R AL)) | |
1230 | (propertize (string ?\x200e) 'invisible t)) | |
1231 | (t | |
1232 | ""))) | |
2076c87c JB |
1233 | (beg (point-min)) |
1234 | (end (point-max)) | |
1235 | (pos (point)) | |
1236 | (total (buffer-size)) | |
1237 | (percent (if (> total 50000) | |
1238 | ;; Avoid overflow from multiplying by 100! | |
1239 | (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1)) | |
1240 | (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1)))) | |
1241 | (hscroll (if (= (window-hscroll) 0) | |
1242 | "" | |
1243 | (format " Hscroll=%d" (window-hscroll)))) | |
1244 | (col (current-column))) | |
1245 | (if (= pos end) | |
1246 | (if (or (/= beg 1) (/= end (1+ total))) | |
a17a79c0 | 1247 | (message "point=%d of %d (%d%%) <%d-%d> column=%d%s" |
2076c87c | 1248 | pos total percent beg end col hscroll) |
a17a79c0 | 1249 | (message "point=%d of %d (EOB) column=%d%s" |
63219d53 | 1250 | pos total col hscroll)) |
c6fcc518 | 1251 | (let ((coding buffer-file-coding-system) |
a41b50ca | 1252 | encoded encoding-msg display-prop under-display) |
c6fcc518 KH |
1253 | (if (or (not coding) |
1254 | (eq (coding-system-type coding) t)) | |
b56a5ae0 | 1255 | (setq coding (default-value 'buffer-file-coding-system))) |
8f924df7 | 1256 | (if (eq (char-charset char) 'eight-bit) |
28fd4883 | 1257 | (setq encoding-msg |
41882805 | 1258 | (format "(%d, #o%o, #x%x, raw-byte)" char char char)) |
a41b50ca KH |
1259 | ;; Check if the character is displayed with some `display' |
1260 | ;; text property. In that case, set under-display to the | |
1261 | ;; buffer substring covered by that property. | |
d4ef2b50 | 1262 | (setq display-prop (get-char-property pos 'display)) |
a41b50ca | 1263 | (if display-prop |
d4ef2b50 | 1264 | (let ((to (or (next-single-char-property-change pos 'display) |
a41b50ca KH |
1265 | (point-max)))) |
1266 | (if (< to (+ pos 4)) | |
1267 | (setq under-display "") | |
1268 | (setq under-display "..." | |
1269 | to (+ pos 4))) | |
1270 | (setq under-display | |
1271 | (concat (buffer-substring-no-properties pos to) | |
1272 | under-display))) | |
1273 | (setq encoded (and (>= char 128) (encode-coding-char char coding)))) | |
28fd4883 | 1274 | (setq encoding-msg |
a41b50ca KH |
1275 | (if display-prop |
1276 | (if (not (stringp display-prop)) | |
a17a79c0 | 1277 | (format "(%d, #o%o, #x%x, part of display \"%s\")" |
a41b50ca | 1278 | char char char under-display) |
a17a79c0 | 1279 | (format "(%d, #o%o, #x%x, part of display \"%s\"->\"%s\")" |
a41b50ca KH |
1280 | char char char under-display display-prop)) |
1281 | (if encoded | |
a17a79c0 | 1282 | (format "(%d, #o%o, #x%x, file %s)" |
a41b50ca KH |
1283 | char char char |
1284 | (if (> (length encoded) 1) | |
1285 | "..." | |
1286 | (encoded-string-description encoded coding))) | |
a17a79c0 | 1287 | (format "(%d, #o%o, #x%x)" char char char))))) |
e5e89e48 | 1288 | (if detail |
24dad5d5 | 1289 | ;; We show the detailed information about CHAR. |
0b69eec5 | 1290 | (describe-char (point))) |
24dad5d5 | 1291 | (if (or (/= beg 1) (/= end (1+ total))) |
12587bbb | 1292 | (message "Char: %s%s %s point=%d of %d (%d%%) <%d-%d> column=%d%s" |
e5a902cf KH |
1293 | (if (< char 256) |
1294 | (single-key-description char) | |
f0d16a7f | 1295 | (buffer-substring-no-properties (point) (1+ (point)))) |
71cc0b74 EZ |
1296 | bidi-fixer |
1297 | encoding-msg pos total percent beg end col hscroll) | |
12587bbb | 1298 | (message "Char: %s%s %s point=%d of %d (%d%%) column=%d%s" |
a41b50ca KH |
1299 | (if enable-multibyte-characters |
1300 | (if (< char 128) | |
1301 | (single-key-description char) | |
1302 | (buffer-substring-no-properties (point) (1+ (point)))) | |
1303 | (single-key-description char)) | |
71cc0b74 | 1304 | bidi-fixer encoding-msg pos total percent col hscroll)))))) |
2d88b556 | 1305 | \f |
71a05b36 | 1306 | ;; Initialize read-expression-map. It is defined at C level. |
30c7e542 SM |
1307 | (defvar read-expression-map |
1308 | (let ((m (make-sparse-keymap))) | |
1309 | (define-key m "\M-\t" 'completion-at-point) | |
1310 | ;; Might as well bind TAB to completion, since inserting a TAB char is | |
1311 | ;; much too rarely useful. | |
1312 | (define-key m "\t" 'completion-at-point) | |
1313 | (set-keymap-parent m minibuffer-local-map) | |
1314 | m)) | |
1315 | ||
1316 | (defun read-minibuffer (prompt &optional initial-contents) | |
1317 | "Return a Lisp object read using the minibuffer, unevaluated. | |
1318 | Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS | |
1319 | is a string to insert in the minibuffer before reading. | |
1320 | \(INITIAL-CONTENTS can also be a cons of a string and an integer. | |
1321 | Such arguments are used as in `read-from-minibuffer'.)" | |
1322 | ;; Used for interactive spec `x'. | |
1323 | (read-from-minibuffer prompt initial-contents minibuffer-local-map | |
9a2c9b47 | 1324 | t 'minibuffer-history)) |
30c7e542 SM |
1325 | |
1326 | (defun eval-minibuffer (prompt &optional initial-contents) | |
1327 | "Return value of Lisp expression read using the minibuffer. | |
1328 | Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS | |
1329 | is a string to insert in the minibuffer before reading. | |
1330 | \(INITIAL-CONTENTS can also be a cons of a string and an integer. | |
1331 | Such arguments are used as in `read-from-minibuffer'.)" | |
1332 | ;; Used for interactive spec `X'. | |
17e5c0cc | 1333 | (eval (read--expression prompt initial-contents))) |
854c16c5 | 1334 | |
ad6aa5ed CY |
1335 | (defvar minibuffer-completing-symbol nil |
1336 | "Non-nil means completing a Lisp symbol in the minibuffer.") | |
2403c841 | 1337 | (make-obsolete-variable 'minibuffer-completing-symbol nil "24.1" 'get) |
ad6aa5ed | 1338 | |
d658acb6 CY |
1339 | (defvar minibuffer-default nil |
1340 | "The current default value or list of default values in the minibuffer. | |
1341 | The functions `read-from-minibuffer' and `completing-read' bind | |
1342 | this variable locally.") | |
1343 | ||
b49df39d | 1344 | (defcustom eval-expression-print-level 4 |
2f7e1f5a | 1345 | "Value for `print-level' while printing value in `eval-expression'. |
d26b26dc | 1346 | A value of nil means no limit." |
b49df39d | 1347 | :group 'lisp |
058d4999 | 1348 | :type '(choice (const :tag "No Limit" nil) integer) |
b49df39d RS |
1349 | :version "21.1") |
1350 | ||
1351 | (defcustom eval-expression-print-length 12 | |
2f7e1f5a | 1352 | "Value for `print-length' while printing value in `eval-expression'. |
d26b26dc | 1353 | A value of nil means no limit." |
b49df39d | 1354 | :group 'lisp |
058d4999 | 1355 | :type '(choice (const :tag "No Limit" nil) integer) |
b49df39d RS |
1356 | :version "21.1") |
1357 | ||
1358 | (defcustom eval-expression-debug-on-error t | |
2f7e1f5a | 1359 | "If non-nil set `debug-on-error' to t in `eval-expression'. |
ed8bcabe | 1360 | If nil, don't change the value of `debug-on-error'." |
b49df39d RS |
1361 | :group 'lisp |
1362 | :type 'boolean | |
1363 | :version "21.1") | |
1364 | ||
fa219ebd JL |
1365 | (defun eval-expression-print-format (value) |
1366 | "Format VALUE as a result of evaluated expression. | |
1367 | Return a formatted string which is displayed in the echo area | |
1368 | in addition to the value printed by prin1 in functions which | |
1369 | display the result of expression evaluation." | |
1370 | (if (and (integerp value) | |
b41594fd JL |
1371 | (or (eq standard-output t) |
1372 | (zerop (prefix-numeric-value current-prefix-arg)))) | |
fa219ebd | 1373 | (let ((char-string |
b41594fd JL |
1374 | (if (and (characterp value) |
1375 | (char-displayable-p value)) | |
1376 | (prin1-char value)))) | |
fa219ebd | 1377 | (if char-string |
1b5fd09e SM |
1378 | (format " (#o%o, #x%x, %s)" value value char-string) |
1379 | (format " (#o%o, #x%x)" value value))))) | |
fa219ebd | 1380 | |
69489f1d LL |
1381 | (defvar eval-expression-minibuffer-setup-hook nil |
1382 | "Hook run by `eval-expression' when entering the minibuffer.") | |
1383 | ||
17e5c0cc SM |
1384 | (defun read--expression (prompt &optional initial-contents) |
1385 | (let ((minibuffer-completing-symbol t)) | |
1386 | (minibuffer-with-setup-hook | |
1387 | (lambda () | |
1388 | (add-hook 'completion-at-point-functions | |
1389 | #'lisp-completion-at-point nil t) | |
1390 | (run-hooks 'eval-expression-minibuffer-setup-hook)) | |
1391 | (read-from-minibuffer prompt initial-contents | |
1392 | read-expression-map t | |
1393 | 'read-expression-history)))) | |
1394 | ||
8570b0ca | 1395 | ;; We define this, rather than making `eval' interactive, |
ac052b48 | 1396 | ;; for the sake of completion of names like eval-region, eval-buffer. |
6c8f113e SM |
1397 | (defun eval-expression (exp &optional insert-value) |
1398 | "Evaluate EXP and print value in the echo area. | |
15853710 LMI |
1399 | When called interactively, read an Emacs Lisp expression and |
1400 | evaluate it. | |
a6a1ee53 | 1401 | Value is also consed on to front of the variable `values'. |
6c8f113e | 1402 | Optional argument INSERT-VALUE non-nil (interactively, |
a7a8618b | 1403 | with prefix argument) means insert the result into the current buffer |
b41594fd JL |
1404 | instead of printing it in the echo area. With a zero prefix arg, |
1405 | insert the result with no limit on the length and level of lists, | |
1406 | and include additional formats for integers (octal, hexadecimal, | |
1407 | and character). Truncates long output according to the value | |
1408 | of the variables `eval-expression-print-length' | |
a7a8618b | 1409 | and `eval-expression-print-level'. |
b4f73994 RS |
1410 | |
1411 | If `eval-expression-debug-on-error' is non-nil, which is the default, | |
1412 | this command arranges for all errors to enter the debugger." | |
adca5fa6 | 1413 | (interactive |
17e5c0cc | 1414 | (list (read--expression "Eval: ") |
ecb7ad00 | 1415 | current-prefix-arg)) |
eaae8106 | 1416 | |
ed8bcabe | 1417 | (if (null eval-expression-debug-on-error) |
6c8f113e | 1418 | (push (eval exp lexical-binding) values) |
ed8bcabe GM |
1419 | (let ((old-value (make-symbol "t")) new-value) |
1420 | ;; Bind debug-on-error to something unique so that we can | |
99d99081 | 1421 | ;; detect when evalled code changes it. |
ed8bcabe | 1422 | (let ((debug-on-error old-value)) |
6c8f113e | 1423 | (push (eval exp lexical-binding) values) |
ed8bcabe | 1424 | (setq new-value debug-on-error)) |
99d99081 | 1425 | ;; If evalled code has changed the value of debug-on-error, |
ed8bcabe GM |
1426 | ;; propagate that change to the global binding. |
1427 | (unless (eq old-value new-value) | |
1428 | (setq debug-on-error new-value)))) | |
eaae8106 | 1429 | |
b41594fd JL |
1430 | (let ((print-length (and (not (zerop (prefix-numeric-value insert-value))) |
1431 | eval-expression-print-length)) | |
1432 | (print-level (and (not (zerop (prefix-numeric-value insert-value))) | |
1433 | eval-expression-print-level)) | |
6c8f113e SM |
1434 | (deactivate-mark)) |
1435 | (if insert-value | |
6b61353c KH |
1436 | (with-no-warnings |
1437 | (let ((standard-output (current-buffer))) | |
b41594fd JL |
1438 | (prog1 |
1439 | (prin1 (car values)) | |
1440 | (when (zerop (prefix-numeric-value insert-value)) | |
1441 | (let ((str (eval-expression-print-format (car values)))) | |
1442 | (if str (princ str))))))) | |
fa219ebd JL |
1443 | (prog1 |
1444 | (prin1 (car values) t) | |
1445 | (let ((str (eval-expression-print-format (car values)))) | |
1446 | (if str (princ str t))))))) | |
2076c87c JB |
1447 | |
1448 | (defun edit-and-eval-command (prompt command) | |
1449 | "Prompting with PROMPT, let user edit COMMAND and eval result. | |
1450 | COMMAND is a Lisp expression. Let user edit that expression in | |
1451 | the minibuffer, then read and evaluate the result." | |
9f4b6084 | 1452 | (let ((command |
6b61353c KH |
1453 | (let ((print-level nil) |
1454 | (minibuffer-history-sexp-flag (1+ (minibuffer-depth)))) | |
1455 | (unwind-protect | |
1456 | (read-from-minibuffer prompt | |
1457 | (prin1-to-string command) | |
1458 | read-expression-map t | |
1459 | 'command-history) | |
1460 | ;; If command was added to command-history as a string, | |
1461 | ;; get rid of that. We want only evaluable expressions there. | |
1462 | (if (stringp (car command-history)) | |
1463 | (setq command-history (cdr command-history))))))) | |
5d6c83ae KH |
1464 | |
1465 | ;; If command to be redone does not match front of history, | |
1466 | ;; add it to the history. | |
1467 | (or (equal command (car command-history)) | |
1468 | (setq command-history (cons command command-history))) | |
2076c87c JB |
1469 | (eval command))) |
1470 | ||
ebb61177 | 1471 | (defun repeat-complex-command (arg) |
2076c87c JB |
1472 | "Edit and re-evaluate last complex command, or ARGth from last. |
1473 | A complex command is one which used the minibuffer. | |
1474 | The command is placed in the minibuffer as a Lisp form for editing. | |
1475 | The result is executed, repeating the command as changed. | |
5626c14e JB |
1476 | If the command has been changed or is not the most recent previous |
1477 | command it is added to the front of the command history. | |
1478 | You can use the minibuffer history commands \ | |
1479 | \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element] | |
eb6e9899 | 1480 | to get different commands to edit and resubmit." |
2076c87c | 1481 | (interactive "p") |
ba343182 | 1482 | (let ((elt (nth (1- arg) command-history)) |
2076c87c JB |
1483 | newcmd) |
1484 | (if elt | |
854c16c5 | 1485 | (progn |
eab22e27 | 1486 | (setq newcmd |
74ae5fab RS |
1487 | (let ((print-level nil) |
1488 | (minibuffer-history-position arg) | |
99ea24de | 1489 | (minibuffer-history-sexp-flag (1+ (minibuffer-depth)))) |
9f4b6084 MR |
1490 | (unwind-protect |
1491 | (read-from-minibuffer | |
1492 | "Redo: " (prin1-to-string elt) read-expression-map t | |
1493 | (cons 'command-history arg)) | |
1494 | ||
1495 | ;; If command was added to command-history as a | |
1496 | ;; string, get rid of that. We want only | |
1497 | ;; evaluable expressions there. | |
1498 | (if (stringp (car command-history)) | |
1499 | (setq command-history (cdr command-history)))))) | |
db16f109 RS |
1500 | |
1501 | ;; If command to be redone does not match front of history, | |
1502 | ;; add it to the history. | |
1503 | (or (equal newcmd (car command-history)) | |
1504 | (setq command-history (cons newcmd command-history))) | |
f663baa7 SM |
1505 | (unwind-protect |
1506 | (progn | |
1507 | ;; Trick called-interactively-p into thinking that `newcmd' is | |
1508 | ;; an interactive call (bug#14136). | |
1509 | (add-hook 'called-interactively-p-functions | |
1510 | #'repeat-complex-command--called-interactively-skip) | |
1511 | (eval newcmd)) | |
1512 | (remove-hook 'called-interactively-p-functions | |
1513 | #'repeat-complex-command--called-interactively-skip))) | |
536b728a RS |
1514 | (if command-history |
1515 | (error "Argument %d is beyond length of command history" arg) | |
1516 | (error "There are no previous complex commands to repeat"))))) | |
7133b7ee | 1517 | |
f663baa7 SM |
1518 | (defun repeat-complex-command--called-interactively-skip (i _frame1 frame2) |
1519 | (and (eq 'eval (cadr frame2)) | |
1520 | (eq 'repeat-complex-command | |
1521 | (cadr (backtrace-frame i #'called-interactively-p))) | |
1522 | 1)) | |
1523 | ||
b6c2bfff SM |
1524 | (defvar extended-command-history nil) |
1525 | ||
7133b7ee JL |
1526 | (defun read-extended-command () |
1527 | "Read command name to invoke in `execute-extended-command'." | |
1528 | (minibuffer-with-setup-hook | |
1529 | (lambda () | |
1530 | (set (make-local-variable 'minibuffer-default-add-function) | |
1531 | (lambda () | |
1532 | ;; Get a command name at point in the original buffer | |
1533 | ;; to propose it after M-n. | |
1534 | (with-current-buffer (window-buffer (minibuffer-selected-window)) | |
1535 | (and (commandp (function-called-at-point)) | |
1536 | (format "%S" (function-called-at-point))))))) | |
1537 | ;; Read a string, completing from and restricting to the set of | |
1538 | ;; all defined commands. Don't provide any initial input. | |
1539 | ;; Save the command read on the extended-command history list. | |
1540 | (completing-read | |
1541 | (concat (cond | |
1542 | ((eq current-prefix-arg '-) "- ") | |
1543 | ((and (consp current-prefix-arg) | |
1544 | (eq (car current-prefix-arg) 4)) "C-u ") | |
1545 | ((and (consp current-prefix-arg) | |
1546 | (integerp (car current-prefix-arg))) | |
1547 | (format "%d " (car current-prefix-arg))) | |
1548 | ((integerp current-prefix-arg) | |
1549 | (format "%d " current-prefix-arg))) | |
1550 | ;; This isn't strictly correct if `execute-extended-command' | |
1551 | ;; is bound to anything else (e.g. [menu]). | |
1552 | ;; It could use (key-description (this-single-command-keys)), | |
1553 | ;; but actually a prompt other than "M-x" would be confusing, | |
1554 | ;; because "M-x" is a well-known prompt to read a command | |
1555 | ;; and it serves as a shorthand for "Extended command: ". | |
1556 | "M-x ") | |
1557 | obarray 'commandp t nil 'extended-command-history))) | |
1558 | ||
b593d6a9 AH |
1559 | (defcustom suggest-key-bindings t |
1560 | "Non-nil means show the equivalent key-binding when M-x command has one. | |
1561 | The value can be a length of time to show the message for. | |
1562 | If the value is non-nil and not a number, we wait 2 seconds." | |
1563 | :group 'keyboard | |
1564 | :type '(choice (const :tag "off" nil) | |
1565 | (integer :tag "time" 2) | |
1566 | (other :tag "on"))) | |
1567 | ||
1568 | (defun execute-extended-command (prefixarg &optional command-name) | |
1569 | ;; Based on Fexecute_extended_command in keyboard.c of Emacs. | |
1570 | ;; Aaron S. Hawley <aaron.s.hawley(at)gmail.com> 2009-08-24 | |
1571 | "Read function name, then read its arguments and call it. | |
1572 | ||
89c0dda8 | 1573 | To pass a numeric argument to the command you are invoking, specify |
b593d6a9 AH |
1574 | the numeric argument to this command. |
1575 | ||
1576 | Noninteractively, the argument PREFIXARG is the prefix argument to | |
1577 | give to the command you invoke, if it asks for an argument." | |
1578 | (interactive (list current-prefix-arg (read-extended-command))) | |
1579 | ;; Emacs<24 calling-convention was with a single `prefixarg' argument. | |
89c0dda8 GM |
1580 | (if (null command-name) |
1581 | (setq command-name (let ((current-prefix-arg prefixarg)) ; for prompt | |
1582 | (read-extended-command)))) | |
b593d6a9 AH |
1583 | (let* ((function (and (stringp command-name) (intern-soft command-name))) |
1584 | (binding (and suggest-key-bindings | |
f2d6a3df SM |
1585 | (not executing-kbd-macro) |
1586 | (where-is-internal function overriding-local-map t)))) | |
b593d6a9 AH |
1587 | (unless (commandp function) |
1588 | (error "`%s' is not a valid command name" command-name)) | |
b593d6a9 | 1589 | (setq this-command function) |
f2d6a3df SM |
1590 | ;; Normally `real-this-command' should never be changed, but here we really |
1591 | ;; want to pretend that M-x <cmd> RET is nothing more than a "key | |
1592 | ;; binding" for <cmd>, so the command the user really wanted to run is | |
1593 | ;; `function' and not `execute-extended-command'. The difference is | |
1594 | ;; visible in cases such as M-x <cmd> RET and then C-x z (bug#11506). | |
1595 | (setq real-this-command function) | |
b593d6a9 AH |
1596 | (let ((prefix-arg prefixarg)) |
1597 | (command-execute function 'record)) | |
1598 | ;; If enabled, show which key runs this command. | |
1599 | (when binding | |
1600 | ;; But first wait, and skip the message if there is input. | |
1601 | (let* ((waited | |
1602 | ;; If this command displayed something in the echo area; | |
1603 | ;; wait a few seconds, then display our suggestion message. | |
1604 | (sit-for (cond | |
1605 | ((zerop (length (current-message))) 0) | |
1606 | ((numberp suggest-key-bindings) suggest-key-bindings) | |
1607 | (t 2))))) | |
1608 | (when (and waited (not (consp unread-command-events))) | |
1609 | (with-temp-message | |
1610 | (format "You can run the command `%s' with %s" | |
1611 | function (key-description binding)) | |
1612 | (sit-for (if (numberp suggest-key-bindings) | |
1613 | suggest-key-bindings | |
1614 | 2)))))))) | |
b6c2bfff SM |
1615 | |
1616 | (defun command-execute (cmd &optional record-flag keys special) | |
1617 | ;; BEWARE: Called directly from the C code. | |
1618 | "Execute CMD as an editor command. | |
1619 | CMD must be a symbol that satisfies the `commandp' predicate. | |
1620 | Optional second arg RECORD-FLAG non-nil | |
1621 | means unconditionally put this command in the variable `command-history'. | |
1622 | Otherwise, that is done only if an arg is read using the minibuffer. | |
1623 | The argument KEYS specifies the value to use instead of (this-command-keys) | |
1624 | when reading the arguments; if it is nil, (this-command-keys) is used. | |
1625 | The argument SPECIAL, if non-nil, means that this command is executing | |
1626 | a special event, so ignore the prefix argument and don't clear it." | |
1627 | (setq debug-on-next-call nil) | |
1628 | (let ((prefixarg (unless special | |
1629 | (prog1 prefix-arg | |
1630 | (setq current-prefix-arg prefix-arg) | |
1631 | (setq prefix-arg nil))))) | |
1632 | (and (symbolp cmd) | |
1633 | (get cmd 'disabled) | |
1634 | ;; FIXME: Weird calling convention! | |
1635 | (run-hooks 'disabled-command-function)) | |
1636 | (let ((final cmd)) | |
1637 | (while | |
1638 | (progn | |
1639 | (setq final (indirect-function final)) | |
1640 | (if (autoloadp final) | |
1641 | (setq final (autoload-do-load final cmd))))) | |
1642 | (cond | |
1643 | ((arrayp final) | |
1644 | ;; If requested, place the macro in the command history. For | |
1645 | ;; other sorts of commands, call-interactively takes care of this. | |
1646 | (when record-flag | |
1647 | (push `(execute-kbd-macro ,final ,prefixarg) command-history) | |
1648 | ;; Don't keep command history around forever. | |
1649 | (when (and (numberp history-length) (> history-length 0)) | |
1650 | (let ((cell (nthcdr history-length command-history))) | |
1651 | (if (consp cell) (setcdr cell nil))))) | |
1652 | (execute-kbd-macro final prefixarg)) | |
1653 | (t | |
1654 | ;; Pass `cmd' rather than `final', for the backtrace's sake. | |
1655 | (prog1 (call-interactively cmd record-flag keys) | |
1656 | (when (and (symbolp cmd) | |
1657 | (get cmd 'byte-obsolete-info) | |
1658 | (not (get cmd 'command-execute-obsolete-warned))) | |
1659 | (put cmd 'command-execute-obsolete-warned t) | |
1660 | (message "%s" (macroexp--obsolete-warning | |
1661 | cmd (get cmd 'byte-obsolete-info) "command"))))))))) | |
2d88b556 | 1662 | \f |
854c16c5 RS |
1663 | (defvar minibuffer-history nil |
1664 | "Default minibuffer history list. | |
1665 | This is used for all minibuffer input | |
e5f0c02f EZ |
1666 | except when an alternate history list is specified. |
1667 | ||
1668 | Maximum length of the history list is determined by the value | |
1669 | of `history-length', which see.") | |
854c16c5 | 1670 | (defvar minibuffer-history-sexp-flag nil |
6b61353c KH |
1671 | "Control whether history list elements are expressions or strings. |
1672 | If the value of this variable equals current minibuffer depth, | |
1673 | they are expressions; otherwise they are strings. | |
7979163c | 1674 | \(That convention is designed to do the right thing for |
6b61353c | 1675 | recursive uses of the minibuffer.)") |
e91f80c4 | 1676 | (setq minibuffer-history-variable 'minibuffer-history) |
535c8bdb | 1677 | (setq minibuffer-history-position nil) ;; Defvar is in C code. |
854c16c5 | 1678 | (defvar minibuffer-history-search-history nil) |
e91f80c4 | 1679 | |
93cee14b RS |
1680 | (defvar minibuffer-text-before-history nil |
1681 | "Text that was in this minibuffer before any history commands. | |
1682 | This is nil if there have not yet been any history commands | |
1683 | in this use of the minibuffer.") | |
1684 | ||
1685 | (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize) | |
1686 | ||
1687 | (defun minibuffer-history-initialize () | |
1688 | (setq minibuffer-text-before-history nil)) | |
1689 | ||
06b60517 | 1690 | (defun minibuffer-avoid-prompt (_new _old) |
6e7d0ff7 MB |
1691 | "A point-motion hook for the minibuffer, that moves point out of the prompt." |
1692 | (constrain-to-field nil (point-max))) | |
1693 | ||
6e30a99a | 1694 | (defcustom minibuffer-history-case-insensitive-variables nil |
1d2b0303 | 1695 | "Minibuffer history variables for which matching should ignore case. |
6e30a99a RS |
1696 | If a history variable is a member of this list, then the |
1697 | \\[previous-matching-history-element] and \\[next-matching-history-element]\ | |
1698 | commands ignore case when searching it, regardless of `case-fold-search'." | |
1699 | :type '(repeat variable) | |
1700 | :group 'minibuffer) | |
1701 | ||
e91f80c4 | 1702 | (defun previous-matching-history-element (regexp n) |
854c16c5 RS |
1703 | "Find the previous history element that matches REGEXP. |
1704 | \(Previous history elements refer to earlier actions.) | |
1705 | With prefix argument N, search for Nth previous match. | |
5c2010f0 | 1706 | If N is negative, find the next or Nth next match. |
9889af08 EZ |
1707 | Normally, history elements are matched case-insensitively if |
1708 | `case-fold-search' is non-nil, but an uppercase letter in REGEXP | |
1709 | makes the search case-sensitive. | |
6e30a99a | 1710 | See also `minibuffer-history-case-insensitive-variables'." |
854c16c5 | 1711 | (interactive |
c1172a19 | 1712 | (let* ((enable-recursive-minibuffers t) |
c1172a19 RS |
1713 | (regexp (read-from-minibuffer "Previous element matching (regexp): " |
1714 | nil | |
1715 | minibuffer-local-map | |
1716 | nil | |
5794c45d RS |
1717 | 'minibuffer-history-search-history |
1718 | (car minibuffer-history-search-history)))) | |
c1172a19 RS |
1719 | ;; Use the last regexp specified, by default, if input is empty. |
1720 | (list (if (string= regexp "") | |
a8e96cea KH |
1721 | (if minibuffer-history-search-history |
1722 | (car minibuffer-history-search-history) | |
71873e2b | 1723 | (user-error "No previous history search regexp")) |
c1172a19 | 1724 | regexp) |
854c16c5 | 1725 | (prefix-numeric-value current-prefix-arg)))) |
e276a14a MB |
1726 | (unless (zerop n) |
1727 | (if (and (zerop minibuffer-history-position) | |
1728 | (null minibuffer-text-before-history)) | |
efaac2e6 | 1729 | (setq minibuffer-text-before-history |
6d74d713 | 1730 | (minibuffer-contents-no-properties))) |
e276a14a MB |
1731 | (let ((history (symbol-value minibuffer-history-variable)) |
1732 | (case-fold-search | |
1733 | (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped | |
1734 | ;; On some systems, ignore case for file names. | |
1735 | (if (memq minibuffer-history-variable | |
1736 | minibuffer-history-case-insensitive-variables) | |
1737 | t | |
1738 | ;; Respect the user's setting for case-fold-search: | |
1739 | case-fold-search) | |
1740 | nil)) | |
1741 | prevpos | |
1742 | match-string | |
1743 | match-offset | |
1744 | (pos minibuffer-history-position)) | |
1745 | (while (/= n 0) | |
1746 | (setq prevpos pos) | |
1747 | (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history))) | |
1748 | (when (= pos prevpos) | |
71873e2b SM |
1749 | (user-error (if (= pos 1) |
1750 | "No later matching history item" | |
1751 | "No earlier matching history item"))) | |
e276a14a MB |
1752 | (setq match-string |
1753 | (if (eq minibuffer-history-sexp-flag (minibuffer-depth)) | |
7908d27c | 1754 | (let ((print-level nil)) |
e276a14a MB |
1755 | (prin1-to-string (nth (1- pos) history))) |
1756 | (nth (1- pos) history))) | |
1757 | (setq match-offset | |
1758 | (if (< n 0) | |
1759 | (and (string-match regexp match-string) | |
1760 | (match-end 0)) | |
1761 | (and (string-match (concat ".*\\(" regexp "\\)") match-string) | |
1762 | (match-beginning 1)))) | |
1763 | (when match-offset | |
1764 | (setq n (+ n (if (< n 0) 1 -1))))) | |
1765 | (setq minibuffer-history-position pos) | |
1766 | (goto-char (point-max)) | |
efaac2e6 | 1767 | (delete-minibuffer-contents) |
e276a14a | 1768 | (insert match-string) |
6d74d713 | 1769 | (goto-char (+ (minibuffer-prompt-end) match-offset)))) |
e1e04350 SM |
1770 | (if (memq (car (car command-history)) '(previous-matching-history-element |
1771 | next-matching-history-element)) | |
854c16c5 | 1772 | (setq command-history (cdr command-history)))) |
e91f80c4 | 1773 | |
e91f80c4 | 1774 | (defun next-matching-history-element (regexp n) |
854c16c5 RS |
1775 | "Find the next history element that matches REGEXP. |
1776 | \(The next history element refers to a more recent action.) | |
1777 | With prefix argument N, search for Nth next match. | |
5c2010f0 | 1778 | If N is negative, find the previous or Nth previous match. |
9889af08 EZ |
1779 | Normally, history elements are matched case-insensitively if |
1780 | `case-fold-search' is non-nil, but an uppercase letter in REGEXP | |
1781 | makes the search case-sensitive." | |
854c16c5 | 1782 | (interactive |
c1172a19 | 1783 | (let* ((enable-recursive-minibuffers t) |
c1172a19 RS |
1784 | (regexp (read-from-minibuffer "Next element matching (regexp): " |
1785 | nil | |
1786 | minibuffer-local-map | |
1787 | nil | |
e967cd11 RS |
1788 | 'minibuffer-history-search-history |
1789 | (car minibuffer-history-search-history)))) | |
c1172a19 RS |
1790 | ;; Use the last regexp specified, by default, if input is empty. |
1791 | (list (if (string= regexp "") | |
e967cd11 RS |
1792 | (if minibuffer-history-search-history |
1793 | (car minibuffer-history-search-history) | |
71873e2b | 1794 | (user-error "No previous history search regexp")) |
c1172a19 | 1795 | regexp) |
854c16c5 | 1796 | (prefix-numeric-value current-prefix-arg)))) |
e91f80c4 | 1797 | (previous-matching-history-element regexp (- n))) |
2076c87c | 1798 | |
8dc3ba7d MB |
1799 | (defvar minibuffer-temporary-goal-position nil) |
1800 | ||
7f914bbe | 1801 | (defvar minibuffer-default-add-function 'minibuffer-default-add-completions |
0eb5f40f JL |
1802 | "Function run by `goto-history-element' before consuming default values. |
1803 | This is useful to dynamically add more elements to the list of default values | |
7f914bbe JL |
1804 | when `goto-history-element' reaches the end of this list. |
1805 | Before calling this function `goto-history-element' sets the variable | |
1806 | `minibuffer-default-add-done' to t, so it will call this function only | |
1807 | once. In special cases, when this function needs to be called more | |
1808 | than once, it can set `minibuffer-default-add-done' to nil explicitly, | |
1809 | overriding the setting of this variable to t in `goto-history-element'.") | |
1810 | ||
1811 | (defvar minibuffer-default-add-done nil | |
1812 | "When nil, add more elements to the end of the list of default values. | |
1813 | The value nil causes `goto-history-element' to add more elements to | |
1814 | the list of defaults when it reaches the end of this list. It does | |
1815 | this by calling a function defined by `minibuffer-default-add-function'.") | |
1816 | ||
1817 | (make-variable-buffer-local 'minibuffer-default-add-done) | |
1818 | ||
1819 | (defun minibuffer-default-add-completions () | |
1820 | "Return a list of all completions without the default value. | |
1821 | This function is used to add all elements of the completion table to | |
1822 | the end of the list of defaults just after the default value." | |
7f914bbe JL |
1823 | (let ((def minibuffer-default) |
1824 | (all (all-completions "" | |
1825 | minibuffer-completion-table | |
e96d62cd | 1826 | minibuffer-completion-predicate))) |
7f914bbe JL |
1827 | (if (listp def) |
1828 | (append def all) | |
1829 | (cons def (delete def all))))) | |
1830 | ||
297b8ccd JL |
1831 | (defun goto-history-element (nabs) |
1832 | "Puts element of the minibuffer history in the minibuffer. | |
1833 | The argument NABS specifies the absolute history position." | |
1834 | (interactive "p") | |
7f914bbe JL |
1835 | (when (and (not minibuffer-default-add-done) |
1836 | (functionp minibuffer-default-add-function) | |
1837 | (< nabs (- (if (listp minibuffer-default) | |
1838 | (length minibuffer-default) | |
1839 | 1)))) | |
1840 | (setq minibuffer-default-add-done t | |
1841 | minibuffer-default (funcall minibuffer-default-add-function))) | |
b38fc7f1 JL |
1842 | (let ((minimum (if minibuffer-default |
1843 | (- (if (listp minibuffer-default) | |
1844 | (length minibuffer-default) | |
1845 | 1)) | |
1846 | 0)) | |
297b8ccd JL |
1847 | elt minibuffer-returned-to-present) |
1848 | (if (and (zerop minibuffer-history-position) | |
1849 | (null minibuffer-text-before-history)) | |
1850 | (setq minibuffer-text-before-history | |
1851 | (minibuffer-contents-no-properties))) | |
1852 | (if (< nabs minimum) | |
71873e2b SM |
1853 | (user-error (if minibuffer-default |
1854 | "End of defaults; no next item" | |
1855 | "End of history; no default available"))) | |
297b8ccd | 1856 | (if (> nabs (length (symbol-value minibuffer-history-variable))) |
71873e2b | 1857 | (user-error "Beginning of history; no preceding item")) |
297b8ccd JL |
1858 | (unless (memq last-command '(next-history-element |
1859 | previous-history-element)) | |
1860 | (let ((prompt-end (minibuffer-prompt-end))) | |
1861 | (set (make-local-variable 'minibuffer-temporary-goal-position) | |
1862 | (cond ((<= (point) prompt-end) prompt-end) | |
1863 | ((eobp) nil) | |
1864 | (t (point)))))) | |
1865 | (goto-char (point-max)) | |
1866 | (delete-minibuffer-contents) | |
1867 | (setq minibuffer-history-position nabs) | |
b38fc7f1 JL |
1868 | (cond ((< nabs 0) |
1869 | (setq elt (if (listp minibuffer-default) | |
1870 | (nth (1- (abs nabs)) minibuffer-default) | |
1871 | minibuffer-default))) | |
297b8ccd JL |
1872 | ((= nabs 0) |
1873 | (setq elt (or minibuffer-text-before-history "")) | |
1874 | (setq minibuffer-returned-to-present t) | |
1875 | (setq minibuffer-text-before-history nil)) | |
1876 | (t (setq elt (nth (1- minibuffer-history-position) | |
1877 | (symbol-value minibuffer-history-variable))))) | |
1878 | (insert | |
1879 | (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth)) | |
1880 | (not minibuffer-returned-to-present)) | |
1881 | (let ((print-level nil)) | |
1882 | (prin1-to-string elt)) | |
1883 | elt)) | |
1884 | (goto-char (or minibuffer-temporary-goal-position (point-max))))) | |
1885 | ||
ebb61177 | 1886 | (defun next-history-element (n) |
1459a43b RS |
1887 | "Puts next element of the minibuffer history in the minibuffer. |
1888 | With argument N, it uses the Nth following element." | |
2076c87c | 1889 | (interactive "p") |
0818b15e | 1890 | (or (zerop n) |
297b8ccd | 1891 | (goto-history-element (- minibuffer-history-position n)))) |
2076c87c | 1892 | |
ebb61177 | 1893 | (defun previous-history-element (n) |
1459a43b RS |
1894 | "Puts previous element of the minibuffer history in the minibuffer. |
1895 | With argument N, it uses the Nth previous element." | |
2076c87c | 1896 | (interactive "p") |
297b8ccd JL |
1897 | (or (zerop n) |
1898 | (goto-history-element (+ minibuffer-history-position n)))) | |
d0678801 RM |
1899 | |
1900 | (defun next-complete-history-element (n) | |
a4d1159b GM |
1901 | "Get next history element which completes the minibuffer before the point. |
1902 | The contents of the minibuffer after the point are deleted, and replaced | |
1903 | by the new completion." | |
d0678801 | 1904 | (interactive "p") |
b5e6f936 RM |
1905 | (let ((point-at-start (point))) |
1906 | (next-matching-history-element | |
a4d1159b | 1907 | (concat |
efaac2e6 | 1908 | "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point)))) |
a4d1159b | 1909 | n) |
b5e6f936 RM |
1910 | ;; next-matching-history-element always puts us at (point-min). |
1911 | ;; Move to the position we were at before changing the buffer contents. | |
c80e3b4a | 1912 | ;; This is still sensible, because the text before point has not changed. |
b5e6f936 | 1913 | (goto-char point-at-start))) |
d0678801 RM |
1914 | |
1915 | (defun previous-complete-history-element (n) | |
1f6fcec3 | 1916 | "\ |
a4d1159b GM |
1917 | Get previous history element which completes the minibuffer before the point. |
1918 | The contents of the minibuffer after the point are deleted, and replaced | |
1919 | by the new completion." | |
d0678801 RM |
1920 | (interactive "p") |
1921 | (next-complete-history-element (- n))) | |
a4d1159b | 1922 | |
efaac2e6 | 1923 | ;; For compatibility with the old subr of the same name. |
a4d1159b GM |
1924 | (defun minibuffer-prompt-width () |
1925 | "Return the display width of the minibuffer prompt. | |
f33321ad | 1926 | Return 0 if current buffer is not a minibuffer." |
a4d1159b GM |
1927 | ;; Return the width of everything before the field at the end of |
1928 | ;; the buffer; this should be 0 for normal buffers. | |
efaac2e6 | 1929 | (1- (minibuffer-prompt-end))) |
2d88b556 | 1930 | \f |
297b8ccd JL |
1931 | ;; isearch minibuffer history |
1932 | (add-hook 'minibuffer-setup-hook 'minibuffer-history-isearch-setup) | |
1933 | ||
1934 | (defvar minibuffer-history-isearch-message-overlay) | |
1935 | (make-variable-buffer-local 'minibuffer-history-isearch-message-overlay) | |
1936 | ||
1937 | (defun minibuffer-history-isearch-setup () | |
1938 | "Set up a minibuffer for using isearch to search the minibuffer history. | |
1939 | Intended to be added to `minibuffer-setup-hook'." | |
1940 | (set (make-local-variable 'isearch-search-fun-function) | |
1941 | 'minibuffer-history-isearch-search) | |
1942 | (set (make-local-variable 'isearch-message-function) | |
1943 | 'minibuffer-history-isearch-message) | |
1944 | (set (make-local-variable 'isearch-wrap-function) | |
1945 | 'minibuffer-history-isearch-wrap) | |
1946 | (set (make-local-variable 'isearch-push-state-function) | |
1947 | 'minibuffer-history-isearch-push-state) | |
1948 | (add-hook 'isearch-mode-end-hook 'minibuffer-history-isearch-end nil t)) | |
1949 | ||
1950 | (defun minibuffer-history-isearch-end () | |
1951 | "Clean up the minibuffer after terminating isearch in the minibuffer." | |
1952 | (if minibuffer-history-isearch-message-overlay | |
1953 | (delete-overlay minibuffer-history-isearch-message-overlay))) | |
1954 | ||
1955 | (defun minibuffer-history-isearch-search () | |
1956 | "Return the proper search function, for isearch in minibuffer history." | |
8cbd80f7 JL |
1957 | (lambda (string bound noerror) |
1958 | (let ((search-fun | |
1959 | ;; Use standard functions to search within minibuffer text | |
1960 | (isearch-search-fun-default)) | |
1961 | found) | |
1962 | ;; Avoid lazy-highlighting matches in the minibuffer prompt when | |
1963 | ;; searching forward. Lazy-highlight calls this lambda with the | |
1964 | ;; bound arg, so skip the minibuffer prompt. | |
1965 | (if (and bound isearch-forward (< (point) (minibuffer-prompt-end))) | |
1966 | (goto-char (minibuffer-prompt-end))) | |
1967 | (or | |
1968 | ;; 1. First try searching in the initial minibuffer text | |
1969 | (funcall search-fun string | |
1970 | (if isearch-forward bound (minibuffer-prompt-end)) | |
1971 | noerror) | |
1972 | ;; 2. If the above search fails, start putting next/prev history | |
1973 | ;; elements in the minibuffer successively, and search the string | |
1974 | ;; in them. Do this only when bound is nil (i.e. not while | |
1975 | ;; lazy-highlighting search strings in the current minibuffer text). | |
1976 | (unless bound | |
1977 | (condition-case nil | |
1978 | (progn | |
1979 | (while (not found) | |
1980 | (cond (isearch-forward | |
1981 | (next-history-element 1) | |
1982 | (goto-char (minibuffer-prompt-end))) | |
1983 | (t | |
1984 | (previous-history-element 1) | |
1985 | (goto-char (point-max)))) | |
1986 | (setq isearch-barrier (point) isearch-opoint (point)) | |
1987 | ;; After putting the next/prev history element, search | |
1988 | ;; the string in them again, until next-history-element | |
1989 | ;; or previous-history-element raises an error at the | |
1990 | ;; beginning/end of history. | |
1991 | (setq found (funcall search-fun string | |
1992 | (unless isearch-forward | |
1993 | ;; For backward search, don't search | |
1994 | ;; in the minibuffer prompt | |
1995 | (minibuffer-prompt-end)) | |
1996 | noerror))) | |
1997 | ;; Return point of the new search result | |
1998 | (point)) | |
1999 | ;; Return nil when next(prev)-history-element fails | |
2000 | (error nil))))))) | |
297b8ccd JL |
2001 | |
2002 | (defun minibuffer-history-isearch-message (&optional c-q-hack ellipsis) | |
2003 | "Display the minibuffer history search prompt. | |
2004 | If there are no search errors, this function displays an overlay with | |
2005 | the isearch prompt which replaces the original minibuffer prompt. | |
2006 | Otherwise, it displays the standard isearch message returned from | |
9fc9a531 | 2007 | the function `isearch-message'." |
297b8ccd JL |
2008 | (if (not (and (minibufferp) isearch-success (not isearch-error))) |
2009 | ;; Use standard function `isearch-message' when not in the minibuffer, | |
2010 | ;; or search fails, or has an error (like incomplete regexp). | |
2011 | ;; This function overwrites minibuffer text with isearch message, | |
2012 | ;; so it's possible to see what is wrong in the search string. | |
2013 | (isearch-message c-q-hack ellipsis) | |
2014 | ;; Otherwise, put the overlay with the standard isearch prompt over | |
2015 | ;; the initial minibuffer prompt. | |
2016 | (if (overlayp minibuffer-history-isearch-message-overlay) | |
2017 | (move-overlay minibuffer-history-isearch-message-overlay | |
2018 | (point-min) (minibuffer-prompt-end)) | |
2019 | (setq minibuffer-history-isearch-message-overlay | |
2020 | (make-overlay (point-min) (minibuffer-prompt-end))) | |
2021 | (overlay-put minibuffer-history-isearch-message-overlay 'evaporate t)) | |
2022 | (overlay-put minibuffer-history-isearch-message-overlay | |
2023 | 'display (isearch-message-prefix c-q-hack ellipsis)) | |
2024 | ;; And clear any previous isearch message. | |
2025 | (message ""))) | |
2026 | ||
2027 | (defun minibuffer-history-isearch-wrap () | |
1d2b0303 | 2028 | "Wrap the minibuffer history search when search fails. |
297b8ccd JL |
2029 | Move point to the first history element for a forward search, |
2030 | or to the last history element for a backward search." | |
8cbd80f7 JL |
2031 | ;; When `minibuffer-history-isearch-search' fails on reaching the |
2032 | ;; beginning/end of the history, wrap the search to the first/last | |
2033 | ;; minibuffer history element. | |
2034 | (if isearch-forward | |
2035 | (goto-history-element (length (symbol-value minibuffer-history-variable))) | |
2036 | (goto-history-element 0)) | |
2037 | (setq isearch-success t) | |
297b8ccd JL |
2038 | (goto-char (if isearch-forward (minibuffer-prompt-end) (point-max)))) |
2039 | ||
2040 | (defun minibuffer-history-isearch-push-state () | |
2041 | "Save a function restoring the state of minibuffer history search. | |
2042 | Save `minibuffer-history-position' to the additional state parameter | |
2043 | in the search status stack." | |
a4648137 SM |
2044 | (let ((pos minibuffer-history-position)) |
2045 | (lambda (cmd) | |
2046 | (minibuffer-history-isearch-pop-state cmd pos)))) | |
297b8ccd | 2047 | |
06b60517 | 2048 | (defun minibuffer-history-isearch-pop-state (_cmd hist-pos) |
297b8ccd | 2049 | "Restore the minibuffer history search state. |
5626c14e | 2050 | Go to the history element by the absolute history position HIST-POS." |
297b8ccd JL |
2051 | (goto-history-element hist-pos)) |
2052 | ||
2053 | \f | |
2076c87c | 2054 | ;Put this on C-x u, so we can force that rather than C-_ into startup msg |
8cb95edf | 2055 | (define-obsolete-function-alias 'advertised-undo 'undo "23.2") |
2076c87c | 2056 | |
1e96c007 | 2057 | (defconst undo-equiv-table (make-hash-table :test 'eq :weakness t) |
713c9020 RS |
2058 | "Table mapping redo records to the corresponding undo one. |
2059 | A redo record for undo-in-region maps to t. | |
2060 | A redo record for ordinary undo maps to the following (earlier) undo.") | |
1e96c007 SM |
2061 | |
2062 | (defvar undo-in-region nil | |
2063 | "Non-nil if `pending-undo-list' is not just a tail of `buffer-undo-list'.") | |
2064 | ||
2065 | (defvar undo-no-redo nil | |
2066 | "If t, `undo' doesn't go through redo entries.") | |
2067 | ||
a7fe694c RS |
2068 | (defvar pending-undo-list nil |
2069 | "Within a run of consecutive undo commands, list remaining to be undone. | |
8ac28be5 | 2070 | If t, we undid all the way to the end of it.") |
a7fe694c | 2071 | |
2076c87c JB |
2072 | (defun undo (&optional arg) |
2073 | "Undo some previous changes. | |
2074 | Repeat this command to undo more changes. | |
5626c14e | 2075 | A numeric ARG serves as a repeat count. |
65627aad | 2076 | |
3c1b77ca | 2077 | In Transient Mark mode when the mark is active, only undo changes within |
1e96c007 | 2078 | the current region. Similarly, when not in Transient Mark mode, just \\[universal-argument] |
3c1b77ca | 2079 | as an argument limits undo to changes within the current region." |
65627aad | 2080 | (interactive "*P") |
2e033693 RS |
2081 | ;; Make last-command indicate for the next command that this was an undo. |
2082 | ;; That way, another undo will undo more. | |
2083 | ;; If we get to the end of the undo history and get an error, | |
2084 | ;; another undo command will find the undo history empty | |
2085 | ;; and will get another error. To begin undoing the undos, | |
2086 | ;; you must type some other command. | |
82f8cd94 CY |
2087 | (let* ((modified (buffer-modified-p)) |
2088 | ;; For an indirect buffer, look in the base buffer for the | |
2089 | ;; auto-save data. | |
2090 | (base-buffer (or (buffer-base-buffer) (current-buffer))) | |
2091 | (recent-save (with-current-buffer base-buffer | |
2092 | (recent-auto-save-p))) | |
2093 | message) | |
6b61353c KH |
2094 | ;; If we get an error in undo-start, |
2095 | ;; the next command should not be a "consecutive undo". | |
2096 | ;; So set `this-command' to something other than `undo'. | |
2097 | (setq this-command 'undo-start) | |
2098 | ||
e967cd11 | 2099 | (unless (and (eq last-command 'undo) |
a7fe694c RS |
2100 | (or (eq pending-undo-list t) |
2101 | ;; If something (a timer or filter?) changed the buffer | |
2102 | ;; since the previous command, don't continue the undo seq. | |
2103 | (let ((list buffer-undo-list)) | |
2104 | (while (eq (car list) nil) | |
2105 | (setq list (cdr list))) | |
2106 | ;; If the last undo record made was made by undo | |
2107 | ;; it shows nothing else happened in between. | |
2108 | (gethash list undo-equiv-table)))) | |
1e96c007 | 2109 | (setq undo-in-region |
d34c311a | 2110 | (or (region-active-p) (and arg (not (numberp arg))))) |
1e96c007 | 2111 | (if undo-in-region |
3c1b77ca MB |
2112 | (undo-start (region-beginning) (region-end)) |
2113 | (undo-start)) | |
2114 | ;; get rid of initial undo boundary | |
2115 | (undo-more 1)) | |
9a1120ea | 2116 | ;; If we got this far, the next command should be a consecutive undo. |
6b61353c | 2117 | (setq this-command 'undo) |
1e96c007 SM |
2118 | ;; Check to see whether we're hitting a redo record, and if |
2119 | ;; so, ask the user whether she wants to skip the redo/undo pair. | |
2120 | (let ((equiv (gethash pending-undo-list undo-equiv-table))) | |
2121 | (or (eq (selected-window) (minibuffer-window)) | |
d67d3afd GM |
2122 | (setq message (format "%s%s!" |
2123 | (if (or undo-no-redo (not equiv)) | |
2124 | "Undo" "Redo") | |
2125 | (if undo-in-region " in region" "")))) | |
0047373b | 2126 | (when (and (consp equiv) undo-no-redo) |
1e96c007 SM |
2127 | ;; The equiv entry might point to another redo record if we have done |
2128 | ;; undo-redo-undo-redo-... so skip to the very last equiv. | |
2129 | (while (let ((next (gethash equiv undo-equiv-table))) | |
2130 | (if next (setq equiv next)))) | |
2131 | (setq pending-undo-list equiv))) | |
3c1b77ca | 2132 | (undo-more |
d34c311a | 2133 | (if (numberp arg) |
3c1b77ca MB |
2134 | (prefix-numeric-value arg) |
2135 | 1)) | |
1e96c007 | 2136 | ;; Record the fact that the just-generated undo records come from an |
713c9020 RS |
2137 | ;; undo operation--that is, they are redo records. |
2138 | ;; In the ordinary case (not within a region), map the redo | |
2139 | ;; record to the following undos. | |
1e96c007 | 2140 | ;; I don't know how to do that in the undo-in-region case. |
86f0d932 SM |
2141 | (let ((list buffer-undo-list)) |
2142 | ;; Strip any leading undo boundaries there might be, like we do | |
2143 | ;; above when checking. | |
2144 | (while (eq (car list) nil) | |
2145 | (setq list (cdr list))) | |
2146 | (puthash list (if undo-in-region t pending-undo-list) | |
2147 | undo-equiv-table)) | |
2512c9f0 RS |
2148 | ;; Don't specify a position in the undo record for the undo command. |
2149 | ;; Instead, undoing this should move point to where the change is. | |
2150 | (let ((tail buffer-undo-list) | |
003550c5 GM |
2151 | (prev nil)) |
2152 | (while (car tail) | |
2153 | (when (integerp (car tail)) | |
2154 | (let ((pos (car tail))) | |
1e96c007 SM |
2155 | (if prev |
2156 | (setcdr prev (cdr tail)) | |
2157 | (setq buffer-undo-list (cdr tail))) | |
003550c5 GM |
2158 | (setq tail (cdr tail)) |
2159 | (while (car tail) | |
2160 | (if (eq pos (car tail)) | |
2161 | (if prev | |
2162 | (setcdr prev (cdr tail)) | |
2163 | (setq buffer-undo-list (cdr tail))) | |
2164 | (setq prev tail)) | |
2165 | (setq tail (cdr tail))) | |
2166 | (setq tail nil))) | |
2167 | (setq prev tail tail (cdr tail)))) | |
e967cd11 RS |
2168 | ;; Record what the current undo list says, |
2169 | ;; so the next command can tell if the buffer was modified in between. | |
2076c87c | 2170 | (and modified (not (buffer-modified-p)) |
82f8cd94 CY |
2171 | (with-current-buffer base-buffer |
2172 | (delete-auto-save-file-if-necessary recent-save))) | |
cb3b2ec0 RS |
2173 | ;; Display a message announcing success. |
2174 | (if message | |
f6e7ec02 | 2175 | (message "%s" message)))) |
2076c87c | 2176 | |
e967cd11 RS |
2177 | (defun buffer-disable-undo (&optional buffer) |
2178 | "Make BUFFER stop keeping undo information. | |
2179 | No argument or nil as argument means do this for the current buffer." | |
2180 | (interactive) | |
0d808a63 | 2181 | (with-current-buffer (if buffer (get-buffer buffer) (current-buffer)) |
d020fce0 | 2182 | (setq buffer-undo-list t))) |
e967cd11 | 2183 | |
1e96c007 SM |
2184 | (defun undo-only (&optional arg) |
2185 | "Undo some previous changes. | |
2186 | Repeat this command to undo more changes. | |
5626c14e | 2187 | A numeric ARG serves as a repeat count. |
1e96c007 SM |
2188 | Contrary to `undo', this will not redo a previous undo." |
2189 | (interactive "*p") | |
2190 | (let ((undo-no-redo t)) (undo arg))) | |
1e96c007 | 2191 | |
52d1110d RS |
2192 | (defvar undo-in-progress nil |
2193 | "Non-nil while performing an undo. | |
2194 | Some change-hooks test this variable to do something different.") | |
2195 | ||
8ac28be5 | 2196 | (defun undo-more (n) |
2076c87c | 2197 | "Undo back N undo-boundaries beyond what was already undone recently. |
ff1fbe3e RS |
2198 | Call `undo-start' to get ready to undo recent changes, |
2199 | then call `undo-more' one or more times to undo them." | |
a7fe694c | 2200 | (or (listp pending-undo-list) |
71873e2b SM |
2201 | (user-error (concat "No further undo information" |
2202 | (and undo-in-region " for region")))) | |
52d1110d | 2203 | (let ((undo-in-progress t)) |
b553f685 AM |
2204 | ;; Note: The following, while pulling elements off |
2205 | ;; `pending-undo-list' will call primitive change functions which | |
2206 | ;; will push more elements onto `buffer-undo-list'. | |
8ac28be5 | 2207 | (setq pending-undo-list (primitive-undo n pending-undo-list)) |
a7fe694c RS |
2208 | (if (null pending-undo-list) |
2209 | (setq pending-undo-list t)))) | |
2076c87c | 2210 | |
3bace969 AH |
2211 | (defun primitive-undo (n list) |
2212 | "Undo N records from the front of the list LIST. | |
2213 | Return what remains of the list." | |
2214 | ||
2215 | ;; This is a good feature, but would make undo-start | |
2216 | ;; unable to do what is expected. | |
2217 | ;;(when (null (car (list))) | |
2218 | ;; ;; If the head of the list is a boundary, it is the boundary | |
2219 | ;; ;; preceding this command. Get rid of it and don't count it. | |
2220 | ;; (setq list (cdr list)))) | |
2221 | ||
2222 | (let ((arg n) | |
2223 | ;; In a writable buffer, enable undoing read-only text that is | |
2224 | ;; so because of text properties. | |
2225 | (inhibit-read-only t) | |
2226 | ;; Don't let `intangible' properties interfere with undo. | |
2227 | (inhibit-point-motion-hooks t) | |
2228 | ;; We use oldlist only to check for EQ. ++kfs | |
2229 | (oldlist buffer-undo-list) | |
2230 | (did-apply nil) | |
2231 | (next nil)) | |
2232 | (while (> arg 0) | |
a4648137 | 2233 | (while (setq next (pop list)) ;Exit inner loop at undo boundary. |
3bace969 | 2234 | ;; Handle an integer by setting point to that value. |
a4648137 SM |
2235 | (pcase next |
2236 | ((pred integerp) (goto-char next)) | |
2237 | ;; Element (t . TIME) records previous modtime. | |
2238 | ;; Preserve any flag of NONEXISTENT_MODTIME_NSECS or | |
2239 | ;; UNKNOWN_MODTIME_NSECS. | |
2240 | (`(t . ,time) | |
2241 | ;; If this records an obsolete save | |
2242 | ;; (not matching the actual disk file) | |
2243 | ;; then don't mark unmodified. | |
2244 | (when (or (equal time (visited-file-modtime)) | |
2245 | (and (consp time) | |
2246 | (equal (list (car time) (cdr time)) | |
2247 | (visited-file-modtime)))) | |
2248 | (when (fboundp 'unlock-buffer) | |
2249 | (unlock-buffer)) | |
2250 | (set-buffer-modified-p nil))) | |
2251 | ;; Element (nil PROP VAL BEG . END) is property change. | |
2252 | (`(nil . ,(or `(,prop ,val ,beg . ,end) pcase--dontcare)) | |
2253 | (when (or (> (point-min) beg) (< (point-max) end)) | |
2254 | (error "Changes to be undone are outside visible portion of buffer")) | |
2255 | (put-text-property beg end prop val)) | |
2256 | ;; Element (BEG . END) means range was inserted. | |
2257 | (`(,(and beg (pred integerp)) . ,(and end (pred integerp))) | |
2258 | ;; (and `(,beg . ,end) `(,(pred integerp) . ,(pred integerp))) | |
2259 | ;; Ideally: `(,(pred integerp beg) . ,(pred integerp end)) | |
2260 | (when (or (> (point-min) beg) (< (point-max) end)) | |
2261 | (error "Changes to be undone are outside visible portion of buffer")) | |
2262 | ;; Set point first thing, so that undoing this undo | |
2263 | ;; does not send point back to where it is now. | |
2264 | (goto-char beg) | |
2265 | (delete-region beg end)) | |
2266 | ;; Element (apply FUN . ARGS) means call FUN to undo. | |
2267 | (`(apply . ,fun-args) | |
2268 | (let ((currbuff (current-buffer))) | |
2269 | (if (integerp (car fun-args)) | |
2270 | ;; Long format: (apply DELTA START END FUN . ARGS). | |
2271 | (pcase-let* ((`(,delta ,start ,end ,fun . ,args) fun-args) | |
2272 | (start-mark (copy-marker start nil)) | |
2273 | (end-mark (copy-marker end t))) | |
2274 | (when (or (> (point-min) start) (< (point-max) end)) | |
2275 | (error "Changes to be undone are outside visible portion of buffer")) | |
2276 | (apply fun args) ;; Use `save-current-buffer'? | |
2277 | ;; Check that the function did what the entry | |
2278 | ;; said it would do. | |
2279 | (unless (and (= start start-mark) | |
2280 | (= (+ delta end) end-mark)) | |
2281 | (error "Changes to be undone by function different than announced")) | |
2282 | (set-marker start-mark nil) | |
2283 | (set-marker end-mark nil)) | |
2284 | (apply fun-args)) | |
2285 | (unless (eq currbuff (current-buffer)) | |
2286 | (error "Undo function switched buffer")) | |
2287 | (setq did-apply t))) | |
2288 | ;; Element (STRING . POS) means STRING was deleted. | |
2289 | (`(,(and string (pred stringp)) . ,(and pos (pred integerp))) | |
2290 | (when (let ((apos (abs pos))) | |
2291 | (or (< apos (point-min)) (> apos (point-max)))) | |
2292 | (error "Changes to be undone are outside visible portion of buffer")) | |
2293 | (if (< pos 0) | |
2294 | (progn | |
2295 | (goto-char (- pos)) | |
2296 | (insert string)) | |
2297 | (goto-char pos) | |
2298 | ;; Now that we record marker adjustments | |
2299 | ;; (caused by deletion) for undo, | |
2300 | ;; we should always insert after markers, | |
2301 | ;; so that undoing the marker adjustments | |
2302 | ;; put the markers back in the right place. | |
2303 | (insert string) | |
2304 | (goto-char pos))) | |
2305 | ;; (MARKER . OFFSET) means a marker MARKER was adjusted by OFFSET. | |
2306 | (`(,(and marker (pred markerp)) . ,(and offset (pred integerp))) | |
2307 | (when (marker-buffer marker) | |
2308 | (set-marker marker | |
2309 | (- marker offset) | |
2310 | (marker-buffer marker)))) | |
2311 | (_ (error "Unrecognized entry in undo list %S" next)))) | |
3bace969 AH |
2312 | (setq arg (1- arg))) |
2313 | ;; Make sure an apply entry produces at least one undo entry, | |
2314 | ;; so the test in `undo' for continuing an undo series | |
2315 | ;; will work right. | |
2316 | (if (and did-apply | |
2317 | (eq oldlist buffer-undo-list)) | |
2318 | (setq buffer-undo-list | |
2319 | (cons (list 'apply 'cdr nil) buffer-undo-list)))) | |
2320 | list) | |
2321 | ||
65627aad RS |
2322 | ;; Deep copy of a list |
2323 | (defun undo-copy-list (list) | |
2324 | "Make a copy of undo list LIST." | |
2325 | (mapcar 'undo-copy-list-1 list)) | |
2326 | ||
2327 | (defun undo-copy-list-1 (elt) | |
2328 | (if (consp elt) | |
2329 | (cons (car elt) (undo-copy-list-1 (cdr elt))) | |
2330 | elt)) | |
2331 | ||
2332 | (defun undo-start (&optional beg end) | |
2333 | "Set `pending-undo-list' to the front of the undo list. | |
2334 | The next call to `undo-more' will undo the most recently made change. | |
2335 | If BEG and END are specified, then only undo elements | |
2336 | that apply to text between BEG and END are used; other undo elements | |
2337 | are ignored. If BEG and END are nil, all undo elements are used." | |
2338 | (if (eq buffer-undo-list t) | |
71873e2b | 2339 | (user-error "No undo information in this buffer")) |
1e722f9f | 2340 | (setq pending-undo-list |
65627aad RS |
2341 | (if (and beg end (not (= beg end))) |
2342 | (undo-make-selective-list (min beg end) (max beg end)) | |
2343 | buffer-undo-list))) | |
2344 | ||
2345 | (defvar undo-adjusted-markers) | |
2346 | ||
2347 | (defun undo-make-selective-list (start end) | |
2348 | "Return a list of undo elements for the region START to END. | |
2349 | The elements come from `buffer-undo-list', but we keep only | |
2350 | the elements inside this region, and discard those outside this region. | |
2351 | If we find an element that crosses an edge of this region, | |
2352 | we stop and ignore all further elements." | |
2353 | (let ((undo-list-copy (undo-copy-list buffer-undo-list)) | |
2354 | (undo-list (list nil)) | |
2355 | undo-adjusted-markers | |
2356 | some-rejected | |
06b60517 | 2357 | undo-elt temp-undo-list delta) |
65627aad RS |
2358 | (while undo-list-copy |
2359 | (setq undo-elt (car undo-list-copy)) | |
2360 | (let ((keep-this | |
2361 | (cond ((and (consp undo-elt) (eq (car undo-elt) t)) | |
2362 | ;; This is a "was unmodified" element. | |
2363 | ;; Keep it if we have kept everything thus far. | |
2364 | (not some-rejected)) | |
2365 | (t | |
2366 | (undo-elt-in-region undo-elt start end))))) | |
2367 | (if keep-this | |
2368 | (progn | |
2369 | (setq end (+ end (cdr (undo-delta undo-elt)))) | |
2370 | ;; Don't put two nils together in the list | |
2371 | (if (not (and (eq (car undo-list) nil) | |
2372 | (eq undo-elt nil))) | |
2373 | (setq undo-list (cons undo-elt undo-list)))) | |
2374 | (if (undo-elt-crosses-region undo-elt start end) | |
2375 | (setq undo-list-copy nil) | |
2376 | (setq some-rejected t) | |
2377 | (setq temp-undo-list (cdr undo-list-copy)) | |
2378 | (setq delta (undo-delta undo-elt)) | |
2379 | ||
2380 | (when (/= (cdr delta) 0) | |
2381 | (let ((position (car delta)) | |
2382 | (offset (cdr delta))) | |
2383 | ||
e1e04350 SM |
2384 | ;; Loop down the earlier events adjusting their buffer |
2385 | ;; positions to reflect the fact that a change to the buffer | |
2386 | ;; isn't being undone. We only need to process those element | |
2387 | ;; types which undo-elt-in-region will return as being in | |
2388 | ;; the region since only those types can ever get into the | |
2389 | ;; output | |
65627aad RS |
2390 | |
2391 | (while temp-undo-list | |
2392 | (setq undo-elt (car temp-undo-list)) | |
2393 | (cond ((integerp undo-elt) | |
2394 | (if (>= undo-elt position) | |
2395 | (setcar temp-undo-list (- undo-elt offset)))) | |
2396 | ((atom undo-elt) nil) | |
2397 | ((stringp (car undo-elt)) | |
2398 | ;; (TEXT . POSITION) | |
2399 | (let ((text-pos (abs (cdr undo-elt))) | |
2400 | (point-at-end (< (cdr undo-elt) 0 ))) | |
2401 | (if (>= text-pos position) | |
1e722f9f | 2402 | (setcdr undo-elt (* (if point-at-end -1 1) |
65627aad RS |
2403 | (- text-pos offset)))))) |
2404 | ((integerp (car undo-elt)) | |
2405 | ;; (BEGIN . END) | |
2406 | (when (>= (car undo-elt) position) | |
2407 | (setcar undo-elt (- (car undo-elt) offset)) | |
2408 | (setcdr undo-elt (- (cdr undo-elt) offset)))) | |
2409 | ((null (car undo-elt)) | |
2410 | ;; (nil PROPERTY VALUE BEG . END) | |
2411 | (let ((tail (nthcdr 3 undo-elt))) | |
2412 | (when (>= (car tail) position) | |
2413 | (setcar tail (- (car tail) offset)) | |
2414 | (setcdr tail (- (cdr tail) offset)))))) | |
2415 | (setq temp-undo-list (cdr temp-undo-list)))))))) | |
2416 | (setq undo-list-copy (cdr undo-list-copy))) | |
2417 | (nreverse undo-list))) | |
2418 | ||
2419 | (defun undo-elt-in-region (undo-elt start end) | |
2420 | "Determine whether UNDO-ELT falls inside the region START ... END. | |
2421 | If it crosses the edge, we return nil." | |
2422 | (cond ((integerp undo-elt) | |
2423 | (and (>= undo-elt start) | |
12a93712 | 2424 | (<= undo-elt end))) |
65627aad RS |
2425 | ((eq undo-elt nil) |
2426 | t) | |
2427 | ((atom undo-elt) | |
2428 | nil) | |
2429 | ((stringp (car undo-elt)) | |
2430 | ;; (TEXT . POSITION) | |
2431 | (and (>= (abs (cdr undo-elt)) start) | |
2432 | (< (abs (cdr undo-elt)) end))) | |
2433 | ((and (consp undo-elt) (markerp (car undo-elt))) | |
2434 | ;; This is a marker-adjustment element (MARKER . ADJUSTMENT). | |
2435 | ;; See if MARKER is inside the region. | |
2436 | (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers))) | |
2437 | (unless alist-elt | |
2438 | (setq alist-elt (cons (car undo-elt) | |
2439 | (marker-position (car undo-elt)))) | |
2440 | (setq undo-adjusted-markers | |
2441 | (cons alist-elt undo-adjusted-markers))) | |
2442 | (and (cdr alist-elt) | |
2443 | (>= (cdr alist-elt) start) | |
12a93712 | 2444 | (<= (cdr alist-elt) end)))) |
65627aad RS |
2445 | ((null (car undo-elt)) |
2446 | ;; (nil PROPERTY VALUE BEG . END) | |
2447 | (let ((tail (nthcdr 3 undo-elt))) | |
2448 | (and (>= (car tail) start) | |
12a93712 | 2449 | (<= (cdr tail) end)))) |
65627aad RS |
2450 | ((integerp (car undo-elt)) |
2451 | ;; (BEGIN . END) | |
2452 | (and (>= (car undo-elt) start) | |
12a93712 | 2453 | (<= (cdr undo-elt) end))))) |
65627aad RS |
2454 | |
2455 | (defun undo-elt-crosses-region (undo-elt start end) | |
2456 | "Test whether UNDO-ELT crosses one edge of that region START ... END. | |
2457 | This assumes we have already decided that UNDO-ELT | |
2458 | is not *inside* the region START...END." | |
2459 | (cond ((atom undo-elt) nil) | |
2460 | ((null (car undo-elt)) | |
2461 | ;; (nil PROPERTY VALUE BEG . END) | |
2462 | (let ((tail (nthcdr 3 undo-elt))) | |
1f8a132d RS |
2463 | (and (< (car tail) end) |
2464 | (> (cdr tail) start)))) | |
65627aad RS |
2465 | ((integerp (car undo-elt)) |
2466 | ;; (BEGIN . END) | |
1f8a132d RS |
2467 | (and (< (car undo-elt) end) |
2468 | (> (cdr undo-elt) start))))) | |
65627aad RS |
2469 | |
2470 | ;; Return the first affected buffer position and the delta for an undo element | |
2471 | ;; delta is defined as the change in subsequent buffer positions if we *did* | |
2472 | ;; the undo. | |
2473 | (defun undo-delta (undo-elt) | |
2474 | (if (consp undo-elt) | |
2475 | (cond ((stringp (car undo-elt)) | |
2476 | ;; (TEXT . POSITION) | |
2477 | (cons (abs (cdr undo-elt)) (length (car undo-elt)))) | |
2478 | ((integerp (car undo-elt)) | |
2479 | ;; (BEGIN . END) | |
2480 | (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt)))) | |
2481 | (t | |
2482 | '(0 . 0))) | |
2483 | '(0 . 0))) | |
b6e8e8e5 | 2484 | |
1223933d | 2485 | (defcustom undo-ask-before-discard nil |
28cb725d LT |
2486 | "If non-nil ask about discarding undo info for the current command. |
2487 | Normally, Emacs discards the undo info for the current command if | |
2488 | it exceeds `undo-outer-limit'. But if you set this option | |
2489 | non-nil, it asks in the echo area whether to discard the info. | |
a3545af4 | 2490 | If you answer no, there is a slight risk that Emacs might crash, so |
28cb725d LT |
2491 | only do it if you really want to undo the command. |
2492 | ||
2493 | This option is mainly intended for debugging. You have to be | |
2494 | careful if you use it for other purposes. Garbage collection is | |
2495 | inhibited while the question is asked, meaning that Emacs might | |
2496 | leak memory. So you should make sure that you do not wait | |
2497 | excessively long before answering the question." | |
2498 | :type 'boolean | |
2499 | :group 'undo | |
bf247b6e | 2500 | :version "22.1") |
28cb725d | 2501 | |
a1a801de RS |
2502 | (defvar undo-extra-outer-limit nil |
2503 | "If non-nil, an extra level of size that's ok in an undo item. | |
2504 | We don't ask the user about truncating the undo list until the | |
28cb725d LT |
2505 | current item gets bigger than this amount. |
2506 | ||
2507 | This variable only matters if `undo-ask-before-discard' is non-nil.") | |
a1a801de RS |
2508 | (make-variable-buffer-local 'undo-extra-outer-limit) |
2509 | ||
28cb725d LT |
2510 | ;; When the first undo batch in an undo list is longer than |
2511 | ;; undo-outer-limit, this function gets called to warn the user that | |
2512 | ;; the undo info for the current command was discarded. Garbage | |
2513 | ;; collection is inhibited around the call, so it had better not do a | |
2514 | ;; lot of consing. | |
b6e8e8e5 RS |
2515 | (setq undo-outer-limit-function 'undo-outer-limit-truncate) |
2516 | (defun undo-outer-limit-truncate (size) | |
28cb725d LT |
2517 | (if undo-ask-before-discard |
2518 | (when (or (null undo-extra-outer-limit) | |
2519 | (> size undo-extra-outer-limit)) | |
2520 | ;; Don't ask the question again unless it gets even bigger. | |
2521 | ;; This applies, in particular, if the user quits from the question. | |
2522 | ;; Such a quit quits out of GC, but something else will call GC | |
2523 | ;; again momentarily. It will call this function again, | |
2524 | ;; but we don't want to ask the question again. | |
2525 | (setq undo-extra-outer-limit (+ size 50000)) | |
2526 | (if (let (use-dialog-box track-mouse executing-kbd-macro ) | |
d5aa078b | 2527 | (yes-or-no-p (format "Buffer `%s' undo info is %d bytes long; discard it? " |
28cb725d LT |
2528 | (buffer-name) size))) |
2529 | (progn (setq buffer-undo-list nil) | |
2530 | (setq undo-extra-outer-limit nil) | |
2531 | t) | |
2532 | nil)) | |
2533 | (display-warning '(undo discard-info) | |
2534 | (concat | |
d5aa078b | 2535 | (format "Buffer `%s' undo info was %d bytes long.\n" |
28cb725d LT |
2536 | (buffer-name) size) |
2537 | "The undo info was discarded because it exceeded \ | |
2538 | `undo-outer-limit'. | |
2539 | ||
2540 | This is normal if you executed a command that made a huge change | |
2541 | to the buffer. In that case, to prevent similar problems in the | |
2542 | future, set `undo-outer-limit' to a value that is large enough to | |
2543 | cover the maximum size of normal changes you expect a single | |
2544 | command to make, but not so large that it might exceed the | |
2545 | maximum memory allotted to Emacs. | |
2546 | ||
2547 | If you did not execute any such command, the situation is | |
2548 | probably due to a bug and you should report it. | |
2549 | ||
2550 | You can disable the popping up of this buffer by adding the entry | |
14f01bef CY |
2551 | \(undo discard-info) to the user option `warning-suppress-types', |
2552 | which is defined in the `warnings' library.\n") | |
28cb725d LT |
2553 | :warning) |
2554 | (setq buffer-undo-list nil) | |
2555 | t)) | |
e1e04350 | 2556 | \f |
009ef402 | 2557 | (defvar shell-command-history nil |
e5f0c02f EZ |
2558 | "History list for some commands that read shell commands. |
2559 | ||
2560 | Maximum length of the history list is determined by the value | |
2561 | of `history-length', which see.") | |
009ef402 | 2562 | |
6d341a2a | 2563 | (defvar shell-command-switch (purecopy "-c") |
59fc41e5 RS |
2564 | "Switch used to have the shell execute its command line argument.") |
2565 | ||
cc039f78 | 2566 | (defvar shell-command-default-error-buffer nil |
fb7ada5f | 2567 | "Buffer name for `shell-command' and `shell-command-on-region' error output. |
637fff82 | 2568 | This buffer is used when `shell-command' or `shell-command-on-region' |
cc039f78 KH |
2569 | is run interactively. A value of nil means that output to stderr and |
2570 | stdout will be intermixed in the output stream.") | |
2571 | ||
a98a2fe8 | 2572 | (declare-function mailcap-file-default-commands "mailcap" (files)) |
e0987650 | 2573 | (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep)) |
a98a2fe8 JL |
2574 | |
2575 | (defun minibuffer-default-add-shell-commands () | |
263bc3fa | 2576 | "Return a list of all commands associated with the current file. |
086a7dd4 | 2577 | This function is used to add all related commands retrieved by `mailcap' |
a98a2fe8 JL |
2578 | to the end of the list of defaults just after the default value." |
2579 | (interactive) | |
2580 | (let* ((filename (if (listp minibuffer-default) | |
2581 | (car minibuffer-default) | |
2582 | minibuffer-default)) | |
2583 | (commands (and filename (require 'mailcap nil t) | |
2584 | (mailcap-file-default-commands (list filename))))) | |
2585 | (setq commands (mapcar (lambda (command) | |
2586 | (concat command " " filename)) | |
2587 | commands)) | |
2588 | (if (listp minibuffer-default) | |
2589 | (append minibuffer-default commands) | |
2590 | (cons minibuffer-default commands)))) | |
2591 | ||
914a0ae1 | 2592 | (declare-function shell-completion-vars "shell" ()) |
2e7bd464 | 2593 | |
e5c4079c SM |
2594 | (defvar minibuffer-local-shell-command-map |
2595 | (let ((map (make-sparse-keymap))) | |
2596 | (set-keymap-parent map minibuffer-local-map) | |
1bcace58 | 2597 | (define-key map "\t" 'completion-at-point) |
e5c4079c | 2598 | map) |
1d2b0303 | 2599 | "Keymap used for completing shell commands in minibuffer.") |
e5c4079c SM |
2600 | |
2601 | (defun read-shell-command (prompt &optional initial-contents hist &rest args) | |
2602 | "Read a shell command from the minibuffer. | |
2603 | The arguments are the same as the ones of `read-from-minibuffer', | |
2604 | except READ and KEYMAP are missing and HIST defaults | |
2605 | to `shell-command-history'." | |
1bcace58 | 2606 | (require 'shell) |
d6601455 JL |
2607 | (minibuffer-with-setup-hook |
2608 | (lambda () | |
914a0ae1 | 2609 | (shell-completion-vars) |
d6601455 JL |
2610 | (set (make-local-variable 'minibuffer-default-add-function) |
2611 | 'minibuffer-default-add-shell-commands)) | |
2612 | (apply 'read-from-minibuffer prompt initial-contents | |
2613 | minibuffer-local-shell-command-map | |
2614 | nil | |
2615 | (or hist 'shell-command-history) | |
2616 | args))) | |
e5c4079c | 2617 | |
17711ed9 JL |
2618 | (defcustom async-shell-command-buffer 'confirm-new-buffer |
2619 | "What to do when the output buffer is used by another shell command. | |
2620 | This option specifies how to resolve the conflict where a new command | |
2621 | wants to direct its output to the buffer `*Async Shell Command*', | |
2622 | but this buffer is already taken by another running shell command. | |
2623 | ||
2624 | The value `confirm-kill-process' is used to ask for confirmation before | |
2625 | killing the already running process and running a new process | |
2626 | in the same buffer, `confirm-new-buffer' for confirmation before running | |
2627 | the command in a new buffer with a name other than the default buffer name, | |
2628 | `new-buffer' for doing the same without confirmation, | |
2629 | `confirm-rename-buffer' for confirmation before renaming the existing | |
2630 | output buffer and running a new command in the default buffer, | |
2631 | `rename-buffer' for doing the same without confirmation." | |
2632 | :type '(choice (const :tag "Confirm killing of running command" | |
2633 | confirm-kill-process) | |
2634 | (const :tag "Confirm creation of a new buffer" | |
2635 | confirm-new-buffer) | |
2636 | (const :tag "Create a new buffer" | |
2637 | new-buffer) | |
2638 | (const :tag "Confirm renaming of existing buffer" | |
2639 | confirm-rename-buffer) | |
2640 | (const :tag "Rename the existing buffer" | |
2641 | rename-buffer)) | |
2642 | :group 'shell | |
2a1e2476 | 2643 | :version "24.3") |
17711ed9 | 2644 | |
c945a962 JL |
2645 | (defun async-shell-command (command &optional output-buffer error-buffer) |
2646 | "Execute string COMMAND asynchronously in background. | |
2647 | ||
6dafa0d5 JL |
2648 | Like `shell-command', but adds `&' at the end of COMMAND |
2649 | to execute it asynchronously. | |
2650 | ||
5ad4bef5 | 2651 | The output appears in the buffer `*Async Shell Command*'. |
6dafa0d5 | 2652 | That buffer is in shell mode. |
5ad4bef5 | 2653 | |
9139632a JL |
2654 | You can configure `async-shell-command-buffer' to specify what to do in |
2655 | case when `*Async Shell Command*' buffer is already taken by another | |
2656 | running shell command. To run COMMAND without displaying the output | |
2657 | in a window you can configure `display-buffer-alist' to use the action | |
2658 | `display-buffer-no-window' for the buffer `*Async Shell Command*'. | |
2659 | ||
5ad4bef5 SM |
2660 | In Elisp, you will often be better served by calling `start-process' |
2661 | directly, since it offers more control and does not impose the use of a | |
2662 | shell (with its need to quote arguments)." | |
c945a962 JL |
2663 | (interactive |
2664 | (list | |
2665 | (read-shell-command "Async shell command: " nil nil | |
6dafa0d5 JL |
2666 | (let ((filename |
2667 | (cond | |
2668 | (buffer-file-name) | |
2669 | ((eq major-mode 'dired-mode) | |
2670 | (dired-get-filename nil t))))) | |
2671 | (and filename (file-relative-name filename)))) | |
c945a962 JL |
2672 | current-prefix-arg |
2673 | shell-command-default-error-buffer)) | |
2674 | (unless (string-match "&[ \t]*\\'" command) | |
2675 | (setq command (concat command " &"))) | |
2676 | (shell-command command output-buffer error-buffer)) | |
2677 | ||
cc039f78 | 2678 | (defun shell-command (command &optional output-buffer error-buffer) |
2076c87c | 2679 | "Execute string COMMAND in inferior shell; display output, if any. |
0b3f96d4 | 2680 | With prefix argument, insert the COMMAND's output at point. |
d382f610 | 2681 | |
6dafa0d5 | 2682 | If COMMAND ends in `&', execute it asynchronously. |
d382f610 | 2683 | The output appears in the buffer `*Async Shell Command*'. |
6dafa0d5 JL |
2684 | That buffer is in shell mode. You can also use |
2685 | `async-shell-command' that automatically adds `&'. | |
d382f610 | 2686 | |
939ac10c GM |
2687 | Otherwise, COMMAND is executed synchronously. The output appears in |
2688 | the buffer `*Shell Command Output*'. If the output is short enough to | |
2689 | display in the echo area (which is determined by the variables | |
2690 | `resize-mini-windows' and `max-mini-window-height'), it is shown | |
2691 | there, but it is nonetheless available in buffer `*Shell Command | |
e1e04350 | 2692 | Output*' even though that buffer is not automatically displayed. |
d0d74413 | 2693 | |
07f458c1 | 2694 | To specify a coding system for converting non-ASCII characters |
5626c14e | 2695 | in the shell command output, use \\[universal-coding-system-argument] \ |
07f458c1 RS |
2696 | before this command. |
2697 | ||
2698 | Noninteractive callers can specify coding systems by binding | |
2699 | `coding-system-for-read' and `coding-system-for-write'. | |
2700 | ||
d0d74413 RS |
2701 | The optional second argument OUTPUT-BUFFER, if non-nil, |
2702 | says to put the output in some other buffer. | |
2703 | If OUTPUT-BUFFER is a buffer or buffer name, put the output there. | |
2704 | If OUTPUT-BUFFER is not a buffer and not nil, | |
2705 | insert output in current buffer. (This cannot be done asynchronously.) | |
e7791447 CY |
2706 | In either case, the buffer is first erased, and the output is |
2707 | inserted after point (leaving mark after it). | |
cc039f78 | 2708 | |
2e033693 RS |
2709 | If the command terminates without error, but generates output, |
2710 | and you did not specify \"insert it in the current buffer\", | |
2711 | the output can be displayed in the echo area or in its buffer. | |
2712 | If the output is short enough to display in the echo area | |
2713 | \(determined by the variable `max-mini-window-height' if | |
5626c14e JB |
2714 | `resize-mini-windows' is non-nil), it is shown there. |
2715 | Otherwise,the buffer containing the output is displayed. | |
2e033693 RS |
2716 | |
2717 | If there is output and an error, and you did not specify \"insert it | |
2718 | in the current buffer\", a message about the error goes at the end | |
2719 | of the output. | |
2720 | ||
2721 | If there is no output, or if output is inserted in the current buffer, | |
2722 | then `*Shell Command Output*' is deleted. | |
2723 | ||
cc039f78 KH |
2724 | If the optional third argument ERROR-BUFFER is non-nil, it is a buffer |
2725 | or buffer name to which to direct the command's standard error output. | |
2726 | If it is nil, error output is mingled with regular output. | |
2727 | In an interactive call, the variable `shell-command-default-error-buffer' | |
5ad4bef5 SM |
2728 | specifies the value of ERROR-BUFFER. |
2729 | ||
2730 | In Elisp, you will often be better served by calling `call-process' or | |
2731 | `start-process' directly, since it offers more control and does not impose | |
2732 | the use of a shell (with its need to quote arguments)." | |
cc039f78 | 2733 | |
a98a2fe8 JL |
2734 | (interactive |
2735 | (list | |
d6601455 | 2736 | (read-shell-command "Shell command: " nil nil |
e0987650 JL |
2737 | (let ((filename |
2738 | (cond | |
2739 | (buffer-file-name) | |
2740 | ((eq major-mode 'dired-mode) | |
2741 | (dired-get-filename nil t))))) | |
2742 | (and filename (file-relative-name filename)))) | |
a98a2fe8 JL |
2743 | current-prefix-arg |
2744 | shell-command-default-error-buffer)) | |
c7edd03c KH |
2745 | ;; Look for a handler in case default-directory is a remote file name. |
2746 | (let ((handler | |
2747 | (find-file-name-handler (directory-file-name default-directory) | |
2748 | 'shell-command))) | |
2749 | (if handler | |
cc039f78 | 2750 | (funcall handler 'shell-command command output-buffer error-buffer) |
c7edd03c KH |
2751 | (if (and output-buffer |
2752 | (not (or (bufferp output-buffer) (stringp output-buffer)))) | |
2e033693 | 2753 | ;; Output goes in current buffer. |
cc039f78 | 2754 | (let ((error-file |
1e722f9f | 2755 | (if error-buffer |
b005abd5 | 2756 | (make-temp-file |
171a45d9 EZ |
2757 | (expand-file-name "scor" |
2758 | (or small-temporary-file-directory | |
2759 | temporary-file-directory))) | |
cc039f78 KH |
2760 | nil))) |
2761 | (barf-if-buffer-read-only) | |
63437623 | 2762 | (push-mark nil t) |
cc039f78 KH |
2763 | ;; We do not use -f for csh; we will not support broken use of |
2764 | ;; .cshrcs. Even the BSD csh manual says to use | |
2765 | ;; "if ($?prompt) exit" before things which are not useful | |
2766 | ;; non-interactively. Besides, if someone wants their other | |
2767 | ;; aliases for shell commands then they can still have them. | |
1e722f9f | 2768 | (call-process shell-file-name nil |
cc039f78 KH |
2769 | (if error-file |
2770 | (list t error-file) | |
2771 | t) | |
2772 | nil shell-command-switch command) | |
2773 | (when (and error-file (file-exists-p error-file)) | |
2774 | (if (< 0 (nth 7 (file-attributes error-file))) | |
2775 | (with-current-buffer (get-buffer-create error-buffer) | |
2776 | (let ((pos-from-end (- (point-max) (point)))) | |
2777 | (or (bobp) | |
2778 | (insert "\f\n")) | |
2779 | ;; Do no formatting while reading error file, | |
2780 | ;; because that can run a shell command, and we | |
2781 | ;; don't want that to cause an infinite recursion. | |
2782 | (format-insert-file error-file nil) | |
2783 | ;; Put point after the inserted errors. | |
2784 | (goto-char (- (point-max) pos-from-end))) | |
2785 | (display-buffer (current-buffer)))) | |
2786 | (delete-file error-file)) | |
2787 | ;; This is like exchange-point-and-mark, but doesn't | |
2788 | ;; activate the mark. It is cleaner to avoid activation, | |
2789 | ;; even though the command loop would deactivate the mark | |
2790 | ;; because we inserted text. | |
2791 | (goto-char (prog1 (mark t) | |
2792 | (set-marker (mark-marker) (point) | |
2793 | (current-buffer))))) | |
2e033693 | 2794 | ;; Output goes in a separate buffer. |
c7edd03c KH |
2795 | ;; Preserve the match data in case called from a program. |
2796 | (save-match-data | |
aab5d2c5 | 2797 | (if (string-match "[ \t]*&[ \t]*\\'" command) |
c7edd03c KH |
2798 | ;; Command ending with ampersand means asynchronous. |
2799 | (let ((buffer (get-buffer-create | |
2800 | (or output-buffer "*Async Shell Command*"))) | |
2801 | (directory default-directory) | |
2802 | proc) | |
2803 | ;; Remove the ampersand. | |
2804 | (setq command (substring command 0 (match-beginning 0))) | |
17711ed9 | 2805 | ;; Ask the user what to do with already running process. |
c7edd03c | 2806 | (setq proc (get-buffer-process buffer)) |
17711ed9 JL |
2807 | (when proc |
2808 | (cond | |
2809 | ((eq async-shell-command-buffer 'confirm-kill-process) | |
2810 | ;; If will kill a process, query first. | |
2811 | (if (yes-or-no-p "A command is running in the default buffer. Kill it? ") | |
c7edd03c KH |
2812 | (kill-process proc) |
2813 | (error "Shell command in progress"))) | |
17711ed9 JL |
2814 | ((eq async-shell-command-buffer 'confirm-new-buffer) |
2815 | ;; If will create a new buffer, query first. | |
2816 | (if (yes-or-no-p "A command is running in the default buffer. Use a new buffer? ") | |
2817 | (setq buffer (generate-new-buffer | |
2818 | (or output-buffer "*Async Shell Command*"))) | |
2819 | (error "Shell command in progress"))) | |
2820 | ((eq async-shell-command-buffer 'new-buffer) | |
2821 | ;; It will create a new buffer. | |
2822 | (setq buffer (generate-new-buffer | |
2823 | (or output-buffer "*Async Shell Command*")))) | |
2824 | ((eq async-shell-command-buffer 'confirm-rename-buffer) | |
2825 | ;; If will rename the buffer, query first. | |
2826 | (if (yes-or-no-p "A command is running in the default buffer. Rename it? ") | |
2827 | (progn | |
2828 | (with-current-buffer buffer | |
2829 | (rename-uniquely)) | |
2830 | (setq buffer (get-buffer-create | |
2831 | (or output-buffer "*Async Shell Command*")))) | |
2832 | (error "Shell command in progress"))) | |
2833 | ((eq async-shell-command-buffer 'rename-buffer) | |
2834 | ;; It will rename the buffer. | |
2835 | (with-current-buffer buffer | |
2836 | (rename-uniquely)) | |
2837 | (setq buffer (get-buffer-create | |
2838 | (or output-buffer "*Async Shell Command*")))))) | |
1e96c007 | 2839 | (with-current-buffer buffer |
c7edd03c | 2840 | (setq buffer-read-only nil) |
50f8cd96 TH |
2841 | ;; Setting buffer-read-only to nil doesn't suffice |
2842 | ;; if some text has a non-nil read-only property, | |
2843 | ;; which comint sometimes adds for prompts. | |
2844 | (let ((inhibit-read-only t)) | |
2845 | (erase-buffer)) | |
9139632a | 2846 | (display-buffer buffer '(nil (allow-no-window . t))) |
c7edd03c | 2847 | (setq default-directory directory) |
1e722f9f | 2848 | (setq proc (start-process "Shell" buffer shell-file-name |
c7edd03c KH |
2849 | shell-command-switch command)) |
2850 | (setq mode-line-process '(":%s")) | |
c2020c27 | 2851 | (require 'shell) (shell-mode) |
c7edd03c | 2852 | (set-process-sentinel proc 'shell-command-sentinel) |
50c737e4 JL |
2853 | ;; Use the comint filter for proper handling of carriage motion |
2854 | ;; (see `comint-inhibit-carriage-motion'),. | |
2855 | (set-process-filter proc 'comint-output-filter) | |
c7edd03c | 2856 | )) |
50c737e4 | 2857 | ;; Otherwise, command is executed synchronously. |
cc039f78 KH |
2858 | (shell-command-on-region (point) (point) command |
2859 | output-buffer nil error-buffer))))))) | |
eaae8106 | 2860 | |
f69aad2b MB |
2861 | (defun display-message-or-buffer (message |
2862 | &optional buffer-name not-this-window frame) | |
2863 | "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer. | |
2864 | MESSAGE may be either a string or a buffer. | |
2865 | ||
2866 | A buffer is displayed using `display-buffer' if MESSAGE is too long for | |
939ac10c GM |
2867 | the maximum height of the echo area, as defined by `max-mini-window-height' |
2868 | if `resize-mini-windows' is non-nil. | |
f69aad2b | 2869 | |
2a3f00bf MB |
2870 | Returns either the string shown in the echo area, or when a pop-up |
2871 | buffer is used, the window used to display it. | |
2872 | ||
f69aad2b MB |
2873 | If MESSAGE is a string, then the optional argument BUFFER-NAME is the |
2874 | name of the buffer used to display it in the case where a pop-up buffer | |
2875 | is used, defaulting to `*Message*'. In the case where MESSAGE is a | |
2876 | string and it is displayed in the echo area, it is not specified whether | |
2877 | the contents are inserted into the buffer anyway. | |
2878 | ||
2879 | Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer', | |
2880 | and only used if a buffer is displayed." | |
39a8d88a | 2881 | (cond ((and (stringp message) (not (string-match "\n" message))) |
f69aad2b MB |
2882 | ;; Trivial case where we can use the echo area |
2883 | (message "%s" message)) | |
2884 | ((and (stringp message) | |
39a8d88a | 2885 | (= (string-match "\n" message) (1- (length message)))) |
f69aad2b MB |
2886 | ;; Trivial case where we can just remove single trailing newline |
2887 | (message "%s" (substring message 0 (1- (length message))))) | |
2888 | (t | |
2889 | ;; General case | |
2890 | (with-current-buffer | |
2891 | (if (bufferp message) | |
2892 | message | |
2893 | (get-buffer-create (or buffer-name "*Message*"))) | |
2894 | ||
2895 | (unless (bufferp message) | |
2896 | (erase-buffer) | |
2897 | (insert message)) | |
2898 | ||
2899 | (let ((lines | |
2900 | (if (= (buffer-size) 0) | |
2901 | 0 | |
62ffcd76 | 2902 | (count-screen-lines nil nil nil (minibuffer-window))))) |
4f017185 RS |
2903 | (cond ((= lines 0)) |
2904 | ((and (or (<= lines 1) | |
aab5d2c5 RS |
2905 | (<= lines |
2906 | (if resize-mini-windows | |
2907 | (cond ((floatp max-mini-window-height) | |
2908 | (* (frame-height) | |
2909 | max-mini-window-height)) | |
2910 | ((integerp max-mini-window-height) | |
2911 | max-mini-window-height) | |
2912 | (t | |
2913 | 1)) | |
2914 | 1))) | |
2915 | ;; Don't use the echo area if the output buffer is | |
4c36be58 | 2916 | ;; already displayed in the selected frame. |
61b80ebf | 2917 | (not (get-buffer-window (current-buffer)))) |
f69aad2b MB |
2918 | ;; Echo area |
2919 | (goto-char (point-max)) | |
2920 | (when (bolp) | |
2921 | (backward-char 1)) | |
2922 | (message "%s" (buffer-substring (point-min) (point)))) | |
2923 | (t | |
2924 | ;; Buffer | |
2925 | (goto-char (point-min)) | |
31252c00 MB |
2926 | (display-buffer (current-buffer) |
2927 | not-this-window frame)))))))) | |
f69aad2b MB |
2928 | |
2929 | ||
2076c87c JB |
2930 | ;; We have a sentinel to prevent insertion of a termination message |
2931 | ;; in the buffer itself. | |
2932 | (defun shell-command-sentinel (process signal) | |
bcad4985 | 2933 | (if (memq (process-status process) '(exit signal)) |
1e722f9f | 2934 | (message "%s: %s." |
bcad4985 KH |
2935 | (car (cdr (cdr (process-command process)))) |
2936 | (substring signal 0 -1)))) | |
2076c87c | 2937 | |
d0d74413 | 2938 | (defun shell-command-on-region (start end command |
cce1c318 | 2939 | &optional output-buffer replace |
63619f42 | 2940 | error-buffer display-error-buffer) |
2076c87c JB |
2941 | "Execute string COMMAND in inferior shell with region as input. |
2942 | Normally display output (if any) in temp buffer `*Shell Command Output*'; | |
a0184aeb DL |
2943 | Prefix arg means replace the region with it. Return the exit code of |
2944 | COMMAND. | |
56c0450e | 2945 | |
07f458c1 RS |
2946 | To specify a coding system for converting non-ASCII characters |
2947 | in the input and output to the shell command, use \\[universal-coding-system-argument] | |
2948 | before this command. By default, the input (from the current buffer) | |
9f847f41 EZ |
2949 | is encoded using coding-system specified by `process-coding-system-alist', |
2950 | falling back to `default-process-coding-system' if no match for COMMAND | |
2951 | is found in `process-coding-system-alist'. | |
07f458c1 | 2952 | |
63619f42 RS |
2953 | Noninteractive callers can specify coding systems by binding |
2954 | `coding-system-for-read' and `coding-system-for-write'. | |
2076c87c | 2955 | |
2e033693 RS |
2956 | If the command generates output, the output may be displayed |
2957 | in the echo area or in a buffer. | |
2958 | If the output is short enough to display in the echo area | |
2959 | \(determined by the variable `max-mini-window-height' if | |
c88b867f CY |
2960 | `resize-mini-windows' is non-nil), it is shown there. |
2961 | Otherwise it is displayed in the buffer `*Shell Command Output*'. | |
2962 | The output is available in that buffer in both cases. | |
2e033693 RS |
2963 | |
2964 | If there is output and an error, a message about the error | |
c88b867f CY |
2965 | appears at the end of the output. If there is no output, or if |
2966 | output is inserted in the current buffer, the buffer `*Shell | |
2967 | Command Output*' is deleted. | |
2968 | ||
2969 | Optional fourth arg OUTPUT-BUFFER specifies where to put the | |
0950aa27 JL |
2970 | command's output. If the value is a buffer or buffer name, |
2971 | put the output there. If the value is nil, use the buffer | |
2972 | `*Shell Command Output*'. Any other value, excluding nil, | |
2973 | means to insert the output in the current buffer. In either case, | |
2974 | the output is inserted after point (leaving mark after it). | |
c88b867f CY |
2975 | |
2976 | Optional fifth arg REPLACE, if non-nil, means to insert the | |
2977 | output in place of text from START to END, putting point and mark | |
8923a211 RS |
2978 | around it. |
2979 | ||
c88b867f CY |
2980 | Optional sixth arg ERROR-BUFFER, if non-nil, specifies a buffer |
2981 | or buffer name to which to direct the command's standard error | |
2982 | output. If nil, error output is mingled with regular output. | |
2983 | When called interactively, `shell-command-default-error-buffer' | |
2984 | is used for ERROR-BUFFER. | |
2985 | ||
2986 | Optional seventh arg DISPLAY-ERROR-BUFFER, if non-nil, means to | |
2987 | display the error buffer if there were any errors. When called | |
2988 | interactively, this is t." | |
195ce311 RS |
2989 | (interactive (let (string) |
2990 | (unless (mark) | |
2991 | (error "The mark is not set now, so there is no region")) | |
2992 | ;; Do this before calling region-beginning | |
2993 | ;; and region-end, in case subprocess output | |
2994 | ;; relocates them while we are in the minibuffer. | |
e5c4079c | 2995 | (setq string (read-shell-command "Shell command on region: ")) |
2b03c506 RS |
2996 | ;; call-interactively recognizes region-beginning and |
2997 | ;; region-end specially, leaving them in the history. | |
2998 | (list (region-beginning) (region-end) | |
cae49185 RS |
2999 | string |
3000 | current-prefix-arg | |
7fd47839 | 3001 | current-prefix-arg |
63619f42 RS |
3002 | shell-command-default-error-buffer |
3003 | t))) | |
cce1c318 | 3004 | (let ((error-file |
171a45d9 | 3005 | (if error-buffer |
b005abd5 | 3006 | (make-temp-file |
171a45d9 EZ |
3007 | (expand-file-name "scor" |
3008 | (or small-temporary-file-directory | |
3009 | temporary-file-directory))) | |
a0184aeb DL |
3010 | nil)) |
3011 | exit-status) | |
7fd47839 RS |
3012 | (if (or replace |
3013 | (and output-buffer | |
748d6ca4 | 3014 | (not (or (bufferp output-buffer) (stringp output-buffer))))) |
7fd47839 RS |
3015 | ;; Replace specified region with output from command. |
3016 | (let ((swap (and replace (< start end)))) | |
3017 | ;; Don't muck with mark unless REPLACE says we should. | |
3018 | (goto-char start) | |
30883773 | 3019 | (and replace (push-mark (point) 'nomsg)) |
a0184aeb | 3020 | (setq exit-status |
b3531901 | 3021 | (call-process-region start end shell-file-name replace |
a0184aeb DL |
3022 | (if error-file |
3023 | (list t error-file) | |
3024 | t) | |
3025 | nil shell-command-switch command)) | |
e1e04350 SM |
3026 | ;; It is rude to delete a buffer which the command is not using. |
3027 | ;; (let ((shell-buffer (get-buffer "*Shell Command Output*"))) | |
3028 | ;; (and shell-buffer (not (eq shell-buffer (current-buffer))) | |
3029 | ;; (kill-buffer shell-buffer))) | |
7fd47839 RS |
3030 | ;; Don't muck with mark unless REPLACE says we should. |
3031 | (and replace swap (exchange-point-and-mark))) | |
3032 | ;; No prefix argument: put the output in a temp buffer, | |
3033 | ;; replacing its entire contents. | |
3034 | (let ((buffer (get-buffer-create | |
d4bbcbb4 | 3035 | (or output-buffer "*Shell Command Output*")))) |
7fd47839 RS |
3036 | (unwind-protect |
3037 | (if (eq buffer (current-buffer)) | |
3038 | ;; If the input is the same buffer as the output, | |
3039 | ;; delete everything but the specified region, | |
3040 | ;; then replace that region with the output. | |
3041 | (progn (setq buffer-read-only nil) | |
3042 | (delete-region (max start end) (point-max)) | |
3043 | (delete-region (point-min) (min start end)) | |
3044 | (setq exit-status | |
3045 | (call-process-region (point-min) (point-max) | |
1e722f9f | 3046 | shell-file-name t |
7fd47839 RS |
3047 | (if error-file |
3048 | (list t error-file) | |
3049 | t) | |
a0184aeb DL |
3050 | nil shell-command-switch |
3051 | command))) | |
3052 | ;; Clear the output buffer, then run the command with | |
3053 | ;; output there. | |
c2e303c8 | 3054 | (let ((directory default-directory)) |
7fdbcd83 | 3055 | (with-current-buffer buffer |
c2e303c8 GM |
3056 | (setq buffer-read-only nil) |
3057 | (if (not output-buffer) | |
3058 | (setq default-directory directory)) | |
3059 | (erase-buffer))) | |
7fd47839 RS |
3060 | (setq exit-status |
3061 | (call-process-region start end shell-file-name nil | |
3062 | (if error-file | |
3063 | (list buffer error-file) | |
3064 | buffer) | |
a0184aeb | 3065 | nil shell-command-switch command))) |
2e033693 | 3066 | ;; Report the output. |
9a98fa64 | 3067 | (with-current-buffer buffer |
f1180544 | 3068 | (setq mode-line-process |
d4bbcbb4 AS |
3069 | (cond ((null exit-status) |
3070 | " - Error") | |
3071 | ((stringp exit-status) | |
3072 | (format " - Signal [%s]" exit-status)) | |
3073 | ((not (equal 0 exit-status)) | |
3074 | (format " - Exit [%d]" exit-status))))) | |
f69aad2b MB |
3075 | (if (with-current-buffer buffer (> (point-max) (point-min))) |
3076 | ;; There's some output, display it | |
9a98fa64 | 3077 | (display-message-or-buffer buffer) |
f69aad2b | 3078 | ;; No output; error? |
94ddbe6d RS |
3079 | (let ((output |
3080 | (if (and error-file | |
3081 | (< 0 (nth 7 (file-attributes error-file)))) | |
e83cc1f7 LMI |
3082 | (format "some error output%s" |
3083 | (if shell-command-default-error-buffer | |
e0457abe LMI |
3084 | (format " to the \"%s\" buffer" |
3085 | shell-command-default-error-buffer) | |
e83cc1f7 | 3086 | "")) |
94ddbe6d | 3087 | "no output"))) |
d4bbcbb4 AS |
3088 | (cond ((null exit-status) |
3089 | (message "(Shell command failed with error)")) | |
3090 | ((equal 0 exit-status) | |
3091 | (message "(Shell command succeeded with %s)" | |
3092 | output)) | |
3093 | ((stringp exit-status) | |
3094 | (message "(Shell command killed by signal %s)" | |
3095 | exit-status)) | |
3096 | (t | |
3097 | (message "(Shell command failed with code %d and %s)" | |
3098 | exit-status output)))) | |
e1e04350 SM |
3099 | ;; Don't kill: there might be useful info in the undo-log. |
3100 | ;; (kill-buffer buffer) | |
3101 | )))) | |
f69aad2b | 3102 | |
cc039f78 KH |
3103 | (when (and error-file (file-exists-p error-file)) |
3104 | (if (< 0 (nth 7 (file-attributes error-file))) | |
3105 | (with-current-buffer (get-buffer-create error-buffer) | |
3106 | (let ((pos-from-end (- (point-max) (point)))) | |
3107 | (or (bobp) | |
3108 | (insert "\f\n")) | |
3109 | ;; Do no formatting while reading error file, | |
3110 | ;; because that can run a shell command, and we | |
3111 | ;; don't want that to cause an infinite recursion. | |
3112 | (format-insert-file error-file nil) | |
3113 | ;; Put point after the inserted errors. | |
3114 | (goto-char (- (point-max) pos-from-end))) | |
63619f42 RS |
3115 | (and display-error-buffer |
3116 | (display-buffer (current-buffer))))) | |
cc039f78 | 3117 | (delete-file error-file)) |
a0184aeb | 3118 | exit-status)) |
1e722f9f | 3119 | |
d589bd99 RS |
3120 | (defun shell-command-to-string (command) |
3121 | "Execute shell command COMMAND and return its output as a string." | |
3122 | (with-output-to-string | |
17cc9013 RS |
3123 | (with-current-buffer |
3124 | standard-output | |
b511b994 | 3125 | (process-file shell-file-name nil t nil shell-command-switch command)))) |
0457dd55 KG |
3126 | |
3127 | (defun process-file (program &optional infile buffer display &rest args) | |
3128 | "Process files synchronously in a separate process. | |
3129 | Similar to `call-process', but may invoke a file handler based on | |
3130 | `default-directory'. The current working directory of the | |
3131 | subprocess is `default-directory'. | |
3132 | ||
3133 | File names in INFILE and BUFFER are handled normally, but file | |
3134 | names in ARGS should be relative to `default-directory', as they | |
3135 | are passed to the process verbatim. \(This is a difference to | |
3136 | `call-process' which does not support file handlers for INFILE | |
3137 | and BUFFER.\) | |
3138 | ||
3139 | Some file handlers might not support all variants, for example | |
3140 | they might behave as if DISPLAY was nil, regardless of the actual | |
3141 | value passed." | |
3142 | (let ((fh (find-file-name-handler default-directory 'process-file)) | |
3143 | lc stderr-file) | |
3144 | (unwind-protect | |
3145 | (if fh (apply fh 'process-file program infile buffer display args) | |
8de40f9f | 3146 | (when infile (setq lc (file-local-copy infile))) |
0457dd55 | 3147 | (setq stderr-file (when (and (consp buffer) (stringp (cadr buffer))) |
85af630d KG |
3148 | (make-temp-file "emacs"))) |
3149 | (prog1 | |
3150 | (apply 'call-process program | |
3151 | (or lc infile) | |
3152 | (if stderr-file (list (car buffer) stderr-file) buffer) | |
3153 | display args) | |
0e9c7693 | 3154 | (when stderr-file (copy-file stderr-file (cadr buffer) t)))) |
0457dd55 KG |
3155 | (when stderr-file (delete-file stderr-file)) |
3156 | (when lc (delete-file lc))))) | |
3157 | ||
2c4f2562 MA |
3158 | (defvar process-file-side-effects t |
3159 | "Whether a call of `process-file' changes remote files. | |
3160 | ||
e153c136 | 3161 | By default, this variable is always set to `t', meaning that a |
2c4f2562 MA |
3162 | call of `process-file' could potentially change any file on a |
3163 | remote host. When set to `nil', a file handler could optimize | |
e153c136 | 3164 | its behavior with respect to remote file attribute caching. |
2c4f2562 | 3165 | |
e153c136 GM |
3166 | You should only ever change this variable with a let-binding; |
3167 | never with `setq'.") | |
2c4f2562 | 3168 | |
7cb76caa MA |
3169 | (defun start-file-process (name buffer program &rest program-args) |
3170 | "Start a program in a subprocess. Return the process object for it. | |
5a5abb2c | 3171 | |
7cb76caa | 3172 | Similar to `start-process', but may invoke a file handler based on |
5a5abb2c MA |
3173 | `default-directory'. See Info node `(elisp)Magic File Names'. |
3174 | ||
3175 | This handler ought to run PROGRAM, perhaps on the local host, | |
3176 | perhaps on a remote host that corresponds to `default-directory'. | |
3177 | In the latter case, the local part of `default-directory' becomes | |
3178 | the working directory of the process. | |
7cb76caa MA |
3179 | |
3180 | PROGRAM and PROGRAM-ARGS might be file names. They are not | |
91f11424 MA |
3181 | objects of file handler invocation. File handlers might not |
3182 | support pty association, if PROGRAM is nil." | |
7cb76caa MA |
3183 | (let ((fh (find-file-name-handler default-directory 'start-file-process))) |
3184 | (if fh (apply fh 'start-file-process name buffer program program-args) | |
3185 | (apply 'start-process name buffer program program-args)))) | |
7d668f2c CY |
3186 | \f |
3187 | ;;;; Process menu | |
3188 | ||
3189 | (defvar tabulated-list-format) | |
3190 | (defvar tabulated-list-entries) | |
3191 | (defvar tabulated-list-sort-key) | |
3192 | (declare-function tabulated-list-init-header "tabulated-list" ()) | |
cf5bee67 GM |
3193 | (declare-function tabulated-list-print "tabulated-list" |
3194 | (&optional remember-pos)) | |
7d668f2c CY |
3195 | |
3196 | (defvar process-menu-query-only nil) | |
3197 | ||
3198 | (define-derived-mode process-menu-mode tabulated-list-mode "Process Menu" | |
3199 | "Major mode for listing the processes called by Emacs." | |
3200 | (setq tabulated-list-format [("Process" 15 t) | |
3201 | ("Status" 7 t) | |
3202 | ("Buffer" 15 t) | |
3203 | ("TTY" 12 t) | |
3204 | ("Command" 0 t)]) | |
3205 | (make-local-variable 'process-menu-query-only) | |
3206 | (setq tabulated-list-sort-key (cons "Process" nil)) | |
3207 | (add-hook 'tabulated-list-revert-hook 'list-processes--refresh nil t) | |
3208 | (tabulated-list-init-header)) | |
3209 | ||
3210 | (defun list-processes--refresh () | |
2d16b285 CY |
3211 | "Recompute the list of processes for the Process List buffer. |
3212 | Also, delete any process that is exited or signaled." | |
7d668f2c CY |
3213 | (setq tabulated-list-entries nil) |
3214 | (dolist (p (process-list)) | |
2d16b285 CY |
3215 | (cond ((memq (process-status p) '(exit signal closed)) |
3216 | (delete-process p)) | |
3217 | ((or (not process-menu-query-only) | |
3218 | (process-query-on-exit-flag p)) | |
3219 | (let* ((buf (process-buffer p)) | |
3220 | (type (process-type p)) | |
3221 | (name (process-name p)) | |
3222 | (status (symbol-name (process-status p))) | |
3223 | (buf-label (if (buffer-live-p buf) | |
3224 | `(,(buffer-name buf) | |
3225 | face link | |
3226 | help-echo ,(concat "Visit buffer `" | |
3227 | (buffer-name buf) "'") | |
3228 | follow-link t | |
3229 | process-buffer ,buf | |
3230 | action process-menu-visit-buffer) | |
3231 | "--")) | |
3232 | (tty (or (process-tty-name p) "--")) | |
3233 | (cmd | |
3234 | (if (memq type '(network serial)) | |
3235 | (let ((contact (process-contact p t))) | |
3236 | (if (eq type 'network) | |
3237 | (format "(%s %s)" | |
3238 | (if (plist-get contact :type) | |
3239 | "datagram" | |
3240 | "network") | |
3241 | (if (plist-get contact :server) | |
3242 | (format "server on %s" | |
7fcc0070 AS |
3243 | (or |
3244 | (plist-get contact :host) | |
3245 | (plist-get contact :local))) | |
2d16b285 CY |
3246 | (format "connection to %s" |
3247 | (plist-get contact :host)))) | |
3248 | (format "(serial port %s%s)" | |
3249 | (or (plist-get contact :port) "?") | |
3250 | (let ((speed (plist-get contact :speed))) | |
3251 | (if speed | |
3252 | (format " at %s b/s" speed) | |
3253 | ""))))) | |
3254 | (mapconcat 'identity (process-command p) " ")))) | |
3255 | (push (list p (vector name status buf-label tty cmd)) | |
3256 | tabulated-list-entries)))))) | |
7d668f2c CY |
3257 | |
3258 | (defun process-menu-visit-buffer (button) | |
3259 | (display-buffer (button-get button 'process-buffer))) | |
3260 | ||
3261 | (defun list-processes (&optional query-only buffer) | |
fec92060 | 3262 | "Display a list of all processes that are Emacs sub-processes. |
7d668f2c CY |
3263 | If optional argument QUERY-ONLY is non-nil, only processes with |
3264 | the query-on-exit flag set are listed. | |
3265 | Any process listed as exited or signaled is actually eliminated | |
3266 | after the listing is made. | |
3267 | Optional argument BUFFER specifies a buffer to use, instead of | |
97ad0769 | 3268 | \"*Process List*\". |
fec92060 EZ |
3269 | The return value is always nil. |
3270 | ||
3271 | This function lists only processes that were launched by Emacs. To | |
3272 | see other processes running on the system, use `list-system-processes'." | |
7d668f2c | 3273 | (interactive) |
3726838a EZ |
3274 | (or (fboundp 'process-list) |
3275 | (error "Asynchronous subprocesses are not supported on this system")) | |
7d668f2c CY |
3276 | (unless (bufferp buffer) |
3277 | (setq buffer (get-buffer-create "*Process List*"))) | |
3278 | (with-current-buffer buffer | |
3279 | (process-menu-mode) | |
3280 | (setq process-menu-query-only query-only) | |
3281 | (list-processes--refresh) | |
3282 | (tabulated-list-print)) | |
989681bb JB |
3283 | (display-buffer buffer) |
3284 | nil) | |
2d88b556 | 3285 | \f |
1b43f83f | 3286 | (defvar universal-argument-map |
02ef6c1a SM |
3287 | (let ((map (make-sparse-keymap)) |
3288 | (universal-argument-minus | |
3289 | ;; For backward compatibility, minus with no modifiers is an ordinary | |
3290 | ;; command if digits have already been entered. | |
3291 | `(menu-item "" negative-argument | |
3292 | :filter ,(lambda (cmd) | |
3293 | (if (integerp prefix-arg) nil cmd))))) | |
3294 | (define-key map [switch-frame] | |
3295 | (lambda (e) (interactive "e") | |
3296 | (handle-switch-frame e) (universal-argument--mode))) | |
69d4c3c4 | 3297 | (define-key map [?\C-u] 'universal-argument-more) |
02ef6c1a | 3298 | (define-key map [?-] universal-argument-minus) |
69d4c3c4 KH |
3299 | (define-key map [?0] 'digit-argument) |
3300 | (define-key map [?1] 'digit-argument) | |
3301 | (define-key map [?2] 'digit-argument) | |
3302 | (define-key map [?3] 'digit-argument) | |
3303 | (define-key map [?4] 'digit-argument) | |
3304 | (define-key map [?5] 'digit-argument) | |
3305 | (define-key map [?6] 'digit-argument) | |
3306 | (define-key map [?7] 'digit-argument) | |
3307 | (define-key map [?8] 'digit-argument) | |
3308 | (define-key map [?9] 'digit-argument) | |
bd7acc8d GM |
3309 | (define-key map [kp-0] 'digit-argument) |
3310 | (define-key map [kp-1] 'digit-argument) | |
3311 | (define-key map [kp-2] 'digit-argument) | |
3312 | (define-key map [kp-3] 'digit-argument) | |
3313 | (define-key map [kp-4] 'digit-argument) | |
3314 | (define-key map [kp-5] 'digit-argument) | |
3315 | (define-key map [kp-6] 'digit-argument) | |
3316 | (define-key map [kp-7] 'digit-argument) | |
3317 | (define-key map [kp-8] 'digit-argument) | |
3318 | (define-key map [kp-9] 'digit-argument) | |
02ef6c1a | 3319 | (define-key map [kp-subtract] universal-argument-minus) |
69d4c3c4 KH |
3320 | map) |
3321 | "Keymap used while processing \\[universal-argument].") | |
3322 | ||
02ef6c1a | 3323 | (defun universal-argument--mode () |
8cd22a08 | 3324 | (set-transient-map universal-argument-map)) |
6b61353c | 3325 | |
e8d1a377 KH |
3326 | (defun universal-argument () |
3327 | "Begin a numeric argument for the following command. | |
3328 | Digits or minus sign following \\[universal-argument] make up the numeric argument. | |
3329 | \\[universal-argument] following the digits or minus sign ends the argument. | |
3330 | \\[universal-argument] without digits or minus sign provides 4 as argument. | |
3331 | Repeating \\[universal-argument] without digits or minus sign | |
0565d307 RS |
3332 | multiplies the argument by 4 each time. |
3333 | For some commands, just \\[universal-argument] by itself serves as a flag | |
a697fc62 RS |
3334 | which is different in effect from any particular numeric argument. |
3335 | These commands include \\[set-mark-command] and \\[start-kbd-macro]." | |
69d4c3c4 KH |
3336 | (interactive) |
3337 | (setq prefix-arg (list 4)) | |
02ef6c1a | 3338 | (universal-argument--mode)) |
e8d1a377 | 3339 | |
69d4c3c4 | 3340 | (defun universal-argument-more (arg) |
02ef6c1a SM |
3341 | ;; A subsequent C-u means to multiply the factor by 4 if we've typed |
3342 | ;; nothing but C-u's; otherwise it means to terminate the prefix arg. | |
e8d1a377 | 3343 | (interactive "P") |
02ef6c1a SM |
3344 | (setq prefix-arg (if (consp arg) |
3345 | (list (* 4 (car arg))) | |
3346 | (if (eq arg '-) | |
3347 | (list -4) | |
3348 | arg))) | |
3349 | (when (consp prefix-arg) (universal-argument--mode))) | |
e8d1a377 KH |
3350 | |
3351 | (defun negative-argument (arg) | |
3352 | "Begin a negative numeric argument for the next command. | |
3353 | \\[universal-argument] following digits or minus sign ends the argument." | |
3354 | (interactive "P") | |
02ef6c1a SM |
3355 | (setq prefix-arg (cond ((integerp arg) (- arg)) |
3356 | ((eq arg '-) nil) | |
3357 | (t '-))) | |
3358 | (universal-argument--mode)) | |
69d4c3c4 KH |
3359 | |
3360 | (defun digit-argument (arg) | |
3361 | "Part of the numeric argument for the next command. | |
3362 | \\[universal-argument] following digits or minus sign ends the argument." | |
3363 | (interactive "P") | |
8989a920 GM |
3364 | (let* ((char (if (integerp last-command-event) |
3365 | last-command-event | |
3366 | (get last-command-event 'ascii-character))) | |
bd7acc8d | 3367 | (digit (- (logand char ?\177) ?0))) |
02ef6c1a SM |
3368 | (setq prefix-arg (cond ((integerp arg) |
3369 | (+ (* arg 10) | |
3370 | (if (< arg 0) (- digit) digit))) | |
3371 | ((eq arg '-) | |
3372 | ;; Treat -0 as just -, so that -01 will work. | |
3373 | (if (zerop digit) '- (- digit))) | |
3374 | (t | |
3375 | digit)))) | |
3376 | (universal-argument--mode)) | |
2d88b556 | 3377 | \f |
8f92b8ad SM |
3378 | |
3379 | (defvar filter-buffer-substring-functions nil | |
d36ed1c8 SM |
3380 | "This variable is a wrapper hook around `filter-buffer-substring'.") |
3381 | (make-obsolete-variable 'filter-buffer-substring-functions | |
3382 | 'filter-buffer-substring-function "24.4") | |
3383 | ||
3384 | (defvar filter-buffer-substring-function #'buffer-substring--filter | |
3385 | "Function to perform the filtering in `filter-buffer-substring'. | |
3386 | The function is called with 3 arguments: | |
34c99998 GM |
3387 | \(BEG END DELETE). The arguments BEG, END, and DELETE are the same |
3388 | as those of `filter-buffer-substring' in each case. | |
d36ed1c8 | 3389 | It should return the buffer substring between BEG and END, after filtering.") |
8f92b8ad | 3390 | |
7fcce20f RS |
3391 | (defvar buffer-substring-filters nil |
3392 | "List of filter functions for `filter-buffer-substring'. | |
3393 | Each function must accept a single argument, a string, and return | |
3394 | a string. The buffer substring is passed to the first function | |
3395 | in the list, and the return value of each function is passed to | |
d36ed1c8 | 3396 | the next. |
34c99998 GM |
3397 | As a special convention, point is set to the start of the buffer text |
3398 | being operated on (i.e., the first argument of `filter-buffer-substring') | |
3399 | before these functions are called.") | |
8f92b8ad | 3400 | (make-obsolete-variable 'buffer-substring-filters |
d36ed1c8 | 3401 | 'filter-buffer-substring-function "24.1") |
7fcce20f | 3402 | |
8f92b8ad | 3403 | (defun filter-buffer-substring (beg end &optional delete) |
7fcce20f | 3404 | "Return the buffer substring between BEG and END, after filtering. |
d36ed1c8 SM |
3405 | The hook `filter-buffer-substring-function' performs the actual filtering. |
3406 | By default, no filtering is done. | |
7fcce20f RS |
3407 | |
3408 | If DELETE is non-nil, the text between BEG and END is deleted | |
3409 | from the buffer. | |
3410 | ||
398c9ffb KS |
3411 | This function should be used instead of `buffer-substring', |
3412 | `buffer-substring-no-properties', or `delete-and-extract-region' | |
3413 | when you want to allow filtering to take place. For example, | |
d36ed1c8 | 3414 | major or minor modes can use `filter-buffer-substring-function' to |
398c9ffb KS |
3415 | extract characters that are special to a buffer, and should not |
3416 | be copied into other buffers." | |
d36ed1c8 SM |
3417 | (funcall filter-buffer-substring-function beg end delete)) |
3418 | ||
f73f4ce6 | 3419 | ;; FIXME: `with-wrapper-hook' is obsolete |
d36ed1c8 | 3420 | (defun buffer-substring--filter (beg end &optional delete) |
8f92b8ad SM |
3421 | (with-wrapper-hook filter-buffer-substring-functions (beg end delete) |
3422 | (cond | |
3423 | ((or delete buffer-substring-filters) | |
3424 | (save-excursion | |
3425 | (goto-char beg) | |
3426 | (let ((string (if delete (delete-and-extract-region beg end) | |
3427 | (buffer-substring beg end)))) | |
3428 | (dolist (filter buffer-substring-filters) | |
3429 | (setq string (funcall filter string))) | |
3430 | string))) | |
3431 | (t | |
3432 | (buffer-substring beg end))))) | |
398c9ffb | 3433 | |
7fcce20f | 3434 | |
93be67de | 3435 | ;;;; Window system cut and paste hooks. |
70e14c01 JB |
3436 | |
3437 | (defvar interprogram-cut-function nil | |
3438 | "Function to call to make a killed region available to other programs. | |
621b9d6c CY |
3439 | Most window systems provide a facility for cutting and pasting |
3440 | text between different programs, such as the clipboard on X and | |
3441 | MS-Windows, or the pasteboard on Nextstep/Mac OS. | |
70e14c01 | 3442 | |
621b9d6c CY |
3443 | This variable holds a function that Emacs calls whenever text is |
3444 | put in the kill ring, to make the new kill available to other | |
3445 | programs. The function takes one argument, TEXT, which is a | |
3446 | string containing the text which should be made available.") | |
70e14c01 JB |
3447 | |
3448 | (defvar interprogram-paste-function nil | |
3449 | "Function to call to get text cut from other programs. | |
621b9d6c CY |
3450 | Most window systems provide a facility for cutting and pasting |
3451 | text between different programs, such as the clipboard on X and | |
3452 | MS-Windows, or the pasteboard on Nextstep/Mac OS. | |
3453 | ||
3454 | This variable holds a function that Emacs calls to obtain text | |
3455 | that other programs have provided for pasting. The function is | |
3456 | called with no arguments. If no other program has provided text | |
3457 | to paste, the function should return nil (in which case the | |
3458 | caller, usually `current-kill', should use the top of the Emacs | |
3459 | kill ring). If another program has provided text to paste, the | |
3460 | function should return that text as a string (in which case the | |
3461 | caller should put this string in the kill ring as the latest | |
3462 | kill). | |
3463 | ||
3464 | The function may also return a list of strings if the window | |
1d2b0303 | 3465 | system supports multiple selections. The first string will be |
621b9d6c CY |
3466 | used as the pasted text, but the other will be placed in the kill |
3467 | ring for easy access via `yank-pop'. | |
3468 | ||
3469 | Note that the function should return a string only if a program | |
3470 | other than Emacs has provided a string for pasting; if Emacs | |
3471 | provided the most recent string, the function should return nil. | |
3472 | If it is difficult to tell whether Emacs or some other program | |
3473 | provided the current string, it is probably good enough to return | |
3474 | nil if the string is equal (according to `string=') to the last | |
3475 | text Emacs provided.") | |
2d88b556 | 3476 | \f |
70e14c01 | 3477 | |
eaae8106 | 3478 | |
70e14c01 | 3479 | ;;;; The kill ring data structure. |
2076c87c JB |
3480 | |
3481 | (defvar kill-ring nil | |
70e14c01 JB |
3482 | "List of killed text sequences. |
3483 | Since the kill ring is supposed to interact nicely with cut-and-paste | |
3484 | facilities offered by window systems, use of this variable should | |
3485 | interact nicely with `interprogram-cut-function' and | |
3486 | `interprogram-paste-function'. The functions `kill-new', | |
3487 | `kill-append', and `current-kill' are supposed to implement this | |
3488 | interaction; you may want to use them instead of manipulating the kill | |
3489 | ring directly.") | |
2076c87c | 3490 | |
bffa4d92 | 3491 | (defcustom kill-ring-max 60 |
1d2b0303 | 3492 | "Maximum length of kill ring before oldest elements are thrown away." |
69c1dd37 RS |
3493 | :type 'integer |
3494 | :group 'killing) | |
2076c87c JB |
3495 | |
3496 | (defvar kill-ring-yank-pointer nil | |
3497 | "The tail of the kill ring whose car is the last thing yanked.") | |
3498 | ||
4ed8c7aa | 3499 | (defcustom save-interprogram-paste-before-kill nil |
e8ab3908 | 3500 | "Save clipboard strings into kill ring before replacing them. |
4ed8c7aa SS |
3501 | When one selects something in another program to paste it into Emacs, |
3502 | but kills something in Emacs before actually pasting it, | |
3503 | this selection is gone unless this variable is non-nil, | |
3504 | in which case the other program's selection is saved in the `kill-ring' | |
3505 | before the Emacs kill and one can still paste it using \\[yank] \\[yank-pop]." | |
3506 | :type 'boolean | |
3507 | :group 'killing | |
3508 | :version "23.2") | |
3509 | ||
ba83a64e | 3510 | (defcustom kill-do-not-save-duplicates nil |
e2f1fdab LL |
3511 | "Do not add a new string to `kill-ring' if it duplicates the last one. |
3512 | The comparison is done using `equal-including-properties'." | |
ba83a64e SS |
3513 | :type 'boolean |
3514 | :group 'killing | |
3515 | :version "23.2") | |
3516 | ||
3472b6c6 | 3517 | (defun kill-new (string &optional replace) |
70e14c01 | 3518 | "Make STRING the latest kill in the kill ring. |
3e505153 | 3519 | Set `kill-ring-yank-pointer' to point to it. |
f914dc91 KH |
3520 | If `interprogram-cut-function' is non-nil, apply it to STRING. |
3521 | Optional second argument REPLACE non-nil means that STRING will replace | |
be5936a7 KS |
3522 | the front of the kill ring, rather than being added to the list. |
3523 | ||
4ed8c7aa SS |
3524 | When `save-interprogram-paste-before-kill' and `interprogram-paste-function' |
3525 | are non-nil, saves the interprogram paste string(s) into `kill-ring' before | |
3526 | STRING. | |
3527 | ||
2a262563 KS |
3528 | When the yank handler has a non-nil PARAM element, the original STRING |
3529 | argument is not used by `insert-for-yank'. However, since Lisp code | |
f33321ad | 3530 | may access and use elements from the kill ring directly, the STRING |
2a262563 | 3531 | argument should still be a \"useful\" string for such uses." |
0665f661 | 3532 | (unless (and kill-do-not-save-duplicates |
e2f1fdab LL |
3533 | ;; Due to text properties such as 'yank-handler that |
3534 | ;; can alter the contents to yank, comparison using | |
3535 | ;; `equal' is unsafe. | |
3536 | (equal-including-properties string (car kill-ring))) | |
0665f661 JL |
3537 | (if (fboundp 'menu-bar-update-yank-menu) |
3538 | (menu-bar-update-yank-menu string (and replace (car kill-ring))))) | |
4ed8c7aa SS |
3539 | (when save-interprogram-paste-before-kill |
3540 | (let ((interprogram-paste (and interprogram-paste-function | |
3541 | (funcall interprogram-paste-function)))) | |
3542 | (when interprogram-paste | |
0665f661 JL |
3543 | (dolist (s (if (listp interprogram-paste) |
3544 | (nreverse interprogram-paste) | |
3545 | (list interprogram-paste))) | |
3546 | (unless (and kill-do-not-save-duplicates | |
e2f1fdab | 3547 | (equal-including-properties s (car kill-ring))) |
0665f661 JL |
3548 | (push s kill-ring)))))) |
3549 | (unless (and kill-do-not-save-duplicates | |
e2f1fdab | 3550 | (equal-including-properties string (car kill-ring))) |
0665f661 JL |
3551 | (if (and replace kill-ring) |
3552 | (setcar kill-ring string) | |
3553 | (push string kill-ring) | |
3554 | (if (> (length kill-ring) kill-ring-max) | |
3555 | (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))) | |
70e14c01 JB |
3556 | (setq kill-ring-yank-pointer kill-ring) |
3557 | (if interprogram-cut-function | |
08d4877e | 3558 | (funcall interprogram-cut-function string))) |
70e14c01 | 3559 | |
3472b6c6 | 3560 | (defun kill-append (string before-p) |
70e14c01 JB |
3561 | "Append STRING to the end of the latest kill in the kill ring. |
3562 | If BEFORE-P is non-nil, prepend STRING to the kill. | |
be5936a7 KS |
3563 | If `interprogram-cut-function' is set, pass the resulting kill to it." |
3564 | (let* ((cur (car kill-ring))) | |
3565 | (kill-new (if before-p (concat string cur) (concat cur string)) | |
3566 | (or (= (length cur) 0) | |
3472b6c6 | 3567 | (equal nil (get-text-property 0 'yank-handler cur)))))) |
70e14c01 | 3568 | |
4496b02b | 3569 | (defcustom yank-pop-change-selection nil |
621b9d6c CY |
3570 | "Whether rotating the kill ring changes the window system selection. |
3571 | If non-nil, whenever the kill ring is rotated (usually via the | |
3572 | `yank-pop' command), Emacs also calls `interprogram-cut-function' | |
3573 | to copy the new kill to the window system selection." | |
4496b02b RS |
3574 | :type 'boolean |
3575 | :group 'killing | |
3576 | :version "23.1") | |
3577 | ||
70e14c01 JB |
3578 | (defun current-kill (n &optional do-not-move) |
3579 | "Rotate the yanking point by N places, and then return that kill. | |
f019fb21 LMI |
3580 | If N is zero and `interprogram-paste-function' is set to a |
3581 | function that returns a string or a list of strings, and if that | |
3582 | function doesn't return nil, then that string (or list) is added | |
3583 | to the front of the kill ring and the string (or first string in | |
3584 | the list) is returned as the latest kill. | |
4496b02b RS |
3585 | |
3586 | If N is not zero, and if `yank-pop-change-selection' is | |
3587 | non-nil, use `interprogram-cut-function' to transfer the | |
3588 | kill at the new yank point into the window system selection. | |
3589 | ||
3590 | If optional arg DO-NOT-MOVE is non-nil, then don't actually | |
3591 | move the yanking point; just return the Nth kill forward." | |
3592 | ||
70e14c01 JB |
3593 | (let ((interprogram-paste (and (= n 0) |
3594 | interprogram-paste-function | |
3595 | (funcall interprogram-paste-function)))) | |
3596 | (if interprogram-paste | |
3597 | (progn | |
3598 | ;; Disable the interprogram cut function when we add the new | |
3599 | ;; text to the kill ring, so Emacs doesn't try to own the | |
3600 | ;; selection, with identical text. | |
3601 | (let ((interprogram-cut-function nil)) | |
d4cb4833 GM |
3602 | (if (listp interprogram-paste) |
3603 | (mapc 'kill-new (nreverse interprogram-paste)) | |
3604 | (kill-new interprogram-paste))) | |
3605 | (car kill-ring)) | |
70e14c01 | 3606 | (or kill-ring (error "Kill ring is empty")) |
47096a67 PE |
3607 | (let ((ARGth-kill-element |
3608 | (nthcdr (mod (- n (length kill-ring-yank-pointer)) | |
3609 | (length kill-ring)) | |
3610 | kill-ring))) | |
4496b02b RS |
3611 | (unless do-not-move |
3612 | (setq kill-ring-yank-pointer ARGth-kill-element) | |
3613 | (when (and yank-pop-change-selection | |
3614 | (> n 0) | |
3615 | interprogram-cut-function) | |
3616 | (funcall interprogram-cut-function (car ARGth-kill-element)))) | |
70e14c01 | 3617 | (car ARGth-kill-element))))) |
c88ab9ce | 3618 | |
c88ab9ce | 3619 | |
eaae8106 | 3620 | |
70e14c01 | 3621 | ;;;; Commands for manipulating the kill ring. |
c88ab9ce | 3622 | |
69c1dd37 | 3623 | (defcustom kill-read-only-ok nil |
1d2b0303 | 3624 | "Non-nil means don't signal an error for killing read-only text." |
69c1dd37 RS |
3625 | :type 'boolean |
3626 | :group 'killing) | |
e6291fe1 | 3627 | |
3472b6c6 | 3628 | (defun kill-region (beg end &optional region) |
66e9b2b2 RS |
3629 | "Kill (\"cut\") text between point and mark. |
3630 | This deletes the text from the buffer and saves it in the kill ring. | |
2076c87c | 3631 | The command \\[yank] can retrieve it from there. |
ba2b460a | 3632 | \(If you want to save the region without killing it, use \\[kill-ring-save].) |
81558867 EZ |
3633 | |
3634 | If you want to append the killed region to the last killed text, | |
3635 | use \\[append-next-kill] before \\[kill-region]. | |
3636 | ||
2aa7a8bf JB |
3637 | If the buffer is read-only, Emacs will beep and refrain from deleting |
3638 | the text, but put the text in the kill ring anyway. This means that | |
3639 | you can use the killing commands to copy text from a read-only buffer. | |
2076c87c | 3640 | |
a4aae1a5 CY |
3641 | Lisp programs should use this function for killing text. |
3642 | (To delete text, use `delete-region'.) | |
c15dc81f | 3643 | Supply two arguments, character positions indicating the stretch of text |
2076c87c JB |
3644 | to be killed. |
3645 | Any command that calls this function is a \"kill command\". | |
3646 | If the previous command was also a kill command, | |
3647 | the text killed this time appends to the text killed last time | |
3472b6c6 SM |
3648 | to make one entry in the kill ring. |
3649 | ||
3650 | The optional argument REGION if non-nil, indicates that we're not just killing | |
3651 | some text between BEG and END, but we're killing the region." | |
76da3455 DK |
3652 | ;; Pass mark first, then point, because the order matters when |
3653 | ;; calling `kill-append'. | |
3654 | (interactive (list (mark) (point) 'region)) | |
f39d6be0 RS |
3655 | (unless (and beg end) |
3656 | (error "The mark is not set now, so there is no region")) | |
ccd19b9f | 3657 | (condition-case nil |
3472b6c6 SM |
3658 | (let ((string (if region |
3659 | (funcall region-extract-function 'delete) | |
3660 | (filter-buffer-substring beg end 'delete)))) | |
a1eb02bd SM |
3661 | (when string ;STRING is nil if BEG = END |
3662 | ;; Add that string to the kill ring, one way or another. | |
3663 | (if (eq last-command 'kill-region) | |
3472b6c6 SM |
3664 | (kill-append string (< end beg)) |
3665 | (kill-new string nil))) | |
8a7cda9b | 3666 | (when (or string (eq last-command 'kill-region)) |
6b61353c | 3667 | (setq this-command 'kill-region)) |
05c22d87 | 3668 | (setq deactivate-mark t) |
6b61353c | 3669 | nil) |
ccd19b9f KH |
3670 | ((buffer-read-only text-read-only) |
3671 | ;; The code above failed because the buffer, or some of the characters | |
3672 | ;; in the region, are read-only. | |
3673 | ;; We should beep, in case the user just isn't aware of this. | |
3674 | ;; However, there's no harm in putting | |
3675 | ;; the region's text in the kill ring, anyway. | |
3472b6c6 | 3676 | (copy-region-as-kill beg end region) |
cb3e1b4c RS |
3677 | ;; Set this-command now, so it will be set even if we get an error. |
3678 | (setq this-command 'kill-region) | |
3679 | ;; This should barf, if appropriate, and give us the correct error. | |
ccd19b9f | 3680 | (if kill-read-only-ok |
6b61353c | 3681 | (progn (message "Read only text copied to kill ring") nil) |
ccd19b9f KH |
3682 | ;; Signal an error if the buffer is read-only. |
3683 | (barf-if-buffer-read-only) | |
3684 | ;; If the buffer isn't read-only, the text is. | |
3685 | (signal 'text-read-only (list (current-buffer))))))) | |
2076c87c | 3686 | |
a382890a KH |
3687 | ;; copy-region-as-kill no longer sets this-command, because it's confusing |
3688 | ;; to get two copies of the text when the user accidentally types M-w and | |
3689 | ;; then corrects it with the intended C-w. | |
3472b6c6 | 3690 | (defun copy-region-as-kill (beg end &optional region) |
2076c87c | 3691 | "Save the region as if killed, but don't kill it. |
0e264847 | 3692 | In Transient Mark mode, deactivate the mark. |
46947372 | 3693 | If `interprogram-cut-function' is non-nil, also save the text for a window |
b66eb11b RS |
3694 | system cut and paste. |
3695 | ||
3472b6c6 SM |
3696 | The optional argument REGION if non-nil, indicates that we're not just copying |
3697 | some text between BEG and END, but we're copying the region. | |
3698 | ||
90769946 | 3699 | This command's old key binding has been given to `kill-ring-save'." |
76da3455 DK |
3700 | ;; Pass mark first, then point, because the order matters when |
3701 | ;; calling `kill-append'. | |
3702 | (interactive (list (mark) (point) | |
3703 | (prefix-numeric-value current-prefix-arg))) | |
3472b6c6 | 3704 | (let ((str (if region |
ae4002ce | 3705 | (funcall region-extract-function nil) |
3472b6c6 | 3706 | (filter-buffer-substring beg end)))) |
2076c87c | 3707 | (if (eq last-command 'kill-region) |
3472b6c6 SM |
3708 | (kill-append str (< end beg)) |
3709 | (kill-new str))) | |
d34c311a | 3710 | (setq deactivate-mark t) |
2076c87c JB |
3711 | nil) |
3712 | ||
3472b6c6 | 3713 | (defun kill-ring-save (beg end &optional region) |
0964e562 | 3714 | "Save the region as if killed, but don't kill it. |
0e264847 | 3715 | In Transient Mark mode, deactivate the mark. |
0964e562 | 3716 | If `interprogram-cut-function' is non-nil, also save the text for a window |
0e264847 RS |
3717 | system cut and paste. |
3718 | ||
81558867 EZ |
3719 | If you want to append the killed line to the last killed text, |
3720 | use \\[append-next-kill] before \\[kill-ring-save]. | |
3721 | ||
3472b6c6 SM |
3722 | The optional argument REGION if non-nil, indicates that we're not just copying |
3723 | some text between BEG and END, but we're copying the region. | |
3724 | ||
0e264847 | 3725 | This command is similar to `copy-region-as-kill', except that it gives |
90769946 | 3726 | visual feedback indicating the extent of the region being copied." |
76da3455 DK |
3727 | ;; Pass mark first, then point, because the order matters when |
3728 | ;; calling `kill-append'. | |
3729 | (interactive (list (mark) (point) | |
3730 | (prefix-numeric-value current-prefix-arg))) | |
3472b6c6 | 3731 | (copy-region-as-kill beg end region) |
2549c068 CY |
3732 | ;; This use of called-interactively-p is correct because the code it |
3733 | ;; controls just gives the user visual feedback. | |
32226619 | 3734 | (if (called-interactively-p 'interactive) |
2549c068 CY |
3735 | (indicate-copied-region))) |
3736 | ||
3737 | (defun indicate-copied-region (&optional message-len) | |
3738 | "Indicate that the region text has been copied interactively. | |
3739 | If the mark is visible in the selected window, blink the cursor | |
3740 | between point and mark if there is currently no active region | |
3741 | highlighting. | |
3742 | ||
3743 | If the mark lies outside the selected window, display an | |
3744 | informative message containing a sample of the copied text. The | |
3745 | optional argument MESSAGE-LEN, if non-nil, specifies the length | |
3746 | of this sample text; it defaults to 40." | |
3747 | (let ((mark (mark t)) | |
3748 | (point (point)) | |
3749 | ;; Inhibit quitting so we can make a quit here | |
3750 | ;; look like a C-g typed as a command. | |
3751 | (inhibit-quit t)) | |
3752 | (if (pos-visible-in-window-p mark (selected-window)) | |
3753 | ;; Swap point-and-mark quickly so as to show the region that | |
3754 | ;; was selected. Don't do it if the region is highlighted. | |
3755 | (unless (and (region-active-p) | |
3756 | (face-background 'region)) | |
3757 | ;; Swap point and mark. | |
3758 | (set-marker (mark-marker) (point) (current-buffer)) | |
3759 | (goto-char mark) | |
3760 | (sit-for blink-matching-delay) | |
3761 | ;; Swap back. | |
3762 | (set-marker (mark-marker) mark (current-buffer)) | |
3763 | (goto-char point) | |
3764 | ;; If user quit, deactivate the mark | |
3765 | ;; as C-g would as a command. | |
3766 | (and quit-flag mark-active | |
3767 | (deactivate-mark))) | |
3768 | (let ((len (min (abs (- mark point)) | |
3769 | (or message-len 40)))) | |
3770 | (if (< point mark) | |
3771 | ;; Don't say "killed"; that is misleading. | |
3772 | (message "Saved text until \"%s\"" | |
3773 | (buffer-substring-no-properties (- mark len) mark)) | |
3774 | (message "Saved text from \"%s\"" | |
3775 | (buffer-substring-no-properties mark (+ mark len)))))))) | |
2076c87c | 3776 | |
c75d4986 | 3777 | (defun append-next-kill (&optional interactive) |
33f5d4c1 CY |
3778 | "Cause following command, if it kills, to add to previous kill. |
3779 | If the next command kills forward from point, the kill is | |
3780 | appended to the previous killed text. If the command kills | |
3781 | backward, the kill is prepended. Kill commands that act on the | |
3782 | region, such as `kill-region', are regarded as killing forward if | |
3783 | point is after mark, and killing backward if point is before | |
3784 | mark. | |
3785 | ||
3786 | If the next command is not a kill command, `append-next-kill' has | |
3787 | no effect. | |
3788 | ||
c75d4986 KH |
3789 | The argument is used for internal purposes; do not supply one." |
3790 | (interactive "p") | |
3791 | ;; We don't use (interactive-p), since that breaks kbd macros. | |
3792 | (if interactive | |
2076c87c JB |
3793 | (progn |
3794 | (setq this-command 'kill-region) | |
3795 | (message "If the next command is a kill, it will append")) | |
3796 | (setq last-command 'kill-region))) | |
cfb4f123 | 3797 | \f |
93be67de | 3798 | ;; Yanking. |
2076c87c | 3799 | |
2170b1bd CY |
3800 | (defcustom yank-handled-properties |
3801 | '((font-lock-face . yank-handle-font-lock-face-property) | |
3802 | (category . yank-handle-category-property)) | |
3803 | "List of special text property handling conditions for yanking. | |
3804 | Each element should have the form (PROP . FUN), where PROP is a | |
3805 | property symbol and FUN is a function. When the `yank' command | |
3806 | inserts text into the buffer, it scans the inserted text for | |
3807 | stretches of text that have `eq' values of the text property | |
3808 | PROP; for each such stretch of text, FUN is called with three | |
3809 | arguments: the property's value in that text, and the start and | |
3810 | end positions of the text. | |
3811 | ||
3812 | This is done prior to removing the properties specified by | |
3813 | `yank-excluded-properties'." | |
3814 | :group 'killing | |
9c5a5c77 GM |
3815 | :type '(repeat (cons (symbol :tag "property symbol") |
3816 | function)) | |
2170b1bd CY |
3817 | :version "24.3") |
3818 | ||
cfb4f123 RS |
3819 | ;; This is actually used in subr.el but defcustom does not work there. |
3820 | (defcustom yank-excluded-properties | |
2170b1bd CY |
3821 | '(category field follow-link fontified font-lock-face help-echo |
3822 | intangible invisible keymap local-map mouse-face read-only | |
3823 | yank-handler) | |
3137dda8 | 3824 | "Text properties to discard when yanking. |
c6ff5a4c | 3825 | The value should be a list of text properties to discard or t, |
2170b1bd CY |
3826 | which means to discard all text properties. |
3827 | ||
3828 | See also `yank-handled-properties'." | |
cfb4f123 | 3829 | :type '(choice (const :tag "All" t) (repeat symbol)) |
c9f0110e | 3830 | :group 'killing |
2170b1bd | 3831 | :version "24.3") |
cfb4f123 | 3832 | |
120de5bd | 3833 | (defvar yank-window-start nil) |
be5936a7 | 3834 | (defvar yank-undo-function nil |
44f5a7b2 KS |
3835 | "If non-nil, function used by `yank-pop' to delete last stretch of yanked text. |
3836 | Function is called with two parameters, START and END corresponding to | |
3837 | the value of the mark and point; it is guaranteed that START <= END. | |
3838 | Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.") | |
120de5bd | 3839 | |
6b61353c | 3840 | (defun yank-pop (&optional arg) |
ff1fbe3e RS |
3841 | "Replace just-yanked stretch of killed text with a different stretch. |
3842 | This command is allowed only immediately after a `yank' or a `yank-pop'. | |
2076c87c | 3843 | At such a time, the region contains a stretch of reinserted |
ff1fbe3e | 3844 | previously-killed text. `yank-pop' deletes that text and inserts in its |
2076c87c JB |
3845 | place a different stretch of killed text. |
3846 | ||
3847 | With no argument, the previous kill is inserted. | |
ff1fbe3e RS |
3848 | With argument N, insert the Nth previous kill. |
3849 | If N is negative, this is a more recent kill. | |
2076c87c JB |
3850 | |
3851 | The sequence of kills wraps around, so that after the oldest one | |
a0e8eaa3 EZ |
3852 | comes the newest one. |
3853 | ||
3854 | When this command inserts killed text into the buffer, it honors | |
3855 | `yank-excluded-properties' and `yank-handler' as described in the | |
3856 | doc string for `insert-for-yank-1', which see." | |
2076c87c JB |
3857 | (interactive "*p") |
3858 | (if (not (eq last-command 'yank)) | |
3859 | (error "Previous command was not a yank")) | |
3860 | (setq this-command 'yank) | |
6b61353c | 3861 | (unless arg (setq arg 1)) |
3a5da8a8 RS |
3862 | (let ((inhibit-read-only t) |
3863 | (before (< (point) (mark t)))) | |
8254897f KS |
3864 | (if before |
3865 | (funcall (or yank-undo-function 'delete-region) (point) (mark t)) | |
3866 | (funcall (or yank-undo-function 'delete-region) (mark t) (point))) | |
be5936a7 | 3867 | (setq yank-undo-function nil) |
fd0f4056 | 3868 | (set-marker (mark-marker) (point) (current-buffer)) |
cfb4f123 | 3869 | (insert-for-yank (current-kill arg)) |
120de5bd RS |
3870 | ;; Set the window start back where it was in the yank command, |
3871 | ;; if possible. | |
3872 | (set-window-start (selected-window) yank-window-start t) | |
fd0f4056 RS |
3873 | (if before |
3874 | ;; This is like exchange-point-and-mark, but doesn't activate the mark. | |
3875 | ;; It is cleaner to avoid activation, even though the command | |
3876 | ;; loop would deactivate the mark because we inserted text. | |
3877 | (goto-char (prog1 (mark t) | |
3878 | (set-marker (mark-marker) (point) (current-buffer)))))) | |
0964e562 | 3879 | nil) |
2076c87c JB |
3880 | |
3881 | (defun yank (&optional arg) | |
f894e671 | 3882 | "Reinsert (\"paste\") the last stretch of killed text. |
2170b1bd CY |
3883 | More precisely, reinsert the most recent kill, which is the |
3884 | stretch of killed text most recently killed OR yanked. Put point | |
3885 | at the end, and set mark at the beginning without activating it. | |
3886 | With just \\[universal-argument] as argument, put point at beginning, and mark at end. | |
3887 | With argument N, reinsert the Nth most recent kill. | |
3888 | ||
3889 | When this command inserts text into the buffer, it honors the | |
3890 | `yank-handled-properties' and `yank-excluded-properties' | |
3891 | variables, and the `yank-handler' text property. See | |
3892 | `insert-for-yank-1' for details. | |
a0e8eaa3 | 3893 | |
a9b9303c | 3894 | See also the command `yank-pop' (\\[yank-pop])." |
2076c87c | 3895 | (interactive "*P") |
120de5bd | 3896 | (setq yank-window-start (window-start)) |
456c617c RS |
3897 | ;; If we don't get all the way thru, make last-command indicate that |
3898 | ;; for the following command. | |
3899 | (setq this-command t) | |
2076c87c | 3900 | (push-mark (point)) |
cfb4f123 RS |
3901 | (insert-for-yank (current-kill (cond |
3902 | ((listp arg) 0) | |
6b61353c | 3903 | ((eq arg '-) -2) |
cfb4f123 | 3904 | (t (1- arg))))) |
2076c87c | 3905 | (if (consp arg) |
fd0f4056 RS |
3906 | ;; This is like exchange-point-and-mark, but doesn't activate the mark. |
3907 | ;; It is cleaner to avoid activation, even though the command | |
3908 | ;; loop would deactivate the mark because we inserted text. | |
3909 | (goto-char (prog1 (mark t) | |
3910 | (set-marker (mark-marker) (point) (current-buffer))))) | |
456c617c | 3911 | ;; If we do get all the way thru, make this-command indicate that. |
be5936a7 KS |
3912 | (if (eq this-command t) |
3913 | (setq this-command 'yank)) | |
0964e562 | 3914 | nil) |
70e14c01 JB |
3915 | |
3916 | (defun rotate-yank-pointer (arg) | |
3917 | "Rotate the yanking point in the kill ring. | |
5626c14e | 3918 | With ARG, rotate that many kills forward (or backward, if negative)." |
70e14c01 JB |
3919 | (interactive "p") |
3920 | (current-kill arg)) | |
2d88b556 | 3921 | \f |
93be67de KH |
3922 | ;; Some kill commands. |
3923 | ||
3924 | ;; Internal subroutine of delete-char | |
3925 | (defun kill-forward-chars (arg) | |
3926 | (if (listp arg) (setq arg (car arg))) | |
3927 | (if (eq arg '-) (setq arg -1)) | |
673e5169 | 3928 | (kill-region (point) (+ (point) arg))) |
93be67de KH |
3929 | |
3930 | ;; Internal subroutine of backward-delete-char | |
3931 | (defun kill-backward-chars (arg) | |
3932 | (if (listp arg) (setq arg (car arg))) | |
3933 | (if (eq arg '-) (setq arg -1)) | |
673e5169 | 3934 | (kill-region (point) (- (point) arg))) |
93be67de KH |
3935 | |
3936 | (defcustom backward-delete-char-untabify-method 'untabify | |
1d2b0303 | 3937 | "The method for untabifying when deleting backward. |
1e722f9f SS |
3938 | Can be `untabify' -- turn a tab to many spaces, then delete one space; |
3939 | `hungry' -- delete all whitespace, both tabs and spaces; | |
3940 | `all' -- delete all whitespace, including tabs, spaces and newlines; | |
93be67de | 3941 | nil -- just delete one character." |
1e722f9f | 3942 | :type '(choice (const untabify) (const hungry) (const all) (const nil)) |
03167a34 | 3943 | :version "20.3" |
93be67de KH |
3944 | :group 'killing) |
3945 | ||
3946 | (defun backward-delete-char-untabify (arg &optional killp) | |
3947 | "Delete characters backward, changing tabs into spaces. | |
3948 | The exact behavior depends on `backward-delete-char-untabify-method'. | |
3949 | Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil. | |
3950 | Interactively, ARG is the prefix arg (default 1) | |
3951 | and KILLP is t if a prefix arg was specified." | |
3952 | (interactive "*p\nP") | |
3953 | (when (eq backward-delete-char-untabify-method 'untabify) | |
3954 | (let ((count arg)) | |
3955 | (save-excursion | |
3956 | (while (and (> count 0) (not (bobp))) | |
3957 | (if (= (preceding-char) ?\t) | |
3958 | (let ((col (current-column))) | |
3959 | (forward-char -1) | |
3960 | (setq col (- col (current-column))) | |
f33321ad | 3961 | (insert-char ?\s col) |
93be67de KH |
3962 | (delete-char 1))) |
3963 | (forward-char -1) | |
3964 | (setq count (1- count)))))) | |
0b1596c6 | 3965 | (let* ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t") |
1e722f9f | 3966 | ((eq backward-delete-char-untabify-method 'all) |
0b1596c6 JB |
3967 | " \t\n\r"))) |
3968 | (n (if skip | |
109aa8a9 LL |
3969 | (let* ((oldpt (point)) |
3970 | (wh (- oldpt (save-excursion | |
3971 | (skip-chars-backward skip) | |
3972 | (constrain-to-field nil oldpt))))) | |
0b1596c6 JB |
3973 | (+ arg (if (zerop wh) 0 (1- wh)))) |
3974 | arg))) | |
3975 | ;; Avoid warning about delete-backward-char | |
3976 | (with-no-warnings (delete-backward-char n killp)))) | |
93be67de KH |
3977 | |
3978 | (defun zap-to-char (arg char) | |
5626c14e | 3979 | "Kill up to and including ARGth occurrence of CHAR. |
93be67de KH |
3980 | Case is ignored if `case-fold-search' is non-nil in the current buffer. |
3981 | Goes backward if ARG is negative; error if CHAR not found." | |
74beb59f LMI |
3982 | (interactive (list (prefix-numeric-value current-prefix-arg) |
3983 | (read-char "Zap to char: " t))) | |
a6c39c14 EZ |
3984 | ;; Avoid "obsolete" warnings for translation-table-for-input. |
3985 | (with-no-warnings | |
3986 | (if (char-table-p translation-table-for-input) | |
3987 | (setq char (or (aref translation-table-for-input char) char)))) | |
93be67de KH |
3988 | (kill-region (point) (progn |
3989 | (search-forward (char-to-string char) nil nil arg) | |
93be67de | 3990 | (point)))) |
eaae8106 | 3991 | |
93be67de KH |
3992 | ;; kill-line and its subroutines. |
3993 | ||
3994 | (defcustom kill-whole-line nil | |
cb442973 | 3995 | "If non-nil, `kill-line' with no arg at start of line kills the whole line." |
93be67de KH |
3996 | :type 'boolean |
3997 | :group 'killing) | |
3998 | ||
3999 | (defun kill-line (&optional arg) | |
4000 | "Kill the rest of the current line; if no nonblanks there, kill thru newline. | |
5626c14e | 4001 | With prefix argument ARG, kill that many lines from point. |
93be67de | 4002 | Negative arguments kill lines backward. |
8be7408c | 4003 | With zero argument, kills the text before point on the current line. |
93be67de KH |
4004 | |
4005 | When calling from a program, nil means \"no arg\", | |
4006 | a number counts as a prefix arg. | |
4007 | ||
4008 | To kill a whole line, when point is not at the beginning, type \ | |
602157ab | 4009 | \\[move-beginning-of-line] \\[kill-line] \\[kill-line]. |
93be67de | 4010 | |
022de23e LMI |
4011 | If `show-trailing-whitespace' is non-nil, this command will just |
4012 | kill the rest of the current line, even if there are only | |
4013 | nonblanks there. | |
4014 | ||
9fc9a531 | 4015 | If option `kill-whole-line' is non-nil, then this command kills the whole line |
93be67de KH |
4016 | including its terminating newline, when used at the beginning of a line |
4017 | with no argument. As a consequence, you can always kill a whole line | |
602157ab | 4018 | by typing \\[move-beginning-of-line] \\[kill-line]. |
d3f22784 | 4019 | |
81558867 EZ |
4020 | If you want to append the killed line to the last killed text, |
4021 | use \\[append-next-kill] before \\[kill-line]. | |
4022 | ||
d3f22784 EZ |
4023 | If the buffer is read-only, Emacs will beep and refrain from deleting |
4024 | the line, but put the line in the kill ring anyway. This means that | |
1a534b89 RS |
4025 | you can use this command to copy text from a read-only buffer. |
4026 | \(If the variable `kill-read-only-ok' is non-nil, then this won't | |
4027 | even beep.)" | |
e761e42c | 4028 | (interactive "P") |
93be67de KH |
4029 | (kill-region (point) |
4030 | ;; It is better to move point to the other end of the kill | |
4031 | ;; before killing. That way, in a read-only buffer, point | |
4032 | ;; moves across the text that is copied to the kill ring. | |
4033 | ;; The choice has no effect on undo now that undo records | |
4034 | ;; the value of point from before the command was run. | |
4035 | (progn | |
4036 | (if arg | |
4037 | (forward-visible-line (prefix-numeric-value arg)) | |
4038 | (if (eobp) | |
4039 | (signal 'end-of-buffer nil)) | |
5560dc5d RS |
4040 | (let ((end |
4041 | (save-excursion | |
4042 | (end-of-visible-line) (point)))) | |
4043 | (if (or (save-excursion | |
6b61353c KH |
4044 | ;; If trailing whitespace is visible, |
4045 | ;; don't treat it as nothing. | |
4046 | (unless show-trailing-whitespace | |
4047 | (skip-chars-forward " \t" end)) | |
5560dc5d RS |
4048 | (= (point) end)) |
4049 | (and kill-whole-line (bolp))) | |
4050 | (forward-visible-line 1) | |
4051 | (goto-char end)))) | |
93be67de KH |
4052 | (point)))) |
4053 | ||
348de80b KG |
4054 | (defun kill-whole-line (&optional arg) |
4055 | "Kill current line. | |
5626c14e JB |
4056 | With prefix ARG, kill that many lines starting from the current line. |
4057 | If ARG is negative, kill backward. Also kill the preceding newline. | |
01ba9662 | 4058 | \(This is meant to make \\[repeat] work well with negative arguments.\) |
5626c14e | 4059 | If ARG is zero, kill current line but exclude the trailing newline." |
f8b0f284 | 4060 | (interactive "p") |
186133b4 | 4061 | (or arg (setq arg 1)) |
6c770e38 LT |
4062 | (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp))) |
4063 | (signal 'end-of-buffer nil)) | |
4064 | (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp))) | |
4065 | (signal 'beginning-of-buffer nil)) | |
4066 | (unless (eq last-command 'kill-region) | |
4067 | (kill-new "") | |
4068 | (setq last-command 'kill-region)) | |
348de80b | 4069 | (cond ((zerop arg) |
6c770e38 LT |
4070 | ;; We need to kill in two steps, because the previous command |
4071 | ;; could have been a kill command, in which case the text | |
4072 | ;; before point needs to be prepended to the current kill | |
4073 | ;; ring entry and the text after point appended. Also, we | |
4074 | ;; need to use save-excursion to avoid copying the same text | |
4075 | ;; twice to the kill ring in read-only buffers. | |
4076 | (save-excursion | |
4077 | (kill-region (point) (progn (forward-visible-line 0) (point)))) | |
348de80b KG |
4078 | (kill-region (point) (progn (end-of-visible-line) (point)))) |
4079 | ((< arg 0) | |
6c770e38 LT |
4080 | (save-excursion |
4081 | (kill-region (point) (progn (end-of-visible-line) (point)))) | |
4082 | (kill-region (point) | |
4083 | (progn (forward-visible-line (1+ arg)) | |
4084 | (unless (bobp) (backward-char)) | |
4085 | (point)))) | |
348de80b | 4086 | (t |
6c770e38 LT |
4087 | (save-excursion |
4088 | (kill-region (point) (progn (forward-visible-line 0) (point)))) | |
4089 | (kill-region (point) | |
4090 | (progn (forward-visible-line arg) (point)))))) | |
12a93712 | 4091 | |
93be67de KH |
4092 | (defun forward-visible-line (arg) |
4093 | "Move forward by ARG lines, ignoring currently invisible newlines only. | |
4094 | If ARG is negative, move backward -ARG lines. | |
4095 | If ARG is zero, move to the beginning of the current line." | |
4096 | (condition-case nil | |
4097 | (if (> arg 0) | |
12a93712 RS |
4098 | (progn |
4099 | (while (> arg 0) | |
93be67de | 4100 | (or (zerop (forward-line 1)) |
12a93712 RS |
4101 | (signal 'end-of-buffer nil)) |
4102 | ;; If the newline we just skipped is invisible, | |
4103 | ;; don't count it. | |
4104 | (let ((prop | |
4105 | (get-char-property (1- (point)) 'invisible))) | |
4106 | (if (if (eq buffer-invisibility-spec t) | |
4107 | prop | |
4108 | (or (memq prop buffer-invisibility-spec) | |
4109 | (assq prop buffer-invisibility-spec))) | |
4110 | (setq arg (1+ arg)))) | |
4111 | (setq arg (1- arg))) | |
4112 | ;; If invisible text follows, and it is a number of complete lines, | |
4113 | ;; skip it. | |
4114 | (let ((opoint (point))) | |
4115 | (while (and (not (eobp)) | |
4116 | (let ((prop | |
4117 | (get-char-property (point) 'invisible))) | |
4118 | (if (eq buffer-invisibility-spec t) | |
4119 | prop | |
4120 | (or (memq prop buffer-invisibility-spec) | |
4121 | (assq prop buffer-invisibility-spec))))) | |
4122 | (goto-char | |
4123 | (if (get-text-property (point) 'invisible) | |
4124 | (or (next-single-property-change (point) 'invisible) | |
4125 | (point-max)) | |
4126 | (next-overlay-change (point))))) | |
4127 | (unless (bolp) | |
4128 | (goto-char opoint)))) | |
93be67de | 4129 | (let ((first t)) |
f5fd8833 JB |
4130 | (while (or first (<= arg 0)) |
4131 | (if first | |
93be67de KH |
4132 | (beginning-of-line) |
4133 | (or (zerop (forward-line -1)) | |
4134 | (signal 'beginning-of-buffer nil))) | |
12a93712 RS |
4135 | ;; If the newline we just moved to is invisible, |
4136 | ;; don't count it. | |
4137 | (unless (bobp) | |
4138 | (let ((prop | |
4139 | (get-char-property (1- (point)) 'invisible))) | |
f5fd8833 JB |
4140 | (unless (if (eq buffer-invisibility-spec t) |
4141 | prop | |
4142 | (or (memq prop buffer-invisibility-spec) | |
4143 | (assq prop buffer-invisibility-spec))) | |
4144 | (setq arg (1+ arg))))) | |
4145 | (setq first nil)) | |
12a93712 RS |
4146 | ;; If invisible text follows, and it is a number of complete lines, |
4147 | ;; skip it. | |
4148 | (let ((opoint (point))) | |
93be67de KH |
4149 | (while (and (not (bobp)) |
4150 | (let ((prop | |
4151 | (get-char-property (1- (point)) 'invisible))) | |
4152 | (if (eq buffer-invisibility-spec t) | |
4153 | prop | |
4154 | (or (memq prop buffer-invisibility-spec) | |
4155 | (assq prop buffer-invisibility-spec))))) | |
4156 | (goto-char | |
4157 | (if (get-text-property (1- (point)) 'invisible) | |
4158 | (or (previous-single-property-change (point) 'invisible) | |
4159 | (point-min)) | |
12a93712 RS |
4160 | (previous-overlay-change (point))))) |
4161 | (unless (bolp) | |
4162 | (goto-char opoint))))) | |
93be67de KH |
4163 | ((beginning-of-buffer end-of-buffer) |
4164 | nil))) | |
70e14c01 | 4165 | |
93be67de KH |
4166 | (defun end-of-visible-line () |
4167 | "Move to end of current visible line." | |
4168 | (end-of-line) | |
4169 | ;; If the following character is currently invisible, | |
4170 | ;; skip all characters with that same `invisible' property value, | |
4171 | ;; then find the next newline. | |
4172 | (while (and (not (eobp)) | |
5560dc5d RS |
4173 | (save-excursion |
4174 | (skip-chars-forward "^\n") | |
4175 | (let ((prop | |
4176 | (get-char-property (point) 'invisible))) | |
4177 | (if (eq buffer-invisibility-spec t) | |
4178 | prop | |
4179 | (or (memq prop buffer-invisibility-spec) | |
4180 | (assq prop buffer-invisibility-spec)))))) | |
4181 | (skip-chars-forward "^\n") | |
93be67de | 4182 | (if (get-text-property (point) 'invisible) |
64fee311 CY |
4183 | (goto-char (or (next-single-property-change (point) 'invisible) |
4184 | (point-max))) | |
93be67de KH |
4185 | (goto-char (next-overlay-change (point)))) |
4186 | (end-of-line))) | |
2d88b556 | 4187 | \f |
2076c87c JB |
4188 | (defun insert-buffer (buffer) |
4189 | "Insert after point the contents of BUFFER. | |
4190 | Puts mark after the inserted text. | |
6cb6e7a2 GM |
4191 | BUFFER may be a buffer or a buffer name. |
4192 | ||
4193 | This function is meant for the user to run interactively. | |
1e96c007 | 4194 | Don't call it from programs: use `insert-buffer-substring' instead!" |
c3d4f949 | 4195 | (interactive |
a3e7c391 FP |
4196 | (list |
4197 | (progn | |
4198 | (barf-if-buffer-read-only) | |
4199 | (read-buffer "Insert buffer: " | |
290d5b58 | 4200 | (if (eq (selected-window) (next-window)) |
a3e7c391 | 4201 | (other-buffer (current-buffer)) |
290d5b58 | 4202 | (window-buffer (next-window))) |
a3e7c391 | 4203 | t)))) |
1e96c007 SM |
4204 | (push-mark |
4205 | (save-excursion | |
4206 | (insert-buffer-substring (get-buffer buffer)) | |
4207 | (point))) | |
1537a263 | 4208 | nil) |
3e2fb4db | 4209 | (put 'insert-buffer 'interactive-only 'insert-buffer-substring) |
2076c87c JB |
4210 | |
4211 | (defun append-to-buffer (buffer start end) | |
4212 | "Append to specified buffer the text of the region. | |
4213 | It is inserted into that buffer before its point. | |
4214 | ||
4215 | When calling from a program, give three arguments: | |
4216 | BUFFER (or buffer name), START and END. | |
4217 | START and END specify the portion of the current buffer to be copied." | |
70e14c01 | 4218 | (interactive |
5d771766 | 4219 | (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t)) |
23efee2c | 4220 | (region-beginning) (region-end))) |
85626eef SM |
4221 | (let* ((oldbuf (current-buffer)) |
4222 | (append-to (get-buffer-create buffer)) | |
4223 | (windows (get-buffer-window-list append-to t t)) | |
4224 | point) | |
4225 | (save-excursion | |
4226 | (with-current-buffer append-to | |
4227 | (setq point (point)) | |
4228 | (barf-if-buffer-read-only) | |
4229 | (insert-buffer-substring oldbuf start end) | |
4230 | (dolist (window windows) | |
4231 | (when (= (window-point window) point) | |
4232 | (set-window-point window (point)))))))) | |
2076c87c JB |
4233 | |
4234 | (defun prepend-to-buffer (buffer start end) | |
4235 | "Prepend to specified buffer the text of the region. | |
4236 | It is inserted into that buffer after its point. | |
4237 | ||
4238 | When calling from a program, give three arguments: | |
4239 | BUFFER (or buffer name), START and END. | |
4240 | START and END specify the portion of the current buffer to be copied." | |
4241 | (interactive "BPrepend to buffer: \nr") | |
4242 | (let ((oldbuf (current-buffer))) | |
7fdbcd83 | 4243 | (with-current-buffer (get-buffer-create buffer) |
74399eac | 4244 | (barf-if-buffer-read-only) |
2076c87c JB |
4245 | (save-excursion |
4246 | (insert-buffer-substring oldbuf start end))))) | |
4247 | ||
4248 | (defun copy-to-buffer (buffer start end) | |
4249 | "Copy to specified buffer the text of the region. | |
4250 | It is inserted into that buffer, replacing existing text there. | |
4251 | ||
4252 | When calling from a program, give three arguments: | |
4253 | BUFFER (or buffer name), START and END. | |
4254 | START and END specify the portion of the current buffer to be copied." | |
4255 | (interactive "BCopy to buffer: \nr") | |
4256 | (let ((oldbuf (current-buffer))) | |
1b5fd09e | 4257 | (with-current-buffer (get-buffer-create buffer) |
74399eac | 4258 | (barf-if-buffer-read-only) |
2076c87c JB |
4259 | (erase-buffer) |
4260 | (save-excursion | |
4261 | (insert-buffer-substring oldbuf start end))))) | |
2d88b556 | 4262 | \f |
54bd972f | 4263 | (define-error 'mark-inactive (purecopy "The mark is not active now")) |
62d1c1fc | 4264 | |
0251bafb RS |
4265 | (defvar activate-mark-hook nil |
4266 | "Hook run when the mark becomes active. | |
4267 | It is also run at the end of a command, if the mark is active and | |
6cbb0bb0 | 4268 | it is possible that the region may have changed.") |
0251bafb RS |
4269 | |
4270 | (defvar deactivate-mark-hook nil | |
4271 | "Hook run when the mark becomes inactive.") | |
4272 | ||
af39530e | 4273 | (defun mark (&optional force) |
f00239cf RS |
4274 | "Return this buffer's mark value as integer, or nil if never set. |
4275 | ||
4276 | In Transient Mark mode, this function signals an error if | |
4277 | the mark is not active. However, if `mark-even-if-inactive' is non-nil, | |
4278 | or the argument FORCE is non-nil, it disregards whether the mark | |
4279 | is active, and returns an integer or nil in the usual way. | |
af39530e | 4280 | |
2076c87c JB |
4281 | If you are using this in an editing command, you are most likely making |
4282 | a mistake; see the documentation of `set-mark'." | |
0e3a7b14 | 4283 | (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive) |
af39530e | 4284 | (marker-position (mark-marker)) |
62d1c1fc | 4285 | (signal 'mark-inactive nil))) |
2076c87c | 4286 | |
c440407a GM |
4287 | ;; Behind display-selections-p. |
4288 | (declare-function x-selection-owner-p "xselect.c" | |
4289 | (&optional selection terminal)) | |
4290 | (declare-function x-selection-exists-p "xselect.c" | |
4291 | (&optional selection terminal)) | |
4292 | ||
cdca8255 | 4293 | (defun deactivate-mark (&optional force) |
6e9bad14 CY |
4294 | "Deactivate the mark. |
4295 | If Transient Mark mode is disabled, this function normally does | |
4296 | nothing; but if FORCE is non-nil, it deactivates the mark anyway. | |
4297 | ||
4298 | Deactivating the mark sets `mark-active' to nil, updates the | |
4299 | primary selection according to `select-active-regions', and runs | |
4300 | `deactivate-mark-hook'. | |
4301 | ||
4302 | If Transient Mark mode was temporarily enabled, reset the value | |
4303 | of the variable `transient-mark-mode'; if this causes Transient | |
4304 | Mark mode to be disabled, don't change `mark-active' to nil or | |
4305 | run `deactivate-mark-hook'." | |
f9be2e35 | 4306 | (when (or transient-mark-mode force) |
7c23dd44 CY |
4307 | (when (and (if (eq select-active-regions 'only) |
4308 | (eq (car-safe transient-mark-mode) 'only) | |
4309 | select-active-regions) | |
9852377f CY |
4310 | (region-active-p) |
4311 | (display-selections-p)) | |
4312 | ;; The var `saved-region-selection', if non-nil, is the text in | |
4313 | ;; the region prior to the last command modifying the buffer. | |
4314 | ;; Set the selection to that, or to the current region. | |
4315 | (cond (saved-region-selection | |
4316 | (x-set-selection 'PRIMARY saved-region-selection) | |
4317 | (setq saved-region-selection nil)) | |
d75be97d CY |
4318 | ;; If another program has acquired the selection, region |
4319 | ;; deactivation should not clobber it (Bug#11772). | |
4320 | ((and (/= (region-beginning) (region-end)) | |
4321 | (or (x-selection-owner-p 'PRIMARY) | |
4322 | (null (x-selection-exists-p 'PRIMARY)))) | |
9852377f | 4323 | (x-set-selection 'PRIMARY |
3472b6c6 | 4324 | (funcall region-extract-function nil))))) |
f9be2e35 CY |
4325 | (if (and (null force) |
4326 | (or (eq transient-mark-mode 'lambda) | |
4327 | (and (eq (car-safe transient-mark-mode) 'only) | |
4328 | (null (cdr transient-mark-mode))))) | |
4329 | ;; When deactivating a temporary region, don't change | |
4330 | ;; `mark-active' or run `deactivate-mark-hook'. | |
109cfe4e CY |
4331 | (setq transient-mark-mode nil) |
4332 | (if (eq (car-safe transient-mark-mode) 'only) | |
4333 | (setq transient-mark-mode (cdr transient-mark-mode))) | |
4334 | (setq mark-active nil) | |
4335 | (run-hooks 'deactivate-mark-hook)))) | |
19d35374 | 4336 | |
2977fc37 SM |
4337 | (defun activate-mark () |
4338 | "Activate the mark." | |
4339 | (when (mark t) | |
4340 | (setq mark-active t) | |
4341 | (unless transient-mark-mode | |
31646597 KD |
4342 | (setq transient-mark-mode 'lambda)) |
4343 | (run-hooks 'activate-mark-hook))) | |
98b2fff4 | 4344 | |
2076c87c JB |
4345 | (defun set-mark (pos) |
4346 | "Set this buffer's mark to POS. Don't use this function! | |
4347 | That is to say, don't use this function unless you want | |
4348 | the user to see that the mark has moved, and you want the previous | |
4349 | mark position to be lost. | |
4350 | ||
4351 | Normally, when a new mark is set, the old one should go on the stack. | |
f59006cb | 4352 | This is why most applications should use `push-mark', not `set-mark'. |
2076c87c | 4353 | |
ff1fbe3e | 4354 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
4355 | purposes. The mark saves a location for the user's convenience. |
4356 | Most editing commands should not alter the mark. | |
4357 | To remember a location for internal use in the Lisp program, | |
4358 | store it in a Lisp variable. Example: | |
4359 | ||
4360 | (let ((beg (point))) (forward-line 1) (delete-region beg (point)))." | |
4361 | ||
fcadf1c7 RS |
4362 | (if pos |
4363 | (progn | |
4364 | (setq mark-active t) | |
4365 | (run-hooks 'activate-mark-hook) | |
4366 | (set-marker (mark-marker) pos (current-buffer))) | |
24c22852 | 4367 | ;; Normally we never clear mark-active except in Transient Mark mode. |
f9be2e35 CY |
4368 | ;; But when we actually clear out the mark value too, we must |
4369 | ;; clear mark-active in any mode. | |
4370 | (deactivate-mark t) | |
24c22852 | 4371 | (set-marker (mark-marker) nil))) |
2076c87c | 4372 | |
d03b9b31 | 4373 | (defcustom use-empty-active-region nil |
6a9127b6 CY |
4374 | "Whether \"region-aware\" commands should act on empty regions. |
4375 | If nil, region-aware commands treat empty regions as inactive. | |
4376 | If non-nil, region-aware commands treat the region as active as | |
4377 | long as the mark is active, even if the region is empty. | |
4378 | ||
58b356e9 CY |
4379 | Region-aware commands are those that act on the region if it is |
4380 | active and Transient Mark mode is enabled, and on the text near | |
4381 | point otherwise." | |
d03b9b31 RS |
4382 | :type 'boolean |
4383 | :version "23.1" | |
4384 | :group 'editing-basics) | |
4385 | ||
cb3a9d33 | 4386 | (defun use-region-p () |
6a9127b6 CY |
4387 | "Return t if the region is active and it is appropriate to act on it. |
4388 | This is used by commands that act specially on the region under | |
16f2e9fc | 4389 | Transient Mark mode. |
6a9127b6 | 4390 | |
c876b263 | 4391 | The return value is t if Transient Mark mode is enabled and the |
e5b826ae CY |
4392 | mark is active; furthermore, if `use-empty-active-region' is nil, |
4393 | the region must not be empty. Otherwise, the return value is nil. | |
16f2e9fc CY |
4394 | |
4395 | For some commands, it may be appropriate to ignore the value of | |
4396 | `use-empty-active-region'; in that case, use `region-active-p'." | |
d34c311a | 4397 | (and (region-active-p) |
d03b9b31 RS |
4398 | (or use-empty-active-region (> (region-end) (region-beginning))))) |
4399 | ||
02d52519 | 4400 | (defun region-active-p () |
afa39f21 | 4401 | "Return t if Transient Mark mode is enabled and the mark is active. |
6a9127b6 | 4402 | |
16f2e9fc CY |
4403 | Some commands act specially on the region when Transient Mark |
4404 | mode is enabled. Usually, such commands should use | |
4405 | `use-region-p' instead of this function, because `use-region-p' | |
4406 | also checks the value of `use-empty-active-region'." | |
02d52519 RS |
4407 | (and transient-mark-mode mark-active)) |
4408 | ||
3472b6c6 SM |
4409 | |
4410 | (defvar redisplay-unhighlight-region-function | |
4411 | (lambda (rol) (when (overlayp rol) (delete-overlay rol)))) | |
4412 | ||
4413 | (defvar redisplay-highlight-region-function | |
4414 | (lambda (start end window rol) | |
4415 | (if (not (overlayp rol)) | |
4416 | (let ((nrol (make-overlay start end))) | |
4417 | (funcall redisplay-unhighlight-region-function rol) | |
4418 | (overlay-put nrol 'window window) | |
4419 | (overlay-put nrol 'face 'region) | |
4420 | nrol) | |
4421 | (unless (and (eq (overlay-buffer rol) (current-buffer)) | |
4422 | (eq (overlay-start rol) start) | |
4423 | (eq (overlay-end rol) end)) | |
4424 | (move-overlay rol start end (current-buffer))) | |
4425 | rol))) | |
4426 | ||
4427 | (defun redisplay--update-region-highlight (window) | |
4428 | (with-current-buffer (window-buffer window) | |
4429 | (let ((rol (window-parameter window 'internal-region-overlay))) | |
4430 | (if (not (region-active-p)) | |
4431 | (funcall redisplay-unhighlight-region-function rol) | |
4432 | (let* ((pt (window-point window)) | |
4433 | (mark (mark)) | |
4434 | (start (min pt mark)) | |
4435 | (end (max pt mark)) | |
4436 | (new | |
4437 | (funcall redisplay-highlight-region-function | |
4438 | start end window rol))) | |
4439 | (unless (equal new rol) | |
4440 | (set-window-parameter window 'internal-region-overlay | |
4441 | new))))))) | |
4442 | ||
4443 | (defun redisplay--update-region-highlights (windows) | |
4444 | (with-demoted-errors "redisplay--update-region-highlights: %S" | |
4445 | (if (null windows) | |
4446 | (redisplay--update-region-highlight (selected-window)) | |
4447 | (unless (listp windows) (setq windows (window-list-1 nil nil t))) | |
4448 | (if highlight-nonselected-windows | |
4449 | (mapc #'redisplay--update-region-highlight windows) | |
4450 | (let ((msw (and (window-minibuffer-p) (minibuffer-selected-window)))) | |
4451 | (dolist (w windows) | |
4452 | (if (or (eq w (selected-window)) (eq w msw)) | |
4453 | (redisplay--update-region-highlight w) | |
4454 | (funcall redisplay-unhighlight-region-function | |
4455 | (window-parameter w 'internal-region-overlay))))))))) | |
4456 | ||
4457 | (add-function :before pre-redisplay-function | |
4458 | #'redisplay--update-region-highlights) | |
4459 | ||
4460 | ||
4461 | (defvar-local mark-ring nil | |
e55e2267 | 4462 | "The list of former marks of the current buffer, most recent first.") |
e55e2267 | 4463 | (put 'mark-ring 'permanent-local t) |
2076c87c | 4464 | |
69c1dd37 | 4465 | (defcustom mark-ring-max 16 |
1d2b0303 | 4466 | "Maximum size of mark ring. Start discarding off end if gets this big." |
69c1dd37 RS |
4467 | :type 'integer |
4468 | :group 'editing-basics) | |
2076c87c | 4469 | |
dc029f0b RM |
4470 | (defvar global-mark-ring nil |
4471 | "The list of saved global marks, most recent first.") | |
4472 | ||
69c1dd37 | 4473 | (defcustom global-mark-ring-max 16 |
1d2b0303 | 4474 | "Maximum size of global mark ring. \ |
69c1dd37 RS |
4475 | Start discarding off end if gets this big." |
4476 | :type 'integer | |
4477 | :group 'editing-basics) | |
dc029f0b | 4478 | |
868c2f49 | 4479 | (defun pop-to-mark-command () |
5626c14e JB |
4480 | "Jump to mark, and pop a new position for mark off the ring. |
4481 | \(Does not affect global mark ring\)." | |
868c2f49 KS |
4482 | (interactive) |
4483 | (if (null (mark t)) | |
4484 | (error "No mark set in this buffer") | |
fb2c06a3 RS |
4485 | (if (= (point) (mark t)) |
4486 | (message "Mark popped")) | |
868c2f49 KS |
4487 | (goto-char (mark t)) |
4488 | (pop-mark))) | |
4489 | ||
d00ffe21 | 4490 | (defun push-mark-command (arg &optional nomsg) |
868c2f49 | 4491 | "Set mark at where point is. |
5626c14e | 4492 | If no prefix ARG and mark is already set there, just activate it. |
d00ffe21 | 4493 | Display `Mark set' unless the optional second arg NOMSG is non-nil." |
868c2f49 KS |
4494 | (interactive "P") |
4495 | (let ((mark (marker-position (mark-marker)))) | |
4496 | (if (or arg (null mark) (/= mark (point))) | |
d00ffe21 | 4497 | (push-mark nil nomsg t) |
868c2f49 | 4498 | (setq mark-active t) |
0251bafb | 4499 | (run-hooks 'activate-mark-hook) |
d00ffe21 KS |
4500 | (unless nomsg |
4501 | (message "Mark activated"))))) | |
868c2f49 | 4502 | |
6a936796 | 4503 | (defcustom set-mark-command-repeat-pop nil |
1d2b0303 | 4504 | "Non-nil means repeating \\[set-mark-command] after popping mark pops it again. |
ebd2fc0d RS |
4505 | That means that C-u \\[set-mark-command] \\[set-mark-command] |
4506 | will pop the mark twice, and | |
4507 | C-u \\[set-mark-command] \\[set-mark-command] \\[set-mark-command] | |
4508 | will pop the mark three times. | |
4509 | ||
7b17b503 | 4510 | A value of nil means \\[set-mark-command]'s behavior does not change |
ebd2fc0d | 4511 | after C-u \\[set-mark-command]." |
6a936796 | 4512 | :type 'boolean |
034ce0ec | 4513 | :group 'editing-basics) |
6a936796 | 4514 | |
2076c87c | 4515 | (defun set-mark-command (arg) |
fb2c06a3 RS |
4516 | "Set the mark where point is, or jump to the mark. |
4517 | Setting the mark also alters the region, which is the text | |
4518 | between point and mark; this is the closest equivalent in | |
4519 | Emacs to what some editors call the \"selection\". | |
146adea3 | 4520 | |
fb2c06a3 RS |
4521 | With no prefix argument, set the mark at point, and push the |
4522 | old mark position on local mark ring. Also push the old mark on | |
4523 | global mark ring, if the previous mark was set in another buffer. | |
146adea3 | 4524 | |
17923ef2 CY |
4525 | When Transient Mark Mode is off, immediately repeating this |
4526 | command activates `transient-mark-mode' temporarily. | |
66ef2df9 | 4527 | |
146adea3 | 4528 | With prefix argument \(e.g., \\[universal-argument] \\[set-mark-command]\), \ |
fb2c06a3 | 4529 | jump to the mark, and set the mark from |
146adea3 EZ |
4530 | position popped off the local mark ring \(this does not affect the global |
4531 | mark ring\). Use \\[pop-global-mark] to jump to a mark popped off the global | |
66ef2df9 | 4532 | mark ring \(see `pop-global-mark'\). |
18c5df40 | 4533 | |
2ef0a47e | 4534 | If `set-mark-command-repeat-pop' is non-nil, repeating |
146adea3 | 4535 | the \\[set-mark-command] command with no prefix argument pops the next position |
2ef0a47e | 4536 | off the local (or global) mark ring and jumps there. |
66ef2df9 | 4537 | |
fb2c06a3 RS |
4538 | With \\[universal-argument] \\[universal-argument] as prefix |
4539 | argument, unconditionally set mark where point is, even if | |
4540 | `set-mark-command-repeat-pop' is non-nil. | |
7cb42362 | 4541 | |
ff1fbe3e | 4542 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
4543 | purposes. See the documentation of `set-mark' for more information." |
4544 | (interactive "P") | |
109cfe4e CY |
4545 | (cond ((eq transient-mark-mode 'lambda) |
4546 | (setq transient-mark-mode nil)) | |
4547 | ((eq (car-safe transient-mark-mode) 'only) | |
4548 | (deactivate-mark))) | |
868c2f49 | 4549 | (cond |
18c5df40 KS |
4550 | ((and (consp arg) (> (prefix-numeric-value arg) 4)) |
4551 | (push-mark-command nil)) | |
868c2f49 | 4552 | ((not (eq this-command 'set-mark-command)) |
1841f9e3 KS |
4553 | (if arg |
4554 | (pop-to-mark-command) | |
4555 | (push-mark-command t))) | |
6a936796 RS |
4556 | ((and set-mark-command-repeat-pop |
4557 | (eq last-command 'pop-to-mark-command)) | |
66ef2df9 KS |
4558 | (setq this-command 'pop-to-mark-command) |
4559 | (pop-to-mark-command)) | |
6a936796 RS |
4560 | ((and set-mark-command-repeat-pop |
4561 | (eq last-command 'pop-global-mark) | |
4562 | (not arg)) | |
66ef2df9 KS |
4563 | (setq this-command 'pop-global-mark) |
4564 | (pop-global-mark)) | |
868c2f49 | 4565 | (arg |
1841f9e3 | 4566 | (setq this-command 'pop-to-mark-command) |
868c2f49 | 4567 | (pop-to-mark-command)) |
2977fc37 SM |
4568 | ((eq last-command 'set-mark-command) |
4569 | (if (region-active-p) | |
4570 | (progn | |
4571 | (deactivate-mark) | |
4572 | (message "Mark deactivated")) | |
4573 | (activate-mark) | |
4574 | (message "Mark activated"))) | |
868c2f49 | 4575 | (t |
5dea55d2 | 4576 | (push-mark-command nil)))) |
2076c87c | 4577 | |
fd0f4056 | 4578 | (defun push-mark (&optional location nomsg activate) |
2076c87c | 4579 | "Set mark at LOCATION (point, by default) and push old mark on mark ring. |
f1382a3d RM |
4580 | If the last global mark pushed was not in the current buffer, |
4581 | also push LOCATION on the global mark ring. | |
fd0f4056 | 4582 | Display `Mark set' unless the optional second arg NOMSG is non-nil. |
2076c87c | 4583 | |
ff1fbe3e | 4584 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
9a1277dd RS |
4585 | purposes. See the documentation of `set-mark' for more information. |
4586 | ||
de9606f0 | 4587 | In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil." |
1a0d0b6a | 4588 | (unless (null (mark t)) |
2076c87c | 4589 | (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring)) |
1a0d0b6a JPW |
4590 | (when (> (length mark-ring) mark-ring-max) |
4591 | (move-marker (car (nthcdr mark-ring-max mark-ring)) nil) | |
4592 | (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))) | |
9a1277dd | 4593 | (set-marker (mark-marker) (or location (point)) (current-buffer)) |
dc029f0b | 4594 | ;; Now push the mark on the global mark ring. |
f1382a3d | 4595 | (if (and global-mark-ring |
e08d3f7c | 4596 | (eq (marker-buffer (car global-mark-ring)) (current-buffer))) |
f1382a3d RM |
4597 | ;; The last global mark pushed was in this same buffer. |
4598 | ;; Don't push another one. | |
4599 | nil | |
4600 | (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring)) | |
1a0d0b6a JPW |
4601 | (when (> (length global-mark-ring) global-mark-ring-max) |
4602 | (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil) | |
4603 | (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))) | |
efcf38c7 | 4604 | (or nomsg executing-kbd-macro (> (minibuffer-depth) 0) |
2076c87c | 4605 | (message "Mark set")) |
8cdc660f RS |
4606 | (if (or activate (not transient-mark-mode)) |
4607 | (set-mark (mark t))) | |
2076c87c JB |
4608 | nil) |
4609 | ||
4610 | (defun pop-mark () | |
4611 | "Pop off mark ring into the buffer's actual mark. | |
4612 | Does not set point. Does nothing if mark ring is empty." | |
1a0d0b6a JPW |
4613 | (when mark-ring |
4614 | (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker))))) | |
4615 | (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer)) | |
1a0d0b6a JPW |
4616 | (move-marker (car mark-ring) nil) |
4617 | (if (null (mark t)) (ding)) | |
0137bae6 JL |
4618 | (setq mark-ring (cdr mark-ring))) |
4619 | (deactivate-mark)) | |
2076c87c | 4620 | |
c613687b SM |
4621 | (define-obsolete-function-alias |
4622 | 'exchange-dot-and-mark 'exchange-point-and-mark "23.3") | |
868c2f49 | 4623 | (defun exchange-point-and-mark (&optional arg) |
af39530e RS |
4624 | "Put the mark where point is now, and point where the mark is now. |
4625 | This command works even when the mark is not active, | |
868c2f49 | 4626 | and it reactivates the mark. |
109cfe4e | 4627 | |
5626c14e | 4628 | If Transient Mark mode is on, a prefix ARG deactivates the mark |
109cfe4e | 4629 | if it is active, and otherwise avoids reactivating it. If |
5626c14e | 4630 | Transient Mark mode is off, a prefix ARG enables Transient Mark |
109cfe4e | 4631 | mode temporarily." |
868c2f49 | 4632 | (interactive "P") |
109cfe4e CY |
4633 | (let ((omark (mark t)) |
4634 | (temp-highlight (eq (car-safe transient-mark-mode) 'only))) | |
2977fc37 SM |
4635 | (if (null omark) |
4636 | (error "No mark set in this buffer")) | |
4637 | (set-mark (point)) | |
4638 | (goto-char omark) | |
109cfe4e CY |
4639 | (cond (temp-highlight |
4640 | (setq transient-mark-mode (cons 'only transient-mark-mode))) | |
4641 | ((or (and arg (region-active-p)) ; (xor arg (not (region-active-p))) | |
4642 | (not (or arg (region-active-p)))) | |
4643 | (deactivate-mark)) | |
4644 | (t (activate-mark))) | |
2977fc37 | 4645 | nil)) |
e23c2c21 | 4646 | |
11ff3b67 | 4647 | (defcustom shift-select-mode t |
84db11d6 SM |
4648 | "When non-nil, shifted motion keys activate the mark momentarily. |
4649 | ||
4650 | While the mark is activated in this way, any shift-translated point | |
4651 | motion key extends the region, and if Transient Mark mode was off, it | |
4652 | is temporarily turned on. Furthermore, the mark will be deactivated | |
4653 | by any subsequent point motion key that was not shift-translated, or | |
4654 | by any action that normally deactivates the mark in Transient Mark mode. | |
4655 | ||
4656 | See `this-command-keys-shift-translated' for the meaning of | |
11ff3b67 AS |
4657 | shift-translation." |
4658 | :type 'boolean | |
4659 | :group 'editing-basics) | |
84db11d6 SM |
4660 | |
4661 | (defun handle-shift-selection () | |
337c561c CY |
4662 | "Activate/deactivate mark depending on invocation thru shift translation. |
4663 | This function is called by `call-interactively' when a command | |
4664 | with a `^' character in its `interactive' spec is invoked, before | |
4665 | running the command itself. | |
4666 | ||
4667 | If `shift-select-mode' is enabled and the command was invoked | |
4668 | through shift translation, set the mark and activate the region | |
4669 | temporarily, unless it was already set in this way. See | |
4670 | `this-command-keys-shift-translated' for the meaning of shift | |
4671 | translation. | |
4672 | ||
4673 | Otherwise, if the region has been activated temporarily, | |
4674 | deactivate it, and restore the variable `transient-mark-mode' to | |
4675 | its earlier value." | |
84db11d6 | 4676 | (cond ((and shift-select-mode this-command-keys-shift-translated) |
9852377f CY |
4677 | (unless (and mark-active |
4678 | (eq (car-safe transient-mark-mode) 'only)) | |
91023c68 | 4679 | (setq transient-mark-mode |
84db11d6 SM |
4680 | (cons 'only |
4681 | (unless (eq transient-mark-mode 'lambda) | |
4682 | transient-mark-mode))) | |
4683 | (push-mark nil nil t))) | |
4684 | ((eq (car-safe transient-mark-mode) 'only) | |
4685 | (setq transient-mark-mode (cdr transient-mark-mode)) | |
4686 | (deactivate-mark)))) | |
109cfe4e | 4687 | |
6710df48 | 4688 | (define-minor-mode transient-mark-mode |
e23c2c21 | 4689 | "Toggle Transient Mark mode. |
06e21633 CY |
4690 | With a prefix argument ARG, enable Transient Mark mode if ARG is |
4691 | positive, and disable it otherwise. If called from Lisp, enable | |
4692 | Transient Mark mode if ARG is omitted or nil. | |
e23c2c21 | 4693 | |
06e21633 CY |
4694 | Transient Mark mode is a global minor mode. When enabled, the |
4695 | region is highlighted whenever the mark is active. The mark is | |
4696 | \"deactivated\" by changing the buffer, and after certain other | |
4697 | operations that set the mark but whose main purpose is something | |
4698 | else--for example, incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]. | |
cfa70244 | 4699 | |
8e843bc4 EZ |
4700 | You can also deactivate the mark by typing \\[keyboard-quit] or |
4701 | \\[keyboard-escape-quit]. | |
1465c66b | 4702 | |
8ecba97d CY |
4703 | Many commands change their behavior when Transient Mark mode is |
4704 | in effect and the mark is active, by acting on the region instead | |
4705 | of their usual default part of the buffer's text. Examples of | |
4706 | such commands include \\[comment-dwim], \\[flush-lines], \\[keep-lines], | |
705a5933 | 4707 | \\[query-replace], \\[query-replace-regexp], \\[ispell], and \\[undo]. |
8ecba97d CY |
4708 | To see the documentation of commands which are sensitive to the |
4709 | Transient Mark mode, invoke \\[apropos-documentation] and type \"transient\" | |
4710 | or \"mark.*active\" at the prompt." | |
43d16385 | 4711 | :global t |
9d794026 GM |
4712 | ;; It's defined in C/cus-start, this stops the d-m-m macro defining it again. |
4713 | :variable transient-mark-mode) | |
109cfe4e | 4714 | |
d0c4882d RS |
4715 | (defvar widen-automatically t |
4716 | "Non-nil means it is ok for commands to call `widen' when they want to. | |
4717 | Some commands will do this in order to go to positions outside | |
4718 | the current accessible part of the buffer. | |
4719 | ||
4720 | If `widen-automatically' is nil, these commands will do something else | |
4721 | as a fallback, and won't change the buffer bounds.") | |
4722 | ||
38111a5a SM |
4723 | (defvar non-essential nil |
4724 | "Whether the currently executing code is performing an essential task. | |
4725 | This variable should be non-nil only when running code which should not | |
4726 | disturb the user. E.g. it can be used to prevent Tramp from prompting the | |
4727 | user for a password when we are simply scanning a set of files in the | |
4728 | background or displaying possible completions before the user even asked | |
4729 | for it.") | |
4730 | ||
dc029f0b RM |
4731 | (defun pop-global-mark () |
4732 | "Pop off global mark ring and jump to the top location." | |
4733 | (interactive) | |
52b6d445 RS |
4734 | ;; Pop entries which refer to non-existent buffers. |
4735 | (while (and global-mark-ring (not (marker-buffer (car global-mark-ring)))) | |
4736 | (setq global-mark-ring (cdr global-mark-ring))) | |
dc029f0b RM |
4737 | (or global-mark-ring |
4738 | (error "No global mark set")) | |
4739 | (let* ((marker (car global-mark-ring)) | |
4740 | (buffer (marker-buffer marker)) | |
4741 | (position (marker-position marker))) | |
34c31301 RS |
4742 | (setq global-mark-ring (nconc (cdr global-mark-ring) |
4743 | (list (car global-mark-ring)))) | |
dc029f0b RM |
4744 | (set-buffer buffer) |
4745 | (or (and (>= position (point-min)) | |
4746 | (<= position (point-max))) | |
d0c4882d | 4747 | (if widen-automatically |
60aee8b2 RS |
4748 | (widen) |
4749 | (error "Global mark position is outside accessible part of buffer"))) | |
dc029f0b RM |
4750 | (goto-char position) |
4751 | (switch-to-buffer buffer))) | |
2d88b556 | 4752 | \f |
95791033 | 4753 | (defcustom next-line-add-newlines nil |
1d2b0303 | 4754 | "If non-nil, `next-line' inserts newline to avoid `end of buffer' error." |
69c1dd37 | 4755 | :type 'boolean |
e1d6e383 | 4756 | :version "21.1" |
69c1dd37 | 4757 | :group 'editing-basics) |
38ebcf29 | 4758 | |
295f6616 | 4759 | (defun next-line (&optional arg try-vscroll) |
2076c87c | 4760 | "Move cursor vertically down ARG lines. |
295f6616 | 4761 | Interactively, vscroll tall lines if `auto-window-vscroll' is enabled. |
c544322e EZ |
4762 | Non-interactively, use TRY-VSCROLL to control whether to vscroll tall |
4763 | lines: if either `auto-window-vscroll' or TRY-VSCROLL is nil, this | |
4764 | function will not vscroll. | |
4765 | ||
18331b90 EZ |
4766 | ARG defaults to 1. |
4767 | ||
2076c87c JB |
4768 | If there is no character in the target line exactly under the current column, |
4769 | the cursor is positioned after the character in that line which spans this | |
4770 | column, or at the end of the line if it is not long enough. | |
38ebcf29 | 4771 | If there is no line in the buffer after this one, behavior depends on the |
1a2c3941 RS |
4772 | value of `next-line-add-newlines'. If non-nil, it inserts a newline character |
4773 | to create a line, and moves the cursor to that line. Otherwise it moves the | |
e47d38f6 | 4774 | cursor to the end of the buffer. |
2076c87c | 4775 | |
53a22af4 CY |
4776 | If the variable `line-move-visual' is non-nil, this command moves |
4777 | by display lines. Otherwise, it moves by buffer lines, without | |
4778 | taking variable-width characters or continued lines into account. | |
4779 | ||
2076c87c | 4780 | The command \\[set-goal-column] can be used to create |
85969cb1 RS |
4781 | a semipermanent goal column for this command. |
4782 | Then instead of trying to move exactly vertically (or as close as possible), | |
4783 | this command moves to the specified goal column (or as close as possible). | |
4784 | The goal column is stored in the variable `goal-column', which is nil | |
064f328a EZ |
4785 | when there is no goal column. Note that setting `goal-column' |
4786 | overrides `line-move-visual' and causes this command to move by buffer | |
4787 | lines rather than by display lines. | |
2076c87c JB |
4788 | |
4789 | If you are thinking of using this in a Lisp program, consider | |
4790 | using `forward-line' instead. It is usually easier to use | |
4791 | and more reliable (no dependence on goal column, etc.)." | |
109cfe4e | 4792 | (interactive "^p\np") |
6b61353c | 4793 | (or arg (setq arg 1)) |
028922cf | 4794 | (if (and next-line-add-newlines (= arg 1)) |
207d7545 GM |
4795 | (if (save-excursion (end-of-line) (eobp)) |
4796 | ;; When adding a newline, don't expand an abbrev. | |
4797 | (let ((abbrev-mode nil)) | |
24886813 | 4798 | (end-of-line) |
15575807 | 4799 | (insert (if use-hard-newlines hard-newline "\n"))) |
295f6616 | 4800 | (line-move arg nil nil try-vscroll)) |
32226619 | 4801 | (if (called-interactively-p 'interactive) |
1cd095c6 | 4802 | (condition-case err |
295f6616 | 4803 | (line-move arg nil nil try-vscroll) |
1cd095c6 JL |
4804 | ((beginning-of-buffer end-of-buffer) |
4805 | (signal (car err) (cdr err)))) | |
295f6616 | 4806 | (line-move arg nil nil try-vscroll))) |
2076c87c | 4807 | nil) |
3e2fb4db | 4808 | (put 'next-line 'interactive-only 'forward-line) |
2076c87c | 4809 | |
295f6616 | 4810 | (defun previous-line (&optional arg try-vscroll) |
2076c87c | 4811 | "Move cursor vertically up ARG lines. |
295f6616 | 4812 | Interactively, vscroll tall lines if `auto-window-vscroll' is enabled. |
c544322e EZ |
4813 | Non-interactively, use TRY-VSCROLL to control whether to vscroll tall |
4814 | lines: if either `auto-window-vscroll' or TRY-VSCROLL is nil, this | |
4815 | function will not vscroll. | |
4816 | ||
18331b90 EZ |
4817 | ARG defaults to 1. |
4818 | ||
2076c87c JB |
4819 | If there is no character in the target line exactly over the current column, |
4820 | the cursor is positioned after the character in that line which spans this | |
4821 | column, or at the end of the line if it is not long enough. | |
4822 | ||
53a22af4 CY |
4823 | If the variable `line-move-visual' is non-nil, this command moves |
4824 | by display lines. Otherwise, it moves by buffer lines, without | |
4825 | taking variable-width characters or continued lines into account. | |
4826 | ||
2076c87c | 4827 | The command \\[set-goal-column] can be used to create |
85969cb1 RS |
4828 | a semipermanent goal column for this command. |
4829 | Then instead of trying to move exactly vertically (or as close as possible), | |
4830 | this command moves to the specified goal column (or as close as possible). | |
4831 | The goal column is stored in the variable `goal-column', which is nil | |
064f328a EZ |
4832 | when there is no goal column. Note that setting `goal-column' |
4833 | overrides `line-move-visual' and causes this command to move by buffer | |
4834 | lines rather than by display lines. | |
2076c87c JB |
4835 | |
4836 | If you are thinking of using this in a Lisp program, consider using | |
c2e8a012 | 4837 | `forward-line' with a negative argument instead. It is usually easier |
2076c87c | 4838 | to use and more reliable (no dependence on goal column, etc.)." |
109cfe4e | 4839 | (interactive "^p\np") |
6b61353c | 4840 | (or arg (setq arg 1)) |
32226619 | 4841 | (if (called-interactively-p 'interactive) |
1cd095c6 | 4842 | (condition-case err |
295f6616 | 4843 | (line-move (- arg) nil nil try-vscroll) |
1cd095c6 JL |
4844 | ((beginning-of-buffer end-of-buffer) |
4845 | (signal (car err) (cdr err)))) | |
295f6616 | 4846 | (line-move (- arg) nil nil try-vscroll)) |
2076c87c | 4847 | nil) |
2bb3a748 | 4848 | (put 'previous-line 'interactive-only |
3e2fb4db | 4849 | "use `forward-line' with negative argument instead.") |
eaae8106 | 4850 | |
69c1dd37 | 4851 | (defcustom track-eol nil |
1d2b0303 | 4852 | "Non-nil means vertical motion starting at end of line keeps to ends of lines. |
2076c87c | 4853 | This means moving to the end of each line moved onto. |
4efebb82 | 4854 | The beginning of a blank line does not count as the end of a line. |
9fc9a531 | 4855 | This has no effect when the variable `line-move-visual' is non-nil." |
69c1dd37 RS |
4856 | :type 'boolean |
4857 | :group 'editing-basics) | |
4858 | ||
4859 | (defcustom goal-column nil | |
064f328a | 4860 | "Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil. |
9fc9a531 | 4861 | A non-nil setting overrides the variable `line-move-visual', which see." |
69c1dd37 RS |
4862 | :type '(choice integer |
4863 | (const :tag "None" nil)) | |
4864 | :group 'editing-basics) | |
912c6728 | 4865 | (make-variable-buffer-local 'goal-column) |
2076c87c JB |
4866 | |
4867 | (defvar temporary-goal-column 0 | |
4868 | "Current goal column for vertical motion. | |
4efebb82 | 4869 | It is the column where point was at the start of the current run |
774409a1 CY |
4870 | of vertical motion commands. |
4871 | ||
9fc9a531 | 4872 | When moving by visual lines via the function `line-move-visual', it is a cons |
774409a1 CY |
4873 | cell (COL . HSCROLL), where COL is the x-position, in pixels, |
4874 | divided by the default column width, and HSCROLL is the number of | |
4875 | columns by which window is scrolled from left margin. | |
4876 | ||
4877 | When the `track-eol' feature is doing its job, the value is | |
4efebb82 | 4878 | `most-positive-fixnum'.") |
2076c87c | 4879 | |
bbf41690 | 4880 | (defcustom line-move-ignore-invisible t |
1d2b0303 | 4881 | "Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines. |
69c1dd37 RS |
4882 | Outline mode sets this." |
4883 | :type 'boolean | |
4884 | :group 'editing-basics) | |
098fc1fb | 4885 | |
a2cf21a2 | 4886 | (defcustom line-move-visual t |
4efebb82 CY |
4887 | "When non-nil, `line-move' moves point by visual lines. |
4888 | This movement is based on where the cursor is displayed on the | |
4889 | screen, instead of relying on buffer contents alone. It takes | |
66e0718b | 4890 | into account variable-width characters and line continuation. |
064f328a EZ |
4891 | If nil, `line-move' moves point by logical lines. |
4892 | A non-nil setting of `goal-column' overrides the value of this variable | |
fe5f08dd | 4893 | and forces movement by logical lines. |
0e23d96a EZ |
4894 | A window that is horizontally scrolled also forces movement by logical |
4895 | lines." | |
a2cf21a2 | 4896 | :type 'boolean |
66e0718b CY |
4897 | :group 'editing-basics |
4898 | :version "23.1") | |
4efebb82 | 4899 | |
e740f9d2 GM |
4900 | ;; Only used if display-graphic-p. |
4901 | (declare-function font-info "font.c" (name &optional frame)) | |
4902 | ||
9aff9b38 EZ |
4903 | (defun default-font-height () |
4904 | "Return the height in pixels of the current buffer's default face font." | |
1a67b811 EZ |
4905 | (let ((default-font (face-font 'default))) |
4906 | (cond | |
4907 | ((and (display-multi-font-p) | |
4908 | ;; Avoid calling font-info if the frame's default font was | |
4909 | ;; not changed since the frame was created. That's because | |
4910 | ;; font-info is expensive for some fonts, see bug #14838. | |
4911 | (not (string= (frame-parameter nil 'font) default-font))) | |
4912 | (aref (font-info default-font) 3)) | |
4913 | (t (frame-char-height))))) | |
9aff9b38 | 4914 | |
35cb8a3e EZ |
4915 | (defun default-line-height () |
4916 | "Return the pixel height of current buffer's default-face text line. | |
4917 | ||
4918 | The value includes `line-spacing', if any, defined for the buffer | |
4919 | or the frame." | |
4920 | (let ((dfh (default-font-height)) | |
4921 | (lsp (if (display-graphic-p) | |
4922 | (or line-spacing | |
4923 | (default-value 'line-spacing) | |
4924 | (frame-parameter nil 'line-spacing) | |
4925 | 0) | |
4926 | 0))) | |
4927 | (if (floatp lsp) | |
4928 | (setq lsp (* dfh lsp))) | |
4929 | (+ dfh lsp))) | |
4930 | ||
9aff9b38 EZ |
4931 | (defun window-screen-lines () |
4932 | "Return the number of screen lines in the text area of the selected window. | |
4933 | ||
4934 | This is different from `window-text-height' in that this function counts | |
4935 | lines in units of the height of the font used by the default face displayed | |
35cb8a3e EZ |
4936 | in the window, not in units of the frame's default font, and also accounts |
4937 | for `line-spacing', if any, defined for the window's buffer or frame. | |
9aff9b38 EZ |
4938 | |
4939 | The value is a floating-point number." | |
4940 | (let ((canonical (window-text-height)) | |
4941 | (fch (frame-char-height)) | |
35cb8a3e EZ |
4942 | (dlh (default-line-height))) |
4943 | (/ (* (float canonical) fch) dlh))) | |
9aff9b38 | 4944 | |
b704b1f0 KS |
4945 | ;; Returns non-nil if partial move was done. |
4946 | (defun line-move-partial (arg noerror to-end) | |
4947 | (if (< arg 0) | |
4948 | ;; Move backward (up). | |
4949 | ;; If already vscrolled, reduce vscroll | |
9aff9b38 | 4950 | (let ((vs (window-vscroll nil t)) |
35cb8a3e EZ |
4951 | (dlh (default-line-height))) |
4952 | (when (> vs dlh) | |
4953 | (set-window-vscroll nil (- vs dlh) t))) | |
b704b1f0 KS |
4954 | |
4955 | ;; Move forward (down). | |
e437f99a | 4956 | (let* ((lh (window-line-height -1)) |
35cb8a3e | 4957 | (rowh (car lh)) |
e437f99a KS |
4958 | (vpos (nth 1 lh)) |
4959 | (ypos (nth 2 lh)) | |
4960 | (rbot (nth 3 lh)) | |
9583ec36 | 4961 | (this-lh (window-line-height)) |
35cb8a3e | 4962 | (this-height (car this-lh)) |
9583ec36 | 4963 | (this-ypos (nth 2 this-lh)) |
35cb8a3e EZ |
4964 | (dlh (default-line-height)) |
4965 | (wslines (window-screen-lines)) | |
5dc8a629 EZ |
4966 | (edges (window-inside-pixel-edges)) |
4967 | (winh (- (nth 3 edges) (nth 1 edges) 1)) | |
35cb8a3e EZ |
4968 | py vs last-line) |
4969 | (if (> (mod wslines 1.0) 0.0) | |
4970 | (setq wslines (round (+ wslines 0.5)))) | |
e437f99a | 4971 | (when (or (null lh) |
ab49d60b EZ |
4972 | (>= rbot dlh) |
4973 | (<= ypos (- dlh)) | |
9583ec36 | 4974 | (null this-lh) |
ab49d60b | 4975 | (<= this-ypos (- dlh))) |
e437f99a | 4976 | (unless lh |
0e7a5039 KS |
4977 | (let ((wend (pos-visible-in-window-p t nil t))) |
4978 | (setq rbot (nth 3 wend) | |
ab49d60b | 4979 | rowh (nth 4 wend) |
0e7a5039 | 4980 | vpos (nth 5 wend)))) |
9583ec36 EZ |
4981 | (unless this-lh |
4982 | (let ((wstart (pos-visible-in-window-p nil nil t))) | |
4983 | (setq this-ypos (nth 2 wstart) | |
4984 | this-height (nth 4 wstart)))) | |
4985 | (setq py | |
4986 | (or (nth 1 this-lh) | |
23de972a EZ |
4987 | (let ((ppos (posn-at-point)) |
4988 | col-row) | |
4989 | (setq col-row (posn-actual-col-row ppos)) | |
4990 | (if col-row | |
4991 | (- (cdr col-row) (window-vscroll)) | |
4992 | (cdr (posn-col-row ppos)))))) | |
35cb8a3e EZ |
4993 | ;; VPOS > 0 means the last line is only partially visible. |
4994 | ;; But if the part that is visible is at least as tall as the | |
4995 | ;; default font, that means the line is actually fully | |
4996 | ;; readable, and something like line-spacing is hidden. So in | |
4997 | ;; that case we accept the last line in the window as still | |
4998 | ;; visible, and consider the margin as starting one line | |
4999 | ;; later. | |
5000 | (if (and vpos (> vpos 0)) | |
5001 | (if (and rowh | |
5002 | (>= rowh (default-font-height)) | |
5003 | (< rowh dlh)) | |
5004 | (setq last-line (min (- wslines scroll-margin) vpos)) | |
5005 | (setq last-line (min (- wslines scroll-margin 1) (1- vpos))))) | |
e437f99a | 5006 | (cond |
9583ec36 EZ |
5007 | ;; If last line of window is fully visible, and vscrolling |
5008 | ;; more would make this line invisible, move forward. | |
ab49d60b | 5009 | ((and (or (< (setq vs (window-vscroll nil t)) dlh) |
9583ec36 | 5010 | (null this-height) |
ab49d60b | 5011 | (<= this-height dlh)) |
9583ec36 | 5012 | (or (null rbot) (= rbot 0))) |
e437f99a | 5013 | nil) |
9583ec36 EZ |
5014 | ;; If cursor is not in the bottom scroll margin, and the |
5015 | ;; current line is is not too tall, move forward. | |
5dc8a629 | 5016 | ((and (or (null this-height) (<= this-height winh)) |
9583ec36 EZ |
5017 | vpos |
5018 | (> vpos 0) | |
35cb8a3e | 5019 | (< py last-line)) |
e437f99a KS |
5020 | nil) |
5021 | ;; When already vscrolled, we vscroll some more if we can, | |
5022 | ;; or clear vscroll and move forward at end of tall image. | |
9583ec36 EZ |
5023 | ((> vs 0) |
5024 | (when (or (and rbot (> rbot 0)) | |
ab49d60b EZ |
5025 | (and this-height (> this-height dlh))) |
5026 | (set-window-vscroll nil (+ vs dlh) t))) | |
e437f99a | 5027 | ;; If cursor just entered the bottom scroll margin, move forward, |
ab49d60b | 5028 | ;; but also optionally vscroll one line so redisplay won't recenter. |
9583ec36 EZ |
5029 | ((and vpos |
5030 | (> vpos 0) | |
35cb8a3e | 5031 | (= py last-line)) |
ab49d60b | 5032 | ;; Don't vscroll if the partially-visible line at window |
5dc8a629 | 5033 | ;; bottom is not too tall (a.k.a. "just one more text |
ab49d60b EZ |
5034 | ;; line"): in that case, we do want redisplay to behave |
5035 | ;; normally, i.e. recenter or whatever. | |
5036 | ;; | |
5037 | ;; Note: ROWH + RBOT from the value returned by | |
5038 | ;; pos-visible-in-window-p give the total height of the | |
5039 | ;; partially-visible glyph row at the end of the window. As | |
5040 | ;; we are dealing with floats, we disregard sub-pixel | |
5041 | ;; discrepancies between that and DLH. | |
5dc8a629 | 5042 | (if (and rowh rbot (>= (- (+ rowh rbot) winh) 1)) |
ab49d60b | 5043 | (set-window-vscroll nil dlh t)) |
e437f99a KS |
5044 | (line-move-1 arg noerror to-end) |
5045 | t) | |
5046 | ;; If there are lines above the last line, scroll-up one line. | |
9583ec36 | 5047 | ((and vpos (> vpos 0)) |
e437f99a KS |
5048 | (scroll-up 1) |
5049 | t) | |
5050 | ;; Finally, start vscroll. | |
5051 | (t | |
ab49d60b | 5052 | (set-window-vscroll nil dlh t))))))) |
b704b1f0 KS |
5053 | |
5054 | ||
03ceda9e RS |
5055 | ;; This is like line-move-1 except that it also performs |
5056 | ;; vertical scrolling of tall images if appropriate. | |
5057 | ;; That is not really a clean thing to do, since it mixes | |
5058 | ;; scrolling with cursor motion. But so far we don't have | |
5059 | ;; a cleaner solution to the problem of making C-n do something | |
5060 | ;; useful given a tall image. | |
ed02c1db | 5061 | (defun line-move (arg &optional noerror to-end try-vscroll) |
bc6494ef CY |
5062 | (if noninteractive |
5063 | (forward-line arg) | |
5064 | (unless (and auto-window-vscroll try-vscroll | |
5065 | ;; Only vscroll for single line moves | |
5066 | (= (abs arg) 1) | |
7cf95797 EZ |
5067 | ;; Under scroll-conservatively, the display engine |
5068 | ;; does this better. | |
5069 | (zerop scroll-conservatively) | |
bc6494ef CY |
5070 | ;; But don't vscroll in a keyboard macro. |
5071 | (not defining-kbd-macro) | |
5072 | (not executing-kbd-macro) | |
5073 | (line-move-partial arg noerror to-end)) | |
5074 | (set-window-vscroll nil 0 t) | |
5075 | (if (and line-move-visual | |
5076 | ;; Display-based column are incompatible with goal-column. | |
5077 | (not goal-column) | |
5078 | ;; When the text in the window is scrolled to the left, | |
5079 | ;; display-based motion doesn't make sense (because each | |
5080 | ;; logical line occupies exactly one screen line). | |
fbfd0e1d EZ |
5081 | (not (> (window-hscroll) 0)) |
5082 | ;; Likewise when the text _was_ scrolled to the left | |
5083 | ;; when the current run of vertical motion commands | |
5084 | ;; started. | |
5085 | (not (and (memq last-command | |
5086 | `(next-line previous-line ,this-command)) | |
5087 | auto-hscroll-mode | |
5088 | (numberp temporary-goal-column) | |
5089 | (>= temporary-goal-column | |
5090 | (- (window-width) hscroll-margin))))) | |
9583ec36 EZ |
5091 | (prog1 (line-move-visual arg noerror) |
5092 | ;; If we moved into a tall line, set vscroll to make | |
5093 | ;; scrolling through tall images more smooth. | |
9aff9b38 | 5094 | (let ((lh (line-pixel-height)) |
5dc8a629 EZ |
5095 | (edges (window-inside-pixel-edges)) |
5096 | (dlh (default-line-height)) | |
5097 | winh) | |
5098 | (setq winh (- (nth 3 edges) (nth 1 edges) 1)) | |
9583ec36 EZ |
5099 | (if (and (< arg 0) |
5100 | (< (point) (window-start)) | |
5dc8a629 | 5101 | (> lh winh)) |
9aff9b38 EZ |
5102 | (set-window-vscroll |
5103 | nil | |
ab49d60b | 5104 | (- lh dlh) t)))) |
bc6494ef | 5105 | (line-move-1 arg noerror to-end))))) |
4efebb82 CY |
5106 | |
5107 | ;; Display-based alternative to line-move-1. | |
5108 | ;; Arg says how many lines to move. The value is t if we can move the | |
5109 | ;; specified number of lines. | |
5110 | (defun line-move-visual (arg &optional noerror) | |
34be836c | 5111 | (let ((opoint (point)) |
774409a1 | 5112 | (hscroll (window-hscroll)) |
34be836c | 5113 | target-hscroll) |
774409a1 CY |
5114 | ;; Check if the previous command was a line-motion command, or if |
5115 | ;; we were called from some other command. | |
34be836c CY |
5116 | (if (and (consp temporary-goal-column) |
5117 | (memq last-command `(next-line previous-line ,this-command))) | |
5118 | ;; If so, there's no need to reset `temporary-goal-column', | |
5119 | ;; but we may need to hscroll. | |
5120 | (if (or (/= (cdr temporary-goal-column) hscroll) | |
5121 | (> (cdr temporary-goal-column) 0)) | |
5122 | (setq target-hscroll (cdr temporary-goal-column))) | |
5123 | ;; Otherwise, we should reset `temporary-goal-column'. | |
5124 | (let ((posn (posn-at-point))) | |
5125 | (cond | |
5126 | ;; Handle the `overflow-newline-into-fringe' case: | |
5127 | ((eq (nth 1 posn) 'right-fringe) | |
5128 | (setq temporary-goal-column (cons (- (window-width) 1) hscroll))) | |
5129 | ((car (posn-x-y posn)) | |
5130 | (setq temporary-goal-column | |
5131 | (cons (/ (float (car (posn-x-y posn))) | |
5132 | (frame-char-width)) hscroll)))))) | |
5133 | (if target-hscroll | |
5134 | (set-window-hscroll (selected-window) target-hscroll)) | |
060ca408 EZ |
5135 | ;; vertical-motion can move more than it was asked to if it moves |
5136 | ;; across display strings with newlines. We don't want to ring | |
5137 | ;; the bell and announce beginning/end of buffer in that case. | |
5138 | (or (and (or (and (>= arg 0) | |
5139 | (>= (vertical-motion | |
5140 | (cons (or goal-column | |
5141 | (if (consp temporary-goal-column) | |
5142 | (car temporary-goal-column) | |
5143 | temporary-goal-column)) | |
5144 | arg)) | |
5145 | arg)) | |
5146 | (and (< arg 0) | |
5147 | (<= (vertical-motion | |
5148 | (cons (or goal-column | |
5149 | (if (consp temporary-goal-column) | |
5150 | (car temporary-goal-column) | |
5151 | temporary-goal-column)) | |
5152 | arg)) | |
5153 | arg))) | |
624a662f CY |
5154 | (or (>= arg 0) |
5155 | (/= (point) opoint) | |
5156 | ;; If the goal column lies on a display string, | |
5157 | ;; `vertical-motion' advances the cursor to the end | |
5158 | ;; of the string. For arg < 0, this can cause the | |
5159 | ;; cursor to get stuck. (Bug#3020). | |
5160 | (= (vertical-motion arg) arg))) | |
5161 | (unless noerror | |
5162 | (signal (if (< arg 0) 'beginning-of-buffer 'end-of-buffer) | |
5163 | nil))))) | |
16c2f92f | 5164 | |
8c745744 RS |
5165 | ;; This is the guts of next-line and previous-line. |
5166 | ;; Arg says how many lines to move. | |
bbf41690 | 5167 | ;; The value is t if we can move the specified number of lines. |
06b60517 | 5168 | (defun line-move-1 (arg &optional noerror _to-end) |
2596511d RS |
5169 | ;; Don't run any point-motion hooks, and disregard intangibility, |
5170 | ;; for intermediate positions. | |
5171 | (let ((inhibit-point-motion-hooks t) | |
5172 | (opoint (point)) | |
fef11f15 | 5173 | (orig-arg arg)) |
774409a1 CY |
5174 | (if (consp temporary-goal-column) |
5175 | (setq temporary-goal-column (+ (car temporary-goal-column) | |
5176 | (cdr temporary-goal-column)))) | |
2596511d RS |
5177 | (unwind-protect |
5178 | (progn | |
41d22ee0 | 5179 | (if (not (memq last-command '(next-line previous-line))) |
2596511d RS |
5180 | (setq temporary-goal-column |
5181 | (if (and track-eol (eolp) | |
5182 | ;; Don't count beg of empty line as end of line | |
5183 | ;; unless we just did explicit end-of-line. | |
ab9623c2 | 5184 | (or (not (bolp)) (eq last-command 'move-end-of-line))) |
3137dda8 | 5185 | most-positive-fixnum |
2596511d | 5186 | (current-column)))) |
bbf41690 | 5187 | |
3137dda8 SM |
5188 | (if (not (or (integerp selective-display) |
5189 | line-move-ignore-invisible)) | |
2596511d | 5190 | ;; Use just newline characters. |
e9cd25fe | 5191 | ;; Set ARG to 0 if we move as many lines as requested. |
2596511d RS |
5192 | (or (if (> arg 0) |
5193 | (progn (if (> arg 1) (forward-line (1- arg))) | |
5194 | ;; This way of moving forward ARG lines | |
5195 | ;; verifies that we have a newline after the last one. | |
5196 | ;; It doesn't get confused by intangible text. | |
5197 | (end-of-line) | |
e9cd25fe RS |
5198 | (if (zerop (forward-line 1)) |
5199 | (setq arg 0))) | |
2596511d | 5200 | (and (zerop (forward-line arg)) |
e9cd25fe RS |
5201 | (bolp) |
5202 | (setq arg 0))) | |
bbf41690 RS |
5203 | (unless noerror |
5204 | (signal (if (< arg 0) | |
5205 | 'beginning-of-buffer | |
5206 | 'end-of-buffer) | |
5207 | nil))) | |
2596511d | 5208 | ;; Move by arg lines, but ignore invisible ones. |
07889873 | 5209 | (let (done) |
bbf41690 RS |
5210 | (while (and (> arg 0) (not done)) |
5211 | ;; If the following character is currently invisible, | |
5212 | ;; skip all characters with that same `invisible' property value. | |
c65e6942 | 5213 | (while (and (not (eobp)) (invisible-p (point))) |
bbf41690 | 5214 | (goto-char (next-char-property-change (point)))) |
fef11f15 CY |
5215 | ;; Move a line. |
5216 | ;; We don't use `end-of-line', since we want to escape | |
40b1a3a9 | 5217 | ;; from field boundaries occurring exactly at point. |
07889873 CY |
5218 | (goto-char (constrain-to-field |
5219 | (let ((inhibit-field-text-motion t)) | |
5220 | (line-end-position)) | |
5221 | (point) t t | |
5222 | 'inhibit-line-move-field-capture)) | |
e9ab825f | 5223 | ;; If there's no invisibility here, move over the newline. |
3e43ae87 KS |
5224 | (cond |
5225 | ((eobp) | |
5226 | (if (not noerror) | |
5227 | (signal 'end-of-buffer nil) | |
5228 | (setq done t))) | |
5229 | ((and (> arg 1) ;; Use vertical-motion for last move | |
5230 | (not (integerp selective-display)) | |
c65e6942 | 5231 | (not (invisible-p (point)))) |
3e43ae87 KS |
5232 | ;; We avoid vertical-motion when possible |
5233 | ;; because that has to fontify. | |
5234 | (forward-line 1)) | |
5235 | ;; Otherwise move a more sophisticated way. | |
5236 | ((zerop (vertical-motion 1)) | |
5237 | (if (not noerror) | |
5238 | (signal 'end-of-buffer nil) | |
5239 | (setq done t)))) | |
bbf41690 RS |
5240 | (unless done |
5241 | (setq arg (1- arg)))) | |
22c8bff1 | 5242 | ;; The logic of this is the same as the loop above, |
e9ab825f | 5243 | ;; it just goes in the other direction. |
bbf41690 | 5244 | (while (and (< arg 0) (not done)) |
ac6701ea CY |
5245 | ;; For completely consistency with the forward-motion |
5246 | ;; case, we should call beginning-of-line here. | |
5247 | ;; However, if point is inside a field and on a | |
5248 | ;; continued line, the call to (vertical-motion -1) | |
5249 | ;; below won't move us back far enough; then we return | |
5250 | ;; to the same column in line-move-finish, and point | |
5251 | ;; gets stuck -- cyd | |
5252 | (forward-line 0) | |
3e43ae87 KS |
5253 | (cond |
5254 | ((bobp) | |
5255 | (if (not noerror) | |
5256 | (signal 'beginning-of-buffer nil) | |
5257 | (setq done t))) | |
5258 | ((and (< arg -1) ;; Use vertical-motion for last move | |
5259 | (not (integerp selective-display)) | |
c65e6942 | 5260 | (not (invisible-p (1- (point))))) |
3e43ae87 KS |
5261 | (forward-line -1)) |
5262 | ((zerop (vertical-motion -1)) | |
5263 | (if (not noerror) | |
5264 | (signal 'beginning-of-buffer nil) | |
5265 | (setq done t)))) | |
bbf41690 RS |
5266 | (unless done |
5267 | (setq arg (1+ arg)) | |
5268 | (while (and ;; Don't move over previous invis lines | |
5269 | ;; if our target is the middle of this line. | |
5270 | (or (zerop (or goal-column temporary-goal-column)) | |
5271 | (< arg 0)) | |
c65e6942 | 5272 | (not (bobp)) (invisible-p (1- (point)))) |
bbf41690 RS |
5273 | (goto-char (previous-char-property-change (point)))))))) |
5274 | ;; This is the value the function returns. | |
5275 | (= arg 0)) | |
af894fc9 | 5276 | |
e9cd25fe | 5277 | (cond ((> arg 0) |
2a1e0c92 CY |
5278 | ;; If we did not move down as far as desired, at least go |
5279 | ;; to end of line. Be sure to call point-entered and | |
5280 | ;; point-left-hooks. | |
5281 | (let* ((npoint (prog1 (line-end-position) | |
5282 | (goto-char opoint))) | |
5283 | (inhibit-point-motion-hooks nil)) | |
5284 | (goto-char npoint))) | |
e9cd25fe | 5285 | ((< arg 0) |
f9872a6b JL |
5286 | ;; If we did not move up as far as desired, |
5287 | ;; at least go to beginning of line. | |
2a1e0c92 CY |
5288 | (let* ((npoint (prog1 (line-beginning-position) |
5289 | (goto-char opoint))) | |
5290 | (inhibit-point-motion-hooks nil)) | |
5291 | (goto-char npoint))) | |
e9cd25fe | 5292 | (t |
20782abb | 5293 | (line-move-finish (or goal-column temporary-goal-column) |
fef11f15 | 5294 | opoint (> orig-arg 0))))))) |
2076c87c | 5295 | |
20782abb | 5296 | (defun line-move-finish (column opoint forward) |
af894fc9 RS |
5297 | (let ((repeat t)) |
5298 | (while repeat | |
5299 | ;; Set REPEAT to t to repeat the whole thing. | |
5300 | (setq repeat nil) | |
5301 | ||
1f980920 | 5302 | (let (new |
963355a4 | 5303 | (old (point)) |
5ed619e0 | 5304 | (line-beg (line-beginning-position)) |
1f980920 RS |
5305 | (line-end |
5306 | ;; Compute the end of the line | |
20782abb | 5307 | ;; ignoring effectively invisible newlines. |
bbf41690 | 5308 | (save-excursion |
a5b4a6a0 RS |
5309 | ;; Like end-of-line but ignores fields. |
5310 | (skip-chars-forward "^\n") | |
c65e6942 | 5311 | (while (and (not (eobp)) (invisible-p (point))) |
20782abb | 5312 | (goto-char (next-char-property-change (point))) |
a5b4a6a0 | 5313 | (skip-chars-forward "^\n")) |
bbf41690 | 5314 | (point)))) |
1f980920 RS |
5315 | |
5316 | ;; Move to the desired column. | |
54b99340 | 5317 | (line-move-to-column (truncate column)) |
963355a4 CY |
5318 | |
5319 | ;; Corner case: suppose we start out in a field boundary in | |
5320 | ;; the middle of a continued line. When we get to | |
5321 | ;; line-move-finish, point is at the start of a new *screen* | |
5322 | ;; line but the same text line; then line-move-to-column would | |
5323 | ;; move us backwards. Test using C-n with point on the "x" in | |
5324 | ;; (insert "a" (propertize "x" 'field t) (make-string 89 ?y)) | |
5325 | (and forward | |
5326 | (< (point) old) | |
5327 | (goto-char old)) | |
5328 | ||
1f980920 | 5329 | (setq new (point)) |
af894fc9 RS |
5330 | |
5331 | ;; Process intangibility within a line. | |
594a1605 CY |
5332 | ;; With inhibit-point-motion-hooks bound to nil, a call to |
5333 | ;; goto-char moves point past intangible text. | |
5334 | ||
5335 | ;; However, inhibit-point-motion-hooks controls both the | |
5336 | ;; intangibility and the point-entered/point-left hooks. The | |
5337 | ;; following hack avoids calling the point-* hooks | |
5338 | ;; unnecessarily. Note that we move *forward* past intangible | |
5339 | ;; text when the initial and final points are the same. | |
d584e29d | 5340 | (goto-char new) |
af894fc9 RS |
5341 | (let ((inhibit-point-motion-hooks nil)) |
5342 | (goto-char new) | |
5343 | ||
5344 | ;; If intangibility moves us to a different (later) place | |
5345 | ;; in the same line, use that as the destination. | |
5346 | (if (<= (point) line-end) | |
1f980920 RS |
5347 | (setq new (point)) |
5348 | ;; If that position is "too late", | |
5349 | ;; try the previous allowable position. | |
5350 | ;; See if it is ok. | |
5351 | (backward-char) | |
20782abb RS |
5352 | (if (if forward |
5353 | ;; If going forward, don't accept the previous | |
5354 | ;; allowable position if it is before the target line. | |
f1e2a033 | 5355 | (< line-beg (point)) |
20782abb RS |
5356 | ;; If going backward, don't accept the previous |
5357 | ;; allowable position if it is still after the target line. | |
5358 | (<= (point) line-end)) | |
1f980920 RS |
5359 | (setq new (point)) |
5360 | ;; As a last resort, use the end of the line. | |
5361 | (setq new line-end)))) | |
af894fc9 RS |
5362 | |
5363 | ;; Now move to the updated destination, processing fields | |
5364 | ;; as well as intangibility. | |
5365 | (goto-char opoint) | |
5366 | (let ((inhibit-point-motion-hooks nil)) | |
5367 | (goto-char | |
e94e78cc CY |
5368 | ;; Ignore field boundaries if the initial and final |
5369 | ;; positions have the same `field' property, even if the | |
5370 | ;; fields are non-contiguous. This seems to be "nicer" | |
5371 | ;; behavior in many situations. | |
5372 | (if (eq (get-char-property new 'field) | |
5373 | (get-char-property opoint 'field)) | |
5374 | new | |
5375 | (constrain-to-field new opoint t t | |
5376 | 'inhibit-line-move-field-capture)))) | |
af894fc9 | 5377 | |
1f980920 | 5378 | ;; If all this moved us to a different line, |
af894fc9 RS |
5379 | ;; retry everything within that new line. |
5380 | (when (or (< (point) line-beg) (> (point) line-end)) | |
5381 | ;; Repeat the intangibility and field processing. | |
5382 | (setq repeat t)))))) | |
5383 | ||
5384 | (defun line-move-to-column (col) | |
5385 | "Try to find column COL, considering invisibility. | |
5386 | This function works only in certain cases, | |
5387 | because what we really need is for `move-to-column' | |
5388 | and `current-column' to be able to ignore invisible text." | |
a615252b RS |
5389 | (if (zerop col) |
5390 | (beginning-of-line) | |
095f9ae4 | 5391 | (move-to-column col)) |
af894fc9 RS |
5392 | |
5393 | (when (and line-move-ignore-invisible | |
c65e6942 | 5394 | (not (bolp)) (invisible-p (1- (point)))) |
af894fc9 RS |
5395 | (let ((normal-location (point)) |
5396 | (normal-column (current-column))) | |
5397 | ;; If the following character is currently invisible, | |
5398 | ;; skip all characters with that same `invisible' property value. | |
5399 | (while (and (not (eobp)) | |
c65e6942 | 5400 | (invisible-p (point))) |
af894fc9 RS |
5401 | (goto-char (next-char-property-change (point)))) |
5402 | ;; Have we advanced to a larger column position? | |
5403 | (if (> (current-column) normal-column) | |
5404 | ;; We have made some progress towards the desired column. | |
5405 | ;; See if we can make any further progress. | |
5406 | (line-move-to-column (+ (current-column) (- col normal-column))) | |
5407 | ;; Otherwise, go to the place we originally found | |
5408 | ;; and move back over invisible text. | |
5409 | ;; that will get us to the same place on the screen | |
5410 | ;; but with a more reasonable buffer position. | |
5411 | (goto-char normal-location) | |
5ed619e0 | 5412 | (let ((line-beg (line-beginning-position))) |
c65e6942 | 5413 | (while (and (not (bolp)) (invisible-p (1- (point)))) |
af894fc9 RS |
5414 | (goto-char (previous-char-property-change (point) line-beg)))))))) |
5415 | ||
bbf41690 | 5416 | (defun move-end-of-line (arg) |
f00239cf | 5417 | "Move point to end of current line as displayed. |
bbf41690 RS |
5418 | With argument ARG not nil or 1, move forward ARG - 1 lines first. |
5419 | If point reaches the beginning or end of buffer, it stops there. | |
fdb77e6f CY |
5420 | |
5421 | To ignore the effects of the `intangible' text or overlay | |
5422 | property, bind `inhibit-point-motion-hooks' to t. | |
5423 | If there is an image in the current line, this function | |
5424 | disregards newlines that are part of the text on which the image | |
5425 | rests." | |
109cfe4e | 5426 | (interactive "^p") |
bbf41690 RS |
5427 | (or arg (setq arg 1)) |
5428 | (let (done) | |
5429 | (while (not done) | |
5430 | (let ((newpos | |
5431 | (save-excursion | |
4efebb82 CY |
5432 | (let ((goal-column 0) |
5433 | (line-move-visual nil)) | |
bbf41690 | 5434 | (and (line-move arg t) |
3f2e7735 EZ |
5435 | ;; With bidi reordering, we may not be at bol, |
5436 | ;; so make sure we are. | |
5437 | (skip-chars-backward "^\n") | |
bbf41690 RS |
5438 | (not (bobp)) |
5439 | (progn | |
c65e6942 | 5440 | (while (and (not (bobp)) (invisible-p (1- (point)))) |
3137dda8 SM |
5441 | (goto-char (previous-single-char-property-change |
5442 | (point) 'invisible))) | |
bbf41690 RS |
5443 | (backward-char 1))) |
5444 | (point))))) | |
5445 | (goto-char newpos) | |
5446 | (if (and (> (point) newpos) | |
5447 | (eq (preceding-char) ?\n)) | |
5448 | (backward-char 1) | |
5449 | (if (and (> (point) newpos) (not (eobp)) | |
5450 | (not (eq (following-char) ?\n))) | |
4efebb82 CY |
5451 | ;; If we skipped something intangible and now we're not |
5452 | ;; really at eol, keep going. | |
bbf41690 RS |
5453 | (setq arg 1) |
5454 | (setq done t))))))) | |
5455 | ||
0cbb497c | 5456 | (defun move-beginning-of-line (arg) |
f00239cf RS |
5457 | "Move point to beginning of current line as displayed. |
5458 | \(If there's an image in the line, this disregards newlines | |
5459 | which are part of the text that the image rests on.) | |
5460 | ||
0cbb497c KS |
5461 | With argument ARG not nil or 1, move forward ARG - 1 lines first. |
5462 | If point reaches the beginning or end of buffer, it stops there. | |
f00239cf | 5463 | To ignore intangibility, bind `inhibit-point-motion-hooks' to t." |
109cfe4e | 5464 | (interactive "^p") |
0cbb497c | 5465 | (or arg (setq arg 1)) |
398c9ffb | 5466 | |
ad47c4a0 | 5467 | (let ((orig (point)) |
3137dda8 | 5468 | first-vis first-vis-field-value) |
1fffd65f RS |
5469 | |
5470 | ;; Move by lines, if ARG is not 1 (the default). | |
5471 | (if (/= arg 1) | |
4efebb82 CY |
5472 | (let ((line-move-visual nil)) |
5473 | (line-move (1- arg) t))) | |
1fffd65f | 5474 | |
99d99081 | 5475 | ;; Move to beginning-of-line, ignoring fields and invisible text. |
1fffd65f | 5476 | (skip-chars-backward "^\n") |
c65e6942 | 5477 | (while (and (not (bobp)) (invisible-p (1- (point)))) |
621a4cc8 | 5478 | (goto-char (previous-char-property-change (point))) |
1fffd65f | 5479 | (skip-chars-backward "^\n")) |
ad47c4a0 RS |
5480 | |
5481 | ;; Now find first visible char in the line | |
c65e6942 | 5482 | (while (and (not (eobp)) (invisible-p (point))) |
ad47c4a0 RS |
5483 | (goto-char (next-char-property-change (point)))) |
5484 | (setq first-vis (point)) | |
5485 | ||
5486 | ;; See if fields would stop us from reaching FIRST-VIS. | |
5487 | (setq first-vis-field-value | |
5488 | (constrain-to-field first-vis orig (/= arg 1) t nil)) | |
5489 | ||
5490 | (goto-char (if (/= first-vis-field-value first-vis) | |
5491 | ;; If yes, obey them. | |
5492 | first-vis-field-value | |
5493 | ;; Otherwise, move to START with attention to fields. | |
5494 | ;; (It is possible that fields never matter in this case.) | |
5495 | (constrain-to-field (point) orig | |
5496 | (/= arg 1) t nil))))) | |
0cbb497c KS |
5497 | |
5498 | ||
85be9ec4 SM |
5499 | ;; Many people have said they rarely use this feature, and often type |
5500 | ;; it by accident. Maybe it shouldn't even be on a key. | |
d5ab2033 | 5501 | (put 'set-goal-column 'disabled t) |
2076c87c JB |
5502 | |
5503 | (defun set-goal-column (arg) | |
5504 | "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line]. | |
5505 | Those commands will move to this position in the line moved to | |
5506 | rather than trying to keep the same horizontal position. | |
5626c14e | 5507 | With a non-nil argument ARG, clears out the goal column |
912c6728 RS |
5508 | so that \\[next-line] and \\[previous-line] resume vertical motion. |
5509 | The goal column is stored in the variable `goal-column'." | |
2076c87c JB |
5510 | (interactive "P") |
5511 | (if arg | |
5512 | (progn | |
5513 | (setq goal-column nil) | |
5514 | (message "No goal column")) | |
5515 | (setq goal-column (current-column)) | |
8a26c165 DG |
5516 | ;; The older method below can be erroneous if `set-goal-column' is bound |
5517 | ;; to a sequence containing % | |
5518 | ;;(message (substitute-command-keys | |
5519 | ;;"Goal column %d (use \\[set-goal-column] with an arg to unset it)") | |
5520 | ;;goal-column) | |
5521 | (message "%s" | |
63219d53 | 5522 | (concat |
8a26c165 DG |
5523 | (format "Goal column %d " goal-column) |
5524 | (substitute-command-keys | |
5525 | "(use \\[set-goal-column] with an arg to unset it)"))) | |
63219d53 | 5526 | |
8a26c165 | 5527 | ) |
2076c87c | 5528 | nil) |
2d88b556 | 5529 | \f |
a2cf21a2 CY |
5530 | ;;; Editing based on visual lines, as opposed to logical lines. |
5531 | ||
5532 | (defun end-of-visual-line (&optional n) | |
5533 | "Move point to end of current visual line. | |
5534 | With argument N not nil or 1, move forward N - 1 visual lines first. | |
5535 | If point reaches the beginning or end of buffer, it stops there. | |
5536 | To ignore intangibility, bind `inhibit-point-motion-hooks' to t." | |
5537 | (interactive "^p") | |
5538 | (or n (setq n 1)) | |
5539 | (if (/= n 1) | |
5540 | (let ((line-move-visual t)) | |
5541 | (line-move (1- n) t))) | |
ef187c24 CY |
5542 | ;; Unlike `move-beginning-of-line', `move-end-of-line' doesn't |
5543 | ;; constrain to field boundaries, so we don't either. | |
a2cf21a2 CY |
5544 | (vertical-motion (cons (window-width) 0))) |
5545 | ||
5546 | (defun beginning-of-visual-line (&optional n) | |
5547 | "Move point to beginning of current visual line. | |
5548 | With argument N not nil or 1, move forward N - 1 visual lines first. | |
5549 | If point reaches the beginning or end of buffer, it stops there. | |
5550 | To ignore intangibility, bind `inhibit-point-motion-hooks' to t." | |
5551 | (interactive "^p") | |
5552 | (or n (setq n 1)) | |
ef187c24 CY |
5553 | (let ((opoint (point))) |
5554 | (if (/= n 1) | |
5555 | (let ((line-move-visual t)) | |
5556 | (line-move (1- n) t))) | |
5557 | (vertical-motion 0) | |
5558 | ;; Constrain to field boundaries, like `move-beginning-of-line'. | |
5559 | (goto-char (constrain-to-field (point) opoint (/= n 1))))) | |
a2cf21a2 CY |
5560 | |
5561 | (defun kill-visual-line (&optional arg) | |
5562 | "Kill the rest of the visual line. | |
ad4c1f62 CY |
5563 | With prefix argument ARG, kill that many visual lines from point. |
5564 | If ARG is negative, kill visual lines backward. | |
5565 | If ARG is zero, kill the text before point on the current visual | |
5566 | line. | |
a2cf21a2 CY |
5567 | |
5568 | If you want to append the killed line to the last killed text, | |
5569 | use \\[append-next-kill] before \\[kill-line]. | |
5570 | ||
5571 | If the buffer is read-only, Emacs will beep and refrain from deleting | |
5572 | the line, but put the line in the kill ring anyway. This means that | |
5573 | you can use this command to copy text from a read-only buffer. | |
5574 | \(If the variable `kill-read-only-ok' is non-nil, then this won't | |
5575 | even beep.)" | |
5576 | (interactive "P") | |
ad4c1f62 CY |
5577 | ;; Like in `kill-line', it's better to move point to the other end |
5578 | ;; of the kill before killing. | |
2066b4fe CY |
5579 | (let ((opoint (point)) |
5580 | (kill-whole-line (and kill-whole-line (bolp)))) | |
a2cf21a2 CY |
5581 | (if arg |
5582 | (vertical-motion (prefix-numeric-value arg)) | |
ad4c1f62 CY |
5583 | (end-of-visual-line 1) |
5584 | (if (= (point) opoint) | |
5585 | (vertical-motion 1) | |
5586 | ;; Skip any trailing whitespace at the end of the visual line. | |
5587 | ;; We used to do this only if `show-trailing-whitespace' is | |
5588 | ;; nil, but that's wrong; the correct thing would be to check | |
5589 | ;; whether the trailing whitespace is highlighted. But, it's | |
5590 | ;; OK to just do this unconditionally. | |
5591 | (skip-chars-forward " \t"))) | |
2066b4fe CY |
5592 | (kill-region opoint (if (and kill-whole-line (looking-at "\n")) |
5593 | (1+ (point)) | |
5594 | (point))))) | |
a2cf21a2 CY |
5595 | |
5596 | (defun next-logical-line (&optional arg try-vscroll) | |
5597 | "Move cursor vertically down ARG lines. | |
1d2b0303 | 5598 | This is identical to `next-line', except that it always moves |
a2cf21a2 CY |
5599 | by logical lines instead of visual lines, ignoring the value of |
5600 | the variable `line-move-visual'." | |
5601 | (interactive "^p\np") | |
5602 | (let ((line-move-visual nil)) | |
5603 | (with-no-warnings | |
5604 | (next-line arg try-vscroll)))) | |
5605 | ||
5606 | (defun previous-logical-line (&optional arg try-vscroll) | |
5607 | "Move cursor vertically up ARG lines. | |
5608 | This is identical to `previous-line', except that it always moves | |
5609 | by logical lines instead of visual lines, ignoring the value of | |
5610 | the variable `line-move-visual'." | |
5611 | (interactive "^p\np") | |
5612 | (let ((line-move-visual nil)) | |
5613 | (with-no-warnings | |
5614 | (previous-line arg try-vscroll)))) | |
5615 | ||
4dec5cff CY |
5616 | (defgroup visual-line nil |
5617 | "Editing based on visual lines." | |
5618 | :group 'convenience | |
5619 | :version "23.1") | |
5620 | ||
a2cf21a2 CY |
5621 | (defvar visual-line-mode-map |
5622 | (let ((map (make-sparse-keymap))) | |
5623 | (define-key map [remap kill-line] 'kill-visual-line) | |
5624 | (define-key map [remap move-beginning-of-line] 'beginning-of-visual-line) | |
5625 | (define-key map [remap move-end-of-line] 'end-of-visual-line) | |
b316b2b8 CY |
5626 | ;; These keybindings interfere with xterm function keys. Are |
5627 | ;; there any other suitable bindings? | |
5628 | ;; (define-key map "\M-[" 'previous-logical-line) | |
5629 | ;; (define-key map "\M-]" 'next-logical-line) | |
a2cf21a2 CY |
5630 | map)) |
5631 | ||
4dec5cff CY |
5632 | (defcustom visual-line-fringe-indicators '(nil nil) |
5633 | "How fringe indicators are shown for wrapped lines in `visual-line-mode'. | |
5634 | The value should be a list of the form (LEFT RIGHT), where LEFT | |
5635 | and RIGHT are symbols representing the bitmaps to display, to | |
5636 | indicate wrapped lines, in the left and right fringes respectively. | |
5637 | See also `fringe-indicator-alist'. | |
5638 | The default is not to display fringe indicators for wrapped lines. | |
5639 | This variable does not affect fringe indicators displayed for | |
5640 | other purposes." | |
5641 | :type '(list (choice (const :tag "Hide left indicator" nil) | |
5642 | (const :tag "Left curly arrow" left-curly-arrow) | |
5643 | (symbol :tag "Other bitmap")) | |
5644 | (choice (const :tag "Hide right indicator" nil) | |
5645 | (const :tag "Right curly arrow" right-curly-arrow) | |
5646 | (symbol :tag "Other bitmap"))) | |
5647 | :set (lambda (symbol value) | |
5648 | (dolist (buf (buffer-list)) | |
5649 | (with-current-buffer buf | |
5650 | (when (and (boundp 'visual-line-mode) | |
5651 | (symbol-value 'visual-line-mode)) | |
5652 | (setq fringe-indicator-alist | |
5653 | (cons (cons 'continuation value) | |
5654 | (assq-delete-all | |
5655 | 'continuation | |
5656 | (copy-tree fringe-indicator-alist))))))) | |
5657 | (set-default symbol value))) | |
5658 | ||
748e001a CY |
5659 | (defvar visual-line--saved-state nil) |
5660 | ||
a2cf21a2 | 5661 | (define-minor-mode visual-line-mode |
06e21633 CY |
5662 | "Toggle visual line based editing (Visual Line mode). |
5663 | With a prefix argument ARG, enable Visual Line mode if ARG is | |
5664 | positive, and disable it otherwise. If called from Lisp, enable | |
5665 | the mode if ARG is omitted or nil. | |
5666 | ||
5667 | When Visual Line mode is enabled, `word-wrap' is turned on in | |
5668 | this buffer, and simple editing commands are redefined to act on | |
5669 | visual lines, not logical lines. See Info node `Visual Line | |
5670 | Mode' for details." | |
a2cf21a2 | 5671 | :keymap visual-line-mode-map |
4dec5cff | 5672 | :group 'visual-line |
ea92f9f3 | 5673 | :lighter " Wrap" |
a2cf21a2 CY |
5674 | (if visual-line-mode |
5675 | (progn | |
748e001a CY |
5676 | (set (make-local-variable 'visual-line--saved-state) nil) |
5677 | ;; Save the local values of some variables, to be restored if | |
5678 | ;; visual-line-mode is turned off. | |
5679 | (dolist (var '(line-move-visual truncate-lines | |
5680 | truncate-partial-width-windows | |
5681 | word-wrap fringe-indicator-alist)) | |
5682 | (if (local-variable-p var) | |
37820ea9 | 5683 | (push (cons var (symbol-value var)) |
748e001a | 5684 | visual-line--saved-state))) |
a2cf21a2 | 5685 | (set (make-local-variable 'line-move-visual) t) |
c58140f4 CY |
5686 | (set (make-local-variable 'truncate-partial-width-windows) nil) |
5687 | (setq truncate-lines nil | |
5688 | word-wrap t | |
5689 | fringe-indicator-alist | |
4dec5cff CY |
5690 | (cons (cons 'continuation visual-line-fringe-indicators) |
5691 | fringe-indicator-alist))) | |
a2cf21a2 | 5692 | (kill-local-variable 'line-move-visual) |
4dec5cff | 5693 | (kill-local-variable 'word-wrap) |
c58140f4 CY |
5694 | (kill-local-variable 'truncate-lines) |
5695 | (kill-local-variable 'truncate-partial-width-windows) | |
748e001a CY |
5696 | (kill-local-variable 'fringe-indicator-alist) |
5697 | (dolist (saved visual-line--saved-state) | |
5698 | (set (make-local-variable (car saved)) (cdr saved))) | |
5699 | (kill-local-variable 'visual-line--saved-state))) | |
a2cf21a2 CY |
5700 | |
5701 | (defun turn-on-visual-line-mode () | |
5702 | (visual-line-mode 1)) | |
5703 | ||
5704 | (define-globalized-minor-mode global-visual-line-mode | |
15ac32d5 | 5705 | visual-line-mode turn-on-visual-line-mode) |
5a97d2da | 5706 | |
2d88b556 | 5707 | \f |
2076c87c JB |
5708 | (defun transpose-chars (arg) |
5709 | "Interchange characters around point, moving forward one character. | |
5710 | With prefix arg ARG, effect is to take character before point | |
5711 | and drag it forward past ARG other characters (backward if ARG negative). | |
5712 | If no argument and at end of line, the previous two chars are exchanged." | |
5713 | (interactive "*P") | |
5714 | (and (null arg) (eolp) (forward-char -1)) | |
5715 | (transpose-subr 'forward-char (prefix-numeric-value arg))) | |
5716 | ||
5717 | (defun transpose-words (arg) | |
5718 | "Interchange words around point, leaving point at end of them. | |
5719 | With prefix arg ARG, effect is to take word before or around point | |
5720 | and drag it forward past ARG other words (backward if ARG negative). | |
5721 | If ARG is zero, the words around or after point and around or after mark | |
5722 | are interchanged." | |
41d22ee0 | 5723 | ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'. |
2076c87c JB |
5724 | (interactive "*p") |
5725 | (transpose-subr 'forward-word arg)) | |
5726 | ||
5727 | (defun transpose-sexps (arg) | |
5728 | "Like \\[transpose-words] but applies to sexps. | |
5729 | Does not work on a sexp that point is in the middle of | |
5730 | if it is a list or string." | |
5731 | (interactive "*p") | |
41d22ee0 SM |
5732 | (transpose-subr |
5733 | (lambda (arg) | |
5734 | ;; Here we should try to simulate the behavior of | |
5735 | ;; (cons (progn (forward-sexp x) (point)) | |
5736 | ;; (progn (forward-sexp (- x)) (point))) | |
5737 | ;; Except that we don't want to rely on the second forward-sexp | |
5738 | ;; putting us back to where we want to be, since forward-sexp-function | |
5739 | ;; might do funny things like infix-precedence. | |
5740 | (if (if (> arg 0) | |
5741 | (looking-at "\\sw\\|\\s_") | |
5742 | (and (not (bobp)) | |
5743 | (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_")))) | |
5744 | ;; Jumping over a symbol. We might be inside it, mind you. | |
5745 | (progn (funcall (if (> arg 0) | |
5746 | 'skip-syntax-backward 'skip-syntax-forward) | |
5747 | "w_") | |
5748 | (cons (save-excursion (forward-sexp arg) (point)) (point))) | |
5749 | ;; Otherwise, we're between sexps. Take a step back before jumping | |
5750 | ;; to make sure we'll obey the same precedence no matter which direction | |
5751 | ;; we're going. | |
5752 | (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .") | |
5753 | (cons (save-excursion (forward-sexp arg) (point)) | |
5754 | (progn (while (or (forward-comment (if (> arg 0) 1 -1)) | |
5755 | (not (zerop (funcall (if (> arg 0) | |
5756 | 'skip-syntax-forward | |
5757 | 'skip-syntax-backward) | |
5758 | "."))))) | |
5759 | (point))))) | |
5760 | arg 'special)) | |
2076c87c JB |
5761 | |
5762 | (defun transpose-lines (arg) | |
5763 | "Exchange current line and previous line, leaving point after both. | |
5764 | With argument ARG, takes previous line and moves it past ARG lines. | |
5765 | With argument 0, interchanges line point is in with line mark is in." | |
5766 | (interactive "*p") | |
5767 | (transpose-subr (function | |
5768 | (lambda (arg) | |
d3f4ef3f | 5769 | (if (> arg 0) |
2076c87c | 5770 | (progn |
d3f4ef3f AS |
5771 | ;; Move forward over ARG lines, |
5772 | ;; but create newlines if necessary. | |
5773 | (setq arg (forward-line arg)) | |
5774 | (if (/= (preceding-char) ?\n) | |
5775 | (setq arg (1+ arg))) | |
5776 | (if (> arg 0) | |
5777 | (newline arg))) | |
2076c87c JB |
5778 | (forward-line arg)))) |
5779 | arg)) | |
5780 | ||
36020642 GM |
5781 | ;; FIXME seems to leave point BEFORE the current object when ARG = 0, |
5782 | ;; which seems inconsistent with the ARG /= 0 case. | |
5783 | ;; FIXME document SPECIAL. | |
e1e04350 | 5784 | (defun transpose-subr (mover arg &optional special) |
36020642 GM |
5785 | "Subroutine to do the work of transposing objects. |
5786 | Works for lines, sentences, paragraphs, etc. MOVER is a function that | |
5787 | moves forward by units of the given object (e.g. forward-sentence, | |
5788 | forward-paragraph). If ARG is zero, exchanges the current object | |
5789 | with the one containing mark. If ARG is an integer, moves the | |
5790 | current object past ARG following (if ARG is positive) or | |
5791 | preceding (if ARG is negative) objects, leaving point after the | |
5792 | current object." | |
e1e04350 SM |
5793 | (let ((aux (if special mover |
5794 | (lambda (x) | |
5795 | (cons (progn (funcall mover x) (point)) | |
5796 | (progn (funcall mover (- x)) (point)))))) | |
5797 | pos1 pos2) | |
5798 | (cond | |
5799 | ((= arg 0) | |
5800 | (save-excursion | |
5801 | (setq pos1 (funcall aux 1)) | |
41849bf9 | 5802 | (goto-char (or (mark) (error "No mark set in this buffer"))) |
e1e04350 SM |
5803 | (setq pos2 (funcall aux 1)) |
5804 | (transpose-subr-1 pos1 pos2)) | |
5805 | (exchange-point-and-mark)) | |
5806 | ((> arg 0) | |
5807 | (setq pos1 (funcall aux -1)) | |
5808 | (setq pos2 (funcall aux arg)) | |
5809 | (transpose-subr-1 pos1 pos2) | |
5810 | (goto-char (car pos2))) | |
5811 | (t | |
5812 | (setq pos1 (funcall aux -1)) | |
5813 | (goto-char (car pos1)) | |
5814 | (setq pos2 (funcall aux arg)) | |
5815 | (transpose-subr-1 pos1 pos2))))) | |
5816 | ||
5817 | (defun transpose-subr-1 (pos1 pos2) | |
5818 | (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1)))) | |
5819 | (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2)))) | |
5820 | (when (> (car pos1) (car pos2)) | |
5821 | (let ((swap pos1)) | |
5822 | (setq pos1 pos2 pos2 swap))) | |
5823 | (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose")) | |
dc7d7552 | 5824 | (atomic-change-group |
aa26f345 CY |
5825 | ;; This sequence of insertions attempts to preserve marker |
5826 | ;; positions at the start and end of the transposed objects. | |
5827 | (let* ((word (buffer-substring (car pos2) (cdr pos2))) | |
5828 | (len1 (- (cdr pos1) (car pos1))) | |
5829 | (len2 (length word)) | |
5830 | (boundary (make-marker))) | |
5831 | (set-marker boundary (car pos2)) | |
5832 | (goto-char (cdr pos1)) | |
5833 | (insert-before-markers word) | |
5834 | (setq word (delete-and-extract-region (car pos1) (+ (car pos1) len1))) | |
5835 | (goto-char boundary) | |
5836 | (insert word) | |
5837 | (goto-char (+ boundary len1)) | |
5838 | (delete-region (point) (+ (point) len2)) | |
5839 | (set-marker boundary nil)))) | |
2d88b556 | 5840 | \f |
6b61353c | 5841 | (defun backward-word (&optional arg) |
b7e91b0c | 5842 | "Move backward until encountering the beginning of a word. |
f73f4ce6 XF |
5843 | With argument ARG, do this that many times. |
5844 | If ARG is omitted or nil, move point backward one word." | |
109cfe4e | 5845 | (interactive "^p") |
6b61353c | 5846 | (forward-word (- (or arg 1)))) |
2076c87c | 5847 | |
a1a801de | 5848 | (defun mark-word (&optional arg allow-extend) |
705a5933 JL |
5849 | "Set mark ARG words away from point. |
5850 | The place mark goes is the same place \\[forward-word] would | |
5851 | move to with the same argument. | |
a1a801de | 5852 | Interactively, if this command is repeated |
771069f8 | 5853 | or (in Transient Mark mode) if the mark is active, |
705a5933 | 5854 | it marks the next ARG words after the ones already marked." |
a1a801de RS |
5855 | (interactive "P\np") |
5856 | (cond ((and allow-extend | |
5857 | (or (and (eq last-command this-command) (mark t)) | |
d34c311a | 5858 | (region-active-p))) |
705a5933 JL |
5859 | (setq arg (if arg (prefix-numeric-value arg) |
5860 | (if (< (mark) (point)) -1 1))) | |
cad113ae KG |
5861 | (set-mark |
5862 | (save-excursion | |
5863 | (goto-char (mark)) | |
5864 | (forward-word arg) | |
5865 | (point)))) | |
5866 | (t | |
5867 | (push-mark | |
5868 | (save-excursion | |
705a5933 | 5869 | (forward-word (prefix-numeric-value arg)) |
cad113ae KG |
5870 | (point)) |
5871 | nil t)))) | |
2076c87c JB |
5872 | |
5873 | (defun kill-word (arg) | |
5874 | "Kill characters forward until encountering the end of a word. | |
5626c14e | 5875 | With argument ARG, do this that many times." |
e761e42c | 5876 | (interactive "p") |
89ee2bf6 | 5877 | (kill-region (point) (progn (forward-word arg) (point)))) |
2076c87c JB |
5878 | |
5879 | (defun backward-kill-word (arg) | |
654ec269 | 5880 | "Kill characters backward until encountering the beginning of a word. |
5626c14e | 5881 | With argument ARG, do this that many times." |
e761e42c | 5882 | (interactive "p") |
2076c87c | 5883 | (kill-word (- arg))) |
d7c64071 | 5884 | |
0f7df535 RS |
5885 | (defun current-word (&optional strict really-word) |
5886 | "Return the symbol or word that point is on (or a nearby one) as a string. | |
5887 | The return value includes no text properties. | |
1e8c5ac4 | 5888 | If optional arg STRICT is non-nil, return nil unless point is within |
0fa19a57 RS |
5889 | or adjacent to a symbol or word. In all cases the value can be nil |
5890 | if there is no word nearby. | |
0f7df535 RS |
5891 | The function, belying its name, normally finds a symbol. |
5892 | If optional arg REALLY-WORD is non-nil, it finds just a word." | |
d7c64071 | 5893 | (save-excursion |
0f7df535 | 5894 | (let* ((oldpoint (point)) (start (point)) (end (point)) |
81d17173 | 5895 | (syntaxes (if really-word "w" "w_")) |
0f7df535 RS |
5896 | (not-syntaxes (concat "^" syntaxes))) |
5897 | (skip-syntax-backward syntaxes) (setq start (point)) | |
d7c64071 | 5898 | (goto-char oldpoint) |
0f7df535 RS |
5899 | (skip-syntax-forward syntaxes) (setq end (point)) |
5900 | (when (and (eq start oldpoint) (eq end oldpoint) | |
5901 | ;; Point is neither within nor adjacent to a word. | |
5902 | (not strict)) | |
5903 | ;; Look for preceding word in same line. | |
9b026d9f | 5904 | (skip-syntax-backward not-syntaxes (line-beginning-position)) |
0f7df535 RS |
5905 | (if (bolp) |
5906 | ;; No preceding word in same line. | |
5907 | ;; Look for following word in same line. | |
5908 | (progn | |
9b026d9f | 5909 | (skip-syntax-forward not-syntaxes (line-end-position)) |
0f7df535 RS |
5910 | (setq start (point)) |
5911 | (skip-syntax-forward syntaxes) | |
5912 | (setq end (point))) | |
5913 | (setq end (point)) | |
5914 | (skip-syntax-backward syntaxes) | |
5915 | (setq start (point)))) | |
5916 | ;; If we found something nonempty, return it as a string. | |
5917 | (unless (= start end) | |
020db25f | 5918 | (buffer-substring-no-properties start end))))) |
2d88b556 | 5919 | \f |
69c1dd37 | 5920 | (defcustom fill-prefix nil |
1d2b0303 | 5921 | "String for filling to insert at front of new line, or nil for none." |
69c1dd37 RS |
5922 | :type '(choice (const :tag "None" nil) |
5923 | string) | |
5924 | :group 'fill) | |
2076c87c | 5925 | (make-variable-buffer-local 'fill-prefix) |
f31b1257 | 5926 | (put 'fill-prefix 'safe-local-variable 'string-or-null-p) |
2076c87c | 5927 | |
69c1dd37 | 5928 | (defcustom auto-fill-inhibit-regexp nil |
1d2b0303 | 5929 | "Regexp to match lines which should not be auto-filled." |
69c1dd37 RS |
5930 | :type '(choice (const :tag "None" nil) |
5931 | regexp) | |
5932 | :group 'fill) | |
2076c87c JB |
5933 | |
5934 | (defun do-auto-fill () | |
ce558208 GM |
5935 | "The default value for `normal-auto-fill-function'. |
5936 | This is the default auto-fill function, some major modes use a different one. | |
5937 | Returns t if it really did any work." | |
621a3f62 | 5938 | (let (fc justify give-up |
a0170800 | 5939 | (fill-prefix fill-prefix)) |
c18465c4 | 5940 | (if (or (not (setq justify (current-justification))) |
8f066a20 RS |
5941 | (null (setq fc (current-fill-column))) |
5942 | (and (eq justify 'left) | |
5943 | (<= (current-column) fc)) | |
621a3f62 SM |
5944 | (and auto-fill-inhibit-regexp |
5945 | (save-excursion (beginning-of-line) | |
eed5698b RS |
5946 | (looking-at auto-fill-inhibit-regexp)))) |
5947 | nil ;; Auto-filling not required | |
3db1e3b5 BG |
5948 | (if (memq justify '(full center right)) |
5949 | (save-excursion (unjustify-current-line))) | |
a0170800 RS |
5950 | |
5951 | ;; Choose a fill-prefix automatically. | |
e1e04350 SM |
5952 | (when (and adaptive-fill-mode |
5953 | (or (null fill-prefix) (string= fill-prefix ""))) | |
5954 | (let ((prefix | |
5955 | (fill-context-prefix | |
fb5b2591 SM |
5956 | (save-excursion (fill-forward-paragraph -1) (point)) |
5957 | (save-excursion (fill-forward-paragraph 1) (point))))) | |
e1e04350 SM |
5958 | (and prefix (not (equal prefix "")) |
5959 | ;; Use auto-indentation rather than a guessed empty prefix. | |
0e53a373 | 5960 | (not (and fill-indent-according-to-mode |
d99f8496 | 5961 | (string-match "\\`[ \t]*\\'" prefix))) |
e1e04350 | 5962 | (setq fill-prefix prefix)))) |
f1180544 | 5963 | |
eed5698b | 5964 | (while (and (not give-up) (> (current-column) fc)) |
e47d38f6 | 5965 | ;; Determine where to split the line. |
db893d00 RS |
5966 | (let* (after-prefix |
5967 | (fill-point | |
621a3f62 SM |
5968 | (save-excursion |
5969 | (beginning-of-line) | |
5970 | (setq after-prefix (point)) | |
5971 | (and fill-prefix | |
5972 | (looking-at (regexp-quote fill-prefix)) | |
5973 | (setq after-prefix (match-end 0))) | |
5974 | (move-to-column (1+ fc)) | |
5975 | (fill-move-to-break-point after-prefix) | |
5976 | (point)))) | |
db893d00 RS |
5977 | |
5978 | ;; See whether the place we found is any good. | |
e47d38f6 RS |
5979 | (if (save-excursion |
5980 | (goto-char fill-point) | |
41d22ee0 SM |
5981 | (or (bolp) |
5982 | ;; There is no use breaking at end of line. | |
5983 | (save-excursion (skip-chars-forward " ") (eolp)) | |
5984 | ;; It is futile to split at the end of the prefix | |
5985 | ;; since we would just insert the prefix again. | |
5986 | (and after-prefix (<= (point) after-prefix)) | |
5987 | ;; Don't split right after a comment starter | |
5988 | ;; since we would just make another comment starter. | |
5989 | (and comment-start-skip | |
5990 | (let ((limit (point))) | |
5991 | (beginning-of-line) | |
5992 | (and (re-search-forward comment-start-skip | |
5993 | limit t) | |
5994 | (eq (point) limit)))))) | |
5995 | ;; No good place to break => stop trying. | |
5996 | (setq give-up t) | |
5997 | ;; Ok, we have a useful place to break the line. Do it. | |
5998 | (let ((prev-column (current-column))) | |
5999 | ;; If point is at the fill-point, do not `save-excursion'. | |
6000 | ;; Otherwise, if a comment prefix or fill-prefix is inserted, | |
6001 | ;; point will end up before it rather than after it. | |
6002 | (if (save-excursion | |
6003 | (skip-chars-backward " \t") | |
6004 | (= (point) fill-point)) | |
0b727f9d | 6005 | (default-indent-new-line t) |
41d22ee0 SM |
6006 | (save-excursion |
6007 | (goto-char fill-point) | |
0b727f9d | 6008 | (default-indent-new-line t))) |
41d22ee0 SM |
6009 | ;; Now do justification, if required |
6010 | (if (not (eq justify 'left)) | |
e47d38f6 | 6011 | (save-excursion |
e1e04350 SM |
6012 | (end-of-line 0) |
6013 | (justify-current-line justify nil t))) | |
41d22ee0 SM |
6014 | ;; If making the new line didn't reduce the hpos of |
6015 | ;; the end of the line, then give up now; | |
6016 | ;; trying again will not help. | |
6017 | (if (>= (current-column) prev-column) | |
6018 | (setq give-up t)))))) | |
24ebf92e | 6019 | ;; Justify last line. |
e2504204 | 6020 | (justify-current-line justify t t) |
1e722f9f | 6021 | t))) |
2076c87c | 6022 | |
0b727f9d | 6023 | (defvar comment-line-break-function 'comment-indent-new-line |
fb7ada5f | 6024 | "Mode-specific function which line breaks and continues a comment. |
0b727f9d RS |
6025 | This function is called during auto-filling when a comment syntax |
6026 | is defined. | |
6027 | The function should take a single optional argument, which is a flag | |
6028 | indicating whether it should use soft newlines.") | |
6029 | ||
6030 | (defun default-indent-new-line (&optional soft) | |
6031 | "Break line at point and indent. | |
6032 | If a comment syntax is defined, call `comment-indent-new-line'. | |
6033 | ||
6034 | The inserted newline is marked hard if variable `use-hard-newlines' is true, | |
6035 | unless optional argument SOFT is non-nil." | |
6036 | (interactive) | |
6037 | (if comment-start | |
6038 | (funcall comment-line-break-function soft) | |
6039 | ;; Insert the newline before removing empty space so that markers | |
6040 | ;; get preserved better. | |
6041 | (if soft (insert-and-inherit ?\n) (newline 1)) | |
6042 | (save-excursion (forward-char -1) (delete-horizontal-space)) | |
6043 | (delete-horizontal-space) | |
6044 | ||
6045 | (if (and fill-prefix (not adaptive-fill-mode)) | |
6046 | ;; Blindly trust a non-adaptive fill-prefix. | |
6047 | (progn | |
6048 | (indent-to-left-margin) | |
6049 | (insert-before-markers-and-inherit fill-prefix)) | |
6050 | ||
6051 | (cond | |
6052 | ;; If there's an adaptive prefix, use it unless we're inside | |
6053 | ;; a comment and the prefix is not a comment starter. | |
6054 | (fill-prefix | |
6055 | (indent-to-left-margin) | |
6056 | (insert-and-inherit fill-prefix)) | |
6057 | ;; If we're not inside a comment, just try to indent. | |
6058 | (t (indent-according-to-mode)))))) | |
6059 | ||
24ebf92e RS |
6060 | (defvar normal-auto-fill-function 'do-auto-fill |
6061 | "The function to use for `auto-fill-function' if Auto Fill mode is turned on. | |
6062 | Some major modes set this.") | |
6063 | ||
c75505b4 | 6064 | (put 'auto-fill-function :minor-mode-function 'auto-fill-mode) |
74ab01ff SM |
6065 | ;; `functions' and `hooks' are usually unsafe to set, but setting |
6066 | ;; auto-fill-function to nil in a file-local setting is safe and | |
6067 | ;; can be useful to prevent auto-filling. | |
6068 | (put 'auto-fill-function 'safe-local-variable 'null) | |
ca425c7c | 6069 | |
80ac5d4d | 6070 | (define-minor-mode auto-fill-mode |
06e21633 CY |
6071 | "Toggle automatic line breaking (Auto Fill mode). |
6072 | With a prefix argument ARG, enable Auto Fill mode if ARG is | |
6073 | positive, and disable it otherwise. If called from Lisp, enable | |
6074 | the mode if ARG is omitted or nil. | |
6075 | ||
6076 | When Auto Fill mode is enabled, inserting a space at a column | |
6077 | beyond `current-fill-column' automatically breaks the line at a | |
6078 | previous space. | |
24ebf92e | 6079 | |
ca425c7c LMI |
6080 | When `auto-fill-mode' is on, the `auto-fill-function' variable is |
6081 | non-`nil'. | |
6082 | ||
24ebf92e RS |
6083 | The value of `normal-auto-fill-function' specifies the function to use |
6084 | for `auto-fill-function' when turning Auto Fill mode on." | |
2ee3d7f0 SM |
6085 | :variable (auto-fill-function |
6086 | . (lambda (v) (setq auto-fill-function | |
6087 | (if v normal-auto-fill-function))))) | |
d7465b15 RS |
6088 | |
6089 | ;; This holds a document string used to document auto-fill-mode. | |
6090 | (defun auto-fill-function () | |
6091 | "Automatically break line at a previous space, in insertion of text." | |
6092 | nil) | |
6093 | ||
6094 | (defun turn-on-auto-fill () | |
6095 | "Unconditionally turn on Auto Fill mode." | |
6096 | (auto-fill-mode 1)) | |
3a99c819 GM |
6097 | |
6098 | (defun turn-off-auto-fill () | |
6099 | "Unconditionally turn off Auto Fill mode." | |
6100 | (auto-fill-mode -1)) | |
6101 | ||
7cbf1dc1 | 6102 | (custom-add-option 'text-mode-hook 'turn-on-auto-fill) |
d7465b15 RS |
6103 | |
6104 | (defun set-fill-column (arg) | |
4cc0ea11 | 6105 | "Set `fill-column' to specified argument. |
923efb99 | 6106 | Use \\[universal-argument] followed by a number to specify a column. |
4cc0ea11 | 6107 | Just \\[universal-argument] as argument means to use the current column." |
7c373357 SM |
6108 | (interactive |
6109 | (list (or current-prefix-arg | |
6110 | ;; We used to use current-column silently, but C-x f is too easily | |
6111 | ;; typed as a typo for C-x C-f, so we turned it into an error and | |
6112 | ;; now an interactive prompt. | |
6113 | (read-number "Set fill-column to: " (current-column))))) | |
f4520363 RS |
6114 | (if (consp arg) |
6115 | (setq arg (current-column))) | |
6116 | (if (not (integerp arg)) | |
6117 | ;; Disallow missing argument; it's probably a typo for C-x C-f. | |
f33321ad | 6118 | (error "set-fill-column requires an explicit argument") |
f4520363 RS |
6119 | (message "Fill column set to %d (was %d)" arg fill-column) |
6120 | (setq fill-column arg))) | |
2d88b556 | 6121 | \f |
2076c87c | 6122 | (defun set-selective-display (arg) |
ff1fbe3e RS |
6123 | "Set `selective-display' to ARG; clear it if no arg. |
6124 | When the value of `selective-display' is a number > 0, | |
6125 | lines whose indentation is >= that value are not displayed. | |
6126 | The variable `selective-display' has a separate value for each buffer." | |
2076c87c JB |
6127 | (interactive "P") |
6128 | (if (eq selective-display t) | |
6129 | (error "selective-display already in use for marked lines")) | |
c88ab9ce ER |
6130 | (let ((current-vpos |
6131 | (save-restriction | |
6132 | (narrow-to-region (point-min) (point)) | |
6133 | (goto-char (window-start)) | |
6134 | (vertical-motion (window-height))))) | |
6135 | (setq selective-display | |
6136 | (and arg (prefix-numeric-value arg))) | |
6137 | (recenter current-vpos)) | |
290d5b58 | 6138 | (set-window-start (selected-window) (window-start)) |
2076c87c JB |
6139 | (princ "selective-display set to " t) |
6140 | (prin1 selective-display t) | |
6141 | (princ "." t)) | |
6142 | ||
40a64816 | 6143 | (defvaralias 'indicate-unused-lines 'indicate-empty-lines) |
40a64816 | 6144 | |
b3228584 | 6145 | (defun toggle-truncate-lines (&optional arg) |
1d8c2ccc LMI |
6146 | "Toggle truncating of long lines for the current buffer. |
6147 | When truncating is off, long lines are folded. | |
4837b516 | 6148 | With prefix argument ARG, truncate long lines if ARG is positive, |
1d8c2ccc LMI |
6149 | otherwise fold them. Note that in side-by-side windows, this |
6150 | command has no effect if `truncate-partial-width-windows' is | |
6151 | non-nil." | |
0bb64d76 PA |
6152 | (interactive "P") |
6153 | (setq truncate-lines | |
6154 | (if (null arg) | |
6155 | (not truncate-lines) | |
46cdfe8f RS |
6156 | (> (prefix-numeric-value arg) 0))) |
6157 | (force-mode-line-update) | |
4f017185 RS |
6158 | (unless truncate-lines |
6159 | (let ((buffer (current-buffer))) | |
6160 | (walk-windows (lambda (window) | |
6161 | (if (eq buffer (window-buffer window)) | |
6162 | (set-window-hscroll window 0))) | |
6163 | nil t))) | |
46cdfe8f RS |
6164 | (message "Truncate long lines %s" |
6165 | (if truncate-lines "enabled" "disabled"))) | |
0bb64d76 | 6166 | |
c899b3db JL |
6167 | (defun toggle-word-wrap (&optional arg) |
6168 | "Toggle whether to use word-wrapping for continuation lines. | |
6169 | With prefix argument ARG, wrap continuation lines at word boundaries | |
6170 | if ARG is positive, otherwise wrap them at the right screen edge. | |
6171 | This command toggles the value of `word-wrap'. It has no effect | |
6172 | if long lines are truncated." | |
6173 | (interactive "P") | |
6174 | (setq word-wrap | |
6175 | (if (null arg) | |
6176 | (not word-wrap) | |
6177 | (> (prefix-numeric-value arg) 0))) | |
6178 | (force-mode-line-update) | |
6179 | (message "Word wrapping %s" | |
6180 | (if word-wrap "enabled" "disabled"))) | |
6181 | ||
ca0a881a | 6182 | (defvar overwrite-mode-textual (purecopy " Ovwrt") |
b6a22db0 | 6183 | "The string displayed in the mode line when in overwrite mode.") |
ca0a881a | 6184 | (defvar overwrite-mode-binary (purecopy " Bin Ovwrt") |
b6a22db0 JB |
6185 | "The string displayed in the mode line when in binary overwrite mode.") |
6186 | ||
80ac5d4d | 6187 | (define-minor-mode overwrite-mode |
06e21633 CY |
6188 | "Toggle Overwrite mode. |
6189 | With a prefix argument ARG, enable Overwrite mode if ARG is | |
6190 | positive, and disable it otherwise. If called from Lisp, enable | |
6191 | the mode if ARG is omitted or nil. | |
6192 | ||
6193 | When Overwrite mode is enabled, printing characters typed in | |
6194 | replace existing text on a one-for-one basis, rather than pushing | |
6195 | it to the right. At the end of a line, such characters extend | |
6196 | the line. Before a tab, such characters insert until the tab is | |
6197 | filled in. \\[quoted-insert] still inserts characters in | |
6198 | overwrite mode; this is supposed to make it easier to insert | |
6199 | characters when necessary." | |
2ee3d7f0 SM |
6200 | :variable (overwrite-mode |
6201 | . (lambda (v) (setq overwrite-mode (if v 'overwrite-mode-textual))))) | |
b6a22db0 | 6202 | |
80ac5d4d | 6203 | (define-minor-mode binary-overwrite-mode |
06e21633 CY |
6204 | "Toggle Binary Overwrite mode. |
6205 | With a prefix argument ARG, enable Binary Overwrite mode if ARG | |
6206 | is positive, and disable it otherwise. If called from Lisp, | |
6207 | enable the mode if ARG is omitted or nil. | |
6208 | ||
6209 | When Binary Overwrite mode is enabled, printing characters typed | |
6210 | in replace existing text. Newlines are not treated specially, so | |
6211 | typing at the end of a line joins the line to the next, with the | |
6212 | typed character between them. Typing before a tab character | |
6213 | simply replaces the tab with the character typed. | |
6214 | \\[quoted-insert] replaces the text at the cursor, just as | |
6215 | ordinary typing characters do. | |
6216 | ||
6217 | Note that Binary Overwrite mode is not its own minor mode; it is | |
6218 | a specialization of overwrite mode, entered by setting the | |
b6a22db0 | 6219 | `overwrite-mode' variable to `overwrite-mode-binary'." |
2ee3d7f0 SM |
6220 | :variable (overwrite-mode |
6221 | . (lambda (v) (setq overwrite-mode (if v 'overwrite-mode-binary))))) | |
eaae8106 | 6222 | |
6710df48 | 6223 | (define-minor-mode line-number-mode |
06e21633 CY |
6224 | "Toggle line number display in the mode line (Line Number mode). |
6225 | With a prefix argument ARG, enable Line Number mode if ARG is | |
6226 | positive, and disable it otherwise. If called from Lisp, enable | |
6227 | the mode if ARG is omitted or nil. | |
8dc9e2ef | 6228 | |
32f2f98e EZ |
6229 | Line numbers do not appear for very large buffers and buffers |
6230 | with very long lines; see variables `line-number-display-limit' | |
6231 | and `line-number-display-limit-width'." | |
efeb22bf | 6232 | :init-value t :global t :group 'mode-line) |
bcad4985 | 6233 | |
6710df48 | 6234 | (define-minor-mode column-number-mode |
06e21633 CY |
6235 | "Toggle column number display in the mode line (Column Number mode). |
6236 | With a prefix argument ARG, enable Column Number mode if ARG is | |
6237 | positive, and disable it otherwise. | |
6238 | ||
6239 | If called from Lisp, enable the mode if ARG is omitted or nil." | |
efeb22bf | 6240 | :global t :group 'mode-line) |
6b61353c KH |
6241 | |
6242 | (define-minor-mode size-indication-mode | |
06e21633 CY |
6243 | "Toggle buffer size display in the mode line (Size Indication mode). |
6244 | With a prefix argument ARG, enable Size Indication mode if ARG is | |
6245 | positive, and disable it otherwise. | |
6246 | ||
6247 | If called from Lisp, enable the mode if ARG is omitted or nil." | |
efeb22bf | 6248 | :global t :group 'mode-line) |
f3ee9200 SM |
6249 | |
6250 | (define-minor-mode auto-save-mode | |
06e21633 CY |
6251 | "Toggle auto-saving in the current buffer (Auto Save mode). |
6252 | With a prefix argument ARG, enable Auto Save mode if ARG is | |
6253 | positive, and disable it otherwise. | |
6254 | ||
6255 | If called from Lisp, enable the mode if ARG is omitted or nil." | |
f3ee9200 SM |
6256 | :variable ((and buffer-auto-save-file-name |
6257 | ;; If auto-save is off because buffer has shrunk, | |
6258 | ;; then toggling should turn it on. | |
6259 | (>= buffer-saved-size 0)) | |
6260 | . (lambda (val) | |
6261 | (setq buffer-auto-save-file-name | |
6262 | (cond | |
6263 | ((null val) nil) | |
6264 | ((and buffer-file-name auto-save-visited-file-name | |
6265 | (not buffer-read-only)) | |
6266 | buffer-file-name) | |
6267 | (t (make-auto-save-file-name)))))) | |
6268 | ;; If -1 was stored here, to temporarily turn off saving, | |
6269 | ;; turn it back on. | |
6270 | (and (< buffer-saved-size 0) | |
6271 | (setq buffer-saved-size 0))) | |
2d88b556 | 6272 | \f |
4b384a8f | 6273 | (defgroup paren-blinking nil |
020db25f | 6274 | "Blinking matching of parens and expressions." |
4b384a8f SM |
6275 | :prefix "blink-matching-" |
6276 | :group 'paren-matching) | |
6277 | ||
69c1dd37 | 6278 | (defcustom blink-matching-paren t |
1d2b0303 | 6279 | "Non-nil means show matching open-paren when close-paren is inserted." |
69c1dd37 | 6280 | :type 'boolean |
4b384a8f | 6281 | :group 'paren-blinking) |
2076c87c | 6282 | |
69c1dd37 | 6283 | (defcustom blink-matching-paren-on-screen t |
1d2b0303 | 6284 | "Non-nil means show matching open-paren when it is on screen. |
1c2ba4e7 | 6285 | If nil, don't show it (but the open-paren can still be shown |
92aa8a33 LT |
6286 | when it is off screen). |
6287 | ||
9cb370a9 | 6288 | This variable has no effect if `blink-matching-paren' is nil. |
a9f72e5f | 6289 | \(In that case, the open-paren is never shown.) |
9cb370a9 | 6290 | It is also ignored if `show-paren-mode' is enabled." |
69c1dd37 | 6291 | :type 'boolean |
4b384a8f | 6292 | :group 'paren-blinking) |
29fc44dd | 6293 | |
fd413a37 | 6294 | (defcustom blink-matching-paren-distance (* 100 1024) |
1d2b0303 | 6295 | "If non-nil, maximum distance to search backwards for matching open-paren. |
66d44a36 | 6296 | If nil, search stops at the beginning of the accessible portion of the buffer." |
fd413a37 | 6297 | :version "23.2" ; 25->100k |
66d44a36 | 6298 | :type '(choice (const nil) integer) |
4b384a8f | 6299 | :group 'paren-blinking) |
2076c87c | 6300 | |
69c1dd37 | 6301 | (defcustom blink-matching-delay 1 |
1d2b0303 | 6302 | "Time in seconds to delay after showing a matching paren." |
4b384a8f SM |
6303 | :type 'number |
6304 | :group 'paren-blinking) | |
72dddf8b | 6305 | |
69c1dd37 | 6306 | (defcustom blink-matching-paren-dont-ignore-comments nil |
1d2b0303 | 6307 | "If nil, `blink-matching-paren' ignores comments. |
ab6b3b16 RS |
6308 | More precisely, when looking for the matching parenthesis, |
6309 | it skips the contents of comments that end before point." | |
69c1dd37 | 6310 | :type 'boolean |
4b384a8f | 6311 | :group 'paren-blinking) |
903b7f65 | 6312 | |
b13ebb5c SM |
6313 | (defun blink-matching-check-mismatch (start end) |
6314 | "Return whether or not START...END are matching parens. | |
6315 | END is the current point and START is the blink position. | |
6316 | START might be nil if no matching starter was found. | |
6317 | Returns non-nil if we find there is a mismatch." | |
6318 | (let* ((end-syntax (syntax-after (1- end))) | |
6319 | (matching-paren (and (consp end-syntax) | |
6320 | (eq (syntax-class end-syntax) 5) | |
6321 | (cdr end-syntax)))) | |
6322 | ;; For self-matched chars like " and $, we can't know when they're | |
6323 | ;; mismatched or unmatched, so we can only do it for parens. | |
6324 | (when matching-paren | |
6325 | (not (and start | |
6326 | (or | |
6327 | (eq (char-after start) matching-paren) | |
6328 | ;; The cdr might hold a new paren-class info rather than | |
6329 | ;; a matching-char info, in which case the two CDRs | |
6330 | ;; should match. | |
6331 | (eq matching-paren (cdr-safe (syntax-after start))))))))) | |
6332 | ||
6333 | (defvar blink-matching-check-function #'blink-matching-check-mismatch | |
6334 | "Function to check parentheses mismatches. | |
6335 | The function takes two arguments (START and END) where START is the | |
6336 | position just before the opening token and END is the position right after. | |
6337 | START can be nil, if it was not found. | |
6338 | The function should return non-nil if the two tokens do not match.") | |
6339 | ||
84c73ba0 DG |
6340 | (defvar blink-matching--overlay |
6341 | (let ((ol (make-overlay (point) (point) nil t))) | |
6342 | (overlay-put ol 'face 'show-paren-match) | |
6343 | (delete-overlay ol) | |
6344 | ol) | |
6345 | "Overlay used to highlight the matching paren.") | |
6346 | ||
2076c87c | 6347 | (defun blink-matching-open () |
84c73ba0 | 6348 | "Momentarily highlight the beginning of the sexp before point." |
2076c87c | 6349 | (interactive) |
b13ebb5c SM |
6350 | (when (and (not (bobp)) |
6351 | blink-matching-paren) | |
1d0e3fc8 | 6352 | (let* ((oldpos (point)) |
b13ebb5c | 6353 | (message-log-max nil) ; Don't log messages about paren matching. |
3137dda8 SM |
6354 | (blinkpos |
6355 | (save-excursion | |
6356 | (save-restriction | |
5fc4038e CY |
6357 | (if blink-matching-paren-distance |
6358 | (narrow-to-region | |
6359 | (max (minibuffer-prompt-end) ;(point-min) unless minibuf. | |
6360 | (- (point) blink-matching-paren-distance)) | |
6361 | oldpos)) | |
3137dda8 SM |
6362 | (let ((parse-sexp-ignore-comments |
6363 | (and parse-sexp-ignore-comments | |
6364 | (not blink-matching-paren-dont-ignore-comments)))) | |
6365 | (condition-case () | |
4b9c0a49 SM |
6366 | (progn |
6367 | (forward-sexp -1) | |
b13ebb5c SM |
6368 | ;; backward-sexp skips backward over prefix chars, |
6369 | ;; so move back to the matching paren. | |
6370 | (while (and (< (point) (1- oldpos)) | |
984edd22 SM |
6371 | (let ((code (syntax-after (point)))) |
6372 | (or (eq (syntax-class code) 6) | |
6373 | (eq (logand 1048576 (car code)) | |
6374 | 1048576)))) | |
b13ebb5c | 6375 | (forward-char 1)) |
4b9c0a49 | 6376 | (point)) |
3137dda8 | 6377 | (error nil)))))) |
b13ebb5c | 6378 | (mismatch (funcall blink-matching-check-function blinkpos oldpos))) |
3137dda8 | 6379 | (cond |
b13ebb5c SM |
6380 | (mismatch |
6381 | (if blinkpos | |
28e271f0 | 6382 | (if (minibufferp) |
5fc4038e CY |
6383 | (minibuffer-message "Mismatched parentheses") |
6384 | (message "Mismatched parentheses")) | |
b13ebb5c | 6385 | (if (minibufferp) |
5fc4038e CY |
6386 | (minibuffer-message "No matching parenthesis found") |
6387 | (message "No matching parenthesis found")))) | |
c34a9669 | 6388 | ((not blinkpos) nil) |
3137dda8 | 6389 | ((pos-visible-in-window-p blinkpos) |
84c73ba0 DG |
6390 | ;; Matching open within window, temporarily highlight char |
6391 | ;; after blinkpos but only if `blink-matching-paren-on-screen' | |
6392 | ;; is non-nil. | |
3137dda8 SM |
6393 | (and blink-matching-paren-on-screen |
6394 | (not show-paren-mode) | |
84c73ba0 DG |
6395 | (unwind-protect |
6396 | (progn | |
6397 | (move-overlay blink-matching--overlay blinkpos (1+ blinkpos) | |
6398 | (current-buffer)) | |
6399 | (sit-for blink-matching-delay)) | |
6400 | (delete-overlay blink-matching--overlay)))) | |
3137dda8 SM |
6401 | (t |
6402 | (save-excursion | |
6403 | (goto-char blinkpos) | |
6404 | (let ((open-paren-line-string | |
6405 | ;; Show what precedes the open in its line, if anything. | |
6406 | (cond | |
6407 | ((save-excursion (skip-chars-backward " \t") (not (bolp))) | |
6408 | (buffer-substring (line-beginning-position) | |
6409 | (1+ blinkpos))) | |
6410 | ;; Show what follows the open in its line, if anything. | |
6411 | ((save-excursion | |
6412 | (forward-char 1) | |
6413 | (skip-chars-forward " \t") | |
6414 | (not (eolp))) | |
6415 | (buffer-substring blinkpos | |
6416 | (line-end-position))) | |
6417 | ;; Otherwise show the previous nonblank line, | |
6418 | ;; if there is one. | |
6419 | ((save-excursion (skip-chars-backward "\n \t") (not (bobp))) | |
6420 | (concat | |
6421 | (buffer-substring (progn | |
6422 | (skip-chars-backward "\n \t") | |
6423 | (line-beginning-position)) | |
6424 | (progn (end-of-line) | |
6425 | (skip-chars-backward " \t") | |
6426 | (point))) | |
6427 | ;; Replace the newline and other whitespace with `...'. | |
6428 | "..." | |
6429 | (buffer-substring blinkpos (1+ blinkpos)))) | |
6430 | ;; There is nothing to show except the char itself. | |
6431 | (t (buffer-substring blinkpos (1+ blinkpos)))))) | |
6432 | (message "Matches %s" | |
6433 | (substring-no-properties open-paren-line-string))))))))) | |
6434 | ||
8f4e9110 SM |
6435 | (defvar blink-paren-function 'blink-matching-open |
6436 | "Function called, if non-nil, whenever a close parenthesis is inserted. | |
6437 | More precisely, a char with closeparen syntax is self-inserted.") | |
6438 | ||
6439 | (defun blink-paren-post-self-insert-function () | |
6440 | (when (and (eq (char-before) last-command-event) ; Sanity check. | |
6441 | (memq (char-syntax last-command-event) '(?\) ?\$)) | |
6442 | blink-paren-function | |
6443 | (not executing-kbd-macro) | |
b13ebb5c SM |
6444 | (not noninteractive) |
6445 | ;; Verify an even number of quoting characters precede the close. | |
6446 | (= 1 (logand 1 (- (point) | |
6447 | (save-excursion | |
6448 | (forward-char -1) | |
6449 | (skip-syntax-backward "/\\") | |
6450 | (point)))))) | |
8f4e9110 SM |
6451 | (funcall blink-paren-function))) |
6452 | ||
3b8d5131 JT |
6453 | (put 'blink-paren-post-self-insert-function 'priority 100) |
6454 | ||
8f4e9110 SM |
6455 | (add-hook 'post-self-insert-hook #'blink-paren-post-self-insert-function |
6456 | ;; Most likely, this hook is nil, so this arg doesn't matter, | |
6457 | ;; but I use it as a reminder that this function usually | |
3b8d5131 JT |
6458 | ;; likes to be run after others since it does |
6459 | ;; `sit-for'. That's also the reason it get a `priority' prop | |
6460 | ;; of 100. | |
8f4e9110 | 6461 | 'append) |
2d88b556 | 6462 | \f |
9a1277dd RS |
6463 | ;; This executes C-g typed while Emacs is waiting for a command. |
6464 | ;; Quitting out of a program does not go through here; | |
6465 | ;; that happens in the QUIT macro at the C code level. | |
2076c87c | 6466 | (defun keyboard-quit () |
d5dae4e1 | 6467 | "Signal a `quit' condition. |
af39530e RS |
6468 | During execution of Lisp code, this character causes a quit directly. |
6469 | At top-level, as an editor command, this simply beeps." | |
2076c87c | 6470 | (interactive) |
9852377f CY |
6471 | ;; Avoid adding the region to the window selection. |
6472 | (setq saved-region-selection nil) | |
6473 | (let (select-active-regions) | |
6474 | (deactivate-mark)) | |
8a7644e9 KS |
6475 | (if (fboundp 'kmacro-keyboard-quit) |
6476 | (kmacro-keyboard-quit)) | |
f5e13057 | 6477 | (setq defining-kbd-macro nil) |
fb5b2591 SM |
6478 | (let ((debug-on-quit nil)) |
6479 | (signal 'quit nil))) | |
2076c87c | 6480 | |
1c6c6fde RS |
6481 | (defvar buffer-quit-function nil |
6482 | "Function to call to \"quit\" the current buffer, or nil if none. | |
6483 | \\[keyboard-escape-quit] calls this function when its more local actions | |
c80e3b4a | 6484 | \(such as canceling a prefix argument, minibuffer or region) do not apply.") |
1c6c6fde | 6485 | |
c66587fe RS |
6486 | (defun keyboard-escape-quit () |
6487 | "Exit the current \"mode\" (in a generalized sense of the word). | |
6488 | This command can exit an interactive command such as `query-replace', | |
6489 | can clear out a prefix argument or a region, | |
6490 | can get out of the minibuffer or other recursive edit, | |
1c6c6fde RS |
6491 | cancel the use of the current buffer (for special-purpose buffers), |
6492 | or go back to just one window (by deleting all but the selected window)." | |
c66587fe RS |
6493 | (interactive) |
6494 | (cond ((eq last-command 'mode-exited) nil) | |
67189e62 JL |
6495 | ((region-active-p) |
6496 | (deactivate-mark)) | |
c66587fe RS |
6497 | ((> (minibuffer-depth) 0) |
6498 | (abort-recursive-edit)) | |
6499 | (current-prefix-arg | |
6500 | nil) | |
1b657835 RS |
6501 | ((> (recursion-depth) 0) |
6502 | (exit-recursive-edit)) | |
1c6c6fde RS |
6503 | (buffer-quit-function |
6504 | (funcall buffer-quit-function)) | |
c66587fe | 6505 | ((not (one-window-p t)) |
1b657835 RS |
6506 | (delete-other-windows)) |
6507 | ((string-match "^ \\*" (buffer-name (current-buffer))) | |
6508 | (bury-buffer)))) | |
c66587fe | 6509 | |
2d88b556 RS |
6510 | (defun play-sound-file (file &optional volume device) |
6511 | "Play sound stored in FILE. | |
6512 | VOLUME and DEVICE correspond to the keywords of the sound | |
6513 | specification for `play-sound'." | |
6514 | (interactive "fPlay sound file: ") | |
6515 | (let ((sound (list :file file))) | |
6516 | (if volume | |
6517 | (plist-put sound :volume volume)) | |
6518 | (if device | |
6519 | (plist-put sound :device device)) | |
6520 | (push 'sound sound) | |
6521 | (play-sound sound))) | |
6522 | ||
56abefac | 6523 | \f |
7683b5c2 | 6524 | (defcustom read-mail-command 'rmail |
1d2b0303 | 6525 | "Your preference for a mail reading package. |
9023837e DL |
6526 | This is used by some keybindings which support reading mail. |
6527 | See also `mail-user-agent' concerning sending mail." | |
f6714ede GM |
6528 | :type '(radio (function-item :tag "Rmail" :format "%t\n" rmail) |
6529 | (function-item :tag "Gnus" :format "%t\n" gnus) | |
6530 | (function-item :tag "Emacs interface to MH" | |
6531 | :format "%t\n" mh-rmail) | |
6532 | (function :tag "Other")) | |
7683b5c2 DL |
6533 | :version "21.1" |
6534 | :group 'mail) | |
6535 | ||
cbd61418 | 6536 | (defcustom mail-user-agent 'message-user-agent |
1d2b0303 | 6537 | "Your preference for a mail composition package. |
9023837e | 6538 | Various Emacs Lisp packages (e.g. Reporter) require you to compose an |
a31ca314 RS |
6539 | outgoing email message. This variable lets you specify which |
6540 | mail-sending package you prefer. | |
6541 | ||
6542 | Valid values include: | |
6543 | ||
cfc47664 GM |
6544 | `message-user-agent' -- use the Message package. |
6545 | See Info node `(message)'. | |
6546 | `sendmail-user-agent' -- use the Mail package. | |
9023837e DL |
6547 | See Info node `(emacs)Sending Mail'. |
6548 | `mh-e-user-agent' -- use the Emacs interface to the MH mail system. | |
6549 | See Info node `(mh-e)'. | |
9023837e | 6550 | `gnus-user-agent' -- like `message-user-agent', but with Gnus |
5f0af64f LI |
6551 | paraphernalia if Gnus is running, particularly |
6552 | the Gcc: header for archiving. | |
a31ca314 RS |
6553 | |
6554 | Additional valid symbols may be available; check with the author of | |
15d0c9b1 DL |
6555 | your package for details. The function should return non-nil if it |
6556 | succeeds. | |
9023837e DL |
6557 | |
6558 | See also `read-mail-command' concerning reading mail." | |
cfc47664 GM |
6559 | :type '(radio (function-item :tag "Message package" |
6560 | :format "%t\n" | |
6561 | message-user-agent) | |
6562 | (function-item :tag "Mail package" | |
69c1dd37 RS |
6563 | :format "%t\n" |
6564 | sendmail-user-agent) | |
6565 | (function-item :tag "Emacs interface to MH" | |
6566 | :format "%t\n" | |
6567 | mh-e-user-agent) | |
cfc47664 | 6568 | (function-item :tag "Message with full Gnus features" |
9023837e DL |
6569 | :format "%t\n" |
6570 | gnus-user-agent) | |
69c1dd37 | 6571 | (function :tag "Other")) |
cfc47664 | 6572 | :version "23.2" ; sendmail->message |
69c1dd37 | 6573 | :group 'mail) |
a31ca314 | 6574 | |
3d68fa99 CY |
6575 | (defcustom compose-mail-user-agent-warnings t |
6576 | "If non-nil, `compose-mail' warns about changes in `mail-user-agent'. | |
6577 | If the value of `mail-user-agent' is the default, and the user | |
6578 | appears to have customizations applying to the old default, | |
6579 | `compose-mail' issues a warning." | |
6580 | :type 'boolean | |
6581 | :version "23.2" | |
6582 | :group 'mail) | |
6583 | ||
360b5483 | 6584 | (defun rfc822-goto-eoh () |
41002397 GM |
6585 | "If the buffer starts with a mail header, move point to the header's end. |
6586 | Otherwise, moves to `point-min'. | |
6587 | The end of the header is the start of the next line, if there is one, | |
6588 | else the end of the last line. This function obeys RFC822." | |
360b5483 | 6589 | (goto-char (point-min)) |
e1e04350 SM |
6590 | (when (re-search-forward |
6591 | "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move) | |
6592 | (goto-char (match-beginning 0)))) | |
360b5483 | 6593 | |
fd59d131 EZ |
6594 | ;; Used by Rmail (e.g., rmail-forward). |
6595 | (defvar mail-encode-mml nil | |
6596 | "If non-nil, mail-user-agent's `sendfunc' command should mml-encode | |
6597 | the outgoing message before sending it.") | |
6598 | ||
d0008a00 | 6599 | (defun compose-mail (&optional to subject other-headers continue |
25ca2e61 CY |
6600 | switch-function yank-action send-actions |
6601 | return-action) | |
d0008a00 RS |
6602 | "Start composing a mail message to send. |
6603 | This uses the user's chosen mail composition package | |
6604 | as selected with the variable `mail-user-agent'. | |
6605 | The optional arguments TO and SUBJECT specify recipients | |
6606 | and the initial Subject field, respectively. | |
6607 | ||
6608 | OTHER-HEADERS is an alist specifying additional | |
6609 | header fields. Elements look like (HEADER . VALUE) where both | |
6610 | HEADER and VALUE are strings. | |
6611 | ||
6612 | CONTINUE, if non-nil, says to continue editing a message already | |
9dda5b0e | 6613 | being composed. Interactively, CONTINUE is the prefix argument. |
d0008a00 RS |
6614 | |
6615 | SWITCH-FUNCTION, if non-nil, is a function to use to | |
6616 | switch to and display the buffer used for mail composition. | |
6617 | ||
6618 | YANK-ACTION, if non-nil, is an action to perform, if and when necessary, | |
06720de2 RS |
6619 | to insert the raw text of the message being replied to. |
6620 | It has the form (FUNCTION . ARGS). The user agent will apply | |
6621 | FUNCTION to ARGS, to insert the raw text of the original message. | |
6622 | \(The user agent will also run `mail-citation-hook', *after* the | |
6623 | original text has been inserted in this way.) | |
d0008a00 RS |
6624 | |
6625 | SEND-ACTIONS is a list of actions to call when the message is sent. | |
25ca2e61 CY |
6626 | Each action has the form (FUNCTION . ARGS). |
6627 | ||
6628 | RETURN-ACTION, if non-nil, is an action for returning to the | |
6629 | caller. It has the form (FUNCTION . ARGS). The function is | |
6630 | called after the mail has been sent or put aside, and the mail | |
6631 | buffer buried." | |
b5f019be RS |
6632 | (interactive |
6633 | (list nil nil nil current-prefix-arg)) | |
3d68fa99 CY |
6634 | |
6635 | ;; In Emacs 23.2, the default value of `mail-user-agent' changed | |
6636 | ;; from sendmail-user-agent to message-user-agent. Some users may | |
6637 | ;; encounter incompatibilities. This hack tries to detect problems | |
6638 | ;; and warn about them. | |
6639 | (and compose-mail-user-agent-warnings | |
6640 | (eq mail-user-agent 'message-user-agent) | |
6641 | (let (warn-vars) | |
6642 | (dolist (var '(mail-mode-hook mail-send-hook mail-setup-hook | |
6643 | mail-yank-hooks mail-archive-file-name | |
6644 | mail-default-reply-to mail-mailing-lists | |
5e1d4968 | 6645 | mail-self-blind)) |
3d68fa99 CY |
6646 | (and (boundp var) |
6647 | (symbol-value var) | |
6648 | (push var warn-vars))) | |
6649 | (when warn-vars | |
6650 | (display-warning 'mail | |
6651 | (format "\ | |
6652 | The default mail mode is now Message mode. | |
6653 | You have the following Mail mode variable%s customized: | |
6654 | \n %s\n\nTo use Mail mode, set `mail-user-agent' to sendmail-user-agent. | |
7f0b7b3e | 6655 | To disable this warning, set `compose-mail-user-agent-warnings' to nil." |
3d68fa99 CY |
6656 | (if (> (length warn-vars) 1) "s" "") |
6657 | (mapconcat 'symbol-name | |
6658 | warn-vars " ")))))) | |
6659 | ||
676b1a74 | 6660 | (let ((function (get mail-user-agent 'composefunc))) |
25ca2e61 CY |
6661 | (funcall function to subject other-headers continue switch-function |
6662 | yank-action send-actions return-action))) | |
b5f019be RS |
6663 | |
6664 | (defun compose-mail-other-window (&optional to subject other-headers continue | |
25ca2e61 CY |
6665 | yank-action send-actions |
6666 | return-action) | |
b5f019be | 6667 | "Like \\[compose-mail], but edit the outgoing message in another window." |
25ca2e61 | 6668 | (interactive (list nil nil nil current-prefix-arg)) |
b5f019be | 6669 | (compose-mail to subject other-headers continue |
25ca2e61 CY |
6670 | 'switch-to-buffer-other-window yank-action send-actions |
6671 | return-action)) | |
b5f019be RS |
6672 | |
6673 | (defun compose-mail-other-frame (&optional to subject other-headers continue | |
25ca2e61 CY |
6674 | yank-action send-actions |
6675 | return-action) | |
b5f019be | 6676 | "Like \\[compose-mail], but edit the outgoing message in another frame." |
25ca2e61 | 6677 | (interactive (list nil nil nil current-prefix-arg)) |
b5f019be | 6678 | (compose-mail to subject other-headers continue |
25ca2e61 CY |
6679 | 'switch-to-buffer-other-frame yank-action send-actions |
6680 | return-action)) | |
6681 | ||
56abefac | 6682 | \f |
610c1c68 | 6683 | (defvar set-variable-value-history nil |
987ec16d EZ |
6684 | "History of values entered with `set-variable'. |
6685 | ||
6686 | Maximum length of the history list is determined by the value | |
6687 | of `history-length', which see.") | |
610c1c68 | 6688 | |
d6281b4e | 6689 | (defun set-variable (variable value &optional make-local) |
610c1c68 | 6690 | "Set VARIABLE to VALUE. VALUE is a Lisp object. |
d6281b4e RS |
6691 | VARIABLE should be a user option variable name, a Lisp variable |
6692 | meant to be customized by users. You should enter VALUE in Lisp syntax, | |
6693 | so if you want VALUE to be a string, you must surround it with doublequotes. | |
610c1c68 RS |
6694 | VALUE is used literally, not evaluated. |
6695 | ||
6696 | If VARIABLE has a `variable-interactive' property, that is used as if | |
6697 | it were the arg to `interactive' (which see) to interactively read VALUE. | |
6698 | ||
6699 | If VARIABLE has been defined with `defcustom', then the type information | |
16236388 RS |
6700 | in the definition is used to check that VALUE is valid. |
6701 | ||
6702 | With a prefix argument, set VARIABLE to VALUE buffer-locally." | |
e9dfb72e RS |
6703 | (interactive |
6704 | (let* ((default-var (variable-at-point)) | |
b4d3bc10 | 6705 | (var (if (custom-variable-p default-var) |
7fd0ef0d JL |
6706 | (read-variable (format "Set variable (default %s): " default-var) |
6707 | default-var) | |
6708 | (read-variable "Set variable: "))) | |
6b61353c KH |
6709 | (minibuffer-help-form '(describe-variable var)) |
6710 | (prop (get var 'variable-interactive)) | |
0684376b JB |
6711 | (obsolete (car (get var 'byte-obsolete-variable))) |
6712 | (prompt (format "Set %s %s to value: " var | |
6b61353c | 6713 | (cond ((local-variable-p var) |
0684376b | 6714 | "(buffer-local)") |
6b61353c KH |
6715 | ((or current-prefix-arg |
6716 | (local-variable-if-set-p var)) | |
0684376b JB |
6717 | "buffer-locally") |
6718 | (t "globally")))) | |
6719 | (val (progn | |
6720 | (when obsolete | |
6721 | (message (concat "`%S' is obsolete; " | |
6722 | (if (symbolp obsolete) "use `%S' instead" "%s")) | |
6723 | var obsolete) | |
6724 | (sit-for 3)) | |
6725 | (if prop | |
6726 | ;; Use VAR's `variable-interactive' property | |
6727 | ;; as an interactive spec for prompting. | |
6728 | (call-interactively `(lambda (arg) | |
6729 | (interactive ,prop) | |
6730 | arg)) | |
a343d218 SM |
6731 | (read-from-minibuffer prompt nil |
6732 | read-expression-map t | |
6733 | 'set-variable-value-history | |
6734 | (format "%S" (symbol-value var))))))) | |
6b61353c | 6735 | (list var val current-prefix-arg))) |
610c1c68 | 6736 | |
d6281b4e RS |
6737 | (and (custom-variable-p variable) |
6738 | (not (get variable 'custom-type)) | |
6739 | (custom-load-symbol variable)) | |
6740 | (let ((type (get variable 'custom-type))) | |
610c1c68 RS |
6741 | (when type |
6742 | ;; Match with custom type. | |
36755dd9 | 6743 | (require 'cus-edit) |
610c1c68 | 6744 | (setq type (widget-convert type)) |
d6281b4e | 6745 | (unless (widget-apply type :match value) |
1e722f9f | 6746 | (error "Value `%S' does not match type %S of %S" |
d6281b4e | 6747 | value (car type) variable)))) |
16236388 RS |
6748 | |
6749 | (if make-local | |
d6281b4e | 6750 | (make-local-variable variable)) |
f1180544 | 6751 | |
d6281b4e | 6752 | (set variable value) |
a2aef080 GM |
6753 | |
6754 | ;; Force a thorough redisplay for the case that the variable | |
6755 | ;; has an effect on the display, like `tab-width' has. | |
6756 | (force-mode-line-update)) | |
56abefac | 6757 | \f |
e8a700bf RS |
6758 | ;; Define the major mode for lists of completions. |
6759 | ||
e2947429 SM |
6760 | (defvar completion-list-mode-map |
6761 | (let ((map (make-sparse-keymap))) | |
6762 | (define-key map [mouse-2] 'mouse-choose-completion) | |
6763 | (define-key map [follow-link] 'mouse-face) | |
6764 | (define-key map [down-mouse-2] nil) | |
6765 | (define-key map "\C-m" 'choose-completion) | |
6766 | (define-key map "\e\e\e" 'delete-completion-window) | |
6767 | (define-key map [left] 'previous-completion) | |
6768 | (define-key map [right] 'next-completion) | |
45f8cb0c | 6769 | (define-key map "q" 'quit-window) |
abef340a | 6770 | (define-key map "z" 'kill-this-buffer) |
e2947429 | 6771 | map) |
98b45886 | 6772 | "Local map for completion list buffers.") |
e8a700bf RS |
6773 | |
6774 | ;; Completion mode is suitable only for specially formatted data. | |
ac29eb79 | 6775 | (put 'completion-list-mode 'mode-class 'special) |
e8a700bf | 6776 | |
98b45886 RS |
6777 | (defvar completion-reference-buffer nil |
6778 | "Record the buffer that was current when the completion list was requested. | |
6779 | This is a local variable in the completion list buffer. | |
ec39964e | 6780 | Initial value is nil to avoid some compiler warnings.") |
3819736b | 6781 | |
83434bda RS |
6782 | (defvar completion-no-auto-exit nil |
6783 | "Non-nil means `choose-completion-string' should never exit the minibuffer. | |
f6714ede | 6784 | This also applies to other functions such as `choose-completion'.") |
83434bda | 6785 | |
d5e63715 SM |
6786 | (defvar completion-base-position nil |
6787 | "Position of the base of the text corresponding to the shown completions. | |
6788 | This variable is used in the *Completions* buffers. | |
6789 | Its value is a list of the form (START END) where START is the place | |
6790 | where the completion should be inserted and END (if non-nil) is the end | |
6791 | of the text to replace. If END is nil, point is used instead.") | |
6792 | ||
a2a25d24 SM |
6793 | (defvar completion-list-insert-choice-function #'completion--replace |
6794 | "Function to use to insert the text chosen in *Completions*. | |
382c953b | 6795 | Called with three arguments (BEG END TEXT), it should replace the text |
a2a25d24 SM |
6796 | between BEG and END with TEXT. Expected to be set buffer-locally |
6797 | in the *Completions* buffer.") | |
6798 | ||
98b45886 | 6799 | (defvar completion-base-size nil |
3c59150d CY |
6800 | "Number of chars before point not involved in completion. |
6801 | This is a local variable in the completion list buffer. | |
6802 | It refers to the chars in the minibuffer if completing in the | |
6803 | minibuffer, or in `completion-reference-buffer' otherwise. | |
6804 | Only characters in the field at point are included. | |
6805 | ||
6806 | If nil, Emacs determines which part of the tail end of the | |
6807 | buffer's text is involved in completion by comparing the text | |
6808 | directly.") | |
d5e63715 | 6809 | (make-obsolete-variable 'completion-base-size 'completion-base-position "23.2") |
f6b293e3 | 6810 | |
1c6c6fde RS |
6811 | (defun delete-completion-window () |
6812 | "Delete the completion list window. | |
6813 | Go to the window from which completion was requested." | |
6814 | (interactive) | |
6815 | (let ((buf completion-reference-buffer)) | |
ddb2b181 | 6816 | (if (one-window-p t) |
290d5b58 | 6817 | (if (window-dedicated-p) (delete-frame)) |
ddb2b181 RS |
6818 | (delete-window (selected-window)) |
6819 | (if (get-buffer-window buf) | |
6820 | (select-window (get-buffer-window buf)))))) | |
1c6c6fde | 6821 | |
dde69dbe RS |
6822 | (defun previous-completion (n) |
6823 | "Move to the previous item in the completion list." | |
6824 | (interactive "p") | |
6825 | (next-completion (- n))) | |
6826 | ||
6827 | (defun next-completion (n) | |
6828 | "Move to the next item in the completion list. | |
1f238ac2 | 6829 | With prefix argument N, move N items (negative N means move backward)." |
dde69dbe | 6830 | (interactive "p") |
58dd38f1 SM |
6831 | (let ((beg (point-min)) (end (point-max))) |
6832 | (while (and (> n 0) (not (eobp))) | |
dde69dbe | 6833 | ;; If in a completion, move to the end of it. |
58dd38f1 SM |
6834 | (when (get-text-property (point) 'mouse-face) |
6835 | (goto-char (next-single-property-change (point) 'mouse-face nil end))) | |
dde69dbe | 6836 | ;; Move to start of next one. |
58dd38f1 SM |
6837 | (unless (get-text-property (point) 'mouse-face) |
6838 | (goto-char (next-single-property-change (point) 'mouse-face nil end))) | |
6839 | (setq n (1- n))) | |
6840 | (while (and (< n 0) (not (bobp))) | |
6841 | (let ((prop (get-text-property (1- (point)) 'mouse-face))) | |
6842 | ;; If in a completion, move to the start of it. | |
6843 | (when (and prop (eq prop (get-text-property (point) 'mouse-face))) | |
b61a81c2 | 6844 | (goto-char (previous-single-property-change |
58dd38f1 SM |
6845 | (point) 'mouse-face nil beg))) |
6846 | ;; Move to end of the previous completion. | |
6847 | (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face)) | |
6848 | (goto-char (previous-single-property-change | |
6849 | (point) 'mouse-face nil beg))) | |
6850 | ;; Move to the start of that one. | |
6851 | (goto-char (previous-single-property-change | |
6852 | (point) 'mouse-face nil beg)) | |
6853 | (setq n (1+ n)))))) | |
dde69dbe | 6854 | |
d5e63715 SM |
6855 | (defun choose-completion (&optional event) |
6856 | "Choose the completion at point." | |
6857 | (interactive (list last-nonmenu-event)) | |
6858 | ;; In case this is run via the mouse, give temporary modes such as | |
6859 | ;; isearch a chance to turn off. | |
6860 | (run-hooks 'mouse-leave-buffer-hook) | |
a2a25d24 SM |
6861 | (with-current-buffer (window-buffer (posn-window (event-start event))) |
6862 | (let ((buffer completion-reference-buffer) | |
6863 | (base-size completion-base-size) | |
6864 | (base-position completion-base-position) | |
6865 | (insert-function completion-list-insert-choice-function) | |
6866 | (choice | |
6867 | (save-excursion | |
6868 | (goto-char (posn-point (event-start event))) | |
6869 | (let (beg end) | |
6870 | (cond | |
6871 | ((and (not (eobp)) (get-text-property (point) 'mouse-face)) | |
6872 | (setq end (point) beg (1+ (point)))) | |
6873 | ((and (not (bobp)) | |
6874 | (get-text-property (1- (point)) 'mouse-face)) | |
6875 | (setq end (1- (point)) beg (point))) | |
6876 | (t (error "No completion here"))) | |
6877 | (setq beg (previous-single-property-change beg 'mouse-face)) | |
6878 | (setq end (or (next-single-property-change end 'mouse-face) | |
6879 | (point-max))) | |
bd56924f | 6880 | (buffer-substring-no-properties beg end))))) |
a2a25d24 SM |
6881 | |
6882 | (unless (buffer-live-p buffer) | |
6883 | (error "Destination buffer is dead")) | |
69d565e2 | 6884 | (quit-window nil (posn-window (event-start event))) |
a2a25d24 SM |
6885 | |
6886 | (with-current-buffer buffer | |
6887 | (choose-completion-string | |
6888 | choice buffer | |
6889 | (or base-position | |
6890 | (when base-size | |
6891 | ;; Someone's using old completion code that doesn't know | |
6892 | ;; about base-position yet. | |
6893 | (list (+ base-size (field-beginning)))) | |
6894 | ;; If all else fails, just guess. | |
6895 | (list (choose-completion-guess-base-position choice))) | |
6896 | insert-function))))) | |
80298193 RS |
6897 | |
6898 | ;; Delete the longest partial match for STRING | |
6899 | ;; that can be found before POINT. | |
d5e63715 SM |
6900 | (defun choose-completion-guess-base-position (string) |
6901 | (save-excursion | |
6902 | (let ((opoint (point)) | |
6903 | len) | |
6904 | ;; Try moving back by the length of the string. | |
6905 | (goto-char (max (- (point) (length string)) | |
6906 | (minibuffer-prompt-end))) | |
6907 | ;; See how far back we were actually able to move. That is the | |
6908 | ;; upper bound on how much we can match and delete. | |
6909 | (setq len (- opoint (point))) | |
6910 | (if completion-ignore-case | |
6911 | (setq string (downcase string))) | |
6912 | (while (and (> len 0) | |
6913 | (let ((tail (buffer-substring (point) opoint))) | |
6914 | (if completion-ignore-case | |
6915 | (setq tail (downcase tail))) | |
6916 | (not (string= tail (substring string 0 len))))) | |
6917 | (setq len (1- len)) | |
6918 | (forward-char 1)) | |
6919 | (point)))) | |
6920 | ||
80298193 | 6921 | (defun choose-completion-delete-max-match (string) |
59f7af81 | 6922 | (declare (obsolete choose-completion-guess-base-position "23.2")) |
d5e63715 | 6923 | (delete-region (choose-completion-guess-base-position string) (point))) |
80298193 | 6924 | |
ba36181b | 6925 | (defvar choose-completion-string-functions nil |
bbbbb15b KS |
6926 | "Functions that may override the normal insertion of a completion choice. |
6927 | These functions are called in order with four arguments: | |
6928 | CHOICE - the string to insert in the buffer, | |
6929 | BUFFER - the buffer in which the choice should be inserted, | |
4837b516 | 6930 | MINI-P - non-nil if BUFFER is a minibuffer, and |
12829a07 RS |
6931 | BASE-SIZE - the number of characters in BUFFER before |
6932 | the string being completed. | |
6933 | ||
bbbbb15b KS |
6934 | If a function in the list returns non-nil, that function is supposed |
6935 | to have inserted the CHOICE in the BUFFER, and possibly exited | |
12829a07 | 6936 | the minibuffer; no further functions will be called. |
ba36181b | 6937 | |
12829a07 RS |
6938 | If all functions in the list return nil, that means to use |
6939 | the default method of inserting the completion in BUFFER.") | |
74d0290b | 6940 | |
a2a25d24 SM |
6941 | (defun choose-completion-string (choice &optional |
6942 | buffer base-position insert-function) | |
12829a07 | 6943 | "Switch to BUFFER and insert the completion choice CHOICE. |
b39792eb BG |
6944 | BASE-POSITION says where to insert the completion. |
6945 | INSERT-FUNCTION says how to insert the completion and falls | |
6946 | back on `completion-list-insert-choice-function' when nil." | |
12829a07 RS |
6947 | |
6948 | ;; If BUFFER is the minibuffer, exit the minibuffer | |
6949 | ;; unless it is reading a file name and CHOICE is a directory, | |
6950 | ;; or completion-no-auto-exit is non-nil. | |
6951 | ||
d5e63715 SM |
6952 | ;; Some older code may call us passing `base-size' instead of |
6953 | ;; `base-position'. It's difficult to make any use of `base-size', | |
6954 | ;; so we just ignore it. | |
6955 | (unless (consp base-position) | |
6956 | (message "Obsolete `base-size' passed to choose-completion-string") | |
6957 | (setq base-position nil)) | |
6958 | ||
1a0d0b6a JPW |
6959 | (let* ((buffer (or buffer completion-reference-buffer)) |
6960 | (mini-p (minibufferp buffer))) | |
cf52ad58 RS |
6961 | ;; If BUFFER is a minibuffer, barf unless it's the currently |
6962 | ;; active minibuffer. | |
f436a90a | 6963 | (if (and mini-p |
a2a25d24 SM |
6964 | (not (and (active-minibuffer-window) |
6965 | (equal buffer | |
45486731 | 6966 | (window-buffer (active-minibuffer-window)))))) |
cf52ad58 | 6967 | (error "Minibuffer is not active for completion") |
17aa3385 KS |
6968 | ;; Set buffer so buffer-local choose-completion-string-functions works. |
6969 | (set-buffer buffer) | |
f1180544 | 6970 | (unless (run-hook-with-args-until-success |
d99f8496 | 6971 | 'choose-completion-string-functions |
d5e63715 SM |
6972 | ;; The fourth arg used to be `mini-p' but was useless |
6973 | ;; (since minibufferp can be used on the `buffer' arg) | |
6974 | ;; and indeed unused. The last used to be `base-size', so we | |
6975 | ;; keep it to try and avoid breaking old code. | |
6976 | choice buffer base-position nil) | |
a2a25d24 SM |
6977 | ;; This remove-text-properties should be unnecessary since `choice' |
6978 | ;; comes from buffer-substring-no-properties. | |
c80e3b4a | 6979 | ;;(remove-text-properties 0 (length choice) '(mouse-face nil) choice) |
d99f8496 | 6980 | ;; Insert the completion into the buffer where it was requested. |
a2a25d24 SM |
6981 | (funcall (or insert-function completion-list-insert-choice-function) |
6982 | (or (car base-position) (point)) | |
6983 | (or (cadr base-position) (point)) | |
6984 | choice) | |
6985 | ;; Update point in the window that BUFFER is showing in. | |
bbbbb15b KS |
6986 | (let ((window (get-buffer-window buffer t))) |
6987 | (set-window-point window (point))) | |
6988 | ;; If completing for the minibuffer, exit it with this choice. | |
6989 | (and (not completion-no-auto-exit) | |
6138158d | 6990 | (minibufferp buffer) |
bbbbb15b KS |
6991 | minibuffer-completion-table |
6992 | ;; If this is reading a file name, and the file name chosen | |
6993 | ;; is a directory, don't exit the minibuffer. | |
85be9ec4 SM |
6994 | (let* ((result (buffer-substring (field-beginning) (point))) |
6995 | (bounds | |
6996 | (completion-boundaries result minibuffer-completion-table | |
6997 | minibuffer-completion-predicate | |
6998 | ""))) | |
6999 | (if (eq (car bounds) (length result)) | |
7000 | ;; The completion chosen leads to a new set of completions | |
7001 | ;; (e.g. it's a directory): don't exit the minibuffer yet. | |
7002 | (let ((mini (active-minibuffer-window))) | |
7003 | (select-window mini) | |
7004 | (when minibuffer-auto-raise | |
7005 | (raise-frame (window-frame mini)))) | |
7006 | (exit-minibuffer)))))))) | |
80298193 | 7007 | |
e2947429 | 7008 | (define-derived-mode completion-list-mode nil "Completion List" |
e8a700bf | 7009 | "Major mode for buffers showing lists of possible completions. |
80298193 RS |
7010 | Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\ |
7011 | to select the completion near point. | |
7012 | Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\ | |
3a77346c GM |
7013 | with the mouse. |
7014 | ||
7015 | \\{completion-list-mode-map}" | |
e2947429 | 7016 | (set (make-local-variable 'completion-base-size) nil)) |
e8a700bf | 7017 | |
c8d6d636 GM |
7018 | (defun completion-list-mode-finish () |
7019 | "Finish setup of the completions buffer. | |
7020 | Called from `temp-buffer-show-hook'." | |
7021 | (when (eq major-mode 'completion-list-mode) | |
1c9bd870 | 7022 | (setq buffer-read-only t))) |
c8d6d636 GM |
7023 | |
7024 | (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish) | |
7025 | ||
f5fab556 MY |
7026 | |
7027 | ;; Variables and faces used in `completion-setup-function'. | |
747a0e2f | 7028 | |
d0fd0916 JPW |
7029 | (defcustom completion-show-help t |
7030 | "Non-nil means show help message in *Completions* buffer." | |
7031 | :type 'boolean | |
7032 | :version "22.1" | |
7033 | :group 'completion) | |
7034 | ||
f5fab556 MY |
7035 | ;; This function goes in completion-setup-hook, so that it is called |
7036 | ;; after the text of the completion list buffer is written. | |
e8a700bf | 7037 | (defun completion-setup-function () |
1b5fd09e | 7038 | (let* ((mainbuf (current-buffer)) |
a9e3ff69 | 7039 | (base-dir |
30c7e542 SM |
7040 | ;; FIXME: This is a bad hack. We try to set the default-directory |
7041 | ;; in the *Completions* buffer so that the relative file names | |
7042 | ;; displayed there can be treated as valid file names, independently | |
7043 | ;; from the completion context. But this suffers from many problems: | |
7044 | ;; - It's not clear when the completions are file names. With some | |
7045 | ;; completion tables (e.g. bzr revision specs), the listed | |
7046 | ;; completions can mix file names and other things. | |
7047 | ;; - It doesn't pay attention to possible quoting. | |
7048 | ;; - With fancy completion styles, the code below will not always | |
7049 | ;; find the right base directory. | |
a9e3ff69 SM |
7050 | (if minibuffer-completing-file-name |
7051 | (file-name-as-directory | |
7052 | (expand-file-name | |
30c7e542 SM |
7053 | (buffer-substring (minibuffer-prompt-end) |
7054 | (- (point) (or completion-base-size 0)))))))) | |
621a3f62 | 7055 | (with-current-buffer standard-output |
d5e63715 | 7056 | (let ((base-size completion-base-size) ;Read before killing localvars. |
a2a25d24 SM |
7057 | (base-position completion-base-position) |
7058 | (insert-fun completion-list-insert-choice-function)) | |
e2947429 | 7059 | (completion-list-mode) |
d5e63715 | 7060 | (set (make-local-variable 'completion-base-size) base-size) |
a2a25d24 SM |
7061 | (set (make-local-variable 'completion-base-position) base-position) |
7062 | (set (make-local-variable 'completion-list-insert-choice-function) | |
7063 | insert-fun)) | |
1b5fd09e | 7064 | (set (make-local-variable 'completion-reference-buffer) mainbuf) |
a9e3ff69 | 7065 | (if base-dir (setq default-directory base-dir)) |
d0fd0916 JPW |
7066 | ;; Maybe insert help string. |
7067 | (when completion-show-help | |
7068 | (goto-char (point-min)) | |
7069 | (if (display-mouse-p) | |
7070 | (insert (substitute-command-keys | |
7071 | "Click \\[mouse-choose-completion] on a completion to select it.\n"))) | |
7072 | (insert (substitute-command-keys | |
7073 | "In this buffer, type \\[choose-completion] to \ | |
7074 | select the completion near point.\n\n")))))) | |
c88ab9ce | 7075 | |
e8a700bf | 7076 | (add-hook 'completion-setup-hook 'completion-setup-function) |
dde69dbe | 7077 | |
1b5fd09e SM |
7078 | (define-key minibuffer-local-completion-map [prior] 'switch-to-completions) |
7079 | (define-key minibuffer-local-completion-map "\M-v" 'switch-to-completions) | |
dde69dbe RS |
7080 | |
7081 | (defun switch-to-completions () | |
7082 | "Select the completion list window." | |
7083 | (interactive) | |
ab14d7d5 | 7084 | (let ((window (or (get-buffer-window "*Completions*" 0) |
042b7cc6 | 7085 | ;; Make sure we have a completions window. |
ab14d7d5 SM |
7086 | (progn (minibuffer-completion-help) |
7087 | (get-buffer-window "*Completions*" 0))))) | |
fdbd7c4d KH |
7088 | (when window |
7089 | (select-window window) | |
042b7cc6 JL |
7090 | ;; In the new buffer, go to the first completion. |
7091 | ;; FIXME: Perhaps this should be done in `minibuffer-completion-help'. | |
7092 | (when (bobp) | |
7093 | (next-completion 1))))) | |
f6039de6 JL |
7094 | \f |
7095 | ;;; Support keyboard commands to turn on various modifiers. | |
82072f33 RS |
7096 | |
7097 | ;; These functions -- which are not commands -- each add one modifier | |
7098 | ;; to the following event. | |
7099 | ||
06b60517 | 7100 | (defun event-apply-alt-modifier (_ignore-prompt) |
1e96c007 | 7101 | "\\<function-key-map>Add the Alt modifier to the following event. |
70cf9f08 | 7102 | For example, type \\[event-apply-alt-modifier] & to enter Alt-&." |
82072f33 | 7103 | (vector (event-apply-modifier (read-event) 'alt 22 "A-"))) |
06b60517 | 7104 | (defun event-apply-super-modifier (_ignore-prompt) |
1e96c007 | 7105 | "\\<function-key-map>Add the Super modifier to the following event. |
70cf9f08 | 7106 | For example, type \\[event-apply-super-modifier] & to enter Super-&." |
82072f33 | 7107 | (vector (event-apply-modifier (read-event) 'super 23 "s-"))) |
06b60517 | 7108 | (defun event-apply-hyper-modifier (_ignore-prompt) |
1e96c007 | 7109 | "\\<function-key-map>Add the Hyper modifier to the following event. |
70cf9f08 | 7110 | For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&." |
82072f33 | 7111 | (vector (event-apply-modifier (read-event) 'hyper 24 "H-"))) |
06b60517 | 7112 | (defun event-apply-shift-modifier (_ignore-prompt) |
1e96c007 | 7113 | "\\<function-key-map>Add the Shift modifier to the following event. |
70cf9f08 | 7114 | For example, type \\[event-apply-shift-modifier] & to enter Shift-&." |
82072f33 | 7115 | (vector (event-apply-modifier (read-event) 'shift 25 "S-"))) |
06b60517 | 7116 | (defun event-apply-control-modifier (_ignore-prompt) |
1e96c007 | 7117 | "\\<function-key-map>Add the Ctrl modifier to the following event. |
70cf9f08 | 7118 | For example, type \\[event-apply-control-modifier] & to enter Ctrl-&." |
82072f33 | 7119 | (vector (event-apply-modifier (read-event) 'control 26 "C-"))) |
06b60517 | 7120 | (defun event-apply-meta-modifier (_ignore-prompt) |
1e96c007 | 7121 | "\\<function-key-map>Add the Meta modifier to the following event. |
70cf9f08 | 7122 | For example, type \\[event-apply-meta-modifier] & to enter Meta-&." |
82072f33 RS |
7123 | (vector (event-apply-modifier (read-event) 'meta 27 "M-"))) |
7124 | ||
7125 | (defun event-apply-modifier (event symbol lshiftby prefix) | |
7126 | "Apply a modifier flag to event EVENT. | |
7127 | SYMBOL is the name of this modifier, as a symbol. | |
7128 | LSHIFTBY is the numeric value of this modifier, in keyboard events. | |
7129 | PREFIX is the string that represents this modifier in an event type symbol." | |
7130 | (if (numberp event) | |
7131 | (cond ((eq symbol 'control) | |
90bebcb0 KH |
7132 | (if (and (<= (downcase event) ?z) |
7133 | (>= (downcase event) ?a)) | |
82072f33 | 7134 | (- (downcase event) ?a -1) |
90bebcb0 KH |
7135 | (if (and (<= (downcase event) ?Z) |
7136 | (>= (downcase event) ?A)) | |
82072f33 RS |
7137 | (- (downcase event) ?A -1) |
7138 | (logior (lsh 1 lshiftby) event)))) | |
7139 | ((eq symbol 'shift) | |
7140 | (if (and (<= (downcase event) ?z) | |
7141 | (>= (downcase event) ?a)) | |
7142 | (upcase event) | |
7143 | (logior (lsh 1 lshiftby) event))) | |
7144 | (t | |
7145 | (logior (lsh 1 lshiftby) event))) | |
7146 | (if (memq symbol (event-modifiers event)) | |
7147 | event | |
7148 | (let ((event-type (if (symbolp event) event (car event)))) | |
7149 | (setq event-type (intern (concat prefix (symbol-name event-type)))) | |
7150 | (if (symbolp event) | |
7151 | event-type | |
7152 | (cons event-type (cdr event))))))) | |
7153 | ||
e5fff738 KH |
7154 | (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier) |
7155 | (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier) | |
7156 | (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier) | |
7157 | (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier) | |
7158 | (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier) | |
7159 | (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier) | |
f6039de6 | 7160 | \f |
a3d1480b JB |
7161 | ;;;; Keypad support. |
7162 | ||
9b77469a SM |
7163 | ;; Make the keypad keys act like ordinary typing keys. If people add |
7164 | ;; bindings for the function key symbols, then those bindings will | |
7165 | ;; override these, so this shouldn't interfere with any existing | |
7166 | ;; bindings. | |
a3d1480b | 7167 | |
0d173134 | 7168 | ;; Also tell read-char how to handle these keys. |
e1e04350 | 7169 | (mapc |
a3d1480b JB |
7170 | (lambda (keypad-normal) |
7171 | (let ((keypad (nth 0 keypad-normal)) | |
7172 | (normal (nth 1 keypad-normal))) | |
0d173134 | 7173 | (put keypad 'ascii-character normal) |
a3d1480b | 7174 | (define-key function-key-map (vector keypad) (vector normal)))) |
4a9c6865 JL |
7175 | ;; See also kp-keys bound in bindings.el. |
7176 | '((kp-space ?\s) | |
a3d1480b JB |
7177 | (kp-tab ?\t) |
7178 | (kp-enter ?\r) | |
a3d1480b | 7179 | (kp-separator ?,) |
31cd2dd4 SM |
7180 | (kp-equal ?=) |
7181 | ;; Do the same for various keys that are represented as symbols under | |
7182 | ;; GUIs but naturally correspond to characters. | |
7183 | (backspace 127) | |
7184 | (delete 127) | |
7185 | (tab ?\t) | |
7186 | (linefeed ?\n) | |
7187 | (clear ?\C-l) | |
7188 | (return ?\C-m) | |
7189 | (escape ?\e) | |
7190 | )) | |
f54b0d85 | 7191 | \f |
1e722f9f | 7192 | ;;;; |
b005abd5 | 7193 | ;;;; forking a twin copy of a buffer. |
1e722f9f | 7194 | ;;;; |
b005abd5 SM |
7195 | |
7196 | (defvar clone-buffer-hook nil | |
7197 | "Normal hook to run in the new buffer at the end of `clone-buffer'.") | |
7198 | ||
64663f06 SM |
7199 | (defvar clone-indirect-buffer-hook nil |
7200 | "Normal hook to run in the new buffer at the end of `clone-indirect-buffer'.") | |
7201 | ||
b005abd5 SM |
7202 | (defun clone-process (process &optional newname) |
7203 | "Create a twin copy of PROCESS. | |
7204 | If NEWNAME is nil, it defaults to PROCESS' name; | |
7205 | NEWNAME is modified by adding or incrementing <N> at the end as necessary. | |
7206 | If PROCESS is associated with a buffer, the new process will be associated | |
7207 | with the current buffer instead. | |
7208 | Returns nil if PROCESS has already terminated." | |
7209 | (setq newname (or newname (process-name process))) | |
7210 | (if (string-match "<[0-9]+>\\'" newname) | |
7211 | (setq newname (substring newname 0 (match-beginning 0)))) | |
7212 | (when (memq (process-status process) '(run stop open)) | |
7213 | (let* ((process-connection-type (process-tty-name process)) | |
b005abd5 SM |
7214 | (new-process |
7215 | (if (memq (process-status process) '(open)) | |
ed7069af KS |
7216 | (let ((args (process-contact process t))) |
7217 | (setq args (plist-put args :name newname)) | |
7218 | (setq args (plist-put args :buffer | |
403ca8d9 KS |
7219 | (if (process-buffer process) |
7220 | (current-buffer)))) | |
ed7069af | 7221 | (apply 'make-network-process args)) |
b005abd5 SM |
7222 | (apply 'start-process newname |
7223 | (if (process-buffer process) (current-buffer)) | |
7224 | (process-command process))))) | |
ed7069af KS |
7225 | (set-process-query-on-exit-flag |
7226 | new-process (process-query-on-exit-flag process)) | |
b005abd5 SM |
7227 | (set-process-inherit-coding-system-flag |
7228 | new-process (process-inherit-coding-system-flag process)) | |
7229 | (set-process-filter new-process (process-filter process)) | |
7230 | (set-process-sentinel new-process (process-sentinel process)) | |
403ca8d9 | 7231 | (set-process-plist new-process (copy-sequence (process-plist process))) |
b005abd5 SM |
7232 | new-process))) |
7233 | ||
b75b82ab | 7234 | ;; things to maybe add (currently partly covered by `funcall mode'): |
b005abd5 SM |
7235 | ;; - syntax-table |
7236 | ;; - overlays | |
7237 | (defun clone-buffer (&optional newname display-flag) | |
6b61353c KH |
7238 | "Create and return a twin copy of the current buffer. |
7239 | Unlike an indirect buffer, the new buffer can be edited | |
7240 | independently of the old one (if it is not read-only). | |
7241 | NEWNAME is the name of the new buffer. It may be modified by | |
7242 | adding or incrementing <N> at the end as necessary to create a | |
7243 | unique buffer name. If nil, it defaults to the name of the | |
7244 | current buffer, with the proper suffix. If DISPLAY-FLAG is | |
7245 | non-nil, the new buffer is shown with `pop-to-buffer'. Trying to | |
7246 | clone a file-visiting buffer, or a buffer whose major mode symbol | |
7247 | has a non-nil `no-clone' property, results in an error. | |
7248 | ||
7249 | Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the | |
7250 | current buffer with appropriate suffix. However, if a prefix | |
7251 | argument is given, then the command prompts for NEWNAME in the | |
7252 | minibuffer. | |
b005abd5 | 7253 | |
b005abd5 SM |
7254 | This runs the normal hook `clone-buffer-hook' in the new buffer |
7255 | after it has been set up properly in other respects." | |
61acfe7f RS |
7256 | (interactive |
7257 | (progn | |
7258 | (if buffer-file-name | |
7259 | (error "Cannot clone a file-visiting buffer")) | |
7260 | (if (get major-mode 'no-clone) | |
7261 | (error "Cannot clone a buffer in %s mode" mode-name)) | |
f6039de6 JL |
7262 | (list (if current-prefix-arg |
7263 | (read-buffer "Name of new cloned buffer: " (current-buffer))) | |
61acfe7f | 7264 | t))) |
b005abd5 SM |
7265 | (if buffer-file-name |
7266 | (error "Cannot clone a file-visiting buffer")) | |
7267 | (if (get major-mode 'no-clone) | |
7268 | (error "Cannot clone a buffer in %s mode" mode-name)) | |
7269 | (setq newname (or newname (buffer-name))) | |
7270 | (if (string-match "<[0-9]+>\\'" newname) | |
7271 | (setq newname (substring newname 0 (match-beginning 0)))) | |
7272 | (let ((buf (current-buffer)) | |
7273 | (ptmin (point-min)) | |
7274 | (ptmax (point-max)) | |
7275 | (pt (point)) | |
7276 | (mk (if mark-active (mark t))) | |
7277 | (modified (buffer-modified-p)) | |
7278 | (mode major-mode) | |
7279 | (lvars (buffer-local-variables)) | |
7280 | (process (get-buffer-process (current-buffer))) | |
7281 | (new (generate-new-buffer (or newname (buffer-name))))) | |
7282 | (save-restriction | |
7283 | (widen) | |
7284 | (with-current-buffer new | |
7285 | (insert-buffer-substring buf))) | |
7286 | (with-current-buffer new | |
7287 | (narrow-to-region ptmin ptmax) | |
7288 | (goto-char pt) | |
7289 | (if mk (set-mark mk)) | |
7290 | (set-buffer-modified-p modified) | |
7291 | ||
7292 | ;; Clone the old buffer's process, if any. | |
7293 | (when process (clone-process process)) | |
7294 | ||
7295 | ;; Now set up the major mode. | |
7296 | (funcall mode) | |
7297 | ||
7298 | ;; Set up other local variables. | |
9ca2204b JB |
7299 | (mapc (lambda (v) |
7300 | (condition-case () ;in case var is read-only | |
7301 | (if (symbolp v) | |
7302 | (makunbound v) | |
7303 | (set (make-local-variable (car v)) (cdr v))) | |
7304 | (error nil))) | |
7305 | lvars) | |
b005abd5 SM |
7306 | |
7307 | ;; Run any hooks (typically set up by the major mode | |
7308 | ;; for cloning to work properly). | |
7309 | (run-hooks 'clone-buffer-hook)) | |
0a487199 SM |
7310 | (if display-flag |
7311 | ;; Presumably the current buffer is shown in the selected frame, so | |
7312 | ;; we want to display the clone elsewhere. | |
7313 | (let ((same-window-regexps nil) | |
7314 | (same-window-buffer-names)) | |
7315 | (pop-to-buffer new))) | |
b005abd5 SM |
7316 | new)) |
7317 | ||
fa65f20b | 7318 | |
7e3afb04 | 7319 | (defun clone-indirect-buffer (newname display-flag &optional norecord) |
fa65f20b GM |
7320 | "Create an indirect buffer that is a twin copy of the current buffer. |
7321 | ||
01ba9662 | 7322 | Give the indirect buffer name NEWNAME. Interactively, read NEWNAME |
fa65f20b GM |
7323 | from the minibuffer when invoked with a prefix arg. If NEWNAME is nil |
7324 | or if not called with a prefix arg, NEWNAME defaults to the current | |
7325 | buffer's name. The name is modified by adding a `<N>' suffix to it | |
1d2b0303 JB |
7326 | or by incrementing the N in an existing suffix. Trying to clone a |
7327 | buffer whose major mode symbol has a non-nil `no-clone-indirect' | |
7328 | property results in an error. | |
fa65f20b GM |
7329 | |
7330 | DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'. | |
7e3afb04 GM |
7331 | This is always done when called interactively. |
7332 | ||
f33321ad | 7333 | Optional third arg NORECORD non-nil means do not put this buffer at the |
7e3afb04 | 7334 | front of the list of recently selected ones." |
61acfe7f RS |
7335 | (interactive |
7336 | (progn | |
7337 | (if (get major-mode 'no-clone-indirect) | |
7338 | (error "Cannot indirectly clone a buffer in %s mode" mode-name)) | |
7339 | (list (if current-prefix-arg | |
f6039de6 | 7340 | (read-buffer "Name of indirect buffer: " (current-buffer))) |
61acfe7f RS |
7341 | t))) |
7342 | (if (get major-mode 'no-clone-indirect) | |
7343 | (error "Cannot indirectly clone a buffer in %s mode" mode-name)) | |
fa65f20b GM |
7344 | (setq newname (or newname (buffer-name))) |
7345 | (if (string-match "<[0-9]+>\\'" newname) | |
7346 | (setq newname (substring newname 0 (match-beginning 0)))) | |
7347 | (let* ((name (generate-new-buffer-name newname)) | |
7348 | (buffer (make-indirect-buffer (current-buffer) name t))) | |
64663f06 SM |
7349 | (with-current-buffer buffer |
7350 | (run-hooks 'clone-indirect-buffer-hook)) | |
fa65f20b | 7351 | (when display-flag |
58dd38f1 | 7352 | (pop-to-buffer buffer norecord)) |
fa65f20b GM |
7353 | buffer)) |
7354 | ||
7355 | ||
1fffd65f RS |
7356 | (defun clone-indirect-buffer-other-window (newname display-flag &optional norecord) |
7357 | "Like `clone-indirect-buffer' but display in another window." | |
2ef0a47e RS |
7358 | (interactive |
7359 | (progn | |
7360 | (if (get major-mode 'no-clone-indirect) | |
7361 | (error "Cannot indirectly clone a buffer in %s mode" mode-name)) | |
7362 | (list (if current-prefix-arg | |
f6039de6 | 7363 | (read-buffer "Name of indirect buffer: " (current-buffer))) |
2ef0a47e | 7364 | t))) |
acd39eb6 | 7365 | (let ((pop-up-windows t)) |
1fffd65f | 7366 | (clone-indirect-buffer newname display-flag norecord))) |
7e3afb04 | 7367 | |
f54b0d85 | 7368 | \f |
1d4b11bf GM |
7369 | ;;; Handling of Backspace and Delete keys. |
7370 | ||
30a2fded | 7371 | (defcustom normal-erase-is-backspace 'maybe |
3784ec80 | 7372 | "Set the default behavior of the Delete and Backspace keys. |
30a2fded KL |
7373 | |
7374 | If set to t, Delete key deletes forward and Backspace key deletes | |
7375 | backward. | |
7376 | ||
7377 | If set to nil, both Delete and Backspace keys delete backward. | |
7378 | ||
7379 | If set to 'maybe (which is the default), Emacs automatically | |
3784ec80 | 7380 | selects a behavior. On window systems, the behavior depends on |
30a2fded KL |
7381 | the keyboard used. If the keyboard has both a Backspace key and |
7382 | a Delete key, and both are mapped to their usual meanings, the | |
7383 | option's default value is set to t, so that Backspace can be used | |
7384 | to delete backward, and Delete can be used to delete forward. | |
7385 | ||
7386 | If not running under a window system, customizing this option | |
7387 | accomplishes a similar effect by mapping C-h, which is usually | |
7388 | generated by the Backspace key, to DEL, and by mapping DEL to C-d | |
7389 | via `keyboard-translate'. The former functionality of C-h is | |
7390 | available on the F1 key. You should probably not use this | |
7391 | setting if you don't have both Backspace, Delete and F1 keys. | |
f060b834 GM |
7392 | |
7393 | Setting this variable with setq doesn't take effect. Programmatically, | |
7f62656b | 7394 | call `normal-erase-is-backspace-mode' (which see) instead." |
30a2fded KL |
7395 | :type '(choice (const :tag "Off" nil) |
7396 | (const :tag "Maybe" maybe) | |
7397 | (other :tag "On" t)) | |
1d4b11bf GM |
7398 | :group 'editing-basics |
7399 | :version "21.1" | |
7400 | :set (lambda (symbol value) | |
7401 | ;; The fboundp is because of a problem with :set when | |
7402 | ;; dumping Emacs. It doesn't really matter. | |
7f62656b EZ |
7403 | (if (fboundp 'normal-erase-is-backspace-mode) |
7404 | (normal-erase-is-backspace-mode (or value 0)) | |
1d4b11bf GM |
7405 | (set-default symbol value)))) |
7406 | ||
30a2fded KL |
7407 | (defun normal-erase-is-backspace-setup-frame (&optional frame) |
7408 | "Set up `normal-erase-is-backspace-mode' on FRAME, if necessary." | |
7409 | (unless frame (setq frame (selected-frame))) | |
7410 | (with-selected-frame frame | |
ed8dad6b | 7411 | (unless (terminal-parameter nil 'normal-erase-is-backspace) |
08ea6d2f SM |
7412 | (normal-erase-is-backspace-mode |
7413 | (if (if (eq normal-erase-is-backspace 'maybe) | |
7414 | (and (not noninteractive) | |
7415 | (or (memq system-type '(ms-dos windows-nt)) | |
0fda9b75 | 7416 | (memq window-system '(w32 ns)) |
08ea6d2f SM |
7417 | (and (memq window-system '(x)) |
7418 | (fboundp 'x-backspace-delete-keys-p) | |
7419 | (x-backspace-delete-keys-p)) | |
7420 | ;; If the terminal Emacs is running on has erase char | |
7421 | ;; set to ^H, use the Backspace key for deleting | |
7422 | ;; backward, and the Delete key for deleting forward. | |
7423 | (and (null window-system) | |
7424 | (eq tty-erase-char ?\^H)))) | |
7425 | normal-erase-is-backspace) | |
7426 | 1 0))))) | |
1d4b11bf | 7427 | |
80ac5d4d | 7428 | (define-minor-mode normal-erase-is-backspace-mode |
7f62656b | 7429 | "Toggle the Erase and Delete mode of the Backspace and Delete keys. |
06e21633 CY |
7430 | With a prefix argument ARG, enable this feature if ARG is |
7431 | positive, and disable it otherwise. If called from Lisp, enable | |
7432 | the mode if ARG is omitted or nil. | |
7f62656b | 7433 | |
30a2fded KL |
7434 | On window systems, when this mode is on, Delete is mapped to C-d |
7435 | and Backspace is mapped to DEL; when this mode is off, both | |
7436 | Delete and Backspace are mapped to DEL. (The remapping goes via | |
7437 | `local-function-key-map', so binding Delete or Backspace in the | |
7438 | global or local keymap will override that.) | |
7f62656b EZ |
7439 | |
7440 | In addition, on window systems, the bindings of C-Delete, M-Delete, | |
7441 | C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in | |
7442 | the global keymap in accordance with the functionality of Delete and | |
7443 | Backspace. For example, if Delete is remapped to C-d, which deletes | |
7444 | forward, C-Delete is bound to `kill-word', but if Delete is remapped | |
7445 | to DEL, which deletes backward, C-Delete is bound to | |
7446 | `backward-kill-word'. | |
7447 | ||
7448 | If not running on a window system, a similar effect is accomplished by | |
7449 | remapping C-h (normally produced by the Backspace key) and DEL via | |
7450 | `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL | |
7451 | to C-d; if it's off, the keys are not remapped. | |
7452 | ||
7453 | When not running on a window system, and this mode is turned on, the | |
7454 | former functionality of C-h is available on the F1 key. You should | |
7455 | probably not turn on this mode on a text-only terminal if you don't | |
7456 | have both Backspace, Delete and F1 keys. | |
7457 | ||
7458 | See also `normal-erase-is-backspace'." | |
2ee3d7f0 SM |
7459 | :variable ((eq (terminal-parameter nil 'normal-erase-is-backspace) 1) |
7460 | . (lambda (v) | |
7461 | (setf (terminal-parameter nil 'normal-erase-is-backspace) | |
7462 | (if v 1 0)))) | |
80ac5d4d SM |
7463 | (let ((enabled (eq 1 (terminal-parameter |
7464 | nil 'normal-erase-is-backspace)))) | |
0103b7c9 | 7465 | |
9e2a2647 | 7466 | (cond ((or (memq window-system '(x w32 ns pc)) |
0103b7c9 | 7467 | (memq system-type '(ms-dos windows-nt))) |
06b60517 JB |
7468 | (let ((bindings |
7469 | `(([M-delete] [M-backspace]) | |
7470 | ([C-M-delete] [C-M-backspace]) | |
7471 | ([?\e C-delete] [?\e C-backspace])))) | |
0103b7c9 KL |
7472 | |
7473 | (if enabled | |
7474 | (progn | |
b8a47412 | 7475 | (define-key local-function-key-map [delete] [deletechar]) |
afdadaf7 | 7476 | (define-key local-function-key-map [kp-delete] [deletechar]) |
28a90c44 SM |
7477 | (define-key local-function-key-map [backspace] [?\C-?]) |
7478 | (dolist (b bindings) | |
7479 | ;; Not sure if input-decode-map is really right, but | |
7480 | ;; keyboard-translate-table (used below) only works | |
7481 | ;; for integer events, and key-translation-table is | |
7482 | ;; global (like the global-map, used earlier). | |
7483 | (define-key input-decode-map (car b) nil) | |
7484 | (define-key input-decode-map (cadr b) nil))) | |
0103b7c9 KL |
7485 | (define-key local-function-key-map [delete] [?\C-?]) |
7486 | (define-key local-function-key-map [kp-delete] [?\C-?]) | |
28a90c44 SM |
7487 | (define-key local-function-key-map [backspace] [?\C-?]) |
7488 | (dolist (b bindings) | |
7489 | (define-key input-decode-map (car b) (cadr b)) | |
7490 | (define-key input-decode-map (cadr b) (car b)))))) | |
0103b7c9 KL |
7491 | (t |
7492 | (if enabled | |
ec9f4754 | 7493 | (progn |
0103b7c9 KL |
7494 | (keyboard-translate ?\C-h ?\C-?) |
7495 | (keyboard-translate ?\C-? ?\C-d)) | |
7496 | (keyboard-translate ?\C-h ?\C-h) | |
7497 | (keyboard-translate ?\C-? ?\C-?)))) | |
7498 | ||
32226619 | 7499 | (if (called-interactively-p 'interactive) |
0103b7c9 | 7500 | (message "Delete key deletes %s" |
b08016f2 | 7501 | (if (eq 1 (terminal-parameter nil 'normal-erase-is-backspace)) |
0103b7c9 | 7502 | "forward" "backward"))))) |
ea82f0df | 7503 | \f |
aca8bee5 | 7504 | (defvar vis-mode-saved-buffer-invisibility-spec nil |
0f7df535 | 7505 | "Saved value of `buffer-invisibility-spec' when Visible mode is on.") |
7f62656b | 7506 | |
af070a1c SM |
7507 | (define-minor-mode read-only-mode |
7508 | "Change whether the current buffer is read-only. | |
7509 | With prefix argument ARG, make the buffer read-only if ARG is | |
7510 | positive, otherwise make it writable. If buffer is read-only | |
7511 | and `view-read-only' is non-nil, enter view mode. | |
7512 | ||
7513 | Do not call this from a Lisp program unless you really intend to | |
3171e303 | 7514 | do the same thing as the \\[read-only-mode] command, including |
af070a1c SM |
7515 | possibly enabling or disabling View mode. Also, note that this |
7516 | command works by setting the variable `buffer-read-only', which | |
7517 | does not affect read-only regions caused by text properties. To | |
7518 | ignore read-only status in a Lisp program (whether due to text | |
7519 | properties or buffer state), bind `inhibit-read-only' temporarily | |
7520 | to a non-nil value." | |
7521 | :variable buffer-read-only | |
7522 | (cond | |
7523 | ((and (not buffer-read-only) view-mode) | |
7524 | (View-exit-and-edit) | |
7525 | (make-local-variable 'view-read-only) | |
7526 | (setq view-read-only t)) ; Must leave view mode. | |
7527 | ((and buffer-read-only view-read-only | |
7528 | ;; If view-mode is already active, `view-mode-enter' is a nop. | |
7529 | (not view-mode) | |
7530 | (not (eq (get major-mode 'mode-class) 'special))) | |
7531 | (view-mode-enter)))) | |
7532 | ||
0f7df535 | 7533 | (define-minor-mode visible-mode |
06e21633 CY |
7534 | "Toggle making all invisible text temporarily visible (Visible mode). |
7535 | With a prefix argument ARG, enable Visible mode if ARG is | |
7536 | positive, and disable it otherwise. If called from Lisp, enable | |
7537 | the mode if ARG is omitted or nil. | |
1d4b11bf | 7538 | |
06e21633 CY |
7539 | This mode works by saving the value of `buffer-invisibility-spec' |
7540 | and setting it to nil." | |
4e57881d | 7541 | :lighter " Vis" |
ab77efd0 | 7542 | :group 'editing-basics |
aca8bee5 SM |
7543 | (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec) |
7544 | (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec) | |
7545 | (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec)) | |
0f7df535 | 7546 | (when visible-mode |
aca8bee5 SM |
7547 | (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec) |
7548 | buffer-invisibility-spec) | |
7549 | (setq buffer-invisibility-spec nil))) | |
4e57881d | 7550 | \f |
2b42da98 GM |
7551 | (defvar messages-buffer-mode-map |
7552 | (let ((map (make-sparse-keymap))) | |
7553 | (set-keymap-parent map special-mode-map) | |
7554 | (define-key map "g" nil) ; nothing to revert | |
7555 | map)) | |
7556 | ||
90582f05 GM |
7557 | (define-derived-mode messages-buffer-mode special-mode "Messages" |
7558 | "Major mode used in the \"*Messages*\" buffer.") | |
7559 | ||
7560 | (defun messages-buffer () | |
7561 | "Return the \"*Messages*\" buffer. | |
7562 | If it does not exist, create and it switch it to `messages-buffer-mode'." | |
7563 | (or (get-buffer "*Messages*") | |
7564 | (with-current-buffer (get-buffer-create "*Messages*") | |
7565 | (messages-buffer-mode) | |
7566 | (current-buffer)))) | |
7567 | ||
7568 | \f | |
e1e04350 | 7569 | ;; Minibuffer prompt stuff. |
9b350152 | 7570 | |
2ec42da9 SM |
7571 | ;;(defun minibuffer-prompt-modification (start end) |
7572 | ;; (error "You cannot modify the prompt")) | |
7573 | ;; | |
7574 | ;; | |
7575 | ;;(defun minibuffer-prompt-insertion (start end) | |
7576 | ;; (let ((inhibit-modification-hooks t)) | |
7577 | ;; (delete-region start end) | |
7578 | ;; ;; Discard undo information for the text insertion itself | |
7579 | ;; ;; and for the text deletion.above. | |
7580 | ;; (when (consp buffer-undo-list) | |
7581 | ;; (setq buffer-undo-list (cddr buffer-undo-list))) | |
7582 | ;; (message "You cannot modify the prompt"))) | |
7583 | ;; | |
7584 | ;; | |
7585 | ;;(setq minibuffer-prompt-properties | |
7586 | ;; (list 'modification-hooks '(minibuffer-prompt-modification) | |
7587 | ;; 'insert-in-front-hooks '(minibuffer-prompt-insertion))) | |
9b350152 | 7588 | |
a2603048 GM |
7589 | \f |
7590 | ;;;; Problematic external packages. | |
7591 | ||
7592 | ;; rms says this should be done by specifying symbols that define | |
7593 | ;; versions together with bad values. This is therefore not as | |
7594 | ;; flexible as it could be. See the thread: | |
7595 | ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00300.html | |
7596 | (defconst bad-packages-alist | |
7597 | ;; Not sure exactly which semantic versions have problems. | |
7598 | ;; Definitely 2.0pre3, probably all 2.0pre's before this. | |
7796ee61 | 7599 | '((semantic semantic-version "\\`2\\.0pre[1-3]\\'" |
a2603048 | 7600 | "The version of `semantic' loaded does not work in Emacs 22. |
72d595b5 GM |
7601 | It can cause constant high CPU load. |
7602 | Upgrade to at least Semantic 2.0pre4 (distributed with CEDET 1.0pre4).") | |
a2603048 GM |
7603 | ;; CUA-mode does not work with GNU Emacs version 22.1 and newer. |
7604 | ;; Except for version 1.2, all of the 1.x and 2.x version of cua-mode | |
7605 | ;; provided the `CUA-mode' feature. Since this is no longer true, | |
7606 | ;; we can warn the user if the `CUA-mode' feature is ever provided. | |
7607 | (CUA-mode t nil | |
7608 | "CUA-mode is now part of the standard GNU Emacs distribution, | |
7609 | so you can now enable CUA via the Options menu or by customizing `cua-mode'. | |
7610 | ||
7611 | You have loaded an older version of CUA-mode which does not work | |
7612 | correctly with this version of Emacs. You should remove the old | |
7613 | version and use the one distributed with Emacs.")) | |
7614 | "Alist of packages known to cause problems in this version of Emacs. | |
7615 | Each element has the form (PACKAGE SYMBOL REGEXP STRING). | |
7616 | PACKAGE is either a regular expression to match file names, or a | |
de0503df | 7617 | symbol (a feature name), like for `with-eval-after-load'. |
a2603048 GM |
7618 | SYMBOL is either the name of a string variable, or `t'. Upon |
7619 | loading PACKAGE, if SYMBOL is t or matches REGEXP, display a | |
7620 | warning using STRING as the message.") | |
7621 | ||
7622 | (defun bad-package-check (package) | |
7623 | "Run a check using the element from `bad-packages-alist' matching PACKAGE." | |
7624 | (condition-case nil | |
7625 | (let* ((list (assoc package bad-packages-alist)) | |
7626 | (symbol (nth 1 list))) | |
7627 | (and list | |
7628 | (boundp symbol) | |
7629 | (or (eq symbol t) | |
7630 | (and (stringp (setq symbol (eval symbol))) | |
9bc505ab JB |
7631 | (string-match-p (nth 2 list) symbol))) |
7632 | (display-warning package (nth 3 list) :warning))) | |
a2603048 GM |
7633 | (error nil))) |
7634 | ||
de0503df SM |
7635 | (dolist (elem bad-packages-alist) |
7636 | (let ((pkg (car elem))) | |
7637 | (with-eval-after-load pkg | |
7638 | (bad-package-check pkg)))) | |
a2603048 | 7639 | |
61e56e2c JB |
7640 | \f |
7641 | ;;; Generic dispatcher commands | |
7642 | ||
b43f6451 | 7643 | ;; Macro `define-alternatives' is used to create generic commands. |
61e56e2c JB |
7644 | ;; Generic commands are these (like web, mail, news, encrypt, irc, etc.) |
7645 | ;; that can have different alternative implementations where choosing | |
7646 | ;; among them is exclusively a matter of user preference. | |
7647 | ||
b43f6451 | 7648 | ;; (define-alternatives COMMAND) creates a new interactive command |
61e56e2c JB |
7649 | ;; M-x COMMAND and a customizable variable COMMAND-alternatives. |
7650 | ;; Typically, the user will not need to customize this variable; packages | |
7651 | ;; wanting to add alternative implementations should use | |
7652 | ;; | |
7653 | ;; ;;;###autoload (push '("My impl name" . my-impl-symbol) COMMAND-alternatives | |
7654 | ||
b43f6451 | 7655 | (defmacro define-alternatives (command &rest customizations) |
61e56e2c JB |
7656 | "Define new command `COMMAND'. |
7657 | The variable `COMMAND-alternatives' will contain alternative | |
7658 | implementations of COMMAND, so that running `C-u M-x COMMAND' | |
7659 | will allow the user to chose among them. | |
7660 | CUSTOMIZATIONS, if non-nil, should be composed of alternating | |
7661 | `defcustom' keywords and values to add to the declaration of | |
11e03d89 | 7662 | `COMMAND-alternatives' (typically :group and :version)." |
61e56e2c JB |
7663 | (let* ((command-name (symbol-name command)) |
7664 | (varalt-name (concat command-name "-alternatives")) | |
7665 | (varalt-sym (intern varalt-name)) | |
7666 | (varimp-sym (intern (concat command-name "--implementation")))) | |
7667 | `(progn | |
7668 | ||
7669 | (defcustom ,varalt-sym nil | |
7670 | ,(format "Alist of alternative implementations for the `%s' command. | |
7671 | ||
7672 | Each entry must be a pair (ALTNAME . ALTFUN), where: | |
7673 | ALTNAME - The name shown at user to describe the alternative implementation. | |
7674 | ALTFUN - The function called to implement this alternative." | |
7675 | command-name) | |
7676 | :type '(alist :key-type string :value-type function) | |
61e56e2c JB |
7677 | ,@customizations) |
7678 | ||
7679 | (defvar ,varimp-sym nil "Internal use only.") | |
7680 | ||
7681 | (defun ,command (&optional arg) | |
7682 | ,(format "Run generic command `%s'. | |
7683 | If used for the first time, or with interactive ARG, ask the user which | |
7684 | implementation to use for `%s'. The variable `%s' | |
7685 | contains the list of implementations currently supported for this command." | |
7686 | command-name command-name varalt-name) | |
7687 | (interactive "P") | |
7688 | (when (or arg (null ,varimp-sym)) | |
7689 | (let ((val (completing-read | |
7690 | ,(format "Select implementation for command `%s': " command-name) | |
7691 | ,varalt-sym nil t))) | |
7692 | (unless (string-equal val "") | |
7693 | (customize-save-variable ',varimp-sym | |
7694 | (cdr (assoc-string val ,varalt-sym)))))) | |
7695 | (if ,varimp-sym | |
7696 | (funcall ,varimp-sym) | |
7697 | (message ,(format "No implementation selected for command `%s'" | |
7698 | command-name))))))) | |
7699 | ||
00398e3b | 7700 | (provide 'simple) |
6b61353c | 7701 | |
c88ab9ce | 7702 | ;;; simple.el ends here |