home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / alt / lucidem / help / 795 < prev    next >
Encoding:
Text File  |  1992-12-14  |  15.9 KB  |  411 lines

  1. x-gateway: rodan.UU.NET from help-lucid-emacs to alt.lucid-emacs.help; Mon, 14 Dec 1992 09:55:44 EST
  2. Date: Mon, 14 Dec 1992 09:51:47 EST
  3. From: ex594bw@pts.mot.com (Bob Weiner)
  4. Message-ID: <9212141451.AA01671@mars.pts.mot.com>
  5. Subject: Re: Filling without removing tabs from 1st line of a paragraph.
  6. Reply-To: bob_weiner@pts.mot.com
  7. Newsgroups: alt.lucid-emacs.help
  8. Path: sparky!uunet!wendy-fate.uu.net!help-lucid-emacs
  9. Sender: help-lucid-emacs-request@lucid.com
  10. Lines: 399
  11.  
  12. > In article <1gao29INNiek@transfer.stratus.com> wayne@sw.stratus.com (Wayne Newberry) writes:
  13. >    I currently turn on auto-fill and I'd prefer to have
  14. >    Lucid Emacs fill using only spaces, rather than a 
  15. >    combination of tabs and spaces.  Is there any way to
  16. >    make this happen?
  17. > I have almost the exact opposite problem.  I have paragraphs that look
  18. > like:
  19. > 14 Dec 92 1.23 me    Some text that goes on for multiple lines, with each
  20. >             continuation line starting with 3 tabs.  The first
  21. >             line of the paragraph has one tab to make the text
  22. >             line up with the continuation lines.
  23. > In emacs 18.58, I could set the fill-prefix to be three tabs and then use
  24. > M-q to fill such a paragraph and it would preserve the tab in the first
  25. > line of the paragraph.  Lucid Emacs 19.3, however, turns that tab into a
  26. > single space, destroying the format.
  27. >             
  28. > 14 Dec 92 1.23 me Some text that goes on for multiple lines, with each
  29. >             continuation line starting with 3 tabs.  The first
  30. >             line of the paragraph has one tab to make the text
  31. >             line up with the continuation lines.
  32. > Is this a bug or a (mis)feature?  Is there a variable I can change to get
  33. > the old functionality back?
  34.  
  35. Use the following "par-align.el" package which is expected to be a part of
  36. GNU Emacs V19; it will solve your problem and works under Lucid or regular
  37. GNU Emacs.  I've used it for years as my exclusive paragraph and region fill
  38. code, so if you like, you can bind it to {M-q}.  I use {M-j} myself for ease
  39. of typing.
  40.  
  41. ;;!emacs
  42. ;;
  43. ;; FILE:         par-align.el
  44. ;; SUMMARY:      Improved paragraph fill, left-align and justify functions
  45. ;; USAGE:        GNU Emacs Lisp Library
  46. ;;
  47. ;; AUTHOR:       Bob Weiner, Applied Research, Motorola, Inc.
  48. ;; E-MAIL:       weiner@mot.com
  49. ;; ORIG-DATE:    14-Apr-89
  50. ;; LAST-MOD:      2-Nov-92 at 11:13:54 by Bob Weiner
  51. ;;
  52. ;; Copyright (C) 1989, 1992    Free Software Foundation, Inc.
  53. ;; Available for use and distribution under the same terms as GNU Emacs.
  54. ;;
  55. ;; This file is not yet part of GNU Emacs.
  56. ;;
  57. ;; MODS:
  58. ;;
  59. ;;   18-Dec-90  (bw)  Added support for TeX and LaTeX modes.
  60. ;;   15-Sep-92  (bw)  Added support for mail and news edit modes.
  61. ;;                    Fixed so only one undo-boundary per fill operation.
  62. ;;   02-Nov-92  (bw)  Fixed so last line of last buffer paragraph is filled
  63. ;;                    properly if doesn't have a trailing newline.
  64. ;;
  65. ;; END-MODS.
  66. ;;
  67. ;; DESCRIPTION:  
  68. ;;
  69. ;;   This package allows GNU Emacs paragraph commands to deal more
  70. ;;   reasonably with indented text and embedded comments.  It lets you
  71. ;;   realign and set the fill prefix on one line of a paragraph, e.g. the
  72. ;;   first, and then have all the other lines match up when you fill the
  73. ;;   paragraph.
  74. ;;   
  75. ;;   An auto-justify minor mode similar to auto-fill from jka@hpfcso.HP.COM
  76. ;;   (Jay Adams) is also included.  Use the 'auto-justify-mode' command to
  77. ;;   toggle it on and off.
  78. ;;
  79. ;;
  80. ;;   I think the 'fill-paragraph-and-align' command eliminates the need for
  81. ;;   Kyle Jones, 'C Comment Edit' package.  Additionally, there is no need
  82. ;;   to edit in a special buffer.
  83. ;;   
  84. ;;   If you set the fill-prefix to some value, then set it to nil (set it at
  85. ;;   the beginning of a line) and then fill the paragraph, the old
  86. ;;   fill-prefix will be stripped off and the paragraph will be filled
  87. ;;   properly with no prefix.  Then just add the new fill-prefix, if any,
  88. ;;   that you want and refill again.
  89. ;;   
  90. ;;   If you have ever tried to fill Emacs Lisp documentation strings or
  91. ;;   embedded comments, you know that all you get is an ugly mess.  You end
  92. ;;   up having to fill after putting blank lines at the beginning and end.
  93. ;;   Now you can fill properly without making any inline changes.  The fill
  94. ;;   routines do not fill the first line of documentation strings since it is
  95. ;;   used by the apropos commands and may intentionally be short.
  96. ;;   
  97. ;;   The fill and movement commands also recognize multiple paragraphs
  98. ;;   within a single comment stretch, just like the ones you see in this
  99. ;;   description text.  Just remember to set the fill prefix properly, just
  100. ;;   as you normally would for the fill-paragraph command.
  101. ;;   
  102. ;;   When filling a region, fill-prefix-prev (old fill prefix) can be
  103. ;;   replaced by fill-prefix throughout the entire region before each
  104. ;;   paragraph is filled.  Simply call the 'fill-region-and-align-all'
  105. ;;   command.
  106. ;;   
  107. ;;   Always fill with point before some of the text to be filled.  If you
  108. ;;   try to fill when point is on a line following all of the text to be
  109. ;;   filled, the next paragraph may be filled by mistake, e.g. when filling
  110. ;;   with point near the end of a comment string.
  111. ;;
  112. ;;   C comments of the form (with any number of '*' chars):
  113. ;;   /*
  114. ;;    * <text>
  115. ;;    * <text>
  116. ;;    */
  117. ;;   are filled well.
  118. ;;
  119. ;;   Multiple line C comments of the form:
  120. ;;   /*        */
  121. ;;   /*        */
  122. ;;   are not filled well, so don't use them.  If you have some already, you
  123. ;;   can convert them to the kind above with the
  124. ;;   'c-comment-make-prefix-only' command found in this package.
  125. ;;
  126. ;;   Comments in UNIX shell scripts of the form (with any number of '#' chars):
  127. ;;   #
  128. ;;   # <text>
  129. ;;   # <text>
  130. ;;   #
  131. ;;   are filled well if the first line of the file begins with a pattern that
  132. ;;   matches the regular expression "^#!/bin/[ck]?sh" as shell scripts should.
  133. ;;
  134. ;;   Fortran comments of the form:
  135. ;;   C
  136. ;;   C <text>
  137. ;;   C <text>
  138. ;;   C
  139. ;;   are filled well.
  140. ;;
  141. ;;   The commands bound immediately below do all of this work.
  142. ;;
  143. ;; DESCRIP-END.
  144.  
  145.  
  146. ;; Suggested key bindings
  147. ;;
  148. (global-set-key "\M-j" 'fill-paragraph-and-align)
  149. (global-set-key "\C-x\C-j" 'fill-region-and-align-all)
  150. (define-key text-mode-map "\C-x\C-j" 'fill-region-and-align)
  151. (define-key indented-text-mode-map "\C-x\C-j" 'fill-region-and-align)
  152. (if (fboundp 'outline-level)
  153.     nil
  154.   (load "outline")
  155.   (provide 'outline))
  156. (define-key outline-mode-map "\C-x\C-j" 'fill-region-and-align)
  157.  
  158. ;; The next two functions are suitable as replacements for the standard
  159. ;; paragraph movement commands if you want them to recognize paragraph
  160. ;; boundaries the way the fill functions below do.
  161. ;;
  162. ;; Suggested key bindings
  163. ;;
  164. ;;(global-set-key "\M-n" 'forward-para)
  165. ;;(global-set-key "\M-p" 'backward-para)
  166.  
  167. (defun forward-para (&optional arg)
  168.   "Move forward to end of paragraph.  With ARG, do it arg times.
  169. A line which  paragraph-start  matches either separates paragraphs,
  170. if  paragraph-separate  matches it also, or is the first line of a paragraph.
  171. A paragraph end is the beginning of a line which is not part of the paragraph
  172. to which the end of the previous line belongs, or the end of the buffer."
  173.   (interactive "p")
  174.   (paragraph-filter 'forward-paragraph arg))
  175.  
  176. (defun backward-para (&optional arg)
  177.   "Move backward to start of paragraph.  With ARG, do it arg times.
  178. A paragraph start is the beginning of a line which is a first-line-of-paragraph
  179. or which is ordinary text and follows a paragraph-separating line, except
  180. if the first real line of a paragraph is preceded by a blank line,
  181. the paragraph starts at that blank line.
  182. See forward-paragraph for more information."
  183.   (interactive "p")
  184.   (or arg (setq arg 1))
  185.   (paragraph-filter 'forward-paragraph (- arg)))
  186.  
  187.  
  188. (defconst fill-prefix-prev nil
  189.   "Previous string inserted at front of new line during filling, or nil for none.
  190. Setting this variable automatically makes it local to the current buffer.")
  191. (make-variable-buffer-local 'fill-prefix-prev)
  192.  
  193. ;; Redefine this function so that it sets 'fill-prefix-prev' also.
  194. (defun set-fill-prefix ()
  195.   "Set the fill-prefix to the current line up to point.
  196. Also sets fill-prefix-prev to previous value of fill-prefix.
  197. Filling expects lines to start with the fill prefix and reinserts the fill
  198. prefix in each resulting line."
  199.   (interactive)
  200.   (setq fill-prefix-prev fill-prefix
  201.     fill-prefix (buffer-substring
  202.               (save-excursion (beginning-of-line) (point))
  203.               (point)))
  204.   (if (equal fill-prefix-prev "")
  205.       (setq fill-prefix-prev nil))
  206.   (if (equal fill-prefix "")
  207.       (setq fill-prefix nil))
  208.   (if fill-prefix
  209.       (message "fill-prefix: \"%s\"" fill-prefix)
  210.     (message "fill-prefix cancelled")))
  211.  
  212. (defun fill-paragraph-and-align (justify-flag)
  213.   "Fill current paragraph.  Prefix arg JUSTIFY-FLAG means justify as well.
  214. Does not alter fill prefix on first line of paragraph.  Any whitespace
  215. separated version of the fill-prefix, or fill-prefix-prev when fill-prefix is
  216. nil, at the beginning of lines is deleted before the fill is performed.  This
  217. aligns all lines in a paragraph properly after the fill-prefix is changed.
  218. Works well within text, singly delimited C comments, Lisp comments and
  219. documentation strings, and Fortran comments."
  220.   (interactive "P")
  221.   (paragraph-filter 'fill-para-align justify-flag))
  222.  
  223. (defun fill-region-and-align-all (justify-flag)
  224.   "Fill each line in region.  Prefix arg JUSTIFY-FLAG means justify as well.
  225. Replace any whitespace separated version of fill-prefix-prev with fill-prefix
  226. in all lines of region.  This aligns all lines throughout the region properly
  227. after the fill-prefix is changed.  Works well within text, singly delimited C
  228. comments, UNIX shell scripts, Lisp comments and documentation strings, and
  229. Fortran comments.
  230. See also documentation for fill-region-and-align."
  231.   (interactive "P")
  232.   (paragraph-filter 'fill-region-align justify-flag t))
  233.  
  234. (defun fill-region-and-align (justify-flag &optional align-all)
  235.   "Fill each line in region.  Prefix arg JUSTIFY-FLAG means justify as well.
  236. Optional ALIGN-ALL non-nil means replace fill-prefix-prev with fill-prefix in
  237. all lines of region, otherwise, does not alter prefix in paragraph separator
  238. lines and first lines of paragraphs.
  239. Any whitespace separated version of fill-prefix, or fill-prefix-prev when
  240. fill-prefix is nil, at the beginning of appropriate lines is removed before the
  241. fill is performed.  This aligns all lines in a paragraph properly after the
  242. fill-prefix is changed.  Works well within text, singly delimited C comments,
  243. UNIX shell scripts, Lisp comments and documentation strings, and Fortran comments."
  244.   (interactive "P")
  245.   (paragraph-filter 'fill-region-align justify-flag align-all))
  246.  
  247. (defun paragraph-filter (func arg1 &optional arg2)
  248.   ;;                      LISP          C/C++        Fortran        Eiffel
  249.   ;;                      --------------------------------------------------
  250.   ;; comment-start        ";"           "/*\\|//"    nil (^[Cc*])   "--"     
  251.   ;; comment-start-skip   ";+ *"        "/\\*+ *"    "![ \t]*"      "--+[ \t]*"
  252.   ;; comment-end          "" ([^J^L])   " */\\|$"    "" (^J)        "" (^J)
  253.   ;;
  254.   (let* ((*C* (memq major-mode '(c-mode c++-mode)))
  255.      (*lsp*  (memq major-mode
  256.                '(emacs-lisp-mode lisp-interaction-mode scheme-mode
  257.              lisp-mode)))
  258.      (*mail* (memq major-mode '(mail-mode rmail-edit-mode
  259.                     news-reply-mode)))
  260.      (*txt*  (memq major-mode
  261.                '(text-mode indented-text-mode texinfo-mode
  262.              para-mode LaTeX-mode latex-mode TeX-mode 
  263.              tex-mode scribe-mode outline-mode picture-mode)))
  264.      (extra-para-sep
  265.        (concat "^"
  266.            ;; Don't change the '?' in the following lines to a '+', it
  267.            ;; will break certain fill boundary conditions.
  268.            (if (eq major-mode 'fortran-mode) "[cC*]?")
  269.            "[ \t]*\\("
  270.            (cond ((and (< (point-min) 2) (> (point-max) 10)
  271.                    (string-match "^#!/bin/[ck]?sh"
  272.                        (buffer-substring 1 11)))
  273.               "#*")
  274.              ;; Don't change the '?' in the following lines to a
  275.              ;; '+', it will break certain fill boundary
  276.              ;; conditions.
  277.              (comment-start-skip
  278.                (concat "\\(" comment-start-skip "\\)?")))
  279.            (if *C* "\\|\*+/?\\|//.*")
  280.            (if *mail* "\\|^[!$%&|<> \t]+")
  281.            "\\)[ \t]*$"
  282.            (if *lsp* "\\|^[ \t]*\\([[(]\\|\"\\)")
  283.            ;; For Interleaf TPS, texinfo and Scribe markup documents
  284.            (if *txt* "\\|^[@\\<]")
  285.            ))
  286.      (paragraph-separate
  287.        (concat paragraph-separate "\\|" extra-para-sep))
  288.      (paragraph-start
  289.        (concat paragraph-start "\\|" extra-para-sep)))
  290.     (if arg2
  291.     (funcall func arg1 arg2)
  292.       (funcall func arg1))))
  293.  
  294. (defun fill-region-align (justify-flag &optional align-all)
  295.   (if align-all
  296.       (replace-fill-str fill-prefix-prev fill-prefix))
  297.   (save-excursion
  298.     (let ((end (max (point) (mark))))
  299.       (goto-char (min (point) (mark)))
  300.       (while (and (not (eobp)) (< (point) end))
  301.     (fill-para-align justify-flag align-all)
  302.     (forward-paragraph)
  303.     ;; Forward to real paragraph end if not in lisp mode
  304.     (or *lsp* (re-search-forward (concat "\\'\\|" paragraph-separate)))))))
  305.  
  306. (defun fill-para-align (justify-flag &optional leave-prefix)
  307.   (save-excursion
  308.     (end-of-line)
  309.     ;; Backward to para begin
  310.     (re-search-backward (concat "\\`\\|" paragraph-separate))
  311.     (forward-line (if (looking-at extra-para-sep) 2 1))
  312.     (let ((region-start (point)))
  313.       (forward-line -1)
  314.       (let ((from (point)))
  315.     (forward-paragraph)
  316.     ;; Forward to real paragraph end if not in lisp mode
  317.     (or *lsp* (re-search-forward (concat "\\'\\|" paragraph-separate)))
  318.     (or (= (point) (point-max)) (beginning-of-line))
  319.     (or leave-prefix
  320.         (replace-fill-str
  321.           (or fill-prefix fill-prefix-prev)
  322.           "" nil region-start (point)))
  323.     (fill-region-as-paragraph from (point) justify-flag)))))
  324.  
  325. (defun replace-fill-str (fill-str-prev fill-str &optional suffix start end)
  326.   "Replace whitespace separated FILL-STR-PREV with FILL-STR.
  327. Optional SUFFIX non-nil means replace at ends of lines, default is beginnings.
  328. Optional arguments START and END specify the replace region, default is the
  329. current region."
  330.   (if fill-str-prev
  331.       (progn (if (not start) (setq start (min (point) (mark))))
  332.          (if (not end)   (setq end   (max (point) (mark))))
  333.          (if (not fill-str) (setq fill-str ""))
  334.          (save-excursion
  335.            (save-restriction
  336.          (narrow-to-region start end)
  337.          (goto-char (point-min))
  338.          (let ((prefix
  339.              (concat
  340.                (if suffix nil "^")
  341.                "[ \t]*"
  342.                (regexp-quote
  343.                  ;; Get non-whitespace separated fill-str-prev
  344.                  (substring
  345.                    fill-str-prev
  346.                    (or (string-match "[^ \t]" fill-str-prev) 0)
  347.                    (if (string-match
  348.                      "[ \t]*\\(.*[^ \t]\\)[ \t]*$"
  349.                      fill-str-prev)
  350.                    (match-end 1))))
  351.                "[ \t]*"
  352.                (if suffix "$"))))
  353.            (while (re-search-forward prefix nil t)
  354.              (replace-match fill-str nil t))))))))
  355.  
  356. (defun c-comment-make-prefix-only ()
  357.   "Make multiply-delimited C comments in region singly delimited.
  358. Converts comments of form:
  359. /*  <text>  */      to    /*
  360. /*  <text>  */             * <text>
  361.                            * <text>
  362.                            */"
  363.   (interactive)
  364.   (replace-fill-str "/*" " * ")
  365.   (replace-fill-str "*/" "" t)
  366.   (save-excursion
  367.     (goto-char (min (point) (mark)))
  368.     (insert "/*\n"))
  369.   (save-excursion
  370.     (goto-char (max (point) (mark)))
  371.     (insert (if (bolp) " */\n" "\n */"))))
  372.  
  373. ;;; Auto-justify minor mode
  374. ;;
  375. ;; From: jka@hpfcso.HP.COM (Jay Adams)
  376. ;; Newsgroups: comp.emacs
  377. ;; Subject:  auto-justify
  378. ;; Date: 7 Sep 89 01:30:18 GMT
  379. ;; Organization: Hewlett-Packard, Fort Collins, CO, USA
  380.  
  381. (defvar auto-justify-mode '()
  382.   "Non-nil if auto-justifying minor mode is on.")
  383.  
  384. (or (assoc 'auto-justify-mode minor-mode-alist)
  385.     (setq minor-mode-alist
  386.           (cons '(auto-justify-mode " Justify")
  387.                 minor-mode-alist)))
  388.            
  389. (defun auto-justify-mode ()
  390.   "Toggle auto-justify minor mode."
  391.   (interactive)
  392.   (if (eq auto-fill-hook 'do-auto-fill-justify)
  393.       (setq auto-fill-hook 'do-auto-fill auto-justify-mode nil)
  394.     (setq auto-fill-hook 'do-auto-fill-justify
  395.           auto-justify-mode t)))
  396.  
  397. (defun do-auto-fill-justify ()
  398.   "Same as do-auto-fill but justifies current line."
  399.   (do-auto-fill)
  400.   (save-excursion
  401.     (forward-line -1)
  402.     (justify-current-line)))
  403.  
  404. (provide 'par-align)
  405.