latex-mode-expansions.el (3574B)
1 ;;; latex-mode-expansions.el --- LaTeX-specific expansions for expand-region -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2012-2023 Free Software Foundation, Inc 4 5 ;; Author: Ivan Andrus 6 ;; Based on js-mode-expansions by: Magnar Sveen <magnars@gmail.com> 7 ;; Keywords: marking region 8 9 ;; This program is free software; you can redistribute it and/or modify 10 ;; it under the terms of the GNU General Public License as published by 11 ;; the Free Software Foundation, either version 3 of the License, or 12 ;; (at your option) any later version. 13 14 ;; This program is distributed in the hope that it will be useful, 15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 ;; GNU General Public License for more details. 18 19 ;; You should have received a copy of the GNU General Public License 20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>. 21 22 ;;; Commentary: 23 24 ;; This is for AUCTeX, not the builtin latex-mode. 25 26 ;; Feel free to contribute any other expansions for LaTeX at 27 ;; 28 ;; https://github.com/magnars/expand-region.el 29 30 ;;; Code: 31 32 (require 'expand-region-core) 33 ;referenced free variables and functions defined in mode 34 (defvar texmathp-why) 35 (defvar texmathp-tex-commands1) 36 (defvar texmathp-onoff-regexp) 37 (defvar LaTeX-mode-hook) 38 (declare-function LaTeX-mark-environment "latex") 39 (declare-function texmathp "texmathp") 40 41 (defun er/mark-LaTeX-inside-environment () 42 "Like `LaTeX-mark-environment' but marks the inside of the environment. 43 Skips past [] and {} arguments to the environment." 44 (interactive) 45 (LaTeX-mark-environment) 46 (when (looking-at "\\\\begin{") 47 (forward-sexp 2) 48 ;; Assume these are arguments 49 (while (looking-at "[ \t\n]*[{[]") 50 (forward-sexp 1)) 51 ;; Go to next line if there is nothing interesting on this one 52 (skip-syntax-forward " ") ;; newlines are ">" i.e. end comment 53 (when (looking-at "%\\|$") 54 (forward-line)) 55 ;; Clean up the end portion 56 (exchange-point-and-mark) 57 (backward-sexp 2) 58 (skip-syntax-backward " ") 59 (exchange-point-and-mark))) 60 61 (defun er/mark-LaTeX-math () 62 "Mark current math environment." 63 (interactive) 64 (when (texmathp) 65 (let* ((string (car texmathp-why)) 66 (pos (cdr texmathp-why)) 67 (reason (assoc string texmathp-tex-commands1)) 68 (type (cadr reason))) 69 (cond 70 ((eq type 'env-on) ;; environments equation, align, etc. 71 (er/mark-LaTeX-inside-environment)) 72 ((eq type 'arg-on) ;; \ensuremath etc. 73 (goto-char pos) 74 (set-mark (point)) 75 (forward-sexp 2) 76 (exchange-point-and-mark)) 77 ((eq type 'sw-toggle) ;; $ and $$ 78 (goto-char pos) 79 (set-mark (point)) 80 (forward-sexp 1) 81 (exchange-point-and-mark)) 82 ((eq type 'sw-on) ;; \( and \[ 83 (re-search-forward texmathp-onoff-regexp) 84 (set-mark pos) 85 (exchange-point-and-mark)) 86 (t (error (format "Unknown reason to be in math mode: %s" type))))))) 87 88 (defun er/add-latex-mode-expansions () 89 "Adds expansions for buffers in latex-mode" 90 (set (make-local-variable 'er/try-expand-list) 91 (append 92 er/try-expand-list 93 '(LaTeX-mark-environment 94 LaTeX-mark-section 95 er/mark-LaTeX-inside-environment 96 er/mark-LaTeX-math)))) 97 98 (let ((latex-mode-hook LaTeX-mode-hook)) 99 (er/enable-mode-expansions 'latex-mode #'er/add-latex-mode-expansions) 100 (setq LaTeX-mode-hook latex-mode-hook)) 101 102 (provide 'latex-mode-expansions) 103 104 ;; latex-mode-expansions.el ends here