Tutorials & Tips
Posts explaining how to do something, fix something, etc. This is where you’ll find my useful posts.
Unfortunately, given how software evolves over time, I should probably say “once useful”. If you’re digging through the archives, check the dates.
Executable Org-Mode Files
My Emacs config lives in a massive org-mode file and I often want to re-tangle from outside Emacs (because, e.g., I broke my Emacs session for some reason). I could just use a simple Makefile
, but that’s no fun. Instead, I’ve turned my config into a self-tangling script by adding the following two lines to the top of my init.org
file and marking it as executable:
#!/usr/bin/env -S sh -c 'emacs --batch --eval="(setq org-confirm-babel-evaluate nil)" --file "$0" -f org-babel-tangle'
# -*- mode: org; lexical-binding: t -*-
Now I can run ./init.org
in my ~/.emacs.d
directory and my config will tangle itself into the correct files.
This might sound like a weird gimmick – to be honest, it kind of is – but I’ve been tangling my config this way for years at this point and it “just works” with no dependencies other than Emacs.
A similar trick can be used to execute all org-babel blocks in an org-mode file, making org-mode a viable meta-scripting language for tying together scripts written in different languages – even scripts executing across multiple machines via TRAMP.
#!/usr/bin/env -S sh -c 'emacs --batch --eval="(setq org-confirm-babel-evaluate nil)" --file "$0" -f org-babel-execute-buffer'
# -*- mode: org; lexical-binding: t -*-
#+TITLE: Hello World
#+BEGIN_SRC elisp
(message "Hello World!")
#+END_SRC
Fix For Lenovo X1 Carbon Not Charging
I recently made the mistake of plugging my Lenovo X1 Carbon (Gen 5) into a 5 volt USB-C charger overnight. The LED on the side indicated that it was charging but, when I woke up in the morning, it obviously hadn’t. Worse, it now refused to charge even when plugged into the correct charger.
The fix is simple (although undocumented as far as I can tell). Basically, you need to reset the battery as follows:
- Unplug from any power sources (this won’t work if you don’t do this).
- Reboot into the BIOS setup (F1 on boot).
- Navigate to the Power menu.
- Select the “Disable built-in battery” option.
- Wait for the laptop to power off and then wait 30 seconds.
- Connect the power and start the laptop.
This will temporarily disable the battery which seems to reset any “bad charger” bits.
Hopefully, this will save others some time and frustration.
The X1 Carbon is otherwise a great laptop with an awesome keyboard. However, because this is my blog and I can rant all I want here:
- The dedicated Ethernet port is pretty much useless given the ubiquity of USB-C. I’d have much preferred an additional USB or USB-C port.
- The nipple seems a bit firmer than the one on my X220 and also seems to get into the “wandering cursor” state a bit more frequently.
Disabling Firefox Addon Signature Verification
As of Firefox 48, it’s impossible to disable mandatory addon signature verification without monkey patching Firefox or recompiling with MOZ_REQUIRE_SIGNING
unset. Personally, I find this unacceptable as the user should always be in charge. It’s also completely useless as any party powerful enough to disable the signature verification in about:config
could just as easily sideload a powerful (but signed) extension like greasemonkey and then install a malicious greasemonkey script.
Rants aside, the correct solution (for the user) is to either recompile Firefox with mandatory signature verification disabled or use the Firefox Developer build. Unfortunately, Firefox is a monster and recompiling it just isn’t a viable option for me (or anyone with a laptop). Also unfortunately, prepackaged Firefox binaries are missing some useful security features like PIE and dynamic libraries. Finally, the Firefox Developer build is a bit too bleeding edge for my taste (I would like my primary browser to be relatively bug free).
So, I’ve written a (disgusting) script that monkey patches Firefox’s omni.ja
to make signature verification optional again. I’ve only tested it on Arch Linux but it should work on all unix-like systems. However, if your omni.ja
file is not in /usr/lib/firefox/
, you’ll have to tell the script where to find it (i.e., ./nosign.sh /path/to/omni.ja
).
NOTE: This script does not disable addon signature verification, only makes it optional. To turn it off, you still need to set xpinstall.signatures.required
to false in about:config
.
WARNING: This script updates the omni.ja
file IN PLACE (using sudo).
WARNING: Use at your own risk.
Pipe to Emacs
While there are many ways to pipe to emacs, they all involve either shuttling text by repeatedly calling emacsclient or writing to a temporary file. However, neither are necessary.
Basically, while emacs can’t (yet) read from a named pipe (FIFO), it can read standard output from a process so, one gratuitous use of cat
later…
(defun pager-read-pipe (fname)
(let ((buf (generate-new-buffer "*pager*"))
(pname (concat "pager-" fname)))
(with-current-buffer buf (read-only-mode))
(switch-to-buffer buf)
(let ((proc (start-process pname buf "/usr/bin/cat" fname)))
(set-process-sentinel proc (lambda (proc e) ()))
(set-process-filter proc (lambda (proc string)
(when (buffer-live-p (process-buffer proc))
(with-current-buffer (process-buffer proc)
(save-excursion
;; Insert the text, advancing the process marker.
(let ((inhibit-read-only t))
(goto-char (process-mark proc))
(insert string)
(set-auto-mode)
(set-marker (process-mark proc) (point))))))))
proc)))
…and you can read a from named pipe. As an added bonus, this function will try to autodetect the correct mode.
To actually use this, I recommend the following shell script:
#!/bin/bash
set -e
cleanup() {
trap - TERM INT EXIT
if [[ -O "$FIFO" ]]; then
rm -f "$FIFO" || :
fi
if [[ -O "$DIR" ]]; then
rmdir "$DIR" || :
fi
}
trap "cleanup" TERM INT EXIT
SOCKET="${XDG_RUNTIME_DIR:-/run/user/$UID}/emacs/server"
# Create a named pipe in /dev/shm
DIR=$(mktemp -d "/dev/shm/epipe-$$.XXXXXXXXXX")
FIFO="$DIR/fifo"
mkfifo -m 0600 "$DIR/fifo"
# Ask emacs to read from the names socket.
emacsclient -s "$SOCKET" -n --eval "(pager-read-pipe \"$FIFO\")" >/dev/null <&-
exec 1>"$FIFO"
cleanup # Cleanup early. Nobody needs the paths now...
cat
You will probably need to set the SOCKET
variable to your emacs socket filename.
Usage:
dmesg --follow | epipe
Turn the Zim Desktop Wiki into a calendar
Caveat lector: I wrote this post in high school; it’s likely outdated and poorly written.
Why
I recently switched to Arch Linux and decided to ditch evolution (a good but bloated program). Claws Mail works perfectly as an email manager but I couldn’t get its calendar plugin to work properly. I have been using Zim for a while and noticed that it had a very basic calendar plugin; this was exactly what I needed. The plugin allows users to create pages in their wikis for individual days: no complex forms to fill out, just a simple page to keep track of what you are doing on a given day.
What
As Zim lacks desktop integration so I wrote two python scripts for conky integration:
zim-conky_cal.py
Prints a calendar (like the cal command) with the current date and appointments highlighted.
zim-conky_events.py
Lists the next 5 events or all of the events in the current month and the next, whichever comes first.
I also wrote a program for adding events to the calendar (zim-cal.py and zim-cal.ui). Select some text, run the program and double click the date to add your text to calendar. You can also input your own text by clicking the edit button (the big button on the right).
How
First: Enable the calendar plugin (Edit->Preferences->Plugins->Calendar).
Download: zimcal.tar.bz2
Conky scripts
- Edit CAL_PATH to point to the folder that stores your Zim calendar.
- Add
${execpi 300 /path/to/zim-conky_cal.py}
and${execpi 300 /path/to/zim-conky_events.py}
to your conkyrc
Zim-Cal program
- Edit CAL_PATH to point to the folder that stores your Zim calendar.
- Make PROG_PATH point to the directory where you put “zim-cal.ui”
- If you intend to use this program regularly, you should probably assign a global hotkey to it in your window manager.
Make Gksu and Policykit red
Caveat lector: I wrote this post in high school; it’s likely outdated and poorly written.
I was bored one day and decided to make my gksu(do) and policykit dialogs red. The results are actually quite nice.
Add this to the bottom of your gtkrc file:
style "gksu" {
bg[NORMAL] = "#770000"
bg[ACTIVE] = "#550000"
bg[PRELIGHT] = "#990000"
bg[SELECTED] = "#550000"
bg[INSENSITIVE] = "#220000"
}
widget "GksuuiDialog*" style "gksu"widget "PolkitGnomeAuthenticationDialog*" style "gksu"
Packagekit with apturl
Caveat lector: I wrote this post in high school; it’s likely outdated and poorly written.
Although I no longer use packagekit, I still have my apturl script so I thought I would post it. This script will allow you to open apt:// scripts with packagekit. (Just save this to a file, mark it executable, and tell your browser to open apt scripts with it).
#!/bin/bash
/usr/bin/gpk-install-package-name $(echo $* | sed -e 's/apt:\/\?\/\?//')
Useful Bash functions
Caveat lector: I wrote this post in high school; it’s likely outdated and poorly written.
cdd: cd and list the files.
function cdd(){ cd $* ls --color}
changelog: get the change log for a program
changelog() {
log=/usr/share/doc/"$*"/changelog*
if [ -r $log ]; then
less $log
unset log
else
log=/usr/share/doc/"$*"/CHANGELOG*
if [ -r $log ]; then
less $log
fi
fi
}
mkdircd: make a directory and move in.
function mkdircd() {
mkdir $* cd ${!#}
}
Start conky only after the root window loads
Caveat lector: I wrote this post in high school; it’s likely outdated and poorly written.
Every time I logged in, conky would start up before nautilus loaded the desktop. This caused conky to load as a floating window that stayed on top of all other windows. I have finally gotten around to fixing this problem.
Here (gone, email if found) is a simple python script that waits for nautilus to load the desktop before starting conky.
I stored the script referenced in this post in a paste-bin and now it’s gone. Live and learn…
This script is probably very inefficient but it gets the job done.
Humanity Icon for Caffeine
Caveat lector: I wrote this post in high school; it’s likely outdated and poorly written.
For those who don’t know, Caffeine is a small program for Linux that lets a user prevent his or her computer from entering a power save state. If a user wishes, he or she can even configure caffeine to automatically inhibit power-saving when watching a flash movie, or running VLC, Totem etc. For more information, visit its website here.
As a user of both Caffeine and the new Humanity icon theme (the default icon theme in karmic), I made a very basic gray-scale version of the Caffeine icon. You can download it here.