summaryrefslogtreecommitdiffstats
path: root/lisp/ravi-init-tex.el
blob: 95df08fb2b2db9d6e1ea88e54a705cbffc0db5e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
;;; ravi-init-tex.el --- tex and friends

;; Copyright (C) 2014

;; Author:  <ravi@nero.lan>
;; Keywords:

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; TeX and LaTeX support

;;; Code:

(use-package tex-site
  :ensure auctex
  :mode ("\\.tex\\'" . TeX-latex-mode)
  :commands (TeX-latex-mode
             TeX-mode
             tex-mode
             LaTeX-mode
             latex-mode)
  :config
  (progn
    (use-package latex
      :config
      (progn
        (use-package reftex
          :defer t
          :diminish reftex-mode
          :ensure nil)
        ;; fix the "bug" in SP regexp wrap that treats ' as "word"
        (modify-syntax-entry ?' ".")

        (require 'smartparens-latex)
        (sp-local-pair 'latex-mode "\\begin" "\\end")
        (sp-local-tag 'latex-mode "\\ba" "\\begin{align*}" "\\end{align*}")

        (use-package preview :ensure nil)
        (use-package font-latex :ensure nil)
        (fset 'tex-font-lock-subscript 'ignore)

        (sp-with-modes '(tex-mode plain-tex-mode latex-mode)
          (sp-local-pair "\\[" nil :post-handlers '(my-latex-math-block-indent)))

        (defun my-latex-math-block-indent (a action c)
          (when (eq action 'insert)
            (newline-and-indent)
            (save-excursion (newline))))

        (defun my-latex-compile ()
          (interactive)
          (save-buffer)
          (TeX-command "LaTeX" 'TeX-master-file nil))
        (bind-key "C-M-x" 'my-latex-compile LaTeX-mode-map)

        (defvar my-latex-wrap-choices '("emph"
                                        "textsc"))
        (defvar my-latex-wrap-history nil)

        (defun my-latex-wrap (macro-name)
          (interactive (list (ido-completing-read
                              "Macro> "
                              my-latex-wrap-choices
                              nil 'confirm nil my-latex-wrap-history)))
          (when (use-region-p)
            (let ((b (region-beginning))
                  (e (region-end)))
              (goto-char e)
              (insert "}")
              (goto-char b)
              (insert "\\" macro-name "{"))))
        (bind-key "C-c w" 'my-latex-wrap LaTeX-mode-map)

        (defun my-end-of-environment ()
          (interactive)
          (LaTeX-mark-environment)
          (end-of-region))

        (defun my-beginning-of-environment ()
          (interactive)
          (LaTeX-mark-environment)
          (beginning-of-region)
          (deactivate-mark))

        (bind-key "M-n" 'my-end-of-environment LaTeX-mode-map)
        (bind-key "M-p" 'my-beginning-of-environment LaTeX-mode-map)

        ;; Use okular rather than evince
        (setq TeX-view-program-selection
              '((output-dvi "Okular")
                ;; (output-pdf "Okular")
                (output-pdf "PDF Tools")))

        (defun th/pdf-view-revert-buffer-maybe (file)
          (let ((buf (find-buffer-visiting file)))
            (when buf
              (with-current-buffer buf
                (when (derived-mode-p 'pdf-view-mode)
                  (pdf-view-revert-buffer nil t))))))

        (add-hook 'TeX-after-TeX-LaTeX-command-finished-hook
                  #'th/pdf-view-revert-buffer-maybe)

        ;; fix italian quote highlight
        (push '("\"<" "\">") font-latex-quote-list)

        (defun my-latex-remove-command ()
          "Unwrap the expression that point is in or before, also
removing the command name.  By command we understand a symbol
starting with \\ and followed by a block of text enclosed in {}."
          (interactive)
          (let ((ok (sp-get-enclosing-sexp)))
            (cond
             ;; we're inside the { } block
             (ok
              (progn
                (save-excursion
                  (goto-char (sp-get ok :beg))
                  (zap-to-char -1 ?\\ ))
                (sp-splice-sexp)))
             ;; test if we are in looking at the command fromt he front
             ((looking-at "\\\\")
              (zap-up-to-char 1 ?{)
              (sp-unwrap-sexp))
             ;; otherwise we're inside the command name
             (t
              (zap-to-char -1 ?\\ )
              (zap-up-to-char 1 ?{)
              (sp-unwrap-sexp)))))
        (bind-key "C-c d" 'my-latex-remove-command LaTeX-mode-map)
        (bind-key "M-RET" 'LaTeX-insert-item LaTeX-mode-map)

        (use-package company-math
          :config
          (progn
           (defun ravi/company-math-setup ()
             (setq-local company-backends
                         (append '(company-math-symbols-latex company-latex-commands)
                      company-backends)))
           (add-hook 'TeX-mode-hook 'ravi/company-math-setup)))

        (use-package latex-extra
          :hook (LaTex-mode . latex-extra-mode)
          :diminish latex-extra-mode)

        (defun my-LaTeX-preview-math ()
          (interactive)
          (let ((b (save-excursion (while (texmathp) (backward-char 1)) (1- (point))))
                (e (save-excursion (while (texmathp) (forward-char 1)) (point))))
            (preview-region b e)))
        (bind-key "C-<m-key>" 'my-LaTeX-preview-math preview-map)

        (defun my-LaTeX-mode-init ()
          (setq TeX-auto-save t)
          (setq TeX-parse-self t)
          (TeX-PDF-mode t)
          (setq reftex-plug-into-AUCTeX t)
          (reftex-mode t)
          (TeX-fold-mode t)
          (TeX-source-correlate-mode 1)

          (smartparens-mode 1)

          (LaTeX-add-environments
           '("derivation" LaTeX-env-label))
          (TeX-add-symbols '("emph" 1))

          (setq fill-column 88)

          (setq TeX-save-query nil)
          (setq TeX-show-compilation t)

          (when (functionp 'helm-dash)
            (setq-local dash-docs-docsets '("LaTeX")))

          (message "LaTeX mode init complete."))
        ;; ACUTeX replaces latex-mode-hook with LaTeX-mode-hook
        (add-hook 'LaTeX-mode-hook 'my-LaTeX-mode-init)
        )
      :ensure nil)
    )
  )

(provide 'ravi-init-tex)
;;; ravi-init-tex.el ends here