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