rune/
buffer.rs

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//! Buffer operations.
use crate::{
    core::{
        env::{Env, INTERNED_SYMBOLS},
        error::{Type, TypeError},
        gc::{Context, Rt},
        object::{Gc, LispBuffer, Object, ObjectType, OptionalFlag, NIL},
    },
    fns::slice_into_list,
};
use anyhow::{bail, Result};
use rune_core::hashmap::HashMap;
use rune_macros::defun;
use std::sync::LazyLock;
use std::sync::Mutex;

type BufferMap = HashMap<String, &'static LispBuffer>;
// static hashmap containing all the buffers
pub(crate) static BUFFERS: LazyLock<Mutex<BufferMap>> = LazyLock::new(Mutex::default);

#[defun]
pub(crate) fn set_buffer<'ob>(
    buffer_or_name: Object<'ob>,
    env: &mut Rt<Env>,
    cx: &'ob Context,
) -> Result<Object<'ob>> {
    let buffer = resolve_buffer(buffer_or_name, cx)?;
    env.set_buffer(buffer);
    Ok(cx.add(buffer))
}

fn resolve_buffer<'ob>(buffer_or_name: Object, cx: &'ob Context) -> Result<&'ob LispBuffer> {
    match buffer_or_name.untag() {
        ObjectType::Buffer(b) => Ok(b),
        ObjectType::String(name) => {
            let buffer_list = BUFFERS.lock().unwrap();
            let Some(buffer) = buffer_list.get(name.as_ref()) else {
                bail!("No buffer named {}", name);
            };
            Ok(cx.bind(*buffer))
        }
        x => Err(TypeError::new(Type::String, x).into()),
    }
}

#[defun]
fn set_buffer_modified_p(flag: Object) -> Object {
    // TODO: implement
    flag
}

#[defun]
fn buffer_live_p(buffer: Object, env: &Rt<Env>) -> bool {
    match buffer.untag() {
        ObjectType::Buffer(b) => env.with_buffer(b, |_| {}).is_ok(),
        _ => false,
    }
}

#[defun]
fn buffer_name(buffer: Option<Gc<&LispBuffer>>, env: &Rt<Env>) -> Result<String> {
    match buffer {
        Some(buffer) => env.with_buffer(buffer.untag(), |b| b.name.to_string()),
        None => Ok(env.current_buffer.get().name.to_string()),
    }
}

#[defun]
fn rename_buffer(newname: &str, unique: OptionalFlag, env: &mut Rt<Env>) -> Result<String> {
    let buf = env.current_buffer.get_mut();
    if buf.name == newname {
        return Ok(newname.to_string());
    }
    let mut buffer_list = BUFFERS.lock().unwrap();
    let mut replace_buffer = |buffer_list: &mut HashMap<_, _>, newname: &str| {
        let buffer = buffer_list.remove(&buf.name).unwrap();
        buffer_list.insert(newname.into(), buffer);
        buf.name = newname.to_string();
    };
    if buffer_list.contains_key(newname) {
        // there is already a buffer with newname
        if unique.is_none() {
            bail!("rename-buffer failed: Buffer name {newname} is in use");
        }
        let newname = unique_buffer_name(newname, None, &buffer_list);
        replace_buffer(&mut buffer_list, &newname);
        Ok(newname)
    } else {
        replace_buffer(&mut buffer_list, newname);
        Ok(newname.to_string())
    }
}

#[defun]
pub(crate) fn get_buffer_create<'ob>(
    buffer_or_name: Object<'ob>,
    _inhibit_buffer_hooks: Option<Object>,
    cx: &'ob Context,
) -> Result<Object<'ob>> {
    match buffer_or_name.untag() {
        ObjectType::String(name) => {
            let mut buffer_list = BUFFERS.lock().unwrap();
            match buffer_list.get(name.as_ref()) {
                Some(b) => Ok(cx.add(*b)),
                None => {
                    // If not already in the global buffer list, create a new
                    // buffer and add it
                    let buffer: &'static _ = {
                        let global = INTERNED_SYMBOLS.lock().unwrap();
                        let buffer = global.create_buffer(name);
                        // SAFETY: This can be 'static because it is stored in the
                        // global block. Eventually it will be garbage collected
                        unsafe { &*(buffer as *const LispBuffer) }
                    };
                    buffer_list.insert(name.to_string(), buffer);
                    Ok(cx.add(buffer))
                }
            }
        }
        ObjectType::Buffer(_) => Ok(buffer_or_name),
        other => Err(TypeError::new(Type::BufferOrName, other).into()),
    }
}

#[defun]
pub(crate) fn get_buffer<'ob>(
    buffer_or_name: Object<'ob>,
    cx: &'ob Context,
) -> Result<Object<'ob>> {
    match buffer_or_name.untag() {
        ObjectType::String(name) => {
            let buffer_list = BUFFERS.lock().unwrap();
            match buffer_list.get(name.as_ref()) {
                Some(b) => Ok(cx.add(*b)),
                None => Ok(NIL),
            }
        }
        ObjectType::Buffer(_) => Ok(buffer_or_name),
        other => Err(TypeError::new(Type::BufferOrName, other).into()),
    }
}

/// Return a string that is the name of no existing buffer based on NAME.
///
/// If there is no live buffer named NAME, then return NAME.
/// Otherwise modify name by appending \<NUMBER\>, incrementing NUMBER
/// (starting at 2) until an unused name is found, and then return that name.
/// Optional second argument IGNORE specifies a name that is okay to use (if
/// it is in the sequence to be tried) even if a buffer with that name exists.
///
/// If NAME begins with a space (i.e., a buffer that is not normally
/// visible to users), then if buffer NAME already exists a random number
/// is first appended to NAME, to speed up finding a non-existent buffer.
#[defun]
pub(crate) fn generate_new_buffer_name(name: &str, ignore: Option<&str>) -> String {
    unique_buffer_name(name, ignore, &BUFFERS.lock().unwrap())
}

fn unique_buffer_name(name: &str, ignore: Option<&str>, buffer_list: &BufferMap) -> String {
    let valid_name =
        |name: &str| ignore.is_some_and(|x| x == name) || !buffer_list.contains_key(name);

    let mut new_name = name.to_string();
    let mut number = 2;

    // check if the name exists
    while !valid_name(&new_name) {
        if name.starts_with(' ') {
            // use rand to find uniq names faster
            let rand = rand::random::<u32>();
            new_name = format!("{name}-{rand}");
        } else {
            new_name = format!("{name}<{number}>");
            number += 1;
        }
    }
    new_name
}

#[defun]
fn kill_buffer(buffer_or_name: Option<Object>, cx: &Context, env: &mut Rt<Env>) -> bool {
    match buffer_or_name {
        Some(buffer) => match resolve_buffer(buffer, cx) {
            Ok(b) => env.with_buffer_mut(b, |b| b.kill()).unwrap_or(false),
            Err(_) => false,
        },
        None => {
            let killed = env.current_buffer.get_mut().kill();
            // todo, we need to select a new buffer
            env.current_buffer.release();
            killed
        }
    }
}

#[defun]
fn buffer_base_buffer(_buffer: OptionalFlag) -> bool {
    // TODO: implement indirect buffers
    false
}

#[defun]
fn get_file_buffer(_filename: &str) -> bool {
    // TODO: implement file buffers
    false
}

#[defun]
fn buffer_list<'ob>(_frame: OptionalFlag, cx: &'ob Context) -> Object<'ob> {
    // TODO: implement frame parameter
    // TODO: remove this temp vector
    let mut buffer_list: Vec<Object> = Vec::new();
    for buffer in BUFFERS.lock().unwrap().values() {
        buffer_list.push(cx.add(*buffer));
    }
    slice_into_list(&buffer_list, None, cx)
}

// TODO: buffer local
defvar!(FILL_COLUMN, 70);
defvar!(INDENT_TABS_MODE);
defvar!(LEFT_MARGIN, 0);
defvar!(INHIBIT_COMPACTING_FONT_CACHES);
defvar!(NO_UPDATE_AUTOLOADS);
defvar!(TAB_WIDTH, 8);
defvar!(TRUNCATE_LINES);
defvar!(WORD_WRAP);
defvar!(BIDI_DISPLAY_REORDERING);
defvar!(BUFFER_FILE_NAME);

#[cfg(test)]
mod test {
    use super::*;
    use crate::core::gc::RootSet;

    #[test]
    fn test_gen_new_buffer_name() {
        let roots = &RootSet::default();
        let cx = &mut Context::new(roots);

        let name = "gen_buffer_test";
        let new_name = generate_new_buffer_name(name, None);
        assert_eq!(new_name, "gen_buffer_test");

        get_buffer_create(cx.add(name), Some(NIL), cx).unwrap();
        let new_name = generate_new_buffer_name(name, None);
        assert_eq!(new_name, "gen_buffer_test<2>");

        get_buffer_create(cx.add("gen_buffer_test<2>"), Some(NIL), cx).unwrap();
        let new_name = generate_new_buffer_name(name, None);
        assert_eq!(new_name, "gen_buffer_test<3>");

        let new_name = generate_new_buffer_name(name, Some("gen_buffer_test<2>"));
        assert_eq!(new_name, "gen_buffer_test<2>");

        let new_name = generate_new_buffer_name(" gen_buffer_test", None);
        assert_eq!(new_name, " gen_buffer_test");

        get_buffer_create(cx.add(" gen_buffer_test"), Some(NIL), cx).unwrap();
        let new_name = generate_new_buffer_name(" gen_buffer_test", None);
        assert!(new_name.starts_with(" gen_buffer_test-"));
    }

    #[test]
    fn test_create_buffer() {
        let roots = &RootSet::default();
        let cx = &mut Context::new(roots);
        let buffer = get_buffer_create(cx.add("test_create_buffer"), Some(NIL), cx).unwrap();
        assert!(matches!(buffer.untag(), ObjectType::Buffer(_)));
    }
}