Posts

  • Tracking World Time with Emacs

    In today’s highly connected world it’s often useful to keep track of time in several time zones. I work in a company with employees all over the world, so I probably keep track of more time zones than most people.

    So, what are the best ways to do this? I know what you’re thinking - let’s just buy an Omega Aqua Terra Worldtimer mechanical watch for $10,000 and be done with it!1 While this will definitely get the job done and improve the looks of your wrist immensely, there’s a cheaper and more practical option for you - Emacs. Did you know that Emacs has a command named world-clock that does exactly what we want?2 If you invoke it you’ll see something like this:

    Seattle   Monday 11 March 02:45 PDT
    New York  Monday 11 March 05:45 EDT
    London    Monday 11 March 09:45 GMT
    Paris     Monday 11 March 10:45 CET
    Bangalore Monday 11 March 15:15 IST
    Tokyo     Monday 11 March 18:45 JST
    

    Hmm, looks OK but the greatest city in the world (Sofia, Bulgaria) is missing from the list… That’s totally unacceptable! We can fix this by tweaking the variable world-clock-list:

    (setq world-clock-list
          '(("America/Los_Angeles" "Seattle")
            ("America/New_York" "New York")
            ("Europe/London" "London")
            ("Europe/Paris" "Paris")
            ("Europe/Sofia" "Sofia")
            ("Asia/Calcutta" "Bangalore")
            ("Asia/Tokyo" "Tokyo")))
    

    Let’s try M-x world-clock again now:

    Seattle      Monday 11 March 02:51 PDT
    New York     Monday 11 March 05:51 EDT
    London       Monday 11 March 09:51 GMT
    Paris        Monday 11 March 10:51 CET
    Sofia        Monday 11 March 11:51 EET
    Bangalore    Monday 11 March 15:21 IST
    Tokyo        Monday 11 March 18:51 JST
    

    Much better!

    By the way, you don’t really have to edit world-clock-list, as by default it’s configured to mirror the value of zoneinfo-style-world-list. The choice is yours.

    You can also configure the way the world time entries are displayed using world-clock-time-format. Let’s switch to a style with shorter day and month names:

    (setq world-clock-time-format "%a %d %b %R %Z")
    

    This will result in:

    Seattle      Mon 11 Mar 06:06 PDT
    New York     Mon 11 Mar 09:06 EDT
    London       Mon 11 Mar 13:06 GMT
    Paris        Mon 11 Mar 14:06 CET
    Sofia        Mon 11 Mar 15:06 EET
    Bangalore    Mon 11 Mar 18:36 IST
    Tokyo        Mon 11 Mar 22:06 JST
    

    Check out the docstring of format-time-string (C-h f format-time-string) for more details, as the options here are numerous.

    That’s all I have for you today. I hope you learned something useful. Keep hacking!

    1. Mechanical watches are another passion of mine. 

    2. It was named display-time-world before Emacs 28.1. The command was originally introduced in Emacs 23.1. 

  • Run Buffer-specific Commands with M-X

    No Emacs keybinding is more iconic than M-x (execute-extented-command) - it allows you to run any Emacs command from the minibuffer and it sounds a lot like Emacs!1 It doesn’t get better than this… or does it?

    Emacs 29 introduced a new keybinding that’s aiming to give M-x a run for its money - namely M-X (execute-extended-command-for-buffer). It’s described like this:

    Query user for a command relevant for the current mode, and then execute it. This is like ‘execute-extended-command’, but it limits the completions to commands that are particularly relevant to the current buffer. This includes commands that have been marked as being specially designed for the current major mode (and enabled minor modes), as well as commands bound in the active local key maps.

    So, it basically narrows the list of commands that you can execute to those that make the most sense in the context of the current buffer (mostly commands coming from the current major mode and enabled minor modes). I can easily see it becoming more useful than M-x when exploring some functionality that you’re not familiar with, as the results you’d get are narrower and you’d be more likely to find what you’re looking for.

    That’s all I have for you today. On February 29th we covered a small new feature in Emacs 29. That makes me feel good! Keep hacking!

    1. Try pronouncing M-x out loud. 

  • Changing The Emacs Configuration Directory

    I’ve noticed recently that I’ve missed one small, but very handy addition to Emacs 29 - the --init-dir command-line options. According the release notes:

    Emacs now supports setting ‘user-emacs-directory’ via ‘–init-directory’. Use the ‘–init-directory’ command-line option to set ‘user-emacs-directory’.

    Basically, this allows you to instruct Emacs where to read its configuration from. By default that’s .emacs.d, but with this option you can easily override the default. That’s extremely handy when you want to try out different Emacs setups. Here’s some example:

    # Use Emacs Prelude
    $ emacs --init-dir ~/emacs-distros/prelude
    
    # Use Spacemacs
    $ emacs --init-dir ~/emacs-distros/spacemacs
    

    I’m guessing this command-line options will be enough for most people who casually switch between different configurations and will reduce the need for them to rely on more sophisticated tools like chemacs or with-emacs.

    Admittedly, I never used any such tools and I’d just symlink different dirs to .emacs.d, so --init-dir will definitely improve my workflow a bit.

    That’s all I have for you today! Keep hacking!

  • Replacing dash.el with Built-in Emacs APIs

    dash.el has long been a staple in the Emacs community and I give it a lot of credit for driving some upstream API progress. By showing how many people wanted to use dash.el for various reasons (e.g. macros like -if-let and -when-let and numerous functions that transform sequences), a strong case could be made with Emacs’s maintainers that core Emacs Lisp APIs needed to be extended. Eventually that lead to the creation of subr-x.el1 (bundled with Emacs since 24.3) and seq.el (bundled with Emacs since 26.1). The two packages mostly obsolete dash.el and I guess this contributed to it being used less and less these days.

    I recently took over the maintenance of the popular Flycheck package and I’ve noticed that it was still using dash.el, despite targeting Emacs 26.1. As I plan to submit Flycheck to the official NonGNU ELPA package repository, it couldn’t have dependencies that are not present on GNU ELPA and NonGNU ELPA and I needed to remove dash.el. Doing so turned out to be trivial, as there were obvious built-in alternatives for everything Flycheck was using:

    • I replaced -if-let(*) and -when-let(*) with if-let(*) and when-let(*) from subr-x.el.
    • I replaced -any? and -first with seq-find, -all? with seq-every-p and -take with seq-take.

    And that was it! The whole process literally took me 10 minutes, most of them spent checking whether seq-find is the right replacement for -any? and -first.

    One small caveat to keep in mind, when depending on the built-in Emacs packages like seq.el, is that you should typically stick to whatever version of the package was bundled with the version of Emacs that you target (in my case Emacs 26.1 bundles seq.el 2.20), otherwise you’ll need to declare an explicit package dependency on the newer version. Keep in mind, however, this might cause some issues in packages that are depending on your package, but not declaring the same dependencies.2 Transitive dependencies in Emacs are a messy topic…

    So, all it all it was a very quick and easy process and I can totally recommend to other packages that are still relying only on basic dash.el functionality to replace it as well. Now Flycheck is totally dependency-free and it’s ready for submission to NonGNU ELPA!

    1. Originally authored by yours truly. 

    2. See https://github.com/flycheck/flycheck/issues/2054

  • Lookup the Documentation of Functions, Variables and Faces

    Looking up the documentation for some command/function or configuration option/variable is something all Emacs users have to do quite often. Two of the Emacs help commands that I use most often are describe-function (C-h f) and describe-variable (C-h v). Basically they display the documentation for some function or variable. E.g. if you press C-h f and type afterwards describe-function we’d get the following:

    describe-function is an autoloaded interactive compiled Lisp function in
    ‘help-fns.el’.
    
    It is bound to C-h f, <f1> f, <help> f, <menu-bar> <help-menu> <describe>
    <describe-function>.
    
    (describe-function FUNCTION)
    
    Display the full documentation of FUNCTION (a symbol).
    When called from Lisp, FUNCTION may also be a function object.
    
    See the ‘help-enable-symbol-autoload’ variable for special
    handling of autoloaded functions.
    
    Probably introduced at or before Emacs version 22.1.
    

    describe-variable is also useful to check the current value of some variable. Here’s an example:

    clojure-indent-keyword-style is a variable defined in ‘clojure-mode.el’.
    
    Its value is ‘always-align’
    

    I’m guessing most Emacs users are quite familiar with both commands. What I didn’t know until recently, though, is that Emacs 25.1 introduced the related command describe-symbol (C-h o), which works for both functions and variables. I’m guessing most people would benefit from using it over the more specific commands, as it reduces some mental overhead. (the fewer keybindings we have to remember, the better)

    Bonus points - describe-symbol also works with faces, so it’s a good replacement for describe-face (which doesn’t have a default keybinding). One command to rule them all!

    That’s all I have for you today. Keep hacking!

Subscribe via RSS | View Older Posts