Mercurial > emacs
comparison src/fileio.c @ 41151:02fcac02955b
(choose_write_coding_system): New fun, extracted from Fwrite_region.
(Fwrite_region): Use it.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Fri, 16 Nov 2001 14:49:20 +0000 |
parents | ef8a5e50dbe8 |
children | 00d37e30f901 |
comparison
equal
deleted
inserted
replaced
41150:6ce8f906931a | 41151:02fcac02955b |
---|---|
4533 Fset_buffer (buf); | 4533 Fset_buffer (buf); |
4534 Fkill_buffer (tembuf); | 4534 Fkill_buffer (tembuf); |
4535 return Qnil; | 4535 return Qnil; |
4536 } | 4536 } |
4537 | 4537 |
4538 /* Decide the coding-system to encode the data with. */ | |
4539 | |
4540 void | |
4541 choose_write_coding_system (start, end, filename, | |
4542 append, visit, lockname, coding) | |
4543 Lisp_Object start, end, filename, append, visit, lockname; | |
4544 struct coding_system *coding; | |
4545 { | |
4546 Lisp_Object val; | |
4547 | |
4548 if (auto_saving) | |
4549 val = Qnil; | |
4550 else if (!NILP (Vcoding_system_for_write)) | |
4551 val = Vcoding_system_for_write; | |
4552 else | |
4553 { | |
4554 /* If the variable `buffer-file-coding-system' is set locally, | |
4555 it means that the file was read with some kind of code | |
4556 conversion or the variable is explicitly set by users. We | |
4557 had better write it out with the same coding system even if | |
4558 `enable-multibyte-characters' is nil. | |
4559 | |
4560 If it is not set locally, we anyway have to convert EOL | |
4561 format if the default value of `buffer-file-coding-system' | |
4562 tells that it is not Unix-like (LF only) format. */ | |
4563 int using_default_coding = 0; | |
4564 int force_raw_text = 0; | |
4565 | |
4566 val = current_buffer->buffer_file_coding_system; | |
4567 if (NILP (val) | |
4568 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil))) | |
4569 { | |
4570 val = Qnil; | |
4571 if (NILP (current_buffer->enable_multibyte_characters)) | |
4572 force_raw_text = 1; | |
4573 } | |
4574 | |
4575 if (NILP (val)) | |
4576 { | |
4577 /* Check file-coding-system-alist. */ | |
4578 Lisp_Object args[7], coding_systems; | |
4579 | |
4580 args[0] = Qwrite_region; args[1] = start; args[2] = end; | |
4581 args[3] = filename; args[4] = append; args[5] = visit; | |
4582 args[6] = lockname; | |
4583 coding_systems = Ffind_operation_coding_system (7, args); | |
4584 if (CONSP (coding_systems) && !NILP (XCDR (coding_systems))) | |
4585 val = XCDR (coding_systems); | |
4586 } | |
4587 | |
4588 if (NILP (val) | |
4589 && !NILP (current_buffer->buffer_file_coding_system)) | |
4590 { | |
4591 /* If we still have not decided a coding system, use the | |
4592 default value of buffer-file-coding-system. */ | |
4593 val = current_buffer->buffer_file_coding_system; | |
4594 using_default_coding = 1; | |
4595 } | |
4596 | |
4597 if (!force_raw_text | |
4598 && !NILP (Ffboundp (Vselect_safe_coding_system_function))) | |
4599 /* Confirm that VAL can surely encode the current region. */ | |
4600 val = call3 (Vselect_safe_coding_system_function, start, end, val); | |
4601 | |
4602 setup_coding_system (Fcheck_coding_system (val), coding); | |
4603 if (coding->eol_type == CODING_EOL_UNDECIDED | |
4604 && !using_default_coding) | |
4605 { | |
4606 if (! EQ (default_buffer_file_coding.symbol, | |
4607 buffer_defaults.buffer_file_coding_system)) | |
4608 setup_coding_system (buffer_defaults.buffer_file_coding_system, | |
4609 &default_buffer_file_coding); | |
4610 if (default_buffer_file_coding.eol_type != CODING_EOL_UNDECIDED) | |
4611 { | |
4612 Lisp_Object subsidiaries; | |
4613 | |
4614 coding->eol_type = default_buffer_file_coding.eol_type; | |
4615 subsidiaries = Fget (coding->symbol, Qeol_type); | |
4616 if (VECTORP (subsidiaries) | |
4617 && XVECTOR (subsidiaries)->size == 3) | |
4618 coding->symbol | |
4619 = XVECTOR (subsidiaries)->contents[coding->eol_type]; | |
4620 } | |
4621 } | |
4622 | |
4623 if (force_raw_text) | |
4624 setup_raw_text_coding_system (coding); | |
4625 goto done_setup_coding; | |
4626 } | |
4627 | |
4628 setup_coding_system (Fcheck_coding_system (val), coding); | |
4629 | |
4630 done_setup_coding: | |
4631 if (!STRINGP (start) && !NILP (current_buffer->selective_display)) | |
4632 coding->mode |= CODING_MODE_SELECTIVE_DISPLAY; | |
4633 } | |
4634 | |
4538 DEFUN ("write-region", Fwrite_region, Swrite_region, 3, 7, | 4635 DEFUN ("write-region", Fwrite_region, Swrite_region, 3, 7, |
4539 "r\nFWrite region to file: \ni\ni\ni\np", | 4636 "r\nFWrite region to file: \ni\ni\ni\np", |
4540 doc: /* Write current region into specified file. | 4637 doc: /* Write current region into specified file. |
4541 When called from a program, takes three arguments: | 4638 When called from a program, takes three arguments: |
4542 START, END and FILENAME. START and END are buffer positions. | 4639 START, END and FILENAME. START and END are buffer positions. |
4580 #ifdef VMS | 4677 #ifdef VMS |
4581 unsigned char *fname = 0; /* If non-0, original filename (must rename) */ | 4678 unsigned char *fname = 0; /* If non-0, original filename (must rename) */ |
4582 #endif /* VMS */ | 4679 #endif /* VMS */ |
4583 Lisp_Object handler; | 4680 Lisp_Object handler; |
4584 Lisp_Object visit_file; | 4681 Lisp_Object visit_file; |
4585 Lisp_Object annotations; | 4682 Lisp_Object annotations = Qnil; |
4586 Lisp_Object encoded_filename; | 4683 Lisp_Object encoded_filename; |
4587 int visiting = (EQ (visit, Qt) || STRINGP (visit)); | 4684 int visiting = (EQ (visit, Qt) || STRINGP (visit)); |
4588 int quietly = !NILP (visit); | 4685 int quietly = !NILP (visit); |
4589 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; | 4686 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
4590 struct buffer *given_buffer; | 4687 struct buffer *given_buffer; |
4600 validate_region (&start, &end); | 4697 validate_region (&start, &end); |
4601 | 4698 |
4602 GCPRO4 (start, filename, visit, lockname); | 4699 GCPRO4 (start, filename, visit, lockname); |
4603 | 4700 |
4604 /* Decide the coding-system to encode the data with. */ | 4701 /* Decide the coding-system to encode the data with. */ |
4605 { | 4702 choose_write_coding_system (start, end, filename, |
4606 Lisp_Object val; | 4703 append, visit, lockname, &coding); |
4607 | |
4608 if (auto_saving) | |
4609 val = Qnil; | |
4610 else if (!NILP (Vcoding_system_for_write)) | |
4611 val = Vcoding_system_for_write; | |
4612 else | |
4613 { | |
4614 /* If the variable `buffer-file-coding-system' is set locally, | |
4615 it means that the file was read with some kind of code | |
4616 conversion or the variable is explicitly set by users. We | |
4617 had better write it out with the same coding system even if | |
4618 `enable-multibyte-characters' is nil. | |
4619 | |
4620 If it is not set locally, we anyway have to convert EOL | |
4621 format if the default value of `buffer-file-coding-system' | |
4622 tells that it is not Unix-like (LF only) format. */ | |
4623 int using_default_coding = 0; | |
4624 int force_raw_text = 0; | |
4625 | |
4626 val = current_buffer->buffer_file_coding_system; | |
4627 if (NILP (val) | |
4628 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil))) | |
4629 { | |
4630 val = Qnil; | |
4631 if (NILP (current_buffer->enable_multibyte_characters)) | |
4632 force_raw_text = 1; | |
4633 } | |
4634 | |
4635 if (NILP (val)) | |
4636 { | |
4637 /* Check file-coding-system-alist. */ | |
4638 Lisp_Object args[7], coding_systems; | |
4639 | |
4640 args[0] = Qwrite_region; args[1] = start; args[2] = end; | |
4641 args[3] = filename; args[4] = append; args[5] = visit; | |
4642 args[6] = lockname; | |
4643 coding_systems = Ffind_operation_coding_system (7, args); | |
4644 if (CONSP (coding_systems) && !NILP (XCDR (coding_systems))) | |
4645 val = XCDR (coding_systems); | |
4646 } | |
4647 | |
4648 if (NILP (val) | |
4649 && !NILP (current_buffer->buffer_file_coding_system)) | |
4650 { | |
4651 /* If we still have not decided a coding system, use the | |
4652 default value of buffer-file-coding-system. */ | |
4653 val = current_buffer->buffer_file_coding_system; | |
4654 using_default_coding = 1; | |
4655 } | |
4656 | |
4657 if (!force_raw_text | |
4658 && !NILP (Ffboundp (Vselect_safe_coding_system_function))) | |
4659 /* Confirm that VAL can surely encode the current region. */ | |
4660 val = call3 (Vselect_safe_coding_system_function, start, end, val); | |
4661 | |
4662 setup_coding_system (Fcheck_coding_system (val), &coding); | |
4663 if (coding.eol_type == CODING_EOL_UNDECIDED | |
4664 && !using_default_coding) | |
4665 { | |
4666 if (! EQ (default_buffer_file_coding.symbol, | |
4667 buffer_defaults.buffer_file_coding_system)) | |
4668 setup_coding_system (buffer_defaults.buffer_file_coding_system, | |
4669 &default_buffer_file_coding); | |
4670 if (default_buffer_file_coding.eol_type != CODING_EOL_UNDECIDED) | |
4671 { | |
4672 Lisp_Object subsidiaries; | |
4673 | |
4674 coding.eol_type = default_buffer_file_coding.eol_type; | |
4675 subsidiaries = Fget (coding.symbol, Qeol_type); | |
4676 if (VECTORP (subsidiaries) | |
4677 && XVECTOR (subsidiaries)->size == 3) | |
4678 coding.symbol | |
4679 = XVECTOR (subsidiaries)->contents[coding.eol_type]; | |
4680 } | |
4681 } | |
4682 | |
4683 if (force_raw_text) | |
4684 setup_raw_text_coding_system (&coding); | |
4685 goto done_setup_coding; | |
4686 } | |
4687 | |
4688 setup_coding_system (Fcheck_coding_system (val), &coding); | |
4689 | |
4690 done_setup_coding: | |
4691 if (!STRINGP (start) && !NILP (current_buffer->selective_display)) | |
4692 coding.mode |= CODING_MODE_SELECTIVE_DISPLAY; | |
4693 } | |
4694 | |
4695 Vlast_coding_system_used = coding.symbol; | 4704 Vlast_coding_system_used = coding.symbol; |
4696 | 4705 |
4697 filename = Fexpand_file_name (filename, Qnil); | 4706 filename = Fexpand_file_name (filename, Qnil); |
4698 | 4707 |
4699 if (! NILP (mustbenew) && !EQ (mustbenew, Qexcl)) | 4708 if (! NILP (mustbenew) && !EQ (mustbenew, Qexcl)) |