Jump to content

Lua script for UDF header

Recommended Posts

Some may find this a useful addition to SciTe

Here is a Lua script that inserts a blank function header

Place it in SciTEStartup.lua

function InsertUDFHeader()
; Description:      
; Parameter(s):     
; Requirement:      
; Return Value(s):  
; User CallTip:  
; Author(s):        
; Note(s):        


Then add the following to SciTEUser.properties

# 45 Insert AutoIt3 Function Header as per UDF guidlines
  command.name.45.*.au3=Insert UDF Header

Restart SciTE

The should now be an entry in the Tools menu near the bottom.



Link to post
Share on other sites

I have looked at abbreviations before and didn't remember seeing that. I guess it pays to look at the SciTE help file a little more often. :D


Link to post
Share on other sites

With Lua you can make it read the current line and parse out the function parameters and function name automatically. I have a Lua script which does similar for my own function comments format. I place the caret in a line with the Func keyword and hit a hotkey. A template is created with the correct function name and parameters.

As a matter of fact, here is the script:

function InsertFunctionHeader()
    local line, pos = editor:GetCurLine()
    local pos = editor.CurrentPos - pos
    local lineNum = editor:LineFromPosition(pos)
-- "()([Ff][Uu][Nn][Cc][%s]*([%w_]*)%(.-%))([^\r\n]*)"
--  local from, to, name = string.find(line, "Func ([%w_]*)")
    local from, to, name = string.find(line, "[Ff][Uu][Nn][Cc][%s]*([%w_]*)")
    if to ~= nil then
--      local pfrom, pto, pname = string.find(line, "(%([%w%s\"_,$%=@-%.]*[%)_])")
        local pfrom, pto, pname = string.find(line, "(%(.-%))")
        local i = 0
        while string.find(pname, "_%s*$") do    -- Found an underscore, so need to get the next line, too
            i = i + 1
            local tmp = editor:GetLine(lineNum+i)
            local wfrom = string.find(pname, "_%s*$")
            local nfrom = string.find(tmp, "[^%s]")
            pname = string.sub(pname, 1, wfrom-1) .. string.sub(tmp, nfrom, -1)
            pname =  string.gsub(pname, "[\n\r]", "")

        local ws = editor:WordStartPosition(editor.CurrentPos)
        local we = editor:WordEndPosition(editor.CurrentPos)
        local word = editor:textrange(ws, we)
        -- Must strip the new lines for this to work.
        line = string.gsub(line, "[\n\r]", "")
        word = string.gsub(word, "[\n\r]", "")
        if word == "" then
            editor:AddText(_CreateFunctionHeader("Name", ""))
        elseif word == line then
            local lend = editor.CurrentPos
            local lstart = editor.CurrentPos
            editor:SetSel(lstart, lend)
            editor:ReplaceSel(_CreateFunctionHeader(word, "") .. "Func " .. word .. "()\r\nEndFunc; " .. word)
end -- InsertFunctionheader()

And here is the _CreateFunctionHeader() function I use:

function _CreateFunctionHeader(s, p)
    local res = "; " .. string.rep("=", 67) .. "\r\n; " .. s .. p .. "\r\n;\r\n; Description.\r\n; Parameters:"
    local params = false
    for byref, parameter, optional in string.gfind(p, "(%w-)%s*($[%w_]+)%s*([=]?)") do
        if parameter ~= "" and parameter ~= nil then
            params = true
            if byref ~= "" and byref ~= nil then
                res = res .. "\r\n;\t" .. parameter .. " - IN/OUT - "
            elseif optional ~= "" and optional ~= nil then
                res = res .. "\r\n;\t" .. parameter .. " - IN/OPTIONAL - "
                res = res .. "\r\n;\t" .. parameter .. " - IN - "
    if params == false then
        res = res .. "\r\n;\tNone."
    res = res .. "\r\n; Returns:\r\n;\tNone.\r\n" .. "; " .. string.rep("=", 67) .. "\r\n"
    return res
end -- _CreateFunctionHeader()
Link to post
Share on other sites

@Valik, Awsome.


Type setupudf and then press the spacebar.

I knew that abbreviations(in general) were there, I just hadn't realized the breadth. :D I have alread added a couple of custom abbreviations. Now, to just get in the habit of using abbreviations. :wacko:

Link to post
Share on other sites


Thanks for posting your scipts. I modified the CreateFunctionHeader to produce output more inline with the standard UDF headers. I also added a right context menu entry in scite.

I am updating some older scripts and the partial autocompletion by using your Lua script has made the task easier.

Once Again, Thanks,


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...