forked from magnars/expand-region.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latex-mode-expansions.el
104 lines (88 loc) · 3.44 KB
/
latex-mode-expansions.el
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
;;; latex-mode-expansions.el --- LaTeX-specific expansions for expand-region
;; Copyright (C) 2012 Ivan Andrus
;; Author: Ivan Andrus
;; Based on js-mode-expansions by: Magnar Sveen <[email protected]>
;; Keywords: marking region
;; 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:
;; This is for AUCTeX, not the builtin latex-mode.
;; Feel free to contribute any other expansions for LaTeX at
;;
;; https://github.com/magnars/expand-region.el
;;; Code:
(require 'expand-region-core)
;referenced free variables and functions defined in mode
(defvar texmathp-why)
(defvar texmathp-tex-commands1)
(defvar texmathp-onoff-regexp)
(defvar LaTeX-mode-hook)
(declare-function LaTeX-mark-environment "latex")
(declare-function texmathp "texmathp")
(defun er/mark-LaTeX-inside-environment ()
"Like `LaTeX-mark-environment' but marks the inside of the environment.
Skips past [] and {} arguments to the environment."
(interactive)
(LaTeX-mark-environment)
(when (looking-at "\\\\begin{")
(forward-sexp 2)
;; Assume these are arguments
(while (looking-at "[ \t\n]*[{[]")
(forward-sexp 1))
;; Go to next line if there is nothing interesting on this one
(skip-syntax-forward " ") ;; newlines are ">" i.e. end comment
(when (looking-at "%\\|$")
(forward-line))
;; Clean up the end portion
(exchange-point-and-mark)
(backward-sexp 2)
(skip-syntax-backward " ")
(exchange-point-and-mark)))
(defun er/mark-LaTeX-math ()
"Mark current math environment."
(interactive)
(when (texmathp)
(let* ((string (car texmathp-why))
(pos (cdr texmathp-why))
(reason (assoc string texmathp-tex-commands1))
(type (cadr reason)))
(cond
((eq type 'env-on) ;; environments equation, align, etc.
(er/mark-LaTeX-inside-environment))
((eq type 'arg-on) ;; \ensuremath etc.
(goto-char pos)
(set-mark (point))
(forward-sexp 2)
(exchange-point-and-mark))
((eq type 'sw-toggle) ;; $ and $$
(goto-char pos)
(set-mark (point))
(forward-sexp 1)
(exchange-point-and-mark))
((eq type 'sw-on) ;; \( and \[
(re-search-forward texmathp-onoff-regexp)
(set-mark pos)
(exchange-point-and-mark))
(t (error (format "Unknown reason to be in math mode: %s" type)))))))
(defun er/add-latex-mode-expansions ()
"Adds expansions for buffers in latex-mode"
(set (make-local-variable 'er/try-expand-list)
(append
er/try-expand-list
'(LaTeX-mark-environment
LaTeX-mark-section
er/mark-LaTeX-inside-environment
er/mark-LaTeX-math))))
(let ((latex-mode-hook LaTeX-mode-hook))
(er/enable-mode-expansions 'latex-mode 'er/add-latex-mode-expansions)
(setq LaTeX-mode-hook latex-mode-hook))
(provide 'latex-mode-expansions)
;; latex-mode-expansions.el ends here