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