forked from davidshepherd7/emacs-read-stdin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emacs-read-stdin.sh
42 lines (36 loc) · 1.27 KB
/
emacs-read-stdin.sh
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
31
32
33
34
35
36
37
38
39
40
41
42
# include this file in your .bashrc/.zshrc with source "emacs-pipe.sh"
# The emacs or emacsclient to use
_emacsstdin()
{
# Replace with `emacs` to not run as server/client
emacsclient -c -n "$@"
}
# An emacs or emacsclient to use for only in terminal
_emacststdin()
{
# Replace with `emacs` to not run as server/client
emacsclient -t "$@"
}
# An emacs 'alias' with the ability to read from stdin
e()
{
# If the argument is - then write stdin to a tempfile and open the
# tempfile in emacs gui mode
# If the argument is -t then write stdin to a tempfile and open the
# tempfile in emacs terminal mode
if [ $# -ge 1 ] && [ "$1" = - ]; then
tempfile="$(mktemp "emacs-stdin-$USER.XXXXXXX" --tmpdir)"
cat - > "$tempfile"
_emacsstdin --eval "(find-file \"$tempfile\")" \
--eval '(set-visited-file-name nil)' \
--eval '(rename-buffer "*stdin*" t))'
elif [ $# -ge 1 ] && [ "$1" = -t ]; then
tempfile="$(mktemp "emacs-stdin-$USER.XXXXXXX" --tmpdir)"
cat - > "$tempfile"
_emacststdin --eval "(find-file \"$tempfile\")" \
--eval '(set-visited-file-name nil)' \
--eval '(rename-buffer "*stdin*" t))'
else
_emacsstdin "$@"
fi
}