Skip to content

Commit

Permalink
Speed up zk--id-list
Browse files Browse the repository at this point in the history
Forego calling zk--alist when STR is nil (which is always, except for a
single case in zk-index, which requires case insensitivity, which is the one
thing that zk--id-list provides that can't be had from a combo of
zk--directory-files and zk--parse-file, as the regexp arg in
zk--directory-files is case sensitive. Ech.)
  • Loading branch information
localauthor committed Apr 28, 2023
1 parent 8e85728 commit 5f40cae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion zk-desktop.el
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ To quickly change this setting, call `zk-desktop-add-toggle'."
;; replace titles
(goto-char (point-min))
(let* ((zk-alist (zk--alist))
(ids (zk--id-list nil zk-alist)))
(ids (zk--id-list)))
(while (re-search-forward zk-id-regexp nil t)
(let* ((beg (line-beginning-position))
(end (line-end-position))
Expand Down
47 changes: 23 additions & 24 deletions zk.el
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,20 @@ The ID is created using `zk-id-time-string-format'."

(defun zk--id-list (&optional str zk-alist)
"Return a list of zk IDs for notes in `zk-directory'.
Optional search for regexp STR in note title, case-insenstive.
Takes an optional ZK-ALIST, for efficiency if `zk--id-list' is
called in an internal loop."
(let ((zk-alist (or zk-alist (zk--alist)))
(case-fold-search t)
(ids))
(dolist (item zk-alist)
(if str
(when (string-match str (cadr item))
(push (car item) ids))
(push (car item) ids)))
ids))
Optional search for STR in note title, case-insenstive. Takes an
optional ZK-ALIST, for efficiency if `zk--id-list' is called in
an internal loop."
(if str
(let ((zk-alist (or zk-alist (zk--alist)))
(case-fold-search t)
(ids))
(dolist (item zk-alist)
(if str
(when (string-match str (cadr item))
(push (car item) ids))
(push (car item) ids)))
ids)
(zk--parse-file 'id (zk--directory-files t))))

(defun zk--id-unavailable-p (str)
"Return t if provided string STR is already in use as an id."
Expand Down Expand Up @@ -496,7 +498,7 @@ optional ZK-ALIST, for efficiency if `zk--parse-id' is called
in an internal loop."
(let* ((zk-alist (or zk-alist
(zk--alist)))
(zk-id-list (zk--id-list nil zk-alist))
(zk-id-list (zk--id-list))
(return
(cond ((eq target 'file-path)
(cond ((stringp ids)
Expand Down Expand Up @@ -756,7 +758,7 @@ Optionally call a custom function by setting the variable
(defun zk--links-in-note-list ()
"Return list of zk files that are linked from the current buffer."
(let* ((zk-alist (zk--alist))
(zk-ids (zk--id-list nil zk-alist))
(zk-ids (zk--id-list))
id-list)
(save-buffer)
(save-excursion
Expand Down Expand Up @@ -886,7 +888,7 @@ brackets \"[[\" initiates completion."
"Copy link and title for id or file ARG at point."
(interactive (list (funcall zk-select-file-function "Copy link: ")))
(let* ((zk-alist (zk--alist))
(zk-id-list (zk--id-list nil zk-alist))
(zk-id-list (zk--id-list))
(id (cond ((member arg zk-id-list)
arg)
((member (car arg) zk-id-list)
Expand Down Expand Up @@ -996,12 +998,10 @@ Select TAG, with completion, from list of all tags in zk notes."
"\\|"))
(user-error "No dead links found"))))

(defun zk--unlinked-notes-list (&optional zk-alist)
"Return list of IDs for notes that no notes link to.
Takes an optional ZK-ALIST."
(let* ((zk-alist (or zk-alist (zk--alist)))
(all-link-ids (zk--grep-link-id-list))
(all-ids (zk--id-list nil zk-alist)))
(defun zk--unlinked-notes-list ()
"Return list of IDs for notes that no notes link to."
(let* ((all-link-ids (zk--grep-link-id-list))
(all-ids (zk--id-list)))
(remq nil (mapcar
(lambda (x)
(when (not (member x all-link-ids))
Expand All @@ -1012,9 +1012,8 @@ Takes an optional ZK-ALIST."
(defun zk-unlinked-notes ()
"Find unlinked notes."
(interactive)
(let* ((zk-alist (zk--alist))
(ids (zk--unlinked-notes-list zk-alist))
(notes (zk--parse-id 'file-path ids zk-alist)))
(let* ((ids (zk--unlinked-notes-list))
(notes (zk--parse-id 'file-path ids)))
(if notes
(find-file (funcall zk-select-file-function "Unlinked notes: " notes))
(user-error "No unlinked notes found"))))
Expand Down

0 comments on commit 5f40cae

Please sign in to comment.