logo
  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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Utility macros for use in the rest of penrose.

/// kick off an external program as part of a key/mouse binding.
///
/// NOTE: this explicitly redirects stderr to /dev/null
///
/// ```no_run
/// # #[macro_use] extern crate penrose;
/// # use penrose::__test_helpers::*;
/// # fn example() -> TestKeyHandler {
/// # Box::new(
/// run_external!("dmenu_run")
/// # )}
/// ```
#[macro_export]
macro_rules! run_external {
    ($cmd:tt) => {{
        Box::new(move |_: &mut $crate::core::manager::WindowManager<_>| {
            $crate::core::helpers::spawn($cmd)
        }) as $crate::core::bindings::KeyEventHandler<_>
    }};
}

/// Kick off an internal method on the window manager as part of a key binding
///
/// ```no_run
/// # #[macro_use] extern crate penrose;
/// # use penrose::__test_helpers::*;
/// # fn example() -> TestKeyHandler {
/// # Box::new(
/// run_internal!(cycle_client, Forward)
/// # )}
/// ```
#[macro_export]
macro_rules! run_internal {
    ($func:ident) => {
        Box::new(|wm: &mut $crate::core::manager::WindowManager<_>| {
            wm.$func()
        }) as $crate::core::bindings::KeyEventHandler<_>
    };

    ($func:ident, $($arg:expr),+) => {
        Box::new(move |wm: &mut $crate::core::manager::WindowManager<_>| {
            wm.$func($($arg),+)
        }) as $crate::core::bindings::KeyEventHandler<_>
    };
}

/// Helper for spawning external processes and ignoring the output
#[macro_export]
macro_rules! spawn {
    { $cmd:expr } => {
        $crate::core::helpers::spawn($cmd)
    };

    { $cmd:expr, $($arg:expr),+ } => {
        $crate::core::helpers::spawn_with_args($cmd, &[$($arg),+])
    };
}

/// Helper for spawning external processes and capturing the output
#[macro_export]
macro_rules! spawn_for_output {
    { $cmd:expr } => {
        $crate::core::helpers::spawn_for_output($cmd).map(|s|
            s.trim().split('\n').map(String::from).collect::<Vec<String>>()
        )
    };

    { $cmd:expr, $($arg:expr),+ } => {
        $crate::core::helpers::spawn_for_output_with_args($cmd, &[$($arg),+]).map(|s|
            s.trim().split('\n').map(String::from).collect::<Vec<String>>()
        )
    };
}

/// Make creating a HashMap a little less verbose
///
/// ```
/// # #[macro_use] extern crate penrose;
/// map! {
///     1 => "one",
///     2 => "two",
///     3 => "three",
/// };
/// ```
#[macro_export]
macro_rules! map {
    {} => { ::std::collections::HashMap::new() };

    { $($key:expr => $value:expr),+, } => {
        {
            let mut _map = ::std::collections::HashMap::new();
            $(_map.insert($key, $value);)+
            _map
        }
    };
}

/// Generate user keybindings with optional compile time validation.
///
/// # Example
///
/// ```no_run
/// # #[macro_use] extern crate penrose;
/// # use penrose::__test_helpers::*;
/// # fn example() -> TestKeyBindings {
/// let key_bindings = gen_keybindings! {
///     "M-semicolon" => run_external!("dmenu_run");
///     "M-Return" => run_external!("alacritty");
///     "XF86AudioMute" => run_external!("amixer set Master toggle");
///     "M-A-Escape" => run_internal!(exit);
///
///     "M-j" => run_internal!(cycle_client, Forward);
///     "M-k" => run_internal!(cycle_client, Backward);
///     "M-S-j" => run_internal!(drag_client, Forward);
///     "M-S-k" => run_internal!(drag_client, Backward);
///     "M-S-q" => run_internal!(kill_client);
///
///     "M-Tab" => run_internal!(toggle_workspace);
///
///     "M-grave" => run_internal!(cycle_layout, Forward);
///     "M-S-grave" => run_internal!(cycle_layout, Backward);
///     "M-A-Up" => run_internal!(update_max_main, More);
///     "M-A-Down" => run_internal!(update_max_main, Less);
///     "M-A-Right" => run_internal!(update_main_ratio, More);
///     "M-A-Left" => run_internal!(update_main_ratio, Less);
///
///     map: { "1", "2", "3", "4", "5", "6", "7", "8", "9" } to index_selectors(9) => {
///         "M-{}" => focus_workspace (REF);
///         "M-S-{}" => client_to_workspace (REF);
///     };
/// };
/// # key_bindings }
/// ```
///
/// # Sections
///
/// ### Direct binding
///
/// ```no_run
/// # #[macro_use] extern crate penrose;
/// # use penrose::__test_helpers::*;
/// # fn example() -> TestKeyBindings {
/// # gen_keybindings! {
/// "M-j" => run_internal!(cycle_client, Forward);
/// "M-S-j" => run_internal!(drag_client, Forward);
/// "M-Return" => run_external!("alacritty");
/// # }};
/// ```
///
/// This is what the majority of your keybindings will look like.
///
/// Should be a string literal and an expression that satisfies the [KeyEventHandler][1] type. The
/// [run_internal] and [run_external] helper macros can be used for simplifying bindings that
/// perform common actions like spawning external programs or triggering methods on the
/// [WindowManager][2].
///
/// ### Map block
///
/// Bind a common pattern via a template.
///
/// ```no_run
/// # #[macro_use] extern crate penrose;
/// # use penrose::__test_helpers::*;
/// # fn example() -> TestKeyBindings {
/// # gen_keybindings! {
/// // VAL: values are passed to the method directly
/// map: { "Up", "Down" } to vec![More, Less] => {
///     "M-{}" => update_max_main (VAL);
/// };
///
/// // REF: values are passed to the method as references
/// map: { "1", "2", "3", "4", "5", "6", "7", "8", "9" } to index_selectors(9) => {
///     "M-{}" => focus_workspace (REF);
///     "M-S-{}" => client_to_workspace (REF);
/// };
/// # }};
/// ```
///
/// When you have a common pattern for multiple key bindings (such as focusing a given workspace)
/// you can use a `map` block to avoid having to write them all out explicitly. The required format
/// is as follows:
/// ```markdown
/// map: { "str", "literal", "key", "names" } to `impl Iterator` => {
///     "<modifiers>-{}" => `WindowManager method` ( arg, ... );
/// }
/// ```
///
/// Note that the key names _must_ be string literals, not just `&str` references. The arguments to
/// the [WindowManager][2] method can be passed by reference using `REF` or by value using `VAL`.
/// Any additional arguments can be passed explicitly if they are required by the method.
///
/// [1]: crate::core::bindings::KeyEventHandler
/// [2]: crate::core::manager::WindowManager
#[macro_export]
macro_rules! gen_keybindings {
    { $($tokens:tt)* } => {
        {
            let mut map = ::std::collections::HashMap::new();
            let codes = $crate::core::helpers::keycodes_from_xmodmap();
            let parse = $crate::xcb::helpers::parse_key_binding;
            __private!(@parsekey map, codes, parse, [], [], $($tokens)*);
            map
        }
    };
}

/// Make creating all of the mouse bindings less verbose
#[macro_export]
macro_rules! gen_mousebindings {
    {
        $($kind:ident $button:ident + [$($modifier:ident),+] => $action:expr),+
    } => {
        {
            // HashMap<(MouseEventKind, MouseState), MouseEventHandler>
            let mut _map = ::std::collections::HashMap::new();

            $(
                let mut modifiers = Vec::new();
                $(modifiers.push($crate::core::bindings::ModifierKey::$modifier);)+

                let state = $crate::core::bindings::MouseState::new(
                    $crate::core::bindings::MouseButton::$button,
                    modifiers
                );

                let kind = $crate::core::bindings::MouseEventKind::$kind;
                _map.insert(
                    (kind, state),
                    Box::new($action) as $crate::core::bindings::MouseEventHandler<_>
                );
            )+

            _map
        }
    };
}

/// Quickly create a simple string error
#[macro_export]
macro_rules! perror {
    ($msg:expr) => {
        $crate::PenroseError::Raw($msg.to_string())
    };

    ($template:expr, $($arg:expr),+) => {
        $crate::PenroseError::Raw(format!($template, $($arg),+))
    };
}

// Helper for converting Vec<String> -> &[&str]
macro_rules! str_slice {
    ($string_vec:expr) => {
        &$string_vec.iter().map(AsRef::as_ref).collect::<Vec<&str>>()
    };
}

// Helper for quickly converting args to debug strings
macro_rules! strings {
    { $($arg:expr),+ } => { vec![$(format!("{:?}", $arg)),+] }
}

macro_rules! cast_slice {
    ($s:expr, $t:ty) => {
        $s.iter().map(|&v| v as $t).collect::<Vec<$t>>()
    };
}

// Auto generate a struct and associated builder struct with getter methods
// on the generated (private) struct fields but no setters.
//
// NOTE: requires that you provide a `validate` method on the builder and
//       some way of getting an initial value of the real struct (i.e. impl
//       Default)
#[doc(hidden)]
#[macro_export]
macro_rules! __with_builder_and_getters {
    {
        $(#[$struct_outer:meta])*
        $name:ident;
        $(#[$builder_outer:meta])*
        $builder_name:ident;

        $(
            $(#[$field_outer:meta])*
            $(VecImplInto $vecintofield:ident : $vecintoty:ty;)?
            $(ImplInto $intofield:ident : $intoty:ty;)?
            $(ImplTry $errty:ty; $tryfield:ident : $tryty:ty;)?
            $(Concrete $field:ident : $ty:ty;)?
            => $default:expr;
        )+
    } => {
        $(#[$struct_outer])*
        pub struct $name {
            $(
                pub(crate)
                $($vecintofield : Vec<$vecintoty>,)?
                $($intofield : $intoty,)?
                $($tryfield : $tryty,)?
                $($field: $ty,)?
            )+
        }

        impl $name {
            /// Make a new associated builder struct containing the field values of this struct
            pub fn builder(&self) -> $builder_name {
                $builder_name {
                    inner: self.clone(),
                }
            }

            $(
                /// Obtain a reference to
                $(#[$field_outer])*
                $(pub fn $vecintofield(&self) -> &Vec<$vecintoty> {
                        &self.$vecintofield
                })?
                $(pub fn $intofield(&self) -> &$intoty {
                        &self.$intofield
                })?
                $(pub fn $tryfield(&self) -> &$tryty {
                        &self.$tryfield
                })?
                $(pub fn $field(&self) -> &$ty {
                        &self.$field
                })?
            )+
        }

        impl Default for $name {
            fn default() -> Self {
                Self {
                    $(
                        $($vecintofield: $default.into_iter().map(|e| e.into()).collect(),)?
                        $($intofield: $default.into(),)?
                        $($tryfield: $default.try_into().unwrap(),)?
                        $($field: $default,)?
                    )+
                }
            }
        }

        $(#[$builder_outer])*
        pub struct $builder_name {
            inner: $name,
        }

        impl $builder_name {
            /// Validate and build the underlying struct
            pub fn build(&self) -> std::result::Result<$name, String> {
                self.validate()?;
                Ok(self.inner.clone())
            }

            $(
                /// Set the value of
                $(#[$field_outer])*
                $(pub fn $vecintofield<T, U>(&mut self, val: T) -> &mut $builder_name
                where
                    T: IntoIterator<Item = U>,
                    U: Into<$vecintoty>,
                {
                    self.inner.$vecintofield = val.into_iter().map(|elem| elem.into()).collect();
                    self
                })?
                $(pub fn $intofield<T>(&mut self, val: T) -> &mut $builder_name
                where
                    T: Into<$intoty>
                {
                    self.inner.$intofield = val.into();
                    self
                })?
                $(pub fn $tryfield<T>(&mut self, val: T) -> crate::Result<&mut $builder_name>
                where
                    T: TryInto<$tryty, Error=$errty>,
                {
                    self.inner.$tryfield = val.try_into()?;
                    Ok(self)
                })?
                $(pub fn $field(&mut self, val: $ty) -> &mut $builder_name {
                    self.inner.$field = val;
                    self
                })?
            )+
        }
    }
}

// __impl_stub_xcon! {
//     for Foo;

//     client_properties: {}
//     client_handler: {}
//     client_config: {}
//     event_handler: {}
//     state: {}
//     conn: {}
// }
#[doc(hidden)]
#[macro_export]
macro_rules! __impl_stub_xcon {
    {
        for $struct:ident;

        atom_queries: { $($atomquery:tt)* }
        client_properties: { $($cprops:tt)* }
        client_handler: { $($chandler:tt)* }
        client_config: { $($cconfig:tt)* }
        event_handler: { $($ehandler:tt)* }
        state: { $($state:tt)* }
        conn: { $($conn:tt)* }
    } => {
        impl $crate::core::xconnection::StubXAtomQuerier for $struct { $($atomquery)* }
        impl $crate::core::xconnection::StubXClientProperties for $struct { $($cprops)* }
        impl $crate::core::xconnection::StubXClientHandler for $struct { $($chandler)* }
        impl $crate::core::xconnection::StubXClientConfig for $struct { $($cconfig)* }
        impl $crate::core::xconnection::StubXEventHandler for $struct { $($ehandler)* }
        impl $crate::core::xconnection::StubXState for $struct { $($state)* }
        impl $crate::core::xconnection::StubXConn for $struct { $($conn)* }
    }
}

#[doc(hidden)]
#[macro_export]
macro_rules! test_cases {
    {
        $test_name:ident;
        args: ( $($arg:ident: $t:ty),* $(,)? );

        $(
            case: $case_name:ident => ( $($param:expr),* );
        )+
        body: $body:expr
    } => {
        paste::paste! {
            fn [<$test_name _helper>]($($arg: $t),*) {
                $body
            }
        }

        $(
            paste::paste! {
                #[test]
                fn [<$test_name _ $case_name>]() {
                    [<$test_name _helper>]($($param),*)
                }
            }
        )+
    };
}

// Helper to avoid polluting the documented patterns in other public macros
#[doc(hidden)]
#[macro_export]
macro_rules! __private {

    /*
     *  @parsekey :: handle each of the valid cases in an invocation of gen_keybindings
     */

    {   @parsekey $map:expr, $codes:expr, $parse:expr,
        [ $($patt:expr,)* ], [ $(($($template:expr),+; $($name:expr),+)),* ],
        map: { $($str:expr),+ } to $to:expr => {
            $( $binding:expr => $method:ident ( $($params:tt)* ); )+
        };
        $($tail:tt)*
    } => {
        {
            let keynames = &[$($str),+];
            $(
                for (name, arg) in keynames.iter().zip($to.into_iter()) {
                    let binding = format!($binding, name);
                    match $parse(binding.clone(), &$codes) {
                        None => panic!("invalid key binding: {}", binding),
                        Some(key_code) => $map.insert(
                            key_code,
                            run_internal!(
                                $method,
                                __private!(@parsemapparams arg; []; $($params,)*)
                            )
                        ),
                    };
                }
            )+

            __private!(@parsekey $map, $codes, $parse,
                [ $($patt,)* ], [ $(($($template),+; $($name),+),)* ($($binding),+; $($str),+) ],
                $($tail)*
            );
        }
    };

    // parse a single simple key binding (validated if $validate is true)
    {   @parsekey $map:expr, $codes:expr, $parse:expr,
        [ $($patt:expr,)* ], [ $(($($template:expr),+; $($name:expr),+)),* ],
        $binding:expr => $action:expr;
        $($tail:tt)*
    } => {
        match $parse($binding.to_string(), &$codes) {
            None => panic!("invalid key binding: {}", $binding),
            Some(key_code) => $map.insert(key_code, $action),
        };
        __private!(@parsekey $map, $codes, $parse,
            [ $binding, $($patt,)* ], [ $(($($template),+; $($name),+)),* ],
            $($tail)*
        );
    };

    // TODO: remove this target in 0.2.2
    {   @parsekey $map:expr, $codes:expr, $parse:expr,
        [ $($patt:expr,)* ], [ $(($($template:expr),+; $($name:expr),+)),* ],
        $(refmap)? $(map)? [ $from:expr ] in { $($binding:expr => $method:ident [ $to:expr ];)+ };
        $($tail:tt)*
    } => {
        compile_error!(
            "the '(ref)map [ <impl Iterator> ] in { ... }' pattern is deprecated: please use \
            'map: { ... } to <impl Iterator> => { ... }' instead. Examples are available in \
            the documentation for the 'gen_keybindings' macro."
        )
    };

    // base case (should be out of tokens)
    {   @parsekey $map:expr, $codes:expr, $parse:expr,
        [ $($patt:expr,)* ], [ $(($($template:expr),+; $($name:expr),+)),* ],
        $($tail:tt)*
    } => {
        $(compile_error!(stringify!("unexpected tokens in gen_keybindings macro: " $tail));)*
        validate_user_bindings!(
            ( $($patt),* )
            ( $((($($template),+) ($($name),+)))* )
        )
    };

    /*
     *  @parsemapparams :: run variable replacement for a `map` block in `gen_keybindings`
     */

    { @parsemapparams $replacement:expr; [ $(,$arg:expr)* ];
      REF, $($params:tt)*
    } => {
        __private!(@parsemapparams $replacement; [$($arg),* , &$replacement]; $($params)*)
    };

    { @parsemapparams $replacement:expr; [ $(,$arg:expr)* ];
      VAL, $($params:tt)*
    } => {
        __private!(@parsemapparams $replacement; [$($arg),* , $replacement]; $($params)*)
    };

    { @parsemapparams $replacement:expr; [ $(,$arg:expr),* ];
      $expr:expr, $($params:tt)*
    } => {
        __private!(@parsemapparams $replacement; [$($arg),* , $expr]; $($params)*)
    };

    { @parsemapparams $replacement:expr; [ $(,$arg:expr)* ]; } => { $($arg),* };
}