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 / indent.el < prev    next >
Encoding:
Text File  |  1993-01-17  |  7.8 KB  |  244 lines

  1. ;; Indentation 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. ;Now in loaddefs.el
  22. ;(defvar indent-line-function
  23. ;  'indent-to-left-margin
  24. ;  "Function to indent current line.")
  25.  
  26. (defun indent-according-to-mode ()
  27.   "Indent line in proper way for current major mode."
  28.   (interactive)
  29.   (funcall indent-line-function))
  30.  
  31. (defun indent-for-tab-command ()
  32.   "Indent line in proper way for current major mode."
  33.   (interactive)
  34.   (if (eq indent-line-function 'indent-to-left-margin)
  35.       (insert-tab)
  36.     (funcall indent-line-function)))
  37.  
  38. (defun insert-tab ()
  39.   (if abbrev-mode
  40.       (expand-abbrev))
  41.   (if indent-tabs-mode
  42.       (insert ?\t)
  43.     (indent-to (* tab-width (1+ (/ (current-column) tab-width))))))
  44.  
  45. (defun indent-rigidly (start end arg)
  46.   "Indent all lines starting in the region sideways by ARG columns.
  47. Called from a program, takes three arguments, START, END and ARG."
  48.   (interactive "r\np")
  49.   (save-excursion
  50.     (goto-char end)
  51.     (setq end (point-marker))
  52.     (goto-char start)
  53.     (or (bolp) (forward-line 1))
  54.     (while (< (point) end)
  55.       (let ((indent (current-indentation)))
  56.     (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  57.     (or (eolp)
  58.         (indent-to (max 0 (+ indent arg)) 0)))
  59.       (forward-line 1))
  60.     (move-marker end nil)
  61.     (setq zmacs-region-stays 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. A value of nil means really perform 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. \(If there is a fill prefix, make each line start with the fill prefix.)
  86. With argument COLUMN, indent each line to that column.
  87. Called from a program, takes three args: START, END and COLUMN."
  88.   (interactive "r\nP")
  89.   (if (null arg)
  90.       (if fill-prefix
  91.       (save-excursion
  92.         (goto-char end)
  93.         (setq end (point-marker))
  94.         (goto-char start)
  95.         (let ((regexp (regexp-quote fill-prefix)))
  96.         (while (< (point) end)
  97.           (or (looking-at regexp)
  98.           (insert fill-prefix))
  99.           (forward-line 1))))
  100.     (if indent-region-function
  101.         (funcall indent-region-function start end)
  102.       (save-excursion
  103.       (goto-char end)
  104.       (setq end (point-marker))
  105.       (goto-char start)
  106.       (or (bolp) (forward-line 1))
  107.       (while (< (point) end)
  108.         (funcall indent-line-function)
  109.         (forward-line 1))
  110.       (move-marker end nil))))
  111.     (setq arg (prefix-numeric-value arg))
  112.     (save-excursion
  113.       (goto-char end)
  114.       (setq end (point-marker))
  115.       (goto-char start)
  116.       (or (bolp) (forward-line 1))
  117.       (while (< (point) end)
  118.     (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  119.     (or (eolp)
  120.     (indent-to arg 0))
  121.     (forward-line 1))
  122.       (move-marker end nil))))
  123.  
  124. (defun indent-relative-maybe ()
  125.   "Indent a new line like previous nonblank line."
  126.   (interactive)
  127.   (indent-relative t))
  128.  
  129. (defun indent-relative (&optional unindented-ok)
  130.   "Space out to under next indent point in previous nonblank line.
  131. An indent point is a non-whitespace character following whitespace.
  132. If the previous nonblank line has no indent points beyond
  133. the column point starts at, `tab-to-tab-stop' is done instead."
  134.   (interactive "P")
  135.   (if abbrev-mode (expand-abbrev))
  136.   (let ((start-column (current-column))
  137.     indent)
  138.     (save-excursion
  139.       (beginning-of-line)
  140.       (if (re-search-backward "^[^\n]" nil t)
  141.       (let ((end (save-excursion (forward-line 1) (point))))
  142.         (move-to-column start-column)
  143.         ;; Is start-column inside a tab on this line?
  144.         (if (> (current-column) start-column)
  145.         (backward-char 1))
  146.         (or (looking-at "[ \t]")
  147.         unindented-ok
  148.         (skip-chars-forward "^ \t" end))
  149.         (skip-chars-forward " \t" end)
  150.         (or (= (point) end) (setq indent (current-column))))))
  151.     (if indent
  152.     (let ((opoint (point-marker)))
  153.       (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  154.       (indent-to indent 0)
  155.       (if (> opoint (point))
  156.           (goto-char opoint))
  157.       (move-marker opoint nil))
  158.       (tab-to-tab-stop))))
  159.  
  160. (defvar tab-stop-list
  161.   '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
  162.   "*List of tab stop positions used by `tab-to-tab-stops'.")
  163.  
  164. (defvar edit-tab-stops-map nil "Keymap used in `edit-tab-stops'.")
  165. (if edit-tab-stops-map
  166.     nil
  167.   (setq edit-tab-stops-map (make-sparse-keymap))
  168.   (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes)
  169.   (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes))
  170.  
  171. (defvar edit-tab-stops-buffer nil
  172.   "Buffer whose tab stops are being edited--in case
  173. the variable `tab-stop-list' is local in that buffer.")
  174.  
  175. (defun edit-tab-stops ()
  176.   "Edit the tab stops used by tab-to-tab-stop.
  177. Creates a buffer *Tab Stops* containing text describing the tab stops.
  178. A colon indicates a column where there is a tab stop.
  179. You can add or remove colons and then do C-c C-c to make changes take effect."
  180.   (interactive)
  181.   (setq edit-tab-stops-buffer (current-buffer))
  182.   (switch-to-buffer (get-buffer-create "*Tab Stops*"))
  183.   (use-local-map edit-tab-stops-map)
  184.   (make-local-variable 'indent-tabs-mode)
  185.   (setq indent-tabs-mode nil)
  186.   (overwrite-mode 1)
  187.   (setq truncate-lines t)
  188.   (erase-buffer)
  189.   (let ((tabs tab-stop-list))
  190.     (while tabs
  191.       (indent-to (car tabs) 0)
  192.       (insert ?:)
  193.       (setq tabs (cdr tabs))))
  194.   (let ((count 0))
  195.     (insert ?\n)
  196.     (while (< count 8)
  197.       (insert (+ count ?0))
  198.     (insert "         ")
  199.       (setq count (1+ count)))
  200.     (insert ?\n)
  201.     (while (> count 0)
  202.       (insert "0123456789")
  203.       (setq count (1- count))))
  204.   (insert "\nTo install changes, type C-c C-c")
  205.   (goto-char (point-min)))
  206.  
  207. (defun edit-tab-stops-note-changes ()
  208.   "Put edited tab stops into effect."
  209.   (interactive)
  210.     (let (tabs)
  211.       (save-excursion
  212.     (goto-char 1)
  213.     (end-of-line)
  214.     (while (search-backward ":" nil t)
  215.       (setq tabs (cons (current-column) tabs))))
  216.       (bury-buffer (prog1 (current-buffer)
  217.               (switch-to-buffer edit-tab-stops-buffer)))
  218.       (setq tab-stop-list tabs))
  219.   (message "Tab stops installed"))
  220.  
  221. (defun tab-to-tab-stop ()
  222.   "Insert spaces or tabs to next defined tab-stop column.
  223. The variable `tab-stop-list' is a list of columns at which there are tab stops.
  224. Use \\[edit-tab-stops] to edit them interactively."
  225.   (interactive)
  226.   (if abbrev-mode (expand-abbrev))
  227.   (let ((tabs tab-stop-list))
  228.     (while (and tabs (>= (current-column) (car tabs)))
  229.       (setq tabs (cdr tabs)))
  230.     (if tabs
  231.     (indent-to (car tabs))
  232.       (insert ?\ ))))
  233.  
  234. (defun move-to-tab-stop ()
  235.   "Move point to next defined tab-stop column.
  236. The variable `tab-stop-list' is a list of columns at which there are tab stops.
  237. Use \\[edit-tab-stops] to edit them interactively."
  238.   (interactive)
  239.   (let ((tabs tab-stop-list))
  240.     (while (and tabs (>= (current-column) (car tabs)))
  241.       (setq tabs (cdr tabs)))
  242.     (if tabs
  243.     (move-to-column (car tabs) t))))
  244.