Skip to content

Instantly share code, notes, and snippets.

@kbauer
Created August 20, 2015 11:51
Show Gist options
  • Save kbauer/c644f7ea170bfadf51ad to your computer and use it in GitHub Desktop.
Save kbauer/c644f7ea170bfadf51ad to your computer and use it in GitHub Desktop.
Calculate the number of non-empty, non-commented code lines in an emacs lisp project or configuration file. For simple configurations run `emacs-lines-of-code ~/.emacs`
#!/usr/bin/env bash
set -e -E -u
source libstacktrace || true
DEBUG=false
MANUAL="
USAGE: $0 ( DIRECTORY | FILE ) ...
Tries to calculate the lines of .el code in directory.
"
main() {
if [[ $# = 0 ]]; then
die 1 "$MANUAL"
fi
local numlines_total=0
while [[ $# -gt 0 ]]; do
local directory="$1"
shift 1
debug 'get_el_files "$1" | filter_code_lines' "$directory"
local num_code_lines=$(
get_el_files "$directory" \
| filter_code_lines \
| wc -l)
local numlines_total=$((numlines_total + num_code_lines))
printf "%6d code lines in '%s'\n" "$num_code_lines" "$directory"
done
printf "%6d code lines in total\n" "$numlines_total"
}
die() {
local exitcode="$1"
shift 1
echo "$*" >&2
exit $exitcode
}
debug () {
if $DEBUG; then
local command="$1"
shift 1
eval "$command"
fi
}
get_el_files () {
local directory="$1"
if [[ -f $directory ]]; then
echo "$directory"
else
find "$directory" -iname "*.el"
fi
}
filter_code_lines () {
xargs -d $'\n' grep -vP '^\s*;|^\s*$'
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment