about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--doc/ranger.pod4
-rw-r--r--ranger/config/rc.conf1
-rw-r--r--ranger/core/filter_stack.py17
3 files changed, 22 insertions, 0 deletions
diff --git a/doc/ranger.pod b/doc/ranger.pod
index 4fac5d50..772107e0 100644
--- a/doc/ranger.pod
+++ b/doc/ranger.pod
@@ -628,6 +628,10 @@ the current directory.  For a more permanent solution, use the command
 
 Apply a new filename filter.
 
+=item .m
+
+Apply a new mimetype filter.
+
 =item .d
 
 Apply the typefilter "directory".
diff --git a/ranger/config/rc.conf b/ranger/config/rc.conf
index 4da1ffc0..a32ab494 100644
--- a/ranger/config/rc.conf
+++ b/ranger/config/rc.conf
@@ -566,6 +566,7 @@ copymap zf zz
 
 # Filter stack
 map .n console filter_stack add name%space
+map .m console filter_stack add mime%space
 map .d filter_stack add type d
 map .f filter_stack add type f
 map .l filter_stack add type l
diff --git a/ranger/core/filter_stack.py b/ranger/core/filter_stack.py
index ff9f4080..2ca2b1c5 100644
--- a/ranger/core/filter_stack.py
+++ b/ranger/core/filter_stack.py
@@ -6,6 +6,7 @@
 from __future__ import (absolute_import, division, print_function)
 
 import re
+import mimetypes
 
 from ranger.container.directory import accept_file, InodeFilterConstants
 
@@ -48,6 +49,22 @@ class NameFilter(BaseFilter):
         return "<Filter: name =~ /{}/>".format(self.pattern)
 
 
+@stack_filter("mime")
+class MimeFilter(BaseFilter):
+    def __init__(self, pattern):
+        self.pattern = pattern
+        self.regex = re.compile(pattern)
+
+    def __call__(self, fobj):
+        mimetype, _ = mimetypes.guess_type(fobj.relative_path)
+        if mimetype is None:
+            return False
+        return self.regex.search(mimetype)
+
+    def __str__(self):
+        return "<Filter: mimetype =~ /{}/>".format(self.pattern)
+
+
 @stack_filter("type")
 class TypeFilter(BaseFilter):
     type_to_function = {