Commit 1ddad36e authored by Stefan Monnier's avatar Stefan Monnier
Browse files

(fill-paragraph): Consider a comment-start as

a paragraph starter when filling non-comments.
(fill-comment-paragraph): Use comment-search-forward.
Recognize paragraph starters and separators within comments.
Use adaptive-fill-prefix when it works.  Don't narrow while doing the fill.
parent b2b41304
2002-11-07 Stefan Monnier <monnier@cs.yale.edu> 2002-11-07 Stefan Monnier <monnier@cs.yale.edu>
   
* textmodes/fill.el (fill-paragraph): Consider a comment-start as
a paragraph starter when filling non-comments.
(fill-comment-paragraph): Use comment-search-forward.
Recognize paragraph starters and separators within comments.
Use adaptive-fill-prefix when it works. Don't narrow while doing the fill.
* progmodes/compile.el (grep-default-command): New fun. * progmodes/compile.el (grep-default-command): New fun.
(grep): Use it. (grep): Use it.
(compilation-menu-map): New var. (compilation-menu-map): New var.
......
...@@ -704,14 +704,21 @@ If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling." ...@@ -704,14 +704,21 @@ If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling."
(funcall function arg))) (funcall function arg)))
;; Then try our syntax-aware filling code. ;; Then try our syntax-aware filling code.
(and fill-paragraph-handle-comment (and fill-paragraph-handle-comment
;; Our code only handle \n-terminated comments right now. ;; Our code only handles \n-terminated comments right now.
comment-start comment-start-skip (equal comment-end "") comment-start comment-start-skip (equal comment-end "")
(let ((fill-paragraph-handle-comment nil)) (let ((fill-paragraph-handle-comment nil))
(fill-comment-paragraph arg))) (fill-comment-paragraph arg)))
;; If it all fails, default to the good ol' text paragraph filling. ;; If it all fails, default to the good ol' text paragraph filling.
(let ((before (point)) (let ((before (point))
(paragraph-start paragraph-start)
;; Fill prefix used for filling the paragraph. ;; Fill prefix used for filling the paragraph.
fill-pfx) 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 (save-excursion
;; To make sure the return value of forward-paragraph is meaningful, ;; To make sure the return value of forward-paragraph is meaningful,
;; we have to start from the beginning of line, otherwise skipping ;; we have to start from the beginning of line, otherwise skipping
...@@ -741,86 +748,103 @@ If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling." ...@@ -741,86 +748,103 @@ If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling."
(defun fill-comment-paragraph (&optional justify) (defun fill-comment-paragraph (&optional justify)
"Fill current comment. "Fill current comment.
If we're not in a comment, just return nil so that the caller If we're not in a comment, just return nil so that the caller
can take care of filling." can take care of filling. JUSTIFY is used as in `fill-paragraph'."
(let ((comment-re (concat "[ \t]*" comment-start-skip)) (comment-normalize-vars)
has-comment ; Non-nil if the current line contains a comment. (let (has-code-and-comment ; Non-nil if it contains code and a comment.
has-code-and-comment ; Non-nil if it contains code and a comment. comin comstart)
comment-fill-prefix) ; If has-comment, the appropriate fill-prefix.
;; Figure out what kind of comment we are looking at. ;; Figure out what kind of comment we are looking at.
(save-excursion (save-excursion
(beginning-of-line) (beginning-of-line)
(cond (when (setq comstart (comment-search-forward (line-end-position) t))
(setq comin (point))
;; A line with nothing but a comment on it? (goto-char comstart) (skip-chars-backward " \t")
((looking-at comment-re) (setq has-code-and-comment (not (bolp)))))
(setq has-comment t
comment-fill-prefix (match-string 0))) (if (not comstart)
;; A line with some code, followed by a comment? Remember that the
;; semi which starts the comment shouldn't be part of a string or
;; character.
((let ((state (syntax-ppss (line-end-position))))
(when (nth 4 state)
(goto-char (nth 8 state))
(save-restriction
;; comment-start-skip sometimes tries to count \ quoting from ^.
(narrow-to-region (point) (point-max))
(looking-at comment-start-skip))))
(setq has-comment t has-code-and-comment t)
(setq comment-fill-prefix
(concat (make-string (/ (current-column) tab-width) ?\t)
(make-string (% (current-column) tab-width) ?\ )
(match-string 0))))))
(if (not has-comment)
;; Return nil, so the normal filling will take place. ;; Return nil, so the normal filling will take place.
nil nil
;; Narrow to include only the comment, and then fill the region. ;; Narrow to include only the comment, and then fill the region.
(save-excursion (let* ((fill-prefix fill-prefix)
(save-restriction (comment-re (concat "[ \t]*\\(?:" comment-start-skip "\\)"))
(beginning-of-line) (comment-fill-prefix ; Compute a fill prefix.
(narrow-to-region (save-excursion
;; Find the first line we should include in the region to fill. (goto-char comstart)
(save-excursion (if has-code-and-comment
(while (and (zerop (forward-line -1)) (concat (make-string (/ (current-column) tab-width) ?\t)
(looking-at comment-re))) (make-string (% (current-column) tab-width) ?\ )
;; We may have gone too far. Go forward again. (buffer-substring (point) comin))
(or (looking-at (concat ".*" comment-start-skip)) (buffer-substring (line-beginning-position) comin))))
(forward-line 1)) beg end)
(point)) (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 (looking-at (concat ".*" comment-start-skip))
1 2))))
;; Find the beginning of the first line past the region to fill. ;; Find the beginning of the first line past the region to fill.
(save-excursion (save-excursion
(while (progn (forward-line 1) (while (progn (forward-line 1)
(looking-at comment-re))) (looking-at comment-re)))
(point))) (point)))
;; Obey paragraph starters and boundaries within comments.
;; Lines with only semicolons on them can be paragraph boundaries. (let* ((paragraph-separate
(let* ((paragraph-separate ;; Use the default values since they correspond to
(concat paragraph-start "\\|" comment-re "$")) ;; the values to use for plain text.
(paragraph-ignore-fill-prefix nil) (concat paragraph-separate "\\|[ \t]*\\(?:"
(fill-prefix comment-fill-prefix) comment-start-skip "\\)\\(?:"
(after-line (if has-code-and-comment (default-value 'paragraph-separate) "\\)"))
(line-beginning-position 2))) (paragraph-start
(end (progn (concat paragraph-start "\\|[ \t]*\\(?:"
(forward-paragraph) comment-start-skip "\\)\\(?:"
(or (bolp) (newline 1)) (default-value 'paragraph-start) "\\)"))
(point))) (paragraph-ignore-fill-prefix nil)
;; If this comment starts on a line with code, (fill-prefix comment-fill-prefix)
;; include that like in the filling. (after-line (if has-code-and-comment
(beg (progn (backward-paragraph) (line-beginning-position 2)))
(if (eq (point) after-line) )
(forward-line -1)) (setq end (progn
(point)))) (forward-paragraph)
(fill-region-as-paragraph beg end (or (bolp) (newline 1))
justify nil (point)))
(save-excursion ;; If this comment starts on a line with code,
(goto-char beg) ;; include that line in the filling.
(if (looking-at fill-prefix) (setq beg (progn (backward-paragraph)
nil (if (eq (point) after-line)
(re-search-forward comment-start-skip)))) (forward-line -1))
t)))))) (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) (defun fill-region (from to &optional justify nosqueeze to-eop)
"Fill each of the paragraphs in the region. "Fill each of the paragraphs in the region.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment