我想设置一个命令,将行内容放在两个§
个字符之间而不移动该点(不包括包含§
的行)。
这是我目前的尝试
(defun copy-section ()
"Copy current section, that is lines between two §."
(interactive)
(save-excursion
(when (not (search-backward-regexp "§" nil t))
(goto-char (point-min)) )
(forward-line 1)
(when (not (search-forward-regexp "§" nil t))
(goto-char (point-max)) )
(move-beginning-of-line nil)
(kill-ring-save (mark) (point)) ) )
它运作良好,但文档中关于移动标记是坏风格的评论使我认为有更好的方法来实现相同的结果。 将位置保存到变量(我不知道如何操作)可以实现更清晰的功能。
上面的部分代码来自ergoemacs。
答案 0 :(得分:3)
此版本将您的部分的开头和结尾保存在临时局部变量中,并且根本不使用该标记:
(defun copy-section ()
"Copy current page as defined by form feed characters."
(interactive)
(let (start end)
(save-excursion
(when (not (search-backward-regexp "§" nil t))
(goto-char (point-min)) )
(forward-line 1)
(setq start (point))
(when (not (search-forward-regexp "§" nil t))
(goto-char (point-max)) )
(move-beginning-of-line nil)
(setq end (point))
(kill-ring-save start end))))
答案 1 :(得分:3)
不需要“正则表达式”形式,因为只查找了一个字符
(defun copy-section ()
"Copy current section, that is lines between two §."
(interactive)
(save-excursion
(let* ((start (and (search-backward "§" nil t)
(forward-line 1)
(point)))
(end (progn (and start (search-forward "§" nil t))
(forward-line -1)
(end-of-line)
(point))))
(and start end (kill-new (buffer-substring-no-properties start end))))))