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
16 changes: 16 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,17 @@ 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() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer we called this offset -- it's not actually an address (0 wouldn't make sense as an address here).

self.gap_size()
} else {
0
};

unsafe { self.beg_addr().offset(base_addr + n - self.beg_byte()) 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();
LispObject::from_bool(
buffer_ref.pt == buffer_ref.begv || buffer_ref.fetch_byte(buffer_ref.pt_byte - 1) == 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();
LispObject::from_bool(
buffer_ref.pt == buffer_ref.zv() || buffer_ref.fetch_byte(buffer_ref.pt_byte) == b'\n',
Copy link
Member Author

Choose a reason for hiding this comment

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

cargo fmt told me about that final comma, but.... does it make sense? I find it a bit strange, no?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed, it's a little odd. Perhaps it's a rustfmt bug.

I like that rustfmt is a tool that you just run and it fixes formatting without human input. We could file a bug upstream, but I'd leave the comma for now if rustfmt wants it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think it's a big, it's just consistent in always having a final comma on multi-line lists of expressions. The argument for having the trailing comma in the first place, less churn when an element is added, applies to single arguments as well.

)
}
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