aboutsummaryrefslogtreecommitdiff
path: root/bin/ec
blob: 78483a79d0a82f48611d660de30c3c44de9fca5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env bash

export LC_CTYPE=zh_CN.UTF-8

# Number of current visible frames,
# Emacs daemon always has a visible frame called F1 
visible_frames() {
  emacsclient -a "" -e '(length (visible-frame-list))'
}

change_focus() {
  emacsclient -n -e "(select-frame-set-input-focus (selected-frame))" > /dev/null
}


if [[ $(uname) == Darwin ]]; then
  # try switching to the frame incase it is just minimized
  # will start a server if not running
  test "$(visible_frames)" -eq "1" && change_focus
  if [ "$(visible_frames)" -lt  "2" ]; then # need to create a frame
    # -c $@ with no args just opens the scratch buffer
    emacsclient -n -c "$@" && change_focus
  else # there is already a visible frame besides the daemon, so
    change_focus
    # -n $@ errors if there are no args
    test  "$#" -ne "0" && emacsclient -n "$@"
  fi
else
  emacsclient -a "" -n -c "$@"
fi