2022-09-05 Comments in srt files

As I mentioned several times here, I do quite a lot of editing of srt subtitle files for videos. Sometimes I have problems when I translate subtitles into Polish, and then I usually ask native speakers about some obscure idiom or even more obscure (at least for me) cultural reference. In such cases, I figured I’d really like it if I could put comments into srt files to remember these places.

Well, it turns out that I can! Of course, IT being IT, I couldn’t find any “official” docs about it, but I found some thread on VLC forums about some bug connected to the comments; the thread was pretty old (more than 15 years!), so – quite expectedly – comments work in VLC now. (They also do work in MPV, which makes me pretty happy, as that is what subed-mode uses.) Thing is, the comment syntax is rather weird – to comment something out, you need to put it between ‌{\ and }‌.

Now the only thing I lacked was having Emacs understand the srt comment syntax. I have to admit that I’ve never dived into how Emacs implements comments – given the various possible syntaxes in various possible languages, the mechanism is pretty general and complicated. Fortunately, I had the good idea to search the internet first, and it turns out that there exists a very high-level way to define comment delimiters (note: that answer does not use setq-local):

(add-hook 'subed-srt-mode-hook
	  (lambda ()
	    (setq-local comment-start "{\\")
	    (setq-local comment-end "}")))

This has one drawback – while it makes comment commands like M-; use the newly defined delimiters, it does not show them using font-lock-comment-face. Fortunately, this is easy to remedy. Here is the secret magic spell to add to the lambda above:

(modify-syntax-entry ?\{ ". 1")
(modify-syntax-entry ?\\ ". 2")
(modify-syntax-entry ?\} ">")

What this does is change the syntax of all three characters in a suitable way, taking care to make ‌{ the first character in a two-character comment (much like / in C) and \ the second one (like * in C). (The closing curly brace is just a “comment ender”.) It still has one drawback – now the braces are no longer treated as delimiters. This is not a big issue, though – I doubt many people use curly braces in subtitles (I certainly don’t), and even if they do, moving or killing by sexps doesn’t seem that useful in srt files anyway.

By the way, I made a pull request to incorporate this change in subed-srt-mode, so if Sacha Chua (who maintains subed-mode) accepts it, comments in srt files will just work for everyone.

CategoryEnglish, CategoryBlog, CategoryEmacs