2014-01-10 36 views
3

我想創造一個emacs的宏,將插入一個乳膠的註釋塊像一些centerd文字:Emacs的中心插入註釋塊

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%%%    Comment 1     %%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%%%   Comment 2 Commenttext 3   %%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

這是可能的emacs-lisp

回答

3

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))) 
+0

爲了completenes,也許你應該包括'ty-move-point-forward-out或者用一個標準命令('forward-paragraph'?)替換它,以便非lispers可以立即使用它... – Francesco

+0

糟糕,沒有注意到我在那裏有一些個人功能。固定! – Tyler

2

這裏有一個yasnippet,您可以使用:

# -*- mode: snippet -*- 
# name: huge_comment 
# key: hc 
# -- 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%%%${1:$(repeat-char (- 33 (/ (length yas-text) 2)) " ")}$1${1:$(repeat-char (- 74 (length yas-text) (- 33 (/ (length yas-text) 2))) " ")}%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
$0 

如何使用它:類型hc,叫yas-expand並開始鍵入文本。它會自動重新對中 。

這段代碼將從latex-modetext-mode工作。但我注意到,如果您使用AUCTeX,則會出現一個錯誤,即 弄亂了光標位置。在這種情況下,您可以暫時將 切換爲text-mode

0

問題是在emacs-lisp中是否可行。是的。有幾種方法可以做到這一點。 我將展示一種您可以在其中註釋幾行文本的方法。 也許,第一行是文本部分的標題,第二行是這部分的作者。

更好的方法是建議LaTeX-indent-line函數。這樣你可以編輯評論文本和重新縮進。當我找到時間時,我會向你展示這個變體。

用法:將您的評論寫爲純文本。用鼠標將文本標記爲區域,然後運行以下命令。

(defun LaTeX-centered-comment (b e) 
    "Convert region into centered comment." 
    (interactive "r") 
    (let* ((n (count-lines b e))) 
    (goto-char b) 
    (beginning-of-line) 
    (insert-char ?% fill-column) 
    (insert ?\n) 
    (setq b (point)) 
    (center-line n) 
    (goto-char b) 
    (loop for i from 1 upto n do 
     (replace-region (point) (+ (point) 3) "%%%") 
     (end-of-line) 
     (insert-char ?\ (max 0 (- fill-column (- (point) (line-beginning-position)) 3))) 
     (insert "%%%") 
     (forward-line)) 
    (insert-char ?% fill-column) 
    (insert ?\n) 
    ))