Skip to content

Instantly share code, notes, and snippets.

View wsgac's full-sized avatar

Wojciech S. Gac wsgac

  • Keepit
  • Warszawa, Poland
View GitHub Profile
@wsgac
wsgac / green-threads-example.lisp
Created May 14, 2026 15:15
Simple example of using Green Threads to dispatch work and collect results
(ql:quickload :green-threads)
(cl-cont:defun/cc channel-squared (n h &key (threads 20))
(let ((in (make-instance 'gt:channel))
(out (make-instance 'gt:channel)))
;; Spawn workers
(loop
:repeat threads
:do (gt:with-green-thread
(loop
@wsgac
wsgac / mixins.lisp
Last active October 21, 2025 20:56
Commutative CLOS mixins demo
(defvar level)
(defclass setup-mixin () ())
(defmethod run :around ((m setup-mixin))
(let ((level 0))
(call-next-method)))
(defclass mixin-1 () ())
@wsgac
wsgac / mark-next-previous.el
Created July 16, 2025 11:27
Make Eww recognize additional types of URLs as next/previous
(defvar navigation-group-regex "postnav\\|navigation"
"Regular expression to match the class of the navigation container")
(defvar prev-link-labels-regex "earlier\\|previous"
"Regular expression matching the `previous` link")
(defvar next-link-labels-regex "later\\|next"
"Regular expression matching the `next` link")
(defun my-eww-add-rel-prev-next ()
"Add rel=\"prev\" and rel=\"next\" attributes to postnav links in EWW."
(when (eq major-mode 'eww-mode)
@wsgac
wsgac / get-file-path.el
Created June 11, 2025 09:59
Elisp function for getting position in files + an opinionated keybinding
(require 'cl-lib)
(defun get-current-file-path (prefix)
"Get filesystem from current buffer, provided it's a file buffer. With
prefix 4 also append line number. With prefix 16 append column number to
everything. Put the resulting string in the kill-ring and also display
it int the minibuffer."
(interactive "p")
(let* ((file-name (buffer-file-name (current-buffer)))
(position (when (>= prefix 4) (list ":" (int-to-string (line-number-at-pos)))))
@wsgac
wsgac / random-state.lisp
Created June 5, 2025 13:04
Common Lisp random state
;; Stores random state in a structure SB-KERNEL::RANDOM-STATE
*random-state*
;; Get the actual state vector
(sb-kernel::random-state-state *random-state*)
;; Intercept the state vector (copying it)
(defvar *rs1* (copy-seq (sb-kernel::random-state-state *random-state*)))
;; Sample a bunch of random numbers
@wsgac
wsgac / tooltips.el
Created May 9, 2025 14:41
Kanji-mode exploration with tooltips
;;;;;;;;;;;;;;
;; Tooltips ;;
;;;;;;;;;;;;;;
;; TODO: These need polishing and weaving them into the broader
;; picture
(defun get-point-coordinates ()
(let* ((posn (posn-at-point))
(abs-pos (posn-x-y posn))
@wsgac
wsgac / invert_recursive_dict.py
Created October 19, 2024 22:14
Recursive dict reversal in Python
def invert_recursive_dict(d: Dict) -> Dict:
"""
Assuming `d` is a recursive Dict, invert it by mapping its leaves to the
lists/paths of keys leading to them in the original dict.
"""
def invert(d, path: List, acc: Dict):
if isinstance(d, Dict):
# Handle dict contents recursively
for k, i in d.items():
invert(i, path+[k], acc)
@wsgac
wsgac / .psqlrc
Created July 29, 2024 09:41
More reasonable PSQL config
-- prevent noisy loading of psqlrc file
\set QUIET yes
--customize prompt
\set PROMPT1 '\n%[%033[1;31m%]➤ %[%033[2;37m%]%`\! date "+%F %I:%M %p %Z"`%[%033[0m%] %[%033[1;36m%]%n%@%/%[%033[1;31m%]%x %[%033[K%]%[%033[0m%]\n%[%033[1;33m%]%R%#%[%033[0m%] '
\set PROMPT2 '%[%033[1;33m%]%R%#%[%033[0m%] '
--show timing info for queries
\timing
-- allow pasting of values to avoid stupid indenting
\set paste
--get rid of duplicates in history
@wsgac
wsgac / lookup-fiveam-fixtures.lisp
Last active July 25, 2024 22:29
Teach SLIME how to jump to new kinds of definitions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; HACK: Teach SLIME about jumping to FiveAM fixture definitions ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'sb-introspect)
(ql:quickload :swank)
(ql:quickload :fiveam)
;; Poor man's defadvice - stolen from https://gist.github.com/spacebat/46740966846623148c014ab261050bc0
@wsgac
wsgac / paredit-setup.el
Last active September 24, 2024 14:41
A `use-package` enabled configuration of Paredit with an annoying problem worked around
;;;;;;;;;;;;;
;; Paredit ;;
;;;;;;;;;;;;;
(use-package paredit
:ensure t
:init
:config
(show-paren-mode t)
;; Workaround for superfluous spaces inserted in e.g. pathnames, as