Emacs insert centered comment block

痴心易碎 提交于 2019-12-07 11:00:52

问题


I would like to create a macro for emacs that will insert a latex comment block with some centerd text like:

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


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

Is this possible in emacs-lisp?


回答1:


Emacs comes with the command comment-box for this purpose. It produces centered comment boxes, although the width of the box varies depending on the content. E.g., with the region set around the following line:

This is a comment

when you call M-x comment-box the text is transformed to:

;;;;;;;;;;;;;;;;;;;;;;;
;; This is a comment ;;
;;;;;;;;;;;;;;;;;;;;;;;

I use a modifed version that places the comment box around the current line if the region isn't active, and then steps out of the comment afterwards. It also temporarily reduces the fill-column, so the comment box is not wider than your longest line:

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



回答2:


Here's a yasnippet that you can use:

# -*- 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

How to use it: type hc, call yas-expand and start typing the text. It will re-center itself automatically.

This snippet will work from latex-mode or text-mode. I've noticed however a bug that messes up the cursor position if you're using AUCTeX. In that case, you can momentarily switch to text-mode.




回答3:


The question was whether it is possible in emacs-lisp. Yes it is. There are several ways to do it. I will show one way where you can also comment several lines of text. Maybe, in the first line there is the title of the part of text and in the second one there is the author of this part.

A better way would be to advice LaTeX-indent-line function. This way you could edit the comment text and re-indent. When I find time I will show you also this variant.

Usage: Write your comment as clear text. Mark text as region with the mouse and then run the following command.

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


来源:https://stackoverflow.com/questions/21042321/emacs-insert-centered-comment-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!