Simple Word Processing With Enscript

The Software

  $EDITOR
  enscript
  a postscript viewer
  pr (for tabular data)

  A list of the software sources and example files can be
  found at the end of this page.


Introduction

  Enscript converts plain text files into PostScript and other
  formats, such as  HTML and RTF, and can also send files directly
  to a printer.  It is much more advanced than plain lpr or lp, and
  also supports special filters for highlighting source code.

  It is useful for printing letters, or technical files, and
  supports using mixed fonts, such as italic and bold fonts for
  emphasis.

  You may find it best to use real tabs in documents, set to a
  small tabstop, rather than spaces.  This makes it a bit easier to
  align things like addresses to the right margin without them
  wrapping off the edge to the next line.  In vim you would need to
  `set noexpandtab'.

  Note: I've cut some long lines with a `\' for readability.

Method

 The steps in enscript printing:

  1. Process a plain text file with enscript and --output to a file
  2. Preview output in a PostScript viewer
  3. Repeat the pipe command omitting any `--output'

  The last command will send the PS output to the printer.
  The output can rival that of any GUI word processor.  The
  source documents are plain text, so they will be much
  easier to use normal file operations like `grep' on.


Print Previewing

  Any PostScript viewer will do.

  Viewers for the framebuffer:

    fimgs/fim
    fbgs/fbi

  For X Windows:

    gv
    evince
    (plenty more)

  Fim is `fbi improved'.  It supports scripting and a lot of
  customisation.  It includes a script `fimgs', which converts a
  PDF or PS to png images in a temporary directory using
  `ghostscript', and the images are viewed in `fim'.


Options


 General Options

    -e[char], -e[\octal], --escapes[=char]
      Allow escape codes.  These are enscript's most
      powerful feature.  It's possible to change fonts and
      can allow inserting raw postscript.  It's not possible
      to use `^@' in a Vim key map, but `^_' (-e\037) works
      fine.
      Default: ^@

    -f font, --font=name
      Main body font name and size.
      Default: Courier10 or Courier7

    -M name, --media=name
      Media - e.g. A4, letter etc.
      Default: letter

    --margins=left:right:top:bottom
      Margins in PS points (72 DPI)
      Default: none.

    -o file, -p file, --output=file
      Output to file.
      Default: send to printer

    -T num, --tabsize=num
      Tabsize to use.
      Default: 8

    -w lang, --language=lang
      Output language:

      PostScript, HTML, OverStrike (for line printers and
      `less'), RTF, ANSI (terminal control codes.)
      Default: PostScript

    --word-wrap

    -X enc, --encoding=enc
      Input encoding.  The manual has a long list.
      Default: latin1

    -B, --no-header
      Do not print page headers.


  Options that may be useful for technical documents or source code:

    -G, --fancy-header[=name]
      Print a fancy header (very customisable)

    -F name, --header-font=name
      Font for headers.

    -C[start_line], --line-numbers[=start_line]

    -E[lang], --highlight[=lang]
      Source code highlighter.

      List of highlight filters included with enscript 1.6.6:

      ada asm awk bash c changelog cpp csh delphi diff diffs diffu
      elisp f90 fortran fortran_pp haskell html idl inf java
      javascript ksh m4 mail makefile matlab nroff objc outline
      pascal perl postscript pyrex python rfc scheme sh skill sql
      states synopsys tcl tcsh tex vba verilog vhdl vrml wmlscript
      zsh

    -b header_string, --header=header_string
      Use text string as header.  Can use %Format escapes
      described in the manual.  E.G.:

      enscript --header='$n %W Page $% of $=' *.c

    --color[=bool]

    --footer=footer_string
      Similar to --header.

  The most used settings can be added to ~/.enscriptrc.


Aliasing Commands

  Aliases save typing and avoid making typos.

  Proportional font, with media size and margins that might be
  suitable for UK/European correspondence:

  alias ep='enscript -e\037 -BM A4 --word-wrap -fTimes-Roman12 \
    --margins=50:50:50:50 -T2'

  Fixed-width font with a `fancy header', perhaps more useful for
  source code:

  alias ep-mono='enscript -e\037 -GM A4 --word-wrap -fCourier12 \
    --margins=50:50:50:50 -FCourier12 -T2'

  Example alias usage: `ep letter1.txt -o out.ps'

Encodings

  This means we need to find a workaround for some characters like
  UTF-8 bullets, and e.g. use something like a `middle dot'
  instead.

  Supported encodings from the man page:

  88591, latin1 ISO-8859-1 (ISO Latin1) (default).
  88592, latin2 ISO-8859-2 (ISO Latin2)
  88593, latin3 ISO-8859-3 (ISO Latin3)
  88594, latin4 ISO-8859-4 (ISO Latin4)
  88595, cyrillic ISO-8859-5 (ISO Cyrillic)
  88597, greek ISO-8859-7 (ISO Greek)
  88599, latin5 ISO-8859-9 (ISO Latin5)
  885910, latin6 ISO-8859-10 (ISO Latin6)
  ascii   7-bit ascii
  asciifise, asciifi, asciise 7-bit ascii with some scandinavian
    (Finland, Sweden) extensions
  asciidkno, asciidk, asciino 7-bit ascii with some scandinavian
    (Denmark, Norway) extensions
  ibmpc, pc, dos IBM PC charset
  mac     Mac charset
  vms     VMS multinational charset
  hp8     HP Roman-8 charset
  koi8    Adobe Standard Cyrillic Font KOI8 charset
  ps, PS  PostScript font's default encoding
  pslatin1, ISOLatin1Encoding
    PostScript interpreter's `ISOLatin1Encoding'

  There is an application called `paps' that is similar to enscript
  and supports UTF-8, however it doesn't support changing fonts at
  the time of writing, nor any of the advanced features of
  enscript.


Editing

Some example Vim settings:

  " Stop lines breaking and limit the terminal width to 68
  " characters.
  set columns=68
  set textwidth=0
  set linebreak
  set nolist

  " No line numbering and small tab stops
  set nonumber
  set tabstop=2
  set softtabstop=2
  set shiftwidth=2
  set noexpandtab


Using Emphasis

  Escape codes in enscript begin with a `NUL' or "^@" (Ctrl-@).
  These can't be entered in Vim automatically via a keymap,
  as they get transposed to newlines, but we can change "^@" to
  something else and use enscript's `-e' flag to use that instead.

  The man page has a list of escape format codes near the end, (see
  `man enscript | less -p "^SPECIAL ESCAPES"'), and can also inject
  PostScript code.  The syntax to change the font is:
  `^@font{NEWFONTNAME}'.

  To use an italic or bold font we simply have to switch to that
  font before the word(s) we want emphasised, and then switch back
  to the default font afterwards.  E.G.:

  This is^@font{Times-Italic12} italic ^@font{default}text.

  A list of fonts can be found in /usr/share/enscript/afm/font.map

  We can make some helper mappings in vim to save typing:

  " normal/command mode font maps
  nmap <buffer> ,ii i^_font{Times-Italic12}<Esc>ea^_font{default}<Esc>
  nmap <buffer> ,bb i^_font{Times-Bold12}<Esc>ea^_font{default}<Esc>

  " visual select mode font maps
  vmap <buffer> ,ii <Esc>`>a^_font{default}<Esc>`<i^_font{Times-Italic12}<Esc>
  vmap <buffer> ,bb <Esc>`>a^_font{default}<Esc>`<i^_font{Times-Bold12}<Esc>

  For the nmap maps, place the cursor on the first letter of a
  word, press comma followed by `bb' for bold, or `ii' for italic
  etc.  The vmap visual mode maps work by selecting text in visual
  mode and then using the same comma key combinations.

  This will insert a tab and a round bullet at the start of the
  current line:

  nmap <buffer> ,tt I<Tab>·<Tab><Esc>

  Since enscript doesn't support the UTF-8 bullet symbols, this is
  actually a `middle dot' from ISO-8559-1.  It may look like a
  small dot in the terminal, but it's much larger when rendered in
  PostScript or printed out:

  char dec col/row oct hex  description
  [·]  183  11/07  267  B7  MIDDLE DOT

More here:

http://www.columbia.edu/kermit/latin1.html and
http://www.columbia.edu/kermit/latin9.html
and a good reference and list:
https://en.wikipedia.org/wiki/ISO/IEC_8859

  Map <leader> + <Tab> to `make' enscript:

  nmap <buffer> <leader><tab> :silent make!<CR>

  Either `makeprg' or `&mp' need to be set for `make' to work:

  let &mp = 'enscript -e\037 -MA4 --word-wrap --margins=50:50:50:50 \
  -T4 -o %:r.ps; fimgs -r 240 %:r.ps'

  The above sets the output file name to the same as the edited
  text file name, but changes the extension to .ps, and then loads
  the file in fimgs.  The -r 240 flag is the resolution in fim.


Vim settings in an Autogroup

  An example autogroup that sets up a make command and binds for
  italic and bold.  It's easier to add these commands to a
  ~/.vim/after/ftplugin/text.vim though, which I've linked to
  below.

  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  augroup text
    au!
    autocmd BufRead *txt
    \ let &mp = 'enscript -e\037 -DCollate -MA4 --word-wrap \
    --margins=50:50:50:50 -T4  -o %:r.ps' |
    \ nnoremap <leader><tab> :silent make!<CR> |
    \ nnoremap ,ii :normal i^_font{Times-Italic12}<Esc>ea^_font{default}<Esc> |
    \ nnoremap ,bb :normal i^_font{Times-Bold12}<Esc>ea^_font{default}<Esc>
  augroup END

  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""


Example Template Shell Script

  ############################################################

  #!/bin/sh

  # Exit on most errors
  set -e

  PREFIX="$1"
  TEMPLATE="$HOME/letters/templates/letter-template.txt"
  LETTERDATE=$( date +"%A, %d %B %Y" )
  DATE=$( date +%Y-%m-%d-%S )
  FNAME="$PREFIX-$DATE.txt"

  sed "s|@DATE@|$LETTERDATE|g" "$TEMPLATE" > "$FNAME"
  $EDITOR "$FNAME"

  ############################################################

  This takes the first argument to use as a prefix for the file
  name and adds the date: prefix-YYYY-MM-DD-SS.txt

  Fill the template with your name and address and put @DATE@ where
  the date would normally go.


Headers

  There are a few example header files that come with
  enscript.  They are written in the PostScript language.
  Here is a simple example:

 % -- code follows this line --
 %HeaderHeight: 50
 %Format: pagenumstr Re: Appointment on 19/12/2019, J Smith, ($%/$=)

 /do_header { % print the header
   gsave
     d_header_x d_header_y translate
     0 20 moveto
     0.2 setgray
     pagenumstr show
   grestore
 } def

  To try it, copy the file to ~/.enscript/, use the -G
  flag (instead of -B), or --fancy-header=standard-letter,
  or add the following to ~/.enscriptrc:

  DefaultFancyHeader: standard-letter

  This will print:

  Re: Appointment on 19/12/2019, J Smith, (1/4)

  At the top of each page, where the numbers in parentheses are
  page number/total pages, although the --header flag can do simple
  headers like this inline.  There are more example header files in
  /usr/share/enscript/.


Tabular Data With PR

  pr can paginate and columnise text, which can then be piped to
  enscript, lp/lpr, or redirected to a new file.

  A Few of The Options

  -COLUMNS
    Number of columns, e.g. -3

  -a, --across
    Print across, then down

  -l lines, --length=lines
    Page length
    Default: 66

  -o margin, --indent=MARGIN
    Number of characters to use for margin
    Default: 0

  -w width, --width=WIDTH
    Width of page in characters
    Default: 72

  Data is entered in a single column, which pr will columnise.
  E.G.:

  % cat cols.txt:

  one
  two
  three
  four
  five
  six

  % pr -a -3 -l 15 -w 35 cols.txt

  Output:

  2019-03-03 12:47  cols.txt   Page 1


  one         two         three
  four        five        six


Software List:

You may have some or all of these available in your distro's
software repo.

enscript homepage:
https://www.gnu.org/software/enscript
fim (fbi improved) homepage:
http://www.nongnu.org/fbi-improved/
fbida (the original fbi) homepage:
https://www.kraxel.org/blog/linux/fbida/
ghostscript homepage:
https://ghostscript.com/
gv homepages:
http://pages.cs.wisc.edu/~ghost/gv/gv.htm
https://sourceforge.net/projects/gnu-gv/
pr is part of GNU Core Utils:
https://www.gnu.org/software/coreutils/manual/html_node/pr-invocation.html
paps homepage:
https://github.com/dov/paps



File List: