2018-09-03 Emacs obsolete commands

In one of my previous posts I mentioned that the Info-edit command is obsolete. You might be wondering what that means exactly. (I was, for sure.) Well, here’s the story.

As the Elisp reference manual says:
You can mark a named function as “obsolete”, meaning that it may be removed at some point in the future. This causes Emacs to warn that the function is obsolete whenever it byte-compiles code containing that function, and whenever it displays the documentation for that function. In all other respects, an obsolete function behaves like any other function.

This is not the whole truth, however: it turns out that marking a command obsolete makes it harder to run it with M-x (which makes sense). This is documented in the Emacs manual:
When ‘M-x’ completes on commands, it ignores the commands that are declared “obsolete”; for these, you will have to type their full name. Obsolete commands are those for which newer, better alternatives exist, and which are slated for removal in some future Emacs release.
This is quite neat.

And to learn how Emacs marks functions as obsolete, let us look at an example:

(make-obsolete 'Info-edit
	       "editing Info nodes by hand is not recommended." "24.4")

You may type C-h f Info-edit RET to see how this works. Consult the Elisp reference to learn more about making functions (and variables) obsolete.

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

2018-08-27 Whitespace in Emacs regexen

Some time ago I wanted to search for a sequence of two words in a buffer. Normally I’d search for lorem<space>ipsum, but what if there is a newline between them? Or a non-breaking space? Or a tab?

Well, Emacs has you covered – but in a thick layer of mud. I think I untangled a bit of that mess, and here’s what I’ve found.

First of all, every character has a “syntax class”. This is mostly useful in programming modes, and while you can use syntax classes in text-mode buffers and their ilk, it may not be the best idea. For instance, take this (text-mode) buffer:

lorem ipsum dolor sit amet
lorem	ipsum dolor sit amet
lorem ipsum dolor sit amet
ipsum dolor sit amet

(in the second line, there is a tab between “lorem” and “ipsum”, and in the third one a non-breaking space).

If you search for lorem\s-ipsum (\s- means “any character of syntax “whitespace”), which can also be written as lorem[[:space:]]ipsum, you will find all occurrences with the exception of the one with the non-breaking space, which has syntax “punctuation”. (This looks nonsensical until you realize, possibly with the help of Eli Zaretskii, that it actually does make sense.)

Of course, you can search for lorem[[:space:] ]ipsum, but this is kind of ugly.

Happily, there is not only [:space:], but also [:blank:], which matches horizontal whitespace, as defined by Annex C of the Unicode Technical Standard #18. Unfortunately, it doesn’t match a newline (which makes sense, but is annoying in this context), so you probably want to search for lorem[[:blank:]^J]ipsum (where ^J is a control character entered with C-q C-j). Ugh.

If that were not enough, you can also regex-search for “categories”. This is a concept I didn’t know about until very recently. It is similar to the syntax classes in that it is an Emacs-only concept (so not related to e.g. Unicode) and buffer-local, but different in that one character may belong to several categories. Apparently, this is not widely used in the Emacs sources (I noticed that filling functions use it, though), but I can imagine it’s potentially interesting. For instance, you can regex-search for consonants or Japanese characters.

As you can see, searching for whitespace turns out to be quite a rabbit hole. If you don’t need to match newlines (i.e., you don’t use hard newlines in your prose), \s- is probably your best bet, but in general you have to be careful. I hope this helps a bit.

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

2018-08-18 Embedding files in Org-mode revisited

Some time ago I published a snippet of Elisp code to embed arbitrary files in Org-mode files using Base64 encoding. I was recently asked if it was possible to view this image.

Well, this being Emacs, of course it’s possible. The question is, how exactly. One way would be to call xdg-open (or equivalent) right after decoding the file to somewhere in the filesystem. (The file must be Base64-decoded, at least to some temporary directory, for any external program to do anything with it.) While I didn’t check it, I heard that the openwith mode might help with this.

Another possibility is to embed links to the files in the Org-mode file itself. This has the advantage of not having to run an external program and the disadvantage that it won’t “show” audio or video in the same way as images.

Anyway, here’s an updated version that inserts a link to the file you have just inserted. In the previous version, you had to change the filename manually; here, I decided to do this automatically (I just strip the path so that the file becomes local to the current directory).

(defun org-insert-file (filename)
      "Insert Elisp code block recreating FILE in the current
      (interactive "f")
      (let ((basename (file-name-nondirectory filename))
	       (insert-file-contents-literally filename)
	       (base64-encode-region (point-min) (point-max))
	  (insert (format "[[./%s]]\n#+BEGIN_SRC emacs-lisp :results output silent\n  (with-temp-file %S\n    (insert (base64-decode-string\n      %S)))\n#+END_SRC" basename basename base64-string)))
	(copy-file filename basename 1)

Notice a few things about this code. First of all, we use the file-name-nondirectory function, which strips the directory part from the filename. The forward-line is a nicety so that the point won’t end up invisible on the image. In order to copy the file to the current directory, we use the copy-file function with a third argument being any integer (this means that we allow overwriting a file, but request an explicit user confirmation for that). Finally, we turn on displaying inline images (which won’t do much good if the file is not an image – but in that case, we will just have a link, hopefully one that can be opened with C-c C-o).

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryOrgMode

Comments on this page