]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/emacs/emacs.plugin.zsh
fede5b0c4c76023932b4544e60d8515bc5931560
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / emacs / emacs.plugin.zsh
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.
5
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.
11
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
15
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}"
20
21 # Path to custom emacsclient launcher
22 export EMACS_PLUGIN_LAUNCHER="${0:A:h}/emacsclient.sh"
23
24 # set EDITOR if not already defined.
25 export EDITOR="${EDITOR:-${EMACS_PLUGIN_LAUNCHER}}"
26
27 alias emacs="$EMACS_PLUGIN_LAUNCHER --no-wait"
28 alias e=emacs
29 # open terminal emacsclient
30 alias te="$EMACS_PLUGIN_LAUNCHER -nw"
31
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'
36
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
42
43   # add chpwd hook
44   autoload -Uz add-zsh-hook
45   add-zsh-hook chpwd chpwd_emacs
46 fi
47
48 # Write to standard output the path to the file
49 # opened in the current buffer.
50 function efile {
51   local cmd="(buffer-file-name (window-buffer))"
52   local file="$("$EMACS_PLUGIN_LAUNCHER" --eval "$cmd" | tr -d \")"
53
54   if [[ -z "$file" ]]; then
55     echo "Can't deduce current buffer filename." >&2
56     return 1
57   fi
58
59   echo "$file"
60 }
61
62 # Write to standard output the directory of the file
63 # opened in the the current buffer
64 function ecd {
65   local file
66   file="$(efile)" || return $?
67   echo "${file:h}"
68 }