Created
July 12, 2013 22:48
-
-
Save nhoriguchi/5988431 to your computer and use it in GitHub Desktop.
emacs wrapper to keep sets of opened files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# emopen: emacs wrapper to keep sets of opened files | |
# | |
# Author: Naoya Horiguchi <nao.horiguchi@gmail.com> | |
# | |
# Before using this, you need add the following elisp code on your .emacs: | |
# | |
# (defun get-list-opened-files () | |
# "Get the list of opend files to recover in next open." | |
# (interactive) | |
# (if (file-exists-p "~/.emacs_opened_files") (delete-file "~/.emacs_opened_files")) | |
# (append-to-file | |
# (mapconcat 'identity (remove nil (mapcar 'buffer-file-name (buffer-list))) " ") | |
# nil "~/.emacs_opened_files") | |
# ) | |
# (add-hook 'kill-emacs-hook 'get-list-opened-files) | |
usage() { | |
local sname=`basename $BASH_SOURCE` | |
echo "Usage: $sname [-lnh] <session>" | |
echo "" | |
echo "Description:" | |
echo " -l: show the list of saved sessions" | |
echo " -n: create a new session" | |
echo " (useful when new session name partially matches existing ones)" | |
echo " -h: show this message" | |
echo "" | |
exit 0 | |
} | |
show_list() { | |
ls -1 ${RECORDBASE}* | while read f ; do | |
basename $f | sed s/\.emacs_opened_files\.// | |
done | |
exit 0 | |
} | |
show_candidates() { | |
echo "Ambiguous session name: $MODE" | |
for f in $@ ; do | |
echo " $f" | |
done | |
echo "" | |
echo "If you launch a new session, use -n option." | |
exit 1 | |
} | |
parse_options() { | |
while getopts lnh OPT ; do | |
case $OPT in | |
l ) show_list ;; | |
n ) NEWSESSION=true ;; | |
h ) usage ;; | |
esac | |
done | |
return $[OPTIND-1] | |
} | |
RECORDBASE="${HOME}/.emacs_opened_files" | |
NEWSESSION=false | |
parse_options $@ | |
shift $? | |
# defalut mode | |
MODE=work | |
[ $# -gt 0 ] && MODE=$1 | |
RECORDFILE=${RECORDBASE}.${MODE} | |
RECORDFILEBASE=$(basename ${RECORDBASE}.${MODE}) | |
CANDIDATE="$(find $(dirname $RECORDFILE) -maxdepth 1 -name "${RECORDFILEBASE}*")" | |
NRWORDS=$(echo "$CANDIDATE" | wc -w) | |
if [ "$NRWORDS" -eq 0 -o "${NEWSESSION}" = true ] ; then | |
echo "Open a new session named '$MODE'" | |
emacs -nw | |
elif [ "$NRWORDS" -gt 1 ] ; then | |
show_candidates $CANDIDATE | |
else | |
echo "Open the session '$MODE'" | |
RECORDFILE=$CANDIDATE | |
str="" | |
# reverse the order of file paths | |
for f in $(cat ${RECORDFILE}) ; do | |
str="${f} ${str}" | |
done | |
emacs -nw ${str} | |
fi | |
[ -e "${RECORDBASE}" ] && mv ${RECORDBASE} ${RECORDFILE} | |
echo "you can reopen all files with \"emopen $MODE\"." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment