2020-05-12 19:06:19 +00:00
|
|
|
;;; org-roam-macs.el --- Macros/utility functions -*- coding: utf-8; lexical-binding: t; -*-
|
2020-03-28 13:16:28 +00:00
|
|
|
|
|
|
|
;; Copyright © 2020 Jethro Kuan <jethrokuan95@gmail.com>
|
|
|
|
|
|
|
|
;; Author: Jethro Kuan <jethrokuan95@gmail.com>
|
2020-05-10 05:48:16 +00:00
|
|
|
;; URL: https://github.com/org-roam/org-roam
|
2020-03-28 13:16:28 +00:00
|
|
|
;; Keywords: org-mode, roam, convenience
|
2020-07-26 17:03:35 +00:00
|
|
|
;; Version: 1.2.1
|
2020-05-27 05:20:06 +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-sqlite3 "1.0.0"))
|
2020-03-28 13:16:28 +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.
|
|
|
|
;;
|
|
|
|
;; 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.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
2020-05-10 21:17:55 +00:00
|
|
|
;; This library implements macros and utility functions used throughout
|
|
|
|
;; org-roam.
|
2020-03-28 13:16:28 +00:00
|
|
|
;;
|
|
|
|
;;
|
|
|
|
;;; Code:
|
|
|
|
;;;; Library Requires
|
2020-07-07 05:07:10 +00:00
|
|
|
(require 'dash)
|
2020-03-28 13:16:28 +00:00
|
|
|
|
2020-05-10 08:44:28 +00:00
|
|
|
(defvar org-roam-verbose)
|
|
|
|
|
2020-08-07 08:06:24 +00:00
|
|
|
;;;; Utility Functions
|
|
|
|
(defun org-roam--list-interleave (lst separator)
|
|
|
|
"Interleaves elements in LST with SEPARATOR."
|
|
|
|
(when lst
|
|
|
|
(let ((new-lst (list (pop lst))))
|
|
|
|
(dolist (it lst)
|
|
|
|
(nconc new-lst (list separator it)))
|
|
|
|
new-lst)))
|
|
|
|
|
2020-06-06 08:48:42 +00:00
|
|
|
(defmacro org-roam--with-temp-buffer (file &rest body)
|
2020-03-28 13:16:28 +00:00
|
|
|
"Execute BODY within a temp buffer.
|
2020-06-06 08:48:42 +00:00
|
|
|
Like `with-temp-buffer', but propagates `org-roam-directory'.
|
|
|
|
If FILE, set `org-roam-temp-file-name' to file and insert its contents."
|
|
|
|
(declare (indent 1) (debug t))
|
2020-03-28 13:16:28 +00:00
|
|
|
(let ((current-org-roam-directory (make-symbol "current-org-roam-directory")))
|
|
|
|
`(let ((,current-org-roam-directory org-roam-directory))
|
|
|
|
(with-temp-buffer
|
2020-07-22 12:59:35 +00:00
|
|
|
(let ((org-roam-directory ,current-org-roam-directory)
|
|
|
|
(org-mode-hook nil))
|
|
|
|
(org-mode)
|
2020-06-06 08:48:42 +00:00
|
|
|
(when ,file
|
|
|
|
(insert-file-contents ,file)
|
|
|
|
(setq-local org-roam-file-name ,file))
|
2020-03-28 13:16:28 +00:00
|
|
|
,@body)))))
|
|
|
|
|
2020-05-10 21:17:55 +00:00
|
|
|
(defun org-roam-message (format-string &rest args)
|
|
|
|
"Pass FORMAT-STRING and ARGS to `message' when `org-roam-verbose' is t."
|
|
|
|
(when org-roam-verbose
|
|
|
|
(apply #'message `(,(concat "(org-roam) " format-string) ,@args))))
|
2020-05-09 10:08:08 +00:00
|
|
|
|
2020-07-07 05:07:10 +00:00
|
|
|
(defun org-roam-string-quote (str)
|
|
|
|
"Quote STR."
|
|
|
|
(->> str
|
|
|
|
(s-replace "\\" "\\\\")
|
|
|
|
(s-replace "\"" "\\\"")))
|
|
|
|
|
2020-08-05 12:52:27 +00:00
|
|
|
;;; Link Utilities
|
|
|
|
(defun org-roam-replace-fuzzy-link (new-loc &optional desc)
|
|
|
|
"Replace the current fuzzy link (e.g. [[Foo]]) with a NEW-LOC.
|
|
|
|
If DESC, also replace the desc"
|
|
|
|
(save-match-data
|
|
|
|
(unless (org-in-regexp org-link-bracket-re 1)
|
|
|
|
(user-error "No link at point"))
|
|
|
|
(let ((desc (or desc (match-string-no-properties 1)))
|
|
|
|
(remove (list (match-beginning 0) (match-end 0))))
|
|
|
|
(apply #'delete-region remove)
|
|
|
|
(insert (org-link-make-string new-loc desc)))
|
|
|
|
(sit-for 0)))
|
|
|
|
|
2020-07-27 18:16:39 +00:00
|
|
|
;;; Shielding regions
|
|
|
|
(defun org-roam-shield-region (beg end)
|
|
|
|
"Shield REGION against modifications.
|
|
|
|
REGION must be a cons-cell containing the marker to the region
|
|
|
|
beginning and maximum values."
|
|
|
|
(when (and beg end)
|
|
|
|
(add-text-properties beg end
|
|
|
|
'(font-lock-face org-roam-link-shielded
|
|
|
|
read-only t)
|
|
|
|
(marker-buffer beg))
|
|
|
|
(cons beg end)))
|
|
|
|
|
|
|
|
(defun org-roam-unshield-region (beg end)
|
|
|
|
"Unshield the shielded REGION."
|
|
|
|
(when (and beg end)
|
|
|
|
(let ((inhibit-read-only t))
|
|
|
|
(remove-text-properties beg end
|
|
|
|
'(font-lock-face org-roam-link-shielded
|
|
|
|
read-only t)
|
|
|
|
(marker-buffer beg)))
|
|
|
|
(cons beg end)))
|
|
|
|
|
2020-03-28 13:16:28 +00:00
|
|
|
(provide 'org-roam-macs)
|
|
|
|
|
|
|
|
;;; org-roam-macs.el ends here
|