List is quiet currently, unless Gmane is playing tricks again...
Anyway, that's an opportunity to publish a Lua macro that I use for some
time now, and seems to be stable.
This allows me to add braces in the BSD/Allman style (braces stand on
their own line, and are on the same column than the preceding
statement), which is my preferred and the one used at my job (for Java
code).
Perhaps it can be adapted to K&R style (which is used in Scintilla/SciTE
code), I didn't tried...
I associated this function to Ctrl+=, because the parallel lines of the
= character are mnemonic... Plus it is accessible from my French keyboard.
Works well with:
indent.automatic=1
indent.opening=0
indent.closing=0
settings.
-- Add braces { } (or soft brackets) around the selected line(s),
-- or if there is no selection, around the current line.
-- Assume *whole* lines are selected, including EOLs.
-- Also suppose selected text is already at right indentation level,
-- either to add braces around the single line of an existing if (for,
while, etc.),
-- or tabbing the selection before performing this operation.
function AddBraces()
local text, len = GetSelectedText('line')
if len == 0 then return end -- Empty buffer!
local eol = GetEOL()
-- Get indentation level
local _, _, indent, nextChar = string.find(text, "^([\t ]+)(.)")
if indent ~= nil then
if nextChar ~= nil and nextChar ~= '\n' and nextChar ~= '\r' then
if string.sub(indent, 1, 1) == '\t' then
-- One tab less
indent = string.sub(indent, 2)
else -- Indent with spaces
-- Skip number of ident spaces
indent = string.sub(indent, 1 + editor.Indent)
end
-- ELSE, we have a line with only whitespace,
-- chances are we are just after a if/for/while...
-- and just want braces at same level
end
else -- Not indented. Shouldn't be, but play nice...
indent = ''
end
text = indent .. '{' .. eol .. text .. indent .. '}' .. eol
editor:ReplaceSel(text)
editor:LineUp(); editor:LineUp()
end
Helper functions:
(these are generic, so doing more than needed above)
-- Return the selected text and its length.
-- If no text is selected, select the current line and return it.
function GetSelectedText(defaultSel)
local text, len = editor:GetSelText()
if len == 0 then
if defaultSel == 'line' then
-- No selection, select the current line with EOL
text, len = SelectCurrentLine(true)
elseif defaultSel == 'word' then
text, len = SelectCurrentWord()
else
print"Bad parameter for GetSelectedText!"
end
end
return text, len
end
function SelectCurrentLine(bWithEOL)
local lineStartPos, lineEndPos = GetLineData(-1)
if bWithEOL then
local eol = editor.CharAt[lineEndPos]
if eol == 10 or eol == 13 then
lineEndPos = lineEndPos + 1
eol = editor.CharAt[lineEndPos]
if eol == 10 then
lineEndPos = lineEndPos + 1
end
end
end
editor:SetSel(lineStartPos, lineEndPos)
end
-- Given a line number (-1 for the line where the caret is),
-- return the position of the start of the line, of its end, and
-- the number of the line
function GetLineData(lineNb)
if lineNb < 0 then
lineNb = editor:LineFromPosition(editor.CurrentPos)
end
local lineStartPos = editor:PositionFromLine(lineNb)
local lineEndPos = editor.LineEndPosition[lineNb]
return lineStartPos, lineEndPos, lineNb
end
function SelectCurrentWord()
editor:WordRight()
editor:WordLeftExtend() -- Could be better, if followed by spaces,
select these spaces too...
return editor:GetSelText()
end
-- Get EOL char(s), assuming consistency in the file
function GetEOL()
local eol = "\r\n"
if editor.EOLMode == SC_EOL_CR then
eol = "\r"
elseif editor.EOLMode == SC_EOL_LF then
eol = "\n"
end
return eol
end
If you know a better SelectCurrentWord function, working whatever
follows the word (punctuation, whitespace, nothing...), I would be glad.
--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --
_______________________________________________
Scite-interest mailing list
[email protected]
http://mailman.lyra.org/mailman/listinfo/scite-interest