Skip to content

Instantly share code, notes, and snippets.

View stuartpb's full-sized avatar

Stuart P. Bentley stuartpb

View GitHub Profile
@stuartpb
stuartpb / gist:1343278
Created November 6, 2011 18:24
A hacky bit of code I wrote up in a couple seconds to make the table constructors for some glyphs
for _,s in ipairs{"AZ","09","!!","??","::","..","--"} do
local b1=string.byte(s:sub(1,1))
local b2=string.byte(s:sub(2,2))
for i=b1, b2 do
print(string.format([[
[%q]={
{},
},]],string.char(i)))
end
end
@stuartpb
stuartpb / drag.lua
Created October 3, 2011 20:21
grab, drag, and release handler framework for IUPLua
do
local dox, doy
local function drag(x, y)
end
local function grab(x, y)
dox, doy = x, y
drag(x, y)
end
local function release(x, y)
dox, doy = nil, nil
@stuartpb
stuartpb / gist:1023377
Created June 13, 2011 18:32
Classic style 5.2 environmental module function
--has the same signature as classic module (almost)
local function module(name, encenv)
local mod_t = {}
local envmt = {}
function envmt.__index(t, k)
return mod_t[k] or encenv[k]
end
function envmt.__newindex(t, k, v)
mod_t[k] = v
@stuartpb
stuartpb / gist:1023370
Created June 13, 2011 18:27
A nice environmental module style for 5.2
--takes the environment to encapsulate
local function modme(encenv)
local retmod = {}
local envmt = {}
function envmt.__index(t, k)
return retmod[k] or encenv[k]
end
function envmt.__newindex(t, k, v)
retmod[k] = v
end
local iup = require "iuplua"
local cairo = require "lcairo"
local drawf, errmsg
local function showerr(hdc, err)
uhoh = cairo.CreateContext(hdc)
cairo.set_source_rgb(uhoh,1,0,0)
cairo.select_font_face (uhoh,"Consolas",
cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
# the full list is at http://www.scintilla.org/SciTEDoc.html #
#settings provided in Lua for Windows' SciTEGlobal.properties
import minilfw
#font change
font.base=font:Consolas,size:8
font.small=$(font.base)
font.comment=$(font.base)
font.code.comment.box=$(font.comment)
#! /home/stuserbot/bin/lua
local rt = {}
local getenv = os.getenv
local strupper = string.upper
local strlower = string.lower
local format = string.format
local gsub = string.gsub
local setmetatable = setmetatable
local write = io.write
@stuartpb
stuartpb / osfacopy.lua
Created May 25, 2011 15:29
Monstrous one-size-fits-all Lua table copy
return function (t,o)
local refs = {}
local rk, rv
if type(o.recurse) == "string" then
if string.find(o.recurse,'k') then
rk = true end
if string.find(o.recurse,'v') then
rv = true end
elseif o.recurse then
rk=true; rv=true
@stuartpb
stuartpb / example.lua
Last active December 14, 2021 18:11
Lua script for fancy named parameters with default values
local fp = require "fancyparams"
myfunction = fp(
{{"a"},{"b",7},{"c",5}},
function(a, b, c)
print(a, b, c)
end
)
@stuartpb
stuartpb / gist:975183
Created May 16, 2011 19:47
Counts the occurrences of a phrase in an RFC
local src = http.request"http://tools.ietf.org/id/draft-ietf-ldapext-ldap-c-api-05.txt"
src = string.gsub(src,[[
Expires: May 2001 -%[Page %d+%]
C LDAP API -C LDAP Application Program Interface -17 November 2000]],"\n")
local function count(pat)
return select(2,string.gsub(src,pat,""))
end
local function plain(str)