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