branch: externals/greader
commit 40945abc8f6029d321c42426c9d0844c94dcdfeb
Author: Michelangelo Rodriguez <michelangelo.rodrig...@gmail.com>
Commit: Michelangelo Rodriguez <michelangelo.rodrig...@gmail.com>

    greader.el: Added minor mode "greader-enriched-mode".
    
    This minor mode causes greader to announce clickable elements
    as it reads the text.
    The mode is accompanied by variables and support functions.
---
 greader.el | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/greader.el b/greader.el
index beb7f2d17f..3a1379f2bc 100644
--- a/greader.el
+++ b/greader.el
@@ -1441,5 +1441,55 @@ or you can add the blocks and then start reading."
     (when greader-greader-mode-was-active
       (greader-mode 1)))))
 
+(defcustom greader-enriched-tag "link: "
+  "Message to be spoken when clicking elements are encountered.
+If the value is a string, that string will be used.
+If it is a function, it must return a string."
+  :type '(choice (string :tag "String")
+                 (function :tag "function")))
+
+(defun greader--get-enriched-tag ()
+  "Return the value of `greader-enriched-tag'."
+  (pcase greader-enriched-tag
+    ((pred functionp)
+     (funcall #'greader-enriched-tag))
+    ((pred stringp)
+     greader-enriched-tag)))
+
+(defun greader-scrap-links (input-string)
+  "Modify the INPUT-STRING string to precede each link with 'link: '"
+  (let ((pos 0)
+        (modified nil)
+        (result (copy-sequence input-string)))
+    ;; Search for the location of the next link.
+    (while (setq pos (next-single-property-change pos 'mouse-face result))
+      ;; Check if the location has the 'mouse-face' property set.
+      (when (get-text-property pos 'mouse-face result)
+        (setq modified t)
+       ;; Add 'link:' before the location.
+        (setq result (concat (substring result 0 pos)
+                             (greader--get-enriched-tag)
+                             (substring result pos)))
+       ;; Update the search position taking into account the "link: " string 
we just entered.
+        (setq pos (+ pos 6))))
+    ;; Return the modified string if links were found.
+    (if modified result nil)))
+
+;;;###autoload
+(define-minor-mode greader-enriched-mode
+  "This mode causes greader to announce clickable objects.
+To configure the message with which it must be announced
+the element, configure the `greader-enriched-tag' variable."
+  :global t
+  :lighter "rich"
+  (if greader-enriched-mode
+      (progn
+       (unless greader-mode
+         (greader-mode 1))
+       (add-hook 'greader-after-get-sentence-functions
+                 #'greader-scrap-links))
+    (remove-hook 'greader-after-get-sentence-functions
+                #'greader-scrap-links)))
+
 (provide 'greader)
 ;;; greader.el ends here

Reply via email to