User:Riviera/Emacs layout: Difference between revisions

From XPUB & Lens-Based wiki
(Functions for Typesetting with Emacs)
(No difference)

Revision as of 22:09, 22 May 2024

An aim of the reading, writing and research methods seminars this trimester is to create a personal reader. In a previous wiki post, I discussed my engagement with free software based solutions to the typographical challenges posed by this task. This wiki post picks up on the same theme from a perspective closer to these wiki posts about Emacs. The outcome is a result of minimally typesetting, and then printing Emacs buffers. The font is Deja Vu Mono, the text has double height line spacing and it’s paginated more or less by hand.

To facilitate with this endeavor, I wrote a handful of functions in Emacs Lisp. Below is an example:

(defun configure-right-margin (right-margin-width file 
                    &optional fill-column justify)
  "Configure RIGHT-MARGIN-WIDTH in FILE, optionally set FILL-COLUMN or JUSTIFY"
  (find-file-other-window file)
  (progn
    (set-right-margin (point-min) (point-max) right-margin-width)
    (if fill-column
    (set-fill-column fill-column)
      nil)
    (if justify
    (fill-region (point-min) (point-max) justify)
       nil)))

The function weaves together various actions which I would need to perform by hand in Emacs for each text. The idea is that one could call this script on a file whilst running Emacs in Batch Mode. I hope this could be achieved by running a command such as:

$ emacs -Q --batch -l paginate.el --eval '(configure-right-margin 8 "foo.txt")'

This command runs Emacs non-interactively. It loads the file with the function definitions into memory and then evaluates one of those defined functions. Presumably there is a way in which this could be hooked into a python or Bash script.

Here’s another function which I wrote:

(defun paginate (page-length initial-page-number offset)
  (progn
    (insert-char 12) ; form feed
    (insert-char 13 2) ; carriage return twice
    (newline)
    ;; format page number
    (insert-char 32 (- fill-column offset)) ; space
    (insert "[" (int-to-string initial-page-number) "]") ; pg number in header
    (setq initial-page-number (1+ initial-page-number))
    (newline)
    (insert-char 13)
    (open-line 1)
    (forward-line (- page-length 4))
    (forward-line)
    (if (= (point) (point-max))
    (beginning-of-buffer)
      (list (forward-line (- 0 1))
        (paginate page-length initial-page-number offset)))))

The script inserts particular characters at a given point on occasions which are determined by variables such as page-length. Functions in lines 12-17 illustrate the spatial aspects of Emacs. Furthermore, it is a recursive function which, in this case, means it calls itself until completion.