Selecting bracketed text
This is an example of
- Using StrongED's built in Functions
- Defining a new HotKey action.
- Developing a new StrongED feature
- Using functions:
- BlockClear
- BlockMark_Continuous
- CaretLeft
- GotoBlock_End
- GotoBracket
- Pop
- Push
This was posted to the mailing list by Fred Graute. It develops a HotKey definition which should be added to the appropriate ModeFile. It assigns a new key-press
Control-Shift-B to mark a block of bracketed text. Note that all key-bindings should be on a single line.
A block of text within brackets could contain further brackets so nesting has to be taken into account.
GotoBracket
The key to this is the GotoBracket function which will move the caret to the matching bracket of a pair. The caret needs to be directly to the right of one of the enclosing brackets. GotoBracket takes nesting into account and ignores brackets in comments and strings.
The first attempt(*) was to use:
# select block inside brackets
cs-B Push BlockClear BlockMark_Continuous GotoBracket BlockMark_Continuous Pop
This works. However the opening bracket is not selected but the closing bracket is. It would be nice if either both brackets were selected or neither.
To overcome this two key-bindings were defined cs-B and cs-B 2. Pressing cs-B once should select the enclosed text but not the brackets, hitting cs-B twice should select both text and brackets.
# select block inside brackets (excl brackets)
cs-B Push BlockClear BlockMark_Continuous GotoBracket CaretLeft BlockMark_Continuous Pop
# select block inside brackets (incl brackets)
cs-B 2 Push BlockClear CaretLeft BlockMark_Continuous CaretRight GotoBracket BlockMark_Continuous Pop
Alas, there is a snag with this too. If the caret is after the opening bracket it works as intended:
cs-B now selects the brackets and
cs-B 2 excludes them. But if the caret is after the closing bracket it doesn't work properly.
The functions need to be specific about which end of the selecting needs to be adjusted. Normally the opening bracket is excluded and the closing bracket is included. Therefore...
To exclude both brackets the end of the selection needs to be adjusted.
To include the brackets the start of the selection has to be adjusted.
# select block inside brackets (excl brackets)
cs-B Push BlockClear BlockMark_Continuous GotoBracket BlockMark_Continuous GotoBlock_End CaretLeft BlockMark_Continuous Pop
# select block inside brackets (incl brackets)
cs-B 2 Push BlockClear BlockMark_Continuous GotoBracket BlockMark_Continuous GotoBlock_Start CaretLeft BlockMark_Continuous Pop
Now everything works as desired, it no longer matters which bracket the cursor is to the right of. Hopefully this is not only useful as an addition but also as a brief demonstration of how to develop new StrongED (mode) features.