branch: externals/denote-review
commit d90b049ce40af36b3369e5f2bd6476bae5d9e483
Author: Matto Fransen <[email protected]>
Commit: Matto Fransen <[email protected]>

    use of with-temp-buffer and refactored
---
 denote-review.el | 158 +++++++++++++++++++++----------------------------------
 1 file changed, 60 insertions(+), 98 deletions(-)

diff --git a/denote-review.el b/denote-review.el
index de0e5a0812..830f92ba14 100644
--- a/denote-review.el
+++ b/denote-review.el
@@ -83,30 +83,27 @@ Defaults to regexp for org filetype."
 
 ;; Setting and getting the reviewdate
 
-(defun denote-review-insert-reviewdate-line (mydate)
-  "Insert the review date MYDATE frontmatter line.
+(defun denote-review-insert-reviewdate-line (date)
+  "Insert the review date DATE frontmatter line.
 Format according to variable `denote-file-type'.
 Insert just after the identifier line."
   (cond ((eq denote-file-type 'markdown-yaml)
-         (format "reviewdate: %s" mydate))
+         (format "reviewdate: %s" date))
         ((eq denote-file-type 'markdown-toml)
-         (format "reviewdate = %s" mydate))
+         (format "reviewdate = %s" date))
         ((eq denote-file-type 'text)
-         (format "reviewdate: %s" mydate))
-        (t (format "#+reviewdate: [%s]" mydate))))
+         (format "reviewdate: %s" date))
+        (t (format "#+reviewdate: [%s]" date))))
 
 (defun denote-review-insert-date (&optional thisdate insert-regexp)
   "Insert current date in ISO 8601 format as reviewdate.
 Or use THISDATE, when not nil.
 INSERT-REGEXP is regepx to search for appropriate insert location."
   (goto-char (point-min))
-  (let ((mydate (format-time-string "%F")))
-    (unless (null thisdate)
-      (setq mydate thisdate))
+  (let ((date (or thisdate (format-time-string "%F"))))
     (re-search-forward insert-regexp nil t)
-    (end-of-line)
-    (newline)
-    (insert (denote-review-insert-reviewdate-line mydate))))
+    (goto-char (line-end-position))
+    (insert "\n" (denote-review-insert-reviewdate-line date))))
 
 ;;;###autoload
 (defun denote-review-set-date ()
@@ -151,20 +148,20 @@ Both regexp's set to match format based on variable 
`denote-file-type'"
 (defun denote-review-bulk-set-date (filename current-date-p)
   "Opens FILENAME and insert a reviewdate.
 When CURRENT-DATE-P is not null, use current date."
-  (let ((fpath filename)
-        (fname (file-name-nondirectory filename))
+  (let ((fname (file-name-nondirectory filename))
         (search-regexp (denote-review-search-regexp-for-filetype))
         (insert-regexp (denote-review-insert-regexp-location-for-filetype)))
-    (find-file fpath)
-    (if (null current-date-p)
-        (denote-review-set-initial-date
-         (denote-review-get-date-from-filename fname)
-         search-regexp insert-regexp)
-      (denote-review-set-initial-date (format-time-string "%F")
-                                      search-regexp
-                                      insert-regexp))
-    (save-buffer)
-    (kill-buffer fname)))
+    (with-temp-buffer
+      (insert-file-contents filename)
+      (goto-char (point-min))
+      (if (null current-date-p)
+          (denote-review-set-initial-date
+           (denote-review-get-date-from-filename fname)
+           search-regexp insert-regexp)
+        (denote-review-set-initial-date (format-time-string "%F")
+                                        search-regexp
+                                        insert-regexp))
+      (write-region nil nil filename))))
 
 (defun denote-review-set-date-dired-marked-files ()
   "Insert a reviewdate in the marked files.
@@ -172,25 +169,24 @@ Set a reviewdate according the identifier in the filename,
 when called with the Universal Argument use current date.
 Does not overwrite existing reviewdates."
   (interactive)
-  (if (eq major-mode 'dired-mode)
-      (mapcar (lambda (file)
-                (when (denote-file-is-writable-and-supported-p file)
-                  (denote-review-bulk-set-date file current-prefix-arg)))
-              (dired-get-marked-files))
-    (error (format "Command can only be used in a Dired buffer."))))
+  (unless (derived-mode-p 'dired-mode)
+    (error (format "Command can only be used in a Dired buffer.")))
+  (dolist (file (dired-get-marked-files))
+    (when (denote-file-is-writable-and-supported-p file)
+      (denote-review-bulk-set-date file current-prefix-arg))))
 
 ;; Collect keywords and prompt for a keyword to filter by.
 
 (defun denote-review-get-path ()
   "Prompt for a path when needed."
-  (let ((mypath '()))
+  (let ((path '()))
     (when (boundp 'denote-directory)
-      (setq mypath (append mypath denote-directory)))
+      (setq path denote-directory))
     (when (boundp 'denote-silo-directories)
-      (setq mypath (append mypath denote-silo-directories)))
-    (if (listp mypath)
+      (setq path (append path denote-silo-directories)))
+    (if (listp path)
         (completing-read
-         "Select a directory (using completion): " mypath)
+         "Select a directory (using completion): " path)
       denote-directory)))
 
 (defun denote-review-get-keyword-list (denotepath)
@@ -210,83 +206,49 @@ Does not overwrite existing reviewdates."
   (let ((denotepath (denote-review-get-path)))
     (cons denotepath
           (completing-read
-           "Select a keyword (using completion) :"
-           (append (list "All")
-                   (denote-review-get-keyword-list denotepath))))))
+           "Select a keyword (using completion): "
+           (denote-review-get-keyword-list denotepath)))))
 
 ;; Collect data to fill the tabular mode list
 
-(defun denote-review-check-date-of-file (myfile search-regexp)
-  "Get the reviewdate of MYFILE.
+(defun denote-review-check-date-of-file (filename search-regexp)
+  "Get the reviewdate of FILENAME.
 SEARCH-REGEXP is regexp to search for reviewdate.
 It is set to match format based on variable `denote-file-type'"
-  (let ((mybuffer (find-file myfile))
-        myreviewdate)
-    (setq myreviewdate (denote-review-get-date
-                        search-regexp))
-    (kill-buffer mybuffer)
-    myreviewdate))
+ (with-temp-buffer
+   (insert-file-contents filename)
+   (denote-review-get-date
+    search-regexp)))
 
 (defun denote-review-collect-files (denotepath-and-keyword)
   "Fetch reviewdate from the files in DENOTEPATH-AND-KEYWORD.
 Filter filenames according to DENOTEPATH-AND-KEYWORD.
 DENOTEPATH-AND-KEYWORD is a cons of a path and a keyword.
 Create a list in the format required by `tabulated-list-mode'."
-  (let ((list-of-files '())
-        (search-regexp (denote-review-search-regexp-for-filetype))
+  (let ((search-regexp (denote-review-search-regexp-for-filetype))
         (denote-directory (car denotepath-and-keyword)))
-    (save-excursion
-      (mapc
-       (lambda (myfile)
-         (when (or (string= (cdr denotepath-and-keyword) "All")
-                   (string-match
-                    (format "_%s" (cdr denotepath-and-keyword)) myfile))
-           (let ((reviewdate (denote-review-check-date-of-file
-                              myfile
-                              search-regexp)))
-             (unless (null reviewdate)
-               (push (list myfile
-                           (vector
-                            reviewdate
-                            (file-name-nondirectory myfile)))
-                     list-of-files)))))
-       (denote-directory-files nil t nil)))
-    (when (null list-of-files)
-      (error (format
-              "No files with a reviewdate found (filter: keyword %s)"
-              (cdr denotepath-and-keyword))))
-    list-of-files))
+    (or (mapcan (lambda (myfile)
+                 (and (or (string= (cdr denotepath-and-keyword) "")
+                          (string-match
+                           (format "_%s" (cdr denotepath-and-keyword)) myfile))
+                      (and-let* ((reviewdate (denote-review-check-date-of-file
+                                              myfile
+                                              search-regexp)))
+                        `((,myfile
+                           [,reviewdate
+                            ,(file-name-nondirectory myfile)])))))
+               (denote-directory-files nil t nil))
+       (error (format "No files with a reviewdate found (filter: keyword %s)"
+                      (cdr denotepath-and-keyword))))))
 
 (defun denote-review-collect-files--revert (denotepath-and-keyword)
   "Re-populated `tabulated-list-entries'.
 DENOTEPATH-AND-KEYWORD is a cons of a path and a keyword."
-  (let ((list-of-files '())
-        (search-regexp (denote-review-search-regexp-for-filetype))
-        (denote-directory (car denotepath-and-keyword)))
-    (save-excursion
-      (mapc
-       (lambda (myfile)
-         (when (or (string= (cdr denotepath-and-keyword) "All")
-                   (string-match
-                    (format "_%s" (cdr denotepath-and-keyword)) myfile))
-           (let ((reviewdate (denote-review-check-date-of-file
-                              myfile
-                              search-regexp)))
-             (unless (null reviewdate)
-               (push (list myfile
-                           (vector
-                            reviewdate
-                            (file-name-nondirectory myfile)))
-                     list-of-files)))))
-       (denote-directory-files nil t nil)))
-    (when (null list-of-files)
-      (error (format
-              "No files with a reviewdate found (filter: keyword %s)"
-              (cdr denotepath-and-keyword))))
-    (setq tabulated-list-entries list-of-files)
-    (setq tabulated-list-sort-key (cons "Reviewdate" nil))
-    (tabulated-list-init-header)
-    (tabulated-list-print t)))
+  (setq tabulated-list-entries
+        (denote-review-collect-files denotepath-and-keyword))
+  (setq tabulated-list-sort-key (cons "Reviewdate" nil))
+  (tabulated-list-init-header)
+  (tabulated-list-print t))
 
 ;; Mode map for tabulated list and actions.
 
@@ -317,7 +279,6 @@ Must be called from the tabulated list view."
 
 (defvar-keymap denote-review-mode-map
   :doc "Keymap for `denote-review-mode-map'."
-  :parent tabulated-list-mode-map
   "RET" #'denote-review-goto-file
   "e" #'denote-review-edit-file
   "o" #'denote-review-read-only-goto-file
@@ -348,7 +309,8 @@ Filter by keyword."
                                   denotepath-and-keyword))
     (add-hook 'tabulated-list-revert-hook
               (lambda ()
-                (denote-review-collect-files--revert denotepath-and-keyword)) 
0 t)
+                (denote-review-collect-files--revert denotepath-and-keyword))
+             nil t)
     (tabulated-list-print t)
     (display-buffer (current-buffer))
     (setq mode-line-buffer-identification

Reply via email to