Skip to content

Instantly share code, notes, and snippets.

@cabo
cabo / merge-patch-out.txt
Created December 10, 2013 10:47
JSON Merge-Patch examples: A simple implementation of json merge-patch (http://tools.ietf.org/html/draft-ietf-appsawg-json-merge-patch/). Code to extract the examples from the draft helps see the difference that eliding purge_nulls makes. Because of recent changes to JSON (non-containers at top level), requires "oj" gem.
Let's see where we don't agree (this code doesn't do purge_nulls):
Mismatch:
orig: {"a":"foo","b":[3,null,{"x":null}]}
patch: {"b":[3,null,{"x":null}]}
draft: {"a":"foo","b":[3,{}]}
actual: {"a":"foo","b":[3,null,{"x":null}]}
Mismatch:
orig: [1,2]
patch: [1,null,3]
draft: [1,3]
@cabo
cabo / studip-gruppen-machen.rb
Last active April 18, 2016 20:18
Stud.IP: Create a batch of student groups
#!/usr/bin/env ruby
require 'mechanize'
URL = 'https://elearning.uni-bremen.de'
agent = Mechanize.new
page = agent.get(URL)
login_page = page.link_with(text: /for registered users/).click
@cabo
cabo / gist:5388730
Created April 15, 2013 14:59
Convert an IEEE 754 Half-precision (16-bit) float into a native float in 11 instructions and 1 branch. Portable to IEEE 754 Single-precision implementations (i.e., everything post-VAX).
float decode_half(int half) {
union { uint32_t u; float f; } val;
val.u = (half & 0x7fff) << 13 | (half & 0x8000) << 16;
if ((half & 0x7c00) != 0x7c00)
return val.f * 0x1p112;
val.u |= 0x7f800000;
return val.f;
}
@cabo
cabo / electric-pair-better-inhibit.el
Created April 10, 2013 18:34
electric-pair-better-inhibit: Use this as electric-pair-inhibit-predicate if you want parentheses to come in pairs. (Works from Emacs 24 bzr-version ~ 112221 upwards.)
(defun electric-pair-better-inhibit (char)
(or
(eq char (char-after))
(and
(eq char (char-before (1- (point))))
(let* ((syntax (electric-pair-syntax char)))
(not (eq syntax ?\())))
(eq (char-syntax (following-char)) ?w)))
@cabo
cabo / nxml-hacks.el
Created August 7, 2012 07:48
Little helpers for nxml
(defun cabo:nxml-balanced-close-start-tag-block ()
"Close the start-tag before point with `>' and insert a balancing end-tag.
Point is left between the start-tag and the end-tag.
If there is nothing but whitespace before the `<' that opens the
start-tag, then put point on a blank line, and put the end-tag on
another line aligned with the start-tag."
(interactive "*")
(nxml-complete)
(nxml-balanced-close-start-tag-block))
@cabo
cabo / nfsping.c
Created July 5, 2012 08:23
nfsping: find out whether the NFS server is still answering requests
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <rpc/rpc.h>
#include <sys/time.h>
#include <string.h>
#include <arpa/inet.h>
@cabo
cabo / ej.sh
Created June 19, 2012 21:26
.profile snippet for completed command line eject (via hdiutil)
function ej() {
for i
do
echo ejecting /Volumes/"$i"...
hdiutil eject /Volumes/"$i"
done
}
function _ej() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local v=$(cd /Volumes/; find -x . -type d -depth 1 | sed s/..//)
@cabo
cabo / hhac.el
Created March 14, 2012 14:57
hs-hide-all-comments: get rid of all this verbose nonsense that nobody cares about :-)
(defun hs-hide-all-comments ()
"Hide all top level blocks, if they are comments, displaying only first line.
Move point to the beginning of the line, and run the normal hook
`hs-hide-hook'. See documentation for `run-hooks'."
(interactive)
(hs-life-goes-on
(save-excursion
(unless hs-allow-nesting
(hs-discard-overlays (point-min) (point-max)))
(goto-char (point-min))
@cabo
cabo / mm-to-org.rb
Created February 2, 2012 16:26
Convert a freemind mindmap to Emacs org/outline text
#!/opt/local/bin/ruby1.9
require 'nokogiri'
doc = Nokogiri::XML(ARGF).css("node").each do |node|
if t = node["TEXT"]
puts "#{"*" * node.ancestors("node").size}* #{t}"
end
end
@cabo
cabo / saner-drag-and-drop.el
Created January 22, 2012 12:16
More useful drag and drop behavior for Emacs on OSX
; Upon drag-and-drop to a frame (OSX window): Find the file in the frame,
; with shift: insert filename(s), space-separated;
; with control: insert filename(s) as lines, repeating the beginning of the line;
; with meta: insert file contents
; note that the emacs window must be activated (CMD-TAB) for the modifiers to register
(define-key global-map [M-ns-drag-file] 'ns-insert-file)
(define-key global-map [S-ns-drag-file] 'ns-insert-filename)
(define-key global-map [C-ns-drag-file] 'ns-insert-filename-as-lines)
(define-key global-map [ns-drag-file] 'ns-find-file-in-frame)