diff options
| author | Kenichi Handa | 1997-08-22 01:19:27 +0000 |
|---|---|---|
| committer | Kenichi Handa | 1997-08-22 01:19:27 +0000 |
| commit | c9e8239214363ba7afb352a8a50ca3656b9d3b6d (patch) | |
| tree | 3f3131073ba0e3391c60eedec876c3279959083a /src | |
| parent | 6b0efe7393e75bccf715f7248409749b43407916 (diff) | |
| download | emacs-c9e8239214363ba7afb352a8a50ca3656b9d3b6d.tar.gz emacs-c9e8239214363ba7afb352a8a50ca3656b9d3b6d.zip | |
(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.c | 77 |
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; | |||
| 169 | /* Lisp functions for translating file formats */ | 169 | /* Lisp functions for translating file formats */ |
| 170 | Lisp_Object Qformat_decode, Qformat_annotate_function; | 170 | Lisp_Object Qformat_decode, Qformat_annotate_function; |
| 171 | 171 | ||
| 172 | /* Function to be called to decide a coding system of a reading file. */ | ||
| 173 | Lisp_Object Vauto_file_coding_system_function; | ||
| 174 | |||
| 172 | /* Functions to be called to process text properties in inserted file. */ | 175 | /* Functions to be called to process text properties in inserted file. */ |
| 173 | Lisp_Object Vafter_insert_file_functions; | 176 | Lisp_Object Vafter_insert_file_functions; |
| 174 | 177 | ||
| @@ -3063,26 +3066,6 @@ This does code conversion according to the value of\n\ | |||
| 3063 | goto handled; | 3066 | goto handled; |
| 3064 | } | 3067 | } |
| 3065 | 3068 | ||
| 3066 | /* Decide the coding-system of the file. */ | ||
| 3067 | { | ||
| 3068 | Lisp_Object val; | ||
| 3069 | |||
| 3070 | if (!NILP (Vcoding_system_for_read)) | ||
| 3071 | val = Vcoding_system_for_read; | ||
| 3072 | else if (NILP (current_buffer->enable_multibyte_characters)) | ||
| 3073 | val = Qemacs_mule; | ||
| 3074 | else | ||
| 3075 | { | ||
| 3076 | Lisp_Object args[6], coding_systems; | ||
| 3077 | |||
| 3078 | args[0] = Qinsert_file_contents, args[1] = filename, args[2] = visit, | ||
| 3079 | args[3] = beg, args[4] = end, args[5] = replace; | ||
| 3080 | coding_systems = Ffind_operation_coding_system (6, args); | ||
| 3081 | val = CONSP (coding_systems) ? XCONS (coding_systems)->car : Qnil; | ||
| 3082 | } | ||
| 3083 | setup_coding_system (Fcheck_coding_system (val), &coding); | ||
| 3084 | } | ||
| 3085 | |||
| 3086 | fd = -1; | 3069 | fd = -1; |
| 3087 | 3070 | ||
| 3088 | #ifndef APOLLO | 3071 | #ifndef APOLLO |
| @@ -3154,6 +3137,50 @@ This does code conversion according to the value of\n\ | |||
| 3154 | } | 3137 | } |
| 3155 | } | 3138 | } |
| 3156 | 3139 | ||
| 3140 | /* Decide the coding-system of the file. */ | ||
| 3141 | { | ||
| 3142 | Lisp_Object val = Qnil; | ||
| 3143 | |||
| 3144 | if (!NILP (Vcoding_system_for_read)) | ||
| 3145 | val = Vcoding_system_for_read; | ||
| 3146 | else if (NILP (current_buffer->enable_multibyte_characters)) | ||
| 3147 | val = Qemacs_mule; | ||
| 3148 | else | ||
| 3149 | { | ||
| 3150 | if (SYMBOLP (Vauto_file_coding_system_function) | ||
| 3151 | && Fboundp (Vauto_file_coding_system_function)) | ||
| 3152 | { | ||
| 3153 | /* Find a coding system specified in a few lines at the | ||
| 3154 | head of the file. We assume that the fist 1K bytes is | ||
| 3155 | sufficient fot this purpose. */ | ||
| 3156 | int nread = read (fd, read_buf, 1024); | ||
| 3157 | |||
| 3158 | if (nread < 0) | ||
| 3159 | error ("IO error reading %s: %s", | ||
| 3160 | XSTRING (filename)->data, strerror (errno)); | ||
| 3161 | else if (nread > 0) | ||
| 3162 | { | ||
| 3163 | val = call1 (Vauto_file_coding_system_function, | ||
| 3164 | make_string (read_buf, nread)); | ||
| 3165 | /* Rewind the file for the actual read done later. */ | ||
| 3166 | if (lseek (fd, 0, 0) < 0) | ||
| 3167 | report_file_error ("Setting file position", | ||
| 3168 | Fcons (filename, Qnil)); | ||
| 3169 | } | ||
| 3170 | } | ||
| 3171 | if (NILP (val)) | ||
| 3172 | { | ||
| 3173 | Lisp_Object args[6], coding_systems; | ||
| 3174 | |||
| 3175 | args[0] = Qinsert_file_contents, args[1] = filename, | ||
| 3176 | args[2] = visit, args[3] = beg, args[4] = end, args[5] = replace; | ||
| 3177 | coding_systems = Ffind_operation_coding_system (6, args); | ||
| 3178 | if (CONSP (coding_systems)) val = XCONS (coding_systems)->car; | ||
| 3179 | } | ||
| 3180 | } | ||
| 3181 | setup_coding_system (Fcheck_coding_system (val), &coding); | ||
| 3182 | } | ||
| 3183 | |||
| 3157 | /* If requested, replace the accessible part of the buffer | 3184 | /* If requested, replace the accessible part of the buffer |
| 3158 | with the file contents. Avoid replacing text at the | 3185 | with the file contents. Avoid replacing text at the |
| 3159 | beginning or end of the buffer that matches the file contents; | 3186 | 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\ | |||
| 5126 | for its argument."); | 5153 | for its argument."); |
| 5127 | Vfile_name_handler_alist = Qnil; | 5154 | Vfile_name_handler_alist = Qnil; |
| 5128 | 5155 | ||
| 5156 | DEFVAR_LISP ("auto-file-coding-system-function", | ||
| 5157 | &Vauto_file_coding_system_function, | ||
| 5158 | "If non-nil, a function to call to decide a coding system of file. | ||
| 5159 | One argument is passed to this function: the string of the first | ||
| 5160 | few lines of a file to be read. | ||
| 5161 | This function should return a coding system to decode the file contents | ||
| 5162 | specified in the heading lines with the format: | ||
| 5163 | -*- ... coding: CODING-SYSTEM; ... -*-"); | ||
| 5164 | Vauto_file_coding_system_function = Qnil; | ||
| 5165 | |||
| 5129 | DEFVAR_LISP ("after-insert-file-functions", &Vafter_insert_file_functions, | 5166 | DEFVAR_LISP ("after-insert-file-functions", &Vafter_insert_file_functions, |
| 5130 | "A list of functions to be called at the end of `insert-file-contents'.\n\ | 5167 | "A list of functions to be called at the end of `insert-file-contents'.\n\ |
| 5131 | Each is passed one argument, the number of bytes inserted. It should return\n\ | 5168 | Each is passed one argument, the number of bytes inserted. It should return\n\ |