我最近从 vi 切换到 emacs,现在我正在将最重要的宏移植到 emacs。我最需要的是能够使用字符串为标记的文本区域添加前缀,包括页眉和页脚:
原文:
line 1
line 2
line 3
line 4
标记第二行和第三行后,我希望 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 询问我一个字符串。
有什么想法吗?
最佳答案
我已经通过动态生成一个解决此类问题的方法 代码片段 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)))
包含视频,什么???
这是一个video它的实际应用:
关于emacs - 标记区域并插入前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15545480/