~protesilaos/denote

2

Use filename from `denote-link-or-create' prompt as the new note's name

Details
Message ID
<NF6pFBq--3-9@tutanota.com>
DKIM signature
missing
Download raw message
Hello.


While using denote, I found that I happened to link to not-yet-exist notes a lot. To do that, I use `denote-link-or-create' function. The problem is, the function asks for the name of the note to link to, or it'll ask you for a new name (and tags) if the note doesn't exist. I think this is an interruption in the flow of note taking, for in such situation I normally search the notes by their names, thus I type the name of the note but I still have to type the name again since the note doesn't exist. Having a way to take the name from the prompt as the new note's name would be a good QoL improvement.


Regards,
Guo.

--
Sent with Tutanota, enjoy secure & ad-free emails.
Details
Message ID
<87lep66iqq.fsf@protesilaos.com>
In-Reply-To
<NF6pFBq--3-9@tutanota.com> (view parent)
DKIM signature
missing
Download raw message
> From: guo.yong@tutanota.com
> Date: Mon, 24 Oct 2022 04:31:53 +0200
>
> Hello.

Hello Guo,

> While using denote, I found that I happened to link to not-yet-exist
> notes a lot. To do that, I use `denote-link-or-create' function. The
> problem is, the function asks for the name of the note to link to, or
> it'll ask you for a new name (and tags) if the note doesn't exist. I
> think this is an interruption in the flow of note taking, for in such
> situation I normally search the notes by their names, thus I type the
> name of the note but I still have to type the name again since the
> note doesn't exist. Having a way to take the name from the prompt as
> the new note's name would be a good QoL improvement.

I agree.  This is a command that was added recently and I did not want
to expand it too much before getting some user feedback on it.  Better
start small: adding new functionality is easier than removing it,
because we do not need to deal with backward incompatibilities and with
possibly breaking the user's workflow.

I will work on your suggestion to see what can be achieved.

More to follow.

All the best,
Protesilaos (or simply "Prot")

-- 
Protesilaos Stavrou
https://protesilaos.com
Details
Message ID
<87ilk97vdg.fsf@protesilaos.com>
In-Reply-To
<87lep66iqq.fsf@protesilaos.com> (view parent)
DKIM signature
missing
Download raw message
Patch: +10 -0
> From: Protesilaos Stavrou <info@protesilaos.com>
> Date: Mon, 24 Oct 2022 05:46:53 +0300
>
>> From: guo.yong@tutanota.com
>> Date: Mon, 24 Oct 2022 04:31:53 +0200
>>
>> Hello.
>
> Hello Guo,
>
>> While using denote, I found that I happened to link to not-yet-exist
>> notes a lot. To do that, I use `denote-link-or-create' function. The
>> problem is, the function asks for the name of the note to link to, or
>> it'll ask you for a new name (and tags) if the note doesn't exist. I
>> think this is an interruption in the flow of note taking, for in such
>> situation I normally search the notes by their names, thus I type the
>> name of the note but I still have to type the name again since the
>> note doesn't exist. Having a way to take the name from the prompt as
>> the new note's name would be a good QoL improvement.
>
> I agree.  This is a command that was added recently and I did not want
> to expand it too much before getting some user feedback on it.  Better
> start small: adding new functionality is easier than removing it,
> because we do not need to deal with backward incompatibilities and with
> possibly breaking the user's workflow.
>
> I will work on your suggestion to see what can be achieved.
>
> More to follow.

I am back!  A non-intrusive way of addressing this issue is to leverage
the minibuffer histories.  This allows us to type 'M-p' (the command is
'previous-history-element') at Denote's title prompt and get the last
input.  The diff further below makes this possible, but I am also
copying the code for your convenience.

Some notes before you check the code:

1. We should not hardcode prompts, such as for file and keywords because
   that will contradict the user option 'denote-prompts'.  Concretely, a
   user may want to create+link to a note that has a specific date, is
   in a given subdirectory, and starts with a certain template.

2. The input we provide at the standard file prompt should not be taken
   literally.  The reason is that it is common for a user to rely on the
   completion style to narrow the list of candidates.  They are not
   necessarily typing the exact title they would like.  For example, I
   use the 'orderless' package (among built-in options) and will often
   type searches out-of-order like "_journal test this" which would
   match a file that is "20221024T062254--this-is-a-test__journal.txt".

   By adding the last search to the minibuffer history, we let the user
   edit the title before they submit it.

What do you think?  The code and diff are below.


(defun denote--extract-title-from-file-history ()
  "Extract last file title input from `file-name-history'."
  ;; We do not need to check if `file-name-history' is initialised
  ;; because it is defined in files.el.  My understanding is that it
  ;; is always loaded.
  (when-let ((title (expand-file-name (car file-name-history))))
    (string-match (denote-directory) title)
    (substring title (match-end 0))))

;;;###autoload
(defun denote-link-or-create (target &optional id-only)
  "Use `denote-link' on TARGET file, creating it if necessary.

If TARGET file does not exist, call `denote-link-after-creating'
which runs the `denote' command interactively to create the file.
The established link will then be targeting that new file.

With optional ID-ONLY as a prefix argument create a link that
consists of just the identifier.  Else try to also include the
file's title.  This has the same meaning as in `denote-link'."
  (interactive (list (denote-file-prompt) current-prefix-arg))
  (if (file-exists-p target)
      (denote-link target id-only)
    (push (denote--extract-title-from-file-history) denote--title-history)
    (call-interactively #'denote-link-after-creating)))



 denote.el | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/denote.el b/denote.el
index 70e2a3b..0173e66 100644
--- a/denote.el
+++ b/denote.el
@@ -2451,6 +2451,15 @@ (defun denote-link-after-creating (&optional id-only)
      (setq path (buffer-file-name)))
    (denote-link path id-only)))

(defun denote--extract-title-from-file-history ()
  "Extract last file title input from `file-name-history'."
  ;; We do not need to check if `file-name-history' is initialised
  ;; because it is defined in files.el.  My understanding is that it
  ;; is always loaded.
  (when-let ((title (expand-file-name (car file-name-history))))
    (string-match (denote-directory) title)
    (substring title (match-end 0))))

;;;###autoload
(defun denote-link-or-create (target &optional id-only)
  "Use `denote-link' on TARGET file, creating it if necessary.
@@ -2465,6 +2474,7 @@ (defun denote-link-or-create (target &optional id-only)
  (interactive (list (denote-file-prompt) current-prefix-arg))
  (if (file-exists-p target)
      (denote-link target id-only)
    (push (denote--extract-title-from-file-history) denote--title-history)
    (call-interactively #'denote-link-after-creating)))

(defalias 'denote-link-to-existing-or-new-note (symbol-function 'denote-link-or-create))


-- 
Protesilaos Stavrou
https://protesilaos.com
Reply to thread Export thread (mbox)