Refactoring an Emacs theme

I’m gradually working on a custom theme for emacs, and I wanted to make it easier to update and read. (It’s not quite ready to share, but I look forward to doing just that when it is.) Surprising no-one, the theme format on emacs is not incredibly … “clean.” It’s basically a command that passes in a huge list of lists (of lists… this is lisp, after all). That command is custom-theme-set-faces.

(custom-theme-set-faces
 'my-cool-theme                         ;theme name
 '(
   (FACE SPEC)
   (FACE SPEC)
   ))

FACE is the symbol for a particular element, for example default for default text or error which is used for error messages. Easy. SPEC, however is a bit more complicated. SPEC is a list of DISPLAY ATTS pairs, each list of attributes pertaining to its paired display. If this were CSS, I think it would be similar to including the @media selector for each rule.

(DISPLAY can simply be default to apply to all displays, but I haven’t seen this used much and am trying to follow suit.)

So now we have this.

(custom-theme-set-faces
 'my-cool-theme                         ;theme name
 '(
   (FACE ((DISPLAY ATTS) (DISPLAY ATTS)))
   (FACE ((DISPLAY ATTS) (DISPLAY ATTS)))
   ))

ATTS is property list, where you can specify the width, height, etc. for the font face, on that display.

So plugging in some actual values, we have the following:

(custom-theme-set-faces
 'my-cool-theme                         ;theme name
 '(
   ;; error face, using default DISPLAY (simpler)
   (error ( (default (:foreground "red" :background "black")) ) )

   ;; default face using DISPLAY specified by a a list of conditions (less simpler)
   ;; In this case, a color terminal that supports at least 89 colors.
   (default ( ( ((class color) (min-colors 89)) (:foreground "white" :background "black")) ) )
   ))

I was working with a bunch of lines that basically looked like the second case. There was some substitution, but it was still painful to see all that repetition.

(let
    ((class '((class color) (min-colors 89))))
  ;; ...
  `(default ((,class (:foreground "white" :background "black"))))
  ;; ...
)

I wanted to remove the DISPLAY component completely and just add it later using code. This would mean a cleaner and easier to maintain theme. The excellent emacs-doom-themes has a very elegant solution, but I didn’t quite understand all the elisp and thought I could roll a simpler version myself as a good learning experience.

Lots of fumbling around elisp ensued, but I now have something I’m happy with.

;; the simple-faces are bound to a list `two-fifteen-simple-faces'
(default (:background ,bg1 :foreground ,fg1))
(error   (:foreground "red" :background "black"))
;; ...

;; Add the display to the simple-faces
(while two-fifteen-simple-faces
  (let*
      ((simple-face (car two-fifteen-simple-faces))
       (fname (car simple-face))
       (fattrs (cadr simple-face)))
    (setq two-fifteen-simple-faces (cdr two-fifteen-simple-faces))
    (setq built-faces (append
                       built-faces
                       `((,fname ((,class ,fattrs))))))))

;; apply custom-theme-set-faces to built-faces
(apply #'custom-theme-set-faces 'two-fifteen built-faces)

Next up is making a palette with re-usable colours, which I’ve started w/the bg1 and fg1.

Here are some functions and modes I’m finding extremely helpful when working on the theme:

  • describe-face : Describe the face at the point (cursor)
  • list-faces-display : List faces in use on display
  • rainbow-mode : Display colour values using the actual colour
  • list-colors-display : visually browse colours names and codes
  • align-regexp : Align attribute blocks of the theme to make it easier to read
  • sort-lines : Sort sections alphabetically
  • Iedit and narrow-to-region : Refactoring
  • Keyboard macros, OF COURSE. : start ( C-x ( ), stop ( C-x ) ), run ( C-x e )

There are moments where I do something and a big fat grin creeps onto my face, appreciating what I just did using Emacs. You know what I’m talking about! 🙂

Oh right, here’s a picture of what the theme currently looks like. It’s called two-fifteen. 2019-08-05_emacs_two_fifteen_theme.png

Python 3 Iterable Unpacking Catch-all

I’m one of those terrible people that still haven’t switched over to using Python3 …just yet. But one thing that pleasantly surprised me recently was learning of the “new” starred expression catch-all that can be used when unpacking iterables. This lets you use up to one starred expression to receive the rest of the items of an iterable during an unpacking assignment.

full_name = "Steven W J Brown"

#python2
names             = full_name.split()                # ['Steven', 'W', 'J', 'Brown']
first, last       = names[0], names[-1]              # 2.A: ignore middle names
first, mids, last = names[0], names[1:-1], names[-1] # 2.B: store middle names

#python3, 
first, *_, last    = full_name.split() # 3.A: ignore middle names
first, *mids, last = full_name.split() # 3.B: store middle names

It’s easy to read, eliminates the need for an intermediary variable and makes the code cleaner. And it can be used in loops. More details here: PEP 3132. Love it.

I really have to start re-configuring my environments for python3….

Org-mode: Indirect Buffer, Narrowing to tree

Here’s a really quick Emacs Org-mode tip that I find extremely useful.

In your config add the following two lines:

(setq org-indirect-buffer-display 'new-frame)
(setq org-src-window-setup 'other-frame)

(Or just run it in your scratch buffer to try it out for your current session.)

The first line sets the org-indirect-buffer-display variable to new-frame. Now when you call org-tree-to-indirect-buffer or org-agenda-tree-to-indirect-buffer (C-c C-x b) from an Org buffer or an Org agenda item, Emacs will create a new frame narrowed to that Org sub-tree. I find this much more useful than the default other-window as I usually operate with one frame vertically split with two windows, and like to maintain manual control over the contents of the windows.

Similarly, I like a frame-focused behaviour when editing literal source blocks, which is what the second line sets. While in an Org source block, calling org-edit-special (C-c ' ) will open a new frame narrowed to the current source block and in the appropriate major mode (python-mode, for example). The variable for this setting is org-src-window-setup.

To see the documentation for either variables, use the Emacs help system! (C-h v variable-name)

See also: Emacs Indirect-Buffers, Emacs Buffer Narrowing, org-narrow-to-subtree

Emacs and Org-mode

Well this is odd. I’ve returned to a text editor I was introduced to in university, but wasn’t really taken with until just recently. The editor is Emacs and the reason is org-mode. Apparently, my story is not all that uncommon and org-mode (with evil mode) has even converted many Vim users.

Emacs has been around since 1976. (Nineteen-seventy-six! And it’s still actively developed! That’s insane!) Org-mode is an Emacs extension (major-mode) that has been in development since 2003 and included in Emacs core since 2006. Org-mode is basically an everything-mode; it allows for really intuitive outlines and section management, source code blocks, inline evaluation of those blocks and literal programming, comprehensive exporting, intra- and inter-document hyperlinking, tables/spreadsheets, presentations, ….the list goes on, and on, and on. And on.

The Boston Emacs Meetup has quite a few good Emacs videos with live demonstrations, and I really enjoyed this one, by Harry Schwartz:

(YouTube: Getting Started With Org Mode)

As I continue to migrate all-the-things over to Emacs and Org-mode, it made me think about why I didn’t put more time into learning Emacs before.

When I was introduced to Emacs in university (somewhere around 2002 or 2003), org-mode was not included in Emacs and relatively young. At that time, Java was being promoted heavily in education, and for programming Java, it makes the most sense to use a Java IDE such as Eclipse or NetBeans which make the verbose language far more tolerable. So that’s generally what I did. But for my C or C++ programming, and general text file editing, I usually chose Emacs over Vi(m) due to it being more similar to what I was used to – but that’s it. I could move about in Emacs without leaving home-row, and I could make a few basic changes in VIM, save, and quit without panicking1. I could have been using Notepad++ and would have been just as happy. I was not taking advantage of either Emacs or Vim.

I never learned about org-mode until roughly 2 years ago (somewhere between June and August, 2016). Since then, I’ve gradually migrated as-much-as-I-fucking-can to these wonderful plain text files. I manage my work projects and todo lists. My agenda. My emacs config file. My blog(s); this post (via org2blog). I write documentation that can be exported into a variety of formats. And I keep learning new things; both for Emacs and for org-mode. Just recently, the org-mode time tracking finally clicked for me. I now schedule my days better and try to track where my time goes. This is extremely useful in evaluating what’s eating up my time, and estimating how long tasks will take so I can plan better.

2018-08-12_emacs.png

The draw of org-mode has also made me appreciate Emacs, of course. I connect to Jupyter notebooks via ein and work on them with conveniences not offered in the browser interface2. I’ve heavily customized my environment, picked up a bit of elisp, and I’ve started tweeking my own theme. I recently moved from primarily using Linux to primarily using Windows, which has presented a few challenges with Emacs’ setup, but it’s helped me develop a consistent environment between work and home.

Both Emacs and org-mode are powerful but take a significant time investment to configure and use effectively. Spacemacs, a distribution of Emacs that comes heavily pre-configured and tries to combine the best of Emacs and Vim, offers a simple way to see what an advanced setup could look like. It’s not how I started, and some consider it too bloated, but it’s very well put together and certainly worth looking at.

Anyway, I’ll probably be reviving this blog with the odd org-mode or Emacs post, as that is what I’m playing with, these days. But in addition to the video above, a good intro would be Carsten Dominik (org-mode’s creator) presentation at Google:

YouTube: Emacs Org-mode – a system for note-taking and project planning

Footnotes:

1

I have a great appreciation for Vim’s modal editing and language. Tempted to use Evil Mode, but I have enough things on my plate, at the moment. 🙂

2

I want to investigate moving my Jupyter notebook products to org-mode files under Git, only exporting them to notebooks, when needed… but that’s probably a separate post.

Script: Wireless Strength Polling/Logging/Graphing

Initially, I wrote this script to give me frequent feedback on the signal strength. This is useful when adjusting antennas to that sweet spot that give stronger signals; especially if you’re testing some homemade tinfoil parabolic reflectors! 😉 If you have a portable wireless device, like a netbook, you can ssh into your (wireless) desktop and run wireless-strength to get realtime feedback on adjustments to the access point’s antenna… assuming the connection doesn’t break. 😛 And, of course, you can just walk around running it on your mobile device to create a kind of wireless heatmap.

If you have gnuplot, you can also generate graphs from the data with ws-plot. This is me walking around my house with my notebook:

I started in my room (40% 🙁 ), which is where the first peak is – near the window. Left my room, back to 40%, peak near window again, then bathroom… 40%. The climb from 40-80% is me walking towards the TV room (PS3 lives in a solid 80% zone, at least!). Walked upstairs, got 100% in most areas (that’s where the Access Point is) – tried a bedroom, dropped to 40%.

Download the scripts and get more details here: https://github.com/izm/wireless-strength

Nerdy Ramblings

This script originated years back, but I recently tried using it on my laptop and it didn’t work! Unacceptable! The original parsed the output of iwconfig. But what showed up as “Quality=30/70” on my desktop, would show up as “Quality:4” on my laptop. COMPLETELY DIFFERENT. Even the character after “Quality” was different! o.O The output of iwconfig is driver dependent, which is why I looked to network-manager. I figured there’s probably a nice dbus command I can send to network-manager for that purpose – but I got tired of looking and decided to just parse output again. :/ Luckily, network-manager includes nm-tool. It’s certainly not a clean solution, but it works for now.

I also used updating rewriting the script as an excuse to get better acquainted with git – which I’m really liking.

Every time I do a bash script, I vow to do the next script in Python. I like Python and I don’t like Bash… but there’s a certain… nativeness or dependency-free elegance to bash scripts. Still, I hate writing them, and the next script’s in Python! 😛

Theme updates

Made some adjustments to my blog theme.

Old

New

  • search box graphic and placeholder text
  • added jetpack subscriptions module and some css to handle it
  • gave page a max width so it doesn’t look ridiculous on wide screen monitors.
  • sidebar: removed dotted borders added white space, toned down colours – reduced noise
  • Removed grey post-meta-content (categories, tags) boxes. Put all post meta data in TOP, with comments repeated at bottom. (Similar to when I started this theme!)
  • Added hover highlight to gallery images (now consistent with image links not in a gallery). Cleaned up some of the code (to not generate HTML comments) and random fixes here and there.
  • Removed lots of black. Too harsh.

You might have to click refresh. And you might not even notice any changes. 😛

Feel free to leave a comment or use Markup to make suggestions. Markup’s a pretty cool tool.

It’s interesting to compare the progression of my themes. Well, interesting for me, at least. 🙂

I think the next time I decide to work on a theme, I will start from scratch. Clean slate. Make it more consistent with my root page.

On a related note, the latest version of WordPress, 3.3.2, is really nice.

Happy Mother’s Day 2012!

It was Mother’s Day! I love cooking, so I made something special for my mom. Actually, me and 2 of my sisters were the kitchen workers today. I made a salad. So I know my mom likes this shrimp and avocado salad that is served at the restaurant where I went to culinary school. Fine. I’ll make that. Again. At home. For her. 🙂

This is how it turned out:

This was a sample plate I did. I changed the plating a bit… moved the tomatoes face-down on the cucumbers.. and added a full steamed prawn. But it looked something like this.

It was good! If I were to do it again, I’d play around with the plating some more… and I’d make the balsamic reduction thicker (when I tried to spell “Mom” in cursive on my mom’s plate, it gradually expanded until you couldn’t read it…. >.< ). But it turned out pretty good. But the chive oil… oh man… is it ever perdy. Put it in a custard cup in some good light: sexy.

That other thing in the photo is dry whole wheat pasta – another experiment. ^_^

Happy Mother’s Day!

Dessert

After hurting my shoulder, I had to take a 2 month break from culinary school. Hopefully, I’m fit for the kitchen again, because I go back to school tomorrow. Better start doing more cooking!

Last night, I had company coming that I knew liked dessert. There wasn’t going to be many of us, so I decided to make crème brûlée, a classic French dessert.

Turned out good, but I guess I could have caramelized them a bit more.

Then I had the dilemma of what to do with the egg whites. I’ve seen a few Australian cooking shows recently, and pavlova is something that is always mentioned and held in high regard. So I decided to try it.

I guess it turned out okay. No weeping at the top, but the bottom had a bit of syrup weeping out. I’m thinking it may have been slightly undercooked. It deflated a bit as it was cooling, but that’s normal, apparently.

All pavlova recipes I found on the Internet called for an insane amount of sugar, so I found the most reasonable one and cut it back a bit. Even after cutting the sugar in the recipe, I still found it cloyingly sweet. So for a sauce, I made a tart raspberry and mango coulis. The combination was actually quite good. It should also be served with fresh fruit and some kind of creamy sauce… pastry cream or whipped cream or something. Since we were already having crème brûlé, making a creamy accompaniment wasn’t really appealing to me. Was thinking fried bananas might be nice. Next time.

Gedit 3.2 GDP Completions on Ubuntu 11.10 Oneiric Ocelot

Curiosity gets the best of me sometimes. Okay, most of the time. Did you know GNOME’s text editor, gedit, has a plethora of extensions which can basically transform it into an IDE? Something I’ve always wanted is intellisense-style autocompletion. The closest thing I’ve found for gedit is GDP Completions Plugin in the gedit-developer-plugins package in Ubuntu.

sudo aptitude install gedit-developer-plugins

However, there’s a bug in that package and the popup menu doesn’t actually work. Ctrl + Space is supposed to bring it up. So you want to add the Gedit Developer Plugins PPA and upgrade to the more recent version.

sudo add-apt-repository ppa:user/ppa-name
sudo apt-get update
sudo apt-get upgrade gedit-developer-plugins

If you try to run gedit now, you’ll notice it won’t… run, that is. Great. I know, right? The problem is that the bzr plugin (also included in the gedit-developer-plugins package) is trying to use the gtk2 version of bzr-gtk, but that doesn’t work in the gtk3 gedit. Anyway, you can pull a copy of the gtk3 bazaar plugin into your local bzr plugins directory. (I found this info here). Create ~/.bazaar/plugins/ if it doesn’t exist.

mkdir ~/.bazaar/plugins
cd ~/.bazaar/plugins
bzr branch lp:bzr-gtk/gtk3 gtk

The gedit-developer-plugins package and gedit should work after that! An alternative to the above would be to add a PPA that includes bzr-gtk 3. Not sure if one exists at the moment, but that would be a cleaner solution. And you thought it would be simple. I know I did. 😛

It’s not as polished or featured as other implementations, but it’s a good start. Here’s a screenshot after I type os. then hit Ctrl + Space:

Further reading

External tools plugin

Rolling your own gedit 3 plugin

Back to School with Cheesecake

Recently, I’ve been busy for a variety of reasons. The most significant reason? I’ve gone back to school! 😮

Shocking, I know. But it’s probably not what you expect. Since April 11th, I’ve been going to culinary school in downtown Vancouver. It’s an 11 block program (each block consists of 4 weeks, so… 44 weeks) – I should be done in February 2012. In addition, I just completed a 4-month stage (unpaid internship) at a hotel. I would go there after class (or before class, depending on the class) about 3 times a week. Lots of getting up early, and some getting home late. It can be tough work. I’ve been physically exhausted. Heck, I’m exhausted, right now. And you know what? I love it. There is something really satisfying about cooking. You produce something you can be proud of in relatively short period of time… and the kitchen can be an exhilarating place to be. I’ve always liked a bit of adrenaline in my life and I guess I didn’t find that in an office environment.

Anyway, I haven’t been taking photos at school. And I should be. So today I took some photos. I’m currently in block 7: baking and pastry. Here are some cheesecakes I decorated today. My partner and I had finished pretty much everything, so we had lots of time to play around. 🙂

whipped cream rosettes with orange segments and strawberry. Probably should have left the orange bits off the rosettes, but oh well.


rosettes on top of kiwi slices, with strawberry on top of orange segments.


The cheescakes turned out really well. No burning, no cracks (thanks to a sour cream topping), and quite level and smooth. Clean!


Spent way too much time on this, but had a lot of fun doing it. Marzipan pumpkin and chocolate "Booo" leaning on rosettes, Messy spider is 2 strawberries covered in chocolate with marzipan eyes and chocolate disc legs. All on chocolate webbing.

The cheesecakes go for $18. It’s a good deal considering how much cream cheese is in them… each about 750g? But whoever gets that halloween one will have a bunch of chocolate shrapnel when they attempt to cut it. Hah! 🙂