org-roam-compat.el (11014B)
1 ;;; org-roam-compat.el --- Backward compatibility code -*- coding: utf-8; lexical-binding: t; -*- 2 3 ;; Copyright © 2020-2022 Jethro Kuan <jethrokuan95@gmail.com> 4 5 ;; Author: Jethro Kuan <jethrokuan95@gmail.com> 6 ;; URL: https://github.com/org-roam/org-roam 7 ;; Keywords: org-mode, roam, convenience 8 ;; Version: 2.2.2 9 ;; Package-Requires: ((emacs "26.1")) 10 11 ;; This file is NOT part of GNU Emacs. 12 13 ;; This program is free software; you can redistribute it and/or modify 14 ;; it under the terms of the GNU General Public License as published by 15 ;; the Free Software Foundation; either version 3, or (at your option) 16 ;; any later version. 17 ;; 18 ;; This program is distributed in the hope that it will be useful, 19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 ;; GNU General Public License for more details. 22 ;; 23 ;; You should have received a copy of the GNU General Public License 24 ;; along with GNU Emacs; see the file COPYING. If not, write to the 25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 26 ;; Boston, MA 02110-1301, USA. 27 28 ;;; Commentary: 29 ;; 30 ;; This file is dedicated to maintain backward compatibility with older older 31 ;; Emacsen and Org-roam versions. 32 ;; 33 ;;; Code: 34 (require 'org-roam) 35 36 ;;; Backports 37 ;; REVIEW Remove when 26.x support is dropped. This is exact the same as 38 ;; `directory-files-recursively' from Emacs 26, but with FOLLOW-SYMLINKS 39 ;; parameter from Emacs 27. 40 (defun org-roam--directory-files-recursively (dir regexp 41 &optional include-directories predicate 42 follow-symlinks) 43 "Return list of all files under directory DIR whose names match REGEXP. 44 This function works recursively. Files are returned in \"depth 45 first\" order, and files from each directory are sorted in 46 alphabetical order. Each file name appears in the returned list 47 in its absolute form. 48 49 By default, the returned list excludes directories, but if 50 optional argument INCLUDE-DIRECTORIES is non-nil, they are 51 included. 52 53 PREDICATE can be either nil (which means that all subdirectories 54 of DIR are descended into), t (which means that subdirectories that 55 can't be read are ignored), or a function (which is called with 56 the name of each subdirectory, and should return non-nil if the 57 subdirectory is to be descended into). 58 59 If FOLLOW-SYMLINKS is non-nil, symbolic links that point to 60 directories are followed. Note that this can lead to infinite 61 recursion." 62 (let* ((result nil) 63 (files nil) 64 (dir (directory-file-name dir)) 65 ;; When DIR is "/", remote file names like "/method:" could 66 ;; also be offered. We shall suppress them. 67 (tramp-mode (and tramp-mode (file-remote-p (expand-file-name dir))))) 68 (dolist (file (sort (file-name-all-completions "" dir) 69 'string<)) 70 (unless (member file '("./" "../")) 71 (if (directory-name-p file) 72 (let* ((leaf (substring file 0 (1- (length file)))) 73 (full-file (concat dir "/" leaf))) 74 ;; Don't follow symlinks to other directories. 75 (when (and (or (not (file-symlink-p full-file)) 76 (and (file-symlink-p full-file) 77 follow-symlinks)) 78 ;; Allow filtering subdirectories. 79 (or (eq predicate nil) 80 (eq predicate t) 81 (funcall predicate full-file))) 82 (let ((sub-files 83 (if (eq predicate t) 84 (condition-case nil 85 (org-roam--directory-files-recursively 86 full-file regexp include-directories 87 predicate follow-symlinks) 88 (file-error nil)) 89 (org-roam--directory-files-recursively 90 full-file regexp include-directories 91 predicate follow-symlinks)))) 92 (setq result (nconc result sub-files)))) 93 (when (and include-directories 94 (string-match regexp leaf)) 95 (setq result (nconc result (list full-file))))) 96 (when (string-match regexp file) 97 (push (concat dir "/" file) files))))) 98 (nconc result (nreverse files)))) 99 100 ;;; Compatibility hacks and patches 101 (advice-add #'org-id-add-location :around #'org-roam--handle-absent-org-id-locations-file-a) 102 (defun org-roam--handle-absent-org-id-locations-file-a (fn &rest args) 103 "Gracefully handle errors related to absence of `org-id-locations-file'. 104 FN is `org-id-add-location' that comes from advice and ARGS are 105 passed to it." 106 (condition-case err 107 (apply fn args) 108 ;; `org-id' makes the assumption that `org-id-locations-file' will be stored in `user-emacs-directory' 109 ;; which always exist if you have Emacs, so it uses `with-temp-file' to write to the file. However, the 110 ;; users *do* change the path to this file and `with-temp-file' unable to create the file, if the path to 111 ;; it consists of directories that don't exist. We'll have to handle this ourselves. 112 (error 113 (advice-remove 'org-id-add-location #'org-roam--handle-absent-org-id-locations-file-a) 114 (if (file-exists-p (file-truename org-id-locations-file)) 115 (signal (car err) (cdr err)) 116 ;; Pre-allocate the hash table to avoid weird access related errors during the regeneration. 117 (or org-id-locations (setq org-id-locations (make-hash-table :test 'equal))) 118 ;; If permissions allow that, try to create the user specified directory path to 119 ;; `org-id-locations-file' ourselves. 120 (condition-case _err 121 (progn (org-roam-message (concat "`org-id-locations-file' (%s) doesn't exist. " 122 "Trying to regenerate it (this may take a while)...") 123 org-id-locations-file) 124 (make-directory (file-name-directory (file-truename org-id-locations-file))) 125 (org-roam-update-org-id-locations) 126 (apply fn args)) 127 ;; In case of failure (lack of permissions), we'll patch it to at least handle the current session 128 ;; without errors. 129 (file-error (org-roam-message "Failed to regenerate `org-id-locations-file'") 130 (lwarn 'org-roam :error " 131 -------- 132 WARNING: `org-id-locations-file' (%s) doesn't exist! 133 Org-roam is unable to create it for you. 134 -------- 135 136 This happens when Emacs doesn't have permissions to create the 137 path to your `org-id-locations-file'. Org-roam will now fallback 138 storing the file in your current `org-roam-directory', but the 139 warning will keep popup with each new session. 140 141 To stop this warning from popping up, set `org-id-locations-file' 142 to the location you want and ensure that the path exists on your 143 filesystem, then run M-x `org-roam-update-org-id-locations'. 144 145 Note: While Org-roam doesn't depend on `org-id-locations-file' to 146 lookup IDs for the nodes that are stored in the database, it 147 still tries to keep it updated so IDs work across other files in 148 Org-mode, so the IDs used in your `org-roam-directory' would be 149 able to cross-reference outside of `org-roam-directory'. It also 150 allows to keep linking with \"id:\" links within the current 151 `org-roam-directory' to headings and files that are excluded from 152 identification (e.g. with \"ROAM_EXCLUDE\" property) as Org-roam 153 nodes." org-id-locations-file) 154 (setq org-id-locations-file 155 (expand-file-name ".orgids" (file-truename org-roam-directory))) 156 (apply fn args))))))) 157 158 ;;;; Deprecated :if-new capture template keyword 159 (with-eval-after-load 'org-roam-capture 160 (add-to-list 'org-roam-capture--template-keywords :if-new) 161 162 (let ((inhibit-warning-p t)) ; REVIEW Set this to nil close to next major release 163 (advice-add 'org-roam-capture--get-target :around #'org-roam-capture--get-if-new-target-a) 164 (defun org-roam-capture--get-if-new-target-a (fn &rest args) 165 "Get the current capture target using deprecated :if-new property." 166 (if-let ((target (org-roam-capture--get :if-new))) 167 (prog1 target 168 (unless inhibit-warning-p 169 (lwarn 'org-roam-capture :warning 170 (mapconcat 171 #'identity 172 ["`:if-new' property is deprecated in favor of `:target'." 173 "This warning will popup once per each session. In order to get" 174 "rid of it, rename all the references to the `:if-new' property" 175 "in your capture templates to `:target'."] 176 "\n")) 177 ;; Don't irritate the user too much. Displaying the warning once per session should be enough. 178 (setq inhibit-warning-p t))) 179 (apply fn args))))) 180 181 ;;; Obsolete aliases (remove after next major release) 182 (define-obsolete-function-alias 183 'org-roam-setup 184 'org-roam-db-autosync-enable "org-roam 2.0") 185 (define-obsolete-function-alias 186 'org-roam-teardown 187 'org-roam-db-autosync-disable "org-roam 2.0") 188 189 (define-obsolete-variable-alias 190 'org-roam-current-node 191 'org-roam-buffer-current-node "org-roam 2.0") 192 (define-obsolete-variable-alias 193 'org-roam-current-directory 194 'org-roam-buffer-current-directory "org-roam 2.0") 195 (define-obsolete-function-alias 196 'org-roam-buffer-render 197 'org-roam-buffer-render-contents "org-roam 2.0") 198 (define-obsolete-function-alias 199 'org-roam-buffer 200 'org-roam-buffer-display-dedicated "org-roam 2.0") 201 (define-obsolete-function-alias 202 'org-roam-visit-thing 203 'org-roam-buffer-visit-thing "org-roam 2.0") 204 205 (define-obsolete-function-alias 206 'org-roam-dailies-find-today 207 'org-roam-dailies-goto-today "org-roam 2.0") 208 (define-obsolete-function-alias 209 'org-roam-dailies-find-yesterday 210 'org-roam-dailies-goto-yesterday "org-roam 2.0") 211 (define-obsolete-function-alias 212 'org-roam-dailies-find-tomorrow 213 'org-roam-dailies-goto-tomorrow "org-roam 2.0") 214 (define-obsolete-function-alias 215 'org-roam-dailies-find-next-note 216 'org-roam-dailies-goto-next-note "org-roam 2.0") 217 (define-obsolete-function-alias 218 'org-roam-dailies-find-previous-note 219 'org-roam-dailies-goto-previous-note "org-roam 2.0") 220 (define-obsolete-function-alias 221 'org-roam-dailies-find-date 222 'org-roam-dailies-goto-date "org-roam 2.0") 223 224 (define-obsolete-function-alias 225 'org-roam-add-property 226 'org-roam-property-add "org-roam 2.1") 227 228 (define-obsolete-function-alias 229 'org-roam-remove-property 230 'org-roam-property-remove "org-roam 2.1") 231 232 (define-obsolete-variable-alias 233 'org-roam-mode-section-functions 234 'org-roam-mode-sections "org-roam 2.2.0") 235 236 ;;; Obsolete functions 237 (make-obsolete 'org-roam-get-keyword 'org-collect-keywords "org-roam 2.0") 238 239 (provide 'org-roam-compat) 240 241 ;;; org-roam-compat.el ends here