Home About me Blog Po polsku

Content AND Presentation

(Note to English-speaking readers: the links entitled Komentarze na tej stronie lead to comment pages.)

2015-07-04 C-x 4 bindings

Today I’m going to write about some lesser-known Emacs bindings, namely ones beginning with C-x 4. (In general, C-x DIGIT bindings are sometimes interesting; while everyone knows C-x 1 and C-x 2, it might be a good idea to press C-x 3, C-x 8 C-h and a few other combinations.)

C-x 4, however, is a treasure chest. Well, maybe not that much, but there are a few really useful things beginning with it. So, let’s get started.

First, C-x 4 C-f (or C-x 4 ffind-file-other-window), which finds a file in another (newly created) window. Basically, it is a shorthand for C-x 2 C-x o C-x C-f (maybe not exactly, but you get the idea). Quite useful if you happen to need to edit a file while seeing the previous one.

Next is C-x 4 C-o (display-buffer), which is (potentially) even more useful. It opens another window (again, like C-x 2), but instead if showing the same buffer, shows another one, the name of which is read from the minibuffer. Note that the newly opened window does not get selected! Just in case you forgot, you can scroll the other window with C-M-v and C-M-S-v.

Another useful command is C-x 4 0 (kill-buffer-and-window), which not only deletes the current window (like C-x 0), but also kills its buffer. There is also C-x 4 b (switch-to-buffer-other-window), which does exactly what it says on the tin, C-x 4 d (dired-other-window – you can guess what it does), and one of the best of them all: C-x 4 c. This last keychord is bound to the command clone-indirect-buffer-other-window. It splits the current window, clones the current buffer and visits the clone in the newly opened window.

What is it useful for? Well, if you open the same buffer in two windows simultaneously, both windows share a few things; one of them is narrowing. I’m a heavy narrowing addict, and very often find myself editing (or viewing) a buffer narrowed to some fragment (be it a defun, a LaTeX section, or an Org-mode subtree). Sometimes, however, I need something from some other part of the file I’m editing, and C-x 2 is not enough then: if I widen the buffer in one window, it gets widened globally. What I can do then is: press C-x 4 c C-x n w, do whatever I need, and C-x 4 0.

Note: I hear that Drew Adams’ Wide-N library is another way to solve a similar problem. I will definitely try it at some point. Still, I think that C-x 4 bindings – and C-x 4 c in particular – are worth knowing anyway! And don’t forget that you can press C-x 4 C-h to see all of them.

CategoryEnglish, CategoryBlog, CategoryEmacs

Komentarze na tej stronie

2015-06-27 Arriving at Amen

I do not often write book reviews on my blog (in fact, this is probably the first time;-)). However, I have recently read a book which is rather exceptional, so I’m going to make an exception.

Leah Libresco’s Arriving at Amen is a strange book. It is written by a former atheist turned Catholic, and this more because of intellectual reasoning than by an emotional impulse (which is, by the way, definitely a reasonable way – pun intended).

It is not, however, an apologetic book. Instead, the author decided to write a primer on prayer. This is really, really strange: usually, books on prayer are written by giants of spiritual life, mystics etc. (think St. John of the Cross, for instance).

Or are they? What is a “giant of spiritual life”, anyway? Is it really a Catholic notion? (Well, Jesus is one, and Mary too – in a different way, of course – but we’re talking about sinners here.) To be more specific: would you consider St. Peter a “giant of spiritual life”? Or St. John? Both had their weak moments (according to the Gospels, St. Peter indeed had quite a few of them). So you aren’t a “giant of spiritual life” through extraordinary willpower or anything like that; you are a “giant of spiritual life” (more or less) if you are humble enough to admit that you are not, and let God guide you instead of trying to do something “clever” on your own.

You might argue that comparing a neophyte (Libresco was received into the Catholic Church less than 3 years ago) to the apostles is not very appropriate. You know what? They were neophytes, too (in a sense). St. Paul began his teaching rather quickly after his conversion, didn’t he? Well – you might argue – but St. Paul was a good (Jewish) theologian already. That’s true, but Libresco, while not a theologian, is a philosopher (not in a sense of having a diploma, but in a sense of having a habit of sitting down and thinking in order to learn what’s going on), so in my book it’s close enough.

Also, Libresco is definitely a geek, and that’s really cool, since the geek culture is not particularly Christian, and having a Catholic to speak its language is a nice thing. Her geekiness is easily seen from the list of authors she quotes in the book: while St. Augustine, GKC, or JRRT are no surprise in any modern text on Catholicism, OSC, Alan Perlis and Randall Munroe are.

But what’s so special about the text itself, apart from the author? As I said, it’s a primer on prayer. The subtitle reads: Seven Catholic Prayers that even I May Offer. It turns out that the choice of these prayers is very, very interesting. Let me list them in the order they appear in the book: Petition, Confession, Examen, Rosary, Divine Office, Lectio Divina and the Mass. While petitionary prayer and the Mass are rather obvious choices, none of the rest is so – most of them seem not to be extremely popular nowadays (which is a shame). For instance, the stereotype of confession is as far of the feeling of joy as possible, for instance; all Catholics practicing it regularly know how false the stereotype is, and Libresco writes about it brilliantly.

I have to say that I really could not put down this book, even if it has a few (really, few) weaker places. It is full of insightful observations and surprising analogies. It makes me really want to try the Divine Office again (after a few failed attempts, I have to say), or other practices mentioned (though seemingly I was never brave enough to try to practice Examen or Lectio Divina). Leah Libresco, thank you so much for this book!

OK, this post is already quite long, so I’ll finish it here. Expect, however, some nice quotations in the near future. Irrespective whether you are Catholic or not (and you should be, really!), you may find them interesting and inspiring.

CategoryEnglish, CategoryBlog, CategoryFaith

Komentarze na tej stronie

2015-06-20 Fast-calc

I’ve been a fan of accounting for a long time now. My affair with bookkeeping started when I studied economics, and took a course in Financial accounting (not that I had any choice – it was a compulsory one). I was then exposed to the brilliant ideas of double-entry accounting, first codified by Luca Pacioli in the late XV century. As a mathematician, I cannot overestimate the fact that double-entry bookkeeping is in fact an abstract formalized system. What’s even more impressive is that the bookkeepers are the ones who do negative numbers right! Indeed, in bookkeeping there is no such thing as a “negative number” (well, except for some special circumstances, but never mind). Instead, each account has two sides, and its “balance” is effectively the difference between them. In other words, each “number” in accounting is in reality a pair of numbers, and if two such pairs (a,b) and (c,d) have the property that a+d=b+c, they are identical from the balance standpoint. Looks familiar? (Though to be honest, bookkeepers do not use the language of “pairs” and “equivalence”.)

When I was looking for a tool to manage my personal finance some twelve years ago, I stumbled upon GnuCash. It was free as in beer, worked under Linux (I used Mandrake Linux, later renamed Mandriva, at that time), and featured a full-blown double-entry system. What else could I ask for?

Then I got married, and I lost motivation to track my (our) spending (the income part was easy, but you really need to do both in an accounting system). I had a few attempts to track our family finance in GnuCash, but they never caught on.

Fast forward to 2014, and I discovered Ledger. Wow. Already being a user of Org-mode, and thus sold on the idea of keeping my data in plain text, I instantly fell in love. (BTW, GnuCash keeps its data in format whose name I prefer not to mention; suffice it to say that it has an ominous three-letter buzzword abbreviation and is basically s-expressions reinvented with angle brackets and more verbose notation.) When I learned that there’s a special Emacs mode for editing Ledger files, I was just squeaking with delight.

Now I’ve been using Ledger and ledger-mode for almost a year. Don’t get me wrong: I’m happy with it, but it doesn’t work as smoothly as I would wish to. For a long time I wanted to do something about it; now the time has come. I started to read ledger-mode source code, and decided to augment it with some customizations.

The first one I wanted to do was a way to quickly perform computations. In Ledger, you can put an arithmetic expression in place of an amount (though you have to put it in parentheses), but I didn’t feel like cluttering my ledger file with that. An obvious solution is to use Calc – but firing Calc, performing computations, and then yanking them back into the file is a bit awkward. I decided that I would check the programmer’s interface for Calc, and it turned out that it’s a very nice one. Basically, you have to know about one function (unless you really, really care for performance, or have other special needs) – calc-eval. In its basic form, it takes a string with an expression and gives you back a string with the result. Writing a function which looks back, checks whether it sees an expression (using a primitive regex) and replaces it with the result was trivial.

This was already a nice thing to have, but it was not enough. I decided that I want my function to always give results to two decimal places, and append the currency name (in Poland we put the currency name or abbreviation after the amount). Since I wanted my function to remain a general-purpose device, I decided to delegate the latter behavior to a prefix argument. Here is the code:

;; Fast calculation: interactively replace simple arithmetic
;; expressions with their values in arbitrary buffers

(defvar fast-calc-suffix " PLN"
  "The default suffix for C-u M-x fast-calc.  Useful in ledger-mode.")

(defun fast-calc (currency)
  "Replace the arithmetic expression to the left of the point
with its value.  The arithmetic expression is defined as a simple
regex match."
  (interactive "P")
  (when (looking-back "[-+/*().0-9]\\{3,\\} *" (line-beginning-position) t)
    (replace-match (save-match-data
		     (calc-eval (if currency
				     (concat (match-string-no-properties 0) "+0.0")
				     '(fix 2))
				  (match-string-no-properties 0))))
		   t t)
    (if currency (insert fast-calc-suffix))))

(global-set-key (kbd "C-z c") #'fast-calc)

It looks a lot more complicated than it is (well, I could simplify it a bit by using a backquote instead of list, but I do not like to use it outside macros), but in fact is quite straightforward. The only interesting thing is the +0.0 part; this is needed to convert an int (in case the result of computation is an integer) to a float. Notice the calc-eval invocation, where I set the format to “fixed-point” with two decimal places.

CategoryBlog, CategoryEnglish, CategoryEmacs

Komentarze na tej stronie


(Więcej means More in Polish; click it to see older entries.)

CategoryEnglish, CategoryBlog