From 339a2e080025b188004c9f0a23ab31b990507b6c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 12 Apr 2018 13:49:12 +0530 Subject: [PATCH] Add a word select type to the hints kitten --- CHANGELOG.rst | 5 +++++ kittens/hints/main.py | 36 ++++++++++++++++++++++++++++++------ kitty/kitty.conf | 3 +++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b5250d1d6..05d998e7b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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. diff --git a/kittens/hints/main.py b/kittens/hints/main.py index 29081afc9..e2f5fffa5 100644 --- a/kittens/hints/main.py +++ b/kittens/hints/main.py @@ -2,6 +2,7 @@ # vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2018, Kovid Goyal +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))) diff --git a/kitty/kitty.conf b/kitty/kitty.conf index ebc8f8020..bc2af1da5 100644 --- a/kitty/kitty.conf +++ b/kitty/kitty.conf @@ -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 # }}}