-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpipe-to-emacs-server.sh
executable file
·200 lines (154 loc) · 7.55 KB
/
pipe-to-emacs-server.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
declare -r SCRIPT_NAME="${BASH_SOURCE[0]##*/}" # This script's filename only, without any path components.
declare -r VERSION_NUMBER="2.05"
declare -r -i EXIT_CODE_SUCCESS=0
declare -r -i EXIT_CODE_ERROR=1
abort ()
{
echo >&2 && echo "Error in script \"$SCRIPT_NAME\": $*" >&2
exit $EXIT_CODE_ERROR
}
is_var_set ()
{
if [ "${!1-first}" == "${!1-second}" ]; then return 0; else return 1; fi
}
declare -r EMACS_BASE_PATH_ENV_VAR_NAME="EMACS_BASE_PATH"
declare -r PIPE_TO_EMACS_ENV_VAR_NAME="PIPE_TO_EMACS_FUNCNAME"
display_help ()
{
echo
echo "$SCRIPT_NAME version $VERSION_NUMBER"
echo "Copyright (c) 2011-2022 R. Diez - Licensed under the GNU AGPLv3"
echo "Based on a similar utility by Phil Jackson ([email protected])"
echo
echo "This tool helps you pipe the output of a shell console command to a new Emacs window."
echo
echo "The Emacs instance receiving the text must already be running in the local PC, and must have started the Emacs server, as this script uses the 'emacsclient' tool. See Emacs' function 'server-start' for details. I tried to implement this script so that it would start Emacs automatically if not already there, but I could not find a clean solution. See this script's source code for more information. The reason why the Emacs server must be running locally is that the generated lisp code needs to open a local temporary file where the piped text is stored."
echo
echo "If you Emacs is not on the PATH, se environment variable $EMACS_BASE_PATH_ENV_VAR_NAME."
echo
echo "If you are running on Cygwin and want to use the native Windows Emacs (the Win32 version instead of the Cygwin one), set environment variable PIPETOEMACS_WIN32_PATH to point to your Emacs binaries. For example:"
echo " export PIPETOEMACS_WIN32_PATH=\"c:/emacs-24.3\""
echo
echo "Usage examples:"
echo " ls -la | $SCRIPT_NAME"
echo " my-program 2>&1 | $SCRIPT_NAME # Include output to stderr too."
echo
echo "You can also specify one of the following options:"
echo " --help displays this help text"
echo " --version displays the tool's version number (currently $VERSION_NUMBER)"
echo " --license prints license information"
echo
echo "Normally, this script automatically builds Lisp code to load a temporary file with the piped text."
echo "But if environment variable $PIPE_TO_EMACS_ENV_VAR_NAME exists, then this script takes its value"
echo "as the name of a Lisp function to call which takes one argument with the temporary filename."
echo "You must provide that function in your Emacs configuration."
echo
echo "Exit status: 0 means success, anything else is an error."
echo
echo "Feedback: Please send feedback to rdiezmail-tools at yahoo.de"
echo
}
display_license()
{
cat - <<EOF
Copyright (c) 2011-2022 R. Diez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3 as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License version 3 for more details.
You should have received a copy of the GNU Affero General Public License version 3
along with this program. If not, see L<http://www.gnu.org/licenses/>.
EOF
}
if (( $# != 0 )); then
case "$1" in
--help)
display_help
exit $EXIT_CODE_SUCCESS;;
--license)
display_license
exit $EXIT_CODE_SUCCESS;;
--version)
echo "$VERSION_NUMBER"
exit $EXIT_CODE_SUCCESS;;
--*) abort "Unknown option \"$1\".";;
*) abort "This tool takes no arguments. Run this tool with the --help option for usage information.";;
esac
fi
# If the user starts the tool from a terminal without piping any input...
if [ -t 0 ]
then
abort "This tool is designed to take text data from stdin, by using a pipe to forward data from another process. Run this tool with the --help option for usage information."
fi
TMP_FILENAME="$(mktemp --tmpdir "tmp.$SCRIPT_NAME.XXXXXXXXXX.txt")"
# If variable PIPETOEMACS_WIN32_PATH is set...
if [ "${PIPETOEMACS_WIN32_PATH:=""}" != "" ]; then
TMP_FILENAME_FOR_EMACS_LISP="$(cygpath --mixed -- "$TMP_FILENAME")"
EMACSCLIENT="$PIPETOEMACS_WIN32_PATH/bin/emacsclient"
else
TMP_FILENAME_FOR_EMACS_LISP="$TMP_FILENAME"
if is_var_set "$EMACS_BASE_PATH_ENV_VAR_NAME"; then
declare -r EMACSCLIENT="${!EMACS_BASE_PATH_ENV_VAR_NAME}/bin/emacsclient"
else
declare -r EMACSCLIENT="emacsclient"
fi
fi
cat - > "$TMP_FILENAME"
if is_var_set "$PIPE_TO_EMACS_ENV_VAR_NAME"; then
LISP_CODE="(${!PIPE_TO_EMACS_ENV_VAR_NAME} \"$TMP_FILENAME_FOR_EMACS_LISP\")"
else
LISP_CODE="(progn "
# Emacs function 'switch-to-buffer' replaces the current window (pane) with the piped contents.
# There is advice on the Internet about using 'pop-to-buffer-same-window' for
# this purpose instead, but I did not understand the reason why.
#
# In any case, I find that behaviour annoying, because I usually run this script from within a shell window
# inside Emacs, and I would like to keep that shell visible. This is why I have switched
# to using 'pop-to-buffer'.
declare -r WINDOW_FUNCTION="pop-to-buffer"
LISP_CODE+="($WINDOW_FUNCTION (generate-new-buffer \"*stdin*\"))"
# As of january 2014, the Cygwin console seems to using UTF-8. If you pipe text to Emacs, international characters
# will probably not work. Forcing UTF-8 here seems to do the trick. On Linux, everything should be UTF-8 anyway.
LISP_CODE+="(let ((coding-system-for-read 'utf-8))"
LISP_CODE+="(insert-file-contents \"$TMP_FILENAME_FOR_EMACS_LISP\")"
LISP_CODE+=")"
LISP_CODE+="(goto-char (point-max))"
# This does not seem necessary:
# LISP_CODE+="(select-frame-set-input-focus (window-frame (selected-window)))"
LISP_CODE+=")"
fi
# About why an Emacs server instance must already be running:
#
# The 'emacsclient' tool has an '--alternate-editor' argument that can start a new Emacs instance
# if an existing one is not reachable over the server socket. The trouble is, as of version 24.3,
# the new Emacs instance is not started with the --eval argument, so this script breaks.
#
# On this web page I found the following workaround:
# http://www.emacswiki.org/emacs/EmacsClient
# If you start emacsclient with argument '--alternate-editor="/usr/bin/false"', it will fail,
# and then you can start Emacs with the --eval argument. Caveats are:
# - emacsclient prints ugly error messages.
# - emacsclient does not document that its exit code will indicate a failure if --alternate-editor fails.
# - There is no way to tell whether something else failed.
# - Starting "emacs --eval" is problematic. This script would then wait forever for the new Emacs instance to terminate.
# If Emacs were to be started in the background, there is no way to find out if it failed.
# After considering all the options, I decide to keep this script clean and simple, at the cost
# of demanding an existing Emacs server. For serious Emacs users, that is the most common scenario anyway.
set +o errexit
"$EMACSCLIENT" -e "$LISP_CODE" >/dev/null
declare -r -i EMACS_CLIENT_EXIT_CODE="$?"
set -o errexit
# Alternative: Let Emacs delete the file with (delete-file "filename"). But beware that we would need
# to delete it here anyway if emacsclient fails.
rm -- "$TMP_FILENAME"
if (( EMACS_CLIENT_EXIT_CODE != 0 )); then
# abort "emacsclient failed with exit code $EMACS_CLIENT_EXIT_CODE."
exit $EMACS_CLIENT_EXIT_CODE
fi