emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Citations, continued


From: Rasmus
Subject: Re: [O] Citations, continued
Date: Mon, 02 Feb 2015 23:47:34 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

John Kitchin <address@hidden> writes:

> #+BEGIN_SRC emacs-lisp :results silent
> (org-add-link-type
>  "slink" ...)
> #+END_SRC

Thanks John, this is great!

I managed to chop down my citation setup to the following:

[[cite:key :pre pre :post post :type type]]

with reasonable support for other backends.  I only use bibtex and I used
to separate pre/post with ";" so there's some legacy code in there.
Everything is hard-coded to my system/taste, written quickly and (very)
lightly tested, but maybe it will still be useful to somebody. . .

—Rasmus


(with-eval-after-load 'org
  (require 'reftex-cite)
  
  (defmacro rasmus/org-bib-add-type (type)
    ;; TODO: maybe this can be made more effective?
    ;; Seems to work OK...
    `(org-add-link-type
      ,type
      'rasmus/org-bib-follow
      ,(lambda (path description backend)
         (funcall 'rasmus/org-bib-format path description backend
                  ;; cite defaults to textcite
                  (if (equal type "cite") "textcite" type)))))
  
  (mapc (lambda (type) (funcall 'rasmus/org-bib-add-type type))
        '("cite" "textcite" "parentcite" "citeyear" "citeauthor"))
  
  (defun rasmus/org-bib-follow (path)
    "Find the pdf version of citation."
    (let* ((stream (read (format "(%s)" path))))
      (rasmus/find-lit (car head))))

  (defun rasmus/find-lit (key)
    "Open pdf file associated with KEY from `reftex-default-bibliography'."
    (let* ((bib (file-name-directory (car reftex-default-bibliography)))
           (file (concat bib path (concat "/" key ".pdf"))))
      (when (file-exists-p file) (find-file file))))

  (defun rasmus/org-bib-format (path description backend &optional type*)
    "Format a org-link citation.

Support links of the type

[[type*:key :pre PRE :post POST :type TYPE**]]

Or

[[Key*:key][POST;PRE]]

Based on John K's great post here:
    http://permalink.gmane.org/gmane.emacs.orgmode/94575";
    (let* ((key
            ;; key is a single symbol by assumption
            (and (string-match "\\` *\\([^ ]+\\) *" path)
                 (prog1 (match-string 1 path)
                   (setq path (replace-match "" nil nil path)))))
           ;; generate plist
           (data (read (format "(%s)"
                               (replace-regexp-in-string
                                "\\(:\\w+\\) \\([^:]+\\) ?" "\\1 \"\\2\" "
                                path))))
           (type (or (plist-get data :type) type* "textcite"))
           (pre  (org-trim (or (plist-get data :pre)
                               ;; support my "old" syntax
                               (and description
                                    (cadr (split-string description ";"))) "")))
           (post (org-trim
                  (or (funcall (lambda (txt)
                                 (and txt
                                      (let ((res (string-to-number txt)))
                                        (if (zerop res) txt
                                          (concat (if (> (length txt) 1)  "pp." 
"p.") " " txt)))))
                               (plist-get data :post))
                      (and description
                           (car (split-string description ";")))
                      "")))
           (entry (or (save-window-excursion
                        (bibtex-search-entry key t 0)
                        (bibtex-parse-entry))
                      (error (format "unknown key: %s" key))))
           (author (or (reftex-format-citation entry "%2a") ""))
           (year (or  (reftex-format-citation entry "%y") "")))
      (if (org-export-derived-backend-p backend 'latex)
          (format "\\%s[%s][%s]{%s}" type pre post key)
        ;; TODO: This should probably be wrapped in <cite>.</cite> with html...
        (cl-case (intern type)
          (parencite
           (format "(%s %s %s %s)"
                   pre author
                   (or (and (org-string-nw-p year) (concat year ", ")) "")
                   post))
          (citeyear
           (format "%s %s %s" pre year post))
          (citeauthor
           (format "%s %s %s" pre author post))
          (fullcite
           (reftex-format-citation entry reftex-cite-view-format))
          (t ;; textcite
           (format "%s (%s%s%s)"
                   author
                   (and (org-string-nw-p pre) (concat pre " "))
                   year
                   (and (org-string-nw-p post) (concat ", " post)))))))))


-- 
Dung makes an excellent fertilizer




reply via email to

[Prev in Thread] Current Thread [Next in Thread]