Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! These are macros to make getting very nested fields in the `Editor` struct easier
//! These are macros instead of functions because functions will have to take `&mut self`
//! However, rust doesn't know that you only want a partial borrow instead of borrowing the
//! entire struct which `&mut self` says.  This makes it impossible to do other mutable
//! stuff to the struct because it is already borrowed. Because macros are expanded,
//! this circumvents the problem because it is just like indexing fields by hand and then
//! putting a `&mut` in front of it. This way rust can see that we are only borrowing a
//! part of the struct and not the entire thing.

/// Get the current view and document mutably as a tuple.
/// Returns `(&mut View, &mut Document)`
#[macro_export]
macro_rules! current {
    ($editor:expr) => {{
        let view = $crate::view_mut!($editor);
        let id = view.doc;
        let doc = $editor.documents.get_mut(&id).unwrap();
        (view, doc)
    }};
}

#[macro_export]
macro_rules! current_ref {
    ($editor:expr) => {{
        let view = $editor.tree.get($editor.tree.focus);
        let doc = &$editor.documents[&view.doc];
        (view, doc)
    }};
}

/// Get the current document mutably.
/// Returns `&mut Document`
#[macro_export]
macro_rules! doc_mut {
    ($editor:expr) => {{
        $crate::current!($editor).1
    }};
}

/// Get the current view mutably.
/// Returns `&mut View`
#[macro_export]
macro_rules! view_mut {
    ($editor:expr) => {{
        $editor.tree.get_mut($editor.tree.focus)
    }};
}

/// Get the current view immutably
/// Returns `&View`
#[macro_export]
macro_rules! view {
    ($editor:expr) => {{
        $editor.tree.get($editor.tree.focus)
    }};
}

#[macro_export]
macro_rules! doc {
    ($editor:expr) => {{
        $crate::current_ref!($editor).1
    }};
}

// Keymap macros

#[macro_export]
macro_rules! key {
    ($key:ident) => {
        $crate::input::KeyEvent {
            code: $crate::keyboard::KeyCode::$key,
            modifiers: $crate::keyboard::KeyModifiers::NONE,
        }
    };
    ($($ch:tt)*) => {
        $crate::input::KeyEvent {
            code: $crate::keyboard::KeyCode::Char($($ch)*),
            modifiers: $crate::keyboard::KeyModifiers::NONE,
        }
    };
}

#[macro_export]
macro_rules! shift {
    ($key:ident) => {
        $crate::input::KeyEvent {
            code: $crate::keyboard::KeyCode::$key,
            modifiers: $crate::keyboard::KeyModifiers::SHIFT,
        }
    };
    ($($ch:tt)*) => {
        $crate::input::KeyEvent {
            code: $crate::keyboard::KeyCode::Char($($ch)*),
            modifiers: $crate::keyboard::KeyModifiers::SHIFT,
        }
    };
}

#[macro_export]
macro_rules! ctrl {
    ($key:ident) => {
        $crate::input::KeyEvent {
            code: $crate::keyboard::KeyCode::$key,
            modifiers: $crate::keyboard::KeyModifiers::CONTROL,
        }
    };
    ($($ch:tt)*) => {
        $crate::input::KeyEvent {
            code: $crate::keyboard::KeyCode::Char($($ch)*),
            modifiers: $crate::keyboard::KeyModifiers::CONTROL,
        }
    };
}

#[macro_export]
macro_rules! alt {
    ($key:ident) => {
        $crate::input::KeyEvent {
            code: $crate::keyboard::KeyCode::$key,
            modifiers: $crate::keyboard::KeyModifiers::ALT,
        }
    };
    ($($ch:tt)*) => {
        $crate::input::KeyEvent {
            code: $crate::keyboard::KeyCode::Char($($ch)*),
            modifiers: $crate::keyboard::KeyModifiers::ALT,
        }
    };
}