BROWSER = dillo
EDITOR = vim
PAGER = less

data       matches  '(([A-Za-z]+):(//)?)? *(.*)' into _ protocol _ file
file       matches  '.*(\.([A-Za-z0-9]+))(\?[^#]*)?(#.*)?' into _ extension query section
file       types    mime
file       at       path


rules      for      directory
mime       matches  'inode/directory'
open       with     xfiles -- %file


# desktop rules

rules      for      application
protocol   matches  'focus'
run        with     focus %file

rules      for      screenshot
data       matches  'scrot'
run        with     sh -c 'maim -s -- "'$HOME'/tmp/scrot-$(date +%%Y-%%m-%%d-%%H%%M%%S).png"'

rules      for      screenshot
protocol   matches  'scrot'
run        with     maim -s -- %file

rules      for      window manager control
protocol   matches  'wm'
run        with     shodc -- %file


# multimedia rules

rules      for      music playlist
path       matches  $HOME/mus/.*
extension  imatches m3u
open       with     sh -c 'mpc clear && mpc load "$@" && mpc shuffle && mpc play' plumb %path

rules      for      playlist
extension  imatches m3u
open       with     mpv --playlist=%file

#rules      for      music
#path       matches  $HOME/mus/.*
#mime       matches  '(application/(octet-stream|ogg))|(audio/.*)'
#open       with     sh -c 'printf "%s\n" "$(realpath "$@")" | mpc insert ; mpc next ; mpc play' plumb %file
#info       with     mediainfo -- %file

rules      for      audio
mime       matches  '(application/(octet-stream|ogg))|(audio/.*)'
open       with     mpv -- %file
info       with     mediainfo -- %file

rules      for      multimedia
mime       matches  'video/.*'
open       with     mpv -- %file
info       with     mediainfo -- %file

rules      for      external image
protocol   matches  'gophers?'
extension  imatches 'jpe?g|png|gif|xpm|xbm|webp'
open       with     sh -c 'curl -Lfsm 10 -- "$1" | display -' sh %data

rules      for      external media file
protocol   matches  '(gopher|http)s?'
extension  imatches 'mp4|webm|ogv|ogg|opus|mpg|mpeg|mov|flac|wav|m4a|mp3|mkv|avi|jpe?g|png|gif|xpm|xbm|webp'
open       with     mpv --loop -- %data

rules      for      telegram media file
protocol   matches  '(http)s?'
file       matches  'cobaia.vm/tg/.*'
open       with     sh -c 'for file ; do set -- "$@" "$file." ; shift ; done ; mpv --loop -- "$@"' sh %data


# archives

ZIPBEG   = 'for file ; do dir="${file%.*}" ; mkdir -p "$dir" && '
ZIPEND   = ' ; done'

rules      for      gzip archive
mime       matches  'application/(x-)?gzip'
info       with     tar tzf %file
unzip      with     sh -c  $ZIPBEG'tar xzvf "$file" -C "$dir"'$ZIPEND plumb %file

rules      for      bzip2 archive
mime       matches  'application/(x-)?bzip2'
info       with     tar tjf %file
unzip      with     sh -c  $ZIPBEG'tar xjvf "$file" -C "$dir"'$ZIPEND plumb %file

rules      for      tar archive
mime       matches  'application/(x-)?tar'
info       with     tar tf %file
unzip      with     sh -c  $ZIPBEG'tar xvf "$file" -C "$dir"'$ZIPEND plumb %file

rules      for      zip archive
mime       matches  'application/(x-)?zip'
info       with     unzip -l -- %file
unzip      with     sh -c  $ZIPBEG'unzip -d "$dir" -- "$file"'$ZIPEND plumb %file

rules      for      7z archive
mime       matches  'application/(x-)?7z(-compressed)?'
info       with     7z l %file
unzip      with     sh -c  $ZIPBEG'7z x -o"$dir" "$file"'$ZIPEND plumb %file


# local file rules

rules      for      bitmap font
protocol   matches  '(file)?'
extension  imatches bdf
open       with     gbdfed %file

rules      for      youtube link
extension  imatches 'yt(dlp)?'
path       matches  $HOME/var/news/.*
open       with     mpv --ytdl-format=mp4+ba --playlist=%file

rules      for      mediawiki file
extension  imatches 'wiki|mediawiki'
open       with     $TERMCMD -e sh -c 'cat "$@" | pandoc -f mediawiki -t markdown | less' plumb %file

rules      for      paged document
protocol   matches  '(file)?'
mime       matches  '.*(pdf|postscript|eps|ps|epub)'
open       with     zathura -- %file
info       with     sh -c 'for file ; do pdftotext -layout -- "$file" - ; done' plumb %file

rules      for      paged document
protocol   matches  '(file)?'
extension  matches  'pdf|eps|ps|epub'
open       with     zathura -- %file

rules      for      scanned document
protocol   matches  '(file)?'
mime       matches  '.*djvu'
open       with     djview -zoom=width -continuous=yes -sidebyside=no -scrollbars=no -frame=yes -toolbar=no -menubar=no -statusbar=no -sidebar=no %file

rules      for      pixmap
protocol   matches  '(file)?'
extension  imatches 'xpm|xbm'
open       with     sh -c 'case "$#" in (1) set -- "$(dirname "$1")" ;; esac ; xbmbrowser "$@"' plumb %file
edit       with     gimp -- %file

rules      for      gif
protocol   matches  '(file)?'
mime       matches  'image/gif'
open       with     mpv --loop -- %file
info       with     sh -c 'anytopnm <"$1" | pnmscalefixed -w 50 | ppmtopgm | pgmtopbm | pbmtoascii' plumb %file

rules      for      image
protocol   matches  '(file)?'
mime       matches  'image/.*'
open       with     img %file
edit       with     gimp -- %file
info       with     sh -c 'anytopnm <"$1" | pnmscalefixed -w 50 | ppmtopgm | pgmtopbm | pbmtoascii' plumb %file

rules      for      man page
protocol   matches  '(man)?'
extension  imatches '[0-9]'
open       with     xman %file
edit       with     $TERMCMD -e $EDITOR -- %file
info       with     sh -c 'exec mandoc -Tascii -- "$@" | col -b | expand -t 8' plumb %file

rules      for      html file
mime       matches  'text/html'
open       with     $TERMCMD -e sh -c 'cat "$@" | webdump | less' plumb %file
edit       with     $TERMCMD -e $EDITOR -- %file

rules      for      text file
protocol   matches  '(file)?'
mime       matches  '(text/.*)|(.*/(x-empty|x-shellscript))'
open       with     $TERMCMD -e $PAGER -- %file
edit       with     $TERMCMD -e $EDITOR -- %file
info       with     cat -- %file

rules      for      text file
protocol   matches  '(file)?'
extension  imatches 'txt|md|awk|sh|json'
open       with     $TERMCMD -e $PAGER -- %file
edit       with     $TERMCMD -e $EDITOR -- %file
info       with     cat -- %file


# external document rules

rules      for      youtube video
protocol   matches  'ytdl|https?'
file       matches  '((.+\.)?y(ew|ou)tu\.?be(\.com)?/.*)?(.*/watch\?v=)?([A-Za-z0-9_-]{11,})([&?].*)?'
open       with     mpv --ytdl-format=mp4+ba -- ytdl://%file

rules      for      youtube video
data       matches  '(.+\.)?y(ew|ou)tu\.?be(\.com)?/.*(.*/watch\?v=)?([A-Za-z0-9_-]{11,})([&?].*)?'
open       with     mpv --ytdl-format=mp4+ba -- ytdl://%file

rules      for      web media
protocol   matches  '(https?)?'
file       matches  '(www\.)?(instagram\.com|v\.redd\.it)/.*'
open       with     mpv -- ytdl://%file

rules      for      external paged document
protocol   matches  '(gopher|http)s?'
extension  imatches 'pdf|eps|ps|djvu'
open       with     sh -c 'cd /tmp && curl -LOfsm 10 --max-filesize 10M -- "$@" 2>/dev/null && zathura -- "$(basename "$@")"' sh %data

rules      for      web page
protocol   matches  '(gopher|http)s?'
open       with     $BROWSER -- %data

HOST = '([A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)*)'
TLDS = '(\.(net|org|com|en|us|io|sh|xyz|dev|coffee))'
PORT = '(:[0-9]+)?'
PATH = '(/?[^?/#]*)*'
QERY = '(\??[^#]*)?'
FRAG = '(#?.*)?'
rules      for      web page (without protocol)
file       matches  $HOST$TLDS$PORT$PATH$QERY$FRAG
open       with     $BROWSER -- https://%file


# web search rules

rules      for      youtube search
protocol   matches  'y(tdl|outube)?'
search     with     $BROWSER -- https://inv.nadeko.net/search?q=%file

rules      for      wikipedia search
protocol   matches  'w(iki)?'
search     with     $BROWSER -- https://en.wikipedia.org/w/index.php?go=Go&search=%file

rules      for      wiktionary search
protocol   matches  'd(ict)?'
search     with     $BROWSER -- https://en.wiktionary.org/w/index.php?go=Go&search=%file

rules      for      web search
search     with     $BROWSER -- https://duckduckgo.com/lite/?kp=-1&kd=-1&q=%file
