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