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