Springe zum Hauptinhalt

Here’s a nifty little Lua script for the Geanylua plug-in for the Geany text editor: install it into your ~/.config/geany/plugins/geanylua folder and add the name of the script to ~/.config/geany/plugins/geanylua/hotkeys.cfg to allow binding it to a keyboard shortcut (I use Ctrl-Alt-b, see the Geanylua documentation for more information on assigning keys to scripts).

When the script is activated, it will select the block corresponding to the current code folding level of the line the cursor is on in the active document. E.g. if you place the cursor in the body of a Python function on a line that is on the same indentation level as the first line of the body of the function, it will select the whole function. Also, if the cursor is on the opening line of a block, e.g. a line starting with class, def, if, etc., the script will select the block started by this line.

Note: this strategy does not work well in conjunction with functions preceded by a decorator, since the selection will not include the decorator line. For this reason, the selection is inverted, i.e. it starts at the end of the block and ends at the beginning. This means you can hold down shift and move the cursor backwards, and the selection will extend from the beginning of the block backwards, instead of the end of the selection being shortened as it would be for a normal selection.

Here’s the code of the Lua script as a Gist:

Lua script to select folding block in Geany editor

Select-folding-block.lua

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
--[[
    Select block corresponding to current folding level
--]]

line, col = geany.rowcol()
-- Scintilla uses 0-based line numbering internally
line = line - 1
--~print("L: " .. line .. " C: " .. col)
fstart = geany.scintilla("SCI_GETFOLDPARENT", line)
--~print("S: " .. fstart+1)
fend = geany.scintilla("SCI_GETLASTCHILD", line, -1)
--~print("E: " .. fend+1)

if fend == line then
    fend = geany.scintilla("SCI_GETLASTCHILD", fstart, -1)
    sstart = geany.rowcol(fstart + 1, 0)
    send = geany.rowcol(fend + 2, 0)
else
    sstart = geany.rowcol(line + 1, 0)
    send = geany.rowcol(fend + 2, 0)
end

geany.select(send, sstart)
Gist https://gist.github.com/SpotlightKid/9964085

The script makes use of some Scintilla messages, which are only accessible in Geanylua via the geany.scintilla() function. The available messages are documented here.