带有评论包装的文本编辑器

时间:2009-12-23 05:14:31

标签: editor comments text-editor

我通常在Debian(GNU / Linux)下使用Geany或Hi-Tide进行固件开发,主要是C(但也读旧的汇编程序)。我使用单行注释来记录代码,当我重新键入某些内容并且必须手动重新断开每一行以使其保持在80个字符的边距时,它真的很烦我。

是否有文本编辑器可以重新包装连续的单行注释(并在我键入时自动执行此操作)?那就是:

/// 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.

...我想要一个将其重新包装到

的编辑器
/// 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.

...最好在我输入时明智地做到这一点。

我试过了:

  • Hi-Tide(基于Eclipse 3.3)
  • Geany
  • jEdit的
  • UniversalIndentGUI +一堆美化师(我找不到任何有效的格式化工具,也不是一个很好的工作流程)
  • GVim - 下一行开始//should have been ...而不是/// should have been ...

更新:只是详细说明我接受的答案 - 我已经使用了snapshot emacs,还需要额外的filladapt mode

2 个答案:

答案 0 :(得分:6)

Vim肯定可以这样做。

首先,您需要告诉Vim“///”是comment prefix(默认情况下不是这样):

:set comments^=:///

如果希望换行按类型进行,请设置首选textwidth:

:set textwidth=80

要格式化现有段落,请使用gq命令的任何变体。例如,您可以:

  • 选择一个段落visually并输入 g q
  • 键入 g q j 从当前行重新换行到段落末尾

答案 1 :(得分:6)

在Emacs中,要开始自动换行,请输入 auto-fill-mode 。要设置线宽,请运行 C-u⟨⟩C-x f 。 Emacs,或真正的CC模式,将预测您的评论结构,以便打字 ///这是一个很长的行,会导致

/// This is a really long line that
/// shoul‸

您可以随时使用 M-q 重新填写一个段落。

如果你想用每个按键自动重新填充,那么很可能会有一些内部命令或第三方库,但你可以使用这个elisp代码:

;;; 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) ))

这可能不是非常强大或与最佳实践保持一致,并且可能不完全令人满意,因为它不适用于一般编辑,例如 C-d backspace ,它会稍微降低编辑器的速度,但这是一个开始。