branch: elpa/adoc-mode
commit 7085ee90524fdea7861ac3248c37097a3fd1a1c8
Author: Bozhidar Batsov <[email protected]>
Commit: Bozhidar Batsov <[email protected]>

    [Fix #29] Add adoc-follow-thing-at-point
    
    Add an interactive command that follows the thing at point:
    - URLs: opens in browser via `browse-url`
    - `include::` macros: opens the referenced file
    - xrefs/cross-references: jumps to the anchor
    
    Bound to `C-c C-o` and `M-.`, and available from the AsciiDoc menu.
---
 CHANGELOG.md |  1 +
 adoc-mode.el | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1b0e6171fb..80df3a1986 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@
 - [#48](https://github.com/bbatsov/adoc-mode/pull/48): Add support for 
displaying images.
 - Add font-lock support for Asciidoctor inline macros: `kbd:[]`, `btn:[]`, 
`menu:[]`, `pass:[]`, `stem:[]`, `latexmath:[]`, `asciimath:[]`.
 - [#59](https://github.com/bbatsov/adoc-mode/issues/59): Add nested `imenu` 
index support (enabled by default via `adoc-imenu-create-index-function`).
+- [#29](https://github.com/bbatsov/adoc-mode/issues/29): Add 
`adoc-follow-thing-at-point` to follow URLs, `include::` macros, and xrefs 
(bound to `C-c C-o` and `M-.`).
 
 ### Changes
 
diff --git a/adoc-mode.el b/adoc-mode.el
index 60ceb0a673..25c1de1285 100644
--- a/adoc-mode.el
+++ b/adoc-mode.el
@@ -2678,6 +2678,30 @@ for multiline constructs to be matched."
     (push-mark)
     (goto-char pos)))
 
+(defun adoc-follow-thing-at-point ()
+  "Follow the link or reference at point.
+When point is on a URL, open it in a browser.
+When point is on an `include::' macro, open the referenced file.
+When point is on an xref or cross-reference, jump to its anchor."
+  (interactive)
+  (cond
+   ;; include:: macro — open the file
+   ((save-excursion
+      (beginning-of-line)
+      (looking-at "include1?::\\([^ \t\n\\[]+\\)"))
+    (let ((file (match-string-no-properties 1)))
+      (if (file-exists-p file)
+          (find-file file)
+        (user-error "File not found: %s" file))))
+   ;; xref at point — jump to anchor
+   ((adoc-xref-id-at-point)
+    (adoc-goto-ref-label (adoc-xref-id-at-point)))
+   ;; URL at point — open in browser
+   ((thing-at-point 'url)
+    (browse-url (thing-at-point 'url t)))
+   (t
+    (user-error "Nothing to follow at point"))))
+
 (defun adoc-promote (&optional arg)
   "Promotes the structure at point ARG levels.
 
@@ -3628,12 +3652,15 @@ ITEMS is a list of (name pos . level)."
     (define-key map "\C-c\C-p" 'adoc-promote)
     (define-key map "\C-c\C-t" 'adoc-toggle-title-type)
     (define-key map "\C-c\C-a" 'adoc-goto-ref-label)
+    (define-key map "\C-c\C-o" 'adoc-follow-thing-at-point)
+    (define-key map (kbd "M-.") 'adoc-follow-thing-at-point)
     (easy-menu-define adoc-mode-menu map "Menu for adoc mode"
       `("AsciiDoc"
         ["Promote" adoc-promote]
         ["Demote" adoc-demote]
         ["Toggle title type" adoc-toggle-title-type]
         ["Adjust title underline" adoc-adjust-title-del]
+        ["Follow thing at point" adoc-follow-thing-at-point]
         ["Goto anchor" adoc-goto-ref-label]
         "---"
         ;; names|wording / rough order/ help texts are from asciidoc manual

Reply via email to