Skip to content

Instantly share code, notes, and snippets.

@hmenke
hmenke / rest.py
Last active February 28, 2020 04:28
SourceForge to GitHub migration script for Issues
#!/usr/bin/python3
# This script uses the undocumented new issues API of GitHub.
# Read more about it here: https://gist.github.com/jonmagic/5282384165e0f86ef105
import datetime
import pprint
import pytz
import re
import requests
@hmenke
hmenke / mqueue.c
Last active May 17, 2022 12:24
Example of POSIX mqueue to communicate with pthreads.
#ifndef __cplusplus
#define _GNU_SOURCE
#endif
// C standard library
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@hmenke
hmenke / china.tex
Last active November 17, 2019 22:37
MetaFun flags
\startMPpage
vardef star(expr r) =
save x, y ;
z1 = (r*cosd(90+0*360/5),r*sind(90+0*360/5)) ;
z2 = (r*cosd(90+1*360/5),r*sind(90+1*360/5)) ;
z3 = (r*cosd(90+2*360/5),r*sind(90+2*360/5)) ;
z4 = (r*cosd(90+3*360/5),r*sind(90+3*360/5)) ;
z5 = (r*cosd(90+4*360/5),r*sind(90+4*360/5)) ;
z1 -- z3 -- z5 -- z2 -- z4 -- cycle
enddef ;
@hmenke
hmenke / rand.c
Last active January 5, 2019 06:54
Legacy RNG from Lua 5.2 for Lua 5.3
/*
This program reimplements the GNU glibc random number generator.
https://www.mathstat.dal.ca/~selinger/random/
https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=stdlib/random_r.c
*/
#include <assert.h>
#include <stdlib.h>
@hmenke
hmenke / json.lua
Last active December 19, 2018 21:37
Very simple JSON parser and serializer with LPEG
local lpeg = assert(require("lpeg"))
local C, Cf, Cg, Ct, P, R, S, V =
lpeg.C, lpeg.Cf, lpeg.Cg, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.V
-- number parsing
local digit = R"09"
local dot = P"."
local eE = S"eE"
local sign = S"+-"^-1
local mantissa = digit^1 * dot * digit^0 + dot * digit^1 + digit^1
@hmenke
hmenke / protrusion-quality.tex
Created September 10, 2018 02:07
Quality protrusion vectors in LuaTeX
\documentclass{article}
\usepackage{fontspec}
\usepackage{luacode}
\usepackage{xcolor}
\begin{luacode*}
local byte = string.byte
fonts.expansions.setups['quality'] = {
stretch = 2, shrink = 2, step = .5, factor = 1,
@hmenke
hmenke / XeTeXemulate.sty
Last active January 28, 2023 01:42
Emulate XeTeX font primitives in LuaTeX
\ifcsname ProvidesPackage\endcsname
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{XeTeXemulate}[2018/09/03 v0.0 Emulate the XeTeX font primitives in LuaTeX]
\fi
\def\XeTeXemulate{%
\chardef\XeTeXversion=0
\def\XeTeXrevision{.99999}%
% Circumvent \outer error
\csname newcount\endcsname\XeTeXtracingfonts \XeTeXtracingfonts=0
@hmenke
hmenke / luacode.tex
Last active April 23, 2018 11:06
Absolutely minimal boilerplate for a luacode environment
\protected\def\startluacode
{\begingroup
\catcode`\^^M=12
\catcode`\%=12
\catcode`\#=12
\startluacodeindeed}
\def\startluacodeindeed#1\stopluacode
{\expanded{\noexpand\endgroup\directlua{#1}}}
@hmenke
hmenke / matheval.lua
Last active February 29, 2020 21:10
Simple mathematical expression parser and evaluator in Lua with LPEG
local lpeg = require"lpeg"
local C, P, R, S, V = lpeg.C, lpeg.P, lpeg.R, lpeg.S, lpeg.V
local white = S(" \t") ^ 0
local digit = R("09")
local dot = P(".")
local eE = S("eE")
local sign = S("+-")^-1
local mantissa = digit^1 * dot * digit^0 + dot * digit^1 + digit^1
@hmenke
hmenke / R.lua
Last active October 19, 2022 07:29
Call other scripting languages from LuaJIT via FFI
local ffi = assert(require("ffi"))
local libR = assert(ffi.load("libR.so"))
local c_str = function(text)
local str = ffi.new("char[?]", #text + 1)
ffi.copy(str, text)
return str
end
ffi.cdef[[