]> git.eshelyaron.com Git - dotfiles.git/commitdiff
Also follow and annotate shr links
authorEshel Yaron <eshel@areionsec.com>
Sun, 15 May 2022 10:13:42 +0000 (13:13 +0300)
committerEshel Yaron <eshel@areionsec.com>
Sun, 15 May 2022 10:13:42 +0000 (13:13 +0300)
.emacs.d/esy.org

index 0ed8b26eb2ebc4edbaccda8dd6fc0fd3e0777c7b..d74fc859f8b5fbfad3fd9b90cf28906a3b59facd 100644 (file)
@@ -1306,7 +1306,6 @@ Add the timezones of places of interest to the list of clocks shown by
                           (get-text-property (point)
                                              'completion--string))))
            (pos (cdr (assoc completion esy/preview-table))))
-      (message "%S" completion-extra-properties)
       (with-selected-window esy/preview-window
         (unless (= (goto-char pos) (point))
           (widen)
@@ -1315,14 +1314,28 @@ Add the timezones of places of interest to the list of clocks shown by
         (pulse-momentary-highlight-one-line))))
 
   (defun esy/buttons (&optional buffer)
-    "Return an alist of buttons in BUFFER."
+    "Return an alist of buttons in BUFFER.
+  Buttons following point appear first in the resulting list."
     (let ((buf (or buffer (current-buffer))))
       (with-current-buffer buf
         (let ((button (next-button (point-min))))
           (let ((index 1)
-                (buttons nil))
+                (buttons-before nil)
+                (buttons-after nil))
+            (while (and button (< (button-start button) (point)))
+              (setq buttons-before
+                    (cons `(,(concat (propertize (format "%d " index)
+                                                 'invisible t)
+                                     (truncate-string-to-width
+                                      (esy/inline (button-label button))
+                                      64 nil ?\s t))
+                            .
+                            ,(button-start button))
+                          buttons-before))
+              (setq index (1+ index))
+              (setq button (next-button (button-end button))))
             (while button
-              (setq buttons
+              (setq buttons-after
                     (cons `(,(concat (propertize (format "%d " index)
                                                  'invisible t)
                                      (truncate-string-to-width
@@ -1330,10 +1343,10 @@ Add the timezones of places of interest to the list of clocks shown by
                                       64 nil ?\s t))
                             .
                             ,(button-start button))
-                          buttons))
+                          buttons-after))
               (setq index (1+ index))
               (setq button (next-button (button-end button))))
-            (reverse buttons))))))
+            (append (reverse buttons-after) (reverse buttons-before)))))))
 
   (defun esy/inline (str)
     "Inline STR."
@@ -1346,8 +1359,13 @@ Add the timezones of places of interest to the list of clocks shown by
     (unwind-protect
         (let ((esy/preview-window window)
               (esy/preview-buffer buffer)
-              (esy/preview-table collection))
-          (completing-read prompt collection nil t))
+              (esy/preview-table collection)
+              (completions-sort nil)
+              ;  completion-extra-properties seems to get clobbered?
+              ;; (completion-extra-properties '(:annotate-function
+              ;;                                esy/annotate-button))
+              (completion-annotate-function #'esy/annotate-button))
+          (completing-read prompt collection nil t nil nil (caar collection)))
       (advice-remove #'next-completion #'esy/preview-completion)))
 
   (defun esy/annotate-button (key)
@@ -1356,33 +1374,35 @@ Add the timezones of places of interest to the list of clocks shown by
       (let* ((button (button-at
                       (cdr
                        (assoc key esy/preview-table))))
-             (type (button-type button))
-             (action (button-get button 'action)))
+             (type (or (button-type button)
+                       (button-get button 'action)))
+             (url (button-get button 'shr-url)))
         (esy/inline
          (if type
-             (format "%32S #'%S"
-                     type
-                     action)
-           (when action (format "#'%S" action)))))))
+             (if url
+                 (format "%S %s" type url)
+               (format "%S" type))
+           (when url
+             (format "%s" url)))))))
 
   (defun esy/push-button ()
     "Push a button in the current buffer with comlpetions and preview."
     (interactive)
-    (save-excursion
-      (let* ((buf (current-buffer))
+    (if-let ((buf (current-buffer))
              (win (selected-window))
-             (table (esy/buttons))
-             (completions-sort nil)
-             (completion-annotate-function #'esy/annotate-button))
-        (if table
-            (push-button
-             (cdr (assoc
-                   (esy/completing-read-with-preview "Button: "
-                                                     table
-                                                     win
-                                                     buf)
-                   table)))
-          (user-error "No buttons found in current buffer")))))
+             (table (esy/buttons)))
+        (let* ((choice (save-excursion
+                         (esy/completing-read-with-preview "Button: "
+                                                           table
+                                                           win
+                                                           buf)))
+               (pos (cdr (assoc choice table))))
+          (or (ignore-errors
+                (push-button pos))
+              (progn
+                (goto-char pos)
+                (shr-browse-url))))
+      (user-error "No buttons in current buffer")))
 
 #+end_src