diff --git a/cspell.json b/cspell.json index 65ac5b6a..c55d3a1a 100644 --- a/cspell.json +++ b/cspell.json @@ -1 +1 @@ -{"version":"0.2","flagWords":[],"language":"en","words":["Punct","KEYMAP","splitn","crossterm","YAZI","peekable","ratatui","syntect","pbpaste","pbcopy","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","Konsole","Überzug","pkgs","pdftoppm","poppler","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink","linemodes","SIGSTOP","sevenzip","rsplitn","replacen","DECSET","DECRQM","repeek","cwds","tcsi","Hyprland","Wayfire","SWAYSOCK","btime","nsec","codegen","gethostname","fchmod","fdfind","Rustc","rustc","ffprobe","vframes","luma","obase","outln","errln","tmtheme","twox","cfgs","fstype","objc","rdev","runloop","exfat","rclone","DECRQSS","DECSCUSR","libvterm","Uninit","lockin","rposition","resvg","foldhash","tilded","futs","chdir","hashbrown","JEMALLOC","RUSTFLAGS","RDONLY","GETPATH","fcntl","casefold","inodes","Splatable"]} \ No newline at end of file +{"language":"en","flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","peekable","ratatui","syntect","pbpaste","pbcopy","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","Konsole","Überzug","pkgs","pdftoppm","poppler","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink","linemodes","SIGSTOP","sevenzip","rsplitn","replacen","DECSET","DECRQM","repeek","cwds","tcsi","Hyprland","Wayfire","SWAYSOCK","btime","nsec","codegen","gethostname","fchmod","fdfind","Rustc","rustc","ffprobe","vframes","luma","obase","outln","errln","tmtheme","twox","cfgs","fstype","objc","rdev","runloop","exfat","rclone","DECRQSS","DECSCUSR","libvterm","Uninit","lockin","rposition","resvg","foldhash","tilded","futs","chdir","hashbrown","JEMALLOC","RUSTFLAGS","RDONLY","GETPATH","fcntl","casefold","inodes","Splatable","casefied"],"version":"0.2"} \ No newline at end of file diff --git a/yazi-config/preset/keymap-default.toml b/yazi-config/preset/keymap-default.toml index dee02f00..7342a93c 100644 --- a/yazi-config/preset/keymap-default.toml +++ b/yazi-config/preset/keymap-default.toml @@ -301,9 +301,10 @@ keymap = [ { on = "p", run = "paste", desc = "Paste copied characters after the cursor" }, { on = "P", run = "paste --before", desc = "Paste copied characters before the cursor" }, - # Undo/Redo - { on = "u", run = "undo", desc = "Undo the last operation" }, - { on = "", run = "redo", desc = "Redo the last operation" }, + # Undo/Redo/Casefy + { on = "u", run = [ "undo", "casefy lower" ], desc = "Undo, or lowercase if in visual mode" }, + { on = "U", run = "casefy upper", desc = "Uppercase" }, + { on = "", run = "redo", desc = "Redo the last operation" }, # Help { on = "~", run = "help", desc = "Open help" }, diff --git a/yazi-parser/src/input/casefy.rs b/yazi-parser/src/input/casefy.rs new file mode 100644 index 00000000..30628c91 --- /dev/null +++ b/yazi-parser/src/input/casefy.rs @@ -0,0 +1,25 @@ +use mlua::{ExternalError, FromLua, IntoLua, Lua, Value}; +use yazi_shared::event::CmdCow; + +#[derive(Debug)] +pub struct CasefyOpt { + pub upper: bool, +} + +impl From for CasefyOpt { + fn from(c: CmdCow) -> Self { Self { upper: c.str(0) == "upper" } } +} + +impl FromLua for CasefyOpt { + fn from_lua(_: Value, _: &Lua) -> mlua::Result { Err("unsupported".into_lua_err()) } +} + +impl IntoLua for CasefyOpt { + fn into_lua(self, _: &Lua) -> mlua::Result { Err("unsupported".into_lua_err()) } +} + +impl CasefyOpt { + pub fn transform(&self, s: &str) -> String { + if self.upper { s.to_ascii_uppercase() } else { s.to_ascii_lowercase() } + } +} diff --git a/yazi-parser/src/input/mod.rs b/yazi-parser/src/input/mod.rs index 9f587eba..7f002580 100644 --- a/yazi-parser/src/input/mod.rs +++ b/yazi-parser/src/input/mod.rs @@ -1 +1 @@ -yazi_macro::mod_flat!(backspace backward close complete delete forward insert kill paste r#move show); +yazi_macro::mod_flat!(backspace backward casefy close complete delete forward insert kill paste r#move show); diff --git a/yazi-widgets/src/input/commands/casefy.rs b/yazi-widgets/src/input/commands/casefy.rs new file mode 100644 index 00000000..8ab6f6e6 --- /dev/null +++ b/yazi-widgets/src/input/commands/casefy.rs @@ -0,0 +1,31 @@ +use std::ops::Range; + +use anyhow::Result; +use yazi_macro::{act, render, succ}; +use yazi_parser::input::CasefyOpt; +use yazi_shared::data::Data; + +use crate::input::{Input, op::InputOp}; + +impl Input { + pub fn casefy(&mut self, opt: CasefyOpt) -> Result { + let snap = self.snap_mut(); + if !matches!(snap.op, InputOp::Select(_)) { + succ!(); + } + + let range = snap.op.range(snap.cursor, true).unwrap(); + let Range { start, end } = snap.idx(range.start)..snap.idx(range.end); + + let (start, end) = (start.unwrap(), end.unwrap()); + let casefied = opt.transform(&snap.value[start..end]); + + snap.value.replace_range(start..end, &casefied); + snap.op = InputOp::None; + snap.cursor = range.start; + self.snaps.tag(self.limit).then(|| self.flush_value()); + + act!(r#move, self)?; + succ!(render!()); + } +} diff --git a/yazi-widgets/src/input/commands/commands.rs b/yazi-widgets/src/input/commands/commands.rs index ba6ed220..86644ee3 100644 --- a/yazi-widgets/src/input/commands/commands.rs +++ b/yazi-widgets/src/input/commands/commands.rs @@ -35,6 +35,8 @@ impl Input { on!(undo); on!(redo); + + on!(casefy); } InputMode::Insert => { on!(visual); diff --git a/yazi-widgets/src/input/commands/mod.rs b/yazi-widgets/src/input/commands/mod.rs index 9eafa31b..8e01d1f5 100644 --- a/yazi-widgets/src/input/commands/mod.rs +++ b/yazi-widgets/src/input/commands/mod.rs @@ -1 +1 @@ -yazi_macro::mod_flat!(backspace backward commands complete delete escape forward insert kill paste r#move r#type redo replace undo visual yank); +yazi_macro::mod_flat!(backspace backward casefy commands complete delete escape forward insert kill paste r#move r#type redo replace undo visual yank); diff --git a/yazi-widgets/src/input/commands/undo.rs b/yazi-widgets/src/input/commands/undo.rs index 1f0c1a6f..934299b0 100644 --- a/yazi-widgets/src/input/commands/undo.rs +++ b/yazi-widgets/src/input/commands/undo.rs @@ -3,10 +3,14 @@ use yazi_macro::{act, render, succ}; use yazi_parser::VoidOpt; use yazi_shared::data::Data; -use crate::input::{Input, InputMode}; +use crate::input::{Input, InputMode, InputOp}; impl Input { pub fn undo(&mut self, _: VoidOpt) -> Result { + if self.snap().op != InputOp::None { + succ!(); + } + if !self.snaps.undo() { succ!(); }