Emacs Hooks

Daniel Liden has a useful post on elementary hook functions. From the (Elisp) user perspective, there’s not a lot to know. All you need is

  1. One of more functions taking no arguments. These are called “hook functions”.
  2. A variable containing a list of hook functions to call. By convention, this variable’s name ends with -hook.
  3. A function, add-hook, to add hook functions to the list of hooks.
  4. A function to run the hook functions at the appropriate time.

It turns out there are two main functions to run hooks:

  1. run-mode-hooks to run hooks associated with a major mode
  2. run-hooks to run hooks not associated with modes

The existence of a special function for mode hooks raises an obvious question: “Why do we need a special function for mode hooks?” I didn’t know so of course I had to see how they differ. Run-mode-hooks is more complicated and basically takes care of worrying about doing the right thing when it’s called from a derived mode. Complicated but not too exciting.

One surprising fact that I discovered is that run-hooks is a builtin function in the C core. I always imagined that run-hooks was a simple function along the lines of

(defun run-hooks (hooks)
  (dolist (h hooks)
    (funcall h)))

The C version does essentially the same thing except that the list of functions can also contain a sublist of functions. Here it is (with the doc string removed):

DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
  (ptrdiff_t nargs, Lisp_Object *args)
{
  ptrdiff_t i;

  for (i = 0; i < nargs; i++)
    run_hook (args[i]);

  return Qnil;
}

It could hardly be simpler so why is it in the C core? My guess is because it’s needed before the rest of the Lisp run time is established although I’m having a hard time making sense of that. If anyone knows for sure, leave a comment.

Posted in General | Tagged | Leave a comment

Google Loses Its Users’ Data

Some Google Drive users are reporting that they’ve lost up to 6 or more months of data. I know I should feel sorry for these people. If I were a better person, I would but all I can do is laugh. Because, you know what? They’ve been warned repeatedly. I’ve been preaching for years that only fools commit their only copy of important data to a single source—especially one that they don’t control. Of course, only a few—enlightened and discerning—folks read Irreal but any knowledgeable person will tell you the same. The concept is not esoteric.

There are plenty of reasons not to trust Google with your data. They’ve demonstrated over and over again a willingness to delete accounts for political or other reasons that infract some aspect of their world view. But the issue here is larger and extends beyond Google.

Everyone knows, you’d think, that large companies should be proactive in protecting their data by having several copies available in case of disaster but the same concept applies to the individual user. As an example, consider university researchers. At any given moment they’re probably working on some project that involves a load of data, notes, and the write up of intermediate results. Now imagine that all that data suddenly disappeared. It could certainly be career threatening and in some fields—medical research, say—life threatening. The point is, even the “little guy” has to worry about data security.

Given all that, why would you entrust your important data to a single source? It’s easy and not very expensive to secure your data even if you use Google Drive or something like it to share information. I pay $120 a year for completely transparent backup (Backblaze). I don’t have to do anything: it just automatically backs up my files as they change. For less than $100 you can get a portable USB drive to keep (yet another) local copy of your important files. If you’re not doing something like this, you’re courting disaster. Don’t do that. Don’t end up reading about yourself in The Register.

Posted in General | Tagged | Leave a comment

Lem: An Alternative To Emacs?

Fermin gave an interesting talk to the 2023 EmacsConf. The title of the talk was The Emacsen family, the design of an Emacs and the importance of Lisp. He starts with the premise that Lisp is the right language for Emacs implementation and extension.

That takes him to Elisp. It was, he says, a pretty nice language when Stallman designed it and today it’s still “good enough”. But that brings up the question, “Why not Common Lisp?” As we all know, RMS is not a fan but there’s more to it. When Emacs was being developed, a final specification was still 10 years away and there weren’t any (or at least any “free”) implementations available so RMS rolled his own.

But, Fermin says, we can do much better today with Common Lisp. In particular, the SBCL implementation is fast, compiles to native code, and is stable. That brings him to Lem. Lem is Emacs reimagined and implemented in Common Lisp.

The developers have made a conscious effort to make Lem familiar to Emacs users but also to make it more general and easier to extend. Take a look at the video or read to transcript for the details.

One thing I like about it is that it’s 100% Common Lisp. There’s no C core; just Lisp all the way down. That makes it easier to customize or extend any part of the editor.

It’s an interesting project but I doubt it will gain much traction among Emacsians. Using Common Lisp solves at lot of problems with Emacs but Emacs users are famously conservative about such things. Just consider the fury that a small change to how registers work provoked (see this reddit post and the Emacs-devel thread about it starting here).

In any event, it’s an interesting talk and worth watching. The video is 18 minutes 27 seconds so plan accordingly.

Posted in General | Tagged | Leave a comment

Bugs, Linux, and Open Source

Millions of pixels have been sacrificed exclaiming the glories and greatness of open source. Most everybody but Microsoft agrees and even Microsoft seems to have come around lately. At least on paper. There is, to be sure, a lot to say in its favor and most of what I’ve read against it appears more like special pleading than reasoned argument.

It’s surprising, then, to see an argument in its favor that I hadn’t seen before. A couple of years ago, koderski over at the Gamedev subreddit had an interesting observation. He noted that 38% of the bug reports on his game came from the Linux community. That might not seem too surprising except that Linux users are only 5.8% of the game’s user base.

You might think that those figures mean that the Linux version is buggy but when koderski looked at the data we found that only 3 out of the 400 bug reports from Linux users were platform specific. The rest were bugs that existed for all users. As koderski puts it, each Linux user gets you 650% more bug reports. He says that that’s like having a free QA department.

Koderski puts this down to Linux (and by extension, open source) users being trained to report bugs thanks to their involvement with open source, which depends on such user participation. Linus’s law, as formulated by Eric Raymond, famously asserts that given enough eyeballs, all bugs are shallow but this is a bit different. We don’t have fellow developers pursuing the code, just users who noticed that something was wrong and took the time to report it to the developer.

Posted in General | Tagged , | Leave a comment

Laas

As many of you know, I’ve long been fascinated by those who can write LaTeX at speed. By “at speed” I mean, for example, being able to keep up with a math professor’s lecture in the classroom. It seems almost impossible but guys like Gilles Castel showed that it could be done. Castel did his work in Vim but, of course, the same thing can be done in Emacs as Karthink demonstrates.

Castel depended on the Vim equivalent of yasnippet. Karthink also used yasnippet but his main tool was CDLaTeX. Recently, I came across a post that suggested that laas was much more efficient than CDLaTeX. I haven’t tried it but looking at the builtin shortcuts, it seems like it would be very useful for entering LaTeX at speed.

If you, too, would like to take LaTeX notes in your classes or other real time situations, you should definitely take a look at laas. And, of course, you absolutely must read Castel’s and Karthink’s posts.

My days in the classroom are long over but I was trained as a mathematician and if I were back in school today, I would definitely want to be able to take my notes in LaTeX. As I say, it seems like an impossible dream but Castel and Karthink showed that it’s possible.

Posted in General | Tagged | Leave a comment

Document Navigation

Charles Choi, as we know, likes to have things the way he likes to have them. Being an Emacs user, it’s easy for him to scratch many of those itches. In his latest post, he complains about the inconsistent navigation methods for various Emacs documentation commands. They all seem to behave differently and most of them don’t follow the standard Emacs navigation conventions.

He considers, Info, Help, Man, and Shortdoc. The worst, by far, is Info. The only thing I can think of that has a worse UI is the standalone Info utility, which I consider unusable. I never use Shortdoc so I won’t have much to say about it but Help and Man seem to me to be okay, if a little inconsistent.

Choi has his own ideas of what those interfaces should be and wrote some code to implement them. It’s easy to understand and adapt the code to your own preferences. My preference is that all these Emacs functions should follow the standard Emacs navigation conventions. Since these buffers are all read-only, I’d like n and p to behave like Ctrl+n and Ctrl+p.

Some of the documentation—Info in particular—have a complicated structure the needs additional commands to navigate them. For those, I don’t particularly care what they use as long as they’re at least semi-consistent among the different documentation types.

Choi’s ideas are different from mine so take a look and see what you think. One particularly nice idea is his paragraph movement commands. He uses highlight mode to mark the first line of the current paragraph. It’s a small thing but helps you keep your place.

As usual, Choi’s post is interesting and thought provoking. Take a look just to get some ideas as to what, if anything, you’d like to do about these commands.

Posted in General | Tagged | Leave a comment

Multilingual Editing

Protesilaos Stavrou (Prot) has an excellent short video that discusses how to use multilingual characters in Emacs. Prot, of course, is Greek and often wants to write Greek in Emacs. You can do that by switching keyboards at the system level but then—especially with languages like Greek—the Emacs keybindings break because the system is sending the wrong code to Emacs.

Happily, there’s another way: toggle-input-method bound to Ctrl+\ by default. This mode allows you to type with a non-English character set but still have Emacs perform correctly.

You can set a “default” input method that will become active when you type Ctrl+\ but if you add the universal prefix, Emacs will allow you to choose the input method you want. For example, I have my input method set to TeX so I can enter things like em dashes and various mathematical constructs but I’m currently learning Spanish so if I want to write in Spanish, I can toggle the input method to Spanish. Regardless of what input method I’m using, Emacs and its keybindings continue to operate.

Finally, there’s activate-transient-input-method, bound to Ctrl+x \, that allows you to switch to the default input method for the next character only. That’s nice for me when I simply want to add an em dash (—) but otherwise use my normal English input method. Prot uses it to add single Greek characters to his English text. As with toggle-input-method, adding the universal prefix will allow you to select the input method for the next character.

Prot’s video is 10 minutes, 59 seconds so it should be easy to fit it in. These are really useful commands even if you aren’t bilingual.

Posted in General | Tagged | Leave a comment

Literate Documentation with Emacs and Org Mode

Back in 2019, I wrote about Mike Hamrick’s excellent talk on using Emacs and Org mode for writing technical documents. I’ve mentioned it a couple times since and sometimes watch it again for some of the many great ideas embedded in it.

For the SeaGL 2023 conference, Hamrick updated his talk and showed how to produce a much more complicated document using Literate Document techniques. The goal was to produce a document showing how to build Emacs on a bare-bones Linux system. That means he has to worry about installing the necessary prerequisites, and getting everything configured for a successful Emacs build.

That sounds like a pain but is pretty straightforward. The wrinkle is that he wants to produce documents for both Red Hat and Debian based systems, which have different directory structures and utilities. He could, of course, write one and clone it making the necessary changes but we all know why that’s a terrible idea. Instead, he wrote a single document with code blocks that configure it for the required system.

In the end, he not only ends up with a nice looking document tailored to the target system, but the document includes code blocks that will actually do the install including downloading the prerequisites and making any necessary configuration changes.

The talk is pretty fast paced but he has a GitLab repo that contains everything from the talk including the final Org document so you needn’t stress about trying to take everything from the video. This is a really good talk and along with the 2019 version is something that everyone writing documents with Org mode should be familiar with. You should definitely invest the time to watch them both.

Posted in General | Tagged , | Leave a comment

Denote 2.2.0 Released

Good news for those of you who been following my posts [1, 2] on Protesilaos Stavrou’s Denote. The changes that I wrote about are now officially available in Version 2.2.0. The two main changes are the ones I wrote about—sorting in dynamic blocks and the denote dired buffer—but there are other significant enhancements as well.

One of those is a revamping of the rename functions to allow blank name components. You can read about these and the other changes in Prot’s announcement, which also links to some videos that describe and demonstrate these changes.

One of the really great things about this project is that Prot has taken pains to write a good manual and keep it up to date. As he says, the videos will eventually go out of date but the manual will always be the source of truth.

If you’re interested in a vaguely Zettelkasten-like note taking application, you should take a look at Denote. Its distinguishing feature is the systematic naming scheme that captures most of the significant metadata about a file in its name. That makes it easy to tell what the file’s about just from the name. If you want more information, take a look at the manual.

Posted in General | Tagged | Leave a comment

Marking Cut Regions

Just a quickie today. Tory Anderson has an interesting post illustrating something that I had no idea existed. You may—especially if you’re an old timer—be familiar with ’’cut marks“. They’re usually used to delimit code that an end user may want to cut out from longer text and use somewhere else. For example

Here is some introductory text that explains blah blah blah
--8<---------------cut here---------------start------------->8---
(defun do-things ()
  (do-something)
  (do-something-else))
--8<---------------cut here---------------end--------------->8---
Some more blathering.  

As Anderson says, they’re not used as much as they were in the past but they can still be useful. Emacs, of course, has a way to insert them automatically. All you need do is mark the region you want to cut and call message-mark-inserted-region to insert the cut marks.

Historically, they were used in emails so Emacs has the keybinding Ctrl+c Meta+m in message mode to insert them but you can insert them anywhere by marking the region and calling message-mark-inserted-region directly. Note that Ctrl+c Meta+m works perfectly well in mu4e and probably all the other Emacs-based mail clients. If you find yourself using it more than once a month outside of email, you can bind your own key sequence to it.

Posted in General | Tagged | Leave a comment