scripts

Various scripts for UNIX-like systems
git clone https://git.tronto.net/scripts
Download | Log | Files | Refs | README

xplumb (1433B)


      1 #!/bin/sh
      2 
      3 # My interpretation of a plumber (plan9 style).
      4 # It scans the selected text (xsel) and decides what to do with it among
      5 # some possibilities (open file, open url, man page...). If none is matched,
      6 # a choice is prompted.
      7 
      8 # Requires: dmenu (or similar), mail-compose, open-file, terminal, websearch,
      9 #           xedit, xsel
     10 
     11 # TODO:
     12 #	send mail to email address;
     13 #	calendar if it is a date
     14 #	choice: pipe to another program
     15 
     16 browser=${BROWSER:-firefox}
     17 menu=${MENU:-dmenu}
     18 menuopts="-l 10"
     19 
     20 text=$(xsel | sed 's/\n/ /g')
     21 if [ "${#text}" -ge 11 ]; then
     22 	shorttext=$(echo "$text" | cut -b 1-10)...
     23 else
     24 	shorttext="$text"
     25 fi
     26 
     27 choice() {
     28 	chosen=$(printf "websearch\nedit" | \
     29 	         $menu $menuopts -p "What to do with \"$shorttext\"?")
     30 
     31 	if [ "$chosen" = "websearch" ]; then
     32 		websearch "$text"
     33 	elif [ "$chosen" = "edit" ]; then
     34 		f=$(mktemp)
     35 		xsel > "$f"
     36 		xedit "$f"
     37 	fi
     38 }
     39 
     40 trymail() {
     41 	addr=$(echo "$1" | addressgrep)
     42 	( [ -n "$addr" ] && mail-compose "$addr" ) || return 1
     43 }
     44 
     45 tryman() {
     46 	number=$(echo "$1" | sed 's/[^1-8]//g')
     47 	name=$(echo "$1" | sed 's/([1-8])//g' | sed 's/ //g')
     48 	if [ "$(man -w "$number" "$name" | wc -l)" = 1 ]; then
     49 		terminal -e man "$number" "$name"
     50 		return 0
     51 	else
     52 		return 1
     53 	fi
     54 }
     55 
     56 tryurl() {
     57 	url=$(echo "$1" | urlgrep)
     58 	( [ -n "$url" ] && "$browser" "$url" ) || return 1
     59 }
     60 
     61 #open-file -s spawn "$text" || \
     62 tryurl    "$text" || \
     63 trymail   "$text" || \
     64 tryman    "$text" || \
     65 choice