Parent directory

emacs.plugin.zsh

2166 bytes
 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
13autoload -Uz is-at-least
14is-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
180="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
190="${${(M)0:#/*}:-$PWD/$0}"
20
21# Path to custom emacsclient launcher
22export EMACS_PLUGIN_LAUNCHER="${0:A:h}/emacsclient.sh"
23
24# set EDITOR if not already defined.
25export EDITOR="${EDITOR:-${EMACS_PLUGIN_LAUNCHER}}"
26
27alias emacs="$EMACS_PLUGIN_LAUNCHER --no-wait"
28alias e=emacs
29# open terminal emacsclient
30alias te="$EMACS_PLUGIN_LAUNCHER -nw"
31
32# same than M-x eval but from outside Emacs.
33alias eeval="$EMACS_PLUGIN_LAUNCHER --eval"
34# create a new X frame
35alias eframe='emacsclient --alternate-editor="" --create-frame'
36
37# Emacs ANSI Term tracking
38if [[ -n "$INSIDE_EMACS" ]] && [[ "$INSIDE_EMACS" != "vterm" ]]; 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
46fi
47
48# Write to standard output the path to the file
49# opened in the current buffer.
50function 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 current buffer
64function ecd {
65  local file
66  file="$(efile)" || return $?
67  echo "${file:h}"
68}