Commit | Line | Data |
---|---|---|
c88ab9ce ER |
1 | ;;; simple.el --- basic editing commands for Emacs |
2 | ||
d5ab2033 | 3 | ;; Copyright (C) 1985, 1986, 1987, 1992, 1993 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 | ||
3a801d0c | 21 | ;;; Code: |
2076c87c JB |
22 | |
23 | (defun open-line (arg) | |
ff1fbe3e | 24 | "Insert a newline and leave point before it. |
616ed245 RS |
25 | If there is a fill prefix, insert the fill prefix on the new line |
26 | if the line would have been empty. | |
27 | With arg N, insert N newlines." | |
2076c87c | 28 | (interactive "*p") |
616ed245 RS |
29 | (let* ((do-fill-prefix (and fill-prefix (bolp))) |
30 | (flag (and (null do-fill-prefix) (bolp) (not (bobp))))) | |
62c48f87 RS |
31 | ;; If this is a simple case, and we are at the beginning of a line, |
32 | ;; actually insert the newline *before* the preceding newline | |
33 | ;; instead of after. That makes better display behavior. | |
34 | (if flag | |
35 | (progn | |
36 | ;; If undo is enabled, don't let this hack be visible: | |
37 | ;; record the real value of point as the place to move back to | |
38 | ;; if we undo this insert. | |
39 | (if (and buffer-undo-list (not (eq buffer-undo-list t))) | |
40 | (setq buffer-undo-list (cons (point) buffer-undo-list))) | |
41 | (forward-char -1))) | |
2076c87c JB |
42 | (while (> arg 0) |
43 | (save-excursion | |
616ed245 RS |
44 | (insert ?\n)) |
45 | (if do-fill-prefix (insert fill-prefix)) | |
2076c87c JB |
46 | (setq arg (1- arg))) |
47 | (if flag (forward-char 1)))) | |
48 | ||
49 | (defun split-line () | |
50 | "Split current line, moving portion beyond point vertically down." | |
51 | (interactive "*") | |
52 | (skip-chars-forward " \t") | |
53 | (let ((col (current-column)) | |
54 | (pos (point))) | |
55 | (insert ?\n) | |
56 | (indent-to col 0) | |
57 | (goto-char pos))) | |
58 | ||
59 | (defun quoted-insert (arg) | |
60 | "Read next input character and insert it. | |
ff1fbe3e | 61 | This is useful for inserting control characters. |
dbc4e1c1 JB |
62 | You may also type up to 3 octal digits, to insert a character with that code. |
63 | `quoted-insert' inserts the character even in overstrike mode; if you | |
64 | use overstrike as your normal editing mode, you can use this function | |
65 | to insert characters when necessary." | |
2076c87c JB |
66 | (interactive "*p") |
67 | (let ((char (read-quoted-char))) | |
68 | (while (> arg 0) | |
69 | (insert char) | |
70 | (setq arg (1- arg))))) | |
71 | ||
72 | (defun delete-indentation (&optional arg) | |
73 | "Join this line to previous and fix up whitespace at join. | |
ccc58657 | 74 | If there is a fill prefix, delete it from the beginning of this line. |
2076c87c JB |
75 | With argument, join this line to following line." |
76 | (interactive "*P") | |
77 | (beginning-of-line) | |
78 | (if arg (forward-line 1)) | |
79 | (if (eq (preceding-char) ?\n) | |
80 | (progn | |
81 | (delete-region (point) (1- (point))) | |
ccc58657 RS |
82 | ;; If the second line started with the fill prefix, |
83 | ;; delete the prefix. | |
84 | (if (and fill-prefix | |
01b8e020 | 85 | (<= (+ (point) (length fill-prefix)) (point-max)) |
ccc58657 RS |
86 | (string= fill-prefix |
87 | (buffer-substring (point) | |
88 | (+ (point) (length fill-prefix))))) | |
89 | (delete-region (point) (+ (point) (length fill-prefix)))) | |
2076c87c JB |
90 | (fixup-whitespace)))) |
91 | ||
92 | (defun fixup-whitespace () | |
93 | "Fixup white space between objects around point. | |
94 | Leave one space or none, according to the context." | |
95 | (interactive "*") | |
96 | (save-excursion | |
97 | (delete-horizontal-space) | |
98 | (if (or (looking-at "^\\|\\s)") | |
99 | (save-excursion (forward-char -1) | |
100 | (looking-at "$\\|\\s(\\|\\s'"))) | |
101 | nil | |
102 | (insert ?\ )))) | |
103 | ||
104 | (defun delete-horizontal-space () | |
105 | "Delete all spaces and tabs around point." | |
106 | (interactive "*") | |
107 | (skip-chars-backward " \t") | |
108 | (delete-region (point) (progn (skip-chars-forward " \t") (point)))) | |
109 | ||
110 | (defun just-one-space () | |
111 | "Delete all spaces and tabs around point, leaving one space." | |
112 | (interactive "*") | |
113 | (skip-chars-backward " \t") | |
114 | (if (= (following-char) ? ) | |
115 | (forward-char 1) | |
116 | (insert ? )) | |
117 | (delete-region (point) (progn (skip-chars-forward " \t") (point)))) | |
118 | ||
119 | (defun delete-blank-lines () | |
120 | "On blank line, delete all surrounding blank lines, leaving just one. | |
121 | On isolated blank line, delete that one. | |
122 | On nonblank line, delete all blank lines that follow it." | |
123 | (interactive "*") | |
124 | (let (thisblank singleblank) | |
125 | (save-excursion | |
126 | (beginning-of-line) | |
127 | (setq thisblank (looking-at "[ \t]*$")) | |
70e14c01 | 128 | ;; Set singleblank if there is just one blank line here. |
2076c87c JB |
129 | (setq singleblank |
130 | (and thisblank | |
131 | (not (looking-at "[ \t]*\n[ \t]*$")) | |
132 | (or (bobp) | |
133 | (progn (forward-line -1) | |
134 | (not (looking-at "[ \t]*$"))))))) | |
70e14c01 | 135 | ;; Delete preceding blank lines, and this one too if it's the only one. |
2076c87c JB |
136 | (if thisblank |
137 | (progn | |
138 | (beginning-of-line) | |
139 | (if singleblank (forward-line 1)) | |
140 | (delete-region (point) | |
141 | (if (re-search-backward "[^ \t\n]" nil t) | |
142 | (progn (forward-line 1) (point)) | |
143 | (point-min))))) | |
70e14c01 JB |
144 | ;; Delete following blank lines, unless the current line is blank |
145 | ;; and there are no following blank lines. | |
2076c87c JB |
146 | (if (not (and thisblank singleblank)) |
147 | (save-excursion | |
148 | (end-of-line) | |
149 | (forward-line 1) | |
150 | (delete-region (point) | |
151 | (if (re-search-forward "[^ \t\n]" nil t) | |
152 | (progn (beginning-of-line) (point)) | |
70e14c01 JB |
153 | (point-max))))) |
154 | ;; Handle the special case where point is followed by newline and eob. | |
155 | ;; Delete the line, leaving point at eob. | |
156 | (if (looking-at "^[ \t]*\n\\'") | |
157 | (delete-region (point) (point-max))))) | |
2076c87c JB |
158 | |
159 | (defun back-to-indentation () | |
160 | "Move point to the first non-whitespace character on this line." | |
161 | (interactive) | |
162 | (beginning-of-line 1) | |
163 | (skip-chars-forward " \t")) | |
164 | ||
165 | (defun newline-and-indent () | |
166 | "Insert a newline, then indent according to major mode. | |
ff1fbe3e | 167 | Indentation is done using the value of `indent-line-function'. |
2076c87c | 168 | In programming language modes, this is the same as TAB. |
ff1fbe3e RS |
169 | In some text modes, where TAB inserts a tab, this command indents to the |
170 | column specified by the variable `left-margin'." | |
2076c87c JB |
171 | (interactive "*") |
172 | (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
46947372 | 173 | (newline) |
2076c87c JB |
174 | (indent-according-to-mode)) |
175 | ||
176 | (defun reindent-then-newline-and-indent () | |
177 | "Reindent current line, insert newline, then indent the new line. | |
178 | Indentation of both lines is done according to the current major mode, | |
ff1fbe3e | 179 | which means calling the current value of `indent-line-function'. |
2076c87c JB |
180 | In programming language modes, this is the same as TAB. |
181 | In some text modes, where TAB inserts a tab, this indents to the | |
ff1fbe3e | 182 | column specified by the variable `left-margin'." |
2076c87c JB |
183 | (interactive "*") |
184 | (save-excursion | |
185 | (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
186 | (indent-according-to-mode)) | |
46947372 | 187 | (newline) |
2076c87c JB |
188 | (indent-according-to-mode)) |
189 | ||
190 | ;; Internal subroutine of delete-char | |
191 | (defun kill-forward-chars (arg) | |
192 | (if (listp arg) (setq arg (car arg))) | |
193 | (if (eq arg '-) (setq arg -1)) | |
194 | (kill-region (point) (+ (point) arg))) | |
195 | ||
196 | ;; Internal subroutine of backward-delete-char | |
197 | (defun kill-backward-chars (arg) | |
198 | (if (listp arg) (setq arg (car arg))) | |
199 | (if (eq arg '-) (setq arg -1)) | |
200 | (kill-region (point) (- (point) arg))) | |
201 | ||
202 | (defun backward-delete-char-untabify (arg &optional killp) | |
203 | "Delete characters backward, changing tabs into spaces. | |
204 | Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil. | |
205 | Interactively, ARG is the prefix arg (default 1) | |
206 | and KILLP is t if prefix arg is was specified." | |
207 | (interactive "*p\nP") | |
208 | (let ((count arg)) | |
209 | (save-excursion | |
210 | (while (and (> count 0) (not (bobp))) | |
211 | (if (= (preceding-char) ?\t) | |
212 | (let ((col (current-column))) | |
213 | (forward-char -1) | |
214 | (setq col (- col (current-column))) | |
215 | (insert-char ?\ col) | |
216 | (delete-char 1))) | |
217 | (forward-char -1) | |
218 | (setq count (1- count))))) | |
219 | (delete-backward-char arg killp) | |
220 | ;; In overwrite mode, back over columns while clearing them out, | |
221 | ;; unless at end of line. | |
222 | (and overwrite-mode (not (eolp)) | |
223 | (save-excursion (insert-char ?\ arg)))) | |
224 | ||
225 | (defun zap-to-char (arg char) | |
226 | "Kill up to and including ARG'th occurrence of CHAR. | |
227 | Goes backward if ARG is negative; error if CHAR not found." | |
228 | (interactive "p\ncZap to char: ") | |
229 | (kill-region (point) (progn | |
230 | (search-forward (char-to-string char) nil nil arg) | |
231 | ; (goto-char (if (> arg 0) (1- (point)) (1+ (point)))) | |
232 | (point)))) | |
233 | ||
234 | (defun beginning-of-buffer (&optional arg) | |
235 | "Move point to the beginning of the buffer; leave mark at previous position. | |
236 | With arg N, put point N/10 of the way from the true beginning. | |
ff1fbe3e RS |
237 | |
238 | Don't use this command in Lisp programs! | |
2076c87c JB |
239 | \(goto-char (point-min)) is faster and avoids clobbering the mark." |
240 | (interactive "P") | |
241 | (push-mark) | |
242 | (goto-char (if arg | |
243 | (if (> (buffer-size) 10000) | |
244 | ;; Avoid overflow for large buffer sizes! | |
245 | (* (prefix-numeric-value arg) | |
246 | (/ (buffer-size) 10)) | |
247 | (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10)) | |
248 | (point-min))) | |
249 | (if arg (forward-line 1))) | |
250 | ||
251 | (defun end-of-buffer (&optional arg) | |
252 | "Move point to the end of the buffer; leave mark at previous position. | |
253 | With arg N, put point N/10 of the way from the true end. | |
ff1fbe3e RS |
254 | |
255 | Don't use this command in Lisp programs! | |
2076c87c JB |
256 | \(goto-char (point-max)) is faster and avoids clobbering the mark." |
257 | (interactive "P") | |
258 | (push-mark) | |
259 | (goto-char (if arg | |
260 | (- (1+ (buffer-size)) | |
261 | (if (> (buffer-size) 10000) | |
262 | ;; Avoid overflow for large buffer sizes! | |
263 | (* (prefix-numeric-value arg) | |
264 | (/ (buffer-size) 10)) | |
265 | (/ (* (buffer-size) (prefix-numeric-value arg)) 10))) | |
266 | (point-max))) | |
3a801d0c ER |
267 | ;; If we went to a place in the middle of the buffer, |
268 | ;; adjust it to the beginning of a line. | |
2076c87c | 269 | (if arg (forward-line 1) |
3a801d0c ER |
270 | ;; If the end of the buffer is not already on the screen, |
271 | ;; then scroll specially to put it near, but not at, the bottom. | |
272 | (if (let ((old-point (point))) | |
273 | (save-excursion | |
274 | (goto-char (window-start)) | |
275 | (vertical-motion (window-height)) | |
276 | (< (point) old-point))) | |
277 | (recenter -3)))) | |
2076c87c JB |
278 | |
279 | (defun mark-whole-buffer () | |
70e14c01 JB |
280 | "Put point at beginning and mark at end of buffer. |
281 | You probably should not use this function in Lisp programs; | |
282 | it is usually a mistake for a Lisp function to use any subroutine | |
283 | that uses or sets the mark." | |
2076c87c JB |
284 | (interactive) |
285 | (push-mark (point)) | |
286 | (push-mark (point-max)) | |
287 | (goto-char (point-min))) | |
288 | ||
289 | (defun count-lines-region (start end) | |
290 | "Print number of lines and charcters in the region." | |
291 | (interactive "r") | |
292 | (message "Region has %d lines, %d characters" | |
293 | (count-lines start end) (- end start))) | |
294 | ||
295 | (defun what-line () | |
296 | "Print the current line number (in the buffer) of point." | |
297 | (interactive) | |
298 | (save-restriction | |
299 | (widen) | |
300 | (save-excursion | |
301 | (beginning-of-line) | |
302 | (message "Line %d" | |
303 | (1+ (count-lines 1 (point))))))) | |
304 | ||
305 | (defun count-lines (start end) | |
306 | "Return number of lines between START and END. | |
307 | This is usually the number of newlines between them, | |
ff1fbe3e | 308 | but can be one more if START is not equal to END |
2076c87c JB |
309 | and the greater of them is not at the start of a line." |
310 | (save-excursion | |
311 | (save-restriction | |
312 | (narrow-to-region start end) | |
313 | (goto-char (point-min)) | |
314 | (if (eq selective-display t) | |
315 | (let ((done 0)) | |
316 | (while (re-search-forward "[\n\C-m]" nil t 40) | |
317 | (setq done (+ 40 done))) | |
318 | (while (re-search-forward "[\n\C-m]" nil t 1) | |
319 | (setq done (+ 1 done))) | |
320 | done) | |
321 | (- (buffer-size) (forward-line (buffer-size))))))) | |
322 | ||
323 | (defun what-cursor-position () | |
324 | "Print info on cursor position (on screen and within buffer)." | |
325 | (interactive) | |
326 | (let* ((char (following-char)) | |
327 | (beg (point-min)) | |
328 | (end (point-max)) | |
329 | (pos (point)) | |
330 | (total (buffer-size)) | |
331 | (percent (if (> total 50000) | |
332 | ;; Avoid overflow from multiplying by 100! | |
333 | (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1)) | |
334 | (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1)))) | |
335 | (hscroll (if (= (window-hscroll) 0) | |
336 | "" | |
337 | (format " Hscroll=%d" (window-hscroll)))) | |
338 | (col (current-column))) | |
339 | (if (= pos end) | |
340 | (if (or (/= beg 1) (/= end (1+ total))) | |
341 | (message "point=%d of %d(%d%%) <%d - %d> column %d %s" | |
342 | pos total percent beg end col hscroll) | |
343 | (message "point=%d of %d(%d%%) column %d %s" | |
344 | pos total percent col hscroll)) | |
345 | (if (or (/= beg 1) (/= end (1+ total))) | |
346 | (message "Char: %s (0%o) point=%d of %d(%d%%) <%d - %d> column %d %s" | |
347 | (single-key-description char) char pos total percent beg end col hscroll) | |
348 | (message "Char: %s (0%o) point=%d of %d(%d%%) column %d %s" | |
349 | (single-key-description char) char pos total percent col hscroll))))) | |
350 | ||
351 | (defun fundamental-mode () | |
352 | "Major mode not specialized for anything in particular. | |
353 | Other major modes are defined by comparison with this one." | |
354 | (interactive) | |
355 | (kill-all-local-variables)) | |
356 | ||
854c16c5 RS |
357 | (defvar read-expression-map (copy-keymap minibuffer-local-map) |
358 | "Minibuffer keymap used for reading Lisp expressions.") | |
359 | (define-key read-expression-map "\M-\t" 'lisp-complete-symbol) | |
360 | ||
2076c87c JB |
361 | (put 'eval-expression 'disabled t) |
362 | ||
363 | ;; We define this, rather than making eval interactive, | |
364 | ;; for the sake of completion of names like eval-region, eval-current-buffer. | |
365 | (defun eval-expression (expression) | |
366 | "Evaluate EXPRESSION and print value in minibuffer. | |
eb57c304 | 367 | Value is also consed on to front of the variable `values'." |
854c16c5 RS |
368 | (interactive (list (read-from-minibuffer "Eval: " |
369 | nil read-expression-map t))) | |
2076c87c JB |
370 | (setq values (cons (eval expression) values)) |
371 | (prin1 (car values) t)) | |
372 | ||
373 | (defun edit-and-eval-command (prompt command) | |
374 | "Prompting with PROMPT, let user edit COMMAND and eval result. | |
375 | COMMAND is a Lisp expression. Let user edit that expression in | |
376 | the minibuffer, then read and evaluate the result." | |
854c16c5 RS |
377 | (let ((command (read-from-minibuffer prompt |
378 | (prin1-to-string command) | |
379 | read-expression-map t))) | |
2076c87c JB |
380 | ;; Add edited command to command history, unless redundant. |
381 | (or (equal command (car command-history)) | |
382 | (setq command-history (cons command command-history))) | |
383 | (eval command))) | |
384 | ||
ebb61177 | 385 | (defun repeat-complex-command (arg) |
2076c87c JB |
386 | "Edit and re-evaluate last complex command, or ARGth from last. |
387 | A complex command is one which used the minibuffer. | |
388 | The command is placed in the minibuffer as a Lisp form for editing. | |
389 | The result is executed, repeating the command as changed. | |
390 | If the command has been changed or is not the most recent previous command | |
391 | it is added to the front of the command history. | |
eb6e9899 RS |
392 | You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element] |
393 | to get different commands to edit and resubmit." | |
2076c87c | 394 | (interactive "p") |
ba343182 | 395 | (let ((elt (nth (1- arg) command-history)) |
ebb61177 | 396 | (minibuffer-history-position arg) |
ba343182 | 397 | (minibuffer-history-sexp-flag t) |
2076c87c JB |
398 | newcmd) |
399 | (if elt | |
854c16c5 | 400 | (progn |
2076c87c JB |
401 | (setq newcmd (read-from-minibuffer "Redo: " |
402 | (prin1-to-string elt) | |
854c16c5 | 403 | read-expression-map |
770970cb RS |
404 | t |
405 | (cons 'command-history | |
406 | arg))) | |
854c16c5 RS |
407 | ;; If command was added to command-history as a string, |
408 | ;; get rid of that. We want only evallable expressions there. | |
409 | (if (stringp (car command-history)) | |
410 | (setq command-history (cdr command-history))) | |
2076c87c JB |
411 | ;; If command to be redone does not match front of history, |
412 | ;; add it to the history. | |
413 | (or (equal newcmd (car command-history)) | |
414 | (setq command-history (cons newcmd command-history))) | |
415 | (eval newcmd)) | |
416 | (ding)))) | |
e91f80c4 | 417 | \f |
854c16c5 RS |
418 | (defvar minibuffer-history nil |
419 | "Default minibuffer history list. | |
420 | This is used for all minibuffer input | |
421 | except when an alternate history list is specified.") | |
422 | (defvar minibuffer-history-sexp-flag nil | |
423 | "Nonzero when doing history operations on `command-history'. | |
424 | More generally, indicates that the history list being acted on | |
425 | contains expressions rather than strings.") | |
e91f80c4 RS |
426 | (setq minibuffer-history-variable 'minibuffer-history) |
427 | (setq minibuffer-history-position nil) | |
854c16c5 | 428 | (defvar minibuffer-history-search-history nil) |
e91f80c4 | 429 | |
29929437 | 430 | (mapcar |
d0678801 RM |
431 | (lambda (key-and-command) |
432 | (mapcar | |
433 | (lambda (keymap-and-completionp) | |
434 | ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P). | |
435 | ;; If the cdr of KEY-AND-COMMAND (the command) is a cons, | |
436 | ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t. | |
437 | (define-key (symbol-value (car keymap-and-completionp)) | |
438 | (car key-and-command) | |
439 | (let ((command (cdr key-and-command))) | |
440 | (if (consp command) | |
441 | (if (cdr keymap-and-completionp) | |
442 | (cdr command) | |
443 | (car command)) | |
444 | command)))) | |
445 | '((minibuffer-local-map . nil) | |
446 | (minibuffer-local-ns-map . nil) | |
447 | (minibuffer-local-completion-map . t) | |
448 | (minibuffer-local-must-match-map . t) | |
449 | (read-expression-map . nil)))) | |
450 | ;; In completion maps, use the completion-oriented history commands. | |
451 | '(("\en" . (next-history-element . next-complete-history-element)) | |
452 | ([next] . (next-history-element . next-complete-history-element)) | |
453 | ("\ep" . (previous-history-element . previous-complete-history-element)) | |
454 | ([prior] . (previous-history-element . previous-complete-history-element)) | |
29929437 JB |
455 | ("\er" . previous-matching-history-element) |
456 | ("\es" . next-matching-history-element))) | |
e91f80c4 | 457 | |
e91f80c4 | 458 | (defun previous-matching-history-element (regexp n) |
854c16c5 RS |
459 | "Find the previous history element that matches REGEXP. |
460 | \(Previous history elements refer to earlier actions.) | |
461 | With prefix argument N, search for Nth previous match. | |
462 | If N is negative, find the next or Nth next match." | |
463 | (interactive | |
464 | (let ((enable-recursive-minibuffers t) | |
465 | (minibuffer-history-sexp-flag nil)) | |
466 | (list (read-from-minibuffer "Previous element matching (regexp): " | |
467 | nil | |
468 | minibuffer-local-map | |
469 | nil | |
470 | 'minibuffer-history-search-history) | |
471 | (prefix-numeric-value current-prefix-arg)))) | |
e91f80c4 | 472 | (let ((history (symbol-value minibuffer-history-variable)) |
ccc58657 | 473 | prevpos |
e91f80c4 RS |
474 | (pos minibuffer-history-position)) |
475 | (while (/= n 0) | |
476 | (setq prevpos pos) | |
477 | (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history))) | |
478 | (if (= pos prevpos) | |
479 | (error (if (= pos 1) | |
ccc58657 RS |
480 | "No later matching history item" |
481 | "No earlier matching history item"))) | |
e91f80c4 RS |
482 | (if (string-match regexp |
483 | (if minibuffer-history-sexp-flag | |
484 | (prin1-to-string (nth (1- pos) history)) | |
485 | (nth (1- pos) history))) | |
854c16c5 | 486 | (setq n (+ n (if (< n 0) 1 -1))))) |
e91f80c4 RS |
487 | (setq minibuffer-history-position pos) |
488 | (erase-buffer) | |
489 | (let ((elt (nth (1- pos) history))) | |
490 | (insert (if minibuffer-history-sexp-flag | |
491 | (prin1-to-string elt) | |
492 | elt))) | |
854c16c5 RS |
493 | (goto-char (point-min))) |
494 | (if (or (eq (car (car command-history)) 'previous-matching-history-element) | |
495 | (eq (car (car command-history)) 'next-matching-history-element)) | |
496 | (setq command-history (cdr command-history)))) | |
e91f80c4 | 497 | |
e91f80c4 | 498 | (defun next-matching-history-element (regexp n) |
854c16c5 RS |
499 | "Find the next history element that matches REGEXP. |
500 | \(The next history element refers to a more recent action.) | |
501 | With prefix argument N, search for Nth next match. | |
502 | If N is negative, find the previous or Nth previous match." | |
503 | (interactive | |
504 | (let ((enable-recursive-minibuffers t) | |
505 | (minibuffer-history-sexp-flag nil)) | |
506 | (list (read-from-minibuffer "Next element matching (regexp): " | |
507 | nil | |
508 | minibuffer-local-map | |
509 | nil | |
510 | 'minibuffer-history-search-history) | |
511 | (prefix-numeric-value current-prefix-arg)))) | |
e91f80c4 | 512 | (previous-matching-history-element regexp (- n))) |
2076c87c | 513 | |
ebb61177 RS |
514 | (defun next-history-element (n) |
515 | "Insert the next element of the minibuffer history into the minibuffer." | |
2076c87c | 516 | (interactive "p") |
ebb61177 RS |
517 | (let ((narg (min (max 1 (- minibuffer-history-position n)) |
518 | (length (symbol-value minibuffer-history-variable))))) | |
519 | (if (= minibuffer-history-position narg) | |
520 | (error (if (= minibuffer-history-position 1) | |
ccc58657 RS |
521 | "End of history; no next item" |
522 | "Beginning of history; no preceding item")) | |
2076c87c | 523 | (erase-buffer) |
ebb61177 | 524 | (setq minibuffer-history-position narg) |
ba343182 RS |
525 | (let ((elt (nth (1- minibuffer-history-position) |
526 | (symbol-value minibuffer-history-variable)))) | |
527 | (insert | |
528 | (if minibuffer-history-sexp-flag | |
529 | (prin1-to-string elt) | |
770970cb | 530 | elt))) |
2076c87c JB |
531 | (goto-char (point-min))))) |
532 | ||
ebb61177 | 533 | (defun previous-history-element (n) |
3ee3a076 | 534 | "Inserts the previous element of the minibuffer history into the minibuffer." |
2076c87c | 535 | (interactive "p") |
2c5e21c1 | 536 | (next-history-element (- n))) |
d0678801 RM |
537 | |
538 | (defun next-complete-history-element (n) | |
539 | "\ | |
540 | Get previous element of history which is a completion of minibuffer contents." | |
541 | (interactive "p") | |
c7e69876 RM |
542 | (next-matching-history-element |
543 | (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n)) | |
d0678801 RM |
544 | |
545 | (defun previous-complete-history-element (n) | |
546 | "Get next element of history which is a completion of minibuffer contents." | |
547 | (interactive "p") | |
548 | (next-complete-history-element (- n))) | |
e91f80c4 | 549 | \f |
2076c87c JB |
550 | (defun goto-line (arg) |
551 | "Goto line ARG, counting from line 1 at beginning of buffer." | |
552 | (interactive "NGoto line: ") | |
553 | (save-restriction | |
554 | (widen) | |
555 | (goto-char 1) | |
556 | (if (eq selective-display t) | |
557 | (re-search-forward "[\n\C-m]" nil 'end (1- arg)) | |
558 | (forward-line (1- arg))))) | |
559 | ||
560 | ;Put this on C-x u, so we can force that rather than C-_ into startup msg | |
561 | (fset 'advertised-undo 'undo) | |
562 | ||
563 | (defun undo (&optional arg) | |
564 | "Undo some previous changes. | |
565 | Repeat this command to undo more changes. | |
566 | A numeric argument serves as a repeat count." | |
567 | (interactive "*p") | |
568 | (let ((modified (buffer-modified-p))) | |
71e40adf JB |
569 | (or (eq (selected-window) (minibuffer-window)) |
570 | (message "Undo!")) | |
2076c87c JB |
571 | (or (eq last-command 'undo) |
572 | (progn (undo-start) | |
573 | (undo-more 1))) | |
574 | (setq this-command 'undo) | |
575 | (undo-more (or arg 1)) | |
576 | (and modified (not (buffer-modified-p)) | |
577 | (delete-auto-save-file-if-necessary)))) | |
578 | ||
579 | (defun undo-start () | |
ff1fbe3e RS |
580 | "Set `pending-undo-list' to the front of the undo list. |
581 | The next call to `undo-more' will undo the most recently made change." | |
2076c87c JB |
582 | (if (eq buffer-undo-list t) |
583 | (error "No undo information in this buffer")) | |
584 | (setq pending-undo-list buffer-undo-list)) | |
585 | ||
586 | (defun undo-more (count) | |
587 | "Undo back N undo-boundaries beyond what was already undone recently. | |
ff1fbe3e RS |
588 | Call `undo-start' to get ready to undo recent changes, |
589 | then call `undo-more' one or more times to undo them." | |
2076c87c JB |
590 | (or pending-undo-list |
591 | (error "No further undo information")) | |
592 | (setq pending-undo-list (primitive-undo count pending-undo-list))) | |
593 | ||
594 | (defvar last-shell-command "") | |
595 | (defvar last-shell-command-on-region "") | |
596 | ||
597 | (defun shell-command (command &optional flag) | |
598 | "Execute string COMMAND in inferior shell; display output, if any. | |
599 | If COMMAND ends in ampersand, execute it asynchronously. | |
600 | ||
601 | Optional second arg non-nil (prefix arg, if interactive) | |
602 | means insert output in current buffer after point (leave mark after it). | |
603 | This cannot be done asynchronously." | |
604 | (interactive (list (read-string "Shell command: " last-shell-command) | |
605 | current-prefix-arg)) | |
606 | (if flag | |
607 | (progn (barf-if-buffer-read-only) | |
608 | (push-mark) | |
609 | ;; We do not use -f for csh; we will not support broken use of | |
610 | ;; .cshrcs. Even the BSD csh manual says to use | |
611 | ;; "if ($?prompt) exit" before things which are not useful | |
612 | ;; non-interactively. Besides, if someone wants their other | |
613 | ;; aliases for shell commands then they can still have them. | |
614 | (call-process shell-file-name nil t nil | |
615 | "-c" command) | |
616 | (exchange-point-and-mark)) | |
617 | ;; Preserve the match data in case called from a program. | |
618 | (let ((data (match-data))) | |
619 | (unwind-protect | |
620 | (if (string-match "[ \t]*&[ \t]*$" command) | |
621 | ;; Command ending with ampersand means asynchronous. | |
622 | (let ((buffer (get-buffer-create "*shell-command*")) | |
623 | (directory default-directory) | |
624 | proc) | |
625 | ;; Remove the ampersand. | |
626 | (setq command (substring command 0 (match-beginning 0))) | |
627 | ;; If will kill a process, query first. | |
628 | (setq proc (get-buffer-process buffer)) | |
629 | (if proc | |
630 | (if (yes-or-no-p "A command is running. Kill it? ") | |
631 | (kill-process proc) | |
632 | (error "Shell command in progress"))) | |
633 | (save-excursion | |
634 | (set-buffer buffer) | |
635 | (erase-buffer) | |
636 | (display-buffer buffer) | |
637 | (setq default-directory directory) | |
638 | (setq proc (start-process "Shell" buffer | |
639 | shell-file-name "-c" command)) | |
640 | (setq mode-line-process '(": %s")) | |
641 | (set-process-sentinel proc 'shell-command-sentinel) | |
642 | (set-process-filter proc 'shell-command-filter) | |
643 | )) | |
644 | (shell-command-on-region (point) (point) command nil)) | |
645 | (store-match-data data))))) | |
646 | ||
647 | ;; We have a sentinel to prevent insertion of a termination message | |
648 | ;; in the buffer itself. | |
649 | (defun shell-command-sentinel (process signal) | |
650 | (if (memq (process-status process) '(exit signal)) | |
651 | (progn | |
652 | (message "%s: %s." | |
653 | (car (cdr (cdr (process-command process)))) | |
654 | (substring signal 0 -1)) | |
655 | (save-excursion | |
656 | (set-buffer (process-buffer process)) | |
657 | (setq mode-line-process nil)) | |
658 | (delete-process process)))) | |
659 | ||
660 | (defun shell-command-filter (proc string) | |
661 | ;; Do save-excursion by hand so that we can leave point numerically unchanged | |
662 | ;; despite an insertion immediately after it. | |
663 | (let* ((obuf (current-buffer)) | |
664 | (buffer (process-buffer proc)) | |
665 | opoint | |
666 | (window (get-buffer-window buffer)) | |
667 | (pos (window-start window))) | |
668 | (unwind-protect | |
669 | (progn | |
670 | (set-buffer buffer) | |
671 | (setq opoint (point)) | |
672 | (goto-char (point-max)) | |
673 | (insert-before-markers string)) | |
674 | ;; insert-before-markers moved this marker: set it back. | |
675 | (set-window-start window pos) | |
676 | ;; Finish our save-excursion. | |
677 | (goto-char opoint) | |
678 | (set-buffer obuf)))) | |
679 | ||
680 | (defun shell-command-on-region (start end command &optional flag interactive) | |
681 | "Execute string COMMAND in inferior shell with region as input. | |
682 | Normally display output (if any) in temp buffer `*Shell Command Output*'; | |
683 | Prefix arg means replace the region with it. | |
684 | Noninteractive args are START, END, COMMAND, FLAG. | |
685 | Noninteractively FLAG means insert output in place of text from START to END, | |
686 | and put point at the end, but don't alter the mark. | |
687 | ||
688 | If the output is one line, it is displayed in the echo area, | |
689 | but it is nonetheless available in buffer `*Shell Command Output*' | |
690 | even though that buffer is not automatically displayed. If there is no output | |
691 | or output is inserted in the current buffer then `*Shell Command Output*' is | |
692 | deleted." | |
082c7686 | 693 | (interactive (list (region-beginning) (region-end) |
2076c87c JB |
694 | (read-string "Shell command on region: " |
695 | last-shell-command-on-region) | |
696 | current-prefix-arg | |
697 | (prefix-numeric-value current-prefix-arg))) | |
698 | (if flag | |
699 | ;; Replace specified region with output from command. | |
700 | (let ((swap (and interactive (< (point) (mark))))) | |
701 | ;; Don't muck with mark | |
702 | ;; unless called interactively. | |
703 | (and interactive (push-mark)) | |
704 | (call-process-region start end shell-file-name t t nil | |
705 | "-c" command) | |
706 | (if (get-buffer "*Shell Command Output*") | |
707 | (kill-buffer "*Shell Command Output*")) | |
708 | (and interactive swap (exchange-point-and-mark))) | |
709 | ;; No prefix argument: put the output in a temp buffer, | |
710 | ;; replacing its entire contents. | |
711 | (let ((buffer (get-buffer-create "*Shell Command Output*"))) | |
712 | (if (eq buffer (current-buffer)) | |
713 | ;; If the input is the same buffer as the output, | |
714 | ;; delete everything but the specified region, | |
715 | ;; then replace that region with the output. | |
716 | (progn (delete-region end (point-max)) | |
717 | (delete-region (point-min) start) | |
718 | (call-process-region (point-min) (point-max) | |
719 | shell-file-name t t nil | |
720 | "-c" command)) | |
721 | ;; Clear the output buffer, then run the command with output there. | |
722 | (save-excursion | |
723 | (set-buffer buffer) | |
724 | (erase-buffer)) | |
725 | (call-process-region start end shell-file-name | |
726 | nil buffer nil | |
727 | "-c" command)) | |
728 | ;; Report the amount of output. | |
729 | (let ((lines (save-excursion | |
730 | (set-buffer buffer) | |
731 | (if (= (buffer-size) 0) | |
732 | 0 | |
733 | (count-lines (point-min) (point-max)))))) | |
734 | (cond ((= lines 0) | |
735 | (message "(Shell command completed with no output)") | |
736 | (kill-buffer "*Shell Command Output*")) | |
737 | ((= lines 1) | |
738 | (message "%s" | |
739 | (save-excursion | |
740 | (set-buffer buffer) | |
741 | (goto-char (point-min)) | |
742 | (buffer-substring (point) | |
743 | (progn (end-of-line) (point)))))) | |
744 | (t | |
745 | (set-window-start (display-buffer buffer) 1))))))) | |
746 | \f | |
747 | (defun universal-argument () | |
748 | "Begin a numeric argument for the following command. | |
749 | Digits or minus sign following \\[universal-argument] make up the numeric argument. | |
750 | \\[universal-argument] following the digits or minus sign ends the argument. | |
751 | \\[universal-argument] without digits or minus sign provides 4 as argument. | |
752 | Repeating \\[universal-argument] without digits or minus sign | |
753 | multiplies the argument by 4 each time." | |
754 | (interactive nil) | |
c637ae6f JB |
755 | (let ((factor 4) |
756 | key) | |
70e14c01 JB |
757 | ;; (describe-arg (list factor) 1) |
758 | (setq key (read-key-sequence nil t)) | |
c637ae6f JB |
759 | (while (equal (key-binding key) 'universal-argument) |
760 | (setq factor (* 4 factor)) | |
70e14c01 JB |
761 | ;; (describe-arg (list factor) 1) |
762 | (setq key (read-key-sequence nil t))) | |
c637ae6f JB |
763 | (prefix-arg-internal key factor nil))) |
764 | ||
765 | (defun prefix-arg-internal (key factor value) | |
2076c87c JB |
766 | (let ((sign 1)) |
767 | (if (and (numberp value) (< value 0)) | |
768 | (setq sign -1 value (- value))) | |
769 | (if (eq value '-) | |
770 | (setq sign -1 value nil)) | |
70e14c01 | 771 | ;; (describe-arg value sign) |
c637ae6f JB |
772 | (while (equal key "-") |
773 | (setq sign (- sign) factor nil) | |
70e14c01 JB |
774 | ;; (describe-arg value sign) |
775 | (setq key (read-key-sequence nil t))) | |
bd307392 JB |
776 | (while (and (stringp key) |
777 | (= (length key) 1) | |
c637ae6f JB |
778 | (not (string< key "0")) |
779 | (not (string< "9" key))) | |
780 | (setq value (+ (* (if (numberp value) value 0) 10) | |
781 | (- (aref key 0) ?0)) | |
782 | factor nil) | |
70e14c01 JB |
783 | ;; (describe-arg value sign) |
784 | (setq key (read-key-sequence nil t))) | |
2076c87c | 785 | (setq prefix-arg |
c637ae6f | 786 | (cond (factor (list factor)) |
2076c87c JB |
787 | ((numberp value) (* value sign)) |
788 | ((= sign -1) '-))) | |
c637ae6f JB |
789 | ;; Calling universal-argument after digits |
790 | ;; terminates the argument but is ignored. | |
791 | (if (eq (key-binding key) 'universal-argument) | |
792 | (progn | |
793 | (describe-arg value sign) | |
70e14c01 | 794 | (setq key (read-key-sequence nil t)))) |
dbc4e1c1 | 795 | (setq unread-command-events (append key '())))) |
c637ae6f JB |
796 | |
797 | (defun describe-arg (value sign) | |
798 | (cond ((numberp value) | |
799 | (message "Arg: %d" (* value sign))) | |
800 | ((consp value) | |
801 | (message "Arg: [%d]" (car value))) | |
802 | ((< sign 0) | |
803 | (message "Arg: -")))) | |
2076c87c JB |
804 | |
805 | (defun digit-argument (arg) | |
806 | "Part of the numeric argument for the next command. | |
807 | \\[universal-argument] following digits or minus sign ends the argument." | |
808 | (interactive "P") | |
c637ae6f JB |
809 | (prefix-arg-internal (char-to-string (logand last-command-char ?\177)) |
810 | nil arg)) | |
2076c87c JB |
811 | |
812 | (defun negative-argument (arg) | |
813 | "Begin a negative numeric argument for the next command. | |
814 | \\[universal-argument] following digits or minus sign ends the argument." | |
815 | (interactive "P") | |
c637ae6f | 816 | (prefix-arg-internal "-" nil arg)) |
2076c87c JB |
817 | \f |
818 | (defun forward-to-indentation (arg) | |
819 | "Move forward ARG lines and position at first nonblank character." | |
820 | (interactive "p") | |
821 | (forward-line arg) | |
822 | (skip-chars-forward " \t")) | |
823 | ||
824 | (defun backward-to-indentation (arg) | |
825 | "Move backward ARG lines and position at first nonblank character." | |
826 | (interactive "p") | |
827 | (forward-line (- arg)) | |
828 | (skip-chars-forward " \t")) | |
829 | ||
830 | (defun kill-line (&optional arg) | |
831 | "Kill the rest of the current line; if no nonblanks there, kill thru newline. | |
832 | With prefix argument, kill that many lines from point. | |
833 | Negative arguments kill lines backward. | |
834 | ||
835 | When calling from a program, nil means \"no arg\", | |
836 | a number counts as a prefix arg." | |
837 | (interactive "P") | |
838 | (kill-region (point) | |
c2e8a012 JB |
839 | ;; Don't shift point before doing the delete; that way, |
840 | ;; undo will record the right position of point. | |
841 | (save-excursion | |
2076c87c JB |
842 | (if arg |
843 | (forward-line (prefix-numeric-value arg)) | |
844 | (if (eobp) | |
845 | (signal 'end-of-buffer nil)) | |
846 | (if (looking-at "[ \t]*$") | |
847 | (forward-line 1) | |
848 | (end-of-line))) | |
849 | (point)))) | |
850 | \f | |
70e14c01 JB |
851 | ;;;; Window system cut and paste hooks. |
852 | ||
853 | (defvar interprogram-cut-function nil | |
854 | "Function to call to make a killed region available to other programs. | |
855 | ||
856 | Most window systems provide some sort of facility for cutting and | |
857 | pasting text between the windows of different programs. On startup, | |
858 | this variable is set to a function which emacs will call whenever text | |
859 | is put in the kill ring to make the new kill available to other | |
860 | programs. | |
861 | ||
862 | The function takes one argument, TEXT, which is a string containing | |
863 | the text which should be made available.") | |
864 | ||
865 | (defvar interprogram-paste-function nil | |
866 | "Function to call to get text cut from other programs. | |
867 | ||
868 | Most window systems provide some sort of facility for cutting and | |
869 | pasting text between the windows of different programs. On startup, | |
870 | this variable is set to a function which emacs will call to obtain | |
871 | text that other programs have provided for pasting. | |
872 | ||
873 | The function should be called with no arguments. If the function | |
874 | returns nil, then no other program has provided such text, and the top | |
875 | of the Emacs kill ring should be used. If the function returns a | |
daa37602 JB |
876 | string, that string should be put in the kill ring as the latest kill. |
877 | ||
878 | Note that the function should return a string only if a program other | |
879 | than Emacs has provided a string for pasting; if Emacs provided the | |
880 | most recent string, the function should return nil. If it is | |
881 | difficult to tell whether Emacs or some other program provided the | |
882 | current string, it is probably good enough to return nil if the string | |
883 | is equal (according to `string=') to the last text Emacs provided.") | |
70e14c01 JB |
884 | |
885 | ||
886 | \f | |
887 | ;;;; The kill ring data structure. | |
2076c87c JB |
888 | |
889 | (defvar kill-ring nil | |
70e14c01 JB |
890 | "List of killed text sequences. |
891 | Since the kill ring is supposed to interact nicely with cut-and-paste | |
892 | facilities offered by window systems, use of this variable should | |
893 | interact nicely with `interprogram-cut-function' and | |
894 | `interprogram-paste-function'. The functions `kill-new', | |
895 | `kill-append', and `current-kill' are supposed to implement this | |
896 | interaction; you may want to use them instead of manipulating the kill | |
897 | ring directly.") | |
2076c87c JB |
898 | |
899 | (defconst kill-ring-max 30 | |
900 | "*Maximum length of kill ring before oldest elements are thrown away.") | |
901 | ||
902 | (defvar kill-ring-yank-pointer nil | |
903 | "The tail of the kill ring whose car is the last thing yanked.") | |
904 | ||
70e14c01 JB |
905 | (defun kill-new (string) |
906 | "Make STRING the latest kill in the kill ring. | |
907 | Set the kill-ring-yank pointer to point to it. | |
908 | If `interprogram-cut-function' is non-nil, apply it to STRING." | |
909 | (setq kill-ring (cons string kill-ring)) | |
910 | (if (> (length kill-ring) kill-ring-max) | |
911 | (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)) | |
912 | (setq kill-ring-yank-pointer kill-ring) | |
913 | (if interprogram-cut-function | |
914 | (funcall interprogram-cut-function string))) | |
915 | ||
2076c87c | 916 | (defun kill-append (string before-p) |
70e14c01 JB |
917 | "Append STRING to the end of the latest kill in the kill ring. |
918 | If BEFORE-P is non-nil, prepend STRING to the kill. | |
88c1aa79 | 919 | If `interprogram-cut-function' is set, pass the resulting kill to |
70e14c01 | 920 | it." |
2076c87c JB |
921 | (setcar kill-ring |
922 | (if before-p | |
923 | (concat string (car kill-ring)) | |
70e14c01 JB |
924 | (concat (car kill-ring) string))) |
925 | (if interprogram-cut-function | |
926 | (funcall interprogram-cut-function (car kill-ring)))) | |
927 | ||
928 | (defun current-kill (n &optional do-not-move) | |
929 | "Rotate the yanking point by N places, and then return that kill. | |
930 | If N is zero, `interprogram-paste-function' is set, and calling it | |
931 | returns a string, then that string is added to the front of the | |
932 | kill ring and returned as the latest kill. | |
933 | If optional arg DO-NOT-MOVE is non-nil, then don't actually move the | |
934 | yanking point; just return the Nth kill forward." | |
935 | (let ((interprogram-paste (and (= n 0) | |
936 | interprogram-paste-function | |
937 | (funcall interprogram-paste-function)))) | |
938 | (if interprogram-paste | |
939 | (progn | |
940 | ;; Disable the interprogram cut function when we add the new | |
941 | ;; text to the kill ring, so Emacs doesn't try to own the | |
942 | ;; selection, with identical text. | |
943 | (let ((interprogram-cut-function nil)) | |
944 | (kill-new interprogram-paste)) | |
945 | interprogram-paste) | |
946 | (or kill-ring (error "Kill ring is empty")) | |
947 | (let* ((length (length kill-ring)) | |
948 | (ARGth-kill-element | |
949 | (nthcdr (% (+ n (- length (length kill-ring-yank-pointer))) | |
950 | length) | |
951 | kill-ring))) | |
952 | (or do-not-move | |
953 | (setq kill-ring-yank-pointer ARGth-kill-element)) | |
954 | (car ARGth-kill-element))))) | |
c88ab9ce | 955 | |
c88ab9ce | 956 | |
70e14c01 JB |
957 | \f |
958 | ;;;; Commands for manipulating the kill ring. | |
c88ab9ce | 959 | |
2076c87c JB |
960 | (defun kill-region (beg end) |
961 | "Kill between point and mark. | |
962 | The text is deleted but saved in the kill ring. | |
963 | The command \\[yank] can retrieve it from there. | |
964 | \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].) | |
965 | ||
966 | This is the primitive for programs to kill text (as opposed to deleting it). | |
967 | Supply two arguments, character numbers indicating the stretch of text | |
968 | to be killed. | |
969 | Any command that calls this function is a \"kill command\". | |
970 | If the previous command was also a kill command, | |
971 | the text killed this time appends to the text killed last time | |
972 | to make one entry in the kill ring." | |
79926889 | 973 | (interactive "*r") |
70e14c01 | 974 | (cond |
79926889 JB |
975 | ;; If the buffer was read-only, we used to just do a |
976 | ;; copy-region-as-kill. This was never what I wanted - usually I | |
977 | ;; was making a mistake and trying to edit a file checked into RCS - | |
978 | ;; so I've taken the code out. | |
70e14c01 JB |
979 | ((not (or (eq buffer-undo-list t) |
980 | (eq last-command 'kill-region) | |
981 | (eq beg end))) | |
982 | ;; Don't let the undo list be truncated before we can even access it. | |
109d300c | 983 | (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100))) |
70e14c01 JB |
984 | (delete-region beg end) |
985 | ;; Take the same string recorded for undo | |
986 | ;; and put it in the kill-ring. | |
987 | (kill-new (car (car buffer-undo-list))) | |
988 | (setq this-command 'kill-region))) | |
989 | (t | |
2076c87c | 990 | (copy-region-as-kill beg end) |
70e14c01 | 991 | (delete-region beg end)))) |
2076c87c | 992 | |
2076c87c JB |
993 | (defun copy-region-as-kill (beg end) |
994 | "Save the region as if killed, but don't kill it. | |
46947372 JB |
995 | If `interprogram-cut-function' is non-nil, also save the text for a window |
996 | system cut and paste." | |
2076c87c JB |
997 | (interactive "r") |
998 | (if (eq last-command 'kill-region) | |
999 | (kill-append (buffer-substring beg end) (< end beg)) | |
70e14c01 JB |
1000 | (kill-new (buffer-substring beg end))) |
1001 | (setq this-command 'kill-region) | |
2076c87c JB |
1002 | nil) |
1003 | ||
1004 | (defun kill-ring-save (beg end) | |
1005 | "Save the region as if killed, but don't kill it." | |
1006 | (interactive "r") | |
1007 | (copy-region-as-kill beg end) | |
3a801d0c ER |
1008 | (if (interactive-p) |
1009 | (save-excursion | |
1010 | (let ((other-end (if (= (point) beg) end beg))) | |
1011 | (if (pos-visible-in-window-p other-end (selected-window)) | |
1012 | (progn | |
1013 | (goto-char other-end) | |
1014 | (sit-for 1)) | |
1015 | (let* ((killed-text (current-kill 0)) | |
1016 | (message-len (min (length killed-text) 40))) | |
1017 | (if (= (point) beg) | |
1018 | ;; Don't say "killed"; that is misleading. | |
1019 | (message "Saved text until \"%s\"" | |
1020 | (substring killed-text (- message-len))) | |
1021 | (message "Saved text from \"%s\"" | |
1022 | (substring killed-text 0 message-len))))))))) | |
2076c87c JB |
1023 | |
1024 | (defun append-next-kill () | |
ff1fbe3e | 1025 | "Cause following command, if it kills, to append to previous kill." |
2076c87c JB |
1026 | (interactive) |
1027 | (if (interactive-p) | |
1028 | (progn | |
1029 | (setq this-command 'kill-region) | |
1030 | (message "If the next command is a kill, it will append")) | |
1031 | (setq last-command 'kill-region))) | |
1032 | ||
2076c87c | 1033 | (defun yank-pop (arg) |
ff1fbe3e RS |
1034 | "Replace just-yanked stretch of killed text with a different stretch. |
1035 | This command is allowed only immediately after a `yank' or a `yank-pop'. | |
2076c87c | 1036 | At such a time, the region contains a stretch of reinserted |
ff1fbe3e | 1037 | previously-killed text. `yank-pop' deletes that text and inserts in its |
2076c87c JB |
1038 | place a different stretch of killed text. |
1039 | ||
1040 | With no argument, the previous kill is inserted. | |
ff1fbe3e RS |
1041 | With argument N, insert the Nth previous kill. |
1042 | If N is negative, this is a more recent kill. | |
2076c87c JB |
1043 | |
1044 | The sequence of kills wraps around, so that after the oldest one | |
1045 | comes the newest one." | |
1046 | (interactive "*p") | |
1047 | (if (not (eq last-command 'yank)) | |
1048 | (error "Previous command was not a yank")) | |
1049 | (setq this-command 'yank) | |
1050 | (let ((before (< (point) (mark)))) | |
1051 | (delete-region (point) (mark)) | |
2076c87c | 1052 | (set-mark (point)) |
70e14c01 | 1053 | (insert (current-kill arg)) |
2076c87c JB |
1054 | (if before (exchange-point-and-mark)))) |
1055 | ||
1056 | (defun yank (&optional arg) | |
1057 | "Reinsert the last stretch of killed text. | |
1058 | More precisely, reinsert the stretch of killed text most recently | |
ff1fbe3e RS |
1059 | killed OR yanked. Put point at end, and set mark at beginning. |
1060 | With just C-u as argument, same but put point at beginning (and mark at end). | |
1061 | With argument N, reinsert the Nth most recently killed stretch of killed | |
2076c87c JB |
1062 | text. |
1063 | See also the command \\[yank-pop]." | |
1064 | (interactive "*P") | |
2076c87c | 1065 | (push-mark (point)) |
70e14c01 JB |
1066 | (insert (current-kill (cond |
1067 | ((listp arg) 0) | |
1068 | ((eq arg '-) -1) | |
1069 | (t (1- arg))))) | |
2076c87c JB |
1070 | (if (consp arg) |
1071 | (exchange-point-and-mark))) | |
70e14c01 JB |
1072 | |
1073 | (defun rotate-yank-pointer (arg) | |
1074 | "Rotate the yanking point in the kill ring. | |
1075 | With argument, rotate that many kills forward (or backward, if negative)." | |
1076 | (interactive "p") | |
1077 | (current-kill arg)) | |
1078 | ||
2076c87c JB |
1079 | \f |
1080 | (defun insert-buffer (buffer) | |
1081 | "Insert after point the contents of BUFFER. | |
1082 | Puts mark after the inserted text. | |
1083 | BUFFER may be a buffer or a buffer name." | |
58ff020d RS |
1084 | (interactive (list (progn (barf-if-buffer-read-only) |
1085 | (read-buffer "Insert buffer: " (other-buffer) t)))) | |
2076c87c JB |
1086 | (or (bufferp buffer) |
1087 | (setq buffer (get-buffer buffer))) | |
1088 | (let (start end newmark) | |
1089 | (save-excursion | |
1090 | (save-excursion | |
1091 | (set-buffer buffer) | |
1092 | (setq start (point-min) end (point-max))) | |
1093 | (insert-buffer-substring buffer start end) | |
1094 | (setq newmark (point))) | |
1095 | (push-mark newmark))) | |
1096 | ||
1097 | (defun append-to-buffer (buffer start end) | |
1098 | "Append to specified buffer the text of the region. | |
1099 | It is inserted into that buffer before its point. | |
1100 | ||
1101 | When calling from a program, give three arguments: | |
1102 | BUFFER (or buffer name), START and END. | |
1103 | START and END specify the portion of the current buffer to be copied." | |
70e14c01 JB |
1104 | (interactive |
1105 | (list (read-buffer "Append to buffer: " (other-buffer nil t) t))) | |
2076c87c JB |
1106 | (let ((oldbuf (current-buffer))) |
1107 | (save-excursion | |
1108 | (set-buffer (get-buffer-create buffer)) | |
1109 | (insert-buffer-substring oldbuf start end)))) | |
1110 | ||
1111 | (defun prepend-to-buffer (buffer start end) | |
1112 | "Prepend to specified buffer the text of the region. | |
1113 | It is inserted into that buffer after its point. | |
1114 | ||
1115 | When calling from a program, give three arguments: | |
1116 | BUFFER (or buffer name), START and END. | |
1117 | START and END specify the portion of the current buffer to be copied." | |
1118 | (interactive "BPrepend to buffer: \nr") | |
1119 | (let ((oldbuf (current-buffer))) | |
1120 | (save-excursion | |
1121 | (set-buffer (get-buffer-create buffer)) | |
1122 | (save-excursion | |
1123 | (insert-buffer-substring oldbuf start end))))) | |
1124 | ||
1125 | (defun copy-to-buffer (buffer start end) | |
1126 | "Copy to specified buffer the text of the region. | |
1127 | It is inserted into that buffer, replacing existing text there. | |
1128 | ||
1129 | When calling from a program, give three arguments: | |
1130 | BUFFER (or buffer name), START and END. | |
1131 | START and END specify the portion of the current buffer to be copied." | |
1132 | (interactive "BCopy to buffer: \nr") | |
1133 | (let ((oldbuf (current-buffer))) | |
1134 | (save-excursion | |
1135 | (set-buffer (get-buffer-create buffer)) | |
1136 | (erase-buffer) | |
1137 | (save-excursion | |
1138 | (insert-buffer-substring oldbuf start end))))) | |
1139 | \f | |
1140 | (defun mark () | |
1141 | "Return this buffer's mark value as integer, or nil if no mark. | |
1142 | If you are using this in an editing command, you are most likely making | |
1143 | a mistake; see the documentation of `set-mark'." | |
1144 | (marker-position (mark-marker))) | |
1145 | ||
1146 | (defun set-mark (pos) | |
1147 | "Set this buffer's mark to POS. Don't use this function! | |
1148 | That is to say, don't use this function unless you want | |
1149 | the user to see that the mark has moved, and you want the previous | |
1150 | mark position to be lost. | |
1151 | ||
1152 | Normally, when a new mark is set, the old one should go on the stack. | |
1153 | This is why most applications should use push-mark, not set-mark. | |
1154 | ||
ff1fbe3e | 1155 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
1156 | purposes. The mark saves a location for the user's convenience. |
1157 | Most editing commands should not alter the mark. | |
1158 | To remember a location for internal use in the Lisp program, | |
1159 | store it in a Lisp variable. Example: | |
1160 | ||
1161 | (let ((beg (point))) (forward-line 1) (delete-region beg (point)))." | |
1162 | ||
1163 | (set-marker (mark-marker) pos (current-buffer))) | |
1164 | ||
1165 | (defvar mark-ring nil | |
1166 | "The list of saved former marks of the current buffer, | |
1167 | most recent first.") | |
1168 | (make-variable-buffer-local 'mark-ring) | |
1169 | ||
1170 | (defconst mark-ring-max 16 | |
1171 | "*Maximum size of mark ring. Start discarding off end if gets this big.") | |
1172 | ||
1173 | (defun set-mark-command (arg) | |
1174 | "Set mark at where point is, or jump to mark. | |
79926889 JB |
1175 | With no prefix argument, set mark, and push old mark position on mark ring. |
1176 | With argument, jump to mark, and pop a new position for mark off the ring. | |
2076c87c | 1177 | |
ff1fbe3e | 1178 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
1179 | purposes. See the documentation of `set-mark' for more information." |
1180 | (interactive "P") | |
1181 | (if (null arg) | |
1182 | (push-mark) | |
1183 | (if (null (mark)) | |
1184 | (error "No mark set in this buffer") | |
1185 | (goto-char (mark)) | |
1186 | (pop-mark)))) | |
1187 | ||
1188 | (defun push-mark (&optional location nomsg) | |
1189 | "Set mark at LOCATION (point, by default) and push old mark on mark ring. | |
1190 | Displays \"Mark set\" unless the optional second arg NOMSG is non-nil. | |
1191 | ||
ff1fbe3e | 1192 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
1193 | purposes. See the documentation of `set-mark' for more information." |
1194 | (if (null (mark)) | |
1195 | nil | |
1196 | (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring)) | |
1197 | (if (> (length mark-ring) mark-ring-max) | |
1198 | (progn | |
1199 | (move-marker (car (nthcdr mark-ring-max mark-ring)) nil) | |
1200 | (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))) | |
1201 | (set-mark (or location (point))) | |
1202 | (or nomsg executing-macro (> (minibuffer-depth) 0) | |
1203 | (message "Mark set")) | |
1204 | nil) | |
1205 | ||
1206 | (defun pop-mark () | |
1207 | "Pop off mark ring into the buffer's actual mark. | |
1208 | Does not set point. Does nothing if mark ring is empty." | |
1209 | (if mark-ring | |
1210 | (progn | |
1211 | (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker))))) | |
1212 | (set-mark (+ 0 (car mark-ring))) | |
1213 | (move-marker (car mark-ring) nil) | |
1214 | (if (null (mark)) (ding)) | |
1215 | (setq mark-ring (cdr mark-ring))))) | |
1216 | ||
1217 | (fset 'exchange-dot-and-mark 'exchange-point-and-mark) | |
1218 | (defun exchange-point-and-mark () | |
1219 | "Put the mark where point is now, and point where the mark is now." | |
1220 | (interactive nil) | |
1221 | (let ((omark (mark))) | |
1222 | (if (null omark) | |
1223 | (error "No mark set in this buffer")) | |
1224 | (set-mark (point)) | |
1225 | (goto-char omark) | |
1226 | nil)) | |
1227 | \f | |
1228 | (defun next-line (arg) | |
1229 | "Move cursor vertically down ARG lines. | |
1230 | If there is no character in the target line exactly under the current column, | |
1231 | the cursor is positioned after the character in that line which spans this | |
1232 | column, or at the end of the line if it is not long enough. | |
1233 | If there is no line in the buffer after this one, | |
1234 | a newline character is inserted to create a line | |
1235 | and the cursor moves to that line. | |
1236 | ||
1237 | The command \\[set-goal-column] can be used to create | |
1238 | a semipermanent goal column to which this command always moves. | |
1239 | Then it does not try to move vertically. This goal column is stored | |
1240 | in `goal-column', which is nil when there is none. | |
1241 | ||
1242 | If you are thinking of using this in a Lisp program, consider | |
1243 | using `forward-line' instead. It is usually easier to use | |
1244 | and more reliable (no dependence on goal column, etc.)." | |
1245 | (interactive "p") | |
1246 | (if (= arg 1) | |
1247 | (let ((opoint (point))) | |
1248 | (forward-line 1) | |
1249 | (if (or (= opoint (point)) | |
1250 | (not (eq (preceding-char) ?\n))) | |
1251 | (insert ?\n) | |
1252 | (goto-char opoint) | |
1253 | (line-move arg))) | |
1254 | (line-move arg)) | |
1255 | nil) | |
1256 | ||
1257 | (defun previous-line (arg) | |
1258 | "Move cursor vertically up ARG lines. | |
1259 | If there is no character in the target line exactly over the current column, | |
1260 | the cursor is positioned after the character in that line which spans this | |
1261 | column, or at the end of the line if it is not long enough. | |
1262 | ||
1263 | The command \\[set-goal-column] can be used to create | |
1264 | a semipermanent goal column to which this command always moves. | |
1265 | Then it does not try to move vertically. | |
1266 | ||
1267 | If you are thinking of using this in a Lisp program, consider using | |
c2e8a012 | 1268 | `forward-line' with a negative argument instead. It is usually easier |
2076c87c JB |
1269 | to use and more reliable (no dependence on goal column, etc.)." |
1270 | (interactive "p") | |
1271 | (line-move (- arg)) | |
1272 | nil) | |
1273 | ||
1274 | (defconst track-eol nil | |
1275 | "*Non-nil means vertical motion starting at end of line keeps to ends of lines. | |
1276 | This means moving to the end of each line moved onto. | |
1277 | The beginning of a blank line does not count as the end of a line.") | |
1278 | ||
912c6728 RS |
1279 | (defvar goal-column nil |
1280 | "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil.") | |
1281 | (make-variable-buffer-local 'goal-column) | |
2076c87c JB |
1282 | |
1283 | (defvar temporary-goal-column 0 | |
1284 | "Current goal column for vertical motion. | |
1285 | It is the column where point was | |
1286 | at the start of current run of vertical motion commands. | |
c637ae6f | 1287 | When the `track-eol' feature is doing its job, the value is 9999.") |
2076c87c JB |
1288 | |
1289 | (defun line-move (arg) | |
1290 | (if (not (or (eq last-command 'next-line) | |
1291 | (eq last-command 'previous-line))) | |
1292 | (setq temporary-goal-column | |
1293 | (if (and track-eol (eolp) | |
1294 | ;; Don't count beg of empty line as end of line | |
1295 | ;; unless we just did explicit end-of-line. | |
1296 | (or (not (bolp)) (eq last-command 'end-of-line))) | |
1297 | 9999 | |
1298 | (current-column)))) | |
1299 | (if (not (integerp selective-display)) | |
1300 | (forward-line arg) | |
1301 | ;; Move by arg lines, but ignore invisible ones. | |
1302 | (while (> arg 0) | |
1303 | (vertical-motion 1) | |
1304 | (forward-char -1) | |
1305 | (forward-line 1) | |
1306 | (setq arg (1- arg))) | |
1307 | (while (< arg 0) | |
1308 | (vertical-motion -1) | |
1309 | (beginning-of-line) | |
1310 | (setq arg (1+ arg)))) | |
1311 | (move-to-column (or goal-column temporary-goal-column)) | |
1312 | nil) | |
1313 | ||
d5ab2033 JB |
1314 | ;;; Many people have said they rarely use this feature, and often type |
1315 | ;;; it by accident. Maybe it shouldn't even be on a key. | |
1316 | (put 'set-goal-column 'disabled t) | |
2076c87c JB |
1317 | |
1318 | (defun set-goal-column (arg) | |
1319 | "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line]. | |
1320 | Those commands will move to this position in the line moved to | |
1321 | rather than trying to keep the same horizontal position. | |
1322 | With a non-nil argument, clears out the goal column | |
912c6728 RS |
1323 | so that \\[next-line] and \\[previous-line] resume vertical motion. |
1324 | The goal column is stored in the variable `goal-column'." | |
2076c87c JB |
1325 | (interactive "P") |
1326 | (if arg | |
1327 | (progn | |
1328 | (setq goal-column nil) | |
1329 | (message "No goal column")) | |
1330 | (setq goal-column (current-column)) | |
1331 | (message (substitute-command-keys | |
1332 | "Goal column %d (use \\[set-goal-column] with an arg to unset it)") | |
1333 | goal-column)) | |
1334 | nil) | |
1335 | \f | |
1336 | (defun transpose-chars (arg) | |
1337 | "Interchange characters around point, moving forward one character. | |
1338 | With prefix arg ARG, effect is to take character before point | |
1339 | and drag it forward past ARG other characters (backward if ARG negative). | |
1340 | If no argument and at end of line, the previous two chars are exchanged." | |
1341 | (interactive "*P") | |
1342 | (and (null arg) (eolp) (forward-char -1)) | |
1343 | (transpose-subr 'forward-char (prefix-numeric-value arg))) | |
1344 | ||
1345 | (defun transpose-words (arg) | |
1346 | "Interchange words around point, leaving point at end of them. | |
1347 | With prefix arg ARG, effect is to take word before or around point | |
1348 | and drag it forward past ARG other words (backward if ARG negative). | |
1349 | If ARG is zero, the words around or after point and around or after mark | |
1350 | are interchanged." | |
1351 | (interactive "*p") | |
1352 | (transpose-subr 'forward-word arg)) | |
1353 | ||
1354 | (defun transpose-sexps (arg) | |
1355 | "Like \\[transpose-words] but applies to sexps. | |
1356 | Does not work on a sexp that point is in the middle of | |
1357 | if it is a list or string." | |
1358 | (interactive "*p") | |
1359 | (transpose-subr 'forward-sexp arg)) | |
1360 | ||
1361 | (defun transpose-lines (arg) | |
1362 | "Exchange current line and previous line, leaving point after both. | |
1363 | With argument ARG, takes previous line and moves it past ARG lines. | |
1364 | With argument 0, interchanges line point is in with line mark is in." | |
1365 | (interactive "*p") | |
1366 | (transpose-subr (function | |
1367 | (lambda (arg) | |
1368 | (if (= arg 1) | |
1369 | (progn | |
1370 | ;; Move forward over a line, | |
1371 | ;; but create a newline if none exists yet. | |
1372 | (end-of-line) | |
1373 | (if (eobp) | |
1374 | (newline) | |
1375 | (forward-char 1))) | |
1376 | (forward-line arg)))) | |
1377 | arg)) | |
1378 | ||
1379 | (defun transpose-subr (mover arg) | |
1380 | (let (start1 end1 start2 end2) | |
1381 | (if (= arg 0) | |
1382 | (progn | |
1383 | (save-excursion | |
1384 | (funcall mover 1) | |
1385 | (setq end2 (point)) | |
1386 | (funcall mover -1) | |
1387 | (setq start2 (point)) | |
1388 | (goto-char (mark)) | |
1389 | (funcall mover 1) | |
1390 | (setq end1 (point)) | |
1391 | (funcall mover -1) | |
1392 | (setq start1 (point)) | |
1393 | (transpose-subr-1)) | |
1394 | (exchange-point-and-mark))) | |
1395 | (while (> arg 0) | |
1396 | (funcall mover -1) | |
1397 | (setq start1 (point)) | |
1398 | (funcall mover 1) | |
1399 | (setq end1 (point)) | |
1400 | (funcall mover 1) | |
1401 | (setq end2 (point)) | |
1402 | (funcall mover -1) | |
1403 | (setq start2 (point)) | |
1404 | (transpose-subr-1) | |
1405 | (goto-char end2) | |
1406 | (setq arg (1- arg))) | |
1407 | (while (< arg 0) | |
1408 | (funcall mover -1) | |
1409 | (setq start2 (point)) | |
1410 | (funcall mover -1) | |
1411 | (setq start1 (point)) | |
1412 | (funcall mover 1) | |
1413 | (setq end1 (point)) | |
1414 | (funcall mover 1) | |
1415 | (setq end2 (point)) | |
1416 | (transpose-subr-1) | |
1417 | (setq arg (1+ arg))))) | |
1418 | ||
1419 | (defun transpose-subr-1 () | |
1420 | (if (> (min end1 end2) (max start1 start2)) | |
1421 | (error "Don't have two things to transpose")) | |
1422 | (let ((word1 (buffer-substring start1 end1)) | |
1423 | (word2 (buffer-substring start2 end2))) | |
1424 | (delete-region start2 end2) | |
1425 | (goto-char start2) | |
1426 | (insert word1) | |
1427 | (goto-char (if (< start1 start2) start1 | |
1428 | (+ start1 (- (length word1) (length word2))))) | |
1429 | (delete-char (length word1)) | |
1430 | (insert word2))) | |
1431 | \f | |
1432 | (defconst comment-column 32 | |
1433 | "*Column to indent right-margin comments to. | |
8a8fa723 JB |
1434 | Setting this variable automatically makes it local to the current buffer. |
1435 | Each mode establishes a different default value for this variable; you | |
1436 | can the value for a particular mode using that mode's hook.") | |
2076c87c JB |
1437 | (make-variable-buffer-local 'comment-column) |
1438 | ||
1439 | (defconst comment-start nil | |
1440 | "*String to insert to start a new comment, or nil if no comment syntax defined.") | |
1441 | ||
1442 | (defconst comment-start-skip nil | |
1443 | "*Regexp to match the start of a comment plus everything up to its body. | |
1444 | If there are any \\(...\\) pairs, the comment delimiter text is held to begin | |
1445 | at the place matched by the close of the first pair.") | |
1446 | ||
1447 | (defconst comment-end "" | |
1448 | "*String to insert to end a new comment. | |
1449 | Should be an empty string if comments are terminated by end-of-line.") | |
1450 | ||
1451 | (defconst comment-indent-hook | |
1452 | '(lambda () comment-column) | |
1453 | "Function to compute desired indentation for a comment. | |
1454 | This function is called with no args with point at the beginning of | |
1455 | the comment's starting delimiter.") | |
1456 | ||
1457 | (defun indent-for-comment () | |
1458 | "Indent this line's comment to comment column, or insert an empty comment." | |
1459 | (interactive "*") | |
1460 | (beginning-of-line 1) | |
1461 | (if (null comment-start) | |
1462 | (error "No comment syntax defined") | |
1463 | (let* ((eolpos (save-excursion (end-of-line) (point))) | |
1464 | cpos indent begpos) | |
1465 | (if (re-search-forward comment-start-skip eolpos 'move) | |
1466 | (progn (setq cpos (point-marker)) | |
1467 | ;; Find the start of the comment delimiter. | |
1468 | ;; If there were paren-pairs in comment-start-skip, | |
1469 | ;; position at the end of the first pair. | |
1470 | (if (match-end 1) | |
1471 | (goto-char (match-end 1)) | |
1472 | ;; If comment-start-skip matched a string with internal | |
1473 | ;; whitespace (not final whitespace) then the delimiter | |
1474 | ;; start at the end of that whitespace. | |
1475 | ;; Otherwise, it starts at the beginning of what was matched. | |
1476 | (skip-chars-backward " \t" (match-beginning 0)) | |
1477 | (skip-chars-backward "^ \t" (match-beginning 0))))) | |
1478 | (setq begpos (point)) | |
1479 | ;; Compute desired indent. | |
1480 | (if (= (current-column) | |
1481 | (setq indent (funcall comment-indent-hook))) | |
1482 | (goto-char begpos) | |
1483 | ;; If that's different from current, change it. | |
1484 | (skip-chars-backward " \t") | |
1485 | (delete-region (point) begpos) | |
1486 | (indent-to indent)) | |
1487 | ;; An existing comment? | |
1488 | (if cpos | |
1489 | (progn (goto-char cpos) | |
1490 | (set-marker cpos nil)) | |
1491 | ;; No, insert one. | |
1492 | (insert comment-start) | |
1493 | (save-excursion | |
1494 | (insert comment-end)))))) | |
1495 | ||
1496 | (defun set-comment-column (arg) | |
1497 | "Set the comment column based on point. | |
1498 | With no arg, set the comment column to the current column. | |
1499 | With just minus as arg, kill any comment on this line. | |
1500 | With any other arg, set comment column to indentation of the previous comment | |
1501 | and then align or create a comment on this line at that column." | |
1502 | (interactive "P") | |
1503 | (if (eq arg '-) | |
1504 | (kill-comment nil) | |
1505 | (if arg | |
1506 | (progn | |
1507 | (save-excursion | |
1508 | (beginning-of-line) | |
1509 | (re-search-backward comment-start-skip) | |
1510 | (beginning-of-line) | |
1511 | (re-search-forward comment-start-skip) | |
1512 | (goto-char (match-beginning 0)) | |
1513 | (setq comment-column (current-column)) | |
1514 | (message "Comment column set to %d" comment-column)) | |
1515 | (indent-for-comment)) | |
1516 | (setq comment-column (current-column)) | |
1517 | (message "Comment column set to %d" comment-column)))) | |
1518 | ||
1519 | (defun kill-comment (arg) | |
1520 | "Kill the comment on this line, if any. | |
1521 | With argument, kill comments on that many lines starting with this one." | |
1522 | ;; this function loses in a lot of situations. it incorrectly recognises | |
1523 | ;; comment delimiters sometimes (ergo, inside a string), doesn't work | |
1524 | ;; with multi-line comments, can kill extra whitespace if comment wasn't | |
1525 | ;; through end-of-line, et cetera. | |
1526 | (interactive "P") | |
1527 | (or comment-start-skip (error "No comment syntax defined")) | |
1528 | (let ((count (prefix-numeric-value arg)) endc) | |
1529 | (while (> count 0) | |
1530 | (save-excursion | |
1531 | (end-of-line) | |
1532 | (setq endc (point)) | |
1533 | (beginning-of-line) | |
1534 | (and (string< "" comment-end) | |
1535 | (setq endc | |
1536 | (progn | |
1537 | (re-search-forward (regexp-quote comment-end) endc 'move) | |
1538 | (skip-chars-forward " \t") | |
1539 | (point)))) | |
1540 | (beginning-of-line) | |
1541 | (if (re-search-forward comment-start-skip endc t) | |
1542 | (progn | |
1543 | (goto-char (match-beginning 0)) | |
1544 | (skip-chars-backward " \t") | |
1545 | (kill-region (point) endc) | |
1546 | ;; to catch comments a line beginnings | |
1547 | (indent-according-to-mode)))) | |
1548 | (if arg (forward-line 1)) | |
1549 | (setq count (1- count))))) | |
1550 | ||
1551 | (defun comment-region (beg end &optional arg) | |
1552 | "Comment the region; third arg numeric means use ARG comment characters. | |
1553 | If ARG is negative, delete that many comment characters instead. | |
1554 | Comments are terminated on each line, even for syntax in which newline does | |
1555 | not end the comment. Blank lines do not get comments." | |
1556 | ;; if someone wants it to only put a comment-start at the beginning and | |
1557 | ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x | |
1558 | ;; is easy enough. No option is made here for other than commenting | |
1559 | ;; every line. | |
1560 | (interactive "r\np") | |
1561 | (or comment-start (error "No comment syntax is defined")) | |
1562 | (if (> beg end) (let (mid) (setq mid beg beg end end mid))) | |
1563 | (save-excursion | |
1564 | (save-restriction | |
1565 | (let ((cs comment-start) (ce comment-end)) | |
1566 | (cond ((not arg) (setq arg 1)) | |
1567 | ((> arg 1) | |
1568 | (while (> (setq arg (1- arg)) 0) | |
1569 | (setq cs (concat cs comment-start) | |
1570 | ce (concat ce comment-end))))) | |
1571 | (narrow-to-region beg end) | |
1572 | (goto-char beg) | |
1573 | (while (not (eobp)) | |
1574 | (if (< arg 0) | |
1575 | (let ((count arg)) | |
1576 | (while (and (> 1 (setq count (1+ count))) | |
1577 | (looking-at (regexp-quote cs))) | |
1578 | (delete-char (length cs))) | |
1579 | (if (string= "" ce) () | |
1580 | (setq count arg) | |
1581 | (while (> 1 (setq count (1+ count))) | |
1582 | (end-of-line) | |
1583 | ;; this is questionable if comment-end ends in whitespace | |
1584 | ;; that is pretty brain-damaged though | |
1585 | (skip-chars-backward " \t") | |
1586 | (backward-char (length ce)) | |
1587 | (if (looking-at (regexp-quote ce)) | |
6e88ed49 RS |
1588 | (delete-char (length ce))))) |
1589 | (forward-line 1)) | |
2076c87c JB |
1590 | (if (looking-at "[ \t]*$") () |
1591 | (insert cs) | |
1592 | (if (string= "" ce) () | |
1593 | (end-of-line) | |
1594 | (insert ce))) | |
1595 | (search-forward "\n" nil 'move))))))) | |
1596 | \f | |
1597 | (defun backward-word (arg) | |
1598 | "Move backward until encountering the end of a word. | |
1599 | With argument, do this that many times. | |
ff1fbe3e | 1600 | In programs, it is faster to call `forward-word' with negative arg." |
2076c87c JB |
1601 | (interactive "p") |
1602 | (forward-word (- arg))) | |
1603 | ||
1604 | (defun mark-word (arg) | |
1605 | "Set mark arg words away from point." | |
1606 | (interactive "p") | |
1607 | (push-mark | |
1608 | (save-excursion | |
1609 | (forward-word arg) | |
1610 | (point)))) | |
1611 | ||
1612 | (defun kill-word (arg) | |
1613 | "Kill characters forward until encountering the end of a word. | |
1614 | With argument, do this that many times." | |
1615 | (interactive "p") | |
01b8e020 | 1616 | (kill-region (point) (save-excursion (forward-word arg) (point)))) |
2076c87c JB |
1617 | |
1618 | (defun backward-kill-word (arg) | |
1619 | "Kill characters backward until encountering the end of a word. | |
1620 | With argument, do this that many times." | |
1621 | (interactive "p") | |
1622 | (kill-word (- arg))) | |
1623 | \f | |
1624 | (defconst fill-prefix nil | |
1625 | "*String for filling to insert at front of new line, or nil for none. | |
1626 | Setting this variable automatically makes it local to the current buffer.") | |
1627 | (make-variable-buffer-local 'fill-prefix) | |
1628 | ||
1629 | (defconst auto-fill-inhibit-regexp nil | |
1630 | "*Regexp to match lines which should not be auto-filled.") | |
1631 | ||
1632 | (defun do-auto-fill () | |
1633 | (let (give-up) | |
1634 | (or (and auto-fill-inhibit-regexp | |
1635 | (save-excursion (beginning-of-line) | |
1636 | (looking-at auto-fill-inhibit-regexp))) | |
1637 | (while (and (not give-up) (> (current-column) fill-column)) | |
1638 | (let ((fill-point | |
1639 | (let ((opoint (point))) | |
1640 | (save-excursion | |
1641 | (move-to-column (1+ fill-column)) | |
1642 | (skip-chars-backward "^ \t\n") | |
1643 | (if (bolp) | |
1644 | (re-search-forward "[ \t]" opoint t)) | |
1645 | (skip-chars-backward " \t") | |
1646 | (point))))) | |
1647 | ;; If there is a space on the line before fill-point, | |
1648 | ;; and nonspaces precede it, break the line there. | |
1649 | (if (save-excursion | |
1650 | (goto-char fill-point) | |
1651 | (not (bolp))) | |
1652 | ;; If point is at the fill-point, do not `save-excursion'. | |
1653 | ;; Otherwise, if a comment prefix or fill-prefix is inserted, | |
1654 | ;; point will end up before it rather than after it. | |
1655 | (if (save-excursion | |
1656 | (skip-chars-backward " \t") | |
1657 | (= (point) fill-point)) | |
1658 | (indent-new-comment-line) | |
1659 | (save-excursion | |
1660 | (goto-char fill-point) | |
1661 | (indent-new-comment-line))) | |
1662 | ;; No place to break => stop trying. | |
1663 | (setq give-up t))))))) | |
1664 | ||
1665 | (defconst comment-multi-line nil | |
1666 | "*Non-nil means \\[indent-new-comment-line] should continue same comment | |
c88ab9ce ER |
1667 | on new line, with no new terminator or starter. |
1668 | This is obsolete because you might as well use \\[newline-and-indent].") | |
2076c87c JB |
1669 | |
1670 | (defun indent-new-comment-line () | |
1671 | "Break line at point and indent, continuing comment if presently within one. | |
c88ab9ce ER |
1672 | The body of the continued comment is indented under the previous comment line. |
1673 | ||
1674 | This command is intended for styles where you write a comment per line, | |
1675 | starting a new comment (and terminating it if necessary) on each line. | |
1676 | If you want to continue one comment across several lines, use \\[newline-and-indent]." | |
2076c87c JB |
1677 | (interactive "*") |
1678 | (let (comcol comstart) | |
1679 | (skip-chars-backward " \t") | |
1680 | (delete-region (point) | |
1681 | (progn (skip-chars-forward " \t") | |
1682 | (point))) | |
1683 | (insert ?\n) | |
c88ab9ce ER |
1684 | (if (not comment-multi-line) |
1685 | (save-excursion | |
1686 | (if (and comment-start-skip | |
1687 | (let ((opoint (point))) | |
1688 | (forward-line -1) | |
1689 | (re-search-forward comment-start-skip opoint t))) | |
1690 | ;; The old line is a comment. | |
1691 | ;; Set WIN to the pos of the comment-start. | |
1692 | ;; But if the comment is empty, look at preceding lines | |
1693 | ;; to find one that has a nonempty comment. | |
1694 | (let ((win (match-beginning 0))) | |
1695 | (while (and (eolp) (not (bobp)) | |
1696 | (let (opoint) | |
1697 | (beginning-of-line) | |
1698 | (setq opoint (point)) | |
1699 | (forward-line -1) | |
1700 | (re-search-forward comment-start-skip opoint t))) | |
1701 | (setq win (match-beginning 0))) | |
1702 | ;; Indent this line like what we found. | |
1703 | (goto-char win) | |
1704 | (setq comcol (current-column)) | |
1705 | (setq comstart (buffer-substring (point) (match-end 0))))))) | |
2076c87c JB |
1706 | (if comcol |
1707 | (let ((comment-column comcol) | |
1708 | (comment-start comstart) | |
1709 | (comment-end comment-end)) | |
1710 | (and comment-end (not (equal comment-end "")) | |
c88ab9ce | 1711 | ; (if (not comment-multi-line) |
2076c87c JB |
1712 | (progn |
1713 | (forward-char -1) | |
1714 | (insert comment-end) | |
1715 | (forward-char 1)) | |
c88ab9ce ER |
1716 | ; (setq comment-column (+ comment-column (length comment-start)) |
1717 | ; comment-start "") | |
1718 | ; ) | |
1719 | ) | |
2076c87c JB |
1720 | (if (not (eolp)) |
1721 | (setq comment-end "")) | |
1722 | (insert ?\n) | |
1723 | (forward-char -1) | |
1724 | (indent-for-comment) | |
1725 | (save-excursion | |
1726 | ;; Make sure we delete the newline inserted above. | |
1727 | (end-of-line) | |
1728 | (delete-char 1))) | |
1729 | (if fill-prefix | |
1730 | (insert fill-prefix) | |
1731 | (indent-according-to-mode))))) | |
1732 | ||
1733 | (defun auto-fill-mode (&optional arg) | |
1734 | "Toggle auto-fill mode. | |
1735 | With arg, turn auto-fill mode on if and only if arg is positive. | |
1736 | In auto-fill mode, inserting a space at a column beyond fill-column | |
1737 | automatically breaks the line at a previous space." | |
1738 | (interactive "P") | |
1739 | (prog1 (setq auto-fill-function | |
1740 | (if (if (null arg) | |
1741 | (not auto-fill-function) | |
1742 | (> (prefix-numeric-value arg) 0)) | |
1743 | 'do-auto-fill | |
1744 | nil)) | |
1745 | ;; update mode-line | |
1746 | (set-buffer-modified-p (buffer-modified-p)))) | |
1747 | ||
1748 | (defun turn-on-auto-fill () | |
1749 | "Unconditionally turn on Auto Fill mode." | |
1750 | (auto-fill-mode 1)) | |
1751 | ||
1752 | (defun set-fill-column (arg) | |
ff1fbe3e RS |
1753 | "Set `fill-column' to current column, or to argument if given. |
1754 | The variable `fill-column' has a separate value for each buffer." | |
2076c87c JB |
1755 | (interactive "P") |
1756 | (setq fill-column (if (integerp arg) arg (current-column))) | |
1757 | (message "fill-column set to %d" fill-column)) | |
1758 | \f | |
1759 | (defun set-selective-display (arg) | |
ff1fbe3e RS |
1760 | "Set `selective-display' to ARG; clear it if no arg. |
1761 | When the value of `selective-display' is a number > 0, | |
1762 | lines whose indentation is >= that value are not displayed. | |
1763 | The variable `selective-display' has a separate value for each buffer." | |
2076c87c JB |
1764 | (interactive "P") |
1765 | (if (eq selective-display t) | |
1766 | (error "selective-display already in use for marked lines")) | |
c88ab9ce ER |
1767 | (let ((current-vpos |
1768 | (save-restriction | |
1769 | (narrow-to-region (point-min) (point)) | |
1770 | (goto-char (window-start)) | |
1771 | (vertical-motion (window-height))))) | |
1772 | (setq selective-display | |
1773 | (and arg (prefix-numeric-value arg))) | |
1774 | (recenter current-vpos)) | |
2076c87c JB |
1775 | (set-window-start (selected-window) (window-start (selected-window))) |
1776 | (princ "selective-display set to " t) | |
1777 | (prin1 selective-display t) | |
1778 | (princ "." t)) | |
1779 | ||
1780 | (defun overwrite-mode (arg) | |
1781 | "Toggle overwrite mode. | |
1782 | With arg, turn overwrite mode on iff arg is positive. | |
1783 | In overwrite mode, printing characters typed in replace existing text | |
1784 | on a one-for-one basis, rather than pushing it to the right." | |
1785 | (interactive "P") | |
1786 | (setq overwrite-mode | |
1787 | (if (null arg) (not overwrite-mode) | |
1788 | (> (prefix-numeric-value arg) 0))) | |
1789 | (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line. | |
1790 | \f | |
1791 | (defvar blink-matching-paren t | |
1792 | "*Non-nil means show matching open-paren when close-paren is inserted.") | |
1793 | ||
1794 | (defconst blink-matching-paren-distance 4000 | |
1795 | "*If non-nil, is maximum distance to search for matching open-paren | |
1796 | when close-paren is inserted.") | |
1797 | ||
1798 | (defun blink-matching-open () | |
1799 | "Move cursor momentarily to the beginning of the sexp before point." | |
1800 | (interactive) | |
1801 | (and (> (point) (1+ (point-min))) | |
1802 | (/= (char-syntax (char-after (- (point) 2))) ?\\ ) | |
1803 | blink-matching-paren | |
1804 | (let* ((oldpos (point)) | |
1805 | (blinkpos) | |
1806 | (mismatch)) | |
1807 | (save-excursion | |
1808 | (save-restriction | |
1809 | (if blink-matching-paren-distance | |
1810 | (narrow-to-region (max (point-min) | |
1811 | (- (point) blink-matching-paren-distance)) | |
1812 | oldpos)) | |
1813 | (condition-case () | |
1814 | (setq blinkpos (scan-sexps oldpos -1)) | |
1815 | (error nil))) | |
1816 | (and blinkpos (/= (char-syntax (char-after blinkpos)) | |
1817 | ?\$) | |
1818 | (setq mismatch | |
1819 | (/= (char-after (1- oldpos)) | |
1820 | (logand (lsh (aref (syntax-table) | |
1821 | (char-after blinkpos)) | |
1822 | -8) | |
1823 | 255)))) | |
1824 | (if mismatch (setq blinkpos nil)) | |
1825 | (if blinkpos | |
1826 | (progn | |
1827 | (goto-char blinkpos) | |
1828 | (if (pos-visible-in-window-p) | |
1829 | (sit-for 1) | |
1830 | (goto-char blinkpos) | |
1831 | (message | |
1832 | "Matches %s" | |
1833 | (if (save-excursion | |
1834 | (skip-chars-backward " \t") | |
1835 | (not (bolp))) | |
1836 | (buffer-substring (progn (beginning-of-line) (point)) | |
1837 | (1+ blinkpos)) | |
1838 | (buffer-substring blinkpos | |
1839 | (progn | |
1840 | (forward-char 1) | |
1841 | (skip-chars-forward "\n \t") | |
1842 | (end-of-line) | |
1843 | (point))))))) | |
1844 | (cond (mismatch | |
1845 | (message "Mismatched parentheses")) | |
1846 | ((not blink-matching-paren-distance) | |
1847 | (message "Unmatched parenthesis")))))))) | |
1848 | ||
1849 | ;Turned off because it makes dbx bomb out. | |
1850 | (setq blink-paren-function 'blink-matching-open) | |
1851 | ||
1852 | ; this is just something for the luser to see in a keymap -- this is not | |
1853 | ; how quitting works normally! | |
1854 | (defun keyboard-quit () | |
1855 | "Signal a quit condition." | |
1856 | (interactive) | |
1857 | (signal 'quit nil)) | |
1858 | ||
1859 | (define-key global-map "\C-g" 'keyboard-quit) | |
1860 | \f | |
1861 | (defun set-variable (var val) | |
1862 | "Set VARIABLE to VALUE. VALUE is a Lisp object. | |
1863 | When using this interactively, supply a Lisp expression for VALUE. | |
3a801d0c ER |
1864 | If you want VALUE to be a string, you must surround it with doublequotes. |
1865 | ||
1866 | If VARIABLE has a `variable-interactive' property, that is used as if | |
1867 | it were the arg to `interactive' (which see) to interactively read the value." | |
2076c87c JB |
1868 | (interactive |
1869 | (let* ((var (read-variable "Set variable: ")) | |
1870 | (minibuffer-help-form | |
1871 | '(funcall myhelp)) | |
1872 | (myhelp | |
1873 | (function | |
1874 | (lambda () | |
1875 | (with-output-to-temp-buffer "*Help*" | |
1876 | (prin1 var) | |
1877 | (princ "\nDocumentation:\n") | |
1878 | (princ (substring (documentation-property var 'variable-documentation) | |
1879 | 1)) | |
1880 | (if (boundp var) | |
1881 | (let ((print-length 20)) | |
1882 | (princ "\n\nCurrent value: ") | |
1883 | (prin1 (symbol-value var)))) | |
1884 | nil))))) | |
1885 | (list var | |
3a801d0c ER |
1886 | (let ((prop (get var 'variable-interactive))) |
1887 | (if prop | |
1888 | ;; Use VAR's `variable-interactive' property | |
1889 | ;; as an interactive spec for prompting. | |
1890 | (call-interactively (list 'lambda '(arg) | |
1891 | (list 'interactive prop) | |
1892 | 'arg)) | |
1893 | (eval-minibuffer (format "Set %s to value: " var))))))) | |
2076c87c | 1894 | (set var val)) |
c88ab9ce ER |
1895 | |
1896 | ;;; simple.el ends here |