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