summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2021-07-16 14:42:10 +0200
committerLeah Neukirchen <leah@vuxu.org>2021-07-16 14:42:10 +0200
commit5b29d0622879b6fd6b02c999a8ce34292fa2f978 (patch)
tree34f49a2d8075814ae89b134de3060e0cdd314183
parentae2eed350bb19bdbe965512289df31059cbd263f (diff)
downloadmoar-5b29d0622879b6fd6b02c999a8ce34292fa2f978.tar.gz
moar-5b29d0622879b6fd6b02c999a8ce34292fa2f978.tar.xz
moar-5b29d0622879b6fd6b02c999a8ce34292fa2f978.zip
add inner targets HEAD master
Paragraphs ending with ^target-id will be found by [^target-id].
-rw-r--r--moar.el48
1 files changed, 42 insertions, 6 deletions
diff --git a/moar.el b/moar.el
index bbda0fe..b897297 100644
--- a/moar.el
+++ b/moar.el
@@ -71,12 +71,25 @@
           (narrowed (buffer-narrowed-p)))
       (widen)
       (goto-char (point-min))
-      (if (re-search-forward (concat "\C-l\n[^\n]*" (regexp-quote text))
-                             nil t)
-          (move-beginning-of-line 1)
-        (goto-char old-point)
-        (when (y-or-n-p (concat "Link `" text "' not found, create?"))
-          (moar-add-page text)))
+      (if (string-match "\\^" text)
+          (progn
+            (let ((target (substring text (match-end 0))))
+              (if (re-search-forward (concat "\\(^\\|\\s-\\)\\^"
+                                             (regexp-quote target)
+                                             "$")
+                                     nil t)
+                  (progn
+                    (backward-paragraph)
+                    (forward-char))
+                (message "Link `^%s' not found." target)
+                (goto-char old-point)
+              )))
+        (if (re-search-forward (concat "\C-l\n[^\n]*" (regexp-quote text))
+                               nil t)
+            (move-beginning-of-line 1)
+          (goto-char old-point)
+          (when (y-or-n-p (concat "Link `" text "' not found, create?"))
+            (moar-add-page text))))
       (when narrowed
         (narrow-to-page)))))
 
@@ -191,6 +204,28 @@
       (when target
         (insert "[" target "]")))))
 
+(defun moar-generate-random-inner-target ()
+  (cl-loop repeat 3
+           concat (string (seq-random-elt
+                           "0123456789abcdefghijklmnopqrstuvwxyz"))))
+
+(defun moar-insert-inner-target ()
+  (interactive)
+  (let ((target (moar-generate-random-inner-target)))
+    (save-excursion
+      (save-restriction
+        (widen)
+        (goto-char 1)
+        (while (re-search-forward (concat "\\^" target) nil t)
+          (goto-char 1)
+          (setq target (moar-generate-random-inner-target)))))
+    (save-excursion
+      (forward-paragraph)
+      (backward-char)
+      (insert " ^" target)
+      (kill-new (concat "^" target))
+      (message "Copied link target `^%s'" target))))
+
 (defun moar-visit-link-interactive ()
   (interactive)
   (let* ((links (moar-all-links))
@@ -298,6 +333,7 @@
 (define-key moar-mode-map [mouse-1] 'moar-follow-link-from-mouse)
 
 (define-key moar-mode-map (kbd "C-c [") 'moar-insert-link)
+(define-key moar-mode-map (kbd "C-c ^") 'moar-insert-inner-target)
 (define-key moar-mode-map (kbd "C-c C-v") 'moar-visit-link-interactive)
 (define-key moar-mode-map (kbd "C-c C-b") 'moar-visit-backlink-interactive)
 (define-key moar-mode-map (kbd "C-c C-t") 'moar-visit-today)