Skip to content

Fill paragraph in block ({- ... -}) comments is generates sometimes several comment opens ({-) #354

Closed
@jyp

Description

@jyp

Calling fill-paragraph on this snippet:

f x = x {- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed risus ipsum. -}

yields (if the current point is between "Lorem" and "sed"):

f x = x {- Lorem ipsum dolor sit amet, consectetur adipiscing
        {- elit. Donec sed
risus ipsum.-}

I have developed a replacement for haskell-fill-paragraph which
consistently generates:

f x = x {- Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Donec sed risus ipsum. -}

Which, I believe, is a correct behaviour.

The function follows.

(defun haskell-fill-paragraph (justify)
    (save-excursion
      ;; Fill paragraph should only work in comments.
      ;; The -- comments are handled properly by default
      ;; The {- -} comments are handled specially here.
      (let* ((syntax-values (syntax-ppss))
             (comment-num (nth 4 syntax-values)))
        (cond
         ((eq t comment-num)
          ;; standard fill works wonders inside a non-nested comment
          (fill-comment-paragraph justify))

         ((integerp comment-num)
          ;; we are in a nested comment. lets narrow to comment content
          ;; and use plain paragraph fill for that
          (let* ((comment-start-point (nth 8 syntax-values))
                 (comment-end-point
                  (save-excursion
                    (goto-char comment-start-point)
                    (forward-comment 1)
                    (point)))
                 (paragraph-start-point (save-excursion (backward-paragraph) (point)))
                 (paragraph-end-point (save-excursion (forward-paragraph) (point))))
            ;; If a nested comment is broken into several paragraphs, then fill only the current one.
            (fill-region (max paragraph-start-point comment-start-point)
                         (min paragraph-end-point comment-end-point)
                         justify))
          t ;; return t so that fill-paragraph does not keep
            ;; going. (This route was successfull regardless the
            ;; output of fill-region)
          )
         ((eolp)
          ;; do nothing outside of a comment
          t)
         (t
          ;; go to end of line and try again
          (end-of-line)
          (haskell-fill-paragraph justify))))))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions