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