Content AND Presentation

2022-11-26 Extracting Youtube subtitles in Emacs

A few days ago I read about an amazing package. Like the OP, I very much dislike watching videos with lectures – text is much, much better way to communicate. (In fact, when I was a student, I preferred to skip lectures and learn from my colleagues’ notes instead. It was much more efficient.) I decided that I really need to try out youtube-sub-extractor.el.

It turned out that it’s very easy to do so. I installed the package and immediately tried it out. It turns out that it’s fairly simple to use – just call youtube-sub-extractor-extract-subs, give it a Youtube URL, and you get a buffer with the subtitles. From that buffer you can call two functions – youtube-sub-extractor-copy-ts-link to get a link with a timestamp to that particular moment in the video (kind of like what I did a few weeks ago), bound to RET, and youtube-sub-extractor-browse-ts-link to open the video at the corresponding moment, bound to C-c C-o. What’s especially nice is that the latter function uses browse-url under the hood, so it opens mpv from my Emacs.

Now this is definitely pretty cool, but there is one thing I’d like to add here. As I mentioned before, I sometimes have a YouTube link in my buffer, and killing it only to be able to yank it after I call youtube-sub-extractor-extract-subs is a nuisance. This is Emacs, so fixing this is a matter of seconds:

(require 'thingatpt)
(defun youtube-sub-extractor-extract-subs-at-point ()
  "Extract subtitles from a YouTube link at point."
  (youtube-sub-extractor-extract-subs (thing-at-point-url-at-point)))

And that’s it for today!

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

2022-11-19 Streamlining my workflow with Magit and BitBucket

We use BitBucket at our company, which is some source of frustration for us. One of the issues we have with BitBucket is that it seems to lack a decent CLI tool. This means that in order to e.g. create a pull request, you go to the website, click a few times and only then confirm that a PR is really what you want.

Well, after some time I learned that it’s not that bad. When you git push your changes, Git (on the command line) responds with the URL you need to go to create a pull request out of the branch you just pushed. (This is most probably achieved via post-receive or some other Git hook.) Nice.

Well, of course I don’t use Git from the command line – I use Magit. So naturally I wanted Emacs to extract that information for me and open the URL in the browser. It turns out that it was easier than I thought. Using the famous Emacs self-documenting capabilities, I found two commands: magit-section-backward, and, most importantly, magit-process-buffer. (Go check their docstrings to learn what they do, or perhaps confirm your guess.) So, I hacked up a pretty simple function to grab the data from the Magit process buffer and open the PR page in my browser.

(defun magit-open-pull-request-pr ()
  "Open the pull request URL if applicable."
    (set-buffer (magit-process-buffer t))
    (goto-char (point-max))
        (search-backward-regexp "remote: \\(To create a merge\\|Create pull\\) request" nil t)
      (forward-line 1)
      (re-search-forward "remote: +" (line-end-position) t)

As a bonus, I modified the first regex in the code above so that the same command works for GitLab (which we also use for a few projects).

I feel like a broken record, but once again Emacs turns out to be an extremely user-friendly environment (provided that the user is a tinkerer kind, of course).

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

2022-11-14 Doubling backslashes

Today’s post is not meant to be very useful to most people, but it serves as a demonstration of a point (well, that, and a bit of advertisement;-)).

However strange it may sound, a few days ago I have a very atypical need. I needed to move some LaTeX code to a JSON file. This meant that all the backslashes had to be doubled, of course – LaTeX code is full of them, and they need to be escaped in JSON. Obviously, query-replace​’ing backslashes with double backslashes is easy, and query-replace only operates on the region when it is active, but I wanted to make sure I didn’t replace them twice by accidentally marking too much. So – partly as an exercise, I guess – I decided to write a command to replace every backslash in the region by two backslashes, but only if it was a single one.


(defun double-slashes-in-region (beg end)
  "Change every single dash in region to a double one."
  (interactive "*r")
      (narrow-to-region beg end)
      (goto-char (point-min))
      (while (search-forward "\\" nil t)
        (backward-char 1)
        (when (looking-at "\\\\[^\\\\]")
          (insert "\\"))
        (forward-char 2)))))

The real kick is that you can write such a function basically in no time (assuming you have some experience). In fact, it took me exactly 6 minutes to put it together (including the inevitable debugging – of course I didn’t get it right the first time!;-)).

And here, I cannot not mention that I wrote a textbook on Emacs Lisp. Check it out if you want to learn to code little convenience Elisp functions, too!

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page


CategoryEnglish, CategoryBlog