summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lepus.uberspace.de>2017-06-07 22:34:51 +0200
committerhut <hut@lepus.uberspace.de>2017-06-07 22:34:51 +0200
commit1aaed9a08ae859404d293648cb6ff396563ce73f (patch)
tree7ebef732729a93cd3bb91e8e935232968a8a9da2 /ranger
parentf1e44781ea1ff6734d770781201e8cf22ba30ba0 (diff)
parent691a8dcceff440e832a63d0b19a34252da80c8dd (diff)
downloadranger-1aaed9a08ae859404d293648cb6ff396563ce73f.tar.gz
Merge branch 'master' of https://github.com/toonn/ranger
Diffstat (limited to 'ranger')
-rwxr-xr-xranger/config/commands.py60
-rw-r--r--ranger/config/rc.conf8
2 files changed, 64 insertions, 4 deletions
diff --git a/ranger/config/commands.py b/ranger/config/commands.py
index 4cedc4e1..832a33a6 100755
--- a/ranger/config/commands.py
+++ b/ranger/config/commands.py
@@ -1670,3 +1670,63 @@ class linemode(default_linemode):
         # Ask the browsercolumns to redraw
         for col in self.fm.ui.browser.columns:
             col.need_redraw = True
+
+
+class yank(Command):
+    """:yank [name|dir|path]
+
+    Copies the file's name (default), directory or path into both the primary X
+    selection and the clipboard.
+    """
+
+    modes = {
+        '': 'basename',
+        'name': 'basename',
+        'dir': 'dirname',
+        'path': 'path',
+    }
+
+    def execute(self):
+        import subprocess
+
+        def clipboards():
+            from ranger.ext.get_executables import get_executables
+            clipboard_managers = {
+                'xclip': [
+                    ['xclip'],
+                    ['xclip', '-selection', 'clipboard'],
+                ],
+                'xsel': [
+                    ['xsel'],
+                    ['xsel', '-b'],
+                ],
+                'pbcopy': [
+                    ['pbcopy'],
+                ],
+            }
+            ordered_managers = ['pbcopy', 'xclip', 'xsel']
+            executables = get_executables()
+            for manager in ordered_managers:
+                if manager in executables:
+                    return clipboard_managers[manager]
+            return []
+
+        clipboard_commands = clipboards()
+
+        selection = self.get_selection_attr(self.modes[self.arg(1)])
+        new_clipboard_contents = "\n".join(selection)
+        for command in clipboard_commands:
+            process = subprocess.Popen(command, universal_newlines=True,
+                                       stdin=subprocess.PIPE)
+            process.communicate(input=new_clipboard_contents)
+
+    def get_selection_attr(self, attr):
+        return [getattr(item, attr) for item in
+                self.fm.thistab.get_selection()]
+
+    def tab(self, tabnum):
+        return (
+            self.start(1) + mode for mode
+            in sorted(self.modes.keys())
+            if mode
+        )
diff --git a/ranger/config/rc.conf b/ranger/config/rc.conf
index 9af5a953..09fef327 100644
--- a/ranger/config/rc.conf
+++ b/ranger/config/rc.conf
@@ -375,10 +375,10 @@ map g? cd /usr/share/doc/ranger
 map E  edit
 map du shell -p du --max-depth=1 -h --apparent-size
 map dU shell -p du --max-depth=1 -h --apparent-size | sort -rh
-map yp shell -f echo -n %d/%f     | xsel -i && xsel -o | xsel -i -b
-map yd shell -f echo -n %d        | xsel -i && xsel -o | xsel -i -b
-map yn shell -f echo -n %f        | xsel -i && xsel -o | xsel -i -b
-map ys shell -f printf '%%s\n' %s | xsel -i && xsel -o | xsel -i -b
+map yp yank path
+map yd yank dir
+map yn yank name
+map ys yank name
 
 # Filesystem Operations
 map =  chmod