mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-17 c4001ccd1864293b64aa37d83a9d9457eb875e70
commit | author | age
5cb5f7 1 ;;; smartparens-latex.el --- Additional configuration for (La)TeX based modes.  -*- lexical-binding: t; -*-
C 2
3 ;; Copyright (C) 2013-2016 Matus Goljer
4
5 ;; Author: Matus Goljer <matus.goljer@gmail.com>
6 ;; Maintainer: Matus Goljer <matus.goljer@gmail.com>
7 ;; Created: 14 Feb 2013
8 ;; Keywords: abbrev convenience editing
9 ;; URL: https://github.com/Fuco1/smartparens
10
11 ;; This file is not part of GNU Emacs.
12
13 ;;; License:
14
15 ;; This file is part of Smartparens.
16
17 ;; Smartparens is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
21
22 ;; Smartparens is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 ;; GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with Smartparens.  If not, see <http://www.gnu.org/licenses/>.
29
30 ;;; Commentary:
31
32 ;; This file provides some additional configuration for (La)TeX based
33 ;; modes.  To use it, simply add:
34 ;;
35 ;; (require 'smartparens-latex)
36 ;;
37 ;; into your configuration.  You can use this in conjunction with the
38 ;; default config or your own configuration.
39
40 ;; If you have good ideas about what should be added please file an
41 ;; issue on the github tracker.
42
43 ;; For more info, see github readme at
44 ;; https://github.com/Fuco1/smartparens
45
46 ;;; Code:
47
48 (require 'smartparens)
49
50 (defun sp-latex-insert-spaces-inside-pair (_id action _context)
51   "ID, ACTION, CONTEXT."
52   (when (eq action 'insert)
53     (insert "  ")
54     (backward-char 1))
55   (when (and (eq action 'wrap)
56              (save-excursion
57                (goto-char (sp-get sp-last-wrapped-region :beg-in))
58                (not (sp--looking-back-p "[[{(]"))))
59     (save-excursion
60       (goto-char (sp-get sp-last-wrapped-region :end-in))
61       (insert " ")
62       (goto-char (sp-get sp-last-wrapped-region :beg-in))
63       (insert " "))))
64
65 (defun sp-latex-skip-match-apostrophe (ms _mb me)
66   "MS, MB, ME."
67   (when (equal ms "'")
68     (save-excursion
69       (goto-char me)
70       (looking-at-p "\\sw"))))
71
72 (defun sp-latex-skip-double-quote (_id action _context)
73   "ID, ACTION, CONTEXT."
74   (when (eq action 'insert)
75     (when (looking-at-p "''''")
76       (delete-char -2)
77       (delete-char 2)
78       (forward-char 2))))
79
80 (defun sp-latex-point-after-backslash (id action _context)
81   "Return t if point follows a backslash, nil otherwise.
82 This predicate is only tested on \"insert\" action.
83 ID, ACTION, CONTEXT."
84   (when (eq action 'insert)
85     (let ((trigger (sp-get-pair id :trigger)))
86       (looking-back (concat "\\\\" (regexp-quote (if trigger trigger id))) nil))))
87
88 (add-to-list 'sp-navigate-skip-match
89              '((tex-mode plain-tex-mode latex-mode) . sp--backslash-skip-match))
90
91 (sp-with-modes '(
92                  tex-mode
93                  plain-tex-mode
94                  latex-mode
95                  LaTeX-mode
96                  )
97   (sp-local-pair "`" "'"
98                  :actions '(:rem autoskip)
99                  :skip-match 'sp-latex-skip-match-apostrophe
100                  :unless '(sp-latex-point-after-backslash))
101   ;; math modes, yay.  The :actions are provided automatically if
102   ;; these pairs do not have global definitions.
103   (sp-local-pair "$" "$")
104   (sp-local-pair "\\[" "\\]"
105                  :unless '(sp-latex-point-after-backslash))
106
107   ;; disable useless pairs.
108   (sp-local-pair "\\\\(" nil :actions nil)
109   (sp-local-pair "'" nil :actions nil)
110   (sp-local-pair "\\\"" nil :actions nil)
111
112   ;; quote should insert ``'' instead of double quotes.  If we ever
113   ;; need to insert ", C-q is our friend.
114   (sp-local-pair "``" "''"
115                  :trigger "\""
116                  :unless '(sp-latex-point-after-backslash)
117                  :post-handlers '(sp-latex-skip-double-quote))
118
119   ;; add the prefix function sticking to {} pair
120   (sp-local-pair "{" nil :prefix "\\\\\\(\\sw\\|\\s_\\)*")
121
122   ;; do not add more space when slurping
123   (sp-local-pair "{" "}")
124   (sp-local-pair "(" ")")
125   (sp-local-pair "[" "]")
126
127   ;; pairs for big brackets.  Needs more research on what pairs are
128   ;; useful to add here.  Post suggestions if you know some.
129   (sp-local-pair "\\left(" "\\right)"
130                  :trigger "\\l("
131                  :when '(sp-in-math-p)
132                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
133   (sp-local-pair "\\left[" "\\right]"
134                  :trigger "\\l["
135                  :when '(sp-in-math-p)
136                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
137   (sp-local-pair "\\left\\{" "\\right\\}"
138                  :trigger "\\l{"
139                  :when '(sp-in-math-p)
140                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
141   (sp-local-pair "\\left|" "\\right|"
142                  :trigger "\\l|"
143                  :when '(sp-in-math-p)
144                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
145   (sp-local-pair "\\bigl(" "\\bigr)"
146                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
147   (sp-local-pair "\\biggl(" "\\biggr)"
148                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
149   (sp-local-pair "\\Bigl(" "\\Bigr)"
150                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
151   (sp-local-pair "\\Biggl(" "\\Biggr)"
152                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
153   (sp-local-pair "\\bigl[" "\\bigr]"
154                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
155   (sp-local-pair "\\biggl[" "\\biggr]"
156                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
157   (sp-local-pair "\\Bigl[" "\\Bigr]"
158                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
159   (sp-local-pair "\\Biggl[" "\\Biggr]"
160                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
161   (sp-local-pair "\\bigl\\{" "\\bigr\\}"
162                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
163   (sp-local-pair "\\biggl\\{" "\\biggr\\}"
164                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
165   (sp-local-pair "\\Bigl\\{" "\\Bigr\\}"
166                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
167   (sp-local-pair "\\Biggl\\{" "\\Biggr\\}"
168                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
169   (sp-local-pair "\\lfloor" "\\rfloor"
170                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
171   (sp-local-pair "\\lceil" "\\rceil"
172                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
173   (sp-local-pair "\\langle" "\\rangle"
174                  :post-handlers '(sp-latex-insert-spaces-inside-pair))
175   (sp-local-pair  "\\lVert" "\\rVert"
176           :when '(sp-in-math-p)
177           :trigger "\\lVert"
178           :post-handlers '(sp-latex-insert-spaces-inside-pair))
179   (sp-local-pair  "\\lvert" "\\rvert"
180           :when '(sp-in-math-p)
181           :trigger "\\lvert"
182           :post-handlers '(sp-latex-insert-spaces-inside-pair))
183
184   ;; some common wrappings
185   (sp-local-tag "\"" "``" "''" :actions '(wrap))
186   (sp-local-tag "\\b" "\\begin{_}" "\\end{_}")
187   (sp-local-tag "bi" "\\begin{itemize}" "\\end{itemize}")
188   (sp-local-tag "be" "\\begin{enumerate}" "\\end{enumerate}"))
189
190 (provide 'smartparens-latex)
191
192 ;;; smartparens-latex.el ends here