aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEli Zaretskii2016-01-06 20:25:45 +0200
committerEli Zaretskii2016-01-06 20:25:45 +0200
commitc63246628461f748d66a8a07ba008de2e00fd33a (patch)
tree6a58d3db5f15daf1c9566cb91c4c2a897051a3d2
parent2f32cb547feb0864eae9345b50a1119decca28c0 (diff)
downloademacs-c63246628461f748d66a8a07ba008de2e00fd33a.tar.gz
emacs-c63246628461f748d66a8a07ba008de2e00fd33a.zip
Obey coding-system-for-write when writing stdout/stderr in batch
* src/print.c (printchar_to_stream): * src/xdisp.c (message_to_stderr): If coding-system-for-write has a non-nil value, use it to encode output in preference to locale-coding-system. See the discussions in http://lists.gnu.org/archive/html/emacs-devel/2016-01/msg00048.html for the details. * doc/lispref/os.texi (Terminal Output): Document how to send non-ASCII text via 'send-string-to-terminal'. (Batch Mode): Document how text written to standard streams is encoded. Fix inaccuracy regarding which output streams are used by output functions in batch mode.
-rw-r--r--doc/lispref/os.texi24
-rw-r--r--src/print.c12
-rw-r--r--src/xdisp.c11
3 files changed, 38 insertions, 9 deletions
diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi
index 969c1d6e1ea..7206cd4ef86 100644
--- a/doc/lispref/os.texi
+++ b/doc/lispref/os.texi
@@ -2085,6 +2085,8 @@ than optimal. To fix the problem, set @code{baud-rate}.
2085@defun send-string-to-terminal string &optional terminal 2085@defun send-string-to-terminal string &optional terminal
2086This function sends @var{string} to @var{terminal} without alteration. 2086This function sends @var{string} to @var{terminal} without alteration.
2087Control characters in @var{string} have terminal-dependent effects. 2087Control characters in @var{string} have terminal-dependent effects.
2088(If you need to display non-ASCII text on the terminal, encode it
2089using one of the functions described in @ref{Explicit Encoding}.)
2088This function operates only on text terminals. @var{terminal} may be 2090This function operates only on text terminals. @var{terminal} may be
2089a terminal object, a frame, or @code{nil} for the selected frame's 2091a terminal object, a frame, or @code{nil} for the selected frame's
2090terminal. In batch mode, @var{string} is sent to @code{stdout} when 2092terminal. In batch mode, @var{string} is sent to @code{stdout} when
@@ -2252,13 +2254,21 @@ loads the library named @var{file}, or @samp{-f @var{function}}, which
2252calls @var{function} with no arguments, or @samp{--eval @var{form}}. 2254calls @var{function} with no arguments, or @samp{--eval @var{form}}.
2253 2255
2254 Any Lisp program output that would normally go to the echo area, 2256 Any Lisp program output that would normally go to the echo area,
2255either using @code{message}, or using @code{prin1}, etc., with @code{t} 2257either using @code{message}, or using @code{prin1}, etc., with
2256as the stream, goes instead to Emacs's standard error descriptor when 2258@code{t} as the stream, goes instead to Emacs's standard descriptors
2257in batch mode. Similarly, input that would normally come from the 2259when in batch mode: @code{message} writes to the standard error
2258minibuffer is read from the standard input descriptor. 2260descriptor, while @code{prin1} and other print functions write to the
2259Thus, Emacs behaves much like a noninteractive 2261standard output. Similarly, input that would normally come from the
2260application program. (The echo area output that Emacs itself normally 2262minibuffer is read from the standard input descriptor. Thus, Emacs
2261generates, such as command echoing, is suppressed entirely.) 2263behaves much like a noninteractive application program. (The echo
2264area output that Emacs itself normally generates, such as command
2265echoing, is suppressed entirely.)
2266
2267Non-ASCII text written to the standard output or error descriptors is
2268by default encoded using @code{locale-coding-system} (@pxref{Locales})
2269if it is non-@code{nil}; this can be overridden by binding
2270@code{coding-system-for-write} to a coding system of you choice
2271(@pxref{Explicit Encoding}).
2262 2272
2263@defvar noninteractive 2273@defvar noninteractive
2264This variable is non-@code{nil} when Emacs is running in batch mode. 2274This variable is non-@code{nil} when Emacs is running in batch mode.
diff --git a/src/print.c b/src/print.c
index 975675014d9..269d8f250e2 100644
--- a/src/print.c
+++ b/src/print.c
@@ -200,6 +200,13 @@ printchar_to_stream (unsigned int ch, FILE *stream)
200{ 200{
201 Lisp_Object dv IF_LINT (= Qnil); 201 Lisp_Object dv IF_LINT (= Qnil);
202 ptrdiff_t i = 0, n = 1; 202 ptrdiff_t i = 0, n = 1;
203 Lisp_Object coding_system = Vlocale_coding_system;
204 bool encode_p = false;
205
206 if (!NILP (Vcoding_system_for_write))
207 coding_system = Vcoding_system_for_write;
208 if (!NILP (coding_system))
209 encode_p = true;
203 210
204 if (CHAR_VALID_P (ch) && DISP_TABLE_P (Vstandard_display_table)) 211 if (CHAR_VALID_P (ch) && DISP_TABLE_P (Vstandard_display_table))
205 { 212 {
@@ -228,8 +235,11 @@ printchar_to_stream (unsigned int ch, FILE *stream)
228 unsigned char mbstr[MAX_MULTIBYTE_LENGTH]; 235 unsigned char mbstr[MAX_MULTIBYTE_LENGTH];
229 int len = CHAR_STRING (ch, mbstr); 236 int len = CHAR_STRING (ch, mbstr);
230 Lisp_Object encoded_ch = 237 Lisp_Object encoded_ch =
231 ENCODE_SYSTEM (make_multibyte_string ((char *) mbstr, 1, len)); 238 make_multibyte_string ((char *) mbstr, 1, len);
232 239
240 if (encode_p)
241 encoded_ch = code_convert_string_norecord (encoded_ch,
242 coding_system, true);
233 fwrite (SSDATA (encoded_ch), 1, SBYTES (encoded_ch), stream); 243 fwrite (SSDATA (encoded_ch), 1, SBYTES (encoded_ch), stream);
234#ifdef WINDOWSNT 244#ifdef WINDOWSNT
235 if (print_output_debug_flag && stream == stderr) 245 if (print_output_debug_flag && stream == stderr)
diff --git a/src/xdisp.c b/src/xdisp.c
index b18bfd0d49d..ee748bd8680 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -10206,7 +10206,16 @@ message_to_stderr (Lisp_Object m)
10206 } 10206 }
10207 if (STRINGP (m)) 10207 if (STRINGP (m))
10208 { 10208 {
10209 Lisp_Object s = ENCODE_SYSTEM (m); 10209 Lisp_Object coding_system = Vlocale_coding_system;
10210 Lisp_Object s;
10211
10212 if (!NILP (Vcoding_system_for_write))
10213 coding_system = Vcoding_system_for_write;
10214 if (!NILP (coding_system))
10215 s = code_convert_string_norecord (m, coding_system, true);
10216 else
10217 s = m;
10218
10210 fwrite (SDATA (s), SBYTES (s), 1, stderr); 10219 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10211 } 10220 }
10212 if (!cursor_in_echo_area) 10221 if (!cursor_in_echo_area)