(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)
(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
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."
(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)
(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