2

我最近从 vi 切换到 emacs,现在我正在将我最重要的宏移植到 emacs。我最需要的是能够为标记的文本区域添加一个字符串,包括页眉和页脚:

原来的:

line 1
line 2
line 3
line 4

在标记第 2 行和第 3 行之后,我希望 emacs 向我询问一个数字,比如 002,然后执行以下操作,最好记住我的选择:

line 1
*#002# Start:
*$line 2
*$line 3
*#002# End.
line 4

到目前为止,我已经设法使用以下代码插入开始和结束标签:

(defun comment-region (start end)
  "Insert COBOL comments."
  (interactive "r")
  (save-excursion 
    (goto-char end) (insert "*#xxx# End.\n")
    (goto-char start) (insert "*#xxx# Start:\n")
    ))

但是,我似乎不知道如何为该区域中的所有行加上前缀,*$以及如何让 emacs 向我询问一个字符串。

有任何想法吗?

4

3 回答 3

3

我最近通过使用yasnippet动态生成片段来解决这类问题。

这是代码:

(require 'yasnippet)
(defun cobol-comment-region (beg end)
  "comment a region as cobol (lines 2,3 commented)

line 1
*#002# Start:
*$line 2
*$line 3
*#002# End.
line 4
"
  (interactive "*r")
  (setq beg (progn
             (goto-char beg)
             (point-at-bol 1))
       end (progn
             (goto-char end)
             (if (bolp)
                 (point)
               (forward-line 1)
               (if (bolp)
                   (point)
                 (insert "\n")
                 (point)))))
  (let* ((str (replace-regexp-in-string
               "^" "*$" (buffer-substring-no-properties beg (1- end))))
         (template (concat "*#${1:002}# Start:\n"
                           str
                           "\n*#$1# End.\n"))
         (yas-indent-line 'fixed))
    (delete-region beg end)
    (yas-expand-snippet template)))

包括视频,什么???

这是它的实际操作视频

于 2013-03-21T13:18:22.577 回答
1

这是一个更好的方法,但它最后有点尴尬......

(defun comment-region (start end prefix)
  "Insert COBOL comments."
  (interactive "r\nsPrefix: ")
  (save-excursion
    (narrow-to-region start end)
    (goto-char (point-min))
    (insert "*#" prefix " #Start.\n")
    (while (not (eobp))
      (insert "*$")
      (forward-line))
    (insert "*#" prefix " #End.\n")
    (widen)))
于 2013-03-21T12:17:07.087 回答
1

您最好的选择是使用cobol-mode而不是自己编写临时函数。

文件头包含有关如何使用它的详细说明。

然后只需使用C-x Cwhich 运行命令comment-region,它会根据主要模式(在您的情况下为 cobol)评论该区域。

于 2013-03-21T13:47:53.930 回答