0

Context

When writing a long text file that has some structure (program, TeX/LaTeX document, whatever programming or markup language), it can be handy to make structure visually salient into visible sections, by having section titles padded by the comment character of the programming/document language.

Starting point

A good starting point exists in this question.

With the code above, you can type some text and have it become a section title.

But what happens when you want to edit the section title? The padding length should be updated. Code above does not do this.

Concrete example

Let's take a bash script as an example for the syntax.

Code provided by the answer above turns:

some words

into

############################# some words #############################

So far, so good.

But imagine there was a very long section title, that was in the past correctly padded with #### on both sides to the correct length. Then I just shortened the section title to this:

#### just words ####

The code above turns it into:

######################## #### just words #### ########################

I wish it would produce the same as the first example:

############################# just words #############################

In other words it would remove the comment characters left and right before padding, so that the result is always like in the first example.

I did some local research effort but failed to produce something usable.

  • Notice that emacs can already reformat any paragraph, including a comment paragraph, with `fill-paragraph`. Perhaps a good way would be to define a comment style that has comment delimiters padding on left and right and just use that already existing function? – Stéphane Gourichon Jul 09 '23 at 16:47

0 Answers0