@@ -344,14 +344,20 @@ and `org-tags-exclude-from-inheritance'."
344344 (upcase property)))))))
345345 (when it (org-trim it))))
346346
347- (defun org-bibtex-put (property value )
348- (let ((prop (upcase (if (keywordp property)
349- (substring (symbol-name property) 1 )
350- property))))
351- (org-set-property
352- (concat (unless (string= org-bibtex-key-property prop) org-bibtex-prefix)
353- prop)
354- value)))
347+ (defun org-bibtex-put (property value &optional insert-raw )
348+ " Set PROPERTY of headline at point to VALUE.
349+ The PROPERTY will be prefixed with `org-bibtex-prefix' when necessary.
350+ With non-nil optional argument INSERT-RAW, insert node property string
351+ at point."
352+ (let* ((prop (upcase (if (keywordp property)
353+ (substring (symbol-name property) 1 )
354+ property)))
355+ (prop (concat (unless (string= org-bibtex-key-property prop)
356+ org-bibtex-prefix)
357+ prop)))
358+ (if insert-raw
359+ (insert (format " :%s : %s \n " prop value))
360+ (org-set-property prop value))))
355361
356362(defun org-bibtex-headline ()
357363 " Return a bibtex entry of the given headline as a string."
@@ -703,35 +709,44 @@ Return the number of saved entries."
703709 (interactive " fFile: " )
704710 (org-bibtex-read-buffer (find-file-noselect file 'nowarn 'rawfile )))
705711
706- (defun org-bibtex-write ()
707- " Insert a heading built from the first element of `org-bibtex-entries' ."
712+ (defun org-bibtex-write (&optional noindent )
713+ " Insert a heading built from the first element of `org-bibtex-entries' .
714+ When optional argument NOINDENT is non-nil, do not indent the properties
715+ drawer."
708716 (interactive )
709- (when ( = ( length org-bibtex-entries) 0 )
717+ (unless org-bibtex-entries
710718 (error " No entries in `org-bibtex-entries' " ))
711719 (let* ((entry (pop org-bibtex-entries))
712720 (org-special-properties nil ) ; avoids errors with `org-entry-put'
713721 (val (lambda (field ) (cdr (assoc field entry))))
714722 (togtag (lambda (tag ) (org-toggle-tag tag 'on ))))
715723 (org-insert-heading )
716724 (insert (funcall org-bibtex-headline-format-function entry))
717- (org-bibtex-put " TITLE" (funcall val :title ))
725+ (insert " \n :PROPERTIES:\n " )
726+ (org-bibtex-put " TITLE" (funcall val :title ) 'insert )
718727 (org-bibtex-put org-bibtex-type-property-name
719- (downcase (funcall val :type )))
728+ (downcase (funcall val :type ))
729+ 'insert )
720730 (dolist (pair entry)
721731 (pcase (car pair)
722732 (:title nil )
723733 (:type nil )
724- (:key (org-bibtex-put org-bibtex-key-property (cdr pair)))
734+ (:key (org-bibtex-put org-bibtex-key-property (cdr pair) 'insert ))
725735 (:keywords (if org-bibtex-tags-are-keywords
726736 (dolist (kw (split-string (cdr pair) " , *" ))
727737 (funcall
728738 togtag
729739 (replace-regexp-in-string
730740 " [^[:alnum:]_@#%]" " "
731741 (replace-regexp-in-string " [ \t ]+" " _" kw))))
732- (org-bibtex-put (car pair) (cdr pair))))
733- (_ (org-bibtex-put (car pair) (cdr pair)))))
734- (mapc togtag org-bibtex-tags)))
742+ (org-bibtex-put (car pair) (cdr pair) 'insert )))
743+ (_ (org-bibtex-put (car pair) (cdr pair) 'insert ))))
744+ (insert " :END:\n " )
745+ (mapc togtag org-bibtex-tags)
746+ (unless noindent
747+ (org-indent-region
748+ (save-excursion (org-back-to-heading t ) (point ))
749+ (point )))))
735750
736751(defun org-bibtex-yank ()
737752 " If kill ring holds a bibtex entry yank it as an Org headline."
@@ -745,10 +760,12 @@ Return the number of saved entries."
745760(defun org-bibtex-import-from-file (file )
746761 " Read bibtex entries from FILE and insert as Org headlines after point."
747762 (interactive " fFile: " )
748- (dotimes (_ (org-bibtex-read-file file))
749- (save-excursion (org-bibtex-write))
750- (re-search-forward org-property-end-re)
751- (open-line 1 ) (forward-char 1 )))
763+ (let ((pos (point )))
764+ (dotimes (i (org-bibtex-read-file file))
765+ (save-excursion (org-bibtex-write 'noindent ))
766+ (re-search-forward org-property-end-re)
767+ (insert " \n " ))
768+ (org-indent-region pos (point ))))
752769
753770(defun org-bibtex-export-to-kill-ring ()
754771 " Export current headline to kill ring as bibtex entry."
0 commit comments