about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authornfnty <git@nfnty.se>2017-02-01 21:40:42 +0100
committernfnty <git@nfnty.se>2017-02-01 21:44:24 +0100
commitfc9c52848170f2b35ae6c3ac380dd271e8230e69 (patch)
tree7d563392fc5766a21356b929070af819eb3d1d0e
parentdf4c2ca56ff0f1aeb7b696140c8416163170fb1e (diff)
downloadranger-fc9c52848170f2b35ae6c3ac380dd271e8230e69.tar.gz
commands: rename_append: Add flags
Adds ability to position before all extensions.
Adds ability to remove everything except extensions.
Fixes issue when `flat` is used, where it would jump to parent
extension.

Fixes #744
-rw-r--r--doc/ranger.164
-rw-r--r--doc/ranger.pod59
-rwxr-xr-xranger/config/commands.py103
3 files changed, 125 insertions, 101 deletions
diff --git a/doc/ranger.1 b/doc/ranger.1
index 454ed9df..de3caf2e 100644
--- a/doc/ranger.1
+++ b/doc/ranger.1
@@ -910,7 +910,7 @@ ranger.  For your convenience, this is a list of the \*(L"public\*(R" commands i
 \& flat level
 \& grep pattern
 \& help
-\& jump_non [\-r] [\-w]
+\& jump_non [\-FLAGS...]
 \& linemode linemodename
 \& load_copy_buffer
 \& map key command
@@ -925,16 +925,16 @@ ranger.  For your convenience, this is a list of the \*(L"public\*(R" commands i
 \& quit
 \& quit!
 \& relink newpath
-\& rename_append
+\& rename_append [\-FLAGS...]
 \& rename newname
 \& save_copy_buffer
-\& scout [\-FLAGS] pattern
+\& scout [\-FLAGS...] pattern
 \& search pattern
 \& search_inc pattern
 \& set option value
 \& setintag tags option value
 \& setlocal [path=<path>] option value
-\& shell [\-FLAGS] command
+\& shell [\-FLAGS...] command
 \& source filename
 \& terminal
 \& tmap key command
@@ -1076,14 +1076,13 @@ Looks for a string in all marked files or directories.
 .IP "help" 2
 .IX Item "help"
 Provides a quick way to view ranger documentations.
-.IP "jump_non [\fI\-r\fR] [\fI\-w\fR]" 2
-.IX Item "jump_non [-r] [-w]"
+.IP "jump_non [\-\fIflags\fR...]" 2
+.IX Item "jump_non [-flags...]"
 Jumps to first non-directory if highlighted file is a directory and vice versa.
 .Sp
-.Vb 2
-\& \-r    Jump in reverse order
-\& \-w    Wrap around if reaching end of filelist
-.Ve
+Flags:
+ \-r    Jump in reverse order
+ \-w    Wrap around if reaching end of filelist
 .IP "linemode \fIlinemodename\fR" 2
 .IX Item "linemode linemodename"
 Sets the linemode of all files in the current directory.  The linemode may be:
@@ -1167,14 +1166,18 @@ it by typing `` or '' the next time you start ranger.
 .IX Item "relink newpath"
 Change the link destination of the current symlink file to <newpath>. First
 <tab> will load the original link.
-.IP "rename_append" 2
-.IX Item "rename_append"
-Opens the console with \*(L":rename <current file>\*(R" with the cursor automatically
-placed before the file extension
 .IP "rename \fInewname\fR" 2
 .IX Item "rename newname"
 Rename the current file.  If a file with that name already exists, the renaming
 will fail.  Also try the key binding A for appending something to a file name.
+.IP "rename_append [\-\fIflags\fR...]" 2
+.IX Item "rename_append [-flags...]"
+Opens the console with \*(L":rename <current file>\*(R" with the cursor positioned
+before the file extension.
+.Sp
+Flags:
+ \-a    Position before all extensions
+ \-r    Remove everything before extensions
 .IP "save_copy_buffer" 2
 .IX Item "save_copy_buffer"
 Save the copy buffer to \fI~/.config/ranger/copy_buffer\fR.  This can be used to
@@ -1182,25 +1185,22 @@ pass the list of copied files to another ranger instance.
 .IP "scout [\-\fIflags\fR...] [\-\-] \fIpattern\fR" 2
 .IX Item "scout [-flags...] [--] pattern"
 Swiss army knife command for searching, traveling and filtering files.
-The command takes various flags as arguments which can be used to
-influence its behaviour:
 .Sp
-.Vb 10
-\& \-a = automatically open a file on unambiguous match
-\& \-e = open the selected file when pressing enter
-\& \-f = filter files that match the current search pattern
-\& \-g = interpret pattern as a glob pattern
-\& \-i = ignore the letter case of the files
-\& \-k = keep the console open when changing a directory with the command
-\& \-l = letter skipping; e.g. allow "rdme" to match the file "readme"
-\& \-m = mark the matching files after pressing enter
-\& \-M = unmark the matching files after pressing enter
-\& \-p = permanent filter: hide non\-matching files after pressing enter
-\& \-r = interpret pattern as a regular expression pattern
-\& \-s = smart case; like \-i unless pattern contains upper case letters
-\& \-t = apply filter and search pattern as you type
-\& \-v = inverts the match
-.Ve
+Flags:
+ \-a    Automatically open a file on unambiguous match
+ \-e    Open the selected file when pressing enter
+ \-f    Filter files that match the current search pattern
+ \-g    Interpret pattern as a glob pattern
+ \-i    Ignore the letter case of the files
+ \-k    Keep the console open when changing a directory with the command
+ \-l    Letter skipping; e.g. allow \*(L"rdme\*(R" to match the file \*(L"readme\*(R"
+ \-m    Mark the matching files after pressing enter
+ \-M    Unmark the matching files after pressing enter
+ \-p    Permanent filter: hide non-matching files after pressing enter
+ \-r    Interpret pattern as a regular expression pattern
+ \-s    Smart case; like \-i unless pattern contains upper case letters
+ \-t    Apply filter and search pattern as you type
+ \-v    Inverts the match
 .Sp
 Multiple flags can be combined.  For example, \*(L":scout \-gpt\*(R" would create
 a :filter\-like command using globbing.
diff --git a/doc/ranger.pod b/doc/ranger.pod
index a61ae657..46976b4a 100644
--- a/doc/ranger.pod
+++ b/doc/ranger.pod
@@ -928,7 +928,7 @@ ranger.  For your convenience, this is a list of the "public" commands including
  flat level
  grep pattern
  help
- jump_non [-r] [-w]
+ jump_non [-FLAGS...]
  linemode linemodename
  load_copy_buffer
  map key command
@@ -943,16 +943,16 @@ ranger.  For your convenience, this is a list of the "public" commands including
  quit
  quit!
  relink newpath
- rename_append
+ rename_append [-FLAGS...]
  rename newname
  save_copy_buffer
- scout [-FLAGS] pattern
+ scout [-FLAGS...] pattern
  search pattern
  search_inc pattern
  set option value
  setintag tags option value
  setlocal [path=<path>] option value
- shell [-FLAGS] command
+ shell [-FLAGS...] command
  source filename
  terminal
  tmap key command
@@ -1119,14 +1119,14 @@ Looks for a string in all marked files or directories.
 
 Provides a quick way to view ranger documentations.
 
-=item jump_non [I<-r>] [I<-w>]
+=item jump_non [-I<flags>...]
 
 Jumps to first non-directory if highlighted file is a directory and vice versa.
 
+Flags:
  -r    Jump in reverse order
  -w    Wrap around if reaching end of filelist
 
-
 =item linemode I<linemodename>
 
 Sets the linemode of all files in the current directory.  The linemode may be:
@@ -1220,16 +1220,20 @@ it by typing `` or '' the next time you start ranger.
 Change the link destination of the current symlink file to <newpath>. First
 <tab> will load the original link.
 
-=item rename_append
-
-Opens the console with ":rename <current file>" with the cursor automatically
-placed before the file extension
-
 =item rename I<newname>
 
 Rename the current file.  If a file with that name already exists, the renaming
 will fail.  Also try the key binding A for appending something to a file name.
 
+=item rename_append [-I<flags>...]
+
+Opens the console with ":rename <current file>" with the cursor positioned
+before the file extension.
+
+Flags:
+ -a    Position before all extensions
+ -r    Remove everything before extensions
+
 =item save_copy_buffer
 
 Save the copy buffer to I<~/.config/ranger/copy_buffer>.  This can be used to
@@ -1238,23 +1242,22 @@ pass the list of copied files to another ranger instance.
 =item scout [-I<flags>...] [--] I<pattern>
 
 Swiss army knife command for searching, traveling and filtering files.
-The command takes various flags as arguments which can be used to
-influence its behaviour:
-
- -a = automatically open a file on unambiguous match
- -e = open the selected file when pressing enter
- -f = filter files that match the current search pattern
- -g = interpret pattern as a glob pattern
- -i = ignore the letter case of the files
- -k = keep the console open when changing a directory with the command
- -l = letter skipping; e.g. allow "rdme" to match the file "readme"
- -m = mark the matching files after pressing enter
- -M = unmark the matching files after pressing enter
- -p = permanent filter: hide non-matching files after pressing enter
- -r = interpret pattern as a regular expression pattern
- -s = smart case; like -i unless pattern contains upper case letters
- -t = apply filter and search pattern as you type
- -v = inverts the match
+
+Flags:
+ -a    Automatically open a file on unambiguous match
+ -e    Open the selected file when pressing enter
+ -f    Filter files that match the current search pattern
+ -g    Interpret pattern as a glob pattern
+ -i    Ignore the letter case of the files
+ -k    Keep the console open when changing a directory with the command
+ -l    Letter skipping; e.g. allow "rdme" to match the file "readme"
+ -m    Mark the matching files after pressing enter
+ -M    Unmark the matching files after pressing enter
+ -p    Permanent filter: hide non-matching files after pressing enter
+ -r    Interpret pattern as a regular expression pattern
+ -s    Smart case; like -i unless pattern contains upper case letters
+ -t    Apply filter and search pattern as you type
+ -v    Inverts the match
 
 Multiple flags can be combined.  For example, ":scout -gpt" would create
 a :filter-like command using globbing.
diff --git a/ranger/config/commands.py b/ranger/config/commands.py
index 2db6afa5..f132e444 100755
--- a/ranger/config/commands.py
+++ b/ranger/config/commands.py
@@ -573,32 +573,31 @@ class delete(Command):
 
 
 class jump_non(Command):
-    """:jump_non [-r] [-w]
+    """:jump_non [-FLAGS...]
 
     Jumps to first non-directory if highlighted file is a directory and vice versa.
 
-    Options:
-        `-r` Jump in reverse order
-        `-w` Wrap around if reaching end of filelist
+    Flags:
+     -r    Jump in reverse order
+     -w    Wrap around if reaching end of filelist
     """
+    def __init__(self, *args, **kwargs):
+        super(jump_non, self).__init__(*args, **kwargs)
+
+        flags, _ = self.parse_flags()
+        self._flag_reverse = 'r' in flags
+        self._flag_wrap = 'w' in flags
+
     @staticmethod
     def _non(fobj, is_directory):
         return fobj.is_directory if not is_directory else not fobj.is_directory
 
     def execute(self):
-        reverse = False
-        wrap = False
-        for arg in self.args:
-            if arg == '-r':
-                reverse = True
-            elif arg == '-w':
-                wrap = True
-
         tfile = self.fm.thisfile
         passed = False
         found_before = None
         found_after = None
-        for fobj in self.fm.thisdir.files[::-1] if reverse else self.fm.thisdir.files:
+        for fobj in self.fm.thisdir.files[::-1] if self._flag_reverse else self.fm.thisdir.files:
             if fobj.path == tfile.path:
                 passed = True
                 continue
@@ -612,7 +611,7 @@ class jump_non(Command):
 
         if found_after:
             self.fm.select_file(found_after)
-        elif wrap and found_before:
+        elif self._flag_wrap and found_before:
             self.fm.select_file(found_before)
 
 
@@ -835,19 +834,42 @@ class rename(Command):
 
 
 class rename_append(Command):
-    """:rename_append
+    """:rename_append [-FLAGS...]
 
-    Creates an open_console for the rename command, automatically placing
-    the cursor before the file extension.
+    Opens the console with ":rename <current file>" with the cursor positioned
+    before the file extension.
+
+    Flags:
+     -a    Position before all extensions
+     -r    Remove everything before extensions
     """
+    def __init__(self, *args, **kwargs):
+        super(rename_append, self).__init__(*args, **kwargs)
+
+        flags, _ = self.parse_flags()
+        self._flag_ext_all = 'a' in flags
+        self._flag_remove = 'r' in flags
 
     def execute(self):
         tfile = self.fm.thisfile
-        path = tfile.relative_path.replace("%", "%%")
-        if path.find('.') != 0 and path.rfind('.') != -1 and not tfile.is_directory:
-            self.fm.open_console('rename ' + path, position=(7 + path.rfind('.')))
+        relpath = tfile.relative_path.replace('%', '%%')
+        basename = tfile.basename.replace('%', '%%')
+
+        if basename.find('.') <= 0:
+            self.fm.open_console('rename ' + relpath)
+            return
+
+        if self._flag_ext_all:
+            pos_ext = re.search(r'[^.]+', basename).end(0)
         else:
-            self.fm.open_console('rename ' + path)
+            pos_ext = basename.rindex('.')
+        pos = len(relpath) - len(basename) + pos_ext
+
+        if self._flag_remove:
+            relpath = relpath[:-len(basename)] + basename[pos_ext:]
+            pos -= pos_ext
+
+        self.fm.open_console('rename ' + relpath, position=(7 + pos))
 
 
 class chmod(Command):
@@ -1152,26 +1174,25 @@ class pmap(map_):
 
 
 class scout(Command):
-    """:scout [-FLAGS] <pattern>
+    """:scout [-FLAGS...] <pattern>
 
     Swiss army knife command for searching, traveling and filtering files.
-    The command takes various flags as arguments which can be used to
-    influence its behaviour:
-
-    -a = automatically open a file on unambiguous match
-    -e = open the selected file when pressing enter
-    -f = filter files that match the current search pattern
-    -g = interpret pattern as a glob pattern
-    -i = ignore the letter case of the files
-    -k = keep the console open when changing a directory with the command
-    -l = letter skipping; e.g. allow "rdme" to match the file "readme"
-    -m = mark the matching files after pressing enter
-    -M = unmark the matching files after pressing enter
-    -p = permanent filter: hide non-matching files after pressing enter
-    -r = interpret pattern as a regular expression pattern
-    -s = smart case; like -i unless pattern contains upper case letters
-    -t = apply filter and search pattern as you type
-    -v = inverts the match
+
+    Flags:
+     -a    Automatically open a file on unambiguous match
+     -e    Open the selected file when pressing enter
+     -f    Filter files that match the current search pattern
+     -g    Interpret pattern as a glob pattern
+     -i    Ignore the letter case of the files
+     -k    Keep the console open when changing a directory with the command
+     -l    Letter skipping; e.g. allow "rdme" to match the file "readme"
+     -m    Mark the matching files after pressing enter
+     -M    Unmark the matching files after pressing enter
+     -p    Permanent filter: hide non-matching files after pressing enter
+     -r    Interpret pattern as a regular expression pattern
+     -s    Smart case; like -i unless pattern contains upper case letters
+     -t    Apply filter and search pattern as you type
+     -v    Inverts the match
 
     Multiple flags can be combined.  For example, ":scout -gpt" would create
     a :filter-like command using globbing.
@@ -1193,8 +1214,8 @@ class scout(Command):
     INVERT        = 'v'
     # pylint: enable=bad-whitespace
 
-    def __init__(self, *args, **kws):
-        Command.__init__(self, *args, **kws)
+    def __init__(self, *args, **kwargs):
+        super(scout, self).__init__(*args, **kwargs)
         self._regex = None
         self.flags, self.pattern = self.parse_flags()