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