# List buffers in emacs
lsemacs() {
    emacsclient -e "(sort (remove nil (mapcar #'buffer-file-name (buffer-list))) #'string<)" | tr ' ' '\n' | tr '(")' '   ' | sed 's/\s//g'
}

# List files recently modified $1=number of files to list, $2=modified within minutes
lsrecent() {
  count=$1; [ "$count" == '' ] && count=1
  min=$2; [ "$min" == '' ] && min=108000
    find \
	-not -path '*.git*' \
	-not -path '*dist*' \
	-not -path '*vendor*' \
	-mmin -$min -type f \
	-exec grep -Iq . {} \; \
	-exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d' ' -f5 | head -$count
}

# Open recent modified files in emacs
editlast() {
    last=`lsrecent 10 10800`
    echo $last
    if [ "$last" != "" ]
    then
        if pgrep "emacs" > /dev/null
        then
            emacsclient -n $last
        else
            emacs &
            sleep 2 &
            open_last_modified $1
        fi
    fi
}

# Open files from HEAD commit by given pattern
edithead() {
  if [ -z $1 ]
  then
    git show --name-only HEAD
    echo "\nUsage: openhead PATTERN";
  else
    git show --name-only HEAD | grep "$1" | xargs emacsclient -n
  fi
}

# Close all buffers in emacs
closeall() {
    emacsclient -e "(mapc 'kill-buffer (buffer-list))" > /dev/null
}


# Save all buffers in emacs
saveall() {
    emacsclient -e "(save-some-buffers t)" > /dev/null
}
