Skip to content

Commit

Permalink
Code cleanup; describe obsidian-excluded-directories and the use of x…
Browse files Browse the repository at this point in the history
…eft for search in README
  • Loading branch information
jayemar committed Dec 13, 2024
1 parent d42fcf0 commit a1c3cee
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
20 changes: 18 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ In addition to the settings show in the example configurations above, some other
- obsidian-templates-directory :: location for obsidian.el to find template files
- obsidian-daily-note-template :: name of template file to use for daily notes
- obsidian-include-hidden-files :: configure obsidian.el to either track or ignore hidden files
- obsidian-excluded-directories :: list of full directory paths to be excluded from obsidian vault
- obsidian-create-unfound-files-in-inbox :: whether to create files for unfound links in inbox or in same directory as file
- obsidian-backlinks-panel-position :: which side of the window to host backlinks panel: 'left or 'right
- obsidian-backlinks-panel-width :: width of the backlinks panel in characters
Expand Down Expand Up @@ -280,10 +281,25 @@ Use ~obsidian-move-file~ to move the current note to another folder.
#+end_src

** Searching in notes
Use ~obsidian-search~ to look for a string or a regular expression within the notes in your vault.
~obsidian.el~ includes the function ~obsidian-search~ to look for a string or a regular expression within the notes in your vault. After entering a search query, the user can select from a list of files that include the search query.

#+begin_src
M-x obsidian-search RET query RET
M-x obsidian-search RET query RET select-file RET
#+end_src

Alternately, the third party library [[https://sr.ht/~casouri/xeft/][xeft]] can be used for searching through the vault. This package provides search-as-you-type functionality, as well as displaying the context of the file that matches the search.

Below is an example configuration to use with ~obsidian.el~:

#+begin_src elisp
(use-package xeft
:after obsidian
:bind ((:map obsidian-mode-map (("C-c C-g" . xeft))))
:custom
(xeft-directory obsidian-directory)
(xeft-recursive t)
(xeft-file-filter #'obsidian-file-p)
(xeft-title-function #'obsidian-file-title-function))
#+end_src

** Inserting tags
Expand Down
55 changes: 38 additions & 17 deletions obsidian.el
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@

(defgroup obsidian nil "Obsidian Notes group." :group 'text)

(defvar obsidian--relative-path-length nil
"Length of path of `obisidan-directory' used to calculate file relative paths.")

(defcustom obsidian-directory ""
"Path to Obsidian Notes vault."
:group 'obsidian
Expand All @@ -58,7 +61,9 @@
(if (file-exists-p full-path)
(progn
(message "Setting %s to %s" symbol full-path)
(set-default symbol full-path))
(set-default symbol full-path)
(setq obsidian--relative-path-length
(length (file-name-as-directory full-path))))
(user-error (format "Directory %s doesn't exist" full-path))))))

(defcustom obsidian-inbox-directory nil
Expand Down Expand Up @@ -89,10 +94,11 @@ Each directory should be a full path relative to `obsidian-directory`."
:type '(repeat directory))

(defcustom obsidian-create-unfound-files-in-inbox t
"Where to create a file when target file is missing.
"Controls where to create a file when target file is missing.
Controls where to create a new file when visiting a link when the target is
missing. If true, create in inbox, otherwise next to the current buffer."
missing. If true, create in inbox, otherwise create it in the same
directory as the current buffer."
:type 'boolean)

(defcustom obsidian-links-use-vault-path nil
Expand Down Expand Up @@ -337,9 +343,20 @@ FILE is an Org-roam file if:
(file-relative-name f obsidian-directory))

(defun obsidian-expand-file-name (f)
"Take relative file name F and return expanded name."
"Take file F relative to `obsidian-directory' and return absolute path."
(expand-file-name f obsidian-directory))

(defun obsidian-file-to-absolute-path (file)
"Return a full file path for FILE.
The full file path is determined by finding a file with the same name in the
vault cache. If there are multiple files with the same name, the first one
found is returned. If no matches are found, the original FILE is returned."
(let* ((all-files (->> (obsidian-files) (-map #'obsidian-file-relative-name)))
(matches (obsidian--match-files file all-files)))
(if matches
(obsidian-expand-file-name (car matches))
file)))

(defun obsidian-files ()
"Lists all Obsidian Notes files that are not in trash."
(when obsidian-vault-cache
Expand All @@ -351,7 +368,7 @@ FILE is an Org-roam file if:
(-filter #'obsidian-user-directory-p)))

(defun obsidian-remove-front-matter-from-string (s)
"Return S with any front matter removed, returning only th body."
"Return S with any front matter removed, returning only the body."
(if (s-starts-with-p "---" s)
(let ((splits (s-split-up-to "---" s 2)))
(if (eq (length splits) 3)
Expand Down Expand Up @@ -900,22 +917,11 @@ If the file include directories in its path, we create the file relative to
(obsidian-add-file cleaned))
cleaned))

(defun obsidian-file-to-absolute-path (file)
"Return a full file path for FILE.
The full file path is determined by finding a file with the same name in the
vault cache. If there are multiple files with the same name, the first one
found is returned. If no matches are found, the original FILE is returned."
(let* ((all-files (->> (obsidian-files) (-map #'obsidian-file-relative-name)))
(matches (obsidian--match-files file all-files)))
(if matches
(obsidian-expand-file-name (car matches))
file)))

(defun obsidian-find-file (f &optional arg)
"Open file F, offering a choice if multiple files match F.
If ARG is set, the file will be opened in other window."
(let* ((all-files (->> (obsidian-files) (-map #'obsidian-file-relative-name)))
(let* ((all-files (seq-map #'obsidian-file-relative-name (obsidian-files)))
(matches (obsidian--match-files f all-files))
(file (cl-case (length matches)
(0 (obsidian-prepare-new-file-from-rel-path
Expand Down Expand Up @@ -1160,6 +1166,21 @@ The files cache has the following structure:
(obsidian--populate-backlinks-buffer)))
(obsidian-backlink-jump)))

(defun obsidian-file-title-function (file)
"Return the title of FILE.
This is a modified version of the default xeft title function.
Recognize 'title:' if set, else return the first line as title or,
if the first line is empty, return the file name as the title."
(re-search-forward (rx "title:" (* whitespace)) nil t)
(let ((bol (point)) title)
(end-of-line)
(setq title (buffer-substring-no-properties bol (point)))
(if (equal title "")
(file-name-base file)
title)))

;;;###autoload
(defun obsidian-search ()
"Search Obsidian vault for input."
Expand Down

0 comments on commit a1c3cee

Please sign in to comment.