5

I want to have a quick way of writing a heading in an arbitrary file. So in Emacs-lisp mode the selction of Heading in a file and a call of the function would result in:

;;;;;;;;;;;;;;;;;;;; Heading ;;;;;;;;;;;;;;;;;;;;

I.e. automatically filling the rest of the line with the comment symbol of the respective mode and the text being centered, surrounded by a space and the comment symbols. I am almost sure there is something alike, somewhere?

Edit: Okay I found the file newcomment.el, which defines comment-styles such as plain, indent, aligned, box etc. Where aligned should be what I am asking for. But how can I get it running?

Drew
  • 75,699
  • 9
  • 109
  • 225
stevosn
  • 291
  • 2
  • 8
  • FWIW, `M-x center-line` centers one or more lines of text, but without adding the comment chars. – Drew Jul 26 '18 at 16:04

2 Answers2

3

While not exactly what you need but...

You can try comment-box command together with C-u 20 prefix (or whatever number you want):

enter image description here

You can also write your own function to do it (just grab the sources of the comment-box and try to adjust it to remove 1st and last comment lines + add padding you need).

...

...

...

Now, I had a look into the source code and here we are (not bullet-proof but):

enter image description here

The function to use:

(defun comment-fill-aligned (arg)
  "Comment out the current line using fill-column to pad and align with comment chars.

For the fill-column set to 80 it should look like:

elisp:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; hello ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

c:
/************************************* hello  *************************************/
"
  (interactive "p")
  (comment-normalize-vars)
  (let* ((comment-style 'aligned)
        (beg (line-beginning-position))
        (end (line-end-position))
        (com-add (/ (- fill-column (- end beg)) 2)))
    (comment-region beg end (+ comment-add com-add))))

So eval the function and call it interactively with M-x comment-fill-aligned. It works for the current line.

Maxim Kim
  • 1,516
  • 9
  • 17
  • Thanks! I was also working on it but struggling with learning the basics of lisp...I tried to set the comment style to align and the call comment-region, but I guess the lisp syntax is wrong..? `(defun comment-for-heading (arg) "Comment selected region and move it to center surounded by comment symbols." (setq comment-style 'aligned) (comment-region ((/ fill-column 2) arg)) (setq comment-style 'indent) )` – stevosn Jul 25 '18 at 13:37
  • 1
    it is better to use temporary binding with let form instead of setq here. And for comment-region usage -- you didn't provide correct parameters – Maxim Kim Jul 25 '18 at 13:53
  • I see.. I used your function and tweaked it a bit.. there was a different result for an even or odd number of characters in the line ;-) – stevosn Jul 25 '18 at 14:29
  • I didn't try to make it perfect ;). Your solution most probably has an issue with odd number of characters and c-style comments – Maxim Kim Jul 25 '18 at 17:07
0

I adjusted Maxim's code to produce the following function, which accounts for even and odd number of characters present in a line:

(defun comment-fill-aligned (arg)
  "Comment out the current line using fill-column to pad and
align with comment chars."
  (interactive "p")
  (comment-normalize-vars)
  (let* ((comment-style 'aligned)
    (beg (line-beginning-position))
    (end (line-end-position))
    (com-add (/ (- fill-column (+ (- end beg) 2)) 2)))
(comment-region beg end (+ comment-add com-add))
)
  (end-of-line)
  (if (> (current-column) (+ fill-column 1))
  (delete-backward-char 1)
()
   )
  (newline))
stevosn
  • 291
  • 2
  • 8