Skip to main content

zsh/zle/
thingy.rs

1//! ZLE thingies - named bindings to widgets
2//!
3//! Direct port from zsh/Src/Zle/zle_keymap.c thingy structures
4//!
5//! A "thingy" is a named entity that refers to a widget. Multiple thingies
6//! can refer to the same widget. Thingies are reference-counted.
7
8use std::sync::Arc;
9
10use super::widget::Widget;
11
12/// Flags for thingies
13#[derive(Debug, Clone, Copy, Default)]
14pub struct ThingyFlags {
15    /// Thingy is disabled
16    pub disabled: bool,
17    /// Can't refer to a different widget
18    pub immortal: bool,
19}
20
21/// A thingy - a named reference to a widget
22#[derive(Debug, Clone)]
23pub struct Thingy {
24    /// Name of the thingy
25    pub name: String,
26    /// Flags
27    pub flags: ThingyFlags,
28    /// Reference count (for compatibility, though Arc handles this)
29    pub rc: i32,
30    /// Widget this thingy refers to
31    pub widget: Option<Arc<Widget>>,
32}
33
34impl Thingy {
35    /// Create a new thingy with the given name
36    pub fn new(name: &str) -> Self {
37        Thingy {
38            name: name.to_string(),
39            flags: ThingyFlags::default(),
40            rc: 1,
41            widget: None,
42        }
43    }
44
45    /// Create a builtin thingy (references a builtin widget)
46    pub fn builtin(name: &str) -> Self {
47        let widget = Widget::builtin(name);
48        Thingy {
49            name: name.to_string(),
50            flags: ThingyFlags {
51                disabled: false,
52                immortal: true,
53            },
54            rc: 1,
55            widget: Some(Arc::new(widget)),
56        }
57    }
58
59    /// Create a user-defined thingy
60    pub fn user_defined(name: &str, func_name: &str) -> Self {
61        let widget = Widget::user_defined(name, func_name);
62        Thingy {
63            name: name.to_string(),
64            flags: ThingyFlags::default(),
65            rc: 1,
66            widget: Some(Arc::new(widget)),
67        }
68    }
69
70    /// Check if this thingy is a specific named widget
71    pub fn is(&self, name: &str) -> bool {
72        self.name == name
73    }
74
75    /// Check if this thingy is a specific widget or its dot-prefixed variant
76    /// (Used for checking against both "widget" and ".widget")
77    pub fn is_thingy(&self, name: &str) -> bool {
78        self.name == name || self.name == format!(".{}", name)
79    }
80}
81
82/// Standard thingy names used throughout ZLE
83pub mod names {
84    /// Accept and execute a line
85    pub const ACCEPT_LINE: &str = "accept-line";
86    /// Send break (abort)
87    pub const SEND_BREAK: &str = "send-break";
88    /// Insert character
89    pub const SELF_INSERT: &str = "self-insert";
90    /// Delete character or list completions
91    pub const DELETE_CHAR_OR_LIST: &str = "delete-char-or-list";
92    /// Backward delete character
93    pub const BACKWARD_DELETE_CHAR: &str = "backward-delete-char";
94    /// Move backward one character
95    pub const BACKWARD_CHAR: &str = "backward-char";
96    /// Move forward one character
97    pub const FORWARD_CHAR: &str = "forward-char";
98    /// Move to beginning of line
99    pub const BEGINNING_OF_LINE: &str = "beginning-of-line";
100    /// Move to end of line
101    pub const END_OF_LINE: &str = "end-of-line";
102    /// Move backward one word
103    pub const BACKWARD_WORD: &str = "backward-word";
104    /// Move forward one word
105    pub const FORWARD_WORD: &str = "forward-word";
106    /// Kill to end of line
107    pub const KILL_LINE: &str = "kill-line";
108    /// Kill whole line
109    pub const KILL_WHOLE_LINE: &str = "kill-whole-line";
110    /// Kill word forward
111    pub const KILL_WORD: &str = "kill-word";
112    /// Kill word backward
113    pub const BACKWARD_KILL_WORD: &str = "backward-kill-word";
114    /// Yank from kill ring
115    pub const YANK: &str = "yank";
116    /// Undo
117    pub const UNDO: &str = "undo";
118    /// Redo
119    pub const REDO: &str = "redo";
120    /// Clear screen
121    pub const CLEAR_SCREEN: &str = "clear-screen";
122    /// Expand or complete
123    pub const EXPAND_OR_COMPLETE: &str = "expand-or-complete";
124    /// History search backward
125    pub const HISTORY_INCREMENTAL_SEARCH_BACKWARD: &str = "history-incremental-search-backward";
126    /// History search forward
127    pub const HISTORY_INCREMENTAL_SEARCH_FORWARD: &str = "history-incremental-search-forward";
128    /// Up line or history
129    pub const UP_LINE_OR_HISTORY: &str = "up-line-or-history";
130    /// Down line or history
131    pub const DOWN_LINE_OR_HISTORY: &str = "down-line-or-history";
132    /// Transpose characters
133    pub const TRANSPOSE_CHARS: &str = "transpose-chars";
134    /// Delete character
135    pub const DELETE_CHAR: &str = "delete-char";
136    /// Vi command mode
137    pub const VI_CMD_MODE: &str = "vi-cmd-mode";
138    /// Vi insert mode
139    pub const VI_INSERT: &str = "vi-insert";
140}