home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / util / edit / jade / lisp / fill-mode.jl < prev    next >
Lisp/Scheme  |  1994-10-03  |  3KB  |  81 lines

  1. ;;;; fill-mode.jl -- minor mode and functions for filling text
  2. ;;;  Copyright (C) 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4. ;;; This file is part of Jade.
  5.  
  6. ;;; Jade is free software; you can redistribute it and/or modify it
  7. ;;; 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. ;;; Jade is distributed in the hope that it will be useful, but
  12. ;;; 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 Jade; see the file COPYING.  If not, write to
  18. ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. (provide 'fill-mode)
  21.  
  22. (defvar fill-column 71
  23.   "Position at which the text filling commands break lines.")
  24.  
  25. (defvar fill-mode-p nil)
  26. (make-variable-buffer-local 'fill-mode-p)
  27.  
  28. ;;;###autoload
  29. (defun fill-mode ()
  30.   "Minor mode for automatically filling lines, i.e. word-wrapping. This makes
  31. the SPC key checks if the cursor is past the fill-column. If so, the next
  32. line is started."
  33.   (interactive)
  34.   (if fill-mode-p
  35.       (progn
  36.     (setq fill-mode-p nil)
  37.     (remove-minor-mode 'fill-mode "Fill")
  38.     (unbind-keys minor-mode-keymap "SPC"))
  39.     (add-minor-mode 'fill-mode "Fill")
  40.     (setq fill-mode-p t)
  41.     (bind-keys minor-mode-keymap
  42.       "SPC" 'fill-mode-spc)))
  43.  
  44. ;;;###autoload
  45. (defun fill-mode-on ()
  46.   (interactive)
  47.   (unless fill-mode-p
  48.     (fill-mode))
  49.   nil)
  50.  
  51. (defun fill-mode-spc ()
  52.   (interactive)
  53.   (when (> (pos-col (cursor-pos)) fill-column)
  54.     (let
  55.     ((pos (cursor-pos)))
  56.       (set-pos-col pos (1+ fill-column))
  57.       (setq pos (unless (word-start pos) (forward-word -1 pos)))
  58.       (insert "\n" pos)
  59.       (let
  60.       ((end (left-char 1 (copy-pos pos))))
  61.     (when (equal (get-char end) ?\ )
  62.       (delete-area end pos)))
  63.       ;; Hack to auto-indent new line in indented-text-mode
  64.       (when (eq major-mode 'indented-text-mode)
  65.     (let
  66.         ((old-pos (make-mark)))
  67.       (goto-line-start)
  68.       (text-mode-indent-tab)
  69.       (goto-char (mark-pos old-pos))))))
  70.   (insert " "))
  71.  
  72. ;;;###autoload
  73. (defun set-fill-column (&optional column)
  74.   "Sets the column number for filling to (the variable `fill-column') to
  75. COLUMN or the current column."
  76.   (interactive)
  77.   (setq fill-column (if (numberp column)
  78.             column 
  79.               (pos-col (char-to-glyph-pos (cursor-pos)))))
  80.   (format t "Fill column set to %d." (1+ fill-column)))
  81.