Emacs插入居中的注释块

时间:2014-01-10 10:47:16

标签: emacs macros latex elisp center

我想为emacs创建一个宏,它将插入一个带有一些中心文本的乳胶注释块,如:

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


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

这可能在emacs-lisp吗?

3 个答案:

答案 0 :(得分:5)

为此目的,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)))

答案 1 :(得分:3)

以下是您可以使用的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

答案 2 :(得分: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)
    ))