Skip to content

Commit

Permalink
Calculate beginning/end of commented paragraphs.
Browse files Browse the repository at this point in the history
Used for filling and auto-filling commented paragraphs.  
Auto-filled paragraphs were not using the correct line to calculate 
hanging indent.  The `idlwave-commented-paragraph-beg-end' function
now finds the correct commented paragraph boundaries.
  • Loading branch information
jdtsmith committed Aug 3, 2010
1 parent 0a75576 commit 985427d
Showing 1 changed file with 62 additions and 51 deletions.
113 changes: 62 additions & 51 deletions idlwave.el
Expand Up @@ -3286,6 +3286,60 @@ If successful, leaves point after the match, otherwise, does not move point."
(if (not found) (goto-char here)) (if (not found) (goto-char here))
found)) found))


(defun idlwave-commented-paragraph-beg-end ()
"Find and return the beginning and end position of a commented paragraph.
End is calculated as distance from end of buffer, to accommodate
additions from filling."
(let (pre diff fill-prefix-reg bcl start end)
(beginning-of-line)
(setq bcl (point))
(re-search-forward
(concat "^[ \t]*" comment-start "+")
(save-excursion (end-of-line) (point))
t)
;; Get the comment leader on the line and its length
(setq pre (current-column))
;; the comment leader is the indentation plus exactly the
;; number of consecutive ";".
(setq fill-prefix-reg
(concat
(setq fill-prefix
(regexp-quote
(buffer-substring (save-excursion
(beginning-of-line) (point))
(point))))
"[^;]"))

;; Mark the beginning and end of the paragraph
(goto-char bcl)
(while (and (looking-at fill-prefix-reg)
(not (looking-at paragraph-separate))
(not (bobp)))
(forward-line -1))
;; Move to first line of paragraph
(if (and (/= (point) bcl) (not (bobp)))
(forward-line 1))
(setq start (point))
(goto-char bcl)
(while (and (looking-at fill-prefix-reg)
(not (looking-at paragraph-separate))
(not (eobp)))
(forward-line 1))
(beginning-of-line)
(if (or (not (looking-at fill-prefix-reg))
(looking-at paragraph-separate))
(forward-line -1))
(end-of-line)
;; if at end of buffer add a newline (need this because
;; fill-region needs END to be at the beginning of line after
;; the paragraph or it will add a line).
(if (eobp)
(progn (insert ?\n) (backward-char 1)))
;; Set END to the beginning of line after the paragraph
;; N.B. END is calculated as distance from end of buffer
(setq end (- (point-max) (point) 1))
(list start end pre)))

(defun idlwave-fill-paragraph (&optional nohang) (defun idlwave-fill-paragraph (&optional nohang)
"Fill paragraphs in comments. "Fill paragraphs in comments.
A paragraph is made up of all contiguous lines having the same comment A paragraph is made up of all contiguous lines having the same comment
Expand All @@ -3307,8 +3361,7 @@ ignored."
(looking-at comment-start)) (looking-at comment-start))
(let (let
((indent 999) ((indent 999)
pre here diff fill-prefix-reg bcl first-indent first-indent hang here pre start end)
hang start end)
;; Change tabs to spaces in the surrounding paragraph. ;; Change tabs to spaces in the surrounding paragraph.
;; The surrounding paragraph will be the largest containing block of ;; The surrounding paragraph will be the largest containing block of
;; contiguous line comments. Thus, we may be changing tabs in ;; contiguous line comments. Thus, we may be changing tabs in
Expand All @@ -3325,54 +3378,11 @@ ignored."
(setq end (point))) (setq end (point)))
(untabify start end) (untabify start end)
;; ;;
(setq here (point)) (setq here (point)
(beginning-of-line) start (idlwave-commented-paragraph-beg-end)
(setq bcl (point)) end (cadr start)
(re-search-forward pre (car (cddr start))
(concat "^[ \t]*" comment-start "+") start (car start))
(save-excursion (end-of-line) (point))
t)
;; Get the comment leader on the line and its length
(setq pre (current-column))
;; the comment leader is the indentation plus exactly the
;; number of consecutive ";".
(setq fill-prefix-reg
(concat
(setq fill-prefix
(regexp-quote
(buffer-substring (save-excursion
(beginning-of-line) (point))
(point))))
"[^;]"))

;; Mark the beginning and end of the paragraph
(goto-char bcl)
(while (and (looking-at fill-prefix-reg)
(not (looking-at paragraph-separate))
(not (bobp)))
(forward-line -1))
;; Move to first line of paragraph
(if (/= (point) bcl)
(forward-line 1))
(setq start (point))
(goto-char bcl)
(while (and (looking-at fill-prefix-reg)
(not (looking-at paragraph-separate))
(not (eobp)))
(forward-line 1))
(beginning-of-line)
(if (or (not (looking-at fill-prefix-reg))
(looking-at paragraph-separate))
(forward-line -1))
(end-of-line)
;; if at end of buffer add a newline (need this because
;; fill-region needs END to be at the beginning of line after
;; the paragraph or it will add a line).
(if (eobp)
(progn (insert ?\n) (backward-char 1)))
;; Set END to the beginning of line after the paragraph
;; END is calculated as distance from end of buffer
(setq end (- (point-max) (point) 1))
;; ;;
;; Calculate the indentation for the paragraph. ;; Calculate the indentation for the paragraph.
;; ;;
Expand Down Expand Up @@ -3527,7 +3537,8 @@ non-nil."
(let ((here (- (point-max) (point))) (let ((here (- (point-max) (point)))
(indent (indent
(save-excursion (save-excursion
(forward-line -1) (goto-char
(car (idlwave-commented-paragraph-beg-end)))
(idlwave-calc-hanging-indent)))) (idlwave-calc-hanging-indent))))
(if indent (if indent
(progn (progn
Expand Down

0 comments on commit 985427d

Please sign in to comment.