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