Add a word select type to the hints kitten

This commit is contained in:
Kovid Goyal 2018-04-12 13:49:12 +05:30
parent 98c04fb7da
commit 339a2e0800
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 38 additions and 6 deletions

View File

@ -9,6 +9,11 @@ version 0.9.0 [future]
- A new kitty command shell to allow controlling kitty via commands. Press
ctrl+shift+escape to run the shell.
- The hints kitten has become much more powerful. Now in addition to URLs you
can use it to select word, paths, filenames, lines, etc. from the screen.
These can be inserted into the terminal, copied to clipboard or sent to
external programs.
- Linux: Switch to libxkbcommon for keyboard handling. It allows kitty to
support XCompose and dead keys and also react to keyboard remapping/layout
change without needing a restart.

View File

@ -2,6 +2,7 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
import os
import re
import string
import sys
@ -144,10 +145,10 @@ class Hints(Handler):
self.write(self.current_text)
def regex_finditer(pat, line):
def regex_finditer(pat, minimum_match_length, line):
for m in pat.finditer(line):
s, e = m.span(pat.groups)
if e - s > 2:
if e - s >= minimum_match_length:
yield s, e
@ -183,6 +184,10 @@ def run_loop(args, lines, index_map):
raise SystemExit(loop.return_code)
def escape(chars):
return chars.replace('\\', '\\\\').replace('-', r'\-').replace(']', r'\]')
def run(args, text):
if args.type == 'url':
from .url_regex import url_delimiters
@ -191,11 +196,18 @@ def run(args, text):
)
finditer = partial(find_urls, re.compile(url_pat))
elif args.type == 'path':
finditer = partial(regex_finditer, re.compile(r'(?:\S*/\S+)|(?:\S+[.][a-zA-Z0-9]{2,5})'))
finditer = partial(regex_finditer, re.compile(r'(?:\S*/\S+)|(?:\S+[.][a-zA-Z0-9]{2,5})'), args.minimum_match_length)
elif args.type == 'line':
finditer = partial(regex_finditer, re.compile(r'(?m)^\s*(.+)\s*$'))
finditer = partial(regex_finditer, re.compile(r'(?m)^\s*(.+)\s*$'), args.minimum_match_length)
elif args.type == 'word':
chars = args.word_characters
if chars is None:
import json
chars = json.loads(os.environ['KITTY_COMMON_OPTS'])['select_by_word_characters']
pat = re.compile('(?u)[{}\w]{{{},}}'.format(escape(chars), args.minimum_match_length))
finditer = partial(regex_finditer, pat, args.minimum_match_length)
else:
finditer = partial(regex_finditer, re.compile(args.regex))
finditer = partial(regex_finditer, re.compile(args.regex), args.minimum_match_length)
lines = []
index_map = {}
for line in text.splitlines():
@ -220,7 +232,7 @@ terminal window instead. A value of @ will copy the match to the clipboard.
--type
default=url
choices=url,regex,path,line
choices=url,regex,path,line,word
The type of text to search for.
@ -234,6 +246,18 @@ ignoring a prefix/suffix, as needed. The default expression matches lines.
--url-prefixes
default={0}
Comma separated list of recognized URL prefixes.
--word-characters
Characters to consider as part of a word. In addition, all chacraters marked as
alpha-numeric in the unicode database will be considered as word characters.
Defaults to the select_by_word_characters setting from kitty.conf.
--minimum-match-length
default=3
type=int
The minimum number of characters to consider a match.
'''.format, ','.join(sorted(URL_PREFIXES)))

View File

@ -447,6 +447,9 @@ map ctrl+shift+p>shift+f run_kitten text hints --type path
# output of things like: ls -1
map ctrl+shift+p>l run_kitten text hints --type line --program -
# Select words and insert into terminal.
map ctrl+shift+p>w run_kitten text hints --type word --program -
# The hints kitten has many more modes of operation that you can map to different
# shortcuts. For a full description run: kitty +kitten hints --help
# }}}