diff options
author | Jari Vetoniemi | 2015-06-29 20:32:07 +0300 |
---|---|---|
committer | Jari Vetoniemi | 2015-06-29 20:32:07 +0300 |
commit | 8e42b7cb92937fbc5ff2238d22f7debd6170df20 (patch) | |
tree | b266e893038a579d9327b9a87273975a5cfdd0f6 | |
download | aur-8e42b7cb92937fbc5ff2238d22f7debd6170df20.tar.gz |
Initial import
-rw-r--r-- | .SRCINFO | 17 | ||||
-rw-r--r-- | PKGBUILD | 35 | ||||
-rw-r--r-- | linopen.conf | 51 | ||||
-rwxr-xr-x | open | 175 |
4 files changed, 278 insertions, 0 deletions
diff --git a/.SRCINFO b/.SRCINFO new file mode 100644 index 000000000000..4643af9fd846 --- /dev/null +++ b/.SRCINFO @@ -0,0 +1,17 @@ +pkgbase = linopen + pkgdesc = Intelligent and suckless replacement for xdg-open + pkgver = 1.6 + pkgrel = 1 + url = http://cloudef.eu + arch = any + license = WTFPL + provides = xdg-utils + conflicts = xdg-utils + backup = etc/linopen.conf + source = open + source = linopen.conf + md5sums = 15a6b084258db3a5d15eda3e65295445 + md5sums = 37eef2ae627bb018b0638d6fefde7780 + +pkgname = linopen + diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 000000000000..de816078e3f5 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,35 @@ +# Maintainer: Cloudef <mailroxas@gmail.com> +# Intelligent and suckless replacement for xdg-open + +pkgname=linopen +pkgver=1.6 +pkgrel=1 +pkgdesc='Intelligent and suckless replacement for xdg-open' +arch=('any') +url='http://cloudef.eu' +license=('WTFPL') +backup=('etc/linopen.conf') +source=('open' 'linopen.conf') + +# Set this 1 to symlink /usr/bin/open to /usr/bin/xdg-open +# and conflict with xdg-utils +PROVIDE_XDG_OPEN=1 + +if [ $PROVIDE_XDG_OPEN -eq 1 ]; then + provides=('xdg-utils') + conflicts=('xdg-utils') +fi + +package() { + install -Dm755 "$srcdir/open" "${pkgdir}/usr/bin/open" + install -Dm644 "$srcdir/linopen.conf" "${pkgdir}/etc/linopen.conf" + + if [ $PROVIDE_XDG_OPEN -eq 1 ]; then + ln -s "/usr/bin/open" "$pkgdir/usr/bin/xdg-open" + fi +} + +md5sums=('15a6b084258db3a5d15eda3e65295445' + '37eef2ae627bb018b0638d6fefde7780') + +# vim: set ts=8 sw=3 tw=0 : diff --git a/linopen.conf b/linopen.conf new file mode 100644 index 000000000000..4db216e18118 --- /dev/null +++ b/linopen.conf @@ -0,0 +1,51 @@ +# +# linopen configuration +# enviroiment variables can be used +# + +# Specify your terminal emulator here +# for terminal support. +terminal=xterm + +# Specify all programs you want to +# open in terminal like this: +interm=vim + +# There are 4 ways to match filetypes. +# The following examples are in the order +# which linopen chooses the program as well. + +# 1. File extension +# .png:sxiv +# .mp4:mplayer +# .txt:vim + +# 2. Mime type +# image/png:sxiv +# video/mp4:mplayer +# text/plain:vim + +# 3. Mime category +image:sxiv +video:mplayer +audio:mplayer->interm # you can also specify the interm rule explictly after '->' +text:vim + +# 4. Regexp +# Match some protocols by default +?'^http:\/\/':$BROWSER +?'^https:\/\/':$BROWSER +?'^www.':$BROWSER +?'^dvd:\/\/':mplayer +?'^cdda:\/\/':mplayer->interm + +# Directory rule for directories +# ideally you want to use file manager +# here if you are a GUI user. +directory:echo + +# Default rule just echoes back whatever +# was feed. If you are using DE you could +# just map a file manager here and it would +# integrate with your system. +default:echo @@ -0,0 +1,175 @@ +#!/bin/bash +# replaces xdg-open +# inspired from mimi: https://github.com/taylorchu/mimi + +_LINOPEN_CFGRC="$HOME/.linopenrc" +_LINOPEN_CFGSYS="/etc/linopen.conf" +_LINOPEN_CFGARG= + +# helpers +err() { echo "$@"; exit 1; } +usage() { echo "usage: $(basename $0) [-c <config>] [file]"; } + +# pipe configuration +getconfig() { + # '#' are comments in configuration :) + [[ -f "$_LINOPEN_CFGARG" ]] && { egrep -v "^(#|$)" "$_LINOPEN_CFGARG"; return; } + [[ -f "$_LINOPEN_CFGRC" ]] && { egrep -v "^(#|$)" "$_LINOPEN_CFGRC"; return; } + [[ -f "$_LINOPEN_CFGSYS" ]] && { egrep -v "^(#|$)" "$_LINOPEN_CFGSYS"; return; } +} + +# match regexp +# $1 = filename +match_regexp() { + open_with='' + getconfig | grep "^?" | while read cf; do + exp="$(echo "$cf" | sed "s/?'\(.*\)':.*/\1/")" + [[ -n "$(echo "$@" | grep "$exp")" ]] && { + open_with="$(echo "$cf" | sed "s/?'.*':\(.*\)/\1/")"; + } + # exit the loop once a match is encountered + [[ ! "${open_with}" = '' ]] && echo "${open_with}" && exit 0 + done +} + +# get terminal emulator from configuration +get_term() { + # inside echo (strip whitespace) + echo $(getconfig | grep -w "^terminal" | head -1 | cut -d = -f 2 | sed 's/[#].*//') +} + +# check, if program needs terminal +# $1 = program +needs_term() { + [[ -n "$(getconfig | grep -w "^interm=$@")" ]] && + return 0 || return 1 +} + +# check, if we need fork for terminal program +needs_fork() { + # is either ran from shell or from script + if [[ "$(ps -o stat= -p $PPID)" == *S* ]]; then + # + == not backgrounded + [[ "$(ps -o stat= -p $$)" == *+* ]] && + return 1 || return 0 + fi +} + +# launch file with correct program +# $1 = filename +# $2 = forced program +launch() { + local program="$2" + local interm=0 + + # is directory? + [[ -d "$1" ]] && { + program="$(getconfig | grep "^directory:" | cut -d : -f 2)" + } + + # if not directory, and file doesn't exist + # try matching regexp + [[ ! -n "$program" ]] && [[ ! -f "$1" ]] && + regexp="$(match_regexp "$1")" + + # if file not found and no matching regexp + [[ ! -f "$1" ]] && [[ ! -d "$1" ]] && [[ ! -n "$regexp" ]] && { + err "file does not exist: $1"; + } || { + [[ -n "$program" ]] || program="$regexp" + } + + # test against extension + [[ -n "$program" ]] || { + local ext=${1##*.}; + program="$(getconfig | grep "^.$ext:" | cut -d : -f 2)" + } + + # test against whole mime type + [[ -n "$program" ]] || program="$(getconfig | \ + grep "^$(file -L -b --mime-type "$1"):" | head -1 | cut -d : -f 2)" + + # test against video/, text/ (first part of mime type) + [[ -n "$program" ]] || program="$(getconfig | \ + grep "^$(file -L -b --mime-type "$1" | sed 's/\(.*\)\/.*/\1:/')" | \ + head -1 | cut -d : -f 2)" + + # test against regexp + [[ -n "$program" ]] || program="$(match_regexp "$1")" + + # test against default as last try + [[ -n "$program" ]] || program="$(getconfig | grep "^default:" | \ + head -1 | cut -d : -f 2)" + + # check arguments + [[ -n "$(echo "$program" | grep "\->interm")" ]] && interm=1 + + # sed out the arguments || comments + program="$(echo "$program" | sed 's/->.*//;s/[#].*//')" + program="$(echo $program)" # strip leading&&trailing whitespace + + # check if program is enviroiment variable + [[ -n "$(echo "$program" | grep '^\$')" ]] && { + program="$(echo "$program" | sed 's/^\$//')" + program="$(env | grep "$program" | head -1 | cut -d = -f 2)" + } + + # no program found + [[ -n "$program" ]] || + err "could not find program for '$(basename $1)', check your configuration" + + # check if we need term or fork + if [[ $interm -eq 1 ]] || needs_term "$program"; then + if needs_fork; then + # open in new terminal + "$(get_term)" -e "$program" "$1" & + else + # open in current terminal + "$program" "$1" + fi + else + # echo program is exception here + [[ "$program" == "echo" ]] && { echo "$1"; } || { + # open in background (redirects everything to /dev/null) + "$program" "$1" &> /dev/null & + } + fi +} + +# handle file +# $1 = filename +handle() { + local filename="$@" + [[ -n "$(echo $filename | grep "^file://")" ]] && { + filename="${filename##file://}" + } + launch "$filename" +} + +main() { + # print usage if no arguments + [[ -n "$@" ]] || { usage; exit 1; } + + # check configuration argument + [[ "$1" == "-c" ]] && { + shift 1; _LINOPEN_CFGARG="$1"; + [[ -f "$_LINOPEN_CFGARG" ]] || + err "no configuration exists: $_LINOPEN_CFGARG" + shift 1 + } + + # check that everything is ok + [[ -n "$(getconfig)" ]] || + err "no configuration exists: /etc/linopen.conf || ~/.linopenrc or the file is empty" + [[ -n "$(getconfig | grep "^default:")" ]] || + err "rule must exist in configuration: 'default:'" + + # handle arguments + while [[ -n "$1" ]]; do + handle "$1" + shift || break + done +} +main "$@" + +# vim: set ts=8 sw=3 tw=0 : |