Text editor with comment wrapping
I usually use Geany or Hi-Tide under Debian (GNU/Linux) for firmware development, mainly C (but also reading old assembler). I document code using single-line comments, and it really annoys me when I retype something and have to manually re-break every following line to keep it in the 80-character margin.
Is there a text editor that can re-wrap consecutive single-line comments (and do this automatically while I type)? That is, given:
/// This is a really long line that should have been wrapped at "that" but was not.
/// This sentence is in the same
/// paragraph as the last.
...I want an editor that will re-wrap this to
/// This is a really long line that
/// should have been wra开发者_JAVA技巧pped at "that"
/// but was not. This sentence is in
/// the same paragraph as the last.
...preferably doing this sensibly while I type.
I've tried:
- Hi-Tide (based on Eclipse 3.3)
- Geany
- jEdit
- UniversalIndentGUI + a bunch of prettifiers (I couldn't find any formatters that worked, and it's not a great workflow either)
- GVim - next line begins
//should have been
... instead of/// should have been
...
Update: just to elaborate on my accepted answer - I've gone with the snapshot emacs and an extra filladapt mode was also required
In Emacs, to start automatic wrapping, enter auto-fill-mode. To set the line width, run C-u ⟨columns⟩ C-x f. Emacs, or really CC Mode, will anticipate your commenting structure, so that typing /// This is a really long line that shoul will result in
/// This is a really long line that /// shoul‸
And you can refill a paragraph at any time with M-q.
If you want to do refills automatically with each keypress, well there may well be some interal command or third-party library out there, but off-hand you can use this elisp code:
;;; Can't advise SELF-INSERT-COMMAND, so create a wrapper procedure. (defun self-insert-refill (n) (interactive "p") (self-insert-command n)) ;;; Advise SELF-INSERT-REFILL to execute FILL-PARAGRAPH after every ;;; keypress, but *only* if we're inside a comment (defadvice self-insert-refill (after refill-paragraph) (let ((face (or (get-char-property (point) 'read-face-name) (get-char-property (point) 'face))) ) (if (and (eq face 'font-lock-comment-face) (not (string= " " (this-command-keys)))) ; Spaces would get deleted on refill. (fill-paragraph)))) (ad-activate 'self-insert-refill) (add-hook 'c-mode-hook ;; Remap SELF-INSERT-COMMAND to be SELF-INSERT-REFILL. (local-set-key [remap self-insert-command] 'self-insert-refill) ))
This is probably not very robust or in keeping with best-practice, and likely not wholly satisfactory, as it won't work for general editing, e.g. C-d and backspace, and it slows down the editor somewhat, but it's a start.
Vim most certainly can do this.
First, you need to tell Vim that "///" is a comment prefix (it isn't by default):
:set comments^=:///
If you want wrapping to occur as-you-type, set your preferred textwidth:
:set textwidth=80
To format existing paragraphs, use any variation of the gq command. For example, you could:
- Select a paragraph visually and type gq, or
- Type gqj to re-wrap from the current line to the end of the paragraph
精彩评论