From ea8153107706771da2cbc691a51d04d228368922 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Sun, 3 Mar 2013 11:19:50 +0100 Subject: [PATCH] Shell/Scripts: moved all useful functions (for whom it makes sense to call them from Emacs or Ranger) to separate scripts. --- .scripts/abs-fetch | 4 +- .scripts/archive | 96 +++++++ .scripts/asciify | 64 +++++ .scripts/blind-append | 41 +++ .scripts/crun | 32 +++ .scripts/ediff | 3 + .scripts/extract | 26 ++ .scripts/formatc | 38 +++ .scripts/git-check | 9 + .scripts/git-compare | 16 ++ .scripts/ltx | 16 ++ .scripts/pacman-diff | 2 +- .scripts/pdfcompress | 26 ++ .scripts/pdfextract | 13 + .scripts/pdfresize | 10 + .scripts/pkggen | 2 +- .scripts/sanitize | 15 + .scripts/tc-audio-transcode | 8 +- .scripts/tc-video-transcode | 4 +- .scripts/texclean | 32 +++ .scripts/vcsclean | 36 +++ .scripts/wget-batch | 8 + .shell.d/alias_rc | 1 + .shell.d/funs_rc | 538 +----------------------------------- 24 files changed, 498 insertions(+), 542 deletions(-) create mode 100755 .scripts/archive create mode 100755 .scripts/asciify create mode 100755 .scripts/blind-append create mode 100755 .scripts/crun create mode 100755 .scripts/ediff create mode 100755 .scripts/extract create mode 100755 .scripts/formatc create mode 100755 .scripts/git-check create mode 100755 .scripts/git-compare create mode 100755 .scripts/ltx create mode 100755 .scripts/pdfcompress create mode 100755 .scripts/pdfextract create mode 100755 .scripts/pdfresize create mode 100755 .scripts/sanitize create mode 100755 .scripts/texclean create mode 100755 .scripts/vcsclean create mode 100755 .scripts/wget-batch diff --git a/.scripts/abs-fetch b/.scripts/abs-fetch index 641888f5..055a89f6 100755 --- a/.scripts/abs-fetch +++ b/.scripts/abs-fetch @@ -39,7 +39,7 @@ while getopts ":hfy" opt; do ;; :) echo "Missing argument." - echo "Use $0 -h for help." + _printhelp "$0" exit 1 ;; esac @@ -49,7 +49,7 @@ shift $(($OPTIND - 1)) if [ $# -eq 0 ]; then echo "Missing argument." - echo "Use $0 -h for help." + _printhelp "$0" exit 1 fi diff --git a/.scripts/archive b/.scripts/archive new file mode 100755 index 00000000..51c97292 --- /dev/null +++ b/.scripts/archive @@ -0,0 +1,96 @@ +#!/usr/bin/zsh +## We need zsh because of array use here. + +_printhelp() +{ + cat <=/g' \ + -e 's/[±]/+\/-/g' \ + -e 's/[≠]/!=/g' \ + -e 's/[⋅]/./g' \ + -e 's/[×]/x/g' \ + -e 's/[÷]/\//g' \ + -e 's/[↓]/|/g' \ + -e 's/[↑]/^/g' \ + -e 's/[←]/<=/g' \ + -e 's/[→]/=>/g' \ + "$i" +done; diff --git a/.scripts/blind-append b/.scripts/blind-append new file mode 100755 index 00000000..d0836ef6 --- /dev/null +++ b/.scripts/blind-append @@ -0,0 +1,41 @@ +#!/usr/bin/env zsh + +if [ $# -gt 2 ] || [ $# -lt 1 ] || [ "$1" = "-h" ]; then + cat <> "$FILE" + echo "Secret appended to ${FILE} at the end." + exit +fi + + +if [ $# -eq 1 ]; then + echo "$DUMMY" >> "$FILE" +else + sed -i "s/${STRING}/${STRING}${DUMMY}/g" "${FILE}" +fi +echo "Secret appended to ${FILE}." diff --git a/.scripts/crun b/.scripts/crun new file mode 100755 index 00000000..a243e06d --- /dev/null +++ b/.scripts/crun @@ -0,0 +1,32 @@ +#!/bin/sh + +if [ $# -lt 1 ]; then + echo "Usage: ${0##*/} FILE [GCC_OPTS]" + exit +fi + +INPUT="$1" + +shift +GCC_OPTS="-O0 -Wall -Wextra -Wshadow -pthread" +if [ $# -ne 0 ]; then + GCC_OPTS="$@" +fi + +FILE=$(mktemp) +echo "==> gcc \"$INPUT\" -o \"$FILE\" $GCC_OPTS" + +## Zsh compatibility. We need it otherwise word splitting of parameter GCC_OPTS +## will not work. +STATUS="$(set -o | grep 'shwordsplit' | awk '{print $2}')" +[ "$STATUS" = "off" ] && set -o shwordsplit + +gcc "$INPUT" -o "$FILE" $GCC_OPTS + +## Restore Zsh previous options. This will not turn off shwordsplit if it +## was on before calling the function. +[ "$STATUS" = "off" ] && set +o shwordsplit + +echo "==> $FILE" +"$FILE" +rm "$FILE" diff --git a/.scripts/ediff b/.scripts/ediff new file mode 100755 index 00000000..b6443566 --- /dev/null +++ b/.scripts/ediff @@ -0,0 +1,3 @@ +#!/bin/sh + +emacs -q -l ~/.emacs-light --eval "(ediff \"$1\" \"$2\")" diff --git a/.scripts/extract b/.scripts/extract new file mode 100755 index 00000000..0357df28 --- /dev/null +++ b/.scripts/extract @@ -0,0 +1,26 @@ +#!/bin/sh + +[ -n "$(command -v atool)" ] && echo "You should use atool instead." + +if [ -f "$1" ] ; then + case "$1" in + *.tar.bz2) tar xvjf "$1" ;; + *.tar.gz) tar xvzf "$1" ;; + *.tar.xz) tar xvJf "$1" ;; + *.bz2) bunzip2 "$1" ;; + *.rar) unrar x "$1" ;; + *.gz) gunzip "$1" ;; + *.tar) tar xvf "$1" ;; + *.tbz2) tar xvjf "$1" ;; + *.tgz) tar xvzf "$1" ;; + *.txz) tar xvJf "$1" ;; + *.zip) unzip "$1" ;; + *.Z) uncompress "$1" ;; + *.7z) 7z x "$1" ;; + *.xz) unxz "$1" ;; + *.exe) cabextract "$1" ;; + *) echo "[$1]: unrecognized file compression" ;; + esac +else + echo "[$1] is not a valid file" +fi diff --git a/.scripts/formatc b/.scripts/formatc new file mode 100755 index 00000000..e56a3045 --- /dev/null +++ b/.scripts/formatc @@ -0,0 +1,38 @@ +#!/bin/sh + +## Format C using 'indent'. Alternative to indent: astyle, uncrustify. +if [ -z "$(command -v indent)" ]; then + echo "Please install 'indent'" + exit +fi + +_formatc_dir() +{ + find "$1" -type f \ + -name "*.[ch]" \ + -print \ + -exec indent -i4 -ppi4 -bli0 -cli4 -nut {} \; + + ## Remove backup files. + find "$1" -type f \( \ + -name "*.[ch]~" -o \ + -name "*.[ch]~[0-9]*~" \) \ + -delete +} + +if [ $# -eq 0 ]; then + echo "Working on current dir" + _formatc_dir "$PWD" + exit +fi + +for i ; do + echo "[$i]" + + if [ -d "$i" ]; then + _formatc_dir "$i" + else + indent -i4 -ppi4 -bli0 -cli4 -nut "$i" + rm -f "$i~" "$i~[0-9]*~" + fi +done diff --git a/.scripts/git-check b/.scripts/git-check new file mode 100755 index 00000000..90a245ea --- /dev/null +++ b/.scripts/git-check @@ -0,0 +1,9 @@ +#!/bin/sh + +while IFS= read -r FOLDER; do + if [ -z "$(cd "${FOLDER%/*}" && git status -uno | grep "nothing to commit")" ]; then + echo "${FOLDER%/*}" + fi +done < "$REPO" +find "$1" -type f ! -path "*.git/*" | sed 's/^[^\/]*\///g' | sort > "$FOLDER" +rm -f "$REPO" "$FOLDER" + +## Zsh version. +# comm -3 <(git ls-files | sort) <(find "$1" -type f ! -path "*.git/*" | sed 's/^[^\/]*\///g' | sort) diff --git a/.scripts/ltx b/.scripts/ltx new file mode 100755 index 00000000..bae368a2 --- /dev/null +++ b/.scripts/ltx @@ -0,0 +1,16 @@ +#!/bin/sh + +if [ $# -ne 1 ]; then + echo "Usage: ${0##*/} FILE" + + echo "LaTeX quick compiler. It adds the preambule -- and the + \end{document} -- automatically." + exit +fi + +## One line is mandatory. +PREAMBLE='\documentclass[10pt,a4paper]{article}\usepackage[utf8]{inputenc}\usepackage[T1]{fontenc}\usepackage{amsmath,amssymb,amsfonts}\usepackage{geometry}\usepackage{lmodern}\usepackage{marvosym}\usepackage{textcomp}\DeclareUnicodeCharacter{20AC}{\EUR{}}\DeclareUnicodeCharacter{2264}{\leqslant}\DeclareUnicodeCharacter{2265}{\geqslant}\begin{document}\input' + +END='\end{document}' + +pdflatex -file-line-error-style -interaction nonstopmode -jobname="${1%.tex}" "$PREAMBLE" "$1" "$END" diff --git a/.scripts/pacman-diff b/.scripts/pacman-diff index 50cec0b1..91e06800 100755 --- a/.scripts/pacman-diff +++ b/.scripts/pacman-diff @@ -50,7 +50,7 @@ shift $(($OPTIND - 1)) if [ $# -eq 0 ]; then echo "Missing argument." - echo "Use $0 -h for help." + _printhelp "$0" exit 1 fi diff --git a/.scripts/pdfcompress b/.scripts/pdfcompress new file mode 100755 index 00000000..846e879b --- /dev/null +++ b/.scripts/pdfcompress @@ -0,0 +1,26 @@ +#!/bin/sh + +if [ $# -lt 1 ] || [ $# -gt 2 ] || [ "$1" = "-h" ]; then + cat < " - return - fi - - wget -r -l1 --no-parent -A$1 $2 -} - ## Webcam webcam () { @@ -190,7 +181,6 @@ webcam () mplayer -fps 30 -tv driver=v4l2:width=640:height=480:device=/dev/video0 tv:// -flip } - ## Vim-only: search the vim reference manual for a keyword. ## Usage: :h if [ -n "$(command -v vim)" ]; then @@ -200,145 +190,6 @@ if [ -n "$(command -v vim)" ]; then } fi -## Set file/directory owner and permissions. -## Usage: sanitize [FOLDER] -sanitize() -{ - local WORKDIR="$PWD" - if [ $# -eq 1 ]; then - WORKDIR="$1" - fi - - local FMASK=$(umask -S | sed 's/x//g') - local DMASK=$(umask -S) - - find "$WORKDIR" -exec chown -R ${UID}:${GID} {} \; - find "$WORKDIR" -type d -exec chmod ${DMASK} {} \; - find "$WORKDIR" -type f -exec chmod ${FMASK} {} \; -} - -asciify() -{ - _printhelp() - { - cat <=/g' \ - -e 's/[±]/+\/-/g' \ - -e 's/[≠]/!=/g' \ - -e 's/[⋅]/./g' \ - -e 's/[×]/x/g' \ - -e 's/[÷]/\//g' \ - -e 's/[↓]/|/g' \ - -e 's/[↑]/^/g' \ - -e 's/[←]/<=/g' \ - -e 's/[→]/=>/g' \ - "$i" - done; -} - -blind-append() -{ - _printhelp() - { - cat <> "$FILE" - echo "Secret appended to ${FILE} at the end." - return - fi - - - if [ $# -eq 1 ]; then - echo "$DUMMY" >> "$FILE" - else - sed -i "s/${STRING}/${STRING}${DUMMY}/g" "${FILE}" - fi - echo "Secret appended to ${FILE}." - return -} - ## Term properties termcolors256() { @@ -432,269 +283,6 @@ EOF } fi -## Extractor -- Useless when using 'atool'. -if [ ! -n "$(command -v atool)" ]; then - extract () - { - if [ -f $1 ] ; then - case $1 in - *.tar.bz2) tar xvjf $1 ;; - *.tar.gz) tar xvzf $1 ;; - *.tar.xz) tar xvJf $1 ;; - *.bz2) bunzip2 $1 ;; - *.rar) unrar x $1 ;; - *.gz) gunzip $1 ;; - *.tar) tar xvf $1 ;; - *.tbz2) tar xvjf $1 ;; - *.tgz) tar xvzf $1 ;; - *.zip) unzip $1 ;; - *.Z) uncompress $1 ;; - *.7z) 7z x $1 ;; - *.xz) unxz $1 ;; - *.exe) cabextract $1 ;; - *) echo "\`$1': unrecognized file compression" ;; - esac - else - echo "\`$1' is not a valid file" - fi - } -fi - -## WARNING: use this function with caution. It may drastically improve -## compression of some PDF files, but in some case, the output filesize will be -## greater! You should not use it over PDF files embedding pictures as well. -pdfcompress () -{ - if [ $# -lt 1 -o $# -gt 2 ]; then - echo "Usage: pdfcompress PDFFILE [DESTFILE]" - return - fi - - if [ ! -f "$1" ]; then - echo "$1 is not a valid PDF file!" - fi - local INPUTFILE="$1" - - if [ -z "$2" ]; then - gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="${INPUTFILE%%.*}-COMPRESSED.pdf" "${INPUTFILE}" - rm -rf "${INPUTFILE}" - mv "${INPUTFILE%%.*}-COMPRESSED.pdf" "${INPUTFILE}" - else - local OUTPUTFILE="$2" - gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="${OUTPUTFILE}" "${INPUTFILE}" - fi -} - -## Extract a range of pages from a PDF. -pdfextract () -{ - if [ $# -ne 4 ]; then - echo "Usage: $0 PDFFILE FIRSTPAGE LASTPAGE DESTFILE" - return - fi - - if [ ! -f "$1" ]; then - echo "$1 is not a valid PDF file!" - fi - - gs -sDEVICE=pdfwrite -dNOPAUSE -dSAFER -dSAFER -dFirstPage=$2 -dBATCH -dLastPage=$3 -sOutputFile=$4 $1 -} - -## Resize input PDF to an A4 output PDF. -pdfresize () -{ - if [ $# -ne 2 ]; then - echo "Usage: $0 " - return - fi - - gs -q -o $2 -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -dCompatibilityLevel=1.4 $1 -} - -archive () -{ - _printhelp() - { - cat < gcc \"$INPUT\" -o \"$FILE\" $GCC_OPTS" - - ## Zsh compatibility. We need it otherwise word splitting of parameter like - ## TC_SAMPLE will not work. - local STATUS - STATUS="$(set -o | grep 'shwordsplit' | awk '{print $2}')" - [ "$STATUS" = "off" ] && set -o shwordsplit - - gcc "$INPUT" -o "$FILE" $GCC_OPTS - ## Restore Zsh previous options. This will not turn off shwordsplit if it - ## was on before calling the function. - [ "$STATUS" = "off" ] && set +o shwordsplit - - echo "==> $FILE" - "$FILE" - rm "$FILE" -} - -git-check() -{ - while IFS= read -r FOLDER; do - if [ -z "$(cd "${FOLDER%/*}" && git status -uno | grep "nothing to commit")" ]; then - echo "${FOLDER%/*}" - fi - done <