Mercurial > emacs
comparison src/coding.c @ 20556:8f6f64cc1f08
(code_convert_region): Handle bytepos and charpos.
(Fdetect_coding_region): Likewise.
| author | Richard M. Stallman <rms@gnu.org> |
|---|---|
| date | Thu, 01 Jan 1998 07:05:26 +0000 |
| parents | 9960d1196c09 |
| children | b382c9ca6c39 |
comparison
equal
deleted
inserted
replaced
| 20555:80a546059cbd | 20556:8f6f64cc1f08 |
|---|---|
| 3196 Lisp_Object b, e; | 3196 Lisp_Object b, e; |
| 3197 { | 3197 { |
| 3198 int coding_mask, eol_type; | 3198 int coding_mask, eol_type; |
| 3199 Lisp_Object val; | 3199 Lisp_Object val; |
| 3200 int beg, end; | 3200 int beg, end; |
| 3201 int beg_byte, end_byte; | |
| 3201 | 3202 |
| 3202 validate_region (&b, &e); | 3203 validate_region (&b, &e); |
| 3203 beg = XINT (b), end = XINT (e); | 3204 beg = XINT (b), end = XINT (e); |
| 3204 if (beg < GPT && end >= GPT) move_gap (end); | 3205 beg_byte = CHAR_TO_BYTE (beg); |
| 3205 | 3206 end_byte = CHAR_TO_BYTE (end); |
| 3206 coding_mask = detect_coding_mask (POS_ADDR (beg), end - beg); | 3207 |
| 3207 eol_type = detect_eol_type (POS_ADDR (beg), end - beg); | 3208 if (beg < GPT && end >= GPT) |
| 3209 move_gap_both (end, end_byte); | |
| 3210 | |
| 3211 coding_mask = detect_coding_mask (BYTE_POS_ADDR (beg_byte), | |
| 3212 end_byte - beg_byte); | |
| 3213 eol_type = detect_eol_type (BYTE_POS_ADDR (beg_byte), end_byte - beg_byte); | |
| 3208 | 3214 |
| 3209 if (coding_mask == CODING_CATEGORY_MASK_ANY) | 3215 if (coding_mask == CODING_CATEGORY_MASK_ANY) |
| 3210 { | 3216 { |
| 3211 val = Qundecided; | 3217 val = Qundecided; |
| 3212 if (eol_type != CODING_EOL_UNDECIDED | 3218 if (eol_type != CODING_EOL_UNDECIDED |
| 3376 *begp = beg_addr; | 3382 *begp = beg_addr; |
| 3377 *endp = end_addr; | 3383 *endp = end_addr; |
| 3378 return; | 3384 return; |
| 3379 } | 3385 } |
| 3380 | 3386 |
| 3381 /* Encode to (iff ENCODEP is 1) or decode form coding system CODING a | 3387 /* Encode into or decode from (according to ENCODEP) coding system CODING |
| 3382 text between B and E. B and E are buffer position. */ | 3388 the text between char positions B and E. */ |
| 3383 | 3389 |
| 3384 Lisp_Object | 3390 Lisp_Object |
| 3385 code_convert_region (b, e, coding, encodep) | 3391 code_convert_region (b, e, coding, encodep) |
| 3386 Lisp_Object b, e; | 3392 Lisp_Object b, e; |
| 3387 struct coding_system *coding; | 3393 struct coding_system *coding; |
| 3388 int encodep; | 3394 int encodep; |
| 3389 { | 3395 { |
| 3390 int beg, end, len, consumed, produced; | 3396 int beg, end, len, consumed, produced; |
| 3391 char *buf; | 3397 char *buf; |
| 3392 unsigned char *begp, *endp; | 3398 unsigned char *begp, *endp; |
| 3393 int pos = PT; | 3399 int opoint = PT, opoint_byte = PT_BYTE; |
| 3400 int beg_byte, end_byte, len_byte; | |
| 3401 int zv_before = ZV; | |
| 3402 int zv_byte_before = ZV_BYTE; | |
| 3394 | 3403 |
| 3395 validate_region (&b, &e); | 3404 validate_region (&b, &e); |
| 3396 beg = XINT (b), end = XINT (e); | 3405 beg = XINT (b), end = XINT (e); |
| 3406 beg_byte = CHAR_TO_BYTE (beg); | |
| 3407 end_byte = CHAR_TO_BYTE (end); | |
| 3408 | |
| 3397 if (beg < GPT && end >= GPT) | 3409 if (beg < GPT && end >= GPT) |
| 3398 move_gap (end); | 3410 move_gap_both (end, end_byte); |
| 3399 | 3411 |
| 3400 if (encodep && !NILP (coding->pre_write_conversion)) | 3412 if (encodep && !NILP (coding->pre_write_conversion)) |
| 3401 { | 3413 { |
| 3402 /* We must call a pre-conversion function which may put a new | 3414 /* We must call a pre-conversion function which may put a new |
| 3403 text to be converted in a new buffer. */ | 3415 text to be converted in a new buffer. */ |
| 3404 struct buffer *old = current_buffer, *new; | 3416 struct buffer *old = current_buffer, *new; |
| 3405 | 3417 |
| 3406 TEMP_SET_PT (beg); | 3418 TEMP_SET_PT_BOTH (beg, beg_byte); |
| 3407 call2 (coding->pre_write_conversion, b, e); | 3419 call2 (coding->pre_write_conversion, b, e); |
| 3408 if (old != current_buffer) | 3420 if (old != current_buffer) |
| 3409 { | 3421 { |
| 3410 /* Replace the original text by the text just generated. */ | 3422 /* Replace the original text by the text just generated. */ |
| 3411 len = ZV - BEGV; | 3423 len = ZV - BEGV; |
| 3424 len_byte = ZV_BYTE - BEGV_BYTE; | |
| 3412 new = current_buffer; | 3425 new = current_buffer; |
| 3413 set_buffer_internal (old); | 3426 set_buffer_internal (old); |
| 3414 del_range (beg, end); | 3427 del_range_both (beg, end, beg_byte, end_byte, 1); |
| 3415 insert_from_buffer (new, 1, len, 0); | 3428 insert_from_buffer (new, 1, len, 0); |
| 3416 end = beg + len; | 3429 end = beg + len; |
| 3430 end_byte = len_byte; | |
| 3417 } | 3431 } |
| 3418 } | 3432 } |
| 3419 | 3433 |
| 3420 /* We may be able to shrink the conversion region. */ | 3434 /* We may be able to shrink the conversion region. */ |
| 3421 begp = POS_ADDR (beg); endp = begp + (end - beg); | 3435 begp = BYTE_POS_ADDR (beg_byte); |
| 3436 endp = begp + (end_byte - beg_byte); | |
| 3422 shrink_conversion_area (&begp, &endp, coding, encodep); | 3437 shrink_conversion_area (&begp, &endp, coding, encodep); |
| 3423 | 3438 |
| 3424 if (begp == endp) | 3439 if (begp == endp) |
| 3425 /* We need no conversion. */ | 3440 /* We need no conversion. */ |
| 3426 len = end - beg; | 3441 len = end - beg; |
| 3427 else | 3442 else |
| 3428 { | 3443 { |
| 3429 beg += begp - POS_ADDR (beg); | 3444 int shrunk_beg_byte, shrunk_end_byte; |
| 3430 end = beg + (endp - begp); | 3445 int shrunk_beg; |
| 3446 int shrunk_len_byte; | |
| 3447 int new_len_byte; | |
| 3448 int buflen; | |
| 3449 int zv_before; | |
| 3450 | |
| 3451 shrunk_beg_byte = PTR_BYTE_POS (begp); | |
| 3452 shrunk_beg = BYTE_TO_CHAR (shrunk_beg_byte); | |
| 3453 shrunk_end_byte = PTR_BYTE_POS (endp); | |
| 3454 shrunk_len_byte = shrunk_end_byte - shrunk_beg_byte; | |
| 3431 | 3455 |
| 3432 if (encodep) | 3456 if (encodep) |
| 3433 len = encoding_buffer_size (coding, end - beg); | 3457 buflen = encoding_buffer_size (coding, shrunk_len_byte); |
| 3434 else | 3458 else |
| 3435 len = decoding_buffer_size (coding, end - beg); | 3459 buflen = decoding_buffer_size (coding, shrunk_len_byte); |
| 3436 buf = get_conversion_buffer (len); | 3460 buf = get_conversion_buffer (buflen); |
| 3437 | 3461 |
| 3438 coding->last_block = 1; | 3462 coding->last_block = 1; |
| 3439 produced = (encodep | 3463 produced = (encodep |
| 3440 ? encode_coding (coding, POS_ADDR (beg), buf, end - beg, len, | 3464 ? encode_coding (coding, begp, buf, shrunk_len_byte, buflen, |
| 3441 &consumed) | 3465 &consumed) |
| 3442 : decode_coding (coding, POS_ADDR (beg), buf, end - beg, len, | 3466 : decode_coding (coding, begp, buf, shrunk_len_byte, buflen, |
| 3443 &consumed)); | 3467 &consumed)); |
| 3444 | 3468 |
| 3445 len = produced + (beg - XINT (b)) + (XINT (e) - end); | 3469 TEMP_SET_PT_BOTH (shrunk_beg, shrunk_beg_byte); |
| 3446 | |
| 3447 TEMP_SET_PT (beg); | |
| 3448 insert (buf, produced); | 3470 insert (buf, produced); |
| 3449 del_range (PT, PT + end - beg); | 3471 del_range_byte (PT_BYTE, PT_BYTE + shrunk_len_byte, 1); |
| 3450 if (pos >= end) | 3472 |
| 3451 pos = PT + (pos - end); | 3473 if (opoint >= end) |
| 3452 else if (pos > beg) | 3474 { |
| 3453 pos = beg; | 3475 opoint += ZV - zv_before; |
| 3454 TEMP_SET_PT (pos); | 3476 opoint_byte += ZV_BYTE - zv_byte_before; |
| 3455 } | 3477 } |
| 3478 else if (opoint > beg) | |
| 3479 { | |
| 3480 opoint = beg; | |
| 3481 opoint_byte = beg_byte; | |
| 3482 } | |
| 3483 TEMP_SET_PT_BOTH (opoint, opoint_byte); | |
| 3484 | |
| 3485 end += ZV - zv_before; | |
| 3486 } | |
| 3456 | 3487 |
| 3457 if (!encodep && !NILP (coding->post_read_conversion)) | 3488 if (!encodep && !NILP (coding->post_read_conversion)) |
| 3458 { | 3489 { |
| 3490 Lisp_Object insval; | |
| 3491 | |
| 3459 /* We must call a post-conversion function which may alter | 3492 /* We must call a post-conversion function which may alter |
| 3460 the text just converted. */ | 3493 the text just converted. */ |
| 3461 Lisp_Object insval; | 3494 zv_before = ZV; |
| 3462 | 3495 zv_byte_before = ZV_BYTE; |
| 3463 beg = XINT (b); | 3496 |
| 3464 TEMP_SET_PT (beg); | 3497 TEMP_SET_PT_BOTH (beg, beg_byte); |
| 3465 insval = call1 (coding->post_read_conversion, make_number (len)); | 3498 insval = call1 (coding->post_read_conversion, make_number (end - beg)); |
| 3466 CHECK_NUMBER (insval, 0); | 3499 CHECK_NUMBER (insval, 0); |
| 3467 if (pos >= beg + len) | 3500 |
| 3468 pos += XINT (insval) - len; | 3501 if (opoint >= beg + ZV - zv_before) |
| 3469 else if (pos > beg) | 3502 { |
| 3470 pos = beg; | 3503 opoint += ZV - zv_before; |
| 3471 TEMP_SET_PT (pos); | 3504 opoint_byte += ZV_BYTE - zv_byte_before; |
| 3505 } | |
| 3506 else if (opoint > beg) | |
| 3507 { | |
| 3508 opoint = beg; | |
| 3509 opoint_byte = beg_byte; | |
| 3510 } | |
| 3511 TEMP_SET_PT_BOTH (opoint, opoint_byte); | |
| 3472 len = XINT (insval); | 3512 len = XINT (insval); |
| 3473 } | 3513 } |
| 3474 | 3514 |
| 3475 return make_number (len); | 3515 return make_number (len); |
| 3476 } | 3516 } |
| 3517 | |
| 3518 /* Encode or decode (according to ENCODEP) the text of string STR | |
| 3519 using coding CODING. If NOCOPY is nil, we never return STR | |
| 3520 itself, but always a copy. If NOCOPY is non-nil, we return STR | |
| 3521 if no change is needed. */ | |
| 3477 | 3522 |
| 3478 Lisp_Object | 3523 Lisp_Object |
| 3479 code_convert_string (str, coding, encodep, nocopy) | 3524 code_convert_string (str, coding, encodep, nocopy) |
| 3480 Lisp_Object str, nocopy; | 3525 Lisp_Object str, nocopy; |
| 3481 struct coding_system *coding; | 3526 struct coding_system *coding; |
