1 ;;; simple.el --- basic editing commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1993 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 on the new line
26 if the line would have been empty.
27 With arg N, insert N newlines."
29 (let* ((do-fill-prefix (and fill-prefix
(bolp)))
30 (flag (and (null do-fill-prefix
) (bolp) (not (bobp)))))
31 ;; If this is a simple case, and we are at the beginning of a line,
32 ;; actually insert the newline *before* the preceding newline
33 ;; instead of after. That makes better display behavior.
36 ;; If undo is enabled, don't let this hack be visible:
37 ;; record the real value of point as the place to move back to
38 ;; if we undo this insert.
39 (if (and buffer-undo-list
(not (eq buffer-undo-list t
)))
40 (setq buffer-undo-list
(cons (point) buffer-undo-list
)))
45 (if do-fill-prefix
(insert fill-prefix
))
47 (if flag
(forward-char 1))))
50 "Split current line, moving portion beyond point vertically down."
52 (skip-chars-forward " \t")
53 (let ((col (current-column))
59 (defun quoted-insert (arg)
60 "Read next input character and insert it.
61 This is useful for inserting control characters.
62 You may also type up to 3 octal digits, to insert a character with that code.
63 `quoted-insert' inserts the character even in overstrike mode; if you
64 use overstrike as your normal editing mode, you can use this function
65 to insert characters when necessary."
67 (let ((char (read-quoted-char)))
70 (setq arg
(1- arg
)))))
72 (defun delete-indentation (&optional arg
)
73 "Join this line to previous and fix up whitespace at join.
74 If there is a fill prefix, delete it from the beginning of this line.
75 With argument, join this line to following line."
78 (if arg
(forward-line 1))
79 (if (eq (preceding-char) ?
\n)
81 (delete-region (point) (1- (point)))
82 ;; If the second line started with the fill prefix,
85 (<= (+ (point) (length fill-prefix
)) (point-max))
87 (buffer-substring (point)
88 (+ (point) (length fill-prefix
)))))
89 (delete-region (point) (+ (point) (length fill-prefix
))))
92 (defun fixup-whitespace ()
93 "Fixup white space between objects around point.
94 Leave one space or none, according to the context."
97 (delete-horizontal-space)
98 (if (or (looking-at "^\\|\\s)")
99 (save-excursion (forward-char -
1)
100 (looking-at "$\\|\\s(\\|\\s'")))
104 (defun delete-horizontal-space ()
105 "Delete all spaces and tabs around point."
107 (skip-chars-backward " \t")
108 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
110 (defun just-one-space ()
111 "Delete all spaces and tabs around point, leaving one space."
113 (skip-chars-backward " \t")
114 (if (= (following-char) ?
)
117 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
119 (defun delete-blank-lines ()
120 "On blank line, delete all surrounding blank lines, leaving just one.
121 On isolated blank line, delete that one.
122 On nonblank line, delete all blank lines that follow it."
124 (let (thisblank singleblank
)
127 (setq thisblank
(looking-at "[ \t]*$"))
128 ;; Set singleblank if there is just one blank line here.
131 (not (looking-at "[ \t]*\n[ \t]*$"))
133 (progn (forward-line -
1)
134 (not (looking-at "[ \t]*$")))))))
135 ;; Delete preceding blank lines, and this one too if it's the only one.
139 (if singleblank
(forward-line 1))
140 (delete-region (point)
141 (if (re-search-backward "[^ \t\n]" nil t
)
142 (progn (forward-line 1) (point))
144 ;; Delete following blank lines, unless the current line is blank
145 ;; and there are no following blank lines.
146 (if (not (and thisblank singleblank
))
150 (delete-region (point)
151 (if (re-search-forward "[^ \t\n]" nil t
)
152 (progn (beginning-of-line) (point))
154 ;; Handle the special case where point is followed by newline and eob.
155 ;; Delete the line, leaving point at eob.
156 (if (looking-at "^[ \t]*\n\\'")
157 (delete-region (point) (point-max)))))
159 (defun back-to-indentation ()
160 "Move point to the first non-whitespace character on this line."
162 (beginning-of-line 1)
163 (skip-chars-forward " \t"))
165 (defun newline-and-indent ()
166 "Insert a newline, then indent according to major mode.
167 Indentation is done using the value of `indent-line-function'.
168 In programming language modes, this is the same as TAB.
169 In some text modes, where TAB inserts a tab, this command indents to the
170 column specified by the variable `left-margin'."
172 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
174 (indent-according-to-mode))
176 (defun reindent-then-newline-and-indent ()
177 "Reindent current line, insert newline, then indent the new line.
178 Indentation of both lines is done according to the current major mode,
179 which means calling the current value of `indent-line-function'.
180 In programming language modes, this is the same as TAB.
181 In some text modes, where TAB inserts a tab, this indents to the
182 column specified by the variable `left-margin'."
185 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
186 (indent-according-to-mode))
188 (indent-according-to-mode))
190 ;; Internal subroutine of delete-char
191 (defun kill-forward-chars (arg)
192 (if (listp arg
) (setq arg
(car arg
)))
193 (if (eq arg
'-
) (setq arg -
1))
194 (kill-region (point) (+ (point) arg
)))
196 ;; Internal subroutine of backward-delete-char
197 (defun kill-backward-chars (arg)
198 (if (listp arg
) (setq arg
(car arg
)))
199 (if (eq arg
'-
) (setq arg -
1))
200 (kill-region (point) (- (point) arg
)))
202 (defun backward-delete-char-untabify (arg &optional killp
)
203 "Delete characters backward, changing tabs into spaces.
204 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
205 Interactively, ARG is the prefix arg (default 1)
206 and KILLP is t if prefix arg is was specified."
207 (interactive "*p\nP")
210 (while (and (> count
0) (not (bobp)))
211 (if (= (preceding-char) ?
\t)
212 (let ((col (current-column)))
214 (setq col
(- col
(current-column)))
218 (setq count
(1- count
)))))
219 (delete-backward-char arg killp
)
220 ;; In overwrite mode, back over columns while clearing them out,
221 ;; unless at end of line.
222 (and overwrite-mode
(not (eolp))
223 (save-excursion (insert-char ?\ arg
))))
225 (defun zap-to-char (arg char
)
226 "Kill up to and including ARG'th occurrence of CHAR.
227 Goes backward if ARG is negative; error if CHAR not found."
228 (interactive "p\ncZap to char: ")
229 (kill-region (point) (progn
230 (search-forward (char-to-string char
) nil nil arg
)
231 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
234 (defun beginning-of-buffer (&optional arg
)
235 "Move point to the beginning of the buffer; leave mark at previous position.
236 With arg N, put point N/10 of the way from the true beginning.
238 Don't use this command in Lisp programs!
239 \(goto-char (point-min)) is faster and avoids clobbering the mark."
243 (if (> (buffer-size) 10000)
244 ;; Avoid overflow for large buffer sizes!
245 (* (prefix-numeric-value arg
)
246 (/ (buffer-size) 10))
247 (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg
))) 10))
249 (if arg
(forward-line 1)))
251 (defun end-of-buffer (&optional arg
)
252 "Move point to the end of the buffer; leave mark at previous position.
253 With arg N, put point N/10 of the way from the true end.
255 Don't use this command in Lisp programs!
256 \(goto-char (point-max)) is faster and avoids clobbering the mark."
260 (- (1+ (buffer-size))
261 (if (> (buffer-size) 10000)
262 ;; Avoid overflow for large buffer sizes!
263 (* (prefix-numeric-value arg
)
264 (/ (buffer-size) 10))
265 (/ (* (buffer-size) (prefix-numeric-value arg
)) 10)))
267 ;; If we went to a place in the middle of the buffer,
268 ;; adjust it to the beginning of a line.
269 (if arg
(forward-line 1)
270 ;; If the end of the buffer is not already on the screen,
271 ;; then scroll specially to put it near, but not at, the bottom.
272 (if (let ((old-point (point)))
274 (goto-char (window-start))
275 (vertical-motion (window-height))
276 (< (point) old-point
)))
279 (defun mark-whole-buffer ()
280 "Put point at beginning and mark at end of buffer.
281 You probably should not use this function in Lisp programs;
282 it is usually a mistake for a Lisp function to use any subroutine
283 that uses or sets the mark."
286 (push-mark (point-max))
287 (goto-char (point-min)))
289 (defun count-lines-region (start end
)
290 "Print number of lines and charcters in the region."
292 (message "Region has %d lines, %d characters"
293 (count-lines start end
) (- end start
)))
296 "Print the current line number (in the buffer) of point."
303 (1+ (count-lines 1 (point)))))))
305 (defun count-lines (start end
)
306 "Return number of lines between START and END.
307 This is usually the number of newlines between them,
308 but can be one more if START is not equal to END
309 and the greater of them is not at the start of a line."
312 (narrow-to-region start end
)
313 (goto-char (point-min))
314 (if (eq selective-display t
)
316 (while (re-search-forward "[\n\C-m]" nil t
40)
317 (setq done
(+ 40 done
)))
318 (while (re-search-forward "[\n\C-m]" nil t
1)
319 (setq done
(+ 1 done
)))
321 (- (buffer-size) (forward-line (buffer-size)))))))
323 (defun what-cursor-position ()
324 "Print info on cursor position (on screen and within buffer)."
326 (let* ((char (following-char))
330 (total (buffer-size))
331 (percent (if (> total
50000)
332 ;; Avoid overflow from multiplying by 100!
333 (/ (+ (/ total
200) (1- pos
)) (max (/ total
100) 1))
334 (/ (+ (/ total
2) (* 100 (1- pos
))) (max total
1))))
335 (hscroll (if (= (window-hscroll) 0)
337 (format " Hscroll=%d" (window-hscroll))))
338 (col (current-column)))
340 (if (or (/= beg
1) (/= end
(1+ total
)))
341 (message "point=%d of %d(%d%%) <%d - %d> column %d %s"
342 pos total percent beg end col hscroll
)
343 (message "point=%d of %d(%d%%) column %d %s"
344 pos total percent col hscroll
))
345 (if (or (/= beg
1) (/= end
(1+ total
)))
346 (message "Char: %s (0%o) point=%d of %d(%d%%) <%d - %d> column %d %s"
347 (single-key-description char
) char pos total percent beg end col hscroll
)
348 (message "Char: %s (0%o) point=%d of %d(%d%%) column %d %s"
349 (single-key-description char
) char pos total percent col hscroll
)))))
351 (defun fundamental-mode ()
352 "Major mode not specialized for anything in particular.
353 Other major modes are defined by comparison with this one."
355 (kill-all-local-variables))
357 (defvar read-expression-map
(copy-keymap minibuffer-local-map
)
358 "Minibuffer keymap used for reading Lisp expressions.")
359 (define-key read-expression-map
"\M-\t" 'lisp-complete-symbol
)
361 (put 'eval-expression
'disabled t
)
363 ;; We define this, rather than making eval interactive,
364 ;; for the sake of completion of names like eval-region, eval-current-buffer.
365 (defun eval-expression (expression)
366 "Evaluate EXPRESSION and print value in minibuffer.
367 Value is also consed on to front of the variable `values'."
368 (interactive (list (read-from-minibuffer "Eval: "
369 nil read-expression-map t
)))
370 (setq values
(cons (eval expression
) values
))
371 (prin1 (car values
) t
))
373 (defun edit-and-eval-command (prompt command
)
374 "Prompting with PROMPT, let user edit COMMAND and eval result.
375 COMMAND is a Lisp expression. Let user edit that expression in
376 the minibuffer, then read and evaluate the result."
377 (let ((command (read-from-minibuffer prompt
378 (prin1-to-string command
)
379 read-expression-map t
)))
380 ;; Add edited command to command history, unless redundant.
381 (or (equal command
(car command-history
))
382 (setq command-history
(cons command command-history
)))
385 (defun repeat-complex-command (arg)
386 "Edit and re-evaluate last complex command, or ARGth from last.
387 A complex command is one which used the minibuffer.
388 The command is placed in the minibuffer as a Lisp form for editing.
389 The result is executed, repeating the command as changed.
390 If the command has been changed or is not the most recent previous command
391 it is added to the front of the command history.
392 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
393 to get different commands to edit and resubmit."
395 (let ((elt (nth (1- arg
) command-history
))
396 (minibuffer-history-position arg
)
397 (minibuffer-history-sexp-flag t
)
401 (setq newcmd
(read-from-minibuffer "Redo: "
402 (prin1-to-string elt
)
405 (cons 'command-history
407 ;; If command was added to command-history as a string,
408 ;; get rid of that. We want only evallable expressions there.
409 (if (stringp (car command-history
))
410 (setq command-history
(cdr command-history
)))
411 ;; If command to be redone does not match front of history,
412 ;; add it to the history.
413 (or (equal newcmd
(car command-history
))
414 (setq command-history
(cons newcmd command-history
)))
418 (defvar minibuffer-history nil
419 "Default minibuffer history list.
420 This is used for all minibuffer input
421 except when an alternate history list is specified.")
422 (defvar minibuffer-history-sexp-flag nil
423 "Nonzero when doing history operations on `command-history'.
424 More generally, indicates that the history list being acted on
425 contains expressions rather than strings.")
426 (setq minibuffer-history-variable
'minibuffer-history
)
427 (setq minibuffer-history-position nil
)
428 (defvar minibuffer-history-search-history nil
)
431 (lambda (key-and-command)
433 (lambda (keymap-and-completionp)
434 ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P).
435 ;; If the cdr of KEY-AND-COMMAND (the command) is a cons,
436 ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t.
437 (define-key (symbol-value (car keymap-and-completionp
))
438 (car key-and-command
)
439 (let ((command (cdr key-and-command
)))
441 (if (cdr keymap-and-completionp
)
445 '((minibuffer-local-map . nil
)
446 (minibuffer-local-ns-map . nil
)
447 (minibuffer-local-completion-map . t
)
448 (minibuffer-local-must-match-map . t
)
449 (read-expression-map . nil
))))
450 ;; In completion maps, use the completion-oriented history commands.
451 '(("\en" .
(next-history-element . next-complete-history-element
))
452 ([next] . (next-history-element . next-complete-history-element))
453 ("\ep" . (previous-history-element . previous-complete-history-element))
454 ([prior] . (previous-history-element . previous-complete-history-element))
455 ("\er" . previous-matching-history-element)
456 ("\es" . next-matching-history-element)))
458 (defun previous-matching-history-element (regexp n)
459 "Find the previous history element that matches REGEXP.
460 \(Previous history elements refer to earlier actions.)
461 With prefix argument N, search for Nth previous match.
462 If N is negative, find the next or Nth next match."
464 (let ((enable-recursive-minibuffers t)
465 (minibuffer-history-sexp-flag nil))
466 (list (read-from-minibuffer "Previous element matching (regexp): "
470 'minibuffer-history-search-history)
471 (prefix-numeric-value current-prefix-arg))))
472 (let ((history (symbol-value minibuffer-history-variable))
474 (pos minibuffer-history-position))
477 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
480 "No later matching history item"
481 "No earlier matching history item")))
482 (if (string-match regexp
483 (if minibuffer-history-sexp-flag
484 (prin1-to-string (nth (1- pos) history))
485 (nth (1- pos) history)))
486 (setq n (+ n (if (< n 0) 1 -1)))))
487 (setq minibuffer-history-position pos)
489 (let ((elt (nth (1- pos) history)))
490 (insert (if minibuffer-history-sexp-flag
491 (prin1-to-string elt)
493 (goto-char (point-min)))
494 (if (or (eq (car (car command-history)) 'previous-matching-history-element)
495 (eq (car (car command-history)) 'next-matching-history-element))
496 (setq command-history (cdr command-history))))
498 (defun next-matching-history-element (regexp n)
499 "Find the next history element that matches REGEXP.
500 \(The next history element refers to a more recent action.)
501 With prefix argument N, search for Nth next match.
502 If N is negative, find the previous or Nth previous match."
504 (let ((enable-recursive-minibuffers t)
505 (minibuffer-history-sexp-flag nil))
506 (list (read-from-minibuffer "Next element matching (regexp): "
510 'minibuffer-history-search-history)
511 (prefix-numeric-value current-prefix-arg))))
512 (previous-matching-history-element regexp (- n)))
514 (defun next-history-element (n)
515 "Insert the next element of the minibuffer history into the minibuffer."
517 (let ((narg (min (max 1 (- minibuffer-history-position n))
518 (length (symbol-value minibuffer-history-variable)))))
519 (if (= minibuffer-history-position narg)
520 (error (if (= minibuffer-history-position 1)
521 "End of history; no next item"
522 "Beginning of history; no preceding item"))
524 (setq minibuffer-history-position narg)
525 (let ((elt (nth (1- minibuffer-history-position)
526 (symbol-value minibuffer-history-variable))))
528 (if minibuffer-history-sexp-flag
529 (prin1-to-string elt)
531 (goto-char (point-min)))))
533 (defun previous-history-element (n)
534 "Inserts the previous element of the minibuffer history into the minibuffer."
536 (next-history-element (- n)))
538 (defun next-complete-history-element (n)
540 Get previous element of history which is a completion of minibuffer contents."
542 (next-matching-history-element
543 (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n))
545 (defun previous-complete-history-element (n)
546 "Get next element of history which is a completion of minibuffer contents."
548 (next-complete-history-element (- n)))
550 (defun goto-line (arg)
551 "Goto line ARG, counting from line 1 at beginning of buffer."
552 (interactive "NGoto line: ")
556 (if (eq selective-display t)
557 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
558 (forward-line (1- arg)))))
560 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
561 (fset 'advertised-undo 'undo)
563 (defun undo (&optional arg)
564 "Undo some previous changes.
565 Repeat this command to undo more changes.
566 A numeric argument serves as a repeat count."
568 (let ((modified (buffer-modified-p)))
569 (or (eq (selected-window) (minibuffer-window))
571 (or (eq last-command 'undo)
574 (setq this-command 'undo)
575 (undo-more (or arg 1))
576 (and modified (not (buffer-modified-p))
577 (delete-auto-save-file-if-necessary))))
580 "Set `pending-undo-list' to the front of the undo list.
581 The next call to `undo-more' will undo the most recently made change."
582 (if (eq buffer-undo-list t)
583 (error "No undo information in this buffer"))
584 (setq pending-undo-list buffer-undo-list))
586 (defun undo-more (count)
587 "Undo back N undo-boundaries beyond what was already undone recently.
588 Call `undo-start' to get ready to undo recent changes,
589 then call `undo-more' one or more times to undo them."
590 (or pending-undo-list
591 (error "No further undo information"))
592 (setq pending-undo-list (primitive-undo count pending-undo-list)))
594 (defvar last-shell-command "")
595 (defvar last-shell-command-on-region "")
597 (defun shell-command (command &optional flag)
598 "Execute string COMMAND in inferior shell; display output, if any.
599 If COMMAND ends in ampersand, execute it asynchronously.
601 Optional second arg non-nil (prefix arg, if interactive)
602 means insert output in current buffer after point (leave mark after it).
603 This cannot be done asynchronously."
604 (interactive (list (read-string "Shell command: " last-shell-command)
607 (progn (barf-if-buffer-read-only)
609 ;; We do not use -f for csh; we will not support broken use of
610 ;; .cshrcs. Even the BSD csh manual says to use
611 ;; "if ($?prompt) exit" before things which are not useful
612 ;; non-interactively. Besides, if someone wants their other
613 ;; aliases for shell commands then they can still have them.
614 (call-process shell-file-name nil t nil
616 (exchange-point-and-mark))
617 ;; Preserve the match data in case called from a program.
618 (let ((data (match-data)))
620 (if (string-match "[ \t]*&[ \t]*$" command)
621 ;; Command ending with ampersand means asynchronous.
622 (let ((buffer (get-buffer-create "*shell-command*"))
623 (directory default-directory)
625 ;; Remove the ampersand.
626 (setq command (substring command 0 (match-beginning 0)))
627 ;; If will kill a process, query first.
628 (setq proc (get-buffer-process buffer))
630 (if (yes-or-no-p "A command is running. Kill it? ")
632 (error "Shell command in progress")))
636 (display-buffer buffer)
637 (setq default-directory directory)
638 (setq proc (start-process "Shell" buffer
639 shell-file-name "-c" command))
640 (setq mode-line-process '(": %s"))
641 (set-process-sentinel proc 'shell-command-sentinel)
642 (set-process-filter proc 'shell-command-filter)
644 (shell-command-on-region (point) (point) command nil))
645 (store-match-data data)))))
647 ;; We have a sentinel to prevent insertion of a termination message
648 ;; in the buffer itself.
649 (defun shell-command-sentinel (process signal)
650 (if (memq (process-status process) '(exit signal))
653 (car (cdr (cdr (process-command process))))
654 (substring signal 0 -1))
656 (set-buffer (process-buffer process))
657 (setq mode-line-process nil))
658 (delete-process process))))
660 (defun shell-command-filter (proc string)
661 ;; Do save-excursion by hand so that we can leave point numerically unchanged
662 ;; despite an insertion immediately after it.
663 (let* ((obuf (current-buffer))
664 (buffer (process-buffer proc))
666 (window (get-buffer-window buffer))
667 (pos (window-start window)))
671 (setq opoint (point))
672 (goto-char (point-max))
673 (insert-before-markers string))
674 ;; insert-before-markers moved this marker: set it back.
675 (set-window-start window pos)
676 ;; Finish our save-excursion.
680 (defun shell-command-on-region (start end command &optional flag interactive)
681 "Execute string COMMAND in inferior shell with region as input.
682 Normally display output (if any) in temp buffer `*Shell Command Output*';
683 Prefix arg means replace the region with it.
684 Noninteractive args are START, END, COMMAND, FLAG.
685 Noninteractively FLAG means insert output in place of text from START to END,
686 and put point at the end, but don't alter the mark.
688 If the output is one line, it is displayed in the echo area,
689 but it is nonetheless available in buffer `*Shell Command Output*'
690 even though that buffer is not automatically displayed. If there is no output
691 or output is inserted in the current buffer then `*Shell Command Output*' is
693 (interactive (list (region-beginning) (region-end)
694 (read-string "Shell command on region: "
695 last-shell-command-on-region)
697 (prefix-numeric-value current-prefix-arg)))
699 ;; Replace specified region with output from command.
700 (let ((swap (and interactive (< (point) (mark)))))
701 ;; Don't muck with mark
702 ;; unless called interactively.
703 (and interactive (push-mark))
704 (call-process-region start end shell-file-name t t nil
706 (if (get-buffer "*Shell Command Output*")
707 (kill-buffer "*Shell Command Output*"))
708 (and interactive swap (exchange-point-and-mark)))
709 ;; No prefix argument: put the output in a temp buffer,
710 ;; replacing its entire contents.
711 (let ((buffer (get-buffer-create "*Shell Command Output*")))
712 (if (eq buffer (current-buffer))
713 ;; If the input is the same buffer as the output,
714 ;; delete everything but the specified region,
715 ;; then replace that region with the output.
716 (progn (delete-region end (point-max))
717 (delete-region (point-min) start)
718 (call-process-region (point-min) (point-max)
719 shell-file-name t t nil
721 ;; Clear the output buffer, then run the command with output there.
725 (call-process-region start end shell-file-name
728 ;; Report the amount of output.
729 (let ((lines (save-excursion
731 (if (= (buffer-size) 0)
733 (count-lines (point-min) (point-max))))))
735 (message "(Shell command completed with no output)")
736 (kill-buffer "*Shell Command Output*"))
741 (goto-char (point-min))
742 (buffer-substring (point)
743 (progn (end-of-line) (point))))))
745 (set-window-start (display-buffer buffer) 1)))))))
747 (defun universal-argument ()
748 "Begin a numeric argument for the following command.
749 Digits or minus sign following \\[universal-argument] make up the numeric argument.
750 \\[universal-argument] following the digits or minus sign ends the argument.
751 \\[universal-argument] without digits or minus sign provides 4 as argument.
752 Repeating \\[universal-argument] without digits or minus sign
753 multiplies the argument by 4 each time."
757 ;; (describe-arg (list factor) 1)
758 (setq key (read-key-sequence nil t))
759 (while (equal (key-binding key) 'universal-argument)
760 (setq factor (* 4 factor))
761 ;; (describe-arg (list factor) 1)
762 (setq key (read-key-sequence nil t)))
763 (prefix-arg-internal key factor nil)))
765 (defun prefix-arg-internal (key factor value)
767 (if (and (numberp value) (< value 0))
768 (setq sign -1 value (- value)))
770 (setq sign -1 value nil))
771 ;; (describe-arg value sign)
772 (while (equal key "-")
773 (setq sign (- sign) factor nil)
774 ;; (describe-arg value sign)
775 (setq key (read-key-sequence nil t)))
776 (while (and (stringp key)
778 (not (string< key "0"))
779 (not (string< "9" key)))
780 (setq value (+ (* (if (numberp value) value 0) 10)
783 ;; (describe-arg value sign)
784 (setq key (read-key-sequence nil t)))
786 (cond (factor (list factor))
787 ((numberp value) (* value sign))
789 ;; Calling universal-argument after digits
790 ;; terminates the argument but is ignored.
791 (if (eq (key-binding key) 'universal-argument)
793 (describe-arg value sign)
794 (setq key (read-key-sequence nil t))))
795 (setq unread-command-events (append key '()))))
797 (defun describe-arg (value sign)
798 (cond ((numberp value)
799 (message "Arg: %d" (* value sign)))
801 (message "Arg: [%d]" (car value)))
803 (message "Arg: -"))))
805 (defun digit-argument (arg)
806 "Part of the numeric argument for the next command.
807 \\[universal-argument] following digits or minus sign ends the argument."
809 (prefix-arg-internal (char-to-string (logand last-command-char ?\177))
812 (defun negative-argument (arg)
813 "Begin a negative numeric argument for the next command.
814 \\[universal-argument] following digits or minus sign ends the argument."
816 (prefix-arg-internal "-" nil arg))
818 (defun forward-to-indentation (arg)
819 "Move forward ARG lines and position at first nonblank character."
822 (skip-chars-forward " \t"))
824 (defun backward-to-indentation (arg)
825 "Move backward ARG lines and position at first nonblank character."
827 (forward-line (- arg))
828 (skip-chars-forward " \t"))
830 (defun kill-line (&optional arg)
831 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
832 With prefix argument, kill that many lines from point.
833 Negative arguments kill lines backward.
835 When calling from a program, nil means \"no arg\",
836 a number counts as a prefix arg."
839 ;; Don't shift point before doing the delete; that way,
840 ;; undo will record the right position of point.
843 (forward-line (prefix-numeric-value arg))
845 (signal 'end-of-buffer nil))
846 (if (looking-at "[ \t]*$")
851 ;;;; Window system cut and paste hooks.
853 (defvar interprogram-cut-function nil
854 "Function to call to make a killed region available to other programs.
856 Most window systems provide some sort of facility for cutting and
857 pasting text between the windows of different programs. On startup,
858 this variable is set to a function which emacs will call whenever text
859 is put in the kill ring to make the new kill available to other
862 The function takes one argument, TEXT, which is a string containing
863 the text which should be made available.")
865 (defvar interprogram-paste-function nil
866 "Function to call to get text cut from other programs.
868 Most window systems provide some sort of facility for cutting and
869 pasting text between the windows of different programs. On startup,
870 this variable is set to a function which emacs will call to obtain
871 text that other programs have provided for pasting.
873 The function should be called with no arguments. If the function
874 returns nil, then no other program has provided such text, and the top
875 of the Emacs kill ring should be used. If the function returns a
876 string, that string should be put in the kill ring as the latest kill.
878 Note that the function should return a string only if a program other
879 than Emacs has provided a string for pasting; if Emacs provided the
880 most recent string, the function should return nil. If it is
881 difficult to tell whether Emacs or some other program provided the
882 current string, it is probably good enough to return nil if the string
883 is equal (according to `string=') to the last text Emacs provided.")
887 ;;;; The kill ring data structure.
889 (defvar kill-ring nil
890 "List of killed text sequences.
891 Since the kill ring is supposed to interact nicely with cut-and-paste
892 facilities offered by window systems, use of this variable should
893 interact nicely with `interprogram-cut-function' and
894 `interprogram-paste-function'. The functions `kill-new',
895 `kill-append', and `current-kill' are supposed to implement this
896 interaction; you may want to use them instead of manipulating the kill
899 (defconst kill-ring-max 30
900 "*Maximum length of kill ring before oldest elements are thrown away.")
902 (defvar kill-ring-yank-pointer nil
903 "The tail of the kill ring whose car is the last thing yanked.")
905 (defun kill-new (string)
906 "Make STRING the latest kill in the kill ring.
907 Set the kill-ring-yank pointer to point to it.
908 If `interprogram-cut-function' is non-nil, apply it to STRING."
909 (setq kill-ring (cons string kill-ring))
910 (if (> (length kill-ring) kill-ring-max)
911 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
912 (setq kill-ring-yank-pointer kill-ring)
913 (if interprogram-cut-function
914 (funcall interprogram-cut-function string)))
916 (defun kill-append (string before-p)
917 "Append STRING to the end of the latest kill in the kill ring.
918 If BEFORE-P is non-nil, prepend STRING to the kill.
919 If `interprogram-cut-function' is set, pass the resulting kill to
923 (concat string (car kill-ring))
924 (concat (car kill-ring) string)))
925 (if interprogram-cut-function
926 (funcall interprogram-cut-function (car kill-ring))))
928 (defun current-kill (n &optional do-not-move)
929 "Rotate the yanking point by N places, and then return that kill.
930 If N is zero, `interprogram-paste-function' is set, and calling it
931 returns a string, then that string is added to the front of the
932 kill ring and returned as the latest kill.
933 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
934 yanking point; just return the Nth kill forward."
935 (let ((interprogram-paste (and (= n 0)
936 interprogram-paste-function
937 (funcall interprogram-paste-function))))
938 (if interprogram-paste
940 ;; Disable the interprogram cut function when we add the new
941 ;; text to the kill ring, so Emacs doesn't try to own the
942 ;; selection, with identical text.
943 (let ((interprogram-cut-function nil))
944 (kill-new interprogram-paste))
946 (or kill-ring (error "Kill ring is empty"))
947 (let* ((length (length kill-ring))
949 (nthcdr (% (+ n (- length (length kill-ring-yank-pointer)))
953 (setq kill-ring-yank-pointer ARGth-kill-element))
954 (car ARGth-kill-element)))))
958 ;;;; Commands for manipulating the kill ring.
960 (defun kill-region (beg end)
961 "Kill between point and mark.
962 The text is deleted but saved in the kill ring.
963 The command \\[yank] can retrieve it from there.
964 \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
966 This is the primitive for programs to kill text (as opposed to deleting it).
967 Supply two arguments, character numbers indicating the stretch of text
969 Any command that calls this function is a \"kill command\".
970 If the previous command was also a kill command,
971 the text killed this time appends to the text killed last time
972 to make one entry in the kill ring."
975 ;; If the buffer was read-only, we used to just do a
976 ;; copy-region-as-kill. This was never what I wanted - usually I
977 ;; was making a mistake and trying to edit a file checked into RCS -
978 ;; so I've taken the code out.
979 ((not (or (eq buffer-undo-list t)
980 (eq last-command 'kill-region)
982 ;; Don't let the undo list be truncated before we can even access it.
983 (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100)))
984 (delete-region beg end)
985 ;; Take the same string recorded for undo
986 ;; and put it in the kill-ring.
987 (kill-new (car (car buffer-undo-list)))
988 (setq this-command 'kill-region)))
990 (copy-region-as-kill beg end)
991 (delete-region beg end))))
993 (defun copy-region-as-kill (beg end)
994 "Save the region as if killed, but don't kill it.
995 If `interprogram-cut-function' is non-nil, also save the text for a window
996 system cut and paste."
998 (if (eq last-command 'kill-region)
999 (kill-append (buffer-substring beg end) (< end beg))
1000 (kill-new (buffer-substring beg end)))
1001 (setq this-command 'kill-region)
1004 (defun kill-ring-save (beg end)
1005 "Save the region as if killed, but don't kill it."
1007 (copy-region-as-kill beg end)
1010 (let ((other-end (if (= (point) beg) end beg)))
1011 (if (pos-visible-in-window-p other-end (selected-window))
1013 (goto-char other-end)
1015 (let* ((killed-text (current-kill 0))
1016 (message-len (min (length killed-text) 40)))
1018 ;; Don't say "killed"; that is misleading.
1019 (message "Saved text until \"%s\""
1020 (substring killed-text (- message-len)))
1021 (message "Saved text from \"%s\""
1022 (substring killed-text 0 message-len)))))))))
1024 (defun append-next-kill ()
1025 "Cause following command, if it kills, to append to previous kill."
1029 (setq this-command 'kill-region)
1030 (message "If the next command is a kill, it will append"))
1031 (setq last-command 'kill-region)))
1033 (defun yank-pop (arg)
1034 "Replace just-yanked stretch of killed text with a different stretch.
1035 This command is allowed only immediately after a `yank' or a `yank-pop'.
1036 At such a time, the region contains a stretch of reinserted
1037 previously-killed text. `yank-pop' deletes that text and inserts in its
1038 place a different stretch of killed text.
1040 With no argument, the previous kill is inserted.
1041 With argument N, insert the Nth previous kill.
1042 If N is negative, this is a more recent kill.
1044 The sequence of kills wraps around, so that after the oldest one
1045 comes the newest one."
1047 (if (not (eq last-command 'yank))
1048 (error "Previous command was not a yank"))
1049 (setq this-command 'yank)
1050 (let ((before (< (point) (mark))))
1051 (delete-region (point) (mark))
1053 (insert (current-kill arg))
1054 (if before (exchange-point-and-mark))))
1056 (defun yank (&optional arg)
1057 "Reinsert the last stretch of killed text.
1058 More precisely, reinsert the stretch of killed text most recently
1059 killed OR yanked. Put point at end, and set mark at beginning.
1060 With just C-u as argument, same but put point at beginning (and mark at end).
1061 With argument N, reinsert the Nth most recently killed stretch of killed
1063 See also the command \\[yank-pop]."
1066 (insert (current-kill (cond
1071 (exchange-point-and-mark)))
1073 (defun rotate-yank-pointer (arg)
1074 "Rotate the yanking point in the kill ring.
1075 With argument, rotate that many kills forward (or backward, if negative)."
1080 (defun insert-buffer (buffer)
1081 "Insert after point the contents of BUFFER.
1082 Puts mark after the inserted text.
1083 BUFFER may be a buffer or a buffer name."
1084 (interactive (list (progn (barf-if-buffer-read-only)
1085 (read-buffer "Insert buffer: " (other-buffer) t))))
1086 (or (bufferp buffer)
1087 (setq buffer (get-buffer buffer)))
1088 (let (start end newmark)
1092 (setq start (point-min) end (point-max)))
1093 (insert-buffer-substring buffer start end)
1094 (setq newmark (point)))
1095 (push-mark newmark)))
1097 (defun append-to-buffer (buffer start end)
1098 "Append to specified buffer the text of the region.
1099 It is inserted into that buffer before its point.
1101 When calling from a program, give three arguments:
1102 BUFFER (or buffer name), START and END.
1103 START and END specify the portion of the current buffer to be copied."
1105 (list (read-buffer "Append to buffer: " (other-buffer nil t) t)))
1106 (let ((oldbuf (current-buffer)))
1108 (set-buffer (get-buffer-create buffer))
1109 (insert-buffer-substring oldbuf start end))))
1111 (defun prepend-to-buffer (buffer start end)
1112 "Prepend to specified buffer the text of the region.
1113 It is inserted into that buffer after its point.
1115 When calling from a program, give three arguments:
1116 BUFFER (or buffer name), START and END.
1117 START and END specify the portion of the current buffer to be copied."
1118 (interactive "BPrepend to buffer: \nr")
1119 (let ((oldbuf (current-buffer)))
1121 (set-buffer (get-buffer-create buffer))
1123 (insert-buffer-substring oldbuf start end)))))
1125 (defun copy-to-buffer (buffer start end)
1126 "Copy to specified buffer the text of the region.
1127 It is inserted into that buffer, replacing existing text there.
1129 When calling from a program, give three arguments:
1130 BUFFER (or buffer name), START and END.
1131 START and END specify the portion of the current buffer to be copied."
1132 (interactive "BCopy to buffer: \nr")
1133 (let ((oldbuf (current-buffer)))
1135 (set-buffer (get-buffer-create buffer))
1138 (insert-buffer-substring oldbuf start end)))))
1141 "Return this buffer's mark value as integer, or nil if no mark.
1142 If you are using this in an editing command, you are most likely making
1143 a mistake; see the documentation of `set-mark'."
1144 (marker-position (mark-marker)))
1146 (defun set-mark (pos)
1147 "Set this buffer's mark to POS. Don't use this function!
1148 That is to say, don't use this function unless you want
1149 the user to see that the mark has moved, and you want the previous
1150 mark position to be lost.
1152 Normally, when a new mark is set, the old one should go on the stack.
1153 This is why most applications should use push-mark, not set-mark.
1155 Novice Emacs Lisp programmers often try to use the mark for the wrong
1156 purposes. The mark saves a location for the user's convenience.
1157 Most editing commands should not alter the mark.
1158 To remember a location for internal use in the Lisp program,
1159 store it in a Lisp variable. Example:
1161 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
1163 (set-marker (mark-marker) pos (current-buffer)))
1165 (defvar mark-ring nil
1166 "The list of saved former marks of the current buffer,
1167 most recent first.")
1168 (make-variable-buffer-local 'mark-ring)
1170 (defconst mark-ring-max 16
1171 "*Maximum size of mark ring. Start discarding off end if gets this big.")
1173 (defun set-mark-command (arg)
1174 "Set mark at where point is, or jump to mark.
1175 With no prefix argument, set mark, and push old mark position on mark ring.
1176 With argument, jump to mark, and pop a new position for mark off the ring.
1178 Novice Emacs Lisp programmers often try to use the mark for the wrong
1179 purposes. See the documentation of `set-mark' for more information."
1184 (error "No mark set in this buffer")
1188 (defun push-mark (&optional location nomsg)
1189 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
1190 Displays \"Mark set\" unless the optional second arg NOMSG is non-nil.
1192 Novice Emacs Lisp programmers often try to use the mark for the wrong
1193 purposes. See the documentation of `set-mark' for more information."
1196 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
1197 (if (> (length mark-ring) mark-ring-max)
1199 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
1200 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
1201 (set-mark (or location (point)))
1202 (or nomsg executing-macro (> (minibuffer-depth) 0)
1203 (message "Mark set"))
1207 "Pop off mark ring into the buffer's actual mark.
1208 Does not set point. Does nothing if mark ring is empty."
1211 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
1212 (set-mark (+ 0 (car mark-ring)))
1213 (move-marker (car mark-ring) nil)
1214 (if (null (mark)) (ding))
1215 (setq mark-ring (cdr mark-ring)))))
1217 (fset 'exchange-dot-and-mark 'exchange-point-and-mark)
1218 (defun exchange-point-and-mark ()
1219 "Put the mark where point is now, and point where the mark is now."
1221 (let ((omark (mark)))
1223 (error "No mark set in this buffer"))
1228 (defun next-line (arg)
1229 "Move cursor vertically down ARG lines.
1230 If there is no character in the target line exactly under the current column,
1231 the cursor is positioned after the character in that line which spans this
1232 column, or at the end of the line if it is not long enough.
1233 If there is no line in the buffer after this one,
1234 a newline character is inserted to create a line
1235 and the cursor moves to that line.
1237 The command \\[set-goal-column] can be used to create
1238 a semipermanent goal column to which this command always moves.
1239 Then it does not try to move vertically. This goal column is stored
1240 in `goal-column', which is nil when there is none.
1242 If you are thinking of using this in a Lisp program, consider
1243 using `forward-line' instead. It is usually easier to use
1244 and more reliable (no dependence on goal column, etc.)."
1247 (let ((opoint (point)))
1249 (if (or (= opoint (point))
1250 (not (eq (preceding-char) ?\n)))
1257 (defun previous-line (arg)
1258 "Move cursor vertically up ARG lines.
1259 If there is no character in the target line exactly over the current column,
1260 the cursor is positioned after the character in that line which spans this
1261 column, or at the end of the line if it is not long enough.
1263 The command \\[set-goal-column] can be used to create
1264 a semipermanent goal column to which this command always moves.
1265 Then it does not try to move vertically.
1267 If you are thinking of using this in a Lisp program, consider using
1268 `forward-line' with a negative argument instead. It is usually easier
1269 to use and more reliable (no dependence on goal column, etc.)."
1274 (defconst track-eol nil
1275 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
1276 This means moving to the end of each line moved onto.
1277 The beginning of a blank line does not count as the end of a line.")
1279 (defvar goal-column nil
1280 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil.")
1281 (make-variable-buffer-local 'goal-column)
1283 (defvar temporary-goal-column 0
1284 "Current goal column for vertical motion.
1285 It is the column where point was
1286 at the start of current run of vertical motion commands.
1287 When the `track-eol' feature is doing its job, the value is 9999.")
1289 (defun line-move (arg)
1290 (if (not (or (eq last-command 'next-line)
1291 (eq last-command 'previous-line)))
1292 (setq temporary-goal-column
1293 (if (and track-eol (eolp)
1294 ;; Don't count beg of empty line as end of line
1295 ;; unless we just did explicit end-of-line.
1296 (or (not (bolp)) (eq last-command 'end-of-line)))
1299 (if (not (integerp selective-display))
1301 ;; Move by arg lines, but ignore invisible ones.
1306 (setq arg (1- arg)))
1308 (vertical-motion -1)
1310 (setq arg (1+ arg))))
1311 (move-to-column (or goal-column temporary-goal-column))
1314 ;;; Many people have said they rarely use this feature, and often type
1315 ;;; it by accident. Maybe it shouldn't even be on a key.
1316 (put 'set-goal-column 'disabled t)
1318 (defun set-goal-column (arg)
1319 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
1320 Those commands will move to this position in the line moved to
1321 rather than trying to keep the same horizontal position.
1322 With a non-nil argument, clears out the goal column
1323 so that \\[next-line] and \\[previous-line] resume vertical motion.
1324 The goal column is stored in the variable `goal-column'."
1328 (setq goal-column nil)
1329 (message "No goal column"))
1330 (setq goal-column (current-column))
1331 (message (substitute-command-keys
1332 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
1336 (defun transpose-chars (arg)
1337 "Interchange characters around point, moving forward one character.
1338 With prefix arg ARG, effect is to take character before point
1339 and drag it forward past ARG other characters (backward if ARG negative).
1340 If no argument and at end of line, the previous two chars are exchanged."
1342 (and (null arg) (eolp) (forward-char -1))
1343 (transpose-subr 'forward-char (prefix-numeric-value arg)))
1345 (defun transpose-words (arg)
1346 "Interchange words around point, leaving point at end of them.
1347 With prefix arg ARG, effect is to take word before or around point
1348 and drag it forward past ARG other words (backward if ARG negative).
1349 If ARG is zero, the words around or after point and around or after mark
1352 (transpose-subr 'forward-word arg))
1354 (defun transpose-sexps (arg)
1355 "Like \\[transpose-words] but applies to sexps.
1356 Does not work on a sexp that point is in the middle of
1357 if it is a list or string."
1359 (transpose-subr 'forward-sexp arg))
1361 (defun transpose-lines (arg)
1362 "Exchange current line and previous line, leaving point after both.
1363 With argument ARG, takes previous line and moves it past ARG lines.
1364 With argument 0, interchanges line point is in with line mark is in."
1366 (transpose-subr (function
1370 ;; Move forward over a line,
1371 ;; but create a newline if none exists yet.
1376 (forward-line arg))))
1379 (defun transpose-subr (mover arg)
1380 (let (start1 end1 start2 end2)
1387 (setq start2 (point))
1392 (setq start1 (point))
1394 (exchange-point-and-mark)))
1397 (setq start1 (point))
1403 (setq start2 (point))
1406 (setq arg (1- arg)))
1409 (setq start2 (point))
1411 (setq start1 (point))
1417 (setq arg (1+ arg)))))
1419 (defun transpose-subr-1 ()
1420 (if (> (min end1 end2) (max start1 start2))
1421 (error "Don't have two things to transpose"))
1422 (let ((word1 (buffer-substring start1 end1))
1423 (word2 (buffer-substring start2 end2)))
1424 (delete-region start2 end2)
1427 (goto-char (if (< start1 start2) start1
1428 (+ start1 (- (length word1) (length word2)))))
1429 (delete-char (length word1))
1432 (defconst comment-column 32
1433 "*Column to indent right-margin comments to.
1434 Setting this variable automatically makes it local to the current buffer.
1435 Each mode establishes a different default value for this variable; you
1436 can the value for a particular mode using that mode's hook.")
1437 (make-variable-buffer-local 'comment-column)
1439 (defconst comment-start nil
1440 "*String to insert to start a new comment, or nil if no comment syntax defined.")
1442 (defconst comment-start-skip nil
1443 "*Regexp to match the start of a comment plus everything up to its body.
1444 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
1445 at the place matched by the close of the first pair.")
1447 (defconst comment-end ""
1448 "*String to insert to end a new comment.
1449 Should be an empty string if comments are terminated by end-of-line.")
1451 (defconst comment-indent-hook
1452 '(lambda () comment-column)
1453 "Function to compute desired indentation for a comment.
1454 This function is called with no args with point at the beginning of
1455 the comment's starting delimiter.")
1457 (defun indent-for-comment ()
1458 "Indent this line's comment to comment column, or insert an empty comment."
1460 (beginning-of-line 1)
1461 (if (null comment-start)
1462 (error "No comment syntax defined")
1463 (let* ((eolpos (save-excursion (end-of-line) (point)))
1465 (if (re-search-forward comment-start-skip eolpos 'move)
1466 (progn (setq cpos (point-marker))
1467 ;; Find the start of the comment delimiter.
1468 ;; If there were paren-pairs in comment-start-skip,
1469 ;; position at the end of the first pair.
1471 (goto-char (match-end 1))
1472 ;; If comment-start-skip matched a string with internal
1473 ;; whitespace (not final whitespace) then the delimiter
1474 ;; start at the end of that whitespace.
1475 ;; Otherwise, it starts at the beginning of what was matched.
1476 (skip-chars-backward " \t" (match-beginning 0))
1477 (skip-chars-backward "^ \t" (match-beginning 0)))))
1478 (setq begpos (point))
1479 ;; Compute desired indent.
1480 (if (= (current-column)
1481 (setq indent (funcall comment-indent-hook)))
1483 ;; If that's different from current, change it.
1484 (skip-chars-backward " \t")
1485 (delete-region (point) begpos)
1487 ;; An existing comment?
1489 (progn (goto-char cpos)
1490 (set-marker cpos nil))
1492 (insert comment-start)
1494 (insert comment-end))))))
1496 (defun set-comment-column (arg)
1497 "Set the comment column based on point.
1498 With no arg, set the comment column to the current column.
1499 With just minus as arg, kill any comment on this line.
1500 With any other arg, set comment column to indentation of the previous comment
1501 and then align or create a comment on this line at that column."
1509 (re-search-backward comment-start-skip)
1511 (re-search-forward comment-start-skip)
1512 (goto-char (match-beginning 0))
1513 (setq comment-column (current-column))
1514 (message "Comment column set to %d" comment-column))
1515 (indent-for-comment))
1516 (setq comment-column (current-column))
1517 (message "Comment column set to %d" comment-column))))
1519 (defun kill-comment (arg)
1520 "Kill the comment on this line, if any.
1521 With argument, kill comments on that many lines starting with this one."
1522 ;; this function loses in a lot of situations. it incorrectly recognises
1523 ;; comment delimiters sometimes (ergo, inside a string), doesn't work
1524 ;; with multi-line comments, can kill extra whitespace if comment wasn't
1525 ;; through end-of-line, et cetera.
1527 (or comment-start-skip (error "No comment syntax defined"))
1528 (let ((count (prefix-numeric-value arg)) endc)
1534 (and (string< "" comment-end)
1537 (re-search-forward (regexp-quote comment-end) endc 'move)
1538 (skip-chars-forward " \t")
1541 (if (re-search-forward comment-start-skip endc t)
1543 (goto-char (match-beginning 0))
1544 (skip-chars-backward " \t")
1545 (kill-region (point) endc)
1546 ;; to catch comments a line beginnings
1547 (indent-according-to-mode))))
1548 (if arg (forward-line 1))
1549 (setq count (1- count)))))
1551 (defun comment-region (beg end &optional arg)
1552 "Comment the region; third arg numeric means use ARG comment characters.
1553 If ARG is negative, delete that many comment characters instead.
1554 Comments are terminated on each line, even for syntax in which newline does
1555 not end the comment. Blank lines do not get comments."
1556 ;; if someone wants it to only put a comment-start at the beginning and
1557 ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
1558 ;; is easy enough. No option is made here for other than commenting
1560 (interactive "r\np")
1561 (or comment-start (error "No comment syntax is defined"))
1562 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
1565 (let ((cs comment-start) (ce comment-end))
1566 (cond ((not arg) (setq arg 1))
1568 (while (> (setq arg (1- arg)) 0)
1569 (setq cs (concat cs comment-start)
1570 ce (concat ce comment-end)))))
1571 (narrow-to-region beg end)
1576 (while (and (> 1 (setq count (1+ count)))
1577 (looking-at (regexp-quote cs)))
1578 (delete-char (length cs)))
1579 (if (string= "" ce) ()
1581 (while (> 1 (setq count (1+ count)))
1583 ;; this is questionable if comment-end ends in whitespace
1584 ;; that is pretty brain-damaged though
1585 (skip-chars-backward " \t")
1586 (backward-char (length ce))
1587 (if (looking-at (regexp-quote ce))
1588 (delete-char (length ce)))))
1590 (if (looking-at "[ \t]*$") ()
1592 (if (string= "" ce) ()
1595 (search-forward "\n" nil 'move)))))))
1597 (defun backward-word (arg)
1598 "Move backward until encountering the end of a word.
1599 With argument, do this that many times.
1600 In programs, it is faster to call `forward-word' with negative arg."
1602 (forward-word (- arg)))
1604 (defun mark-word (arg)
1605 "Set mark arg words away from point."
1612 (defun kill-word (arg)
1613 "Kill characters forward until encountering the end of a word.
1614 With argument, do this that many times."
1616 (kill-region (point) (save-excursion (forward-word arg) (point))))
1618 (defun backward-kill-word (arg)
1619 "Kill characters backward until encountering the end of a word.
1620 With argument, do this that many times."
1622 (kill-word (- arg)))
1624 (defconst fill-prefix nil
1625 "*String for filling to insert at front of new line, or nil for none.
1626 Setting this variable automatically makes it local to the current buffer.")
1627 (make-variable-buffer-local 'fill-prefix)
1629 (defconst auto-fill-inhibit-regexp nil
1630 "*Regexp to match lines which should not be auto-filled.")
1632 (defun do-auto-fill ()
1634 (or (and auto-fill-inhibit-regexp
1635 (save-excursion (beginning-of-line)
1636 (looking-at auto-fill-inhibit-regexp)))
1637 (while (and (not give-up) (> (current-column) fill-column))
1639 (let ((opoint (point)))
1641 (move-to-column (1+ fill-column))
1642 (skip-chars-backward "^ \t\n")
1644 (re-search-forward "[ \t]" opoint t))
1645 (skip-chars-backward " \t")
1647 ;; If there is a space on the line before fill-point,
1648 ;; and nonspaces precede it, break the line there.
1650 (goto-char fill-point)
1652 ;; If point is at the fill-point, do not `save-excursion'.
1653 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
1654 ;; point will end up before it rather than after it.
1656 (skip-chars-backward " \t")
1657 (= (point) fill-point))
1658 (indent-new-comment-line)
1660 (goto-char fill-point)
1661 (indent-new-comment-line)))
1662 ;; No place to break => stop trying.
1663 (setq give-up t)))))))
1665 (defconst comment-multi-line nil
1666 "*Non-nil means \\[indent-new-comment-line] should continue same comment
1667 on new line, with no new terminator or starter.
1668 This is obsolete because you might as well use \\[newline-and-indent].")
1670 (defun indent-new-comment-line ()
1671 "Break line at point and indent, continuing comment if presently within one.
1672 The body of the continued comment is indented under the previous comment line.
1674 This command is intended for styles where you write a comment per line,
1675 starting a new comment (and terminating it if necessary) on each line.
1676 If you want to continue one comment across several lines, use \\[newline-and-indent]."
1678 (let (comcol comstart)
1679 (skip-chars-backward " \t")
1680 (delete-region (point)
1681 (progn (skip-chars-forward " \t")
1684 (if (not comment-multi-line)
1686 (if (and comment-start-skip
1687 (let ((opoint (point)))
1689 (re-search-forward comment-start-skip opoint t)))
1690 ;; The old line is a comment.
1691 ;; Set WIN to the pos of the comment-start.
1692 ;; But if the comment is empty, look at preceding lines
1693 ;; to find one that has a nonempty comment.
1694 (let ((win (match-beginning 0)))
1695 (while (and (eolp) (not (bobp))
1698 (setq opoint (point))
1700 (re-search-forward comment-start-skip opoint t)))
1701 (setq win (match-beginning 0)))
1702 ;; Indent this line like what we found.
1704 (setq comcol (current-column))
1705 (setq comstart (buffer-substring (point) (match-end 0)))))))
1707 (let ((comment-column comcol)
1708 (comment-start comstart)
1709 (comment-end comment-end))
1710 (and comment-end (not (equal comment-end ""))
1711 ; (if (not comment-multi-line)
1714 (insert comment-end)
1716 ; (setq comment-column (+ comment-column (length comment-start))
1721 (setq comment-end ""))
1724 (indent-for-comment)
1726 ;; Make sure we delete the newline inserted above.
1730 (insert fill-prefix)
1731 (indent-according-to-mode)))))
1733 (defun auto-fill-mode (&optional arg)
1734 "Toggle auto-fill mode.
1735 With arg, turn auto-fill mode on if and only if arg is positive.
1736 In auto-fill mode, inserting a space at a column beyond fill-column
1737 automatically breaks the line at a previous space."
1739 (prog1 (setq auto-fill-function
1741 (not auto-fill-function)
1742 (> (prefix-numeric-value arg) 0))
1746 (set-buffer-modified-p (buffer-modified-p))))
1748 (defun turn-on-auto-fill ()
1749 "Unconditionally turn on Auto Fill mode."
1752 (defun set-fill-column (arg)
1753 "Set `fill-column' to current column, or to argument if given.
1754 The variable `fill-column' has a separate value for each buffer."
1756 (setq fill-column (if (integerp arg) arg (current-column)))
1757 (message "fill-column set to %d" fill-column))
1759 (defun set-selective-display (arg)
1760 "Set `selective-display' to ARG; clear it if no arg.
1761 When the value of `selective-display' is a number > 0,
1762 lines whose indentation is >= that value are not displayed.
1763 The variable `selective-display' has a separate value for each buffer."
1765 (if (eq selective-display t)
1766 (error "selective-display already in use for marked lines"))
1769 (narrow-to-region (point-min) (point))
1770 (goto-char (window-start))
1771 (vertical-motion (window-height)))))
1772 (setq selective-display
1773 (and arg (prefix-numeric-value arg)))
1774 (recenter current-vpos))
1775 (set-window-start (selected-window) (window-start (selected-window)))
1776 (princ "selective-display set to " t)
1777 (prin1 selective-display t)
1780 (defun overwrite-mode (arg)
1781 "Toggle overwrite mode.
1782 With arg, turn overwrite mode on iff arg is positive.
1783 In overwrite mode, printing characters typed in replace existing text
1784 on a one-for-one basis, rather than pushing it to the right."
1786 (setq overwrite-mode
1787 (if (null arg) (not overwrite-mode)
1788 (> (prefix-numeric-value arg) 0)))
1789 (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line.
1791 (defvar blink-matching-paren t
1792 "*Non-nil means show matching open-paren when close-paren is inserted.")
1794 (defconst blink-matching-paren-distance 4000
1795 "*If non-nil, is maximum distance to search for matching open-paren
1796 when close-paren is inserted.")
1798 (defun blink-matching-open ()
1799 "Move cursor momentarily to the beginning of the sexp before point."
1801 (and (> (point) (1+ (point-min)))
1802 (/= (char-syntax (char-after (- (point) 2))) ?\\ )
1803 blink-matching-paren
1804 (let* ((oldpos (point))
1809 (if blink-matching-paren-distance
1810 (narrow-to-region (max (point-min)
1811 (- (point) blink-matching-paren-distance))
1814 (setq blinkpos (scan-sexps oldpos -1))
1816 (and blinkpos (/= (char-syntax (char-after blinkpos))
1819 (/= (char-after (1- oldpos))
1820 (logand (lsh (aref (syntax-table)
1821 (char-after blinkpos))
1824 (if mismatch (setq blinkpos nil))
1827 (goto-char blinkpos)
1828 (if (pos-visible-in-window-p)
1830 (goto-char blinkpos)
1834 (skip-chars-backward " \t")
1836 (buffer-substring (progn (beginning-of-line) (point))
1838 (buffer-substring blinkpos
1841 (skip-chars-forward "\n \t")
1845 (message "Mismatched parentheses"))
1846 ((not blink-matching-paren-distance)
1847 (message "Unmatched parenthesis"))))))))
1849 ;Turned off because it makes dbx bomb out.
1850 (setq blink-paren-function 'blink-matching-open)
1852 ; this is just something for the luser to see in a keymap -- this is not
1853 ; how quitting works normally!
1854 (defun keyboard-quit ()
1855 "Signal a quit condition."
1859 (define-key global-map "\C-g" 'keyboard-quit)
1861 (defun set-variable (var val)
1862 "Set VARIABLE to VALUE. VALUE is a Lisp object.
1863 When using this interactively, supply a Lisp expression for VALUE.
1864 If you want VALUE to be a string, you must surround it with doublequotes.
1866 If VARIABLE has a `variable-interactive' property, that is used as if
1867 it were the arg to `interactive' (which see) to interactively read the value."
1869 (let* ((var (read-variable "Set variable: "))
1870 (minibuffer-help-form
1875 (with-output-to-temp-buffer "*Help*"
1877 (princ "\nDocumentation:\n")
1878 (princ (substring (documentation-property var 'variable-documentation)
1881 (let ((print-length 20))
1882 (princ "\n\nCurrent value: ")
1883 (prin1 (symbol-value var))))
1886 (let ((prop (get var 'variable-interactive)))
1888 ;; Use VAR's `variable-interactive' property
1889 ;; as an interactive spec for prompting.
1890 (call-interactively (list 'lambda '(arg)
1891 (list 'interactive prop)
1893 (eval-minibuffer (format "Set %s to value: " var)))))))
1896 ;;; simple.el ends here