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 some buffer functions #288

Merged
merged 4 commits into from Aug 14, 2017
Merged
Changes from all commits
Commits
File filter
Filter file types
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -69,9 +69,9 @@ fn validate_template(template: String) -> Result<String, i32> {
fn generate_temporary_filename(name: &mut String) {
let len = name.len();
assert!(len >= 6);
let mut name_vec = unsafe { &mut name.as_mut_vec() };
let name_vec = unsafe { &mut name.as_mut_vec() };

let mut bytes = &mut name_vec[len - 6..len];
let bytes = &mut name_vec[len - 6..len];
rand::thread_rng().fill_bytes(bytes);
for byte in bytes.iter_mut() {
*byte = match *byte % 62 {
@@ -3,7 +3,7 @@
use libc::{c_void, c_uchar, ptrdiff_t};

use lisp::{LispObject, ExternalPtr};
use remacs_sys::{Lisp_Buffer, Lisp_Type, Vbuffer_alist, make_lisp_ptr};
use remacs_sys::{Lisp_Buffer, Lisp_Type, Vbuffer_alist, EmacsInt, make_lisp_ptr};
use strings::string_equal;
use lists::{car, cdr};
use threads::ThreadState;
@@ -71,13 +71,41 @@ impl LispBufferRef {
unsafe { (*self.text).z }
}

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

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

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

// Check if buffer is live
#[inline]
pub fn is_live(self) -> bool {
LispObject::from_raw(self.name).is_not_nil()
}
}

impl LispObject {
/// Return SELF as a struct buffer pointer, defaulting to the current buffer.
/// Same as the decode_buffer function in buffer.h
#[inline]
pub fn as_buffer_or_current_buffer(self) -> LispBufferRef {
if self.is_nil() {
ThreadState::current_buffer()
} else {
self.as_buffer_or_error()
}
}
}

/// Return t if OBJECT is an overlay.
#[lisp_fn]
pub fn overlayp(object: LispObject) -> LispObject {
@@ -145,3 +173,41 @@ pub fn buffer_file_name(buffer: LispObject) -> LispObject {

LispObject::from_raw(buf.filename)
}

/// Return t if BUFFER was modified since its file was last read or saved.
/// No argument or nil as argument means use current buffer as BUFFER.
#[lisp_fn(min = "0")]
pub fn buffer_modified_p(buffer: LispObject) -> LispObject {
let buf = buffer.as_buffer_or_current_buffer();
LispObject::from_bool(buf.save_modiff() < buf.modiff())
}

/// Return the name of BUFFER, as a string.
/// BUFFER defaults to the current buffer.
/// Return nil if BUFFER has been killed.
#[lisp_fn(min = "0")]
pub fn buffer_name(buffer: LispObject) -> LispObject {
LispObject::from_raw(buffer.as_buffer_or_current_buffer().name)
}

/// Return BUFFER's tick counter, incremented for each change in text.
/// Each buffer has a tick counter which is incremented each time the
/// text in that buffer is changed. It wraps around occasionally.
/// No argument or nil as argument means use current buffer as BUFFER.
#[lisp_fn(min = "0")]
fn buffer_modified_tick(buffer: LispObject) -> LispObject {
LispObject::from_fixnum(buffer.as_buffer_or_current_buffer().modiff())
}

/// Return BUFFER's character-change tick counter.
/// Each buffer has a character-change tick counter, which is set to the
/// value of the buffer's tick counter (see `buffer-modified-tick'), each
/// time text in that buffer is inserted or deleted. By comparing the
/// values returned by two individual calls of `buffer-chars-modified-tick',
/// you can tell whether a character change occurred in that buffer in
/// between these calls. No argument or nil as argument means use current
/// buffer as BUFFER.
#[lisp_fn(min = "0")]
fn buffer_chars_modified_tick(buffer: LispObject) -> LispObject {
LispObject::from_fixnum(buffer.as_buffer_or_current_buffer().chars_modiff())
}
@@ -66,6 +66,10 @@ pub use base64::base64_decode_1;

// Used in buffer.c
pub use buffers::Fbuffer_live_p;
pub use buffers::Fbuffer_modified_p;

// used in process.c
pub use buffers::Fbuffer_name;

// Used in nsfns.m
pub use buffers::Fbuffer_file_name;
@@ -170,6 +174,10 @@ pub extern "C" fn rust_init_syms() {
defsubr(&*buffers::Sget_buffer);
defsubr(&*buffers::Scurrent_buffer);
defsubr(&*buffers::Sbuffer_file_name);
defsubr(&*buffers::Sbuffer_modified_p);
defsubr(&*buffers::Sbuffer_modified_tick);
defsubr(&*buffers::Sbuffer_chars_modified_tick);
defsubr(&*buffers::Sbuffer_name);
defsubr(&*windows::Swindowp);
defsubr(&*windows::Swindow_live_p);
defsubr(&*process::Sget_process);
@@ -633,7 +633,7 @@ pub fn string_char(ptr: *const c_uchar, advanced: *mut *const c_uchar, len: *mut
#[no_mangle]
pub fn str_to_unibyte(src: *const c_uchar, dst: *mut c_uchar, chars: ptrdiff_t) -> ptrdiff_t {
let mut srcslice = unsafe { slice::from_raw_parts(src, chars as usize) };
let mut dstslice = unsafe { slice::from_raw_parts_mut(dst, chars as usize) };
let dstslice = unsafe { slice::from_raw_parts_mut(dst, chars as usize) };
for i in 0..chars {
let (cp, cplen) = multibyte_char_at(srcslice);
srcslice = &srcslice[cplen..];
@@ -1057,15 +1057,6 @@ is first appended to NAME, to speed up finding a non-existent buffer. */)
}


DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
doc: /* Return the name of BUFFER, as a string.
BUFFER defaults to the current buffer.
Return nil if BUFFER has been killed. */)
(register Lisp_Object buffer)
{
return BVAR (decode_buffer (buffer), name);
}

DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
0, 1, 0,
doc: /* Return the base buffer of indirect buffer BUFFER.
@@ -1223,16 +1214,6 @@ No argument or nil as argument means use current buffer as BUFFER. */)
return result;
}

DEFUN ("buffer-modified-p", Fbuffer_modified_p, Sbuffer_modified_p,
0, 1, 0,
doc: /* Return t if BUFFER was modified since its file was last read or saved.
No argument or nil as argument means use current buffer as BUFFER. */)
(Lisp_Object buffer)
{
struct buffer *buf = decode_buffer (buffer);
return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
}

DEFUN ("force-mode-line-update", Fforce_mode_line_update,
Sforce_mode_line_update, 0, 1, 0,
doc: /* Force redisplay of the current buffer's mode line and header line.
@@ -1327,31 +1308,6 @@ state of the current buffer. Use with care. */)
return flag;
}

DEFUN ("buffer-modified-tick", Fbuffer_modified_tick, Sbuffer_modified_tick,
0, 1, 0,
doc: /* Return BUFFER's tick counter, incremented for each change in text.
Each buffer has a tick counter which is incremented each time the
text in that buffer is changed. It wraps around occasionally.
No argument or nil as argument means use current buffer as BUFFER. */)
(register Lisp_Object buffer)
{
return make_number (BUF_MODIFF (decode_buffer (buffer)));
}

DEFUN ("buffer-chars-modified-tick", Fbuffer_chars_modified_tick,
Sbuffer_chars_modified_tick, 0, 1, 0,
doc: /* Return BUFFER's character-change tick counter.
Each buffer has a character-change tick counter, which is set to the
value of the buffer's tick counter (see `buffer-modified-tick'), each
time text in that buffer is inserted or deleted. By comparing the
values returned by two individual calls of `buffer-chars-modified-tick',
you can tell whether a character change occurred in that buffer in
between these calls. No argument or nil as argument means use current
buffer as BUFFER. */)
(register Lisp_Object buffer)
{
return make_number (BUF_CHARS_MODIFF (decode_buffer (buffer)));
}

DEFUN ("rename-buffer", Frename_buffer, Srename_buffer, 1, 2,
"(list (read-string \"Rename buffer (to new name): \" \
@@ -6129,15 +6085,11 @@ Functions running this hook are, `get-buffer-create',
defsubr (&Sget_buffer_create);
defsubr (&Smake_indirect_buffer);
defsubr (&Sgenerate_new_buffer_name);
defsubr (&Sbuffer_name);
defsubr (&Sbuffer_base_buffer);
defsubr (&Sbuffer_local_value);
defsubr (&Sbuffer_local_variables);
defsubr (&Sbuffer_modified_p);
defsubr (&Sforce_mode_line_update);
defsubr (&Sset_buffer_modified_p);
defsubr (&Sbuffer_modified_tick);
defsubr (&Sbuffer_chars_modified_tick);
defsubr (&Srename_buffer);
defsubr (&Sother_buffer);
defsubr (&Sbuffer_enable_undo);
ProTip! Use n and p to navigate between commits in a pull request.