Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port char-width #1583

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions rust_src/src/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use libc::{c_uchar, ptrdiff_t};

use remacs_macros::lisp_fn;
use crate::remacs_sys::{char_width, buffer_display_table};

use crate::{
lisp::LispObject,
Expand All @@ -26,6 +27,16 @@ pub const fn char_head_p(byte: c_uchar) -> bool {
(byte & 0xC0) != 0x80
}

/// Return width of CHAR when displayed in the current buffer.
/// The width is measured by how many columns it occupies on the screen.
/// Tab is taken to occupy `tab-width' columns.
/// usage: (char-width CHAR)
#[lisp_fn(c_name = "char_width", name = "char-width")]
pub fn char_width_lisp(ch: Codepoint) -> isize {
let width = unsafe { char_width(ch.val() as i32, buffer_display_table()) };
width
}

/// Decrement the buffer byte position `POS_BYTE` of the current buffer
/// to the previous character boundary. No range checking of POS.
///
Expand Down
17 changes: 0 additions & 17 deletions src/character.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,6 @@ char_width (int c, struct Lisp_Char_Table *dp)
}


DEFUN ("char-width", Fchar_width, Schar_width, 1, 1, 0,
doc: /* Return width of CHAR when displayed in the current buffer.
The width is measured by how many columns it occupies on the screen.
Tab is taken to occupy `tab-width' columns.
usage: (char-width CHAR) */)
(Lisp_Object ch)
{
int c;
ptrdiff_t width;

CHECK_CHARACTER (ch);
c = XINT (ch);
width = char_width (c, buffer_display_table ());
return make_number (width);
}

/* Return width of string STR of length LEN when displayed in the
current buffer. The width is measured by how many columns it
occupies on the screen. If PRECISION > 0, return the width of
Expand Down Expand Up @@ -531,7 +515,6 @@ syms_of_character (void)
staticpro (&Vchar_unify_table);
Vchar_unify_table = Qnil;

defsubr (&Schar_width);
defsubr (&Sstring);
defsubr (&Sunibyte_string);
defsubr (&Sget_byte);
Expand Down
6 changes: 6 additions & 0 deletions test/rust_src/src/fns-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
(should (equal (make-char-table 'syntax-table '(3))
(make-char-table 'syntax-table '(3)))))

(ert-deftest test-equal-char-width ()
(should (equal (char-width ?a) 1))
(should (equal (char-width ?1) 1))
(should (equal (char-width ?\n) 0))
(should (equal (char-width ?你) 2)))

(ert-deftest test-equal-vector ()
(should (equal [1 two '(three) "four" [five]]
[1 two '(three) "four" [five]])))
Expand Down