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