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
macro_rules! raw_fn {
    ($env:ident, $name:ident) => { {
        (*$env.raw).$name.expect(stringify!(Required module function does not exist: $name))
    }};
}

macro_rules! raw_call_no_exit {
    ($env:ident, $name:ident $(, $args:expr)*) => {
        unsafe {
            // println!("raw_call_no_exit {:?}", stringify!($name));
            let $name = raw_fn!($env, $name);
            $name($env.raw $(, $args)*)
        }
    };
}

macro_rules! raw_call {
    ($env:expr, $name:ident $(, $args:expr)*) => {
        {
            // println!("raw_call {:?}", stringify!($name));
            let env = $env;
            let result = unsafe {
                let $name = raw_fn!(env, $name);
                $name(env.raw $(, $args)*)
            };
            env.handle_exit(result)
        }
    };
}

macro_rules! raw_call_value {
    ($env:ident, $name:ident $(, $args:expr)*) => {
        {
            // println!("raw_call_value {:?}", stringify!($name));
            let result: $crate::Result<$crate::raw::emacs_value> = raw_call!($env, $name $(, $args)*);
            result.map(|raw| unsafe {
                // TODO: In some cases, we can get away without protection. Try optimizing them.
                $crate::Value::new_protected(raw, $env)
            })
        }
    };
}

// TODO: Export this.
macro_rules! call_lisp {
    ($env:ident, $name:expr $(, $arg:expr)*) => {
        {
            // println!("call_lisp {:?}", $name);
            let symbol: $crate::Value<'_> = $env.intern($name)?;
            let args = &mut [$($arg.raw,)*];
            raw_call_value!($env, funcall, symbol.raw, args.len() as $crate::deps::libc::ptrdiff_t, args.as_mut_ptr())
        }
    };
}

macro_rules! enable_transfers {
    ($($name:ident;)*) => {$(
        impl<T> $crate::Transfer for $name<T> {
            fn type_name() -> &'static str { stringify!($name) }
        }

        impl<'e, T> $crate::IntoLisp<'e> for $name<T> {
            fn into_lisp(self, env: &$crate::Env) -> $crate::Result<$crate::Value<'_>> {
                ::std::boxed::Box::new(self).into_lisp(env)
            }
        }
    )*};
}

/// Declares that this module is GPL-compatible. Emacs will not load it otherwise.
#[macro_export]
#[allow(non_snake_case)]
macro_rules! plugin_is_GPL_compatible {
    () => {
        /// This states that the module is GPL-compliant.
        /// Emacs won't load the module if this symbol is undefined.
        #[no_mangle]
        #[allow(non_upper_case_globals)]
        pub static plugin_is_GPL_compatible: $crate::deps::libc::c_int = 0;
    };
}

// TODO: Deprecate this in favor of #[module].

/// Registers a function as the initialization hook. [`#[module]`] is preferred over this low-level
/// interface.
///
/// This declares `emacs_module_init` and `emacs_rs_module_init`, by wrapping the given function,
/// whose signature must be `fn(&Env) -> Result<Value>`.
///
/// [`#[module]`]: ../emacs_macros/attr.module.html
#[macro_export]
macro_rules! module_init {
    ($init:ident) => {
        /// Entry point for Emacs's module loader.
        #[no_mangle]
        pub unsafe extern "C" fn emacs_module_init(
            runtime: *mut $crate::raw::emacs_runtime,
        ) -> $crate::deps::libc::c_int {
            $crate::func::HandleInit::handle_init($crate::Env::from_runtime(runtime), $init)
        }

        // TODO: Exclude this in release build.
        /// Entry point for live-reloading (by `rs-module`) during development.
        #[no_mangle]
        pub unsafe extern "C" fn emacs_rs_module_init(
            raw: *mut $crate::raw::emacs_env,
        ) -> $crate::deps::libc::c_int {
            $crate::func::HandleInit::handle_init($crate::Env::new(raw), $init)
        }
    };
}

// TODO: Consider making this a function, using `data` to do the actual routing, like in
// https://github.com/Wilfred/remacs/pull/516.
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! lambda {
    // Default function-specific data is (unused) null pointer.
    ($env:expr, $func:path, $arities:expr, $doc:expr $(,)*) => {
        lambda!($env, $func, $arities, $doc, ::std::ptr::null_mut())
    };

    // Default doc string is empty.
    ($env:expr, $func:path, $arities:expr $(,)*) => {
        lambda!($env, $func, $arities, "")
    };

    // Declare a wrapper function.
    ($env:expr, $func:path, $arities:expr, $doc:expr, $data:expr $(,)*) => {{
        use $crate::func::HandleCall;
        use $crate::func::Manage;
        // TODO: Generate identifier from $func.
        unsafe extern "C" fn extern_lambda(
            env: *mut $crate::raw::emacs_env,
            nargs: $crate::deps::libc::ptrdiff_t,
            args: *mut $crate::raw::emacs_value,
            data: *mut $crate::deps::libc::c_void,
        ) -> $crate::raw::emacs_value {
            let env = $crate::Env::new(env);
            let env = $crate::CallEnv::new(env, nargs, args, data);
            env.handle_call($func)
        }

        $env.make_function(extern_lambda, $arities, $doc, $data)
    }};
}

// TODO: Use `$crate::` instead of `local_inner_macros` once everyone is on 1.30.
// See https://doc.rust-lang.org/nightly/edition-guide/rust-2018/macros/macro-changes.html#macros-using-local_inner_macros.
/// Exports Rust functions to the Lisp runtime. [`#[defun]`] is preferred over this low-level
/// interface.
///
/// [`#[defun]`]: ../emacs_macros/attr.defun.html
#[macro_export(local_inner_macros)]
macro_rules! export_functions {
    // Cut trailing comma in top-level.
    ($env:expr, $prefix:expr, $mappings:tt,) => {
        export_functions!($env, $prefix, $mappings)
    };
    // Cut trailing comma in mappings.
    ($env:expr, $prefix:expr, {
        $( $name:expr => $declaration:tt ),+,
    }) => {
        export_functions!($env, $prefix, {
            $( $name => $declaration ),*
        })
    };
    // Expand each mapping.
    ($env:expr, $prefix:expr, {
        $( $name:expr => $declaration:tt ),*
    }) => {
        {
            use $crate::func::Manage;
            $( export_functions!(decl, $env, $prefix, $name, $declaration)?; )*
        }
    };

    // Cut trailing comma in declaration.
    (decl, $env:expr, $prefix:expr, $name:expr, ($func:path, $( $opt:expr ),+,)) => {
        export_functions!(decl, $env, $prefix, $name, ($func, $( $opt ),*))
    };
    // Create a function and set a symbol to it.
    (decl, $env:expr, $prefix:expr, $name:expr, ($func:path, $( $opt:expr ),+)) => {
        $env.fset(
            &_emacs_format!("{}{}", $prefix, $name),
            lambda!($env, $func, $($opt),*)?
        )
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! _emacs_format {
    ($($inner:tt)*) => {
        format!($($inner)*)
    }
}

#[deprecated(since = "0.7.0", note = "Please use `emacs::plugin_is_GPL_compatible!` instead")]
#[doc(hidden)]
#[macro_export(local_inner_macros)]
#[allow(non_snake_case)]
macro_rules! emacs_plugin_is_GPL_compatible {
    ($($inner:tt)*) => {
        plugin_is_GPL_compatible!($($inner)*);
    };
}

#[deprecated(since = "0.7.0", note = "Please use `#[emacs::module]` instead")]
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! emacs_module_init {
    ($($inner:tt)*) => {
        module_init!($($inner)*);
    };
}

#[deprecated(since = "0.7.0", note = "Please use `#[defun]` instead")]
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! emacs_export_functions {
    ($($inner:tt)*) => {
        export_functions!($($inner)*)
    };
}

#[deprecated(since = "0.7.0", note = "Please use `emacs::lambda!` instead")]
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! emacs_lambda {
    ($($inner:tt)*) => {
        lambda!($($inner)*)
    };
}