home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / prim / simple.el < prev    next >
Encoding:
Text File  |  1993-02-14  |  55.0 KB  |  1,590 lines

  1. ;; Basic editing commands for Emacs
  2. ;; Copyright (C) 1985-1993 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 2, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. ;;; Changes for zmacs-style active-regions:
  22. ;;;
  23. ;;; beginning-of-buffer, end-of-buffer, count-lines-region, 
  24. ;;; count-lines-buffer, what-line, what-cursor-position, set-goal-column,
  25. ;;; set-fill-column, prefix-arg-internal, and line-move (which is used by
  26. ;;; next-line and previous-line) set zmacs-region-stays to t, so that they
  27. ;;; don't affect the current region-hilighting state.
  28. ;;;
  29. ;;; mark-whole-buffer, mark-word, exchange-point-and-mark, and
  30. ;;; set-mark-command (without an argument) call zmacs-activate-region.
  31. ;;;
  32. ;;; mark takes an optional arg like the new Fmark_marker() does.  When 
  33. ;;; the region is not active, mark returns nil unless the optional arg is true.
  34. ;;;
  35. ;;; push-mark, pop-mark, exchange-point-and-mark, and set-marker, and
  36. ;;; set-mark-command use (mark t) so that they can access the mark whether
  37. ;;; the region is active or not.  
  38. ;;;
  39. ;;; shell-command, shell-command-on-region, yank, and yank-pop (which all
  40. ;;; push a mark) have been altered to call exchange-point-and-mark with an
  41. ;;; argument, meaning "don't activate the region".  These commands  only use
  42. ;;; exchange-point-and-mark to position the newly-pushed mark correctly, so
  43. ;;; this isn't a user-visible change.  These functions have also been altered
  44. ;;; to use (mark t) for the same reason.
  45.  
  46. (defun open-line (arg)
  47.   "Insert a newline and leave point before it.  If there is a fill
  48. prefix, inserts the fill prefix after the newline that it inserts.
  49. With arg, inserts that many newlines."
  50.   (interactive "*p")
  51.   (let ((flag (and (bolp) (not (bobp)))))
  52.     (if flag (forward-char -1))
  53.     (while (> arg 0)
  54.       (save-excursion
  55.         (insert ?\n)
  56. ;; screw this
  57. ;;    (if fill-prefix (insert fill-prefix))
  58.     )
  59.       (setq arg (1- arg)))
  60.     (if flag (forward-char 1))))
  61.  
  62. (defun split-line ()
  63.   "Split current line, moving portion beyond point vertically down."
  64.   (interactive "*")
  65.   (skip-chars-forward " \t")
  66.   (let ((col (current-column))
  67.     (pos (point)))
  68.     (insert ?\n)
  69.     (indent-to col 0)
  70.     (goto-char pos)))
  71.  
  72. (defun quoted-insert (arg)
  73.   "Read next input character and insert it.
  74. Useful for inserting control characters.
  75. You may also type up to 3 octal digits, to insert a character with that code"
  76.   (interactive "*p")
  77.   (let ((char (read-quoted-char)))
  78.     (while (> arg 0)
  79.       (insert char)
  80.       (setq arg (1- arg)))))
  81.  
  82. (defun delete-indentation (&optional arg)
  83.   "Join this line to previous and fix up whitespace at join.
  84. With argument, join this line to following line."
  85.   (interactive "*P")
  86.   (beginning-of-line)
  87.   (if arg (forward-line 1))
  88.   (if (eq (preceding-char) ?\n)
  89.       (progn
  90.     (delete-region (point) (1- (point)))
  91.     (fixup-whitespace))))
  92.  
  93. (defun fixup-whitespace ()
  94.   "Fixup white space between objects around point.
  95. Leave one space or none, according to the context."
  96.   (interactive "*")
  97.   (save-excursion
  98.     (delete-horizontal-space)
  99.     (if (or (looking-at "^\\|\\s)")
  100.         (save-excursion (forward-char -1)
  101.                 (looking-at "$\\|\\s(\\|\\s'")))
  102.     nil
  103.       (insert ?\ ))))
  104.  
  105. (defun delete-horizontal-space ()
  106.   "Delete all spaces and tabs around point."
  107.   (interactive "*")
  108.   (skip-chars-backward " \t")
  109.   (delete-region (point) (progn (skip-chars-forward " \t") (point))))
  110.  
  111. (defun just-one-space ()
  112.   "Delete all spaces and tabs around point, leaving one space."
  113.   (interactive "*")
  114.   (skip-chars-backward " \t")
  115.   (if (= (following-char) ? )
  116.       (forward-char 1)
  117.     (insert ? ))
  118.   (delete-region (point) (progn (skip-chars-forward " \t") (point))))
  119.  
  120. (defun delete-blank-lines ()
  121.   "On blank line, delete all surrounding blank lines, leaving just one.
  122. On isolated blank line, delete that one.
  123. On nonblank line, delete all blank lines that follow it."
  124.   (interactive "*")
  125.   (let (thisblank singleblank)
  126.     (save-excursion
  127.       (beginning-of-line)
  128.       (setq thisblank (looking-at "[ \t]*$"))
  129.       (setq singleblank
  130.         (and thisblank
  131.          (not (looking-at "[ \t]*\n[ \t]*$"))
  132.          (or (bobp)
  133.              (progn (forward-line -1)
  134.                 (not (looking-at "[ \t]*$")))))))
  135.     (if thisblank
  136.     (progn
  137.       (beginning-of-line)
  138.       (if singleblank (forward-line 1))
  139.       (delete-region (point)
  140.              (if (re-search-backward "[^ \t\n]" nil t)
  141.                  (progn (forward-line 1) (point))
  142.                (point-min)))))
  143.     (if (not (and thisblank singleblank))
  144.     (save-excursion
  145.       (end-of-line)
  146.       (forward-line 1)
  147.       (delete-region (point)
  148.              (if (re-search-forward "[^ \t\n]" nil t)
  149.                  (progn (beginning-of-line) (point))
  150.                (point-max)))))))
  151.  
  152. (defun back-to-indentation ()
  153.   "Move point to the first non-whitespace character on this line."
  154.   (interactive)
  155.   (beginning-of-line 1)
  156.   (skip-chars-forward " \t"))
  157.  
  158. (defun newline-and-indent ()
  159.   "Insert a newline, then indent according to major mode.
  160. Indentation is done using the current indent-line-function.
  161. In programming language modes, this is the same as TAB.
  162. In some text modes, where TAB inserts a tab, this indents to the
  163. specified left-margin column."
  164.   (interactive "*")
  165.   (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  166.   (insert ?\n)
  167.   (indent-according-to-mode))
  168.  
  169. (defun reindent-then-newline-and-indent ()
  170.   "Reindent current line, insert newline, then indent the new line.
  171. Indentation of both lines is done according to the current major mode,
  172. which means that the current value of indent-line-function is called.
  173. In programming language modes, this is the same as TAB.
  174. In some text modes, where TAB inserts a tab, this indents to the
  175. specified left-margin column."
  176.   (interactive "*")
  177.   (save-excursion
  178.     (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  179.     (indent-according-to-mode))
  180.   (insert ?\n)
  181.   (indent-according-to-mode))
  182.  
  183. ;; Internal subroutine of delete-char
  184. (defun kill-forward-chars (arg)
  185.   (if (listp arg) (setq arg (car arg)))
  186.   (if (eq arg '-) (setq arg -1))
  187.   (kill-region (point) (+ (point) arg)))
  188.  
  189. ;; Internal subroutine of backward-delete-char
  190. (defun kill-backward-chars (arg)
  191.   (if (listp arg) (setq arg (car arg)))
  192.   (if (eq arg '-) (setq arg -1))
  193.   (kill-region (point) (- (point) arg)))
  194.  
  195. (defun backward-delete-char-untabify (arg &optional killp)
  196.   "Delete characters backward, changing tabs into spaces.
  197. Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
  198. Interactively, ARG is the prefix arg (default 1)
  199. and KILLP is t if prefix arg is was specified."
  200.   (interactive "*p\nP")
  201.   (let ((count arg))
  202.     (save-excursion
  203.       (while (and (> count 0) (not (bobp)))
  204.     (if (= (preceding-char) ?\t)
  205.         (let ((col (current-column)))
  206.           (forward-char -1)
  207.           (setq col (- col (current-column)))
  208.           (insert-char ?\ col)
  209.           (delete-char 1)))
  210.     (forward-char -1)
  211.     (setq count (1- count)))))
  212.   (delete-backward-char arg killp)
  213.   ;; In overwrite mode, back over columns while clearing them out,
  214.   ;; unless at end of line.
  215.   (and overwrite-mode (not (eolp))
  216.        (save-excursion (insert-char ?\  arg))))
  217.  
  218. (defun zap-to-char (arg char)
  219.   "Kill up to and including ARG'th occurrence of CHAR.
  220. Goes backward if ARG is negative; error if CHAR not found."
  221.   (interactive "*p\ncZap to char: ")
  222.   (kill-region (point) (progn
  223.              (search-forward (char-to-string char) nil nil arg)
  224. ;             (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
  225.              (point))))
  226.  
  227. (defun beginning-of-buffer (&optional arg)
  228.   "Move point to the beginning of the buffer; leave mark at previous position.
  229. With arg N, put point N/10 of the way from the true beginning.
  230. Don't use this in Lisp programs!
  231. \(goto-char (point-min)) is faster and avoids clobbering the mark."
  232.   (interactive "P")
  233.   (push-mark)
  234.   (goto-char (if arg
  235.          (if (> (buffer-size) 10000)
  236.              ;; Avoid overflow for large buffer sizes!
  237.              (* (prefix-numeric-value arg)
  238.             (/ (buffer-size) 10))
  239.            (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10))
  240.            (point-min)))
  241.   (setq zmacs-region-stays t)
  242.   (if arg (forward-line 1)))
  243.  
  244. ;;; ought to be used elsewhere too, but this is where it annoys me most -- gumby
  245. (defvar dont-redisplay-when-moving-within-window nil)
  246.  
  247. (defun end-of-buffer (&optional arg)
  248.   "Move point to the end of the buffer; leave mark at previous position.
  249. With arg N, put point N/10 of the way from the true end.
  250. Don't use this in Lisp programs!
  251. \(goto-char (point-max)) is faster and avoids clobbering the mark."
  252.   (interactive "P")
  253.   (push-mark)
  254.   (setq zmacs-region-stays t)
  255.   (let ((scroll-to-end (not (pos-visible-in-window-p (point-max)))))
  256.     (goto-char (if arg
  257.            (- (1+ (buffer-size))
  258.               (if (> (buffer-size) 10000)
  259.               ;; Avoid overflow for large buffer sizes!
  260.               (* (prefix-numeric-value arg)
  261.                  (/ (buffer-size) 10))
  262.             (/ (* (buffer-size) (prefix-numeric-value arg)) 10)))
  263.          (point-max)))
  264.     (cond (arg (forward-line 1))
  265.       (scroll-to-end (recenter -3)))))
  266.  
  267. (defun mark-beginning-of-buffer (&optional arg)
  268.   "Push a mark at the beginning of the buffer; leave point where it is.
  269. With arg N, push mark N/10 of the way from the true beginning."
  270.   (interactive "P")
  271.   (push-mark (if arg
  272.          (if (> (buffer-size) 10000)
  273.              ;; Avoid overflow for large buffer sizes!
  274.              (* (prefix-numeric-value arg)
  275.             (/ (buffer-size) 10))
  276.            (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10))
  277.            (point-min)))
  278.   (zmacs-activate-region))
  279. (fset 'mark-bob 'mark-beginning-of-buffer)
  280.  
  281. (defun mark-end-of-buffer (&optional arg)
  282.   "Push a mark at the end of the buffer; leave point where it is.
  283. With arg N, push mark N/10 of the way from the true end."
  284.   (interactive "P")
  285.   (push-mark (if arg
  286.          (- (1+ (buffer-size))
  287.             (if (> (buffer-size) 10000)
  288.             ;; Avoid overflow for large buffer sizes!
  289.             (* (prefix-numeric-value arg)
  290.                (/ (buffer-size) 10))
  291.               (/ (* (buffer-size) (prefix-numeric-value arg)) 10)))
  292.            (point-max)))
  293.   (zmacs-activate-region))
  294. (fset 'mark-eob 'mark-end-of-buffer)
  295.  
  296. (defun mark-whole-buffer ()
  297.   "Put point at beginning and mark at end of buffer."
  298.   (interactive)
  299.   (push-mark (point))
  300.   (push-mark (point-max))
  301.   (goto-char (point-min))
  302.   (zmacs-activate-region))
  303.  
  304. (defun eval-current-buffer (&optional printflag)
  305.   "Evaluate the current buffer as Lisp code.
  306. Programs can pass argument PRINTFLAG which controls printing of output:
  307. nil means discard it; anything else is stream for print."
  308.   (interactive)
  309.   (eval-buffer (current-buffer) printflag))
  310.  
  311. (defun count-words-buffer (b)
  312.   (interactive "b")
  313.   (save-excursion
  314.     (let ((buf (or b (current-buffer))))
  315.       (set-buffer buf)
  316.       (message "Buffer has %d words"
  317.            (count-words-region (point-min) (point-max))))))
  318.  
  319. (defun count-words-region (debut fin)
  320.   (interactive "r")
  321.   (save-excursion
  322.     (let ((cnt 0))
  323.       (goto-char debut)
  324.       (while (< (point) fin)
  325.     (if (forward-word 1)
  326.         (setq cnt (1+ cnt))))
  327.       (message "Region has %d words" cnt)
  328.       cnt)))
  329.  
  330. (defun count-lines-region (start end)
  331.   "Print number of lines in the region."
  332.   (interactive "r")
  333.   (prog1 (message "Region has %d lines" (count-lines start end))
  334.     (setq zmacs-region-stays t)))
  335.  
  336. (defun count-lines-buffer (b)
  337.   (interactive "b")
  338.   (prog1
  339.    (save-excursion
  340.     (let ((buf (or b (current-buffer))))
  341.       (set-buffer buf)
  342.       (message "Buffer has %d lines" (count-lines (point-min) (point-max)))))
  343.    (setq zmacs-region-stays t)))
  344.  
  345. (defun what-line ()
  346.   "Print the current line number (in the buffer) of point."
  347.   (interactive)
  348.   (prog1
  349.    (save-restriction
  350.     (widen)
  351.     (save-excursion
  352.       (beginning-of-line)
  353.       (message "Line %d"
  354.            (1+ (count-lines 1 (point))))))
  355.    (setq zmacs-region-stays t)))
  356.  
  357. (defun count-lines (start end)
  358.   "Return number of lines between START and END.
  359. This is usually the number of newlines between them,
  360. but will be one more if START is not equal to END
  361. and the greater of them is not at the start of a line."
  362.   (save-excursion
  363.     (save-restriction
  364.       (narrow-to-region start end)
  365.       (goto-char (point-min))
  366.       (if (eq selective-display t)
  367.       (let ((done 0))
  368.         (while (re-search-forward "[\n\C-m]" nil t 40)
  369.           (setq done (+ 40 done)))
  370.         (while (re-search-forward "[\n\C-m]" nil t 1)
  371.           (setq done (+ 1 done)))
  372.         done)
  373.     (- (buffer-size) (forward-line (buffer-size)))))))
  374.  
  375. (defun what-cursor-position ()
  376.   "Print info on cursor position (on screen and within buffer)."
  377.   (interactive)
  378.   (prog1
  379.    (let* ((char (following-char))
  380.      (beg (point-min))
  381.      (end (point-max))
  382.          (pos (point))
  383.      (total (buffer-size))
  384.      (percent (if (> total 50000)
  385.               ;; Avoid overflow from multiplying by 100!
  386.               (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
  387.             (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
  388.      (hscroll (if (= (window-hscroll) 0)
  389.               ""
  390.             (format " Hscroll=%d" (window-hscroll))))
  391.      (col (current-column)))
  392.     (if (= pos end)
  393.     (if (or (/= beg 1) (/= end (1+ total)))
  394.         (message "point=%d of %d(%d%%) <%d - %d>  column %d %s"
  395.              pos total percent beg end col hscroll)
  396.       (message "point=%d of %d(%d%%)  column %d %s"
  397.            pos total percent col hscroll))
  398.       (if (or (/= beg 1) (/= end (1+ total)))
  399.       (message "Char: %s (0%o)  point=%d of %d(%d%%) <%d - %d>  column %d %s"
  400.            (single-key-description char) char pos total percent beg end col hscroll)
  401.     (message "Char: %s (0%o)  point=%d of %d(%d%%)  column %d %s"
  402.          (single-key-description char) char pos total percent col hscroll))))
  403.    (setq zmacs-region-stays t)))
  404.  
  405. (defun fundamental-mode ()
  406.   "Major mode not specialized for anything in particular.
  407. Other major modes are defined by comparison with this one."
  408.   (interactive)
  409.   (kill-all-local-variables))
  410.  
  411. (put 'eval-expression 'disabled t)
  412.  
  413. ;; We define this, rather than making  eval  interactive,
  414. ;; for the sake of completion of names like eval-region, eval-current-buffer.
  415. (defun eval-expression (expression)
  416.   "Evaluate EXPRESSION and print value in minibuffer.
  417. Value is also consed on to front of the variable `values'."
  418.   (interactive (list (read-from-minibuffer "Eval: "
  419.                        nil
  420.                                            read-expression-map t
  421.                        'minibuffer-sexp-history)))
  422.   (setq values (cons (eval expression) values))
  423.   (prin1 (car values) t))
  424.  
  425. (defun edit-and-eval-command (prompt command)
  426.   "Prompting with PROMPT, let user edit COMMAND and eval result.
  427. COMMAND is a Lisp expression.  Let user edit that expression in
  428. the minibuffer, then read and evaluate the result."
  429.   (let ((command (read-from-minibuffer prompt
  430.                        (prin1-to-string command)
  431.                        read-expression-map t
  432.                        'minibuffer-sexp-history)))
  433.     ;; Add edited command to command history, unless redundant.
  434.     (or (equal command (car command-history))
  435.     (setq command-history (cons command command-history)))
  436.     (eval command)))
  437.  
  438. (defun repeat-complex-command (arg)
  439.   "Edit and re-evaluate last complex command, or ARGth from last.
  440. A complex command is one which used the minibuffer.
  441. The command is placed in the minibuffer as a Lisp form for editing.
  442. The result is executed, repeating the command as changed.
  443. If the command has been changed or is not the most recent previous command
  444. it is added to the front of the command history.
  445. You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
  446. to get different commands to edit and resubmit."
  447.   (interactive "p")
  448.   (let ((elt (nth (1- arg) command-history))
  449.     (minibuffer-history-position arg)
  450.     (minibuffer-history-sexp-flag t)
  451.     newcmd)
  452.     (if elt
  453.     (progn
  454.       (setq newcmd (read-from-minibuffer "Redo: " 
  455.             (condition-case ()
  456.                 (let ((print-readably t)) (prin1-to-string elt))
  457.               (error (prin1-to-string elt)))
  458.             read-expression-map t (cons 'command-history arg)))
  459.       ;; If command was added to command-history as a string,
  460.       ;; get rid of that.  We want only evallable expressions there.
  461.       (if (stringp (car command-history))
  462.           (setq command-history (cdr command-history)))
  463.       ;; If command to be redone does not match front of history,
  464.       ;; add it to the history.
  465.       (or (equal newcmd (car command-history))
  466.           (setq command-history (cons newcmd command-history)))
  467.       (eval newcmd))
  468.       (ding))))
  469.  
  470.  
  471. (defun goto-line (arg)
  472.   "Goto line ARG, counting from line 1 at beginning of buffer."
  473.   (interactive "NGoto line: ")
  474.   (save-restriction
  475.     (widen)
  476.     (goto-char 1)
  477.     (if (eq selective-display t)
  478.     (re-search-forward "[\n\C-m]" nil 'end (1- arg))
  479.     (forward-line (1- arg)))))
  480.  
  481. ;Put this on C-x u, so we can force that rather than C-_ into startup msg
  482. (fset 'advertised-undo 'undo)
  483.  
  484. (defun undo (&optional arg)
  485.   "Undo some previous changes.
  486. Repeat this command to undo more changes.
  487. A numeric argument serves as a repeat count."
  488.   (interactive "*p")
  489.   (let ((modified (buffer-modified-p)))
  490.     (message "Undo!")
  491.     (or (eq last-command 'undo)
  492.     (progn (undo-start)
  493.            (undo-more 1)))
  494.     (setq this-command 'undo)
  495.     (undo-more (or arg 1))
  496.     (and modified (not (buffer-modified-p))
  497.      (delete-auto-save-file-if-necessary))))
  498.  
  499. (defun undo-start ()
  500.   "Move undo-pointer to front of undo records.
  501. The next call to undo-more will undo the most recently made change."
  502.   (if (eq buffer-undo-list t)
  503.       (error "No undo information in this buffer"))
  504.   (setq pending-undo-list buffer-undo-list))
  505.  
  506. (defun undo-more (count)
  507.   "Undo back N undo-boundaries beyond what was already undone recently.
  508. Call undo-start to get ready to undo recent changes,
  509. then call undo-more one or more times to undo them."
  510.   (or pending-undo-list
  511.       (error "No further undo information"))
  512.   (setq pending-undo-list (primitive-undo count pending-undo-list)))
  513.  
  514. (defvar last-shell-command "")
  515. (defvar last-shell-command-on-region "")
  516.  
  517. (defun shell-command (command &optional flag)
  518.   "Execute string COMMAND in inferior shell; display output, if any.
  519. If COMMAND ends in ampersand, execute it asynchronously.
  520.  
  521. Optional second arg non-nil (prefix arg, if interactive)
  522. means insert output in current buffer after point (leave mark after it).
  523. This cannot be done asynchronously."
  524.   (interactive (list (read-shell-command "Shell command: " last-shell-command)
  525.              current-prefix-arg))
  526.   (if flag
  527.       (progn (barf-if-buffer-read-only)
  528.          (push-mark)
  529.          (call-process shell-file-name nil t nil
  530.                "-c" command)
  531.          (exchange-point-and-mark t))
  532.     ;; Preserve the match data in case called from a program.
  533.     (let ((data (match-data)))
  534.       (unwind-protect
  535.       (if (string-match "[ \t]*&[ \t]*$" command)
  536.           ;; Command ending with ampersand means asynchronous.
  537.           (progn
  538.          (require 'background) ; whizzy comint background code
  539.          (background (substring command 0 (match-beginning 0))))
  540.         (shell-command-on-region (point) (point) command nil))
  541.     (store-match-data data)))))
  542.  
  543. (defun shell-command-on-region (start end command &optional flag interactive)
  544.   "Execute string COMMAND in inferior shell with region as input.
  545. Normally display output (if any) in temp buffer `*Shell Command Output*';
  546. Prefix arg means replace the region with it.
  547. Noninteractive args are START, END, COMMAND, FLAG.
  548. Noninteractively FLAG means insert output in place of text from START to END,
  549. and put point at the end, but don't alter the mark.
  550.  
  551. If the output is one line, it is displayed in the echo area,
  552. but it is nonetheless available in buffer `*Shell Command Output*'
  553. even though that buffer is not automatically displayed.  If there is no output
  554. or output is inserted in the current buffer then `*Shell Command Output*' is
  555. deleted." 
  556.   (interactive (list (min (point) (mark)) (max (point) (mark))
  557.              (read-shell-command "Shell command on region: "
  558.                      last-shell-command-on-region)
  559.              current-prefix-arg
  560.              (prefix-numeric-value current-prefix-arg)))
  561.   (if flag
  562.       ;; Replace specified region with output from command.
  563.       (let ((swap (and interactive (< (point) (mark)))))
  564.     ;; Don't muck with mark
  565.     ;; unless called interactively.
  566.     (and interactive (push-mark))
  567.     (call-process-region start end shell-file-name t t nil
  568.                  "-c" command)
  569.     (if (get-buffer "*Shell Command Output*")
  570.         (kill-buffer "*Shell Command Output*"))
  571.     (and interactive swap (exchange-point-and-mark t)))
  572.     (let ((buffer (get-buffer-create "*Shell Command Output*")))
  573.       (save-excursion
  574.     (set-buffer buffer)
  575.     (erase-buffer))
  576.       (if (eq buffer (current-buffer))
  577.       (setq start 1 end 1))
  578.       (call-process-region start end shell-file-name
  579.                nil buffer nil
  580.                "-c" command)
  581.       (let ((lines (save-excursion
  582.              (set-buffer buffer)
  583.              (if (= (buffer-size) 0)
  584.              0
  585.                (count-lines (point-min) (point-max))))))
  586.     (cond ((= lines 0)
  587.            (message "(Shell command completed with no output)")
  588.            (kill-buffer "*Shell Command Output*"))
  589.           ((= lines 1)
  590.            (message "%s"
  591.             (save-excursion
  592.               (set-buffer buffer)
  593.               (goto-char (point-min))
  594.               (buffer-substring (point)
  595.                         (progn (end-of-line) (point))))))
  596.           (t 
  597.            (set-window-start (display-buffer buffer) 1)))))))
  598.  
  599. (defun universal-argument ()
  600.   "Begin a numeric argument for the following command.
  601. Digits or minus sign following this command make up the numeric argument.
  602. If no digits or minus sign follow, this command by itself provides 4 as argument.
  603. Used more than once, this command multiplies the argument by 4 each time."
  604.   (interactive nil)
  605.   (let* ((c-u 4)
  606.      (start-char last-command-event)
  607.      (event (allocate-event)))
  608.     (while (equal start-char (next-command-event event))
  609.       (setq c-u (* 4 c-u)))
  610.     (prefix-arg-internal event c-u nil)))
  611.  
  612. (defun prefix-arg-internal (event c-u value)
  613.   (setq zmacs-region-stays t)
  614.   (let ((sign 1)
  615.     char)
  616.     (if (key-press-event-p event) (setq char (event-key event)))
  617.     (if (and (numberp value) (< value 0))
  618.     (setq sign -1 value (- value)))
  619.     (if (eq value '-)
  620.     (setq sign -1 value nil))
  621.     (while (eq ?- char)
  622.       (setq sign (- sign) c-u nil)
  623.       (next-command-event event)
  624.       (if (key-press-event-p event) (setq char (event-key event))))
  625.     (while (and (numberp char) (>= char ?0) (<= char ?9))
  626.       (setq value (+ (* (if (numberp value) value 0) 10) (- char ?0)) c-u nil)
  627.       (next-command-event event)
  628.       (if (key-press-event-p event) (setq char (event-key event))))
  629.     (setq prefix-arg
  630.       (cond (c-u (list c-u))
  631.         ((numberp value) (* value sign))
  632.         ((= sign -1) '-)))
  633.     (setq unread-command-event event)))
  634.  
  635. (defun digit-argument (arg)
  636.   "Part of the numeric argument for the next command."
  637.   (interactive "P")
  638.   (prefix-arg-internal last-command-event nil arg))
  639.  
  640. (defun negative-argument (arg)
  641.   "Begin a negative numeric argument for the next command."
  642.   (interactive "P")
  643.   (prefix-arg-internal (character-to-event ?- (allocate-event)) nil arg))
  644.  
  645. (defun forward-to-indentation (arg)
  646.   "Move forward ARG lines and position at first nonblank character."
  647.   (interactive "p")
  648.   (forward-line arg)
  649.   (skip-chars-forward " \t"))
  650.  
  651. (defun backward-to-indentation (arg)
  652.   "Move backward ARG lines and position at first nonblank character."
  653.   (interactive "p")
  654.   (forward-line (- arg))
  655.   (skip-chars-forward " \t"))
  656.  
  657. (defun kill-line (&optional arg)
  658.   "Kill the rest of the current line; if no nonblanks there, kill thru newline.
  659. With prefix argument, kill that many lines from point.
  660. Negative arguments kill lines backward.
  661.  
  662. When calling from a program, nil means \"no arg\",
  663. a number counts as a prefix arg."
  664.   (interactive "*P")
  665.   (kill-region (point)
  666.            (progn
  667.          (if arg
  668.              (forward-line (prefix-numeric-value arg))
  669.            (if (eobp)
  670.                (signal 'end-of-buffer nil))
  671.            (if (looking-at "[ \t]*$")
  672.                (forward-line 1)
  673.              (end-of-line)))
  674.          (point))))
  675.  
  676. ;;;; The kill ring
  677.  
  678. (defvar kill-ring nil
  679.   "List of killed text sequences.")
  680.  
  681. (defconst kill-ring-max 30
  682.   "*Maximum length of kill ring before oldest elements are thrown away.")
  683.  
  684. (defvar kill-ring-yank-pointer nil
  685.   "The tail of the kill ring whose car is the last thing yanked.")
  686.  
  687. (defun kill-append (string before-p)
  688.   (setcar kill-ring
  689.       (if before-p
  690.           (concat string (car kill-ring))
  691.           (concat (car kill-ring) string))))
  692.  
  693. (defun kill-region (beg end &optional verbose)
  694.   "Kill between point and mark.
  695. The text is deleted but saved in the kill ring.
  696. The command \\[yank] can retrieve it from there.
  697. \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
  698.  
  699. This is the primitive for programs to kill text (as opposed to deleting it).
  700. Supply two arguments, character numbers indicating the stretch of text
  701.  to be killed.
  702. Any command that calls this function is a \"kill command\".
  703. If the previous command was also a kill command,
  704. the text killed this time appends to the text killed last time
  705. to make one entry in the kill ring."
  706.   (interactive "*r\np")
  707. ;  (interactive
  708. ;   (let ((region-hack (and zmacs-regions (eq last-command 'yank))))
  709. ;     ;; This lets "^Y^W" work.  I think this is dumb, but zwei did it.
  710. ;     (if region-hack (zmacs-activate-region))
  711. ;     (prog1
  712. ;     (list (point) (mark) current-prefix-arg)
  713. ;       (if region-hack (zmacs-deactivate-region)))))
  714.   (or (and beg end) (error (if zmacs-regions
  715.                    "The region is not active now"
  716.                  "The mark is not set now")))
  717.   (if verbose (message "Killing %d characters"
  718.                (- (max beg end) (min beg end))))
  719.   (copy-region-as-kill beg end)
  720.   (delete-region beg end))
  721.  
  722. (defvar kill-hooks nil
  723.   "If non-nil, this should be a function or functions of one argument which
  724. are called with the string most recently added to the kill ring.  You can use
  725. this to, for example, make the most recent kill become the X Clipboard
  726. selection.")
  727.  
  728. (defun copy-region-as-kill (beg end)
  729.   "Save the region as if killed, but don't kill it."
  730.   (interactive "r")
  731.   (if (eq last-command 'kill-region)
  732.       (kill-append (buffer-substring beg end) (< end beg))
  733.     (setq kill-ring (cons (buffer-substring beg end) kill-ring))
  734.     (if (> (length kill-ring) kill-ring-max)
  735.     (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
  736.   (setq kill-ring-yank-pointer kill-ring) ; do now, as kill-hooks may error
  737.   (let ((rest kill-hooks))
  738.     (if (or (not (consp rest)) (eq (car-safe rest) 'lambda))
  739.     (setq rest (list rest)))
  740.     (while rest
  741.       (funcall (car rest) (car kill-ring))
  742.       (setq rest (cdr rest))))
  743.   (setq this-command 'kill-region)
  744.   nil)
  745.  
  746. (defun kill-ring-save (beg end)
  747.   "Save the region as if killed, but don't kill it."
  748.   (interactive "r")
  749.   (copy-region-as-kill beg end) ; copy before delay, for xclipboard's benefit
  750.   (save-excursion
  751.     (goto-char (if (= (point) end) beg end))
  752.     (if (pos-visible-in-window-p (point))
  753.     (sit-for 0))))
  754.  
  755. (defun append-next-kill ()
  756.   "Cause following command, if kill, to append to previous kill."
  757.   (interactive)
  758.   (if (interactive-p)
  759.       (progn
  760.     (setq this-command 'kill-region)
  761.     (message "If the next command is a kill, it will append"))
  762.     (setq last-command 'kill-region))
  763.   (setq zmacs-region-stays t))
  764.  
  765. (defun rotate-yank-pointer (arg)
  766.   "Rotate the yanking point in the kill ring."
  767.   (interactive "p")
  768.   (let ((length (length kill-ring)))
  769.     (if (zerop length)
  770.     (error "Kill ring is empty")
  771.       (setq kill-ring-yank-pointer
  772.         (nthcdr (% (+ arg (- length (length kill-ring-yank-pointer)))
  773.                length)
  774.             kill-ring)))))
  775.  
  776. (defun yank-pop (arg)
  777.   "Replace just-yanked stretch of killed-text with a different stretch.
  778. This command is allowed only immediately after a  yank  or a  yank-pop.
  779. At such a time, the region contains a stretch of reinserted
  780. previously-killed text.  yank-pop  deletes that text and inserts in its
  781. place a different stretch of killed text.
  782.  
  783. With no argument, the previous kill is inserted.
  784. With argument n, the n'th previous kill is inserted.
  785. If n is negative, this is a more recent kill.
  786.  
  787. The sequence of kills wraps around, so that after the oldest one
  788. comes the newest one."
  789.   (interactive "*p")
  790.   (if (not (eq last-command 'yank))
  791.       (error "Previous command was not a yank"))
  792.   (setq this-command 'yank)
  793.   (let ((before (< (point) (mark t))))
  794.     (delete-region (point) (mark t))
  795.     (rotate-yank-pointer arg)
  796.     (set-mark (point))
  797.     (insert (car kill-ring-yank-pointer))
  798.     (if before (exchange-point-and-mark t))))
  799.  
  800. (defun yank (&optional arg)
  801.   "Reinsert the last stretch of killed text.
  802. More precisely, reinsert the stretch of killed text most recently
  803. killed OR yanked.
  804. With just C-U as argument, same but put point in front (and mark at end).
  805. With argument n, reinsert the nth most recently killed stretch of killed
  806. text.
  807. See also the command \\[yank-pop]."
  808.   (interactive "*P")
  809.   (rotate-yank-pointer (if (listp arg) 0
  810.              (if (eq arg '-) -1
  811.                (1- arg))))
  812.   (push-mark (point))
  813.   (insert (car kill-ring-yank-pointer))
  814.   (if (consp arg)
  815.       (exchange-point-and-mark t)))
  816.  
  817. (defun insert-buffer (buffer)
  818.   "Insert after point the contents of BUFFER.
  819. Puts mark after the inserted text.
  820. BUFFER may be a buffer or a buffer name."
  821.   (interactive (list (read-buffer "Insert buffer: " (other-buffer) t)))
  822.   (or (bufferp buffer)
  823.       (setq buffer (get-buffer buffer)))
  824.   (let (start end newmark)
  825.     (save-excursion
  826.       (save-excursion
  827.     (set-buffer buffer)
  828.     (setq start (point-min) end (point-max)))
  829.       (insert-buffer-substring buffer start end)
  830.       (setq newmark (point)))
  831.     (push-mark newmark)))
  832.  
  833. (defun append-to-buffer (buffer start end)
  834.   "Append to specified buffer the text of the region.
  835. It is inserted into that buffer before its point.
  836.  
  837. When calling from a program, give three arguments:
  838. a buffer or the name of one, and two character numbers
  839. specifying the portion of the current buffer to be copied."
  840.   (interactive "BAppend to buffer: \nr")
  841.   (let ((oldbuf (current-buffer)))
  842.     (save-excursion
  843.       (set-buffer (get-buffer-create buffer))
  844.       (insert-buffer-substring oldbuf start end))))
  845.  
  846. (defun prepend-to-buffer (buffer start end)
  847.   "Prepend to specified buffer the text of the region.
  848. It is inserted into that buffer after its point.
  849.  
  850. When calling from a program, give three arguments:
  851. a buffer or the name of one, and two character numbers
  852. specifying the portion of the current buffer to be copied."
  853.   (interactive "BPrepend to buffer: \nr")
  854.   (let ((oldbuf (current-buffer)))
  855.     (save-excursion
  856.       (set-buffer (get-buffer-create buffer))
  857.       (save-excursion
  858.     (insert-buffer-substring oldbuf start end)))))
  859.  
  860. (defun copy-to-buffer (buffer start end)
  861.   "Copy to specified buffer the text of the region.
  862. It is inserted into that buffer, replacing existing text there.
  863.  
  864. When calling from a program, give three arguments:
  865. a buffer or the name of one, and two character numbers
  866. specifying the portion of the current buffer to be copied."
  867.   (interactive "BCopy to buffer: \nr")
  868.   (let ((oldbuf (current-buffer)))
  869.     (save-excursion
  870.       (set-buffer (get-buffer-create buffer))
  871.       (erase-buffer)
  872.       (save-excursion
  873.     (insert-buffer-substring oldbuf start end)))))
  874.  
  875. (defun mark (&optional inactive-p)
  876.   "Return this buffer's mark value as integer, or nil if no mark.
  877.  
  878. If `zmacs-regions' is true, then this returns nil unless the region is
  879. currently in the active (hilighted) state.  With an argument of t, this
  880. returns the mark (if there is one) regardless of the active-region state.
  881. You should *generally* not use the mark unless the region is active, if
  882. the user has expressed a preference for the active-region model.
  883.  
  884. If you are using this in an editing command, you are most likely making
  885. a mistake\; see the documentation of `set-mark'."
  886.   (let ((m (mark-marker inactive-p)))
  887.     (and m (marker-position m))))
  888.  
  889. (defun set-mark (pos)
  890.   "Set this buffer's mark to POS.  Don't use this function!
  891. That is to say, don't use this function unless you want
  892. the user to see that the mark has moved, and you want the previous
  893. mark position to be lost.
  894.  
  895. Normally, when a new mark is set, the old one should go on the stack.
  896. This is why most applications should use push-mark, not set-mark.
  897.  
  898. Novice emacs-lisp programmers often try to use the mark for the wrong
  899. purposes.  The mark saves a location for the user's convenience.
  900. Most editing commands should not alter the mark.
  901. To remember a location for internal use in the Lisp program,
  902. store it in a Lisp variable.  Example:
  903.  
  904.    (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
  905.  
  906.   (set-marker (mark-marker t) pos (current-buffer)))
  907.  
  908. (defvar mark-ring nil
  909.   "The list of saved former marks of the current buffer,
  910. most recent first.")
  911. (make-variable-buffer-local 'mark-ring)
  912.  
  913. (defconst mark-ring-max 16
  914.   "*Maximum size of mark ring.  Start discarding off end if gets this big.")
  915.  
  916. (defun set-mark-command (arg)
  917.   "Set mark at where point is, or jump to mark.
  918. With no prefix argument, set mark, and push previous mark on mark ring.
  919. With argument, jump to mark, and pop into mark off the mark ring.
  920.  
  921. Novice emacs-lisp programmers often try to use the mark for the wrong
  922. purposes.  See the documentation of `set-mark' for more information."
  923.   (interactive "P")
  924.   (if (null arg)
  925.       (progn
  926.     (push-mark)
  927.     (zmacs-activate-region))
  928.     (if (null (mark t))
  929.     (error "No mark set in this buffer")
  930.       (goto-char (mark t))
  931.       (pop-mark))))
  932.  
  933. (defun push-mark (&optional location nomsg)
  934.   "Set mark at LOCATION (point, by default) and push old mark on mark ring.
  935. Displays \"Mark set\" unless the optional second arg NOMSG is non-nil.
  936.  
  937. Novice emacs-lisp programmers often try to use the mark for the wrong
  938. purposes.  See the documentation of `set-mark' for more information."
  939.   (if (null (mark t))
  940.       nil
  941.     (setq mark-ring (cons (copy-marker (mark-marker t)) mark-ring))
  942.     (if (> (length mark-ring) mark-ring-max)
  943.     (progn
  944.       (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
  945.       (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
  946.   (set-mark (or location (point)))
  947.   (or nomsg executing-macro (> (minibuffer-depth) 0)
  948.       (message "Mark set"))
  949.   nil)
  950.  
  951. (defun pop-mark ()
  952.   "Pop off mark ring into the buffer's actual mark.
  953. Does not set point.  Does nothing if mark ring is empty."
  954.   (if mark-ring
  955.       (progn
  956.     (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker t)))))
  957.     (set-mark (car mark-ring))  ; do not activate region
  958.     (move-marker (car mark-ring) nil)
  959.     (if (null (mark t)) (ding))  ; how can this happen?
  960.     (setq mark-ring (cdr mark-ring)))))
  961.  
  962. (fset 'exchange-dot-and-mark 'exchange-point-and-mark)
  963. (defun exchange-point-and-mark (&optional dont-activate-region)
  964.   "Put the mark where point is now, and point where the mark is now."
  965.   (interactive nil)
  966.   (let ((omark (mark t)))
  967.     (if (null omark)
  968.     (error "No mark set in this buffer"))
  969.     (set-mark (point))
  970.     (goto-char omark)
  971.     (or dont-activate-region (zmacs-activate-region))
  972.     nil))
  973.  
  974. (defvar next-line-extends-end-of-buffer t
  975.   "*If non-nil, when \\[next-line] is invoked on the last line of a buffer,
  976. a newline character is inserted to create a new line.
  977. If nil, \\[next-line] signals an `end-of-buffer' in that situation.")
  978.  
  979. (defun next-line (arg)
  980.   "Move cursor vertically down ARG lines.
  981. If there is no character in the target line exactly under the current column,
  982. the cursor is positioned after the character in that line which spans this
  983. column, or at the end of the line if it is not long enough.
  984.  
  985. If the variable `next-line-extends-end-of-buffer' is true and there is no line
  986. in the buffer after this one, a newline character is inserted to create
  987. a line and the cursor moves to that line.
  988.  
  989. The command \\[set-goal-column] can be used to create
  990. a semipermanent goal column to which this command always moves.
  991. Then it does not try to move vertically.  This goal column is stored
  992. in `goal-column', which is nil when there is none.
  993.  
  994. If you are thinking of using this in a Lisp program, consider
  995. using `forward-line' instead.  It is usually easier to use
  996. and more reliable (no dependence on goal column, etc.)."
  997.   (interactive "p")
  998.   (if (= arg 1)
  999.       (let ((opoint (point)))
  1000.     (forward-line 1)
  1001.     (if (or (= opoint (point))
  1002.         (not (eq (preceding-char) ?\n)))
  1003.         (progn
  1004.           (if (not next-line-extends-end-of-buffer)
  1005.           (signal 'end-of-buffer nil))
  1006.           (insert ?\n)
  1007.           (setq zmacs-region-stays t))
  1008.       (goto-char opoint)
  1009.       (line-move arg)))
  1010.     (line-move arg))
  1011.   nil)
  1012.  
  1013. (defun previous-line (arg)
  1014.   "Move cursor vertically up ARG lines.
  1015. If there is no character in the target line exactly over the current column,
  1016. the cursor is positioned after the character in that line which spans this
  1017. column, or at the end of the line if it is not long enough.
  1018.  
  1019. The command \\[set-goal-column] can be used to create
  1020. a semipermanent goal column to which this command always moves.
  1021. Then it does not try to move vertically.
  1022.  
  1023. If you are thinking of using this in a Lisp program, consider using
  1024. `forward-line' with negative argument instead..  It is usually easier
  1025. to use and more reliable (no dependence on goal column, etc.)."
  1026.   (interactive "p")
  1027.   (line-move (- arg))
  1028.   nil)
  1029.  
  1030. (defconst track-eol nil
  1031.   "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
  1032. This means moving to the end of each line moved onto.")
  1033.  
  1034. (make-variable-buffer-local
  1035.  (defvar goal-column nil
  1036.    "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."))
  1037.  
  1038. (defvar temporary-goal-column 0
  1039.   "Current goal column for vertical motion.
  1040. It is the column where point was at the start of current run of vertical motion commands.")
  1041.  
  1042. (defun line-move (arg)
  1043.   (if (not (or (eq last-command 'next-line)
  1044.            (eq last-command 'previous-line)))
  1045.       (setq temporary-goal-column
  1046.         (if (and track-eol (eolp))
  1047.         9999
  1048.           (current-column))))
  1049.   (if (not (integerp selective-display))
  1050.       (forward-line arg)
  1051.     ;; Move by arg lines, but ignore invisible ones.
  1052.     (while (> arg 0)
  1053.       (vertical-motion 1)
  1054.       (forward-char -1)
  1055.       (forward-line 1)
  1056.       (setq arg (1- arg)))
  1057.     (while (< arg 0)
  1058.       (vertical-motion -1)
  1059.       (beginning-of-line)
  1060.       (setq arg (1+ arg))))
  1061.   (move-to-column (or goal-column temporary-goal-column))
  1062.   (setq zmacs-region-stays t)
  1063.   nil)
  1064.  
  1065.  
  1066. (defun set-goal-column (arg)
  1067.   "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
  1068. Those commands will move to this position in the line moved to
  1069. rather than trying to keep the same horizontal position.
  1070. With a non-nil argument, clears out the goal column
  1071. so that \\[next-line] and \\[previous-line] resume vertical motion."
  1072.   (interactive "P")
  1073.   (if arg
  1074.       (progn
  1075.         (setq goal-column nil)
  1076.         (message "No goal column"))
  1077.     (setq goal-column (current-column))
  1078.     (message (substitute-command-keys
  1079.           "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
  1080.          goal-column))
  1081.   (setq zmacs-region-stays t)
  1082.   nil)
  1083.  
  1084. (defun transpose-chars (arg)
  1085.   "Interchange characters around point, moving forward one character.
  1086. With prefix arg ARG, effect is to take character before point
  1087. and drag it forward past ARG other characters (backward if ARG negative).
  1088. If no argument and at end of line, the previous two chars are exchanged."
  1089.   (interactive "*P")
  1090.   (and (null arg) (eolp) (forward-char -1))
  1091.   (transpose-subr 'forward-char (prefix-numeric-value arg)))
  1092.  
  1093. (defun transpose-words (arg)
  1094.   "Interchange words around point, leaving point at end of them.
  1095. With prefix arg ARG, effect is to take word before or around point
  1096. and drag it forward past ARG other words (backward if ARG negative).
  1097. If ARG is zero, the words around or after point and around or after mark
  1098. are interchanged."
  1099.   (interactive "*p")
  1100.   (transpose-subr 'forward-word arg))
  1101.  
  1102. (defun transpose-sexps (arg)
  1103.   "Like \\[transpose-words] but applies to sexps.
  1104. Does not work on a sexp that point is in the middle of
  1105. if it is a list or string."
  1106.   (interactive "*p")
  1107.   (transpose-subr 'forward-sexp arg))
  1108.  
  1109. (defun transpose-lines (arg)
  1110.   "Exchange current line and previous line, leaving point after both.
  1111. With argument ARG, takes previous line and moves it past ARG lines.
  1112. With argument 0, interchanges line point is in with line mark is in."
  1113.   (interactive "*p")
  1114.   (transpose-subr (function
  1115.            (lambda (arg)
  1116.              (if (= arg 1)
  1117.              (progn
  1118.                ;; Move forward over a line,
  1119.                ;; but create a newline if none exists yet.
  1120.                (end-of-line)
  1121.                (if (eobp)
  1122.                    (newline)
  1123.                  (forward-char 1)))
  1124.                (forward-line arg))))
  1125.           arg))
  1126.  
  1127. (defun transpose-subr (mover arg)
  1128.   (let (start1 end1 start2 end2)
  1129.     (if (= arg 0)
  1130.     (progn
  1131.       (save-excursion
  1132.         (funcall mover 1)
  1133.         (setq end2 (point))
  1134.         (funcall mover -1)
  1135.         (setq start2 (point))
  1136.         (goto-char (mark t))
  1137.         (funcall mover 1)
  1138.         (setq end1 (point))
  1139.         (funcall mover -1)
  1140.         (setq start1 (point))
  1141.         (transpose-subr-1))
  1142.       (exchange-point-and-mark t)))
  1143.     (while (> arg 0)
  1144.       (funcall mover -1)
  1145.       (setq start1 (point))
  1146.       (funcall mover 1)
  1147.       (setq end1 (point))
  1148.       (funcall mover 1)
  1149.       (setq end2 (point))
  1150.       (funcall mover -1)
  1151.       (setq start2 (point))
  1152.       (transpose-subr-1)
  1153.       (goto-char end2)
  1154.       (setq arg (1- arg)))
  1155.     (while (< arg 0)
  1156.       (funcall mover -1)
  1157.       (setq start2 (point))
  1158.       (funcall mover -1)
  1159.       (setq start1 (point))
  1160.       (funcall mover 1)
  1161.       (setq end1 (point))
  1162.       (funcall mover 1)
  1163.       (setq end2 (point))
  1164.       (transpose-subr-1)
  1165.       (setq arg (1+ arg)))))
  1166.  
  1167. (defun transpose-subr-1 ()
  1168.   (if (> (min end1 end2) (max start1 start2))
  1169.       (error "Don't have two things to transpose"))
  1170.   (let ((word1 (buffer-substring start1 end1))
  1171.     (word2 (buffer-substring start2 end2)))
  1172.     (delete-region start2 end2)
  1173.     (goto-char start2)
  1174.     (insert word1)
  1175.     (goto-char (if (< start1 start2) start1
  1176.          (+ start1 (- (length word1) (length word2)))))
  1177.     (delete-char (length word1))
  1178.     (insert word2)))
  1179.  
  1180. (defconst comment-column 32
  1181.   "*Column to indent right-margin comments to.
  1182. Setting this variable automatically makes it local to the current buffer.")
  1183. (make-variable-buffer-local 'comment-column)
  1184.  
  1185. (defconst comment-start nil
  1186.   "*String to insert to start a new comment, or nil if no comment syntax defined.")
  1187.  
  1188. (defconst comment-start-skip nil
  1189.   "*Regexp to match the start of a comment plus everything up to its body.
  1190. If there are any \\(...\\) pairs, the comment delimiter text is held to begin
  1191. at the place matched by the close of the first pair.")
  1192.  
  1193. (defconst comment-end ""
  1194.   "*String to insert to end a new comment.
  1195. Should be an empty string if comments are terminated by end-of-line.")
  1196.  
  1197. (defconst comment-indent-hook
  1198.   '(lambda () comment-column)
  1199.   "Function to compute desired indentation for a comment.
  1200. This function is called with no args with point at the beginning of
  1201. the comment's starting delimiter.")
  1202.  
  1203. (defun indent-for-comment ()
  1204.   "Indent this line's comment to comment column, or insert an empty comment."
  1205.   (interactive "*")
  1206.   (beginning-of-line 1)
  1207.   (if (null comment-start)
  1208.       (error "No comment syntax defined")
  1209.     (let* ((eolpos (save-excursion (end-of-line) (point)))
  1210.        cpos indent begpos)
  1211.       (if (re-search-forward comment-start-skip eolpos 'move)
  1212.       (progn (setq cpos (point-marker))
  1213.          ;; Find the start of the comment delimiter.
  1214.          ;; If there were paren-pairs in comment-start-skip,
  1215.          ;; position at the end of the first pair.
  1216.          (if (match-end 1)
  1217.              (goto-char (match-end 1))
  1218.            ;; If comment-start-skip matched a string with internal
  1219.            ;; whitespace (not final whitespace) then the delimiter
  1220.            ;; start at the end of that whitespace.
  1221.            ;; Otherwise, it starts at the beginning of what was matched.
  1222.            (skip-chars-backward " \t" (match-beginning 0))
  1223.            (skip-chars-backward "^ \t" (match-beginning 0)))))
  1224.       (setq begpos (point))
  1225.       ;; Compute desired indent.
  1226.       (if (= (current-column)
  1227.          (setq indent (funcall comment-indent-hook)))
  1228.       (goto-char begpos)
  1229.     ;; If that's different from current, change it.
  1230.     (skip-chars-backward " \t")
  1231.     (delete-region (point) begpos)
  1232.     (indent-to indent))
  1233.       ;; An existing comment?
  1234.       (if cpos 
  1235.       (progn (goto-char cpos)
  1236.          (set-marker cpos nil))
  1237.     ;; No, insert one.
  1238.     (insert comment-start)
  1239.     (save-excursion
  1240.       (insert comment-end))))))
  1241.  
  1242. (defun set-comment-column (arg)
  1243.   "Set the comment column based on point.
  1244. With no arg, set the comment column to the current column.
  1245. With just minus as arg, kill any comment on this line.
  1246. With any other arg, set comment column to indentation of the previous comment
  1247.  and then align or create a comment on this line at that column."
  1248.   (interactive "P")
  1249.   (if (eq arg '-)
  1250.       (kill-comment nil)
  1251.     (if arg
  1252.     (progn
  1253.       (save-excursion
  1254.         (beginning-of-line)
  1255.         (re-search-backward comment-start-skip)
  1256.         (beginning-of-line)
  1257.         (re-search-forward comment-start-skip)
  1258.         (goto-char (match-beginning 0))
  1259.         (setq comment-column (current-column))
  1260.         (message "Comment column set to %d" comment-column))
  1261.       (indent-for-comment))
  1262.       (setq comment-column (current-column))
  1263.       (message "Comment column set to %d" comment-column))))
  1264.  
  1265. (defun kill-comment (arg)
  1266.   "Kill the comment on this line, if any.
  1267. With argument, kill comments on that many lines starting with this one."
  1268.   ;; this function loses in a lot of situations.  it incorrectly recognises
  1269.   ;; comment delimiters sometimes (ergo, inside a string), doesn't work
  1270.   ;; with multi-line comments, can kill extra whitespace if comment wasn't
  1271.   ;; through end-of-line, et cetera.
  1272.   (interactive "*P")
  1273.   (or comment-start-skip (error "No comment syntax defined"))
  1274.   (let ((count (prefix-numeric-value arg)) endc)
  1275.     (while (> count 0)
  1276.       (save-excursion
  1277.     (end-of-line)
  1278.     (setq endc (point))
  1279.     (beginning-of-line)
  1280.     (and (string< "" comment-end)
  1281.          (setq endc
  1282.            (progn
  1283.              (re-search-forward (regexp-quote comment-end) endc 'move)
  1284.              (skip-chars-forward " \t")
  1285.              (point))))
  1286.     (beginning-of-line)
  1287.     (if (re-search-forward comment-start-skip endc t)
  1288.         (progn
  1289.           (goto-char (match-beginning 0))
  1290.           (skip-chars-backward " \t")
  1291.           (kill-region (point) endc)
  1292.           ;; to catch comments a line beginnings
  1293.           (indent-according-to-mode))))
  1294.       (if arg (forward-line 1))
  1295.       (setq count (1- count)))))
  1296.  
  1297. (defun backward-word (arg)
  1298.   "Move backward until encountering the end of a word.
  1299. With argument, do this that many times.
  1300. In programs, it is faster to call forward-word with negative arg."
  1301.   (interactive "p")
  1302.   (forward-word (- arg)))
  1303.  
  1304. (defun mark-word (arg)
  1305.   "Set mark arg words away from point."
  1306.   (interactive "p")
  1307.   (push-mark
  1308.     (save-excursion
  1309.       (forward-word arg)
  1310.       (point)))
  1311.   (zmacs-activate-region))
  1312.  
  1313. (defun kill-word (arg)
  1314.   "Kill characters forward until encountering the end of a word.
  1315. With argument, do this that many times."
  1316.   (interactive "*p")
  1317.   (kill-region (point) (progn (forward-word arg) (point))))
  1318.  
  1319. (defun backward-kill-word (arg)
  1320.   "Kill characters backward until encountering the end of a word.
  1321. With argument, do this that many times."
  1322.   (interactive "*p")
  1323.   (kill-word (- arg)))
  1324.  
  1325. (defconst fill-prefix nil
  1326.   "*String for filling to insert at front of new line, or nil for none.
  1327. Setting this variable automatically makes it local to the current buffer.")
  1328. (make-variable-buffer-local 'fill-prefix)
  1329.  
  1330. (defconst auto-fill-inhibit-regexp nil
  1331.   "*Regexp to match lines which should not be auto-filled.")
  1332.  
  1333. (defun do-auto-fill ()
  1334.   (let (give-up)
  1335.     (or (and auto-fill-inhibit-regexp
  1336.          (save-excursion (beginning-of-line)
  1337.                  (looking-at auto-fill-inhibit-regexp)))
  1338.     (while (and (not give-up) (> (current-column) fill-column))
  1339.       (let ((fill-point
  1340.          (let ((opoint (point)))
  1341.            (save-excursion
  1342.              (move-to-column (1+ fill-column))
  1343.              (skip-chars-backward "^ \t\n")
  1344.              (if (bolp)
  1345.              (re-search-forward "[ \t]" opoint t))
  1346.              (skip-chars-backward " \t")
  1347.              (point)))))
  1348.         ;; If there is a space on the line before fill-point,
  1349.         ;; and nonspaces precede it, break the line there.
  1350.         (if (save-excursion
  1351.           (goto-char fill-point)
  1352.           (not (bolp)))
  1353.         ;; If point is at the fill-point, do not `save-excursion'.
  1354.         ;; Otherwise, if a comment prefix or fill-prefix is inserted,
  1355.         ;; point will end up before it rather than after it.
  1356.         (if (save-excursion
  1357.               (skip-chars-backward " \t")
  1358.               (= (point) fill-point))
  1359.             (indent-new-comment-line)
  1360.           (save-excursion
  1361.             (goto-char fill-point)
  1362.             (indent-new-comment-line)))
  1363.           ;; No place to break => stop trying.
  1364.           (setq give-up t)))))))
  1365.  
  1366. (defconst comment-multi-line nil
  1367.   "*Non-nil means \\[indent-new-comment-line] should continue same comment
  1368. on new line, with no new terminator or starter.")
  1369.  
  1370. (defun indent-new-comment-line ()
  1371.   "Break line at point and indent, continuing comment if presently within one.
  1372. The body of the continued comment is indented under the previous comment line."
  1373.   (interactive "*")
  1374.   (let (comcol comstart)
  1375.     (skip-chars-backward " \t")
  1376.     (delete-region (point)
  1377.            (progn (skip-chars-forward " \t")
  1378.               (point)))
  1379.     (insert ?\n)
  1380.     (save-excursion
  1381.       (if (and comment-start-skip
  1382.            (let ((opoint (point)))
  1383.          (forward-line -1)
  1384.          (re-search-forward comment-start-skip opoint t)))
  1385.       ;; The old line is a comment.
  1386.       ;; Set WIN to the pos of the comment-start.
  1387.       ;; But if the comment is empty, look at preceding lines
  1388.       ;; to find one that has a nonempty comment.
  1389.       (let ((win (match-beginning 0)))
  1390.         (while (and (eolp) (not (bobp))
  1391.             (let (opoint)
  1392.               (beginning-of-line)
  1393.               (setq opoint (point))
  1394.               (forward-line -1)
  1395.               (re-search-forward comment-start-skip opoint t)))
  1396.           (setq win (match-beginning 0)))
  1397.         ;; Indent this line like what we found.
  1398.         (goto-char win)
  1399.         (setq comcol (current-column))
  1400.         (setq comstart (buffer-substring (point) (match-end 0))))))
  1401.     (if comcol
  1402.     (let ((comment-column comcol)
  1403.           (comment-start comstart)
  1404.           (comment-end comment-end))
  1405.       (and comment-end (not (equal comment-end ""))
  1406.            (if (not comment-multi-line)
  1407.            (progn
  1408.              (forward-char -1)
  1409.              (insert comment-end)
  1410.              (forward-char 1))
  1411.          (setq comment-column (+ comment-column (length comment-start))
  1412.                comment-start "")))
  1413.       (if (not (eolp))
  1414.           (setq comment-end ""))
  1415.       (insert ?\n)
  1416.       (forward-char -1)
  1417.       (indent-for-comment)
  1418.       (save-excursion
  1419.         ;; Make sure we delete the newline inserted above.
  1420.         (end-of-line)
  1421.         (delete-char 1)))
  1422.       (if fill-prefix
  1423.       (insert fill-prefix)
  1424.     (indent-according-to-mode)))))
  1425.  
  1426. (defun auto-fill-mode (&optional arg)
  1427.   "Toggle auto-fill mode.
  1428. With arg, turn auto-fill mode on if and only if arg is positive.
  1429. In auto-fill mode, inserting a space at a column beyond  fill-column
  1430. automatically breaks the line at a previous space."
  1431.   (interactive "P")
  1432.   (prog1 (setq auto-fill-function
  1433.            (if (if (null arg)
  1434.                (not auto-fill-function)
  1435.                (> (prefix-numeric-value arg) 0))
  1436.            'do-auto-fill
  1437.            nil))
  1438.     ;; update mode-line
  1439.     (set-buffer-modified-p (buffer-modified-p))))
  1440.  
  1441. (defun turn-on-auto-fill ()
  1442.   "Unconditionally turn on Auto Fill mode."
  1443.   (auto-fill-mode 1))
  1444.  
  1445. (defun set-fill-column (arg)
  1446.   "Set fill-column to current column, or to argument if given.
  1447. fill-column's value is separate for each buffer."
  1448.   (interactive "P")
  1449.   (setq fill-column (if (integerp arg) arg (current-column)))
  1450.   (setq zmacs-region-stays t)
  1451.   (message "fill-column set to %d" fill-column))
  1452.  
  1453. (defun set-selective-display (arg)
  1454.   "Set selective-display to ARG; clear it if no arg.
  1455. When selective-display is a number > 0,
  1456. lines whose indentation is >= selective-display are not displayed.
  1457. selective-display's value is separate for each buffer.
  1458.  
  1459. WARNING: selective-display does not currently work with Lucid Emacs.
  1460. This is a known bug that will be fixed eventually."
  1461.   (interactive "P")
  1462.   (if (eq selective-display t)
  1463.       (error "selective-display already in use for marked lines"))
  1464.   (setq selective-display
  1465.     (and arg (prefix-numeric-value arg)))
  1466.   (set-window-start (selected-window) (window-start (selected-window)))
  1467.   (princ "selective-display set to " t)
  1468.   (prin1 selective-display t)
  1469.   (princ "." t))
  1470.  
  1471. (defun overwrite-mode (arg)
  1472.   "Toggle overwrite mode.
  1473. With arg, turn overwrite mode on iff arg is positive.
  1474. In overwrite mode, printing characters typed in replace existing text
  1475. on a one-for-one basis, rather than pushing it to the right."
  1476.   (interactive "P")
  1477.   (setq overwrite-mode
  1478.     (if (null arg) (not overwrite-mode)
  1479.       (> (prefix-numeric-value arg) 0)))
  1480.   (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line.
  1481.  
  1482. (defvar blink-matching-paren t
  1483.   "*Non-nil means show matching open-paren when close-paren is inserted.")
  1484.  
  1485. (defconst blink-matching-paren-distance 4000
  1486.   "*If non-nil, is maximum distance to search for matching open-paren
  1487. when close-paren is inserted.")
  1488.  
  1489. (defun blink-matching-open ()
  1490.   "Move cursor momentarily to the beginning of the sexp before point."
  1491.   (and (> (point) (1+ (point-min)))
  1492.        (/= (char-syntax (char-after (- (point) 2))) ?\\ )
  1493.        blink-matching-paren
  1494.        (let* ((oldpos (point))
  1495.           (parse-sexp-ignore-comments t)
  1496.           (blinkpos)
  1497.           (mismatch))
  1498.      (save-excursion
  1499.        (save-restriction
  1500.          (if blink-matching-paren-distance
  1501.          (narrow-to-region (max (point-min)
  1502.                     (- (point) blink-matching-paren-distance))
  1503.                    oldpos))
  1504.          (condition-case ()
  1505.          (setq blinkpos (scan-sexps oldpos -1))
  1506.            (error nil)))
  1507.        (and blinkpos (/= (char-syntax (char-after blinkpos))
  1508.                  ?\$)
  1509.         (setq mismatch
  1510.               (/= (char-after (1- oldpos))
  1511.               (logand (lsh (aref (syntax-table)
  1512.                          (char-after blinkpos))
  1513.                        -8)
  1514.                   255))))
  1515.        (if mismatch (setq blinkpos nil))
  1516.        (if blinkpos
  1517.            (progn
  1518.         (goto-char blinkpos)
  1519.         (if (pos-visible-in-window-p)
  1520.             (sit-for 1)
  1521.           (goto-char blinkpos)
  1522.           (message
  1523.            "Matches %s"
  1524.            (if (save-excursion
  1525.              (skip-chars-backward " \t")
  1526.              (not (bolp)))
  1527.                (buffer-substring (progn (beginning-of-line) (point))
  1528.                      (1+ blinkpos))
  1529.              (buffer-substring blinkpos
  1530.                        (progn
  1531.                     (forward-char 1)
  1532.                     (skip-chars-forward "\n \t")
  1533.                     (end-of-line)
  1534.                     (point)))))))
  1535.          (cond (mismatch
  1536.             (message "Mismatched parentheses"))
  1537.            ((not blink-matching-paren-distance)
  1538.             (message "Unmatched parenthesis"))))))))
  1539.  
  1540. ;Turned off because it makes dbx bomb out.
  1541. (setq blink-paren-function 'blink-matching-open)
  1542.  
  1543. ; this is just something for the luser to see in a keymap -- this is not
  1544. ;  how quitting works normally!
  1545. (defun keyboard-quit ()
  1546.   "Signal a `quit' condition."
  1547.   (interactive)
  1548.   (signal 'quit nil))
  1549.  
  1550. ;;; right-thing-p?
  1551. ;(defun keyboard-quit ()
  1552. ;  "Signal a `quit' condition.
  1553. ;If this character is typed while lisp code is executing, it will be treated
  1554. ; as an interrupt.
  1555. ;If this character is typed at top-level, this simply beeps.
  1556. ;If `zmacs-regions' is true, and the zmacs region is active, then this
  1557. ; key deactivates the region without beeping or signalling."
  1558. ;  (interactive)
  1559. ;  (if (and zmacs-regions (zmacs-deactivate-region))
  1560. ;      ;; pseudo-zmacs compatibility: don't beep if this ^G is simply
  1561. ;      ;; deactivating the region.  If it is inactive, beep.
  1562. ;      nil
  1563. ;    (signal 'quit nil)))
  1564.  
  1565.  
  1566. (defun set-variable (var val)
  1567.   "Set VARIABLE to VALUE.  VALUE is a Lisp object.
  1568. When using this interactively, supply a Lisp expression for VALUE.
  1569. If you want VALUE to be a string, you must surround it with doublequotes."
  1570.   (interactive
  1571.    (let* ((var (read-variable "Set variable: "))
  1572.       (minibuffer-help-form
  1573.        '(funcall myhelp))
  1574.       (myhelp
  1575.        (function
  1576.         (lambda ()
  1577.           (with-output-to-temp-buffer "*Help*"
  1578.         (prin1 var)
  1579.         (princ "\nDocumentation:\n")
  1580.         (princ (substring (documentation-property var 'variable-documentation)
  1581.                   1))
  1582.         (if (boundp var)
  1583.             (let ((print-length 20))
  1584.               (princ "\n\nCurrent value: ")
  1585.               (prin1 (symbol-value var))))
  1586.         nil)))))
  1587.      (list var
  1588.        (eval-minibuffer (format "Set %s to value: " var)))))
  1589.   (set var val))
  1590.