Emacs,更改默认的 reftex 引用

发布于 2024-09-17 04:55:43 字数 299 浏览 1 评论 0原文

我想在使用 reftex 时修改 emacs 的行为,以便在按“抄送 [”并选择引用格式后,出现的默认正则表达式将为我提供上次使用的引用(正常行为是默认到光标之前的单词,这很少有任何用处)。我经常连续多次引用相同的来源,特别是在一张纸上做笔记时,所以这将是节省几次击键的好方法,这就是我们使用 emacs 的目的,对吧:)

我知道有点口齿不清,所以我希望最终我自己能找到一种方法来做到这一点,但我认为值得四处询问,看看是否有人先做到了,没有必要重新发明轮子。 (如果您确实想要此功能,但也不知道如何实现它,请告诉我,我完成后会给您发送电子邮件。)

谢谢

I'd like to modify emacs' behaviour when using reftex, so that after pressing 'C-c [' and choosing a citation format the default regex that comes up is one that will give me the citation I used last (the normal behaviour is to default to the word before the cursor, which is rarely of any use). I often cite the same source many times in a row, particularly when making notes on a single paper, so this would be a nice way to save a few keystrokes, and that's what we're all using emacs for, right :)

I know a little lisp, so I expect I'll end up working out a way to do this myself eventually, but I thought it'd be worth asking around to see if anyone else has done it first, no point re-inventing the wheel. (If you do want this feature, but also don't know how to achieve it, let me know and I'll drop you an email when I've done it.)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒芜了季节 2024-09-24 04:55:43

加载 reftex 后(例如在 AUCTeX 模式挂钩中)重新定义 reftex-get-bibkey-default 应该可以做到。最简单的是:

   (defun reftex-get-bibkey-default () (car reftex-cite-regexp-hist) )

但是,这将破坏默认行为,并且如果您的历史记录为空,则不会返回任何内容,为了在这种情况下保留 reftex 中的“前一个单词”行为,然后使用历史记录,您可以重新定义是这样的:

(defun reftex-get-bibkey-default () (if reftex-cite-regexp-hist
                                            (car reftex-cite-regexp-hist)
                                          (let* ((macro (reftex-what-macro 1)))
                                            (save-excursion
                                              (if (and macro (string-match "cite" (car macro)))
                                                  (goto-char (cdr macro)))
                                              (skip-chars-backward "^a-zA-Z0-9")
                                              (reftex-this-word)))
                                          )
    )

redifining reftex-get-bibkey-default after you have loaded reftex (e.g. in your AUCTeX mode hook) should do it. The simplest would thus be:

   (defun reftex-get-bibkey-default () (car reftex-cite-regexp-hist) )

However, this will destroy the default behaviour and wouldn't return anything if your history is empty, to keep the "previous word at point" behaviour from reftex in that case and then use the history, you can redefine it this way:

(defun reftex-get-bibkey-default () (if reftex-cite-regexp-hist
                                            (car reftex-cite-regexp-hist)
                                          (let* ((macro (reftex-what-macro 1)))
                                            (save-excursion
                                              (if (and macro (string-match "cite" (car macro)))
                                                  (goto-char (cdr macro)))
                                              (skip-chars-backward "^a-zA-Z0-9")
                                              (reftex-this-word)))
                                          )
    )
好听的两个字的网名 2024-09-24 04:55:43

(警告:这是我写的第一个 elisp 代码,长度超过 3 行,它可能是糟糕的代码。但它似乎有效。但是任何关于风格或最佳实践的评论将不胜感激。)

我'我已经解决了!只需将以下代码添加到 .emacs 中,它的行为就完全符合我的希望。如果您之前没有引用过任何内容,那么它会表现正常,否则默认引用是最后使用的。

(defvar reftex-last-citation nil)

(defadvice reftex-citation (after reftex-citation-and-remember-citation activate)
  "Save last citation to 'reftex-last-citation after running 'reftex-citation"
  (setq reftex-last-citation ad-return-value))

(defadvice reftex-get-bibkey-default (around reftex-just-return-last-citation activate)
  "If there is a 'reftex-last-citation then just return that instead of running 'reftex-get-bibkey-default"
  (if reftex-last-citation
      (setq ad-return-value reftex-last-citation)
    ad-do-it))

感谢莫蒂默的帮助,没有你的起点我永远不会到达这里!

(只是想知道,您的解决方案是否有任何原因不使用 defadvice?正如我上面暗示的那样,elisp 对我来说都是非常新的,所以了解最好的方法会很有用事物。)

(WARNING: This is the first elisp code I've ever written which is more than 3 lines long, it could be terrible code. But it seems to work. But any comments on style or best practices would be most appreciated.)

I've worked it out! Just add the following code to .emacs it behaves exactly as I'd hoped. If you've not cited anything before then it behaves as normal, otherwise the default citation is the last used.

(defvar reftex-last-citation nil)

(defadvice reftex-citation (after reftex-citation-and-remember-citation activate)
  "Save last citation to 'reftex-last-citation after running 'reftex-citation"
  (setq reftex-last-citation ad-return-value))

(defadvice reftex-get-bibkey-default (around reftex-just-return-last-citation activate)
  "If there is a 'reftex-last-citation then just return that instead of running 'reftex-get-bibkey-default"
  (if reftex-last-citation
      (setq ad-return-value reftex-last-citation)
    ad-do-it))

Thanks for the help Mortimer, without your starting point I'd never had got here!

(Just wondering, is there any reason why your solution didn't use defadvice? As I implied above, elisp is all very new to me, so it'd be useful to know the best way of doing things.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文