Textadept Documentation

repository·default·Indexed 21 days ago

https://github.com/orbitalquark/textadept

A fast, minimalist, and highly extensible cross-platform text editor for programmers built with C, C++, and Lua. The documentation covers installation across Windows, macOS, and Linux, the use of optional modules like LSP and spellcheck, and a comprehensive Lua API for managing buffers, views, key bindings, and custom command line arguments.

Tokens
48.3K
Snippets
128
Records
219
Agent score
74%

What's inside Textadept

  1. How projects work in Textadept

    default

    Textadept does not have an explicit "Open Project" command. Instead, a project is defined as a parent directory containing a recognized version control directory (Git, Mercurial, SVN, Bazaar, or Fossil).

    Textadept determines the current project context by:

    1. Walking up the parent directory tree of the current buffer.
    2. Walking up the current working directory (cwd) tree.

    You can set a default project by passing a directory as a command-line argument when starting Textadept, or by changing the working directory within the editor using the Lua command lfs.chdir('/path/to/folder').

  2. Scripting Textadept with Lua

    default

    Textadept is an event-driven application where nearly all features (syntax highlighting, file I/O, search/replace) are implemented in Lua. It includes an internal copy of Lua 5.4.

    Core Concepts

    • Event-Driven Model: Textadept responds to inputs (key presses, mouse clicks, state changes) by executing Lua functions. For example, pressing Ctrl+O triggers the io.open_file() function via the events.KEYPRESS event.
    • Entry Point: Your ~/.textadept/init.lua file is the primary location for custom scripting. Use it to define custom key bindings, menu items, and event handlers.

    Common Scripting Tasks

    • Custom Key Bindings & Menus: Manipulate buffer contents or extend the textadept.menu.menubar.
    • Event Handling: Listen for events like events.FILE_SAVED to trigger asynchronous processes (e.g., linters via os.spawn) or events.BUFFER_BEFORE_SWITCH to implement auto-save.
    • UI Customization: Modify the find/replace pane using ui.find or add items to the right-click context menu by appending to textadept.menu.context_menu.
    • CLI Extensions: Register new command line arguments using args.register().
  3. Configure selection drawing layers

    default

    The view.selection_layer property determines how selections are rendered relative to the text.

    Available modes:

    • view.LAYER_BASE: Selections are drawn opaquely on the background.
    • view.LAYER_UNDER_TEXT: Selections are drawn translucently under the text.
    • view.LAYER_OVER_TEXT: Selections are drawn translucently over the text.

    Default value is view.LAYER_BASE.

  4. Replace text using target ranges

    default

    Textadept uses a target range—a user-defined region of text—to allow certain functions to operate without altering the current selection or scrolling the view.

    Managing the Target Range

    • buffer:set_target_range(start_pos, end_pos): Manually defines the target range.
    • buffer:target_from_selection(): Sets the target range to be the current main selection.

    Replacing Text

    • buffer:replace_target(text): Replaces the text in the target range with the provided string. This returns the length of the replacement text. Calling this with an empty string deletes the target range.
    • buffer:replace_sel(text): Replaces the current selection and scrolls the caret into view.

    Note: buffer:replace_target is preferred when you want to modify text without affecting the user's current selection or view position.

  5. How language detection and lexers work

    default

    Textadept identifies programming languages to apply syntax highlighting using a three-step fallback mechanism:

    1. First Line Patterns: Checks the first line against patterns in lexer.detect_patterns.
    2. File Extensions: Checks the file extension against lexer.detect_extensions.
    3. Fallback: Uses a plain text lexer.

    You can manually change a buffer's lexer using Ctrl+Alt+L (Windows/Linux/BSD), ^⌘L (macOS), or M-L (terminal).

  6. Apply transforms to placeholders

    default

    Placeholder transforms allow you to modify the text of a mirrored or captured placeholder using regex. The syntax is ${n/*regex*/*format*/*options*}.

    Supported Format Tokens:

    • $m or ${m}: The content of the m-th capture (0 is the whole match).
    • ${m:/upcase}, ${m:/downcase}, ${m:/capitalize}: Built-in text transformations.
    • ${m:?*if*:*else*}: Inserts if if capture m is non-empty, otherwise else.
    • ${m:+*if*}: Inserts if if capture m is non-empty, otherwise nothing.
    • ${m:*default*}: Inserts default if capture m is empty, otherwise mirrors the content.
    • ${m:-*default*}: Inserts default if capture m is empty, otherwise mirrors the content.

    Options:

    • g: Global replacement (replace all matches, not just the first).
    -- Example: Creating an attribute with a getter and setter
    -- Uses /./ to match any character and /upcase to transform it
    snippets.attr = [[
    	${1:int} ${2:name};
    
    	${1} get${2/./${0:/upcase}/}() { return $2; }
    	void set${2/./${0:/upcase}/}(${1} ${3:value}) { $2 = $3; }
    ]]
  7. Understand Terminal Version limitations

    default

    When running Textadept in a terminal, certain GUI features are unavailable due to terminal constraints. Users should be aware of the following limitations:

    • Visuals: No alpha/transparency, no images in autocompletion (shows first character instead), no zoom, and no style settings (font name, size, italics).
    • UI Elements: No drag and drop, no mouse cursor types, and no hotspot underlines on hover.
    • Drawing: No buffered/two-phase drawing, no extra line ascent/descent, and no fold lines above/below lines.
    • Markers/Guides: Fold marker highlighting is limited to bold (no color), and indent guide highlighting is limited to white.
    • Interaction: Not all key sequences (like Shift+Arrow) are recognized, and caret styles (period, line style, width) are unavailable.
    • Indicators: Only INDIC_ROUNDBOX and INDIC_STRAIGHTBOX are supported, but they lack translucency and rounded corners.
  8. Identify the Operating System and UI Environment

    default

    Textadept provides global variables to detect the current environment. This is useful for writing platform-specific or UI-specific logic.

    • OS: Returns the operating system as one of: 'windows', 'macos', 'linux', or 'bsd'.
    • UI: Returns the user interface type as one of: 'qt', 'gtk', or 'terminal'.
    if OS == 'windows' then ... end
    
    if UI == 'terminal' then ... end
  9. How buffers and views work in Textadept

    default

    Textadept splits the Scintilla editing component's API into two distinct parts: buffers and views.

    • Buffers: Responsible for the data model, including text editing, selections, and navigation.
    • Views: Responsible for the visual representation, such as text display, selection display, margins, markers, and highlights.

    Key Relationships

    • Interchangeability: The API is largely interchangeable. view.field or view:function() are often equivalent to buffer.field or buffer:function(). However, a view operation is only equivalent to a buffer operation if the buffer is the one currently contained by that view (buffer == view.buffer).
    • Focus: Only one buffer and one view are considered "current" (having focus) at a time.
    • Background Edits: You can perform "background" edits on non-current buffers. For example, calling buf:replace_sel('') on a buffer that is not the current one will still modify that buffer's text, even if it isn't visible in the current view.
    • Visual vs. Data: Operations like buffer:select_all() on a non-current buffer will modify the selection data in that buffer, but it will not result in a visible selection in the current view.
  10. Use placeholders and tab stops

    default

    Placeholders allow you to create templates that you can navigate using the Tab key.

    • Tab Stops: Use $n or ${n}. When the snippet is inserted, the caret starts at $1. Tab moves to $2, and so on.
    • Exit Point: After the last placeholder, the caret moves to $0 if it exists, otherwise to the end of the snippet.
    • Default Values: Use ${n:default} to provide a value that can be overwritten.
    • Mirrors: Use the same index (e.g., $1) in multiple places. Typing in one will automatically update all others with that index.
    • Multiple Choice: Use ${n|option1,option2|} to provide a list of selectable items.
    -- A snippet with tab stops and default values
    snippets.lua.fori = [[
    for ${1:i} = ${2:1}, $3 do
    	$0
    end]]
    
    -- A snippet with mirrors (HTML tags)
    snippets.tag = '<${1:div}>$0</$1>'
    
    -- A snippet with multiple choice
    snippets.choice = '${1|foo,bar,baz|}'
  11. Define key sequences and modifiers

    default

    Key sequences are strings combining modifiers and the character/key.

    Modifiers:

    • Windows/Linux/BSD/Terminal: 'ctrl', 'alt', 'shift'.
    • macOS: 'ctrl', 'alt' (Option), 'cmd' (Command), 'shift'.
    • Terminal: Alt is represented as 'meta'.

    Key Values:

    • For values < 255, use the character representation (e.g., ctrl+shift+\t for Ctrl+Shift+Tab).
    • For values > 255, use the keys.KEYSYMS lookup table (e.g., ctrl+right).