]> code.delx.au - gnu-emacs/blobdiff - lisp/textmodes/fill.el
*** empty log message ***
[gnu-emacs] / lisp / textmodes / fill.el
index b600cd910310c53744a053a8bd9b5bde5880da38..9218c565c97967178f628b3102fb7e16e2385cf6 100644 (file)
@@ -1,7 +1,9 @@
-;;; 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 Free Software Foundation, Inc.
+;; Copyright (C) 1985, 1986, 1992, 1994, 1995, 1996, 1997, 1999, 2001, 2002,
+;;   2003, 2004, 2005, 2006 Free Software Foundation, Inc.
 
+;; Maintainer: FSF
 ;; Keywords: wp
 
 ;; This file is part of GNU Emacs.
@@ -18,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:
 
 
 ;;; Code:
 
+(defgroup fill nil
+  "Indenting and filling text."
+  :link '(custom-manual "(emacs)Filling")
+  :group 'editing)
+
 (defcustom fill-individual-varying-indent nil
   "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
 Non-nil means changing indent doesn't end a paragraph.
@@ -37,45 +44,39 @@ A value of nil means that any change in indentation starts a new paragraph."
   :type 'boolean
   :group 'fill)
 
-(defcustom sentence-end-double-space t
-  "*Non-nil means a single space does not end a sentence.
-
-If you change this, you should also change `sentence-end'.
-See Info node `Sentences'."
-  :type 'boolean
-  :group 'fill)
-
 (defcustom colon-double-space nil
   "*Non-nil means put two spaces after a colon when filling."
   :type 'boolean
   :group 'fill)
-
-(defcustom sentence-end-without-period nil
-  "*Non-nil means a sentence will end without period.
-For example, Thai text ends with double space but without period."
-  :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.")
 
-(defvar enable-kinsoku t
-  "*Non-nil means enable \"kinsoku\" processing on filling paragraph.
+(defvar fill-paragraph-handle-comment t
+  "Non-nil means paragraph filling will try to pay attention to comments.")
+
+(defcustom enable-kinsoku t
+  "*Non-nil means enable \"kinsoku\" processing on filling paragraphs.
 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.")
+See the documentation of `kinsoku' for more information."
+  :type 'boolean
+  :group 'fill)
 
 (defun set-fill-prefix ()
   "Set the fill prefix to the current line up to point.
 Filling expects lines to start with the fill prefix and
 reinserts the fill prefix in each resulting line."
   (interactive)
-  (setq fill-prefix (buffer-substring
-                    (save-excursion (move-to-left-margin) (point))
-                    (point)))
-  (if (equal fill-prefix "")
-      (setq fill-prefix nil))
+  (let ((left-margin-pos (save-excursion (move-to-left-margin) (point))))
+    (if (> (point) left-margin-pos)
+       (progn
+         (setq fill-prefix (buffer-substring left-margin-pos (point)))
+         (if (equal fill-prefix "")
+             (setq fill-prefix nil)))
+      (setq fill-prefix nil)))
   (if fill-prefix
       (message "fill-prefix: \"%s\"" fill-prefix)
     (message "fill-prefix cancelled")))
@@ -85,7 +86,10 @@ reinserts the fill prefix in each resulting line."
   :type 'boolean
   :group 'fill)
 
-(defcustom adaptive-fill-regexp "[ \t]*\\([-|#;>*]+ *\\|(?[0-9]+[.)] *\\)*"
+(defcustom adaptive-fill-regexp
+  ;; Added `!' for doxygen comments starting with `//!' or `/*!'.
+  ;; Added `%' for TeX comments.
+  (purecopy "[ \t]*\\([-!|#%;>*\e,A7\e$,1s"s#sC\e$,2"F\e(B]+[ \t]*\\|(?[0-9]+[.)][ \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
@@ -111,11 +115,14 @@ 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.
-This function is used when `adaptive-fill-regexp' does not match."
-  :type 'function
+  "*Function to call to choose a fill prefix for a paragraph, or nil.
+nil means the function has not determined the fill prefix."
+  :type '(choice (const nil) function)
   :group 'fill)
 
+(defvar fill-indent-according-to-mode nil ;Screws up CC-mode's filling tricks.
+  "Whether or not filling should try to use the major mode's indentation.")
+
 (defun current-fill-column ()
   "Return the fill-column to use for this line.
 The fill-column to use for a buffer is stored in the variable `fill-column',
@@ -130,7 +137,8 @@ number equals or exceeds the local fill-column - right-margin difference."
               (here-col 0)
               (eol (progn (end-of-line) (point)))
               margin fill-col change col)
-         ;; Look separately at each region of line with a different right-margin.
+         ;; Look separately at each region of line with a different
+         ;; right-margin.
          (while (and (setq margin (get-text-property here 'right-margin)
                            fill-col (- fill-column (or margin 0))
                            change (text-property-not-all
@@ -149,42 +157,68 @@ 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")
-  (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 ?\ )
-    (while (and (< (point) end)
-               (re-search-forward "   *" end t))
-      (delete-region
-       (+ (match-beginning 0)
-         ;; Determine number of spaces to leave:
-         (save-excursion
-           (skip-chars-backward " ]})\"'")
-           (cond ((and sentence-end-double-space
-                       (or (memq (preceding-char) '(?. ?? ?!))
-                           (and sentence-end-without-period
-                                (= (char-syntax (preceding-char)) ?w)))) 2)
-                 ((and colon-double-space
-                       (= (preceding-char) ?:))  2)
-                 ((char-equal (preceding-char) ?\n)  0)
-                 (t 1))))
-       (match-end 0)))
-    ;; Make sure sentences ending at end of line get an extra space.
-    ;; loses on split abbrevs ("Mr.\nSmith")
-    (goto-char beg)
-    (while (and (< (point) end)
-               (re-search-forward "[.?!][])}\"']*$" end t))
-      ;; We insert before markers in case a caller such as
-      ;; do-auto-fill has done a save-excursion with point at the end
-      ;; of the line and wants it to stay at the end of the line.
-      (insert-before-markers-and-inherit ? ))))
+  (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 ?\s)
+      (while (and (< (point) end)
+                 (re-search-forward end-spc-re end t))
+       (delete-region
+        (cond
+         ;; `sentence-end' matched and did not match all spaces.
+         ;; I.e. it only matched the number of spaces it needs: drop the rest.
+         ((and (match-end 1) (> (match-end 0) (match-end 1)))  (match-end 1))
+         ;; `sentence-end' matched but with nothing left.  Either that means
+         ;; nothing should be removed, or it means it's the "old-style"
+         ;; sentence-end which matches all it can.  Keep only 2 spaces.
+         ;; We probably don't even need to check `sentence-end-double-space'.
+         ((match-end 1)
+          (min (match-end 0)
+               (+ (if sentence-end-double-space 2 1)
+                  (save-excursion (goto-char (match-end 0))
+                                  (skip-chars-backward " ")
+                                  (point)))))
+         (t ;; It's not an end of sentence.
+          (+ (match-beginning 0)
+             ;; Determine number of spaces to leave:
+             (save-excursion
+               (skip-chars-backward " ]})\"'")
+               (cond ((and sentence-end-double-space
+                           (or (memq (preceding-char) '(?. ?? ?!))
+                               (and sentence-end-without-period
+                                    (= (char-syntax (preceding-char)) ?w)))) 2)
+                     ((and colon-double-space
+                           (= (preceding-char) ?:))  2)
+                     ((char-equal (preceding-char) ?\n)  0)
+                     (t 1))))))
+        (match-end 0))))))
+
+(defun fill-common-string-prefix (s1 s2)
+  "Return the longest common prefix of strings S1 and S2, or nil if none."
+  (let ((cmp (compare-strings s1 nil nil s2 nil nil)))
+    (if (eq cmp t)
+       s1
+      (setq cmp (1- (abs cmp)))
+      (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-prefix' and `adaptive-fill-function'
+This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function'
 and `adaptive-fill-first-line-regexp'.  `paragraph-start' also plays a role;
 we reject a prefix based on a one-line paragraph if that prefix would
 act as a paragraph-separator."
@@ -194,49 +228,46 @@ act as a paragraph-separator."
     (goto-char from)
     (if (eolp) (forward-line 1))
     ;; Move to the second line unless there is just one.
-    (let ((firstline (point))
-         first-line-prefix
+    (move-to-left-margin)
+    (let (first-line-prefix
          ;; Non-nil if we are on the second line.
-         at-second
-         second-line-prefix
-         start)
-      (move-to-left-margin)
-      (setq start (point))
+         second-line-prefix)
       (setq first-line-prefix
-           (cond ((looking-at paragraph-start) nil)
-                 ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
-                  (buffer-substring-no-properties start (match-end 0)))
-                 (adaptive-fill-function (funcall adaptive-fill-function))))
+           ;; 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.
+           ;; ((looking-at paragraph-start) nil)
+           (fill-match-adaptive-prefix))
       (forward-line 1)
-      (if (>= (point) to)
-         (goto-char firstline)
-       (setq at-second t)
-       (move-to-left-margin)
-       (setq start (point))
-       (setq second-line-prefix
-             (cond ((looking-at paragraph-start) nil)
-                   ((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 at-second
-         ;; If we get a fill prefix from the second line,
-         ;; make sure it or something compatible is on the first line too.
-         (and second-line-prefix first-line-prefix
-              ;; If the first line has the second line prefix too, use it.
-              (if (or (string-match (concat "\\`"
-                                            (regexp-quote second-line-prefix)
-                                            "\\(\\'\\|[ \t]\\)")
-                                    first-line-prefix)
-                      ;; If the second line prefix is whitespace, use it.
-                      (string-match "\\`[ \t]+\\'" second-line-prefix))
-                  second-line-prefix
-                ;; If the second line has the first line prefix,
-                ;; plus whitespace, use the part that the first line shares.
-                (if (string-match (concat "\\`"
-                                            (regexp-quote first-line-prefix)
-                                            "[ \t]*\\'")
-                                  second-line-prefix)
-                    first-line-prefix)))
+      (if (< (point) to)
+          (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.
@@ -252,37 +283,104 @@ 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
                                           (concat result "a"))))
                  result)))))))
 
-(defvar fill-nobreak-predicate nil
-  "If non-nil, a predicate for recognizing places not to break a line.
-The predicate is called with no arguments, with point at the place
-to be tested.  If it returns t, fill commands do not break the line there.")
+(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 (concat "[ \t]*\\sw+" "\\(?:" (sentence-end) "\\)"))
+      (save-excursion
+       (skip-chars-backward " \t")
+       (and (/= (skip-syntax-backward "w") 0)
+            (/= (skip-chars-backward " \t") 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.
+This is used in `fill-nobreak-predicate' to prevent breaking lines just
+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]*[])}\e,A;\e,b;\e(B?!;:-]")
+      (save-excursion
+       (skip-chars-backward " \t")
+       (unless (bolp)
+         (backward-char 1)
+         (or (looking-at "[([{\e,A+\e,b+\e(B]")
+             ;; Don't cut right after a single-letter word.
+             (and (memq (preceding-char) '(?\t ?\s))
+                  (eq (char-syntax (following-char)) ?w)))))))
+
+(defcustom fill-nobreak-predicate nil
+  "List of predicates for recognizing places not to break a line.
+The predicates are called with no arguments, with point at the place to
+be tested.  If it returns t, fill commands do not break the line there."
+  :group 'fill
+  :type 'hook
+  :options '(fill-french-nobreak-p fill-single-word-nobreak-p))
+
+(defcustom fill-nobreak-invisible nil
+  "Non-nil means that fill commands do not break lines in invisible text."
+  :type 'boolean
+  :group 'fill)
+
+(defun fill-nobreak-p ()
+  "Return nil if breaking the line at point is allowed.
+Can be customized with the variables `fill-nobreak-predicate'
+and `fill-nobreak-invisible'."
+  (or
+   (and fill-nobreak-invisible (line-move-invisible-p (point)))
+   (unless (bolp)
+    (or
+     ;; Don't break after a period followed by just one space.
+     ;; Move back to the previous place to break.
+     ;; The reason is that if a period ends up at the end of a
+     ;; line, further fills will assume it ends a sentence.
+     ;; If we now know it does not end a sentence, avoid putting
+     ;; it at the end of the line.
+     (and sentence-end-double-space
+         (save-excursion
+           (skip-chars-backward " ")
+           (and (eq (preceding-char) ?.)
+                (looking-at " \\([^ ]\\|$\\)"))))
+     ;; Another approach to the same problem.
+     (save-excursion
+       (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")
+        ;; 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
 ;; require special functions to find line breaking point.
-(let ((alist '((katakana-jisx0201 . kinsoku)
-              (chinese-gb2312 . kinsoku)
-              (japanese-jisx0208 . kinsoku)
-              (japanese-jisx0212 . kinsoku)
-              (chinese-big5-1 . kinsoku)
-              (chinese-big5-2 . kinsoku))))
-  (while alist
-    (put-charset-property (car (car alist)) 'fill-find-break-point-function
-                         (cdr (car alist)))
-    (setq alist (cdr alist))))
+(dolist (pair '((katakana-jisx0201 . kinsoku)
+               (chinese-gb2312 . kinsoku)
+               (japanese-jisx0208 . kinsoku)
+               (japanese-jisx0212 . kinsoku)
+               (chinese-big5-1 . kinsoku)
+               (chinese-big5-2 . kinsoku)))
+  (put-charset-property (car pair) 'fill-find-break-point-function (cdr pair)))
 
 (defun fill-find-break-point (limit)
   "Move point to a proper line breaking position of the current line.
 Don't move back past the buffer position LIMIT.
 
 This function is called when we are going to break the current line
-after or before a non-ascii character.  If the charset of the
+after or before a non-ASCII character.  If the charset of the
 character has the property `fill-find-break-point-function', this
 function calls the property value as a function with one arg LINEBEG.
 If the charset has no such property, do nothing."
@@ -292,13 +390,205 @@ If the charset has no such property, do nothing."
     (if (eq charset 'ascii)
        (setq ch (preceding-char)
              charset (char-charset ch)))
-    (if (eq charset 'ascii)
-       nil
-      (if (eq charset 'composition)
-         (setq charset (char-charset (composite-char-component ch 0)))))
-  (setq func (get-charset-property charset 'fill-find-break-point-function))
-  (if (and func (fboundp func))
-      (funcall func limit))))
+    (if (charsetp charset)
+       (setq func
+             (get-charset-property charset 'fill-find-break-point-function)))
+    (if (and func (fboundp func))
+       (funcall func limit))))
+
+(defun fill-delete-prefix (from to prefix)
+  "Delete the fill prefix from every line except the first.
+The first line may not even have a fill prefix.
+Point is moved to just past the fill prefix on the first line."
+  (let ((fpre (if (and prefix (not (string-match "\\`[ \t]*\\'" prefix)))
+                 (concat "[ \t]*\\("
+                         (replace-regexp-in-string
+                          "[ \t]+" "[ \t]*"
+                          (regexp-quote prefix))
+                         "\\)?[ \t]*")
+               "[ \t]*")))
+    (goto-char from)
+    (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)))
+      (forward-line 1))
+    (goto-char from)
+    (if (looking-at fpre)
+       (goto-char (match-end 0)))
+    (setq from (point))))
+
+;; The `fill-space' property carries the string with which a newline
+;; should be replaced when unbreaking a line (in fill-delete-newlines).
+;; It is added to newline characters by fill-newline when the default
+;; behavior of fill-delete-newlines is not what we want.
+(add-to-list 'text-property-default-nonsticky '(fill-space . t))
+
+(defun fill-delete-newlines (from to justify nosqueeze squeeze-after)
+  (goto-char from)
+  ;; Make sure sentences ending at end of line get an extra space.
+  ;; loses on split abbrevs ("Mr.\nSmith")
+  (let ((eol-double-space-re
+        (cond
+         ((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) "$"))
+         ;; 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 ?\s))
+         (memq (char-after (match-beginning 0))
+               sentence-end-without-space-list)
+         (insert-and-inherit ?\s))))
+
+  (goto-char from)
+  (if enable-multibyte-characters
+      ;; Delete unnecessay newlines surrounded by words.  The
+      ;; character category `|' means that we can break a line
+      ;; at the character.  And, charset property
+      ;; `nospace-between-words' tells how to concatenate
+      ;; words.  If the value is non-nil, never put spaces
+      ;; between words, thus delete a newline between them.
+      ;; If the value is nil, delete a newline only when a
+      ;; character preceding a newline has text property
+      ;; `nospace-between-words'.
+      (while (search-forward "\n" to t)
+       (if (get-text-property (match-beginning 0) 'fill-space)
+           (replace-match (get-text-property (match-beginning 0) 'fill-space))
+         (let ((prev (char-before (match-beginning 0)))
+               (next (following-char)))
+           (if (and (or (aref (char-category-set next) ?|)
+                        (aref (char-category-set prev) ?|))
+                    (or (get-charset-property (char-charset prev)
+                                              'nospace-between-words)
+                        (get-text-property (1- (match-beginning 0))
+                                           'nospace-between-words)))
+               (delete-char -1))))))
+
+  (goto-char from)
+  (skip-chars-forward " \t")
+  ;; Then change all newlines to spaces.
+  (subst-char-in-region from to ?\n ?\s)
+  (if (and nosqueeze (not (eq justify 'full)))
+      nil
+    (canonically-space-region (or squeeze-after (point)) to)
+    ;; Remove trailing whitespace.
+    ;; Maybe canonically-space-region should do that.
+    (goto-char to) (delete-char (- (skip-chars-backward " \t"))))
+  (goto-char from))
+
+(defun fill-move-to-break-point (linebeg)
+  "Move to the position where the line should be broken.
+The break position will be always after LINEBEG and generally before point."
+  ;; If the fill column is before linebeg, move to linebeg.
+  (if (> linebeg (point)) (goto-char linebeg))
+  ;; Move back to the point where we can break the line
+  ;; at.  We break the line between word or after/before
+  ;; the character which has character category `|'.  We
+  ;; search space, \c| followed by a character, or \c|
+  ;; following a character.  If not found, place
+  ;; the point at linebeg.
+  (while
+      (when (re-search-backward "[ \t]\\|\\c|.\\|.\\c|" linebeg 0)
+       ;; In case of space, we place the point at next to
+       ;; the point where the break occurs actually,
+       ;; because we don't want to change the following
+       ;; logic of original Emacs.  In case of \c|, the
+       ;; point is at the place where the break occurs.
+       (forward-char 1)
+       (when (fill-nobreak-p) (skip-chars-backward " \t" linebeg))))
+
+  ;; Move back over the single space between the words.
+  (skip-chars-backward " \t")
+
+  ;; If the left margin and fill prefix by themselves
+  ;; pass the fill-column. or if they are zero
+  ;; but we have no room for even one word,
+  ;; keep at least one word or a character which has
+  ;; category `|' anyway.
+  (if (>= linebeg (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)))
+         ;; Find a breakable point while ignoring the
+         ;; following spaces.
+         (skip-chars-forward " \t")
+         (if (looking-at "\\c|")
+             (forward-char 1)
+           (let ((pos (save-excursion
+                        (skip-chars-forward "^ \n\t")
+                        (point))))
+             (if (re-search-forward "\\c|" pos t)
+                 (forward-char -1)
+               (goto-char pos))))
+         (setq first nil)))
+
+    (if enable-multibyte-characters
+       ;; If we are going to break the line after or
+       ;; before a non-ascii character, we may have to
+       ;; run a special function for the charset of the
+       ;; character to find the correct break point.
+       (if (not (and (eq (charset-after (1- (point))) 'ascii)
+                     (eq (charset-after (point)) 'ascii)))
+           ;; 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.
+  (skip-chars-backward " \t")
+  (insert ?\n)
+  ;; Give newline the properties of the space(s) it replaces
+  (set-text-properties (1- (point)) (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))
+       ;; When refilling later on, this newline would normally not be replaced
+       ;; by a space, so we need to mark it specially to re-install the space
+       ;; when we unfill.
+       (put-text-property (1- (point)) (point) 'fill-space (match-string 1)))
+  ;; If we don't want breaks in invisible text, don't insert
+  ;; an invisible newline.
+  (if fill-nobreak-invisible
+      (remove-text-properties (1- (point)) (point)
+                             '(invisible t)))
+  (if (or fill-prefix
+         (not fill-indent-according-to-mode))
+      (fill-indent-to-left-margin)
+    (indent-according-to-mode))
+  ;; Insert the fill prefix after indentation.
+  (and fill-prefix (not (equal fill-prefix ""))
+       ;; Markers that were after the whitespace are now at point: insert
+       ;; before them so they don't get stuck before the prefix.
+       (insert-before-markers-and-inherit fill-prefix)))
+
+(defun fill-indent-to-left-margin ()
+  "Indent current line to the column given by `current-left-margin'."
+  (let ((beg (point)))
+    (indent-line-to (current-left-margin))
+    (put-text-property beg (point) 'face 'default)))
 
 (defun fill-region-as-paragraph (from to &optional justify
                                      nosqueeze squeeze-after)
@@ -317,6 +607,8 @@ justification.  Fourth arg NOSQUEEZE non-nil means not to make spaces
 between words canonical before filling.  Fifth arg SQUEEZE-AFTER, if non-nil,
 means don't canonicalize spaces before that position.
 
+Return the `fill-prefix' used for filling.
+
 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."
   (interactive (progn
@@ -325,9 +617,6 @@ space does not end a sentence, so don't break a line there."
                       (if current-prefix-arg 'full))))
   (unless (memq justify '(t nil none full center left right))
     (setq justify 'full))
-  ;; Arrange for undoing the fill to restore point.
-  (if (and buffer-undo-list (not (eq buffer-undo-list t)))
-      (setq buffer-undo-list (cons (point) buffer-undo-list)))
 
   ;; Make sure "to" is the endpoint.
   (goto-char (min from to))
@@ -340,7 +629,7 @@ space does not end a sentence, so don't break a line there."
 
     (beginning-of-line)
     (setq from (point))
-  
+
     ;; Delete all but one soft newline at end of region.
     ;; And leave TO before that one.
     (goto-char to)
@@ -351,312 +640,279 @@ space does not end a sentence, so don't break a line there."
          (delete-backward-char 1)
        (backward-char 1)
        (setq oneleft t)))
-    (setq to (point))
-;;;     ;; If there was no newline, and there is text in the paragraph, then
-;;;     ;; create a newline.
-;;;     (if (and (not oneleft) (> to from-plus-indent))
-;;;    (newline))
+    (setq to (copy-marker (point) t))
+    ;; ;; If there was no newline, and there is text in the paragraph, then
+    ;; ;; create a newline.
+    ;; (if (and (not oneleft) (> to from-plus-indent))
+    ;;         (newline))
     (goto-char from-plus-indent))
 
   (if (not (> to (point)))
-      nil ; There is no paragraph, only whitespace: exit now.
+      nil ;; There is no paragraph, only whitespace: exit now.
 
     (or justify (setq justify (current-justification)))
 
     ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
     (let ((fill-prefix fill-prefix))
       ;; Figure out how this paragraph is indented, if desired.
-      (if (and adaptive-fill-mode
-              (or (null fill-prefix) (string= fill-prefix "")))
-         (setq fill-prefix (fill-context-prefix from to)))
+      (when (and adaptive-fill-mode
+                (or (null fill-prefix) (string= fill-prefix "")))
+       (setq fill-prefix (fill-context-prefix from to))
+       ;; Ignore a white-space only fill-prefix
+       ;; if we indent-according-to-mode.
+       (when (and fill-prefix fill-indent-according-to-mode
+                  (string-match "\\`[ \t]*\\'" fill-prefix))
+         (setq fill-prefix nil)))
+
+      (goto-char from)
+      (beginning-of-line)
 
-      (save-restriction
+      (if (not justify)          ; filling disabled: just check indentation
+         (progn
+           (goto-char from)
+           (while (< (point) to)
+             (if (and (not (eolp))
+                      (< (current-indentation) (current-left-margin)))
+                 (fill-indent-to-left-margin))
+             (forward-line 1)))
+
+       (if use-hard-newlines
+           (remove-list-of-text-properties from to '(hard)))
+       ;; Make sure first line is indented (at least) to left margin...
+       (if (or (memq justify '(right center))
+               (< (current-indentation) (current-left-margin)))
+           (fill-indent-to-left-margin))
+       ;; Delete the fill-prefix from every line.
+       (fill-delete-prefix from to fill-prefix)
+       (setq from (point))
+
+       ;; FROM, and point, are now before the text to fill,
+       ;; but after any fill prefix on the first line.
+
+       (fill-delete-newlines from to justify nosqueeze squeeze-after)
+
+       ;; This is the actual filling loop.
        (goto-char from)
-       (beginning-of-line)
-       (narrow-to-region (point) to)
-
-       (if (not justify)           ; filling disabled: just check indentation
-           (progn
-             (goto-char from)
-             (while (not (eobp))
-               (if (and (not (eolp))
-                        (< (current-indentation) (current-left-margin)))
-                   (indent-to-left-margin))
-               (forward-line 1)))
-
-         (if use-hard-newlines
-             (remove-text-properties from (point-max) '(hard nil)))
-         ;; Make sure first line is indented (at least) to left margin...
-         (if (or (memq justify '(right center))
-                 (< (current-indentation) (current-left-margin)))
-             (indent-to-left-margin))
-         ;; Delete the fill prefix from every line except the first.
-         ;; The first line may not even have a fill prefix.
-         (goto-char from)
-         (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
-                          (concat "[ \t]*"
-                                  (regexp-quote fill-prefix)
-                                  "[ \t]*"))))
-           (and fpre
-                (progn
-                  (if (>= (+ (current-left-margin) (length fill-prefix))
-                          (current-fill-column))
-                      (error "fill-prefix too long for specified width"))
-                  (goto-char from)
-                  (forward-line 1)
-                  (while (not (eobp))
-                    (if (looking-at fpre)
-                        (delete-region (point) (match-end 0)))
-                    (forward-line 1))
-                  (goto-char from)
-                  (if (looking-at fpre)
-                      (goto-char (match-end 0)))
-                  (setq from (point)))))
-         ;; Remove indentation from lines other than the first.
-         (beginning-of-line 2)
-         (indent-region (point) (point-max) 0)
-         (goto-char from)
-
-         ;; FROM, and point, are now before the text to fill,
-         ;; but after any fill prefix on the first line.
-
-         ;; Make sure sentences ending at end of line get an extra space.
-         ;; loses on split abbrevs ("Mr.\nSmith")
-         (while (re-search-forward "[.?!][])}\"']*$" nil t)
-           (or (eobp) (insert-and-inherit ?\ )))
-
-         (goto-char from)        
-         (if enable-multibyte-characters
-             ;; Delete unnecessay newlines surrounded by words.  The
-             ;; character category `|' means that we can break a line
-             ;; at the character.  And, charset property
-             ;; `nospace-between-words' tells how to concatenate
-             ;; words.  If the value is non-nil, never put spaces
-             ;; between words, thus delete a newline between them.
-             ;; If the value is nil, delete a newline only when a
-             ;; character preceding a newline has text property
-             ;; `nospace-between-words'.
-             (while (search-forward "\n" nil t)
-               (let ((prev (char-before (match-beginning 0)))
-                     (next (following-char)))
-                 (if (cmpcharp prev)
-                     (setq prev (composite-char-component prev 0)))
-                 (if (cmpcharp next)
-                     (setq next (composite-char-component next 0)))
-                 (if (and (or (aref (char-category-set next) ?|)
-                              (aref (char-category-set prev) ?|))
-                          (or (get-charset-property (char-charset prev)
-                                                    'nospace-between-words)
-                              (get-text-property (1- (match-beginning 0))
-                                                 'nospace-between-words)))
-                     (delete-char -1)))))
-
-         (goto-char from)
-         (skip-chars-forward " \t")
-         ;; Then change all newlines to spaces.
-         (subst-char-in-region from (point-max) ?\n ?\ )
-         (if (and nosqueeze (not (eq justify 'full)))
-             nil
-           (canonically-space-region (or squeeze-after (point)) (point-max))
-           (goto-char (point-max))
-           (delete-horizontal-space)
-           (insert-and-inherit " "))
-         (goto-char (point-min))
-
-         ;; This is the actual filling loop.
-         (let ((prefixcol 0) linebeg)
-           (while (not (eobp))
-             (setq linebeg (point))
-             (move-to-column (1+ (current-fill-column)))
-             (if (eobp)
-                 (or nosqueeze (delete-horizontal-space))
-               ;; Move back to the point where we can break the line
-               ;; at.  We break the line between word or after/before
-               ;; the character which has character category `|'.  We
-               ;; search space, \c| followed by a character, or \c|
-               ;; following a character.  If not found, place
-               ;; the point at linebeg.
-               (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg 0)
-                   ;; In case of space, we place the point at next to
-                   ;; the point where the break occurs acutually,
-                   ;; because we don't want to change the following
-                   ;; logic of original Emacs.  In case of \c|, the
-                   ;; point is at the place where the break occurs.
-                   (forward-char 1))
-               ;; Don't break after a period followed by just one space.
-               ;; Move back to the previous place to break.
-               ;; The reason is that if a period ends up at the end of a line,
-               ;; further fills will assume it ends a sentence.
-               ;; If we now know it does not end a sentence,
-               ;; avoid putting it at the end of the line.
-               (while (or (and sentence-end-double-space
-                               (> (point) (+ linebeg 2))
-                               (eq (preceding-char) ?\ )
-                               (not (eq (following-char) ?\ ))
-                               (eq (char-after (- (point) 2)) ?\.)
-                               (progn (forward-char -2) t))
-                          (and fill-nobreak-predicate
-                               (funcall fill-nobreak-predicate)
-                               (goto-char (match-beginning 0))))
-                 (if (re-search-backward " \\|\\c|.\\|.\\c|" linebeg 0)
-                     (forward-char 1)))
-               ;; If the left margin and fill prefix by themselves
-               ;; pass the fill-column. or if they are zero
-               ;; but we have no room for even one word,
-               ;; keep at least one word or a character which has
-               ;; category `|'anyway .
-               ;; This handles ALL BUT the first line of the paragraph.
-               (if (if (zerop prefixcol)
-                       (save-excursion
-                         (skip-chars-backward " \t" linebeg)
-                         (bolp))
-                     (>= prefixcol (current-column)))
-                   ;; Ok, skip at least one word or one \c| character.
-                   ;; Meanwhile, don't stop at a period followed by one space.
-                   (let ((first t))
-                     (move-to-column prefixcol)
-                     (while (and (not (eobp))
-                                 (or first
-                                     (and (not (bobp))
-                                          sentence-end-double-space
-                                          (save-excursion (forward-char -1)
-                                                          (and (looking-at "\\. ")
-                                                               (not (looking-at "\\.  ")))))
-                                     (and fill-nobreak-predicate
-                                          (funcall fill-nobreak-predicate))))
-                       ;; Find a breakable point while ignoring the
-                       ;; following spaces.
-                       (skip-chars-forward " \t")
-                       (if (looking-at "\\c|")
-                           (forward-char 1)
-                         (let ((pos (save-excursion
-                                      (skip-chars-forward "^ \n\t")
-                                      (point))))
-                           (if (re-search-forward "\\c|" pos t)
-                               (forward-char -1)
-                             (goto-char pos))))
-                       (setq first nil)))
-                 ;; Normally, move back over the single space between the words.
-                 (if (= (preceding-char) ?\ ) (forward-char -1))
-
-                 (if enable-multibyte-characters
-                     ;; If we are going to break the line after or
-                     ;; before a non-ascii character, we may have to
-                     ;; run a special function for the charset of the
-                     ;; character to find the correct break point.
-                     (if (not (and (eq (charset-after (1- (point))) 'ascii)
-                                   (eq (charset-after (point)) 'ascii)))
-                         (fill-find-break-point (+ linebeg
-                                                   (current-left-margin)
-                                                   (length fill-prefix))))))
-
-               ;; If the left margin and fill prefix by themselves
-               ;; pass the fill-column, keep at least one word.
-               ;; This handles the first line of the paragraph.
-               (if (and (zerop prefixcol)
-                        (let ((fill-point (point)) nchars)
-                          (save-excursion
-                            (move-to-left-margin)
-                            (setq nchars (- fill-point (point)))
-                            (or (< nchars 0)
-                                (and fill-prefix
-                                     (< nchars (length fill-prefix))
-                                     (string= (buffer-substring (point) fill-point)
-                                              (substring fill-prefix 0 nchars)))))))
-                   ;; Ok, skip at least one word.  But
-                   ;; don't stop at a period followed by just one space.
-                   (let ((first t))
-                     (while (and (not (eobp))
-                                 (or first
-                                     (and (not (bobp))
-                                          sentence-end-double-space
-                                          (save-excursion (forward-char -1)
-                                                          (and (looking-at "\\. ")
-                                                               (not (looking-at "\\.  ")))))
-                                     (and fill-nobreak-predicate
-                                          (funcall fill-nobreak-predicate))))
-                       ;; Find a breakable point while ignoring the
-                       ;; following spaces.
-                       (skip-chars-forward " \t")
-                       (if (looking-at "\\c|")
-                           (forward-char 1)
-                         (let ((pos (save-excursion
-                                      (skip-chars-forward "^ \n\t")
-                                      (point))))
-                           (if (re-search-forward "\\c|" pos t)
-                               (forward-char -1)
-                             (goto-char pos))))
-                       (setq first nil))))
-               ;; Check again to see if we got to the end of the paragraph.
-               (if (save-excursion (skip-chars-forward " \t") (eobp))
-                   (or nosqueeze (delete-horizontal-space))
-                 ;; Replace whitespace here with one newline, then indent to left
-                 ;; margin.
-                 (skip-chars-backward " \t")
-                 (if (and (= (following-char) ?\ )
-                          (or (aref (char-category-set (preceding-char)) ?|)
-                              (looking-at "[ \t]+\\c|")))
-                     ;; We need one space at end of line so that
-                     ;; further filling won't delete it.  NOTE: We
-                     ;; intentionally leave this one space to
-                     ;; distingush the case that user wants to put
-                     ;; space between \c| characters.
-                     (forward-char 1))
-                 (insert ?\n)
-                 ;; Give newline the properties of the space(s) it replaces
-                 (set-text-properties (1- (point)) (point)
-                                      (text-properties-at (point)))
-                 (indent-to-left-margin)
-                 ;; Insert the fill prefix after indentation.
-                 ;; Set prefixcol so whitespace in the prefix won't get lost.
-                 (and fill-prefix (not (equal fill-prefix ""))
-                      (progn
-                        (insert-and-inherit fill-prefix)
-                        (setq prefixcol (current-column))))))
-             ;; Justify the line just ended, if desired.
-             (if justify
-                (if (save-excursion (skip-chars-forward " \t") (eobp))
-                    (progn
-                      (delete-horizontal-space)
-                      (justify-current-line justify t t))
-                   (forward-line -1)
-                   (justify-current-line justify nil t)
-                   (forward-line 1))))))
-       ;; Leave point after final newline.
-       (goto-char (point-max)))
-      (unless (eobp)
-       (forward-char 1)))))
+       (let (linebeg)
+         (while (< (point) to)
+           (setq linebeg (point))
+           (move-to-column (current-fill-column))
+           (if (when (< (point) to)
+                 ;; Find the position where we'll break the line.
+                 (forward-char 1) ;Use an immediately following space, if any.
+                 (fill-move-to-break-point linebeg)
+                 ;; Check again to see if we got to the end of
+                 ;; the paragraph.
+                 (skip-chars-forward " \t")
+                 (< (point) to))
+               ;; Found a place to cut.
+               (progn
+                 (fill-newline)
+                 (when justify
+                   ;; Justify the line just ended, if desired.
+                   (save-excursion
+                     (forward-line -1)
+                     (justify-current-line justify nil t))))
+
+             (goto-char to)
+             ;; Justify this last line, if desired.
+             (if justify (justify-current-line justify t t))))))
+      ;; Leave point after final newline.
+      (goto-char to)
+      (unless (eobp) (forward-char 1))
+      ;; Return the fill-prefix we used
+      fill-prefix)))
+
+(defsubst skip-line-prefix (prefix)
+  "If point is inside the string PREFIX at the beginning of line, move past it."
+  (when (and prefix
+            (< (- (point) (line-beginning-position)) (length prefix))
+            (save-excursion
+              (beginning-of-line)
+              (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.
+  "Fill paragraph at or after point.  Prefix ARG means 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.
 
 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."
+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."
   (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))
-           ;; If fill-paragraph is called recursively,
-           ;; don't give fill-paragraph-function a second chance.
-           fill-paragraph-function)
+           (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
-         (forward-paragraph)
-         (or (bolp) (newline 1))
-         (let ((end (point))
-               (beg (progn (backward-paragraph) (point))))
-           (goto-char before)
-           (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)))))))
+         ;; 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)))
+
+(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.
+       comin comstart)
+    ;; Figure out what kind of comment we are looking at.
+    (save-excursion
+      (beginning-of-line)
+      (when (setq comstart (comment-search-forward (line-end-position) t))
+       (setq comin (point))
+       (goto-char comstart) (skip-chars-backward " \t")
+       (setq has-code-and-comment (not (bolp)))))
+
+    (if (not comstart)
+       ;; Return nil, so the normal filling will take place.
+       nil
+
+      ;; Narrow to include only the comment, and then fill the region.
+      (let* ((fill-prefix fill-prefix)
+            (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.
+             (save-excursion
+               (goto-char comstart)
+               (if has-code-and-comment
+                   (concat
+                    (if (not indent-tabs-mode)
+                        (make-string (current-column) ?\s)
+                      (concat
+                       (make-string (/ (current-column) tab-width) ?\t)
+                       (make-string (% (current-column) tab-width) ?\s)))
+                    (buffer-substring (point) comin))
+                 (buffer-substring (line-beginning-position) comin))))
+            beg end)
+       (save-excursion
+         (save-restriction
+           (beginning-of-line)
+           (narrow-to-region
+            ;; Find the first line we should include in the region to fill.
+            (if has-code-and-comment
+                (line-beginning-position)
+              (save-excursion
+                (while (and (zerop (forward-line -1))
+                            (looking-at comment-re)))
+                ;; We may have gone too far.  Go forward again.
+                (line-beginning-position
+                 (if (progn
+                       (goto-char
+                        (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)))
+           ;; Obey paragraph starters and boundaries within comments.
+           (let* ((paragraph-separate
+                   ;; Use the default values since they correspond to
+                   ;; the values to use for plain text.
+                   (concat paragraph-separate "\\|[ \t]*\\(?:"
+                           comment-start-skip "\\)\\(?:"
+                           (default-value 'paragraph-separate) "\\)"))
+                  (paragraph-start
+                   (concat paragraph-start "\\|[ \t]*\\(?:"
+                           comment-start-skip "\\)\\(?:"
+                           (default-value 'paragraph-start) "\\)"))
+                  ;; We used to reply 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
+                  ;; we treat comment-marker changes as paragraph boundaries.
+                  ;; (paragraph-ignore-fill-prefix nil)
+                  ;; (fill-prefix comment-fill-prefix)
+                  (after-line (if has-code-and-comment
+                                  (line-beginning-position 2))))
+             (setq end (progn (forward-paragraph) (point)))
+             ;; If this comment starts on a line with code,
+             ;; include that line in the filling.
+             (setq beg (progn (backward-paragraph)
+                              (if (eq (point) after-line)
+                                  (forward-line -1))
+                              (point)))))
+
+         ;; Find the fill-prefix to use.
+         (cond
+          (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)))
+          (t
+           (setq fill-prefix comment-fill-prefix)))
+
+         ;; Don't fill with narrowing.
+         (or
+          (fill-region-as-paragraph
+           beg end justify nil
+           ;; Don't canonicalize spaces within the code just before
+           ;; the comment.
+           (save-excursion
+             (goto-char beg)
+             (if (looking-at fill-prefix)
+                 nil
+               (re-search-forward comment-start-skip))))
+          ;; Make sure we don't return nil.
+          t))))))
 
 (defun fill-region (from to &optional justify nosqueeze to-eop)
   "Fill each of the paragraphs in the region.
@@ -671,7 +927,9 @@ 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 `use-hard-newlines' is on).
+hard newline, if variable `use-hard-newlines' is on).
+
+Return the fill-prefix used for filling the last paragraph.
 
 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."
@@ -681,46 +939,45 @@ space does not end a sentence, so don't break a line there."
                       (if current-prefix-arg 'full))))
   (unless (memq justify '(t nil none full center left right))
     (setq justify 'full))
-  (let (end beg)
-    (save-restriction
-      (goto-char (max from to))
-      (if to-eop
-         (progn (skip-chars-backward "\n")
-                (forward-paragraph)))
-      (setq end (point))
-      (goto-char (setq beg (min from to)))
-      (beginning-of-line)
-      (narrow-to-region (point) end)
-      (while (not (eobp))
-       (let ((initial (point))
-             end)
-         ;; If using hard newlines, break at every one for filling
-         ;; purposes rather than using paragraph breaks. 
-         (if use-hard-newlines
-             (progn 
-               (while (and (setq end (text-property-any (point) (point-max)
-                                                        'hard t))
-                           (not (= ?\n (char-after end)))
-                           (not (= end (point-max))))
-                 (goto-char (1+ end)))
-               (setq end (if end (min (point-max) (1+ end)) (point-max)))
-               (goto-char initial))
-           (forward-paragraph 1)
-           (setq end (point))
-           (forward-paragraph -1))
-         (if (< (point) beg)
-             (goto-char beg))
-         (if (>= (point) initial)
-             (fill-region-as-paragraph (point) end justify nosqueeze)
-           (goto-char end)))))))
+  (let (max beg fill-pfx)
+    (goto-char (max from to))
+    (when to-eop
+      (skip-chars-backward "\n")
+      (forward-paragraph))
+    (setq max (copy-marker (point) t))
+    (goto-char (setq beg (min from to)))
+    (beginning-of-line)
+    (while (< (point) max)
+      (let ((initial (point))
+           end)
+       ;; If using hard newlines, break at every one for filling
+       ;; purposes rather than using paragraph breaks.
+       (if use-hard-newlines
+           (progn
+             (while (and (setq end (text-property-any (point) max
+                                                      'hard t))
+                         (not (= ?\n (char-after end)))
+                         (not (>= end max)))
+               (goto-char (1+ end)))
+             (setq end (if end (min max (1+ end)) max))
+             (goto-char initial))
+         (forward-paragraph 1)
+         (setq end (min max (point)))
+         (forward-paragraph -1))
+       (if (< (point) beg)
+           (goto-char beg))
+       (if (>= (point) initial)
+           (setq fill-pfx
+                 (fill-region-as-paragraph (point) end justify nosqueeze))
+         (goto-char end))))
+    fill-pfx))
 
 \f
 (defcustom default-justification 'left
   "*Method of justifying text not otherwise specified.
 Possible values are `left', `right', `full', `center', or `none'.
 The requested kind of justification is done whenever lines are filled.
-The `justification' text-property  can locally override this variable.
-This variable automatically becomes buffer-local when set in any fashion."
+The `justification' text-property can locally override this variable."
   :type '(choice (const left)
                 (const right)
                 (const full)
@@ -734,9 +991,9 @@ This variable automatically becomes buffer-local when set in any fashion."
 This returns the value of the text-property `justification',
 or the variable `default-justification' if there is no text-property.
 However, it returns nil rather than `none' to mean \"don't justify\"."
-  (let ((j (or (get-text-property 
+  (let ((j (or (get-text-property
                ;; Make sure we're looking at paragraph body.
-               (save-excursion (skip-chars-forward " \t") 
+               (save-excursion (skip-chars-forward " \t")
                                (if (and (eobp) (not (bobp)))
                                    (1- (point)) (point)))
                'justification)
@@ -745,15 +1002,17 @@ However, it returns nil rather than `none' to mean \"don't justify\"."
        nil
       j)))
 
-(defun set-justification (begin end value &optional whole-par)
-  "Set the region's justification style.
-The kind of justification to use is prompted for.
+(defun set-justification (begin end style &optional whole-par)
+  "Set the region's justification style to STYLE.
+This commands prompts for the kind of justification to use.
 If the mark is not active, this command operates on the current paragraph.
-If the mark is active, the region is used.  However, if the beginning and end
-of the region are not at paragraph breaks, they are moved to the beginning and
-end of the paragraphs they are in.
-If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
-breaks.
+If the mark is active, it operates on the region.  However, if the
+beginning and end of the region are not at paragraph breaks, they are
+moved to the beginning and end \(respectively) of the paragraphs they
+are in.
+
+If variable `use-hard-newlines' is true, all hard newlines are
+taken to be paragraph breaks.
 
 When calling from a program, operates just on region between BEGIN and END,
 unless optional fourth arg WHOLE-PAR is non-nil.  In that case bounds are
@@ -772,7 +1031,7 @@ extended to include entire paragraphs as in the interactive command."
     (save-restriction
       (if whole-par
          (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
-               (paragraph-ignore-fill-prefix (if use-hard-newlines t 
+               (paragraph-ignore-fill-prefix (if use-hard-newlines t
                                                paragraph-ignore-fill-prefix)))
            (goto-char begin)
            (while (and (bolp) (not (eobp))) (forward-char 1))
@@ -785,7 +1044,7 @@ extended to include entire paragraphs as in the interactive command."
 
       (narrow-to-region (point-min) end)
       (unjustify-region begin (point-max))
-      (put-text-property begin (point-max) 'justification value)
+      (put-text-property begin (point-max) 'justification style)
       (fill-region begin (point-max) nil t))))
 
 (defun set-justification-none (b e)
@@ -797,6 +1056,7 @@ If the mark is not active, this applies to the current paragraph."
 
 (defun set-justification-left (b e)
   "Make paragraphs in the region left-justified.
+This means they are flush at the left margin and ragged on the right.
 This is usually the default, but see the variable `default-justification'.
 If the mark is not active, this applies to the current paragraph."
   (interactive (list (if mark-active (region-beginning) (point))
@@ -804,15 +1064,15 @@ If the mark is not active, this applies to the current paragraph."
   (set-justification b e 'left t))
 
 (defun set-justification-right (b e)
-  "Make paragraphs in the region right-justified:
-Flush at the right margin and ragged on the left.
+  "Make paragraphs in the region right-justified.
+This means they are flush at the right margin and ragged on the left.
 If the mark is not active, this applies to the current paragraph."
   (interactive (list (if mark-active (region-beginning) (point))
                     (if mark-active (region-end) (point))))
   (set-justification b e 'right t))
 
 (defun set-justification-full (b e)
-  "Make paragraphs in the region fully justified:
+  "Make paragraphs in the region fully justified.
 This makes lines flush on both margins by inserting spaces between words.
 If the mark is not active, this applies to the current paragraph."
   (interactive (list (if mark-active (region-beginning) (point))
@@ -828,7 +1088,7 @@ If the mark is not active, this applies to the current paragraph."
 
 ;; A line has up to six parts:
 ;;
-;;           >>>                    hello.                    
+;;           >>>                    hello.
 ;; [Indent-1][FP][    Indent-2     ][text][trailing whitespace][newline]
 ;;
 ;; "Indent-1" is the left-margin indentation; normally it ends at column
@@ -840,7 +1100,7 @@ If the mark is not active, this applies to the current paragraph."
 ;; Trailing whitespace is not counted as part of the line length when
 ;; center- or right-justifying.
 ;;
-;; All parts of the line are optional, although the final newline can 
+;; All parts of the line are optional, although the final newline can
 ;;     only be missing on the last line of the buffer.
 
 (defun justify-current-line (&optional how eop nosqueeze)
@@ -848,7 +1108,7 @@ If the mark is not active, this applies to the current paragraph."
 Normally does full justification: adds spaces to the line to make it end at
 the column given by `current-fill-column'.
 Optional first argument HOW specifies alternate type of justification:
-it can be `left', `right', `full', `center', or `none'.  
+it can be `left', `right', `full', `center', or `none'.
 If HOW is t, will justify however the `current-justification' function says to.
 If HOW is nil or missing, full justification is done by default.
 Second arg EOP non-nil means that this is the last line of the paragraph, so
@@ -871,13 +1131,11 @@ 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)
        ;; Check if this is the last line of the paragraph.
-       (if (and use-hard-newlines (null eop) 
+       (if (and use-hard-newlines (null eop)
                 (get-text-property (point) 'hard))
            (setq eop t))
        (skip-chars-backward " \t")
@@ -891,14 +1149,14 @@ otherwise it is made canonical."
          (beginning-of-line)
          (skip-chars-forward " \t")
          ;; Skip over fill-prefix.
-         (if (and fill-prefix 
+         (if (and fill-prefix
                   (not (string-equal fill-prefix ""))
                   (equal fill-prefix
-                         (buffer-substring 
+                         (buffer-substring
                           (point) (min (point-max) (+ (length fill-prefix)
                                                       (point))))))
              (forward-char (length fill-prefix))
-           (if (and adaptive-fill-mode 
+           (if (and adaptive-fill-mode
                     (looking-at adaptive-fill-regexp))
                (goto-char (match-end 0))))
          (setq fp-end (point))
@@ -910,11 +1168,11 @@ otherwise it is made canonical."
          (setq endcol (current-column))
 
          ;; HOW can't be null or left--we would have exited already
-         (cond ((eq 'right how) 
+         (cond ((eq 'right how)
                 (setq ncols (- fc endcol))
                 (if (< ncols 0)
                     ;; Need to remove some indentation
-                    (delete-region 
+                    (delete-region
                      (progn (goto-char fp-end)
                             (if (< (current-column) (+ indent ncols))
                                 (move-to-column (+ indent ncols) t))
@@ -924,7 +1182,7 @@ otherwise it is made canonical."
                   (goto-char beg)
                   (indent-to (+ indent ncols))
                   ;; If point was at beginning of text, keep it there.
-                  (if (= beg pos) 
+                  (if (= beg pos)
                       (move-marker pos (point)))))
 
                ((eq 'center how)
@@ -968,7 +1226,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
@@ -988,16 +1246,15 @@ extra spaces between words.  It does nothing in other justification modes."
          ((eq  nil  justify) nil)
          ((eq 'full justify)           ; full justify: remove extra spaces
           (beginning-of-line-text)
-          (canonically-space-region
-           (point) (save-excursion (end-of-line) (point))))
+          (canonically-space-region (point) (line-end-position)))
          ((memq justify '(center right))
           (save-excursion
             (move-to-left-margin nil t)
             ;; Position ourselves after any fill-prefix.
-            (if (and fill-prefix 
+            (if (and fill-prefix
                      (not (string-equal fill-prefix ""))
                      (equal fill-prefix
-                            (buffer-substring 
+                            (buffer-substring
                              (point) (min (point-max) (+ (length fill-prefix)
                                                          (point))))))
                 (forward-char (length fill-prefix)))
@@ -1007,7 +1264,7 @@ extra spaces between words.  It does nothing in other justification modes."
 (defun unjustify-region (&optional begin end)
   "Remove justification whitespace from region.
 For centered or right-justified regions, this function removes any indentation
-past the left margin from each line.  For full-justified lines, it removes 
+past the left margin from each line.  For full-justified lines, it removes
 extra spaces between words.  It does nothing in other justification modes.
 Arguments BEGIN and END are optional; default is the whole buffer."
   (save-excursion
@@ -1028,12 +1285,12 @@ 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.
-Also, if CITATION-REGEXP is non-nil,  don't fill header lines."
+Also, if CITATION-REGEXP is non-nil, don't fill header lines."
   (interactive (progn
                 (barf-if-buffer-read-only)
                 (list (region-beginning) (region-end)
@@ -1043,7 +1300,7 @@ Also, if CITATION-REGEXP is non-nil,  don't fill header lines."
 
 (defun fill-individual-paragraphs (min max &optional justify citation-regexp)
   "Fill paragraphs of uniform indentation within the region.
-This command divides the region into \"paragraphs\", 
+This command divides the region into \"paragraphs\",
 treating every change in indentation level or prefix as a paragraph boundary,
 then fills each paragraph using its indentation level as the fill prefix.
 
@@ -1063,7 +1320,7 @@ 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.
-Also, if CITATION-REGEXP is non-nil,  don't fill header lines."
+Also, if CITATION-REGEXP is non-nil, don't fill header lines."
   (interactive (progn
                 (barf-if-buffer-read-only)
                 (list (region-beginning) (region-end)
@@ -1082,7 +1339,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))
@@ -1102,7 +1359,8 @@ Also, if CITATION-REGEXP is non-nil,  don't fill header lines."
                   (if (not (and fill-prefix
                                 (looking-at fill-prefix-regexp)))
                       (setq fill-prefix
-                            (fill-individual-paragraphs-prefix citation-regexp)
+                            (fill-individual-paragraphs-prefix
+                             citation-regexp)
                             fill-prefix-regexp (regexp-quote fill-prefix)))
                   (forward-line 1)
                   (if (bolp)
@@ -1114,81 +1372,80 @@ Also, if CITATION-REGEXP is non-nil,  don't fill header lines."
                        (if fill-individual-varying-indent
                            ;; If this line is a separator line, with or
                            ;; without prefix, end the paragraph.
-                           (and 
+                           (and
                             (not (looking-at paragraph-separate))
                             (save-excursion
                               (not (and (looking-at fill-prefix-regexp)
-                                        (progn (forward-char (length fill-prefix))
-                                               (looking-at paragraph-separate))))))
+                                        (progn (forward-char
+                                                (length fill-prefix))
+                                               (looking-at
+                                                paragraph-separate))))))
                          ;; If this line has more or less indent
                          ;; than the fill prefix wants, end the paragraph.
                          (and (looking-at fill-prefix-regexp)
+                              ;; If fill prefix is shorter than a new
+                              ;; fill prefix computed here, end paragraph.
+                              (let ((this-line-fill-prefix
+                                     (fill-individual-paragraphs-prefix
+                                      citation-regexp)))
+                                (>= (length fill-prefix)
+                                    (length this-line-fill-prefix)))
                               (save-excursion
-                                (not (progn (forward-char (length fill-prefix))
+                                (not (progn (forward-char
+                                             (length fill-prefix))
                                             (or (looking-at "[ \t]")
                                                 (looking-at paragraph-separate)
-                                                (looking-at paragraph-start))))))))))
+                                                (looking-at paragraph-start)))))
+                              (not (and (equal fill-prefix "")
+                                        citation-regexp
+                                        (looking-at citation-regexp))))))))
          ;; Fill this paragraph, but don't add a newline at the end.
          (let ((had-newline (bolp)))
            (fill-region-as-paragraph start (point) justify)
            (if (and (bolp) (not had-newline))
                (delete-char -1))))))))
-
 (defun fill-individual-paragraphs-prefix (citation-regexp)
-  (or (let ((adaptive-fill-first-line-regexp "")
-           just-one-line-prefix
-           two-lines-prefix
-           one-line-citation-part
-           two-lines-citation-part
-           adjusted-two-lines-citation-part)
-       (setq just-one-line-prefix
-             (fill-context-prefix
-              (point)
-              (save-excursion (forward-line 1)
-                              (point))))
-       (setq two-lines-prefix
-             (fill-context-prefix
-              (point)
-              (save-excursion (forward-line 2)
-                              (point))))
-       (when just-one-line-prefix
-         (setq one-line-citation-part
-               (if citation-regexp
-                   (fill-individual-paragraphs-citation just-one-line-prefix
-                                                        citation-regexp)
-                 just-one-line-prefix)))
-       (when two-lines-prefix
-         (setq two-lines-citation-part
-               (if citation-regexp
-                   (fill-individual-paragraphs-citation two-lines-prefix
-                                                        citation-regexp)
-                 just-one-line-prefix))
-         (or two-lines-citation-part (setq two-lines-citation-part ""))
-         (setq adjusted-two-lines-citation-part
-               (substring two-lines-citation-part 0
-                          (string-match "[ \t]*\\'"
-                                        two-lines-citation-part))))
+  (let* ((adaptive-fill-first-line-regexp ".*")
+        (just-one-line-prefix
+         ;; Accept any prefix rather than just the ones matched by
+         ;; adaptive-fill-first-line-regexp.
+         (fill-context-prefix (point) (line-beginning-position 2)))
+        (two-lines-prefix
+         (fill-context-prefix (point) (line-beginning-position 3))))
+    (if (not just-one-line-prefix)
+       (buffer-substring
+        (point) (save-excursion (skip-chars-forward " \t") (point)))
        ;; See if the citation part of JUST-ONE-LINE-PREFIX
        ;; is the same as that of TWO-LINES-PREFIX,
        ;; except perhaps with longer whitespace.
-       (if (and just-one-line-prefix
-                two-lines-prefix
+      (if (and just-one-line-prefix two-lines-prefix
+              (let* ((one-line-citation-part
+                      (fill-individual-paragraphs-citation
+                       just-one-line-prefix citation-regexp))
+                     (two-lines-citation-part
+                      (fill-individual-paragraphs-citation
+                       two-lines-prefix citation-regexp))
+                     (adjusted-two-lines-citation-part
+                      (substring two-lines-citation-part 0
+                                 (string-match "[ \t]*\\'"
+                                               two-lines-citation-part))))
+                (and
                 (string-match (concat "\\`"
-                                      (regexp-quote adjusted-two-lines-citation-part)
+                                      (regexp-quote
+                                       adjusted-two-lines-citation-part)
                                       "[ \t]*\\'")
                               one-line-citation-part)
                 (>= (string-width one-line-citation-part)
-                    (string-width two-lines-citation-part)))
+                     (string-width two-lines-citation-part)))))
            two-lines-prefix
-         just-one-line-prefix))
-      (buffer-substring 
-       (point)
-       (save-excursion (skip-chars-forward " \t")
-                      (point)))))
+       just-one-line-prefix))))
 
 (defun fill-individual-paragraphs-citation (string citation-regexp)
-  (string-match citation-regexp
-               string)
-  (match-string 0 string))  
+  (if citation-regexp
+      (if (string-match citation-regexp string)
+         (match-string 0 string)
+       "")
+    string))
 
+;; arch-tag: 727ad455-1161-4fa9-8df5-0f74b179216d
 ;;; fill.el ends here