;; 2026-denote.el. -*- lexical-binding: nil; -*-
;; This file:
;;   https://anggtwu.net/elisp/2026-denote.el.html
;;   https://anggtwu.net/elisp/2026-denote.el
;;           (find-angg "elisp/2026-denote.el")
;; Author: Eduardo Ochs <eduardoochs@gmail.com>
;;
;; (defun e () (interactive) (find-angg "elisp/2026-denote.el"))
;; (find-es "org" "denote")
;; (find-es "org" "denote-video")
;; (find-es "org" "org-link-parameters")

;; (load (buffer-file-name))

;; «.select-files»	(to "select-files")
;; «.elisp-hyperlinks»	(to "elisp-hyperlinks")
;; «.variants-of-kl»	(to "variants-of-kl")



;; «select-files»  (to ".select-files")
;; Some tests:
;; (find-eppp (ee-denote-files))
;; (find-eppp (ee-denote-files-with-title ".*"))
;; (find-eppp (ee-denote-files-with-title "foo"))
;; (ee-denote-file-with-title "foo")
;; (ee-denote-file-with-title "bar")
;; (ee-denote-file-with-title "angg-1")
;; (ee-denote-file-with-title "angg")
;; (ee-denote-file-with-title "^angg-1$")
;;
(defun ee-denote-files ()
  "Return the names of all the \"denote files\" in `denote-directory'."
  (cl-loop for fname in (directory-files denote-directory 'full-name)
           if (and (not (backup-file-name-p fname))
		   (denote-file-has-denoted-filename-p fname))
	   collect fname))

(defun ee-denote-files-with-title (regexp)
  "Like `ee-denote-files', but return only the files whose titles match REGEXP."
  (cl-loop for fname in (ee-denote-files)
	   if (string-match regexp (denote-retrieve-filename-title fname))
	   collect fname))

(defun ee-denote-file-with-title (regexp)
  "Return the file in `denote-directory' whose title matches REGEXP.
If there is more than one file like that, issue an error.
If there is none, issue an error."
  (let ((fnames (ee-denote-files-with-title regexp)))
    (if (= 1 (length fnames))
	(car fnames)
      (error (format "Found %s denote notes matching that title"
		     (length fnames))))))

(defun ee-denote-file-with-id (id)
  "Return the file in `denote-directory' whose id matches ID.
If there isn't a file like that, issue an error."
  (let ((fname (denote-link--ol-resolve-link-to-target id)))
    (if (equal fname id)
	(error "No denote note with that id")
      fname)))



;; «elisp-hyperlinks»  (to ".elisp-hyperlinks")
;;
(defun find-denoteid (id &rest rest) 
  (apply 'find-fline (ee-denote-file-with-id id) rest))

(defun find-denotetitle (title &rest rest)
  (let ((fname (ee-denote-file-with-title (format "^%s$" title))))
    (apply 'find-fline fname rest)))
    


;; «variants-of-kl»  (to ".variants-of-kl")
;; Based on: (find-eev "eev-kla.el" "default-args")
;;
(defun ee-kl-denote-id (&optional fname)
  "Returns the denote id of FNAME, or of the current file."
  (denote-retrieve-filename-identifier
   (or fname (buffer-file-name))))

(defun ee-kl-denote-title (&optional fname)
  "Returns the denote title of FNAME, or of the current file."
  (denote-retrieve-filename-title
   (or fname (buffer-file-name))))




(defun kldi (&optional arg)
  "<K>ill <L>ink to <D>enote <I>d."
  (interactive)
  (ee-kl-kill `(find-denoteid ,(ee-kl-denote-id))))

(defun kldis (&optional arg)
  "<K>ill <L>ink to <D>enote <I>d and <S>string."
  (interactive)
  (ee-kl-kill `(find-denoteid ,(ee-kl-denote-id) ,(ee-kl-region))))

(defun kldt (&optional arg)
  "<K>ill <L>ink to <D>enote <T>itle."
  (interactive)
  (ee-kl-kill `(find-denotetitle ,(ee-kl-denote-title))))

(defun kldts (&optional arg)
  "<K>ill <L>ink to <D>enote <T>itle and <S>tring."
  (interactive)
  (ee-kl-kill `(find-denotetitle ,(ee-kl-denote-title) ,(ee-kl-region))))


(defalias 'kld  'kldt)
(defalias 'klds 'kldts)




;; Local Variables:
;; coding:  utf-8-unix
;; End:
