emacs/var/elfeed/db/data/5b/5b2d55cc264ba6950e407d3bb27b17303122d7bc
2022-01-03 12:49:32 -06:00

47 lines
6.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>The <a href="https://developer.apple.com/design/human-interface-guidelines/macos/visual-design/color/">Apples Human Interface Guidelines</a> have a list of named system colors for macOS. These color names can be used for Emacs “faces”, i.e. color-and-font settings of text.</p>
<figure><a href="https://christiantietze.de/posts/2021/11/lin.el-macos-system-colors/20211109104817_lin-vs-hl-line-mode.png"><img alt="" src="https://christiantietze.de/posts/2021/11/lin.el-macos-system-colors/20211109104817_lin-vs-hl-line-mode.png" /></a>LIN to the left in a (squished) email list, hl-mode to the right in regular text</figure>
<p>Recently, this came in handy when Protesilaos Stavrou published <a href="https://gitlab.com/protesilaos/lin/">his LIN package source code</a> its basically an extension of the built-in <code>hl-mode</code>, read: highlight-line-mode. The default highlights the current line. Thats kind of useful to find your insertion point more quickly. But its also used in selection interfaces to highlight the currently selected line: email modes like <code>message-mode</code>, <code>notmuch</code>, <code>mu4e</code>, and feed reader <code>elfeed</code> use this to show that the actions you can perform pertains to the currently focused or highlighted line. Thats where Prots LIN package comes into play: it helps distinguish between highlighting the line in text editing modes and highlighting the line in selection interfaces, so you can use different colors.</p>
<p>You could say that <a href="https://gitlab.com/protesilaos/lin/">LIN</a> adds a semantic layer on top of “highlight this line”: it can distinguish between “highlight this line of text” and “highlight the selection”.</p>
<p>Being on macOS, I wanted the system default selection color. Blue background with white foreground color.</p>
<p>I used this approach for <code>hl-line-mode</code> since May and <a href="https://christiantietze.de/posts/2021/05/macos-native-highlight-colors/">wrote about this</a> back then. I couldnt use <code>hl-line</code> to highlight my line in text documents if I wanted to, though, because of the color choice. This new approach improves that thanks to <a href="https://gitlab.com/protesilaos/lin/">LIN</a>.</p>
<p>To use macOSs named colors for selections as LINs background and foreground instead of specifying color values directly, tweak the two LIN faces:</p>
<div class="language-elisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">memq</span> <span class="nv">window-system</span> <span class="o">'</span><span class="p">(</span><span class="nv">mac</span> <span class="nv">ns</span><span class="p">))</span>
<span class="p">(</span><span class="nv">set-face-attribute</span> <span class="ss">'lin-hl</span> <span class="no">nil</span>
<span class="ss">:background</span> <span class="s">"selectedContentBackgroundColor"</span><span class="p">)</span>
<span class="c1">;; To also override the foreground (see `lin-override-foreground'):</span>
<span class="p">(</span><span class="nv">set-face-attribute</span> <span class="ss">'lin-hl-override-fg</span> <span class="no">nil</span>
<span class="ss">:foreground</span> <span class="s">"alternateSelectedControlTextColor"</span>
<span class="ss">:background</span> <span class="s">"selectedContentBackgroundColor"</span><span class="p">))</span>
</code></pre></div></div>
<p>The cool part about named system colors on macOS is that they are “Dynamic System Colors”: in dark mode, they produce a different value than in light mode. That means you dont need to pick dark and light colors individually. The same color names will work.</p>
<p>If you switch your OS or apps appearance to dark mode, though, you need to effectively reload the colors. Unlike native macOS apps, Emacs wont automatically use the correct color for the current appearance.</p>
<p>So you need to trigger an update of LINs faces to pick up the color values if you change the appearance from dark to light, or light to dark. macOS builds of Emacs have a hook for this, and you can perform a color update by adding a function to <code>ns-system-appearance-change-functions</code>:</p>
<div class="language-elisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">my-lin-macos-system-colors</span> <span class="p">()</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">memq</span> <span class="nv">window-system</span> <span class="o">'</span><span class="p">(</span><span class="nv">mac</span> <span class="nv">ns</span><span class="p">))</span>
<span class="p">(</span><span class="nv">set-face-attribute</span> <span class="ss">'lin-hl</span> <span class="no">nil</span>
<span class="ss">:background</span> <span class="s">"selectedContentBackgroundColor"</span><span class="p">)</span>
<span class="c1">;; To also override the foreground (see `lin-override-foreground'):</span>
<span class="p">(</span><span class="nv">set-face-attribute</span> <span class="ss">'lin-hl-override-fg</span> <span class="no">nil</span>
<span class="ss">:foreground</span> <span class="s">"alternateSelectedControlTextColor"</span>
<span class="ss">:background</span> <span class="s">"selectedContentBackgroundColor"</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">memq</span> <span class="nv">window-system</span> <span class="o">'</span><span class="p">(</span><span class="nv">mac</span> <span class="nv">ns</span><span class="p">))</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'ns-system-appearance-change-functions</span> <span class="nf">#'</span><span class="nv">my-lin-macos-system-colors</span><span class="p">))</span>
</code></pre></div></div>
<p>This information is now also part of <a href="https://gitlab.com/protesilaos/lin/">LINs README</a> and, if Prots past efforts to document his packages holds true, will also become part of the manual when you install this package.</p>
<hr /><p>Receive Christians <a href="https://christiantietze.de/newsletter/">new posts via email</a></p>