olets / zsh-abbr

The zsh manager for auto-expanding abbreviations, inspired by fish. ~13,000 unique cloners as of May '24, 580+ Homebrew installs 6/23-6/24
https://zsh-abbr.olets.dev
Other
510 stars 18 forks source link

Allow to define cursor position after expansion #63

Closed burneyy closed 4 months ago

burneyy commented 1 year ago
olets commented 1 year ago

Thanks for the PR @burneyy!

~My immediate zsh-abbr priority is getting v5 out. (Long-time listeners will know it's been in the works for a while. Work and afk life is also busy, so I'm reserving the right to get delayed šŸ˜ƒ However I'm actively working on a push to the finish line ā€” in fact that's why I'm at my computer on this lovely weekend afternoon!) I'd be happy to talk more about this and to look at your implementation after I've cut that release.~

Edit: released 5ļøāƒ£

 

specify the position of the cursor after expanding the abbreviation

Neat idea! No promises yet ā€”I want to take time to think about any possible drawbacksā€” but it seems useful and like a safer way of satisfying #21.

 

__CURSOR__

Something specific to zsh-abbr would be safer. Theoretically possible that someone would be using __CURSOR__ for something else. Maybe ABBR_EXPANSION_CURSOR?

I think abbr-expand-and-accept should probably cut out the cursor keyword from the expansion. What do you think?

 

omit the trailing whitespace

You can already do this by binding abbr-expand ~(docs link; I know it's hard to find in the very long README. v5 will come with a better docs)~ edit: link in new docs

In a sense it would be reasonable to disable abbr-expand-and-space's space in the custom cursor location caseā€¦ but instead I would expect abbr-expand-and-space to still add space at the end of the entire expansion (not immediately before where the cursor keyword was).

I'd rather not blur the line between abbr-expand and abbr-expand-and-space. My initial thought is it's okay to require custom-cursor-position powerusers to use a custom binding.

Is it possible for both abbr-expand and abbr-expand-and-space to support the cursor position?

 

Immediately trigger the syntax highlighting

So far my decision has been to not build in support for any specific syntax highlighters. It adds maintenance overhead / risk.

Have you tried something like this? (in .zshrc)

expand-abbreviation-and-highlight-line() {
  abbr-expand
  _zsh_highlight
}

zle -N expand-abbreviation-and-highlight-line

bindkey "mybindinggoeshere" expand-abbreviation-and-highlight-line

# load zsh-abbr here

(Untested)

olets commented 1 year ago

(edited my comment ā€” burneyy if you're already here and reading, reload)

burneyy commented 1 year ago

Hey @olets, thanks for the quick response!

__CURSOR__

Something specific to zsh-abbr would be safer. Theoretically possible that someone would be using __CURSOR__ for something else. Maybe ABBR_EXPANSION_CURSOR?

I have considered that as well, however since googling for __CURSOR__ didn't lead to any reasonable results, I thought it was safe and unique enough - but indeed something a bit more verbose might be safer. I also considered introducing an option that needs to be set when adding an abbreviation with a custom cursor position like abbr add -c myabbr="some command with a custom __CURSOR__ position" but this would need to be saved somehow along with the abbreviations and is probably too much overhead, what do you think?

I think abbr-expand-and-accept should probably cut out the cursor keyword from the expansion. What do you think?

Possibly, yeah... I personally do not have a use-case for abbreviations with a custom cursor position that would be useful also when executing immediately. I do stuff like rm -rf <some_location>/__CURSOR__ where I actually prefer to keep the __CURSOR__ string when I accidentally press enter such that not the whole <some_location> directory gets deleted. It is something to think about for sure.

omit the trailing whitespace

You can already do this by binding abbr-expand (docs link; I know it's hard to find in the very long README. v5 will come with a better docs).

True, but I would rather like to save this information along with the abbreviation and not having to think about it when expanding it.

In a sense it would be reasonable to disable abbr-expand-and-space's space in the custom cursor location caseā€¦ but instead I would expect abbr-expand-and-space to still add space at the end of the entire expansion (not immediately before where the cursor keyword was).

I'd rather not blur the line between abbr-expand and abbr-expand-and-space. My initial thought is it's okay to require custom-cursor-position powerusers to use a custom binding.

Is it possible for both abbr-expand and abbr-expand-and-space to support the cursor position?

I kind of think of pressing space to just trigger the expansion and not actually inserting a literal space. So in fact, I would even rather have to specify a space within the definition of the abbreviation than by default appending one when pressing the space button. But I guess that is just me and probably highly debatable... ;)

Immediately trigger the syntax highlighting

So far my decision has been to not build in support for any specific syntax highlighters. It adds maintenance overhead / risk.

Have you tried something like this? (in .zshrc)

expand-abbreviation-and-highlight-line() {
  abbr-expand
  _zsh_highlight
}

zle -N expand-abbreviation-and-highlight-line

bindkey "mybindinggoeshere" expand-abbreviation-and-highlight-line

# load zsh-abbr here

(Untested)

Yes looks reasonable to do this externally, I will remove that part of the PR again I guess.

Have a nice remaining weekend!

burneyy commented 1 year ago

I have now removed the syntax highlighting part from the PR.

For anyone interested, you can put the following snippet into your .zshrc to enable it externally:

# Enable syntax highlighting after expanding with space
expand_abbreviation_and_space_and_highlight() {
  _abbr_widget_expand_and_space
  _zsh_highlight
}
zle -N expand_abbreviation_and_space_and_highlight
bindkey " " expand_abbreviation_and_space_and_highlight
burneyy commented 1 year ago

Following our discussion, I have tweaked the implementation now as follows:

  1. The cursor position marker (default: __ABBR_CURSOR__) can be adjusted through the parameter ABBR_CURSOR_MARKER
  2. The cursor position marker is now supported for all three types of expansions (expand, expand-and-accept, expand-and-space)
  3. For expand-and-space, no additional space is inserted when a custom cursor position was specified

Side note: It allows for template-like abbreviations such as abbr templ="first here __ABBR_CURSOR__ then here __ABBR_CURSOR__ then there __ABBR_CURSOR__" which jumps from one cursor marker to the next after each expansion i.e. after each press of space

Caveats: Typing in __ABBR_CURSOR__ (outside of the usage of zsh-abbr) will cause the cursor to jump to that position. Maybe it is safer to disable that functionality by default? However, I think that string is specific enough to not be typed in another context (without being aware of it at least)

olets commented 1 year ago

the following snippet ā€¦ _abbr_widget_expand_and_space

Glad this came up! _abbr_widget_expand_and_space is supposed to be deprecated. Use abbr-expand-and-space instead. In v5 _abbr_widget_expand_and_space will be gone and abbr-expand-and-space will be your only option. I've cut a new version, v4.8.1, to add deprecation warnings.

I would even rather have to specify a space within the definition of the abbreviation than by default appending one when pressing the space button

For anyone else who wants this: this is supported even without this PR's changes. Disable the default keybindings (ABBR_DEFAULT_BINDINGS=0) and then bind space to abbr-expand.

burneyy commented 1 year ago

Glad this came up! _abbr_widget_expand_and_space is supposed to be deprecated. Use abbr-expand-and-space instead. In v5 _abbr_widget_expand_and_space will be gone and abbr-expand-and-space will be your only option. I've cut a new version, v4.8.1, to add deprecation warnings.

Got it!

For anyone else who wants this: this is supported even without this PR's changes. Disable the default keybindings (ABBR_DEFAULT_BINDINGS=0) and then bind space to abbr-expand.

But unfortunately then, one cannot use space as a ā€žnormalā€œ space anywhere else. I guess I could add a new function abbr-expand-or-space that does what I intendedā€¦

burneyy commented 1 year ago

Btw, there is a big update incoming for abbreviations in fish where one of the new features is support for a custom cursor position

keremciu commented 1 year ago

this is a great update! it will be very useful for my abbrs

olets commented 1 year ago

Quick note to say other things are keeping me busy but this is still on my mind and something I'd like to eventually support

gforien commented 1 year ago

Very useful feature indeed

olets commented 4 months ago

Thanks for your patience, all!

Replaced this with

which is includes this PR but separates the "set cursor during expansion" and "templates" into two separate features. I used % for the default markers, to match fish (see https://fishshell.com/docs/current/cmds/abbr.html#add-subcommand).

It's released in v5.4.0

Docs:

olets commented 4 months ago

@burneyy lmk if you'd like to be listed as a contributor in https://zsh-abbr.olets.dev/community/ and https://github.com/olets/zsh-abbr#community

burneyy commented 2 months ago

@burneyy lmk if you'd like to be listed as a contributor in https://zsh-abbr.olets.dev/community/ and https://github.com/olets/zsh-abbr#community

Great that it has found its way into the release now, and yeah I would be happy to be listed among the contributors :)

olets commented 2 months ago

@all-contributors please add @burneyy for code. (#63)

allcontributors[bot] commented 2 months ago

@olets

I've put up a pull request to add @burneyy! :tada:

olets commented 2 months ago

@burneyy please see https://github.com/olets/zsh-abbr/pull/113#issuecomment-2088853394