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
|
2020-03-06 12:39:28 +00:00
|
|
|
;; Version: 1.0.0-rc1
|
2020-03-08 07:58:13 +00:00
|
|
|
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (s "1.12.0") (org "9.3") (emacsql "3.0.0") (emacsql-sqlite "1.0.0"))
|
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-03-01 07:36:39 +00:00
|
|
|
;;;; Library Requires
|
2020-02-29 07:56:08 +00:00
|
|
|
(require 'org)
|
2020-02-05 11:41:12 +00:00
|
|
|
(require 'org-element)
|
2020-03-01 07:36:39 +00:00
|
|
|
(require 'ob-core) ;for org-babel-parse-header-arguments
|
2020-04-13 04:12:30 +00:00
|
|
|
(eval-when-compile (require 'subr-x))
|
2020-02-29 07:56:08 +00:00
|
|
|
(require 'dash)
|
2020-02-04 18:53:07 +00:00
|
|
|
(require 's)
|
2020-02-09 04:44:04 +00:00
|
|
|
(require 'f)
|
2020-02-29 07:56:08 +00:00
|
|
|
(require 'cl-lib)
|
2020-03-28 13:16:28 +00:00
|
|
|
;;;; org-roam features
|
2020-04-09 04:16:22 +00:00
|
|
|
(require 'org-roam-compat)
|
2020-03-28 13:16:28 +00:00
|
|
|
(require 'org-roam-macs)
|
|
|
|
(require 'org-roam-db)
|
2020-04-10 06:38:41 +00:00
|
|
|
(require 'org-roam-buffer)
|
2020-03-28 13:16:28 +00:00
|
|
|
(require 'org-roam-capture)
|
|
|
|
(require 'org-roam-graph)
|
|
|
|
(require 'org-roam-completion)
|
2020-03-21 19:16:38 +00:00
|
|
|
|
2020-04-06 08:48:17 +00:00
|
|
|
;; To detect cite: links
|
|
|
|
(require 'org-ref nil t)
|
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
;;;; Customizable Variables
|
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-29 07:56:08 +00:00
|
|
|
(defgroup org-roam-faces nil
|
2020-03-05 16:19:11 +00:00
|
|
|
"Faces used by Org-roam."
|
2020-02-29 07:56:08 +00:00
|
|
|
:group 'org-roam
|
|
|
|
:group 'faces)
|
2020-02-05 05:13:33 +00:00
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
(defcustom org-roam-directory (expand-file-name "~/org-roam/")
|
|
|
|
"Default path to Org-roam files.
|
|
|
|
|
2020-04-13 06:55:12 +00:00
|
|
|
All Org files, at any level of nesting, are considered part of the Org-roam."
|
2020-03-01 07:36:39 +00:00
|
|
|
:type 'directory
|
|
|
|
:group 'org-roam)
|
|
|
|
|
2020-02-10 07:27:42 +00:00
|
|
|
(defcustom org-roam-link-title-format "%s"
|
2020-03-09 14:18:04 +00:00
|
|
|
"The formatter used when inserting Org-roam links that use their title.
|
|
|
|
Formatter may be a function that takes title as its only argument."
|
|
|
|
:type '(choice
|
|
|
|
(string :tag "String Format" "%s")
|
|
|
|
(function :tag "Custom function"))
|
2020-02-10 07:27:42 +00:00
|
|
|
:group 'org-roam)
|
|
|
|
|
2020-02-16 10:31:51 +00:00
|
|
|
(defcustom org-roam-encrypt-files nil
|
2020-03-06 12:15:33 +00:00
|
|
|
"Whether to encrypt new files. If true, create files with .org.gpg extension."
|
2020-02-16 10:31:51 +00:00
|
|
|
:type 'boolean
|
|
|
|
:group 'org-roam)
|
|
|
|
|
2020-03-27 07:09:52 +00:00
|
|
|
(defcustom org-roam-verbose t
|
|
|
|
"Echo messages that are not errors."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'org-roam)
|
|
|
|
|
2020-04-15 07:02:05 +00:00
|
|
|
(defcustom org-roam-file-extensions '("org")
|
|
|
|
"Detected file extensions to include in the Org-roam ecosystem.
|
|
|
|
While the file extensions may be different, the file format needs
|
|
|
|
to be an `org-mode' file, and it is the user's responsibility to
|
|
|
|
ensure that."
|
|
|
|
:type '(repeat string)
|
|
|
|
:group 'org-roam)
|
|
|
|
|
|
|
|
;;;; Dynamic variables
|
|
|
|
(defvar org-roam-last-window nil
|
|
|
|
"Last window `org-roam' was called from.")
|
|
|
|
|
2020-02-06 11:26:38 +00:00
|
|
|
;;; Utilities
|
2020-03-01 07:36:39 +00:00
|
|
|
;;;; General Utilities
|
|
|
|
(defun org-roam--plist-to-alist (plist)
|
|
|
|
"Return an alist of the property-value pairs in PLIST."
|
|
|
|
(let (res)
|
|
|
|
(while plist
|
|
|
|
(let ((prop (intern (substring (symbol-name (pop plist)) 1 nil)))
|
|
|
|
(val (pop plist)))
|
|
|
|
(push (cons prop val) res)))
|
|
|
|
res))
|
|
|
|
|
|
|
|
(defun org-roam--aliases-str-to-list (str)
|
|
|
|
"Function to transform string STR into list of alias titles.
|
|
|
|
|
|
|
|
This snippet is obtained from ox-hugo:
|
|
|
|
https://github.com/kaushalmodi/ox-hugo/blob/a80b250987bc770600c424a10b3bca6ff7282e3c/ox-hugo.el#L3131"
|
|
|
|
(when (stringp str)
|
|
|
|
(let* ((str (org-trim str))
|
|
|
|
(str-list (split-string str "\n"))
|
|
|
|
ret)
|
|
|
|
(dolist (str-elem str-list)
|
|
|
|
(let* ((format-str ":dummy '(%s)") ;The :dummy key is discarded in the `lst' var below.
|
|
|
|
(alist (org-babel-parse-header-arguments (format format-str str-elem)))
|
|
|
|
(lst (cdr (car alist)))
|
|
|
|
(str-list2 (mapcar (lambda (elem)
|
|
|
|
(cond
|
|
|
|
((symbolp elem)
|
|
|
|
(symbol-name elem))
|
|
|
|
(t
|
|
|
|
elem)))
|
|
|
|
lst)))
|
|
|
|
(setq ret (append ret str-list2))))
|
|
|
|
ret)))
|
|
|
|
|
|
|
|
;;;; File functions and predicates
|
|
|
|
(defun org-roam--touch-file (path)
|
|
|
|
"Touches an empty file at PATH."
|
|
|
|
(make-directory (file-name-directory path) t)
|
|
|
|
(f-touch path))
|
|
|
|
|
|
|
|
(defun org-roam--file-name-extension (filename)
|
|
|
|
"Return file name extension for FILENAME.
|
2020-03-08 07:58:13 +00:00
|
|
|
Like `file-name-extension', but does not strip version number."
|
2020-03-01 07:36:39 +00:00
|
|
|
(save-match-data
|
|
|
|
(let ((file (file-name-nondirectory filename)))
|
|
|
|
(if (and (string-match "\\.[^.]*\\'" file)
|
|
|
|
(not (eq 0 (match-beginning 0))))
|
|
|
|
(substring file (+ (match-beginning 0) 1))))))
|
|
|
|
|
|
|
|
(defun org-roam--org-file-p (path)
|
|
|
|
"Check if PATH is pointing to an org file."
|
|
|
|
(let ((ext (org-roam--file-name-extension path)))
|
2020-04-15 07:02:05 +00:00
|
|
|
(when (string= ext "gpg") ; Handle encrypted files
|
|
|
|
(setq ext (org-roam--file-name-extension (file-name-sans-extension path))))
|
|
|
|
(member ext org-roam-file-extensions)))
|
2020-03-01 07:36:39 +00:00
|
|
|
|
|
|
|
(defun org-roam--org-roam-file-p (&optional file)
|
2020-03-08 07:58:13 +00:00
|
|
|
"Return t if FILE is part of Org-roam system, nil otherwise.
|
|
|
|
If FILE is not specified, use the current buffer's file-path."
|
2020-03-01 07:36:39 +00:00
|
|
|
(let ((path (or file
|
2020-03-10 02:41:43 +00:00
|
|
|
(buffer-file-name))))
|
2020-03-01 07:36:39 +00:00
|
|
|
(and path
|
|
|
|
(org-roam--org-file-p path)
|
|
|
|
(f-descendant-of-p (file-truename path)
|
|
|
|
(file-truename org-roam-directory)))))
|
|
|
|
|
2020-02-29 14:09:04 +00:00
|
|
|
(defun org-roam--list-files (dir)
|
|
|
|
"Return all Org-roam files located within DIR, at any nesting level.
|
|
|
|
Ignores hidden files and directories."
|
2020-02-29 07:56:08 +00:00
|
|
|
(if (file-exists-p dir)
|
|
|
|
(let ((files (directory-files dir t "." t))
|
|
|
|
(dir-ignore-regexp (concat "\\(?:"
|
|
|
|
"\\."
|
|
|
|
"\\|\\.\\."
|
|
|
|
"\\)$"))
|
|
|
|
result)
|
|
|
|
(dolist (file files)
|
|
|
|
(cond
|
|
|
|
((file-directory-p file)
|
2020-03-08 07:58:13 +00:00
|
|
|
(unless (string-match dir-ignore-regexp file)
|
2020-02-29 14:09:04 +00:00
|
|
|
(setq result (append (org-roam--list-files file) result))))
|
2020-02-29 07:56:08 +00:00
|
|
|
((and (file-readable-p file)
|
|
|
|
(org-roam--org-file-p file))
|
|
|
|
(setq result (cons (file-truename file) result)))))
|
|
|
|
result)))
|
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
(defun org-roam--list-all-files ()
|
2020-03-08 07:58:13 +00:00
|
|
|
"Return a list of all Org-roam files within `org-roam-directory'."
|
2020-03-01 07:36:39 +00:00
|
|
|
(org-roam--list-files (file-truename org-roam-directory)))
|
|
|
|
|
|
|
|
;;;; Org extraction functions
|
|
|
|
(defun org-roam--extract-global-props (props)
|
|
|
|
"Extract PROPS from the current org buffer.
|
|
|
|
The search terminates when the first property is encountered."
|
|
|
|
(let ((buf (org-element-parse-buffer))
|
|
|
|
res)
|
|
|
|
(dolist (prop props)
|
|
|
|
(let ((p (org-element-map buf 'keyword
|
|
|
|
(lambda (kw)
|
|
|
|
(when (string= (org-element-property :key kw) prop)
|
|
|
|
(org-element-property :value kw)))
|
|
|
|
:first-match t)))
|
|
|
|
(push (cons prop p) res)))
|
|
|
|
res))
|
|
|
|
|
2020-02-29 14:09:04 +00:00
|
|
|
(defun org-roam--extract-links (&optional file-path)
|
|
|
|
"Extracts all link items within the current buffer.
|
|
|
|
Link items are of the form:
|
|
|
|
|
2020-04-06 08:48:17 +00:00
|
|
|
[from to type properties]
|
2020-02-29 14:09:04 +00:00
|
|
|
|
|
|
|
This is the format that emacsql expects when inserting into the database.
|
|
|
|
FILE-FROM is typically the buffer file path, but this may not exist, for example
|
2020-03-10 16:55:42 +00:00
|
|
|
in temp buffers. In cases where this occurs, we do know the file path, and pass
|
2020-02-29 14:09:04 +00:00
|
|
|
it as FILE-PATH."
|
2020-02-29 07:56:08 +00:00
|
|
|
(let ((file-path (or file-path
|
2020-03-10 02:41:43 +00:00
|
|
|
(file-truename (buffer-file-name)))))
|
2020-02-29 07:56:08 +00:00
|
|
|
(org-element-map (org-element-parse-buffer) 'link
|
|
|
|
(lambda (link)
|
2020-04-06 08:48:17 +00:00
|
|
|
(let* ((type (org-element-property :type link))
|
|
|
|
(path (org-element-property :path link))
|
|
|
|
(start (org-element-property :begin link))
|
|
|
|
(link-type (cond ((and (string= type "file")
|
|
|
|
(org-roam--org-file-p path))
|
|
|
|
"roam")
|
2020-04-09 04:26:40 +00:00
|
|
|
((and
|
|
|
|
(require 'org-ref nil t)
|
|
|
|
(-contains? org-ref-cite-types type))
|
2020-04-06 08:48:17 +00:00
|
|
|
"cite")
|
|
|
|
(t nil))))
|
|
|
|
(when link-type
|
2020-02-29 07:56:08 +00:00
|
|
|
(goto-char start)
|
|
|
|
(let* ((element (org-element-at-point))
|
|
|
|
(begin (or (org-element-property :content-begin element)
|
|
|
|
(org-element-property :begin element)))
|
|
|
|
(content (or (org-element-property :raw-value element)
|
|
|
|
(buffer-substring
|
|
|
|
begin
|
|
|
|
(or (org-element-property :content-end element)
|
|
|
|
(org-element-property :end element)))))
|
|
|
|
(content (string-trim content)))
|
|
|
|
(vector file-path
|
2020-04-06 08:48:17 +00:00
|
|
|
(cond ((string= link-type "roam")
|
|
|
|
(file-truename (expand-file-name path (file-name-directory file-path))))
|
|
|
|
((string= link-type "cite")
|
|
|
|
path))
|
|
|
|
link-type
|
2020-02-29 07:56:08 +00:00
|
|
|
(list :content content :point begin)))))))))
|
|
|
|
|
|
|
|
(defun org-roam--extract-titles ()
|
|
|
|
"Extract the titles from current buffer.
|
|
|
|
Titles are obtained via the #+TITLE property, or aliases
|
|
|
|
specified via the #+ROAM_ALIAS property."
|
|
|
|
(let* ((props (org-roam--extract-global-props '("TITLE" "ROAM_ALIAS")))
|
|
|
|
(aliases (cdr (assoc "ROAM_ALIAS" props)))
|
|
|
|
(title (cdr (assoc "TITLE" props)))
|
|
|
|
(alias-list (org-roam--aliases-str-to-list aliases)))
|
|
|
|
(if title
|
|
|
|
(cons title alias-list)
|
|
|
|
alias-list)))
|
|
|
|
|
|
|
|
(defun org-roam--extract-ref ()
|
|
|
|
"Extract the ref from current buffer."
|
|
|
|
(cdr (assoc "ROAM_KEY" (org-roam--extract-global-props '("ROAM_KEY")))))
|
2020-03-01 07:36:39 +00:00
|
|
|
;;;; Title/Path/Slug conversion
|
2020-02-25 16:11:38 +00:00
|
|
|
(defun org-roam--path-to-slug (path)
|
|
|
|
"Return a slug from PATH."
|
|
|
|
(-> path
|
|
|
|
(file-relative-name (file-truename org-roam-directory))
|
|
|
|
(file-name-sans-extension)))
|
|
|
|
|
|
|
|
(defun org-roam--get-title-or-slug (path)
|
2020-03-10 16:55:42 +00:00
|
|
|
"Convert `PATH' to the file title, if it exists. Else, return the path."
|
2020-03-28 13:16:28 +00:00
|
|
|
(or (car (org-roam-db--get-titles path))
|
2020-02-29 14:09:04 +00:00
|
|
|
(org-roam--path-to-slug path)))
|
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-03-05 17:47:22 +00:00
|
|
|
(cl-flet* ((nonspacing-mark-p (char)
|
2020-03-08 07:58:13 +00:00
|
|
|
(eq 'Mn (get-char-code-property char 'general-category)))
|
2020-03-05 17:47:22 +00:00
|
|
|
(strip-nonspacing-marks (s)
|
2020-03-08 07:58:13 +00:00
|
|
|
(apply #'string (seq-remove #'nonspacing-mark-p
|
|
|
|
(ucs-normalize-NFD-string s))))
|
2020-03-21 04:40:13 +00:00
|
|
|
(cl-replace (title pair)
|
2020-03-28 13:16:28 +00:00
|
|
|
(replace-regexp-in-string (car pair) (cdr pair) title)))
|
2020-02-22 16:53:16 +00:00
|
|
|
(let* ((pairs `(("[^[:alnum:][:digit:]]" . "_") ;; convert anything not alphanumeric
|
|
|
|
("__*" . "_") ;; remove sequential underscores
|
|
|
|
("^_" . "") ;; remove starting underscore
|
|
|
|
("_$" . ""))) ;; remove ending underscore
|
2020-03-21 04:40:13 +00:00
|
|
|
(slug (-reduce-from #'cl-replace (strip-nonspacing-marks title) pairs)))
|
2020-02-22 16:53:16 +00:00
|
|
|
(s-downcase slug))))
|
2020-02-12 13:16:29 +00:00
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
;;; Interactive Commands
|
|
|
|
;;;; org-roam-insert
|
2020-03-09 14:18:04 +00:00
|
|
|
(defun org-roam--format-link-title (title)
|
2020-03-28 13:16:28 +00:00
|
|
|
"Return the link title, given the file TITLE."
|
2020-03-09 14:18:04 +00:00
|
|
|
(if (functionp org-roam-link-title-format)
|
|
|
|
(funcall org-roam-link-title-format title)
|
|
|
|
(format org-roam-link-title-format title)))
|
|
|
|
|
2020-03-21 19:16:38 +00:00
|
|
|
(defun org-roam--format-link (target &optional description)
|
2020-03-17 09:06:49 +00:00
|
|
|
"Formats an org link for a given file TARGET and link DESCRIPTION."
|
|
|
|
(let* ((here (-> (or (buffer-base-buffer)
|
2020-03-19 16:57:32 +00:00
|
|
|
(current-buffer))
|
|
|
|
(buffer-file-name)
|
|
|
|
(file-truename)
|
|
|
|
(file-name-directory))))
|
2020-03-21 18:46:36 +00:00
|
|
|
(org-link-make-string
|
|
|
|
(concat "file:" (file-relative-name target here))
|
|
|
|
description)))
|
2020-03-17 09:06:49 +00:00
|
|
|
|
2020-02-17 15:15:27 +00:00
|
|
|
(defun org-roam-insert (prefix)
|
2020-03-08 07:58:13 +00:00
|
|
|
"Find an Org-roam file, and insert a relative org link to it at point.
|
2020-02-17 15:15:27 +00:00
|
|
|
If PREFIX, downcase the title before insertion."
|
|
|
|
(interactive "P")
|
2020-03-10 02:57:49 +00:00
|
|
|
(unless (org-roam--org-roam-file-p
|
|
|
|
(buffer-file-name (buffer-base-buffer)))
|
2020-03-10 02:36:36 +00:00
|
|
|
(user-error "Not in an Org-roam file"))
|
2020-02-21 06:44:43 +00:00
|
|
|
(let* ((region (and (region-active-p)
|
|
|
|
;; following may lose active region, so save it
|
|
|
|
(cons (region-beginning) (region-end))))
|
|
|
|
(region-text (when region
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
(car region) (cdr region))))
|
2020-02-25 16:11:38 +00:00
|
|
|
(completions (org-roam--get-title-path-completions))
|
2020-03-28 13:16:28 +00:00
|
|
|
(title (org-roam-completion--completing-read "File: " completions
|
|
|
|
:initial-input region-text))
|
2020-02-21 06:44:43 +00:00
|
|
|
(region-or-title (or region-text title))
|
2020-03-17 09:06:49 +00:00
|
|
|
(target-file-path (cdr (assoc title completions)))
|
|
|
|
(link-description (org-roam--format-link-title (if prefix
|
|
|
|
(downcase region-or-title)
|
|
|
|
region-or-title))))
|
|
|
|
(if (and target-file-path
|
|
|
|
(file-exists-p target-file-path))
|
|
|
|
(progn
|
|
|
|
(when region ;; Remove previously selected text.
|
|
|
|
(delete-region (car region) (cdr region)))
|
|
|
|
(insert (org-roam--format-link target-file-path link-description)))
|
2020-04-10 09:03:49 +00:00
|
|
|
(when (org-roam-capture--in-process-p)
|
|
|
|
(user-error "Nested Org-roam capture processes not supported"))
|
|
|
|
(let ((org-roam-capture--info (list (cons 'title title)
|
|
|
|
(cons 'slug (org-roam--title-to-slug title))))
|
|
|
|
(org-roam-capture--context 'title))
|
|
|
|
(add-hook 'org-capture-after-finalize-hook #'org-roam-capture--insert-link-h)
|
|
|
|
(setq org-roam-capture-additional-template-props (list :region region
|
|
|
|
:link-description link-description
|
|
|
|
:capture-fn 'org-roam-insert))
|
|
|
|
(org-roam--capture)))))
|
2020-03-17 09:06:49 +00:00
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
;;;; org-roam-find-file
|
2020-02-25 16:11:38 +00:00
|
|
|
(defun org-roam--get-title-path-completions ()
|
|
|
|
"Return a list of cons pairs for titles to absolute path of Org-roam files."
|
2020-03-28 13:16:28 +00:00
|
|
|
(let* ((rows (org-roam-db-query [:select [file titles] :from titles]))
|
2020-02-29 07:56:08 +00:00
|
|
|
res)
|
|
|
|
(dolist (row rows)
|
|
|
|
(let ((file-path (car row))
|
|
|
|
(titles (cadr row)))
|
|
|
|
(if titles
|
|
|
|
(dolist (title titles)
|
2020-02-29 14:09:04 +00:00
|
|
|
(push (cons title file-path) res))
|
|
|
|
(push (cons (org-roam--path-to-slug file-path)
|
|
|
|
file-path) res))))
|
2020-02-25 16:11:38 +00:00
|
|
|
res))
|
|
|
|
|
2020-03-09 08:43:48 +00:00
|
|
|
(defun org-roam-find-file (&optional initial-prompt)
|
|
|
|
"Find and open an Org-roam file.
|
|
|
|
INITIAL-PROMPT is the initial title prompt."
|
2020-02-10 06:03:24 +00:00
|
|
|
(interactive)
|
2020-02-25 16:11:38 +00:00
|
|
|
(let* ((completions (org-roam--get-title-path-completions))
|
2020-03-28 13:16:28 +00:00
|
|
|
(title (org-roam-completion--completing-read "File: " completions
|
|
|
|
:initial-input initial-prompt))
|
2020-03-04 16:21:24 +00:00
|
|
|
(file-path (cdr (assoc title completions))))
|
|
|
|
(if file-path
|
|
|
|
(find-file file-path)
|
2020-04-10 09:03:49 +00:00
|
|
|
(if (org-roam-capture--in-process-p)
|
2020-03-20 12:17:56 +00:00
|
|
|
(user-error "Org-roam capture in process")
|
2020-03-28 13:16:28 +00:00
|
|
|
(let ((org-roam-capture--info (list (cons 'title title)
|
2020-03-20 12:17:56 +00:00
|
|
|
(cons 'slug (org-roam--title-to-slug title))))
|
2020-03-28 13:16:28 +00:00
|
|
|
(org-roam-capture--context 'title))
|
|
|
|
(add-hook 'org-capture-after-finalize-hook #'org-roam-capture--find-file-h)
|
2020-03-29 09:29:57 +00:00
|
|
|
(org-roam--capture))))))
|
2020-02-06 08:14:23 +00:00
|
|
|
|
2020-04-10 12:11:39 +00:00
|
|
|
;;;; org-roam-find-directory
|
|
|
|
(defun org-roam-find-directory ()
|
|
|
|
"Find and open `org-roam-directory'."
|
|
|
|
(interactive)
|
|
|
|
(find-file org-roam-directory))
|
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
;;;; org-roam-find-ref
|
|
|
|
(defun org-roam--get-ref-path-completions ()
|
2020-04-11 04:36:59 +00:00
|
|
|
"Return a list of cons pairs for refs to absolute path of Org-roam files."
|
2020-03-28 13:16:28 +00:00
|
|
|
(let ((rows (org-roam-db-query [:select [ref file] :from refs])))
|
2020-03-01 07:36:39 +00:00
|
|
|
(mapcar (lambda (row)
|
|
|
|
(cons (car row)
|
|
|
|
(cadr row))) rows)))
|
2020-02-20 05:38:31 +00:00
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
(defun org-roam-find-ref (&optional info)
|
2020-03-08 07:58:13 +00:00
|
|
|
"Find and open an Org-roam file from a ref.
|
2020-03-01 07:36:39 +00:00
|
|
|
INFO is an alist containing additional information."
|
2020-02-17 07:48:54 +00:00
|
|
|
(interactive)
|
2020-03-01 07:36:39 +00:00
|
|
|
(let* ((completions (org-roam--get-ref-path-completions))
|
|
|
|
(ref (or (cdr (assoc 'ref info))
|
2020-03-28 13:16:28 +00:00
|
|
|
(org-roam-completion--completing-read "Ref: "
|
2020-04-10 20:13:16 +00:00
|
|
|
completions
|
2020-03-28 13:16:28 +00:00
|
|
|
:require-match t))))
|
2020-03-04 16:21:24 +00:00
|
|
|
(find-file (cdr (assoc ref completions)))))
|
2020-03-01 07:36:39 +00:00
|
|
|
|
|
|
|
;;;; org-roam-switch-to-buffer
|
|
|
|
(defun org-roam--get-roam-buffers ()
|
2020-03-08 07:58:13 +00:00
|
|
|
"Return a list of buffers that are Org-roam files."
|
2020-03-01 07:36:39 +00:00
|
|
|
(--filter (and (with-current-buffer it (derived-mode-p 'org-mode))
|
|
|
|
(buffer-file-name it)
|
|
|
|
(org-roam--org-roam-file-p (buffer-file-name it)))
|
|
|
|
(buffer-list)))
|
|
|
|
|
|
|
|
(defun org-roam-switch-to-buffer ()
|
2020-03-08 07:58:13 +00:00
|
|
|
"Switch to an existing Org-roam buffer."
|
2020-03-01 07:36:39 +00:00
|
|
|
(interactive)
|
|
|
|
(let* ((roam-buffers (org-roam--get-roam-buffers))
|
|
|
|
(names-and-buffers (mapcar (lambda (buffer)
|
|
|
|
(cons (or (org-roam--get-title-or-slug
|
|
|
|
(buffer-file-name buffer))
|
|
|
|
(buffer-name buffer))
|
|
|
|
buffer))
|
|
|
|
roam-buffers)))
|
|
|
|
(unless roam-buffers
|
2020-03-06 12:15:33 +00:00
|
|
|
(user-error "No roam buffers"))
|
2020-03-28 13:16:28 +00:00
|
|
|
(when-let ((name (org-roam-completion--completing-read "Buffer: " names-and-buffers
|
|
|
|
:require-match t)))
|
2020-03-01 07:36:39 +00:00
|
|
|
(switch-to-buffer (cdr (assoc name names-and-buffers))))))
|
2020-02-06 11:26:38 +00:00
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
;;;; Daily notes
|
2020-03-13 05:29:49 +00:00
|
|
|
(defcustom org-roam-date-title-format "%Y-%m-%d"
|
2020-03-21 19:16:38 +00:00
|
|
|
"Format string passed to `format-time-string' for getting a date file's title."
|
|
|
|
:type 'string
|
|
|
|
:group 'org-roam)
|
2020-03-13 05:29:49 +00:00
|
|
|
|
|
|
|
(defcustom org-roam-date-filename-format "%Y-%m-%d"
|
2020-03-21 19:16:38 +00:00
|
|
|
"Format string passed to `format-time-string' for getting a date file's filename."
|
|
|
|
:type 'string
|
|
|
|
:group 'org-roam)
|
2020-03-13 05:29:49 +00:00
|
|
|
|
2020-03-28 13:16:28 +00:00
|
|
|
(defun org-roam--file-path-from-id (id)
|
|
|
|
"The file path for an Org-roam file, with identifier ID."
|
|
|
|
(file-truename
|
|
|
|
(expand-file-name
|
|
|
|
(if org-roam-encrypt-files
|
|
|
|
(concat id ".org.gpg")
|
|
|
|
(concat id ".org"))
|
|
|
|
org-roam-directory)))
|
|
|
|
|
2020-02-23 09:11:49 +00:00
|
|
|
(defun org-roam--file-for-time (time)
|
|
|
|
"Create and find file for TIME."
|
2020-03-13 05:29:49 +00:00
|
|
|
(let* ((title (format-time-string org-roam-date-title-format time))
|
|
|
|
(filename (format-time-string org-roam-date-filename-format time))
|
|
|
|
(file-path (org-roam--file-path-from-id filename)))
|
2020-03-06 11:35:22 +00:00
|
|
|
(if (file-exists-p file-path)
|
2020-03-17 17:11:08 +00:00
|
|
|
(find-file file-path)
|
2020-03-28 13:16:28 +00:00
|
|
|
(let ((org-roam-capture-templates (list (list "d" "daily" 'plain (list 'function #'org-roam-capture--get-point)
|
2020-03-06 11:35:22 +00:00
|
|
|
""
|
|
|
|
:immediate-finish t
|
2020-03-24 03:47:40 +00:00
|
|
|
:file-name "${filename}"
|
2020-03-06 11:35:22 +00:00
|
|
|
:head "#+TITLE: ${title}")))
|
2020-03-28 13:16:28 +00:00
|
|
|
(org-roam-capture--context 'title)
|
|
|
|
(org-roam-capture--info (list (cons 'title title)
|
2020-03-24 03:47:40 +00:00
|
|
|
(cons 'filename filename))))
|
2020-03-28 13:16:28 +00:00
|
|
|
(add-hook 'org-capture-after-finalize-hook #'org-roam-capture--find-file-h)
|
2020-03-29 09:29:57 +00:00
|
|
|
(org-roam--capture)))))
|
2020-02-23 09:11:49 +00:00
|
|
|
|
2020-01-21 10:19:51 +00:00
|
|
|
(defun org-roam-today ()
|
2020-02-23 09:11:49 +00:00
|
|
|
"Create and find file for today."
|
2020-01-21 10:19:51 +00:00
|
|
|
(interactive)
|
2020-03-17 17:11:08 +00:00
|
|
|
(org-roam--file-for-time (current-time)))
|
2020-02-06 11:26:38 +00:00
|
|
|
|
2020-04-02 05:32:22 +00:00
|
|
|
(defun org-roam-tomorrow (n)
|
|
|
|
"Create and find the file for tomorrow.
|
|
|
|
With numeric argument N, use N days in the future."
|
|
|
|
(interactive "p")
|
|
|
|
(org-roam--file-for-time (time-add (* n 86400) (current-time))))
|
|
|
|
|
|
|
|
(defun org-roam-yesterday (n)
|
|
|
|
"Create and find the file for yesterday.
|
|
|
|
With numeric argument N, use N days in the past."
|
|
|
|
(interactive "p")
|
|
|
|
(org-roam-tomorrow (- n)))
|
2020-03-01 18:53:53 +00:00
|
|
|
|
2020-02-07 08:47:47 +00:00
|
|
|
(defun org-roam-date ()
|
|
|
|
"Create the file for any date using the calendar."
|
|
|
|
(interactive)
|
|
|
|
(let ((time (org-read-date nil 'to-time nil "Date: ")))
|
2020-03-17 17:11:08 +00:00
|
|
|
(org-roam--file-for-time time)))
|
2020-01-21 10:19:51 +00:00
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
;;; The org-roam buffer
|
|
|
|
;;;; org-roam-link-face
|
|
|
|
(defface org-roam-link
|
|
|
|
'((t :inherit org-link))
|
|
|
|
"Face for org-roam link."
|
|
|
|
:group 'org-roam-faces)
|
|
|
|
|
2020-03-08 06:54:56 +00:00
|
|
|
(defface org-roam-backlink
|
|
|
|
'((t :inherit org-block))
|
|
|
|
"Face for org-roam backlinks in backlinks buffer"
|
|
|
|
:group 'org-roam-faces)
|
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
(defun org-roam--roam-link-face (path)
|
|
|
|
"Conditional face for org file links.
|
2020-03-23 07:34:21 +00:00
|
|
|
Applies `org-roam-link-face' if PATH corresponds to a Roam file."
|
2020-03-01 07:36:39 +00:00
|
|
|
(if (org-roam--org-roam-file-p path)
|
|
|
|
'org-roam-link
|
|
|
|
'org-link))
|
|
|
|
|
|
|
|
;;;; org-roam-backlinks-mode
|
2020-03-08 07:58:13 +00:00
|
|
|
(defvar org-roam-backlinks-mode-map
|
2020-03-27 05:06:44 +00:00
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map [mouse-1] 'org-open-at-point)
|
|
|
|
(define-key map (kbd "RET") 'org-open-at-point)
|
|
|
|
map)
|
2020-03-08 07:58:13 +00:00
|
|
|
"Keymap for `org-roam-backlinks-mode'.")
|
|
|
|
|
2020-03-27 05:06:44 +00:00
|
|
|
(define-minor-mode org-roam-backlinks-mode
|
|
|
|
"Minor mode for the `org-roam-buffer'.
|
|
|
|
\\{org-roam-backlinks-mode-map}"
|
|
|
|
:lighter " Backlinks"
|
|
|
|
:keymap org-roam-backlinks-mode-map
|
|
|
|
|
|
|
|
(if org-roam-backlinks-mode
|
|
|
|
(add-hook 'org-open-at-point-functions
|
|
|
|
'org-roam-open-at-point nil 'local)
|
|
|
|
(remove-hook 'org-open-at-point-functions
|
|
|
|
'org-roam-open-at-point 'local)))
|
2020-02-18 03:41:13 +00:00
|
|
|
|
2020-02-25 15:09:37 +00:00
|
|
|
(defun org-roam-open-at-point ()
|
2020-03-27 05:06:44 +00:00
|
|
|
"Open an Org-roam link or visit the text previewed at point.
|
2020-03-08 07:58:13 +00:00
|
|
|
When point is on an Org-roam link, open the link in the Org-roam window.
|
|
|
|
When point is on the Org-roam preview text, open the link in the Org-roam
|
2020-02-25 15:09:37 +00:00
|
|
|
window, and navigate to the point.
|
2020-03-27 05:06:44 +00:00
|
|
|
This function hooks into `org-open-at-point' via `org-open-at-point-functions'."
|
|
|
|
(cond
|
|
|
|
;; Org-roam link
|
|
|
|
((let* ((context (org-element-context))
|
|
|
|
(type (org-element-property :type context))
|
|
|
|
(path (org-element-property :path context)))
|
2020-02-25 15:09:37 +00:00
|
|
|
(when (and (eq (org-element-type context) 'link)
|
2020-03-27 05:06:44 +00:00
|
|
|
(string= "file" type)
|
|
|
|
(org-roam--org-roam-file-p (file-truename path)))
|
|
|
|
(org-roam--find-file path)
|
2020-02-25 15:09:37 +00:00
|
|
|
(org-show-context)
|
2020-03-27 05:06:44 +00:00
|
|
|
t)))
|
|
|
|
;; Org-roam preview text
|
|
|
|
((when-let ((file-from (get-text-property (point) 'file-from))
|
|
|
|
(p (get-text-property (point) 'file-from-point)))
|
|
|
|
(org-roam--find-file file-from)
|
|
|
|
(goto-char p)
|
|
|
|
(org-show-context)
|
|
|
|
t))
|
|
|
|
;; If called via `org-open-at-point', fall back to default behavior.
|
2020-03-28 13:16:28 +00:00
|
|
|
(t nil)))
|
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-04-06 08:48:17 +00:00
|
|
|
(defun org-roam--get-backlinks (target)
|
|
|
|
"Return the backlinks for TARGET.
|
|
|
|
TARGET may be a file, for Org-roam file links, or a citation key,
|
|
|
|
for Org-ref cite links."
|
|
|
|
(org-roam-db-query [:select [from, to, properties] :from links
|
|
|
|
:where (= to $s1)
|
|
|
|
:order-by (asc from)]
|
|
|
|
target))
|
2020-02-29 07:56:08 +00:00
|
|
|
|
2020-04-10 06:38:41 +00:00
|
|
|
;;;###autoload
|
|
|
|
(defalias 'org-roam 'org-roam-buffer-toggle-display)
|
2020-03-01 07:36:39 +00:00
|
|
|
|
|
|
|
;;; The global minor org-roam-mode
|
|
|
|
(defvar org-roam-mode-map
|
|
|
|
(make-sparse-keymap)
|
2020-03-10 16:55:42 +00:00
|
|
|
"Keymap for mode `org-roam-mode'.")
|
2020-02-20 09:33:30 +00:00
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
;;;###autoload
|
|
|
|
(define-minor-mode org-roam-mode
|
|
|
|
"Minor mode for Org-roam.
|
2020-02-22 13:11:14 +00:00
|
|
|
|
2020-03-01 07:36:39 +00:00
|
|
|
This mode sets up several hooks, to ensure that the cache is updated on file
|
|
|
|
changes, renames and deletes. It is also in charge of graceful termination of
|
|
|
|
the database connection.
|
|
|
|
|
|
|
|
When called interactively, toggle `org-roam-mode'. with prefix
|
|
|
|
ARG, enable `org-roam-mode' if ARG is positive, otherwise disable
|
|
|
|
it.
|
|
|
|
|
|
|
|
When called from Lisp, enable `org-roam-mode' if ARG is omitted,
|
|
|
|
nil, or positive. If ARG is `toggle', toggle `org-roam-mode'.
|
|
|
|
Otherwise, behave as if called interactively."
|
2020-03-05 16:19:11 +00:00
|
|
|
:lighter " Org-roam"
|
2020-03-01 07:36:39 +00:00
|
|
|
:keymap org-roam-mode-map
|
|
|
|
:group 'org-roam
|
|
|
|
:require 'org-roam
|
|
|
|
:global t
|
|
|
|
(cond
|
|
|
|
(org-roam-mode
|
2020-03-28 13:16:28 +00:00
|
|
|
(org-roam-db-build-cache)
|
2020-03-01 07:36:39 +00:00
|
|
|
(add-hook 'find-file-hook #'org-roam--find-file-hook-function)
|
2020-03-28 13:16:28 +00:00
|
|
|
(add-hook 'kill-emacs-hook #'org-roam-db--close-all)
|
2020-03-01 07:36:39 +00:00
|
|
|
(advice-add 'rename-file :after #'org-roam--rename-file-advice)
|
|
|
|
(advice-add 'delete-file :before #'org-roam--delete-file-advice))
|
|
|
|
(t
|
|
|
|
(remove-hook 'find-file-hook #'org-roam--find-file-hook-function)
|
2020-03-28 13:16:28 +00:00
|
|
|
(remove-hook 'kill-emacs-hook #'org-roam-db--close-all)
|
2020-03-01 07:36:39 +00:00
|
|
|
(advice-remove 'rename-file #'org-roam--rename-file-advice)
|
|
|
|
(advice-remove 'delete-file #'org-roam--delete-file-advice)
|
2020-03-28 13:16:28 +00:00
|
|
|
(org-roam-db--close-all)
|
2020-03-01 07:36:39 +00:00
|
|
|
;; Disable local hooks for all org-roam buffers
|
|
|
|
(dolist (buf (org-roam--get-roam-buffers))
|
|
|
|
(with-current-buffer buf
|
2020-03-08 07:58:13 +00:00
|
|
|
(org-link-set-parameters "file" :face 'org-link)
|
2020-04-10 06:38:41 +00:00
|
|
|
(remove-hook 'post-command-hook #'org-roam-buffer--update-maybe t)
|
2020-03-28 13:16:28 +00:00
|
|
|
(remove-hook 'after-save-hook #'org-roam-db--update-file t))))))
|
2020-02-22 13:11:14 +00:00
|
|
|
|
2020-02-20 09:33:30 +00:00
|
|
|
(defun org-roam--find-file-hook-function ()
|
2020-03-08 07:58:13 +00:00
|
|
|
"Called by `find-file-hook' when mode `org-roam-mode' is on."
|
2020-02-20 09:33:30 +00:00
|
|
|
(when (org-roam--org-roam-file-p)
|
2020-02-25 15:06:40 +00:00
|
|
|
(setq org-roam-last-window (get-buffer-window))
|
2020-04-10 06:38:41 +00:00
|
|
|
(add-hook 'post-command-hook #'org-roam-buffer--update-maybe nil t)
|
2020-03-28 13:16:28 +00:00
|
|
|
(add-hook 'after-save-hook #'org-roam-db--update-file nil t)
|
2020-03-08 07:58:13 +00:00
|
|
|
(org-link-set-parameters "file" :face 'org-roam--roam-link-face)
|
2020-04-10 06:38:41 +00:00
|
|
|
(org-roam-buffer--update-maybe :redisplay t)))
|
2020-02-25 15:06:40 +00:00
|
|
|
|
2020-02-20 09:33:30 +00:00
|
|
|
(defun org-roam--delete-file-advice (file &optional _trash)
|
2020-03-08 07:58:13 +00:00
|
|
|
"Advice for maintaining cache consistency when FILE is deleted."
|
2020-02-29 18:42:55 +00:00
|
|
|
(when (and (not (auto-save-file-name-p file))
|
|
|
|
(org-roam--org-roam-file-p file))
|
2020-03-28 13:16:28 +00:00
|
|
|
(org-roam-db--clear-file (file-truename file))))
|
2020-02-20 09:33:30 +00:00
|
|
|
|
2020-03-08 07:58:13 +00:00
|
|
|
(defun org-roam--rename-file-advice (file new-file &rest _args)
|
2020-02-20 00:32:54 +00:00
|
|
|
"Rename backlinks of FILE to refer to NEW-FILE."
|
|
|
|
(when (and (not (auto-save-file-name-p file))
|
|
|
|
(not (auto-save-file-name-p new-file))
|
|
|
|
(org-roam--org-roam-file-p new-file))
|
2020-03-28 13:16:28 +00:00
|
|
|
(org-roam-db--ensure-built)
|
2020-04-06 08:48:17 +00:00
|
|
|
(let* ((files-to-rename (org-roam-db-query [:select :distinct [from]
|
|
|
|
:from links
|
|
|
|
:where (= to $s1)
|
|
|
|
:and (= type $s2)]
|
|
|
|
file
|
|
|
|
"roam"))
|
2020-02-20 00:32:54 +00:00
|
|
|
(path (file-truename file))
|
|
|
|
(new-path (file-truename new-file))
|
|
|
|
(slug (org-roam--get-title-or-slug file))
|
2020-03-10 16:34:08 +00:00
|
|
|
(old-title (org-roam--format-link-title slug))
|
2020-03-28 13:16:28 +00:00
|
|
|
(new-slug (or (car (org-roam-db--get-titles path))
|
2020-02-29 14:09:04 +00:00
|
|
|
(org-roam--path-to-slug new-path)))
|
2020-03-10 16:34:08 +00:00
|
|
|
(new-title (org-roam--format-link-title new-slug)))
|
2020-03-28 13:16:28 +00:00
|
|
|
(org-roam-db--clear-file file)
|
2020-02-29 07:56:08 +00:00
|
|
|
(dolist (file-from files-to-rename)
|
|
|
|
(let* ((file-from (car file-from))
|
|
|
|
(file-from (if (string-equal (file-truename file-from)
|
|
|
|
path)
|
|
|
|
new-path
|
|
|
|
file-from))
|
|
|
|
(file-dir (file-name-directory file-from))
|
|
|
|
(relative-path (file-relative-name new-path file-dir))
|
|
|
|
(old-relative-path (file-relative-name path file-dir))
|
|
|
|
(slug-regex (regexp-quote (format "[[file:%s][%s]]" old-relative-path old-title)))
|
|
|
|
(named-regex (concat
|
|
|
|
(regexp-quote (format "[[file:%s][" old-relative-path))
|
|
|
|
"\\(.*\\)"
|
|
|
|
(regexp-quote "]]"))))
|
|
|
|
(with-temp-file file-from
|
|
|
|
(insert-file-contents file-from)
|
|
|
|
(while (re-search-forward slug-regex nil t)
|
|
|
|
(replace-match (format "[[file:%s][%s]]" relative-path new-title)))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (re-search-forward named-regex nil t)
|
|
|
|
(replace-match (format "[[file:%s][\\1]]" relative-path))))
|
2020-03-28 13:16:28 +00:00
|
|
|
(org-roam-db--update-file file-from)))
|
|
|
|
(org-roam-db--update-file new-path))))
|
2020-02-29 07:56: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:
|