-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcustom-hints.py
executable file
·58 lines (48 loc) · 2.42 KB
/
custom-hints.py
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
#!/usr/bin/env python3
"""
kitty -o 'map f1 kitten hints --customize-processing custom-hints.py'
When you press the F1 key you will be able to select a word to look it up in the Google dictionary.
https://sw.kovidgoyal.net/kitty/kittens/hints/#completely-customizing-the-matching-and-actions-of-the-kitten
ref https://github.com/kovidgoyal/kitty/blob/master/kitty/boss.py
ref https://github.com/kovidgoyal/kitty/blob/master/kittens/hints/main.py
"""
import re
from kitty.clipboard import (
set_primary_selection,
set_clipboard_string
)
RE_PATH = (
r'(?=[ \t\n]|"|\(|\[|<|\')?'
'(~/|/)?'
'([-a-zA-Z0-9_+-,.]+/[^ \t\n\r|:"\'$%&)>\]]*)'
)
RE_URL = (r"(https?://|git@|git://|ssh://|s*ftp://|file:///)"
"[a-zA-Z0-9?=%/_.:,;~@!#$&()*+-]*")
RE_COMMON_FILENAME = r'\s?([a-zA-Z0-9_.-/]*[a-zA-Z0-9_.-]+\.(ini|yml|yaml|vim|toml|conf|lua|go|php|rs|py|js|vue|jsx|html|htm|md|mp3|wav|flac|mp4|mkv|dll|exe|sh|txt|log|gz|tar|rar|7z|zip|mod|sum|iso|patch))\s?'
RE_URL_OR_PATH = RE_COMMON_FILENAME + "|" + RE_PATH + "|" + RE_URL
def mark(text, args, Mark, extra_cli_args, *a):
# This function is responsible for finding all
# matching text. extra_cli_args are any extra arguments
# passed on the command line when invoking the kitten.
# We mark all individual word for potential selection
for idx, m in enumerate(re.finditer(RE_URL_OR_PATH, text)):
start, end = m.span()
mark_text = text[start:end].replace('\n', '').replace('\0', '').strip()
# The empty dictionary below will be available as groupdicts
# in handle_result() and can contain arbitrary data.
yield Mark(idx, start, end, mark_text, {})
def handle_result(args, data, target_window_id, boss, extra_cli_args, *a):
# This function is responsible for performing some
# action on the selected text.
# matches is a list of the selected entries and groupdicts contains
# the arbitrary data associated with each entry in mark() above
matches, groupdicts = [], []
for m, g in zip(data['match'], data['groupdicts']):
if m:
matches.append(m), groupdicts.append(g)
for word, match_data in zip(matches, groupdicts):
# Lookup the word in a dictionary, the open_url function
# will open the provided url in the system browser
# boss.open_url(f'https://www.google.com/search?q=define:{word}')
# set_primary_selection(word)
set_clipboard_string(word)