[code] Command Entry Line: Editable "macros"

From: Gavin Holt <holtgavin.att.gmail.com>
Date: Mon, 17 Feb 2020 22:56:52 +0000


I really like the command entry line, especially for Lua with syntax
highlighting. Inspired by TECO[1] I have written some code to
facilitate editable "macros" (attached as stringext.lua).

For simplicity I have extended the standard Lua string library so a
chain of functions can be applied to the selected input stream e.g.

    all():gsub("Lua ","_Marker1_ "):gsub("Python ","Lua
"):gsub("_Marker1_ ","Python "):ins()

I have coded global functions to make selections
(word,sel,line,para,block,all) which could be moved into another name
space. By returning a string these selection functions can call any
methods in the Lua string library.

Although extending the standard libraries may be considered
tasteless[2], it does create a neat syntax and avoids any messy
parser/DSL implementation. These actions will only be undertaken if
text~="\b" or "\a" - see conditional execution below.

Examples using new functions added to the string library:

    -- Prefix lines at column one
    block():pre("-- "):ins()

    -- Postfix lines at EOL
    block():post("-- Needs a tidy up"):ins()

    -- Sort lines in block

    -- Remove line duplicates

Although these functions are hiding in the string library they have
access to the whole Lua/Textadept state:

    -- Find all numbers and sum them

    -- Evaluate the string as lua and insert returned string
    line():eval([[return os.date():sub(1,8).." "..text]]):ins()

    -- Call a named function, passing text and insert returned string

Conditional execution in the pipeline (ab)uses some less often used
control characters.
Returning "\a" will cause ins() to abandon the changes leaving the
buffer untouched.
Returning "\b" will cause ins() to delete the selection.

    -- Unconditional delete: Del will send "\b" unless it receives "\a".

    -- Simple if: Send text if python is found, else "\a"
    all():iff("python "):gsub("python","Lua"):ins()

    -- Chained if (AND): Send text if both python and better are
found, else "\a"
    all():iff("python "):iff("better "):gsub("python","Lua"):ins()

    -- Varadic if (OR): Send text if python or ruby found, else "\a"
    para():iff("python ","ruby "):gsub("python ","Lua "):gsub("ruby
","Lua "):ins()

    -- If not (NOT): Send text if Lua not found, else "\a"
    para():iffnot("Lua "):gsub("functional ","procedural "):ins()

    -- Chained if not (NAND): Send text if Lua and Luajit not found, else "\a"
    para():iffnot("Lua "):iffnot("Luajit "):gsub("fast ","slow "):ins()

    -- Varadic if not (NOR): Send text if Lua or Luajit not found, else "\a"
    para():iffnot("Lua ","Luajit "):gsub("functional ","procedural "):ins()

    -- Delete conditionally: Send "\b" if python found,
    para():if("python "):del():ins()

The string.ins() function delivers the transformed text back to the
buffer and attempts to re-select the text.

The string.clip() function simply places the results on the clipboard
(nb. my version is specific to windows).

I have chosen to set begin_undo_action and end_undo_action at either
end of the "pipe" so buffer actions can be quickly undone.

I guess there will be an optimum number of functions that can be
permed to cover 99% of use cases. Any contributions welcome.

Please note the syntax does not allow for tables of functions e.g.
string.mymacro.propercase() cannot to used as

I would be very grateful for some help extending the textadept command
entry line module to maximise the utility of these macros:

Q1. Keyboard function to "repeat last macro": I can't find where the
last command line is stored
Q2. History for the command entry line.
Q3. Snippets for the command entry line.

Kind Regards Gavin Holt

[1] https://github.com/rhaberkorn/sciteco

[2] https://stackoverflow.com/a/2032066

You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Mon 17 Feb 2020 - 17:56:52 EST

This archive was generated by hypermail 2.2.0 : Tue 18 Feb 2020 - 06:46:59 EST