Tonλ's blog May the λ be with you

Emacs - Activate touchpad when org-mode exports html

by @ardumont on

In this previous post, I explained how to switch on and off the touchpad depending on the software focused at hand1.

Toggle on/off from the emacs repl

Pre-requisite

First, we need a means to call rapidly the shell command from the emacs world (and still being able to see what command is used):

(defun run-shl (&rest cmd)
  "A simpler command to run-shell-command with multiple params"
  (shell-command-to-string (apply #'concatenate 'string cmd)))

Now evaluate this snippet of code. Place yourself behind the last parenthesis and execute the keybinding C-x C-e. This loads your emacs lisp into the current session.

Code

Then we call the script to toggle on/off the touchpad: Here is the code:

(defun toggle-touchpad-manual (status)
  "Activate/Deactivate the touchpad depending on the status parameter (0/1)."
  (run-shl "toggle-touchpad-manual.sh " status))

Note

I do not detail the full path to the shell script because my PATH is correctly setuped. If this does not work for you, either:

  • fix your PATH
  • or gives the full path of such script.

Check

Now we can call this from any emacs buffer and see that it's working.

First, we must reactivate the touchpad (recall that I'm in emacs). My stumpwm binding is C-t T for me.

(toggle-touchpad-manual "0")

Then C-x C-e to evaluate. When trying to scroll with the touchpad, nothing happens, the touchpad is disabled, cool!

(toggle-touchpad-manual "1")

Then C-x C-e to evaluate. The touchpad is enabled, way cool!

Hook

At last, the means to tell emacs, activate the touchpad when exporting in html. This means already exist in emacs, it's called a hook.

Definition

A hook is basically a list of functions to apply at a certain moment in the life cycle of the mode you are using. This is a mode mechanism that permits emacs user to extend the standard behaviour.

It is often used for example to load a mode depending on the extension of a file or grouping mode together. All lispy mode likes to load paredit (to ease the manipulation of s-expression).

Example

Here is a hook for the clojure-mode (the naming convention is name-of-the-mode followed by a -hook). In this example, I added a function jack-in-once to the clojure-mode.

This will trigger the call of such a function every time a clojure-mode is launched.

(add-hook 'clojure-mode-hook 'jack-in-once)

org-mode one

The export html is a org-mode functionality.

By searching the org-mode hooks, I found org-export-html-final-hook which can be used for what I want.

So here is the elisp snippet:

(add-hook 'org-export-html-final-hook
          (lambda () (toggle-touchpad-manual "1")))

Again evaluate it for it to be active for your current session.

Note Here I tell org-mode to call the the function (lambda) each time an html export is about to finish.

As a result, now when exporting the org-mode into html and opening the browser my touchpad is enabled.

Thus, my setup is consistent again with the rest of the system.

Source

(defun run-shl (&rest cmd)
  "A simpler command to run-shell-command with multiple params"
  (shell-command-to-string (apply #'concatenate 'string cmd)))

(defun toggle-touchpad-manual (status)
  "Activate/Deactivate the touchpad depending on the status parameter (0/1)."
  (run-shl "toggle-touchpad-manual.sh " status))

(add-hook 'org-export-html-final-hook
          (lambda () (toggle-touchpad-manual "1")))

src: user-orgmode-pack/init.el

Conclusion

The same as the conclusion of this article, emacs rocks!

Footnotes:

1

Recall that I'm using stumpwm, thus meaning that I have only one window focused at a time (I can do more but that's the way I use it).

It was perfect until I realized that when in emacs, I export in html and open the browser… But then, the touchpad is off… which is now inconsistent with my stumpwm setup.

I completely forgot this use case. But, no panic, this time, emacs to the rescue!

Typically, what I want is, when in:

  • emacs, touchpad off
  • browser, touchpad on.

From hereupon, I will explain how to setup emacs so that when exporting the html and see the result in the browser, the touchpad is on too.

Latest posts