2

我想设置一个命令,将行的内容放在两个§字符之间而不移动点(不包括包含 的行§)。

这是我目前的尝试

(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

4

2 回答 2

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))))
于 2013-07-12T14:04:56.620 回答
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))))))
于 2013-07-12T18:53:22.710 回答