;; 05-latex.el --- LaTeX Settings

;; Copyright (C) 2007 - 2020 Steve Youngs

;;     Author: Steve Youngs <steve@sxemacs.org>
;; Maintainer: Steve Youngs <steve@sxemacs.org>
;;    Created: <2007-12-02>
;; Time-stamp: <Thursday Apr  9, 2020 06:49:13 steve>
;;   Download: <https://downloads.sxemacs.org/SYinits/>
;;   HTMLised: <https://www.sxemacs.org/SYinits/05-latex.html>
;;   Git Repo: git clone https://git.sxemacs.org/syinit
;;   Keywords: init, compile

;; This file is part of SYinit

;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; 1. Redistributions of source code must retain the above copyright
;;    notice, this list of conditions and the following disclaimer.
;;
;; 2. Redistributions in binary form must reproduce the above copyright
;;    notice, this list of conditions and the following disclaimer in the
;;    documentation and/or other materials provided with the distribution.
;;
;; 3. Neither the name of the author nor the names of any contributors
;;    may be used to endorse or promote products derived from this
;;    software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;; DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;; Commentary:
;;
;;   Do you remember back in the old days when you'd write a letter
;;   on a piece of paper, stick it in an envelope, put a stamp on it
;;   and have the postal service deliver it for you?  Well I still do
;;   that and LaTeX is how I get it done.  At least the writing,
;;   formatting, and printing side of it anyway.

;;; Credits:
;;
;;   The HTML version of this file was created with Hrvoje Niksic's
;;   htmlize.el which is part of the XEmacs "text-modes" package.
;;

;;; Todo:
;;
;;     

;;; Code:
;:*=======================
;:* Initialise aucTeX
(require 'tex-site)
(require 'latex)
(require 'reftex)
(require 'reftex-vars)
(require 'latex-units)
(require 'font-latex)
(require 'latex-toolbar)

;:*=======================
;:* auto-mode-alist
;; LaTeX or latex ???  It doesn't really matter because LaTeX-mode is
;; an alias to latex-mode, I just prefer the former name and like to
;; keep things less messy.
;;
;; Remove the ones set from tex-mode.el in the texinfo pkg that use
;; latex-mode
(remove-alist 'auto-mode-alist "\\.\\(?:sty\\|cls\\|bbl\\)\\'")
(remove-alist 'auto-mode-alist "\\.[tT]e[xX]\\'")
(remove-alist 'auto-mode-alist "\\.ltx\\'")
;; Add them back for LaTeX-mode
(add-to-list 'auto-mode-alist '("\\.\\(?:sty\\|cls\\|bbl\\)\\'" . LaTeX-mode))
(add-to-list 'auto-mode-alist '("\\.[tT]e[xX]\\'" . LaTeX-mode))
(add-to-list 'auto-mode-alist '("\\.ltx\\'" . LaTeX-mode))
(add-to-list 'auto-mode-alist '("\\.drv\\'" . LaTeX-mode))
;; doctex
(add-to-list 'auto-mode-alist '("\\.dtx\\'" . doctex-mode))

;:*=======================
;:* Shutup the byte-compiler
(defvar ispell-extra-args)
(defvar LaTeX-mode-map)
(defvar TeX-shell)

(setq
 LaTeX-float nil
 TeX-arg-cite-note-p t
 TeX-arg-footnote-number-p t
 TeX-arg-item-label-p nil
 TeX-auto-parse-length 999999999
 TeX-auto-private
 (file-name-as-directory
  (expand-file-name "documents/TeXauto" (getenv "HOME")))
 TeX-auto-save t
 TeX-debug-bad-boxes nil
 TeX-macro-private
 (file-name-as-directory
  (expand-file-name "documents/TeX" (getenv "HOME")))
 TeX-outline-extra nil
 TeX-parse-self t
 TeX-printer-default "Single Colour"
 TeX-printer-list
 '(("Duplex Colour" "dvips -f %s|lpr -PDuplex_Colour" "lpq -PDuplex_Colour")
   ("Single Colour" "dvips -f %s|lpr -PSingle_Colour" "lpq -PSingle_Colour")
   ("Duplex Grey" "dvips -f %s|lpr -PDuplex_Grey" "lpq -PDuplex_Grey")
   ("Single Grey" "dvips -f %s|lpr -PSingle_Grey" "lpq -PSingle_Grey")
   ("Photo" "dvips -f %s|lpr -PPhoto" "lpq -PPhoto")
   ("DL Envelope" "dvips -f %s|lpr -PDL_Envelope" "lpq -PDL_Envelope")))

(setq-default LaTeX-default-options "a4paper,12pt")
(setq-default TeX-master t)

(setq TeX-view-style '(("^a5$" "xdvi -thorough %d -paper a5")
                       ("^landscape$" "xdvi -thorough %d -paper a4r -s 4")
                       ("." "xdvi -thorough %d")))

;:*=======================
;:* Count words in a TeX text
(defun sds-word-count (start end)
  "Count lines/words/characters from START to END.
Replacement for count-lines-region."
  (interactive "r")
  (let ((ost (syntax-table)) 
        (nst (copy-syntax-table)))
    (modify-syntax-entry ?_ "w" nst)
    (modify-syntax-entry ?- "w" nst)
    (save-excursion
      (save-restriction
        (narrow-to-region start end)
        (goto-char (min start end))
        (unwind-protect
            (progn (set-syntax-table nst)
                   (message 
                    "Region (%d to %d) has: %d lines; %d words; %d characters."
                             start end (count-lines start end)
                             (string-to-number (how-many "\\<"))
                             (- end start)))
          (set-syntax-table ost))))))

;:*=======================
;:*  RefTeX Minor Mode 

;; Turn on RefTeX Minor Mode for all LaTeX files
(autoload 'reftex-mode     "reftex" "RefTeX Minor Mode" t)
(autoload 'turn-on-reftex  "reftex" "RefTeX Minor Mode" nil)
(autoload 'reftex-citation "reftex-cite" "Make citation" nil)

;: RefTeX is a minor mode with distinct support for \ref, \label and
;: \cite commands in (multi-file) LaTeX documents.
(setq 
 reftex-default-label-alist-entries
 '(Sideways AMSTeX amsmath endnotes fancybox floatfig longtable
            picinpar rotating sidecap subfigure supertab wrapfig LaTeX)
 reftex-enable-partial-scans t
 reftex-extra-bindings t
 reftex-guess-label-type t
 reftex-initialize-temporary-buffers t
 reftex-insert-label-flags '(t t)
 reftex-keep-temporary-buffers t
 reftex-label-alist 
 '((nil ?s nil nil nil ("Capital" "Cap." "Section" "Part") -3)
   (nil ?e nil nil nil ("Equation" "Eq.") -3)
   (nil ?t nil nil nil ("Table") -3)
   (nil ?f nil nil nil ("Figure" "Illustration" "Ill.") -3)
   (nil ?n nil nil nil ("Comment") -3)
   (nil ?i nil nil nil ("Point") -3))
 reftex-label-menu-flags '(t t t t t t t t)
 reftex-plug-into-AUCTeX t
 reftex-save-parse-info t
 reftex-toc-follow-mode t
 reftex-toc-include-labels t
 reftex-use-multiple-selection-buffers t
 reftex-vref-is-default t)

(define-key reftex-mode-map [(shift button=)]
  'reftex-mouse-view-crossref)

;:*=======================
;:* Index support
;;When writing a document with an index you will probably define
;;additional macros which make entries into the index.  Let's look at an
;;example.
;;     \newcommand{\ix}[1]{#1\index{#1}}
;;     \newcommand{\nindex}[1]{\textit{#1}\index[name]{#1}}
(setq reftex-index-macros '(("\\ix{*}" "idx" ?x "" nil nil)
                            ("\\nindex{*}" "name" ?n "" nil nil)
                            index))

(defun return-created-string ()
  "Return a \"Created:\" string."
  (let ((time (current-time-string)))
    (concat "Created at: "
            (substring time 0 20)
            (nth 1 (current-time-zone))
            " "
            (substring time -4) " ")))

;:*=======================
;* Hooks.
(defun sy-LaTeX-toolbar ()
  (interactive)
  (set-specifier left-toolbar-width (cons (current-buffer) 32))
  (latex-toolbar-install))

(defun sy-LaTeX-mode-hook ()
  (turn-on-auto-fill)
  (setq TeX-shell "/bin/zsh")
  (make-local-variable 'ispell-extra-args)
  (push "-t" ispell-extra-args)
  (turn-on-reftex)
  (add-to-list 'TeX-command-list
               '("xpdf" "xpdf %s.pdf" TeX-run-silent t nil))
  (add-to-list 'TeX-command-list
               '("gv" "gv %s.ps" TeX-run-silent t nil))
  (add-to-list 'TeX-command-list
               '("pdflatex" "pdflatex -interaction=nonstopmode %t"
                 TeX-run-command nil t))
  (when (device-on-window-system-p)
    (outl-mouse-minor-mode 1)
    ;; I can't get my extra toolbar to display from the hook, I don't
    ;; yet know why, so instead I bind `S-H-t' locally to load it.
    (local-set-key [(super hyper t)] #'sy-LaTeX-toolbar)))

(add-hook 'LaTeX-mode-hook #'sy-LaTeX-mode-hook 'append)

(setq LaTeX-section-hook
      '(LaTeX-section-heading
        LaTeX-section-title
        LaTeX-section-toc
        LaTeX-section-section
        LaTeX-section-label))

;:*=======================
;:* Texinfo
;; Jumping through hoops to ensure that we DO NOT get the AUCTeX
;; tex-info.  The texinfo.el from the "texinfo" (S)XEmacs package is
;; older, but far, far, superior.  Honestly, the AUCTeX / GNU people
;; have no idea what they're missing.
(eval-after-load 'tex-info
  (progn
    (delq 'texinfo-mode TeX-modes)
    (defalias 'texinfo-mode #'ignore)
    (require 'texinfo)))

(set-face-parent 'texinfo-heading-face 'font-lock-function-name-face)

;; Perdy colours!
(setq texinfo-font-lock-keywords
      `(("@\\([a-zA-Z]+\\|[^ \t\n]\\)" 1 font-lock-keyword-face) ;commands
        ("^\\*\\([^\n:]*\\)" 1 font-lock-function-name-face t) ;menu items
        ("@\\(emph\\|i\\|sc\\){\\([^}]+\\)" 2 'italic)
        ("@\\(strong\\|b\\){\\([^}]+\\)" 2 'bold)
        ("@\\(kbd\\|key\\|url\\|uref\\){\\([^}]+\\)" 2 font-lock-string-face)
        ("@\\(file\\|email\\){\\([^}]+\\)" 2 font-lock-string-face keep)
        ("@\\(samp\\|code\\|var\\|math\\|env\\|command\\|option\\){\\([^}]+\\)"
         2 font-lock-variable-name-face keep)
        ("@\\(cite\\|x?ref\\|pxref\\|dfn\\|inforef\\){\\([^}]+\\)"
         2 font-lock-reference-face) ;; #### XEmacs change
        ("@\\(anchor\\){\\([^}]+\\)" 2 font-lock-type-face)
        ("@\\(dmn\\|acronym\\|value\\){\\([^}]+\\)" 2 font-lock-builtin-face)
        ("@\\(end\\|itemx?\\) +\\(.+\\)" 2 font-lock-keyword-face keep)
        (,(concat "^@\\(" (regexp-opt (mapcar 'car texinfo-section-list) t)
                  "\\)\\(.*\n\\)") 3 texinfo-heading-face t)
        ("@c\\(omment\\)? \\(.*$\\)" 2 font-lock-comment-face)
        ("@node \\(.*$\\)" 1 font-lock-warning-face)
        ("^@[cfvkpt]index \\(.*$\\)" 1 font-lock-variable-name-face)))

(defun sy-texinfo-menu ()
  (easy-menu-add texinfo-mode-menu))

(add-hook 'texinfo-mode-hook #'sy-texinfo-menu)
(add-hook 'texinfo-mode-hook #'font-lock-mode)
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*
(message "LaTeX initialised")
Created with SXEmacs Valid XHTML 1.0 Transitional!
Copyright © 2020 Steve Youngs
Verbatim copying and distribution is permitted in any medium, providing this notice is preserved.
Last modified: Wed Apr 15 18:12:55 AEST 2020