Skip to content

Instantly share code, notes, and snippets.

View spacebat's full-sized avatar

Andy Kirkpatrick spacebat

  • Adelaide, South Australia
View GitHub Profile
@spacebat
spacebat / enable-lexical-binding.el
Created October 27, 2014 11:42
Set lexical-binding to t
(defun lexbind-insert-lexbind-enablement (&optional verbose)
"Attempt to set `lexical-binding' to t in the current buffer's
prop-line."
(macrolet ((with-prop-line (&rest body)
`(save-excursion
(save-restriction
(goto-char 1)
(forward-line 1)
(narrow-to-region 1 (point))
(goto-char (point-min))
@spacebat
spacebat / system.el
Last active August 29, 2015 14:06 — forked from intinig/system.el
(defun source-env-get (script &rest vars)
"Source script in shell, then look for vars in the resulting subshell environment"
(loop for line in (split-string (shell-command-to-string (concat "source " script " && set")) "[\n]" t)
with result
if (string-match "^\\([[:alpha:]]+\\)=\\(.*\\)$" line)
do (let ((var (match-string 1 line))
(val (match-string 2 line)))
(when (or (not vars) (member var vars))
(push (cons var val) result)))
finally return result))
@spacebat
spacebat / template-expand.el
Created July 23, 2014 12:34
Recursive template expansion in emacs lisp
(cl-defun template-expand (string data &key
recur
(notation "${\\([^}]+\\)}")
(default (lambda (x) (error "No expansion found for '%s'" x))))
"Expand variables from the alist `DATA' into the string `STRING'.
The `RECUR' parameter triggers expansion of variables found in
the substituted text.
@spacebat
spacebat / org-capture-templates.el
Created May 7, 2014 04:59
org-capture-templates
(setq org-capture-templates
(("a" "Appointment" entry
(file+headline
(concat org-directory "/taskdiary.org")
"Calendar")
"* APPT %^{Description} %^g\n%?\nAdded: %U")
("n" "Notes" entry
(file+datetree
(concat org-directory "/taskdiary.org"))
"* %^{Description} %^g %?\nAdded: %U")
@spacebat
spacebat / parcel-passer.pl
Created August 8, 2013 06:03
A proof of concept for sending code as strings for evaluation in a child process.
#!/usr/bin/env perl
# ABSTRACT: proof of concept for sending subs to a child process for evaluation
use strict;
use warnings;
use v5.10;
use Data::Dumper;
use Socket;
use Time::HiRes qw(sleep);
(require 'bookmark)
(defvar my-doc-view-bookmark-push-p nil
"Whether to push automatic doc-view bookmarks, or clobber them.")
(defun my-doc-view-open-handler ()
(bookmark-jump (buffer-name)))
(defun my-doc-view-save-handler ()
(if (eq major-mode 'doc-view-mode)
;;; The macro:
(defmacro add-hook-one-time (hook function)
"Add FUNCTION to HOOK. Remove it after its first execution."
(let ((wrapper-function (make-symbol "wrapper-function-symbol")))
`(progn
(defun ,wrapper-function ()
"Wrapper function that will be executed only once, and then removed from the hook."
(funcall ,function)
(remove-hook ,hook ',wrapper-function))
(add-hook ,hook ',wrapper-function))))
@spacebat
spacebat / month-name-to-number.el
Created April 21, 2013 03:46
A bit of month transform code
(setq month-names '("January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December"))
(setq month-names-to-numbers (loop for name in month-names for number from 1 collect (cons name number)))
(defun month-name-to-number (name)
(or (cdr (assoc name month-names-to-numbers))
(error "Month not recognized: '%s'" name)))
(defun month-name-to-number-nice (name)
(let ((number (month-name-to-number name)))
@spacebat
spacebat / insert.el
Created April 8, 2013 22:57
Insert an element before another in a list
(let ((data '((a 1 2 3)(c 5 6 7)))
(insert '(b x y z)))
(loop for prev on data
for next on (cdr data)
if (and next (eq (caar next) 'c))
do (let ((newcons (cons insert next)))
(setcdr prev newcons)))
data)
@spacebat
spacebat / *scratch*.el
Last active December 15, 2015 14:19 — forked from nicferrier/*scratch*.el
I think the [^[] was making the start-of-line/string optional
(let ((regex
(rx
(or bol bos)
(? (not (any "[")))
(group
(>= 2 (and (any "A-Z")(one-or-more (any "a-z")))))))
case-fold-search)
(list
(replace-regexp-in-string regex "{\\1}" "[OneWordOrMore]")
(replace-regexp-in-string regex "{\\1}" "Onewordormore")