Skip to content

feat: add abbreviations #340

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

Merged
merged 11 commits into from
Apr 3, 2025
Merged
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
67 changes: 67 additions & 0 deletions docs/api/hilbish/hilbish.abbr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
title: Module hilbish.abbr
description: command line abbreviations
layout: doc
menu:
docs:
parent: "API"
---


## Introduction
The abbr module manages Hilbish abbreviations. These are words that can be replaced
with longer command line strings when entered.
As an example, `git push` can be abbreviated to `gp`. When the user types
`gp` into the command line, after hitting space or enter, it will expand to `git push`.
Abbreviations can be used as an alternative to aliases. They are saved entirely in the history
Instead of the aliased form of the same command.

## Functions
|||
|----|----|
|<a href="#remove">remove(abbr)</a>|Removes the named `abbr`.|
|<a href="#add">add(abbr, expanded|function, opts)</a>|Adds an abbreviation. The `abbr` is the abbreviation itself,|
<hr>
<div id='add'>
<h4 class='heading'>
hilbish.abbr.add(abbr, expanded|function, opts)
<a href="#add" class='heading-link'>
<i class="fas fa-paperclip"></i>
</a>
</h4>

Adds an abbreviation. The `abbr` is the abbreviation itself,
while `expanded` is what the abbreviation should expand to.
It can be either a function or a string. If it is a function, it will expand to what
the function returns.
`opts` is a table that accepts 1 key: `anywhere`.
`opts.anywhere` defines whether the abbr expands anywhere in the command line or not,
whereas the default behavior is only at the beginning of the line
#### Parameters
`abbr` **`string`**


`expanded|function` **`string`**


`opts` **`table`**


</div>

<hr>
<div id='remove'>
<h4 class='heading'>
hilbish.abbr.remove(abbr)
<a href="#remove" class='heading-link'>
<i class="fas fa-paperclip"></i>
</a>
</h4>

Removes the named `abbr`.
#### Parameters
`abbr` **`string`**


</div>

21 changes: 21 additions & 0 deletions docs/api/hilbish/hilbish.editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,30 @@ directly interact with the line editor in use.
## Functions
|||
|----|----|
|<a href="#editor.deleteByAmount">deleteByAmount(amount)</a>|Deletes characters in the line by the given amount.|
|<a href="#editor.getLine">getLine() -> string</a>|Returns the current input line.|
|<a href="#editor.getVimRegister">getVimRegister(register) -> string</a>|Returns the text that is at the register.|
|<a href="#editor.insert">insert(text)</a>|Inserts text into the Hilbish command line.|
|<a href="#editor.getChar">getChar() -> string</a>|Reads a keystroke from the user. This is in a format of something like Ctrl-L.|
|<a href="#editor.setVimRegister">setVimRegister(register, text)</a>|Sets the vim register at `register` to hold the passed text.|

<hr>
<div id='editor.deleteByAmount'>
<h4 class='heading'>
hilbish.editor.deleteByAmount(amount)
<a href="#editor.deleteByAmount" class='heading-link'>
<i class="fas fa-paperclip"></i>
</a>
</h4>

Deletes characters in the line by the given amount.

#### Parameters
`number` **`amount`**


</div>

<hr>
<div id='editor.getLine'>
<h4 class='heading'>
Expand Down Expand Up @@ -96,6 +114,9 @@ hilbish.editor.setVimRegister(register, text)
Sets the vim register at `register` to hold the passed text.

#### Parameters
`string` **`register`**


`string` **`text`**


Expand Down
22 changes: 21 additions & 1 deletion editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func editorLoader(rtm *rt.Runtime) *rt.Table {
"getVimRegister": {editorGetRegister, 2, false},
"getLine": {editorGetLine, 0, false},
"readChar": {editorReadChar, 0, false},
"deleteByAmount": {editorDeleteByAmount, 1, false},
}

mod := rt.NewTable()
Expand Down Expand Up @@ -47,7 +48,7 @@ func editorInsert(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// #interface editor
// setVimRegister(register, text)
// Sets the vim register at `register` to hold the passed text.
// #aram register string
// #param register string
// #param text string
func editorSetRegister(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
Expand Down Expand Up @@ -106,3 +107,22 @@ func editorReadChar(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {

return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
}

// #interface editor
// deleteByAmount(amount)
// Deletes characters in the line by the given amount.
// #param amount number
func editorDeleteByAmount(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}

amount, err := c.IntArg(0)
if err != nil {
return nil, err
}

lr.rl.DeleteByAmount(int(amount))

return c.Next(), nil
}
3 changes: 3 additions & 0 deletions emmyLuaDocs/hilbish.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ local hilbish = {}
--- @param cmd string
function hilbish.aliases.add(alias, cmd) end

--- Deletes characters in the line by the given amount.
function hilbish.editor.deleteByAmount(amount) end

--- Returns the current input line.
function hilbish.editor.getLine() end

Expand Down
61 changes: 61 additions & 0 deletions nature/abbr.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-- @module hilbish.abbr
-- command line abbreviations
-- The abbr module manages Hilbish abbreviations. These are words that can be replaced
-- with longer command line strings when entered.
-- As an example, `git push` can be abbreviated to `gp`. When the user types
-- `gp` into the command line, after hitting space or enter, it will expand to `git push`.
-- Abbreviations can be used as an alternative to aliases. They are saved entirely in the history
-- Instead of the aliased form of the same command.
local bait = require 'bait'
local hilbish = require 'hilbish'
hilbish.abbr = {
all = {}
}

--- Adds an abbreviation. The `abbr` is the abbreviation itself,
--- while `expanded` is what the abbreviation should expand to.
--- It can be either a function or a string. If it is a function, it will expand to what
--- the function returns.
--- `opts` is a table that accepts 1 key: `anywhere`.
--- `opts.anywhere` defines whether the abbr expands anywhere in the command line or not,
--- whereas the default behavior is only at the beginning of the line
-- @param abbr string
-- @param expanded|function string
-- @param opts table
function hilbish.abbr.add(abbr, expanded, opts)
print(abbr, expanded, opts)
opts = opts or {}
opts.abbr = abbr
opts.expand = expanded
hilbish.abbr.all[abbr] = opts
end

--- Removes the named `abbr`.
-- @param abbr string
function hilbish.abbr.remove(abbr)
hilbish.abbr.all[abbr] = nil
end

bait.catch('hilbish.rawInput', function(c)
-- 0x0d == enter
if c == ' ' or c == string.char(0x0d) then
-- check if the last "word" was a valid abbreviation
local line = hilbish.editor.getLine()
local lineSplits = string.split(line, ' ')
local thisAbbr = hilbish.abbr.all[lineSplits[#lineSplits]]

if thisAbbr and (#lineSplits == 1 or thisAbbr.anywhere == true) then
hilbish.editor.deleteByAmount(-lineSplits[#lineSplits]:len())
if type(thisAbbr.expand) == 'string' then
hilbish.editor.insert(thisAbbr.expand)
elseif type(thisAbbr.expand) == 'function' then
local expandRet = thisAbbr.expand()
if type(expandRet) ~= 'string' then
print(string.format('abbr %s has an expand function that did not return a string. instead it returned: %s', thisAbbr.abbr, expandRet))
return
end
hilbish.editor.insert(expandRet)
end
end
end
end)
1 change: 1 addition & 0 deletions nature/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require 'nature.opts'
require 'nature.vim'
require 'nature.runner'
require 'nature.hummingbird'
require 'nature.abbr'

local shlvl = tonumber(os.getenv 'SHLVL')
if shlvl ~= nil then
Expand Down
4 changes: 4 additions & 0 deletions readline/vimdelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func (rl *Instance) viDeleteByAdjust(adjust int) {
rl.updateHelpers()
}

func (rl *Instance) DeleteByAmount(adjust int) {
rl.viDeleteByAdjust(adjust)
}

func (rl *Instance) vimDeleteToken(r rune) bool {
tokens, _, _ := tokeniseSplitSpaces(rl.line, 0)
pos := int(r) - 48 // convert ASCII to integer
Expand Down
Loading