Skip to content

Instantly share code, notes, and snippets.

@kbauer
kbauer / Meaning
Created July 15, 2015 15:52
For debugging/learning LaTeX internals it is useful to dump the definition of commands. Reading the output of `\show` from the console however is inconvenient. The command `\meaning` is useful here (as it prints the definition to the document) but limited. `\mymeaning` is a wrapper, where you specifiy the name of a macro rather than the `\macro`…
% \mymeaning{command} -> Definition of \command
\makeatletter
\newcommand{\mymeaning}[1]{
\begingroup%
\setlength{\parindent}{0em}
\setlength{\hangindent}{1em}
\edef\@tmp{\ifmmode\noexpand\mathtt\else\noexpand\texttt\fi}
\@tmp{%
\expandafter\string\csname #1\endcsname->%
\expandafter\meaning\csname #1\endcsname;%
@kbauer
kbauer / emacs-lines-of-code
Created August 20, 2015 11:51
Calculate the number of non-empty, non-commented code lines in an emacs lisp project or configuration file. For simple configurations run `emacs-lines-of-code ~/.emacs`
#!/usr/bin/env bash
set -e -E -u
source libstacktrace || true
DEBUG=false
MANUAL="
USAGE: $0 ( DIRECTORY | FILE ) ...
@kbauer
kbauer / chrome-comic-rocket-navigation.ahk
Last active December 15, 2015 16:22
An AutoHotKey script for navigating comic-rocket. For use with other Browsers than Chrome, change the RegEx in the #IfWinActive line accordingly.
SetTitleMatchMode RegEx
#IfWinActive .*Comic Rocket webcomic list - Google Chrome
ComicRocketChangePage(bynum)
{
oldclip := clipboard
Send ^l
Sleep 50
Send ^c
Sleep 50
@kbauer
kbauer / FORVARARGS.h
Last active February 19, 2016 15:40
A c preprocessor macro allowing to iterate over an argument list `A1, A2, ...` to produce `M(1, A1), M(2, A2), ...` or the reverse. Depends on ONEARG.h, see https://gist.github.com/kbauer/d651bae52ab2f72b8d1e . Tested with `gcc -E -std=c99`.
// Usage: _FORVARARGS(M,A0[,A1[,A2[...]]])
//
// Equivalent to M(0,A0) M(1,A1) M(2,A2) ...
// Supports up to 32 Varargs. Emacs lisp code for generating included.
// Define the macro M(N, ARG) to generate useful code.
//
// Usage: _FORVARARGS_R(M,A0[,A1[,A2[...]]])
//
// Equivalent to ... M(2,A2) M(1,A1) M(0,A0), i.e. reverse of _FORVARARGS.
//
@kbauer
kbauer / ONEARG.h
Last active February 19, 2016 15:42
A C preprocessor macro that allows executing code depending on the number of arguments in `__VA_ARGS__`. Test with `gcc -std=c99 -E`. Original version of an `_IFEMPTY` macro was discarded because it depended on the GCC extension syntax `## __VA_ARGS__`.
/// Usage: _ISONEARG() -> 1
/// _ISONEARG(A) -> 1
/// _ISONEARG(A,B,...) -> 0
///
/// Alternative to _IFEMPTY, that works with standard C.
/// Works for up to 100 arguments.
///
/// Examples:
///
/// "_ISONEARG()" -> 1
@kbauer
kbauer / lyx-header-util.py
Last active April 1, 2016 19:29
A script for extracting and overwriting headers in lyx files.
#!/usr/bin/env python3
import sys
import tempfile
import io
import os
MANUAL="""
Usage: my-lyx-headerutil.py COMMAND ARG ...
@kbauer
kbauer / Comic Rocket New Pages Sorted.js
Last active October 8, 2016 19:37
Bookmarklet for sorting the comic-list on http://www.comic-rocket.com/ (after logging in) by the number of unread pages. Confirmed to work in Chrome for Desktop and Chrome for iOS.
/* Comic Rocket New Pages Sorted.js */
var NW = 4; /* number width */
var MODE = 0; /* 1 for reordering the divs, 2 for creating a new format, 0 for dialog */
function g(x,y){return Array.from(x.getElementsByClassName(y))};
function ce(e,tag){var x=document.createElement(tag); if(e){e.appendChild(x)}; return x;}
function lpad(s,n){ s = ""+s;while(s.length<n){s=" "+s;} return s;}
function rpad(s,n){ s = ""+s;while(s.length<n){s=s+" ";} return s;}
var divs = g(document,"comics-item");
var data = divs.map(function(c){
var [current,total] = g(c,"progress-label")[0].textContent.split("/");
@kbauer
kbauer / matrixform.py
Last active March 21, 2017 16:22
An equivalent to Mathematica's MatrixForm[] for pretty-printing sympy.tensor.array.Array
import sympy
from sympy import Matrix, Symbol
from sympy.tensor.array import Array
def matrixform(data):
"""
Translates an Array or nested list, or anything that behaves
similarly, into a structure of nested matrices.
"""
try:
@kbauer
kbauer / display-spotlight.bash
Created August 10, 2017 15:02
Display the uncropped images from Windows Spotlight (lockscreen) in a temporary directory with Windows Explorer. Implemented as `bash` script to be run in Cygwin. Requires image-magick to be installed.
#!/usr/bin/env bash
# -*- coding: iso-safe-unix -*-
set -e -E -u
#### SETTINGS
SPOTLIGHT_PATH="$(cygpath $LOCALAPPDATA)/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets"
OUTPUT_PATH='/tmp/SPOTLIGHT_DISPLAY'
#### CODE
@kbauer
kbauer / tree.bash
Last active August 11, 2017 09:55
A pure bash implementation of the `tree` utility, for when the `tree` binary isn't available. Avoids subshells or subprocesses to improve performance.
#!/usr/bin/env bash
# A fallback, when GNU tree isn't installed.
# Example:
# >>> mkdir -p {A1,A2}/{B1,B2/C1}
# >>> touch A2/B3
# >>> my-tree
# .
# |-- A1