(nroff-mode): Set comment-insert-comment-function rather than
[bpt/emacs.git] / lisp / textmodes / fill.el
index aaa10fb..6a0eb7a 100644 (file)
@@ -1,7 +1,7 @@
-;;; fill.el --- fill commands for Emacs
+;;; fill.el --- fill commands for Emacs                -*- coding: iso-2022-7bit -*-
 
-;; Copyright (C) 1985,86,92,94,95,96,97,1999,2001,02,03,2004
-;;               Free Software Foundation, Inc.
+;; Copyright (C) 1985, 1986, 1992, 1994, 1995, 1996, 1997, 1999, 2001, 2002,
+;;   2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 ;; Keywords: wp
@@ -10,7 +10,7 @@
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -20,8 +20,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
@@ -48,10 +48,12 @@ A value of nil means that any change in indentation starts a new paragraph."
   "*Non-nil means put two spaces after a colon when filling."
   :type 'boolean
   :group 'fill)
+;;;###autoload(put 'colon-double-space 'safe-local-variable 'booleanp)
 
 (defvar fill-paragraph-function nil
   "Mode-specific function to fill a paragraph, or nil if there is none.
-If the function returns nil, then `fill-paragraph' does its normal work.")
+If the function returns nil, then `fill-paragraph' does its normal work.
+A value of t means explicitly \"do nothing special\".")
 
 (defvar fill-paragraph-handle-comment t
   "Non-nil means paragraph filling will try to pay attention to comments.")
@@ -61,7 +63,8 @@ If the function returns nil, then `fill-paragraph' does its normal work.")
 Kinsoku processing is designed to prevent certain characters from being
 placed at the beginning or end of a line by filling.
 See the documentation of `kinsoku' for more information."
-  :type 'boolean)
+  :type 'boolean
+  :group 'fill)
 
 (defun set-fill-prefix ()
   "Set the fill prefix to the current line up to point.
@@ -87,7 +90,8 @@ reinserts the fill prefix in each resulting line."
 (defcustom adaptive-fill-regexp
   ;; Added `!' for doxygen comments starting with `//!' or `/*!'.
   ;; Added `%' for TeX comments.
-  (purecopy "[ \t]*\\([-!|#%;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*")
+  ;; RMS: deleted the code to match `1.' and `(1)'.
+  "[ \t]*\\([-!|#%;>*\e,A7\e$,1s"s#sC\e$,2"F\e(B]+[ \t]*\\)*"
   "*Regexp to match text at start of line that constitutes indentation.
 If Adaptive Fill mode is enabled, a prefix matching this pattern
 on the first and second lines of a paragraph is used as the
@@ -113,8 +117,8 @@ if it would act as a paragraph-starter on the second line."
   :group 'fill)
 
 (defcustom adaptive-fill-function nil
-  "*Function to call to choose a fill prefix for a paragraph, or nil.
-This function is used when `adaptive-fill-regexp' does not match."
+  "Function to call to choose a fill prefix for a paragraph, or nil.
+A nil value means the function has not determined the fill prefix."
   :type '(choice (const nil) function)
   :group 'fill)
 
@@ -155,14 +159,19 @@ Leave one space between words, two at end of sentences or after colons
 and `sentence-end-without-period').
 Remove indentation from each line."
   (interactive "*r")
-  (let ((end-spc-re (concat "\\(" sentence-end "\\) *\\|  +")))
+  ;; Ideally, we'd want to scan the text from the end, so that changes to
+  ;; text don't affect the boundary, but the regexp we match against does
+  ;; not match as eagerly when matching backward, so we instead use
+  ;; a marker.
+  (unless (markerp end) (setq end (copy-marker end t)))
+  (let ((end-spc-re (concat "\\(" (sentence-end) "\\) *\\|  +")))
     (save-excursion
       (goto-char beg)
       ;; Nuke tabs; they get screwed up in a fill.
       ;; This is quick, but loses when a tab follows the end of a sentence.
       ;; Actually, it is difficult to tell that from "Mr.\tSmith".
       ;; Blame the typist.
-      (subst-char-in-region beg end ?\t ?\ )
+      (subst-char-in-region beg end ?\t ?\s)
       (while (and (< (point) end)
                  (re-search-forward end-spc-re end t))
        (delete-region
@@ -204,6 +213,16 @@ Remove indentation from each line."
       (unless (zerop cmp)
        (substring s1 0 cmp)))))
 
+(defun fill-match-adaptive-prefix ()
+  (let ((str (or
+              (and adaptive-fill-function (funcall adaptive-fill-function))
+              (and adaptive-fill-regexp (looking-at adaptive-fill-regexp)
+                   (match-string-no-properties 0)))))
+    (if (>= (+ (current-left-margin) (length str)) (current-fill-column))
+        ;; Death to insanely long prefixes.
+        nil
+      str)))
+
 (defun fill-context-prefix (from to &optional first-line-regexp)
   "Compute a fill prefix from the text between FROM and TO.
 This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function'
@@ -217,55 +236,45 @@ act as a paragraph-separator."
     (if (eolp) (forward-line 1))
     ;; Move to the second line unless there is just one.
     (move-to-left-margin)
-    (let ((firstline (point))
-         first-line-prefix
+    (let (first-line-prefix
          ;; Non-nil if we are on the second line.
-         second-line-prefix
-         start)
-      (setq start (point))
+         second-line-prefix)
       (setq first-line-prefix
            ;; We don't need to consider `paragraph-start' here since it
            ;; will be explicitly checked later on.
            ;; Also setting first-line-prefix to nil prevents
            ;; second-line-prefix from being used.
-           (cond ;; ((looking-at paragraph-start) nil)
-                 ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
-                  (match-string-no-properties 0))
-                 (adaptive-fill-function (funcall adaptive-fill-function))))
+           ;; ((looking-at paragraph-start) nil)
+           (fill-match-adaptive-prefix))
       (forward-line 1)
       (if (< (point) to)
-       (progn
-         (move-to-left-margin)
-         (setq start (point))
-         (setq second-line-prefix
-               (cond ((looking-at paragraph-start) nil) ;Can it happen ? -stef
-                     ((and adaptive-fill-regexp
-                           (looking-at adaptive-fill-regexp))
-                      (buffer-substring-no-properties start (match-end 0)))
-                     (adaptive-fill-function
-                      (funcall adaptive-fill-function))))
-         ;; If we get a fill prefix from the second line,
-         ;; make sure it or something compatible is on the first line too.
-         (when second-line-prefix
-           (unless first-line-prefix (setq first-line-prefix ""))
-           ;; If the non-whitespace chars match the first line,
-           ;; just use it (this subsumes the 2 checks used previously).
-           ;; Used when first line is `/* ...' and second-line is
-           ;; ` * ...'.
-           (let ((tmp second-line-prefix)
-                 (re "\\`"))
-             (while (string-match "\\`[ \t]*\\([^ \t]+\\)" tmp)
-               (setq re (concat re ".*" (regexp-quote (match-string 1 tmp))))
-               (setq tmp (substring tmp (match-end 0))))
-             ;; (assert (string-match "\\`[ \t]*\\'" tmp))
-
-             (if (string-match re first-line-prefix)
-                 second-line-prefix
-
-               ;; Use the longest common substring of both prefixes,
-               ;; if there is one.
-               (fill-common-string-prefix first-line-prefix
-                                          second-line-prefix)))))
+          (progn
+            (move-to-left-margin)
+            (setq second-line-prefix
+                  (cond ((looking-at paragraph-start) nil) ;Can it happen? -Stef
+                        (t (fill-match-adaptive-prefix))))
+            ;; If we get a fill prefix from the second line,
+            ;; make sure it or something compatible is on the first line too.
+            (when second-line-prefix
+              (unless first-line-prefix (setq first-line-prefix ""))
+              ;; If the non-whitespace chars match the first line,
+              ;; just use it (this subsumes the 2 checks used previously).
+              ;; Used when first line is `/* ...' and second-line is
+              ;; ` * ...'.
+              (let ((tmp second-line-prefix)
+                    (re "\\`"))
+                (while (string-match "\\`[ \t]*\\([^ \t]+\\)" tmp)
+                  (setq re (concat re ".*" (regexp-quote (match-string 1 tmp))))
+                  (setq tmp (substring tmp (match-end 0))))
+                ;; (assert (string-match "\\`[ \t]*\\'" tmp))
+
+                (if (string-match re first-line-prefix)
+                    second-line-prefix
+
+                  ;; Use the longest common substring of both prefixes,
+                  ;; if there is one.
+                  (fill-common-string-prefix first-line-prefix
+                                             second-line-prefix)))))
        ;; If we get a fill prefix from a one-line paragraph,
        ;; maybe change it to whitespace,
        ;; and check that it isn't a paragraph starter.
@@ -281,7 +290,7 @@ act as a paragraph-separator."
                                (string-match comment-start-skip
                                              first-line-prefix)))
                       first-line-prefix
-                    (make-string (string-width first-line-prefix) ?\ ))))
+                    (make-string (string-width first-line-prefix) ?\s))))
              ;; But either way, reject it if it indicates the start
              ;; of a paragraph when text follows it.
              (if (not (eq 0 (string-match paragraph-start
@@ -290,12 +299,15 @@ act as a paragraph-separator."
 
 (defun fill-single-word-nobreak-p ()
   "Don't break a line after the first or before the last word of a sentence."
-  (or (looking-at "[ \t]*\\sw+[ \t]*[.?!:][ \t]*$")
+  ;; Actually, allow breaking before the last word of a sentence, so long as
+  ;; it's not the last word of the paragraph.
+  (or (looking-at (concat "[ \t]*\\sw+" "\\(?:" (sentence-end) "\\)[ \t]*$"))
       (save-excursion
        (skip-chars-backward " \t")
        (and (/= (skip-syntax-backward "w") 0)
             (/= (skip-chars-backward " \t") 0)
-            (/= (skip-chars-backward ".?!:") 0)))))
+            (/= (skip-chars-backward ".?!:") 0)
+            (looking-at (sentence-end))))))
 
 (defun fill-french-nobreak-p ()
   "Return nil if French style allows breaking the line at point.
@@ -304,14 +316,14 @@ after an opening paren or just before a closing paren or a punctuation
 mark such as `?' or `:'.  It is common in French writing to put a space
 at such places, which would normally allow breaking the line at those
 places."
-  (or (looking-at "[ \t]*[])}»?!;:-]")
+  (or (looking-at "[ \t]*[])}\e,A;\e,b;\e(B?!;:-]")
       (save-excursion
        (skip-chars-backward " \t")
        (unless (bolp)
          (backward-char 1)
-         (or (looking-at "[([{«]")
+         (or (looking-at "[([{\e,A+\e,b+\e(B]")
              ;; Don't cut right after a single-letter word.
-             (and (memq (preceding-char) '(?\t ?\ ))
+             (and (memq (preceding-char) '(?\t ?\s))
                   (eq (char-syntax (following-char)) ?w)))))))
 
 (defcustom fill-nobreak-predicate nil
@@ -332,7 +344,7 @@ be tested.  If it returns t, fill commands do not break the line there."
 Can be customized with the variables `fill-nobreak-predicate'
 and `fill-nobreak-invisible'."
   (or
-   (and fill-nobreak-invisible (line-move-invisible (point)))
+   (and fill-nobreak-invisible (invisible-p (point)))
    (unless (bolp)
     (or
      ;; Don't break after a period followed by just one space.
@@ -343,17 +355,23 @@ and `fill-nobreak-invisible'."
      ;; it at the end of the line.
      (and sentence-end-double-space
          (save-excursion
-           (skip-chars-backward ". ")
-           (looking-at "\\. \\([^ ]\\|$\\)")))
+           (skip-chars-backward " ")
+           (and (eq (preceding-char) ?.)
+                (looking-at " \\([^ ]\\|$\\)"))))
      ;; Another approach to the same problem.
      (save-excursion
-       (skip-chars-backward ". ")
-       (and (looking-at "\\.")
-           (not (looking-at sentence-end))))
+       (skip-chars-backward " ")
+       (and (eq (preceding-char) ?.)
+           (not (progn (forward-char -1) (looking-at (sentence-end))))))
      ;; Don't split a line if the rest would look like a new paragraph.
      (unless use-hard-newlines
        (save-excursion
-        (skip-chars-forward " \t") (looking-at paragraph-start)))
+        (skip-chars-forward " \t")
+        ;; If this break point is at the end of the line,
+        ;; which can occur for auto-fill, don't consider the newline
+        ;; which follows as a reason to return t.
+        (and (not (eolp))
+             (looking-at paragraph-start))))
      (run-hook-with-args-until-success 'fill-nobreak-predicate)))))
 
 ;; Put `fill-find-break-point-function' property to charsets which
@@ -399,18 +417,19 @@ Point is moved to just past the fill prefix on the first line."
                          "\\)?[ \t]*")
                "[ \t]*")))
     (goto-char from)
-    (if (>= (+ (current-left-margin) (length prefix))
-           (current-fill-column))
-       (error "fill-prefix too long for specified width"))
+    ;; Why signal an error here?  The problem needs to be caught elsewhere.
+    ;; (if (>= (+ (current-left-margin) (length prefix))
+    ;;         (current-fill-column))
+    ;;     (error "fill-prefix too long for specified width"))
     (forward-line 1)
     (while (< (point) to)
       (if (looking-at fpre)
-         (delete-region (point) (match-end 0)))
+          (delete-region (point) (match-end 0)))
       (forward-line 1))
     (goto-char from)
     (if (looking-at fpre)
        (goto-char (match-end 0)))
-    (setq from (point))))
+    (point)))
 
 ;; The `fill-space' property carries the string with which a newline
 ;; should be replaced when unbreaking a line (in fill-delete-newlines).
@@ -424,19 +443,19 @@ Point is moved to just past the fill prefix on the first line."
   ;; loses on split abbrevs ("Mr.\nSmith")
   (let ((eol-double-space-re
         (cond
-         ((not colon-double-space) (concat sentence-end "$"))
+         ((not colon-double-space) (concat (sentence-end) "$"))
          ;; Try to add the : inside the `sentence-end' regexp.
-         ((string-match "\\[[^][]*\\(\\.\\)[^][]*\\]" sentence-end)
-          (concat (replace-match ".:" nil nil sentence-end 1) "$"))
+         ((string-match "\\[[^][]*\\(\\.\\)[^][]*\\]" (sentence-end))
+          (concat (replace-match ".:" nil nil (sentence-end) 1) "$"))
          ;; Can't find the right spot to insert the colon.
          (t "[.?!:][])}\"']*$")))
        (sentence-end-without-space-list
         (string-to-list sentence-end-without-space)))
     (while (re-search-forward eol-double-space-re to t)
-      (or (>= (point) to) (memq (char-before) '(?\t ?\ ))
+      (or (>= (point) to) (memq (char-before) '(?\t ?\s))
          (memq (char-after (match-beginning 0))
                sentence-end-without-space-list)
-         (insert-and-inherit ?\ ))))
+         (insert-and-inherit ?\s))))
 
   (goto-char from)
   (if enable-multibyte-characters
@@ -465,7 +484,7 @@ Point is moved to just past the fill prefix on the first line."
   (goto-char from)
   (skip-chars-forward " \t")
   ;; Then change all newlines to spaces.
-  (subst-char-in-region from to ?\n ?\ )
+  (subst-char-in-region from to ?\n ?\s)
   (if (and nosqueeze (not (eq justify 'full)))
       nil
     (canonically-space-region (or squeeze-after (point)) to)
@@ -507,7 +526,6 @@ The break position will be always after LINEBEG and generally before point."
       ;; Ok, skip at least one word or one \c| character.
       ;; Meanwhile, don't stop at a period followed by one space.
       (let ((to (line-end-position))
-           (fill-nobreak-predicate nil) ;to break sooner.
            (first t))
        (goto-char linebeg)
        (while (and (< (point) to) (or first (fill-nobreak-p)))
@@ -534,6 +552,17 @@ The break position will be always after LINEBEG and generally before point."
            ;; Make sure we take SOMETHING after the fill prefix if any.
            (fill-find-break-point linebeg)))))
 
+;; Like text-properties-at but don't include `composition' property.
+(defun fill-text-properties-at (pos)
+  (let ((l (text-properties-at pos))
+       prop-list)
+    (while l
+      (unless (eq (car l) 'composition)
+       (setq prop-list
+             (cons (car l) (cons (cadr l) prop-list))))
+      (setq l (cddr l)))
+    prop-list))
+
 (defun fill-newline ()
   ;; Replace whitespace here with one newline, then
   ;; indent to left margin.
@@ -541,7 +570,7 @@ The break position will be always after LINEBEG and generally before point."
   (insert ?\n)
   ;; Give newline the properties of the space(s) it replaces
   (set-text-properties (1- (point)) (point)
-                      (text-properties-at (point)))
+                      (fill-text-properties-at (point)))
   (and (looking-at "\\( [ \t]*\\)\\(\\c|\\)?")
        (or (aref (char-category-set (or (char-before (1- (point))) ?\000)) ?|)
           (match-end 2))
@@ -608,7 +637,12 @@ space does not end a sentence, so don't break a line there."
        (oneleft nil))
 
     (beginning-of-line)
-    (setq from (point))
+    ;; We used to round up to whole line, but that prevents us from
+    ;; correctly handling filling of mixed code-and-comment where we do want
+    ;; to fill the comment but not the code.  So only use (point) if it's
+    ;; further than `from', which means that `from' is followed by some
+    ;; number of empty lines.
+    (setq from (max (point) from))
 
     ;; Delete all but one soft newline at end of region.
     ;; And leave TO before that one.
@@ -712,8 +746,16 @@ space does not end a sentence, so don't break a line there."
               (looking-at (regexp-quote prefix))))
     (goto-char (match-end 0))))
 
-(defun fill-paragraph (arg)
-  "Fill paragraph at or after point.  Prefix ARG means justify as well.
+(defun fill-minibuffer-function (arg)
+  "Fill a paragraph in the minibuffer, ignoring the prompt."
+  (save-restriction
+    (narrow-to-region (minibuffer-prompt-end) (point-max))
+    (fill-paragraph arg)))
+
+(defun fill-paragraph (&optional justify region)
+  "Fill paragraph at or after point.
+
+If JUSTIFY is non-nil (interactively, with prefix argument), justify as well.
 If `sentence-end-double-space' is non-nil, then period followed by one
 space does not end a sentence, so don't break a line there.
 the variable `fill-column' controls the width for filling.
@@ -721,65 +763,80 @@ the variable `fill-column' controls the width for filling.
 If `fill-paragraph-function' is non-nil, we call it (passing our
 argument to it), and if it returns non-nil, we simply return its value.
 
-If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling."
+If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling.
+
+Interactively (when `region' is non-nil) in Transient Mark mode when
+the mark is active, call `fill-region' to fill each of the paragraphs
+in the active region."
   (interactive (progn
                 (barf-if-buffer-read-only)
-                (list (if current-prefix-arg 'full))))
-  ;; First try fill-paragraph-function.
-  (or (and fill-paragraph-function
-          (let ((function fill-paragraph-function)
-                ;; If fill-paragraph-function is set, it probably takes care
-                ;; of comments and stuff.  If not, it will have to set
-                ;; fill-paragraph-handle-comment back to t explicitly or
-                ;; return nil.
-                (fill-paragraph-handle-comment nil)
-                fill-paragraph-function)
-            (funcall function arg)))
-      ;; Then try our syntax-aware filling code.
-      (and fill-paragraph-handle-comment
-          ;; Our code only handles \n-terminated comments right now.
-          comment-start (equal comment-end "")
-          (let ((fill-paragraph-handle-comment nil))
-            (fill-comment-paragraph arg)))
-      ;; If it all fails, default to the good ol' text paragraph filling.
-      (let ((before (point))
-           (paragraph-start paragraph-start)
-           ;; Fill prefix used for filling the paragraph.
-           fill-pfx)
-       ;; Try to prevent code sections and comment sections from being
-       ;; filled together.
-       (when (and fill-paragraph-handle-comment comment-start-skip)
-         (setq paragraph-start
-               (concat paragraph-start "\\|[ \t]*\\(?:"
-                       comment-start-skip "\\)")))
-       (save-excursion
-         ;; To make sure the return value of forward-paragraph is meaningful,
-         ;; we have to start from the beginning of line, otherwise skipping
-         ;; past the last few chars of a paragraph-separator would count as
-         ;; a paragraph (and not skipping any chars at EOB would not count
-         ;; as a paragraph even if it is).
-         (move-to-left-margin)
-         (if (not (zerop (forward-paragraph)))
-             ;; There's no paragraph at or after point: give up.
-             (setq fill-pfx "")
-           (let ((end (point))
-                 (beg (progn (backward-paragraph) (point))))
-             (goto-char before)
-             (setq fill-pfx
-                   (if use-hard-newlines
-                       ;; Can't use fill-region-as-paragraph, since this
-                       ;; paragraph may still contain hard newlines.  See
-                       ;; fill-region.
-                       (fill-region beg end arg)
-                     (fill-region-as-paragraph beg end arg))))))
-       fill-pfx)))
+                (list (if current-prefix-arg 'full) t)))
+  (or
+   ;; 1. Fill the region if it is active when called interactively.
+   (and region transient-mark-mode mark-active
+       (not (eq (region-beginning) (region-end)))
+       (or (fill-region (region-beginning) (region-end) justify) t))
+   ;; 2. Try fill-paragraph-function.
+   (and (not (eq fill-paragraph-function t))
+       (or fill-paragraph-function
+           (and (minibufferp (current-buffer))
+                (= 1 (point-min))))
+       (let ((function (or fill-paragraph-function
+                           ;; In the minibuffer, don't count the width
+                           ;; of the prompt.
+                           'fill-minibuffer-function))
+             ;; If fill-paragraph-function is set, it probably takes care
+             ;; of comments and stuff.  If not, it will have to set
+             ;; fill-paragraph-handle-comment back to t explicitly or
+             ;; return nil.
+             (fill-paragraph-handle-comment nil)
+             (fill-paragraph-function t))
+         (funcall function justify)))
+   ;; 3. Try our syntax-aware filling code.
+   (and fill-paragraph-handle-comment
+       ;; Our code only handles \n-terminated comments right now.
+       comment-start (equal comment-end "")
+       (let ((fill-paragraph-handle-comment nil))
+         (fill-comment-paragraph justify)))
+   ;; 4. If it all fails, default to the good ol' text paragraph filling.
+   (let ((before (point))
+        (paragraph-start paragraph-start)
+        ;; Fill prefix used for filling the paragraph.
+        fill-pfx)
+     ;; Try to prevent code sections and comment sections from being
+     ;; filled together.
+     (when (and fill-paragraph-handle-comment comment-start-skip)
+       (setq paragraph-start
+            (concat paragraph-start "\\|[ \t]*\\(?:"
+                    comment-start-skip "\\)")))
+     (save-excursion
+       ;; To make sure the return value of forward-paragraph is meaningful,
+       ;; we have to start from the beginning of line, otherwise skipping
+       ;; past the last few chars of a paragraph-separator would count as
+       ;; a paragraph (and not skipping any chars at EOB would not count
+       ;; as a paragraph even if it is).
+       (move-to-left-margin)
+       (if (not (zerop (forward-paragraph)))
+          ;; There's no paragraph at or after point: give up.
+          (setq fill-pfx "")
+        (let ((end (point))
+              (beg (progn (backward-paragraph) (point))))
+          (goto-char before)
+          (setq fill-pfx
+                (if use-hard-newlines
+                    ;; Can't use fill-region-as-paragraph, since this
+                    ;; paragraph may still contain hard newlines.  See
+                    ;; fill-region.
+                    (fill-region beg end justify)
+                  (fill-region-as-paragraph beg end justify))))))
+     fill-pfx)))
 
 (defun fill-comment-paragraph (&optional justify)
   "Fill current comment.
 If we're not in a comment, just return nil so that the caller
 can take care of filling.  JUSTIFY is used as in `fill-paragraph'."
   (comment-normalize-vars)
-  (let (has-code-and-comment   ; Non-nil if it contains code and a comment.
+  (let (has-code-and-comment ; Non-nil if it contains code and a comment.
        comin comstart)
     ;; Figure out what kind of comment we are looking at.
     (save-excursion
@@ -798,25 +855,31 @@ can take care of filling.  JUSTIFY is used as in `fill-paragraph'."
             (commark
              (comment-string-strip (buffer-substring comstart comin) nil t))
             (comment-re
-             (if (string-match comment-start-skip (concat commark "a"))
-                 (concat "[ \t]*" (regexp-quote commark)
-                         ;; Make sure we only match comments that use
-                         ;; the exact same comment marker.
-                         "[^" (substring commark -1) "]")
-               ;; If the commark needs to be followed by some special
-               ;; set of characters (like @c in TeXinfo), we can't
-               ;; rely just on `commark'.
-               (concat "[ \t]*\\(?:" comment-start-skip "\\)")))
-            (comment-fill-prefix       ; Compute a fill prefix.
+              ;; A regexp more specialized than comment-start-skip, that only
+              ;; matches the current commark rather than any valid commark.
+              ;;
+              ;; The specialized regexp only works for "normal" comment
+              ;; syntax, not for Texinfo's "@c" (which can't be immediately
+              ;; followed by word-chars) or Fortran's "C" (which needs to be
+              ;; at bol), so check that comment-start-skip indeed allows the
+              ;; commark to appear in the middle of the line and followed by
+              ;; word chars.  The choice of "\0" and "a" is mostly arbitrary.
+              (if (string-match comment-start-skip (concat "\0" commark "a"))
+                  (concat "[ \t]*" (regexp-quote commark)
+                          ;; Make sure we only match comments that
+                          ;; use the exact same comment marker.
+                          "[^" (substring commark -1) "]")
+                (concat "[ \t]*\\(?:" comment-start-skip "\\)")))
+             (comment-fill-prefix      ; Compute a fill prefix.
              (save-excursion
                (goto-char comstart)
                (if has-code-and-comment
                    (concat
                     (if (not indent-tabs-mode)
-                        (make-string (current-column) ?\ )
+                        (make-string (current-column) ?\s)
                       (concat
                        (make-string (/ (current-column) tab-width) ?\t)
-                       (make-string (% (current-column) tab-width) ?\ )))
+                       (make-string (% (current-column) tab-width) ?\s)))
                     (buffer-substring (point) comin))
                  (buffer-substring (line-beginning-position) comin))))
             beg end)
@@ -837,12 +900,13 @@ can take care of filling.  JUSTIFY is used as in `fill-paragraph'."
                         (or (comment-search-forward (line-end-position) t)
                             (point)))
                        (looking-at comment-re))
-                     1 2))))
-          ;; Find the beginning of the first line past the region to fill.
-          (save-excursion
-            (while (progn (forward-line 1)
-                          (looking-at comment-re)))
-            (point)))
+                     (progn (setq comstart (point)) 1)
+                   (progn (setq comstart (point)) 2)))))
+            ;; Find the beginning of the first line past the region to fill.
+            (save-excursion
+              (while (progn (forward-line 1)
+                            (looking-at comment-re)))
+              (point)))
            ;; Obey paragraph starters and boundaries within comments.
            (let* ((paragraph-separate
                    ;; Use the default values since they correspond to
@@ -854,7 +918,7 @@ can take care of filling.  JUSTIFY is used as in `fill-paragraph'."
                    (concat paragraph-start "\\|[ \t]*\\(?:"
                            comment-start-skip "\\)\\(?:"
                            (default-value 'paragraph-start) "\\)"))
-                  ;; We used to reply on fill-prefix to break paragraph at
+                  ;; We used to rely on fill-prefix to break paragraph at
                   ;; comment-starter changes, but it did not work for the
                   ;; first line (mixed comment&code).
                   ;; We now use comment-re instead to "manually" make sure
@@ -873,7 +937,7 @@ can take care of filling.  JUSTIFY is used as in `fill-paragraph'."
 
          ;; Find the fill-prefix to use.
          (cond
-          (fill-prefix)                ; Use the user-provided fill prefix.
+          (fill-prefix)          ; Use the user-provided fill prefix.
           ((and adaptive-fill-mode     ; Try adaptive fill mode.
                 (setq fill-prefix (fill-context-prefix beg end))
                 (string-match comment-start-skip fill-prefix)))
@@ -883,7 +947,7 @@ can take care of filling.  JUSTIFY is used as in `fill-paragraph'."
          ;; Don't fill with narrowing.
          (or
           (fill-region-as-paragraph
-           beg end justify nil
+           (max comstart beg) end justify nil
            ;; Don't canonicalize spaces within the code just before
            ;; the comment.
            (save-excursion
@@ -901,13 +965,13 @@ Ordinarily the variable `fill-column' controls the width.
 
 Noninteractively, the third argument JUSTIFY specifies which
 kind of justification to do: `full', `left', `right', `center',
-or `none' (equivalent to nil).  t means handle each paragraph
-as specified by its text properties.
+or `none' (equivalent to nil).  A value of t means handle each
+paragraph as specified by its text properties.
 
-The fourth arg NOSQUEEZE non-nil means to leave
-whitespace other than line breaks untouched, and fifth arg TO-EOP
-non-nil means to keep filling to the end of the paragraph (or next
-hard newline, if variable `use-hard-newlines' is on).
+The fourth arg NOSQUEEZE non-nil means to leave whitespace other
+than line breaks untouched, and fifth arg TO-EOP non-nil means
+to keep filling to the end of the paragraph (or next hard newline,
+if variable `use-hard-newlines' is on).
 
 Return the fill-prefix used for filling the last paragraph.
 
@@ -1111,8 +1175,6 @@ otherwise it is made canonical."
            ncols                       ; new indent point or offset
            (nspaces 0)                 ; number of spaces between words
                                        ; in line (not space characters)
-           fracspace                   ; fractional amount of space to be
-                                       ; added between each words
            (curr-fracspace 0)          ; current fractional space amount
            count)
        (end-of-line)
@@ -1208,7 +1270,7 @@ otherwise it is made canonical."
                         (while (> count 0)
                           (skip-chars-forward " ")
                           (insert-and-inherit
-                           (make-string (/ curr-fracspace nspaces) ?\ ))
+                           (make-string (/ curr-fracspace nspaces) ?\s))
                           (search-forward " " nil t)
                           (setq count (1- count)
                                 curr-fracspace
@@ -1267,8 +1329,8 @@ in the paragraph.
 
 When calling from a program, pass range to fill as first two arguments.
 
-Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
-JUSTIFY to justify paragraphs (prefix arg),
+Optional third and fourth arguments JUSTIFYP and CITATION-REGEXP:
+JUSTIFYP to justify paragraphs (prefix arg).
 When filling a mail message, pass a regexp for CITATION-REGEXP
 which will match the prefix of a line which is a citation marker
 plus whitespace, but no other kind of prefix.
@@ -1297,8 +1359,8 @@ These lines are filled together.
 When calling from a program, pass the range to fill
 as the first two arguments.
 
-Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
-JUSTIFY to justify paragraphs (prefix arg),
+Optional third and fourth arguments JUSTIFY and CITATION-REGEXP:
+JUSTIFY to justify paragraphs (prefix arg).
 When filling a mail message, pass a regexp for CITATION-REGEXP
 which will match the prefix of a line which is a citation marker
 plus whitespace, but no other kind of prefix.
@@ -1321,7 +1383,7 @@ Also, if CITATION-REGEXP is non-nil, don't fill header lines."
              (forward-line 1))))
       (narrow-to-region (point) max)
       ;; Loop over paragraphs.
-      (while (let ((here (point)))
+      (while (progn
               ;; Skip over all paragraph-separating lines
               ;; so as to not include them in any paragraph.
                (while (and (not (eobp))
@@ -1386,6 +1448,7 @@ Also, if CITATION-REGEXP is non-nil, don't fill header lines."
            (fill-region-as-paragraph start (point) justify)
            (if (and (bolp) (not had-newline))
                (delete-char -1))))))))
+
 (defun fill-individual-paragraphs-prefix (citation-regexp)
   (let* ((adaptive-fill-first-line-regexp ".*")
         (just-one-line-prefix
@@ -1429,5 +1492,5 @@ Also, if CITATION-REGEXP is non-nil, don't fill header lines."
        "")
     string))
 
-;;; arch-tag: 727ad455-1161-4fa9-8df5-0f74b179216d
+;; arch-tag: 727ad455-1161-4fa9-8df5-0f74b179216d
 ;;; fill.el ends here