Content AND Presentation

2017-08-21 replace-regexen-in-string

Recently, I had a need to do some replacements in a string. Basically, that meant repeatedly calling replace-regexp-in-string. I strongly disliked the idea of nesting these calls, so I set out to write a simple tool to help with that. Here is the result:

(defun replace-regexen-in-string (replacements string &optional fixedcase literal)
  "Call `replace-regexp-in-string' on STRING multiple times.
REPLACEMENTS is an alist of pairs (regex . replacement).
FIXEDCASE and LITERAL are passed to `replace-regexp-in-string'."
  (let ((result string))
	(mapc (lambda (replacement)
		(setq result
		  (replace-regexp-in-string (car replacement)
						(cdr replacement)

It’s probably not the optimal/most beautiful way to do it. In particular, it is rather imperative and not very functional – I am pretty sure that it could be done more elegantly with reduce, or by a macro which would generate the recursive calls. Maybe some day I will write all three versions, and then measure their performance (I suspect my one should be rather a faster one). But it is optimal in terms of programmer time – I hacked it together in a few minutes, and much of that time was looking up replace-regexp-in-string syntax (and then checking whether the last two optional arguments work fine). Feel free to use it if you need to.

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

2017-08-14 The highlight.el library

Last year, I wrote about a few ways of highlighting things in an Emacs buffer. A few days ago I had a somewhat similar need. This time, however, I didn’t have a regex to match the things I wanted highlighted: I just needed to mark something (as in “region”), and have it permanently highlighted (“permanently” meaning that the highlighting should survive editing commands, but not actually closing the buffer).

Emacs being Emacs, it turned out to be possible (and easy). Download the highlight.el library (authored by the famous Drew Adams) and enjoy a bunch of commands under the prefix hlt-. There are quite a few, but let me mention just two.

First, hlt-highlight-region. It does exactly what it says on the tin, and comes with a counter-spell called (of course) hlt-unhighlight-region.

And here is the other gem: hlt-highlighter-mouse. Issue this command and then drag a portion of the buffer with the mouse. It acts like a highlighter pen! How cool is that? And when you no longer need the highlighting, just say hlt-eraser-mouse and drag again. (Also, you can bind hlt-highlighter and hlt-eraser to drag events instead, e.g. with modifiers like super.)

Also, it is worth remembering that hlt-unhighlight-region removes all highlighting in the region, and if the region is not active, in the buffer.

Read the linked page for more, but even these commands are quite useful!

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

2017-08-07 demo-it recording

Some time ago I stumbled across a very nice library by Howard Abrams, called demo-it. It seems that this tool, combined with org-tree-slide, is a perfect way to do presentations on Emacs, Org-mode, or Calc, which I happen to do from time to time.

One problem I had with demo-it was that preparing a presentation is rather cumbersome. However, it occurred to me that many such presentations just mimic what you normally do in Emacs, and therefore it would be much better to “record” some activities and then generate a demo-it presentation out of them. And while hooking into Emacs command loop to do this is beyond my capabilities (and/or patience, probably), the good people who programmed Emacs already did the hard work for me. Yay for keyboard macros! The only two remaining problems are (1) hooking into keyboard macro stuff (and this is easy thanks to advice) and (2) converting macros into stuff that can be fed into demo-it. The latter part is not trivial, but doable. The thing is, I’d like to use demo-it-insert for “textual” stuff and explicit strings (like C-x 1) for commands. This post of Stefan Monnier set me on the right course, and after some tinkering, I came up with a working proof of concept. It is very far from being polished, and probably quite buggy, but it seems already usable. I hope to work with Mr. Abrams so that this could be fleshed out and merged into his library.

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page


CategoryEnglish, CategoryBlog