Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
150 lines (137 sloc) 5.74 KB
;;; auctex-lua.el --- Lua editing support for AUCTeX
;; Copyright (C) 2013 Sean Allred
;; Author: Sean Allred (
;; Version: 1.0
;; Package-Requires: ((auctex "11.86") (lua-mode "20130419"))
;; URL:
;; 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
;; 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)
(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'."
;; Search for the end of the current environment.
;; Then place a mark.
(push-mark (search-backward "\\end"))
;; Search for the beginning of the current environment.
;; Search for the end of the \begin{...}
(search-forward "}"))
(defun LaTeX-edit-Lua-code-start ()
"Place Lua code in a separate buffer in `lua-mode'."
;; 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'
;; 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
;; '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)
(buffer-substring (point-min)
;; Kill the Lua buffer
;; and switch to its parent
(switch-to-buffer LaTeX-edit-Lua-code-parent-buffer)
;; Mark the current environment
;; 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:
(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)))
(provide 'auctex-lua)
;;; auctex-lua.el ends here
You can’t perform that action at this time.