Skip to content

feat: --execute flag which runs the passed keymap before launch #13847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/completion/hx.bash
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ _hx() {

case "$2" in
-*)
mapfile -t COMPREPLY < <(compgen -W "-h --help --tutor -V --version -v -vv -vvv --health -g --grammar --vsplit --hsplit -c --config --log" -- """$2""")
mapfile -t COMPREPLY < <(compgen -W "-h --help --tutor -V --version -v -vv -vvv --health -g --grammar --vsplit --hsplit -e --execute -c --config --log" -- """$2""")
return 0
;;
*)
Expand Down
2 changes: 2 additions & 0 deletions contrib/completion/hx.elv
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ set edit:completion:arg-completer[hx] = {|@args|
$candidate "--grammar" "(Fetch or build the tree-sitter grammars)"
$candidate "--vsplit" "(Splits all given files vertically)"
$candidate "--hsplit" "(Splits all given files horizontally)"
$candidate "-e" "(Executes the given command on startup)"
$candidate "--execute" "(Executes the given command on startup)"
$candidate "--config" "(Specifies a file to use for configuration)"
$candidate "--log" "(Specifies a file to write log data into)"
}
1 change: 1 addition & 0 deletions contrib/completion/hx.fish
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ complete -c hx -l hsplit -d "Splits all given files horizontally"
complete -c hx -s c -l config -r -d "Specifies a file to use for config"
complete -c hx -l log -r -d "Specifies a file to use for logging"
complete -c hx -s w -l working-dir -d "Specify initial working directory" -xa "(__fish_complete_directories)"
complete -c hx -s e -l execute -d "Executes the given command on startup"

function __hx_langs_ops
hx --health languages | tail -n '+2' | string replace -fr '^(\S+) .*' '$1'
Expand Down
1 change: 1 addition & 0 deletions contrib/completion/hx.nu
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export extern hx [
--version(-V), # Prints version information
--vsplit, # Splits all given files vertically into different windows
--hsplit, # Splits all given files horizontally into different windows
--execute(-e), # Executes the given command on startup
--working-dir(-w): glob, # Specify an initial working directory
...files: glob, # Sets the input file to use, position can also be specified via file[:row[:col]]
]
2 changes: 2 additions & 0 deletions contrib/completion/hx.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ _hx() {
"--hsplit[Splits all given files horizontally]" \
"-c[Specifies a file to use for configuration]" \
"--config[Specifies a file to use for configuration]" \
"-e[Executes the given command on startup]" \
"--execute[Executes the given command on startup]" \
"-w[Specify initial working directory]" \
"--working-dir[Specify initial working directory]" \
"--log[Specifies a file to use for logging]" \
Expand Down
6 changes: 6 additions & 0 deletions helix-term/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub struct Args {
pub config_file: Option<PathBuf>,
pub files: IndexMap<PathBuf, Vec<Position>>,
pub working_directory: Option<PathBuf>,
/// The macro to execute on startup
pub execute: Vec<helix_view::input::KeyEvent>,
}

impl Args {
Expand Down Expand Up @@ -74,6 +76,10 @@ impl Args {
Some(path) => args.log_file = Some(path.into()),
None => anyhow::bail!("--log must specify a path to write"),
},
"-e" | "--execute" => match argv.next().as_deref() {
Some(command) => args.execute = helix_view::input::parse_macro(command)?,
None => anyhow::bail!("--execute receives a command to execute"),
},
"-w" | "--working-dir" => match argv.next().as_deref() {
Some(path) => {
args.working_directory = if Path::new(path).is_dir() {
Expand Down
16 changes: 14 additions & 2 deletions helix-term/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Context, Error, Result};
use crossterm::event::EventStream;
use futures_util::{stream, StreamExt};
use helix_loader::VERSION_AND_GIT_HASH;
use helix_term::application::Application;
use helix_term::args::Args;
Expand Down Expand Up @@ -40,7 +41,7 @@ fn main() -> Result<()> {

#[tokio::main]
async fn main_impl() -> Result<i32> {
let args = Args::parse_args().context("could not parse arguments")?;
let mut args = Args::parse_args().context("could not parse arguments")?;

helix_loader::initialize_config_file(args.config_file.clone());
helix_loader::initialize_log_file(args.log_file.clone());
Expand Down Expand Up @@ -75,6 +76,7 @@ FLAGS:
--hsplit Splits all given files horizontally into different windows
-w, --working-dir <path> Specify an initial working directory
+N Open the first given file at line number N
-e, --execute <command> Executes the given command on startup
",
env!("CARGO_PKG_NAME"),
VERSION_AND_GIT_HASH,
Expand Down Expand Up @@ -147,10 +149,20 @@ FLAGS:
helix_core::config::default_lang_loader()
});

// sequence of key events to execute before launching the editor
let execute_at_start =
stream::iter(std::mem::take(&mut args.execute).into_iter().map(|item| {
Ok(crossterm::event::Event::Key(
crossterm::event::KeyEvent::from(item),
))
}));

// TODO: use the thread local executor to spawn the application task separately from the work pool
let mut app = Application::new(args, config, lang_loader).context("unable to start Helix")?;

let exit_code = app.run(&mut EventStream::new()).await?;
let exit_code = app
.run(&mut execute_at_start.chain(EventStream::new()))
.await?;

Ok(exit_code)
}