;;; org-roam-reflinks.el --- The reflinks section -*- lexical-binding: t -*- ;; Copyright © 2020 Jethro Kuan ;; Author: Jethro Kuan ;; URL: https://github.com/org-roam/org-roam ;; Keywords: org-mode, roam, convenience ;; Version: 2.0.0 ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (s "1.12.0") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; 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: ;; ;; This library provides functionality dealing with nodes. ;; ;;; Code: ;;;; Library Requires (require 'magit-section) (require 'org-roam-structs) (defvar org-roam-mode-sections) (defvar org-roam-mode-map) ;;; Section ;;;; Definition ;;; Functions (cl-defmethod org-roam-populate ((reflink org-roam-reflink)) "Populate REFLINK from database." (setf (org-roam-reflink-source-node reflink) (org-roam-populate (org-roam-reflink-source-node reflink))) reflink) (defun org-roam-reflinks-get (node) "Return the reflinks for NODE." (let ((refs (org-roam-db-query [:select [ref] :from refs :where (= node-id $s1)] (org-roam-node-id node))) links) (pcase-dolist (`(,ref) refs) (pcase-dolist (`(,source-id ,pos ,properties) (org-roam-db-query [:select [source pos properties] :from links :where (= dest $s1)] ref)) (push (org-roam-populate (org-roam-reflink-create :source-node (org-roam-node-create :id source-id) :ref ref :point pos :properties properties)) links))) links)) (defun org-roam-reflinks-sort (a b) "Default sorting function for reflinks A and B. Sorts by title." (string< (org-roam-node-title (org-roam-reflink-source-node a)) (org-roam-node-title (org-roam-reflink-source-node b)))) ;;; Section inserter (cl-defun org-roam-reflinks-insert-section (&key node _file) "Insert reflinks section for NODE." (when (org-roam-node-refs node) (let* ((reflinks (seq-sort #'org-roam-reflinks-sort (org-roam-reflinks-get node)))) (magit-insert-section (org-roam-reflinks) (magit-insert-heading "Reflinks:") (dolist (reflink reflinks) (org-roam-node-insert-section :source-node (org-roam-reflink-source-node reflink) :point (org-roam-reflink-point reflink) :properties (org-roam-reflink-properties reflink))) (insert ?\n))))) (provide 'org-roam-reflinks) ;;; org-roam-reflinks.el ends here