-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
pen-right-click-menu.el
128 lines (114 loc) · 5.02 KB
/
pen-right-click-menu.el
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
(require 'popup)
(require 'right-click-context)
;; Configure the right click menu to have more height
(defset popup-max-height 30)
(cl-defun popup-menu* (list
&key
point
(around t)
(width (popup-preferred-width list))
(height popup-max-height)
max-width
margin
margin-left
margin-right
scroll-bar
symbol
parent
parent-offset
cursor
(keymap popup-menu-keymap)
(fallback 'popup-menu-fallback)
help-delay
nowait
prompt
isearch
(isearch-filter 'popup-isearch-filter-list)
(isearch-cursor-color popup-isearch-cursor-color)
(isearch-keymap popup-isearch-keymap)
isearch-callback
initial-index
&aux menu event)
"Show a popup menu of LIST at POINT. This function returns a
value of the selected item. Almost all arguments are the same as in
`popup-create', except for KEYMAP, FALLBACK, HELP-DELAY, PROMPT,
ISEARCH, ISEARCH-FILTER, ISEARCH-CURSOR-COLOR, ISEARCH-KEYMAP, and
ISEARCH-CALLBACK.
If KEYMAP is a keymap which is used when processing events during
event loop.
If FALLBACK is a function taking two arguments; a key and a
command. FALLBACK is called when no special operation is found on
the key. The default value is `popup-menu-fallback', which does
nothing.
HELP-DELAY is a delay of displaying helps.
If NOWAIT is non-nil, this function immediately returns the menu
instance without entering event loop.
PROMPT is a prompt string when reading events during event loop.
If ISEARCH is non-nil, do isearch as soon as displaying the popup
menu.
ISEARCH-FILTER is a filtering function taking two arguments:
search pattern and list of items. Returns a list of matching items.
ISEARCH-CURSOR-COLOR is a cursor color during isearch. The
default value is `popup-isearch-cursor-color'.
ISEARCH-KEYMAP is a keymap which is used when processing events
during event loop. The default value is `popup-isearch-keymap'.
ISEARCH-CALLBACK is a function taking one argument. `popup-menu'
calls ISEARCH-CALLBACK, if specified, after isearch finished or
isearch canceled. The arguments is whole filtered list of items.
If `INITIAL-INDEX' is non-nil, this is an initial index value for
`popup-select'. Only positive integer is valid."
(and (eq margin t) (setq margin 1))
(or margin-left (setq margin-left margin))
(or margin-right (setq margin-right margin))
(if (and scroll-bar
(integerp margin-right)
(> margin-right 0))
;; Make scroll-bar space as margin-right
(cl-decf margin-right))
(setq menu (popup-create point width height
:max-width max-width
:around around
:face 'popup-menu-face
:mouse-face 'popup-menu-mouse-face
:selection-face 'popup-menu-selection-face
:summary-face 'popup-menu-summary-face
:margin-left margin-left
:margin-right margin-right
:scroll-bar scroll-bar
:symbol symbol
:parent parent
:parent-offset parent-offset))
(unwind-protect
(progn
(popup-set-list menu list)
(if cursor
(popup-jump menu cursor)
(popup-draw menu))
(when initial-index
(dotimes (_i (min (- (length list) 1) initial-index))
(popup-next menu)))
(if nowait
menu
(popup-menu-event-loop menu keymap fallback
:prompt prompt
:help-delay help-delay
:isearch isearch
:isearch-filter isearch-filter
:isearch-cursor-color isearch-cursor-color
:isearch-keymap isearch-keymap
:isearch-callback isearch-callback)))
(unless nowait
(popup-delete menu))))
(defun gpt-test-haskell ()
(let ((lang
;; (pen-pf-test-if-text-is-haskell (selection))
;; Using
(detect-language (selection))))
(message (concat "Language:" lang))
(istr-match-p "Haskell" (message lang))))
;; Here is an example of a GPT-3 right click menu
(setq right-click-context-global-menu-tree
`(("Cancel" :call identity)
("GPT-3: Convert Haskell to Clojure" :call pen-pf-translate-haskell-to-clojure :if (gpt-test-haskell))
("pen (code)"
("asktutor" :call pen-tutor-mode-assist :if (major-mode-p 'prog-mode)))))