Skip to content

Commit 5ba90e1

Browse files
committed
org-bibtex-import-from-file: Improve performance
* lisp/ol-bibtex.el (org-bibtex-put): Add new optional argument to insert node property at point directly, without leveraging `org-set-property'. Add docstring. (org-bibtex-write): Insert headline properties directly. Add new optional argument to suppress indentation. Do not use `length' to check if `org-bibtex-entries' list empty---`length' is too slow on large lists. (org-bibtex-import-from-file): Postpone indentation after all the entries are imported. * lisp/org.el (org-indent-region): Use cache.
1 parent 372788a commit 5ba90e1

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

lisp/ol-bibtex.el

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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."

lisp/org.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18734,7 +18734,7 @@ assumed to be significant there."
1873418734
(end (copy-marker end)))
1873518735
(while (< (point) end)
1873618736
(if (or (looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
18737-
(let* ((element (org-element-at-point-no-context))
18737+
(let* ((element (org-element-at-point))
1873818738
(type (org-element-type element))
1873918739
(element-end (copy-marker (org-element-property :end element)))
1874018740
(ind (org--get-expected-indentation element nil)))

0 commit comments

Comments
 (0)