0

我想要一个切换器,它可以在上面的每一行(点)前面添加/删除“..”(有一个空格——但我不能让它更明显)字符串。这是我最好的选择:

(defun rst-comment-above (Point)
  (interactive "d")
  (save-excursion
    (goto-char 1)
    (cond

     ((numberp (get this-command 'state))
      ((replace-regexp "^\\.\\. " "" nil (point) (get this-command 'state)))
      (put this-command 'state ""))

     (t
     (replace-regexp "^" ".. " nil (point) Point)
     (put this-command 'state Point))
)))

它第一次工作,但第二次它说:

(invalid-function
 (replace-regexp "^\\.\\. " "" nil (point) (get this-command (quote state))))

编辑

@用户4815162342:

所以我评论上面的事情:

我评论上面的东西

然后我插入新行:

我插入新行

然后我想取消注释,我得到:

取消注释后,我得到

但可能它并不那么重要。我一般不会在评论区输入任何内容。我只是注意到这个问题。更重要的是——'state跨会话存储给定文件的。实施起来难吗?

4

2 回答 2

1

该错误来自您调用replace-regexp. 那行应该是:

(replace-regexp "^\\.\\. " "" nil (point) (get this-command 'state))

您的代码还有其他几个问题。

  1. 存储点的当前值效果不佳,因为您将字符添加到缓冲区,这会使点向前移动。这使得(一旦上述语法错误得到修复)该函数错过了“..”的最后几个实例。
    • 解决方法是存储点标记。
  2. 您应该使用(point-min)而不是硬编码从 1 开始的缓冲区,否则当缓冲区缩小生效时,您的代码将无法工作。
  3. 最后,replace-regexp正如其文档所述,不应该从 Lisp 程序中调用。

这是您的功能的修订版本:

(defun rst-comment-above ()
  (interactive)
  (let ((pm (point-marker))
        (prev-marker (get this-command 'rst-prev-marker)))
    (save-excursion
      (goto-char (point-min))
      (cond ((null prev-marker)
             (while (< (point) pm)
               (insert "..")
               (forward-line 1))
             (put this-command 'rst-prev-marker pm))
            (t
             (while (< (point) prev-marker)
               (when (looking-at "^\\.\\.")
                 (replace-match ""))
               (forward-line 1))
             (put this-command 'rst-prev-marker nil))))))
于 2012-09-07T18:09:15.033 回答
0

有什么理由不使用M-;in rst-mode

于 2012-09-08T20:46:16.087 回答