tktext

package module
v0.0.0-...-8e6c812 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2022 License: MIT Imports: 10 Imported by: 1

README

TkText

A Go library for text editing with an API that imitates that of the Tcl/Tk text widget. Note that this library does not provide a GUI like Tk does. Displaying the text is left up to the client, although the library provides functions to assist drawing fixed-width text views.

Currently, this library supports equivalents to the following Tk text widget commands:

  • bbox
  • compare
  • count
  • delete
  • dlineinfo
  • edit
  • get
  • index
  • insert
  • mark
  • replace
  • see
  • xview
  • yview

Notably missing are the search and tag commands, which I don't plan to implement unless someone else has a use case for them.

At this point the public API should be stable, but there are probably bugs yet to be uncovered. Performance has room for improvement, so benchmarking and optimization are likely next steps.

Installation

go get -u github.com/jangler/tktext

Documentation

Bugs

Unicode is not handled correctly in some situations. See http://godoc.org/github.com/jangler/edit for a similar package that handles Unicode correctly in addition to being faster and offering built-in functionality for syntax highlighting (but lacking some niceties of the Tk text widget interface).

Documentation

Overview

Package tktext implements a text-editing buffer with an API and capabilities like that of the Tcl/Tk text widget. The buffer is thread-safe.

Note that any function that takes an index string as a parameter will panic if the index is not well-formed. For documentation on index syntax, see http://www.tcl.tk/man/tcl8.5/TkCmd/text.htm#M7.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gravity

type Gravity uint8

Gravity determines the behavior of a mark during insertions at its position. Right gravity is the default.

const (
	Right Gravity = iota // Mark remains to right of inserted text.
	Left                 // Mark remains to left of inserted text.
)

type Position

type Position struct {
	Line, Char int
}

Position denotes a position in a text buffer.

func (Position) String

func (p Position) String() string

String returns a string representation of the Position that can be used as an index in TkText functions.

type TkText

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

TkText is a text buffer. Internally, the contents are stored as a doubly linked list of line strings.

func New

func New() *TkText

New returns an initialized and empty TkText buffer.

func (*TkText) BBox

func (t *TkText) BBox(index string) (x, y int)

BBox returns the row and column numbers of the given index on the screen. The resulting values may be beyond the bounds of the screen, indicating that the index is not visible.

func (*TkText) Compare

func (t *TkText) Compare(index1, index2 string) int

Compare returns a positive integer if index1 is greater than index2, a negative integer if index1 is less than index2, and zero if the indices are equal.

func (*TkText) CountChars

func (t *TkText) CountChars(index1, index2 string) int

CountChars returns the number of UTF-8 characters between two indices. If index1 is after index2, the result will be a negative number.

func (*TkText) CountDisplayLines

func (t *TkText) CountDisplayLines(index1, index2 string) int

CountDisplayLines returns the number of displayed line breaks between two indices, taking wrapping into account. If index1 is after index2, the result will be a negative number (or zero).

func (*TkText) CountLines

func (t *TkText) CountLines(index1, index2 string) int

CountLines returns the number of line breaks between two indices. If index1 is after index2, the result will be a negative number (or zero).

func (*TkText) DLineInfo

func (t *TkText) DLineInfo(index string) (x, y, width int)

DLineInfo the starting row and column numbers of the display line containing the given index, as well as the width of that line in columns. The resulting values may be beyond the bounds of the screen, indicating that at least part of the line is not visible.

func (*TkText) Delete

func (t *TkText) Delete(index1, index2 string)

Delete deletes the text from index1 to index2. If index1 is after index2, no text is deleted. If the undo mechanism is enabled for the buffer, the operation is pushed onto the undo stack, and the redo stack is cleared.

func (*TkText) EditGetModified

func (t *TkText) EditGetModified() bool

EditGetModified returns true if and only if the buffer contents differ from the last point at which the modified flag was set to false, or if the modified flag is set to true.

func (*TkText) EditRedo

func (t *TkText) EditRedo(name ...string) bool

EditRedo redoes changes to the buffer until a separator is encountered after at least one change, or until the redo stack is empty. Redone changes are pushed onto the undo stack. Returns true if and only if a change was redone. If mark names are given as arguments, the corresponding marks are set to the position of the redone change.

func (*TkText) EditReset

func (t *TkText) EditReset()

EditReset clears the undo and redo stacks.

func (*TkText) EditSeparator

func (t *TkText) EditSeparator()

EditSeparator pushes a separator onto the undo stack if a separator is not already on top and the stack is not empty.

func (*TkText) EditSetModified

func (t *TkText) EditSetModified(modified bool)

EditSetModified sets the modified flag of the widget. If the flag is set to false, EditGetModified compares the buffer contents to the current state to determine whether the buffer was modified. If the flag is set to true, EditGetModified always returns true.

func (*TkText) EditUndo

func (t *TkText) EditUndo(name ...string) bool

EditUndo undoes changes to the buffer until a separator is encountered after at least one change, or until the undo stack is empty. Undone changes are pushed onto the redo stack. Returns true if and only if a change was undone. If mark names are given as arguments, the corresponding marks are set to the position of the undone change.

func (*TkText) Get

func (t *TkText) Get(index1, index2 string) string

Get returns the text between two indices as a string. If index1 is after index2, an empty string will be returned.

func (*TkText) GetScreenLines

func (t *TkText) GetScreenLines() []string

GetScreenLines returns a slice of strings, one for each display line on the screen. The length of each line is no longer than the width of the screen. Fewer lines may be returned if there are not enough to fill the screen.

func (*TkText) Index

func (t *TkText) Index(index string) Position

Index parses a string index and returns an equivalent valid Position in the text buffer.

func (*TkText) Insert

func (t *TkText) Insert(index, s string)

Insert inserts the given text at the given index. If the undo mechanism is enabled for the buffer, the operation is pushed onto the undo stack, and the redo stack is cleared.

func (*TkText) MarkGetGravity

func (t *TkText) MarkGetGravity(name string) (Gravity, error)

MarkGetGravity returns the gravity of the mark with the given name, or an error if a mark with the given name is not set.

func (*TkText) MarkNames

func (t *TkText) MarkNames() []string

MarkNames returns a slice of names of marks that are currently set.

func (*TkText) MarkNext

func (t *TkText) MarkNext(index string) string

MarkNext returns the name of the next mark at or after the given index. If the given index is a mark, that mark will not be returned. An empty string is returned if no mark is found. This function can be used to step through all set marks in order.

func (*TkText) MarkPrevious

func (t *TkText) MarkPrevious(index string) string

MarkPrevious returns the name of the next mark at or before the given index. If the given index is a mark, that mark will not be returned. An empty string is returned if no mark is found. This function can be used to step through all set marks in reverse order.

func (*TkText) MarkSet

func (t *TkText) MarkSet(name, index string)

MarkSet sets a mark with the given name at the given index. If a mark with the given name is already set, its position is updated.

func (*TkText) MarkSetGravity

func (t *TkText) MarkSetGravity(name string, direction Gravity) error

MarkSetGravity sets the gravity of the mark with the given name, or returns an error if a mark with the given name is not set.

func (*TkText) MarkUnset

func (t *TkText) MarkUnset(name ...string)

MarkUnset removes the marks with the given names. It is not an error to remove a mark that is not set.

func (*TkText) Replace

func (t *TkText) Replace(index1, index2, s string)

Replace replaces the text from index1 to index2 with the given text. If index1 is after index2, the operation is equivalent to an insertion at index1. If the undo mechanism is enabled for the buffer, the operation is pushed onto the undo stack, and the redo stack is cleared.

func (*TkText) See

func (t *TkText) See(index string)

See adjusts the view so that the given index is visible. If the index is already visible, no adjustment is made. If the index is less than one page out of view, the view is adjusted so that the index is at the edge of the screen. Otherwise, the view is centered on the index.

func (*TkText) SetSize

func (t *TkText) SetSize(width, height int)

SetSize sets the text display's width and height in characters and lines, respectively.

func (*TkText) SetTabStop

func (t *TkText) SetTabStop(width int)

SetTabStop sets the width in characters of the text display's tab stops. The default is 8.

func (*TkText) SetUndo

func (t *TkText) SetUndo(enabled bool)

SetUndo enables or disables the undo mechanism for the buffer. The mechanism is enabled by default.

func (*TkText) SetWrap

func (t *TkText) SetWrap(mode WrapMode)

SetWrap sets the wrap mode of the text display. The default is None.

func (*TkText) XView

func (t *TkText) XView() (left, right float64)

XView returns two fractions in the range [0, 1]. The first describes the fraction of columns in the buffer that are off-screen to the left, and the second describes the fraction that are NOT off-screen to the right.

func (*TkText) XViewMoveTo

func (t *TkText) XViewMoveTo(fraction float64)

XViewMoveTo adjusts the view so that the given fraction of columns in the buffer are off-screen to the left.

func (*TkText) XViewScroll

func (t *TkText) XViewScroll(chars int)

XViewScroll shifts the horizontal scrolling right by the given number of columns.

func (*TkText) YView

func (t *TkText) YView() (top, bottom float64)

YView returns two fractions in the range [0, 1]. The first describes the fraction of lines in the buffer that are off-screen to the top, and the second describes the fraction that are NOT off-screen to the bottom.

func (*TkText) YViewMoveTo

func (t *TkText) YViewMoveTo(fraction float64)

YViewMoveTo adjusts the view so that the given fraction of lines in the buffer are off-screen to the top.

func (*TkText) YViewScroll

func (t *TkText) YViewScroll(lines int)

YViewScroll shifts the vertical scrolling down by the given number of lines.

type WrapMode

type WrapMode uint8

WrapMode determines the behavior of a text display when a line is too long to fit in the window.

const (
	None WrapMode = iota // Lines are not wrapped.
	Char                 // Wrapping line breaks may occur at any character.
)

Notes

Bugs

  • Unicode is not handled correctly in some situations.

Jump to

Keyboard shortcuts

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