Skip to content

Commit

Permalink
Merge pull request ymherklotz#8 from rockstorm101/id-regexp
Browse files Browse the repository at this point in the history
Allow customising ID regexp pattern
  • Loading branch information
ymherklotz authored Jul 27, 2022
2 parents a6c8ac8 + 890ce58 commit edba7bc
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions zettelkasten.el
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ For supported options, please consult `format-time-string'."
:type 'string
:group 'zettelkasten)

(defcustom zettelkasten-id-regexp "[0-9]+"
"Regexp for IDs."
:type 'string
:group 'zettelkasten)

;;; -----------------------------
;;; HELPER FUNCTIONS FOR NOTE IDs
;;; -----------------------------
Expand All @@ -76,7 +81,9 @@ For supported options, please consult `format-time-string'."

(defun zettelkasten--filename-to-id (filename)
"Convert FILENAME to id."
(string-match (format "\\([0-9]*\\)\\.%s\\'" zettelkasten-extension) filename)
(string-match
(format "\\(%s\\)\\.%s\\'" zettelkasten-id-regexp zettelkasten-extension)
filename)
(match-string 1 filename))

(defun zettelkasten--display-for-search (note)
Expand All @@ -90,7 +97,7 @@ Meant for displaying when searching."
The note may be formatted with some title, which this function
aims to remove."
(string-match "[^0-9]*\\([0-9]+\\)" note)
(string-match (format "[^0-9]*\\(%s\\)" zettelkasten-id-regexp) note)
(match-string 1 note))

(defun zettelkasten--format-link (note &optional link-text)
Expand Down Expand Up @@ -143,7 +150,7 @@ Return the NUMth match. If NUM is nil, return the 0th match."
(mapcar #'zettelkasten--filename-to-id
(directory-files
(expand-file-name zettelkasten-directory) nil
(format "[0-9]+\\.%s$" zettelkasten-extension) t)))
(format "%s\\.%s$" zettelkasten-id-regexp zettelkasten-extension) t)))

(defun zettelkasten--list-notes-grep ()
"Return all the ids and titles of notes in the `zettelkasten-directory'.
Expand All @@ -161,8 +168,8 @@ This is deprecated in favour for `zettelkasten-list-notes'."
(line-beginning-position)
(line-end-position)))
(when (string-match
(format "\\([0-9]*\\)\\.%s:#\\+TITLE: \\(.*\\)"
zettelkasten-extension)
(format "\\(%s\\)\\.%s:#\\+TITLE: \\(.*\\)"
zettelkasten-id-regexp zettelkasten-extension)
current-string)
(setq matched-string (concat (match-string 1 current-string) ": "
(match-string 2 current-string)))
Expand Down

0 comments on commit edba7bc

Please # to comment.