2020-02-09 06:47:28 +00:00
|
|
|
;;; org-roam.el --- Roam Research replica with Org-mode -*- coding: utf-8; lexical-binding: t -*-
|
2020-01-22 14:41:25 +00:00
|
|
|
|
2020-02-12 20:09:47 +00:00
|
|
|
;; Copyright © 2020 Jethro Kuan <jethrokuan95@gmail.com>
|
|
|
|
|
|
|
|
;; Author: Jethro Kuan <jethrokuan95@gmail.com>
|
|
|
|
;; URL: https://github.com/jethrokuan/org-roam
|
|
|
|
;; Keywords: org-mode, roam, convenience
|
|
|
|
;; Version: 0.1.0
|
2020-02-13 08:41:27 +00:00
|
|
|
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (s "1.12.0") (async "1.9.4"))
|
2020-02-12 20:09:47 +00:00
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2020-01-22 14:41:25 +00:00
|
|
|
;;
|
2020-02-12 20:09:47 +00:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
2020-01-22 14:41:25 +00:00
|
|
|
|
2020-02-12 20:09:47 +00:00
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
;; This library is an attempt at injecting Roam functionality into Org-mode.
|
|
|
|
;; This is achieved primarily through building caches for forward links,
|
|
|
|
;; backward links, and file titles.
|
|
|
|
;;
|
|
|
|
;;
|
2020-01-22 14:41:25 +00:00
|
|
|
;;; Code:
|
2020-02-12 20:09:47 +00:00
|
|
|
|
2020-02-12 05:17:31 +00:00
|
|
|
(eval-when-compile (require 'cl-lib))
|
2020-02-09 12:59:39 +00:00
|
|
|
(require 'dash)
|
2020-02-05 11:41:12 +00:00
|
|
|
(require 'org-element)
|
2020-01-22 11:05:04 +00:00
|
|
|
(require 'async)
|
2020-02-02 16:17:17 +00:00
|
|
|
(require 'subr-x)
|
2020-02-04 18:53:07 +00:00
|
|
|
(require 's)
|
2020-02-09 04:44:04 +00:00
|
|
|
(require 'f)
|
2020-02-13 17:52:56 +00:00
|
|
|
(require 'org-roam-utils)
|
2020-01-21 15:30:33 +00:00
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
;;; Customizations
|
2020-01-21 15:30:33 +00:00
|
|
|
(defgroup org-roam nil
|
|
|
|
"Roam Research replica in Org-mode."
|
|
|
|
:group 'org
|
2020-02-12 20:09:47 +00:00
|
|
|
:prefix "org-roam-"
|
|
|
|
:link '(url-link :tag "Github" "https://github.com/jethrokuan/org-roam")
|
|
|
|
:link '(url-link :tag "Online Manual" "https://org-roam.readthedocs.io/"))
|
2020-01-05 09:04:37 +00:00
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
(defcustom org-roam-directory (expand-file-name "~/org-roam/")
|
2020-02-12 19:14:34 +00:00
|
|
|
"Path to Org-roam files.
|
|
|
|
|
|
|
|
All Org files, at any level of nesting, is considered part of the Org-roam."
|
2020-02-18 02:12:36 +00:00
|
|
|
:type 'directory
|
2020-02-06 11:26:38 +00:00
|
|
|
:group 'org-roam)
|
2020-02-05 05:13:33 +00:00
|
|
|
|
2020-02-12 19:14:34 +00:00
|
|
|
(defcustom org-roam-buffer-position 'right
|
2020-01-22 09:12:06 +00:00
|
|
|
"Position of `org-roam' buffer.
|
2020-01-21 17:40:11 +00:00
|
|
|
|
|
|
|
Valid values are
|
|
|
|
* left,
|
|
|
|
* right."
|
|
|
|
:type '(choice (const left)
|
|
|
|
(const right))
|
|
|
|
:group 'org-roam)
|
2020-01-21 15:30:33 +00:00
|
|
|
|
2020-02-17 05:50:40 +00:00
|
|
|
(defcustom org-roam-file-name-function #'org-roam--file-name-timestamp-title
|
|
|
|
"The function used to generate filenames.
|
|
|
|
|
|
|
|
The function takes as parameter `TITLE', a string the user inputs."
|
|
|
|
:group 'org-roam
|
|
|
|
:type '(choice (const :tag "Default" org-roam--file-name-timestamp-title)
|
|
|
|
(function :tag "Personalized function")))
|
2020-02-10 07:27:42 +00:00
|
|
|
|
|
|
|
(defcustom org-roam-link-title-format "%s"
|
|
|
|
"The format string used when inserting org-roam links that use their title."
|
|
|
|
:type 'string
|
|
|
|
:group 'org-roam)
|
|
|
|
|
2020-02-17 05:50:40 +00:00
|
|
|
(defcustom org-roam-filename-noconfirm t
|
|
|
|
"Whether to prompt for confirmation of fil name for new files.
|
|
|
|
|
|
|
|
If nil, always ask for filename."
|
2020-02-13 05:20:48 +00:00
|
|
|
:type 'boolean
|
|
|
|
:group 'org-roam)
|
2020-02-12 16:25:45 +00:00
|
|
|
|
2020-02-12 05:17:31 +00:00
|
|
|
(defcustom org-roam-autopopulate-title t "Whether to autopopulate the title."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'org-roam)
|
|
|
|
|
2020-02-09 12:59:07 +00:00
|
|
|
(defcustom org-roam-buffer-width 0.33 "Width of `org-roam' buffer."
|
|
|
|
:type 'number
|
2020-02-06 11:26:38 +00:00
|
|
|
:group 'org-roam)
|
2020-01-22 11:05:04 +00:00
|
|
|
|
2020-02-09 12:59:07 +00:00
|
|
|
(defcustom org-roam-buffer "*org-roam*"
|
|
|
|
"Org-roam buffer name."
|
2020-02-06 11:26:38 +00:00
|
|
|
:type 'string
|
|
|
|
:group 'org-roam)
|
2020-02-04 18:53:07 +00:00
|
|
|
|
2020-02-16 10:31:51 +00:00
|
|
|
(defcustom org-roam-encrypt-files nil
|
|
|
|
"Whether to encrypt new files. If true, create files with .org.gpg extension."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'org-roam)
|
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
(defcustom org-roam-graph-viewer (executable-find "firefox")
|
|
|
|
"Path to executable for viewing SVG."
|
|
|
|
:type 'string
|
|
|
|
:group 'org-roam)
|
2020-02-03 08:58:45 +00:00
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
(defcustom org-roam-graphviz-executable (executable-find "dot")
|
|
|
|
"Path to graphviz executable."
|
|
|
|
:type 'string
|
|
|
|
:group 'org-roam)
|
2020-01-22 11:05:04 +00:00
|
|
|
|
2020-02-07 04:44:40 +00:00
|
|
|
;;; Polyfills
|
|
|
|
;; These are for functions I use that are only available in newer Emacs
|
|
|
|
|
|
|
|
;; Introduced in Emacs 27.1
|
|
|
|
(unless (fboundp 'make-empty-file)
|
|
|
|
(defun make-empty-file (filename &optional parents)
|
|
|
|
"Create an empty file FILENAME.
|
|
|
|
Optional arg PARENTS, if non-nil then creates parent dirs as needed.
|
|
|
|
|
|
|
|
If called interactively, then PARENTS is non-nil."
|
|
|
|
(interactive
|
|
|
|
(let ((filename (read-file-name "Create empty file: ")))
|
|
|
|
(list filename t)))
|
|
|
|
(when (and (file-exists-p filename) (null parents))
|
|
|
|
(signal 'file-already-exists `("File exists" ,filename)))
|
|
|
|
(let ((paren-dir (file-name-directory filename)))
|
|
|
|
(when (and paren-dir (not (file-exists-p paren-dir)))
|
|
|
|
(make-directory paren-dir parents)))
|
|
|
|
(write-region "" nil filename nil 0)))
|
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
;;; Dynamic variables
|
2020-02-11 08:19:59 +00:00
|
|
|
(defvar org-roam-cache-initialized nil
|
|
|
|
"Boolean value indicating whether the cache is initialized.")
|
|
|
|
|
|
|
|
(defvar org-roam-forward-links-cache (make-hash-table :test #'equal)
|
|
|
|
"Cache containing forward links.")
|
|
|
|
|
|
|
|
(defvar org-roam-backward-links-cache (make-hash-table :test #'equal)
|
|
|
|
"Cache containing backward-links.")
|
|
|
|
|
|
|
|
(defvar org-roam-titles-cache (make-hash-table :test #'equal)
|
|
|
|
"Cache containing titles for org-roam files.")
|
2020-02-04 09:40:44 +00:00
|
|
|
|
2020-02-09 06:47:28 +00:00
|
|
|
(defvar org-roam-current-file nil
|
2020-02-06 11:26:38 +00:00
|
|
|
"Currently displayed file in `org-roam' buffer.")
|
2020-01-21 17:40:11 +00:00
|
|
|
|
2020-02-17 13:46:48 +00:00
|
|
|
(defvar org-roam-last-window nil
|
|
|
|
"Last window `org-roam' was called from.")
|
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
;;; Utilities
|
2020-02-10 09:07:05 +00:00
|
|
|
(defun org-roam--ensure-cache-built ()
|
|
|
|
"Ensures that org-roam cache is built."
|
2020-02-11 08:19:59 +00:00
|
|
|
(unless org-roam-cache-initialized
|
2020-02-10 09:07:05 +00:00
|
|
|
(org-roam--build-cache-async)
|
|
|
|
(user-error "Your Org-Roam cache isn't built yet! Please wait")))
|
|
|
|
|
2020-02-09 04:44:04 +00:00
|
|
|
(defun org-roam--org-roam-file-p ()
|
2020-02-09 06:47:28 +00:00
|
|
|
"Return t if file is part of org-roam system, false otherwise."
|
2020-02-09 04:44:04 +00:00
|
|
|
(and (buffer-file-name (current-buffer))
|
2020-02-15 03:36:10 +00:00
|
|
|
(f-descendant-of-p (file-truename (buffer-file-name (current-buffer)))
|
2020-02-16 03:33:31 +00:00
|
|
|
(file-truename org-roam-directory))))
|
2020-02-09 04:44:04 +00:00
|
|
|
|
2020-02-12 16:25:45 +00:00
|
|
|
(defun org-roam--get-title-from-cache (file)
|
|
|
|
"Return title of `FILE' from the cache."
|
2020-02-11 08:19:59 +00:00
|
|
|
(or (gethash file org-roam-titles-cache)
|
2020-02-12 16:25:45 +00:00
|
|
|
(progn
|
|
|
|
(unless org-roam-cache-initialized
|
|
|
|
(user-error "The Org-Roam caches aren't built! Please run org-roam--build-cache-async"))
|
|
|
|
nil)))
|
2020-02-13 12:08:09 +00:00
|
|
|
|
2020-02-05 05:13:33 +00:00
|
|
|
(defun org-roam--find-all-files ()
|
2020-02-09 06:47:28 +00:00
|
|
|
"Return all org-roam files."
|
2020-02-10 08:28:38 +00:00
|
|
|
(org-roam--find-files (file-truename org-roam-directory)))
|
2020-02-05 05:13:33 +00:00
|
|
|
|
2020-02-16 10:31:51 +00:00
|
|
|
(defun org-roam--make-new-file-path (id &optional absolute)
|
|
|
|
"Make new file path from identifier `ID'.
|
2020-02-05 11:41:12 +00:00
|
|
|
|
2020-02-16 10:31:51 +00:00
|
|
|
If `ABSOLUTE', return an absolute file-path. Else, return a relative file-path."
|
2020-02-10 08:28:38 +00:00
|
|
|
(let ((absolute-file-path (file-truename
|
|
|
|
(expand-file-name
|
2020-02-16 10:31:51 +00:00
|
|
|
(if org-roam-encrypt-files
|
|
|
|
(concat id ".org.gpg")
|
|
|
|
(concat id ".org"))
|
2020-02-10 08:28:38 +00:00
|
|
|
org-roam-directory))))
|
2020-02-09 06:47:28 +00:00
|
|
|
(if absolute
|
|
|
|
absolute-file-path
|
2020-02-10 08:28:38 +00:00
|
|
|
(file-relative-name absolute-file-path
|
|
|
|
(file-truename org-roam-directory)))))
|
2020-02-05 05:13:33 +00:00
|
|
|
|
2020-02-12 16:25:45 +00:00
|
|
|
(defun org-roam--get-title-or-slug (file-path)
|
|
|
|
"Convert `FILE-PATH' to the file title, if it exists. Else, return the path."
|
|
|
|
(or (org-roam--get-title-from-cache file-path)
|
|
|
|
(-> file-path
|
|
|
|
(file-relative-name (file-truename org-roam-directory))
|
|
|
|
(file-name-sans-extension))))
|
2020-02-05 05:13:33 +00:00
|
|
|
|
2020-02-12 16:25:45 +00:00
|
|
|
(defun org-roam--title-to-slug (title)
|
|
|
|
"Convert TITLE to a filename-suitable slug."
|
2020-02-12 13:16:29 +00:00
|
|
|
(let* ((s (s-downcase title))
|
|
|
|
(s (replace-regexp-in-string "[^a-zA-Z0-9_ ]" "" s))
|
|
|
|
(s (s-split " " s))
|
|
|
|
(s (s-join "_" s)))
|
|
|
|
s))
|
|
|
|
|
2020-02-17 05:50:40 +00:00
|
|
|
(defun org-roam--file-name-timestamp-title (title)
|
|
|
|
"Return a file name (without extension) for new files.
|
|
|
|
|
|
|
|
It uses TITLE and the current timestamp to form a unique title."
|
|
|
|
(let ((timestamp (format-time-string "%Y%m%d%H%M%S" (current-time)))
|
|
|
|
(slug (org-roam--title-to-slug title)))
|
|
|
|
(format "%s_%s" timestamp slug)))
|
2020-02-13 12:08:09 +00:00
|
|
|
|
2020-02-10 07:44:40 +00:00
|
|
|
;;; Creating org-roam files
|
2020-02-12 05:17:31 +00:00
|
|
|
(defun org-roam--populate-title (file &optional title)
|
|
|
|
"Populate title line for FILE using TITLE, if provided.
|
|
|
|
If not provided, derive the title from the file name."
|
|
|
|
(let ((title (or title
|
|
|
|
(-> file
|
|
|
|
(file-name-nondirectory)
|
|
|
|
(file-name-sans-extension)
|
|
|
|
(split-string "_")
|
|
|
|
(string-join " ")
|
|
|
|
(s-titleize)))))
|
|
|
|
(write-region
|
|
|
|
(concat
|
|
|
|
"#+TITLE: "
|
|
|
|
title
|
|
|
|
"\n\n")
|
|
|
|
nil file nil)))
|
|
|
|
|
2020-02-12 05:36:45 +00:00
|
|
|
(defun org-roam--make-file (file-path &optional title)
|
|
|
|
"Create an org-roam file at FILE-PATH, optionally setting the TITLE attribute."
|
|
|
|
(if (file-exists-p file-path)
|
2020-02-13 05:20:48 +00:00
|
|
|
(error (format "Aborting, file already exists at %s" file-path))
|
2020-02-12 05:36:45 +00:00
|
|
|
(if org-roam-autopopulate-title
|
|
|
|
(org-roam--populate-title file-path title)
|
2020-02-17 11:02:46 +00:00
|
|
|
(make-empty-file file-path))
|
|
|
|
(save-excursion
|
|
|
|
(with-current-buffer (find-file-noselect file-path)
|
|
|
|
(org-roam--update-cache)))))
|
2020-02-12 05:36:45 +00:00
|
|
|
|
2020-02-10 07:44:40 +00:00
|
|
|
(defun org-roam--new-file-named (slug)
|
|
|
|
"Create a new file named `SLUG'.
|
|
|
|
`SLUG' is the short file name, without a path or a file extension."
|
|
|
|
(interactive "sNew filename (without extension): ")
|
2020-02-16 10:31:51 +00:00
|
|
|
(let ((file-path (org-roam--make-new-file-path slug t)))
|
2020-02-12 05:36:45 +00:00
|
|
|
(unless (file-exists-p file-path)
|
|
|
|
(org-roam--make-file file-path))
|
|
|
|
(find-file file-path)))
|
2020-02-10 07:44:40 +00:00
|
|
|
|
2020-02-17 05:50:40 +00:00
|
|
|
(defun org-roam--get-new-id (title)
|
|
|
|
"Return a new ID, given the note TITLE."
|
|
|
|
(let* ((proposed-slug (funcall org-roam-file-name-function title))
|
|
|
|
(new-slug (if org-roam-filename-noconfirm
|
|
|
|
proposed-slug
|
|
|
|
(read-string "Enter ID (without extension): "
|
|
|
|
proposed-slug)))
|
|
|
|
(file-path (org-roam--make-new-file-path new-slug t)))
|
|
|
|
(if (file-exists-p file-path)
|
|
|
|
(user-error "There's already a file at %s")
|
|
|
|
new-slug)))
|
2020-02-12 14:18:03 +00:00
|
|
|
|
2020-02-10 07:44:40 +00:00
|
|
|
(defun org-roam-new-file ()
|
|
|
|
"Quickly create a new file, using the current timestamp."
|
|
|
|
(interactive)
|
2020-02-17 05:50:40 +00:00
|
|
|
(org-roam--new-file-named (format-time-string "%Y%m%d%H%M%S" (current-time))))
|
2020-02-10 07:44:40 +00:00
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
;;; Inserting org-roam links
|
2020-02-17 15:15:27 +00:00
|
|
|
(defun org-roam-insert (prefix)
|
|
|
|
"Find an org-roam file, and insert a relative org link to it at point.
|
|
|
|
|
|
|
|
If PREFIX, downcase the title before insertion."
|
|
|
|
(interactive "P")
|
2020-02-10 05:46:17 +00:00
|
|
|
(let* ((completions (mapcar (lambda (file)
|
2020-02-12 16:25:45 +00:00
|
|
|
(list (org-roam--get-title-or-slug file)
|
|
|
|
file))
|
2020-02-10 05:46:17 +00:00
|
|
|
(org-roam--find-all-files)))
|
|
|
|
(title (completing-read "File: " completions))
|
2020-02-13 08:04:41 +00:00
|
|
|
(absolute-file-path (or (cadr (assoc title completions))
|
2020-02-16 10:31:51 +00:00
|
|
|
(org-roam--make-new-file-path (org-roam--get-new-id title) t)))
|
2020-02-14 15:14:38 +00:00
|
|
|
(current-file-path (-> (current-buffer)
|
|
|
|
(buffer-file-name)
|
|
|
|
(file-truename)
|
|
|
|
(file-name-directory))))
|
2020-02-13 08:04:41 +00:00
|
|
|
(unless (file-exists-p absolute-file-path)
|
|
|
|
(org-roam--make-file absolute-file-path title))
|
2020-02-05 05:13:33 +00:00
|
|
|
(insert (format "[[%s][%s]]"
|
2020-02-14 15:14:38 +00:00
|
|
|
(concat "file:" (file-relative-name absolute-file-path
|
|
|
|
current-file-path))
|
2020-02-17 15:15:27 +00:00
|
|
|
(format org-roam-link-title-format (if prefix
|
|
|
|
(downcase title)
|
|
|
|
title))))))
|
2020-01-21 15:30:33 +00:00
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
;;; Finding org-roam files
|
2020-02-10 06:03:24 +00:00
|
|
|
(defun org-roam-find-file ()
|
|
|
|
"Find and open an org-roam file."
|
|
|
|
(interactive)
|
|
|
|
(let* ((completions (mapcar (lambda (file)
|
2020-02-12 16:25:45 +00:00
|
|
|
(list (org-roam--get-title-or-slug file) file))
|
2020-02-10 06:03:24 +00:00
|
|
|
(org-roam--find-all-files)))
|
2020-02-12 16:25:45 +00:00
|
|
|
(title-or-slug (completing-read "File: " completions))
|
2020-02-13 08:04:41 +00:00
|
|
|
(absolute-file-path (or (cadr (assoc title-or-slug completions))
|
2020-02-16 10:31:51 +00:00
|
|
|
(org-roam--make-new-file-path
|
2020-02-13 08:04:41 +00:00
|
|
|
(org-roam--get-new-id title-or-slug) t))))
|
|
|
|
(unless (file-exists-p absolute-file-path)
|
|
|
|
(org-roam--make-file absolute-file-path title-or-slug))
|
|
|
|
(find-file absolute-file-path)))
|
2020-02-06 08:14:23 +00:00
|
|
|
|
2020-02-17 07:48:54 +00:00
|
|
|
;;; Building the org-roam cache
|
2020-02-05 11:41:12 +00:00
|
|
|
(defun org-roam--build-cache-async ()
|
2020-02-17 07:48:54 +00:00
|
|
|
"Builds the caches asychronously."
|
2020-01-22 11:05:04 +00:00
|
|
|
(interactive)
|
|
|
|
(async-start
|
|
|
|
`(lambda ()
|
2020-02-13 17:52:56 +00:00
|
|
|
(setq load-path ',load-path)
|
|
|
|
(package-initialize)
|
|
|
|
(require 'org-roam-utils)
|
2020-02-05 05:13:33 +00:00
|
|
|
,(async-inject-variables "org-roam-directory")
|
2020-02-17 07:48:54 +00:00
|
|
|
(org-roam--build-cache org-roam-directory))
|
2020-02-09 06:47:28 +00:00
|
|
|
(lambda (cache)
|
2020-02-11 08:19:59 +00:00
|
|
|
(setq org-roam-forward-links-cache (plist-get cache :forward))
|
|
|
|
(setq org-roam-backward-links-cache (plist-get cache :backward))
|
|
|
|
(setq org-roam-titles-cache (plist-get cache :titles))
|
|
|
|
(setq org-roam-cache-initialized t)
|
2020-02-10 04:50:13 +00:00
|
|
|
(message "Org-roam cache built!"))))
|
2020-02-09 06:47:28 +00:00
|
|
|
|
2020-02-13 07:55:21 +00:00
|
|
|
(defun org-roam--clear-cache ()
|
2020-02-17 07:48:54 +00:00
|
|
|
"Clears all entries in the caches."
|
|
|
|
(interactive)
|
|
|
|
(setq org-roam-cache-initialized nil)
|
|
|
|
(setq org-roam-forward-links-cache (make-hash-table :test #'equal))
|
|
|
|
(setq org-roam-backward-links-cache (make-hash-table :test #'equal))
|
|
|
|
(setq org-roam-titles-cache (make-hash-table :test #'equal)))
|
|
|
|
|
|
|
|
(defun org-roam--clear-file-from-cache ()
|
2020-02-13 07:55:21 +00:00
|
|
|
"Remove any related links to the file.
|
2020-02-09 06:47:28 +00:00
|
|
|
|
|
|
|
This is equivalent to removing the node from the graph."
|
2020-02-13 07:55:21 +00:00
|
|
|
(let ((file (file-truename (buffer-file-name (current-buffer)))))
|
|
|
|
;; Step 1: Remove all existing links for file
|
|
|
|
(when-let ((forward-links (gethash file org-roam-forward-links-cache)))
|
|
|
|
;; Delete backlinks to file
|
|
|
|
(dolist (link forward-links)
|
|
|
|
(when-let ((backward-links (gethash link org-roam-backward-links-cache)))
|
|
|
|
(remhash file backward-links)
|
|
|
|
(puthash link backward-links org-roam-backward-links-cache)))
|
|
|
|
;; Clean out forward links
|
|
|
|
(remhash file org-roam-forward-links-cache))
|
|
|
|
;; Step 2: Remove from the title cache
|
|
|
|
(remhash file org-roam-titles-cache)))
|
|
|
|
|
|
|
|
(defun org-roam--update-cache-title ()
|
|
|
|
"Insert the title of the current buffer into the cache."
|
|
|
|
(when-let ((title (org-roam--extract-title)))
|
|
|
|
(puthash (file-truename (buffer-file-name (current-buffer)))
|
2020-02-10 04:50:13 +00:00
|
|
|
title
|
2020-02-11 08:19:59 +00:00
|
|
|
org-roam-titles-cache)))
|
2020-02-09 06:47:28 +00:00
|
|
|
|
|
|
|
(defun org-roam--update-cache ()
|
2020-02-11 08:19:59 +00:00
|
|
|
"Update org-roam caches for the current buffer file."
|
2020-02-09 06:47:28 +00:00
|
|
|
(save-excursion
|
2020-02-17 07:48:54 +00:00
|
|
|
(org-roam--clear-file-from-cache)
|
2020-02-10 04:50:13 +00:00
|
|
|
;; Insert into title cache
|
2020-02-13 07:55:21 +00:00
|
|
|
(org-roam--update-cache-title)
|
2020-02-10 04:50:13 +00:00
|
|
|
;; Insert new items
|
2020-02-09 06:47:28 +00:00
|
|
|
(let ((items (org-roam--parse-content)))
|
|
|
|
(dolist (item items)
|
2020-02-13 12:08:09 +00:00
|
|
|
(org-roam--insert-item
|
|
|
|
item
|
|
|
|
:forward org-roam-forward-links-cache
|
|
|
|
:backward org-roam-backward-links-cache)))
|
2020-02-11 08:19:59 +00:00
|
|
|
;; Rerender buffer
|
2020-02-12 04:04:48 +00:00
|
|
|
(org-roam--maybe-update-buffer :redisplay t)))
|
2020-02-06 11:26:38 +00:00
|
|
|
|
|
|
|
;;; Org-roam daily notes
|
2020-01-21 10:19:51 +00:00
|
|
|
(defun org-roam-today ()
|
2020-01-22 14:41:25 +00:00
|
|
|
"Create the file for today."
|
2020-01-21 10:19:51 +00:00
|
|
|
(interactive)
|
2020-02-06 11:26:38 +00:00
|
|
|
(org-roam--new-file-named (format-time-string "%Y-%m-%d" (current-time))))
|
|
|
|
|
2020-02-07 08:47:47 +00:00
|
|
|
(defun org-roam-tomorrow ()
|
|
|
|
"Create the file for tomorrow."
|
|
|
|
(interactive)
|
|
|
|
(org-roam--new-file-named (format-time-string "%Y-%m-%d" (time-add 86400 (current-time)))))
|
|
|
|
|
|
|
|
(defun org-roam-date ()
|
|
|
|
"Create the file for any date using the calendar."
|
|
|
|
(interactive)
|
|
|
|
(let ((time (org-read-date nil 'to-time nil "Date: ")))
|
|
|
|
(org-roam--new-file-named (format-time-string "%Y-%m-%d" time))))
|
2020-01-21 10:19:51 +00:00
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
;;; Org-roam buffer updates
|
2020-02-17 13:46:48 +00:00
|
|
|
|
|
|
|
(defun org-roam--find-file (file)
|
|
|
|
"Open FILE in the window `org-roam' was called from."
|
|
|
|
(if (and org-roam-last-window (window-valid-p org-roam-last-window))
|
|
|
|
(progn (with-selected-window org-roam-last-window
|
|
|
|
(find-file file))
|
|
|
|
(select-window org-roam-last-window))
|
|
|
|
(find-file file)))
|
|
|
|
|
2020-02-09 06:47:28 +00:00
|
|
|
(defun org-roam-update (file-path)
|
|
|
|
"Show the backlinks for given org file for file at `FILE-PATH'."
|
2020-02-10 09:07:05 +00:00
|
|
|
(org-roam--ensure-cache-built)
|
2020-02-12 16:25:45 +00:00
|
|
|
(let ((buffer-title (org-roam--get-title-or-slug file-path)))
|
2020-02-10 09:07:05 +00:00
|
|
|
(with-current-buffer org-roam-buffer
|
2020-02-17 13:46:48 +00:00
|
|
|
;; Locally overwrite the file opening function to re-use the
|
|
|
|
;; last window org-roam was called from
|
|
|
|
(setq-local
|
|
|
|
org-link-frame-setup
|
|
|
|
(cons '(file . org-roam--find-file) org-link-frame-setup))
|
2020-02-10 09:07:05 +00:00
|
|
|
(let ((inhibit-read-only t))
|
|
|
|
(erase-buffer)
|
|
|
|
(when (not (eq major-mode 'org-mode))
|
|
|
|
(org-mode))
|
|
|
|
(make-local-variable 'org-return-follows-link)
|
|
|
|
(setq org-return-follows-link t)
|
|
|
|
(insert
|
|
|
|
(propertize buffer-title 'font-lock-face 'org-document-title))
|
2020-02-11 08:19:59 +00:00
|
|
|
(if-let ((backlinks (gethash file-path org-roam-backward-links-cache)))
|
2020-02-10 09:07:05 +00:00
|
|
|
(progn
|
|
|
|
(insert (format "\n\n* %d Backlinks\n"
|
|
|
|
(hash-table-count backlinks)))
|
|
|
|
(maphash (lambda (file-from contents)
|
|
|
|
(insert (format "** [[file:%s][%s]]\n"
|
|
|
|
file-from
|
2020-02-12 16:25:45 +00:00
|
|
|
(org-roam--get-title-or-slug file-from)))
|
2020-02-10 09:07:05 +00:00
|
|
|
(dolist (content contents)
|
|
|
|
(insert (concat (propertize (s-trim (s-replace "\n" " " content))
|
|
|
|
'font-lock-face 'org-block)
|
|
|
|
"\n\n"))))
|
|
|
|
backlinks))
|
|
|
|
(insert "\n\n* No backlinks!")))
|
|
|
|
(read-only-mode 1)))
|
|
|
|
(setq org-roam-current-file file-path))
|
2020-01-21 15:30:33 +00:00
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
;;; Show/hide the org-roam buffer
|
|
|
|
(define-inline org-roam--current-visibility ()
|
|
|
|
"Return whether the current visibility state of the org-roam buffer.
|
|
|
|
Valid states are 'visible, 'exists and 'none."
|
|
|
|
(declare (side-effect-free t))
|
|
|
|
(inline-quote
|
|
|
|
(cond
|
|
|
|
((get-buffer-window org-roam-buffer) 'visible)
|
|
|
|
((get-buffer org-roam-buffer) 'exists)
|
|
|
|
(t 'none))))
|
|
|
|
|
2020-02-09 12:59:39 +00:00
|
|
|
(defun org-roam--set-width (width)
|
|
|
|
"Set the width of the org-roam buffer to `WIDTH'."
|
|
|
|
(unless (one-window-p)
|
|
|
|
(let ((window-size-fixed)
|
|
|
|
(w (max width window-min-width)))
|
|
|
|
(cond
|
|
|
|
((> (window-width) w)
|
|
|
|
(shrink-window-horizontally (- (window-width) w)))
|
|
|
|
((< (window-width) w)
|
|
|
|
(enlarge-window-horizontally (- w (window-width))))))))
|
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
(defun org-roam--setup-buffer ()
|
2020-02-12 19:14:34 +00:00
|
|
|
"Setup the `org-roam' buffer at the `org-roam-buffer-position'."
|
2020-02-09 13:22:24 +00:00
|
|
|
(let ((window (get-buffer-window)))
|
2020-02-09 12:59:39 +00:00
|
|
|
(-> (get-buffer-create org-roam-buffer)
|
|
|
|
(display-buffer-in-side-window
|
2020-02-12 19:14:34 +00:00
|
|
|
`((side . ,org-roam-buffer-position)))
|
2020-02-09 12:59:39 +00:00
|
|
|
(select-window))
|
|
|
|
(org-roam--set-width
|
|
|
|
(round (* (frame-width)
|
2020-02-09 13:22:24 +00:00
|
|
|
org-roam-buffer-width)))
|
|
|
|
(select-window window)))
|
2020-02-06 11:26:38 +00:00
|
|
|
|
2020-01-21 17:40:11 +00:00
|
|
|
(defun org-roam ()
|
2020-02-09 06:47:28 +00:00
|
|
|
"Pops up the window `org-roam-buffer' accordingly."
|
2020-01-21 17:40:11 +00:00
|
|
|
(interactive)
|
2020-02-17 13:46:48 +00:00
|
|
|
(setq org-roam-last-window (get-buffer-window))
|
2020-02-06 11:26:38 +00:00
|
|
|
(pcase (org-roam--current-visibility)
|
2020-01-22 09:12:06 +00:00
|
|
|
('visible (delete-window (get-buffer-window org-roam-buffer)))
|
2020-02-05 11:41:12 +00:00
|
|
|
('exists (org-roam--setup-buffer))
|
|
|
|
('none (org-roam--setup-buffer))))
|
2020-01-21 17:40:11 +00:00
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
;;; The minor mode definition that updates the buffer
|
2020-02-09 04:44:04 +00:00
|
|
|
(defun org-roam--maybe-enable ()
|
2020-02-09 06:47:28 +00:00
|
|
|
"Enable org-roam updating for file, if file is an org-roam file."
|
2020-02-09 04:44:04 +00:00
|
|
|
(when (org-roam--org-roam-file-p)
|
|
|
|
(org-roam--enable)))
|
|
|
|
|
2020-02-05 11:41:12 +00:00
|
|
|
(defun org-roam--enable ()
|
2020-02-09 06:47:28 +00:00
|
|
|
"Enable org-roam updating for file.
|
|
|
|
|
|
|
|
1. If the cache does not yet exist, build it asynchronously.
|
|
|
|
2. Setup hooks for updating the cache, and the org-roam buffer."
|
2020-02-11 08:19:59 +00:00
|
|
|
(unless org-roam-cache-initialized
|
2020-02-09 06:47:28 +00:00
|
|
|
(org-roam--build-cache-async))
|
|
|
|
(add-hook 'post-command-hook #'org-roam--maybe-update-buffer nil t)
|
2020-02-11 08:21:16 +00:00
|
|
|
(add-hook 'after-save-hook #'org-roam--update-cache nil t))
|
2020-01-22 11:05:04 +00:00
|
|
|
|
2020-02-05 11:41:12 +00:00
|
|
|
(defun org-roam--disable ()
|
2020-02-09 06:47:28 +00:00
|
|
|
"Disable org-roam updating for file.
|
|
|
|
|
|
|
|
1. Remove hooks for updating the cache, and the org-roam buffer."
|
2020-02-05 11:41:12 +00:00
|
|
|
(remove-hook 'post-command-hook #'org-roam--maybe-update-buffer)
|
2020-02-09 06:47:28 +00:00
|
|
|
(remove-hook 'after-save-hook #'org-roam--update-cache))
|
2020-02-05 11:41:12 +00:00
|
|
|
|
2020-02-12 04:04:48 +00:00
|
|
|
(cl-defun org-roam--maybe-update-buffer (&key redisplay)
|
2020-01-22 14:41:25 +00:00
|
|
|
"Update `org-roam-buffer' with the necessary information.
|
|
|
|
This needs to be quick/infrequent, because this is run at
|
2020-02-06 11:26:38 +00:00
|
|
|
`post-command-hook'."
|
2020-02-05 11:41:12 +00:00
|
|
|
(with-current-buffer (window-buffer)
|
2020-02-07 05:06:54 +00:00
|
|
|
(when (and (get-buffer org-roam-buffer)
|
2020-02-09 04:44:04 +00:00
|
|
|
(buffer-file-name (current-buffer))
|
2020-02-10 06:03:24 +00:00
|
|
|
(file-exists-p (file-truename (buffer-file-name (current-buffer))))
|
2020-02-12 04:04:48 +00:00
|
|
|
(or redisplay
|
|
|
|
(not (string= org-roam-current-file
|
|
|
|
(file-truename (buffer-file-name (current-buffer)))))))
|
|
|
|
(org-roam-update (file-truename (buffer-file-name (window-buffer)))))))
|
2020-01-22 09:12:06 +00:00
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
(define-minor-mode org-roam-mode
|
|
|
|
"Global minor mode to automatically update the org-roam buffer."
|
|
|
|
:require 'org-roam
|
|
|
|
(if org-roam-mode
|
2020-02-09 04:44:04 +00:00
|
|
|
(org-roam--maybe-enable)
|
2020-02-06 11:26:38 +00:00
|
|
|
(org-roam--disable)))
|
|
|
|
|
|
|
|
;;; Building the Graphviz graph
|
2020-02-03 15:42:58 +00:00
|
|
|
(defun org-roam-build-graph ()
|
|
|
|
"Build graphviz graph output."
|
2020-02-10 09:07:05 +00:00
|
|
|
(org-roam--ensure-cache-built)
|
2020-02-11 08:19:59 +00:00
|
|
|
(with-temp-buffer
|
|
|
|
(insert "digraph {\n")
|
2020-02-13 05:20:48 +00:00
|
|
|
(dolist (file (org-roam--find-all-files))
|
|
|
|
(insert
|
|
|
|
(format " \"%s\" [URL=\"roam://%s\"];\n"
|
|
|
|
(org-roam--get-title-or-slug file)
|
|
|
|
file)))
|
2020-02-11 08:19:59 +00:00
|
|
|
(maphash
|
|
|
|
(lambda (from-link to-links)
|
|
|
|
(dolist (to-link to-links)
|
|
|
|
(insert (format " \"%s\" -> \"%s\";\n"
|
2020-02-12 16:25:45 +00:00
|
|
|
(org-roam--get-title-or-slug from-link)
|
2020-02-13 05:20:48 +00:00
|
|
|
(org-roam--get-title-or-slug to-link)))))
|
2020-02-11 08:19:59 +00:00
|
|
|
org-roam-forward-links-cache)
|
|
|
|
(insert "}")
|
|
|
|
(buffer-string)))
|
2020-02-09 06:47:28 +00:00
|
|
|
|
|
|
|
(defun org-roam-show-graph ()
|
|
|
|
"Generate the org-roam graph in SVG format, and display it using `org-roam-graph-viewer'."
|
2020-02-03 15:42:58 +00:00
|
|
|
(interactive)
|
2020-02-04 09:40:44 +00:00
|
|
|
(unless org-roam-graphviz-executable
|
|
|
|
(setq org-roam-graphviz-executable (executable-find "dot")))
|
|
|
|
(unless org-roam-graphviz-executable
|
|
|
|
(user-error "Can't find graphviz executable. Please check if it is in your path"))
|
2020-02-03 15:42:58 +00:00
|
|
|
(declare (indent 0))
|
2020-02-04 09:40:44 +00:00
|
|
|
(let ((temp-dot (expand-file-name "graph.dot" temporary-file-directory))
|
|
|
|
(temp-graph (expand-file-name "graph.svg" temporary-file-directory))
|
2020-02-03 15:42:58 +00:00
|
|
|
(graph (org-roam-build-graph)))
|
|
|
|
(with-temp-file temp-dot
|
|
|
|
(insert graph))
|
2020-02-04 18:24:25 +00:00
|
|
|
(call-process org-roam-graphviz-executable nil 0 nil temp-dot "-Tsvg" "-o" temp-graph)
|
2020-02-04 09:40:44 +00:00
|
|
|
(call-process org-roam-graph-viewer nil 0 nil temp-graph)))
|
2020-02-03 15:42:58 +00:00
|
|
|
|
2020-01-21 09:12:08 +00:00
|
|
|
(provide 'org-roam)
|
2020-01-22 14:41:25 +00:00
|
|
|
|
|
|
|
;;; org-roam.el ends here
|
2020-02-06 11:26:38 +00:00
|
|
|
|
|
|
|
;; Local Variables:
|
|
|
|
;; outline-regexp: ";;;+ "
|
|
|
|
;; End:
|