开发者

Auto expanding blocks of comments in emacs

I like any comment to appear on its own line. I don't like to put comments on the same line that code is on. In some languages you can write a comment block such as:

/**
 * I am a comment block. This comment block will be automatically expanded by the
 * IDE such that it can contain all of the te开发者_如何学JAVAxt in this block.
**/

I like that. I like how 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, subsequent text will be moved down so that no text goes beyond a certain point to the right. I use Python. Python does not have a multiline block comment. I guess the closest 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 am just wondering if anyone has though of some clever solution such that they can open up a comment block and just start typing. No worrying about having to press return to jump to the next line when the width of the comment line is too large. No having to re-shuffle the comment as a whole when you wish to insert within the comment block. Any ideas?

Summary: I am looking for a way to do multiline contiguous comments (for Python) in emacs, without having to manually format the text in the comment block itself.

Thank you


auto-fill-mode seems to do what you want. When the length of the line exceeds the value of fill-column it breaks the line and insert new comment line.

It's not fully automatic though, if the text is inserted in between , you will have to press M-q to refill.

[Edit: here is a way to smartify the "space" command. Each time you press SPC your comment block will be refilled:

(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?


It's slightly unorthodox but you are not restricted to using strings as comments only for docstrings. The only magical part of having them as the first item is that they will get assigned to the objects __doc__ method. THey can be used anywhere though and won't 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 doesn't make any reference to either of the two strings.

I only mention this because doc strings seems to have all of the features that you are requesting. It is somewhat nonstandard though I don't personally see a problem with it. Multiline comments would be nice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜