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