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