Prima::Edit(3)
π NAME π
Prima::Edit(3) β standard text editor, straight outta the User Contributed Perl Documentation. π
π¬ SYNOPSIS π¬
okay bestie, here's the starter pack β copy it exactly:
use Prima qw(Edit Application);
my $e = Prima::Edit->new(
text => 'Hello $world',
syntaxHilite => 1,
);
run Prima;
π DESCRIPTION π
The class provides text editing capabilities, three types of selection, text wrapping, syntax highlighting, auto indenting, undo and redo function, and search and replace methods. β¨
The module declares the bt:: package that contains integer constants for the selection of block type, used by the blockType property. π
πΈ USAGE πΈ
The text is stored line-wise in the {lines} array; to access it use the get_line method. π«Ά
All keyboard events except the character input and tab keys are processed by the accelerator table ( see Prima::Menu ). The default accelItems table defines names, keyboard combinations, and the corresponding actions to the class functions. The class does not provide functionality to change these mappings. To do so, consult Prima::AccelTable in Prima::Menu. π
π Coordinates π
The class addresses the text space by (X,Y)-coordinates, where X is the visual cluster offset and Y is the line number. The addressing coordinate system can be visual, physical, or logical. See below. π
Cluster shaping and word wrapping can play a role here. Consider f.ex. a text string offset is zero, that for the sake of the example can be wrapped by width and displayed as two lines, offset and is zero. Here, the font substitutes ff text with a single ligature glyph. Here, for example, coord("f") will be (0,1) in all coordinates, but coord("z") is not: π
π§ Physical
The X coordinate is a character offset from character line number Y. These coordinates are identical with and without the wordWrap flag. This coordinate is used for direct text manipulation.
Example: coord("z") is (0,10); π
π Visual
The X coordinate is a glyph cluster offset from the character line number Y. These coordinates are identical with and without the wordWrap flag. This coordinate is used for cursor and selection API.
Example: coord("z") is (0,9); β¨
π§ Logical
The Y coordinate is the wrapped line index. The chunkMap internal array contains addresses in the logical coordinates. The X coordinate is a glyph cluster offset from the line start. This coordinate is used mostly internally.
To access the text chunk-wise, use the get_chunk method.
Example: coord("z") is (1,3); π
π¦ API π¦
okay this is the big one, stay with me. π
π Events π
β¨ ParseSyntax TEXT, RESULT_ARRAY_REF β¨
Called when syntax highlighting is required β TEXT is a string to be parsed, and the parsing results to be stored in RESULT_ARRAY_REF, which is a reference to an array of integer pairs, each representing a single-colored text chunk. The first integer in the pairs is the length of a chunk, the second - color value ( cl::XXX constants ). π₯
π Properties π
autoIndent BOOLEANβ turns the auto indenting on or off. Default value:1. πblockType INTEGERβ defines the type of selection block. Can be one of the following constants:bt::CUAβ normal block, where the first and the last line of the selection can be partial, and the lines between occupy the whole line. CUA stands for 'common user access'. Default keys:Shift + arrow keys. See also:cursor_shift_key. β¨bt::Verticalβ rectangular block, where all selected lines are of the same offsets and lengths. Default key:Alt+B. See also:mark_vertical. πbt::Horizontalβ rectangular block, where the selection occupies the whole line. Default key:Alt+L. See also:mark_horizontal. π¦
cursor X, Yβ selects the visual position of the cursor. πcursorX Xβ selects the visual horizontal position of the cursor.cursorY Yβ selects the visual vertical position of the cursor.cursorWrap BOOLEANβ selects cursor behavior when moved horizontally outside the line. If0, the cursor is not moved. If1, the cursor moved to the adjacent line. See also:cursor_left,cursor_right,word_left,word_right. πinsertMode BOOLEANβ sets the typing mode β if1, the typed text is inserted, if0, the new text overwrites the old text. WheninsertModeis0, the cursor shape is thick and covers the whole character; when1, it is of the default cursor width. Default toggle key:Insert. β¨οΈhiliteNumbers COLORβ selects the color for number highlighting.hiliteQStrings COLORβ selects the color for highlighting the single-quoted strings.hiliteQQStrings COLORβ selects the color for highlighting the double-quoted strings.hiliteIDs ARRAYβ an array of scalar pairs that define words to be highlighted. The first item in the pair is an array of words, and the second item is a color value. πhiliteChars ARRAYβ an array of scalar pairs that define characters to be highlighted. The first item in the pair is a string of characters, and the second item is a color value.hiliteREs ARRAYβ an array of scalar pairs that define character patterns to be highlighted. The first item in the pair is a perl regular expression, and the second item is a color value.
hiliteREsnote: these are tricky. Generally, these assume that whatever is captured in(), is highlighted, and that capturing parentheses match from the first character onwards. So for simple matches like(\d+)(digits) or(#.*)this works fine. Things become more interesting if you need to check text after, or especially before the capture. For this, you need to make sure that whatever text is matched by a regexp, it must not move thepospointer as the regexes are internally concatenated with the\Ganchor before the actual matching takes place (i.e. starting each time from the position the previous regex left off), and use/gcflags (advancingposto the match length). Advancing theposwill nullify color highlighting on the text after the capture but before the end of the match - so you'll need look-ahead assertions for this type of match,(?=pattern)and(?!pattern)(see "Lookaround Assertions" inperlre). π
For example, we have a string ABC123abc, and we want to match 123 followed by abc. This won't work:
hiliteREs => [
'(123)abc',cl::LightRed,
'(abc)', cl::LightGreen
]
while this will:
hiliteREs => [
'(123)(?=abc)',cl::LightRed,
'(abc)', cl::LightGreen
]
If you need to look behind, the corresponding assertions (?<=pattern) and (?<!pattern) could be used, but these are even more restrictive in that they only support fixed-width looks-behinds (NB: \K won't work because of \G either). That way, if we want to match 123 that follows ABC, this won't work:
hiliteREs => [
'(ABC)',cl::LightBlue,
'(?<=[ABC]+)(123)',cl::LightRed,
]
while this will:
hiliteREs => [
'(ABC)',cl::LightBlue,
'(?<=[ABC]{3})(123)',cl::LightRed,
]
mark MARK [ BLOCK_TYPE ]β selects block marking state. If MARK is1, starts the block marking, if0- stops the block marking. When MARK is1, BLOCK_TYPE can be used to set the selection type (bt::XXXconstants ). If BLOCK_TYPE is unset the value ofblockTypeis used. π―markers ARRAYβ an array of arrays with integer pairs, X and Y, where each represents visual coordinates in text. Used as anchor storage for fast navigation. See also:add_marker,delete_marker. πmodified BOOLEANβ a boolean flag that shows if the text was modified. Can be used externally, to check if the text is to be saved to a file, for example. πΎnumLines INTEGERβ returns the number of lines. π’offset INTEGERβ horizontal offset of text lines in pixels.persistentBlock BOOLEANβ selects whether the selection is canceled as soon as the cursor is moved (0) or it persists until the selection is explicitly changed (1). Default value:0.readOnly BOOLEANβ if1, no user input is accepted. Manipulations with text are allowed though. πselection X1, Y1, X2, Y2β accepts two pairs of visual coordinates,(X1,Y1)the beginning and(X2,Y2)the end of the new selection, and sets the block according to theblockTypeproperty. The selection is null if X1 equals to X2 and Y1 equals to Y2.has_selectionmethod returns1if the selection is non-null. β¨selStart X, Yβ manages the selection start. Seeselection, X1 and Y1.selEnd X, Yβ manages the selection end. Seeselection, X2 and Y2.syntaxHilite BOOLEANβ manages the syntax highlighting. πtabIndent INTEGERβ maps the tab (\t) key to atabIndentnumber of space characters.text TEXTβ provides access to all the text data. The lines are separated by the new line (\n) character. See also:textRef. πtextDirection BOOLEANβ if set, indicates RTL text input.textLigation BOOLEANβ if set, text may be rendered at better quality with ligation and kerning, however, that comes with a price that some ligatures may be indivisible and form clusters (f.ex.fforffiligatures). The cursor cannot be positioned inside of a cluster, and thus one can only select them, delete them as a whole, or pressDel/Backspaceon the cluster's edge. πtextRef TEXT_PTRβ provides access to all the text data. The lines are separated by the new line (\n) character. TEXT_PTR is a pointer to a text string. The property is more efficient thantextwith large text because the copying of the text scalar to the stack is eliminated. See also:text. π«ΆtopLine INTEGERβ selects the first line of the text drawn.undoLimit INTEGERβ sets limit on the number of stored atomic undo operations. If0, undo is disabled. Default value:1000. π₯wantTabsBOOLEAN β okay so this one decides how the tab character (\t) gets recognized in user input. If it's1, bestie, it's treated as the verbatim Tab key with an ascii value of0x09β but plot twist: that disallows the toolkit widget tab-driven navigation. If it's0, you can enter the tab character with Ctrl+Tab.
Default value: 0
wantReturnsBOOLEAN β this one controls how the new line (\n) character gets recognized in user input. If it's1, it's recognized as the verbatim CR key producing newline character(s) β but that disallows the default button activation in the toolkit. If it's0, you can enter the new line character with Ctrl+Enter.
Default value: 1
wordDelimitersSTRING β contains a string of characters used for locating a word break. The default STRING value is punctuation marks, space and tab characters, and the"\xff"character.
See also: word_left, word_right
wordWrapBOOLEAN β selects whether long lines get wrapped or can chill outside the horizontal widget borders. A line of text can be represented by more than one line of screen text (chunk). To access the text chunk-wise, use theget_chunkmethod.
Methods π
add_marker X, Yβ Adds visual coordinates X,Y to themarkersproperty. Cute.back_char [ REPEAT = 1 ]β Removes REPEAT times a character left to the cursor. If the cursor is on0X-position, it removes the new-line character and concatenates the two lines.
Default key: Backspace
cancel_blockβ Removes the selection block.
Default key: Alt+U
change_lockedβ Returns1if the logical locking is on, and0if it's off.
See also lock_change.
copyβ Copies the selected text, if any, to the clipboard.
Default key: Ctrl+Insert
copy_blockβ Copies the selected text and inserts it into the cursor position, according to theblockTypevalue.
Default key: Alt+C
cursor_cendβ Moves cursor to the last line.
Default key: Ctrl+End
cursor_chomeβ Moves cursor to the first line.
Default key: Ctrl+Home
cursor_cpgdnβ Moves cursor to the end of text.
Default key: Ctrl+PageDown
cursor_cpgupβ Moves cursor to the beginning of text.
Default key: Ctrl+PageUp
cursor_down [ REPEAT = 1 ]β Moves cursor REPEAT times down.
Default key: Down
cursor_endβ Moves cursor to the end of the line.
Default key: End
cursor_homeβ Moves cursor to the beginning of the line.
Default key: Home
cursor_left [ REPEAT = 1 ]β Moves cursor REPEAT times left.
Default key: Left
cursor_right [ REPEAT = 1 ]β Moves cursor REPEAT times right.
Default key: Right
cursor_up [ REPEAT = 1 ]β Moves cursor REPEAT times up.
Default key: Up
cursor_pgdn [ REPEAT = 1 ]β Moves cursor REPEAT pages down.
Default key: PageDown
cursor_pgup [ REPEAT = 1 ]β Moves cursor REPEAT pages up.
Default key: PageUp
cursor_shift_key [ ACCEL_TABLE_ITEM ]β Performs the action of the cursor movement bound to the ACCEL_TABLE_ITEM action (defined in the"accelTable"or"accelItems"property), and extends the selection block along the cursor movement. Not called directly.cutβ Cuts the selected text into the clipboard.
Default key: Shift+Delete
delete_blockβ Removes the selected text.
Default key: Alt+D
delete_char [ REPEAT = 1 ]β Deletes REPEAT characters from the cursor position.
Default key: Delete
delete_line LINE_ID, [ LINES = 1 ]β Removes LINES of text at LINE_ID.delete_current_chunkβ Removes the chunk (or line, ifwordWrapis0) at the cursor.
Default key: Ctrl+Y
delete_chunk CHUNK_ID, [ CHUNKS = 1 ]β Removes CHUNKS (or lines, ifwordWrapis0) of text at CHUNK_ID.delete_marker INDEXβ Removes marker INDEX in themarkerslist.delete_to_endβ Removes the text to the end of the chunk.
Default key: Ctrl+E
delete_text X, Y, TEXT_LENGTHβ Removes TEXT_LENGTH characters at X,Y physical coordinates.draw_colorchunk CANVAS, LINE_ID, X, Y, COLORβ Paints the syntax-highlighted chunk taken from LINE_ID line index, at X, Y. COLOR is used if the syntax highlighting information containscl::Foreas a color reference.end_blockβ Stops the block selection session.find SEARCH_STRING, [ X = 0, Y = 0, REPLACE_LINE = '', OPTIONS ]β Tries to find (and, if REPLACE_LINE is defined, to replace with) SEARCH_STRING starting from (X,Y) physical coordinates. OPTIONS is an integer that is a combination of thefdo::constants; the same constants are used inPrima::Dialog::FindDialog, which provides a graphic interface to the find and replace facilities of this class.
Returns X1, Y, X2, NEW_STRING where X1.Y-X2.Y are physical coordinates of the string found, and NEW_STRING is the replaced version (if any).
fdo::MatchCaseβ If set, the search is case-sensitive.fdo::WordsOnlyβ If set, SEARCH_STRING must constitute the whole word.fdo::RegularExpressionβ If set, SEARCH_STRING is a regular expression.fdo::BackwardSearchβ If set, the search direction is backward.fdo::ReplacePromptβ Not used in the class, however, used inPrima::Dialog::FindDialog.
See also: examples/editor.pl
get_chunk CHUNK_IDβ Returns the chunk of text located at CHUNK_ID. Returns an empty string if the chunk is nonexistent.get_chunk_cluster_length CHUNK_IDβ Returns the length of a chunk in clusters.get_chunk_dimension CHUNK_IDβ Finds the line number the CHUNK_ID belongs to, and returns the first chunk of that line and how many chunks the line consists of.get_chunk_width TEXT, FROM, LENGTH, [ RETURN_TEXT_PTR ]β Returns the width in pixels ofsubstr( TEXT, FROM, LENGTH ). If FROM is larger than the length of TEXT, TEXT is padded with the space characters. Tab character in TEXT replaced totabIndenttimes space character. If the RETURN_TEXT_PTR pointer is specified, the converted TEXT is stored there.get_line INDEXβ Returns the line of text located at INDEX. Returns an empty string if the line is nonexistent.get_line_cluster_length LINE_IDβ Returns the length of the line in clusters.get_line_dimension INDEXβ Returns two integers representing the line at INDEX in thewordWrapmode: the first value is the corresponding chunk index, and the second is how many chunks are contained in the line.
See also: physical_to_logical.
get_selected_textβ Returns the currently selected text.has_selectionβ Returns a boolean value, indicating if the selection block is active.insert_empty_line LINE_ID, [ REPEAT = 1 ]β Inserts REPEAT empty lines at LINE_ID.insert_line LINE_ID, @TEXTβ Inserts @TEXT strings at LINE_ID.insert_text TEXT, [ HIGHLIGHT = 0 ]β Inserts TEXT at the cursor position. If HIGHLIGHT is set to1, the selection block is canceled and the newly inserted text is selected.lock_change BOOLEANβ Increments (1) or decrements (0) lock count. Used to defer change notification in multi-change calls. When the internal lock count hits zero, theChangenotification is called.physical_to_logical X, Yβ Maps visual X,Y coordinates to the logical coordinate system. Returns the same values whenwordWrapis0.logical_to_physical X, Yβ Maps logical X,Y coordinates to the physical text offset relative to the Y line. Returns the same values whenwordWrapis0.logical_to_visual X, Yβ Maps logical X,Y coordinates to the visual coordinate system. Returns the same values whenwordWrapis0.visual_to_physical X, Yβ Maps visual X,Y coordinates to the physical text offset relative to the Y line. Returns the same X when the line does not contain any right-to-left (RTL) characters or complex glyphs.physical_to_visual X, Yβ Maps the text offset X from line Y to the visual X coordinate. Returns the same X when the line does not contain any right-to-left (RTL) characters or complex glyphs.mark_horizontalβ Starts a block marking session with thebt::Horizontalblock type.
Default key: Alt+L
mark_verticalβ Starts a block marking session with thebt::Verticalblock type.
Default key: Alt+B
overtype_blockβ Copies the selected text and overwrites the text next to the cursor position, according to theblockTypevalue.
Default key: Alt+O
pasteβ Copies text from the clipboard and inserts it at the cursor position.
Default key: Shift+Insert
realize_panningβ Performs deferred widget panning, activated by setting{delayPanning}to1. The deferred operations are those performed byoffsetandtopLine.set_line LINE_ID, TEXT, [ OPERATION, FROM, LENGTH ]β Changes line at LINE_ID to new TEXT. Hint scalars OPERATION, FROM, and LENGTH are used to maintain selection and marking data. OPERATION is an arbitrary string; the ones that are recognized are'overtype','add', and'delete'. FROM and LENGTH define the range of the change; FROM is the cluster offset and LENGTH is the length of the changed text.split_lineβ Splits a line in two at the cursor position.
Default key: Enter (or Ctrl+Enter if wantReturns is 0)
select_allβ Selects all text.start_block [ BLOCK_TYPE ]β Begins the block selection session. The block type is BLOCK_TYPE, if it is specified, or the value of theblockTypeproperty otherwise.update_blockβ Adjusts the selection inside the block session, extending or shrinking it to the current cursor position.word_left [ REPEAT = 1 ]β Moves the cursor REPEAT words to the left.word_right [ REPEAT = 1 ]β Moves the cursor REPEAT words to the right.
AUTHOR β¨
Dmitry Karasik, <dmitry@karasik.eu.org>.
SEE ALSO π«Ά
Prima, Prima::Widget, Prima::Dialog::FindDialog, examples/editor.pl
and that's the tea β β this page came from perl v5.42.0, dated 2024-08-20. bye bestie, go slay some text editing π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms