Tweak my htmlize code in prep for adding my inits to SXEmacs website.
[syinit] / 05-latex.el
1 ;; 05-latex.el --- LaTeX Settings
2
3 ;; Copyright (C) 2007 - 2020 Steve Youngs
4
5 ;;     Author: Steve Youngs <steve@sxemacs.org>
6 ;; Maintainer: Steve Youngs <steve@sxemacs.org>
7 ;;    Created: <2007-12-02>
8 ;; Time-stamp: <Thursday Apr  9, 2020 06:49:13 steve>
9 ;;   Download: <https://downloads.sxemacs.org/SYinits/>
10 ;;   HTMLised: <https://www.sxemacs.org/SYinits/05-latex.html>
11 ;;   Git Repo: git clone https://git.sxemacs.org/syinit
12 ;;   Keywords: init, compile
13
14 ;; This file is part of SYinit
15
16 ;; Redistribution and use in source and binary forms, with or without
17 ;; modification, are permitted provided that the following conditions
18 ;; are met:
19 ;;
20 ;; 1. Redistributions of source code must retain the above copyright
21 ;;    notice, this list of conditions and the following disclaimer.
22 ;;
23 ;; 2. Redistributions in binary form must reproduce the above copyright
24 ;;    notice, this list of conditions and the following disclaimer in the
25 ;;    documentation and/or other materials provided with the distribution.
26 ;;
27 ;; 3. Neither the name of the author nor the names of any contributors
28 ;;    may be used to endorse or promote products derived from this
29 ;;    software without specific prior written permission.
30 ;;
31 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
32 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 ;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 ;; DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 ;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
38 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
39 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
40 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
41 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
43 ;;; Commentary:
44 ;;
45 ;;   Do you remember back in the old days when you'd write a letter
46 ;;   on a piece of paper, stick it in an envelope, put a stamp on it
47 ;;   and have the postal service deliver it for you?  Well I still do
48 ;;   that and LaTeX is how I get it done.  At least the writing,
49 ;;   formatting, and printing side of it anyway.
50
51 ;;; Credits:
52 ;;
53 ;;   The HTML version of this file was created with Hrvoje Niksic's
54 ;;   htmlize.el which is part of the XEmacs "text-modes" package.
55 ;;
56
57 ;;; Todo:
58 ;;
59 ;;     
60
61 ;;; Code:
62 ;:*=======================
63 ;:* Initialise aucTeX
64 (require 'tex-site)
65 (require 'latex)
66 (require 'reftex)
67 (require 'reftex-vars)
68 (require 'latex-units)
69 (require 'font-latex)
70 (require 'latex-toolbar)
71
72 ;:*=======================
73 ;:* auto-mode-alist
74 ;; LaTeX or latex ???  It doesn't really matter because LaTeX-mode is
75 ;; an alias to latex-mode, I just prefer the former name and like to
76 ;; keep things less messy.
77 ;;
78 ;; Remove the ones set from tex-mode.el in the texinfo pkg that use
79 ;; latex-mode
80 (remove-alist 'auto-mode-alist "\\.\\(?:sty\\|cls\\|bbl\\)\\'")
81 (remove-alist 'auto-mode-alist "\\.[tT]e[xX]\\'")
82 (remove-alist 'auto-mode-alist "\\.ltx\\'")
83 ;; Add them back for LaTeX-mode
84 (add-to-list 'auto-mode-alist '("\\.\\(?:sty\\|cls\\|bbl\\)\\'" . LaTeX-mode))
85 (add-to-list 'auto-mode-alist '("\\.[tT]e[xX]\\'" . LaTeX-mode))
86 (add-to-list 'auto-mode-alist '("\\.ltx\\'" . LaTeX-mode))
87 (add-to-list 'auto-mode-alist '("\\.drv\\'" . LaTeX-mode))
88 ;; doctex
89 (add-to-list 'auto-mode-alist '("\\.dtx\\'" . doctex-mode))
90
91 ;:*=======================
92 ;:* Shutup the byte-compiler
93 (defvar ispell-extra-args)
94 (defvar LaTeX-mode-map)
95 (defvar TeX-shell)
96
97 (setq
98  LaTeX-float nil
99  TeX-arg-cite-note-p t
100  TeX-arg-footnote-number-p t
101  TeX-arg-item-label-p nil
102  TeX-auto-parse-length 999999999
103  TeX-auto-private
104  (file-name-as-directory
105   (expand-file-name "documents/TeXauto" (getenv "HOME")))
106  TeX-auto-save t
107  TeX-debug-bad-boxes nil
108  TeX-macro-private
109  (file-name-as-directory
110   (expand-file-name "documents/TeX" (getenv "HOME")))
111  TeX-outline-extra nil
112  TeX-parse-self t
113  TeX-printer-default "Single Colour"
114  TeX-printer-list
115  '(("Duplex Colour" "dvips -f %s|lpr -PDuplex_Colour" "lpq -PDuplex_Colour")
116    ("Single Colour" "dvips -f %s|lpr -PSingle_Colour" "lpq -PSingle_Colour")
117    ("Duplex Grey" "dvips -f %s|lpr -PDuplex_Grey" "lpq -PDuplex_Grey")
118    ("Single Grey" "dvips -f %s|lpr -PSingle_Grey" "lpq -PSingle_Grey")
119    ("Photo" "dvips -f %s|lpr -PPhoto" "lpq -PPhoto")
120    ("DL Envelope" "dvips -f %s|lpr -PDL_Envelope" "lpq -PDL_Envelope")))
121
122 (setq-default LaTeX-default-options "a4paper,12pt")
123 (setq-default TeX-master t)
124
125 (setq TeX-view-style '(("^a5$" "xdvi -thorough %d -paper a5")
126                        ("^landscape$" "xdvi -thorough %d -paper a4r -s 4")
127                        ("." "xdvi -thorough %d")))
128
129 ;:*=======================
130 ;:* Count words in a TeX text
131 (defun sds-word-count (start end)
132   "Count lines/words/characters from START to END.
133 Replacement for count-lines-region."
134   (interactive "r")
135   (let ((ost (syntax-table)) 
136         (nst (copy-syntax-table)))
137     (modify-syntax-entry ?_ "w" nst)
138     (modify-syntax-entry ?- "w" nst)
139     (save-excursion
140       (save-restriction
141         (narrow-to-region start end)
142         (goto-char (min start end))
143         (unwind-protect
144             (progn (set-syntax-table nst)
145                    (message 
146                     "Region (%d to %d) has: %d lines; %d words; %d characters."
147                              start end (count-lines start end)
148                              (string-to-number (how-many "\\<"))
149                              (- end start)))
150           (set-syntax-table ost))))))
151
152 ;:*=======================
153 ;:*  RefTeX Minor Mode 
154
155 ;; Turn on RefTeX Minor Mode for all LaTeX files
156 (autoload 'reftex-mode     "reftex" "RefTeX Minor Mode" t)
157 (autoload 'turn-on-reftex  "reftex" "RefTeX Minor Mode" nil)
158 (autoload 'reftex-citation "reftex-cite" "Make citation" nil)
159
160 ;: RefTeX is a minor mode with distinct support for \ref, \label and
161 ;: \cite commands in (multi-file) LaTeX documents.
162 (setq 
163  reftex-default-label-alist-entries
164  '(Sideways AMSTeX amsmath endnotes fancybox floatfig longtable
165             picinpar rotating sidecap subfigure supertab wrapfig LaTeX)
166  reftex-enable-partial-scans t
167  reftex-extra-bindings t
168  reftex-guess-label-type t
169  reftex-initialize-temporary-buffers t
170  reftex-insert-label-flags '(t t)
171  reftex-keep-temporary-buffers t
172  reftex-label-alist 
173  '((nil ?s nil nil nil ("Capital" "Cap." "Section" "Part") -3)
174    (nil ?e nil nil nil ("Equation" "Eq.") -3)
175    (nil ?t nil nil nil ("Table") -3)
176    (nil ?f nil nil nil ("Figure" "Illustration" "Ill.") -3)
177    (nil ?n nil nil nil ("Comment") -3)
178    (nil ?i nil nil nil ("Point") -3))
179  reftex-label-menu-flags '(t t t t t t t t)
180  reftex-plug-into-AUCTeX t
181  reftex-save-parse-info t
182  reftex-toc-follow-mode t
183  reftex-toc-include-labels t
184  reftex-use-multiple-selection-buffers t
185  reftex-vref-is-default t)
186
187 (define-key reftex-mode-map [(shift button=)]
188   'reftex-mouse-view-crossref)
189
190 ;:*=======================
191 ;:* Index support
192 ;;When writing a document with an index you will probably define
193 ;;additional macros which make entries into the index.  Let's look at an
194 ;;example.
195 ;;     \newcommand{\ix}[1]{#1\index{#1}}
196 ;;     \newcommand{\nindex}[1]{\textit{#1}\index[name]{#1}}
197 (setq reftex-index-macros '(("\\ix{*}" "idx" ?x "" nil nil)
198                             ("\\nindex{*}" "name" ?n "" nil nil)
199                             index))
200
201 (defun return-created-string ()
202   "Return a \"Created:\" string."
203   (let ((time (current-time-string)))
204     (concat "Created at: "
205             (substring time 0 20)
206             (nth 1 (current-time-zone))
207             " "
208             (substring time -4) " ")))
209
210 ;:*=======================
211 ;* Hooks.
212 (defun sy-LaTeX-toolbar ()
213   (interactive)
214   (set-specifier left-toolbar-width (cons (current-buffer) 32))
215   (latex-toolbar-install))
216
217 (defun sy-LaTeX-mode-hook ()
218   (turn-on-auto-fill)
219   (setq TeX-shell "/bin/zsh")
220   (make-local-variable 'ispell-extra-args)
221   (push "-t" ispell-extra-args)
222   (turn-on-reftex)
223   (add-to-list 'TeX-command-list
224                '("xpdf" "xpdf %s.pdf" TeX-run-silent t nil))
225   (add-to-list 'TeX-command-list
226                '("gv" "gv %s.ps" TeX-run-silent t nil))
227   (add-to-list 'TeX-command-list
228                '("pdflatex" "pdflatex -interaction=nonstopmode %t"
229                  TeX-run-command nil t))
230   (when (device-on-window-system-p)
231     (outl-mouse-minor-mode 1)
232     ;; I can't get my extra toolbar to display from the hook, I don't
233     ;; yet know why, so instead I bind `S-H-t' locally to load it.
234     (local-set-key [(super hyper t)] #'sy-LaTeX-toolbar)))
235
236 (add-hook 'LaTeX-mode-hook #'sy-LaTeX-mode-hook 'append)
237
238 (setq LaTeX-section-hook
239       '(LaTeX-section-heading
240         LaTeX-section-title
241         LaTeX-section-toc
242         LaTeX-section-section
243         LaTeX-section-label))
244
245 ;:*=======================
246 ;:* Texinfo
247 ;; Jumping through hoops to ensure that we DO NOT get the AUCTeX
248 ;; tex-info.  The texinfo.el from the "texinfo" (S)XEmacs package is
249 ;; older, but far, far, superior.  Honestly, the AUCTeX / GNU people
250 ;; have no idea what they're missing.
251 (eval-after-load 'tex-info
252   (progn
253     (delq 'texinfo-mode TeX-modes)
254     (defalias 'texinfo-mode #'ignore)
255     (require 'texinfo)))
256
257 (set-face-parent 'texinfo-heading-face 'font-lock-function-name-face)
258
259 ;; Perdy colours!
260 (setq texinfo-font-lock-keywords
261       `(("@\\([a-zA-Z]+\\|[^ \t\n]\\)" 1 font-lock-keyword-face) ;commands
262         ("^\\*\\([^\n:]*\\)" 1 font-lock-function-name-face t) ;menu items
263         ("@\\(emph\\|i\\|sc\\){\\([^}]+\\)" 2 'italic)
264         ("@\\(strong\\|b\\){\\([^}]+\\)" 2 'bold)
265         ("@\\(kbd\\|key\\|url\\|uref\\){\\([^}]+\\)" 2 font-lock-string-face)
266         ("@\\(file\\|email\\){\\([^}]+\\)" 2 font-lock-string-face keep)
267         ("@\\(samp\\|code\\|var\\|math\\|env\\|command\\|option\\){\\([^}]+\\)"
268          2 font-lock-variable-name-face keep)
269         ("@\\(cite\\|x?ref\\|pxref\\|dfn\\|inforef\\){\\([^}]+\\)"
270          2 font-lock-reference-face) ;; #### XEmacs change
271         ("@\\(anchor\\){\\([^}]+\\)" 2 font-lock-type-face)
272         ("@\\(dmn\\|acronym\\|value\\){\\([^}]+\\)" 2 font-lock-builtin-face)
273         ("@\\(end\\|itemx?\\) +\\(.+\\)" 2 font-lock-keyword-face keep)
274         (,(concat "^@\\(" (regexp-opt (mapcar 'car texinfo-section-list) t)
275                   "\\)\\(.*\n\\)") 3 texinfo-heading-face t)
276         ("@c\\(omment\\)? \\(.*$\\)" 2 font-lock-comment-face)
277         ("@node \\(.*$\\)" 1 font-lock-warning-face)
278         ("^@[cfvkpt]index \\(.*$\\)" 1 font-lock-variable-name-face)))
279
280 (defun sy-texinfo-menu ()
281   (easy-menu-add texinfo-mode-menu))
282
283 (add-hook 'texinfo-mode-hook #'sy-texinfo-menu)
284 (add-hook 'texinfo-mode-hook #'font-lock-mode)
285 ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*
286 (message "LaTeX initialised")