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