Emacs爲此提供了命令comment-box
。它會生成居中的評論框,但框的寬度取決於內容。例如,與周圍的區域以下行設置:
This is a comment
當你調用M-x comment-box
文本轉換爲:
;;;;;;;;;;;;;;;;;;;;;;;
;; This is a comment ;;
;;;;;;;;;;;;;;;;;;;;;;;
我用的修飾的版本放在圍繞當前行,如果在評論框該地區不活躍,然後逐步退出評論。它也暫時減少填充列,所以評論框不會比最長的一行更寬:
(defun ty-box-comment (beg end &optional arg)
(interactive "*r\np")
(when (not (region-active-p))
(setq beg (point-at-bol))
(setq end (point-at-eol)))
(let ((fill-column (- fill-column 6)))
(fill-region beg end))
(comment-box beg end arg)
(ty-move-point-forward-out-of-comment))
(defun ty-point-is-in-comment-p()
"t if point is in comment or at the beginning of a commented line, otherwise nil"
(or (nth 4 (syntax-ppss))
(looking-at "^\\s *\\s<")))
(defun ty-move-point-forward-out-of-comment()
"Move point forward until it's no longer in a comment"
(while (ty-point-is-in-comment-p)
(forward-char)))
爲了completenes,也許你應該包括'ty-move-point-forward-out或者用一個標準命令('forward-paragraph'?)替換它,以便非lispers可以立即使用它... – Francesco
糟糕,沒有注意到我在那裏有一些個人功能。固定! – Tyler