Skip to content
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

feat: add abbreviations #340

Open
wants to merge 2 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
19 changes: 19 additions & 0 deletions 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 @@ -106,3 +107,21 @@ func editorReadChar(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {

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

// #interface editor
// deleteByAmount() -> string
// Reads a keystroke from the user. This is in a format of something like Ctrl-L.
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
}
36 changes: 36 additions & 0 deletions nature/abbr.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local bait = require 'bait'
local hilbish = require 'hilbish'
hilbish.abbr = {
_abbrevs = {}
}

function hilbish.abbr.add(opts)
hilbish.abbr._abbrevs[opts.abbr] = opts
end

print 'abbr loaded'
hilbish.abbr.add {
abbr = 'tt',
expand = 'echo titties'
}

hilbish.abbr.add {
abbr = 'idk',
expand = 'i dont know',
anywhere = true
}

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._abbrevs[lineSplits[#lineSplits]]

if thisAbbr and (#lineSplits == 1 or thisAbbr.anywhere == true) then
hilbish.editor.deleteByAmount(-lineSplits[#lineSplits]:len())
hilbish.editor.insert(thisAbbr.expand)
end
end
end)
1 change: 1 addition & 0 deletions nature/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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