noxz-sites

A collection of a builder and various scripts creating the noxz.tech sites
git clone https://noxz.tech/git/noxz-sites.git
Log | Files | README | LICENSE

noxz.tech/articles/simple_groff_compiler_for_vim/index.www
1When writing documents in vim, it's nice to have a quick way of compiling them
2into the desired formats. In my case, I compile my groff files into pdfs while
3at the same time having a preview in
4.ICD zathura .
5Zathura automatically detects
6changes in pdfs and reloads accordingly, which is nice.
7
8.HnS 1
9The compiler code
10.HnE
11
12Below, I will describe the code in short.
13
14.CDS
15.COS
16#!/bin/sh
17
18_FILE=$(readlink -f "$1")
19_DIR=$(dirname "$_FILE")
20_BASE="${_FILE%.*}"
21_PRFX="---COMPILE:"
22_ARGS="$(grep -oP -- "$_PRFX.*$" "$_FILE" |  sed -s "s|$_PRFX||")"
23
24cd "$_DIR" || return
25
26case "$_FILE" in
27*\\.ms)  preconv "$_FILE"                                            \\
28    | refer                                                         \\
29    | tbl -Tpdf                                                     \\
30    | eqn -Tpdf                                                     \\
31    | pic -Tpdf                                                     \\
32    | groff -k -ms $_ARGS -dpaper=a4 -P-pa4 -P-e -Tpdf              \\
33    | gs                                                            \\
34        -q                                                          \\
35        -dNOPAUSE                                                   \\
36        -dBATCH                                                     \\
37        -dPDFSETTINGS=/prepress                                     \\
38        -sDEVICE=pdfwrite                                           \\
39        -dPrinted=false                                             \\
40        -sOutputFile="$_BASE.pdf" -
41    ;;
42esac
43.COE
44.CDE
45
46The script above is called
47.I compile
48and is intended to be used for multiple
49file types. In this version, I have only implemented support for
50.I .ms
51files.
52First, the filename is read from the first argument to the script. From the
53filename, I determine the directory path and the "base name" without the
54filename's extension. In the file I search for the pattern
55.I ---COMPILE:
56as it
57may contain additional arguments for the compiler (used above as
58.I $_ARGS ).
59
60The script then changes the directory to the file's directory and executes a
61chain of programs. First,
62.I preconv
63is executed to handle unicode characters.
64Then,
65.I refer
66handles references,
67.I eqn
68handles mathematical expressions,
69.I pic
70handles pictures, groff compiles everything into an
71.I a4
72pdf using the
73.I ms
74macro set, and finally, Ghostscript runs post processing and embeds used fonts
75into the pdf (while preserving hyperlinks).
76
77Everything is then being outputted to the same "base name", but as a pdf file.
78
79.HnS 1
80Usage in vim
81.HnE
82
83In vim, I have the following key binding set to compile the document upon
84request.
85
86.CDS
87.COS
88map <leader>c :w! \| silent !compile <c-r>%<CR><Esc>:redraw!<CR>
89.COE
90.CDE
91
92There isn't much to say about this. The document is being saved, compiled, and
93then vim is being redrawn (to avoid glitches).
94
95.HnS 1
96Workflow
97.HnE
98
99My usual workflow for writing documents in vim, using the above compiler,
100consists of me first starting a terminal (with tmux). I then open a new tab,
101start vim, and setup everything for a base document in groff. After that, I run
102the compiler to create an empty pdf. In the first tab, I execute zathura to
103view the pdf. As I use dwm (a tiling window manager), I have vim on one side of
104the screen and zathura on the other. Simple and functional!