简体   繁体   中英

bash readline: proxy enter key

When I source this script, through a readline rebind of the the enter key, I pass every line entered through a custom function, which should render normal bash behaviour - except when the current line is detected to be "special":

my_eval () {
    local cur_line="$READLINE_LINE"
    (...) <some special checks and actions if line matches criteria >
    # when crits DON'T match I want normal behaviour:
    eval "$cur_line"  # <- the only way?
    READLINE_LINE=""        
}
bind -x '"\C-M": "my_eval"'

My problem is how to get normal behaviour in such an enter handler: Is calling eval the only way or would it be possible to fall back at this point to readline's normal behaviour when enter is pressed (ie invoke its accept-line function somehow, which is normally bound to \\CM , according to bind -P )?

With the eval based solution I have to take care of handling multiline expressions manually, also redirections for interactive commands, history, display prompt and command before showing result...

could really not find a way to invoke readline's accept-line programmatically from a script. So I tried to mimic the behaviour manually. Far better than eval seems to be to push into history, then use fc -s to run the last command.

That works for me... okay-ish, maybe of use to someone, so I put it on:

https://github.com/axiros/readline_proxy/

Basically I push the expressions as provided within $READLINE_LINE into the history via history -s when they are deemed complete. And then run the last statement in history via fc -s .

Here is the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM