开发者

Emacs, changing the default reftex citation

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 th开发者_运维知识库e 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


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


(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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜