Checking For A Leap Year

Wait! What? I’ve been programming most of my life and have a PhD in Mathematics but I’ve never seen this before. It’s about checking for a leap year. The first thing we all learn is that it’s a leap year if it’s divisible by 4 but that’s not quite right. It’s a bit more complicated. The usual check is something like

int is_a_leap_year( int y )
{
    if ( ( y % 4 ) != 0 ) return 0;
    if ( ( y % 100 ) != 0 ) return 1;
    if ( ( y % 400 ) == 0 ) return 1;
    return 0;
}    

Code like that has been written thousands (millions?) of times in just about every language there is.

What to make of this, then?

int is_a_leap_year( int y )
{
    return ( ( y * 1073750999 ) & 3221352463 ) <= 126976;
}

According to the link, you can make this check in 3 CPU instructions. It seems like magic but there’s a pretty good explanation of how it works at the link. Oddly enough, it’s a pretty direct translation of the normal check. Again, see the link for the details.

Checking for a leap year is not something you usually do in a loop—although it’s easy enough to imagine cases where you would—so it’s probably not a worthwhile project to replace your leap year checks with this code. Nonetheless, you have to love it if only as a curiosity.

Of course, if you’re one of those people who obsess about squeezing every cycle out of your code—I was like that when I was younger—this may seem like gold to you. If nothing else, it will confound and amaze your colleagues.

Posted in Programming | Tagged | Leave a comment

A Garbage Collection Strategy

One of the strengths—or weaknesses, depending on your point of view—of Lisp is garbage collection. That means that memory no longer in use is automatically collected and made available to the system without depending on the programmer to release it the way we do in, say, C. Of course, that comes at a cost. Garbage collection can block other operations while it’s running. That’s especially true in the single threaded Emacs.

One often sees complaints about this in the various forums. There’s all sort of advice about what to do. Some say you should set the garbage collection threshold (the amount of collectible memory) high so that garbage collection isn’t triggered as often. Of course, that means that it takes longer to collect the unused memory.

Another suggestion is to set the threshold low so that unused memory can be collected quickly. That means, though, that garbage collection is run more often. Depending on your workflow, either—or possibly both—of these strategies may work well for you.

There’s a third, arguably better, strategy: only garbage collect when the system is idle. That’s facile advice, of course, because it begs the question of how you know when the system is idle. There’s a useful heuristic. If the system has been idle for x seconds it will probably continue being idle long enough for a garbage collection cycle. The question then boils down to the proper value for x.

Jack Jamison believes x should be small and uses 1.2 seconds in his implementation of this strategy. He says that he’s had excellent results with that value. The older GCMH system uses 15 seconds.

I’ve been using GCMH was several years and can’t remember having any garbage collection issues since I started. GCHH lets you configure the idle time but the 15 second setting has been working well for me so I’m not inclined to mess with it.

In any event, if your Emacs is experiencing garbage collection delays it’s worth trying this strategy. If you want a simple prepackaged solution, GCMH is for you. If you want to keep everything in your init.el and avoid external packages, take a look at Jamison’s post to get started.

Posted in General | Tagged | Leave a comment

Unsetting Keybindings

Back before I became an Emacser, a friend who was an Emacser said that I shouldn’t worry about the Emacs keybindings because I could configure them to be whatever I want. I remember thinking that that would be disaster. After all, what would I do if I were using Emacs on someone else’s computer or even setting up a new computer.

Now, of course, I have a very individualistic configuration that makes working on someone else’s computer uncomfortable. Uncomfortable but not impossible. That’s because although I have all sorts of specialized editing commands, I’ve left the “vanilla” editing commands—cursor movements, file visiting and saving, and other standard operations—alone. That means that if I use someone else’s Emacs I can edit successfully, if not optimally, as long as the guest Emacs also didn’t mess with the standard bindings.

That’s why I’m not entirely sympathetic to The MKat’s post on how to unset default key bindings. She’s got a lot of great advice on how to delete keybindings and I agree that it’s useful information to have. My objection is to the notion that you should delete bindings that you don’t like.

Just yesterday, I discussed how modern keyboards aren’t optmal for Emacs so it’s natural to want to fix that somehow. Yesterday’s post suggests some ways of doing that but none of them involve changing default keybindings.

Sure, some folks have special needs that make one binding or another difficult but in general, I think it’s a bad idea to be messing with them. Of course, this is Emacs and everyone can adjust it to their liking. I’m not arguing with that. I’m only saying that if you stay with the defaults it will be easier for you to use an alien Emacs.

Posted in General | Tagged | Leave a comment

The Best Emacs Keyboards

Over at the Emacs subreddit, surveypoodle asks a perennial question: what are the best keyboards for Emacs? He points out that Emacs pinky seems to have appeared rather late in the editor’s history and speculates that that’s because the keyboards originally used with Emacs were better suited for it than modern keyboards that make many of the bindings awkward. He’s asking what modern keyboards are best for dealing with RSI.

If you’re interested in an answer to the question, see the comments to surveypoodle’s post. There are all sorts of suggestions. Reading through them, I realized that I didn’t care.

I used to yearn for a reincarnation of the Space Cadet keyboard—okay, I still have some vestigial urges—but I realized that these days I do virtually all my work on my laptop. I’m guessing the same is true many of us. Regardless, the use of a laptop pretty much makes the choice of a keyboard for you. Sure, you can use a third party keyboard but unless you’re always working at (the same) desk it’s just too much trouble. As I’ve said before, I do a lot of my work on the couch so an external keyboard isn’t a practical choice for me.

What to do? As much as it has become a cliche, Emacs pinky—or more generally Emacs induced RSI—is a real thing.The best solution I’ve found is some simple keyboard modifications. I’m using a MacBook Pro so keyboard firmware modifications aren’t an option. What I can do, though, is arrange for the modifier keys I use the most to be easy to reach. Two of those, Ctrl and Hyper are, configurable from within Emacs or macOS. The Meta key is bound to Alt by default so there’s nothing to do there. What all this means is that binding Ctrl to caps lock and Hyper to ⌘ Right Cmd, I can use my thumbs for Meta and Hyper and that Ctrl is an easy reach for my pinky.

I know that a lot of you are doing the same or similar things. If you’ve got a better solution, leave a comment.

Posted in General | Tagged | Leave a comment

F1 Pit Stops And Pediatic Heart Surgeons

Synergy. It’s an amazing thing. We usually view it a narrow context: synergy among members of a development team, for example but here’s a story about synergy between two very diverse groups.

The first group is a team performing pediatric heart surgery. It turns out that the journey from the OR to the ICU was itself dangerous despite the best efforts of the surgery team. One day after a two-day sprint of operations the surgeons collapsed exhausted in front of a TV and watched a Formula 1 race. One of the surgeons, Martin Elliott, was struck by the precision and teamwork of the pit crews.

That precision is something to behold. I wrote about it 11 years ago. Watch the video and be amazed. A car comes in and the car goes out two seconds later with four new tires. The action is so fast that it’s hard to follow.

Elliot wondered if those pit crews might have something to teach his team so he approached Ferrari. The Ferrari team tried to be polite but basically laughed at the hospital’s procedures. They were doing everything wrong. One example from the story is that if a critical wire came loose while transporting the baby to the ICU, the whole team would react and try to fix the problem. In contrast, each member of a pit stop crew has a well defined area of responsibility and doesn’t concern himself with anything else. The team members trust the responsible person to take care of any problem in his purview.

Together, the two teams were able to refine the surgical team’s procedures to greatly reduce the number of serious problems. Since then other hospitals have also adopted those procedures. Apparently, they always approach Ferrari skeptically but come away convinced. Read the story. It’s really interesting and inspiring. And if you haven’t seen it, watch that video of a pit crew at work. You’ll be awestruck.

Posted in General | Tagged , | Leave a comment

Window Placement When Using Dired

Courtesy of JTR from The Art Of Not Asking Why, here’s a handy tip for dealing with window placement when bringing up a Dired buffer. There are two problems.

The first doesn’t really concern window placement. Most often JTR wants to bring up a Dired buffer for whatever directory he’s already in. The easy way to do that is to use dired-jump (bound to Ctrl+x Ctrl+j) to open a buffer in the same directory as the current file.

The problem with that is that it opens the Dired buffer in the current window. JTR doesn’t want that. He wants to open Dired in some other window so that he can see his current file and the Dired buffer. The answer to that is simple: just ask Emacs to open Dired in another window. There’s a general protocol for doing that. Simply use Ctrl+x 4 Ctrl+J to call Dired. That will open Dired in the same directory as the current file but in another window.

The Ctrl+x 4 … works for a lot of commands. I use it to open files or buffers in another window several times a day. I used to consider it an esoteric maneuver but now I use it all the time. You can also open your windows in another frame by using 5 instead of 4.

The 4/5 protocol doesn’t work for all commands but it does for a lot of them. It’s a real time saver that I use repeatedly. It’s well worth internalizing.

Posted in General | Tagged | Leave a comment

Hiding Buffers

Alec Barreto over at The Wumpus Warehouse has an interesting post on hiding buffers. The idea is that there are some buffers that you just don’t care about and never want to see. There are others that you might want to see but only rarely.

Barreto shows how to deal with both these cases. The first is easy. Just rename the buffer to begin with a space. Most (or all?) of the normal buffer switching commands will ignore such a buffer. The only way of accessing these buffers, says Barreto, is to specify its name exactly in a switch buffer command.

I’m not sure why anyone would want to do this. If you really don’t want to see the buffer, just kill it. Hiding it like this just accumulates a bunch of buffers that you don’t need and don’t want. You can see how this can get out of hand if you’re one of those people who restarts monthly or less.

The part dealing with buffers you only want to see occasionally is more interesting. If you’re like me, you’ll learn something new. It turns out that many—but not all—of the buffer changing commands can be asked to call a function to determine if that buffer should be shown or not. Since you can make that function do anything you like, you very fine control over what buffers are shown. Take a look at Barreto’s post for some examples.

When I read Barreto’s post I wondered why it didn’t resonate with me. I’m always doing things that generate buffers I don’t care about. Sometimes I delete them and sometimes they disappear into my buffer list to get reaped on the next reboot. I realized that the reason this works for me is because I use ivy-switch-buffer to switch buffers and that provides me with a vertical list to pick the buffer I want to switch to. It also offers fuzzy search so I never search down the buffer list. I think the list is ordered by recent use so the buffer I want is almost always near the top of the list.

Of course, that’s my workflow. Yours probably differs and Barreto’s methods may make more sense for you. Regardless, Barreto’s post will teach you about switch-to-prev-buffer-skip in case, like me, you didn’t already know about it.

Posted in General | Leave a comment

Loading The Emacs Init File

Charles Choi makes an astute observation about the Emacs initialization file from the point of view of the new user: there’s no obvious way of loading it or even knowing that it exists. The problem is especially acute on the Mac where the File → Open menu uses the Mac file loading routines that don’t show dot files. Not very helpful even if you know the name of the file.

Choi is surprised that this far on the problem hasn’t been fixed. It’s hard to argue. On the other hand, I don’t remember having this problem. I have no memory at all of how I overcame it but I and thousands of others have. That doesn’t mean that the problem isn’t real, though.

Choi offers a solution. It’s a simple function that loads the init file even if it hasn’t been created yet. His idea is that the function would be builtin and automatically available to the new user.

Of course, the problem is how does the new user discover this function, or for that matter, what to do with the init file once it’s loaded. It seems to me that at the very least there needs to be some instructions on the startup screen—you know, that screen we all turn off as soon as we learn how. That would help but it’s probably not enough.

What’s really needed is a new-user file that explains how to get started. It could be linked from the startup screen. It wouldn’t need to be long or complicated; just enough to bootstrap the new users and point them to the tutorial.

One thing for sure: Choi is right. It’s really harder than it should be for a new user to learn enough to get up to speed with Emacs.

Posted in General | Tagged | Leave a comment

Toggling Narrowing

Over at the Emacs subreddit, sebhoagie has a short post that some of you may find useful. Sebhoagie really likes narrowing and uses it all the time. Sometimes, though, the narrowed code refers to code outside the narrowed region and he occasionally wants to reference it and then return to the narrowed region.

That’s not a big problem of course. Just widen, search for the referenced code, highlight the original narrowed code, and renarrow. Of course there’s a bit of friction and if you need to do it often it can become a burden.

Sebhoagie has a writeup that explains his solution. The TL;DR is that he wrote a function that narrows to a region but when you widen, it remembers the narrowed region. If you narrow again (using his function) it will renarrow to the previous region and forget the saved region parameters. If you think it through, that does exactly what you want. When you’re finished with the narrowed region you simply widen in the normal way (with widen) and everything is reset.

My solution is to use Artur Malabarba’s narrow-or-widen-dwim that almost always does exactly what I want. Take a look at Malabarba’s post to see how it works. If you’re not using it, you’re definitely working too hard. Still, you may find sebhoagie’s useful. If you have a similar need, his code is a nice companion to Malabarba’s.

Narrowing really can be a superpower as sebhoagie says and it’s worthwhile making its use as easy as possible.

Posted in General | Tagged | Leave a comment

The Apple Decision

By now, everyone knows about Judge Yvonne Gonzalez Rogers’ decision that Apple can no longer refuse to let apps link to payment methods outside of the iOS app system. The idea was that if you ordered anything through an iOS app, Apple would get a percentage of the sale. Everybody but Apple hated the policy.

I’m conflicted on the matter. My inclination is to side with Apple. After all, they built the iOS app platform—without which, by the way, those complaining wouldn’t even exist—and they should get to set the rules. I think it’s a huge stretch to claim that setting those rules is somehow an antitrust issue. Others obviously disagree.

I don’t write iOS apps so my perspective is that of a user. One aspect of that use is the iOS Kindle app. Often, when I finish reading a book, there’s a recommendation for the next book in the series. If I liked the book, I will usually order the next book to read later. It’s not a seamless process. I have to leave the Kindle app, go to the browser, bring up the Amazon site, search for the book, and, finally, order it.

Now, happily, that has changed. The Kindle app now has a button that allows you to go to the book’s Amazon page and order it right from the app. I’m really happy about that but I can’t help thinking that it’s not right. It’s almost as if I’m abetting theft from Apple. As I said, Apple built the platform and others came along and claimed rights to it.

I get that app developers are living on the edge and I do think that Apple can afford to and should let them suggest other payment options. And really, most of us are going to take the easy option and order through the app anyway. Still, it’s Apple’s platform and they should get to set the rules. If you don’t like the rules, build your own platform. If you don’t think that’s possible, ask Steve Jobs or Jeff Besos.

Posted in General | Tagged | Leave a comment