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