1 ;;; simple.el --- basic editing commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1987, 1992 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)
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.
23 (defun open-line (arg)
24 "Insert a newline and leave point before it.
25 If there is a fill prefix, insert the fill prefix after the newline
26 that it inserts. With arg N, insert N newlines."
28 (let ((flag (and (bolp) (not (bobp)))))
29 (if flag (forward-char -1))
33 (if fill-prefix (insert fill-prefix)))
35 (if flag (forward-char 1))))
38 "Split current line, moving portion beyond point vertically down."
40 (skip-chars-forward " \t")
41 (let ((col (current-column))
47 (defun quoted-insert (arg)
48 "Read next input character and insert it.
49 This is useful for inserting control characters.
50 You may also type up to 3 octal digits, to insert a character with that code"
52 (let ((char (read-quoted-char)))
55 (setq arg (1- arg)))))
57 (defun delete-indentation (&optional arg)
58 "Join this line to previous and fix up whitespace at join.
59 If there is a fill prefix, delete it from the beginning of this line.
60 With argument, join this line to following line."
63 (if arg (forward-line 1))
64 (if (eq (preceding-char) ?\n)
66 (delete-region (point) (1- (point)))
67 ;; If the second line started with the fill prefix,
71 (buffer-substring (point)
72 (+ (point) (length fill-prefix)))))
73 (delete-region (point) (+ (point) (length fill-prefix))))
76 (defun fixup-whitespace ()
77 "Fixup white space between objects around point.
78 Leave one space or none, according to the context."
81 (delete-horizontal-space)
82 (if (or (looking-at "^\\|\\s)")
83 (save-excursion (forward-char -1)
84 (looking-at "$\\|\\s(\\|\\s'")))
88 (defun delete-horizontal-space ()
89 "Delete all spaces and tabs around point."
91 (skip-chars-backward " \t")
92 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
94 (defun just-one-space ()
95 "Delete all spaces and tabs around point, leaving one space."
97 (skip-chars-backward " \t")
98 (if (= (following-char) ? )
101 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
103 (defun delete-blank-lines ()
104 "On blank line, delete all surrounding blank lines, leaving just one.
105 On isolated blank line, delete that one.
106 On nonblank line, delete all blank lines that follow it."
108 (let (thisblank singleblank)
111 (setq thisblank (looking-at "[ \t]*$"))
112 ;; Set singleblank if there is just one blank line here.
115 (not (looking-at "[ \t]*\n[ \t]*$"))
117 (progn (forward-line -1)
118 (not (looking-at "[ \t]*$")))))))
119 ;; Delete preceding blank lines, and this one too if it's the only one.
123 (if singleblank (forward-line 1))
124 (delete-region (point)
125 (if (re-search-backward "[^ \t\n]" nil t)
126 (progn (forward-line 1) (point))
128 ;; Delete following blank lines, unless the current line is blank
129 ;; and there are no following blank lines.
130 (if (not (and thisblank singleblank))
134 (delete-region (point)
135 (if (re-search-forward "[^ \t\n]" nil t)
136 (progn (beginning-of-line) (point))
138 ;; Handle the special case where point is followed by newline and eob.
139 ;; Delete the line, leaving point at eob.
140 (if (looking-at "^[ \t]*\n\\'")
141 (delete-region (point) (point-max)))))
143 (defun back-to-indentation ()
144 "Move point to the first non-whitespace character on this line."
146 (beginning-of-line 1)
147 (skip-chars-forward " \t"))
149 (defun newline-and-indent ()
150 "Insert a newline, then indent according to major mode.
151 Indentation is done using the value of `indent-line-function'.
152 In programming language modes, this is the same as TAB.
153 In some text modes, where TAB inserts a tab, this command indents to the
154 column specified by the variable `left-margin'."
156 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
158 (indent-according-to-mode))
160 (defun reindent-then-newline-and-indent ()
161 "Reindent current line, insert newline, then indent the new line.
162 Indentation of both lines is done according to the current major mode,
163 which means calling the current value of `indent-line-function'.
164 In programming language modes, this is the same as TAB.
165 In some text modes, where TAB inserts a tab, this indents to the
166 column specified by the variable `left-margin'."
169 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
170 (indent-according-to-mode))
172 (indent-according-to-mode))
174 ;; Internal subroutine of delete-char
175 (defun kill-forward-chars (arg)
176 (if (listp arg) (setq arg (car arg)))
177 (if (eq arg '-) (setq arg -1))
178 (kill-region (point) (+ (point) arg)))
180 ;; Internal subroutine of backward-delete-char
181 (defun kill-backward-chars (arg)
182 (if (listp arg) (setq arg (car arg)))
183 (if (eq arg '-) (setq arg -1))
184 (kill-region (point) (- (point) arg)))
186 (defun backward-delete-char-untabify (arg &optional killp)
187 "Delete characters backward, changing tabs into spaces.
188 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
189 Interactively, ARG is the prefix arg (default 1)
190 and KILLP is t if prefix arg is was specified."
191 (interactive "*p\nP")
194 (while (and (> count 0) (not (bobp)))
195 (if (= (preceding-char) ?\t)
196 (let ((col (current-column)))
198 (setq col (- col (current-column)))
202 (setq count (1- count)))))
203 (delete-backward-char arg killp)
204 ;; In overwrite mode, back over columns while clearing them out,
205 ;; unless at end of line.
206 (and overwrite-mode (not (eolp))
207 (save-excursion (insert-char ?\ arg))))
209 (defun zap-to-char (arg char)
210 "Kill up to and including ARG'th occurrence of CHAR.
211 Goes backward if ARG is negative; error if CHAR not found."
212 (interactive "p\ncZap to char: ")
213 (kill-region (point) (progn
214 (search-forward (char-to-string char) nil nil arg)
215 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
218 (defun beginning-of-buffer (&optional arg)
219 "Move point to the beginning of the buffer; leave mark at previous position.
220 With arg N, put point N/10 of the way from the true beginning.
222 Don't use this command in Lisp programs!
223 \(goto-char (point-min)) is faster and avoids clobbering the mark."
227 (if (> (buffer-size) 10000)
228 ;; Avoid overflow for large buffer sizes!
229 (* (prefix-numeric-value arg)
230 (/ (buffer-size) 10))
231 (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10))
233 (if arg (forward-line 1)))
235 (defun end-of-buffer (&optional arg)
236 "Move point to the end of the buffer; leave mark at previous position.
237 With arg N, put point N/10 of the way from the true end.
239 Don't use this command in Lisp programs!
240 \(goto-char (point-max)) is faster and avoids clobbering the mark."
244 (- (1+ (buffer-size))
245 (if (> (buffer-size) 10000)
246 ;; Avoid overflow for large buffer sizes!
247 (* (prefix-numeric-value arg)
248 (/ (buffer-size) 10))
249 (/ (* (buffer-size) (prefix-numeric-value arg)) 10)))
251 ;; If we went to a place in the middle of the buffer,
252 ;; adjust it to the beginning of a line.
253 (if arg (forward-line 1)
254 ;; If the end of the buffer is not already on the screen,
255 ;; then scroll specially to put it near, but not at, the bottom.
256 (if (let ((old-point (point)))
258 (goto-char (window-start))
259 (vertical-motion (window-height))
260 (< (point) old-point)))
263 (defun mark-whole-buffer ()
264 "Put point at beginning and mark at end of buffer.
265 You probably should not use this function in Lisp programs;
266 it is usually a mistake for a Lisp function to use any subroutine
267 that uses or sets the mark."
270 (push-mark (point-max))
271 (goto-char (point-min)))
273 (defun count-lines-region (start end)
274 "Print number of lines and charcters in the region."
276 (message "Region has %d lines, %d characters"
277 (count-lines start end) (- end start)))
280 "Print the current line number (in the buffer) of point."
287 (1+ (count-lines 1 (point)))))))
289 (defun count-lines (start end)
290 "Return number of lines between START and END.
291 This is usually the number of newlines between them,
292 but can be one more if START is not equal to END
293 and the greater of them is not at the start of a line."
296 (narrow-to-region start end)
297 (goto-char (point-min))
298 (if (eq selective-display t)
300 (while (re-search-forward "[\n\C-m]" nil t 40)
301 (setq done (+ 40 done)))
302 (while (re-search-forward "[\n\C-m]" nil t 1)
303 (setq done (+ 1 done)))
305 (- (buffer-size) (forward-line (buffer-size)))))))
307 (defun what-cursor-position ()
308 "Print info on cursor position (on screen and within buffer)."
310 (let* ((char (following-char))
314 (total (buffer-size))
315 (percent (if (> total 50000)
316 ;; Avoid overflow from multiplying by 100!
317 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
318 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
319 (hscroll (if (= (window-hscroll) 0)
321 (format " Hscroll=%d" (window-hscroll))))
322 (col (current-column)))
324 (if (or (/= beg 1) (/= end (1+ total)))
325 (message "point=%d of %d(%d%%) <%d - %d> column %d %s"
326 pos total percent beg end col hscroll)
327 (message "point=%d of %d(%d%%) column %d %s"
328 pos total percent col hscroll))
329 (if (or (/= beg 1) (/= end (1+ total)))
330 (message "Char: %s (0%o) point=%d of %d(%d%%) <%d - %d> column %d %s"
331 (single-key-description char) char pos total percent beg end col hscroll)
332 (message "Char: %s (0%o) point=%d of %d(%d%%) column %d %s"
333 (single-key-description char) char pos total percent col hscroll)))))
335 (defun fundamental-mode ()
336 "Major mode not specialized for anything in particular.
337 Other major modes are defined by comparison with this one."
339 (kill-all-local-variables))
341 (put 'eval-expression 'disabled t)
343 ;; We define this, rather than making eval interactive,
344 ;; for the sake of completion of names like eval-region, eval-current-buffer.
345 (defun eval-expression (expression)
346 "Evaluate EXPRESSION and print value in minibuffer.
347 Value is also consed on to front of the variable `values'."
348 (interactive "xEval: ")
349 (setq values (cons (eval expression) values))
350 (prin1 (car values) t))
352 (defun edit-and-eval-command (prompt command)
353 "Prompting with PROMPT, let user edit COMMAND and eval result.
354 COMMAND is a Lisp expression. Let user edit that expression in
355 the minibuffer, then read and evaluate the result."
356 (let ((command (read-minibuffer prompt
357 (prin1-to-string command))))
358 ;; Add edited command to command history, unless redundant.
359 (or (equal command (car command-history))
360 (setq command-history (cons command command-history)))
363 (defun repeat-complex-command (arg)
364 "Edit and re-evaluate last complex command, or ARGth from last.
365 A complex command is one which used the minibuffer.
366 The command is placed in the minibuffer as a Lisp form for editing.
367 The result is executed, repeating the command as changed.
368 If the command has been changed or is not the most recent previous command
369 it is added to the front of the command history.
370 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
371 to get different commands to edit and resubmit."
373 (let ((elt (nth (1- arg) command-history))
374 (minibuffer-history-position arg)
375 (minibuffer-history-sexp-flag t)
378 (let ((minibuffer-history-variable ' command-history))
379 (setq newcmd (read-from-minibuffer "Redo: "
380 (prin1-to-string elt)
383 (cons 'command-history
385 ;; If command to be redone does not match front of history,
386 ;; add it to the history.
387 (or (equal newcmd (car command-history))
388 (setq command-history (cons newcmd command-history)))
392 (defvar minibuffer-history nil)
393 (defvar minibuffer-history-sexp-flag nil)
394 (setq minibuffer-history-variable 'minibuffer-history)
395 (setq minibuffer-history-position nil)
398 (function (lambda (key-and-command)
400 (function (lambda (keymap)
401 (define-key (symbol-value keymap)
402 (car key-and-command)
403 (cdr key-and-command))))
404 '(minibuffer-local-map
405 minibuffer-local-ns-map
406 minibuffer-local-completion-map
407 minibuffer-local-must-match-map))))
408 '(("\en" . next-history-element) ([next] . next-history-element)
409 ("\ep" . previous-history-element) ([prior] . previous-history-element)
410 ("\er" . previous-matching-history-element)
411 ("\es" . next-matching-history-element)))
413 (put 'previous-matching-history-element 'enable-recursive-minibuffers t)
414 (defun previous-matching-history-element (regexp n)
415 (interactive "sPrevious element matching (regexp): \np")
416 (let ((history (symbol-value minibuffer-history-variable))
418 (pos minibuffer-history-position))
421 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
424 "No later matching history item"
425 "No earlier matching history item")))
426 (if (string-match regexp
427 (if minibuffer-history-sexp-flag
428 (prin1-to-string (nth (1- pos) history))
429 (nth (1- pos) history)))
430 (setq n (+ n (if (< n 0) -1 1)))))
431 (setq minibuffer-history-position pos)
433 (let ((elt (nth (1- pos) history)))
434 (insert (if minibuffer-history-sexp-flag
435 (prin1-to-string elt)
437 (goto-char (point-min))))
439 (put 'next-matching-history-element 'enable-recursive-minibuffers t)
440 (defun next-matching-history-element (regexp n)
441 (interactive "sNext element matching (regexp): \np")
442 (previous-matching-history-element regexp (- n)))
444 (defun next-history-element (n)
445 "Insert the next element of the minibuffer history into the minibuffer."
447 (let ((narg (min (max 1 (- minibuffer-history-position n))
448 (length (symbol-value minibuffer-history-variable)))))
449 (if (= minibuffer-history-position narg)
450 (error (if (= minibuffer-history-position 1)
451 "End of history; no next item"
452 "Beginning of history; no preceding item"))
454 (setq minibuffer-history-position narg)
455 (let ((elt (nth (1- minibuffer-history-position)
456 (symbol-value minibuffer-history-variable))))
458 (if minibuffer-history-sexp-flag
459 (prin1-to-string elt)
461 (goto-char (point-min)))))
463 (defun previous-history-element (n)
464 "Inserts the previous element of `command-history' into the minibuffer."
466 (next-history-element (- n)))
468 (defun goto-line (arg)
469 "Goto line ARG, counting from line 1 at beginning of buffer."
470 (interactive "NGoto line: ")
474 (if (eq selective-display t)
475 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
476 (forward-line (1- arg)))))
478 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
479 (fset 'advertised-undo 'undo)
481 (defun undo (&optional arg)
482 "Undo some previous changes.
483 Repeat this command to undo more changes.
484 A numeric argument serves as a repeat count."
486 (let ((modified (buffer-modified-p)))
487 (or (eq (selected-window) (minibuffer-window))
489 (or (eq last-command 'undo)
492 (setq this-command 'undo)
493 (undo-more (or arg 1))
494 (and modified (not (buffer-modified-p))
495 (delete-auto-save-file-if-necessary))))
498 "Set `pending-undo-list' to the front of the undo list.
499 The next call to `undo-more' will undo the most recently made change."
500 (if (eq buffer-undo-list t)
501 (error "No undo information in this buffer"))
502 (setq pending-undo-list buffer-undo-list))
504 (defun undo-more (count)
505 "Undo back N undo-boundaries beyond what was already undone recently.
506 Call `undo-start' to get ready to undo recent changes,
507 then call `undo-more' one or more times to undo them."
508 (or pending-undo-list
509 (error "No further undo information"))
510 (setq pending-undo-list (primitive-undo count pending-undo-list)))
512 (defvar last-shell-command "")
513 (defvar last-shell-command-on-region "")
515 (defun shell-command (command &optional flag)
516 "Execute string COMMAND in inferior shell; display output, if any.
517 If COMMAND ends in ampersand, execute it asynchronously.
519 Optional second arg non-nil (prefix arg, if interactive)
520 means insert output in current buffer after point (leave mark after it).
521 This cannot be done asynchronously."
522 (interactive (list (read-string "Shell command: " last-shell-command)
525 (progn (barf-if-buffer-read-only)
527 ;; We do not use -f for csh; we will not support broken use of
528 ;; .cshrcs. Even the BSD csh manual says to use
529 ;; "if ($?prompt) exit" before things which are not useful
530 ;; non-interactively. Besides, if someone wants their other
531 ;; aliases for shell commands then they can still have them.
532 (call-process shell-file-name nil t nil
534 (exchange-point-and-mark))
535 ;; Preserve the match data in case called from a program.
536 (let ((data (match-data)))
538 (if (string-match "[ \t]*&[ \t]*$" command)
539 ;; Command ending with ampersand means asynchronous.
540 (let ((buffer (get-buffer-create "*shell-command*"))
541 (directory default-directory)
543 ;; Remove the ampersand.
544 (setq command (substring command 0 (match-beginning 0)))
545 ;; If will kill a process, query first.
546 (setq proc (get-buffer-process buffer))
548 (if (yes-or-no-p "A command is running. Kill it? ")
550 (error "Shell command in progress")))
554 (display-buffer buffer)
555 (setq default-directory directory)
556 (setq proc (start-process "Shell" buffer
557 shell-file-name "-c" command))
558 (setq mode-line-process '(": %s"))
559 (set-process-sentinel proc 'shell-command-sentinel)
560 (set-process-filter proc 'shell-command-filter)
562 (shell-command-on-region (point) (point) command nil))
563 (store-match-data data)))))
565 ;; We have a sentinel to prevent insertion of a termination message
566 ;; in the buffer itself.
567 (defun shell-command-sentinel (process signal)
568 (if (memq (process-status process) '(exit signal))
571 (car (cdr (cdr (process-command process))))
572 (substring signal 0 -1))
574 (set-buffer (process-buffer process))
575 (setq mode-line-process nil))
576 (delete-process process))))
578 (defun shell-command-filter (proc string)
579 ;; Do save-excursion by hand so that we can leave point numerically unchanged
580 ;; despite an insertion immediately after it.
581 (let* ((obuf (current-buffer))
582 (buffer (process-buffer proc))
584 (window (get-buffer-window buffer))
585 (pos (window-start window)))
589 (setq opoint (point))
590 (goto-char (point-max))
591 (insert-before-markers string))
592 ;; insert-before-markers moved this marker: set it back.
593 (set-window-start window pos)
594 ;; Finish our save-excursion.
598 (defun shell-command-on-region (start end command &optional flag interactive)
599 "Execute string COMMAND in inferior shell with region as input.
600 Normally display output (if any) in temp buffer `*Shell Command Output*';
601 Prefix arg means replace the region with it.
602 Noninteractive args are START, END, COMMAND, FLAG.
603 Noninteractively FLAG means insert output in place of text from START to END,
604 and put point at the end, but don't alter the mark.
606 If the output is one line, it is displayed in the echo area,
607 but it is nonetheless available in buffer `*Shell Command Output*'
608 even though that buffer is not automatically displayed. If there is no output
609 or output is inserted in the current buffer then `*Shell Command Output*' is
611 (interactive (list (min (point) (mark)) (max (point) (mark))
612 (read-string "Shell command on region: "
613 last-shell-command-on-region)
615 (prefix-numeric-value current-prefix-arg)))
617 ;; Replace specified region with output from command.
618 (let ((swap (and interactive (< (point) (mark)))))
619 ;; Don't muck with mark
620 ;; unless called interactively.
621 (and interactive (push-mark))
622 (call-process-region start end shell-file-name t t nil
624 (if (get-buffer "*Shell Command Output*")
625 (kill-buffer "*Shell Command Output*"))
626 (and interactive swap (exchange-point-and-mark)))
627 ;; No prefix argument: put the output in a temp buffer,
628 ;; replacing its entire contents.
629 (let ((buffer (get-buffer-create "*Shell Command Output*")))
630 (if (eq buffer (current-buffer))
631 ;; If the input is the same buffer as the output,
632 ;; delete everything but the specified region,
633 ;; then replace that region with the output.
634 (progn (delete-region end (point-max))
635 (delete-region (point-min) start)
636 (call-process-region (point-min) (point-max)
637 shell-file-name t t nil
639 ;; Clear the output buffer, then run the command with output there.
643 (call-process-region start end shell-file-name
646 ;; Report the amount of output.
647 (let ((lines (save-excursion
649 (if (= (buffer-size) 0)
651 (count-lines (point-min) (point-max))))))
653 (message "(Shell command completed with no output)")
654 (kill-buffer "*Shell Command Output*"))
659 (goto-char (point-min))
660 (buffer-substring (point)
661 (progn (end-of-line) (point))))))
663 (set-window-start (display-buffer buffer) 1)))))))
665 (defun universal-argument ()
666 "Begin a numeric argument for the following command.
667 Digits or minus sign following \\[universal-argument] make up the numeric argument.
668 \\[universal-argument] following the digits or minus sign ends the argument.
669 \\[universal-argument] without digits or minus sign provides 4 as argument.
670 Repeating \\[universal-argument] without digits or minus sign
671 multiplies the argument by 4 each time."
675 ;; (describe-arg (list factor) 1)
676 (setq key (read-key-sequence nil t))
677 (while (equal (key-binding key) 'universal-argument)
678 (setq factor (* 4 factor))
679 ;; (describe-arg (list factor) 1)
680 (setq key (read-key-sequence nil t)))
681 (prefix-arg-internal key factor nil)))
683 (defun prefix-arg-internal (key factor value)
685 (if (and (numberp value) (< value 0))
686 (setq sign -1 value (- value)))
688 (setq sign -1 value nil))
689 ;; (describe-arg value sign)
690 (while (equal key "-")
691 (setq sign (- sign) factor nil)
692 ;; (describe-arg value sign)
693 (setq key (read-key-sequence nil t)))
694 (while (and (stringp key)
696 (not (string< key "0"))
697 (not (string< "9" key)))
698 (setq value (+ (* (if (numberp value) value 0) 10)
701 ;; (describe-arg value sign)
702 (setq key (read-key-sequence nil t)))
704 (cond (factor (list factor))
705 ((numberp value) (* value sign))
707 ;; Calling universal-argument after digits
708 ;; terminates the argument but is ignored.
709 (if (eq (key-binding key) 'universal-argument)
711 (describe-arg value sign)
712 (setq key (read-key-sequence nil t))))
713 (if (= (length key) 1)
714 ;; Make sure self-insert-command finds the proper character;
715 ;; unread the character and let the command loop process it.
716 (setq unread-command-char (string-to-char key))
717 ;; We can't push back a longer string, so we'll emulate the
718 ;; command loop ourselves.
719 (command-execute (key-binding key)))))
721 (defun describe-arg (value sign)
722 (cond ((numberp value)
723 (message "Arg: %d" (* value sign)))
725 (message "Arg: [%d]" (car value)))
727 (message "Arg: -"))))
729 (defun digit-argument (arg)
730 "Part of the numeric argument for the next command.
731 \\[universal-argument] following digits or minus sign ends the argument."
733 (prefix-arg-internal (char-to-string (logand last-command-char ?\177))
736 (defun negative-argument (arg)
737 "Begin a negative numeric argument for the next command.
738 \\[universal-argument] following digits or minus sign ends the argument."
740 (prefix-arg-internal "-" nil arg))
742 (defun forward-to-indentation (arg)
743 "Move forward ARG lines and position at first nonblank character."
746 (skip-chars-forward " \t"))
748 (defun backward-to-indentation (arg)
749 "Move backward ARG lines and position at first nonblank character."
751 (forward-line (- arg))
752 (skip-chars-forward " \t"))
754 (defun kill-line (&optional arg)
755 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
756 With prefix argument, kill that many lines from point.
757 Negative arguments kill lines backward.
759 When calling from a program, nil means \"no arg\",
760 a number counts as a prefix arg."
765 (forward-line (prefix-numeric-value arg))
767 (signal 'end-of-buffer nil))
768 (if (looking-at "[ \t]*$")
773 ;;;; Window system cut and paste hooks.
775 (defvar interprogram-cut-function nil
776 "Function to call to make a killed region available to other programs.
778 Most window systems provide some sort of facility for cutting and
779 pasting text between the windows of different programs. On startup,
780 this variable is set to a function which emacs will call whenever text
781 is put in the kill ring to make the new kill available to other
784 The function takes one argument, TEXT, which is a string containing
785 the text which should be made available.")
787 (defvar interprogram-paste-function nil
788 "Function to call to get text cut from other programs.
790 Most window systems provide some sort of facility for cutting and
791 pasting text between the windows of different programs. On startup,
792 this variable is set to a function which emacs will call to obtain
793 text that other programs have provided for pasting.
795 The function should be called with no arguments. If the function
796 returns nil, then no other program has provided such text, and the top
797 of the Emacs kill ring should be used. If the function returns a
798 string, that string should be put in the kill ring as the latest kill.
800 Note that the function should return a string only if a program other
801 than Emacs has provided a string for pasting; if Emacs provided the
802 most recent string, the function should return nil. If it is
803 difficult to tell whether Emacs or some other program provided the
804 current string, it is probably good enough to return nil if the string
805 is equal (according to `string=') to the last text Emacs provided.")
809 ;;;; The kill ring data structure.
811 (defvar kill-ring nil
812 "List of killed text sequences.
813 Since the kill ring is supposed to interact nicely with cut-and-paste
814 facilities offered by window systems, use of this variable should
815 interact nicely with `interprogram-cut-function' and
816 `interprogram-paste-function'. The functions `kill-new',
817 `kill-append', and `current-kill' are supposed to implement this
818 interaction; you may want to use them instead of manipulating the kill
821 (defconst kill-ring-max 30
822 "*Maximum length of kill ring before oldest elements are thrown away.")
824 (defvar kill-ring-yank-pointer nil
825 "The tail of the kill ring whose car is the last thing yanked.")
827 (defun kill-new (string)
828 "Make STRING the latest kill in the kill ring.
829 Set the kill-ring-yank pointer to point to it.
830 If `interprogram-cut-function' is non-nil, apply it to STRING."
831 (setq kill-ring (cons string kill-ring))
832 (if (> (length kill-ring) kill-ring-max)
833 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
834 (setq kill-ring-yank-pointer kill-ring)
835 (if interprogram-cut-function
836 (funcall interprogram-cut-function string)))
838 (defun kill-append (string before-p)
839 "Append STRING to the end of the latest kill in the kill ring.
840 If BEFORE-P is non-nil, prepend STRING to the kill.
841 If 'interprogram-cut-function' is set, pass the resulting kill to
845 (concat string (car kill-ring))
846 (concat (car kill-ring) string)))
847 (if interprogram-cut-function
848 (funcall interprogram-cut-function (car kill-ring))))
850 (defun current-kill (n &optional do-not-move)
851 "Rotate the yanking point by N places, and then return that kill.
852 If N is zero, `interprogram-paste-function' is set, and calling it
853 returns a string, then that string is added to the front of the
854 kill ring and returned as the latest kill.
855 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
856 yanking point; just return the Nth kill forward."
857 (let ((interprogram-paste (and (= n 0)
858 interprogram-paste-function
859 (funcall interprogram-paste-function))))
860 (if interprogram-paste
862 ;; Disable the interprogram cut function when we add the new
863 ;; text to the kill ring, so Emacs doesn't try to own the
864 ;; selection, with identical text.
865 (let ((interprogram-cut-function nil))
866 (kill-new interprogram-paste))
868 (or kill-ring (error "Kill ring is empty"))
869 (let* ((length (length kill-ring))
871 (nthcdr (% (+ n (- length (length kill-ring-yank-pointer)))
875 (setq kill-ring-yank-pointer ARGth-kill-element))
876 (car ARGth-kill-element)))))
880 ;;;; Commands for manipulating the kill ring.
882 (defun kill-region (beg end)
883 "Kill between point and mark.
884 The text is deleted but saved in the kill ring.
885 The command \\[yank] can retrieve it from there.
886 \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
888 This is the primitive for programs to kill text (as opposed to deleting it).
889 Supply two arguments, character numbers indicating the stretch of text
891 Any command that calls this function is a \"kill command\".
892 If the previous command was also a kill command,
893 the text killed this time appends to the text killed last time
894 to make one entry in the kill ring."
898 (copy-region-as-kill beg end))
899 ((not (or (eq buffer-undo-list t)
900 (eq last-command 'kill-region)
902 ;; Don't let the undo list be truncated before we can even access it.
903 (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100)))
904 (delete-region beg end)
905 ;; Take the same string recorded for undo
906 ;; and put it in the kill-ring.
907 (kill-new (car (car buffer-undo-list)))
908 (setq this-command 'kill-region)))
910 (copy-region-as-kill beg end)
911 (delete-region beg end))))
913 (defun copy-region-as-kill (beg end)
914 "Save the region as if killed, but don't kill it.
915 If `interprogram-cut-function' is non-nil, also save the text for a window
916 system cut and paste."
918 (if (eq last-command 'kill-region)
919 (kill-append (buffer-substring beg end) (< end beg))
920 (kill-new (buffer-substring beg end)))
921 (setq this-command 'kill-region)
924 (defun kill-ring-save (beg end)
925 "Save the region as if killed, but don't kill it."
927 (copy-region-as-kill beg end)
930 (let ((other-end (if (= (point) beg) end beg)))
931 (if (pos-visible-in-window-p other-end (selected-window))
933 (goto-char other-end)
935 (let* ((killed-text (current-kill 0))
936 (message-len (min (length killed-text) 40)))
938 ;; Don't say "killed"; that is misleading.
939 (message "Saved text until \"%s\""
940 (substring killed-text (- message-len)))
941 (message "Saved text from \"%s\""
942 (substring killed-text 0 message-len)))))))))
944 (defun append-next-kill ()
945 "Cause following command, if it kills, to append to previous kill."
949 (setq this-command 'kill-region)
950 (message "If the next command is a kill, it will append"))
951 (setq last-command 'kill-region)))
953 (defun yank-pop (arg)
954 "Replace just-yanked stretch of killed text with a different stretch.
955 This command is allowed only immediately after a `yank' or a `yank-pop'.
956 At such a time, the region contains a stretch of reinserted
957 previously-killed text. `yank-pop' deletes that text and inserts in its
958 place a different stretch of killed text.
960 With no argument, the previous kill is inserted.
961 With argument N, insert the Nth previous kill.
962 If N is negative, this is a more recent kill.
964 The sequence of kills wraps around, so that after the oldest one
965 comes the newest one."
967 (if (not (eq last-command 'yank))
968 (error "Previous command was not a yank"))
969 (setq this-command 'yank)
970 (let ((before (< (point) (mark))))
971 (delete-region (point) (mark))
973 (insert (current-kill arg))
974 (if before (exchange-point-and-mark))))
976 (defun yank (&optional arg)
977 "Reinsert the last stretch of killed text.
978 More precisely, reinsert the stretch of killed text most recently
979 killed OR yanked. Put point at end, and set mark at beginning.
980 With just C-u as argument, same but put point at beginning (and mark at end).
981 With argument N, reinsert the Nth most recently killed stretch of killed
983 See also the command \\[yank-pop]."
986 (insert (current-kill (cond
991 (exchange-point-and-mark)))
993 (defun rotate-yank-pointer (arg)
994 "Rotate the yanking point in the kill ring.
995 With argument, rotate that many kills forward (or backward, if negative)."
1000 (defun insert-buffer (buffer)
1001 "Insert after point the contents of BUFFER.
1002 Puts mark after the inserted text.
1003 BUFFER may be a buffer or a buffer name."
1004 (interactive (list (read-buffer "Insert buffer: " (other-buffer) t)))
1005 (or (bufferp buffer)
1006 (setq buffer (get-buffer buffer)))
1007 (let (start end newmark)
1011 (setq start (point-min) end (point-max)))
1012 (insert-buffer-substring buffer start end)
1013 (setq newmark (point)))
1014 (push-mark newmark)))
1016 (defun append-to-buffer (buffer start end)
1017 "Append to specified buffer the text of the region.
1018 It is inserted into that buffer before its point.
1020 When calling from a program, give three arguments:
1021 BUFFER (or buffer name), START and END.
1022 START and END specify the portion of the current buffer to be copied."
1024 (list (read-buffer "Append to buffer: " (other-buffer nil t) t)))
1025 (let ((oldbuf (current-buffer)))
1027 (set-buffer (get-buffer-create buffer))
1028 (insert-buffer-substring oldbuf start end))))
1030 (defun prepend-to-buffer (buffer start end)
1031 "Prepend to specified buffer the text of the region.
1032 It is inserted into that buffer after its point.
1034 When calling from a program, give three arguments:
1035 BUFFER (or buffer name), START and END.
1036 START and END specify the portion of the current buffer to be copied."
1037 (interactive "BPrepend to buffer: \nr")
1038 (let ((oldbuf (current-buffer)))
1040 (set-buffer (get-buffer-create buffer))
1042 (insert-buffer-substring oldbuf start end)))))
1044 (defun copy-to-buffer (buffer start end)
1045 "Copy to specified buffer the text of the region.
1046 It is inserted into that buffer, replacing existing text there.
1048 When calling from a program, give three arguments:
1049 BUFFER (or buffer name), START and END.
1050 START and END specify the portion of the current buffer to be copied."
1051 (interactive "BCopy to buffer: \nr")
1052 (let ((oldbuf (current-buffer)))
1054 (set-buffer (get-buffer-create buffer))
1057 (insert-buffer-substring oldbuf start end)))))
1060 "Return this buffer's mark value as integer, or nil if no mark.
1061 If you are using this in an editing command, you are most likely making
1062 a mistake; see the documentation of `set-mark'."
1063 (marker-position (mark-marker)))
1065 (defun set-mark (pos)
1066 "Set this buffer's mark to POS. Don't use this function!
1067 That is to say, don't use this function unless you want
1068 the user to see that the mark has moved, and you want the previous
1069 mark position to be lost.
1071 Normally, when a new mark is set, the old one should go on the stack.
1072 This is why most applications should use push-mark, not set-mark.
1074 Novice Emacs Lisp programmers often try to use the mark for the wrong
1075 purposes. The mark saves a location for the user's convenience.
1076 Most editing commands should not alter the mark.
1077 To remember a location for internal use in the Lisp program,
1078 store it in a Lisp variable. Example:
1080 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
1082 (set-marker (mark-marker) pos (current-buffer)))
1084 (defvar mark-ring nil
1085 "The list of saved former marks of the current buffer,
1086 most recent first.")
1087 (make-variable-buffer-local 'mark-ring)
1089 (defconst mark-ring-max 16
1090 "*Maximum size of mark ring. Start discarding off end if gets this big.")
1092 (defun set-mark-command (arg)
1093 "Set mark at where point is, or jump to mark.
1094 With no prefix argument, set mark, and push previous mark on mark ring.
1095 With argument, jump to mark, and pop into mark off the mark ring.
1097 Novice Emacs Lisp programmers often try to use the mark for the wrong
1098 purposes. See the documentation of `set-mark' for more information."
1103 (error "No mark set in this buffer")
1107 (defun push-mark (&optional location nomsg)
1108 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
1109 Displays \"Mark set\" unless the optional second arg NOMSG is non-nil.
1111 Novice Emacs Lisp programmers often try to use the mark for the wrong
1112 purposes. See the documentation of `set-mark' for more information."
1115 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
1116 (if (> (length mark-ring) mark-ring-max)
1118 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
1119 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
1120 (set-mark (or location (point)))
1121 (or nomsg executing-macro (> (minibuffer-depth) 0)
1122 (message "Mark set"))
1126 "Pop off mark ring into the buffer's actual mark.
1127 Does not set point. Does nothing if mark ring is empty."
1130 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
1131 (set-mark (+ 0 (car mark-ring)))
1132 (move-marker (car mark-ring) nil)
1133 (if (null (mark)) (ding))
1134 (setq mark-ring (cdr mark-ring)))))
1136 (fset 'exchange-dot-and-mark 'exchange-point-and-mark)
1137 (defun exchange-point-and-mark ()
1138 "Put the mark where point is now, and point where the mark is now."
1140 (let ((omark (mark)))
1142 (error "No mark set in this buffer"))
1147 (defun next-line (arg)
1148 "Move cursor vertically down ARG lines.
1149 If there is no character in the target line exactly under the current column,
1150 the cursor is positioned after the character in that line which spans this
1151 column, or at the end of the line if it is not long enough.
1152 If there is no line in the buffer after this one,
1153 a newline character is inserted to create a line
1154 and the cursor moves to that line.
1156 The command \\[set-goal-column] can be used to create
1157 a semipermanent goal column to which this command always moves.
1158 Then it does not try to move vertically. This goal column is stored
1159 in `goal-column', which is nil when there is none.
1161 If you are thinking of using this in a Lisp program, consider
1162 using `forward-line' instead. It is usually easier to use
1163 and more reliable (no dependence on goal column, etc.)."
1166 (let ((opoint (point)))
1168 (if (or (= opoint (point))
1169 (not (eq (preceding-char) ?\n)))
1176 (defun previous-line (arg)
1177 "Move cursor vertically up ARG lines.
1178 If there is no character in the target line exactly over the current column,
1179 the cursor is positioned after the character in that line which spans this
1180 column, or at the end of the line if it is not long enough.
1182 The command \\[set-goal-column] can be used to create
1183 a semipermanent goal column to which this command always moves.
1184 Then it does not try to move vertically.
1186 If you are thinking of using this in a Lisp program, consider using
1187 `forward-line' with negative argument instead.. It is usually easier
1188 to use and more reliable (no dependence on goal column, etc.)."
1193 (defconst track-eol nil
1194 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
1195 This means moving to the end of each line moved onto.
1196 The beginning of a blank line does not count as the end of a line.")
1198 (make-variable-buffer-local
1199 (defvar goal-column nil
1200 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."))
1202 (defvar temporary-goal-column 0
1203 "Current goal column for vertical motion.
1204 It is the column where point was
1205 at the start of current run of vertical motion commands.
1206 When the `track-eol' feature is doing its job, the value is 9999.")
1208 (defun line-move (arg)
1209 (if (not (or (eq last-command 'next-line)
1210 (eq last-command 'previous-line)))
1211 (setq temporary-goal-column
1212 (if (and track-eol (eolp)
1213 ;; Don't count beg of empty line as end of line
1214 ;; unless we just did explicit end-of-line.
1215 (or (not (bolp)) (eq last-command 'end-of-line)))
1218 (if (not (integerp selective-display))
1220 ;; Move by arg lines, but ignore invisible ones.
1225 (setq arg (1- arg)))
1227 (vertical-motion -1)
1229 (setq arg (1+ arg))))
1230 (move-to-column (or goal-column temporary-goal-column))
1234 (defun set-goal-column (arg)
1235 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
1236 Those commands will move to this position in the line moved to
1237 rather than trying to keep the same horizontal position.
1238 With a non-nil argument, clears out the goal column
1239 so that \\[next-line] and \\[previous-line] resume vertical motion."
1243 (setq goal-column nil)
1244 (message "No goal column"))
1245 (setq goal-column (current-column))
1246 (message (substitute-command-keys
1247 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
1251 (defun transpose-chars (arg)
1252 "Interchange characters around point, moving forward one character.
1253 With prefix arg ARG, effect is to take character before point
1254 and drag it forward past ARG other characters (backward if ARG negative).
1255 If no argument and at end of line, the previous two chars are exchanged."
1257 (and (null arg) (eolp) (forward-char -1))
1258 (transpose-subr 'forward-char (prefix-numeric-value arg)))
1260 (defun transpose-words (arg)
1261 "Interchange words around point, leaving point at end of them.
1262 With prefix arg ARG, effect is to take word before or around point
1263 and drag it forward past ARG other words (backward if ARG negative).
1264 If ARG is zero, the words around or after point and around or after mark
1267 (transpose-subr 'forward-word arg))
1269 (defun transpose-sexps (arg)
1270 "Like \\[transpose-words] but applies to sexps.
1271 Does not work on a sexp that point is in the middle of
1272 if it is a list or string."
1274 (transpose-subr 'forward-sexp arg))
1276 (defun transpose-lines (arg)
1277 "Exchange current line and previous line, leaving point after both.
1278 With argument ARG, takes previous line and moves it past ARG lines.
1279 With argument 0, interchanges line point is in with line mark is in."
1281 (transpose-subr (function
1285 ;; Move forward over a line,
1286 ;; but create a newline if none exists yet.
1291 (forward-line arg))))
1294 (defun transpose-subr (mover arg)
1295 (let (start1 end1 start2 end2)
1302 (setq start2 (point))
1307 (setq start1 (point))
1309 (exchange-point-and-mark)))
1312 (setq start1 (point))
1318 (setq start2 (point))
1321 (setq arg (1- arg)))
1324 (setq start2 (point))
1326 (setq start1 (point))
1332 (setq arg (1+ arg)))))
1334 (defun transpose-subr-1 ()
1335 (if (> (min end1 end2) (max start1 start2))
1336 (error "Don't have two things to transpose"))
1337 (let ((word1 (buffer-substring start1 end1))
1338 (word2 (buffer-substring start2 end2)))
1339 (delete-region start2 end2)
1342 (goto-char (if (< start1 start2) start1
1343 (+ start1 (- (length word1) (length word2)))))
1344 (delete-char (length word1))
1347 (defconst comment-column 32
1348 "*Column to indent right-margin comments to.
1349 Setting this variable automatically makes it local to the current buffer.")
1350 (make-variable-buffer-local 'comment-column)
1352 (defconst comment-start nil
1353 "*String to insert to start a new comment, or nil if no comment syntax defined.")
1355 (defconst comment-start-skip nil
1356 "*Regexp to match the start of a comment plus everything up to its body.
1357 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
1358 at the place matched by the close of the first pair.")
1360 (defconst comment-end ""
1361 "*String to insert to end a new comment.
1362 Should be an empty string if comments are terminated by end-of-line.")
1364 (defconst comment-indent-hook
1365 '(lambda () comment-column)
1366 "Function to compute desired indentation for a comment.
1367 This function is called with no args with point at the beginning of
1368 the comment's starting delimiter.")
1370 (defun indent-for-comment ()
1371 "Indent this line's comment to comment column, or insert an empty comment."
1373 (beginning-of-line 1)
1374 (if (null comment-start)
1375 (error "No comment syntax defined")
1376 (let* ((eolpos (save-excursion (end-of-line) (point)))
1378 (if (re-search-forward comment-start-skip eolpos 'move)
1379 (progn (setq cpos (point-marker))
1380 ;; Find the start of the comment delimiter.
1381 ;; If there were paren-pairs in comment-start-skip,
1382 ;; position at the end of the first pair.
1384 (goto-char (match-end 1))
1385 ;; If comment-start-skip matched a string with internal
1386 ;; whitespace (not final whitespace) then the delimiter
1387 ;; start at the end of that whitespace.
1388 ;; Otherwise, it starts at the beginning of what was matched.
1389 (skip-chars-backward " \t" (match-beginning 0))
1390 (skip-chars-backward "^ \t" (match-beginning 0)))))
1391 (setq begpos (point))
1392 ;; Compute desired indent.
1393 (if (= (current-column)
1394 (setq indent (funcall comment-indent-hook)))
1396 ;; If that's different from current, change it.
1397 (skip-chars-backward " \t")
1398 (delete-region (point) begpos)
1400 ;; An existing comment?
1402 (progn (goto-char cpos)
1403 (set-marker cpos nil))
1405 (insert comment-start)
1407 (insert comment-end))))))
1409 (defun set-comment-column (arg)
1410 "Set the comment column based on point.
1411 With no arg, set the comment column to the current column.
1412 With just minus as arg, kill any comment on this line.
1413 With any other arg, set comment column to indentation of the previous comment
1414 and then align or create a comment on this line at that column."
1422 (re-search-backward comment-start-skip)
1424 (re-search-forward comment-start-skip)
1425 (goto-char (match-beginning 0))
1426 (setq comment-column (current-column))
1427 (message "Comment column set to %d" comment-column))
1428 (indent-for-comment))
1429 (setq comment-column (current-column))
1430 (message "Comment column set to %d" comment-column))))
1432 (defun kill-comment (arg)
1433 "Kill the comment on this line, if any.
1434 With argument, kill comments on that many lines starting with this one."
1435 ;; this function loses in a lot of situations. it incorrectly recognises
1436 ;; comment delimiters sometimes (ergo, inside a string), doesn't work
1437 ;; with multi-line comments, can kill extra whitespace if comment wasn't
1438 ;; through end-of-line, et cetera.
1440 (or comment-start-skip (error "No comment syntax defined"))
1441 (let ((count (prefix-numeric-value arg)) endc)
1447 (and (string< "" comment-end)
1450 (re-search-forward (regexp-quote comment-end) endc 'move)
1451 (skip-chars-forward " \t")
1454 (if (re-search-forward comment-start-skip endc t)
1456 (goto-char (match-beginning 0))
1457 (skip-chars-backward " \t")
1458 (kill-region (point) endc)
1459 ;; to catch comments a line beginnings
1460 (indent-according-to-mode))))
1461 (if arg (forward-line 1))
1462 (setq count (1- count)))))
1464 (defun comment-region (beg end &optional arg)
1465 "Comment the region; third arg numeric means use ARG comment characters.
1466 If ARG is negative, delete that many comment characters instead.
1467 Comments are terminated on each line, even for syntax in which newline does
1468 not end the comment. Blank lines do not get comments."
1469 ;; if someone wants it to only put a comment-start at the beginning and
1470 ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
1471 ;; is easy enough. No option is made here for other than commenting
1473 (interactive "r\np")
1474 (or comment-start (error "No comment syntax is defined"))
1475 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
1478 (let ((cs comment-start) (ce comment-end))
1479 (cond ((not arg) (setq arg 1))
1481 (while (> (setq arg (1- arg)) 0)
1482 (setq cs (concat cs comment-start)
1483 ce (concat ce comment-end)))))
1484 (narrow-to-region beg end)
1489 (while (and (> 1 (setq count (1+ count)))
1490 (looking-at (regexp-quote cs)))
1491 (delete-char (length cs)))
1492 (if (string= "" ce) ()
1494 (while (> 1 (setq count (1+ count)))
1496 ;; this is questionable if comment-end ends in whitespace
1497 ;; that is pretty brain-damaged though
1498 (skip-chars-backward " \t")
1499 (backward-char (length ce))
1500 (if (looking-at (regexp-quote ce))
1501 (delete-char (length ce))))))
1502 (if (looking-at "[ \t]*$") ()
1504 (if (string= "" ce) ()
1507 (search-forward "\n" nil 'move)))))))
1509 (defun backward-word (arg)
1510 "Move backward until encountering the end of a word.
1511 With argument, do this that many times.
1512 In programs, it is faster to call `forward-word' with negative arg."
1514 (forward-word (- arg)))
1516 (defun mark-word (arg)
1517 "Set mark arg words away from point."
1524 (defun kill-word (arg)
1525 "Kill characters forward until encountering the end of a word.
1526 With argument, do this that many times."
1528 (kill-region (point) (progn (forward-word arg) (point))))
1530 (defun backward-kill-word (arg)
1531 "Kill characters backward until encountering the end of a word.
1532 With argument, do this that many times."
1534 (kill-word (- arg)))
1536 (defconst fill-prefix nil
1537 "*String for filling to insert at front of new line, or nil for none.
1538 Setting this variable automatically makes it local to the current buffer.")
1539 (make-variable-buffer-local 'fill-prefix)
1541 (defconst auto-fill-inhibit-regexp nil
1542 "*Regexp to match lines which should not be auto-filled.")
1544 (defun do-auto-fill ()
1546 (or (and auto-fill-inhibit-regexp
1547 (save-excursion (beginning-of-line)
1548 (looking-at auto-fill-inhibit-regexp)))
1549 (while (and (not give-up) (> (current-column) fill-column))
1551 (let ((opoint (point)))
1553 (move-to-column (1+ fill-column))
1554 (skip-chars-backward "^ \t\n")
1556 (re-search-forward "[ \t]" opoint t))
1557 (skip-chars-backward " \t")
1559 ;; If there is a space on the line before fill-point,
1560 ;; and nonspaces precede it, break the line there.
1562 (goto-char fill-point)
1564 ;; If point is at the fill-point, do not `save-excursion'.
1565 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
1566 ;; point will end up before it rather than after it.
1568 (skip-chars-backward " \t")
1569 (= (point) fill-point))
1570 (indent-new-comment-line)
1572 (goto-char fill-point)
1573 (indent-new-comment-line)))
1574 ;; No place to break => stop trying.
1575 (setq give-up t)))))))
1577 (defconst comment-multi-line nil
1578 "*Non-nil means \\[indent-new-comment-line] should continue same comment
1579 on new line, with no new terminator or starter.
1580 This is obsolete because you might as well use \\[newline-and-indent].")
1582 (defun indent-new-comment-line ()
1583 "Break line at point and indent, continuing comment if presently within one.
1584 The body of the continued comment is indented under the previous comment line.
1586 This command is intended for styles where you write a comment per line,
1587 starting a new comment (and terminating it if necessary) on each line.
1588 If you want to continue one comment across several lines, use \\[newline-and-indent]."
1590 (let (comcol comstart)
1591 (skip-chars-backward " \t")
1592 (delete-region (point)
1593 (progn (skip-chars-forward " \t")
1596 (if (not comment-multi-line)
1598 (if (and comment-start-skip
1599 (let ((opoint (point)))
1601 (re-search-forward comment-start-skip opoint t)))
1602 ;; The old line is a comment.
1603 ;; Set WIN to the pos of the comment-start.
1604 ;; But if the comment is empty, look at preceding lines
1605 ;; to find one that has a nonempty comment.
1606 (let ((win (match-beginning 0)))
1607 (while (and (eolp) (not (bobp))
1610 (setq opoint (point))
1612 (re-search-forward comment-start-skip opoint t)))
1613 (setq win (match-beginning 0)))
1614 ;; Indent this line like what we found.
1616 (setq comcol (current-column))
1617 (setq comstart (buffer-substring (point) (match-end 0)))))))
1619 (let ((comment-column comcol)
1620 (comment-start comstart)
1621 (comment-end comment-end))
1622 (and comment-end (not (equal comment-end ""))
1623 ; (if (not comment-multi-line)
1626 (insert comment-end)
1628 ; (setq comment-column (+ comment-column (length comment-start))
1633 (setq comment-end ""))
1636 (indent-for-comment)
1638 ;; Make sure we delete the newline inserted above.
1642 (insert fill-prefix)
1643 (indent-according-to-mode)))))
1645 (defun auto-fill-mode (&optional arg)
1646 "Toggle auto-fill mode.
1647 With arg, turn auto-fill mode on if and only if arg is positive.
1648 In auto-fill mode, inserting a space at a column beyond fill-column
1649 automatically breaks the line at a previous space."
1651 (prog1 (setq auto-fill-function
1653 (not auto-fill-function)
1654 (> (prefix-numeric-value arg) 0))
1658 (set-buffer-modified-p (buffer-modified-p))))
1660 (defun turn-on-auto-fill ()
1661 "Unconditionally turn on Auto Fill mode."
1664 (defun set-fill-column (arg)
1665 "Set `fill-column' to current column, or to argument if given.
1666 The variable `fill-column' has a separate value for each buffer."
1668 (setq fill-column (if (integerp arg) arg (current-column)))
1669 (message "fill-column set to %d" fill-column))
1671 (defun set-selective-display (arg)
1672 "Set `selective-display' to ARG; clear it if no arg.
1673 When the value of `selective-display' is a number > 0,
1674 lines whose indentation is >= that value are not displayed.
1675 The variable `selective-display' has a separate value for each buffer."
1677 (if (eq selective-display t)
1678 (error "selective-display already in use for marked lines"))
1681 (narrow-to-region (point-min) (point))
1682 (goto-char (window-start))
1683 (vertical-motion (window-height)))))
1684 (setq selective-display
1685 (and arg (prefix-numeric-value arg)))
1686 (recenter current-vpos))
1687 (set-window-start (selected-window) (window-start (selected-window)))
1688 (princ "selective-display set to " t)
1689 (prin1 selective-display t)
1692 (defun overwrite-mode (arg)
1693 "Toggle overwrite mode.
1694 With arg, turn overwrite mode on iff arg is positive.
1695 In overwrite mode, printing characters typed in replace existing text
1696 on a one-for-one basis, rather than pushing it to the right."
1698 (setq overwrite-mode
1699 (if (null arg) (not overwrite-mode)
1700 (> (prefix-numeric-value arg) 0)))
1701 (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line.
1703 (defvar blink-matching-paren t
1704 "*Non-nil means show matching open-paren when close-paren is inserted.")
1706 (defconst blink-matching-paren-distance 4000
1707 "*If non-nil, is maximum distance to search for matching open-paren
1708 when close-paren is inserted.")
1710 (defun blink-matching-open ()
1711 "Move cursor momentarily to the beginning of the sexp before point."
1713 (and (> (point) (1+ (point-min)))
1714 (/= (char-syntax (char-after (- (point) 2))) ?\\ )
1715 blink-matching-paren
1716 (let* ((oldpos (point))
1721 (if blink-matching-paren-distance
1722 (narrow-to-region (max (point-min)
1723 (- (point) blink-matching-paren-distance))
1726 (setq blinkpos (scan-sexps oldpos -1))
1728 (and blinkpos (/= (char-syntax (char-after blinkpos))
1731 (/= (char-after (1- oldpos))
1732 (logand (lsh (aref (syntax-table)
1733 (char-after blinkpos))
1736 (if mismatch (setq blinkpos nil))
1739 (goto-char blinkpos)
1740 (if (pos-visible-in-window-p)
1742 (goto-char blinkpos)
1746 (skip-chars-backward " \t")
1748 (buffer-substring (progn (beginning-of-line) (point))
1750 (buffer-substring blinkpos
1753 (skip-chars-forward "\n \t")
1757 (message "Mismatched parentheses"))
1758 ((not blink-matching-paren-distance)
1759 (message "Unmatched parenthesis"))))))))
1761 ;Turned off because it makes dbx bomb out.
1762 (setq blink-paren-function 'blink-matching-open)
1764 ; this is just something for the luser to see in a keymap -- this is not
1765 ; how quitting works normally!
1766 (defun keyboard-quit ()
1767 "Signal a quit condition."
1771 (define-key global-map "\C-g" 'keyboard-quit)
1773 (defun set-variable (var val)
1774 "Set VARIABLE to VALUE. VALUE is a Lisp object.
1775 When using this interactively, supply a Lisp expression for VALUE.
1776 If you want VALUE to be a string, you must surround it with doublequotes.
1778 If VARIABLE has a `variable-interactive' property, that is used as if
1779 it were the arg to `interactive' (which see) to interactively read the value."
1781 (let* ((var (read-variable "Set variable: "))
1782 (minibuffer-help-form
1787 (with-output-to-temp-buffer "*Help*"
1789 (princ "\nDocumentation:\n")
1790 (princ (substring (documentation-property var 'variable-documentation)
1793 (let ((print-length 20))
1794 (princ "\n\nCurrent value: ")
1795 (prin1 (symbol-value var))))
1798 (let ((prop (get var 'variable-interactive)))
1800 ;; Use VAR's `variable-interactive' property
1801 ;; as an interactive spec for prompting.
1802 (call-interactively (list 'lambda '(arg)
1803 (list 'interactive prop)
1805 (eval-minibuffer (format "Set %s to value: " var)))))))
1808 ;;; simple.el ends here