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