From d9938427c79b5e60e3c0c3bc750357e9960dab2e Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Tue, 5 Mar 2013 18:47:24 +0100 Subject: [PATCH] Ranger: sxiv multiple files support. dwb: leo searchengine. --- .config/dwb/searchengines | 2 +- .config/ranger/rifle.conf | 2 + .config/ranger/scope.sh | 2 +- .scripts/rifle_sxiv.py | 102 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100755 .scripts/rifle_sxiv.py diff --git a/.config/dwb/searchengines b/.config/dwb/searchengines index e60c832c..cc251536 100644 --- a/.config/dwb/searchengines +++ b/.config/dwb/searchengines @@ -5,6 +5,7 @@ ctan http://www.ctan.org/search?phrase=%s gm https://maps.google.com/maps?q=%s gt http://translate.google.com/#en/fr/%s imdb http://www.imdb.com/find?q=%s&s=all +leo http://dict.leo.org/frde/index_de.html#/search=%s torrent http://torrentz.eu/search?f=%s w http://en.wikipedia.org/wiki/Special:Search?search=%s wb http://en.wikibooks.org/wiki/Special:Search?search=%s @@ -14,4 +15,3 @@ wk http://en.wiktionary.org/wiki/Special:Search?search=%s wkf http://fr.wiktionary.org/wiki/Special:Search?search=%s wr http://www.wordreference.com/enfr/%s yt http://www.youtube.com/results?search_query=%s - diff --git a/.config/ranger/rifle.conf b/.config/ranger/rifle.conf index 2cf7473f..9d2aa289 100644 --- a/.config/ranger/rifle.conf +++ b/.config/ranger/rifle.conf @@ -132,6 +132,8 @@ mime ^video, terminal, !X, has mplayer = mplayer -- "$@" #------------------------------------------- # Image Viewing: #------------------------------------------- +mime ^image, has sxiv, X, flag f = rifle_sxiv.py -fs -- "$@" +ext png|bmp|gif|jpe?g, X, flag f = feh -FZx *.bmp *.jpg *.png *.gif *.jpeg --start-at "$(basename "$1")" mime ^image, has eog, X, flag f = eog -- "$@" mime ^image, has sxiv, X, flag f = sxiv -- "$@" mime ^image, has feh, X, flag f = feh -- "$@" diff --git a/.config/ranger/scope.sh b/.config/ranger/scope.sh index baaaba3b..dad9bdfa 100755 --- a/.config/ranger/scope.sh +++ b/.config/ranger/scope.sh @@ -44,7 +44,7 @@ highlight() { command highlight "$@"; test $? = 0 -o $? = 141; } case "$extension" in # Archive extensions: - 7z|a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\ + a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\ rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip) try als "$path" && { dump | trim; exit 0; } try acat "$path" && { dump | trim; exit 3; } diff --git a/.scripts/rifle_sxiv.py b/.scripts/rifle_sxiv.py new file mode 100755 index 00000000..a28a840b --- /dev/null +++ b/.scripts/rifle_sxiv.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python +# Compatible with ranger 1.6.* +# +# If only one file is selected, this script searches image files in a directory, +# opens them all with sxiv and sets the first argument to the first image +# displayed by sxiv. +# +# If a selection is on, this script will start sxiv over the selection only. +# +# This is supposed to be used in rifle.conf as a workaround for the fact that +# sxiv takes no file name arguments for the first image, just the number. Copy +# this file somewhere into your $PATH and add this at the top of rifle.conf: +# +# mime ^image, has sxiv, X, flag f = path/to/this/script -- "$@" +# +# This wrapper supports parameter, so if you want to start fullscreen and to fit +# all image to window, use +# +# mime ^image, has sxiv, X, flag f = path/to/this/script -fs -- "$@" + +# TODO: support for mimetypes. +# result = [a for a in filelist if v=mimetypes.guess_type(a)[0] and type(v) is str and v.find('image') != -1 ] + +import sys +import os +import subprocess +import mimetypes +import re + +def usage(): + print("Usage: " + re.sub(r".*/", "", sys.argv[0]) + " PICTURES") + + +def sxiv_singlefile(inputfile): + # Turn to an absolute path + if inputfile[0] != '/': + inputfile = os.path.abspath(inputfile) + + inputdir = re.sub(r"/[^/]+$", "/", inputfile) + filelist = os.listdir(inputdir) + filename = inputfile + + ## Note: os.path.join seems to be slow. + result = [ inputdir + a for a in filelist if re.search('.(bmp|gif|jpe?g|png)$', a, re.IGNORECASE) != None ] + list.sort(result) + + ## We get the index of the first argument to know where sxiv should start the display. + try: + count = result.index(inputfile) + 1 + except ValueError: + count = 1 + + result = ["-n" + str(count), "--"] + result + if parameters: + result = parameters + result + result = ["sxiv"] + result + + subprocess.call(result) + +def sxiv_multifile(arglist): + result = [ os.path.abspath(a) for a in arglist ] + list.sort(result) + + result = ["--"] + result + if parameters: + result = parameters + result + result = ["sxiv"] + result + + print(result) + subprocess.call(result) + + + +## MAIN +if len(sys.argv) == 1: + usage_exit() + +arglist = sys.argv +arglist.pop(0) + +## Put all sxiv parameters in a string. +parameters = [] +while len(arglist) != 0 and arglist[0] != "--" and arglist[0][0] == "-": + parameters = parameters + [arglist[0]] + arglist.pop(0) + +if len(arglist) == 0: + usage() + sys.exit(0) + +if arglist[0] == "--": + arglist.pop(0) + +if len(arglist) == 0: + usage() +elif len(arglist) == 1: + sxiv_singlefile(arglist[0]) +elif len(arglist) >= 2: + sxiv_multifile(arglist) + +sys.exit(0) +