about summary refs log tree commit diff stats
path: root/lisp/+link-hint.el
blob: 469ed15619fcbb6e6356fa4d69708961fa66ea8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;;; +link-hint.el -*- lexical-binding: t; -*-

;;; Code:

(require 'cl-lib)
(require 'link-hint)

(defgroup +link-hint nil
  "Extra customizations for `link-hint'."
  :group 'link-hint)

(defcustom +link-hint-open-secondary-types '(gnus-w3m-image-url
                                             gnus-w3m-url
                                             markdown-link
                                             mu4e-attachment
                                             mu4e-url
                                             notmuch-hello
                                             nov-link
                                             org-link
                                             shr-url
                                             text-url
                                             w3m-link
                                             w3m-message-link)
  "Link types to define `:open-secondary' for.")

(defvar +link-hint-map (make-sparse-keymap)
  "Keymap for `link-hint' functionality.")

(cl-defmacro +link-hint-define-keyword (keyword handler docstring
                                                &optional (types 'link-hint-types)
                                                &rest rest
                                                &key multiple &allow-other-keys)
  "Set up a `link-hint' KEYWORD, with optional TYPES.
If TYPES is not present, use `link-hint-types'.

KEYWORD defines the link-hint type.  It will be used to create a
function for opening links of the form \"link-hint-openKEYWORD\".

HANDLER is the function to open a link with.

DOCSTRING is the macro's documentation.

Keyword arguments are passed to `link-hint-define-type' prefixed
with the KEYWORD."
  (declare (indent 2)
           (doc-string 3))
  (let ((types (symbol-value types))
        (func-sym (intern (format "+link-hint-open%s" keyword)))
        (mult-sym (intern (format "%s-multiple" keyword)))
        (expr))
    ;; Define the type
    (push `(dolist (type ',types)
             (link-hint-define-type type
               ,keyword ,handler
               ,@(mapcar (lambda (el)
                           (if (eq el :multiple)
                               mult-sym
                             el))
                         rest)))
          expr)
    ;; Define an opener
    (push `(defun ,func-sym ()
             ,(format "%s\n\nDefined by `+link-hint-define'." docstring)
             (interactive)
             (avy-with link-hint-open-link
               (link-hint--one ,keyword)))
          expr)
    ;; Handle `:multiple'
    (when multiple
      (push `(defun ,(intern (format "+link-hint-open-multiple%s" keyword)) ()
               ,(format "Open multiple links with `%s'.\n\nDefined by `+link-hint-define'."
                        func-sym)
               (avy-with link-hint-open-multiple-links
                 (link-hint--multiple ,keyword)))
            expr)
      (push `(defun ,(intern (format "+link-hint-open-all%s" keyword)) ()
               ,(format "Open all visible links with `%s'.\n\nDefined by `+link-hint-define'."
                        func-sym)
               (avy-with link-hint-open-all-links
                 (link-hint--all ,keyword)))
            expr))
    ;; Return the built expression
    `(progn ,@(nreverse expr))))

(+link-hint-define-keyword :secondary browse-url-secondary-browser-function
  "Open a link in the secondary browser."
  +link-hint-open-secondary-types
  :multiple t)

(defun +link-hint-open-secondary-setup (&optional types)
  "Define the `:open-secondary' link-hint type for TYPES.
If TYPES is nil, define it for `+link-hint-open-secondary-types'."
  (dolist (type (or types +link-hint-open-secondary-types))
    (link-hint-define-type type
      :open-secondary browse-url-secondary-browser-function
      :open-secondary-multiple t)))

(defun +link-hint-open-secondary ()
  "Open a link in the secondary browser."
  (interactive)
  (avy-with link-hint-open-link
    (link-hint--one :open-secondary)))

(defun +link-hint-open-chrome-setup (&optional types)
  "Define the `:open-chrome' link-hint type for TYPES.
If TYPES is nil, define it for `+link-hint-open-secondary-types'."
  (dolist (type (or types +link-hint-open-secondary-types))
    (link-hint-define-type type
      :open-chrome #'browse-url-chrome
      :open-chrome-multiple t)))

(defun +link-hint-open-chrome ()
  "Open a link with chrome."
  (interactive)
  (avy-with link-hint-open-link
    (link-hint--one :open-chrome)))

(defun +link-hint-open-link (prefix)
  "Open a link.
Without a PREFIX, open using `browse-url-browser-function'; with
a PREFIX, use `browse-url-secondary-browser-function'."
  (interactive "P")
  (let ((current-prefix-arg nil))
    (avy-with link-hint-open-link
      (link-hint--one (if prefix :open-secondary :open)))))

;; test: https://www.acdw.net

(defun +link-hint-open-multiple-links (prefix)
  "Open multiple links.
Without a PREFIX, open using `browse-url-browser-function'; with
a PREFIX, use `browse-url-secondary-browser-function'."
  (interactive "P")
  (avy-with link-hint-open-multiple-links
    (link-hint--one (if prefix :open-secondary :open))))

(defun +link-hint-open-all-links (prefix)
  "Open all visible links.
Without a PREFIX, open using `browse-url-browser-function'; with
a PREFIX, use `browse-url-secondary-browser-function'."
  (interactive "P")
  (avy-with link-hint-open-all-links
    (link-hint--one (if prefix :open-secondary :open))))

(provide '+link-hint)
;;; +link-hint.el ends here