emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/denote ab18e32e1b 05/14: Merge pull request #38 from je


From: ELPA Syncer
Subject: [elpa] externals/denote ab18e32e1b 05/14: Merge pull request #38 from jeanphilippegg/file-name-permutations
Date: Sat, 9 Jul 2022 02:57:36 -0400 (EDT)

branch: externals/denote
commit ab18e32e1be6ba9a3954f7f9d21a6d6398a8c79b
Merge: 916344321a 564c139c78
Author: Protesilaos Stavrou <info@protesilaos.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #38 from jeanphilippegg/file-name-permutations
    
    More refactors to remove the big regexp
---
 denote-faces.el | 17 +++++++++------
 denote.el       | 67 +++++++++++++++++++++++++--------------------------------
 2 files changed, 39 insertions(+), 45 deletions(-)

diff --git a/denote-faces.el b/denote-faces.el
index d19c913b1c..a0761473e5 100644
--- a/denote-faces.el
+++ b/denote-faces.el
@@ -83,14 +83,17 @@ and seconds."
   :group 'denote-faces)
 
 (defconst denote-faces-file-name-keywords
-  `((,denote--file-regexp
+  `((,denote--id-regexp
      (1 'denote-faces-date)
-     (2 'denote-faces-time)
-     (3 'denote-faces-delimiter)
-     (4 'denote-faces-title)
-     (5 'denote-faces-delimiter)
-     (6 'denote-faces-keywords)
-     (7 'denote-faces-extension))
+     (2 'denote-faces-time))
+    (,denote--title-regexp
+     (1 'denote-faces-title))
+    (,denote--keywords-regexp
+     (1 'denote-faces-keywords))
+    (,denote--extension-regexp
+     (0 'denote-faces-extension))
+    ("--"
+     (0 'denote-faces-delimiter t))
     ("_"
      (0 'denote-faces-delimiter t)))
   "Keywords for fontification of file names.")
diff --git a/denote.el b/denote.el
index eb5aec037a..340ae55df0 100644
--- a/denote.el
+++ b/denote.el
@@ -239,17 +239,14 @@ are described in the doc string of `format-time-string'."
 (defconst denote--id-regexp "\\([0-9]\\{8\\}\\)\\(T[0-9]\\{6\\}\\)"
   "Regular expression to match `denote--id-format'.")
 
-(defconst denote--file-title-regexp
-  (concat denote--id-regexp "\\(--\\)\\(.*\\)\\(__\\)")
-  "Regular expression to match file names from `denote'.")
+(defconst denote--title-regexp "--\\([0-9A-Za-z-]*\\)"
+  "Regular expression to match keywords.")
 
-(defconst denote--file-regexp
-  (concat denote--file-title-regexp "\\([0-9A-Za-z_-]*\\)\\(\\.?.*\\)")
-  "Regular expression to match the entire file name'.")
+(defconst denote--keywords-regexp "__\\([0-9A-Za-z_-]*\\)"
+  "Regular expression to match keywords.")
 
-(defconst denote--file-only-note-regexp
-  (concat denote--file-regexp "\\.\\(org\\|md\\|txt\\)")
-  "Regular expression to match the entire file name of a note file.")
+(defconst denote--extension-regexp "\\.\\(org\\|md\\|txt\\)"
+  "Regular expression to match supported Denote extensions.")
 
 (defconst denote--punctuation-regexp "[][{}!@#$%^&*()_=+'\"?,.\|;:~`‘’“”/]*"
   "Regular expression of punctionation that should be removed.
@@ -279,16 +276,6 @@ We consider those characters illigal for our purposes.")
       (make-directory path t))
     (file-name-as-directory path)))
 
-(defun denote--extract (regexp str &optional group)
-  "Extract REGEXP from STR, with optional regexp GROUP."
-  (when group
-    (unless (and (integerp group) (> group 0))
-      (error "`%s' is not a positive integer" group)))
-  (with-temp-buffer
-    (insert str)
-    (when (re-search-forward regexp nil t -1)
-      (match-string (or group 1)))))
-
 (defun denote--slug-no-punct (str)
   "Convert STR to a file name slug."
   (replace-regexp-in-string denote--punctuation-regexp "" str))
@@ -328,10 +315,13 @@ trailing hyphen."
 (defun denote--only-note-p (file)
   "Make sure FILE is an actual Denote note.
 FILE is relative to the variable `denote-directory'."
-  (and (not (file-directory-p file))
-       (file-regular-p file)
-       (string-match-p denote--file-only-note-regexp file)
-       (not (string-match-p "[#~]\\'" file))))
+  (let ((file-name (file-name-nondirectory file)))
+    (and (not (file-directory-p file))
+         (file-regular-p file)
+         (string-match-p (concat "\\`" denote--id-regexp
+                                 ".*" denote--extension-regexp "\\'")
+                         file-name)
+         (not (string-match-p "[#~]\\'" file)))))
 
 (defun denote--file-name-relative-to-denote-directory (file)
   "Return file name of FILE relative to the variable `denote-directory'.
@@ -395,24 +385,25 @@ part of the list."
         f))
     (denote--directory-files))))
 
-(defun denote--keywords-in-files ()
-  "Produce list of keywords in `denote--directory-files'."
-  (delq nil (mapcar
-             (lambda (x)
-               (denote--extract denote--file-regexp x 6))
-             ;; REVIEW 2022-07-03: I tested this with ~3000 files.  It
-             ;; has about 2 seconds of delay on my end.  After I placed
-             ;; the list of those files in a variable instead of calling
-             ;; `denote--directory-files', there was no noticeable
-             ;; performance penalty.
-             (denote--directory-files))))
+(defun denote--extract-keywords-from-path (path)
+  "Extract keywords from PATH."
+  (let* ((file-name (file-name-nondirectory path))
+         (kws (when (string-match denote--keywords-regexp file-name)
+                (match-string-no-properties 1 file-name))))
+    (when kws
+      (split-string kws "_"))))
 
 (defun denote--inferred-keywords ()
   "Extract keywords from `denote--directory-files'."
-  (let ((sequence (denote--keywords-in-files)))
-    (mapcan (lambda (s)
-              (split-string s "_" t))
-            sequence)))
+  (delete-dups
+   (mapcan (lambda (p)
+             (denote--extract-keywords-from-path p))
+           ;; REVIEW 2022-07-03: I tested this with ~3000 files.  It
+           ;; has about 2 seconds of delay on my end.  After I placed
+           ;; the list of those files in a variable instead of calling
+           ;; `denote--directory-files', there was no noticeable
+           ;; performance penalty.
+           (denote--directory-files))))
 
 (defun denote-keywords ()
   "Return appropriate list of keyword candidates.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]