Emacs插入居中注释块

use*_*422 4 emacs macros latex elisp center

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

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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%           Comment 2 Commenttext 3           %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Run Code Online (Sandbox Code Playgroud)

这可能emacs-lisp吗?

Tyl*_*ler 5

Emacs随附了comment-box用于此目的的命令。它会产生居中注释框,尽管该框的宽度根据内容而有所不同。例如,区域设置在以下行附近:

This is a comment
Run Code Online (Sandbox Code Playgroud)

调用时M-x comment-box,文本将转换为:

;;;;;;;;;;;;;;;;;;;;;;;
;; This is a comment ;;
;;;;;;;;;;;;;;;;;;;;;;;
Run Code Online (Sandbox Code Playgroud)

我使用一个修改后的版本,如果该区域处于非活动状态,则将注释框放在当前行周围,然后再退出注释。它还暂时减少了填充列,因此注释框的宽度不超过最长的行:

(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)))
Run Code Online (Sandbox Code Playgroud)