From b9c2f006c8a8a4358ac30fec7c7523da8ff5350f Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Fri, 31 Oct 2014 02:08:13 -0400 Subject: Rework citekey collection - Split into two functions instead of having no-sort argument. - Use push instead of add-to-list. --- bog-tests.el | 10 ++-------- bog.el | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/bog-tests.el b/bog-tests.el index 3bc5781..022275e 100644 --- a/bog-tests.el +++ b/bog-tests.el @@ -274,18 +274,12 @@ ;; `bog-collect-references' -(ert-deftest bog-collect-references () +(ert-deftest bog-collect-unique-references () (with-temp-buffer (insert "abc1900word\nhij2000word\nefg1800word\n") - (should (equal (bog-collect-references) + (should (equal (bog-collect-unique-references) '("abc1900word" "efg1800word" "hij2000word"))))) -(ert-deftest bog-collect-references-no-sort () - (with-temp-buffer - (insert "abc1900word\nhij2000word\nefg1800word\n") - (should (equal (bog-collect-references t) - '("efg1800word" "hij2000word" "abc1900word"))))) - ;; `bog-sort-topic-headings-in-buffer' (ert-deftest bog-sort-topic-headings-in-buffer () diff --git a/bog.el b/bog.el index d3f6f92..15ece92 100644 --- a/bog.el +++ b/bog.el @@ -529,7 +529,7 @@ one entry per BibTeX file." "Create buffer that has entries for all citekeys in buffer." (interactive) (let ((bib-buffer (get-buffer-create "*Bib*")) - (refs (-map 'bog-citekey-as-bib (bog-collect-references)))) + (refs (-map 'bog-citekey-as-bib (bog-collect-unique-references)))) (--each refs (unless (file-exists-p it) (user-error "%s does not exist" it))) (switch-to-buffer-other-window bib-buffer) (--each refs @@ -539,18 +539,20 @@ one entry per BibTeX file." (bibtex-mode) (goto-char (point-min)))) -(defun bog-collect-references (&optional no-sort) +(defun bog-collect-unique-references () "Return names in buffer that match `bog-citekey-format'. -If NO-SORT, citekeys are returned in reverse order that they -occur in buffer instead of alphabetical order." +Duplicates are removed and the entries are sorted +alphabetically." + (-distinct (--sort (string-lessp it other) (bog-collect-references)))) + +(defun bog-collect-references () + "Return names in buffer that match `bog-citekey-format'." (let (refs) (save-excursion (goto-char (point-min)) (while (re-search-forward bog-citekey-format nil t) - (add-to-list 'refs (match-string-no-properties 0))) - (if no-sort - refs - (--sort (string-lessp it other) refs))))) + (push (match-string-no-properties 0) refs)) + refs))) (defun bog-citekey-as-bib (citekey) (expand-file-name (concat citekey ".bib") bog-bib-directory)) -- cgit v1.2.3