Zamansky 20: Yanking and Auto Revert

Mike Zamansky has another video up in his Using Emacs Series. This time he provides a video demonstration of counsel-yank-pop that I wrote about previously here on Irreal. As always, things seem clearer and easier to understand when you see it actually happen.

The other thing that Zamansky covers is auto-revert-mode. It checks if the file you are looking at in Emacs has changed on disk and reloads it if so. If you’re working with a single machine that doesn’t seem very useful or even to make sense1 but it’s really useful when you work on more than a single computer.

To see how it can really help your workflow, let’s assume you work on two separates machines—one at work, one at home, say. In order to keep files synced, you use something like Dropbox. Then when you save a file at work, it also updates your machine at home. The problem occurs when you already have the file open in Emacs on your home machine. Now the on-disk copy and the in-buffer copies differ and it’s easy to get a conflict. This is the exact situation auto-revert-mode fixes. When Emacs sees the file change on disk, it automatically reloads the buffer so that when you come back to your home machine you’ll be working with an up-to-date copy of the file.

Footnotes:

1

It can even be useful on a single machine if you have an external process that occasionally updates a file you’re examining in Emacs—a system log file, for example.

Posted in General | Tagged | 1 Comment

Zamansky 19: Conditional Loading of Emacs Config

Mike Zamansky has another video in his Using Emacs Series. This video addresses a problem he has while making the videos. A theme of the series is building an Emacs configuration. Each video considers 1 or 2 packages or features and how to configure and use them. The problem is he needs his entire configuration to get his other work done and it’s a pain to move between the partial and complete configurations.

Zamansky solves this problem by conditionally loading the rest of his configuration. Thus, he can put the partial configuration on GitHub for anyone who’s following along to use but when he uses it for his real work, the rest of his configuration will be loaded. The only wrinkle is what to do if the rest of the configuration is not present as will be the case when those following along at home use his config. His solution is to wrap the loading of the rest of his configuration in an if statement that tests if the file is present:

(defun load-if-exists (f)
  (if (file-readable-p f)
      (load-file f)))

Then he can conditionally load other files with

(load-if-exists "some-file.el")

This has the additional benefit of introducing a bit of Elisp, something that every serious Emacs user will eventually have to get comfortable with.

Most of us won’t have this problem but it does show how to conditionally load files, something that’s useful for us all. Here, for example, is how I load OS and machine specific configuration items

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pull in system and platform specific configurations                    ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Just keep on going if the requisite file isn't there.
;; Manipulations in second load is for "gnu/linux" --> "linux"

(load (car (split-string (system-name) "\\.")) t)
(load (car (reverse (split-string (symbol-name system-type) "/"))) t)

Using load with a second argument of t is functionally equivalent to Zamansky’s load-if-exists.

This is a short video so there’s no reason not to watch it. Even if you’re an experienced Emacser, it’s interesting to see how others solve common problems.

UPDATE [2016-11-12 Sat 12:16]: loadload-file

Posted in General | Tagged | 2 Comments

Org Mode and Python

If you’re a Python user and like to put Python snippets in source blocks in an Org mode file, John Kitchin has some very nice enhancements for you. Kitchin, as Irreal readers surely know by now, is a Chemical Engineering researcher at Carnegie Mellon. He does a huge amount of his work with Org and frequently embeds Python code in Org buffers. Some of this Python is long-running and therefore freezes Emacs because Babel runs the code synchronously.

Part of Kitchin’s enhancement fixes that by arranging for the code to run asynchronously so that you can do other work while the code is running. That’s actually something he figured out a while ago. What’s new is better error handling and a way to look at the results produced so far. His students complained that it’s hard to find the offending line from a stack trace in case of an error. Kitchin’s latest fix solves this by arranging for temporary line numbers to be put in the source block and for the cursor to go to the line that raised the error when it occurs. He also provides a link that you can click on to kill the running code if you need to.

Finally, he wraps autopep8 and pylint is emacs functions so that you can easily reformat code and check for syntax violations before you run it.

All of this is part of his Scimax distribution but this new code has not yet been merged into the master branch because he doesn’t want to change things mid-semester. He plans on merging it in Decemeber but gives a link to the experimental branch if you want to try it out now.

Posted in General | Tagged , | Leave a comment

isearch Tutorial

Bastien Guerry has a nice tutorial on isearch available on his M-x doctor RET site. Unless you’re really an expert on isearch, there’s a lot you don’t know about it. You can, for example, search for the word or character at point. You can ignore multiple spaces so that they get treated the same as one space.

There are many more shortcuts that you probably didn’t know about. Rather than list them all, I’ll just list these items that Guerry says he wishes he had known about sooner:

Ctrl+s Meta+e Edit search string
Ctrl+s Ctrl+w Search for word at point
Ctrl+s Meta+r Toggle regex search
Ctrl+s Meta+s o Run occur on the search string
Ctrl+s Ctrl+Meta+i Complete the current search string

There’s lots more so be sure to take a look at Guerry’s post.

UPDATE [2016-11-10 Thu 19:51]: Fixed key sequence for search word at point.

Posted in General | Tagged | 3 Comments

Better Kill Ring Manipulation

Yesterday, I wrote about Ben Maughan’s tip for maintaining the clipboard over an Emacs kill. If you read Maughan’s post you’ll see that there’s a link to a post on cycling through your Emacs kill ring. I remember seeing this before but it didn’t register. After reading his post on the clipboard, I reread the post on the kill ring and realized that it resolved a long standing irritant for me.

We all know about yank-pop to yank older items on the kill ring but I always felt as if I were flying blind because you can’t see what you’re going to get until you run the command. There was also the problem that if you wanted an old entry you had to first do a normal yank before you could step back through the older entries. Abo-abo’s counsel-yank-pop solves both these problems.

If you bind counsel-yank-pop to Meta+y, you can bring up a menu containing the kill-ring entries and just choose whichever one you want in the usual Ivy way. Filtering and scrolling work just as they always do with Ivy. Maughan’s post describes a small tweak that allows you to repeatedly type Meta+y, to move back in the list. I’m perfectly happy to scroll with Ctrl+n and Ctrl+p so I didn’t bother with that.

This is the second of two packages that I’ve written about recently that transform the kill ring from being annoying to use to simple and enjoyable. The first is undo-tree and the second is counsel-yank-pop (well, really ivy and counsel). If you don’t have them installed, you really should consider it. They will, I promise you, make your life simpler.

Posted in General | Tagged | 8 Comments

Maintaining the System Clipboard Over an Emacs Kill

Ben Maughan has a nice tip for Clipboard/Emacs Kill Ring interaction. In recent versions of Emacs at least, you can yank the value of the system clipboard as long as nothing has been put in the kill ring since the time that the value in the clipboard was set. That works well and I often use it to copy some text from, say, a Web page and then yank it into an Emacs buffer.

The problem is that if you kill something in Emacs in the meantime, the clipboard value is lost because it’s not actually added to the kill ring. Maughan gives us a one-line fix that causes the clipboard to be pushed onto the kill ring if necessary. That means that you can still retrieve it even if there’s an intervening kill. That’s really handy. I often copy text from the browser, return to Emacs, do some editing that involves a kill, and then end up having to copy the text from the browser again. With this fix, it’s still in the kill ring and retrievable. Very nice. I wish I’d known about this sooner.

Posted in General | Tagged | Leave a comment

Exporting JavaScript from a Babel Source Block

One of the nice things about Org mode is that “You’ll never have to write HTML again.” That’s not quite true, of course, but it mostly is. Just write in Org markdown, including tables, and then export it to HTML all from the comfort of Emacs. That’s how Irreal gets published everyday.

If you know a bit about writing non-trivial Web pages, you may want to embed some JavaScript in the final HTML. I do this all the time when I embed a tweet in a post. When I tell Twitter that I want to embed a tweet, it gives me the code (surrounded by <script>, </script>) that I include in the post by simply putting it between #+BEGIN_EXPORT HTML and #+END_EXPORT tags in my Org source. That’s easy because Twitter provides all the code I need.

But what if you want to add your own JavaScript? That’s pretty easy too. You just type <h Tab to get the export tags and then Ctrl+' to be put in a buffer where you can write your JavaScript. That’s fine but there are a couple of problems. First the editing buffer you get put in is in HTML mode not JavaScript mode so you don’t get the syntax highlighting and the other benefits of JavaScript mode. Secondly you have to add all the <script>, </script> boilerplate by hand. Finally, it’s not how you embed source for other languages in an Org document so it’s a special case.

Over at the Emacs reddit, oantolin provides a really nice fix for this. With just a few lines of Elisp he adds JavaScript to the languages that Babel knows about. Now when you want to embed some JavaScript, you just put it in a Babel source block as you would with any other supported language. When you type Ctrl+' you get put in JavaScript mode and the boilerplate will be inserted for you when you execute the block.

What’s really amazing about all this is how easy it is; take a look and see. I hope the Org folks add this to the code base so it will be available without adding the Elisp to your init.el. Even if they don’t, it’s such a minimal amount of code that it’s not much of a burden to add it yourself.

Posted in General | Tagged , | Leave a comment

Loyalty Cards

In yet another case of the Iron Law of Data Collection, we have this example from Seattle. For the last four years, King County Animal Services has been sending notices to residents who have unlicensed pets that they must license those pets or face a $250 fine. But how did they know who had unlicensed pets?

It turns out they gathered data from supermarket loyalty cards—those cards that the cashiers are always asking if you have so they can offer you a discount—to see who was buying pet food and then cross referenced that with pet license data. Last year the county brought in $100,000 of new licenses this way.

Once again, we see data that was collected for an ostensibly benign reason being used for unintended purposes in a privacy violating way. There are no terrorists here, no pedophiles, no drugs, just dogs. While you can make a case that ensuring pets are properly vaccinated serves a public good, it’s the most mundane of public goods and one that certainly doesn’t rise to a level that requires the government to spy on what citizens are buying at the grocery store.

Sadly, most residents aren’t that concerned. They agree it’s pretty creepy but just shrug it away. One resident, in a charming bit of naivety said, “I just don’t get that concerned. Now if they were looking at my bank records and all sorts of more personal stuff than my dog, then maybe I might be.” Why on earth would you think that a government that spies on what you’re purchasing for pet enforcement reasons isn’t looking at your other data for more “serious” violations?

In any event, you shouldn’t be using those cards. Their whole purpose is to spy on your purchasing habits. That’s nominally for the benefit of the supermarket but as we see here, the data can be co-opted by the government for its own use. In my experience the cashiers usually have a card that they will swipe for you anyway so you won’t even lose out on any benefits from the program.

Posted in General | Tagged | 1 Comment

Org 9 Links

John Kitchin has a very nice post and video demonstrating some of the new link features in Org Mode 9. It’s now easy to create custom link types and control the display, look, and action of the resulting links. You can, for example, choose the color you want for the link or what gets displayed in the tool tip, even making it dynamic.

As an example, Kitchin demonstrates a better file link that asks you what you want to do when you follow it. You can, for example, open it as usual, open it in Dired, copy the path, or any other action that makes sense.

A link’s look and action are controlled by a set of properties, which can be individually set for each link type. Here’s a list of the properties cut from org.el.

:follow - A function that takes the link path as an argument.

:export - A function that takes the link path, description and
export-backend as arguments.

:store - A function responsible for storing the link.  See the
function `org-store-link-functions'.

:complete - A function that inserts a link with completion.  The
function takes one optional prefix arg.

:face - A face for the link, or a function that returns a face.
The function takes one argument which is the link path.  The
default face is `org-link'.

:mouse-face - The mouse-face. The default is `highlight'.

:display - `full' will not fold the link in descriptive
display.  Default is `org-link'.

:help-echo - A string or function that takes (window object position)
as arguments and returns a string.

:keymap - A keymap that is active on the link.  The default is
`org-mouse-map'.

:htmlize-link - A function for the htmlize-link.  Defaults
to (list :uri \"type:path\")

:activate-func - A function to run at the end of font-lock
activation.  The function must accept (link-start link-end path bracketp)

There isn’t very much documentation on how to use this new facility so Kitchin’s post is really useful if you want to take advantage of link properties. Have a look at Kitchin’s video to get an idea of what’s possible. It’s 10 minutes long so it won’t be hard to schedule some time to watch.

Posted in General | Tagged , | Leave a comment

Org 9.0

Org Mode version 9.0 has been released and is ready for update. Take a look at the Changes file to see what’s new. I’ve had it installed for less than an hour as I write this so it’s a little too early to draw any conclusions.

The only issue I’ve had to deal with is the new Export block syntax. That’s explained near the top of the Change file and basically involves writing things like

#+BEGIN_EXPORT html

#+END_EXPORT

instead of

#+BEGIN_HTML

#+END_HTML

The shortcuts (<h Tab in this case) will give you the proper syntax so you need worry only about fixing up old files that you want to export. Happily, the Change file includes a bit of Elisp that will do that for you. I just pasted it into my init.el file for the time being so that I can easily fix any files I need to. That would mostly be old blog posts that I want to post an update to but there are a couple of others that I’ll need to fix the next time I export them.

There are a few other non-backward compatible changes but they probably won’t affect many people. Be sure to look at beginning (at least) of the Changes file to make sure you aren’t affected.

Many thanks to Bastien and the other developers for all their hard work. Org keeps getting better and better and I wouldn’t be nearly as productive without it.

Posted in General | Tagged , | Leave a comment