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