[CMake] *Patch* fill-paragraph command in emacs for comments using cmake-mode.el

Miguel A. Figueroa-Villanueva miguelf at ieee.org
Wed Aug 1 20:51:33 EDT 2007


On 8/1/07, James Bigler wrote:
> One of the features I like to use in emacs is the fill-paragraph command
> (usually run with M-q).  For different modes this can do different things.  For
> source files, when you run the command on a comment region, this has the effect
> of reorganizing a multi-line comment to make it look pretty.  This is especially
> important if you add or remove large chunks of the text after formatting it.

<snip>

> You can get it look more like the one on top, by adding a # on the second line
> and running fill-paragraph again, but if your comment doesn't start at the
> begging of the line, this work around doesn't work.
>
> Also, if I have code right after the comment it gets pulled in.
>
> # Comment          # Comment IF(MYVAR)
> IF(MYVAR)      =>
>
> I updated to the most current version of cmake-mode.el found here, but that
> didn't help.
> http://www.cmake.org/cgi-bin/viewcvs.cgi/Docs/cmake-mode.el?rev=1.31&root=CMake&view=auto
>
> I'm wondering if anyone else has experience this and perhaps found something
> that can make it work.  My emacs-lisp knowledge is somewhat meager, so I'm not
> sure where to look to fix this.

The patch below provides a cmake-fill-comment-paragraph and a
cmake-fill-comment-paragraph-justify, for that purpose. Does anyone
care to add it to the one in the docs?

> Thanks,
> James

--Miguel


Index: Docs/cmake-mode.el
===================================================================
RCS file: /cvsroot/CMake/CMake/Docs/cmake-mode.el,v
retrieving revision 1.31
diff -b -u -p -r1.31 cmake-mode.el
--- Docs/cmake-mode.el  23 Sep 2006 20:32:34 -0000      1.31
+++ Docs/cmake-mode.el  2 Aug 2007 00:45:39 -0000
@@ -153,6 +153,106 @@
 ;------------------------------------------------------------------------------

 ;;
+;; Helper functions for fill-comment-paragraph function.
+;;
+(defconst cmake-fill-comment-prefix "# ")
+
+(defun cmake-in-comment-p ()
+  ;; Returns t if inside a comment.
+  (nth 4 (parse-partial-sexp
+          (save-excursion (beginning-of-line) (point))
+          (point))))
+
+;------------------------------------------------------------------------------
+
+;;
+;; Fill comment paragraph function.
+;;
+(defun cmake-fill-comment-paragraph-justify ()
+  "Fills the current comment paragraph with justified margins."
+  (interactive)
+  (cmake-fill-comment-paragraph 1))
+
+(defun cmake-fill-comment-paragraph (&optional justify)
+  "Fills the current comment paragraph."
+  (interactive "P")
+  (let ((opos (point-marker))
+        (begin nil)
+        (end nil)
+        (end-2 nil)
+        (indent nil))
+
+    ;; check if inside comment
+    (if (not (cmake-in-comment-p))
+        (error "not inside comment"))
+
+    ;;
+    ;; find limits of paragraph
+    ;;
+    (message "filling comment paragraph ...")
+    (save-excursion
+      (back-to-indentation)
+      ;; find end of paragraph
+      (while (and (looking-at "#.*$")
+                  (not (looking-at "#[ \t]*$")))
+        (forward-line 1)
+        (back-to-indentation))
+      (beginning-of-line)
+      (setq end (point-marker))
+      (goto-char opos)
+      ;; find begin of paragraph
+      (back-to-indentation)
+      (while (and (looking-at "#.*$")
+                  (not (looking-at "#[ \t]*$")))
+        (forward-line -1)
+        (back-to-indentation))
+      (forward-line 1)
+      ;; get indentation to calculate width for filling
+      (current-indentation)
+      (back-to-indentation)
+      (setq indent (current-column))
+      (setq begin (point-marker)))
+
+    ;; delete leading whitespace and uncomment
+    (save-excursion
+      (goto-char begin)
+      (beginning-of-line)
+      (while (re-search-forward "^[ \t]*#[ \t]*" end t)
+        (replace-match "")))
+
+    ;; calculate fill width
+    (setq fill-column (- fill-column indent
+                         (length cmake-fill-comment-prefix)))
+
+    ;; fill paragraph
+    (fill-region begin (1- end) justify)
+    (setq fill-column (+ fill-column indent
+                         (length cmake-fill-comment-prefix)))
+
+    ;; find end of second last line
+    (save-excursion
+      (goto-char end)
+      (forward-line -2)
+      (end-of-line)
+      (setq end-2 (point-marker)))
+
+    ;; re-comment and re-indent region
+    (save-excursion
+      (goto-char begin)
+      (indent-to indent)
+      (insert cmake-fill-comment-prefix)
+      (while (re-search-forward "\n" (1- end-2) t)
+        (replace-match (concat "\n" cmake-fill-comment-prefix))
+        (beginning-of-line)
+        (indent-to indent)))
+
+    (message "filling comment paragraph ... done")
+    (goto-char opos))
+  t)
+
+;------------------------------------------------------------------------------
+
+;;
 ;; Keyword highlighting regex-to-face map.
 ;;
 (defconst cmake-font-lock-keywords


More information about the CMake mailing list