From mboxrd@z Thu Jan 1 00:00:00 1970 From: Matt Lundin Subject: Re: [ANN] org-bibtex.el --- convert between Org headings and bibtex entries Date: Mon, 25 Apr 2011 11:15:39 -0400 Message-ID: <87oc3uweec.fsf@fastmail.fm> References: <87y6357q81.fsf@gmail.com> <027A8D96-05AB-452C-87D3-85D90D24AFB1@tsdye.com> <87vcy81xtv.fsf@gmail.com> <877hanwbyb.fsf@gmail.com> <89A178C3-C2E4-493D-BE84-12200512B766@tsdye.com> <87ei4sa3cz.fsf@fastmail.fm> <871v0rzp8v.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([140.186.70.92]:52009) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QENVm-0006EK-Ur for emacs-orgmode@gnu.org; Mon, 25 Apr 2011 11:15:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QENVl-0004wf-FW for emacs-orgmode@gnu.org; Mon, 25 Apr 2011 11:15:42 -0400 Received: from out4.smtp.messagingengine.com ([66.111.4.28]:33080) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QENVl-0004wZ-AW for emacs-orgmode@gnu.org; Mon, 25 Apr 2011 11:15:41 -0400 In-Reply-To: <871v0rzp8v.fsf@gmail.com> (Eric Schulte's message of "Sun, 24 Apr 2011 08:21:16 -0600") 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: Eric Schulte Cc: Org Mode Hi Eric, "Eric Schulte" writes: > Matt Lundin writes: > > [...] >>> I understand I may add to the types variable.=C2=A0 When using >>> org-bibtex-create, I can enter any arbitrary field as a PROPERTY; >>> however, org-bibtex ignores anything outside of the universe it knows >>> about.=C2=A0 Would it be bad practice to allow the export of any arbitr= ary >>> field type one has recorded?=C2=A0 I think the emacs bibtex-mode may >>> recognize erroneous bibtex entries.=C2=A0=C2=A0=20 >> >> Bibtex-mode does indeed allow for arbitrary fields, as do bibtex and >> biblatex. AFAIK, they are simply ignored when processing a bib file. One >> limitation that arises when storing bibtex data as org properties is >> that properties drawers are used for much more. For instance, one would >> probably not want to see "logging =3D {lognoterepeat}," in one's exported >> bibtex file. >> >> But for biblatex users, it would indeed be prohibitively expensive to >> have to inform org-mode ahead of time about the innumerable odd fields >> that various biblatex backends define. > > There is already an option for an org-bibtex specific property name > prefix, (namely `org-bibtex-prefix'). Perhaps when this prefix is used, > and the `org-bibtex' functions is called with a prefix argument (note: > entirely different usage of the term "prefix") then only entries which > begin with the `org-bibtex-prefix' would be exported... I believe that > should provide a natural way for arbitrary fields to pass through > org-bibtex without the user needing to explicitly name them, or there > being any danger of contamination from existing org-mode properties. I went ahead and implemented this. (Alas, it meant cluttering up your very elegant org-bibtex-headline with another mapcar.)=20 Assuming that not all users who use a prefix will want to export arbitrary fields, I made the functionality dependent on two variables: org-bibtex-prefix and a org-bibtex-export-arbitrary-fields. But this could be simplified. I also made the key property configurable. The patch was created against a patched org-bibtex.el, so I will wait until your changes get merged into the repo before sending a formal patch. But I thought I'd send it along to see if you think the changes are appropriate. Best, Matt=20 --8<---------------cut here---------------start------------->8--- diff --git a/lisp/org-bibtex.el b/lisp/org-bibtex.el index 9ee30f1..afa3764 100644 --- a/lisp/org-bibtex.el +++ b/lisp/org-bibtex.el @@ -221,6 +221,24 @@ For example setting to 'BIB_' would allow interoperabi= lity with fireforg." :group 'org-bibtex :type 'string) =20 +(defcustom org-bibtex-export-arbitrary-fields nil + "When converting to bibtex allow fields not defined in `org-bibtex-field= s'. +This only has effect if org-bibtex-prefix is defined, so as to +ensure that other org-properties, such as CATEGORY or LOGGING are +not placed in the exported bibtex entry." + :group 'org-bibtex + :type 'boolean) + +;; TODO if ID, test to make sure ID is unique +(defcustom org-bibtex-key-property "CUSTOM_ID" + "Property that holds the bibtex key. +By default, this is CUSTOM_ID, which enables easy linking to +bibtex headlines from within an org file. This can be set to ID +to enable global links, but only with great caution, as global +IDs must be unique." + :group 'org-bibtex + :type 'string) + ;;; Utility functions (defun org-bibtex-get (property) @@ -232,7 +250,7 @@ For example setting to 'BIB_' would allow interoperabil= ity with fireforg." (substring (symbol-name property) 1) property)))) (org-set-property - (concat (unless (string=3D "CUSTOM_ID" prop) org-bibtex-prefix) prop) + (concat (unless (string=3D org-bibtex-key-property prop) org-bibtex-p= refix) prop) value))) =20 (defun org-bibtex-headline () @@ -246,7 +264,7 @@ For example setting to 'BIB_' would allow interoperabil= ity with fireforg." (if (listp e) (apply #'flatten e) (li= st e))) lsts)))) (let ((notes (buffer-string)) - (id (org-bibtex-get "custom_id")) + (id (org-bibtex-get org-bibtex-key-property)) (type (org-bibtex-get "type"))) (when type (let ((entry (format @@ -254,15 +272,23 @@ For example setting to 'BIB_' would allow interoperab= ility with fireforg." (mapconcat (lambda (pair) (format " %s=3D{%s}" (car pair) (cd= r pair))) (remove nil - (mapcar - (lambda (field) - (let ((value (or (org-bibtex-get (from-k field)) - (and (equal :title field) - (org-get-heading))))) - (when value (cons (from-k field) value)))) - (flatten - (get :required (get (to-k type) org-bibtex-types)) - (get :optional (get (to-k type) org-bibtex-types))))) + (if (and org-bibtex-export-arbitrary-fields org-bibtex-prefix) + (mapcar=20 + (lambda (kv) + (when (string-match org-bibtex-prefix (car kv)) + (cons (downcase (replace-regexp-in-string=20 + org-bibtex-prefix "" (car kv))) + (cdr kv)))) + (org-entry-properties nil 'standard)) + (mapcar + (lambda (field) + (let ((value (or (org-bibtex-get (from-k field)) + (and (equal :title field) + (org-get-heading))))) + (when value (cons (from-k field) value)))) + (flatten + (get :required (get (to-k type) org-bibtex-types)) + (get :optional (get (to-k type) org-bibtex-types)))))) ",\n")))) (with-temp-buffer (insert entry) @@ -283,7 +309,7 @@ For example setting to 'BIB_' would allow interoperabil= ity with fireforg." =20 (defun org-bibtex-autokey () "Generate an autokey for the current headline" - (org-bibtex-put "CUSTOM_ID" + (org-bibtex-put org-bibtex-key-property (if org-bibtex-autogen-keys (let ((entry (org-bibtex-headline))) (with-temp-buffer @@ -312,7 +338,7 @@ With optional argument OPTIONAL, also prompt for option= al fields." (let ((prop (org-bibtex-ask field))) (when prop (org-bibtex-put name prop))))))) (when (and type (assoc type org-bibtex-types) - (not (org-bibtex-get "CUSTOM_ID"))) + (not (org-bibtex-get org-bibtex-key-property))) (org-bibtex-autokey))) =20 @@ -488,7 +514,7 @@ This uses `bibtex-parse-entry'." (case (car pair) (:title nil) (:type nil) - (:key (org-bibtex-put "CUSTOM_ID" (cdr pair))) + (:key (org-bibtex-put org-bibtex-key-property (cdr pair))) (otherwise (org-bibtex-put (car pair) (cdr pair)))))))) =20 (provide 'org-bibtex) --8<---------------cut here---------------end--------------->8---