1 # Emacs 23 daemon capability is a killing feature.
2 # One emacs process handles all your frames whether
3 # you use a frame opened in a terminal via a ssh connection or X frames
4 # opened on the same host.
6 # Benefits are multiple
7 # - You don't have the cost of starting Emacs all the time anymore
8 # - Opening a file is as fast as Emacs does not have anything else to do.
9 # - You can share opened buffered across opened frames.
10 # - Configuration changes made at runtime are applied to all frames.
12 # Require emacs version to be minimum 24
13 autoload -Uz is-at-least
14 is-at-least 24 "${${(Az)"$(emacsclient --version 2>/dev/null)"}[2]}" || return 0
16 # Handle $0 according to the standard:
17 # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
18 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
19 0="${${(M)0:#/*}:-$PWD/$0}"
21 # Path to custom emacsclient launcher
22 export EMACS_PLUGIN_LAUNCHER="${0:A:h}/emacsclient.sh"
24 # set EDITOR if not already defined.
25 export EDITOR="${EDITOR:-${EMACS_PLUGIN_LAUNCHER}}"
27 alias emacs="$EMACS_PLUGIN_LAUNCHER --no-wait"
29 # open terminal emacsclient
30 alias te="$EMACS_PLUGIN_LAUNCHER -nw"
32 # same than M-x eval but from outside Emacs.
33 alias eeval="$EMACS_PLUGIN_LAUNCHER --eval"
34 # create a new X frame
35 alias eframe='emacsclient --alternate-editor "" --create-frame'
37 # Emacs ANSI Term tracking
38 if [[ -n "$INSIDE_EMACS" ]]; then
39 chpwd_emacs() { print -P "\033AnSiTc %d"; }
40 print -P "\033AnSiTc %d" # Track current working directory
41 print -P "\033AnSiTu %n" # Track username
44 autoload -Uz add-zsh-hook
45 add-zsh-hook chpwd chpwd_emacs
48 # Write to standard output the path to the file
49 # opened in the current buffer.
51 local cmd="(buffer-file-name (window-buffer))"
52 local file="$("$EMACS_PLUGIN_LAUNCHER" --eval "$cmd" | tr -d \")"
54 if [[ -z "$file" ]]; then
55 echo "Can't deduce current buffer filename." >&2
62 # Write to standard output the directory of the file
63 # opened in the the current buffer
66 file="$(efile)" || return $?