home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / emacs-15.0.3 / lisp / indent.el < prev    next >
Lisp/Scheme  |  1990-07-19  |  7KB  |  227 lines

  1. ;; Indentation commands for Emacs
  2. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is distributed in the hope that it will be useful,
  7. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  8. ;; accepts responsibility to anyone for the consequences of using it
  9. ;; or for whether it serves any particular purpose or works at all,
  10. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  11. ;; License for full details.
  12.  
  13. ;; Everyone is granted permission to copy, modify and redistribute
  14. ;; GNU Emacs, but only under the conditions described in the
  15. ;; GNU Emacs General Public License.   A copy of this license is
  16. ;; supposed to have been given to you along with GNU Emacs so you
  17. ;; can know your rights and responsibilities.  It should be in a
  18. ;; file named COPYING.  Among other things, the copyright notice
  19. ;; and this notice must be preserved on all copies.
  20.  
  21.  
  22. ;Now in loaddefs.el
  23. ;(defvar indent-line-function
  24. ;  'indent-to-left-margin
  25. ;  "Function to indent current line.")
  26.  
  27. (defun indent-according-to-mode ()
  28.   "Indent line in proper way for current major mode."
  29.   (interactive)
  30.   (funcall indent-line-function))
  31.  
  32. (defun indent-for-tab-command ()
  33.   "Indent line in proper way for current major mode."
  34.   (interactive)
  35.   (if (eq indent-line-function 'indent-to-left-margin)
  36.       (insert-tab)
  37.     (funcall indent-line-function)))
  38.  
  39. (defun insert-tab ()
  40.   (if abbrev-mode
  41.       (expand-abbrev))
  42.   (if indent-tabs-mode
  43.       (insert ?\t)
  44.     (indent-to (* tab-width (1+ (/ (current-column) tab-width))))))
  45.  
  46. (defun indent-rigidly (start end arg)
  47.   "Indent all lines starting in the region sideways by ARG columns.
  48. Called from a program, takes three arguments, START, END and ARG."
  49.   (interactive "r\np")
  50.   (save-excursion
  51.     (goto-char end)
  52.     (setq end (point-marker))
  53.     (goto-char start)
  54.     (or (bolp) (forward-line 1))
  55.     (while (< (point) end)
  56.       (let ((indent (current-indentation)))
  57.     (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  58.     (or (eolp)
  59.         (indent-to (max 0 (+ indent arg)) 0)))
  60.       (forward-line 1))
  61.     (move-marker end nil)))
  62.  
  63. ;; This is the default indent-line-function,
  64. ;; used in Fundamental Mode, Text Mode, etc.
  65. (defun indent-to-left-margin ()
  66.   (or (= (current-indentation) left-margin)
  67.       (let (epos)
  68.     (save-excursion
  69.      (beginning-of-line)
  70.      (delete-region (point)
  71.             (progn (skip-chars-forward " \t")
  72.                    (point)))
  73.      (indent-to left-margin)
  74.      (setq epos (point)))
  75.     (if (< (point) epos)
  76.         (goto-char epos)))))
  77.  
  78. (defvar indent-region-function nil
  79.   "Function which is short cut to indent each line in region with Tab.
  80. nil means really call Tab on each line.")
  81.  
  82. (defun indent-region (start end arg)
  83.   "Indent each nonblank line in the region.
  84. With no argument, indent each line with Tab.
  85. With argument COLUMN, indent each line to that column.
  86. Called from a program, takes three args: START, END and COLUMN."
  87.   (interactive "r\nP")
  88.   (if (null arg)
  89.       (if indent-region-function
  90.       (funcall indent-region-function start end)
  91.     (save-excursion
  92.      (goto-char end)
  93.      (setq end (point-marker))
  94.      (goto-char start)
  95.      (or (bolp) (forward-line 1))
  96.      (while (< (point) end)
  97.        (funcall indent-line-function)
  98.        (forward-line 1))
  99.      (move-marker end nil)))
  100.     (setq arg (prefix-numeric-value arg))
  101.   (save-excursion
  102.    (goto-char end)
  103.    (setq end (point-marker))
  104.    (goto-char start)
  105.    (or (bolp) (forward-line 1))
  106.    (while (< (point) end)
  107.      (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  108.      (or (eolp)
  109.      (indent-to arg 0))
  110.      (forward-line 1))
  111.    (move-marker end nil))))
  112.  
  113. (defun indent-relative-maybe ()
  114.   "Indent a new line like previous nonblank line."
  115.   (interactive)
  116.   (indent-relative t))
  117.  
  118. (defun indent-relative (&optional unindented-ok)
  119.   "Space out to under next indent point in previous nonblank line.
  120. An indent point is a non-whitespace character following whitespace.
  121. If the previous nonblank line has no indent points beyond
  122. the column point starts at,  tab-to-tab-stop  is done instead."
  123.   (interactive "P")
  124.   (if abbrev-mode (expand-abbrev))
  125.   (let ((start-column (current-column))
  126.     indent)
  127.     (save-excursion
  128.       (beginning-of-line)
  129.       (if (re-search-backward "^[^\n]" nil t)
  130.       (let ((end (save-excursion (forward-line 1) (point))))
  131.         (move-to-column start-column)
  132.         ;; Is start-column inside a tab on this line?
  133.         (if (> (current-column) start-column)
  134.         (backward-char 1))
  135.         (or (looking-at "[ \t]")
  136.         unindented-ok
  137.         (skip-chars-forward "^ \t" end))
  138.         (skip-chars-forward " \t" end)
  139.         (or (= (point) end) (setq indent (current-column))))))
  140.     (if indent
  141.     (let ((opoint (point-marker)))
  142.       (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  143.       (indent-to indent 0)
  144.       (if (> opoint (point))
  145.           (goto-char opoint))
  146.       (move-marker opoint nil))
  147.       (tab-to-tab-stop))))
  148.  
  149. (defvar tab-stop-list
  150.   '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
  151.   "*List of tab stop positions used by tab-to-tab-stops.")
  152.  
  153. (defvar edit-tab-stops-map nil "Keymap used in edit-tab-stops.")
  154. (if edit-tab-stops-map
  155.     nil
  156.   (setq edit-tab-stops-map (make-sparse-keymap))
  157.   (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes)
  158.   (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes))
  159.  
  160. (defvar edit-tab-stops-buffer nil
  161.   "Buffer whose tab stops are being edited--in case
  162. the variable tab-stop-list is local in that buffer.")
  163.  
  164. (defun edit-tab-stops ()
  165.   "Edit the tab stops used by tab-to-tab-stop.
  166. Creates a buffer *Tab Stops* containing text describing the tab stops.
  167. A colon indicates a column where there is a tab stop.
  168. You can add or remove colons and then do C-c C-c to make changes take effect."
  169.   (interactive)
  170.   (setq edit-tab-stops-buffer (current-buffer))
  171.   (switch-to-buffer (get-buffer-create "*Tab Stops*"))
  172.   (use-local-map edit-tab-stops-map)
  173.   (make-local-variable 'indent-tabs-mode)
  174.   (setq indent-tabs-mode nil)
  175.   (overwrite-mode 1)
  176.   (setq truncate-lines t)
  177.   (erase-buffer)
  178.   (let ((tabs tab-stop-list))
  179.     (while tabs
  180.       (indent-to (car tabs) 0)
  181.       (insert ?:)
  182.       (setq tabs (cdr tabs))))
  183.   (let ((count 0))
  184.     (insert ?\n)
  185.     (while (< count 8)
  186.       (insert (+ count ?0))
  187.     (insert "         ")
  188.       (setq count (1+ count)))
  189.     (insert ?\n)
  190.     (while (> count 0)
  191.       (insert "0123456789")
  192.       (setq count (1- count))))
  193.   (insert "\nTo install changes, type C-c C-c")
  194.   (goto-char (point-min)))
  195.  
  196. (defun edit-tab-stops-note-changes ()
  197.   "Put edited tab stops into effect."
  198.   (interactive)
  199.     (let (tabs)
  200.       (save-excursion
  201.     (goto-char 1)
  202.     (end-of-line)
  203.     (while (search-backward ":" nil t)
  204.       (setq tabs (cons (current-column) tabs))))
  205.       (bury-buffer (prog1 (current-buffer)
  206.               (switch-to-buffer edit-tab-stops-buffer)))
  207.       (setq tab-stop-list tabs))
  208.   (message "Tab stops installed"))
  209.  
  210. (defun tab-to-tab-stop ()
  211.   "Insert spaces or tabs to next defined tab-stop column.
  212. The variable tab-stop-list is a list of columns at which there are tab stops.
  213. Use \\[edit-tab-stops] to edit them interactively."
  214.   (interactive)
  215.   (if abbrev-mode (expand-abbrev))
  216.   (let ((tabs tab-stop-list))
  217.     (while (and tabs (>= (current-column) (car tabs)))
  218.       (setq tabs (cdr tabs)))
  219.     (if tabs
  220.     (indent-to (car tabs))
  221.       (insert ? ))))
  222.  
  223. (define-key global-map "\t" 'indent-for-tab-command)
  224. (define-key esc-map "\034" 'indent-region)
  225. (define-key ctl-x-map "\t" 'indent-rigidly)
  226. (define-key esc-map "i" 'tab-to-tab-stop)
  227.