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