Skip to content

Instantly share code, notes, and snippets.

algal / term-iterm2.el
Created May 5, 2024 22:33
Make emacs see all keystrokes in TTY mode with iTerm2
;; This package makes changes to how emacs interprets CSI escape codes, so that
;; it better understands "CSI u"-style codes, so that more key chords pass
;; through to emacs in terminal mode.
;; It is intended to work with the following terminal confugration:
;; - iTerm2 / Keys / Report modifiers using CSI u: enabled
;; - iTerm2 / Keys / xterm control sequences can enable modifyOtherKeys mode: enabled
;; - iTerm2 / Terminal / Report Terminal Type: xterm-direct
algal /
Last active May 1, 2024 16:42
#!/usr/bin/env python3
import sys
import re
from collections import deque
def usage():
print("Usage: {} [--onebased | -o | -u] ROW COLUMN".format(sys.argv[0]))
algal /
Last active March 7, 2024 19:37
Calls Anthropic's claude-3-opus, passing in the command-line argument as the prompt.
#!/usr/bin/env bash
if [ "$#" -eq 0 ]; then
echo "Usage: $(basename "$0") promt_to_send_to_claude"
echo ""
echo " Requirements: ANTHROPIC_API, defined; jq and curl, installed; bash, version 3 or higher."
exit 1
function claude() {
local json_request
algal / dateify
Last active February 7, 2024 07:49
Rename a file to prefix with its creation date, or undo this. Makes file match the Denote file-naming scheme
# compatibility: works on Debian 12, macOS Sonoma, and in Linux-simulating shell environments in macOS.
# Function to display usage message
usage() {
echo "Usage: $(basename "$0") [OPTION]... [FILE]..."
echo "Rename files by adding or removing a date-time prefix"
echo "in the following format yyyymmddThhmmss--"
algal /
Created January 22, 2024 17:31
bash script to query
#!/usr/bin/env bash
# based off of
if [ "$#" -eq 0 ]; then
echo "Usage: $(basename $0) promt_to_send_to_perplexity"
echo ""
echo " Requirements: PERPLEXITY_API, defined; jq and curl, installed; bash, version 3 or higher."
exit 1
function p() {
algal / run-with-emacs
Last active September 6, 2022 17:25
Boilerplate to write a command line script in emacs lisp
:;exec emacs --no-init-file --no-site-lisp -script "$0" -- "$@" # -*- mode:emacs-lisp -*-
(defun main ()
(require 'cl-lib)
(let ((script-name (nth 2 command-line-args))
(args (cdr command-line-args-left)))
;; assert: ARGS now is a possibly empty list of command line args to the script
;; check for valid arguments here
(when (not args)
(princ (format "usage: %s PATH_TO_FILE" script-name))
algal /
Created May 29, 2021 22:42
binomial coefficients with dynamic programming
# binomial coefficients
Recurrence relation:
C(n,k) is the count of all k-subsets in a set of n items.
C(n,k) = C(n-1,k-1) + C(n-1,k)
Intuition underlying the relation:
1. pick any item from the n-set, call it the special item.
2. Every k-subset either includes the special item or does not.

24 bit color in emacs, under

What's the problem?

The normal way to enable 24 bit color on a recent emacs (27.1) and a recent OS distribution (one which already has the terminal type definition xterm-direct), is just to set TERM=xterm-direct before running emacs.

But this does not work with, probably because of some incompatibility in its underlying library, hterm.

What's the solution

algal / isSubcollection.swift
Last active March 13, 2021 18:37
Look for a subarray in an array in Swift
extension Collection {
/// True if `self` is a range within `collection`
/// - Parameter collection: a `Collection` like `Array`
/// - Returns: true or false
/// Since this works with `Collection` it works with `Array`, `ArraySlic`, etc..
func isSubcollection<U>(of collection:U) -> Bool
where U:Collection, U.Element == Self.Element, Self.Element:Equatable
let (a,b) = (collection,self)
algal / join_pdf.bash
Last active February 16, 2021 02:00
join PDFs on macOS from the command line with zero dependencies
if [ "$#" -lt 2 ]; then
echo "usage: $0 output_pdf_path input_path1..."
echo "This script joins the PDFs at input_path1, input_path2, etc."
echo "into one pdf at output_pdf_path"
echo "Known good: macOS 11.1, and below for many versions"
exit 1