summaryrefslogtreecommitdiff
path: root/.config/wmii-hg/util.sh
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2014-03-27 15:50:32 -0400
committerLuke Shumaker <LukeShu@sbcglobal.net>2014-03-27 15:50:32 -0400
commit9e79a6c3bbc37c2eec19e055566440f0b1173c5c (patch)
tree7516e65c0289b9a68b3a97fe2abbdf0d7dba8d8d /.config/wmii-hg/util.sh
parent5c42078bd052705b69d6ea93ff7617445be43789 (diff)
parentd65775a22e1648eca8e74389a4d9c1c299017cf3 (diff)
Merge remote-tracking branch 'origin/master' into purdue-cs
Conflicts: .config/X11/clientrc .config/bash/rc.d/emacs.sh .config/bash/rc.sh .config/conkeror .config/conkerorrc .config/emacs/custom.el .config/irbrc .config/login.sh .config/maildirproc/att.rc .config/maildirproc/default.rc .config/offlineimaprc .config/ssh/config .config/wmii-hg/autostart .config/wmii-hg/include.sh .config/wmii-hg/quit .config/wmii-hg/rbar_battery .config/wmii-hg/rbar_clock .config/wmii-hg/rbar_cpu .config/wmii-hg/rbar_wifi .conkerorrc .maildirproc/default.rc
Diffstat (limited to '.config/wmii-hg/util.sh')
-rw-r--r--.config/wmii-hg/util.sh129
1 files changed, 129 insertions, 0 deletions
diff --git a/.config/wmii-hg/util.sh b/.config/wmii-hg/util.sh
new file mode 100644
index 0000000..71dbc41
--- /dev/null
+++ b/.config/wmii-hg/util.sh
@@ -0,0 +1,129 @@
+#!/bin/bash
+
+# I moved "fixes" into a separate file because it isn't so much configuration...
+. fixes.sh
+
+################################################################################
+# Added shell features #
+################################################################################
+
+##
+# Usage: dquote STRING
+# Safely double-quotes a string.
+# It escapes ways to execute code, but not variables.
+##
+dquote() {
+ local str=$1
+ str="${str//\\/\\\\}" # backslash
+ str="${str//\"/\\\"}" # dquote
+ str="${str//\$(/\\\$(}" # $(...)
+ str="${str//\`/\\\`}" # backtick
+ printf '"%s"\n' "$str"
+}
+
+##
+# Usage: expand_variables
+# Expands variables read from /dev/stdin
+##
+expand_variables() {
+ while read; do
+ eval printf "'%s\n'" "$(dquote "$REPLY")"
+ done
+}
+
+is_mounted() {
+ local dir="$(readlink -m $1)"
+ local mntpnt="$(cut -d' ' -f2 /proc/mounts|grep -Fx -- "$dir")"
+ [[ $dir = "$mntpnt" ]]
+ return $?
+}
+
+################################################################################
+# PATH manipulation #
+################################################################################
+
+##
+# Usage: path_ls PATH
+# List executables in PATH (PATH is delimited by `:')
+##
+path_ls() {
+ local dirs
+ IFS=: dirs=($@)
+ find -L "${dirs[@]}" -maxdepth 1 -type f -executable -printf '%f\n' 2>/dev/null | sort -u
+}
+
+##
+# Usage: path_which PATH PROGRAM
+# Find the full path of PROGRAM by searching PATH
+##
+path_which() {
+ local mypath=$1
+ local prog=$2
+ local which=$(which which)
+ PATH="$mypath" "$which" -- "$prog" 2>/dev/null
+}
+
+################################################################################
+# wmii convenience functions #
+################################################################################
+
+##
+# Usage: lstags
+# Lists wmii tags
+##
+lstags() {
+ ls "$WMII_DIR/tag" | sed -e 's@/$@@' -e '/^sel$/d'
+}
+
+################################################################################
+# X11 functions #
+################################################################################
+
+##
+# Usage: connected_to_x_server
+# Return status indicates whether there is an X server at $DISPLAY
+##
+connected_to_x_server() {
+ xdpyinfo &>/dev/null
+ return $?
+}
+
+################################################################################
+# My wmii configuration #
+################################################################################
+
+##
+# Usage: scansection [SECTION]
+# Reads the doc comments from a section of wmiirc.
+# If SECTION is not given, it reads all doc comments.
+##
+scansection() {
+ local file=$(conffile config.sh)
+ local sec=$1
+ local tmp=$(mktemp --tmpdir wmii-scansecion.XXXXXXXXXX)
+ # Isolate the sections we want.
+ if [ -n "$sec" ]; then
+ # Find the section
+ < "$file" sed -n "/^\s*$sec\s*()/,/##\s*End $sec/p" | sed '1d;$d'> "$tmp"
+ else
+ # Remove extra lines that mark the end of a section
+ < "$file" sed "/\s*}\s*##\s*End\s/d" > "$tmp"
+ fi
+ < "$tmp" sed -n '/##/p' | while read; do
+ var="$(echo "$REPLY" | sed -nr 's/^\s*(.*)\)\s*##.*/\1/p')"
+ comment="$(echo "$REPLY" | sed -r 's/.*## ?//')"
+ if [ -z "$var" ]; then
+ printf '%s\n' "$comment"
+ else
+ printf '\t%s\t%s\n' "$(echo "$var"|expand_variables)" "$comment"
+ fi
+ done
+ rm -- "$tmp"
+}
+
+##
+# Usage: conffile FILE
+##
+conffile() {
+ echo "$HOME/.wmii-hg/$1"
+}