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