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