use crate::text::motion::Motion;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Target {
Motion(Motion),
Object { key: char, around: bool },
Line,
Selection,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Root {
File,
Home,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Action {
Move(Motion),
SelectObject { key: char, around: bool },
SwapSelectionEnds,
Insert,
Append,
InsertLineStart,
AppendLineEnd,
OpenBelow,
OpenAbove,
Visual,
VisualLine,
NormalMode,
Command,
SearchForward,
SearchBackward,
Operator { op: Operator, target: Target },
DeleteChar,
DeleteCharBack,
DeleteToEol,
ChangeToEol,
YankLine,
SearchNext { reverse: bool },
RepeatFind { reverse: bool },
SearchWordUnderCursor,
AppendParagraph,
Window(char),
ToggleSplit { vertical: bool },
ClosePane,
OnlyPane,
PasteAfter,
PasteBefore,
ReplaceChar(char),
JoinLines,
ToggleCase,
ListItemMove { down: bool },
ListShift { outdent: bool },
Undo,
Redo,
Repeat,
ToggleBold,
ToggleItalic,
ToggleHighlight,
ToggleCode,
InsertLink,
ToggleTask,
AlignTable,
SetHeading(u8),
ClearHeading,
Save,
SaveStayInsert,
Quit { force: bool },
WriteQuit,
CycleFocus,
ToggleTypewriter,
ToggleZen,
ToggleNumbers,
ToggleConceal,
CycleAlign,
FileTree { root: Root },
FindFile { root: Root },
FindBuffer,
AlternateBuffer,
Help,
FollowLink,
OpenExternal,
Nop,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Operator {
Delete,
Change,
Yank,
Indent,
Outdent,
Lowercase,
Uppercase,
}
impl Action {
pub fn edits(&self) -> bool {
match self {
Action::Operator { op, .. } => *op != Operator::Yank,
Action::Insert
| Action::Append
| Action::InsertLineStart
| Action::AppendLineEnd
| Action::OpenBelow
| Action::OpenAbove
| Action::AppendParagraph
| Action::DeleteChar
| Action::DeleteCharBack
| Action::DeleteToEol
| Action::ChangeToEol
| Action::PasteAfter
| Action::PasteBefore
| Action::ReplaceChar(_)
| Action::JoinLines
| Action::ToggleCase
| Action::ListItemMove { .. }
| Action::ListShift { .. }
| Action::Undo
| Action::Redo
| Action::Repeat
| Action::ToggleBold
| Action::ToggleItalic
| Action::ToggleHighlight
| Action::ToggleCode
| Action::InsertLink
| Action::ToggleTask
| Action::AlignTable
| Action::SetHeading(_)
| Action::ClearHeading => true,
_ => false,
}
}
pub fn from_config_name(name: &str) -> Option<Action> {
Some(match name {
"" | "nop" | "none" => Action::Nop,
"save" | "write" => Action::Save,
"save_stay_insert" => Action::SaveStayInsert,
"save_quit" | "write_quit" | "wq" => Action::WriteQuit,
"quit" => Action::Quit { force: false },
"quit!" | "force_quit" => Action::Quit { force: true },
"insert" => Action::Insert,
"append" => Action::Append,
"insert_line_start" => Action::InsertLineStart,
"append_line_end" => Action::AppendLineEnd,
"open_below" => Action::OpenBelow,
"open_above" => Action::OpenAbove,
"normal_mode" | "escape" => Action::NormalMode,
"visual" => Action::Visual,
"visual_line" => Action::VisualLine,
"command" => Action::Command,
"search_forward" | "search" => Action::SearchForward,
"search_backward" => Action::SearchBackward,
"undo" => Action::Undo,
"redo" => Action::Redo,
"repeat" => Action::Repeat,
"paste_after" | "paste" => Action::PasteAfter,
"paste_before" => Action::PasteBefore,
"delete_char" => Action::DeleteChar,
"join_lines" => Action::JoinLines,
"list_item_down" => Action::ListItemMove { down: true },
"list_item_up" => Action::ListItemMove { down: false },
"list_indent" => Action::ListShift { outdent: false },
"list_outdent" => Action::ListShift { outdent: true },
"toggle_bold" | "bold" => Action::ToggleBold,
"toggle_italic" | "italic" => Action::ToggleItalic,
"toggle_highlight" | "highlight" => Action::ToggleHighlight,
"toggle_code" | "code" => Action::ToggleCode,
"insert_link" | "link" => Action::InsertLink,
"toggle_task" | "task" => Action::ToggleTask,
"align_table" | "table" => Action::AlignTable,
"clear_heading" => Action::ClearHeading,
"cycle_focus" | "focus" => Action::CycleFocus,
"toggle_typewriter" | "typewriter" => Action::ToggleTypewriter,
"toggle_conceal" | "conceal" => Action::ToggleConceal,
"toggle_zen" | "zen" => Action::ToggleZen,
"toggle_numbers" | "numbers" | "number" => Action::ToggleNumbers,
"cycle_align" | "align" => Action::CycleAlign,
"help" | "shortcuts" => Action::Help,
"file_tree" | "file_explorer" | "tree" => Action::FileTree { root: Root::File },
"file_tree_home" | "file_explorer_home" => Action::FileTree { root: Root::Home },
"find_file" | "fuzzy_find" | "files" => Action::FindFile { root: Root::File },
"find_file_home" | "fuzzy_find_home" => Action::FindFile { root: Root::Home },
"alternate_buffer" | "alternate" | "alt_buffer" => Action::AlternateBuffer,
"follow_link" | "follow" | "goto_file" => Action::FollowLink,
"open_external" | "open_url" | "xdg_open" => Action::OpenExternal,
"find_buffer" | "buffers" | "switch_buffer" => Action::FindBuffer,
"split_vertical" | "vsplit" => Action::ToggleSplit { vertical: true },
"split_horizontal" | "split" => Action::ToggleSplit { vertical: false },
"close_pane" => Action::ClosePane,
"only_pane" | "only" => Action::OnlyPane,
_ => {
if let Some(n) = name.strip_prefix("heading_") {
return n.parse::<u8>().ok().filter(|n| (1..=6).contains(n)).map(Action::SetHeading);
}
return None;
}
})
}
}