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