2018-03-03 pdf-annot-print-annotation-functions and Windows newlines

Some time ago I wrote about how I use Andreas Politz’s excellent pdf-tools to work with annotated pdfs. I still think this is great, but there was one thing that annoyed me a lot. Multiline annotations made on Windows (I guess) displayed with ^M in place of newlines. That is unfortunate, and I sometimes manually changed them to ^J (i.e., newlines). It dawned upon me, however, that being a programmer, I should be able to do something better about it. So I went to see what function is responsible for displaying annotations. I wasn’t sure whether I’ll advise or modify it (well, I had a slight hope for a hook…) – but what I found surpassed my expectations! It turns out that pdf-tools already has support for modifying annotations before display by running a series of functions on the annotation; the first one to “work” (look up the pdf-annot-print-annotation-functions variable to learn more) is used.

Also, there is one thing in the default value of this variable that really looks interesting – it checks (using a rather simplistic way, I admit) whether the annotation is written in LaTeX, and if yes, uses LaTeX to render it (you need dvipng installed for this to work). This way, you can even have formatting (including math formulæ) in your annotations!

So, the only thing I had to do was to write a function to replace my “returns” with proper newlines.

(defun pdf-annot-convert-return-to-newline (annot)
  "Convert ^M's in STRING to ^J's."
   "\r" "\n"
   (pdf-annot-get annot 'contents)
   t t))
(eval-after-load 'pdf-annot
  (lambda ()

Notice how I pass a lambda instead of a quoted form to eval-after-load – I have recently learned that trick from Chris Wellons. (If I didn’t do that, I’d effectively overwrite pdf-annot-print-annotation-functions.)

Also, this doesn’t affect the way the annotation is displayed after clicking on it. This makes sense, since this way of showing it is intended for editing, so you might want to see its exact value, without any modifications. My use-case, which is the list of annotations displayed with C-c C-a l (which displays both the list and the annotation the point is on), is covered, as is the tooltip.

It’s much better now!

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryTeX, CategoryLaTeX