Scite Simple Template

lua-users home
wiki

Apply tabular values to a template text

This Lua script for Scite (see SciteScripts) helps you repeat the same text template to be filled with tabular values.

Say you have several lines of tab separated values. Each line will be split (tab character) into values. Ex:

af	Afghanistan
za	Afrique Du Sud
al	Albanie
dz	Algérie
de	Allemagne
//ad	Andorre
ao	Angola
ai	Anguilla
aq	Antarctique
ag	Antigua
[...]
fr	France	SELECTED
[...]
zw	Zimbabwe

Now write the template (it can be multiline). It must appear just after your values. Ex:

<option value="<1>" <3>><2></option>

The script will duplicate and fill automatically the template with your values. It is quite simple:

Carefully select the template (from top to bottom) and run the script. The generated text will be inserted after the template. Ex:

<option value="af" >Afghanistan</option>
<option value="za" >Afrique Du Sud</option>
<option value="al" >Albanie</option>
<option value="dz" >Algérie</option>
<option value="de" >Allemagne</option>
<option value="ao" >Angola</option>
<option value="ai" >Anguilla</option>
<option value="aq" >Antarctique</option>
<option value="ag" >Antigua,Et,Barbuda</option>
[...]
<option value="fr" SELECTED>France</option>
[...]
<option value="zw" >Zimbabwe</option>

If you find it usefull, here's the script :

-- we assume the template definition is the current selection
local template = editor:GetSelText()

-- now duplicate and fill the template
-- for each line before the template definition
local lastline = editor:LineFromPosition(editor.SelectionStart)-1
local res = ""
for index=0,lastline do
	local line = editor:GetLine(index)
	if string.len(line) > 2 and string.sub(line,1,2) ~= "//" then
		-- split tab separated columns
		local values = { ["<0>"] = index+1 }
		local i = 0
		for p in string.gfind(line, "[^\t\r\n]+") do
			i = i+1
			values["<"..i..">"] = p
		end
		-- fill template with values
		res = res..string.gsub(template, "(<%d+>)", function(p) return values[p] end)
	end
end

-- insert & select generated content
editor:insert(editor.SelectionEnd, res)
editor:SetSel(editor.SelectionEnd, -1)

I like to hit Alt+Shift+T to run the script:

command.name.33.*=Templatization!
command.mode.33.*=subsystem:lua,savebefore:no,groupundo
command.shortcut.33.*=Alt+Shift+T
command.33.*=dofile $(SciteDefaultHome)/lua/templatization.lua

--Philippe


RecentChanges · preferences
edit · history
Last edited August 31, 2006 7:49 pm GMT (diff)