home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-osu / shell-mode < prev    next >
Encoding:
Text File  |  1989-10-26  |  5.3 KB  |  156 lines

  1. Path: tut.cis.ohio-state.edu!UUNET.UU.NET!telotech!bsa%hal
  2. From: telotech!bsa%hal@UUNET.UU.NET (Brandon S. Allbery)
  3. Newsgroups: gnu.emacs
  4. Subject: (repost) simple-indent mode
  5. Message-ID: <8910030848.AA03962@telotech.UUCP>
  6. Date: 2 Oct 89 12:19:33 GMT
  7. Sender: daemon@tut.cis.ohio-state.edu
  8. Distribution: gnu
  9. Organization: GNUs Not Usenet
  10. Lines: 143
  11.  
  12. I sent this out yesterday, but something seems to have broken suddenly in the
  13. kluge that forces smail and mh to work in a rational way on a Xenix-descended
  14. system that is more a hybrid of everything than compatible with any one.  So
  15. here it is again, via another mailer.
  16.  
  17. Someone was looking for a perl mode.  I don't have one, but I have a major
  18. mode I whipped up to handle random script-like languages:  shell, perl,
  19. Makefiles, SQL, Accell/Language, etc.  To use it, put (load "sindent") in
  20. your .emacs and set up your auto-mode-alist to invoke simple-indent-mode on
  21. whatever files.  It's not perfect, but it works far better than just text mode
  22. and uses a rigid indent, unlike indented-text mode.
  23.  
  24. ++Brandon
  25. ------------------------------------------------------------------------------
  26. ;;; Major mode to do simple indentation
  27. ;;; based on text mode, but redefines TAB to use tab-stop
  28. ;;; obeys left-margin
  29.  
  30. (defvar tab-stop 8
  31.   "*Size of a TAB in simple-indent mode.")
  32. (make-variable-buffer-local 'tab-stop)
  33.  
  34. (defvar sindent-obey-all-tabs nil
  35.   "*If non-nil, all TABs in simple-indent mode use tab-stop.")
  36.  
  37. (defvar sindent-mode-syntax-table nil
  38.   "Syntax table used while in simple-indent mode.")
  39.  
  40. (defvar sindent-mode-abbrev-table nil
  41.   "Abbrev table used while in sindent mode.")
  42. (define-abbrev-table 'sindent-mode-abbrev-table ())
  43.  
  44. (if sindent-mode-syntax-table
  45.     ()
  46.   (setq sindent-mode-syntax-table (make-syntax-table))
  47.   (modify-syntax-entry ?\" ".   " sindent-mode-syntax-table)
  48.   (modify-syntax-entry ?\\ ".   " sindent-mode-syntax-table)
  49.   (modify-syntax-entry ?' "w   " sindent-mode-syntax-table))
  50.  
  51. (defvar sindent-mode-map nil "")
  52. (if sindent-mode-map
  53.     ()
  54.   (setq sindent-mode-map (make-sparse-keymap))
  55.   (define-key sindent-mode-map "\t" 'simple-tab)
  56.   (define-key sindent-mode-map "\177" 'sindent-backward-delete-char)
  57.   (define-key sindent-mode-map "\es" 'center-line)
  58.   (define-key sindent-mode-map "\eS" 'center-paragraph))
  59.  
  60. (defun simple-indent-mode ()
  61.   "Major mode to do simple indentation.  Based on Text mode.
  62. In this mode, TAB moves tab-width spaces in the indentation area of a line,
  63. compacting spaces to real tabs as it goes.  Commands:\\{sindent-mode-map}
  64. Turning on this mode runs text-mode-hook and sindent-mode-hook."
  65.   (interactive)
  66.   (kill-all-local-variables)
  67.   (use-local-map sindent-mode-map)
  68.   (setq mode-name "Simple")
  69.   (setq major-mode 'simple-indent-mode)
  70.   (setq local-abbrev-table sindent-mode-abbrev-table)
  71.   (set (make-local-variable 'indent-line-function) 'simple-indent-line)
  72.   (set-syntax-table sindent-mode-syntax-table)
  73.   (run-hooks 'text-mode-hook 'sindent-mode-hook))
  74.  
  75. ;;; Indent this line to the previous line's indentation.
  76. (defun simple-indent-line ()
  77.   "Indent current line like the previous line, provided that it is empty."
  78.   (and (bolp)
  79.        (eolp)
  80.        (not (bobp))
  81.        (indent-to
  82.     (save-excursion
  83.       (forward-line -1)
  84.       (skip-chars-forward " \t")
  85.       (current-column)))))
  86.  
  87. ;;; Indent to the next tab stop, as defined by the tab-stop variable
  88. (defun simple-tab (arg)
  89.   "Indent to the next tab-stop, as defined by the `tab-stop' variable."
  90.   (interactive "P")
  91.   (if arg
  92.       (let ((count (prefix-numeric-value arg)))
  93.     (and (< count 0)
  94.          (error "Can't tab by negative amount"))
  95.     (while (/= count 0)
  96.       (simple-tab-1)
  97.       (setq count (1- count))))
  98.     (simple-tab-1)))
  99.  
  100. ;;; Do a single tab.
  101. (defun simple-tab-1 ()
  102.   "Perform a single simple tab."
  103.   (if (or sindent-obey-all-tabs
  104.       (save-excursion
  105.         (skip-chars-backward " \t")
  106.         (bolp)))
  107.       (let (goal here)
  108.     (skip-chars-forward " \t")
  109.     (setq here (point))
  110.     (setq goal (+ (- (current-column) left-margin) tab-stop))
  111.     (setq goal (+ (- goal (% goal tab-stop)) left-margin))
  112.     (skip-chars-backward " \t")
  113.     (delete-region (point) here)
  114.     (indent-to goal))
  115.     (tab-to-tab-stop)))
  116.  
  117. ;;; Delete backwards as (delete-backward-char), but if in indent only back
  118. ;;; to previous tab stop
  119. (defun sindent-backward-delete-char (arg &optional killp)
  120.   "Delete charaters backward, obeying simple-indent's tab stops.
  121. Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
  122. Interactively, ARG is the prefix arg (default 1)
  123. and KILLP is t if prefix arg is was specified."
  124.   (interactive "*p\nP")
  125.   (let ((count arg)
  126.     (del-count arg))
  127.     (save-excursion
  128.       (while (and (> count 0) (not (bobp)))
  129.     (if (and (not (bolp))
  130.          (save-excursion
  131.            (skip-chars-backward " \t")
  132.            (bolp)))
  133.         (progn
  134.           (and (= (preceding-char) ?\t)
  135.            (let ((col (current-column)))
  136.              (forward-char -1)
  137.              (setq col (- col (current-column)))
  138.              (insert-char ?\  col)
  139.              (delete-char 1)))
  140.           (if (= (current-column) left-margin)
  141.           (progn
  142.             (setq del-count (+ del-count left-margin -2))
  143.             (forward-char (- (+ 2 left-margin))))
  144.         (let ((cols (% (- (current-column) left-margin) tab-stop)))
  145.           (and (= cols 0)
  146.                (setq cols tab-stop))
  147.           (forward-char (- cols))
  148.           (setq del-count (+ del-count (1- cols))))))
  149.       (forward-char -1))
  150.     (setq count (1- count))))
  151.     (delete-backward-char del-count killp)))
  152.  
  153. (provide 'sindent)
  154.  
  155.  
  156.