summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKenichi Handa <handa@m17n.org>1997-08-22 01:19:27 +0000
committerKenichi Handa <handa@m17n.org>1997-08-22 01:19:27 +0000
commit7257664ecf772472913ec653b9a737545c0316a3 (patch)
treeabc7674996aeb951dce353c5aca2672f96a59b5b /src
parent066c2b9df2665fdfef87add87c7483582673d94f (diff)
downloademacs-7257664ecf772472913ec653b9a737545c0316a3.tar.gz
(Vauto_file_coding_system_function): New variable.
(Finsert_file_contents): Decide coding system after opening a file. Call functions set in Vauto_file_coding_system_function. (syms_of_fileio): Declare auto-file-coding-system as a Lisp variable.
Diffstat (limited to 'src')
-rw-r--r--src/fileio.c77
1 files changed, 57 insertions, 20 deletions
diff --git a/src/fileio.c b/src/fileio.c
index f42fad3bec7..441e86dee26 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -169,6 +169,9 @@ Lisp_Object Vauto_save_file_format;
/* Lisp functions for translating file formats */
Lisp_Object Qformat_decode, Qformat_annotate_function;
+/* Function to be called to decide a coding system of a reading file. */
+Lisp_Object Vauto_file_coding_system_function;
+
/* Functions to be called to process text properties in inserted file. */
Lisp_Object Vafter_insert_file_functions;
@@ -3063,26 +3066,6 @@ This does code conversion according to the value of\n\
goto handled;
}
- /* Decide the coding-system of the file. */
- {
- Lisp_Object val;
-
- if (!NILP (Vcoding_system_for_read))
- val = Vcoding_system_for_read;
- else if (NILP (current_buffer->enable_multibyte_characters))
- val = Qemacs_mule;
- else
- {
- Lisp_Object args[6], coding_systems;
-
- args[0] = Qinsert_file_contents, args[1] = filename, args[2] = visit,
- args[3] = beg, args[4] = end, args[5] = replace;
- coding_systems = Ffind_operation_coding_system (6, args);
- val = CONSP (coding_systems) ? XCONS (coding_systems)->car : Qnil;
- }
- setup_coding_system (Fcheck_coding_system (val), &coding);
- }
-
fd = -1;
#ifndef APOLLO
@@ -3154,6 +3137,50 @@ This does code conversion according to the value of\n\
}
}
+ /* Decide the coding-system of the file. */
+ {
+ Lisp_Object val = Qnil;
+
+ if (!NILP (Vcoding_system_for_read))
+ val = Vcoding_system_for_read;
+ else if (NILP (current_buffer->enable_multibyte_characters))
+ val = Qemacs_mule;
+ else
+ {
+ if (SYMBOLP (Vauto_file_coding_system_function)
+ && Fboundp (Vauto_file_coding_system_function))
+ {
+ /* Find a coding system specified in a few lines at the
+ head of the file. We assume that the fist 1K bytes is
+ sufficient fot this purpose. */
+ int nread = read (fd, read_buf, 1024);
+
+ if (nread < 0)
+ error ("IO error reading %s: %s",
+ XSTRING (filename)->data, strerror (errno));
+ else if (nread > 0)
+ {
+ val = call1 (Vauto_file_coding_system_function,
+ make_string (read_buf, nread));
+ /* Rewind the file for the actual read done later. */
+ if (lseek (fd, 0, 0) < 0)
+ report_file_error ("Setting file position",
+ Fcons (filename, Qnil));
+ }
+ }
+ if (NILP (val))
+ {
+ Lisp_Object args[6], coding_systems;
+
+ args[0] = Qinsert_file_contents, args[1] = filename,
+ args[2] = visit, args[3] = beg, args[4] = end, args[5] = replace;
+ coding_systems = Ffind_operation_coding_system (6, args);
+ if (CONSP (coding_systems)) val = XCONS (coding_systems)->car;
+ }
+ }
+ setup_coding_system (Fcheck_coding_system (val), &coding);
+ }
+
/* If requested, replace the accessible part of the buffer
with the file contents. Avoid replacing text at the
beginning or end of the buffer that matches the file contents;
@@ -5126,6 +5153,16 @@ The function `find-file-name-handler' checks this list for a handler\n\
for its argument.");
Vfile_name_handler_alist = Qnil;
+ DEFVAR_LISP ("auto-file-coding-system-function",
+ &Vauto_file_coding_system_function,
+ "If non-nil, a function to call to decide a coding system of file.
+One argument is passed to this function: the string of the first
+few lines of a file to be read.
+This function should return a coding system to decode the file contents
+specified in the heading lines with the format:
+ -*- ... coding: CODING-SYSTEM; ... -*-");
+ Vauto_file_coding_system_function = Qnil;
+
DEFVAR_LISP ("after-insert-file-functions", &Vafter_insert_file_functions,
"A list of functions to be called at the end of `insert-file-contents'.\n\
Each is passed one argument, the number of bytes inserted. It should return\n\