Navigation Menu

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 window-live-p to Rust #258

Merged
merged 1 commit into from Jul 21, 2017
Merged
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
102 changes: 102 additions & 0 deletions rust_src/remacs-sys/lib.rs
Expand Up @@ -124,6 +124,17 @@ pub enum PseudovecType {
PVEC_FONT, /* Should be last because it's used for range checking. */
}

#[repr(C)]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum TextCursorKinds {
DEFAULT_CURSOR = -2,
NO_CURSOR = -1,
FILLED_BOX_CURSOR,
HOLLOW_BOX_CURSOR,
BAR_CURSOR,
HBAR_CURSOR,
}

// XXX: this can also be char on some archs
pub type bits_word = size_t;

Expand Down Expand Up @@ -286,6 +297,97 @@ pub struct Lisp_Marker {
pub bytepos: ptrdiff_t,
}

/// Represents the cursor position within an Emacs window. For
/// documentation see stuct cursor_pos in window.h.
#[repr(C)]
pub struct CursorPos {
// Pixel position. These are always window relative.
x: c_int,
y: c_int,
// Glyph matrix position.
hpos: c_int,
vpos: c_int,
}

/// Represents an Emacs window. For documentation see struct window in
/// window.h.
#[repr(C)]
pub struct Lisp_Window {
pub header: Lisp_Vectorlike_Header,
pub frame: Lisp_Object,
pub next: Lisp_Object,
pub prev: Lisp_Object,
pub parent: Lisp_Object,
pub normal_lines: Lisp_Object,
pub normal_cols: Lisp_Object,
pub new_total: Lisp_Object,
pub new_normal: Lisp_Object,
pub new_pixel: Lisp_Object,
pub contents: Lisp_Object,
pub start: Lisp_Object,
pub pointm: Lisp_Object,
pub old_pointm: Lisp_Object,
pub temslot: Lisp_Object,
pub vertical_scroll_bar: Lisp_Object,
pub vertical_scroll_bar_type: Lisp_Object,
pub horizontal_scroll_bar: Lisp_Object,
pub horizontal_scroll_bar_type: Lisp_Object,
pub display_table: Lisp_Object,
pub dedicated: Lisp_Object,
pub redisplay_end_trigger: Lisp_Object,
pub combination_limit: Lisp_Object,
pub window_parameters: Lisp_Object,
pub current_matrix: *mut c_void,
pub desired_matrix: *mut c_void,
pub prev_buffers: Lisp_Object,
pub next_buffers: Lisp_Object,
pub use_time: EmacsInt,
pub sequence_number: EmacsInt,
pub pixel_left: c_int,
pub pixel_top: c_int,
pub left_col: c_int,
pub top_line: c_int,
pub pixel_width: c_int,
pub pixel_height: c_int,
pub pixel_width_before_size_change: c_int,
pub pixel_height_before_size_change: c_int,
pub total_cols: c_int,
pub total_lines: c_int,
pub hscroll: ptrdiff_t,
pub min_hscroll: ptrdiff_t,
pub hscroll_whole: ptrdiff_t,
pub last_modified: EmacsInt,
pub last_overlay_modified: EmacsInt,
pub last_point: ptrdiff_t,
pub base_line_number: ptrdiff_t,
pub base_line_pos: ptrdiff_t,
pub column_number_displayed: ptrdiff_t,
pub nrows_scale_factor: c_int,
pub ncols_scale_factor: c_int,
pub cursor: CursorPos,
pub phys_cursor: CursorPos,
pub output_cursor: CursorPos,
pub last_cursor_vpos: c_int,
pub phys_cursor_type: TextCursorKinds,
pub phys_cursor_width: c_int,
pub phys_cursor_ascent: c_int,
pub phys_cursor_height: c_int,
pub left_fringe_width: c_int,
pub right_fringe_width: c_int,
pub left_margin_cols: c_int,
pub right_margin_cols: c_int,
pub scroll_bar_width: c_int,
pub scroll_bar_height: c_int,
pub mode_line_height: c_int,
pub header_line_height: c_int,
pub window_end_pos: ptrdiff_t,
pub window_end_vpos: c_int,
// XXX: in Emacs, a bitfield of 16 booleans
pub flags: u16,
pub vscroll: c_int,
pub window_end_bytepos: ptrdiff_t,
}

/// Represents an Emacs buffer. For documentation see struct buffer in
/// buffer.h.
#[repr(C)]
Expand Down
1 change: 1 addition & 0 deletions rust_src/src/lib.rs
Expand Up @@ -147,6 +147,7 @@ pub extern "C" fn rust_init_syms() {
defsubr(&*buffers::Sbuffer_live_p);
defsubr(&*buffers::Sget_buffer);
defsubr(&*windows::Swindowp);
defsubr(&*windows::Swindow_live_p);
defsubr(&*lists::Satom);
defsubr(&*lists::Slistp);
defsubr(&*lists::Snlistp);
Expand Down
5 changes: 5 additions & 0 deletions rust_src/src/lisp.rs
Expand Up @@ -15,6 +15,7 @@ use multibyte::{Codepoint, LispStringRef, MAX_CHAR};
use symbols::LispSymbolRef;
use vectors::LispVectorlikeRef;
use buffers::LispBufferRef;
use windows::LispWindowRef;
use marker::LispMarkerRef;

use remacs_sys::{EmacsInt, EmacsUint, EmacsDouble, VALMASK, VALBITS, INTTYPEBITS, INTMASK,
Expand Down Expand Up @@ -461,6 +462,10 @@ impl LispObject {
})
}

pub fn as_window(self) -> Option<LispWindowRef> {
self.as_vectorlike().map_or(None, |v| v.as_window())
}

pub fn is_frame(self) -> bool {
self.as_vectorlike().map_or(false, |v| {
v.is_pseudovector(PseudovecType::PVEC_FRAME)
Expand Down
10 changes: 10 additions & 0 deletions rust_src/src/vectors.rs
Expand Up @@ -11,6 +11,7 @@ use lisp::{ExternalPtr, LispObject};
use multibyte::MAX_CHAR;
use lists::{sort_list, inorder};
use buffers::LispBufferRef;
use windows::LispWindowRef;
use remacs_sys::{Qsequencep, EmacsInt, wrong_type_argument, error, PSEUDOVECTOR_FLAG,
PVEC_TYPE_MASK, PSEUDOVECTOR_AREA_BITS, PSEUDOVECTOR_SIZE_MASK, PseudovecType,
Lisp_Vectorlike, Lisp_Vector, Lisp_Bool_Vector, MOST_POSITIVE_FIXNUM};
Expand Down Expand Up @@ -63,6 +64,15 @@ impl LispVectorlikeRef {
None
}
}

#[inline]
pub fn as_window(&self) -> Option<LispWindowRef> {
if self.is_pseudovector(PseudovecType::PVEC_WINDOW) {
Some(unsafe { mem::transmute(*self) })
} else {
None
}
}
}

impl LispVectorRef {
Expand Down
21 changes: 20 additions & 1 deletion rust_src/src/windows.rs
@@ -1,11 +1,30 @@
//! Functions operating on windows.

use lisp::LispObject;
use lisp::{LispObject, ExternalPtr};
use remacs_macros::lisp_fn;
use remacs_sys::Lisp_Window;

pub type LispWindowRef = ExternalPtr<Lisp_Window>;

impl LispWindowRef {
// Check if window is live
#[inline]
pub fn is_live(self) -> bool {
LispObject::from_raw(self.contents).is_buffer()
}
}

/// Return t if OBJECT is a window and nil otherwise.
#[lisp_fn]
fn windowp(object: LispObject) -> LispObject {
LispObject::from_bool(object.is_window())
}

/// Return t if OBJECT is a live window and nil otherwise.
///
/// A live window is a window that displays a buffer.
/// Internal windows and deleted windows are not live.
#[lisp_fn]
pub fn window_live_p(object: LispObject) -> LispObject {
LispObject::from_bool(object.as_window().map_or(false, |m| m.is_live()))
}
10 changes: 0 additions & 10 deletions src/window.c
Expand Up @@ -310,15 +310,6 @@ window. Windows that have been deleted are not valid. */)
return WINDOW_VALID_P (object) ? Qt : Qnil;
}

DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
doc: /* Return t if OBJECT is a live window and nil otherwise.
A live window is a window that displays a buffer.
Internal windows and deleted windows are not live. */)
(Lisp_Object object)
{
return WINDOW_LIVE_P (object) ? Qt : Qnil;
}

/* Frames and windows. */
DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
doc: /* Return the frame that window WINDOW is on.
Expand Down Expand Up @@ -7731,7 +7722,6 @@ displayed after a scrolling operation to be somewhat inaccurate. */);
defsubr (&Sminibuffer_window);
defsubr (&Swindow_minibuffer_p);
defsubr (&Swindow_valid_p);
defsubr (&Swindow_live_p);
defsubr (&Swindow_frame);
defsubr (&Sframe_root_window);
defsubr (&Sframe_first_window);
Expand Down