Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166//! **sued** - shut up editor, a vector-oriented line editor by [Arsalan "Aeri" Kazmi (AeriaVelocity)](https://aeriavelocity.codeberg.page) ([on Codeberg](https://codeberg.org/AeriaVelocity))//!//! sued is a stateless vector-oriented command-based text editor written in Rust,//! with focus on speed, simplicity, ease of use and staying the hell out of your//! way. It's inspired by more contemporary editors, such as the ed family (ed, em,//! ex, vi, Vim, Neovim, etc.).//!//! [Official website](https://aeriavelocity.codeberg.page/sued) | [Codeberg repo](https://codeberg.org/AeriaVelocity/sued)//!//! For info on sued as an editor, read [README.md](https://codeberg.org/AeriaVelocity/sued/src/branch/main/README.md).//! For info on sued as a library, read [README.lib.md](https://codeberg.org/AeriaVelocity/sued/src/branch/main/README.lib.md).//!//! sued is free software licensed under the Apache License, Version 2.0.//!//! For full licensing terms, read [the "Legal" section of README.md](https://codeberg.org/AeriaVelocity/sued/src/branch/main/README.md#legal).
// ^ Looks cumbersome? It's for `rustdoc`, so it uses Markdown. You probably// want to look at the README.md instead.
// Please see the corresponding files for those definitions.pub mod command;pub mod commands;pub mod exit_status;pub mod file_buffer;pub mod helper;pub mod tilde_range;
#[cfg(feature = "lua")]pub mod lua;
use command::*;use exit_status::*;use file_buffer::*;
#[cfg(feature = "lua")]use mlua::Lua;
/// Encapsulates the file buffer and command registry into one state struct.////// This struct is used to pass data between sued's modules and commands.#[derive(Default)]pub struct EditorState { // Encapsulates the file contents and file path. pub buffer: FileBuffer, // Includes all commands available to an instance of sued. pub registry: CommandRegistry, // The symbol you type before a command. Default is `~`. pub prefix: String, // The Y-position of the cursor in the editor. Used for positional editing. // No, there *is* no X-position - that's handled by rustyline. pub cursor: usize, // The existing content on the currently working line. Used by the // `~overtake` command and **shouldn't be modified externally**. pub existing_content: String, // I probably don't need to tell you what this is for. // It would probably be a bad idea to expose Lua to itself. #[cfg(feature = "lua")] pub lua: Option<Lua>, // The currently stashed file buffer and vertical cursor position. Used by // the `~stash` and related commands. #[cfg(feature = "stash")] pub stash: Option<(FileBuffer, usize)>,}
impl EditorState { pub fn new() -> EditorState { EditorState { prefix: "~".to_string(), ..Default::default() } }
pub fn instantiate() -> EditorState { let mut state = EditorState::new(); state.registry = CommandRegistry::instantiate(); state }}
/// Converts a command into a Vec<&str> that `run_command` can operate on.pub fn vecify_command(command: &str) -> Vec<&str> { let mut command_args: Vec<&str> = Vec::new(); for arg in command.split_whitespace() { command_args.push(arg); } command_args}
/// Runs a `sued` command from a string, with `is_repl` set to `false`.////// This is the recommended way to run commands if you're using sued as a library./// It sets `is_repl` to `false`, indicating that you're running sued as a library/// and not in the REPL.pub fn run_sued_command(command_args: Vec<&str>, state: &mut EditorState) -> ExitStatus { run_command(command_args, state, false)}
/// Runs a `sued` command from a string, with `is_repl` set to `true`.////// This is used internally by the sued REPL. It sets `is_repl` to `true`,/// indicating that you're NOT running sued as a library.////// This function is only available when the `repl` feature is enabled, which/// it automatically is when running the sued text editor.#[cfg(feature = "repl")]pub fn run_repl_command(command_args: Vec<&str>, state: &mut EditorState) -> ExitStatus { run_command(command_args, state, true)}
/// Process an editing command passed from `command_args`.////// Requires mutable access to `buffer` and `registry`, since this function will/// need to modify it.////// Related functions are available in [suedfn](crate::suedfn).fn run_command(command_args: Vec<&str>, state: &mut EditorState, is_repl: bool) -> ExitStatus { let Some(command_name) = command_args.first() else { return ExitStatus::Failure("no command provided".to_string()); };
match state.registry.get_command(command_name) { Some(command) => match command.scope { CommandScope::Global => { let result = command.action.call(command_args, state); ExitStatus::Success(result) } CommandScope::FileOnly => { if is_repl { ExitStatus::Failure(format!( "{} is only available in sued as a library, not as an editor", command.name )) } else { let result = command.action.call(command_args, state); ExitStatus::Success(result) } } CommandScope::REPLOnly => { if !is_repl || !cfg!(feature = "repl") { ExitStatus::Failure(format!( "{} is not available in sued as a library", command.name )) } else { let result = command.action.call(command_args, state); ExitStatus::Success(result) } } CommandScope::Private => { ExitStatus::Failure(format!("{} is marked as private", command.name)) } }, None => ExitStatus::Failure(format!("{} is not a command", command_name)), }}
/// A placeholder command to accomodate people using the old `process_command`/// function.////// This redirects the user to `run_sued_command` instead, letting them know/// that the API has changed, and triggers a panic to prevent the program from/// proceeding with an invalid function call.pub fn process_command() -> ! { panic!("`process_command` is removed as of sued v0.20.0; use `run_sued_command` instead");}