-
Notifications
You must be signed in to change notification settings - Fork 2
/
auctex-lua.el
149 lines (137 loc) · 5.74 KB
/
auctex-lua.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
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
;;; auctex-lua.el --- Lua editing support for AUCTeX
;;
;; Copyright (C) 2013 Sean Allred
;;
;; Author: Sean Allred (seallred@smcm.edu)
;; Version: 1.0
;; Package-Requires: ((auctex "11.86") (lua-mode "20130419"))
;; URL: http://github.com/vermiculus/auctex-lua
;;
;; This file is NOT part of Emacs.
;;
;; 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 2
;; 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, write to
;;
;; The Free Software Foundation, Inc.
;; 51 Franklin Street, Fifth Floor
;; Boston, MA, 02110-1301
;; USA.
;;
;; Keywords: latex, lua
;;
;;; Commentary:
;;
;; This package provides some basic utilities for working with Lua
;; code from within AUCTeX. `LaTeX-edit-Lua-code-start' is the entry
;; point of this package; bind it to your favorite key and use it
;; inside of any environment in `LaTeX-Lua-environments'. To commit
;; your changes to the parent buffer and return to it, simply use
;; `save-buffer' (or whichever key it is bound to). The contents of
;; the parent buffer will be updated and the Lua buffer will be killed.
;;
;; Beware! Editing embedded Lua code is asynchronous. If you kill
;; the buffer that was editing it, your changes will be lost! In a
;; future update I will add a `yes-or-no-p' confirmation to killing
;; the buffer, but I've yet to figure that one out.
;;; Code:
(require 'lua-mode)
(defgroup LaTeX-lua nil
"Lua support in AUCTeX."
:group 'LaTeX)
;;;###autoload
(defcustom LaTeX-Lua-environments '("luacode" "luacode*")
"List of environments that will contain Lua code."
:group 'LaTeX-lua
:type '(repeat (string)))
(defvar LaTeX-edit-Lua-code-parent-buffer)
(defvar LaTeX-edit-Lua-code-parent-buffer-point)
(defun LaTeX-mark-environment-contents ()
"Mark the contents of the innermost LaTeX environment. See
also `LaTeX-find-matching-begin' and `LaTeX-find-matching-end'."
(interactive)
;; Search for the end of the current environment.
(LaTeX-find-matching-end)
;; Then place a mark.
(push-mark (search-backward "\\end"))
;; Search for the beginning of the current environment.
(LaTeX-find-matching-begin)
;; Search for the end of the \begin{...}
(search-forward "}"))
;;;###autoload
(defun LaTeX-edit-Lua-code-start ()
"Place Lua code in a separate buffer in `lua-mode'."
(interactive)
;; If we are in a Lua environment,
(if (member (LaTeX-current-environment) LaTeX-Lua-environments)
;; Make a few notes about where we are and what we're looking at
(let* ((lua-parent-buffer (current-buffer))
(lua-where-edit-started (point))
(lua-buffer-name (format "*%s [Lua]*" (buffer-name)))
(lua-buffer (get-buffer-create lua-buffer-name)) ; prepare buffer
(lua-code (progn (LaTeX-mark-environment-contents) ; get lua code
(buffer-substring-no-properties (point) (mark)))))
;; Switch to the Lua buffer we just created.
(switch-to-buffer-other-window lua-buffer)
;; Record some buffer-local variables
(setq LaTeX-edit-Lua-code-parent-buffer lua-parent-buffer)
(setq LaTeX-edit-Lua-code-parent-buffer-point lua-where-edit-started)
;; and switch this buffer to `lua-mode'
(lua-mode)
;; Set key bindings.
(local-set-key [remap save-buffer] 'LaTeX-edit-Lua-code-finish)
;; Fill the buffer with the Lua code.
(insert lua-code))
(message "Not in a Lua code environment.")))
(defun LaTeX-edit-Lua-code-finish ()
"Dump the contents of the current buffer into the
buffer/environment it was called from, replacing what is
currently in that environment.
Note that this function isn't too smart yet; it does not
intelligently handle a scenario where other editing has taken
place in the parent buffer. If point has moved into a different
environment, that environment will be replaced instead of its
original one. Remember, you can always `undo' your changes. See
`LaTeX-mark-environment-contents'."
(interactive)
;; 'Ensure' this is a Lua code buffer that was made with `LaTeX-edit-Lua-code-start'
(if (bufferp LaTeX-edit-Lua-code-parent-buffer)
;; Grab the Lua code
(let* ((lua-code (progn (widen)
(LaTeX-edit-Lua--chomp
(buffer-substring (point-min)
(point-max))))))
;; Kill the Lua buffer
(kill-buffer-and-window)
;; and switch to its parent
(switch-to-buffer LaTeX-edit-Lua-code-parent-buffer)
(save-excursion
;; Mark the current environment
(LaTeX-mark-environment-contents)
;; and replace it
(delete-region (point) (mark))
;; with the Lua code
(insert lua-code))
;; and return point to its rightful place
(goto-char LaTeX-edit-Lua-code-parent-buffer-point))
(message "%s %s"
"Something went wrong."
"Am I *really* in a buffer created with `LaTeX-edit-Lua-code-finish'?")))
;; Adapted from the Elisp Cookbook:
;; http://www.emacswiki.org/emacs/ElispCookbook#toc6
(defun LaTeX-edit-Lua--chomp (str)
"Chomp leading and tailing whitespace from STR."
(while (string-match "\\s*.*\\s*" str)
(setq str (replace-match "" t t str)))
str)
(provide 'auctex-lua)
;;; auctex-lua.el ends here