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