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