Skip to content

Instantly share code, notes, and snippets.

@nhoriguchi
Last active December 19, 2015 22:18
Show Gist options
  • Save nhoriguchi/6026438 to your computer and use it in GitHub Desktop.
Save nhoriguchi/6026438 to your computer and use it in GitHub Desktop.
emopen ver.0.2.1 # exclude files sized over 1MB
#!/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-line-with-buffer (bufname)
# (with-current-buffer bufname (count-lines 1 (point))))
# (defun get-line-option-with-buffer (bufname)
# (if (and (buffer-file-name bufname)
# (> 1000000 (nth 7 (file-attributes (buffer-file-name bufname)))))
# (format "+%s %s\n"
# (with-current-buffer bufname (+ 1 (count-lines 1 (point))))
# (buffer-file-name bufname))))
# (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 'get-line-option-with-buffer (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 ""
}
show_list() {
ls -1 ${RECORDBASE}* | while read f ; do
basename $f | sed s/\.emacs_opened_files\.//
done
}
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 ; exit 0 ;;
n ) NEWSESSION=true ;;
h ) usage ; exit 0 ;;
esac
done
return $[OPTIND-1]
}
RECORDBASE="${HOME}/.emacs_opened_files"
NEWSESSION=false
parse_options $@
shift $?
# defalut mode
if [ $# -eq 0 ] ; then
usage
echo "Available sessions:"
show_list
exit 0
fi
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 $(tac ${RECORDFILE}) ; do
str="${str} ${f}"
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