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 bolp and eolp to rust #293

Merged
merged 11 commits into from Sep 2, 2017
18 changes: 18 additions & 0 deletions rust_src/src/buffers.rs
Expand Up @@ -41,6 +41,11 @@ impl LispBufferRef {
unsafe { (*self.text).gpt_byte }
}

#[inline]
pub fn gap_size(&self) -> ptrdiff_t {
unsafe { (*self.text).gap_size }
}

#[inline]
pub fn gap_end_addr(&self) -> *mut c_uchar {
unsafe {
Expand Down Expand Up @@ -91,6 +96,19 @@ impl LispBufferRef {
pub fn is_live(self) -> bool {
LispObject::from_raw(self.name).is_not_nil()
}

#[inline]
pub fn fetch_byte(&self, n: ptrdiff_t) -> u8 {
let base_addr =
if n >= self.gpt_byte() {
self.gap_size()
} else {
0
};
let byte_addr = (base_addr + n +
self.beg_addr() as ptrdiff_t - self.beg_byte()) as *const u8;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like that should be self.beg_addr().offset(base_addr + n - self.beg_byte())

unsafe {(*byte_addr) as u8 }
}
}

impl LispObject {
Expand Down
19 changes: 19 additions & 0 deletions rust_src/src/editfns.rs
Expand Up @@ -49,3 +49,22 @@ pub fn bobp() -> LispObject {
let buffer_ref = ThreadState::current_buffer();
LispObject::from_bool(buffer_ref.pt == buffer_ref.begv)
}

/// Return t if point is at the beginning of a line.
#[lisp_fn]
pub fn bolp() -> LispObject {
let buffer_ref = ThreadState::current_buffer();
let pt_byte = buffer_ref.pt_byte - 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, calling pt_byte - 1 also pt_byte is a bit confusing.

LispObject::from_bool(buffer_ref.pt == buffer_ref.begv ||
buffer_ref.fetch_byte(pt_byte) as char == '\n')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of as char, compare with b'\n'

}

/// Return t if point is at the end of a line.
///`End of a line' includes point being at the end of the buffer.
#[lisp_fn]
pub fn eolp() -> LispObject {
let buffer_ref = ThreadState::current_buffer();
let pt_byte = buffer_ref.pt_byte;
LispObject::from_bool(buffer_ref.pt == buffer_ref.zv() ||
buffer_ref.fetch_byte(pt_byte) as char == '\n')
}
4 changes: 4 additions & 0 deletions rust_src/src/lib.rs
Expand Up @@ -162,6 +162,8 @@ pub use multibyte::str_to_unibyte;

// Used in window.c, macros.c
pub use interactive::Fprefix_numeric_value;
pub use editfns::Fbolp;
pub use editfns::Feolp;

extern "C" {
fn defsubr(sname: *const Lisp_Subr);
Expand Down Expand Up @@ -319,5 +321,7 @@ pub extern "C" fn rust_init_syms() {
defsubr(&*editfns::Sbuffer_size);
defsubr(&*editfns::Seobp);
defsubr(&*editfns::Sbobp);
defsubr(&*editfns::Sbolp);
defsubr(&*editfns::Seolp);
}
}
21 changes: 0 additions & 21 deletions src/editfns.c
Expand Up @@ -1167,25 +1167,6 @@ At the beginning of the buffer or accessible region, return 0. */)
return temp;
}

DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
doc: /* Return t if point is at the beginning of a line. */)
(void)
{
if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n')
return Qt;
return Qnil;
}

DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
doc: /* Return t if point is at the end of a line.
`End of a line' includes point being at the end of the buffer. */)
(void)
{
if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n')
return Qt;
return Qnil;
}

DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0,
doc: /* Return character in current buffer at position POS.
POS is an integer or a marker and defaults to point.
Expand Down Expand Up @@ -5409,8 +5390,6 @@ functions if all the text being accessed has this property. */);
defsubr (&Sposition_bytes);
defsubr (&Sbyte_to_position);

defsubr (&Sbolp);
defsubr (&Seolp);
defsubr (&Sfollowing_char);
defsubr (&Sprevious_char);
defsubr (&Schar_after);
Expand Down