diff -Nru lua-mode-20110121/.gitignore lua-mode-20130419/.gitignore --- lua-mode-20110121/.gitignore 2011-01-21 12:14:43.000000000 +0000 +++ lua-mode-20130419/.gitignore 1970-01-01 00:00:00.000000000 +0000 @@ -1,2 +0,0 @@ -lua-mode-*.zip -release-notes-* diff -Nru lua-mode-20110121/Makefile lua-mode-20130419/Makefile --- lua-mode-20110121/Makefile 2011-01-21 12:14:43.000000000 +0000 +++ lua-mode-20130419/Makefile 2013-04-19 07:27:32.000000000 +0000 @@ -1,15 +1,25 @@ # Makefile for lua-mode -VERSION=$(shell grep "^;; Version:" lua-mode.el | cut -f 2) +VERSION="$(shell sed -nre '/^;; Version:/ { s/^;; Version:[ \t]+//; p }' lua-mode.el)" DISTFILE = lua-mode-$(VERSION).zip +# EMACS value may be overridden +EMACS?=emacs + +default: + @echo version is $(VERSION) + +compile: + $(EMACS) --batch --no-site-file -f batch-byte-compile lua-mode.el + dist: rm -f $(DISTFILE) && \ - zip $(DISTFILE) -r . -x ".git/*" "*.gitignore" "*.zip" + git archive --format=zip -o $(DISTFILE) --prefix=lua-mode/ HEAD release: - git diff --exit-code && \ + git fetch && \ + git diff remotes/origin/master --exit-code && \ git tag -a -m "Release tag" rel-$(VERSION) && \ - git push origin master && git pull origin master && \ - woger lua-l lua-mode lua-mode "release $(VERSION)" "Emacs major mode for editing Lua files" release-notes-$(VERSION) http://github.com/rrthomas/lua-mode/ + woger lua-l lua-mode lua-mode "release $(VERSION)" "Emacs major mode for editing Lua files" release-notes-$(VERSION) http://github.com/immerrr/lua-mode/ && \ + git push origin master @echo 'Send update to ELPA!' diff -Nru lua-mode-20110121/NEWS lua-mode-20130419/NEWS --- lua-mode-20110121/NEWS 1970-01-01 00:00:00.000000000 +0000 +++ lua-mode-20130419/NEWS 2013-04-19 07:27:32.000000000 +0000 @@ -0,0 +1,37 @@ +* Release rel-20130419 +** highlight hash-bang line as comment + +** make lua-mode-hook editable via customize + +** fix several indentation bugs & quirks + +** fix lua-send-proc not to send previous function when point is at the beginning of a function + +** derive lua-mode from prog-mode for Emacs24 + +** add font-locking for builtins and numeric constants + +** fix a bug causing exponential complexity in a keyword matching regexp + +** add more unindentation cases for block-closing tokens + +** improve multiline highlighting via font-lock-syntactic-keywords + This should make font-locking of multiline literals more fluent & stable. And + it becomes customizable via standard font-lock configuration + +** properly fontify variable definitions in 'local ...' & 'for ...' + Also, perform some basic syntax verification in those lines. Multi-line + constructs not supported yet. + +** fix indentation for blocks starting on continued lines + local foo = + { + bar, + baz + } ^ + 1. these lines should be indented properly now + ^ + 2. the following lines should be unindented properly now + +** extend imenu-generic-expression + Now it matches 'foo = function(...)' function definitions diff -Nru lua-mode-20110121/README lua-mode-20130419/README --- lua-mode-20110121/README 2011-01-21 12:14:43.000000000 +0000 +++ lua-mode-20130419/README 2013-04-19 07:27:32.000000000 +0000 @@ -1,7 +1,7 @@ Lua-mode ======== -Maintainer: Reuben Thomas +Maintainer: immerrr lua-mode is an Emacs major mode for editing Lua files. For documentation, including installation instructions, please see @@ -10,4 +10,4 @@ Please send bug reports and suggestions to the maintainer, or use the trackers at github: -http://github.com/rrthomas/lua-mode +http://github.com/immerrr/lua-mode diff -Nru lua-mode-20110121/README.md lua-mode-20130419/README.md --- lua-mode-20110121/README.md 1970-01-01 00:00:00.000000000 +0000 +++ lua-mode-20130419/README.md 2013-04-19 07:27:32.000000000 +0000 @@ -0,0 +1,65 @@ +# Lua mode + +**lua-mode** is a major mode for editing Lua sources in Emacs. After a rather long hiatus, it's back in active development stage, so make sure to visit [homepage](https://github.com/immerrr/lua-mode) frequently enough. + +If you have a problem or a suggestion about **lua-mode**, please, let me know about it via github's [Issue Tracker](https://github.com/immerrr/lua-mode/issues). + +## INSTALLATION + +### EL-GET INSTALLATION + +[El-get](https://github.com/dimitri/el-get) is a package manager which greatly simplifies adding +modules to your Emacs and keeping them up-to-date. Once you have **el-get** set up, installing +**lua-mode** can be done with + + el-get-install "lua-mode" + +and updating is no more than + + el-get-update "lua-mode"` + +Please, consult with [el-get documentation](https://github.com/dimitri/el-get/blob/master/README.md) for further information. + +### MANUAL INSTALLATION + +To install, you need to make sure that `lua-mode.el` is on your load-path (and optionally byte-compile +it) and to set up Emacs to automatically enable **lua-mode** for `*.lua` files or ones that contain lua +hash-bang line (`#!/usr/bin/lua`). Putting this snippet to `.emacs` should be enough in most cases: +```lisp + ;;;; This snippet enables lua-mode + + ;; This line is not necessary, if lua-mode.el is already on your load-path + (add-to-list 'load-path "/path/to/directory/where/lua-mode-el/resides") + + (autoload 'lua-mode "lua-mode" "Lua editing mode." t) + (add-to-list 'auto-mode-alist '("\\.lua$" . lua-mode)) + (add-to-list 'interpreter-mode-alist '("lua" . lua-mode)) +``` +## USAGE + +**lua-mode** supports some formatting and sending of lines/regions/files to a Lua interpreter. An +interpreter (see variable `lua-default-application`) will be started if you try to send some code and none +is running. You can use the process-buffer (named after the application you chose) as if it were an +interactive shell. See the documentation for `comint.el` for details. + +Lua-mode also works with Hide Show minor mode (see ``hs-minor-mode``). + +## TODO +Currently, there're a lot of features that need fixing (or reimplementing), including but not limited to: + +1. implementing autotesting of indentation engine +1. supporting line-continuation commas +1. fixing close-brace/paren positioning +1. fix syntax handling of multiline comments/literals (including both highlighting & indentation) +1. implementing a crisp scheme of customizing the way lua-mode indents the code +1. cleaning up existing code +1. extending docs & comments + +## CEDET/Semantic integration +Also, there's a rather distant goal to integrate lua-mode with [cedet's semantic](http://cedet.sourceforge.net/semantic.shtml). This would mean an almost complete rewrite, but I think the challenge is worth it. There's a slightest concern about the overhead brought by this dependency, but **semantic** is already being shipped with Emacs, so there might be no problem after all. + +### Use wisent-generated grammar +First stage would be to rewrite syntax handling with help of **semantic/wisent**-generated parser based on the actual Lua grammar. Currently, syntax analysis is done ad-hoc and, despite the model is oversimplified and doesn't treat corner situation well, it's still very tricky and really hard to grasp. + +### Extend cedet/semantic facilities onto Lua +And there's the cherry on the pie: after completing the wisent-generated parser, the next step will be to provide semantic with all the information it needs to do it's magic of code analysis and generation. diff -Nru lua-mode-20110121/TODO lua-mode-20130419/TODO --- lua-mode-20110121/TODO 1970-01-01 00:00:00.000000000 +0000 +++ lua-mode-20130419/TODO 2013-04-19 07:27:32.000000000 +0000 @@ -0,0 +1,92 @@ +# -*- mode: org -*- +#+PROPERTY: COOKIE_DATA todo recursive + +* Useful resources + - Lua5.1 + - [[http://www.lua.org/manual/5.1/manual.html#8][bnf syntax]] + - [[http://www.lua.org/manual/5.1/index.html#index][function index]] + - Lua5.2 + - [[http://www.lua.org/manual/5.2/manual.html#9][syntax]] + - [[http://www.lua.org/manual/5.2/#index][function index]] + + +* TODO Fontify function parameters as variable names +* TODO Fontify variable names in line-continuation statements + - local + foo,bar,baz = xyz,xyyz,xyyz + - function xyzzy( + foo, + bar, + baz + ) + +* TODO left-shift parenthesis that closes function parameter block + local foobar = function( + param1, + param2, + param3 + ) + dosmth() + end + +* TODO issue #33 + a = + { + } + b = + { + } + +* TODO finish works in lua-compat branch for Emacs21/Emacs22 + +* TODO put text property 'lua-last-stmt-begin + - continuation-line detection becomes a one-liner + - multiline fontification becomes easier + - beginning-of-defun is a one-liner + + - if property value is a marker, it will get updated on each edit, + that's not necessary and might cause performance impact + - if property value is an integer, it needs to be updated before use + 1. `buffer-access-fontify-functions' / `buffer-access-fontified-property' + 2. maintain `stmt-begin-up-to-date-pos' point + - after-change function sets `stmt-begin-up-to-date-pos' to + (line-beginning-position) + - (defun get-stmt-beginning-pos (pos) ...) + - (when (< stmt-begin-up-to-date-pos pos) + ... + (setq stmt-begin-up-to-date-pos pos)) + - return property at position pos + +* TODO Support line-continuation statements [0/2] + - [ ] assignments + x,y = long_value, + other_long_value + - [ ] `for ... do' operators + +* TODO update site [0/2] +** TODO reuse section-aware template +** TODO add section for release downloading + +* TODO [#C] Improve lua-prefix-key machinery [0/2] + - [ ] add docs + - [ ] improve setting prefix-key + Currently, prefix-key is parsed from a macro string (which may + contain more than one key) and all keys but the first one are + silently removed. Maybe there's a better way to do it. + +* DONE Fix braces/parens that reduce indent by const offset + this causes wrong behavior when indented block was + anchored to first line, e.g. + my_func( hello, + world + ) + +* DONE lua-mark-all-multiline-literals [5/5] + - [X] process --[[ as multiline comment + - [X] process ---[[ as single-line comment + distinguish by syntax state of last dash before square bracket + - [X] don't recognize [[ in comments or literals + - [X] extend region backwards if BEGIN is inside multiline literal + - [X] don't change buffer-modified-p state + +* TODO [#C] Redo syntax engine using semantic/wisent diff -Nru lua-mode-20110121/debian/changelog lua-mode-20130419/debian/changelog --- lua-mode-20110121/debian/changelog 2011-03-22 06:16:28.000000000 +0000 +++ lua-mode-20130419/debian/changelog 2013-08-06 14:41:19.000000000 +0000 @@ -1,3 +1,31 @@ +lua-mode (20130419-3) unstable; urgency=low + + * Don't build for emacs21, xemacs21 (Closes: #718826, #718827) + + -- Hilko Bengen Tue, 06 Aug 2013 16:41:04 +0200 + +lua-mode (20130419-2) unstable; urgency=low + + * Fixed install file (Closes: #718811) + + -- Hilko Bengen Mon, 05 Aug 2013 22:26:40 +0200 + +lua-mode (20130419-1) unstable; urgency=low + + * Adopted package (Closes: #628938) + * New upstream version + - No longer uses last-command-char which has been deprecated since + Emacs 19.34 (Closes: #705634) + - Correctly indents/aligns multi-line conditions (Closes: #546467) + - Correctly highlights multi-line comments (Closes: #602301) + * Updated watch file + * Various cleanups + - Bumped Standards-Version, updated Homepage, stripped + build-dependencies + - Simplified debian/rules + + -- Hilko Bengen Thu, 01 Aug 2013 18:26:00 +0200 + lua-mode (20110121-1) unstable; urgency=low * New upstream version, which includes patch multiline-strings.diff. diff -Nru lua-mode-20110121/debian/control lua-mode-20130419/debian/control --- lua-mode-20110121/debian/control 2011-03-22 06:08:39.000000000 +0000 +++ lua-mode-20130419/debian/control 2013-08-01 16:38:01.000000000 +0000 @@ -1,16 +1,15 @@ Source: lua-mode Section: editors Priority: optional -Maintainer: Jens Peter Secher -Build-Depends: debhelper (>= 7), emacs23 | emacsen -Standards-Version: 3.9.1 -Vcs-Hg: http://hg.debian.org/hg/collab-maint/lua-mode -Homepage: https://github.com/rrthomas/lua-mode +Maintainer: Hilko Bengen +Build-Depends: debhelper (>= 7), +Standards-Version: 3.9.4 +Homepage: https://github.com/immerrr/lua-mode Package: lua-mode Architecture: all -Depends: emacs23 | emacsen, ${misc:Depends} -Enhances: lua50, lua5.1 +Depends: emacs24 | emacsen, ${misc:Depends} +Enhances: lua50, lua5.1, lua5.2 Description: Emacs mode for editing Lua programs This emacs mode provides syntax highlighting and automatic indentation for Lua, as well as sending lines/regions/files to a lua diff -Nru lua-mode-20110121/debian/emacsen-install lua-mode-20130419/debian/emacsen-install --- lua-mode-20110121/debian/emacsen-install 2011-03-22 06:06:01.000000000 +0000 +++ lua-mode-20130419/debian/emacsen-install 2013-08-06 14:37:41.000000000 +0000 @@ -5,9 +5,9 @@ PACKAGE=lua-mode # Do nothing if generic flavor. -if [ ${FLAVOR} = emacs ]; then - exit 0; -fi +case ${FLAVOR} in + emacs|emacs21|xemacs*) exit 0 ;; +esac echo install/${PACKAGE}: Handling install for emacsen flavor ${FLAVOR} diff -Nru lua-mode-20110121/debian/install lua-mode-20130419/debian/install --- lua-mode-20110121/debian/install 1970-01-01 00:00:00.000000000 +0000 +++ lua-mode-20130419/debian/install 2013-08-05 20:25:33.000000000 +0000 @@ -0,0 +1 @@ +lua-mode.el usr/share/emacs/site-lisp/lua-mode diff -Nru lua-mode-20110121/debian/rules lua-mode-20130419/debian/rules --- lua-mode-20110121/debian/rules 2011-03-22 06:06:01.000000000 +0000 +++ lua-mode-20130419/debian/rules 2013-08-01 16:37:46.000000000 +0000 @@ -1,38 +1,4 @@ #!/usr/bin/make -f -PACKAGE=lua-mode - -get-orig-source: - wget http://luaforge.net/frs/download.php/4628/lua-mode.el -clean: - dh_testdir - dh_testroot - dh_clean - -build: - -install: build - dh_testdir - dh_testroot - dh_prep - dh_installdirs - dh_install lua-mode.el usr/share/emacs/site-lisp/$(PACKAGE) - -binary-indep: build install - dh_testdir - dh_testroot - dh_installchangelogs - dh_installdocs - dh_installemacsen - dh_compress - dh_fixperms - dh_installdeb - dh_gencontrol - dh_md5sums - dh_builddeb - -binary-arch: build install - -binary: binary-indep binary-arch - -.PHONY: get-orig-source build clean install binary-indep binary-arch binary +%: + dh $@ diff -Nru lua-mode-20110121/debian/watch lua-mode-20130419/debian/watch --- lua-mode-20110121/debian/watch 2011-03-22 06:06:01.000000000 +0000 +++ lua-mode-20130419/debian/watch 2013-08-01 09:57:46.000000000 +0000 @@ -1,2 +1,3 @@ version=3 -http://luaforge.net/frs/?group_id=185&release_id=622 /frs/download.php/.*/lua-mode-(.*).tar.gz +https://github.com/immerrr/lua-mode/releases .*/rel-(.*)\.tar\.gz + diff -Nru lua-mode-20110121/lua-mode.el lua-mode-20130419/lua-mode.el --- lua-mode-20110121/lua-mode.el 2011-01-21 12:14:43.000000000 +0000 +++ lua-mode-20130419/lua-mode.el 2013-04-19 07:27:32.000000000 +0000 @@ -1,8 +1,7 @@ ;;; lua-mode.el --- a major-mode for editing Lua scripts -;; Copyright (C) 1997, 2001, 2004, 2006, 2007, 2010, 2011 Free Software Foundation, Inc. - -;; Author: 2010-2011 Reuben Thomas +;; Author: 2011-2013 immerrr +;; 2010-2011 Reuben Thomas ;; 2006 Juergen Hoetzel ;; 2004 various (support for Lua 5 and byte compilation) ;; 2001 Christian Vogler @@ -11,8 +10,10 @@ ;; with tons of assistance from ;; Paul Du Bois and ;; Aaron Smith . -;; URL: http://lua-mode.luaforge.net/ -;; Version: 20110121 +;; +;; URL: http://immerrr.github.com/lua-mode +;; Version: 20130419 +;; ;; This file is NOT part of Emacs. ;; ;; This program is free software; you can redistribute it and/or @@ -32,9 +33,25 @@ ;; Keywords: languages, processes, tools +;; This field is expanded to commit SHA, date & associated heads/tags during +;; archive creation. +;; Revision: 040bc8f (Fri, 19 Apr 2013 11:27:32 +0400 (rel-20130419)) +;; ;;; Commentary: +;; Thanks to d87 for an idea of highlighting lua +;; builtins/numbers + +;; Thanks to Vedat Hallac for sharing some of +;; his fixes and updates to core indentation logics + +;; Thanks to Rafael Sanchez for patch +;; adding lua-mode to interpreter-mode-alist + +;; Thanks to Leonardo Etcheverry for enabling +;; narrow-to-defun functionality + ;; Thanks to Tobias Polzin for function indenting ;; patch: Indent "(" like "{" @@ -80,15 +97,86 @@ ;; You can customise the keybindings either by setting `lua-prefix-key' ;; or by putting the following in your .emacs -;; (setq lua-mode-map (make-sparse-keymap)) -;; and ;; (define-key lua-mode-map ) ;; for all the functions you need. ;;; Code: +(eval-when-compile + (require 'cl)) + (require 'comint) +(eval-and-compile + ;; Backward compatibility for Emacsen < 24.1 + (defalias 'lua--prog-mode + (if (fboundp 'prog-mode) 'prog-mode 'fundamental-mode)) + + (defalias 'lua--cl-assert + (if (fboundp 'cl-assert) 'cl-assert 'assert)) + + (defalias 'lua--cl-labels + (if (fboundp 'cl-labels) 'cl-labels 'flet)) + + ;; for Emacsen < 22.1 + (defalias 'lua--with-no-warnings + (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn)) + + ;; provide backward compatibility for Emacs < 23.2 + ;; called-interactively-p receives an argument starting from Emacs 23.2 + ;; In Emacs 22 & Emacs 23.1 it didn't expect an argument + ;; In Emacs 21 it was called interactively-p + (condition-case nil + (progn (called-interactively-p nil) + ;; if first call succeeds, make lua-called-interactively-p an alias + (defalias 'lua--called-interactively-p 'called-interactively-p)) + + (wrong-number-of-arguments + ;; wrong number of arguments means it's 22.1 <= Emacs < 23.2 + ;; + ;; Newer and smarter Emacsen will warn about obsolete functions + ;; and/or wrong number of arguments. Turning these warnings off, + ;; since it's backward-compatibility-oriented code anyway. + (lua--with-no-warnings + (defun lua--called-interactively-p (kind) + "Return t if containing function was called interactively. + +This function provides lua-mode backward compatibility for +pre-23.2 Emacsen." + (if (eq kind 'interactive) + (interactive-p) + (called-interactively-p))))) + + ;; if not, it's probably < 22.1, provide partial compatibility + ;; + ;; Once again, turning obsolete-function warnings off (see above). + (error + (lua--with-no-warnings + (defun lua--called-interactively-p (&rest opts) + "Return t if containing function was called interactively. + +This function provides lua-mode backward compatibility for pre-22 +Emacsen." + (interactive-p))))) + + ;; backward compatibility for Emacsen < 23.3 + ;; Emacs 23.3 introduced with-silent-modifications macro + (if (fboundp 'with-silent-modifications) + (defalias 'lua--with-silent-modifications 'with-silent-modifications) + + (defmacro lua--with-silent-modifications (&rest body) + "Execute BODY, pretending it does not modifies the buffer. + +This is a reimplementation of macro `with-silent-modifications' +for Emacsen that doesn't contain one (pre-23.3)." + `(let ((old-modified-p (buffer-modified-p)) + (inhibit-modification-hooks t) + (buffer-undo-list t)) + + (unwind-protect + ,@body + (set-buffer-modified-p old-modified-p)))))) + ;; Local variables (defgroup lua nil "Major mode for editing lua code." @@ -137,19 +225,66 @@ (defvar lua-process-buffer nil "Buffer used for communication with Lua subprocess") -(defvar lua-mode-map nil - "Keymap used with lua-mode.") +(defun lua--customize-set-prefix-key (prefix-key-sym prefix-key-val) + (lua--cl-assert (eq prefix-key-sym 'lua-prefix-key)) + (set prefix-key-sym (if (and prefix-key-val (> (length prefix-key-val) 0)) + ;; read-kbd-macro returns a string or a vector + ;; in both cases (elt x 0) is ok + (elt (read-kbd-macro prefix-key-val) 0))) + (if (fboundp 'lua-prefix-key-update-bindings) + (lua-prefix-key-update-bindings))) + +(defcustom lua-prefix-key "\C-c" + "Prefix for all lua-mode commands." + :type 'string + :group 'lua + :set 'lua--customize-set-prefix-key + :get '(lambda (sym) + (let ((val (eval sym))) (if val (single-key-description (eval sym)) "")))) + +(defvar lua-mode-menu (make-sparse-keymap "Lua") + "Keymap for lua-mode's menu.") + +(defvar lua-prefix-mode-map + (eval-when-compile + (let ((result-map (make-sparse-keymap))) + (mapc (lambda (key_defn) + (define-key result-map (read-kbd-macro (car key_defn)) (cdr key_defn))) + '(("C-l" . lua-send-buffer) + ("C-f" . lua-search-documentation))) + result-map)) + "Keymap that is used to define keys accessible by `lua-prefix-key'. + +If the latter is nil, the keymap translates into `lua-mode-map' verbatim.") + +(defvar lua-mode-map + (let ((result-map (make-sparse-keymap)) + prefix-key) + (mapc (lambda (key_defn) + (define-key result-map (read-kbd-macro (car key_defn)) (cdr key_defn))) + ;; here go all the default bindings + ;; backquote enables evaluating certain symbols by comma + `(("}" . lua-electric-match) + ("]" . lua-electric-match) + (")" . lua-electric-match))) + (define-key result-map [menu-bar lua-mode] (cons "Lua" lua-mode-menu)) + + ;; FIXME: see if the declared logic actually works + ;; handle prefix-keyed bindings: + ;; * if no prefix, set prefix-map as parent, i.e. + ;; if key is not defined look it up in prefix-map + ;; * if prefix is set, bind the prefix-map to that key + (if (boundp 'lua-prefix-key) + (define-key result-map (vector lua-prefix-key) lua-prefix-mode-map) + (set-keymap-parent result-map lua-prefix-mode-map)) + result-map) + "Keymap used in lua-mode buffers.") (defvar lua-electric-flag t "If t, electric actions (like automatic reindentation) will happen when an electric key like `{' is pressed") (make-variable-buffer-local 'lua-electric-flag) -(defcustom lua-prefix-key "\C-c" - "Prefix for all lua-mode commands." - :type 'string - :group 'lua) - (defcustom lua-prompt-regexp "[^\n]*\\(>[\t ]+\\)+$" "Regexp which matches the Lua program's prompt." :type 'regexp @@ -161,6 +296,12 @@ :type 'regexp :group 'lua) +(defcustom lua-indent-string-contents nil + "If non-nil, contents of multiline string will be indented. +Otherwise leading amount of whitespace on each line is preserved." + :group 'lua + :type 'boolean) + (defcustom lua-jump-on-traceback t "*Jump to innermost traceback location in *lua* buffer. When this variable is non-nil and a traceback occurs when running Lua code in a @@ -169,8 +310,10 @@ :type 'boolean :group 'lua) -(defvar lua-mode-hook nil - "Hooks called when Lua mode fires up.") +(defcustom lua-mode-hook nil + "Hooks called when Lua mode fires up." + :type 'hook + :group 'lua) (defvar lua-region-start (make-marker) "Start of special region for Lua communication.") @@ -178,9 +321,6 @@ (defvar lua-region-end (make-marker) "End of special region for Lua communication.") -(defvar lua-mode-menu (make-sparse-keymap "Lua") - "Keymap for lua-mode's menu.") - (defvar lua-emacs-menu '(["Restart With Whole File" lua-restart-with-whole-file t] ["Kill Process" lua-kill-process t] @@ -198,57 +338,276 @@ ["Search Documentation" lua-search-documentation t]) "Emacs menu for Lua mode.") +;; the whole defconst is inside eval-when-compile, because it's later referenced +;; inside another eval-and-compile block +(eval-and-compile + (defconst + lua--builtins + (let* + ((modules + '("_G" "_VERSION" "assert" "collectgarbage" "dofile" "error" "getfenv" + "getmetatable" "ipairs" "load" "loadfile" "loadstring" "module" + "next" "pairs" "pcall" "print" "rawequal" "rawget" "rawlen" "rawset" + "require" "select" "setfenv" "setmetatable" "tonumber" "tostring" + "type" "unpack" "xpcall" "self" + ("bit32" . ("arshift" "band" "bnot" "bor" "btest" "bxor" "extract" + "lrotate" "lshift" "replace" "rrotate" "rshift")) + ("coroutine" . ("create" "resume" "running" "status" "wrap" "yield")) + ("debug" . ("debug" "getfenv" "gethook" "getinfo" "getlocal" + "getmetatable" "getregistry" "getupvalue" "getuservalue" + "setfenv" "sethook" "setlocal" "setmetatable" + "setupvalue" "setuservalue" "traceback" "upvalueid" + "upvaluejoin")) + ("io" . ("close" "flush" "input" "lines" "open" "output" "popen" + "read" "stderr" "stdin" "stdout" "tmpfile" "type" "write")) + ("math" . ("abs" "acos" "asin" "atan" "atan2" "ceil" "cos" "cosh" + "deg" "exp" "floor" "fmod" "frexp" "huge" "ldexp" "log" + "log10" "max" "min" "modf" "pi" "pow" "rad" "random" + "randomseed" "sin" "sinh" "sqrt" "tan" "tanh")) + ("os" . ("clock" "date" "difftime" "execute" "exit" "getenv" + "remove" "rename" "setlocale" "time" "tmpname")) + ("package" . ("config" "cpath" "loaded" "loaders" "loadlib" "path" + "preload" "searchers" "searchpath" "seeall")) + ("string" . ("byte" "char" "dump" "find" "format" "gmatch" "gsub" + "len" "lower" "match" "rep" "reverse" "sub" "upper")) + ("table" . ("concat" "insert" "maxn" "pack" "remove" "sort" "unpack" + ))))) + + ;; This code uses \\< and \\> to delimit builtin symbols instead of + ;; \\_< and \\_>, because -- a necessity -- '.' syntax class is hacked + ;; to 'symbol' and \\_> won't detect a symbol boundary in 'foo.bar' and + ;; -- sufficiency -- conveniently, underscore '_' is hacked to count as + ;; word constituent, but only for font-locking. Neither of these hacks + ;; makes sense to me, I'm going to wipe them out as soon as I'm sure + ;; that indentation won't get hurt. --immerrr + ;; + (lua--cl-labels + ((module-name-re (x) + (concat "\\(?1:\\<" + (if (listp x) (car x) x) + "\\>\\)")) + (module-members-re (x) (if (listp x) + (concat "\\(?:[ \t]*\\.[ \t]*" + "\\<\\(?2:" + (regexp-opt (cdr x)) + "\\)\\>\\)?") + ""))) + + (concat + ;; common prefix - beginning-of-line or neither of [ '.', ':' ] to + ;; exclude "foo.string.rep" + "\\(?:\\`\\|[^:. \n\t]\\)" + ;; optional whitespace + "[ \n\t]*" + "\\(?:" + ;; any of modules/functions + (mapconcat (lambda (x) (concat (module-name-re x) + (module-members-re x))) + modules + "\\|") + "\\)")))) + + "A regexp that matches lua builtin functions & variables. + +This is a compilation of 5.1 and 5.2 builtins taken from the +index of respective Lua reference manuals.") + +(defun lua-make-delimited-matcher (elt-regexp sep-regexp end-regexp) + "Construct matcher function for `font-lock-keywords' to match a sequence. + +It's supposed to match sequences with following EBNF: + +ELT-REGEXP { SEP-REGEXP ELT-REGEXP } END-REGEXP + +The sequence is parsed one token at a time. If non-nil is +returned, `match-data' will have one or more of the following +groups set according to next matched token: + +1. matched element token +2. unmatched garbage characters +3. misplaced token (i.e. SEP-REGEXP when ELT-REGEXP is expected) +4. matched separator token +5. matched end token + +Blanks & comments between tokens are silently skipped. +Groups 6-9 can be used in any of argument regexps." + (lexical-let* + ((delimited-matcher-re-template + "\\=\\(?2:.*?\\)\\(?:\\(?%s:\\(?4:%s\\)\\|\\(?5:%s\\)\\)\\|\\(?%s:\\(?1:%s\\)\\)\\)") + ;; There's some magic to this regexp. It works as follows: + ;; + ;; A. start at (point) + ;; B. non-greedy match of garbage-characters (?2:) + ;; C. try matching separator (?4:) or end-token (?5:) + ;; D. try matching element (?1:) + ;; + ;; Simple, but there's a trick: pt.C and pt.D are embraced by one more + ;; group whose purpose is determined only after the template is + ;; formatted (?%s:): + ;; + ;; - if element is expected, then D's parent group becomes "shy" and C's + ;; parent becomes group 3 (aka misplaced token), so if D matches when + ;; an element is expected, it'll be marked with warning face. + ;; + ;; - if separator-or-end-token is expected, then it's the opposite: + ;; C's parent becomes shy and D's will be matched as misplaced token. + (elt-expected-re (format delimited-matcher-re-template + 3 sep-regexp end-regexp "" elt-regexp)) + (sep-or-end-expected-re (format delimited-matcher-re-template + "" sep-regexp end-regexp 3 elt-regexp))) + + (lambda (end) + (let* ((prev-elt-p (match-beginning 1)) + (prev-sep-p (match-beginning 4)) + (prev-end-p (match-beginning 5)) + + (regexp (if prev-elt-p sep-or-end-expected-re elt-expected-re)) + (comment-start (lua-comment-start-pos (syntax-ppss))) + (parse-stop end)) + + ;; If token starts inside comment, or end-token was encountered, stop. + (when (and (not comment-start) + (not prev-end-p)) + ;; Skip all comments & whitespace. forward-comment doesn't have boundary + ;; argument, so make sure point isn't beyond parse-stop afterwards. + (while (and (< (point) end) + (forward-comment 1))) + (goto-char (min (point) parse-stop)) + + ;; Reuse comment-start variable to store beginning of comment that is + ;; placed before line-end-position so as to make sure token search doesn't + ;; enter that comment. + (setq comment-start + (lua-comment-start-pos + (save-excursion + (parse-partial-sexp (point) parse-stop + nil nil nil 'stop-inside-comment))) + parse-stop (or comment-start parse-stop)) + + ;; Now, let's match stuff. If regular matcher fails, declare a span of + ;; non-blanks 'garbage', and the next iteration will start from where the + ;; garbage ends. If couldn't match any garbage, move point to the end + ;; and return nil. + (or (re-search-forward regexp parse-stop t) + (re-search-forward "\\(?1:\\(?2:[^ \t]+\\)\\)" parse-stop 'skip) + (prog1 nil (goto-char end)))))))) + +(defconst lua-local-defun-regexp + ;; Function matchers are very crude, need rewrite at some point. + (rx (or (seq (regexp "\\(?:\\_\\)") + (* blank) + (? (regexp "\\(?1:\\_<[[:alpha:]][[:alnum:]]*\\_>\\)")) + (regexp "\\(?2:.*\\)")) + (seq (? (regexp "\\(?1:\\_<[[:alpha:]][[:alnum:]]*\\_>\\)")) + (* blank) "=" (* blank) + (regexp "\\(?:\\_\\)") + (regexp "\\(?2:.*\\)"))))) + (defvar lua-font-lock-keywords - (eval-when-compile - (list - ;; Handle variable names - ;; local blalba = - ;; ^^^^^^ - '("\\(local[ \t]+\\(\\sw+\\)[ \t]*=\\)" - (2 font-lock-variable-name-face)) - - ;; Function name declarations. - '("^[ \t]*\\_<\\(\\(local[ \t]+\\)?function\\)\\_>[ \t]+\\(\\(\\sw:\\|\\sw\\.\\|\\sw_\\|\\sw\\)+\\)" - (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t)) - - ;; Handle function names in assignments - '("\\(\\(\\sw:\\|\\sw\\.\\|\\sw_\\|\\sw\\)+\\)[ \t]*=[ \t]*\\(function\\)\\_>" - (1 font-lock-function-name-face nil t) (3 font-lock-keyword-face)) - - ;; Multi-line string literals. - '("[^-]\\[=*\\[\\(\\([^]]\\|][^]]\\|]=+[^]]\\)*?\\)]=*]" - (1 font-lock-string-face t)) - - ;; Keywords. - (concat "\\_<" - (regexp-opt '("and" "break" "do" "else" "elseif" "end" "false" - "for" "function" "if" "in" "local" "nil" "not" - "or" "repeat" "return" "then" "true" "until" - "while") t) - "\\_>") + `(;; highlight the hash-bang line "#!/foo/bar/lua" as comment + ("^#!.*$" . font-lock-comment-face) - "Default expressions to highlight in Lua mode."))) + ;; Keywords. + (,(rx symbol-start + (or "and" "break" "do" "else" "elseif" "end" "false" + "for" "function" "if" "in" "local" "nil" "not" + "or" "repeat" "return" "then" "true" "until" + "while") + symbol-end) + . font-lock-keyword-face) + + ;; Highlight lua builtin functions and variables + (,lua--builtins + (1 font-lock-builtin-face) (2 font-lock-builtin-face nil noerror)) + + ;; hexadecimal numbers + ("\\_<0x[[:xdigit:]]+\\_>" . font-lock-constant-face) + + ;; regular numbers + ;; + ;; This regexp relies on '.' being symbol constituent. Whenever this + ;; changes, the regexp needs revisiting --immerrr + (, (rx symbol-start + ;; make a digit on either side of dot mandatory + (or (seq (+ num) (? ".") (* num)) + (seq (* num) (? ".") (+ num))) + (? (regexp "[eE][+-]?") (+ num)) + symbol-end) + . font-lock-constant-face) + + ("^[ \t]*\\_" + (,(lua-make-delimited-matcher "\\_<[[:alpha:]_][[:alnum:]_]*\\_>" "," + "\\(?:\\_\\|=\\(?:[^=]\\|$\\)\\)") + nil nil + (1 font-lock-variable-name-face nil noerror) + (2 font-lock-warning-face t noerror) + (3 font-lock-warning-face t noerror))) + + ;; Handle local variable/function names + ;; local blalba, xyzzy = + ;; ^^^^^^ ^^^^^ + ;; + ;; local function foobar(x,y,z) + ;; ^^^^^^ + ;; local foobar = function(x,y,z) + ;; ^^^^^^ + ("^[ \t]*\\_" + (0 font-lock-keyword-face) + + ((lambda (end) + (re-search-forward + (rx point (* blank) (regexp ,lua-local-defun-regexp)) end t)) + nil nil + (1 font-lock-function-name-face nil noerror)) + + (,(lua-make-delimited-matcher "\\_<[[:alpha:]_][[:alnum:]_]*\\_>" "," + "=\\(?:[^=]\\|$\\)") + nil nil + (1 font-lock-variable-name-face nil noerror) + (2 font-lock-warning-face t noerror) + (3 font-lock-warning-face t noerror))) + + ;; Function matchers are very crude, need rewrite at some point. + ;; Function name declarations. + ("^[ \t]*\\_[ \t]+\\([[:alnum:]_]+\\(?:\\.[[:alnum:]_]+\\)*\\(?::[[:alnum:]_]+\\)?\\)" + (1 font-lock-function-name-face)) + + ;; Function matchers are very crude, need rewrite at some point. + ;; Handle function names in assignments + ("^[ \t]*\\([[:alnum:]_]+\\(?:\\.[[:alnum:]_]+\\)*\\(?::[[:alnum:]_]+\\)?\\)[ \t]*=[ \t]*\\_" + (1 font-lock-function-name-face))) + + "Default expressions to highlight in Lua mode.") (defvar lua-imenu-generic-expression - '((nil "^[ \t]*\\(?:local[ \t]+\\)?function[ \t]+\\(\\(\\sw:\\|\\sw_\\|\\sw\\.\\|\\sw\\)+\\)" 1)) + ;; This regexp matches expressions which look like function + ;; definitions, but are not necessarily allowed by Lua syntax. This + ;; is done on purpose to avoid frustration when making a small error + ;; might cause a function get hidden from imenu index. --immerrr + '((nil "^[ \t]*\\(?:local[ \t]+\\)?function[ \t]+\\([[:alnum:]_:.]+\\)" 1) + (nil "^[ \t]*\\(?:local[ \t]+\\)?\\(\\_<[[:alnum:]_:.]+\\_>\\)[ \t]*=\[ \t]*\\_" 1)) "Imenu generic expression for lua-mode. See `imenu-generic-expression'.") -(defvar lua-mode-abbrev-table nil - "Abbreviation table used in lua-mode buffers.") - (defvar lua-sexp-alist '(("then" . "end") ("function" . "end") ("do" . "end"))) -(define-abbrev-table 'lua-mode-abbrev-table - '( - ("end" "end" lua-indent-line 0) - ("else" "else" lua-indent-line 0) - ("elseif" "elseif" lua-indent-line 0) - )) +(defvar lua-mode-abbrev-table nil + "Abbreviation table used in lua-mode buffers.") -(defconst lua-indent-whitespace " \t" - "Character set that constitutes whitespace for indentation in lua.") +(define-abbrev-table 'lua-mode-abbrev-table + ;; Emacs 23 introduced :system property that prevents abbrev + ;; entries from being written to file specified by abbrev-file-name + ;; + ;; Emacs 22 and earlier had this functionality implemented + ;; by simple nil/non-nil flag as positional parameter + (if (>= emacs-major-version 23) + '(("end" "end" lua-indent-line :system t) + ("else" "else" lua-indent-line :system t) + ("elseif" "elseif" lua-indent-line :system t)) + '(("end" "end" lua-indent-line nil 'system) + ("else" "else" lua-indent-line nil 'system) + ("elseif" "elseif" lua-indent-line nil 'system)))) (eval-and-compile (defalias 'lua-make-temp-file @@ -261,148 +620,254 @@ (temp-directory) temporary-file-directory)))))) -;;;###autoload -(defun lua-mode () - "Major mode for editing Lua code. -The following keys are bound: -\\{lua-mode-map} -" - (interactive) - (let ((switches nil) - s) - (kill-all-local-variables) - (setq major-mode 'lua-mode) - (setq mode-name "Lua") - (setq comint-prompt-regexp lua-prompt-regexp) - (make-local-variable 'lua-default-command-switches) - (set (make-local-variable 'indent-line-function) 'lua-indent-line) - (set (make-local-variable 'comment-start) lua-comment-start) - (set (make-local-variable 'comment-start-skip) lua-comment-start-skip) - (set (make-local-variable 'font-lock-defaults) - '(lua-font-lock-keywords - nil nil ((?_ . "w")))) - (set (make-local-variable 'imenu-generic-expression) - lua-imenu-generic-expression) - (setq local-abbrev-table lua-mode-abbrev-table) - (abbrev-mode 1) - (make-local-variable 'lua-default-eval) - (or lua-mode-map - (lua-setup-keymap)) - (use-local-map lua-mode-map) - (set-syntax-table (copy-syntax-table)) - (modify-syntax-entry ?+ ".") +(defvar lua-mode-syntax-table + (with-syntax-table (copy-syntax-table) + ;; main comment syntax: begins with "--", ends with "\n" (modify-syntax-entry ?- ". 12") + (modify-syntax-entry ?\n ">") + + ;; main string syntax: bounded by ' or " + (modify-syntax-entry ?\' "\"") + (modify-syntax-entry ?\" "\"") + + ;; single-character binary operators: punctuation + (modify-syntax-entry ?+ ".") (modify-syntax-entry ?* ".") (modify-syntax-entry ?/ ".") (modify-syntax-entry ?^ ".") - ;; This might be better as punctuation, as for C, but this way you - ;; can treat table index as symbol. - (modify-syntax-entry ?. "_") ; e.g. `io.string' (modify-syntax-entry ?> ".") (modify-syntax-entry ?< ".") (modify-syntax-entry ?= ".") (modify-syntax-entry ?~ ".") - (modify-syntax-entry ?\n ">") - (modify-syntax-entry ?\' "\"") - (modify-syntax-entry ?\" "\"") - (if (and (featurep 'menubar) - current-menubar - (not (assoc "Lua" current-menubar))) - (progn - (set-buffer-menubar (copy-sequence current-menubar)) - (add-menu nil "Lua" lua-emacs-menu))) - ;; Append Lua menu to popup menu for Emacs. - (if (boundp 'mode-popup-menu) - (setq mode-popup-menu - (cons (concat mode-name " Mode Commands") lua-emacs-menu))) - - ;; hideshow setup - (unless (assq 'lua-mode hs-special-modes-alist) - (add-to-list 'hs-special-modes-alist - `(lua-mode - ,(regexp-opt (mapcar 'car lua-sexp-alist) 'words) ;start - ,(regexp-opt (mapcar 'cdr lua-sexp-alist) 'words) ;end - nil lua-forward-sexp))) - (run-hooks 'lua-mode-hook))) + + ;; '.' character might be better as punctuation, as in C, but this way you + ;; can treat table index as symbol, e.g. `io.string' + (modify-syntax-entry ?. "_") + (syntax-table)) + "`lua-mode' syntax table.") + +;;;###autoload +(define-derived-mode lua-mode lua--prog-mode "Lua" + "Major mode for editing Lua code." + :abbrev-table lua-mode-abbrev-table + :syntax-table lua-mode-syntax-table + :group 'lua + + (setq comint-prompt-regexp lua-prompt-regexp) + (make-local-variable 'lua-default-command-switches) + (set (make-local-variable 'font-lock-defaults) + `(lua-font-lock-keywords ;; keywords + nil ;; keywords-only + nil ;; case-fold + ;; Not sure, why '_' is a word constituent only when font-locking. + ;; --immerrr + ((?_ . "w")) ;; syntax-alist + nil ;; syntax-begin + ;; initialize font-lock buffer-local variables + (font-lock-syntactic-keywords . lua-font-lock-syntactic-keywords) + (font-lock-extra-managed-props . (syntax-table)) + (parse-sexp-lookup-properties . t) + ;; initialize the rest of buffer-local variables + (beginning-of-defun-function . lua-beginning-of-proc) + (end-of-defun-function . lua-end-of-proc) + (indent-line-function . lua-indent-line) + (comment-start . ,lua-comment-start) + (comment-start-skip . ,lua-comment-start-skip) + (imenu-generic-expression . ,lua-imenu-generic-expression))) + + ;; setup menu bar entry (XEmacs style) + (if (and (featurep 'menubar) + (boundp 'current-menubar) + (fboundp 'set-buffer-menubar) + (fboundp 'add-menu) + (not (assoc "Lua" current-menubar))) + (progn + (set-buffer-menubar (copy-sequence current-menubar)) + (add-menu nil "Lua" lua-emacs-menu))) + ;; Append Lua menu to popup menu for Emacs. + (if (boundp 'mode-popup-menu) + (setq mode-popup-menu + (cons (concat mode-name " Mode Commands") lua-emacs-menu))) + + ;; hideshow setup + (unless (assq 'lua-mode hs-special-modes-alist) + (add-to-list 'hs-special-modes-alist + `(lua-mode + ,(regexp-opt (mapcar 'car lua-sexp-alist) 'words) ;start + ,(regexp-opt (mapcar 'cdr lua-sexp-alist) 'words) ;end + nil lua-forward-sexp)))) + ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lua$" . lua-mode)) -(defun lua-setup-keymap () - "Set up keymap for Lua mode. -If the variable `lua-prefix-key' is nil, the bindings go directly -to `lua-mode-map', otherwise they are prefixed with `lua-prefix-key'." - (setq lua-mode-map (make-sparse-keymap)) - (define-key lua-mode-map [menu-bar lua-mode] - (cons "Lua" lua-mode-menu)) - (define-key lua-mode-map "}" 'lua-electric-match) - (define-key lua-mode-map "]" 'lua-electric-match) - (define-key lua-mode-map ")" 'lua-electric-match) - (define-key lua-mode-map (kbd "C-M-a") 'lua-beginning-of-proc) - (define-key lua-mode-map (kbd "C-M-e") 'lua-end-of-proc) - (define-key lua-mode-map (kbd "C-M-") 'lua-beginning-of-proc) - (define-key lua-mode-map (kbd "C-M-") 'lua-end-of-proc) - (let ((map (if lua-prefix-key - (make-sparse-keymap) - lua-mode-map))) - - ;; communication - (define-key map "\C-l" 'lua-send-buffer) - (define-key map "\C-f" 'lua-search-documentation) - (if lua-prefix-key - (define-key lua-mode-map lua-prefix-key map)) - )) +;;;###autoload +(add-to-list 'interpreter-mode-alist '("lua" . lua-mode)) (defun lua-electric-match (arg) "Insert character and adjust indentation." (interactive "P") - (insert-char last-command-char (prefix-numeric-value arg)) + (insert-char last-command-event (prefix-numeric-value arg)) (if lua-electric-flag (lua-indent-line)) (blink-matching-open)) ;; private functions -(defun lua-syntax-status () - "Returns the syntactic status of the character after the point." - (parse-partial-sexp (save-excursion (beginning-of-line) (point)) - (point))) +(defun lua-prefix-key-update-bindings () + (let (old-cons) + (if (eq lua-prefix-mode-map (keymap-parent lua-mode-map)) + ;; if prefix-map is a parent, delete the parent + (set-keymap-parent lua-mode-map nil) + ;; otherwise, look for it among children + (if (setq old-cons (rassoc lua-prefix-mode-map lua-mode-map)) + (delq old-cons lua-mode-map))) + + (if (null lua-prefix-key) + (set-keymap-parent lua-mode-map lua-prefix-mode-map) + (define-key lua-mode-map (vector lua-prefix-key) lua-prefix-mode-map)))) + +(defun lua-set-prefix-key (new-key-str) + "Changes `lua-prefix-key' properly and updates keymaps + +This function replaces previous prefix-key binding with a new one." + (interactive "sNew prefix key (empty string means no key): ") + (lua--customize-set-prefix-key 'lua-prefix-key new-key-str) + (message "Prefix key set to %S" (single-key-description lua-prefix-key)) + (lua-prefix-key-update-bindings)) -(defun lua-string-p () +(defun lua-string-p (&optional pos) "Returns true if the point is in a string." - (elt (lua-syntax-status) 3)) + (save-excursion (elt (syntax-ppss pos) 3))) + +(defun lua-comment-start-pos (parsing-state) + "Return position of comment containing current point. + +If point is not inside a comment, return nil." + (and parsing-state (nth 4 parsing-state) (nth 8 parsing-state))) -(defun lua-comment-p () +(defun lua-comment-p (&optional pos) "Returns true if the point is in a comment." - (elt (lua-syntax-status) 4)) + (save-excursion (elt (syntax-ppss pos) 4))) -(defun lua-comment-or-string-p () +(defun lua-comment-or-string-p (&optional pos) "Returns true if the point is in a comment or string." - (let ((parse-result (lua-syntax-status))) - (or (elt parse-result 3) (elt parse-result 4)))) + (save-excursion (let ((parse-result (syntax-ppss pos))) + (or (elt parse-result 3) (elt parse-result 4))))) + +(defun lua-comment-or-string-start-pos (&optional pos) + "Returns start position of string or comment which contains point. + +If point is not inside string or comment, return nil." + (save-excursion (elt (syntax-ppss pos) 8))) + +;; They're propertized as follows: +;; 1. generic-comment +;; 2. generic-string +;; 3. equals signs +(defconst lua-ml-begin-regexp + "\\(?:\\(?1:-\\)-\\[\\|\\(?2:\\[\\)\\)\\(?3:=*\\)\\[") + + +(defun lua-try-match-multiline-end (end) + "Try to match close-bracket for multiline literal around point. + +Basically, detect form of close bracket from syntactic +information provided at point and re-search-forward to it." + (let ((comment-or-string-start-pos (lua-comment-or-string-start-pos))) + ;; Is there a literal around point? + (and comment-or-string-start-pos + ;; It is, check if the literal is a multiline open-bracket + (save-excursion + (goto-char comment-or-string-start-pos) + (looking-at lua-ml-begin-regexp)) + + ;; Yes it is, look for it matching close-bracket. Close-bracket's + ;; match group is determined by match-group of open-bracket. + (re-search-forward + (format "]%s\\(?%s:]\\)" + (match-string-no-properties 3) + (if (match-beginning 1) 1 2)) + end 'noerror)))) + + +(defun lua-try-match-multiline-begin (limit) + "Try to match multiline open-brackets. + +Find next opening long bracket outside of any string/comment. +If none can be found before reaching LIMIT, return nil." + + (let (last-search-matched) + (while + ;; This loop will iterate skipping all multiline-begin tokens that are + ;; inside strings or comments ending either at EOL or at valid token. + (and (setq last-search-matched + (re-search-forward lua-ml-begin-regexp limit 'noerror)) + + ;; (1+ (match-beginning 0)) is required to handle triple-hyphen + ;; '---[[' situation: regexp matches starting from the second one, + ;; but it's not yet a comment, because it's a part of 2-character + ;; comment-start sequence, so if we try to detect if the opener is + ;; inside a comment from the second hyphen, it'll fail. But the + ;; third one _is_ inside a comment and considering it instead will + ;; fix the issue. --immerrr + (lua-comment-or-string-start-pos (1+ (match-beginning 0))))) + + last-search-matched)) + +(defun lua-match-multiline-literal-bounds (limit) + ;; First, close any multiline literal spanning from previous block. This will + ;; move the point accordingly so as to avoid double traversal. + (or (lua-try-match-multiline-end limit) + (lua-try-match-multiline-begin limit))) + +(defvar lua-font-lock-syntactic-keywords + '((lua-match-multiline-literal-bounds + (1 "!" nil noerror) + (2 "|" nil noerror)))) (defun lua-indent-line () "Indent current line for Lua mode. Return the amount the indentation changed by." - (let ((indent (max 0 (- (lua-calculate-indentation nil) - (lua-calculate-indentation-left-shift)))) - beg shift-amt + (let (indent (case-fold-search nil) + ;; save point as a distance to eob - it's invariant w.r.t indentation (pos (- (point-max) (point)))) - (beginning-of-line) - (setq beg (point)) - (skip-chars-forward lua-indent-whitespace) - (setq shift-amt (- indent (current-column))) - (when (not (zerop shift-amt)) - (delete-region beg (point)) + (back-to-indentation) + (if (lua-comment-or-string-p) + (setq indent (lua-calculate-string-or-comment-indentation)) ;; just restore point position + (setq indent (max 0 (lua-calculate-indentation nil)))) + + (when (not (equal indent (current-column))) + (delete-region (line-beginning-position) (point)) (indent-to indent)) + ;; If initial point was within line's indentation, ;; position after the indentation. Else stay at same point in text. (if (> (- (point-max) pos) (point)) (goto-char (- (point-max) pos))) - shift-amt + indent)) +(defun lua-calculate-string-or-comment-indentation () + "This function should be run when point at (current-indentation) is inside string" + (if (and (lua-string-p) (not lua-indent-string-contents)) + ;; if inside string and strings aren't to be indented, return current indentation + (current-indentation) + + ;; At this point, we know that we're inside comment, so make sure + ;; close-bracket is unindented like a block that starts after + ;; left-shifter. + (let ((left-shifter-p (looking-at "\\s *\\(?:--\\)?\\]\\(?1:=*\\)\\]"))) + (save-excursion + (goto-char (lua-comment-or-string-start-pos)) + (+ (current-indentation) + (if (and left-shifter-p + (looking-at (format "--\\[%s\\[" + (match-string-no-properties 1)))) + 0 + lua-indent-level)))))) + (defun lua-find-regexp (direction regexp &optional limit ignore-p) "Searches for a regular expression in the direction specified. Direction is one of 'forward and 'backward. @@ -417,14 +882,10 @@ (case-fold-search nil)) (catch 'found (while (funcall search-func regexp limit t) - (if (not (funcall ignore-func)) + (if (and (not (funcall ignore-func (match-beginning 0))) + (not (funcall ignore-func (match-end 0)))) (throw 'found (point))))))) -(defun lua-backwards-to-block-begin-or-end () - "Move backwards to nearest block begin or end. Returns nil if not successful." - (interactive) - (lua-find-regexp 'backward lua-block-regexp)) - (defconst lua-block-regexp (eval-when-compile (concat @@ -432,31 +893,33 @@ (regexp-opt '("do" "function" "repeat" "then" "else" "elseif" "end" "until") t) "\\_>\\)\\|" - (regexp-opt '("{" "(" "[" "]" ")" "}") t)) - - )) + (regexp-opt '("{" "(" "[" "]" ")" "}") t)))) (defconst lua-block-token-alist - ;; The absence of "else" is deliberate. This construct in a way both - ;; opens and closes a block. As a result, it is difficult to handle - ;; cleanly. It is also ambiguous - if we are looking for the match - ;; of "else", should we look backward for "then/elseif" or forward - ;; for "end"? - ;; Maybe later we will find a way to handle it. - '(("do" "\\_" open) - ("function" "\\_" open) - ("repeat" "\\_" open) - ("then" "\\_<\\(e\\(lseif\\|nd\\)\\)\\_>" open) - ("{" "}" open) - ("[" "]" open) - ("(" ")" open) - ("elseif" "\\_" close) - ("end" "\\_<\\(do\\|function\\|then\\)\\_>" close) - ("until" "\\_" close) - ("}" "{" close) - ("]" "\\[" close) - (")" "(" close))) - + '(("do" "\\" "\\" middle-or-open) + ("function" "\\" nil open) + ("repeat" "\\" nil open) + ("then" "\\<\\(e\\(lse\\(if\\)?\\|nd\\)\\)\\>" "\\<\\(else\\)?if\\>" middle) + ("{" "}" nil open) + ("[" "]" nil open) + ("(" ")" nil open) + ("if" "\\" nil open) + ("for" "\\" nil open) + ("while" "\\" nil open) + ("else" "\\" "\\" middle) + ("elseif" "\\" "\\" middle) + ("end" nil "\\<\\(do\\|function\\|then\\|else\\)\\>" close) + ("until" nil "\\" close) + ("}" nil "{" close) + ("]" nil "\\[" close) + (")" nil "(" close)) + "This is a list of block token information blocks. +Each token information entry is of the form: + KEYWORD FORWARD-MATCH-REGEXP BACKWARDS-MATCH-REGEXP TOKEN-TYPE +KEYWORD is the token. +FORWARD-MATCH-REGEXP is a regexp that matches all possble tokens when going forward. +BACKWARDS-MATCH-REGEXP is a regexp that matches all possble tokens when going backwards. +TOKEN-TYPE determines where the token occurs on a statement. open indicates that the token appears at start, close indicates that it appears at end, middle indicates that it is a middle type token, and middle-or-open indicates that it can appear both as a middle or an open type.") (defconst lua-indentation-modifier-regexp ;; The absence of else is deliberate, since it does not modify the @@ -464,47 +927,105 @@ ;; else is, to be shifted to the left. (concat "\\(\\_<" - ;; n.b. "local function" is a bit of a hack, allowing only a single space - (regexp-opt '("do" "local function" "function" "repeat" "then") t) + (regexp-opt '("do" "function" "repeat" "then" "if" "else" "elseif" "for" "while") t) "\\_>\\|" (regexp-opt '("{" "(" "[")) "\\)\\|\\(\\_<" - (regexp-opt '("elseif" "end" "until") t) + (regexp-opt '("end" "until") t) "\\_>\\|" (regexp-opt '("]" ")" "}")) "\\)") - ) -(defun lua-find-matching-token-word (token search-start) - (let* ((token-info (assoc token lua-block-token-alist)) - (match (car (cdr token-info))) - (match-type (car (cdr (cdr token-info)))) - (search-direction (if (eq match-type 'open) 'forward 'backward))) +(defun lua-get-block-token-info (token) + "Returns the block token info entry for TOKEN from lua-block-token-alist" + (assoc token lua-block-token-alist)) + +(defun lua-get-token-match-re (token-info direction) + "Returns the relevant match regexp from token info" + (cond + ((eq direction 'forward) (cadr token-info)) + ((eq direction 'backward) (caddr token-info)) + (t nil))) + +(defun lua-get-token-type (token-info) + "Returns the relevant match regexp from token info" + (cadddr token-info)) + +(defun lua-backwards-to-block-begin-or-end () + "Move backwards to nearest block begin or end. Returns nil if not successful." + (interactive) + (lua-find-regexp 'backward lua-block-regexp)) + +(defun lua-find-matching-token-word (token search-start &optional direction) + (let* ((token-info (lua-get-block-token-info token)) + (match-type (lua-get-token-type token-info)) + ;; If we are on a middle token, go backwards. If it is a middle or open, + ;; go forwards + (search-direction (or direction + (if (or (eq match-type 'open) + (eq match-type 'middle-or-open)) + 'forward + 'backward) + 'backward)) + (match (lua-get-token-match-re token-info search-direction)) + maybe-found-pos) ;; if we are searching forward from the token at the current point ;; (i.e. for a closing token), need to step one character forward ;; first, or the regexp will match the opening token. - (if (eq match-type 'open) (forward-char 1)) + (if (eq search-direction 'forward) (forward-char 1)) (if search-start (goto-char search-start)) (catch 'found + ;; If we are attempting to find a matching token for a terminating token + ;; (i.e. a token that starts a statement when searching back, or a token + ;; that ends a statement when searching forward), then we don't need to look + ;; any further. + (if (or (and (eq search-direction 'forward) + (eq match-type 'close)) + (and (eq search-direction 'backward) + (eq match-type 'open))) + (throw 'found nil)) (while (lua-find-regexp search-direction lua-indentation-modifier-regexp) ;; have we found a valid matching token? (let ((found-token (match-string 0)) (found-pos (match-beginning 0))) - (if (string-match match found-token) - (throw 'found found-pos)) - ;; no - then there is a nested block. If we were looking for - ;; a block begin token, found-token must be a block end - ;; token; likewise, if we were looking for a block end token, - ;; found-token must be a block begin token, otherwise there - ;; is a grammatical error in the code. - (if (not (and - (eq (car (cdr (cdr (assoc found-token lua-block-token-alist)))) - match-type) - (lua-find-matching-token-word found-token nil))) - (throw 'found nil))))))) + (let ((found-type (lua-get-token-type + (lua-get-block-token-info found-token)))) + (if (not (and match (string-match match found-token))) + ;; no - then there is a nested block. If we were looking for + ;; a block begin token, found-token must be a block end + ;; token; likewise, if we were looking for a block end token, + ;; found-token must be a block begin token, otherwise there + ;; is a grammatical error in the code. + (if (not (and + (or (eq match-type 'middle) + (eq found-type 'middle) + (eq match-type 'middle-or-open) + (eq found-type 'middle-or-open) + (eq match-type found-type)) + (lua-find-matching-token-word found-token nil + search-direction))) + (when maybe-found-pos + (goto-char maybe-found-pos) + (throw 'found maybe-found-pos))) + ;; yes. + ;; if it is a not a middle kind, report the location + (when (not (or (eq found-type 'middle) + (eq found-type 'middle-or-open))) + (throw 'found found-pos)) + ;; if it is a middle-or-open type, record location, but keep searching. + ;; If we fail to complete the search, we'll report the location + (when (eq found-type 'middle-or-open) + (setq maybe-found-pos found-pos)) + ;; Cannot use tail recursion. too much nesting on long chains of + ;; if/elseif. Will reset variables instead. + (setq token found-token) + (setq token-info (lua-get-block-token-info token)) + (setq match (lua-get-token-match-re token-info search-direction)) + (setq match-type (lua-get-token-type token-info)))))) + maybe-found-pos))) -(defun lua-goto-matching-block-token (&optional search-start parse-start) +(defun lua-goto-matching-block-token (&optional search-start parse-start direction) "Find block begion/end token matching the one at the point. This function moves the point to the token that matches the one at the current point. Returns the point position of the first character of @@ -513,7 +1034,7 @@ (let ((case-fold-search nil)) (if (looking-at lua-indentation-modifier-regexp) (let ((position (lua-find-matching-token-word (match-string 0) - search-start))) + search-start direction))) (and position (goto-char position)))))) @@ -528,30 +1049,24 @@ (let ((position (lua-goto-matching-block-token))) (if (and (not position) (not noreport)) - (error "Not on a block control keyword or brace.") + (error "Not on a block control keyword or brace") position))) -(defun lua-goto-nonblank-previous-line () - "Puts the point at the first previous line that is not blank. -Returns the point, or nil if it reached the beginning of the buffer" - (catch 'found - (beginning-of-line) - (while t - (if (bobp) (throw 'found nil)) - (forward-char -1) - (beginning-of-line) - (if (not (looking-at "\\s *\\(--.*\\)?$")) (throw 'found (point)))))) +(defun lua-forward-line-skip-blanks (&optional back) + "Move 1 line forward (back if BACK is non-nil) skipping blank lines. + +Moves point 1 line forward (or backward) skipping lines that contain +no Lua code besides comments. The point is put to the beginning of +the line. -(defun lua-goto-nonblank-next-line () - "Puts the point at the first next line that is not blank. -Returns the point, or nil if it reached the end of the buffer" +Returns final value of point as integer or nil if operation failed." (catch 'found - (end-of-line) (while t - (forward-line) - (if (eobp) (throw 'found nil)) - (beginning-of-line) - (if (not (looking-at "\\s *\\(--.*\\)?$")) (throw 'found (point)))))) + (unless (eql (forward-line (if back -1 1)) 0) ;; 0 means success + (throw 'found nil)) + (unless (or (looking-at "\\s *\\(--.*\\)?$") + (lua-comment-or-string-p)) + (throw 'found (point)))))) (eval-when-compile (defconst lua-operator-class @@ -562,14 +1077,18 @@ (concat "\\(\\_<" (regexp-opt '("and" "or" "not" "in" "for" "while" - "local" "function") t) + "local" "function" "if" "until" "elseif" "return") t) "\\_>\\|" "\\(^\\|[^" lua-operator-class "]\\)" (regexp-opt '("+" "-" "*" "/" "^" ".." "==" "=" "<" ">" "<=" ">=" "~=") t) "\\)" "\\s *\\=") - )) + ) + "Regexp that matches the ending of a line that needs continuation +This regexp starts from eol and looks for a binary operator or an unclosed +block intro (i.e. 'for' without 'do' or 'if' without 'then') followed by +an optional whitespace till the end of the line.") (defconst lua-cont-bol-regexp (eval-when-compile @@ -582,17 +1101,20 @@ "\\($\\|[^" lua-operator-class "]\\)" "\\)") - )) + ) + "Regexp that matches a line that continues previous one + +This regexp means, starting from point there is an optional whitespace followed +by Lua binary operator. Lua is very liberal when it comes to continuation line, +so we're safe to assume that every line that starts with a binop continues +previous one even though it looked like an end-of-statement.") (defun lua-last-token-continues-p () "Returns true if the last token on this line is a continuation token." - (let (line-begin - line-end) + (let ((line-begin (line-beginning-position)) + (line-end (line-end-position))) (save-excursion - (beginning-of-line) - (setq line-begin (point)) (end-of-line) - (setq line-end (point)) ;; we need to check whether the line ends in a comment and ;; skip that one. (while (lua-find-regexp 'backward "-" line-begin 'lua-string-p) @@ -603,13 +1125,29 @@ (defun lua-first-token-continues-p () "Returns true if the first token on this line is a continuation token." - (let (line-end) + (let ((line-end (line-end-position))) (save-excursion - (end-of-line) - (setq line-end (point)) (beginning-of-line) + ;; if first character of the line is inside string, it's a continuation + ;; if strings aren't supposed to be indented, `lua-calculate-indentation' won't even let + ;; the control inside this function (re-search-forward lua-cont-bol-regexp line-end t)))) +(defconst lua-block-starter-regexp + (eval-when-compile + (concat + "\\(\\_<" + (regexp-opt '("do" "while" "repeat" "until" "if" "then" + "else" "elseif" "end" "for" "local") t) + "\\_>\\)"))) + +(defun lua-first-token-starts-block-p () + "Returns true if the first token on this line is a block starter token." + (let ((line-end (line-end-position))) + (save-excursion + (beginning-of-line) + (re-search-forward (concat "\\s *" lua-block-starter-regexp) line-end t)))) + (defun lua-is-continuing-statement-p (&optional parse-start) "Return non-nil if the line continues a statement. More specifically, return the point in the line that is continued. @@ -622,79 +1160,173 @@ (let ((prev-line nil)) (save-excursion (if parse-start (goto-char parse-start)) - (save-excursion (setq prev-line (lua-goto-nonblank-previous-line))) + (save-excursion (setq prev-line (lua-forward-line-skip-blanks 'back))) (and prev-line + (not (lua-first-token-starts-block-p)) (or (lua-first-token-continues-p) (and (goto-char prev-line) ;; check last token of previous nonblank line (lua-last-token-continues-p))))))) -(defun lua-make-indentation-info-pair () +(defun lua-make-indentation-info-pair (found-token found-pos) "This is a helper function to lua-calculate-indentation-info. Don't use standalone." - (cond ((string-equal found-token "function") - ;; this is the location where we need to start searching for the - ;; matching opening token, when we encounter the next closing token. - ;; It is primarily an optimization to save some searching time. - (cons 'absolute (+ (save-excursion (goto-char found-pos) - (current-column)) - lua-indent-level))) - ((or (string-equal found-token "{") - (string-equal found-token "(")) - (save-excursion - ;; expression follows -> indent at start of next expression - (if (and (not (search-forward-regexp "[[:space:]]--" (line-end-position) t)) - (search-forward-regexp "[^[:space:]]" (line-end-position) t)) - (cons 'absolute (1- (current-column))) - (cons 'relative lua-indent-level)))) - ;; closing tokens follow - ((string-equal found-token "end") - (save-excursion - (lua-goto-matching-block-token nil found-pos) - (if (looking-at "\\_") - (cons 'absolute - (+ (current-indentation) - (lua-calculate-indentation-block-modifier - nil (point)))) - (cons 'relative (- lua-indent-level))))) - ((or (string-equal found-token ")") - (string-equal found-token "}")) - (save-excursion - (lua-goto-matching-block-token nil found-pos) - (cons 'absolute - (+ (current-indentation) - (lua-calculate-indentation-block-modifier - nil (point)))))) - (t - (cons 'relative (if (nth 2 (match-data)) - ;; beginning of a block matched - lua-indent-level - ;; end of a block matched - (- lua-indent-level)))))) + (cond + ;; function is a bit tricky to indent right. They can appear in a lot ot + ;; different contexts. Until I find a shortcut, I'll leave it with a simple + ;; relative indentation. + ;; The special cases are for indenting according to the location of the + ;; function. i.e.: + ;; (cons 'absolute (+ (current-column) lua-indent-level)) + ;; TODO: Fix this. It causes really ugly indentations for in-line functions. + ((string-equal found-token "function") + (cons 'relative lua-indent-level)) + + ;; block openers + ((member found-token (list "{" "(" "[")) + (save-excursion + ;; expression follows -> indent at start of next expression + ;; Last token on the line -> simple relative indent + (if (and (not (search-forward-regexp "[[:space:]]--" (line-end-position) t)) + (search-forward-regexp "[^[:space:]]" (line-end-position) t)) + (cons 'absolute (1- (current-column))) + (cons 'relative lua-indent-level)))) + + ;; These are not really block starters. They should not add to indentation. + ;; The corresponding "then" and "do" handle the indentation. + ((member found-token (list "if" "for" "while")) + (cons 'relative 0)) + ;; closing tokens follow: These are usually taken care of by + ;; lua-calculate-indentation-override. + ;; elseif is a bit of a hack. It is not handled separately, but it needs to + ;; nullify a previous then if on the same line. + ((member found-token (list "until" "elseif")) + (save-excursion + (let ((line (line-number-at-pos))) + (if (and (lua-goto-matching-block-token nil found-pos 'backward) + (= line (line-number-at-pos))) + (cons 'remove-matching 0) + (cons 'relative 0))))) + + ;; else is a special case; if its matching block token is on the same line, + ;; instead of removing the matching token, it has to replace it, so that + ;; either the next line will be indented correctly, or the end on the same + ;; line will remove the effect of the else. + ((string-equal found-token "else") + (save-excursion + (let ((line (line-number-at-pos))) + (if (and (lua-goto-matching-block-token nil found-pos 'backward) + (= line (line-number-at-pos))) + (cons 'replace-matching (cons 'relative lua-indent-level)) + (cons 'relative lua-indent-level))))) + + ;; Block closers. If they are on the same line as their openers, they simply + ;; eat up the matching indentation modifier. Otherwise, they pull + ;; indentation back to the matching block opener. + ((member found-token (list ")" "}" "]" "end")) + (save-excursion + (let ((line (line-number-at-pos))) + (lua-goto-matching-block-token nil found-pos 'backward) + (if (/= line (line-number-at-pos)) + (lua-calculate-indentation-info (point)) + (cons 'remove-matching 0))))) + + ;; Everything else. This is from the original code: If opening a block + ;; (match-data 1 exists), then push indentation one level up, if it is + ;; closing a block, pull it one level down. + ('other-indentation-modifier + (cons 'relative (if (nth 2 (match-data)) + ;; beginning of a block matched + lua-indent-level + ;; end of a block matched + (- lua-indent-level)))))) + +(defun lua-add-indentation-info-pair (pair info) + "Add the given indentation info pair to the list of indentation information. +This function has special case handling for two tokens: remove-matching, +and replace-matching. These two tokens are cleanup tokens that remove or +alter the effect of a previously recorded indentation info. + +When a remove-matching token is encountered, the last recorded info, i.e. +the car of the list is removed. This is used to roll-back an indentation of a +block opening statement when it is closed. + +When a replace-matching token is seen, the last recorded info is removed, +and the cdr of the replace-matching info is added in its place. This is used +when a middle-of the block (the only case is 'else') is seen on the same line +the block is opened." + (cond + ( (listp (cdr-safe pair)) + (nconc pair info)) + ( (eq 'remove-matching (car pair)) + ; Remove head of list + (cdr info)) + ( (eq 'replace-matching (car pair)) + ; remove head of list, and add the cdr of pair instead + (cons (cdr pair) (cdr info))) + ( t + ; Just add the pair + (cons pair info)))) + +(defun lua-calculate-indentation-info-1 (indentation-info bound) + "Helper function for `lua-calculate-indentation-info'. + +Return list of indentation modifiers from point to BOUND." + (while (lua-find-regexp 'forward lua-indentation-modifier-regexp + bound) + (let ((found-token (match-string 0)) + (found-pos (match-beginning 0)) + (found-end (match-end 0)) + (data (match-data))) + (setq indentation-info + (lua-add-indentation-info-pair + (lua-make-indentation-info-pair found-token found-pos) + indentation-info)))) + indentation-info) -(defun lua-calculate-indentation-info (&optional parse-start parse-end) +(defun lua-calculate-indentation-info (&optional parse-end) "For each block token on the line, computes how it affects the indentation. The effect of each token can be either a shift relative to the current indentation level, or indentation to some absolute column. This information is collected in a list of indentation info pairs, which denote absolute and relative each, and the shift/column to indent to." - (let* ((line-end (save-excursion (end-of-line) (point))) - (search-stop (if parse-end (min parse-end line-end) line-end)) - (indentation-info nil)) - (if parse-start (goto-char parse-start)) - (save-excursion - (beginning-of-line) - (while (lua-find-regexp 'forward lua-indentation-modifier-regexp - search-stop) - (let ((found-token (match-string 0)) - (found-pos (match-beginning 0)) - (found-end (match-end 0)) - (data (match-data))) - (setq indentation-info - (cons (lua-make-indentation-info-pair) indentation-info))))) + (let ((combined-line-end (line-end-position)) + indentation-info) + + (while (lua-is-continuing-statement-p) + (lua-forward-line-skip-blanks 'back)) + + ;; calculate indentation modifiers for the line itself + (setq indentation-info (list (cons 'absolute (current-indentation)))) + + (back-to-indentation) + (setq indentation-info + (lua-calculate-indentation-info-1 + indentation-info (min parse-end (line-end-position)))) + + ;; and do the following for each continuation line before PARSE-END + (while (and (eql (forward-line 1) 0) + (<= (point) parse-end)) + + ;; handle continuation lines: + (if (lua-is-continuing-statement-p) + ;; if it's the first continuation line, add one level + (unless (eq (car (car indentation-info)) 'continued-line) + (push (cons 'continued-line lua-indent-level) indentation-info)) + + ;; if it's the first non-continued line, subtract one level + (when (eq (car (car indentation-info)) 'continued-line) + (pop indentation-info))) + + ;; add modifiers found in this continuation line + (setq indentation-info + (lua-calculate-indentation-info-1 + indentation-info (min parse-end (line-end-position))))) + indentation-info)) + (defun lua-accumulate-indentation-info (info) "Accumulates the indentation information previously calculated by lua-calculate-indentation-info. Returns either the relative indentation @@ -702,135 +1334,154 @@ (let ((info-list (reverse info)) (type 'relative) (accu 0)) - (mapcar (lambda (x) - (setq accu (if (eq 'absolute (car x)) - (progn (setq type 'absolute) - (cdr x)) - (+ accu (cdr x))))) - info-list) + (mapc (lambda (x) + (setq accu (if (eq 'absolute (car x)) + (progn (setq type 'absolute) + (cdr x)) + (+ accu (cdr x))))) + info-list) (cons type accu))) -(defun lua-calculate-indentation-block-modifier (&optional parse-start - parse-end) +(defun lua-calculate-indentation-block-modifier (&optional parse-end) "Return amount by which this line modifies the indentation. Beginnings of blocks add lua-indent-level once each, and endings of blocks subtract lua-indent-level once each. This function is used to determine how the indentation of the following line relates to this one." - (if parse-start (goto-char parse-start)) - (let ((case-fold-search nil) - (indentation-info (lua-accumulate-indentation-info - (lua-calculate-indentation-info nil parse-end)))) + (let (indentation-info) + (save-excursion + ;; First go back to the line that starts it all + ;; lua-calculate-indentation-info will scan through the whole thing + (let ((case-fold-search nil)) + (setq indentation-info + (lua-accumulate-indentation-info + (lua-calculate-indentation-info parse-end))))) + (if (eq (car indentation-info) 'absolute) - (- (cdr indentation-info) - (current-indentation) - ;; reduce indentation if this line also starts new continued statement - ;; or next line cont. this line - ;;This is for aesthetic reasons: the indentation should be - ;;dosomething(d + - ;; e + f + g) - ;;not - ;;dosomething(d + - ;; e + f + g)" - (save-excursion - (or (and (lua-last-token-continues-p) lua-indent-level) - (and (lua-goto-nonblank-next-line) (lua-first-token-continues-p) lua-indent-level) - 0))) - (+ (lua-calculate-indentation-left-shift) - (cdr indentation-info) - (if (lua-is-continuing-statement-p) (- lua-indent-level) 0))))) - -(defconst lua-left-shift-regexp-1 - (concat "\\(" - "\\(\\_<" (regexp-opt '("else" "elseif" "until") t) - "\\_>\\)\\($\\|\\s +\\)" - "\\)")) - -(defconst lua-left-shift-regexp-2 - (concat "\\(\\_<" - (regexp-opt '("end") t) - "\\_>\\)")) - -(defconst lua-left-shift-regexp - ;; "else", "elseif", "until" followed by whitespace, or "end"/closing - ;; brackets followed by - ;; whitespace, punctuation, or closing parentheses - (concat lua-left-shift-regexp-1 - "\\|\\(\\(" - lua-left-shift-regexp-2 - "\\|\\(" - (regexp-opt '("]" "}" ")")) - "\\)\\)\\($\\|\\(\\s \\|\\s.\\)*\\)" - "\\)")) - -(defconst lua-left-shift-pos-1 - 2) - -(defconst lua-left-shift-pos-2 - (+ 3 (regexp-opt-depth lua-left-shift-regexp-1))) - -(defconst lua-left-shift-pos-3 - (+ lua-left-shift-pos-2 - (regexp-opt-depth lua-left-shift-regexp-2))) + (- (cdr indentation-info) (current-indentation)) + (cdr indentation-info)))) + + +(eval-when-compile + (defconst lua--function-name-rx + '(seq symbol-start + (+ (any alnum "_")) + (* "." (+ (any alnum "_"))) + (? ":" (+ (any alnum "_"))) + symbol-end) + "Lua function name regexp in `rx'-SEXP format.")) + + +(defconst lua--left-shifter-regexp + (eval-when-compile + (rx + ;; This regexp should answer the following questions: + ;; 1. is there a left shifter regexp on that line? + ;; 2. where does block-open token of that left shifter reside? + ;; + ;; NOTE: couldn't use `group-n' keyword of `rx' macro, because it was + ;; introduced in Emacs 24.2 only, so for the sake of code clarity the named + ;; groups don't really match anything, they just report the position of the + ;; match. + (or (seq (regexp "\\_")) + (seq (eval lua--function-name-rx) (* blank) (regexp "\\(?1:\\)[{(]")) + (seq (or + ;; assignment statement prefix + (seq (* nonl) (not (any "<=>~")) "=" (* blank)) + ;; return statement prefix + (seq word-start "return" word-end (* blank))) + (regexp "\\(?1:\\)") + ;; right hand side + (or "{" + "function" + (seq + (eval lua--function-name-rx) (* blank) + (regexp "\\(?1:\\)") (any "({"))))))) + + "Regular expression that matches left-shifter expression. + +Left-shifter expression is defined as follows. If a block +follows a left-shifter expression, its contents & block-close +token should be indented relative to left-shifter expression +indentation rather then to block-open token. + +For example: + -- 'local a = ' is a left-shifter expression + -- 'function' is a block-open token + local a = function() + -- block contents is indented relative to left-shifter + foobarbaz() + -- block-end token is unindented to left-shifter indentation + end + +The following left-shifter expressions are currently handled: +1. local function definition with function block, begin-end +2. function call with arguments block, () or {} +3. assignment/return statement with + - table constructor block, {} + - function call arguments block, () or {} block + - function expression a.k.a. lambda, begin-end block.") + + +(defun lua-point-is-after-left-shifter-p () + "Check if point is right after a left-shifter expression. + +See `lua--left-shifter-regexp' for description & example of +left-shifter expression. " + (save-excursion + (let ((old-point (point))) + (back-to-indentation) + (and + (/= (point) old-point) + (looking-at lua--left-shifter-regexp) + (= old-point (match-end 1)))))) -(defun lua-calculate-indentation-left-shift (&optional parse-start) - "Return amount, by which this line should be shifted left. +(defun lua-calculate-indentation-override (&optional parse-start) + "Return overriding indentation amount for special cases. Look for an uninterrupted sequence of block-closing tokens that starts at the beginning of the line. For each of these tokens, shift indentation to the left by the amount specified in lua-indent-level." - (let (line-begin - (indentation-modifier 0) + (let ((indentation-modifier 0) (case-fold-search nil) (block-token nil)) (save-excursion (if parse-start (goto-char parse-start)) - (beginning-of-line) - (setq line-begin (point)) - ;; Look for the block-closing token sequence - (skip-chars-forward lua-indent-whitespace) - (catch 'stop - (while (and (looking-at lua-left-shift-regexp) - (not (lua-comment-or-string-p))) - (let ((last-token (or (match-string lua-left-shift-pos-1) - (match-string lua-left-shift-pos-2) - (match-string lua-left-shift-pos-3)))) - (if (not block-token) (setq block-token last-token)) - (if (not (string-equal block-token last-token)) (throw 'stop nil)) - (setq indentation-modifier (+ indentation-modifier - lua-indent-level)) - (forward-char (length (match-string 0)))))) - indentation-modifier))) + ;; Look for the last block closing token + (back-to-indentation) + (if (and (not (lua-comment-or-string-p)) + (looking-at lua-indentation-modifier-regexp) + (let ((token-info (lua-get-block-token-info (match-string 0)))) + (and token-info + (not (eq 'open (lua-get-token-type token-info)))))) + (when (lua-goto-matching-block-token nil nil 'backward) + ;; Exception cases: when the start of the line is an assignment, + ;; go to the start of the assignment instead of the matching item + (let ((block-start-column (current-column)) + (block-start-point (point))) + (if (lua-point-is-after-left-shifter-p) + (current-indentation) + block-start-column))))))) (defun lua-calculate-indentation (&optional parse-start) - "Return appropriate indentation for current line as Lua code. -In usual case returns an integer: the column to indent to." - (let ((pos (point)) - shift-amt) - (save-excursion - (if parse-start (setq pos (goto-char parse-start))) - (beginning-of-line) - (setq shift-amt (if (lua-is-continuing-statement-p) lua-indent-level 0)) - (if (bobp) ; If we're at the beginning of the buffer, no change. - (+ (current-indentation) shift-amt) - ;; This code here searches backwards for a "block beginning/end" - ;; It snarfs the indentation of that, plus whatever amount the - ;; line was shifted left by, because of block end tokens. It - ;; then adds the indentation modifier of that line to obtain the - ;; final level of indentation. - ;; Finally, if this line continues a statement from the - ;; previous line, add another level of indentation. - (if (lua-backwards-to-block-begin-or-end) - ;; now we're at the line with block beginning or end. - (max (+ (current-indentation) - (lua-calculate-indentation-block-modifier) - shift-amt) - 0) - ;; Failed to find a block begin/end. - ;; Just use the previous line's indent. - (goto-char pos) - (beginning-of-line) - (forward-line -1) - (+ (current-indentation) shift-amt)))))) + "Return appropriate indentation for current line as Lua code." + (save-excursion + (let ((continuing-p (lua-is-continuing-statement-p)) + (cur-line-begin-pos (line-beginning-position))) + (or + ;; when calculating indentation, do the following: + ;; 1. check, if the line starts with indentation-modifier (open/close brace) + ;; and if it should be indented/unindented in special way + (lua-calculate-indentation-override) + + (when (lua-forward-line-skip-blanks 'back) + ;; the order of function calls here is important. block modifier + ;; call may change the point to another line + (let* ((modifier + (lua-calculate-indentation-block-modifier cur-line-begin-pos))) + (+ (current-indentation) modifier))) + + ;; 4. if there's no previous line, indentation is 0 + 0)))) (defun lua-beginning-of-proc (&optional arg) "Move backward to the beginning of a lua proc (or similar). @@ -896,10 +1547,14 @@ (forward-line))) ret)) -(defun lua-start-process (name &optional program startfile &rest switches) - "Start a lua process named NAME, running PROGRAM." +(defun lua-start-process (&optional name program startfile &rest switches) + "Start a lua process named NAME, running PROGRAM. +PROGRAM defaults to NAME, which defaults to `lua-default-application'. +When called interactively, switch to the process buffer." + (interactive) (or switches (setq switches lua-default-command-switches)) + (setq name (or name lua-default-application)) (setq program (or program name)) (setq lua-process-buffer (apply 'make-comint name program startfile switches)) (setq lua-process (get-buffer-process lua-process-buffer)) @@ -907,7 +1562,10 @@ (with-current-buffer lua-process-buffer (while (not (lua-prompt-line)) (accept-process-output (get-buffer-process (current-buffer))) - (goto-char (point-max))))) + (goto-char (point-max)))) + ;; when called interactively, switch to process buffer + (if (lua--called-interactively-p 'any) + (switch-to-buffer lua-process-buffer))) (defun lua-kill-process () "Kill lua subprocess and its buffer." @@ -929,9 +1587,29 @@ "Send current line to lua subprocess, found in `lua-process'. If `lua-process' is nil or dead, start a new process first." (interactive) - (let ((start (save-excursion (beginning-of-line) (point))) - (end (save-excursion (end-of-line) (point)))) - (lua-send-region start end))) + (lua-send-region (line-beginning-position) (line-end-position))) + +(defun lua-send-defun (pos) + "Send the function definition around point to lua subprocess." + (interactive "d") + (save-excursion + (let ((start (if (save-match-data (looking-at "^function[ \t]")) + ;; point already at the start of "function". + ;; We need to handle this case explicitly since + ;; lua-beginning-of-proc will move to the + ;; beginning of the _previous_ function. + (point) + ;; point is not at the beginning of function, move + ;; there and bind start to that position + (lua-beginning-of-proc) + (point))) + (end (progn (lua-end-of-proc) (point)))) + + ;; make sure point is in a function defintion before sending to + ;; the subprocess + (if (and (>= pos start) (< pos end)) + (lua-send-region start end) + (error "Not on a function definition"))))) (defun lua-send-region (start end) "Send region to lua subprocess." @@ -973,8 +1651,7 @@ t, otherwise return nil. BUF must exist." (let ((lua-stdin-line-offset (or lua-stdin-line-offset 0)) line file bol err-p) - (save-excursion - (set-buffer buf) + (with-current-buffer buf (goto-char start) (beginning-of-line) (if (re-search-forward lua-traceback-line-re nil t) @@ -1001,7 +1678,7 @@ (if (not (eq major-mode 'lua-mode)) (lua-mode)) ;; FIXME: fix offset when executing region - (goto-line line) + (goto-char (point-min)) (forward-line (1- line)) (message "Jumping to error in file %s on line %d" file line)))) (defun lua-prompt-line () @@ -1025,22 +1702,7 @@ (if lua-always-show (display-buffer lua-process-buffer))) -(defun lua-send-proc () - "Send proc around point to lua subprocess." - (interactive) - (let (beg end) - (save-excursion - (lua-beginning-of-proc) - (setq beg (point)) - (lua-end-of-proc) - (setq end (point))) - (or (and lua-process - (comint-check-proc lua-process-buffer)) - (lua-start-process lua-default-application)) - (comint-simple-send lua-process - (buffer-substring beg end)) - (if lua-always-show - (display-buffer lua-process-buffer)))) +(defalias 'lua-send-proc 'lua-send-defun) ;; FIXME: This needs work... -Bret (defun lua-send-buffer () @@ -1070,29 +1732,24 @@ (interactive) (browse-url (concat lua-search-url-prefix (current-word t)))) -(defun lua-calculate-state (arg prevstate) - ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If - ;; arg is nil or zero, toggle the state. If arg is negative, turn - ;; the state off, and if arg is positive, turn the state on - (if (or (not arg) - (zerop (setq arg (prefix-numeric-value arg)))) - (not prevstate) - (> arg 0))) - (defun lua-toggle-electric-state (&optional arg) "Toggle the electric indentation feature. Optional numeric ARG, if supplied, turns on electric indentation when positive, turns it off when negative, and just toggles it when zero or left out." (interactive "P") - (setq lua-electric-flag (lua-calculate-state arg lua-electric-flag))) + (let ((num_arg (prefix-numeric-value arg))) + (setq lua-electric-flag (cond ((or (null arg) + (zerop num_arg)) (not lua-electric-flag)) + ((< num_arg 0) nil) + ((> num_arg 0) t)))) + (message "%S" lua-electric-flag)) (defun lua-forward-sexp (&optional count) "Forward to block end" (interactive "p") (save-match-data (let* ((count (or count 1)) - (stackheight 0) (block-start (mapcar 'car lua-sexp-alist)) (block-end (mapcar 'cdr lua-sexp-alist)) (block-regex (regexp-opt (append block-start block-end) 'words)) @@ -1145,7 +1802,7 @@ (define-key lua-mode-menu [search-documentation] '("Search Documentation" . lua-search-documentation)) -(provide 'lua-mode) +(provide 'lua-mode) ;;; lua-mode.el ends here diff -Nru lua-mode-20110121/test/indentation/README.md lua-mode-20130419/test/indentation/README.md --- lua-mode-20110121/test/indentation/README.md 1970-01-01 00:00:00.000000000 +0000 +++ lua-mode-20130419/test/indentation/README.md 2013-04-19 07:27:32.000000000 +0000 @@ -0,0 +1,16 @@ +This directory contains files used to test lua-mode indentation engine. Test +input is stored in `*.lua` files which are compared to their respective etalon +files `*.lua.etalon`. + +To run all tests, do +``` +$ ./test_indentation.sh *.lua +``` + +Feel free to add your own tricky cases, if you don't see them here, no specific +guidelines here, just try to bundle them logically into several bigger files. + +Additional lua-mode customization may be performed on a file basis with the use +of [emacs file variables]. + +[emacs file variables]: http://www.gnu.org/software/emacs/manual/html_node/emacs/File-Variables.html diff -Nru lua-mode-20110121/test/indentation/basic_blocks.lua lua-mode-20130419/test/indentation/basic_blocks.lua --- lua-mode-20110121/test/indentation/basic_blocks.lua 1970-01-01 00:00:00.000000000 +0000 +++ lua-mode-20130419/test/indentation/basic_blocks.lua 2013-04-19 07:27:32.000000000 +0000 @@ -0,0 +1,45 @@ +do +a = 2 +c = 5 +end + +if good then +do_good() +elseif bad then +do_bad() +else +do_nothing() +really_do_nothing() +end + +while true do +sun.rise_in_the_east() +end + +repeat +host.offer_hot_beverage(guest) +until guest.good + +for i,v in ipairs(base) do +take(base) +end + +function fact(i) +if i == 0 then +return 1 +else +if i == 1 then +return 1 +else +if i == 2 then +return 2 +else +fact = 2 +for n=3,i do +fact = fact * n +end +return fact +end +end +end +end diff -Nru lua-mode-20110121/test/indentation/basic_blocks.lua.etalon lua-mode-20130419/test/indentation/basic_blocks.lua.etalon --- lua-mode-20110121/test/indentation/basic_blocks.lua.etalon 1970-01-01 00:00:00.000000000 +0000 +++ lua-mode-20130419/test/indentation/basic_blocks.lua.etalon 2013-04-19 07:27:32.000000000 +0000 @@ -0,0 +1,45 @@ +do + a = 2 + c = 5 +end + +if good then + do_good() +elseif bad then + do_bad() +else + do_nothing() + really_do_nothing() +end + +while true do + sun.rise_in_the_east() +end + +repeat + host.offer_hot_beverage(guest) +until guest.good + +for i,v in ipairs(base) do + take(base) +end + +function fact(i) + if i == 0 then + return 1 + else + if i == 1 then + return 1 + else + if i == 2 then + return 2 + else + fact = 2 + for n=3,i do + fact = fact * n + end + return fact + end + end + end +end diff -Nru lua-mode-20110121/test/indentation/test_indentation.sh lua-mode-20130419/test/indentation/test_indentation.sh --- lua-mode-20110121/test/indentation/test_indentation.sh 1970-01-01 00:00:00.000000000 +0000 +++ lua-mode-20130419/test/indentation/test_indentation.sh 2013-04-19 07:27:32.000000000 +0000 @@ -0,0 +1,84 @@ +#!/bin/bash + +# This script can be used to test lua-mode indentation engine. For a slightly +# longer description, run it without parameters and consult the output. + +# Need to preserve input arguments, because subshells overwrite them +declare -a PARAMS +PARAMS=( "$@" ) + +set ${EMACS=emacs} +set ${LUA_MODE=$(dirname $0)/../../lua-mode.el} + +if [ ${#PARAMS[@]} -eq 0 ]; then + cat < [ ... ] + +Test lua-mode indentation: each FILE is checked to be indented as FILE.etalon. +The following environment variables are considered: + + - EMACS (default value: emacs) + specify emacs version to be run + + - LUA_MODE (default value: $(readlink -m $(dirname $0)/../lua-mode.el)) + specify location where tested lua-mode.el resides +EOF + exit 1 +fi + +# All output will be redirected into ERROR_LOG temporary file to make sure +# that if everything goes ok, the script will remain silent. In case of an +# error, ERROR_LOG contents may be retrieved and printed onto the screen. +ERROR_LOG=`mktemp` || { echo "can't create tempfile"; exit 1; } +exec 6<>"$ERROR_LOG" +unlink "$ERROR_LOG" +ERROR_LOG=/proc/$$/fd/6 + +OUTPUT=`mktemp` || { echo "can't create temporary output file"; exit 1; } +exec 7<>"$OUTPUT" +unlink "$OUTPUT" +OUTPUT=/proc/$$/fd/7 + +test_file_indentation() { + INPUT="$1" + ETALON="$INPUT.etalon" + + echo -n "Testing $INPUT... " + + test -f "$INPUT" || { echo "input file '$INPUT' not exists or not a file"; return 1; } + test -f "$ETALON" || { echo "etalon file '$ETALON' not exists or not a file"; return 1; } + +# run emacs with lua-mode and indent input file +# disable backup file creation to prevent emacs from trying to write to procfs + $EMACS --quick --batch \ + --load $LUA_MODE \ + --eval "(setq make-backup-files nil)" \ + --eval "\ +(with-temp-buffer + ;; lua-mode indents by 3s, that's not even a multiple of tab width (4/8) + (set-default 'indent-tabs-mode nil) \ + (insert-file-contents \"$INPUT\") + (lua-mode) + + ;; permit unsafe (e.g. lua-*) local variables and read them + (setq enable-local-variables :all) + (hack-local-variables) + + (indent-region (point-min) (point-max)) + (write-file \"$OUTPUT\"))" \ + > $ERROR_LOG 2>&1 \ + || { echo "indentation failed (Emacs log):"; cat <$ERROR_LOG ; return 1; } + + diff -u $ETALON $OUTPUT >$ERROR_LOG 2>&1 \ + || { echo "indentation mismatch:"; cat <$ERROR_LOG; return 1; } + + echo OK +} + +FAILED=no + +for INPUT in "${PARAMS[@]}"; do + test_file_indentation "$INPUT" || FAILED=yes +done + +test "$FAILED" = "no" || exit 1