branch: externals/org-remark commit d36debe2b460caa020414cfcee6d6be5d36385e5 Author: Noboru Ota <m...@nobiot.com> Commit: Noboru Ota <m...@nobiot.com>
add: factor out org-remark-legacy-convert --- org-remark-convert-legacy.el | 102 ++++++++++++++++++++++++++++++++++++++++++ org-remark-global-tracking.el | 15 ++++--- org-remark.el | 77 ++----------------------------- 3 files changed, 114 insertions(+), 80 deletions(-) diff --git a/org-remark-convert-legacy.el b/org-remark-convert-legacy.el new file mode 100644 index 0000000000..48ef8c0261 --- /dev/null +++ b/org-remark-convert-legacy.el @@ -0,0 +1,102 @@ +;;; org-remark-convert-legacy.el --- Convert legacy Org-marginalia files to Org-remark -*- lexical-binding: t; -*- + +;; Copyright (C) 2022 Noboru Ota + +;; Author: Noboru Ota <m...@nobiot.com> +;; URL: https://github.com/nobiot/org-remark +;; Last modified: 16 January 2022 +;; Created: 16 January 2022 +;; Package-Requires: ((emacs "27.1") (org "9.4")) +;; Keywords: org-mode, annotation, writing, note-taking, marginal notes + +;; 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 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>. + +;;; Commentary: +;; This file is part of Org-remark and contains a feature that helps users of +;; Org-marginalia (former name of Org-remark) convert their legacy +;; marginalia.org files to those compatible with Org-remark. +;; +;; (require 'org-remark-convert-legacy-data) for `org-remark' to use +;; `org-remark-convert-legacy-data' function to automatically convert legacy +;; data on save and load. Alternatively, use the same function as an +;; interactive command on a marginalia.org buffer that contains legacy +;; Org-marginalia data. + +;;; Code: + +(require 'org) +(require 'org-remark) + +(defun org-remark-convert-legacy-data () + "Convert the legacy Org-marginalia properties to those for Org-remark. + +You can call this function interactively to convert the current +buffer. It also gets automatically triggered when you save or +load Org-remark marginal notes file if +`org-remark-convert-legacy' user option is non-nil. + +This function checks whether or not there is at least one legacy entry with +property \"marginalia-source-file\" in the current buffer. + +If one found, this function will: + +1. Create a backup copy with the filename \"<current-file-name>_archive\" +2. Convert all \"marginalia-*\" properties to \"org-remark-*\" equivalents + +- marginalia-source-file -> org-remark-file +- marginalia-id -> org-remark-id +- marginalia-source-beg -> org-remark-beg +- marginalia-source-end -> org-remark-end + +This assumes that all the \"marginalia-*\" properties were used +solely by Org-marginalia." + (interactive) + (org-with-wide-buffer + ;; Check that there is at least one legacy entry in the current buffer + (goto-char (point-min)) + (when (save-excursion (org-find-property "marginalia-source-file")) + ;; Do the process only when there is at least one entry + ;; Create a backup copy + (let ((filename (abbreviate-file-name + (concat (buffer-file-name) "_archive")))) + (write-region (point-min) (point-max) filename) + (message (format "org-remark: created backup file %s" filename))) + ;; Scan the whole marginal notes file + (while (not (org-next-visible-heading 1)) + (when-let (source-file (org-entry-get (point) "marginalia-source-file")) + (org-delete-property "marginalia-source-file") + (org-set-property org-remark-prop-source-file source-file)) + (when-let ((id (org-entry-get (point) "marginalia-id")) + (beg (string-to-number + (org-entry-get (point) + "marginalia-source-beg"))) + (end (string-to-number + (org-entry-get (point) + "marginalia-source-end")))) + (org-delete-property "marginalia-id") + (org-delete-property "marginalia-source-beg") + (org-delete-property "marginalia-source-end") + (org-set-property org-remark-prop-id id) + (org-remark-notes-set-properties beg end))) + (goto-char (point-min)) + (message (format "org-remark: Legacy \"marginalia-*\" properties updated for %s" + (abbreviate-file-name (buffer-file-name)))) + t))) + +(provide 'org-remark-convert-legacy) + +;;; org-remark-convert-legacy.el ends here + diff --git a/org-remark-global-tracking.el b/org-remark-global-tracking.el index 8e44370afc..17662d70f5 100644 --- a/org-remark-global-tracking.el +++ b/org-remark-global-tracking.el @@ -4,7 +4,7 @@ ;; Author: Noboru Ota <m...@nobiot.com> ;; URL: https://github.com/nobiot/org-remark -;; Last modified: 13 January 2022 +;; Last modified: 16 January 2022 ;; Package-Requires: ((emacs "27.1") (org "9.4")) ;; Keywords: org-mode, annotation, writing, note-taking, marginal notes @@ -39,7 +39,8 @@ saved in `org-remark-tracking-file' automatically loads highlights." :group 'org-remark :type 'file) -(defvar org-remark-files-tracked nil) +(defvar org-remark-files-tracked nil + "List of files being tracked by `org-remark-global-tracking-mode'.") ;;;###autoload (define-minor-mode org-remark-global-tracking-mode @@ -55,11 +56,11 @@ locally for the file opened." ;; Activate ;; Prioritise the new `org-remark-tracking-file' over the legacy one (when-let (tracking-file (or (when (file-exists-p - org-remark-tracking-file) - org-remark-tracking-file) - (when (file-exists-p - (org-remark-legacy-tracking-file-get)) - (org-remark-legacy-tracking-file-get)))) + org-remark-tracking-file) + org-remark-tracking-file) + (when (file-exists-p + (org-remark-legacy-tracking-file-get)) + (org-remark-legacy-tracking-file-get)))) (org-remark-tracking-load tracking-file)) ;; `org-remark-tracking-save' should be added to kill hook even when no ;; tracking file existed before -- this would indicate first time use of diff --git a/org-remark.el b/org-remark.el index 797e6a90e6..363a51ed17 100644 --- a/org-remark.el +++ b/org-remark.el @@ -6,7 +6,7 @@ ;; URL: https://github.com/nobiot/org-remark ;; Version: 1.0.0-rc ;; Created: 22 December 2020 -;; Last modified: 15 January 2022 +;; Last modified: 16 January 2022 ;; Package-Requires: ((emacs "27.1") (org "9.4")) ;; Keywords: org-mode, annotation, writing, note-taking, marginal-notes @@ -37,7 +37,7 @@ (require 'org) (require 'org-id) (require 'org-remark-global-tracking) -(declare-function org-collect-keywords "org") +(declare-function org-remark-convert-legacy-data "org-remark-convert-legacy") ;;;; Customization @@ -90,17 +90,6 @@ name." "Define if Org-remark use Org-ID to link back to the main note." :type 'boolean) -(defcustom org-remark-convert-legacy t - "Specify whether or not Org-remark automatically performs data conversion. -The conversion is done via function -`org-remark-covert-legacy-data'. Set to nil if you do not have a -marginalia file from the Org-marginalia package (it's the -previous name of Org-remark). - -It is safe to keep it with minimal performance overhead but -recommended to turn it off if not required." - :type 'boolean) - ;;;; Variables @@ -525,64 +514,6 @@ and removing overlays are not part of the undo tree." (org-remark-single-highlight-remove id arg) t)) -;;;; Legacy data conversion from Org-marginalia - -(defun org-remark-convert-legacy-data () - "Convert the legacy Org-marginalia properties to those for Org-remark. - -You can call this function interactively to convert the current -buffer. It also gets automatically triggered when you save or -load Org-remark marginal notes file if -`org-remark-convert-legacy' user option is non-nil. - -This function checks whether or not there is at least one legacy entry with -property \"marginalia-source-file\" in the current buffer. - -If one found, this function will: - -1. Create a backup copy with the filename \"<current-file-name>_archive\" -2. Convert all \"marginalia-*\" properties to \"org-remark-*\" equivalents - -- marginalia-source-file -> org-remark-file -- marginalia-id -> org-remark-id -- marginalia-source-beg -> org-remark-beg -- marginalia-source-end -> org-remark-end - -This assumes that all the \"marginalia-*\" properties were used -solely by Org-marginalia." - (interactive) - (org-with-wide-buffer - ;; Check that there is at least one legacy entry in the current buffer - (goto-char (point-min)) - (when (save-excursion (org-find-property "marginalia-source-file")) - ;; Do the process only when there is at least one entry - ;; Create a backup copy - (let ((filename (abbreviate-file-name - (concat (buffer-file-name) "_archive")))) - (write-region (point-min) (point-max) filename) - (message (format "org-remark: created backup file %s" filename))) - ;; Scan the whole marginal notes file - (while (not (org-next-visible-heading 1)) - (when-let (source-file (org-entry-get (point) "marginalia-source-file")) - (org-delete-property "marginalia-source-file") - (org-set-property org-remark-prop-source-file source-file)) - (when-let ((id (org-entry-get (point) "marginalia-id")) - (beg (string-to-number - (org-entry-get (point) - "marginalia-source-beg"))) - (end (string-to-number - (org-entry-get (point) - "marginalia-source-end")))) - (org-delete-property "marginalia-id") - (org-delete-property "marginalia-source-beg") - (org-delete-property "marginalia-source-end") - (org-set-property org-remark-prop-id id) - (org-remark-notes-set-properties beg end))) - (goto-char (point-min)) - (message (format "org-remark: Legacy \"marginalia-*\" properties updated for %s" - (abbreviate-file-name (buffer-file-name)))) - t))) - ;;;; Internal Functions @@ -711,7 +642,7 @@ packages such as Org-roam's backlink feature." ;; If it is a new empty marginalia file (when (and (org-remark-empty-buffer-p) org-remark-use-org-id) (org-id-get-create)) - (when org-remark-convert-legacy (org-remark-convert-legacy-data)) + (when (featurep 'org-remark-convert-legacy) (org-remark-convert-legacy-data)) (org-with-wide-buffer (let ((file-headline (or (org-find-property org-remark-prop-source-file path) @@ -817,7 +748,7 @@ Each highlight is a list in the following structure: ;; TODO check if there is any relevant notes for the current file (let ((highlights)) (with-current-buffer notes-buf - (when org-remark-convert-legacy + (when (featurep 'org-remark-convert-legacy) (org-remark-convert-legacy-data)) (org-with-wide-buffer (let ((heading (org-find-property