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

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

[elpa] externals/denote f89f9ee963 4/6: Make denote-date-prompt accept o


From: ELPA Syncer
Subject: [elpa] externals/denote f89f9ee963 4/6: Make denote-date-prompt accept optional INITIAL-DATE and PROMPT-TEXT
Date: Mon, 9 Dec 2024 06:58:48 -0500 (EST)

branch: externals/denote
commit f89f9ee963ff2c0bcb56a99623d71a0218e4a0e5
Author: Protesilaos Stavrou <info@protesilaos.com>
Commit: Protesilaos Stavrou <info@protesilaos.com>

    Make denote-date-prompt accept optional INITIAL-DATE and PROMPT-TEXT
    
    Thanks to Alex Griffin for starting the discussion about this feature
    and to Jean-Philippe Gagné Guay for contributing to it. It was done in
    issue 489: <https://github.com/protesilaos/denote/issues/489>.
---
 README.org           | 19 +++++++++++------
 denote.el            | 58 ++++++++++++++++++++++++++++++++++++++++++----------
 tests/denote-test.el | 19 +++++++++++++++++
 3 files changed, 79 insertions(+), 17 deletions(-)

diff --git a/README.org b/README.org
index a0e4606641..ca0130be67 100644
--- a/README.org
+++ b/README.org
@@ -5915,10 +5915,17 @@ might change them without further notice.
 
 #+findex: denote-date-prompt
 + Function ~denote-date-prompt~ :: Prompt for date, expecting
-  =YYYY-MM-DD= or that plus =HH:MM= (or even =HH:MM:SS=).  Use Org's
+  =YYYY-MM-DD= or that plus =HH:MM= (or even =HH:MM:SS=). Use Org's
   more advanced date selection utility if the user option
-  ~denote-date-prompt-use-org-read-date~ is non-nil.  It requires Org
-  ([[#h:e7ef08d6-af1b-4ab3-bb00-494a653e6d63][The 
denote-date-prompt-use-org-read-date option]]).
+  ~denote-date-prompt-use-org-read-date~ is non-nil. It requires Org
+  ([[#h:e7ef08d6-af1b-4ab3-bb00-494a653e6d63][The 
denote-date-prompt-use-org-read-date option]]). With optional
+  =INITIAL-DATE= use it as the initial minibuffer text. With optional
+  =PROMPT-TEXT= use it in the minibuffer instead of the default
+  prompt. When ~denote-date-prompt-use-org-read-date~ is non-nil, the
+  value of =INITIAL-DATE= is of the format understood by
+  ~org-read-date~. Otherwise, it is a string that can be processed by
+  ~denote-valid-date-p~. [ The =INITIAL-DATE= and =PROMPT-TEXT= are
+  part of {{{development-version}}}. ]
 
 #+findex: denote-command-prompt
 + Function ~denote-command-prompt~ ::  Prompt for command among
@@ -6885,9 +6892,9 @@ Denote is meant to be a collective effort.  Every bit of 
help matters.
   maxbrieiev, mentalisttraceur, pmenair, relict007, skissue.
 
 + Ideas and/or user feedback :: Abin Simon, Aditya Yadav, Alan
-  Schmitt, Aleksandr Vityazev, Alex Hirschfeld, Alexis Purslane,
-  Alfredo Borrás, Alp Eren Kose, Ashton Wiersdorf, Benjamin Kästner,
-  Claudio Migliorelli, Claudiu Tănăselia, Colin McLear,
+  Schmitt, Aleksandr Vityazev, Alex Griffin, Alex Hirschfeld, Alexis
+  Purslane, Alfredo Borrás, Alp Eren Kose, Ashton Wiersdorf, Benjamin
+  Kästner, Claudio Migliorelli, Claudiu Tănăselia, Colin McLear,
   Cosmin-Octavian C, Damien Cassou, Elias Storms, Federico Stilman,
   Florian, Frédéric Willem Frank Ehmsen, Glenna D., Guo Yong,
   Hanspeter Gisler Harold Ollivier, IceAsteroid, Jack Baty, Jay
diff --git a/denote.el b/denote.el
index bd9c4c4c03..d359f3f9fd 100644
--- a/denote.el
+++ b/denote.el
@@ -2707,13 +2707,38 @@ here for clarity."
 
 (declare-function org-read-date "org" (&optional with-time to-time from-string 
prompt default-time default-input inactive))
 
-(defun denote-date-prompt ()
+(defun denote--date-convert (date prefer-type)
+  "Determine how to convert DATE to PREFER-TYPE `:list' or `:string'."
+  (let ((parsed-date (denote-valid-date-p date)))
+    (unless (memq prefer-type '(:list :string))
+      (error "The PREFER-TYPE must be either `:list' or `:string'"))
+    (cond
+     ((listp date)
+      (if (eq prefer-type :list)
+          parsed-date
+        (format-time-string "%F %T" date)))
+     ((stringp date)
+      (if (eq prefer-type :string)
+          date
+        parsed-date))
+     (t
+      (error "The `%s' is neither a list nor a string" date)))))
+
+(defun denote-date-prompt (&optional initial-date prompt-text)
   "Prompt for date, expecting YYYY-MM-DD or that plus HH:MM.
 Use Org's more advanced date selection utility if the user option
-`denote-date-prompt-use-org-read-date' is non-nil."
+`denote-date-prompt-use-org-read-date' is non-nil.
+
+With optional INITIAL-DATE use it as the initial minibuffer
+text.  With optional PROMPT-TEXT use it in the minibuffer instead
+of the default prompt.
+
+When `denote-date-prompt-use-org-read-date' is non-nil, the value of
+INITIAL-DATE is of the format understood by `org-read-date'.  Otherwise,
+it is a string that can be processed by `denote-valid-date-p'."
   (if (and denote-date-prompt-use-org-read-date
            (require 'org nil :no-error))
-      (let* ((time (org-read-date nil t))
+      (let* ((time (org-read-date nil t nil prompt-text (denote--date-convert 
initial-date :list)))
              (org-time-seconds (format-time-string "%S" time))
              (cur-time-seconds (format-time-string "%S" (current-time))))
         ;; When the user does not input a time, org-read-date defaults to 00 
for seconds.
@@ -2722,12 +2747,19 @@ Use Org's more advanced date selection utility if the 
user option
           (setq time (time-add time (string-to-number cur-time-seconds))))
         (format-time-string "%Y-%m-%d %H:%M:%S" time))
     (read-string
-     "DATE and TIME for note (e.g. 2022-06-16 14:30): "
-     nil 'denote-date-history)))
-
-(defun denote-prompt-for-date-return-id ()
-  "Use `denote-date-prompt' and return it as `denote-id-format'."
-  (denote-get-identifier (denote-valid-date-p (denote-date-prompt))))
+     (or
+      "DATE and TIME for note (e.g. 2022-06-16 14:30): "
+      prompt-text)
+     (denote--date-convert initial-date :string)
+     'denote-date-history)))
+
+(defun denote-prompt-for-date-return-id (&optional initial-date prompt-text)
+  "Use `denote-date-prompt' and return it as `denote-id-format'.
+Optional INITIAL-DATE and PROMPT-TEXT have the same meaning as
+`denote-date-prompt'."
+  (denote-get-identifier
+   (denote-valid-date-p
+    (denote-date-prompt initial-date prompt-text))))
 
 (defvar denote-subdirectory-history nil
   "Minibuffer history of `denote-subdirectory-prompt'.")
@@ -3416,7 +3448,9 @@ It is meant to be combined with `denote--rename-file' to 
create
 renaming commands."
   (let* ((file-in-prompt (propertize (file-relative-name file) 'face 
'denote-faces-prompt-current-name))
          (file-type (denote-filetype-heuristics file))
-         (date (denote-valid-date-p (denote-retrieve-filename-identifier 
file)))
+         (date (denote-valid-date-p (or (denote-retrieve-filename-identifier 
file)
+                                        (file-attribute-modification-time 
(file-attributes file))
+                                        (current-time))))
          (title (or (denote-retrieve-title-or-filename file file-type) ""))
          (keywords (denote-extract-keywords-from-path file))
          (signature (or (denote-retrieve-filename-signature file) "")))
@@ -3435,7 +3469,9 @@ renaming commands."
                           signature
                           (format "Rename `%s' with SIGNATURE (empty to 
remove)" file-in-prompt))))
         ('date
-         (setq date (denote-valid-date-p (denote-date-prompt))))))
+         (setq date (denote-valid-date-p (denote-date-prompt
+                                          date
+                                          (format "Rename `%s' with DATE" 
file-in-prompt)))))))
     (list title keywords signature date)))
 
 ;;;###autoload
diff --git a/tests/denote-test.el b/tests/denote-test.el
index 10c24e9647..8dd2ba92b0 100644
--- a/tests/denote-test.el
+++ b/tests/denote-test.el
@@ -498,6 +498,25 @@ does not involve the time zone."
   (should (equal (denote--id-to-date "20240901T090910") "2024-09-01"))
   (should-error (denote--id-to-date "20240901T090910-not-identifier-format")))
 
+(ert-deftest denote-test--denote--date-convert ()
+  "Test that `denote--date-convert' works with dates."
+  (should (and
+           (equal (denote--date-convert '(26454 45206 461174 657000) :list)
+                  '(26454 45206 461174 657000))
+
+           (equal (denote--date-convert '(26454 45206 461174 657000) :string)
+                  "2024-12-09 10:55:50")
+
+           (equal (denote--date-convert "2024-12-09 10:55:50" :list)
+                  '(26454 45206))
+
+           (equal (denote--date-convert "2024-12-09 10:55:50" :string)
+                  "2024-12-09 10:55:50")))
+  (should-error (denote--date-convert '(26454 45206 461174 657000) 
:not-valid-type))
+  (should-error (denote--date-convert "2024-12-09 10:55:50" :not-valid-type))
+  (should-error (denote--date-convert "Not right date" :list))
+  (should-error (denote--date-convert "Not right date" :string)))
+
 ;;;; denote-journal-extras.el
 
 (require 'denote-journal-extras)



reply via email to

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