Auto-expanding comment blocks in emacs

I like that any comment appears on a separate line. I do not like to comment on one line with the code. In some languages ​​you can write a block of comments, for example:

/** * I am a comment block. This comment block will be automatically expanded by the * IDE such that it can contain all of the text in this block. **/ 

I like it. I love the way the comment block just keeps getting more lines as I add more text to it. I like how if I insert text at some arbitrary point in the block, the subsequent text will be moved down so that the text does not go beyond a certain point to the right. I am using Python. Python has no multi-line block comment. I think the closest thing you could get would be:

 # I am a comment block. This comment block will NOT be automatically expanded by # the IDE, because it does not recognize these two comment lines as being joined. 

I also use emacs. I'm just wondering if anyone has any clever solution so that they can open the comment block and just start typing. No need to worry about pressing the return key to go to the next line when the comment line is too wide. No need to re-shuffle the comment as a whole when you want to insert comments into the block. Any ideas?

Summary: I am looking for a way to make multi-line related comments (for Python) in emacs, without having to manually format the text in the comment block itself.

thanks

+7
source share
2 answers

auto-fill-mode seems to do what you want. When a line exceeds the fill-column value, it breaks the line and inserts a new comment line.

This is not fully automatic, although if the text is inserted between them, you need to press Mq to replenish.

[Edit: here is a way to multiply the space command. Every time you press SPC , your comment is blocked:

 (defun refill-when-in-comment () (interactive) (let ((curr-face (get-char-property (point) 'face))) (if (member "comment" (split-string (prin1-to-string curr-face) "-")) (fill-paragraph t) ) ) ) (defun smart-space (arg) (interactive "P") (refill-when-in-comment) (self-insert-command (prefix-numeric-value arg)) ) (global-set-key " " 'smart-space) 

Does this work for you?

+4
source

This is a little unorthodox, but you're not limited to using docstrings as comments only. The only magic part of using them as the first element is that they will be bound to the __doc__ method. THey can be used everywhere, although it will not affect efficiency at all

 >>> import dis >>> def test(): ... """This is a standard doc string""" ... a = 3 # This will get compiled ... """This is a non standard doc string and will not get compiled""" ... >>> dis.dis(test) 3 0 LOAD_CONST 1 (3) 3 STORE_FAST 0 (a) 4 6 LOAD_CONST 2 (None) 9 RETURN_VALUE 

You can see that the generated code does not refer to either of the two lines.

I mention this only because the doc lines seem to have all the functions you request. This is somewhat non-standard, although I personally do not see a problem with this. Multi-line comments would be nice.

+2
source

All Articles