<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title><![CDATA[Blog Zero]]></title>
<description><![CDATA[Blog Zero]]></description>
<link>https://blog0.steelcandy.org/</link>
<lastBuildDate>Tue, 15 Mar 2022 09:20:33 -0400</lastBuildDate>
<item>
  <title><![CDATA[Paging]]></title>
  <description><![CDATA[
<p>
<b>Note:</b> there is a <a href="https://github.com/steelcandy2/empager">public repository</a> containing the source code and formal
documentation for the programs that this post is about.
</p>

<p>
I used the venerable <a href="http://www.greenwoodsoftware.com/less/"><code>less</code></a> as my pager for a long time, but I wanted
something more Emacs-like. And what could be more Emacs-like than Emacs?
Fortunately I found <a href="https://github.com/kaushalmodi/">Kaushal Modi</a>'s excellent <a href="https://github.com/kaushalmodi/eless.git"><code>eless</code></a> shell script, which runs
an instance of Emacs that's been adapted to make it work like a pager.
</p>

<p>
But that was almost 5 years ago, and while much of the core functionality
from <code>eless</code> remains in my version — which I've not very imaginatively named
<a href="https://github.com/steelcandy2/empager"><code>empager</code></a> — I've made a lot of changes to it, both to add functionality and
so that it works as much as possible like Emacs does when I use it as an
editor. It doesn't work exactly the same because I want it to function in as
many environments and situations as possible (such as on newly-installed
systems), so <code>empager</code>
</p>

<ul class="org-ul">
<li>doesn't use any external packages: it only uses ones that are bundled
with Emacs,</li>
<li>doesn't assume that internet access is available, and</li>
<li>has a minimum of external dependencies: as much as possible is contained
in the script itself.</li>
</ul>

<p>
The added functionality includes:
</p>

<ul class="org-ul">
<li>saving search strings and register contents across invocations (which can
be useful when reopening the same file after closing it prematurely, or
when looking for the same pattern in different files)</li>
<li>allowing the line number at which it is to be opened to be specified for
each file (using a syntax similar to the one Emacs itself uses)</li>
<li>allowing the version of each file to open to be specified, provided it's
under git or <a href="2021-11-10-software.html">CVS</a> version control</li>
<li>supporting as many of the <code>less</code> program's command line options as could
be reasonably easily implemented (or, in a few cases, ignored), at least
when they don't conflict with the Emacs-like options that <code>empager</code>
accepts
<ul class="org-ul">
<li>for example, if the <code>-F</code> option is specified and only one file (or
source) is being paged then its contents will be written directly to
the screen without starting Emacs if (and only if) those contents will
all fit on a single screen</li>
</ul></li>
<li>easy saving of positions: each shifted positive digit (e.g. '!' for '1')
saves point (that is, the current cursor position) in the register whose
name is that digit, and the digit itself jumps to the position saved in
that register</li>
<li>opening the current file in your main Emacs instance (using the <code>e</code>
command), with point on the same line
<ul class="org-ul">
<li>since files in a pager can't usually be edited, many single characters
(like <code>e</code>) are repurposed to run commands</li>
</ul></li>
</ul>

<p>
If you end up trying out <code>empager</code> you may notice that there are some
"idiosyncratic" keybindings. They almost always correspond to custom
keybindings that I use in my main Emacs editor, and may not be to everyone's
tastes. In which case you can edit your copy of the script to create a custom
Emacs-based pager of your very own.
</p>

<div id="outline-container-org458f3cb" class="outline-2">
<h2 id="org458f3cb">But Wait: There's More!</h2>
<div class="outline-text-2" id="text-org458f3cb">
<p>
I use <code>empager</code> all the time, so I've given it the much shorter one-letter
name <code>p</code>. I could have defined <code>p</code> as an alias, but sometimes I find that I
want to pass it as an argument to a command like <a href="https://www.gnu.org/software/findutils/"><code>xargs</code></a>. For example, this
</p>

<div class="org-src-container">
<pre class="src src-sh">find . -name <span class="org-string">'*.txt'</span> | xargs p
</pre>
</div>

<p>
(or just <code>sde txt -- p</code> using <a href="2022-01-26-searchdown-find-and-grep-redux.html"><code>searchdown</code></a>) will fail if <code>p</code> is an alias. I
could have also created a symbolic link to <code>empager</code> named <code>p</code>, but the tiny
<code>p</code> script included in the repository is a little more flexible, in that it
runs whichever pager program is specified by the <code>PAGER</code> environment
variable.
</p>

<p>
And speaking of environment variables, <code>empager</code> also takes options from the
<code>EMPAGER</code> environment variable just like <code>less</code> takes them from the <code>LESS</code>
environment variable. (I set it to <code>-FR</code>.) You can also have the <code>man</code>
command use <code>empager</code> as its man page viewer/pager by setting and exporting
the <code>MANPAGER</code> environment variable to <code>empager</code> like so:
</p>

<div class="org-src-container">
<pre class="src src-sh"><span class="org-variable-name">MANPAGER</span>=empager
<span class="org-builtin">export</span> MANPAGER
</pre>
</div>

<p>
However, in a few circumstances <code>empager</code> will display some unsightly raw
escape codes. The only place that this happens consistently for me is in the
help displayed when using the <code>help()</code> function in an interactive Python
shell, so I start interactive Python 2 and 3 sessions using the following
aliases:
</p>

<div class="org-src-container">
<pre class="src src-sh"><span class="org-builtin">alias</span> <span class="org-variable-name">py2</span>=<span class="org-string">'PAGER=bempager python2'</span>
<span class="org-builtin">alias</span> <span class="org-variable-name">py3</span>=<span class="org-string">'MANPAGER=bempager python3'</span>
</pre>
</div>

<p>
where <code>bempager</code> is included in the repository and is a simple wrapper script
that removes any and all backspaces before passing data along to <code>empager</code>.
(Apparently Python 2 uses the <code>PAGER</code> environment variable while Python 3
uses the <code>MANPAGER</code> environment variable.) I also define the alias
</p>

<div class="org-src-container">
<pre class="src src-sh"><span class="org-builtin">alias</span> <span class="org-variable-name">lman</span>=<span class="org-string">'MANPAGER=less man'</span>
</pre>
</div>

<p>
so that if I do encounter a man page that doesn't play well with <code>empager</code>
(or <code>bempager</code>) then I can quickly switch to using <code>less</code> for that page.
</p>

<p>
One final pager-related tweak that I'll share here is a <a href="https://tiswww.cwru.edu/php/chet/readline/rltop.html"><code>readline</code></a> macro that
I've added to my <code>~/.inputrc</code> file:
</p>

<pre class="example" id="org6d4fb5e">
"\C-p": " 2&gt;&amp;1 | p"
</pre>

<p>
This allows me to very quickly redirect a command's regular <i>and</i> error
output to an instance of my default pager program by just typing <code>C-p</code>
(a.k.a. <code>Ctrl+p</code>) at the end of the command. (Emacs users that use <code>C-p</code>
"properly" will of course want to choose a different keybinding.) For
example, if I've typed the command
</p>

<div class="org-src-container">
<pre class="src src-sh">curl wttr.in/:help
</pre>
</div>

<p>
and then I type <code>C-p</code> the command becomes
</p>

<div class="org-src-container">
<pre class="src src-sh">curl wttr.in/:help 2&gt;&amp;1 | p
</pre>
</div>

<p>
and the site's help information is displayed in my default pager instead of
potentially scrolling off the screen. (Remember that you need to type <code>C-x
C-r</code> in the terminal to have any changes you've made to your <code>~/.inputrc</code>
file take effect there.)
</p>

<div class="ieven" id="org57bbb17">
<p>
If you'd like to support this site then check out my web app <a href="https://ineverynook.com/">InEveryNook.com</a>
and see if it's of interest to you, pass it along to anyone you think might
be interested in it, or both. Thanks!
</p>

</div>
</div>
</div>
<div class="taglist"><a href="https://blog0.steelcandy.org/tags.html">Tags</a>: <a href="https://blog0.steelcandy.org/tag-emacs.html">emacs</a> <a href="https://blog0.steelcandy.org/tag-shell.html">shell</a> <a href="https://blog0.steelcandy.org/tag-software.html">software</a> </div>]]></description>
  <category><![CDATA[emacs]]></category>
  <category><![CDATA[shell]]></category>
  <category><![CDATA[software]]></category>
  <link>https://blog0.steelcandy.org/2022-03-14-paging.html</link>
  <pubDate>Mon, 14 Mar 2022 22:52:00 -0400</pubDate>
</item>
<item>
  <title><![CDATA[Relocation: Additional]]></title>
  <description><![CDATA[
<p>
In an <a href="2022-02-10-relocation-basics.html">earlier post</a> I covered the basics of using the main commands provided
by my <a href="https://github.com/steelcandy2/relocate">relocate</a> package to navigate the filesystem more quickly and
efficiently. Now we're going to discuss some additional features of those
commands, as well as some auxiliary commands that are provided by the
package. Then I'll introduce a few shell aliases and functions that are built
on top of the package's commands, and that I find make moving around the
filesystem even easier.
</p>

<div id="outline-container-orgb2ee8e5" class="outline-2">
<h2 id="orgb2ee8e5">Auxiliary Commands and Environment Variables</h2>
<div class="outline-text-2" id="text-orgb2ee8e5">
<p>
One of the problems with relocation aliases is remembering which one aliases
a given directory, especially when you only recently defined the alias or you
only use it infrequently. The <code>rg</code> command addresses that problem by
displaying all of the relocation aliases, along with the directories they
alias, where one or both match a specified regular expression. For example,
if you can't remember the relocation alias that you defined for the
<code>/var/log</code> directory then you can just run
</p>

<div class="org-src-container">
<pre class="src src-bash">rg log
</pre>
</div>

<p>
to display all of the relocation aliases that alias directories containing
<code>log</code> (or that themselves contain <code>log</code>, or both). It's basically a
convenient way to <code>grep</code> your <code>~/.relocations</code> file.
</p>

<p>
The other auxiliary commands depend to a greater or lesser extent on a
feature of the relocation commands that we haven't mentioned yet: in addition
to switching directories they can also set some environment variables to the
pathnames of directories involved in their operation. For instance, when the
<code>r</code> command is used to switch to a different directory then by default it
also
</p>

<ul class="org-ul">
<li>sets the environment variable named <code>r</code> to the pathname of the directory
that it just switched to, and</li>
<li>sets the environment variable named <code>rr</code> to the pathname of the directory
that was the current directory just before it switched to the new one.</li>
</ul>

<p>
(See the comment at the top of the <code>relocation-functions.sh</code> file in the
<a href="https://github.com/steelcandy2/relocate">repository</a> for information about how to prevent the various relocation
commands from setting any environment variables.) The environment variables
can then be used in commands as shorthands for those directories. As an
example, suppose you're in a deeply-nested directory and you want to copy
there all of the text files in the <code>latest</code> subdirectory of your downloads
directory. You could do it by switching to the <code>latest</code> subdirectory using
the <code>r</code> command (something like <code>r dl lat</code> if we assume that you've already
defined <code>dl</code> to be a relocation alias for your downloads directory) and then
running a command like
</p>

<div class="org-src-container">
<pre class="src src-bash">cp *.txt $rr
</pre>
</div>

<p>
there. (You could then run the <code>r</code> command with no argument to return to the
original deeply-nested directory.) If you had wanted to copy the files to the
parent directory of the deeply-nested directory instead then you could
replace the above <code>cp</code> command with
</p>

<div class="org-src-container">
<pre class="src src-bash">cp *.txt $rr/..
</pre>
</div>

<p>
(Note that you'd want to write the <code>$rr</code> and <code>$rr/..</code> parts of those commands
as <code>"$rr"</code> and <code>"$rr/.."</code>, respectively if the value of the <code>rr</code> environment
variable could contain spaces or other whitespace characters.)
</p>

<p>
Since the values of the <code>r</code> and <code>rr</code> environment variables can change every
time you (directly or indirectly: as part of a shell alias or function, for
example) execute the <code>r</code> command you might not always be completely confident
of their current values. When that happens you can use the <code>re</code> auxiliary
command to display the names and values of all of the environment variables
that can be set by a relocation command, and that are defined in the current
shell. It doesn't take any arguments: it always displays all of the relevant
environment variables. Its output might look something like
</p>

<pre class="example" id="org9a1b318">
r  /home/jgm/Downloads
rr /home/jgm
</pre>

<p>
if you had been in your home directory and then executed the command <code>r dl</code>.
</p>

<p>
There's another auxiliary command named <code>rp</code> that just outputs to standard
output the pathname of the directory that the <code>r</code> command would actually
switch to if it were given the same arguments. Which can be helpful for doing
a "trial run" of an actual <code>r</code> command, but otherwise isn't especially
useful. However it also sets some environment variables of its own, which can
be very handy indeed. For example, if we revisit the scenario above where we
wanted to copy text files from the <code>latest</code> subdirectory of your downloads
directory to the current, deeply-nested directory we could use the fact that
the <code>rp</code> command sets the <code>rp</code> environment variable to the pathname of the
directory that it outputs. Then we could copy the files using these commands
instead:
</p>

<div class="org-src-container">
<pre class="src src-bash">rp dl lat
cp $rp/*.txt .
</pre>
</div>

<p>
(or <code>cp "$rp/*.txt" .</code> if you're being careful). Doing it this way has a
couple of small advantages:
</p>

<ul class="org-ul">
<li>you don't have to change the current directory at all, and</li>
<li>it's easier to recover if <code>dl lat</code> doesn't match the directory that you
thought it would: for example, if your downloads directory turned out to
also have a <code>latefees</code> subdirectory then here we could just run the
command <code>rp dl lates</code> without having to first change back to the original
directory (which admittedly would only require running the command <code>r</code>).</li>
</ul>

<p>
I think the main reason why I find myself using the <code>rp</code> command and
environment variable instead of the <code>r</code> and/or <code>rr</code> variables is that I
rarely use the <code>rp</code> command for any other reason, and so I can be much more
confident that its value will be the one that I expect (without having to use
the <code>re</code> command to check).
</p>

<p>
The <code>rp</code> command can also set the <code>r1</code> and <code>r2</code> environment variables to hold
the output of earlier uses of it: the output of the second- and third-most
recent <code>rp</code> commands, if any, will be stored in <code>r1</code> and <code>r2</code>, respectively.
Note that none, some or all of <code>rp</code>, <code>r1</code> and <code>r2</code> can have the same value:
they're set regardless of whether their values will be the same as one of the
others (which makes their values more predictable).
</p>

<p>
There are a few other auxiliary commands, but I don't find myself using them
much, if at all:
</p>

<ul class="org-ul">
<li><code>lrp</code> and <code>lsrp</code> output long and short listings, respectively, of the
contents of the directory whose pathname was output by the last <code>rp</code>
command that was executed (so basically <code>ls -l "$rp"</code> and <code>ls "$rp"</code>,
respectively),</li>
<li><code>lrr</code> and <code>lsrr</code> are similar, except that they operate on the directory
that was the current one before the latest <code>r</code> command changed it (and so
are similar to executing <code>ls -l "$rr"</code> and <code>ls "$rr"</code>, respectively), and</li>
<li><code>rr</code> is just an alias for <code>r .</code>, which (if you remember from the <a href="2022-02-10-relocation-basics.html">basics</a>
post) means it's sometimes a slightly faster way to switch to a directory
in or under the current one.</li>
</ul>

<p>
As examples of that last command being a little faster, suppose that the
current directory only has one subdirectory starting with <code>ch</code>: then even
with tab completion typing the command <code>r ch</code> is a little quicker than typing
<code>cd ch&lt;TAB&gt;</code>. And if you instead wanted to switch to the sole subdirectory of
that subdirectory that starts with <code>gr</code> then typing <code>r ch gr</code> is quicker
still than typing <code>cd ch&lt;TAB&gt;gr&lt;TAB&gt;</code>.
</p>

<p>
Possibly because the savings in keystrokes was so slight, I ended up almost
never using the <code>rr</code> command, at least until I (effectively) made <code>0</code> an
alias for it (see below). Despite it only being a single character shorter
(and despite <code>rr</code>'s second character being the same as its first) I use <code>0</code>
constantly, to the point where it's all but completely replaced my use of the
<code>cd</code> command in such situations.
</p>
</div>
</div>

<div id="outline-container-org08f0b15" class="outline-2">
<h2 id="org08f0b15">Additional Commands</h2>
<div class="outline-text-2" id="text-org08f0b15">
<p>
To make switching directories even easier you can define shell aliases or
functions that can be used to switch to specific directories, or directories
under those directories. For example, if you did a lot of work in the
<code>/usr/local</code> directory and its subdirectories (and their subdirectories &#x2026;)
then you could define a relocation alias <code>ul</code> for it, and also a shell alias
like
</p>

<div class="org-src-container">
<pre class="src src-bash">alias ul='r ul'
</pre>
</div>

<p>
that would let you switch to the directory <code>/usr/local</code> just by executing the
command <code>ul</code>. In addition, the command <code>ul l</code> would presumably relocate you
to <code>/usr/local/lib</code> and <code>ul sh m .1</code> might very well switch you to the
directory <code>/usr/local/share/man/man1</code> (depending on what other directories
exist under <code>/usr/local/</code>).
</p>

<p>
While such aliases can be of use to a select group of people, I've come up
with some that I think could be useful to anyone. They're not part of the
<a href="https://github.com/steelcandy2/relocate">relocate package</a> itself, but you can define them by adding the following to
the file where you define shell aliases (like <code>~/.bashrc</code>, or possibly a
separate file like <code>~/.aliases</code>):
</p>

<div class="org-src-container">
<pre class="src src-bash">alias 1='r 1'
alias 2='r 2'
alias 3='r 3'
alias 4='r 4'
alias 5='r 5'
alias 6='r 6'
alias 7='r 7'
alias 8='r 8'
alias 9='r 9'
alias 10='r 10'
</pre>
</div>

<p>
(You can omit some of the later aliases, but directory hierarchies can get to
be deeper than you'd think.) These aliases assume that your <code>~/.relocations</code>
file contains the <code>1</code>, <code>2</code>, &#x2026;, <code>10</code> relocation aliases that are defined in
the <a href="https://github.com/steelcandy2/relocate/blob/main/.relocations.example"><code>.relocations.example</code></a> file in the repository: if it does then the above
shell aliases can be used to quickly move "up and over" to sibling and
similar directories. For example, if you're in the <code>src</code> subdirectory of a
project's directory and you want to relocate to that project's <code>docs</code>
subdirectory then instead of running the command
</p>

<div class="org-src-container">
<pre class="src src-bash">cd ../docs
</pre>
</div>

<p>
you can just run
</p>

<div class="org-src-container">
<pre class="src src-bash">1 d
</pre>
</div>

<p>
(assuming that <code>docs</code> is the only (or at least the first) subdirectory of the
project directory that starts with <code>d</code>). Which is only a little shorter, but
if instead you had started somewhere deeper under the <code>src</code> subdirectory like
<code>src/static/css</code> then instead of having to enter the command
</p>

<div class="org-src-container">
<pre class="src src-bash">cd ../../../docs
</pre>
</div>

<p>
you could just run the much more concise
</p>

<div class="org-src-container">
<pre class="src src-bash">3 d
</pre>
</div>

<p>
Similarly, one could move from the directory <code>/usr/local/src/emacs-26.3</code> to
<code>/usr/include/X11</code> by running <code>3 in X</code> instead of <code>cd ../../../include/X11</code>
(which is a significant improvement even allowing for the use of tab/filename
completion on the last two components of the destination directory's
pathname).
</p>

<p>
One also shouldn't underestimate the convenience of running one-character
commands like <code>1</code>, <code>2</code> and <code>3</code> in place of <code>cd ..</code>, <code>cd ../..</code> and
<code>cd ../../..</code>, respectively. Especially if it turns out that you run them
anywhere near as often as I seem to.
</p>

<p>
Attentive readers may have noticed that I haven't defined the <code>0</code> alias that
I mentioned above, though they may now have a pretty good idea of why it's
named that. Just as
</p>

<ul class="org-ul">
<li>the <code>1</code> alias goes up one level to the parent directory,</li>
<li>the <code>2</code> alias goes up two levels to the "grandparent" directory,</li>
<li>and so on</li>
</ul>

<p>
(possibly before going down again), the <code>0</code> alias goes "up" zero levels to
the current directory before going down again. Which is just another way of
saying that it switches to a directory under the current directory. (Clearly
running the <code>0</code> command with no arguments isn't very useful.)
</p>

<p>
Thus — like in the <code>rr</code> command examples above — if you ran the command
<code>0 ch</code> then the (first) subdirectory of the current directory that starts
with <code>ch</code> would become the current directory, and if instead you ran the
command <code>0 ch gr</code> then you'd be relocated to the (first) subdirectory of that
subdirectory that starts with <code>gr</code>.
</p>

<p>
While you could simply define <code>0</code> as a shell alias using a definition like
</p>

<div class="org-src-container">
<pre class="src src-bash">alias 0='r .'
</pre>
</div>

<p>
for somewhat obscure reasons you might actually want to define it as a shell
function like this:
</p>

<div class="org-src-container">
<pre class="src src-bash">function 0() {
    r . "${@%/}"
}
</pre>
</div>

<p>
which will remove any trailing slashes from the ends of its arguments, under
the assumption that they were added by tab/filename completion. Unlike with
the <code>1</code>, <code>2</code>, etc. commands/aliases, if you inadvertently (or otherwise) use
tab completion on an argument to <code>0</code> then at least the first argument will
always be a valid one, except that the trailing slash (<code>/</code>) that can (will?)
be added by bash's default tab completion will cause the underlying <code>r</code>
command to fail. But the above shell function prevents such failures,
allowing such uses of the <code>0</code> command to succeed after all. Whew!
</p>

<p>
A lot of the improvements suggested here and above can seem like
insignificant savings of a few keystrokes, but in addition to their making
frequently run commands quicker and easier to type, thus adding up to bigger
savings over time, they also reduce the friction involved in switching to
another directory to start another task. And getting started on a task can
often end up being one of the more difficult parts of completing it.
</p>

<div class="ieven" id="org1e4438f">
<p>
If you'd like to support this site then check out my web app <a href="https://ineverynook.com/">InEveryNook.com</a>
and see if it's of interest to you, pass it along to anyone you think might
be interested in it, or both. Thanks!
</p>

</div>
</div>
</div>
<div class="taglist"><a href="https://blog0.steelcandy.org/tags.html">Tags</a>: <a href="https://blog0.steelcandy.org/tag-shell.html">shell</a> <a href="https://blog0.steelcandy.org/tag-software.html">software</a> </div>]]></description>
  <category><![CDATA[shell]]></category>
  <category><![CDATA[software]]></category>
  <link>https://blog0.steelcandy.org/2022-02-16-relocation-additional.html</link>
  <pubDate>Wed, 16 Feb 2022 22:59:00 -0500</pubDate>
</item>
<item>
  <title><![CDATA[Quickly Tangling]]></title>
  <description><![CDATA[
<p>
<b>Note:</b> there is a <a href="https://github.com/steelcandy2/emacs-tools">public repository</a> that includes the source code for the
program that this post is about.
</p>

<p>
This is a brief post about a short <code>sed</code> script I wrote called
<code>org-quick-tangle</code> that can <b>very</b> quickly <a href="https://www.gnu.org/software/emacs/manual/html_node/org/Extracting-Source-Code.html">tangle</a> an <a href="https://www.gnu.org/software/emacs/manual/html_node/org/Summary.html">org mode</a> file, but that
works properly only on certain files. Specifically, it
</p>

<ul class="org-ul">
<li>only respects <code>:tangle</code> header arguments on source blocks (that is, on
<code>#+begin_src</code> lines) and completely ignores any others, such as those
specified in properties drawers,</li>
<li>writes all of the code blocks (that it doesn't omit) to its standard
output, ignoring any destination file that may have been specified for
them, and</li>
<li>does some special handling of anything that looks like a Lisp comment
line (though it's very unlikely that this handling will have any effect
on tangled source code that's written in non-Lisp languages).</li>
</ul>

<p>
While it should work to tangle any source code within the limitations
outlined above, the primary reason I created it — and the only thing I use it
for currently — is to generate an Emacs Lisp file from my literate Emacs
configuration.
</p>

<p>
How fast is it? On my not overly powerful desktop machine it tangles my 1.4MB
kitchen-sink org mode configuration file into the corresponding 627KB Emacs
Lisp file in about 40 milliseconds, which is all but instantaneous. In
comparison, on the same machine the script that uses <code>org-babel-tangle-file</code>
to do the tangling takes about 2 minutes. However, the slower script's
results do include <a href="https://www.gnu.org/software/emacs/manual/html_node/org/Extracting-Source-Code.html#Jumping-between-code-and-Org-1">links back</a> to the org mode file, so I usually use
<code>org-quick-tangle</code> to save time testing my Emacs configuration changes (which
savings can be considerable if I end up having to tangle multiple times) and
then once I'm sure that the changes work correctly I'll use the
<code>org-babel-tangle-file</code>-based script once to tangle the final Emacs Lisp
file.
</p>

<p>
Hopefully you'll find this <code>sed</code> script useful in reducing the time you spend
tangling, whether it's your Emacs configuration or some other source code.
</p>

<div class="ieven" id="org3aec793">
<p>
If you'd like to support this site then check out my web app <a href="https://ineverynook.com/">InEveryNook.com</a>
and see if it's of interest to you, pass it along to anyone you think might
be interested in it, or both. Thanks!
</p>

</div>
<div class="taglist"><a href="https://blog0.steelcandy.org/tags.html">Tags</a>: <a href="https://blog0.steelcandy.org/tag-emacs.html">emacs</a> <a href="https://blog0.steelcandy.org/tag-sed.html">sed</a> <a href="https://blog0.steelcandy.org/tag-software.html">software</a> </div>]]></description>
  <category><![CDATA[emacs]]></category>
  <category><![CDATA[sed]]></category>
  <category><![CDATA[software]]></category>
  <link>https://blog0.steelcandy.org/2022-02-12-quickly-tangling.html</link>
  <pubDate>Fri, 11 Feb 2022 19:00:00 -0500</pubDate>
</item>
<item>
  <title><![CDATA[Relocation: Basics]]></title>
  <description><![CDATA[
<p>
<b>Note:</b> there is a <a href="https://github.com/steelcandy2/relocate">public repository</a> containing the source code and formal
documentation for the program that this post is about.
</p>

<p>
There seems to be an endless supply of solutions to the problem of moving
around the filesystem quickly and efficiently. So what's one more?
</p>

<p>
I knew that rather than something that made it easier to switch to the most
recently or most frequently visited directories I wanted my "relocation"
command(s) to be deterministic, so that execution of the same command line
would always switch to the same directory (assuming no changes to the
underlying directory structure, and assuming the same initial working
directory when switching to a directory with a relative pathname). That would
make it easier to use the relocation commands in non-interactive situations
like executing command lines retrieved from the command history and when
"stacking" commands: that is, when typing a command or sequence of commands
into the terminal while waiting for an earlier long-running command to
complete.
</p>

<p>
Near the top of the file named <code>relocate-functions.sh</code> in the <a href="https://github.com/steelcandy2/relocate">repository</a> are
instructions for setting up the commands that we'll be discussing, so we
don't cover that here. We also use the short names/aliases for relocation
commands, which get defined only if you use the "easy" installation method.
If you choose to define your own aliases instead<a href="#org075ae3a">¹</a> then it's recommended that
at least the one that's equivalent to the <code>r</code> alias be given a fairly short
name, since the more you have to type the less convenient switching
directories will be.
</p>

<div id="outline-container-org68bc13b" class="outline-2">
<h2 id="org68bc13b">Creating Relocation Aliases</h2>
<div class="outline-text-2" id="text-org68bc13b">
<p>
My relocation commands centre around the idea of <b>relocation aliases</b> (not to
be confused with shell aliases), which are just short alphanumeric names for
(usually absolute, but possibly relative) directory pathnames. These aliases
are defined in the file <code>~/.relocations</code>, which has a very simple format:
</p>

<ul class="org-ul">
<li>each non-blank line defines a relocation alias, and</li>
<li>each such definition consists of the alias followed by the pathname of
the directory it aliases, with a single space separating them.</li>
</ul>

<p>
<b>Note:</b> copying or appending the <code>.relocations.example</code> file in the
repository to <code>~/.relocations</code> will give you some relocation aliases that
may prove useful later.
</p>

<p>
While the file's format is simple enough that one could just edit the file
directly to add a relocation alias, its safer and easier to use the <code>rs</code>
command. (In fact there's no command for deleting a relocation alias since
deleting the corresponding line/definition from the <code>~/.relocations</code> file is
easy enough to do while still making it hard to unintentionally remove an
alias.) The basic syntax of the <code>rs</code> command is simply
</p>

<pre class="example" id="org7b01eb4">
rs alias dir
</pre>

<p>
where 'alias' is the alias being defined and 'dir' is the pathname of the
directory for which 'alias' is to be an alias. For example, the command
</p>

<div class="org-src-container">
<pre class="src src-bash">rs ed ~/.emacs.d
</pre>
</div>

<p>
will define 'ed' to be the relocation alias for the directory <code>~/.emacs.d</code>
(where the '<code>~</code>' part will be replaced with the user's home directory before
the alias is added to the <code>~/.relocations</code> file). By default you can't
replace an existing alias, but the '<code>-f</code>' option can be used to force its
replacement. So if you had earlier defined the '<code>b</code>' relocation alias with a
command like this:
</p>

<div class="org-src-container">
<pre class="src src-bash">rs b ~/backup
</pre>
</div>

<p>
then running a command like this
</p>

<div class="org-src-container">
<pre class="src src-bash">rs -f b ~/bin
</pre>
</div>

<p>
would make '<code>b</code>' an alias for your <code>~/bin</code> directory instead.
</p>

<p>
The <code>rs</code> command can be used to define aliases for directories with absolute
or relative pathnames, depending on whether the directory passed to it is
relative or absolute. The one exception to this occurs when '<code>.</code>' is
specified as the directory, in which case the alias will be an alias for the
<b>absolute</b> pathname of the current working directory. It's a key component of
how I define most of my relocation aliases:
</p>

<ul class="org-ul">
<li>first I make the directory that I want to alias the current working
directory (using <code>cd</code> or <code>r</code>), then</li>
<li>I run a command like <code>rs somealias .</code> to make a relocation alias for it.</li>
</ul>

<p>
In general the more frequently you plan on switching to a directory the
shorter its alias should be, though an alias that you can't remember isn't
very useful either.
</p>
</div>
</div>

<div id="outline-container-orgce64d98" class="outline-2">
<h2 id="orgce64d98">Relocating</h2>
<div class="outline-text-2" id="text-orgce64d98">
<p>
The <code>r</code> command is used to switch — or relocate — to another directory. It
uses the built in <code>cd</code> command internally to actually change the current
working directory, so it interoperates well with <code>cd</code> and you can use
whichever of the two commands is most convenient in any given situation.
</p>

<p>
When run without any arguments the <code>r</code> command relocates to the directory
that was the current directory before the last <code>r</code> or <code>cd</code> command was
executed, just as if you'd run the <code>cd -</code> command (but faster to type). This
default is intentionally different from <code>cd</code>'s default so that you can run
<code>cd</code> to quickly switch to your home directory and <code>r</code> to quickly switch to
the previous directory.
</p>

<p>
When the <code>r</code> command is executed with arguments then the first one must be a
relocation alias. If that's the only argument then — as you might expect — it
changes the current directory to be the one corresponding to that alias. Note
that if you're using the <a href="https://www.gnu.org/software/bash">bash shell</a> then you can use <a href="https://www.gnu.org/software/bash/manual/html_node/Commands-For-Completion.html#Commands-For-Completion">tab completion</a> to
complete the relocation alias once you've typed the start of it.
</p>

<p>
But if you give the <code>r</code> command two or more arguments then
</p>

<ul class="org-ul">
<li>the second argument is used to select a subdirectory of the directory
that the first argument aliases,</li>
<li>the third argument (if there is one) is used to select a subdirectory of
the one selected by the second argument,</li>
<li>and so on while there are still arguments</li>
</ul>

<p>
and the subdirectory selected by the last argument is the one that will
become the current directory. By default the second and subsequent arguments
must match the start of the names of subdirectories, so for example the
command
</p>

<div class="org-src-container">
<pre class="src src-bash">r ed themes
</pre>
</div>

<p>
(where 'ed' is the alias for the <code>~/.emacs.d</code> directory that was defined
above) would make <code>~/.emacs.d/themes</code> the current working directory, but so
would <code>r ed th</code> if there's no other subdirectory whose name starts with 'th',
as would <code>r ed t</code> if no other subdirectory's name starts with 't'. Actually,
<code>r ed th</code> or <code>r ed t</code> might work even if there are other subdirectories with
matching names: when there are multiple matches then the one that comes first
lexicographically is used, while the other matches are reported on standard
error. For example, executing
</p>

<div class="org-src-container">
<pre class="src src-bash">r ed s
</pre>
</div>

<p>
could result in output like
</p>

<pre class="example" id="org77341ff">
Also: /home/jgm/.emacs.d s -&gt; site-packages, snippets
/home/jgm/.emacs.d/saved
</pre>

<p>
(where the first line gets written to standard error and the second — which
is the pathname of the directory that was changed to — gets written to
standard output).
</p>

<p>
Note that if one or more of the arguments after the first one doesn't match
an existing directory then the current directory won't be changed. For
example, if there's no subdirectory of <code>~/.emacs.d</code> that starts with a 'z'
then executing the command
</p>

<div class="org-src-container">
<pre class="src src-bash">r ed z
</pre>
</div>

<p>
won't change the current directory. In particular it will <b>not</b> change the
current directory to <code>~/.emacs.d</code>.
</p>

<p>
There are also two built-in relocation aliases: '.' always aliases the
current directory and '/' always aliases the root directory. So the command
<code>r / u</code> would likely relocate you to the <code>/usr</code> directory, and following it
with the command <code>r . lo b</code> could relocate you to <code>/usr/local/bin</code>.
</p>

<p>
While the second and subsequent arguments to <code>r</code> match the beginnings of
directory names by default, each and every '.' that appears in such an
argument will match any sequence of zero or more characters. So
</p>

<div class="org-src-container">
<pre class="src src-bash">r ed .he
</pre>
</div>

<p>
will match any subdirectory of <code>~/.emacs.d</code> whose name contains 'he', such as
<code>~/.emacs.d/themes</code>, but also <code>~/.emacs.d/eshell</code>. Since a '.' doesn't have
to appear at the start of an argument, the command
</p>

<div class="org-src-container">
<pre class="src src-bash">r ed .he.s
</pre>
</div>

<p>
will relocate you to the (lexicographically) first subdirectory of
<code>~/.emacs.d</code> that contains 'he' followed (eventually) by an 's'. As a last
additional minor convenience, an argument that contains a '.' that doesn't
match a directory name will (effectively) have a '.' prepended to it and will
be tried again. So the command
</p>

<div class="org-src-container">
<pre class="src src-bash">r ed he.s
</pre>
</div>

<p>
can actually relocate to <code>~/.emacs.d/themes</code> too (though <code>r ed hem</code> can't
since the second argument doesn't contain a '.').
</p>

<p>
That should be enough to get you started using the relocation commands. A
<a href="2022-02-16-relocation-additional.html">later post</a> will cover some of their additional features, as well as some
additional related commands.
</p>
</div>
</div>

<div id="outline-container-org075ae3a" class="outline-2">
<h2 id="org075ae3a">Updates</h2>
<div class="outline-text-2" id="text-org075ae3a">
<ol class="org-ol">
<li>For a real-world example of when you might want to define your own alias
in place of our <code>r</code> alias, reddit user <a href="https://www.reddit.com/user/whetu/">whetu</a> <a href="https://www.reddit.com/r/commandline/comments/spo8co/relocate/hwjw2en/?context=3">points out</a> that the alias
<code>r</code> is <a href="https://pubs.opengroup.org/onlinepubs/009695299/utilities/fc.html">preset in the Korn shell</a>.</li>
</ol>

<div class="ieven" id="orged10539">
<p>
If you'd like to support this site then check out my web app <a href="https://ineverynook.com/">InEveryNook.com</a>
and see if it's of interest to you, pass it along to anyone you think might
be interested in it, or both. Thanks!
</p>

</div>
</div>
</div>
<div class="taglist"><a href="https://blog0.steelcandy.org/tags.html">Tags</a>: <a href="https://blog0.steelcandy.org/tag-shell.html">shell</a> <a href="https://blog0.steelcandy.org/tag-software.html">software</a> </div>]]></description>
  <category><![CDATA[shell]]></category>
  <category><![CDATA[software]]></category>
  <link>https://blog0.steelcandy.org/2022-02-10-relocation-basics.html</link>
  <pubDate>Thu, 10 Feb 2022 08:23:00 -0500</pubDate>
</item>
<item>
  <title><![CDATA[Searchdown: Find and Grep Redux]]></title>
  <description><![CDATA[
<p>
<b>Note:</b> there's a <a href="https://github.com/steelcandy2/searchdown">public repository</a> containing the source code and any formal
documentation for the program that this post is about.
</p>

<p>
Searchdown provides a simpler, pithier and (arguably) more standard interface
for <code>find …</code> and <code>find … | xargs …</code> commands, with additional support for
<code>find … | xargs grep …</code> commands.
</p>

<p>
If you need to list or operate on some or all of the files under one or more
directories (where for our purposes a file is under a directory if it's
directly in that directory or if it's under one of that directory's
subdirectories) it's hard to beat the standard <a href="https://www.gnu.org/software/findutils/"><code>find</code></a> command. Among its
advantages are:
</p>

<ul class="org-ul">
<li>it comes preinstalled with most UNIX-like operating systems (as do
<a href="https://www.gnu.org/software/findutils/"><code>xargs</code></a> and <a href="https://www.gnu.org/software/grep/"><code>grep</code></a>),</li>
<li>it's reasonably fast in most cases and allows the user to improve its
speed and efficiency based on their knowledge of the files being
searched, and</li>
<li>its options for selecting which files to find are all but comprehensive:
I don't think I've ever had a set of criteria that I couldn't translate
into <code>find</code> options.</li>
</ul>

<p>
It does have some disadvantages when used interactively, though:
</p>

<ul class="org-ul">
<li>many of its options - including some of the most commonly used ones - are
both long and nonstandard (e.g. <code>-file</code> instead of <code>--file</code> or <code>-f</code>),</li>
<li>you always have to specify at least one directory: there's no default
(and can't be, as a consequence of the command's syntax)</li>
<li>by default it doesn't properly handle pathnames containing spaces or
other special characters, and</li>
<li>it can be overly persnickety about the ordering of some of its options:
order does often affect the results and efficiency of its search, but in
some cases - the <code>-mindepth</code> and <code>-maxdepth</code> options are where I've most
often encountered it - <code>find</code> (or at least the GNU version of <code>find</code>)
will complain about where an option appears in a command but will then
carry on running that command, so presumably it wasn't confused about
what was intended, just displeased.</li>
</ul>

<p>
The main reason that I wrote <code>searchdown</code> was that <code>find</code> commands -
including ones combined with <code>xargs</code> - were among the ones that I used the
most, and were also some of the longer commands to type. I didn't need the
implementation to change, just the interface, at least for the sorts of
<code>find</code> commands that I used frequently. So I wrote a Python program that
</p>

<ul class="org-ul">
<li>provides a simpler, more standard and less verbose command line
interface, and then</li>
<li>builds and runs the corresponding <code>find</code> command.</li>
</ul>

<p>
The options and arguments that <code>searchdown</code> takes are all described in its
usage message (with some delegation to the <code>find</code> command's documentation),
so I won't repeat that here. Instead I'll give some examples of how I use it,
which is often by way of the shell aliases and functions defined in the
<code>searchdown-aliases.sh</code> file that's included in the <code>searchdown</code> git
repository.
</p>

<p>
But first things first. The name of the <code>searchdown</code> command itself is pretty
long: it's more than twice as long as <code>find</code>. Not an auspicious start!
Personally I feel that short command names should be reserved as much as
possible for use by the user for (aliases for or symbolic links to) the
commands that they use the most often, so I tend to give my programs longer
names and then define shorter aliases for them.
</p>

<p>
In the case of <code>searchdown</code> I give it the alias <code>sd</code>, which is how it'll
appear in the examples below. More accurately, <code>sd</code> is an alias for a shell
function (defined in <code>searchdown-aliases.sh</code>) that sorts the results and runs
them through the default pager program, and also assumes that when it's given
a single non-option argument that it's a regular expression to search for in
all of the matching files. So for example the command
</p>

<div class="org-src-container">
<pre class="src src-sh">sd Som.thing
</pre>
</div>

<p>
is equivalent to the <code>find</code> command
</p>

<div class="org-src-container">
<pre class="src src-sh">find -O2 . -print0 | xargs -0r grep -I -D skip -d skip -H -n <span class="org-string">'Som.thing'</span>
</pre>
</div>

<p>
(where most of the <code>grep</code> command options just cause directories and binary
and device files to be ignored). Note that pathnames containing spaces or any
other special characters - including newlines - are handled properly.
</p>

<p>
Sometimes you just want to list all of the files of a given type: the command
</p>

<div class="org-src-container">
<pre class="src src-sh">sd -tl
</pre>
</div>

<p>
will list the pathnames of all of the symbolic links under the current
directory by running the <code>find</code> command
</p>

<div class="org-src-container">
<pre class="src src-sh">find -O2 . -type l | sed <span class="org-string">'s_^\./__'</span>
</pre>
</div>

<p>
(where the <code>sed</code> command just removes <code>./</code> from the start of any pathnames
that start with it). The <code>sdl</code> alias from <code>searchdown-aliases.sh</code> does the
same thing, and the <code>sdf</code> and <code>sdd</code> aliases do the same thing but for regular
files and directories, respectively, instead of symbolic links. And all three
aliases work like <code>sd</code> does in that if they're given a single non-option
argument then they assume it's a regular expression to search for in all of
the files that are found. I use commands of the form <code>sdf foo</code> quite a bit.
</p>

<p>
Another type of <code>searchdown</code> command that I find useful finds a subdirectory
when I can only remember the start of its name
</p>

<div class="org-src-container">
<pre class="src src-sh">sdd -p Pre
</pre>
</div>

<p>
the end of it (<code>sdd -s Suf</code>) or some fragment of it (<code>sdd -c Part</code>). And of
course the <code>sdf</code>, <code>sdl</code> and <code>sd</code> commands can be used in place of <code>sdd</code> to
search for files of other types, or of any type.
</p>

<p>
Emacs users might find the <code>-fb</code> option handy: for example
</p>

<div class="org-src-container">
<pre class="src src-sh">sd -D src -D doc -fb
</pre>
</div>

<p>
will list the pathnames of all of the backup files - that is, files that end
with a tilde (~) - that are under either the <code>src</code> or the <code>doc</code>
subdirectories of the current directory. All of the files that are found can
then be deleted by rerunning the command and adding <code>-- rm</code> to the end of it:
</p>

<div class="org-src-container">
<pre class="src src-sh">sd -D src -D doc -fb -- rm
</pre>
</div>

<p>
which is equivalent to running the <code>find</code> command
</p>

<div class="org-src-container">
<pre class="src src-sh">find -O2 <span class="org-string">'src'</span> <span class="org-string">'doc'</span> -name <span class="org-string">'*~'</span> -print0 | xargs -0r rm
</pre>
</div>

<p>
(The <code>fb</code> alias can be used in place of <code>sd -fb</code>.)
</p>

<p>
And as some final examples I often use <code>searchdown</code>'s <code>-e</code> option to only
match files with a given extension (where the extension can be specified with
or without a leading '.'): <code>sd -e mp3</code> (or <code>sde mp3</code>) will find all of the
MP3 files under the current directory and (like the <code>sd</code>, <code>sdf</code>, etc.
commands) the command <code>sde txt Som.thing</code> will find all of the lines that
match the regular expression 'Som.thing' in all of the text files (that is,
files with the <code>.txt</code> extension) under the current directory, ignoring any
other files. If you often grep or operate on files with a given extension -
markdown files, for example - then it can be worthwhile to define a shell
function or alias for that type of file. There are already several defined in
<code>searchdown-aliases.sh</code> that you can base yours on.
</p>

<p>
I hope this gives you some idea of what <code>searchdown</code> can do, and whether it's
something you'd find useful. I've been using it for a few years now and I
find that I go for months at a time (or even longer) between direct uses of
the <code>find</code> command. In fact, if I hadn't used the <code>find</code> command for so many
years before that - and if I didn't use it from time to time in shell
scripts - I'm not sure how well I'd remember it's syntax! But you don't have
to worry about that either: just add the <code>-n</code> option to any <code>searchdown</code>
command and it'll output the equivalent <code>find</code> command without running it.
(Or use the <code>-v</code> option to both output the command and run it.) This can be
useful for generating <code>find</code> commands for use in shell scripts where you
don't want to add a dependency on another script (i.e. <code>searchdown</code>).
</p>

<div class="ieven" id="org6a3e264">
<p>
If you'd like to support this site then check out my web app <a href="https://ineverynook.com/">InEveryNook.com</a>
and see if it's of interest to you, pass it along to anyone you think might
be interested in it, or both. Thanks!
</p>

</div>
<div class="taglist"><a href="https://blog0.steelcandy.org/tags.html">Tags</a>: <a href="https://blog0.steelcandy.org/tag-python.html">python</a> <a href="https://blog0.steelcandy.org/tag-software.html">software</a> </div>]]></description>
  <category><![CDATA[python]]></category>
  <category><![CDATA[software]]></category>
  <link>https://blog0.steelcandy.org/2022-01-26-searchdown-find-and-grep-redux.html</link>
  <pubDate>Wed, 26 Jan 2022 12:05:00 -0500</pubDate>
</item>
<item>
  <title><![CDATA[InEveryNook.com]]></title>
  <description><![CDATA[
<p>
For those of you who like to just jump right in you can visit
</p>

<p>
<a href="https://ineverynook.com/auth/invited/public464808186">https://ineverynook.com/auth/invited/public464808186</a>
</p>

<p>
to sign up for a free trial account (where you just have to choose a username
and (longish) password and provide any e-mail address) and give
<b>InEveryNook.com</b> a try. But if you get lost or stuck or run into problems
then read on &#x2026;.
</p>

<p>
<b>InEveryNook.com</b> is a web app that helps you, your guests and the people you
live with find things in your home by helping you record and keep track of
where everything is. Then when you're looking for something you can enter
what you're looking for into the search box at the top of most of the app's
pages (including the sign in page) and get a list of all of the matching
items in your home, along with their locations. And your guests (if you've
allowed it) and the people you live with can do the same thing.
</p>

<p>
The app also makes it easier for you to add your things to it by providing
lists of common items and storage places that you can select from. And it
provides some other, related features like allowing someone to report it when
they discover that you're low on, out of or missing an item that they
searched for.
</p>

<p>
I created <b>InEveryNook.com</b> because while in an ideal world I'd have
everything organized and in the first (or even second or third) place I'd
look for it, I came to realize that realistically I won't ever manage that,
and even if I did it wouldn't stay that way. Plus one only has so much
storage space, so things that are used infrequently end up in out-of-the-way
places - like the backs of cupboards or in boxes on high shelves - where it's
easy to forget that you even have them, let alone find them again when you
need them. But I figured that it <i>was</i> realistic for me to (eventually) go
through all my stuff and (with a little reorganizing here and there along the
way) record where each item is stored so that I could search for it again
later. Which is where <b>InEveryNook.com</b> comes in. Now I just have to finish
adding everything &#x2026;.
</p>

<p>
Anyway, I hope you find it useful. If you encounter any problems or if you
have a question or comment then please feel free to <a href="mailto:jgm@steelcandy.org">e-mail me</a> about it. (Or
about anything else here, for that matter.)
</p>
<div class="taglist"><a href="https://blog0.steelcandy.org/tags.html">Tags</a>: <a href="https://blog0.steelcandy.org/tag-software.html">software</a> </div>]]></description>
  <category><![CDATA[software]]></category>
  <link>https://blog0.steelcandy.org/2021-11-11-ineverynook.com.html</link>
  <pubDate>Thu, 11 Nov 2021 12:36:00 -0500</pubDate>
</item>
<item>
  <title><![CDATA[Software]]></title>
  <description><![CDATA[
<p>
If you're wondering why some aspect one of my programs is the way it is and
the blog post about it doesn't explain it then you may find your answer here.
The following are general notes about my software, in no particular order.
</p>

<p>
<b>User interface.</b> I spend most of my time on the command line, in Emacs or
using a web browser, so consequently most of the programs here are command
line (CLI) programs. Some of the Emacs Lisp code here may provide a more
advanced/complex user interface, but since I use the terminal version of
Emacs I wouldn't expect anything too fancy.
</p>

<p>
<b>Platforms.</b> I haven't run anything but Linux for quite some time, so for the
most part my software has only been tested on that operating system. It
should work on all Unix-like OSes (like MacOS or the BSDs), so if a
particular program doesn't then please report it via its <a href="https://github.com/steelcandy2">GitHub</a> page. But
some of my programs may (usually inadvertently) assume that reasonably
current versions of the <a href="https://www.gnu.org/">GNU</a> implementations of core and other utilities are
being used: unless it's fairly straightforward to make such a program work
with other implementations the "fix" will be for you the user to use recent
versions of the GNU implementations of the relevant programs.
</p>

<p>
<b>Standalone shell scripts.</b> In general I prefer that each of my scripts be
self-contained so that just the one file can be copied to another machine and
it will just work (assuming the programs that it executes exist there, of
course). However, this does result in common and boilerplate code being
duplicated across scripts. (This isn't really a maintenance problem for me
since my scripts are generated from an <a href="https://www.orgmode.org/">org mode</a> file in which a given piece
of common or boilerplate code appears only once and is inserted into each of
the generated scripts using <a href="https://www.orgmode.org/manual/Noweb-Reference-Syntax.html">noweb-style references</a>.)
</p>

<p>
<b>Commit histories.</b> The <a href="https://github.com/steelcandy2">GitHub</a> commit histories tend to be very shallow. This
is primarily because locally I use <a href="https://cvs.nongnu.org">CVS</a> rather than git for my personal
projects. (Hey, if it's <a href="http://cvs.afresh1.com/~andrew/o/why-cvs.html">good enough</a> for <a href="https://www.openbsd.org">OpenBSD</a>.) There doesn't seem to be
any easy way to convert files from a CVS repository into a git repository, so
for the most part I just added the current version of each program to a new
git repository and then pushed it to GitHub. Of course, any and all future
changes will show up in git. And I may include with some posts a short
history of the program: the date when the program was first written (or at
least committed) if nothing else, but also any changes of note.
</p>

<p>
<b>Origins.</b> When a program of mine is based on someone else's code or even
inspired by someone else's idea I do my best to indicate that in the source
code: in the comment at the top of the file if it applies to the whole
program, or at the relevant point in the body of the program if it only
applies to that part of the program. Unfortunately the oldest of my programs
don't contain this information, and so what their origins and inspirations
were has been lost.
</p>

<p>
<b>Copyright and licensing.</b> You should check the individual programs to be
sure, but the code of mine that's up on <a href="https://github.com/steelcandy2">GitHub</a> should be covered by the
<a href="https://www.gnu.org/licenses/gpl-3.0.en.html">GPL3+</a> license: that is, version 3 or later of the GNU General Public License.
</p>

<p>
One final note: these programs have been written over a number of years, so
you may notice variations in the style and techniques (and idioms) used.
</p>

<div class="ieven" id="org1094ceb">
<p>
If you'd like to support this site then check out my web app <a href="https://ineverynook.com/">InEveryNook.com</a>
and see if it's of interest to you, pass it along to anyone you think might
be interested in it, or both. Thanks!
</p>

</div>
<div class="taglist"><a href="https://blog0.steelcandy.org/tags.html">Tags</a>: <a href="https://blog0.steelcandy.org/tag-software.html">software</a> <a href="https://blog0.steelcandy.org/tag-meta.html">meta</a> </div>]]></description>
  <category><![CDATA[software]]></category>
  <category><![CDATA[meta]]></category>
  <link>https://blog0.steelcandy.org/2021-11-10-software.html</link>
  <pubDate>Wed, 10 Nov 2021 11:58:00 -0500</pubDate>
</item>
<item>
  <title><![CDATA[Calculating on the Command Line]]></title>
  <description><![CDATA[
<p>
<b>Note:</b> there's a <a href="https://github.com/steelcandy2/calc">public repository</a> containing the source code and any formal
documentation for the program that this post is about.
</p>

<p>
There are any number of ways to get your computer to do math, but I wanted a
command line program to quickly, easily and in as natural a way as possible
do basic calculations like summing a few numbers, calculating an item's
after-tax price or doing basic unit conversions. I also wanted it to operate
on both integer and real values and give results to a reasonable number of
decimal places.
</p>

<p>
Of course, I wasn't going to implement the part that did the actual math
since existing programs already do that. One of the earliest versions of my
<code>calc</code> script was just a minimal wrapper around one such program: <code>bc</code>. Aside
from code to output a usage message my script's implementation was basically
</p>

<div class="org-src-container">
<pre class="src src-bash">echo "scale=2; $*" | bc
</pre>
</div>

<p>
which combined all of the command line arguments into a single expression for
<code>bc</code> to evaluate, after setting the default number of digits after the
decimal to 2 (which is sufficient for most calculations and ideal for ones
involving (my local) currency). Note that the result from <code>bc</code> may be an
integer or may have fewer or more than the default number of digits after the
decimal depending on what's appropriate for the particular computation.
</p>

<p>
(Also note that the default number of digits after the decimal affects
intermediate as well as final results of calculations, and so the result of a
calculation with fewer digits after the decimal may not just be a rounded -
or even truncated - version of the result of the same calculation done with
more digits after the decimal.)
</p>

<p>
This version worked pretty well, especially with <code>=</code> (that is, a single
equals sign) as a symbolic link to <code>calc</code>:
</p>

<div class="org-src-container">
<pre class="src src-bash">= 2 + 2
= 12.34 + 56.78 + 90.12
= 5 / 3
= '42.5 * 17'
= 42.5 \* 17
</pre>
</div>

<table>


<colgroup>
<col  class="org-right">
</colgroup>
<tbody>
<tr>
<td class="org-right">4</td>
</tr>

<tr>
<td class="org-right">159.24</td>
</tr>

<tr>
<td class="org-right">1.66</td>
</tr>

<tr>
<td class="org-right">722.5</td>
</tr>

<tr>
<td class="org-right">722.5</td>
</tr>
</tbody>
</table>

<p>
Having to quote or escape multiplication signs to protect them from being
expanded by the shell into every file in the current directory was less than
ideal, but a simple - if a little brute-force - change
</p>

<div class="org-src-container">
<pre class="src src-bash">echo "scale=2; $*" | tr 'x' '*' | bc
</pre>
</div>

<p>
fixed that. Now the 'x' character could be used as a multiplication sign as
well:
</p>

<div class="org-src-container">
<pre class="src src-bash">= 42.5 x 17
</pre>
</div>

<pre class="example">
722.5
</pre>


<p>
So now the four basic mathematical operations could be used without having to
quote or escape anything. Parentheses still required - and still do require -
escaping or (usually easier) quoting, however:
</p>

<div class="org-src-container">
<pre class="src src-bash">= '(23.40 + 12.34) x 1.12'
</pre>
</div>

<pre class="example">
40.02
</pre>


<p>
The final tweak - until very recently - was to allow the default number of
digits after the decimal to be customized on a per-session or a per-command
basis. The implementation effectively became
</p>

<div class="org-src-container">
<pre class="src src-bash">echo "scale=${CALC_SCALE:-2}; $*" | tr 'x' '*' | bc
</pre>
</div>

<p>
which allowed the <code>CALC_SCALE</code> environment variable to be used to specify
that default:
</p>

<div class="org-src-container">
<pre class="src src-bash">= 5 / 3
export CALC_SCALE=4
= 5 / 3
CALC_SCALE=8 = 5 / 3
= 5 / 3
CALC_SCALE=
= 5 / 3
</pre>
</div>

<table>


<colgroup>
<col  class="org-right">
</colgroup>
<tbody>
<tr>
<td class="org-right">1.66</td>
</tr>

<tr>
<td class="org-right">1.6666</td>
</tr>

<tr>
<td class="org-right">1.66666666</td>
</tr>

<tr>
<td class="org-right">1.6666</td>
</tr>

<tr>
<td class="org-right">1.66</td>
</tr>
</tbody>
</table>

<p>
And that's where it stood until I looked at it again in preparation for
writing this post. While it's still not a very long script its core is
definitely no longer a one-liner. But calculations can now include the
mathematical constants <code>pi</code> and <code>e</code>, as well as the <code>sin()</code>, <code>cos()</code>,
<code>atan()</code>, <code>ln()</code> and <code>exp()</code> functions, and the default number of digits
after decimal is now easier to customize:
</p>

<ul class="org-ul">
<li>the <code>CALC_SCALE</code> environment variable's name was too long for convenient
use on the command line, so the <code>sc</code> environment variable can also be
used: <code>sc</code> is intended for use with a single invocation of <code>calc</code>, while
<code>CALC_SCALE</code> should be used over larger scopes</li>
<li>if the program's basename is an equals sign (=) preceded by one, two or
three digits then the number that those digits represent will be the
default number of digits after the decimal</li>
</ul>

<p>
(A program can be given an additional basename by creating a symbolic link to
it: for example, executing <code>ln -s calc 15=</code> will create a symbolic link that
can be used to perform calculations with a default of 15 digits after the
decimal place.)
</p>

<p>
So calculations like the following are now possible:
</p>

<div class="org-src-container">
<pre class="src src-bash">= 2 x pi x pi
export CALC_SCALE=5
= 2 x pi x pi
sc=8 = 2 x pi x pi
= 2 x pi x pi
8= 2 x pi x pi
9= 'sin(1.2) x sin(1.2) + cos(1.2) x cos(1.2)'
sc=10 = pi
</pre>
</div>

<table>


<colgroup>
<col  class="org-right">
</colgroup>
<tbody>
<tr>
<td class="org-right">19.71</td>
</tr>

<tr>
<td class="org-right">19.73917</td>
</tr>

<tr>
<td class="org-right">19.73920875</td>
</tr>

<tr>
<td class="org-right">19.73917</td>
</tr>

<tr>
<td class="org-right">19.73920875</td>
</tr>

<tr>
<td class="org-right">0.999999996</td>
</tr>

<tr>
<td class="org-right">3.1415926535</td>
</tr>
</tbody>
</table>

<p>
Will I use this additional functionality often enough to warrant the added
complexity? I'm not sure. But if I didn't add it then there wouldn't be any
way for me to find out. It's not a huge increase in absolute complexity in
this case (even if it is in relative complexity), but I think the same
reasoning applies to other, larger programs as well. Of course, I could
always remove the new code if it turns out I don't use it, but who ever does
<i>that</i>?
</p>

<p>
As a final note, Emacs users can use the <code>quick-calc</code> command to access
similar functionality, as I recently <a href="https://www.reddit.com/r/emacs/comments/qvujcj/weekly_tips_tricks_c_thread/">found out</a>. Well, rediscovered actually,
since it turns out that I've had a keybinding for it in my Emacs
configuration since 2018 and had completely forgotten about it: a not
uncommon occurrence.
</p>

<div id="outline-container-history" class="outline-2">
<h2 id="history">Selected Commit History</h2>
<div class="outline-text-2" id="text-history">
<p>
Some selected dates in the <code>calc</code> script's original commit history (which is
<a href="2021-11-10-software.html">different</a> from its GitHub commit history):
</p>

<ul class="org-ul">
<li><span class="timestamp-wrapper"><span class="timestamp">&lt;2007-07-16 Mon&gt; </span></span> initial commit</li>
<li><span class="timestamp-wrapper"><span class="timestamp">&lt;2015-08-31 Mon&gt; </span></span> can use 'x' in place of '*'</li>
<li><span class="timestamp-wrapper"><span class="timestamp">&lt;2017-03-31 Fri&gt; </span></span> can use environment variable to set digits after decimal</li>
<li><span class="timestamp-wrapper"><span class="timestamp">&lt;2021-11-24 Wed&gt; </span></span> can use shorter <code>sc</code> environment variable</li>
<li><span class="timestamp-wrapper"><span class="timestamp">&lt;2021-11-25 Thu&gt; </span></span> can use pi, e, sin(), cos(), atan(), ln() and exp() in
calculations</li>
</ul>

<div class="ieven" id="org5aa2b24">
<p>
If you'd like to support this site then check out my web app <a href="https://ineverynook.com/">InEveryNook.com</a>
and see if it's of interest to you, pass it along to anyone you think might
be interested in it, or both. Thanks!
</p>

</div>
</div>
</div>
<div class="taglist"><a href="https://blog0.steelcandy.org/tags.html">Tags</a>: <a href="https://blog0.steelcandy.org/tag-shell.html">shell</a> <a href="https://blog0.steelcandy.org/tag-software.html">software</a> </div>]]></description>
  <category><![CDATA[shell]]></category>
  <category><![CDATA[software]]></category>
  <link>https://blog0.steelcandy.org/2021-11-02-calculating-on-the-command-line.html</link>
  <pubDate>Tue, 02 Nov 2021 12:36:00 -0400</pubDate>
</item>
</channel>
</rss>
