From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rasmus Subject: Re: Citations, continued Date: Mon, 02 Feb 2015 23:47:34 +0100 Message-ID: <87bnlcdj7d.fsf@gmx.us> References: <87vbjmn6wy.fsf@berkeley.edu> <8761blm6n8.fsf@berkeley.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:60638) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YIPme-00071K-Iu for emacs-orgmode@gnu.org; Mon, 02 Feb 2015 17:47:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YIPmZ-0000J3-D3 for emacs-orgmode@gnu.org; Mon, 02 Feb 2015 17:47:56 -0500 Received: from plane.gmane.org ([80.91.229.3]:56856) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YIPmZ-0000IS-21 for emacs-orgmode@gnu.org; Mon, 02 Feb 2015 17:47:51 -0500 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1YIPmP-0006bW-OW for emacs-orgmode@gnu.org; Mon, 02 Feb 2015 23:47:41 +0100 Received: from 46-253-188-95.dynamic.monzoon.net ([46.253.188.95]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 02 Feb 2015 23:47:41 +0100 Received: from rasmus by 46-253-188-95.dynamic.monzoon.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 02 Feb 2015 23:47:41 +0100 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org John Kitchin 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 . 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