branch: elpa/gnosis
commit 3cd630853384f27780f9b3fcee0e64abf7ab7964
Author: Thanos Apollo <[email protected]>
Commit: Thanos Apollo <[email protected]>
utils: Adjust for link replacement edge cases.
---
gnosis-utils.el | 40 ++++++++++++++++++++++++++++++++++------
1 file changed, 34 insertions(+), 6 deletions(-)
diff --git a/gnosis-utils.el b/gnosis-utils.el
index 2cc89c10cf..34ee84f64e 100644
--- a/gnosis-utils.el
+++ b/gnosis-utils.el
@@ -61,14 +61,42 @@ Optionally, use custom DEFAULT-FACE."
nil t))))
(buffer-string)))
+(defconst gnosis-utils--org-link-re
+ "\\[\\[[^]]*\\]\\[[^]]*\\]\\]\\|\\[\\[[^]]*\\]\\]"
+ "Regexp matching org-mode links: [[target][desc]] or [[target]].")
+
+(defun gnosis-utils-string-outside-links-p (text string)
+ "Return non-nil if STRING appears in TEXT outside of org-links."
+ (let ((case-fold-search nil)
+ (target (regexp-quote string))
+ (pos 0))
+ (catch 'found
+ (while (string-match gnosis-utils--org-link-re text pos)
+ (when (string-match-p target (substring text pos (match-beginning 0)))
+ (throw 'found t))
+ (setq pos (match-end 0)))
+ (string-match-p target (substring text pos)))))
+
(defun gnosis-utils-replace-string-with-link (text string node-id)
- "Replace STRING in TEXT with org-link to NODE-ID.
+ "Replace STRING in TEXT with org-link to NODE-ID, skipping existing links.
Returns (MODIFIED-P . NEW-TEXT)."
- (let ((new-text (replace-regexp-in-string
- (regexp-quote string)
- (format "[[id:%s][%s]]" node-id string)
- text t t)))
- (cons (not (string= text new-text)) new-text)))
+ (let ((case-fold-search nil)
+ (target (regexp-quote string))
+ (replacement (format "[[id:%s][%s]]" node-id string))
+ (pos 0)
+ (parts nil))
+ (while (string-match gnosis-utils--org-link-re text pos)
+ (push (replace-regexp-in-string
+ target replacement
+ (substring text pos (match-beginning 0)) t t)
+ parts)
+ (push (match-string 0 text) parts)
+ (setq pos (match-end 0)))
+ (push (replace-regexp-in-string
+ target replacement (substring text pos) t t)
+ parts)
+ (let ((new-text (apply #'concat (nreverse parts))))
+ (cons (not (string= text new-text)) new-text))))
(provide 'gnosis-utils)
;;; gnosis-utils.el ends here