aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1998-05-13 06:47:40 +0000
committerRichard M. Stallman1998-05-13 06:47:40 +0000
commitfeb9dc27d7e1f2ddc4a4eef84937f701e18e8445 (patch)
tree58e02876c30f6de1c5a62e5153e10cd45c940301
parent74cf0beb862b3ffc6478c8ff409b138e788af561 (diff)
downloademacs-feb9dc27d7e1f2ddc4a4eef84937f701e18e8445.tar.gz
emacs-feb9dc27d7e1f2ddc4a4eef84937f701e18e8445.zip
(Finsert_file_contents): Check that a -*- coding: -*- spec
or a local variables list exists, before Vset_auto_coding_function.
-rw-r--r--src/fileio.c93
1 files changed, 85 insertions, 8 deletions
diff --git a/src/fileio.c b/src/fileio.c
index f9e0b93c73f..e83548244aa 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -3259,7 +3259,7 @@ This does code conversion according to the value of\n\
3259 Lisp_Object p; 3259 Lisp_Object p;
3260 int total; 3260 int total;
3261 int not_regular = 0; 3261 int not_regular = 0;
3262 char read_buf[READ_BUF_SIZE]; 3262 unsigned char read_buf[READ_BUF_SIZE];
3263 struct coding_system coding; 3263 struct coding_system coding;
3264 unsigned char buffer[1 << 14]; 3264 unsigned char buffer[1 << 14];
3265 int replace_handled = 0; 3265 int replace_handled = 0;
@@ -3372,6 +3372,10 @@ This does code conversion according to the value of\n\
3372 3372
3373 if (!NILP (Vcoding_system_for_read)) 3373 if (!NILP (Vcoding_system_for_read))
3374 val = Vcoding_system_for_read; 3374 val = Vcoding_system_for_read;
3375 else if (! NILP (replace))
3376 /* In REPLACE mode, we can use the same coding system
3377 that was used to visit the file. */
3378 val = current_buffer->buffer_file_coding_system;
3375 else 3379 else
3376 { 3380 {
3377 if (! NILP (Vset_auto_coding_function)) 3381 if (! NILP (Vset_auto_coding_function))
@@ -3380,13 +3384,20 @@ This does code conversion according to the value of\n\
3380 or in the tailing several lines of the file. We assume 3384 or in the tailing several lines of the file. We assume
3381 that the 1K-byte and 3K-byte for heading and tailing 3385 that the 1K-byte and 3K-byte for heading and tailing
3382 respectively are sufficient fot this purpose. */ 3386 respectively are sufficient fot this purpose. */
3383 int how_many, nread; 3387 int nread;
3388 int beginning_of_end, end_of_beginning;
3384 3389
3385 if (st.st_size <= (1024 * 4)) 3390 if (st.st_size <= (1024 * 4))
3386 nread = read (fd, read_buf, 1024 * 4); 3391 {
3392 nread = read (fd, read_buf, 1024 * 4);
3393 end_of_beginning = nread;
3394 beginning_of_end = 0;
3395 }
3387 else 3396 else
3388 { 3397 {
3389 nread = read (fd, read_buf, 1024); 3398 nread = read (fd, read_buf, 1024);
3399 end_of_beginning = nread;
3400 beginning_of_end = nread;
3390 if (nread >= 0) 3401 if (nread >= 0)
3391 { 3402 {
3392 if (lseek (fd, st.st_size - (1024 * 3), 0) < 0) 3403 if (lseek (fd, st.st_size - (1024 * 3), 0) < 0)
@@ -3395,17 +3406,83 @@ This does code conversion according to the value of\n\
3395 nread += read (fd, read_buf + nread, 1024 * 3); 3406 nread += read (fd, read_buf + nread, 1024 * 3);
3396 } 3407 }
3397 } 3408 }
3398 3409
3399 if (nread < 0) 3410 if (nread < 0)
3400 error ("IO error reading %s: %s", 3411 error ("IO error reading %s: %s",
3401 XSTRING (orig_filename)->data, strerror (errno)); 3412 XSTRING (orig_filename)->data, strerror (errno));
3402 else if (nread > 0) 3413 else if (nread > 0)
3403 { 3414 {
3415 int i;
3416 int possible_spec = 0;
3417 unsigned char *p, *p1;
3404 Lisp_Object tem; 3418 Lisp_Object tem;
3405 /* Always make this a unibyte string 3419 unsigned char *copy = (unsigned char *) alloca (nread + 1);
3406 because we have not yet decoded it. */ 3420
3407 tem = make_unibyte_string (read_buf, nread); 3421 /* Make a copy of the contents of read_buf in COPY,
3408 val = call1 (Vset_auto_coding_function, tem); 3422 and convert it to lower case so we can compare
3423 more efficiently. */
3424 bcopy (read_buf, copy, nread);
3425 for (i = 0; i < nread; i++)
3426 copy[i] = DOWNCASE (copy[i]);
3427 /* Ensure various comparisons fail at end of data. */
3428 copy[nread] = 0;
3429
3430 /* Now test quickly whether the file contains a -*- line. */
3431 p = copy;
3432 while (*p != '\n' && p - copy < end_of_beginning)
3433 p++;
3434 if (copy[0] == '#' && copy[1] == '!')
3435 while (*p != '\n' && p - copy < end_of_beginning)
3436 p++;
3437 p1 = copy;
3438 while (p - p1 >= 3)
3439 {
3440 if (p1[0] == '-' && p1[1] == '*' && p1[2] == '-')
3441 {
3442 while (p - p1 >= 7)
3443 {
3444 if (! bcmp ("coding:", p1, 7))
3445 {
3446 possible_spec = 1;
3447 goto win;
3448 }
3449 p1++;
3450 }
3451 break;
3452 }
3453 p1++;
3454 }
3455
3456 /* Test quickly whether the file
3457 contains a local variables list. */
3458 p = &copy[nread - 1];
3459 p1 = &copy[beginning_of_end];
3460 while (p > p1)
3461 {
3462 if (p[0] == '\n' && p[1] == '\f')
3463 break;
3464 p--;
3465 }
3466 p1 = &copy[nread];
3467 while (p1 - p >= 16)
3468 {
3469 if (! bcmp ("local variables:", p, 16))
3470 {
3471 possible_spec = 1;
3472 break;
3473 }
3474 p++;
3475 }
3476 win:
3477
3478 if (possible_spec)
3479 {
3480 /* Always make this a unibyte string
3481 because we have not yet decoded it. */
3482 tem = make_unibyte_string (read_buf, nread);
3483 val = call1 (Vset_auto_coding_function, tem);
3484 }
3485
3409 /* Rewind the file for the actual read done later. */ 3486 /* Rewind the file for the actual read done later. */
3410 if (lseek (fd, 0, 0) < 0) 3487 if (lseek (fd, 0, 0) < 0)
3411 report_file_error ("Setting file position", 3488 report_file_error ("Setting file position",