1

我使用 cscope emacs 扩展(并发现它非常有用)。不幸的是,仅搜索当前目录及以下目录的默认行为对我来说是不够的,因此我将 cscope-set-initial-directory 用于我选择的目录。不幸的是,这对我来说也不够用,因为我有多个不同的项目,每个项目都有自己的“初始”目录。

我已经为我在 emacs 中使用最多的三个 cscope 方法设置了热键

(global-set-key [f9] 'cscope-find-this-text-string)
(global-set-key [f8] 'cscope-find-global-definition)
(global-set-key [f7] 'cscope-find-this-symbol)

我认为以某种方式用函数包装这些调用以在给定路径上运行 cscope-set-initial-directory (通过查看缓冲区文件名是什么而生成)会很容易。但我一直没有成功!

(global-set-key [f9] (lambda () (interactive) (cscope-set-initial-directory "blah") 'cscope-find-this-text-string))

不起作用。我也尝试向两个 cscope-hooks 添加钩子,但在我的正常使用模式中,它们似乎都没有被调用。我什至不介意每次切换缓冲区时运行一次它,但我也没有在任何地方看到一个钩子:/。

有人可以帮忙吗?:)

4

1 回答 1

1

免责声明:我尚未安装 cscope,因此无法对此进行测试。

(global-set-key (kbd "<f9>") 'my-cscope-find-this-text-string)
(defun my-cscope-find-this-text-string (dir)
  (interactive "DRoot area to start search:")
  (let ((default-directory dir))
    (call-interactively 'cscope-find-this-text-string)))

基本思想是您要提示从目录开始:这就是对交互式的调用。然后你设置目录: let 语句为你做这件事。然后你调用你想要的原始例程,并通过调用它'call-interactively得到提示。

您将类似地包装其他两个例程。

一旦这对您有用,您可以通过自定义根区域的提示来获得更好的体验,以使其拥有自己的历史变量,该变量在三个例程中共享。

关于您最初的解决方案,这是一个很好的尝试。大多数人没有意识到将'interactive函数转换为命令的必要性。引用名称对您不起作用的原因是引用只是告诉解释器将符号视为符号,而不是对它做任何事情。要调用例程,您通常会这样做:

(lambda ()
    (c-scope-find-this-text-string "some-string"))

Unfortunately, with a direct call like the one just above, you have to provide an argument (the string to search for). So, you can either add some code to prompt for the string, or use the command's built-in code to do the prompting. That's what the 'call-interactively is used for, it calls the command and invokes its 'interactive form which does the prompting.

Also, it's generally a good idea to bind keystrokes to names of commands, as opposed to bare lambdas, for two reasons: first, if you ever do C-h k (M-x describe-key), you get a meaningful command name, and second, if/when you modify the function, you can do so without having to muck with the binding as well.

于 2009-06-17T15:30:42.100 回答