标记区域和插入前缀
我最近从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请求我输入一个字符串。
任何想法?
这是一个更好的方法,但其在最后一个有点尴尬...
(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-21 12:49:42
我建议你使用'forward-line'而不是'next-line' +'beginning-of-line',因为'next-line'对线包裹和其他显示工件很敏感。你也可以使用'(not(eobp))'。 – Stefan 2013-03-21 15:13:53
我忘记了下一线/前线的区别。和eobp。更新了答案以反映这一点。 – 2013-03-21 17:44:01
我已经采取了通过动态生成 片段与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:
最好的办法是使用cobol-mode,而不是写特设功能自己。
文件头包含有关如何使用它的详细说明。
就用C-XÇ它运行的命令comment-region
,根据主要模式(在你的情况,COBOL)的评论的区域。
'comment-region'内置于Emacs中。你正在覆盖一个非常常用的功能。 – 2013-03-21 12:21:38
@event_jr:oops,应该先检查一下。感谢提示! – Philip 2013-03-21 13:57:44