2012-09-07 40 views
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)))) 

编辑

@ user4815162342:

所以我评论以上内容:

I comment the thing above

然后我插入新行:

I insert new lines

然后我想取消对的事情,我也得到:

upon uncommenting the thing, and I get

,不过也许它不是那么重要。我通常不会在评论区域输入任何内容。我只是注意到这个问题。什么是更重要的 - 在会话中存储给定文件的'state。难以实施吗?

+0

而不是(goto-char 1)我推荐'(goto-char(point-min))'。 – Stefan

+0

@Stefan:好的。虽然我现在不使用缩小。 – Adobe

+0

我现在明白你的意思了。我已经更新了我的答案来处理这个案例,并修复了另一个错误。请尝试新版本。 – user4815162342

回答

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)))))) 
+0

你可以选择一个用户名吗?任何随机单词都可以找到,这使得其他人更容易引用您的答案。 –

+0

感谢您的编辑,改进后的格式使答案更加清晰。 – user4815162342

+0

'点标记'确实保持正确的位置 - 如果你插入新的字符到注释文本中 - 而不是新行。虽然'set-mark'和'register-to-point'确实:即使我在这些标记上面输入了一些新的行 - 标记指向了右边的“point”。我可以分别用'anything-mark-ring'(或'icicle-goto-marker')和'point-to-register'来看它。但是他们都是互动的,我看不出有什么方法可以用来达到目的。你能做些什么吗?无论如何感谢你的代码和批评。在这里和那里留下一对冗余副本是很有用的... – Adobe

0

任何理由,你为什么不rst-mode使用M-;

+0

尝试取消注释。 – Adobe

+0

@Adobe:适合我。如果没有,请将其报告为缺陷。 – Stefan