diff options
Diffstat (limited to 'src/character.c')
| -rw-r--r-- | src/character.c | 94 |
1 files changed, 48 insertions, 46 deletions
diff --git a/src/character.c b/src/character.c index 7fc5d647ff5..82bc2bfef4e 100644 --- a/src/character.c +++ b/src/character.c | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | /* Basic character support. | 1 | /* Basic character support. |
| 2 | 2 | ||
| 3 | Copyright (C) 2001-2011 Free Software Foundation, Inc. | 3 | Copyright (C) 2001-2012 Free Software Foundation, Inc. |
| 4 | Copyright (C) 1995, 1997, 1998, 2001 Electrotechnical Laboratory, JAPAN. | 4 | Copyright (C) 1995, 1997, 1998, 2001 Electrotechnical Laboratory, JAPAN. |
| 5 | Licensed to the Free Software Foundation. | 5 | Licensed to the Free Software Foundation. |
| 6 | Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 | 6 | Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
| @@ -258,7 +258,8 @@ multibyte_char_to_unibyte_safe (int c) | |||
| 258 | } | 258 | } |
| 259 | 259 | ||
| 260 | DEFUN ("characterp", Fcharacterp, Scharacterp, 1, 2, 0, | 260 | DEFUN ("characterp", Fcharacterp, Scharacterp, 1, 2, 0, |
| 261 | doc: /* Return non-nil if OBJECT is a character. */) | 261 | doc: /* Return non-nil if OBJECT is a character. |
| 262 | usage: (characterp OBJECT) */) | ||
| 262 | (Lisp_Object object, Lisp_Object ignore) | 263 | (Lisp_Object object, Lisp_Object ignore) |
| 263 | { | 264 | { |
| 264 | return (CHARACTERP (object) ? Qt : Qnil); | 265 | return (CHARACTERP (object) ? Qt : Qnil); |
| @@ -307,6 +308,36 @@ If the multibyte character does not represent a byte, return -1. */) | |||
| 307 | } | 308 | } |
| 308 | } | 309 | } |
| 309 | 310 | ||
| 311 | |||
| 312 | /* Return width (columns) of C considering the buffer display table DP. */ | ||
| 313 | |||
| 314 | static EMACS_INT | ||
| 315 | char_width (int c, struct Lisp_Char_Table *dp) | ||
| 316 | { | ||
| 317 | EMACS_INT width = CHAR_WIDTH (c); | ||
| 318 | |||
| 319 | if (dp) | ||
| 320 | { | ||
| 321 | Lisp_Object disp = DISP_CHAR_VECTOR (dp, c), ch; | ||
| 322 | int i; | ||
| 323 | |||
| 324 | if (VECTORP (disp)) | ||
| 325 | for (i = 0, width = 0; i < ASIZE (disp); i++) | ||
| 326 | { | ||
| 327 | ch = AREF (disp, i); | ||
| 328 | if (CHARACTERP (ch)) | ||
| 329 | { | ||
| 330 | int w = CHAR_WIDTH (XFASTINT (ch)); | ||
| 331 | if (INT_ADD_OVERFLOW (width, w)) | ||
| 332 | string_overflow (); | ||
| 333 | width += w; | ||
| 334 | } | ||
| 335 | } | ||
| 336 | } | ||
| 337 | return width; | ||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 310 | DEFUN ("char-width", Fchar_width, Schar_width, 1, 1, 0, | 341 | DEFUN ("char-width", Fchar_width, Schar_width, 1, 1, 0, |
| 311 | doc: /* Return width of CHAR when displayed in the current buffer. | 342 | doc: /* Return width of CHAR when displayed in the current buffer. |
| 312 | The width is measured by how many columns it occupies on the screen. | 343 | The width is measured by how many columns it occupies on the screen. |
| @@ -314,21 +345,12 @@ Tab is taken to occupy `tab-width' columns. | |||
| 314 | usage: (char-width CHAR) */) | 345 | usage: (char-width CHAR) */) |
| 315 | (Lisp_Object ch) | 346 | (Lisp_Object ch) |
| 316 | { | 347 | { |
| 317 | Lisp_Object disp; | 348 | int c; |
| 318 | int c, width; | 349 | EMACS_INT width; |
| 319 | struct Lisp_Char_Table *dp = buffer_display_table (); | ||
| 320 | 350 | ||
| 321 | CHECK_CHARACTER (ch); | 351 | CHECK_CHARACTER (ch); |
| 322 | c = XINT (ch); | 352 | c = XINT (ch); |
| 323 | 353 | width = char_width (c, buffer_display_table ()); | |
| 324 | /* Get the way the display table would display it. */ | ||
| 325 | disp = dp ? DISP_CHAR_VECTOR (dp, c) : Qnil; | ||
| 326 | |||
| 327 | if (VECTORP (disp)) | ||
| 328 | width = ASIZE (disp); | ||
| 329 | else | ||
| 330 | width = CHAR_WIDTH (c); | ||
| 331 | |||
| 332 | return make_number (width); | 354 | return make_number (width); |
| 333 | } | 355 | } |
| 334 | 356 | ||
| @@ -349,25 +371,16 @@ c_string_width (const unsigned char *str, EMACS_INT len, int precision, | |||
| 349 | 371 | ||
| 350 | while (i_byte < len) | 372 | while (i_byte < len) |
| 351 | { | 373 | { |
| 352 | int bytes, thiswidth; | 374 | int bytes; |
| 353 | Lisp_Object val; | ||
| 354 | int c = STRING_CHAR_AND_LENGTH (str + i_byte, bytes); | 375 | int c = STRING_CHAR_AND_LENGTH (str + i_byte, bytes); |
| 376 | EMACS_INT thiswidth = char_width (c, dp); | ||
| 355 | 377 | ||
| 356 | if (dp) | 378 | if (precision <= 0) |
| 357 | { | ||
| 358 | val = DISP_CHAR_VECTOR (dp, c); | ||
| 359 | if (VECTORP (val)) | ||
| 360 | thiswidth = ASIZE (val); | ||
| 361 | else | ||
| 362 | thiswidth = CHAR_WIDTH (c); | ||
| 363 | } | ||
| 364 | else | ||
| 365 | { | 379 | { |
| 366 | thiswidth = CHAR_WIDTH (c); | 380 | if (INT_ADD_OVERFLOW (width, thiswidth)) |
| 381 | string_overflow (); | ||
| 367 | } | 382 | } |
| 368 | 383 | else if (precision - width < thiswidth) | |
| 369 | if (precision > 0 | ||
| 370 | && (width + thiswidth > precision)) | ||
| 371 | { | 384 | { |
| 372 | *nchars = i; | 385 | *nchars = i; |
| 373 | *nbytes = i_byte; | 386 | *nbytes = i_byte; |
| @@ -422,7 +435,7 @@ lisp_string_width (Lisp_Object string, EMACS_INT precision, | |||
| 422 | { | 435 | { |
| 423 | EMACS_INT chars, bytes, thiswidth; | 436 | EMACS_INT chars, bytes, thiswidth; |
| 424 | Lisp_Object val; | 437 | Lisp_Object val; |
| 425 | int cmp_id; | 438 | ptrdiff_t cmp_id; |
| 426 | EMACS_INT ignore, end; | 439 | EMACS_INT ignore, end; |
| 427 | 440 | ||
| 428 | if (find_composition (i, -1, &ignore, &end, &val, string) | 441 | if (find_composition (i, -1, &ignore, &end, &val, string) |
| @@ -446,18 +459,7 @@ lisp_string_width (Lisp_Object string, EMACS_INT precision, | |||
| 446 | else | 459 | else |
| 447 | c = str[i_byte], bytes = 1; | 460 | c = str[i_byte], bytes = 1; |
| 448 | chars = 1; | 461 | chars = 1; |
| 449 | if (dp) | 462 | thiswidth = char_width (c, dp); |
| 450 | { | ||
| 451 | val = DISP_CHAR_VECTOR (dp, c); | ||
| 452 | if (VECTORP (val)) | ||
| 453 | thiswidth = ASIZE (val); | ||
| 454 | else | ||
| 455 | thiswidth = CHAR_WIDTH (c); | ||
| 456 | } | ||
| 457 | else | ||
| 458 | { | ||
| 459 | thiswidth = CHAR_WIDTH (c); | ||
| 460 | } | ||
| 461 | } | 463 | } |
| 462 | 464 | ||
| 463 | if (precision <= 0) | 465 | if (precision <= 0) |
| @@ -587,7 +589,7 @@ parse_str_as_multibyte (const unsigned char *str, EMACS_INT len, | |||
| 587 | } | 589 | } |
| 588 | 590 | ||
| 589 | /* Arrange unibyte text at STR of NBYTES bytes as a multibyte text. | 591 | /* Arrange unibyte text at STR of NBYTES bytes as a multibyte text. |
| 590 | It actually converts only such 8-bit characters that don't contruct | 592 | It actually converts only such 8-bit characters that don't construct |
| 591 | a multibyte sequence to multibyte forms of Latin-1 characters. If | 593 | a multibyte sequence to multibyte forms of Latin-1 characters. If |
| 592 | NCHARS is nonzero, set *NCHARS to the number of characters in the | 594 | NCHARS is nonzero, set *NCHARS to the number of characters in the |
| 593 | text. It is assured that we can use LEN bytes at STR as a work | 595 | text. It is assured that we can use LEN bytes at STR as a work |
| @@ -668,7 +670,7 @@ str_as_multibyte (unsigned char *str, EMACS_INT len, EMACS_INT nbytes, | |||
| 668 | } | 670 | } |
| 669 | 671 | ||
| 670 | /* Parse unibyte string at STR of LEN bytes, and return the number of | 672 | /* Parse unibyte string at STR of LEN bytes, and return the number of |
| 671 | bytes it may ocupy when converted to multibyte string by | 673 | bytes it may occupy when converted to multibyte string by |
| 672 | `str_to_multibyte'. */ | 674 | `str_to_multibyte'. */ |
| 673 | 675 | ||
| 674 | EMACS_INT | 676 | EMACS_INT |
| @@ -901,7 +903,7 @@ usage: (string &rest CHARACTERS) */) | |||
| 901 | Lisp_Object str; | 903 | Lisp_Object str; |
| 902 | USE_SAFE_ALLOCA; | 904 | USE_SAFE_ALLOCA; |
| 903 | 905 | ||
| 904 | SAFE_ALLOCA (buf, unsigned char *, MAX_MULTIBYTE_LENGTH * n); | 906 | SAFE_NALLOCA (buf, MAX_MULTIBYTE_LENGTH, n); |
| 905 | p = buf; | 907 | p = buf; |
| 906 | 908 | ||
| 907 | for (i = 0; i < n; i++) | 909 | for (i = 0; i < n; i++) |
| @@ -968,7 +970,7 @@ character is a target to get a byte value. In this case, POSITION, if | |||
| 968 | non-nil, is an index of a target character in the string. | 970 | non-nil, is an index of a target character in the string. |
| 969 | 971 | ||
| 970 | If the current buffer (or STRING) is multibyte, and the target | 972 | If the current buffer (or STRING) is multibyte, and the target |
| 971 | character is not ASCII nor 8-bit character, an error is signalled. */) | 973 | character is not ASCII nor 8-bit character, an error is signaled. */) |
| 972 | (Lisp_Object position, Lisp_Object string) | 974 | (Lisp_Object position, Lisp_Object string) |
| 973 | { | 975 | { |
| 974 | int c; | 976 | int c; |