@@ -134,47 +134,76 @@ fn align_view(doc: &Document, view: &mut View, align: Align) {
134 view.offset.row = line.saturating_sub(relative);
134 view.offset.row = line.saturating_sub(relative);
137−/// A command is composed of a static name, and a function that takes the current state plus a count,
137+/// A MappableCommand is either a static command like "jump_view_up" or a Typable command like
138−/// and does a side-effect on the state (usually by creating and applying a transaction).
138+/// :format. It causes a side-effect on the state (usually by creating and applying a transaction).
139+/// Both of these types of commands can be mapped with keybindings in the config.toml.
139−#[derive(Copy, Clone)]
140+#[derive(Clone)]
140−pub struct Command {
141+pub enum MappableCommand {
141− name: &'static str,
148+ name: &'static str,
142− fun: fn(cx: &mut Context),
149+ fun: fn(cx: &mut Context),
143− doc: &'static str,
150+ doc: &'static str,
146−macro_rules! commands {
154+macro_rules! static_commands {
147 ( $($name:ident, $doc:literal,)* ) => {
155 ( $($name:ident, $doc:literal,)* ) => {
149 #[allow(non_upper_case_globals)]
157 #[allow(non_upper_case_globals)]
150− pub const $name: Self = Self {
158+ pub const $name: Self = Self::Static {
151 name: stringify!($name),
159 name: stringify!($name),
152 fun: $name,
160 fun: $name,
153 doc: $doc
161 doc: $doc
157− pub const COMMAND_LIST: &'static [Self] = &[
165+ pub const STATIC_COMMAND_LIST: &'static [Self] = &[
158 $( Self::$name, )*
166 $( Self::$name, )*
163−impl Command {
171+impl MappableCommand {
164 pub fn execute(&self, cx: &mut Context) {
172 pub fn execute(&self, cx: &mut Context) {
165− (self.fun)(cx);
173+ match &self {
174+ MappableCommand::Typable { name, args, doc: _ } => {
175+ let args: Vec<&str> = args.iter().map(|arg| arg.as_str()).collect();
176+ if let Some(command) = cmd::TYPABLE_COMMAND_MAP.get(name.as_str()) {
177+ let mut cx = compositor::Context {
182+ if let Err(e) = (command.fun)(&mut cx, &args, PromptEvent::Validate) {
183+ cx.editor.set_error(format!("{}", e));
187+ MappableCommand::Static { fun, .. } => (fun)(cx),
168− pub fn name(&self) -> &'static str {
191+ pub fn name(&self) -> &str {
169− self.name
192+ match &self {
193+ MappableCommand::Typable { name, .. } => name,
194+ MappableCommand::Static { name, .. } => name,
172− pub fn doc(&self) -> &'static str {
198+ pub fn doc(&self) -> &str {
173− self.doc
199+ match &self {
200+ MappableCommand::Typable { doc, .. } => doc,
201+ MappableCommand::Static { doc, .. } => doc,
176 #[rustfmt::skip]
205 #[rustfmt::skip]
177− commands!(
206+ static_commands!(
178 no_op, "Do nothing",
207 no_op, "Do nothing",
179 move_char_left, "Move left",
208 move_char_left, "Move left",
180 move_char_right, "Move right",
209 move_char_right, "Move right",
@@ -367,33 +396,51 @@ impl Command {
370−impl fmt::Debug for Command {
399+impl fmt::Debug for MappableCommand {
371 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
400 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
372− let Command { name, .. } = self;
401+ f.debug_tuple("MappableCommand")
373− f.debug_tuple("Command").field(name).finish()
402+ .field(&self.name())
377−impl fmt::Display for Command {
407+impl fmt::Display for MappableCommand {
378 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
408 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
379− let Command { name, .. } = self;
380− f.write_str(name)
409+ f.write_str(self.name())
384−impl std::str::FromStr for Command {
413+impl std::str::FromStr for MappableCommand {
385 type Err = anyhow::Error;
414 type Err = anyhow::Error;
387 fn from_str(s: &str) -> Result<Self, Self::Err> {
416 fn from_str(s: &str) -> Result<Self, Self::Err> {
388− Command::COMMAND_LIST
417+ if let Some(suffix) = s.strip_prefix(':') {
418+ let mut typable_command = suffix.split(' ').into_iter().map(|arg| arg.trim());
419+ let name = typable_command
421+ .ok_or_else(|| anyhow!("Expected typable command name"))?;
422+ let args = typable_command
423+ .map(|s| s.to_owned())
424+ .collect::<Vec<String>>();
425+ cmd::TYPABLE_COMMAND_MAP
427+ .map(|cmd| MappableCommand::Typable {
428+ name: cmd.name.to_owned(),
429+ doc: format!(":{} {:?}", cmd.name, args),
432+ .ok_or_else(|| anyhow!("No TypableCommand named '{}'", s))
434+ MappableCommand::STATIC_COMMAND_LIST
390− .copied()
436+ .cloned()
391− .find(|cmd| cmd.name == s)
437+ .find(|cmd| cmd.name() == s)
392− .ok_or_else(|| anyhow!("No command named '{}'", s))
438+ .ok_or_else(|| anyhow!("No command named '{}'", s))
396−impl<'de> Deserialize<'de> for Command {
443+impl<'de> Deserialize<'de> for MappableCommand {
397 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
444 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
399 D: Deserializer<'de>,
446 D: Deserializer<'de>,
@@ -403,9 +450,27 @@ impl<'de> Deserialize<'de> for Command {
406−impl PartialEq for Command {
453+impl PartialEq for MappableCommand {
407 fn eq(&self, other: &Self) -> bool {
454 fn eq(&self, other: &Self) -> bool {
408− self.name() == other.name()
455+ match (self, other) {
457+ MappableCommand::Typable {
458+ name: first_name, ..
460+ MappableCommand::Typable {
461+ name: second_name, ..
463+ ) => first_name == second_name,
465+ MappableCommand::Static {
466+ name: first_name, ..
468+ MappableCommand::Static {
469+ name: second_name, ..
471+ ) => first_name == second_name,
@@ -2843,15 +2908,16 @@ mod cmd {
2846− pub static COMMANDS: Lazy<HashMap<&'static str, &'static TypableCommand>> = Lazy::new(|| {
2911+ pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =
2847− TYPABLE_COMMAND_LIST
2913+ TYPABLE_COMMAND_LIST
2848− .iter()
2914+ .iter()
2849− .flat_map(|cmd| {
2915+ .flat_map(|cmd| {
2850− std::iter::once((cmd.name, cmd))
2916+ std::iter::once((cmd.name, cmd))
2851− .chain(cmd.aliases.iter().map(move |&alias| (alias, cmd)))
2917+ .chain(cmd.aliases.iter().map(move |&alias| (alias, cmd)))
2853− .collect()
2919+ .collect()
2857fn command_mode(cx: &mut Context) {
2923fn command_mode(cx: &mut Context) {
@@ -2877,7 +2943,7 @@ fn command_mode(cx: &mut Context) {
2877 if let Some(cmd::TypableCommand {
2943 if let Some(cmd::TypableCommand {
2878 completer: Some(completer),
2944 completer: Some(completer),
2880− }) = cmd::COMMANDS.get(parts[0])
2946+ }) = cmd::TYPABLE_COMMAND_MAP.get(parts[0])
2882 completer(part)
2948 completer(part)
2883 .into_iter()
2949 .into_iter()
@@ -2912,7 +2978,7 @@ fn command_mode(cx: &mut Context) {
2914 // Handle typable commands
2980 // Handle typable commands
2915− if let Some(cmd) = cmd::COMMANDS.get(parts[0]) {
2981+ if let Some(cmd) = cmd::TYPABLE_COMMAND_MAP.get(parts[0]) {
2916 if let Err(e) = (cmd.fun)(cx, &parts[1..], event) {
2982 if let Err(e) = (cmd.fun)(cx, &parts[1..], event) {
2917 cx.editor.set_error(format!("{}", e));
2983 cx.editor.set_error(format!("{}", e));
@@ -2925,7 +2991,7 @@ fn command_mode(cx: &mut Context) {
2925 prompt.doc_fn = Box::new(|input: &str| {
2991 prompt.doc_fn = Box::new(|input: &str| {
2926 let part = input.split(' ').next().unwrap_or_default();
2992 let part = input.split(' ').next().unwrap_or_default();
2928− if let Some(cmd::TypableCommand { doc, .. }) = cmd::COMMANDS.get(part) {
2994+ if let Some(cmd::TypableCommand { doc, .. }) = cmd::TYPABLE_COMMAND_MAP.get(part) {
2929 return Some(doc);
2995 return Some(doc);