From 0ae55822c4f353438d548a4ee63185a59702ccd2 Mon Sep 17 00:00:00 2001 From: toonn Date: Thu, 23 Aug 2018 13:54:49 +0200 Subject: Add the %any_path macro The `%any` macro is very useful for commands that know about bookmarks. Bookmarks are just short names for paths really, so it'd be nice if they could be used with any commands that take paths as arguments. Now they can! `%any_pathX` where `X` refers to the which `` the macro corresponds to (`%any_path` is synonymous with `%any_path0`) is replaced with the path of the bookmark entered if that bookmark exists. Fixes #1277 --- ranger/core/actions.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ranger/core/actions.py b/ranger/core/actions.py index 83cfcc08..4ee69504 100644 --- a/ranger/core/actions.py +++ b/ranger/core/actions.py @@ -240,10 +240,23 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m cmd = cmd_class(string, quantifier=quantifier) if cmd.resolve_macros and _MacroTemplate.delimiter in cmd.line: - macros = dict(('any%d' % i, key_to_string(char)) - for i, char in enumerate(wildcards if wildcards is not None else [])) + f1 = lambda i, c: ('any{:d}'.format(i), key_to_string(c)) + def f2(i, c): + try: + val = self.fm.bookmarks[key_to_string(c)] + except KeyError: + self.notify('No bookmark defined for `{}`'.format( + key_to_string(c)), bad=True) + val = MACRO_FAIL + return ('any_path{:d}'.format(i), val) + + macros = dict(f(i, char) for f in (f1, f2) for i, char + in enumerate(wildcards if wildcards + is not None else [])) if 'any0' in macros: macros['any'] = macros['any0'] + if 'any_path0' in macros: + macros['any_path'] = macros['any_path0'] try: line = self.substitute_macros(cmd.line, additional=macros, escape=cmd.escape_macros_for_shell) -- cgit 1.4.1-2-gfad0 From 07cc58396035d1c25871632d8ee14cad08dbe69a Mon Sep 17 00:00:00 2001 From: toonn Date: Thu, 23 Aug 2018 21:43:21 +0200 Subject: Fixed up pylint/flake8 warnings --- ranger/core/actions.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ranger/core/actions.py b/ranger/core/actions.py index 4ee69504..73c9fa51 100644 --- a/ranger/core/actions.py +++ b/ranger/core/actions.py @@ -240,19 +240,21 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m cmd = cmd_class(string, quantifier=quantifier) if cmd.resolve_macros and _MacroTemplate.delimiter in cmd.line: - f1 = lambda i, c: ('any{:d}'.format(i), key_to_string(c)) - def f2(i, c): + def any_macro(i, char): + return ('any{:d}'.format(i), key_to_string(char)) + + def anypath_macro(i, char): try: - val = self.fm.bookmarks[key_to_string(c)] + val = self.fm.bookmarks[key_to_string(char)] except KeyError: self.notify('No bookmark defined for `{}`'.format( - key_to_string(c)), bad=True) + key_to_string(char)), bad=True) val = MACRO_FAIL return ('any_path{:d}'.format(i), val) - macros = dict(f(i, char) for f in (f1, f2) for i, char - in enumerate(wildcards if wildcards - is not None else [])) + macros = dict(f(i, char) for f in (any_macro, anypath_macro) + for i, char in enumerate(wildcards if wildcards + is not None else [])) if 'any0' in macros: macros['any'] = macros['any0'] if 'any_path0' in macros: -- cgit 1.4.1-2-gfad0