diff --git a/docs/kittens/hyperlinked_grep.rst b/docs/kittens/hyperlinked_grep.rst index dd7428608..8576b6199 100644 --- a/docs/kittens/hyperlinked_grep.rst +++ b/docs/kittens/hyperlinked_grep.rst @@ -14,13 +14,12 @@ following contents: # Open any file with a fragment in vim, fragments are generated # by the hyperlink_grep kitten and nothing else so far. protocol file - has_fragment yes + fragment_matches [0-9]+ action launch --type=overlay vim +${FRAGMENT} ${FILE_PATH} # Open text files without fragments in the editor protocol file mime text/* - has_fragment no action launch --type=overlay ${EDITOR} ${FILE_PATH} diff --git a/docs/open_actions.rst b/docs/open_actions.rst index e79ecfcb4..2479598af 100644 --- a/docs/open_actions.rst +++ b/docs/open_actions.rst @@ -75,8 +75,9 @@ lines. The various available criteria are: ``url`` A regular expression that must match against the entire (unquoted) URL -``has_fragment`` - A boolean (``yes/no``) on whether the URL has a fragment or not. +``fragment_matches`` + A regular expression that must match against the fragment (part after #) in + the URL ``mime`` A comma separated list of MIME types, for example: ``text/*, image/*, diff --git a/kitty/open_actions.py b/kitty/open_actions.py index 21e2afc09..5dea14bcf 100644 --- a/kitty/open_actions.py +++ b/kitty/open_actions.py @@ -12,7 +12,7 @@ from typing import ( ) from urllib.parse import ParseResult, unquote, urlparse -from .conf.utils import to_bool, to_cmdline +from .conf.utils import to_cmdline from .config import KeyAction, parse_key_action from .constants import config_dir from .typing import MatchType @@ -53,7 +53,7 @@ def parse(lines: Iterable[str]) -> Generator[OpenAction, None, None]: x = parse_key_action(rest) if x is not None: actions.append(x) - elif key in ('mime', 'ext', 'protocol', 'file', 'path', 'url', 'has_fragment'): + elif key in ('mime', 'ext', 'protocol', 'file', 'path', 'url', 'fragment_matches'): if key != 'url': rest = rest.lower() match_criteria.append(MatchCriteria(cast(MatchType, key), rest)) @@ -107,8 +107,14 @@ def url_matches_criterion(purl: 'ParseResult', url: str, unquoted_path: str, mc: return True return False - if mc.type == 'has_fragment': - return to_bool(mc.value) == bool(purl.fragment) + if mc.type == 'fragment_matches': + import re + try: + pat = re.compile(mc.value) + except re.error: + return False + + return pat.search(unquote(purl.fragment)) is not None if mc.type == 'path': import fnmatch diff --git a/kitty/typing.pyi b/kitty/typing.pyi index b2133a16f..7417e6f48 100644 --- a/kitty/typing.pyi +++ b/kitty/typing.pyi @@ -43,7 +43,7 @@ from .config import ( # noqa; noqa ) EdgeLiteral = Literal['left', 'top', 'right', 'bottom'] -MatchType = Literal['mime', 'ext', 'protocol', 'file', 'path', 'url', 'has_fragment'] +MatchType = Literal['mime', 'ext', 'protocol', 'file', 'path', 'url', 'fragment_matches'] GRT_a = Literal['t', 'T', 'q', 'p', 'd'] GRT_f = Literal[24, 32, 100] GRT_t = Literal['d', 'f', 't', 's'] diff --git a/kitty_tests/open_actions.py b/kitty_tests/open_actions.py index 234155fce..cb8b5aa17 100644 --- a/kitty_tests/open_actions.py +++ b/kitty_tests/open_actions.py @@ -29,7 +29,7 @@ class TestOpenActions(BaseTest): spec = ''' protocol file mime text/* -has_fragment yes +fragment_matches . AcTion launch $EDITOR $FILE_PATH $FRAGMENT action