{"id":2519,"date":"2019-08-05T12:53:00","date_gmt":"2019-08-05T19:53:00","guid":{"rendered":"https:\/\/www.stevenbrown.ca\/blog\/?p=2519"},"modified":"2019-08-05T13:33:16","modified_gmt":"2019-08-05T20:33:16","slug":"refactoring-an-emacs-theme","status":"publish","type":"post","link":"https:\/\/www.stevenbrown.ca\/blog\/archives\/2519","title":{"rendered":"Refactoring an Emacs theme"},"content":{"rendered":"<p> I&#8217;m gradually working on a custom theme for emacs, and I wanted to make it easier to update and read.  (It&#8217;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 &#x2026; &#8220;clean.&#8221;  It&#8217;s basically a command that passes in a huge list of lists (of lists&#x2026; this is <i>lisp<\/i>, after all).  That command is <code>custom-theme-set-faces<\/code>. <\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-emacs-lisp\">(custom-theme-set-faces\n 'my-cool-theme                         <span style=\"color: #8D8D84;\">;<\/span><span style=\"color: #998899; font-style: italic;\">theme name<\/span>\n '(\n   (FACE SPEC)\n   (FACE SPEC)\n   ))\n<\/pre>\n<\/div>\n<p> <code>FACE<\/code> is the symbol for a particular element, for example <code>default<\/code> for default text or <code>error<\/code> which is used for error messages.  Easy.  <code>SPEC<\/code>, however is a bit more complicated.  <code>SPEC<\/code> is a list of <code>DISPLAY<\/code> <code>ATTS<\/code> pairs, each list of attributes pertaining to its paired display.  If this were CSS, I think it would be similar to including the <code>@media<\/code> selector for each rule. <\/p>\n<p> (<code>DISPLAY<\/code> can simply be <code>default<\/code> to apply to all displays, but I haven&#8217;t seen this used much and am trying to follow suit.) <\/p>\n<p> So now we have this. <\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-emacs-lisp\">(custom-theme-set-faces\n 'my-cool-theme                         <span style=\"color: #8D8D84;\">;<\/span><span style=\"color: #998899; font-style: italic;\">theme name<\/span>\n '(\n   (FACE ((DISPLAY ATTS) (DISPLAY ATTS)))\n   (FACE ((DISPLAY ATTS) (DISPLAY ATTS)))\n   ))\n<\/pre>\n<\/div>\n<p> <code>ATTS<\/code> is property list, where you can specify the width, height, etc. for the font face, on that display. <\/p>\n<p> So plugging in some actual values, we have the following: <\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-emacs-lisp\">(custom-theme-set-faces\n 'my-cool-theme                         <span style=\"color: #8D8D84;\">;<\/span><span style=\"color: #998899; font-style: italic;\">theme name<\/span>\n '(\n   <span style=\"color: #8D8D84;\">;; <\/span><span style=\"color: #998899; font-style: italic;\">error face, using default DISPLAY (simpler)<\/span>\n   (<span style=\"color: #ff0000; font-weight: bold;\">error<\/span> ( (default (<span style=\"color: #00bfff;\">:foreground<\/span> <span style=\"color: #54ff9f;\">\"red\"<\/span> <span style=\"color: #00bfff;\">:background<\/span> <span style=\"color: #54ff9f;\">\"black\"<\/span>)) ) )\n\n   <span style=\"color: #8D8D84;\">;; <\/span><span style=\"color: #998899; font-style: italic;\">default face using DISPLAY specified by a a list of conditions (less simpler)<\/span>\n   <span style=\"color: #8D8D84;\">;; <\/span><span style=\"color: #998899; font-style: italic;\">In this case, a color terminal that supports at least 89 colors.<\/span>\n   (default ( ( ((class color) (min-colors 89)) (<span style=\"color: #00bfff;\">:foreground<\/span> <span style=\"color: #54ff9f;\">\"white\"<\/span> <span style=\"color: #00bfff;\">:background<\/span> <span style=\"color: #54ff9f;\">\"black\"<\/span>)) ) )\n   ))\n<\/pre>\n<\/div>\n<p> 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. <\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-emacs-lisp\">(<span style=\"color: #00bfff;\">let<\/span>\n    ((class '((class color) (min-colors 89))))\n  <span style=\"color: #8D8D84;\">;; <\/span><span style=\"color: #998899; font-style: italic;\">...<\/span>\n  `(default ((,class (<span style=\"color: #00bfff;\">:foreground<\/span> <span style=\"color: #54ff9f;\">\"white\"<\/span> <span style=\"color: #00bfff;\">:background<\/span> <span style=\"color: #54ff9f;\">\"black\"<\/span>))))\n  <span style=\"color: #8D8D84;\">;; <\/span><span style=\"color: #998899; font-style: italic;\">...<\/span>\n)\n<\/pre>\n<\/div>\n<p> I wanted to remove the <code>DISPLAY<\/code> component completely and just add it later using code.  This would mean a cleaner and easier to maintain theme.  The excellent <a href=\"https:\/\/github.com\/hlissner\/emacs-doom-themes\">emacs-doom-themes<\/a> has a very <a href=\"https:\/\/github.com\/hlissner\/emacs-doom-themes\/blob\/master\/doom-themes.el#L205\">elegant solution<\/a>, but I didn&#8217;t quite understand all the elisp and thought I could roll a simpler version myself as a good learning experience. <\/p>\n<p> Lots of fumbling around elisp ensued, but I now have something I&#8217;m happy with. <\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-emacs-lisp\"><span style=\"color: #8D8D84;\">;; <\/span><span style=\"color: #998899; font-style: italic;\">the simple-faces are bound to a list `<\/span><span style=\"color: #ff7f00; font-style: italic;\">two-fifteen-simple-faces<\/span><span style=\"color: #998899; font-style: italic;\">'<\/span>\n(default (<span style=\"color: #00bfff;\">:background<\/span> ,bg1 <span style=\"color: #00bfff;\">:foreground<\/span> ,fg1))\n(<span style=\"color: #ff0000; font-weight: bold;\">error<\/span>   (<span style=\"color: #00bfff;\">:foreground<\/span> <span style=\"color: #54ff9f;\">\"red\"<\/span> <span style=\"color: #00bfff;\">:background<\/span> <span style=\"color: #54ff9f;\">\"black\"<\/span>))\n<span style=\"color: #8D8D84;\">;; <\/span><span style=\"color: #998899; font-style: italic;\">...<\/span>\n\n<span style=\"color: #8D8D84;\">;; <\/span><span style=\"color: #998899; font-style: italic;\">Add the display to the simple-faces<\/span>\n(<span style=\"color: #00bfff;\">while<\/span> two-fifteen-simple-faces\n  (<span style=\"color: #00bfff;\">let*<\/span>\n      ((simple-face (car two-fifteen-simple-faces))\n       (fname (car simple-face))\n       (fattrs (cadr simple-face)))\n    (<span style=\"color: #00bfff;\">setq<\/span> two-fifteen-simple-faces (cdr two-fifteen-simple-faces))\n    (<span style=\"color: #00bfff;\">setq<\/span> built-faces (append\n                       built-faces\n                       `((,fname ((,class ,fattrs))))))))\n\n<span style=\"color: #8D8D84;\">;; <\/span><span style=\"color: #998899; font-style: italic;\">apply custom-theme-set-faces to built-faces<\/span>\n(apply #'custom-theme-set-faces 'two-fifteen built-faces)\n<\/pre>\n<\/div>\n<p> Next up is making a palette with re-usable colours, which I&#8217;ve started w\/the <code>bg1<\/code> and <code>fg1<\/code>. <\/p>\n<p> Here are some functions and modes I&#8217;m finding extremely helpful when working on the theme: <\/p>\n<ul class=\"org-ul\">\n<li><code>describe-face<\/code> : Describe the face at the point (cursor)<\/li>\n<li><code>list-faces-display<\/code> : List faces in use on display<\/li>\n<li><code>rainbow-mode<\/code> : Display colour values using the actual colour<\/li>\n<li><code>list-colors-display<\/code> : visually browse colours names and codes<\/li>\n<li><code>align-regexp<\/code> : Align attribute blocks of the theme to make it easier to read<\/li>\n<li><code>sort-lines<\/code> : Sort sections alphabetically<\/li>\n<li><code>Iedit<\/code> and <code>narrow-to-region<\/code> : Refactoring<\/li>\n<li>Keyboard macros, <i>OF COURSE<\/i>. :  start ( <code>C-x (<\/code> ), stop ( <code>C-x )<\/code> ), run ( <code>C-x e<\/code> )<\/li>\n<\/ul>\n<p> 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&#8217;m talking about!  \ud83d\ude42 <\/p>\n<p> Oh right, here&#8217;s a picture of what the theme currently looks like.  It&#8217;s called <i>two-fifteen<\/i>. <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.stevenbrown.ca\/blog\/wordpress\/..\/files\/2019\/08\/2019-08-05_emacs_two_fifteen_theme.png?w=750&#038;ssl=1\" alt=\"2019-08-05_emacs_two_fifteen_theme.png\" \/> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m gradually working on a custom theme for emacs, and I wanted to make it easier to update and read. (It&#8217;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 &#x2026; &#8220;clean.&#8221; It&#8217;s basically a command that passes&hellip; <a class=\"more-link\" href=\"https:\/\/www.stevenbrown.ca\/blog\/archives\/2519\">Continue reading <span class=\"screen-reader-text\">Refactoring an Emacs theme<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[254,250],"class_list":["post-2519","post","type-post","status-publish","format-standard","hentry","category-general","tag-elisp","tag-emacs","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4jEMb-ED","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/posts\/2519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/comments?post=2519"}],"version-history":[{"count":5,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/posts\/2519\/revisions"}],"predecessor-version":[{"id":2525,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/posts\/2519\/revisions\/2525"}],"wp:attachment":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/media?parent=2519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/categories?post=2519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/tags?post=2519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}