summaryrefslogtreecommitdiff
path: root/tex-conf.el
blob: 5245c2164292a4c6b8cb1445bba72bdc150e3bb7 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
;;; tex-conf.el --- My configurations of TeX         -*- lexical-binding: t; -*-

;; Copyright (C) 2021  李俊緯

;; Author: 李俊緯 <mmemmew@gmail.com>
;; Keywords: tex

;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This is my configuration file for (La)TeX. In particular I use and
;; configure the package AUCTeX here.

;;; Code:

(add-to-list 'load-path "/usr/local/share/emacs/site-lisp/")
(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)

(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq TeX-view-program-selection
      '((output-dvi "open")
        (output-pdf "PDF Tools")
        (output-html "open")))

;;; I don't want to split the screen.

;;;###autoload
(defun durand-TeX-pdf-tools-sync-view-a ()
  "Advice to `TeX-pdf-tools-sync-view', which won't pop buffers."
  (unless (fboundp 'pdf-tools-install)
    (error "PDF Tools are not available"))
  (unless TeX-PDF-mode
    (error "PDF Tools only work with PDF output"))
  (add-hook 'pdf-sync-backward-redirect-functions
            #'TeX-source-correlate-handle-TeX-region)
  (if (and TeX-source-correlate-mode
           (fboundp 'pdf-sync-forward-search))
      (with-current-buffer (or (when TeX-current-process-region-p
                                 (get-file-buffer (TeX-region-file t)))
                               (current-buffer))
        (pdf-sync-forward-search))
    (let ((pdf (TeX-active-master (TeX-output-extension))))
      (switch-to-buffer (or (find-buffer-visiting pdf)
                            (find-file-noselect pdf))))))

(add-hook 'TeX-after-compilation-finished-functions
          #'TeX-revert-document-buffer)

(setq pdf-sync-forward-display-action
      (list (list #'display-buffer-reuse-window
                  #'display-buffer-same-window)))

(setq pdf-sync-backward-display-action
      (list (list #'display-buffer-reuse-window
                  #'display-buffer-same-window)))

(advice-add #'TeX-pdf-tools-sync-view :override
            #'durand-TeX-pdf-tools-sync-view-a)

;;; Derive labels and ask for confirmation

(setq reftex-insert-label-flags (list t t))

;;; Correlate

(load "/usr/local/share/emacs/site-lisp/auctex/tex.el" nil t)

(setq TeX-source-correlate-start-server t)
(TeX-source-correlate-mode 1)

;;; Section display

(load "/usr/local/share/emacs/site-lisp/auctex/font-latex.el" nil t)

(setq font-latex-fontify-sectioning 1.3)

;;; Add a font macro command

(load "/usr/local/share/emacs/site-lisp/auctex/latex.el" nil t)

(add-to-list 'LaTeX-font-list
             '(?\C-k "" "" "\\mathfrak{" "}"))

;;; Dollars

(setq TeX-electric-math (cons "\\(" "\\)"))

(define-key LaTeX-mode-map (vector ?\§) #'durand-insert-escape)

;;;###autoload
(defun durand-insert-escape ()
  "Insert an escape character."
  (interactive)
  (insert "\\"))

;;; Parens don't require space

(add-hook 'LaTeX-mode-hook #'durand-prepare-latex)

;;; Brackets handling

(mapc
 (function
  (lambda (cell)
    (define-key LaTeX-mode-map (vector (car cell)) (cdr cell))))
 (list
  (cons ?\( #'open-paren)
  (cons ?{ #'open-curly)
  (cons ?\[ #'open-bracket)
  (cons 'backspace #'durand-delete-pair)
  (cons ?\) #'end-exit-paren)
  (cons ?à #'open-back-paren)))

;;;###autoload
(defun open-paren ()
  "open parenthesis inserts a matching pair"
  (interactive)
  (progn
    (insert "()")
    (backward-char)))

;;;###autoload
(defun open-curly ()
  "open curly inserts a matching pair"
  (interactive)
  (progn
    (insert "{}")
    (backward-char)))

;;;###autoload
(defun open-bracket ()
  "open bracket inserts a matching pair"
  (interactive)
  (progn
    (insert "[]")
    (backward-char)))

;;;###autoload
(defun durand-delete-pair ()
  "Delete the matching pair"
  (interactive)
  (cond ((region-active-p) ; if the region is active, then do the original thing
         (delete-backward-char 1))
        ((looking-back "\\(\\\\(\\|\\\\\\[\\)" (- (point) 2))
         (save-excursion
           (backward-char 1)
           (ignore-errors
             (forward-sexp 1)
             (delete-char -2)))
         (delete-char -2))
        ((memq (char-before) '(?\( ?\[ ?\{))
         (save-excursion
           (backward-char 1)
           (ignore-errors
             (forward-sexp 1)
             (delete-char -1)))
         (delete-char -1))
        (t
         (backward-delete-char-untabify 1))))

;;;###autoload
(defun end-exit-paren ()
  "Use closing pasenthesis to exit the parenthesis"
  (interactive)
  (let ((ch (char-after nil))
        (ch-list '(?\) ?\} ?\] ?\$)))
    (cond ((memq ch ch-list) (forward-char))
          ((looking-at-p "\\(\\\\)\\|\\\\]\\|\\\\}\\)")
           (forward-char 2))
          ((looking-at-p "\\\\right")
           (forward-char 6)
           (end-exit-paren))
          (t (self-insert-command 1)))))

;;;###autoload
(defun open-back-paren ()
  "Use \"à\" to go back to the parenthesis."
  (interactive)
  (save-match-data
    (let ((ch (char-before nil))
          (ch-list '(?\) ?\} ?\] ?\$))
          continue)
      (cond ((looking-back "\\(\\\\)\\|\\\\]\\|\\\\}\\)"
                           (max (- (point) 2) (point-min)))
             (forward-char -2)
             (setq continue t))
            ((memq ch ch-list) (backward-char) (setq continue t))
            (t (self-insert-command 1)))
      (cond
       ((and continue
             (looking-back
              "\\\\right"
              (max (- (point) 6) (point-min))))
        (forward-char -6))))))

(make-variable-buffer-local 'parens-require-spaces)

;;;###autoload
(defun durand-prepare-latex ()
  "Prepare some settings in LateX mode."
  (modify-syntax-entry ?\\ "_")
  (turn-on-auto-fill)
  (setq parens-require-spaces nil))

;;; Don't insert a new line between the section and the label.

;;;###autoload
(defun durand-latex-delete-newline ()
  "If the character before point is a newline, then delete it."
  (cond
   ((= (char-before) ?\n)
    (delete-char -1))))

(setq LaTeX-section-hook
      '(LaTeX-section-heading LaTeX-section-title
        LaTeX-section-section durand-latex-delete-newline
        LaTeX-section-label))

;;; Expansions

(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)

;;; Reftex-mode

(add-hook 'LaTeX-mode-hook #'reftex-mode)

;;; Do it all!

(defun durand-tex-do-all (arg)
  "Do everything I want and show the PDF."
  (interactive "P")
  ;; (LaTeX-fill-environment nil)
  (save-buffer 0)
  (TeX-command-run-all arg))

(define-key LaTeX-mode-map (vector 3 ?t) #'durand-tex-do-all)
(define-key LaTeX-mode-map (vector ?\H-t) #'durand-tex-do-all)

(define-key LaTeX-math-mode-map (LaTeX-math-abbrev-prefix) t)

(set-default 'LaTeX-math-abbrev-prefix "ù")

(define-key LaTeX-math-mode-map
  (LaTeX-math-abbrev-prefix) LaTeX-math-keymap)

(cond ((boundp 'LaTeX-math-keymap))
      ((defvar LaTeX-math-keymap)))

(define-key LaTeX-math-keymap [?$] 'durand-insert-display-equation)
(define-key LaTeX-math-keymap [?o] 'durand-insert-o-things)

(setq LaTeX-math-list '((?$ durand-insert-display-equation)
                        (?o durand-insert-o-things)))

;;;###autoload
(defun durand-insert-display-equation ()
  "Insert display equation symbols."
  (interactive)
  (insert "\\[\\]")
  (goto-char (- (point) 2)))

;;;###autoload
(defvar durand-o-things-list nil
  "A list of things associated with the \"o\" key.
Each entry should have the form (key . macro),
where the macro part is without the backslash.")

(setq durand-o-things-list
      (list
       (cons "x" "otimes")
       (cons "s" "sum")
       (cons "p" "prod")
       (cons "+" "oplus")
       (cons "o" "circ")
       (list "{" "{" "}")
       (list "(" "(" ")")
       (list "b" "lvert" "rvert")))

;;;###autoload
(defun durand-insert-o-things ()
  "Insert a symbol associated with \"o\" key.
The list is in the variable `durand-o-things-list'"
  (interactive)
  (let ((thing (minibuffer-with-setup-hook 'durand-headlong-minibuffer-setup-hook
                 (completing-read
                  "Chois une chose associée à \"o\":" (mapcar 'car durand-o-things-list)
                  nil t "^"))))
    (let ((associated (alist-get thing durand-o-things-list nil nil #'string=)))
      (cond
       ((not (consp associated))
        ;; length = 1
        (insert (format "\\%s" (assoc-default thing durand-o-things-list #'string=))))
       ((and (null (cddr associated))
             (string= (car associated) "("))
        ;; special treatment for the parenthesis
        (insert (format "\\left%s" (car associated)))
        (save-excursion
          (insert
           (format "\\right%s" (cadr associated)))))
       ((null (cddr associated))
        ;; length = 2
        (insert (format "\\left\\%s" (car associated)))
        (save-excursion
          (insert
           (format "\\right\\%s" (cadr associated)))))
       (t
        (user-error "Weird associated: %S" associated))))))

;;; Automatic braces

(setq TeX-electric-sub-and-superscript t)

;;; If I need to type nested documents frequently, the following will
;;; come in handy.

;; (setq-default TeX-master nil)

;;; hieroglyphs

(defun durand-tex-hiero ()
  "Translate the hieroglyph code in the region to proper latex
 code."
  (interactive)
  (let ((text (cond ((use-region-p)
                     (buffer-substring-no-properties
                      (region-beginning)
                      (region-end)))
                    ((buffer-substring-no-properties
                      (point-min) (point-max)))))
        (temp-buffer (get-buffer-create "*temp*")))
    (call-process-region
     text nil "sesh" nil temp-buffer)
    (with-current-buffer temp-buffer
      (kill-new (buffer-string)))
    (kill-buffer temp-buffer)))

(provide 'tex-conf)
;;; tex-conf.el ends here