Blog

For the English part of the blog, see Content AND Presentation.

2021-05-08 Disabling distractions

Like many other people, I have to fight with distractions a lot. The main distractors (at least when I work on my computer) are a few websites that rhyme with “litter”, “space crook” and “poo cube”.

Some time ago I decided that I need to take brutal action against them, and I put this in my /etc/hosts file:

# begin distractors
127.0.0.1 twitter.com
127.0.0.1 facebook.com
127.0.0.1 www.facebook.com
127.0.0.1 youtube.com
127.0.0.1 www.youtube.com
# end distractors

This turned out to be a bit too brutal, however – I sometimes really want to be able to use some of these terrible websites. I figured that it would be pretty cool if I could just turn them on for, say 10 minutes (or some other amount of time, decided in advance).

There is one obvious loophole in this idea: if I go to a “forbidden” site and don’t reload the page, I can still spend time there even after it gets disabled via the /etc/hosts trick. Since all of them seem to use JS to fetch more content, this is not a big issue. Also, this is not intended as a safeguard I cannot disable (well, I can, of course, I have the root access;-)), but rather as a nudge for me to stop wasting time.

So, let’s get coding. Enabling and disabling a section of /etc/hosts is easy with sed:

sed -i '/begin distractors/,/end distractors/ s/^# //' /etc/hosts # enable
sed -i '/begin distractors/,/end distractors/ s/^/# /' /etc/hosts # disable

Now, here’s the trick. In order to do something after, say, 10 minutes, I can use the little-known at command-line tool. (On my system, I had to explicitly enable the at system service for it to work.) However, sudo password times out after 5 minutes, so I have to make sure that at is run as root. IOW, at sudo ... wouldn’t work (most probably), but sudo at is fine.

So, here is my enable-distractors.sh script:

#!/bin/bash

if [ "$1" == "" ]
then
	TIMEOUT=10 minutes
else
	TIMEOUT="$1"
fi

echo I will disable distractors again in "$1"!
echo
sudo sed -i '/begin distractors/,/end distractors/ s/^/# /' /etc/hosts
echo sed -i \'/begin distractors/,/end distractors/ s/^# //\' /etc/hosts | sudo at now + "$TIMEOUT"

Before you copy and start using it, beware. This is a script which uses sudo to run sed -i on an important system configuration file in /etc, written by a madman with moderate experience at shell scripts – so use it at your own risk, and definitely back up your /etc/hosts before any experiments!

CategoryEnglish, CategoryBlog

Comments on this page

2021-05-01 Emacs Lisp book revived

Five and a half years ago I mentioned that I was writing an intermediate book on Emacs Lisp programming. Unfortunately, the project started – and then stalled for quite some time.

There were two main reasons: one was that around the same time I took part in another big writing project, together with two more people, which lasted until late 2020. It is now finished, and I am probably not allowed to talk about it in public yet, but it is going to see the light of day quite soon, at least for some values of “soon” (hopefully still in 2021), and I’m very excited about it – and I think my mathematically-inclined readers will be, too.

Another reason was that I changed my job in 2016 and fulfilled one of my long-time dreams – being a “real programmer” (feel free to insert your favorite joke here). It turned out that I had a lot of things to learn (which is great), and quite a few to unlearn (working in academia for more than a decade really does things to you mind, and I am still unlearning some of those…)

Anyway, a few months ago I decided to revisit that project, and I have spent quite some time on writing for the last few months. I am quite proud to say that an early draft of the book is already available here. I would be very thankful for any feedback. The tagline of Leanpub, the platform I’m using, is to “publish early, publish often” – in other words, it is a platform to publish early drafts of books, which people can then buy trusting that the author will finish them. (Also, motivating the author to work hard on finishing them, too!)

Before you go buy the (as of now) one third (approximately) of the book, let me explain a few things about the project, which is now more mature than it was in 2015.

In fact, one of the reasons the project stalled a bit was that it was very difficult for me to decide what to include, in what order etc. Five years ago I made a mistake of just sitting down and writing the bits I felt like to write, and leaving the decisions about what the book as a whole is going to look like for later. This was very wrong. You can write a blog this way, since the posts do not have to form a coherent book-like structure. You can also write a book this way if it is obvious what should be included and in what order (my first book was written this way, and it was ok then – it was pretty short, the list of concepts to include was self-evident, and so was the order, because some of them naturally build on others).

Emacs and Emacs Lisp are really quite complicated, and writing a comprehensive book doesn’t make any sense – it would basically mean rewriting the Emacs Lisp Reference. So, this is what I did instead. I made a list of topics I would like to cover, and spent some time gathering ideas for tools to code which could be a good illustration of them. And not every idea could make that list! One of the goals of the book was: no toy examples, just really useful code. And indeed, most of the code in the book – even if it starts as artificial, simple examples – evolves into fully useful stuff you could include in your init.el.

Also, I didn’t want to include code which would only be useful to a small fraction of people. For instance, there is no AUCTeX-related code in the book, nor any extensions to Org-mode (since not every Emacser is also a LaTeX or Org-mode user).

One thing I am not very happy with is that I’d prefer to have more shorter chapters instead of a few longer ones. (It can change, since right now I’m reconsidering the chapter list – a few days ago I had a nice idea, and it is quite probable that I will add one or two chapters!) This is bad in a way, since someone may find the subject of one of the chapters boring, but a later chapter may build on what was presented in the preceding ones. On the other hand, I wanted to avoid a situation which is very common in programming books, where the author introduces some concept, shows a simplified example use, and proceeds to another one. In my book we do start with simple examples, but at the end of every chapter we are going to arrive at something pretty comprehensive – something that could as well be published on Melpa as a package. (In fact, I might publish some of them at some point in time.)

Also, there are some important concepts which are absent from the book. My idea was to write something which can be considered more or less complete, and once I gather some feedback, maybe expand it. There are indeed quite a few natural candidates for more chapters – testing, writing non-special major modes, running external tools from Emacs, and possibly other stuff. I even dropped some ideas I was pretty sure would make their way to the book, like an intro to functional programming I wrote a few years ago – I may reintroduce them after I write what I have planned now, but I want to focus on delivering a complete and self-contained (even if a bit minimal) book first.

One of the nice things about publishing a work in progress is that it enables the author to get early feedback. So, if you decide to trust me enough to pay for this early version of the book, I’d be very thankful for any feedback. Is it too concise? Too verbose? Are the sentences too short? Too long? And most importantly, is something difficult to understand? Tell me!

To wrap it up for now, expect more updates here, most probably once per month or so. I want to complete the book (or what might become its “first edition” in the future) around September, so stay tuned!

CategoryBlog, CategoryEnglish, CategoryEmacs, CategoryEmacsBook

Comments on this page

2021-04-26 Binding a command to what is normally a prefix key

Some time ago, someone on the help-gnu-emacs asked how to rebind f2 so that it sets the default major mode for the current buffer. (The use case was that he wanted to change the mode of a buffer to perform some task, and then have an easy way to return to the “default” mode, based on the normal way Emacs uses to choose the major mode. That normal way is indeed a bit tricky.)

Well, there are in fact two questions hidden here. The former – resetting the major mode to “default” – is covered by normal-mode, which is a command doing exactly that. (The actual mode selection is done by the set-auto-mode function, invoked by normal-mode.) That one is fairly easy.

The latter question, however, is slightly more tricky. You cannot use the global-set-key to bind f2 to normal-mode interactively, since it is a prefix key by default. However, after unsetting it, it is possible. So, you can say M-: (global-unset-key [f2]) RET and then you are free to say M-x global-set-key RET f2 normal-mode RET. Of course, doing it in one step is also possible: M-: (global-set-key [f2] #'normal-mode) RET. Either way, I do not know of any way to do it without executing some Elisp code.

One more reason to learn a bit of Elisp if you are an Emacs user.

And by the way, I might have something to help learning a bit of Elisp in the very near future. Stay tuned!

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

More...