views:

282

answers:

2

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 wrapped 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

+5  A: 

vim can do this, but doesn't automatically do it as you type. The command "gqj" re-does the line wrapping from the current line to the end of the paragraph.

Rovpedal
yep, it's not always automatic, and it's important that it's not - sometimes you don't want lines in a comment to wrap, especially when you're explaining bits of code with examples etc. manual control is worthwhile.
Peter
@Rovpedal: vim *does* it automatically as you type. or maybe just as *I* type? ;)
just somebody
it does it the first time, but if you're editing previously-written text it doesn't re-wrap it. (which is good, IMO.)
Peter
Um... How can I put this? I know many people really, really love vim, but... no. Not vim.
detly
At any rate, vim doesn't seem to do this. See my original post.
detly
+5  A: 

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.

Cirno de Bergerac
How do I make it auto-fill by default? I added "(setq-default fill-column 55)" and "(add-hook 'cc-mode-hook 'turn-on-auto-fill)" to my .emacs, and... nothing.
detly
That's not the hook name. It's `c-mode-hook`. http://cc-mode.sourceforge.net/html-manual/CC-Hooks.html#CC-Hooks
Cirno de Bergerac
Excellent, it works. I will try out emacs more comprehensively...
detly
Hitting M-q breaks the current line but does not add the leading `///` or re-flow the paragraph. Maybe I'm asking for magic at this point though.
detly
I found this fixed it: http://www.emacswiki.org/emacs/FillAdapt
detly
I updated the procedures so that they don't screw up the entire Emacs session, and to restrict the effect to comments, but it also works in strings for some reason.
Cirno de Bergerac