nyne

package module
v0.0.0-...-9817ccf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2022 License: MIT Imports: 18 Imported by: 0

README

drawing

By Renée French

nyne

nyne is a library and a collection of tools that enables interacting with acme in ways that are more intuitive when coming from traditional text editors. As a library, nyne provides many abstractions on top of the 9fans acme library to make event handling, finding focused windows, and other actions significantly easier (see Event and the provided variables for example). The included commands make working in acme faster and more user friendly, especially when combined with a keyboard mapping tool like skhd.

These are the commands that are included:

  • cmd/a+: Indent selected source code
  • cmd/a-: Unindent selected source code
  • cmd/aspell: A spell checker for acme
  • cmd/com: Comments/uncomments piped text
  • cmd/f+: Increase font size
  • cmd/f-: Decrease font size
  • cmd/font: Wrapper around f+ or f- intended to be invoked from a tool like skhd
  • cmd/md: Shortcuts for working with markdown
  • cmd/move: Shortcuts for moving the cursor
  • cmd/nstart: Used to launching acme along with all dependencies and helpers
  • cmd/nyne: The core autoformatting engine that is run from within acme
  • cmd/nynetab: Implements tab expansion and indentation
  • cmd/save: Utility to execute Put via keyboard bindings
  • cmd/xcom: Wrapper around com intended to be invoked from a tool like skhd
  • cmd/xec: Execute a command in the focused window as if it had been clicked with B2

Configuration

Nyne and the bundled utilities are configured in config.go which defineshow nyne handles different file types, what to write to the menu, etc. Alter this file to your liking before building and installing nyne.

Several of the included tools are intended to be called from a tool like skhd which allows for overriding the application handlers for particular key bindings. See skhdrc for an example of how to use nyne tooling with skhd.

Install

To install nyne, first make sure that you have properly installed Go and then execute the following commands:

% git clone https://github.com/dnjp/nyne
% cd nyne
% go install ./...

This will build and install the included commands and the nyne library itself.

Bugs or Feature Requests

Please feel free to file an issue if you run into any bugs or problems during normal usage. Should you have any questions about how to use or setup nyne, you can start a new discussion thread.

Contributing

Please do! If you have a fix for a bug or a new feature you'd like added to nyne, please fork this repository, commit your changes to a new branch, and submit a PR with your changes.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AcmeDeps = []string{
	"plumber",
	"fontsrv",
}

AcmeDeps are programs that will be executed before acme can launch

View Source
var AcmeHelpers = []string{
	"acme-lsp",
	"acmefocused",
	"skhd",
}

AcmeHelpers are programs that will be executed in an acme subprocess by nstart

View Source
var Config = func() map[string]Filetype {
	c := make(map[string]Filetype)
	err := FillFiletypes(c, Filetypes)
	if err != nil {
		panic(err)
	}
	return c
}()

Config maps file extensions to their formatting specification

View Source
var Filetypes = []Filetype{
	{
		Name:       "cpp",
		Extensions: []string{".cc", ".cpp", ".hpp", ".cxx", ".hxx"},
		Tabwidth:   2,
		Tabexpand:  true,
		Comment:    "// ",
		Commands:   []Command{},
	},
	{
		Name:       "java",
		Extensions: []string{".java"},
		Tabwidth:   2,
		Tabexpand:  true,
		Comment:    "// ",
		Commands:   []Command{},
	},
	{
		Name:       "javascript",
		Extensions: []string{".js", ".ts"},
		Tabwidth:   2,
		Tabexpand:  true,
		Comment:    "// ",
		Commands: []Command{
			{
				Exec: "prettier",
				Args: []string{
					"$NAME",
					"--write",
					"--loglevel",
					"error",
				},
				PrintsToStdout: false,
			},
		},
	},
	{
		Name:       "json",
		Extensions: []string{".json"},
		Tabwidth:   2,
		Tabexpand:  true,
		Comment:    "",
		Commands:   []Command{},
	},
	{
		Name:       "makefile",
		Extensions: []string{"Makefile"},
		Tabwidth:   8,
		Tabexpand:  false,
		Comment:    "# ",
		Commands:   []Command{},
	},
	{
		Name:       "text",
		Extensions: []string{".txt"},
		Tabwidth:   8,
		Tabexpand:  false,
		Comment:    "# ",
		Commands:   []Command{},
	},
	{
		Name:       "shell",
		Extensions: []string{".rc", ".sh"},
		Tabwidth:   8,
		Tabexpand:  false,
		Comment:    "# ",
		Commands:   []Command{},
	},
	{
		Name:       "c",
		Extensions: []string{".c", ".h"},
		Tabwidth:   8,
		Tabexpand:  false,
		Comment:    "/* */",
		Commands:   []Command{},
	},
	{
		Name:       "html",
		Extensions: []string{".html"},
		Tabwidth:   2,
		Tabexpand:  true,
		Comment:    "<!-- -->",
		Commands:   []Command{},
	},
	{
		Name:       "markdown",
		Extensions: []string{".md"},
		Tabwidth:   2,
		Tabexpand:  true,
		Comment:    "",
		Commands:   []Command{},
	},
	{
		Name:       "terraform",
		Extensions: []string{".tf"},
		Tabwidth:   2,
		Tabexpand:  true,
		Comment:    "# ",
		Commands: []Command{
			{
				Exec: "terraform",
				Args: []string{
					"fmt",
					"$NAME",
				},
				PrintsToStdout: false,
			},
		},
	},
	{
		Name:       "toml",
		Extensions: []string{".toml"},
		Tabwidth:   8,
		Tabexpand:  false,
		Comment:    "# ",
		Commands:   []Command{},
	},
	{
		Name:       "yaml",
		Extensions: []string{".yml", ".yaml"},
		Tabwidth:   2,
		Tabexpand:  true,
		Comment:    "# ",
		Commands:   []Command{},
	},
	{
		Name:       "go",
		Extensions: []string{".go", "go.mod", "go.sum"},
		Tabwidth:   8,
		Tabexpand:  false,
		Comment:    "// ",
		Commands:   []Command{},
	},
}

Filetypes define file formatting rules that will be applied

View Source
var Menu = []string{
	" Put  ", "Undo  ", "Redo  ", "win", "\n",
	"|com  ", "|a-  ", "|a+  ", "Ldef  ", "Lrefs  ", "Lcomp",
}

Menu contains the menu options that should be written to the tag

Functions

func Extension

func Extension(in string, def string) string

Extension parses the file extension given a file name

func Filename

func Filename(in string) string

Filename takes the absolute path to a file and returns just the name of the file

func FillFiletypes

func FillFiletypes(dst map[string]Filetype, src []Filetype) error

FillFiletypes updates the filetypes in the given map

func FocusedWinAddr

func FocusedWinAddr() string

FocusedWinAddr returns the address of the active window using acmefocused

func FocusedWinID

func FocusedWinID(addr string) (int, error)

FocusedWinID returns the $winid if present, otherwise it connects to the given addr to find the ID

Derived from https://github.com/fhs/acme-lsp/blob/623cb39c2e31bddda0ad7c216c2f3c2fcfcf237f/cmd/L/main.go#L256

func FontSize

func FontSize(font *draw.Font) (int, error)

FontSize returns the size of the font

func IsHiDPI

func IsHiDPI(font *draw.Font) bool

IsHiDPI returns whether the font is being displayed in a HiDPI context. This is absolutely a hack and should not be trusted.

func Namespace

func Namespace() string

Namespace returns the current namespace

func Nums

func Nums(s string) (nums []int, err error)

Nums extracts numbers from the string, returning all matches

func Tab

func Tab(width int, expand bool) []byte

Tab constructs a tab character or the equivalent width of spaces depending on if expand is set

func Windows

func Windows() (map[int]*Win, error)

Windows returns all open acme windows

Types

type Acme

type Acme struct {
	EventHooks map[Text][]Handler
	WinHooks   map[Text][]WinHandler
	KeyHooks   map[rune]Handler
	// contains filtered or unexported fields
}

Acme implements the Listener interface for acme events

func NewAcme

func NewAcme() *Acme

NewAcme constructs an Acme event listener

func (*Acme) Buf

func (a *Acme) Buf(id int) *Buf

Buf returns the running Buf by its ID

func (*Acme) Listen

func (a *Acme) Listen() error

Listen watches the acme event log for events and executes hooks based on those events

type Action

type Action rune

Action describes what kind of action was taken

const (
	// BodyDelete is a deletion in the window body
	BodyDelete Action = 'D'
	// TagDelete is a deletion in the window tag
	TagDelete Action = 'd'
	// BodyInsert is an insertion into the window body
	BodyInsert Action = 'I'
	// TagInsert is an insert into the window tag
	TagInsert Action = 'i'
	// B3Body is a right click event in the window body
	B3Body Action = 'L'
	// B3Tag is a right click event in the window tag
	B3Tag Action = 'l'
	// B2Body is a middle click event in the window body
	B2Body Action = 'X'
	// B2Tag is a middle click event in the window tag
	B2Tag Action = 'x'
	// DelAction represents a delete event
	DelAction Action = 0x0
)

func NewAction

func NewAction(c2 rune) Action

NewAction constructs a new action

func (Action) Rune

func (a Action) Rune() rune

Rune returns the Action as a rune

type Buf

type Buf struct {
	EventHooks map[Text][]Handler
	WinHooks   map[Text][]WinHandler
	KeyHooks   map[rune]Handler
	// contains filtered or unexported fields
}

Buf implements the BufListener interface and runs on opened acme buffers

func NewBuf

func NewBuf(id int, file string) *Buf

NewBuf constructs an event loop

func (*Buf) File

func (b *Buf) File() string

File returns the buffers active file

func (*Buf) Start

func (b *Buf) Start() error

Start begins the event listener for the window

func (*Buf) Win

func (b *Buf) Win() *Win

Win returns the active acme Window

type Command

type Command struct {
	Exec           string
	Args           []string
	PrintsToStdout bool
}

Command contains options for executing a given command against an acme window

type Condition

type Condition func(Event) bool

Condition is a function that returns under what condition to run event

type Event

type Event struct {
	// Log
	ID                       int
	File                     string
	Origin                   Origin
	Action                   Action
	Text                     Text
	Flag                     Flag
	SelBegin, SelEnd         int
	OrigSelBegin, OrigSelEnd int
	NumBytes                 int
	NumRunes                 int
	ChordArg                 []byte
	ChordLoc                 []byte
	// Hooks
	WriteHooks []Hook
}

Event contains metadata for each Acme event

The message includes the text if it is less than 256 chars. If it is longer than that, the fourth number is 0 and the body must be read through the data file

func NewEvent

func NewEvent(event *acme.Event, id int, file string) (Event, error)

NewEvent constructs an Event from a raw acme event

func (*Event) Log

func (e *Event) Log() (*acme.Event, error)

Log returns a raw acme log event for the Event type

type Filetype

type Filetype struct {
	Name       string
	Extensions []string
	Tabwidth   int
	Tabexpand  bool
	Comment    string
	Commands   []Command
}

Filetype contains the formatting specification for a given file extension

func FindFiletype

func FindFiletype(filename string) (ft Filetype, ok bool)

FindFiletype returns the filetype in the nyne config if present

type Flag

type Flag int

Flag contains the flag for the event. For BodyDelete, TagDelete, BodyInsert, and TagInsert the flag is always zero. For messages with the 1 bit on in the flag, writing the message back to the event file, but with the flag, count, and text omitted, will cause the action to be applied to the file exactly as it would have been if the event file had not been open.

const (

	// IsBuiltin represents a built-in command
	IsBuiltin Flag = iota

	// IsNull represents if the text is a null string that has a
	// non-null expansion; if so, another complete message will
	// follow describing the expansion exactly as if it had been
	// indicated explicitly (its flag will always be 0)
	IsNull

	// HasChordedArg says if the command has an extra (chorded)
	// argument; if so, two more complete messages will follow
	// reporting the argument (with all numbers 0 except the
	// character count) and where it originated, in the form of a
	// fully-qualified button 3 style address.
	HasChordedArg

	// NoReloadNeeded says if acme can interpret the action without
	// loading a new file
	NoReloadNeeded

	// PostExpandFollows says if a second (post-expansion) message
	// follows, analogous to that with X messages
	PostExpandFollows

	// IsFileOrWindow says If the text is a file or window name
	// (perhaps with address) rather than plain literal text.
	IsFileOrWindow
)

func NewFlag

func NewFlag(a Action, rawFlag int) Flag

NewFlag constructs a Flag

type Formatter

type Formatter struct {
	// contains filtered or unexported fields
}

Formatter formats acme windows and buffers

func NewFormatter

func NewFormatter(filetypes []Filetype, menutag []string) (*Formatter, error)

NewFormatter constructs a Formatter

func (*Formatter) Run

func (f *Formatter) Run() error

Run tells the Formatter to begin listening for Acme events

type Handler

type Handler func(Event) (Event, bool)

Handler transforms an event

func Tabexpand

func Tabexpand(condition Condition, win WinFunc, tabwidth TabwidthFunc) (rune, Handler)

Tabexpand expands tabs to spaces

type Hook

type Hook func(Event) error

Hook executes a function on the event after it has been written to the acme log

type Origin

type Origin rune

Origin is the entity that originated the action

const (
	// BodyOrTag represents an action received in the body or tag
	BodyOrTag Origin = 'E'
	// WindowFiles represents an action taken in the file
	WindowFiles Origin = 'F'
	// Keyboard represents an action taken by the keyboard
	Keyboard Origin = 'K'
	// Mouse represents an action taken by the mouse
	Mouse Origin = 'M'
	// DelOrigin represents a delete event
	DelOrigin Origin = 0x0
)

func NewOrigin

func NewOrigin(c1 rune) Origin

NewOrigin constructs an origin from c1

func (Origin) Rune

func (o Origin) Rune() rune

Rune returns the Origin as a rune

type TabwidthFunc

type TabwidthFunc func(Event) int

TabwidthFunc returns the tabwidth based on properties of the Event

type Text

type Text string

Text contains the default acme event types

const (
	// New represents window creation
	New Text = "New"
	// Zerox reprents window creation via zerox
	Zerox Text = "Zerox"
	// Get loads/reloads the file in the window
	Get Text = "Get"
	// Put writes window to the named file
	Put Text = "Put"
	// Del deletes the window
	Del Text = "Del"
	// Focus is received when the window focused
	Focus Text = "Focus"
)

func NewText

func NewText(text []byte) Text

NewText constructs a builtin from the event text

func (Text) Bytes

func (b Text) Bytes() []byte

Bytes returns Text as a slice of bytes

func (Text) String

func (b Text) String() string

String returns Text as a string

type Win

type Win struct {
	ID        int
	File      string
	Lastpoint int
	// contains filtered or unexported fields
}

Win represents the active Acme window

func NewWin

func NewWin() (*Win, error)

NewWin constructs a Win object from acme window

func OpenWin

func OpenWin(id int, file string) (*Win, error)

OpenWin opens an acme window

func (*Win) Addr

func (w *Win) Addr() (q0, q1 int, err error)

Addr returns the current address of the window

Derived from https://github.com/fhs/acme-lsp/blob/623cb39c2e31bddda0ad7c216c2f3c2fcfcf237f/internal/acme/acme.go#L366

func (*Win) AddrFromSelection

func (w *Win) AddrFromSelection() error

AddrFromSelection sets the addr address to that of the user’s selected text in the window.

func (*Win) AppendBody

func (w *Win) AppendBody(data []byte) error

AppendBody appends the given text to the body

func (*Win) AppendTag

func (w *Win) AppendTag(text string) error

AppendTag writes to the windows tag

func (*Win) Body

func (w *Win) Body() ([]byte, error)

Body returns the window body

func (*Win) Char

func (w *Win) Char(q0 int) (c byte, err error)

Char reads the character at q0

func (*Win) Clean

func (w *Win) Clean() error

Clean marks the window clean as though it has just been written.

func (*Win) ClearBody

func (w *Win) ClearBody() error

ClearBody clears the text from the body

func (*Win) ClearTag

func (w *Win) ClearTag() error

ClearTag removes all text in the tag after the vertical bar.

func (*Win) Close

func (w *Win) Close()

Close closes down the window with associated files

func (*Win) CurrentAddr

func (w *Win) CurrentAddr() (q0, q1 int, err error)

CurrentAddr sets the addr to dot and reads the addr

func (*Win) Data

func (w *Win) Data(q0, q1 int) ([]byte, error)

Data reads the data in the body between q0 and q1. It is assumed that CurrentAddr() or similar has been called to properly set the addr and retrieve valid q0 and q1 points.

func (*Win) Del

func (w *Win) Del() error

Del is the equivalent to the Del interactive command.

func (*Win) Dirty

func (w *Win) Dirty() error

Dirty marks the window dirty, the opposite of clean.

func (*Win) DisableNoMark

func (w *Win) DisableNoMark() error

DisableNoMark cancels nomark, returning the window to the usual state wherein each modification to the body must be undone individually.

func (*Win) Dump

func (w *Win) Dump(file string) error

Dump sets the command string to recreate the window from a dump file.

func (*Win) Dumpdir

func (w *Win) Dumpdir(dir string) error

Dumpdir sets the directory in which to run the command to recreate the window from a dump file.

func (*Win) EventChan

func (w *Win) EventChan(id int, filename string, stop <-chan struct{}) (<-chan Event, <-chan error)

EventChan opens a channel to acme events

func (*Win) Exec

func (w *Win) Exec(exec string, args ...string) error

Exec executes the given command in the window tag

func (*Win) Font

func (w *Win) Font() (tab int, font *draw.Font, err error)

Font returns the font for the current win

func (*Win) Get

func (w *Win) Get() error

Get is the equivalent to the Get interactive command with no arguments; accepts no arguments.

func (*Win) LimitSearchToAddr

func (w *Win) LimitSearchToAddr() error

LimitSearchToAddr restricts subsequent searches to the current addr address.

func (*Win) Name

func (w *Win) Name(format string, args ...interface{}) error

Name sets the name for the win

func (*Win) NoMark

func (w *Win) NoMark() error

NoMark turns off automatic ‘marking’ of changes, so a set of related changes may be undone in a single Undo interactive command.

func (*Win) Put

func (w *Win) Put() error

Put is the equivalent to the Put interactive command with no arguments; accepts no arguments.

func (*Win) SelectionFromAddr

func (w *Win) SelectionFromAddr() error

SelectionFromAddr sets the user’s selected text in the window to the text addressed by the addr address.

func (*Win) SetAddr

func (w *Win) SetAddr(fmtstr string, args ...interface{}) error

SetAddr takes an addr which may be written with any textual address in the format understood by button 3 but without the initial colon

func (*Win) SetData

func (w *Win) SetData(data []byte) error

SetData is used in conjunction with addr for random access to the contents of the body. The file offset is ignored when writing the data file; instead the location of the data to be read or written is determined by the state of the addr file. Text, which must contain only whole characters (no ‘partial runes’), written to data replaces the characters addressed by the addr file and sets the address to the null string at the end of the written text. A read from data returns as many whole characters as the read count will permit starting at the beginning of the addr address (the end of the address has no effect) and sets the address to the null string at the end of the returned characters.

func (*Win) SetFont

func (w *Win) SetFont(font string) error

SetFont sets the font for the win

func (*Win) Show

func (w *Win) Show() error

Show guarantees at least some of the selected text is visible on the display.

func (*Win) Tag

func (w *Win) Tag() ([]byte, error)

Tag returns the tag contents

func (*Win) WriteEvent

func (w *Win) WriteEvent(e Event) error

WriteEvent writes the acme event to the log

type WinFunc

type WinFunc func(int) (*Win, error)

WinFunc retrieves the Win by its ID

type WinHandler

type WinHandler func(*Win)

WinHandler transforms the window

Directories

Path Synopsis
cmd
a+
Indents selected source code.
Indents selected source code.
a-
Unindents selected source code.
Unindents selected source code.
aspell
A spell checker for acme Usage of aspell: aspell [spell options]
A spell checker for acme Usage of aspell: aspell [spell options]
com
Comments/uncomments piped text Usage of com: |com `com` uses the `commentstyle` you've configured a given file extension to comment or uncomment a given selection in acme.
Comments/uncomments piped text Usage of com: |com `com` uses the `commentstyle` you've configured a given file extension to comment or uncomment a given selection in acme.
f+
Increase font size Usage of f+: Execute f+ with B2
Increase font size Usage of f+: Execute f+ with B2
f-
Decrease font size Usage of f-: Execute f- with B2
Decrease font size Usage of f-: Execute f- with B2
font
Wrapper around f+ or f- intended to be invoked from a tool like skhd Usage of font: -op string font operation to execute: inc, dec (default "inc")
Wrapper around f+ or f- intended to be invoked from a tool like skhd Usage of font: -op string font operation to execute: inc, dec (default "inc")
md
Shortcuts for working with markdown Usage of md: -op string the operation to perform: link, bold, italic, preview
Shortcuts for working with markdown Usage of md: -op string the operation to perform: link, bold, italic, preview
move
Shortcuts for moving the cursor Usage of move: -d string the direction to move: up, down, left, right, start, end -p move by paragraph (only valid for left and right) -s select text while moving -w move by word (only valid for left and right)
Shortcuts for moving the cursor Usage of move: -d string the direction to move: up, down, left, right, start, end -p move by paragraph (only valid for left and right) -s select text while moving -w move by word (only valid for left and right)
nstart
nstart is used for launching acme along with all of its dependencies and helpers.
nstart is used for launching acme along with all of its dependencies and helpers.
nyne
The core autoformatting engine that is run from within acme.
The core autoformatting engine that is run from within acme.
nynetab
Implements tab expansion and indentation.
Implements tab expansion and indentation.
save
Utility to execute Put via keyboard bindings Usage of save: Execute save from the shell
Utility to execute Put via keyboard bindings Usage of save: Execute save from the shell
xcom
Wrapper around `com` intended to be invoked from a tool like skhd Usage of xcom: Execute xcom from the shell
Wrapper around `com` intended to be invoked from a tool like skhd Usage of xcom: Execute xcom from the shell
xec
Execute a command in the focused window as if it had been clicked with B2 Usage of xec: xec [command]
Execute a command in the focused window as if it had been clicked with B2 Usage of xec: xec [command]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL