From 6184e74ca8256d322276b50b0a4ea2ab072bb83a Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Thu, 25 Jun 2020 15:06:51 +0100 Subject: Use lexical-binding and byte-compile tests * Makefile (test, clean): Byte-compile test suite to catch more errors. * bog-tests.el: Use lexical-binding. Remove the need for cl-lib by replacing single cl-gensym with make-symbol. Declare 'citekey' as a special variable. Add footer line. (bog-tests-with-temp-dir): Replace cl-gensym with make-symbol. (bog-tests-with-temp-text): Allow instrumenting for debugging. Evaluate arguments only once. Don't assume 'citekey' is bound. Pass non-nil FIXEDCASE and LITERAL arguments to replace-match. Simplify with buffer rather than string manipulation. (bog-file-citekeys/multiple-variants): Fix typo caught by byte-compilation. * bog.el: Use lexical-binding. Quote function symbols as such. (bog--with-citekey-cache): Allow instrumenting for debugging. Replace cl-gensym with make-symbol. (bog-selection-method): Simplify and reindent. (bog--agenda-map): New keymap. (bog--with-search-lprops): Use it instead of generating it on the fly. Allow instrumenting for debugging. Don't use org-let which calls eval without lexical-binding. Bind uninterned symbol around body. Use unwind-protect to ensure org-lprops are restored. (bog-search-notes, bog-agenda-redo): Unquote body passed to bog--with-search-lprops now that it no longer uses org-let. (bog-command-map): Make docstring consistent with that of other keymaps. --- bog-tests.el | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'bog-tests.el') diff --git a/bog-tests.el b/bog-tests.el index b1b2ea1..3a1539c 100644 --- a/bog-tests.el +++ b/bog-tests.el @@ -1,4 +1,4 @@ -;;; bog-tests.el --- Tests for Bog +;;; bog-tests.el --- Tests for Bog -*- lexical-binding: t -*- ;; Copyright (C) 2013-2016 Kyle Meyer @@ -19,15 +19,17 @@ ;;; Code: +(require 'bog) (require 'ert) (require 'org) -(require 'cl-lib) -(require 'bog) + +(with-no-warnings ;; Silence "lacks a prefix" warning. + (defvar citekey)) ;; Modified from magit-tests.el. (defmacro bog-tests-with-temp-dir (&rest body) (declare (indent 0) (debug t)) - (let ((dir (cl-gensym))) + (let ((dir (make-symbol "dir"))) `(let ((,dir (file-name-as-directory (make-temp-file "dir" t)))) (unwind-protect (let ((default-directory ,dir)) ,@body) @@ -44,21 +46,18 @@ value of the variable `citekey'. If the string \"\" appears in TEXT then remove it and place the point there before running BODY, otherwise place the point at the beginning of the inserted text." - (declare (indent 1)) - `(let* ((inside-text (if (stringp ,text) ,text (eval ,text))) - (is-citekey (string-match "" inside-text))) - (when (and is-citekey citekey) - (setq inside-text (replace-match citekey nil nil inside-text))) - (with-temp-buffer - (org-mode) - (let ((point (string-match "" inside-text))) - (if point - (progn - (insert (replace-match "" nil nil inside-text)) - (goto-char (1+ (match-beginning 0)))) - (insert inside-text) - (goto-char (point-min)))) - ,@body))) + (declare (indent 1) (debug t)) + `(with-temp-buffer + (org-mode) + (insert ,text) + (goto-char (point-min)) + (when (and (bound-and-true-p citekey) + (search-forward "" nil t)) + (replace-match citekey t t)) + (goto-char (point-min)) + (when (search-forward "" nil t) + (replace-match "" t t)) + ,@body)) ;;; Citekey functions @@ -410,7 +409,7 @@ some text" (concat citekey ".txt") (concat citekey "_0.pdf") (concat citekey "-supplement.pdf"))) - found-files) + files-found) (make-directory bog-file-directory) (dolist (var variants) (write-region "" nil (expand-file-name var bog-file-directory))) @@ -582,3 +581,5 @@ some text" (sort (bog--find-duplicates (list "a" "b" "c" "b" "a")) #'string-lessp)))) + +;;; bog-tests.el ends here -- cgit v1.2.3 From 30fc844a24954601ff3294a43c89f2407760e024 Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Thu, 25 Jun 2020 14:45:26 +0100 Subject: Reindent code Fix indentation and flatten the progn in bog-mode. --- bog-tests.el | 110 ++++++++++++++++++++++++++++++----------------------------- bog.el | 29 ++++++++-------- 2 files changed, 70 insertions(+), 69 deletions(-) (limited to 'bog-tests.el') diff --git a/bog-tests.el b/bog-tests.el index 3a1539c..267c10f 100644 --- a/bog-tests.el +++ b/bog-tests.el @@ -240,22 +240,22 @@ some text and " (ert-deftest bog-citekeys-in-buffer () (should (equal '("abc1900def" "ghi1950jkl" "mno2000pqr") - (bog-tests-with-temp-text - " + (bog-tests-with-temp-text + " * abc1900def ghi1950jkl * mno2000pqr * mno2000pqr" - (sort (bog-citekeys-in-buffer) #'string-lessp))))) + (sort (bog-citekeys-in-buffer) #'string-lessp))))) (ert-deftest bog-heading-citekeys-in-buffer () (should (equal '("abc1900def" "mno2000pqr") - (bog-tests-with-temp-text - " + (bog-tests-with-temp-text + " * abc1900def ghi1950jkl * mno2000pqr" - (bog-heading-citekeys-in-buffer))))) + (bog-heading-citekeys-in-buffer))))) (ert-deftest bog-next-non-heading-citekey/default-arg () (let ((citekey "name2010word")) @@ -352,69 +352,71 @@ other2000key " (ert-deftest bog-all-file-citekeys () (bog-tests-with-temp-dir - (let ((bog-file-directory (expand-file-name "citekey-files"))) - (make-directory bog-file-directory) - (let ((default-directory bog-file-directory)) - (make-directory "key2000butdir")) - (write-region "" nil (expand-file-name "nokey.pdf" bog-file-directory)) - (write-region "" nil (expand-file-name "one2010key.pdf" bog-file-directory)) - (write-region "" nil (expand-file-name "two1980key.txt" bog-file-directory)) - (should (equal (bog-all-file-citekeys) - '("one2010key" "two1980key")))))) + (let ((bog-file-directory (expand-file-name "citekey-files"))) + (make-directory bog-file-directory) + (let ((default-directory bog-file-directory)) + (make-directory "key2000butdir")) + (write-region "" nil (expand-file-name "nokey.pdf" bog-file-directory)) + (write-region "" nil (expand-file-name "one2010key.pdf" + bog-file-directory)) + (write-region "" nil (expand-file-name "two1980key.txt" + bog-file-directory)) + (should (equal (bog-all-file-citekeys) + '("one2010key" "two1980key")))))) (ert-deftest bog-rename-staged-file-to-citekey/one-file () (bog-tests-with-temp-dir - (let ((bog-stage-directory (expand-file-name "stage")) - (bog-file-directory (expand-file-name "citekey-files")) - (citekey "name2010word")) - (make-directory bog-stage-directory) - (make-directory bog-file-directory) - (write-region "" nil (expand-file-name "one.pdf" bog-stage-directory)) - (bog-tests-with-temp-text - " + (let ((bog-stage-directory (expand-file-name "stage")) + (bog-file-directory (expand-file-name "citekey-files")) + (citekey "name2010word")) + (make-directory bog-stage-directory) + (make-directory bog-file-directory) + (write-region "" nil (expand-file-name "one.pdf" bog-stage-directory)) + (bog-tests-with-temp-text + " * top level ** some text" - (bog-rename-staged-file-to-citekey)) - (should (file-exists-p (expand-file-name - (concat citekey ".pdf") bog-file-directory))) - (should-not (file-exists-p (expand-file-name - "one.pdf" bog-stage-directory)))))) + (bog-rename-staged-file-to-citekey)) + (should (file-exists-p (expand-file-name + (concat citekey ".pdf") bog-file-directory))) + (should-not (file-exists-p (expand-file-name + "one.pdf" bog-stage-directory)))))) (ert-deftest bog-rename-staged-file-to-citekey/one-file-subdir () (bog-tests-with-temp-dir - (let ((bog-stage-directory (expand-file-name "stage")) - (bog-file-directory (expand-file-name "citekey-files")) - (citekey "name2010word") - (bog-subdirectory-group 2)) - (make-directory bog-stage-directory) - (make-directory bog-file-directory) - (write-region "" nil (expand-file-name "one.pdf" bog-stage-directory)) - (bog-tests-with-temp-text - " + (let ((bog-stage-directory (expand-file-name "stage")) + (bog-file-directory (expand-file-name "citekey-files")) + (citekey "name2010word") + (bog-subdirectory-group 2)) + (make-directory bog-stage-directory) + (make-directory bog-file-directory) + (write-region "" nil (expand-file-name "one.pdf" bog-stage-directory)) + (bog-tests-with-temp-text + " * top level ** some text" - (bog-rename-staged-file-to-citekey)) - (should (file-exists-p (expand-file-name - (concat "2010/" citekey ".pdf") bog-file-directory))) - (should-not (file-exists-p (expand-file-name - "one.pdf" bog-stage-directory)))))) + (bog-rename-staged-file-to-citekey)) + (should (file-exists-p (expand-file-name (concat "2010/" citekey ".pdf") + bog-file-directory))) + (should-not (file-exists-p (expand-file-name + "one.pdf" bog-stage-directory)))))) (ert-deftest bog-file-citekeys/multiple-variants () (bog-tests-with-temp-dir - (let* ((bog-file-directory (expand-file-name "citekey-files")) - (citekey "name2010word") - (variants (list (concat citekey ".pdf") - (concat citekey ".txt") - (concat citekey "_0.pdf") - (concat citekey "-supplement.pdf"))) - files-found) - (make-directory bog-file-directory) - (dolist (var variants) - (write-region "" nil (expand-file-name var bog-file-directory))) - (setq files-found (bog-citekey-files citekey)) - (should (= (length files-found) 4))))) + (let* ((bog-file-directory (expand-file-name "citekey-files")) + (citekey "name2010word") + (variants (list (concat citekey ".pdf") + (concat citekey ".txt") + (concat citekey "_0.pdf") + (concat citekey "-supplement.pdf"))) + files-found) + (make-directory bog-file-directory) + (dolist (var variants) + (write-region "" nil (expand-file-name var bog-file-directory))) + (setq files-found (bog-citekey-files citekey)) + (should (= (length files-found) 4))))) ;;; BibTeX functions diff --git a/bog.el b/bog.el index 2739caf..f36e41b 100644 --- a/bog.el +++ b/bog.el @@ -1142,7 +1142,7 @@ If the citekey prompt is slow to appear, consider enabling the (citekey (bog-citekey-from-point-or-all-headings no-context)) (marker (with-current-buffer (or (buffer-base-buffer) (current-buffer)) - (bog--find-citekey-heading-in-notes citekey)))) + (bog--find-citekey-heading-in-notes citekey)))) (if marker (with-current-buffer (marker-buffer marker) (org-with-wide-buffer @@ -1433,20 +1433,19 @@ if ARG is omitted or nil. \\{bog-mode-map}" :lighter " Bog" - (progn - (cond - (bog-mode - (if (derived-mode-p 'org-mode) - (add-hook 'org-font-lock-hook #'bog-fontify-non-heading-citekeys) - (font-lock-add-keywords nil bog-citekey-font-lock-keywords))) - (t - (if (derived-mode-p 'org-mode) - (remove-hook 'org-font-lock-hook #'bog-fontify-non-heading-citekeys) - (font-lock-remove-keywords nil bog-citekey-font-lock-keywords)) - (when (bound-and-true-p bog-view-mode) - (bog-view-mode -1)))) - (when font-lock-mode - (funcall bog-font-lock-function)))) + (cond + (bog-mode + (if (derived-mode-p 'org-mode) + (add-hook 'org-font-lock-hook #'bog-fontify-non-heading-citekeys) + (font-lock-add-keywords nil bog-citekey-font-lock-keywords))) + (t + (if (derived-mode-p 'org-mode) + (remove-hook 'org-font-lock-hook #'bog-fontify-non-heading-citekeys) + (font-lock-remove-keywords nil bog-citekey-font-lock-keywords)) + (when (bound-and-true-p bog-view-mode) + (bog-view-mode -1)))) + (when font-lock-mode + (funcall bog-font-lock-function))) ;;; View minor mode -- cgit v1.2.3 From c2cbbd99e2b6b9b79c15acec025333089dbea920 Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Fri, 26 Jun 2020 09:20:00 +0100 Subject: Update copyright notices for 2020 --- bog-tests.el | 1 + bog.el | 1 + 2 files changed, 2 insertions(+) (limited to 'bog-tests.el') diff --git a/bog-tests.el b/bog-tests.el index 267c10f..01c7f08 100644 --- a/bog-tests.el +++ b/bog-tests.el @@ -1,6 +1,7 @@ ;;; bog-tests.el --- Tests for Bog -*- lexical-binding: t -*- ;; Copyright (C) 2013-2016 Kyle Meyer +;; Copyright (C) 2020 Basil L. Contovounesios ;; Author: Kyle Meyer diff --git a/bog.el b/bog.el index 16def76..2e1d5d9 100644 --- a/bog.el +++ b/bog.el @@ -1,6 +1,7 @@ ;;; bog.el --- Extensions for research notes in Org mode -*- lexical-binding: t -*- ;; Copyright (C) 2013-2016 Kyle Meyer +;; Copyright (C) 2020 Basil L. Contovounesios ;; Author: Kyle Meyer ;; URL: https://github.com/kyleam/bog -- cgit v1.2.3