changeset 2320:966a5609669e vim73

Added Lua interfae. (Luis Carvalho)
author Bram Moolenaar <bram@vim.org>
date Wed, 14 Jul 2010 23:23:17 +0200
parents c79ccf947487
children 1902913f2049
files Filelist Makefile runtime/doc/Makefile runtime/doc/eval.txt runtime/doc/help.txt runtime/doc/if_lua.txt runtime/doc/index.txt runtime/doc/tags runtime/doc/todo.txt runtime/doc/various.txt runtime/doc/version7.txt runtime/doc/vi_diff.txt src/Make_bc5.mak src/Make_cyg.mak src/Make_ming.mak src/Make_mvc.mak src/Makefile src/auto/configure src/buffer.c src/config.aap.in src/config.h.in src/config.mk.in src/configure.in src/eval.c src/ex_cmds.h src/ex_docmd.c src/feature.h src/globals.h src/if_lua.c src/main.aap src/main.c src/proto.h src/proto/if_lua.pro src/version.c src/window.c
diffstat 35 files changed, 1835 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/Filelist
+++ b/Filelist
@@ -210,6 +210,7 @@ SRC_UNIX =	\
 SRC_DOS_UNIX =	\
 		src/if_cscope.c \
 		src/if_cscope.h \
+		src/if_lua.c \
 		src/if_mzsch.c \
 		src/if_mzsch.h \
 		src/if_perl.xs \
@@ -219,6 +220,7 @@ SRC_DOS_UNIX =	\
 		src/if_sniff.h \
 		src/if_tcl.c \
 		src/proto/if_cscope.pro \
+		src/proto/if_lua.pro \
 		src/proto/if_mzsch.pro \
 		src/proto/if_perl.pro \
 		src/proto/if_perlsfio.pro \
--- a/Makefile
+++ b/Makefile
@@ -80,8 +80,8 @@ DOSBIN_S =  dosbin_s
 #   runtime/doc/*.txt and nsis/gvim.nsi. Other things in README_os2.txt.  For a
 #   minor/major version: src/GvimExt/GvimExt.reg, src/vim.def, src/vim16.def.
 # - Correct included_patches[] in src/version.c.
-# - Compile Vim with GTK, Perl, Python, TCL, Ruby, MZscheme (if you can make it
-#   work), Cscope and "huge" features.  Exclude workshop and SNiFF.
+# - Compile Vim with GTK, Perl, Python, TCL, Ruby, MZscheme, Lua (if you can
+#   make it work), Cscope and "huge" features.  Exclude workshop and SNiFF.
 # - With these features: "make proto" (requires cproto and Motif installed;
 #   ignore warnings for missing include files, fix problems for syntax errors).
 # - With these features: "make depend" (works best with gcc).
--- a/runtime/doc/Makefile
+++ b/runtime/doc/Makefile
@@ -40,6 +40,7 @@ DOCS = \
 	helphelp.txt \
 	howto.txt \
 	if_cscop.txt \
+	if_lua.txt \
 	if_mzsch.txt \
 	if_ole.txt \
 	if_perl.txt \
@@ -171,6 +172,7 @@ HTMLS = \
 	helphelp.html \
 	howto.html \
 	if_cscop.html \
+	if_lua.html \
 	if_mzsch.html \
 	if_ole.html \
 	if_perl.html \
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -6095,6 +6095,7 @@ lispindent		Compiled with support for li
 listcmds		Compiled with commands for the buffer list |:files|
 			and the argument list |arglist|.
 localmap		Compiled with local mappings and abbr. |:map-local|
+lua			Compiled with Lua interface |Lua|.
 mac			Macintosh version of Vim.
 macunix			Macintosh version of Vim, using Unix files (OS-X).
 menu			Compiled with support for |:menu|.
--- a/runtime/doc/help.txt
+++ b/runtime/doc/help.txt
@@ -158,6 +158,7 @@ GUI ~
 
 Interfaces ~
 |if_cscop.txt|	using Cscope with Vim
+|if_lua.txt|	Lua interface
 |if_mzsch.txt|	MzScheme interface
 |if_perl.txt|	Perl interface
 |if_pyth.txt|	Python interface
new file mode 100644
--- /dev/null
+++ b/runtime/doc/if_lua.txt
@@ -0,0 +1,239 @@
+*if_lua.txt*    For Vim version 7.3a.  Last change: 2008 Aug 31
+
+
+		  VIM REFERENCE MANUAL    by Luis Carvalho
+
+
+The Lua Interface to Vim				*lua* *Lua*
+
+1. Commands			|lua-commands|
+2. The vim module		|lua-vim|
+3. Buffer userdata		|lua-buffer|
+4. Window userdata		|lua-window|
+
+{Vi does not have any of these commands}
+
+The Lua interface is available only when Vim was compiled with the
+|+lua| feature.
+
+==============================================================================
+1. Commands						*lua-commands*
+
+							*:lua*
+:[range]lua {chunk}
+			Execute Lua chunk {chunk}.    {not in Vi}
+
+Examples:
+>
+	:lua print("Hello, Vim!")
+	:lua local curbuf = vim.buffer() curbuf[7] = "line #7"
+<
+
+:[range]lua << {endmarker}
+{script}
+{endmarker}
+			Execute Lua script {script}.  {not in Vi}
+			Note: This command doesn't work when the Lua
+			feature wasn't compiled in.  To avoid errors, see
+			|script-here|.
+
+{endmarker} must NOT be preceded by any white space.  If {endmarker} is
+omitted from after the "<<", a dot '.' must be used after {script}, like
+for the |:append| and |:insert| commands.
+This form of the |:lua| command is mainly useful for including Lua code
+in Vim scripts.
+
+Example:
+>
+	function! CurrentLineInfo()
+	lua << EOF
+	local linenr = vim.window().line
+	local curline = vim.buffer()[linenr]
+	print(string.format("Current line [%d] has %d chars",
+		linenr, #curline))
+	EOF
+	endfunction
+<
+
+							*:luado*
+:[range]luado {body}	Execute Lua function$function (line)${body}$end$ for
+			each line in the [range], with the function argument
+			being set to the text of each line in turn, without a
+			trailing <EOL>. If the value returned by the function
+			is a string it becomes the text of the line in the
+			current turn. The default for [range] is the whole
+			file: "1,$".                  {not in Vi}
+
+Examples:
+>
+	:luado return string.format("%s\t%d", line:reverse(), #line)
+
+	:lua require"lpeg"
+	:lua -- balanced parenthesis grammar:
+	:lua bp = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }
+	:luado if bp:match(line) then return "-->\t" .. line end
+<
+
+							*:luafile*
+:[range]luafile {file}
+			Execute Lua script in {file}. {not in Vi}
+			The whole argument is used as a single file name.
+
+Examples:
+>
+	:luafile script.lua
+	:luafile %
+<
+
+All these commands execute a Lua chunk from either the command line (:lua and
+:luado) or a file (:luafile) with the given line [range]. Similarly to the Lua
+interpreter, each chunk has its own scope and so only global variables are
+shared between command calls. Lua default libraries$table$,$string$,$math$,
+and$package$are available,$io$and$debug$are not, and$os$is restricted to
+functions$date$,$clock$,$time$,$difftime$, and$getenv$. In addition,
+Lua$print$function has its output redirected to the Vim message area, with
+arguments separated by a white space instead of a tab.
+
+Lua uses the "vim" module (see|lua-vim|) to issue commands to Vim
+and manage buffers (|lua-buffer|) and windows (|lua-window|). However,
+procedures that alter buffer content, open new buffers, and change cursor
+position are restricted when the command is executed in the|sandbox|.
+
+
+==============================================================================
+2. The vim module					*lua-vim*
+
+Lua interfaces Vim through the "vim" module. The first and last line of the
+input range are stored in$vim.firstline$and$vim.lastline$respectively. The
+module also includes routines for buffer, window, and current line queries,
+Vim evaluation and command execution, and others.
+
+	$vim.isbuffer(value)$	Returns#true#if$value$is a buffer userdata and
+				$false$otherwise (see|lua-buffer|).
+
+	$vim.buffer($[arg]$)$	If$arg$is a number, returns buffer with number
+				$arg$in the buffer list or, if$arg$is
+				a string, returns buffer whose full or short
+				name is$arg$. In both cases, returns#nil#if
+				the buffer is not found. Otherwise, if
+				$toboolean(arg)$is#true#returns the first
+				buffer in the buffer list or else the current
+				buffer.
+
+	$vim.iswindow(value)$	Returns#true#if$value$is a window userdata and
+				$false$otherwise (see|lua-window|).
+
+	$vim.window($[arg]$)$	If$arg$is a number, returns window with number
+				$arg$or#nil#if not found. Otherwise, if
+				$toboolean(arg)$is#true#returns the first
+				window or else the current window.
+
+	$vim.command(${cmd}$)$	Executes the vim (ex-mode) command {cmd}.
+				Examples: >
+					:lua vim.command"set tw=60"
+					:lua vim.command"normal ddp"
+<
+	$vim.eval(${expr}$)$	Evaluates expression {expr} (see|expression|),
+				converts the result to Lua, and returns it.
+				Vim strings and numbers are directly converted
+				to Lua strings and numbers respectively. Vim
+				lists and dictionaries are converted to Lua
+				tables (lists become integer-keyed tables).
+				Examples: >
+					:lua tw = vim.eval"&tw"
+					:lua print(vim.eval"{'a': 'one'}".a)
+<
+	$vim.line()$		Returns the current line (without the trailing
+				<EOL>), a Lua string.
+
+	$vim.beep()$		Beeps.
+
+	$vim.open(${fname}$)$	Opens a new buffer for file {fname} and
+				returns it. Note that the buffer is not set as
+				current.
+
+
+==============================================================================
+3. Buffer userdata					*lua-buffer*
+
+Buffer userdata represent vim buffers. A buffer userdata$b$has the following
+properties and methods:
+
+Properties
+----------
+	#o#$b()$sets$b$as the current buffer.
+	#o#$#b$is the number of lines in buffer$b$.
+	#o#$b[k]$represents line number$k$:$b[k] = newline$replaces line$k$
+	    with string$newline$and$b[k] =$#nil#deletes line$k$.
+	#o#$b.name$contains the short name of buffer$b$(read-only).
+	#o#$b.fname$contains the full name of buffer$b$(read-only).
+	#o#$b.number$contains the position of buffer$b$in the buffer list
+	    (read-only).
+
+Methods
+-------
+	#o#$b:insert(newline$[, pos]$)$inserts string$newline$at position$pos$
+	    in the buffer. The default value for$pos$is$#b + 1$. If$pos == 0$
+	    then$newline$becomes the first line in the buffer.
+	#o#$b:next()$returns the buffer next to$b$in the buffer list.
+	#o#$b:previous()$returns the buffer previous to$b$in the buffer list.
+	#o#$b:isvalid()$returns#true#if buffer$b$corresponds to a "real" (not
+	    freed from memory) Vim buffer.
+
+Examples:
+>
+	:lua b = vim.buffer() -- current buffer
+	:lua print(b.name, b.number)
+	:lua b[1] = "first line"
+	:lua b:insert("FIRST!", 0)
+	:lua b[1] = nil -- delete top line
+	:lua for i=1,3 do b:insert(math.random()) end
+	:3,4lua for i=vim.lastline,vim.firstline,-1 do b[i] = nil end
+	:lua vim.open"myfile"() -- open buffer and set it as current
+
+	function! ListBuffers()
+	lua << EOF
+	local b = vim.buffer(true) -- first buffer in list
+	while b ~= nil do
+		print(b.number, b.name, #b)
+		b = b:next()
+	end
+	vim.beep()
+	EOF
+	endfunction
+<
+
+==============================================================================
+4. Window userdata					*lua-window*
+
+Window objects represent vim windows. A window userdata$w$has the following
+properties and methods:
+
+Properties
+----------
+	#o#$w()$sets$w$as the current window.
+	#o#$w.buffer$contains the buffer of window$w$(read-only).
+	#o#$w.line$represents the cursor line position in window$w$.
+	#o#$w.col$represents the cursor column position in window$w$.
+	#o#$w.width$represents the width of window$w$.
+	#o#$w.height$represents the height of window$w$.
+
+Methods
+-------
+	#o#$w:next()$returns the window next to$w$.
+	#o#$w:previous()$returns the window previous to$w$.
+	#o#$w:isvalid()$returns#true#if window$w$corresponds to a "real" (not
+	    freed from memory) Vim window.
+
+Examples:
+>
+	:lua w = vim.window() -- current window
+	:lua print(w.buffer.name, w.line, w.col)
+	:lua w.width = w.width + math.random(10)
+	:lua w.height = 2 * math.random() * w.height
+	:lua n,w = 0,vim.window(true) while w~=nil do n,w = n + 1,w:next() end
+	:lua print("There are " .. n .. " windows")
+<
+
+==============================================================================
+ vim:tw=78:ts=8:ft=help:norl:
--- a/runtime/doc/index.txt
+++ b/runtime/doc/index.txt
@@ -1299,6 +1299,9 @@ The commands are sorted on the non-optio
 |:ltag|		:lt[ag]		jump to tag and add matching tags to the
 				location list
 |:lunmap|	:lu[nmap]	like ":unmap!" but includes Lang-Arg mode
+|:lua|		:lua		execute |Lua| command
+|:luado|	:luad[o]	execute Lua command for each line
+|:luafile|	:luaf[ile]	execute |Lua| script file
 |:lvimgrep|	:lv[imgrep]	search for pattern in files
 |:lvimgrepadd|	:lvimgrepa[dd]	like :vimgrep, but append to current list
 |:lwindow|	:lw[indow]	open or close location window
--- a/runtime/doc/tags
+++ b/runtime/doc/tags
@@ -1165,6 +1165,8 @@
 +lispindent	various.txt	/*+lispindent*
 +listcmds	various.txt	/*+listcmds*
 +localmap	various.txt	/*+localmap*
++lua	various.txt	/*+lua*
++lua/dyn	various.txt	/*+lua\/dyn*
 +menu	various.txt	/*+menu*
 +mksession	various.txt	/*+mksession*
 +modify_fname	various.txt	/*+modify_fname*
@@ -2365,6 +2367,9 @@ 90.5	usr_90.txt	/*90.5*
 :lt	tagsrch.txt	/*:lt*
 :ltag	tagsrch.txt	/*:ltag*
 :lu	map.txt	/*:lu*
+:lua	if_lua.txt	/*:lua*
+:luado	if_lua.txt	/*:luado*
+:luafile	if_lua.txt	/*:luafile*
 :lunmap	map.txt	/*:lunmap*
 :lv	quickfix.txt	/*:lv*
 :lvimgrep	quickfix.txt	/*:lvimgrep*
@@ -4280,6 +4285,7 @@ L	motion.txt	/*L*
 Linux-backspace	options.txt	/*Linux-backspace*
 List	eval.txt	/*List*
 Lists	eval.txt	/*Lists*
+Lua	if_lua.txt	/*Lua*
 M	motion.txt	/*M*
 MDI	starting.txt	/*MDI*
 MS-DOS	os_msdos.txt	/*MS-DOS*
@@ -6055,6 +6061,7 @@ hit-enter-prompt	message.txt	/*hit-enter
 hit-return	message.txt	/*hit-return*
 hitest.vim	syntax.txt	/*hitest.vim*
 hjkl	usr_02.txt	/*hjkl*
+hl-ColorColumn	syntax.txt	/*hl-ColorColumn*
 hl-Conceal	syntax.txt	/*hl-Conceal*
 hl-Cursor	syntax.txt	/*hl-Cursor*
 hl-CursorColumn	syntax.txt	/*hl-CursorColumn*
@@ -6248,6 +6255,7 @@ ident-search	tips.txt	/*ident-search*
 idl-syntax	syntax.txt	/*idl-syntax*
 idl.vim	syntax.txt	/*idl.vim*
 if_cscop.txt	if_cscop.txt	/*if_cscop.txt*
+if_lua.txt	if_lua.txt	/*if_lua.txt*
 if_mzsch.txt	if_mzsch.txt	/*if_mzsch.txt*
 if_ole.txt	if_ole.txt	/*if_ole.txt*
 if_perl.txt	if_perl.txt	/*if_perl.txt*
@@ -6424,6 +6432,11 @@ log10()	eval.txt	/*log10()*
 long-lines	version5.txt	/*long-lines*
 lowercase	change.txt	/*lowercase*
 lpc.vim	syntax.txt	/*lpc.vim*
+lua	if_lua.txt	/*lua*
+lua-buffer	if_lua.txt	/*lua-buffer*
+lua-commands	if_lua.txt	/*lua-commands*
+lua-vim	if_lua.txt	/*lua-vim*
+lua-window	if_lua.txt	/*lua-window*
 lua.vim	syntax.txt	/*lua.vim*
 m	motion.txt	/*m*
 m'	motion.txt	/*m'*
--- a/runtime/doc/todo.txt
+++ b/runtime/doc/todo.txt
@@ -1107,9 +1107,6 @@ 6   In the quickfix window statusline ad
 - Patch for adding "J" flag to 'cinoptions': placement of jump label.  (Manuel
   Konig, 2010 Feb 19)  Update by Lech Lorens, Feb 22.
   Need another name, "J" is now used for Javascript.
--   Add Lua interface? (Wolfgang Oertl) patch by Luis Carvalho, 2008 Sep 5
-	Patch for Make_ming.mak from Paul Moore (2008 Sep 1)
-      http://code.google.com/p/vim-iflua/  Download  vim72-lua-0.7.patch.gz
 Needs some work:
 - Have a look at patch to enable screen access from Python. (Marko Mahnic,
   2010 Apr 12)
--- a/runtime/doc/various.txt
+++ b/runtime/doc/various.txt
@@ -338,6 +338,8 @@ N  *+lispindent*	|'lisp'|
 N  *+listcmds*		Vim commands for the list of buffers |buffer-hidden|
 			and argument list |:argdelete|
 N  *+localmap*		Support for mappings local to a buffer |:map-local|
+m  *+lua*		|Lua| interface
+m  *+lua/dyn*		|Lua| interface |/dyn|
 N  *+menu*		|:menu|
 N  *+mksession*		|:mksession|
 N  *+modify_fname*	|filename-modifiers|
--- a/runtime/doc/version7.txt
+++ b/runtime/doc/version7.txt
@@ -7172,6 +7172,9 @@ executed.
 Removed support for GTK 1.  It was no longer maintained and required a lot of
 #ifdefs in the source code.  GTK 2 should be available for every system.
 
+It is no longer allowed to set the 'encoding' option from a modeline.  It
+would corrupt the text.
+
 
 Added							*added-7.3*
 -----
@@ -7188,6 +7191,9 @@ file.
 
 Added the |+conceal| feature.  (Vince Negri)
 
+Added the 'colorcolumn' option: highlight one or more columns in a window.
+E.g. to highlight the column after 'textwidth'. (partly by Gregor Uhlenheuer)
+
 Added support for NetBeans in a terminal.  Added |:nbstart| and |:nbclose|.
 (Xavier de Gaye)
 
@@ -7196,6 +7202,9 @@ log(), sinh(), tan(), tanh().  (Bill McC
 
 gettabvar() and settabvar() functions. (Yegappan Lakshmanan)
 
+Added the |Lua| interface.
+
+
 New syntax files:
 TODO
 
--- a/runtime/doc/vi_diff.txt
+++ b/runtime/doc/vi_diff.txt
@@ -411,7 +411,7 @@ Scripts and Expressions.				|expression|
 	etc., etc.  See |eval|.
 	Debugging and profiling are supported. |debug-scripts| |profile|
 	If this is not enough, an interface is provided to |Python|, |Ruby|,
-	|Tcl|, |Perl| and |MzScheme|.
+	|Tcl|, |Lua|, |Perl| and |MzScheme|.
 
 Viminfo.						|viminfo-file|
 	The command-line history, marks and registers can be stored in a file
--- a/src/Make_bc5.mak
+++ b/src/Make_bc5.mak
@@ -35,6 +35,9 @@
 # LINK		name of the linker ($(BOR)\bin\ilink if OSTYPE is DOS16,
 #		$(BOR)\bin\ilink32 otherwise)
 # GUI		no or yes: set to yes if you want the GUI version (yes)
+# LUA     define to path to Lua dir to get Lua support (not defined)
+#   LUA_VER	  define to version of Lua being used (51)
+#   DYNAMIC_LUA  no or yes: set to yes to load the Lua DLL dynamically (no)
 # PERL		define to path to Perl dir to get Perl support (not defined)
 #   PERL_VER	  define to version of Perl being used (56)
 #   DYNAMIC_PERL  no or yes: set to yes to load the Perl DLL dynamically (no)
@@ -129,6 +132,9 @@ CSCOPE = yes
 NETBEANS = yes
 !endif
 
+### LUA: uncomment this line if you want lua support in vim
+# LUA=c:\lua
+
 ### PERL: uncomment this line if you want perl support in vim
 # PERL=c:\perl
 
@@ -199,6 +205,7 @@ ALIGN = 4
 #   Incompatible when calling external functions (like MSVC-compiled DLLs), so
 #   don't use FASTCALL when linking with external libs.
 !if ("$(FASTCALL)"=="") && \
+	("$(LUA)"=="") && \
 	("$(PYTHON)"=="") && \
 	("$(PERL)"=="") && \
 	("$(TCL)"=="") && \
@@ -292,6 +299,18 @@ INCLUDE = $(BOR)\include;.;proto
 DEFINES = -DFEAT_$(FEATURES) -DWIN32 -DHAVE_PATHDEF \
 	  -DWINVER=$(WINVER) -D_WIN32_WINNT=$(WINVER)
 
+!ifdef LUA
+INTERP_DEFINES = $(INTERP_DEFINES) -DFEAT_LUA
+INCLUDE = $(LUA)\include;$(INCLUDE)
+!  ifndef LUA_VER
+LUA_VER = 51
+!  endif
+!  if ("$(DYNAMIC_LUA)" == "yes")
+INTERP_DEFINES = $(INTERP_DEFINES) -DDYNAMIC_LUA -DDYNAMIC_LUA_DLL=\"lua$(LUA_VER).dll\"
+LUA_LIB_FLAG = /nodefaultlib:
+!  endif
+!endif
+
 !ifdef PERL
 INTERP_DEFINES = $(INTERP_DEFINES) -DFEAT_PERL
 INCLUDE = $(PERL)\lib\core;$(INCLUDE)
@@ -584,6 +603,11 @@ vimobj = $(vimobj) \
 	$(OBJDIR)\if_ole.obj
 !endif
 
+!ifdef LUA
+vimobj = $(vimobj) \
+    $(OBJDIR)\if_lua.obj
+!endif
+
 !ifdef PERL
 vimobj = $(vimobj) \
     $(OBJDIR)\if_perl.obj
@@ -692,6 +716,12 @@ MSG = $(MSG) NETBEANS
 !ifdef XPM
 MSG = $(MSG) XPM
 !endif
+!ifdef LUA
+MSG = $(MSG) LUA
+! if "$(DYNAMIC_LUA)" == "yes"
+MSG = $(MSG)(dynamic)
+! endif
+!endif
 !ifdef PERL
 MSG = $(MSG) PERL
 ! if "$(DYNAMIC_PERL)" == "yes"
@@ -788,6 +818,9 @@ clean:
 	-@del *.ilf
 	-@del *.ils
 	-@del *.tds
+!ifdef LUA
+	-@del lua.lib
+!endif
 !ifdef PERL
 	-@del perl.lib
 !endif
@@ -825,6 +858,9 @@ clean:
 !endif
 !if ($(OSTYPE)==WIN32)
 	import32.lib+
+!ifdef LUA
+	$(LUA_LIB_FLAG)lua.lib+
+!endif
 !ifdef PERL
 	$(PERL_LIB_FLAG)perl.lib+
 !endif
@@ -874,6 +910,9 @@ clean:
 	ole2w32.lib +
 !endif
 	import32.lib+
+!ifdef LUA
+	$(LUA_LIB_FLAG)lua.lib+
+!endif
 !ifdef PERL
 	$(PERL_LIB_FLAG)perl.lib+
 !endif
@@ -912,6 +951,9 @@ test:
 
 $(OBJDIR)\if_ole.obj: if_ole.cpp
 
+$(OBJDIR)\if_lua.obj: if_lua.c lua.lib
+	$(CC) $(CCARG) $(CC1) $(CC2)$@ -pc if_lua.c
+
 $(OBJDIR)\if_perl.obj: if_perl.c perl.lib
 	$(CC) $(CCARG) $(CC1) $(CC2)$@ -pc if_perl.c
 
@@ -966,6 +1008,9 @@ char_u *compiled_user = (char_u *)"$(USE
 char_u *compiled_sys = (char_u *)"$(USERDOMAIN)";
 | auto\pathdef.c
 
+lua.lib: $(LUA)\lib\lua$(LUA_VER).lib
+	coff2omf $(LUA)\lib\lua$(LUA_VER).lib $@
+
 perl.lib: $(PERL)\lib\CORE\perl$(PERL_VER).lib
 	coff2omf $(PERL)\lib\CORE\perl$(PERL_VER).lib $@
 
--- a/src/Make_cyg.mak
+++ b/src/Make_cyg.mak
@@ -24,6 +24,9 @@
 #   MZSCHEME_VER      define to version of MzScheme being used (209_000)
 #   DYNAMIC_MZSCHEME  no or yes: use yes to load the MzScheme DLLs dynamically (yes)
 #   MZSCHEME_DLLS     path to MzScheme DLLs (libmzgc and libmzsch), for "static" build.
+# LUA	define to path to Lua dir to get Lua support (not defined)
+#   LUA_VER	    define to version of Lua being used (51)
+#   DYNAMIC_LUA  no or yes: use yes to load the Lua DLL dynamically (yes)
 # GETTEXT	no or yes: set to yes for dynamic gettext support (yes)
 # ICONV		no or yes: set to yes for dynamic iconv support (yes)
 # MBYTE		no or yes: set to yes to include multibyte support (yes)
@@ -273,6 +276,30 @@ endif
 endif
 
 ##############################
+# DYNAMIC_LUA=yes works.
+# DYNAMIC_LUA=no does not (unresolved externals on link).
+##############################
+ifdef LUA
+DEFINES += -DFEAT_LUA
+INCLUDES += -I$(LUA)/include
+EXTRA_OBJS += $(OUTDIR)/if_lua.o
+
+ifndef DYNAMIC_LUA
+DYNAMIC_LUA = yes
+endif
+
+ifndef LUA_VER
+LUA_VER = 51
+endif
+
+ifeq (yes, $(DYNAMIC_LUA))
+DEFINES += -DDYNAMIC_LUA -DDYNAMIC_LUA_DLL=\"lua$(LUA_VER).dll\"
+else
+EXTRA_LIBS += $(LUA)/lib/lua$(LUA_VER).lib
+endif
+endif
+
+##############################
 ifeq (yes, $(GETTEXT))
 DEFINES += -DDYNAMIC_GETTEXT
 endif
--- a/src/Make_ming.mak
+++ b/src/Make_ming.mak
@@ -104,6 +104,23 @@ PERLLIB=$(PERL)/lib
 PERLLIBS=$(PERLLIB)/Core
 endif
 
+# uncomment 'LUA' if you want a Lua-enabled version
+#LUA=/usr/local
+ifdef LUA
+ifndef DYNAMIC_LUA
+DYNAMIC_LUA=yes
+endif
+
+ifndef LUA_VER
+LUA_VER=51
+endif
+
+ifeq (no,$(DYNAMIC_LUA))
+LUA_LIB = -L$(LUA)/lib -llua
+endif
+
+endif
+
 # uncomment 'MZSCHEME' if you want a MzScheme-enabled version
 #MZSCHEME=d:/plt
 ifdef MZSCHEME
@@ -294,6 +311,13 @@ CFLAGS += -DDYNAMIC_PERL -DDYNAMIC_PERL_
 endif
 endif
 
+ifdef LUA
+CFLAGS += -I$(LUA)/include -DFEAT_LUA
+ifeq (yes, $(DYNAMIC_LUA))
+CFLAGS += -DDYNAMIC_LUA -DDYNAMIC_LUA_DLL=\"lua$(LUA_VER).dll\"
+endif
+endif
+
 ifdef MZSCHEME
 CFLAGS += -I$(MZSCHEME)/include -DFEAT_MZSCHEME -DMZSCHEME_COLLECTS=\"$(MZSCHEME)/collects\"
 ifeq (yes, $(DYNAMIC_MZSCHEME))
@@ -427,6 +451,9 @@ OBJ = \
 ifdef PERL
 OBJ += $(OUTDIR)/if_perl.o
 endif
+ifdef LUA
+OBJ += $(OUTDIR)/if_lua.o
+endif
 ifdef MZSCHEME
 OBJ += $(OUTDIR)/if_mzsch.o
 MZSCHEME_INCL = if_mzsch.h
@@ -549,7 +576,7 @@ uninstal.exe: uninstal.c
 	$(CC) $(CFLAGS) -o uninstal.exe uninstal.c $(LIB)
 
 $(TARGET): $(OUTDIR) $(OBJ)
-	$(CC) $(CFLAGS) $(LFLAGS) -o $@ $(OBJ) $(LIB) -lole32 -luuid $(MZSCHEME_LIBDIR) $(MZSCHEME_LIB) $(PYTHONLIB) $(RUBYLIB)
+	$(CC) $(CFLAGS) $(LFLAGS) -o $@ $(OBJ) $(LIB) -lole32 -luuid $(LUA_LIB) $(MZSCHEME_LIBDIR) $(MZSCHEME_LIB) $(PYTHONLIB) $(RUBYLIB)
 
 upx: exes
 	upx gvim.exe
@@ -632,7 +659,7 @@ ifneq (sh.exe, $(SHELL))
 	@echo 'char_u *default_vim_dir = (char_u *)"$(VIMRCLOC)";' >> pathdef.c
 	@echo 'char_u *default_vimruntime_dir = (char_u *)"$(VIMRUNTIMEDIR)";' >> pathdef.c
 	@echo 'char_u *all_cflags = (char_u *)"$(CC) $(CFLAGS)";' >> pathdef.c
-	@echo 'char_u *all_lflags = (char_u *)"$(CC) $(CFLAGS) $(LFLAGS) -o $(TARGET) $(LIB) -lole32 -luuid $(MZSCHEME_LIBDIR) $(MZSCHEME_LIB) $(PYTHONLIB) $(RUBYLIB)";' >> pathdef.c
+	@echo 'char_u *all_lflags = (char_u *)"$(CC) $(CFLAGS) $(LFLAGS) -o $(TARGET) $(LIB) -lole32 -luuid $(LUA_LIB) $(MZSCHEME_LIBDIR) $(MZSCHEME_LIB) $(PYTHONLIB) $(RUBYLIB)";' >> pathdef.c
 	@echo 'char_u *compiled_user = (char_u *)"$(USERNAME)";' >> pathdef.c
 	@echo 'char_u *compiled_sys = (char_u *)"$(USERDOMAIN)";' >> pathdef.c
 else
@@ -642,7 +669,7 @@ else
 	@echo char_u *default_vim_dir = (char_u *)"$(VIMRCLOC)"; >> pathdef.c
 	@echo char_u *default_vimruntime_dir = (char_u *)"$(VIMRUNTIMEDIR)"; >> pathdef.c
 	@echo char_u *all_cflags = (char_u *)"$(CC) $(CFLAGS)"; >> pathdef.c
-	@echo char_u *all_lflags = (char_u *)"$(CC) $(CFLAGS) $(LFLAGS) -o $(TARGET) $(LIB) -lole32 -luuid $(MZSCHEME_LIBDIR) $(MZSCHEME_LIB) $(PYTHONLIB) $(RUBYLIB)"; >> pathdef.c
+	@echo char_u *all_lflags = (char_u *)"$(CC) $(CFLAGS) $(LFLAGS) -o $(TARGET) $(LIB) -lole32 -luuid $(LUA_LIB) $(MZSCHEME_LIBDIR) $(MZSCHEME_LIB) $(PYTHONLIB) $(RUBYLIB)"; >> pathdef.c
 	@echo char_u *compiled_user = (char_u *)"$(USERNAME)"; >> pathdef.c
 	@echo char_u *compiled_sys = (char_u *)"$(USERDOMAIN)"; >> pathdef.c
 endif
--- a/src/Make_mvc.mak
+++ b/src/Make_mvc.mak
@@ -30,6 +30,11 @@
 #	  is yes)
 #	Global IME support: GIME=yes (requires GUI=yes)
 #
+#	Lua interface:
+#	  LUA=[Path to Lua directory]
+#	  DYNAMIC_LUA=yes (to load the Lua DLL dynamically)
+#	  LUA_VER=[Lua version]  (default is 51)
+#
 #	MzScheme interface:
 #	  MZSCHEME=[Path to MzScheme directory]
 #	  DYNAMIC_MZSCHEME=yes (to load the MzScheme DLLs dynamically)
@@ -152,6 +157,9 @@ OBJDIR = .\ObjC
 !if "$(OLE)" == "yes"
 OBJDIR = $(OBJDIR)O
 !endif
+!ifdef LUA
+OBJDIR = $(OBJDIR)U
+!endif
 !ifdef PERL
 OBJDIR = $(OBJDIR)L
 !endif
@@ -364,6 +372,9 @@ MSVCVER = 10.0
 !if "$(_NMAKE_VER)" == "10.00.30319.01"
 MSVCVER = 10.0
 !endif
+!if "$(_NMAKE_VER)" == "9.00.30729.01"
+MSVCVER = 9.0
+!endif
 !endif
 
 # Abort bulding VIM if version of VC is unrecognised.
@@ -609,6 +620,27 @@ TCL_LIB = $(TCL)\lib\tcl$(TCL_VER)vc.lib
 !endif
 !endif
 
+# Lua interface
+!ifdef LUA
+!ifndef LUA_VER
+LUA_VER = 51
+!endif
+!message Lua requested (version $(LUA_VER)) - root dir is "$(LUA)"
+!if "$(DYNAMIC_LUA)" == "yes"
+!message Lua DLL will be loaded dynamically
+!endif
+CFLAGS = $(CFLAGS) -DFEAT_LUA
+LUA_OBJ = $(OUTDIR)\if_lua.obj
+LUA_INC = /I "$(LUA)\include" /I "$(LUA)"
+!if "$(DYNAMIC_LUA)" == "yes"
+CFLAGS = $(CFLAGS) -DDYNAMIC_LUA \
+		-DDYNAMIC_LUA_DLL=\"lua$(LUA_VER).dll\"
+LUA_LIB = /nodefaultlib:lua$(LUA_VER).lib
+!else
+LUA_LIB = "$(LUA)\lib\lua$(LUA_VER).lib"
+!endif
+!endif
+
 # PYTHON interface
 !ifdef PYTHON
 !ifndef PYTHON_VER
@@ -803,7 +835,7 @@ conflags = $(conflags) /map /mapinfo:lin
 
 LINKARGS1 = $(linkdebug) $(conflags)
 LINKARGS2 = $(CON_LIB) $(GUI_LIB) $(LIBC) $(OLE_LIB)  user32.lib $(SNIFF_LIB) \
-		$(MZSCHEME_LIB) $(PERL_LIB) $(PYTHON_LIB) $(RUBY_LIB) \
+		$(LUA_LIB) $(MZSCHEME_LIB) $(PERL_LIB) $(PYTHON_LIB) $(RUBY_LIB) \
 		$(TCL_LIB) $(NETBEANS_LIB) $(XPM_LIB) $(LINK_PDB)
 
 # Report link time code generation progress if used. 
@@ -819,11 +851,12 @@ all:	$(VIM).exe vimrun.exe install.exe u
 		GvimExt/gvimext.dll
 
 $(VIM).exe: $(OUTDIR) $(OBJ) $(GUI_OBJ) $(OLE_OBJ) $(OLE_IDL) $(MZSCHEME_OBJ) \
-		$(PERL_OBJ) $(PYTHON_OBJ) $(RUBY_OBJ) $(TCL_OBJ) $(SNIFF_OBJ) \
-		$(CSCOPE_OBJ) $(NETBEANS_OBJ) $(XPM_OBJ) version.c version.h
+		$(LUA_OBJ) $(PERL_OBJ) $(PYTHON_OBJ) $(RUBY_OBJ) $(TCL_OBJ) \
+		$(SNIFF_OBJ) $(CSCOPE_OBJ) $(NETBEANS_OBJ) $(XPM_OBJ) \
+		version.c version.h
 	$(CC) $(CFLAGS) version.c
 	$(link) $(LINKARGS1) -out:$(VIM).exe $(OBJ) $(GUI_OBJ) $(OLE_OBJ) \
-		$(MZSCHEME_OBJ) $(PERL_OBJ) $(PYTHON_OBJ) $(RUBY_OBJ) \
+		$(LUA_OBJ) $(MZSCHEME_OBJ) $(PERL_OBJ) $(PYTHON_OBJ) $(RUBY_OBJ) \
 		$(TCL_OBJ) $(SNIFF_OBJ) $(CSCOPE_OBJ) $(NETBEANS_OBJ) \
 		$(XPM_OBJ) $(OUTDIR)\version.obj $(LINKARGS2)
 
@@ -961,6 +994,9 @@ testclean:
 
 $(OUTDIR)/if_cscope.obj: $(OUTDIR) if_cscope.c  $(INCL)
 
+$(OUTDIR)/if_lua.obj: $(OUTDIR) if_lua.c  $(INCL)
+	$(CC) $(CFLAGS) $(LUA_INC) if_lua.c
+
 if_perl.c : if_perl.xs typemap
 	$(PERL_EXE) $(XSUBPP) -prototypes -typemap $(XSUBPP_TYPEMAP) \
 		-typemap typemap if_perl.xs > if_perl.c
--- a/src/Makefile
+++ b/src/Makefile
@@ -38,6 +38,7 @@
 #	  could be lost.
 #	- Uncomment one or more of these lines to include an interface;
 #	  each makes Vim quite a bit bigger:
+#		--enable-luainterp	for Lua interpreter
 #		--enable-perlinterp	for Perl interpreter
 #		--enable-pythoninterp	for Python interpreter
 #		--enable-rubyinterp	for Ruby interpreter
@@ -395,6 +396,13 @@ CClink = $(CC)
 #CONF_OPT_RUBY = --enable-rubyinterp
 #CONF_OPT_RUBY = --enable-rubyinterp --with-ruby-command=ruby1.9.1
 
+# LUA
+# Uncomment this when you want to include the Lua interface.
+#CONF_OPT_LUA = --enable-luainterp
+# Lua installation dir (also in LUA_PREFIX environment variable)
+#CONF_OPT_LUA_PREFIX = --with-lua-prefix=/usr/local
+#CONF_OPT_LUA_PREFIX = --with-lua-prefix=/usr
+
 # MZSCHEME
 # Uncomment this when you want to include the MzScheme interface.
 #CONF_OPT_MZSCHEME = --enable-mzschemeinterp
@@ -1296,7 +1304,7 @@ SHELL = /bin/sh
 .SUFFIXES: .c .o .pro
 
 PRE_DEFS = -Iproto $(DEFS) $(GUI_DEFS) $(GUI_IPATH) $(CPPFLAGS) $(EXTRA_IPATHS)
-POST_DEFS = $(X_CFLAGS) $(MZSCHEME_CFLAGS) $(PERL_CFLAGS) $(ECL_CFLAGS) $(PYTHON_CFLAGS) $(TCL_CFLAGS) $(RUBY_CFLAGS) $(EXTRA_DEFS)
+POST_DEFS = $(X_CFLAGS) $(LUA_CFLAGS) $(MZSCHEME_CFLAGS) $(PERL_CFLAGS) $(ECL_CFLAGS) $(PYTHON_CFLAGS) $(TCL_CFLAGS) $(RUBY_CFLAGS) $(EXTRA_DEFS)
 
 ALL_CFLAGS = $(PRE_DEFS) $(CFLAGS) $(PROFILE_CFLAGS) $(POST_DEFS)
 
@@ -1311,7 +1319,7 @@ LINT_EXTRA = -DUSE_SNIFF -DHANGUL_INPUT 
 DEPEND_CFLAGS = -DPROTO -DDEPEND -DFEAT_GUI $(LINT_CFLAGS)
 
 ALL_LIB_DIRS = $(GUI_LIBS_DIR) $(X_LIBS_DIR)
-ALL_LIBS = $(GUI_LIBS1) $(GUI_X_LIBS) $(GUI_LIBS2) $(X_PRE_LIBS) $(X_LIBS) $(X_EXTRA_LIBS) $(LIBS) $(EXTRA_LIBS) $(MZSCHEME_LIBS) $(PERL_LIBS) $(PYTHON_LIBS) $(TCL_LIBS) $(RUBY_LIBS) $(PROFILE_LIBS)
+ALL_LIBS = $(GUI_LIBS1) $(GUI_X_LIBS) $(GUI_LIBS2) $(X_PRE_LIBS) $(X_LIBS) $(X_EXTRA_LIBS) $(LIBS) $(EXTRA_LIBS) $(LUA_LIBS) $(MZSCHEME_LIBS) $(PERL_LIBS) $(PYTHON_LIBS) $(TCL_LIBS) $(RUBY_LIBS) $(PROFILE_LIBS)
 
 # abbreviations
 DEST_BIN = $(DESTDIR)$(BINDIR)
@@ -1414,13 +1422,13 @@ BASIC_SRC = \
 	window.c \
 	$(OS_EXTRA_SRC)
 
-SRC =	$(BASIC_SRC) $(GUI_SRC) $(HANGULIN_SRC) $(MZSCHEME_SRC) \
+SRC =	$(BASIC_SRC) $(GUI_SRC) $(HANGULIN_SRC) $(LUA_SRC) $(MZSCHEME_SRC) \
 	$(PERL_SRC) $(PYTHON_SRC) $(TCL_SRC) $(RUBY_SRC) \
 	$(SNIFF_SRC) $(WORKSHOP_SRC) $(WSDEBUG_SRC)
 
 TAGS_SRC = *.c *.cpp if_perl.xs
 
-EXTRA_SRC = hangulin.c if_mzsch.c auto/if_perl.c if_perlsfio.c \
+EXTRA_SRC = hangulin.c if_lua.c if_mzsch.c auto/if_perl.c if_perlsfio.c \
 	    if_python.c if_tcl.c if_ruby.c if_sniff.c gui_beval.c \
 	    workshop.c wsdebug.c integration.c netbeans.c
 
@@ -1487,6 +1495,7 @@ OBJ = \
 	objects/undo.o \
 	objects/window.o \
 	$(GUI_OBJ) \
+	$(LUA_OBJ) \
 	$(MZSCHEME_OBJ) \
 	$(PERL_OBJ) \
 	$(PYTHON_OBJ) \
@@ -1587,6 +1596,7 @@ config auto/config.mk: auto/configure co
 		$(CONF_OPT_SNIFF) $(CONF_OPT_FEAT) $(CONF_TERM_LIB) \
 		$(CONF_OPT_COMPBY) $(CONF_OPT_ACL)  $(CONF_OPT_NETBEANS) \
 		$(CONF_ARGS) $(CONF_OPT_MZSCHEME) $(CONF_OPT_PLTHOME) \
+		$(CONF_OPT_LUA) $(CONF_OPT_LUA_PREFIX) \
 		$(CONF_OPT_SYSMOUSE)
 
 # Use "make reconfig" to rerun configure without cached values.
@@ -2439,6 +2449,9 @@ objects/if_cscope.o: if_cscope.c
 objects/if_xcmdsrv.o: if_xcmdsrv.c
 	$(CCC) -o $@ if_xcmdsrv.c
 
+objects/if_lua.o: if_lua.c
+	$(CCC) -o $@ if_lua.c
+
 objects/if_mzsch.o: if_mzsch.c $(MZSCHEME_EXTRA)
 	$(CCC) -o $@ $(MZSCHEME_CFLAGS_EXTRA) if_mzsch.c
  
@@ -2945,6 +2958,10 @@ objects/hangulin.o: hangulin.c vim.h aut
  auto/osdef.h ascii.h keymap.h term.h macros.h option.h structs.h \
  regexp.h gui.h gui_beval.h proto/gui_beval.pro ex_cmds.h proto.h \
  globals.h farsi.h arabic.h
+objects/if_lua.o: if_lua.c vim.h auto/config.h feature.h os_unix.h \
+ auto/osdef.h ascii.h keymap.h term.h macros.h option.h structs.h \
+ regexp.h gui.h gui_beval.h proto/gui_beval.pro ex_cmds.h proto.h \
+ globals.h farsi.h arabic.h
 objects/if_mzsch.o: if_mzsch.c vim.h auto/config.h feature.h os_unix.h \
  auto/osdef.h ascii.h keymap.h term.h macros.h option.h structs.h \
  regexp.h gui.h gui_beval.h proto/gui_beval.pro ex_cmds.h proto.h \
--- a/src/auto/configure
+++ b/src/auto/configure
@@ -662,6 +662,12 @@ MZSCHEME_PRO
 MZSCHEME_OBJ
 MZSCHEME_SRC
 vi_cv_path_mzscheme
+LUA_CFLAGS
+LUA_LIBS
+LUA_PRO
+LUA_OBJ
+LUA_SRC
+vi_cv_path_lua
 compiledby
 dogvimdiff
 dovimdiff
@@ -742,6 +748,8 @@ with_features
 with_compiledby
 enable_xsmp
 enable_xsmp_interact
+enable_luainterp
+with_lua_prefix
 enable_mzschemeinterp
 with_plthome
 enable_perlinterp
@@ -1405,6 +1413,7 @@ Optional Features:
   --disable-selinux	  Don't check for SELinux support.
   --disable-xsmp          Disable XSMP session management
   --disable-xsmp-interact Disable XSMP interaction
+  --enable-luainterp      Include Lua interpreter.
   --enable-mzschemeinterp   Include MzScheme interpreter.
   --enable-perlinterp     Include Perl interpreter.
   --enable-pythoninterp   Include Python interpreter.
@@ -1446,6 +1455,7 @@ Optional Packages:
   --with-modified-by=NAME       name of who modified a release version
   --with-features=TYPE    tiny, small, normal, big or huge (default: normal)
   --with-compiledby=NAME  name to show in :version message
+  --with-lua-prefix=PFX   Prefix where Lua is installed.
   --with-plthome=PLTHOME   Use PLTHOME.
   --with-python-config-dir=PATH  Python's config directory
   --with-tclsh=PATH       which tclsh to use (default: tclsh8.0)
@@ -4562,6 +4572,145 @@ else
 $as_echo "yes" >&6; }
 fi
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking --enable-luainterp argument" >&5
+$as_echo_n "checking --enable-luainterp argument... " >&6; }
+# Check whether --enable-luainterp was given.
+if test "${enable_luainterp+set}" = set; then :
+  enableval=$enable_luainterp;
+else
+  enable_luainterp="no"
+fi
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $enable_luainterp" >&5
+$as_echo "$enable_luainterp" >&6; }
+
+if test "$enable_luainterp" = "yes"; then
+
+
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking --with-lua-prefix argument" >&5
+$as_echo_n "checking --with-lua-prefix argument... " >&6; }
+
+# Check whether --with-lua_prefix was given.
+if test "${with_lua_prefix+set}" = set; then :
+  withval=$with_lua_prefix; with_lua_prefix="$withval"; { $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_lua_prefix" >&5
+$as_echo "$with_lua_prefix" >&6; }
+else
+  with_lua_prefix="";{ $as_echo "$as_me:${as_lineno-$LINENO}: result: \"no\"" >&5
+$as_echo "\"no\"" >&6; }
+fi
+
+
+  if test "X$with_lua_prefix" != "X"; then
+       vi_cv_path_lua_pfx="$with_lua_prefix"
+  else
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking LUA_PREFIX environment var" >&5
+$as_echo_n "checking LUA_PREFIX environment var... " >&6; }
+    if test "X$LUA_PREFIX" != "X"; then
+	{ $as_echo "$as_me:${as_lineno-$LINENO}: result: \"$LUA_PREFIX\"" >&5
+$as_echo "\"$LUA_PREFIX\"" >&6; }
+	vi_cv_path_lua_pfx="$LUA_PREFIX"
+    else
+	{ $as_echo "$as_me:${as_lineno-$LINENO}: result: \"not set\"" >&5
+$as_echo "\"not set\"" >&6; }
+    fi
+  fi
+
+  LUA_INC=
+  if test "X$vi_cv_path_lua_pfx" != "X"; then
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking if lua.h can be found in $vi_cv_path_lua_pfx/include" >&5
+$as_echo_n "checking if lua.h can be found in $vi_cv_path_lua_pfx/include... " >&6; }
+    if test -f $vi_cv_path_lua_pfx/include/lua.h; then
+      { $as_echo "$as_me:${as_lineno-$LINENO}: result: \"yes\"" >&5
+$as_echo "\"yes\"" >&6; }
+    else
+      { $as_echo "$as_me:${as_lineno-$LINENO}: result: \"no\"" >&5
+$as_echo "\"no\"" >&6; }
+            # Extract the first word of "lua", so it can be a program name with args.
+set dummy lua; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if test "${ac_cv_path_vi_cv_path_lua+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+  case $vi_cv_path_lua in
+  [\\/]* | ?:[\\/]*)
+  ac_cv_path_vi_cv_path_lua="$vi_cv_path_lua" # Let the user override the test with a path.
+  ;;
+  *)
+  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
+    ac_cv_path_vi_cv_path_lua="$as_dir/$ac_word$ac_exec_ext"
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+  ;;
+esac
+fi
+vi_cv_path_lua=$ac_cv_path_vi_cv_path_lua
+if test -n "$vi_cv_path_lua"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $vi_cv_path_lua" >&5
+$as_echo "$vi_cv_path_lua" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+
+      if test "X$vi_cv_path_lua" != "X"; then
+                { $as_echo "$as_me:${as_lineno-$LINENO}: checking Lua version" >&5
+$as_echo_n "checking Lua version... " >&6; }
+if test "${vi_cv_version_lua+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+   vi_cv_version_lua=`${vi_cv_path_lua} -e "print(_VERSION:sub(5,7))"`
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $vi_cv_version_lua" >&5
+$as_echo "$vi_cv_version_lua" >&6; }
+        { $as_echo "$as_me:${as_lineno-$LINENO}: checking if lua.h can be found in $vi_cv_path_lua_pfx/include/lua$vi_cv_version_lua" >&5
+$as_echo_n "checking if lua.h can be found in $vi_cv_path_lua_pfx/include/lua$vi_cv_version_lua... " >&6; }
+        if test -f $vi_cv_path_lua_pfx/include/lua$vi_cv_version_lua/lua.h; then
+          { $as_echo "$as_me:${as_lineno-$LINENO}: result: \"yes\"" >&5
+$as_echo "\"yes\"" >&6; }
+          LUA_INC=/lua$vi_cv_version_lua
+        else
+          { $as_echo "$as_me:${as_lineno-$LINENO}: result: \"no\"" >&5
+$as_echo "\"no\"" >&6; }
+          vi_cv_path_lua_pfx=
+        fi
+      fi
+    fi
+  fi
+
+  if test "X$vi_cv_path_lua_pfx" != "X"; then
+    if test "X$vi_cv_version_lua" != "X"; then
+            LUA_LIBS="-L${vi_cv_path_lua_pfx}/lib -llua$vi_cv_version_lua"
+    else
+      LUA_LIBS="-L${vi_cv_path_lua_pfx}/lib -llua"
+    fi
+    LUA_CFLAGS="-I${vi_cv_path_lua_pfx}/include${LUA_INC}"
+    LUA_SRC="if_lua.c"
+    LUA_OBJ="objects/if_lua.o"
+    LUA_PRO="if_lua.pro"
+    $as_echo "#define FEAT_LUA 1" >>confdefs.h
+
+  fi
+
+
+
+
+
+fi
+
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking --enable-mzschemeinterp argument" >&5
 $as_echo_n "checking --enable-mzschemeinterp argument... " >&6; }
 # Check whether --enable-mzschemeinterp was given.
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -595,6 +595,9 @@ free_buffer(buf)
     buf_T	*buf;
 {
     free_buffer_stuff(buf, TRUE);
+#ifdef FEAT_LUA
+    lua_buffer_free(buf);
+#endif
 #ifdef FEAT_MZSCHEME
     mzscheme_buffer_free(buf);
 #endif
--- a/src/config.aap.in
+++ b/src/config.aap.in
@@ -22,6 +22,12 @@ X_PRE_LIBS	= @X_PRE_LIBS@
 X_EXTRA_LIBS	= @X_EXTRA_LIBS@
 X_LIBS		= @X_LIB@
 
+LUA_LIBS	= @LUA_LIBS@
+LUA_SRC		= @LUA_SRC@
+LUA_OBJ		= @LUA_OBJ@
+LUA_CFLAGS	= @LUA_CFLAGS@
+LUA_PRO		= @LUA_PRO@
+
 MZSCHEME_LIBS	= @MZSCHEME_LIBS@
 MZSCHEME_SRC	= @MZSCHEME_SRC@
 MZSCHEME_OBJ	= @MZSCHEME_OBJ@
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -316,6 +316,9 @@
 /* Define if you want huge features. */
 #undef FEAT_HUGE
 
+/* Define if you want to include the Lua interpreter. */
+#undef FEAT_LUA
+
 /* Define if you want to include the MzScheme interpreter. */
 #undef FEAT_MZSCHEME
 
--- a/src/config.mk.in
+++ b/src/config.mk.in
@@ -36,6 +36,12 @@ X_PRE_LIBS	= @X_PRE_LIBS@
 X_EXTRA_LIBS	= @X_EXTRA_LIBS@
 X_LIBS		= @X_LIB@
 
+LUA_LIBS	= @LUA_LIBS@
+LUA_SRC		= @LUA_SRC@
+LUA_OBJ		= @LUA_OBJ@
+LUA_CFLAGS	= @LUA_CFLAGS@
+LUA_PRO		= @LUA_PRO@
+
 MZSCHEME_LIBS	= @MZSCHEME_LIBS@
 MZSCHEME_SRC	= @MZSCHEME_SRC@
 MZSCHEME_OBJ	= @MZSCHEME_OBJ@
--- a/src/configure.in
+++ b/src/configure.in
@@ -410,6 +410,81 @@ else
   AC_MSG_RESULT(yes)
 fi
 
+dnl Check for Lua feature.
+AC_MSG_CHECKING(--enable-luainterp argument)
+AC_ARG_ENABLE(luainterp,
+	[  --enable-luainterp      Include Lua interpreter.], ,
+	[enable_luainterp="no"])
+AC_MSG_RESULT($enable_luainterp)
+
+if test "$enable_luainterp" = "yes"; then
+  dnl -- find the lua executable
+  AC_SUBST(vi_cv_path_lua)
+
+  AC_MSG_CHECKING(--with-lua-prefix argument)
+  AC_ARG_WITH(lua_prefix,
+	[  --with-lua-prefix=PFX   Prefix where Lua is installed.],
+	with_lua_prefix="$withval"; AC_MSG_RESULT($with_lua_prefix),
+	with_lua_prefix="";AC_MSG_RESULT("no"))
+
+  if test "X$with_lua_prefix" != "X"; then
+       vi_cv_path_lua_pfx="$with_lua_prefix"
+  else
+    AC_MSG_CHECKING(LUA_PREFIX environment var)
+    if test "X$LUA_PREFIX" != "X"; then
+	AC_MSG_RESULT("$LUA_PREFIX")
+	vi_cv_path_lua_pfx="$LUA_PREFIX"
+    else
+	AC_MSG_RESULT("not set")
+    fi
+  fi
+
+  LUA_INC=
+  if test "X$vi_cv_path_lua_pfx" != "X"; then
+    AC_MSG_CHECKING(if lua.h can be found in $vi_cv_path_lua_pfx/include)
+    if test -f $vi_cv_path_lua_pfx/include/lua.h; then
+      AC_MSG_RESULT("yes")
+    else
+      AC_MSG_RESULT("no")
+      dnl -- try to find Lua executable
+      AC_PATH_PROG(vi_cv_path_lua, lua)
+      if test "X$vi_cv_path_lua" != "X"; then
+        dnl -- find Lua version
+        AC_CACHE_CHECK(Lua version, vi_cv_version_lua,
+        [ vi_cv_version_lua=`${vi_cv_path_lua} -e "print(_VERSION:sub(5,7))"` ])
+        AC_MSG_CHECKING(if lua.h can be found in $vi_cv_path_lua_pfx/include/lua$vi_cv_version_lua)
+        if test -f $vi_cv_path_lua_pfx/include/lua$vi_cv_version_lua/lua.h; then
+          AC_MSG_RESULT("yes")
+          LUA_INC=/lua$vi_cv_version_lua
+        else
+          AC_MSG_RESULT("no")
+          vi_cv_path_lua_pfx=
+        fi
+      fi
+    fi
+  fi
+
+  if test "X$vi_cv_path_lua_pfx" != "X"; then
+    if test "X$vi_cv_version_lua" != "X"; then
+      dnl Test alternate location using version
+      LUA_LIBS="-L${vi_cv_path_lua_pfx}/lib -llua$vi_cv_version_lua"
+    else
+      LUA_LIBS="-L${vi_cv_path_lua_pfx}/lib -llua"
+    fi
+    LUA_CFLAGS="-I${vi_cv_path_lua_pfx}/include${LUA_INC}"
+    LUA_SRC="if_lua.c"
+    LUA_OBJ="objects/if_lua.o"
+    LUA_PRO="if_lua.pro"
+    AC_DEFINE(FEAT_LUA)
+  fi
+  AC_SUBST(LUA_SRC)
+  AC_SUBST(LUA_OBJ)
+  AC_SUBST(LUA_PRO)
+  AC_SUBST(LUA_LIBS)
+  AC_SUBST(LUA_CFLAGS)
+fi
+
+
 dnl Check for MzScheme feature.
 AC_MSG_CHECKING(--enable-mzschemeinterp argument)
 AC_ARG_ENABLE(mzschemeinterp,
--- a/src/eval.c
+++ b/src/eval.c
@@ -5912,7 +5912,7 @@ list_equal(l1, l2, ic)
 }
 
 #if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
-	|| defined(PROTO)
+	|| defined(FEAT_LUA) || defined(PROTO)
 /*
  * Return the dictitem that an entry in a hashtable points to.
  */
@@ -11912,6 +11912,11 @@ f_has(argvars, rettv)
 #ifdef FEAT_LOCALMAP
 	"localmap",
 #endif
+#ifdef FEAT_LUA
+# ifndef DYNAMIC_LUA
+	"lua",
+# endif
+#endif
 #ifdef FEAT_MENU
 	"menu",
 #endif
@@ -12167,6 +12172,10 @@ f_has(argvars, rettv)
 	else if (STRICMP(name, "iconv") == 0)
 	    n = iconv_enabled(FALSE);
 #endif
+#ifdef DYNAMIC_LUA
+	else if (STRICMP(name, "lua") == 0)
+	    n = lua_enabled(FALSE);
+#endif
 #ifdef DYNAMIC_MZSCHEME
 	else if (STRICMP(name, "mzscheme") == 0)
 	    n = mzscheme_enabled(FALSE);
--- a/src/ex_cmds.h
+++ b/src/ex_cmds.h
@@ -571,6 +571,12 @@ EX(CMD_lrewind,		"lrewind",	ex_cc,
 			RANGE|NOTADR|COUNT|TRLBAR|BANG),
 EX(CMD_ltag,		"ltag",	ex_tag,
 			NOTADR|TRLBAR|BANG|WORD1),
+EX(CMD_lua,		"lua",		ex_lua,
+			RANGE|EXTRA|NEEDARG|CMDWIN),
+EX(CMD_luado,		"luado",	ex_luado,
+			RANGE|DFLALL|EXTRA|NEEDARG|CMDWIN),
+EX(CMD_luafile,		"luafile",	ex_luafile,
+			RANGE|FILE1|NEEDARG|CMDWIN),
 EX(CMD_lunmap,		"lunmap",	ex_unmap,
 			EXTRA|TRLBAR|NOTRLCOM|USECTRLV|CMDWIN),
 EX(CMD_lvimgrep,	"lvimgrep",	ex_vimgrep,
--- a/src/ex_docmd.c
+++ b/src/ex_docmd.c
@@ -248,6 +248,11 @@ static void	ex_popup __ARGS((exarg_T *ea
 # define ex_rundo		ex_ni
 # define ex_wundo		ex_ni
 #endif
+#ifndef FEAT_LUA
+# define ex_lua			ex_script_ni
+# define ex_luado		ex_ni
+# define ex_luafile		ex_ni
+#endif
 #ifndef FEAT_MZSCHEME
 # define ex_mzscheme		ex_script_ni
 # define ex_mzfile		ex_ni
@@ -2543,6 +2548,7 @@ do_one_cmd(cmdlinep, sourcing,
 	    case CMD_leftabove:
 	    case CMD_let:
 	    case CMD_lockmarks:
+	    case CMD_lua:
 	    case CMD_match:
 	    case CMD_mzscheme:
 	    case CMD_perl:
--- a/src/feature.h
+++ b/src/feature.h
@@ -1194,6 +1194,7 @@
 /*
  * These features can only be included by using a configure argument.  See the
  * Makefile for a line to uncomment.
+ * +lua                 Lua interface: "--enable-luainterp"
  * +mzscheme		MzScheme interface: "--enable-mzscheme"
  * +perl		Perl interface: "--enable-perlinterp"
  * +python		Python interface: "--enable-pythoninterp"
--- a/src/globals.h
+++ b/src/globals.h
@@ -1417,7 +1417,8 @@ EXTERN char_u e_libcall[]	INIT(= N_("E36
 #endif
 #if defined(DYNAMIC_PERL) || defined(DYNAMIC_PYTHON) || defined(DYNAMIC_RUBY) \
 	|| defined(DYNAMIC_TCL) || defined(DYNAMIC_ICONV) \
-	|| defined(DYNAMIC_GETTEXT) || defined(DYNAMIC_MZSCHEME)
+	|| defined(DYNAMIC_GETTEXT) || defined(DYNAMIC_MZSCHEME) \
+        || defined(DYNAMIC_LUA)
 EXTERN char_u e_loadlib[]	INIT(= N_("E370: Could not load library %s"));
 EXTERN char_u e_loadfunc[]	INIT(= N_("E448: Could not load library function %s"));
 #endif
new file mode 100644
--- /dev/null
+++ b/src/if_lua.c
@@ -0,0 +1,1099 @@
+/* vi:set ts=8 sts=4 sw=4:
+ *
+ * VIM - Vi IMproved	by Bram Moolenaar
+ *
+ * Lua interface by Luis Carvalho
+ *
+ * Do ":help uganda"  in Vim to read copying and usage conditions.
+ * Do ":help credits" in Vim to see a list of people who contributed.
+ * See README.txt for an overview of the Vim source code.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+#include "vim.h"
+
+/* Only do the following when the feature is enabled.  Needed for "make
+ * depend". */
+#if defined(FEAT_LUA) || defined(PROTO)
+
+#define LUAVIM_CHUNKNAME "vim chunk"
+#define LUAVIM_NAME "vim"
+
+typedef buf_T *luaV_Buffer;
+typedef win_T *luaV_Window;
+typedef void (*msgfunc_T)(char_u *);
+
+static const char LUAVIM_BUFFER[] = "buffer";
+static const char LUAVIM_WINDOW[] = "window";
+static const char LUAVIM_FREE[] = "luaV_free";
+
+#define luaV_getfield(L, s) \
+    lua_pushlightuserdata((L), (void *)(s)); \
+    lua_rawget((L), LUA_REGISTRYINDEX)
+#define luaV_checksandbox(L) \
+    if (sandbox) luaL_error((L), "not allowed in sandbox")
+#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
+#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
+
+
+#ifdef DYNAMIC_LUA
+/* lauxlib */
+#define luaL_register dll_luaL_register
+#define luaL_typerror dll_luaL_typerror
+#define luaL_checklstring dll_luaL_checklstring
+#define luaL_checkinteger dll_luaL_checkinteger
+#define luaL_optinteger dll_luaL_optinteger
+#define luaL_checktype dll_luaL_checktype
+#define luaL_error dll_luaL_error
+#define luaL_loadfile dll_luaL_loadfile
+#define luaL_loadbuffer dll_luaL_loadbuffer
+#define luaL_newstate dll_luaL_newstate
+#define luaL_buffinit dll_luaL_buffinit
+#define luaL_prepbuffer dll_luaL_prepbuffer
+#define luaL_addlstring dll_luaL_addlstring
+#define luaL_pushresult dll_luaL_pushresult
+/* lua */
+#define lua_close dll_lua_close
+#define lua_gettop dll_lua_gettop
+#define lua_settop dll_lua_settop
+#define lua_pushvalue dll_lua_pushvalue
+#define lua_replace dll_lua_replace
+#define lua_isnumber dll_lua_isnumber
+#define lua_isstring dll_lua_isstring
+#define lua_type dll_lua_type
+#define lua_rawequal dll_lua_rawequal
+#define lua_tonumber dll_lua_tonumber
+#define lua_tointeger dll_lua_tointeger
+#define lua_toboolean dll_lua_toboolean
+#define lua_tolstring dll_lua_tolstring
+#define lua_touserdata dll_lua_touserdata
+#define lua_pushnil dll_lua_pushnil
+#define lua_pushnumber dll_lua_pushnumber
+#define lua_pushinteger dll_lua_pushinteger
+#define lua_pushlstring dll_lua_pushlstring
+#define lua_pushstring dll_lua_pushstring
+#define lua_pushfstring dll_lua_pushfstring
+#define lua_pushcclosure dll_lua_pushcclosure
+#define lua_pushboolean dll_lua_pushboolean
+#define lua_pushlightuserdata dll_lua_pushlightuserdata
+#define lua_getfield dll_lua_getfield
+#define lua_rawget dll_lua_rawget
+#define lua_createtable dll_lua_createtable
+#define lua_newuserdata dll_lua_newuserdata
+#define lua_getmetatable dll_lua_getmetatable
+#define lua_setfield dll_lua_setfield
+#define lua_rawset dll_lua_rawset
+#define lua_rawseti dll_lua_rawseti
+#define lua_setmetatable dll_lua_setmetatable
+#define lua_call dll_lua_call
+#define lua_pcall dll_lua_pcall
+/* libs */
+#define luaopen_base dll_luaopen_base
+#define luaopen_table dll_luaopen_table
+#define luaopen_string dll_luaopen_string
+#define luaopen_math dll_luaopen_math
+#define luaopen_os dll_luaopen_os
+#define luaopen_package dll_luaopen_package
+#define luaopen_debug dll_luaopen_debug
+
+/* lauxlib */
+void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
+int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
+const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
+lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
+lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
+void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
+int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
+int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
+int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
+lua_State *(*dll_luaL_newstate) (void);
+void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
+char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
+void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
+void (*dll_luaL_pushresult) (luaL_Buffer *B);
+/* lua */
+void       (*dll_lua_close) (lua_State *L);
+int (*dll_lua_gettop) (lua_State *L);
+void (*dll_lua_settop) (lua_State *L, int idx);
+void (*dll_lua_pushvalue) (lua_State *L, int idx);
+void (*dll_lua_replace) (lua_State *L, int idx);
+int (*dll_lua_isnumber) (lua_State *L, int idx);
+int (*dll_lua_isstring) (lua_State *L, int idx);
+int (*dll_lua_type) (lua_State *L, int idx);
+int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
+lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
+lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
+int (*dll_lua_toboolean) (lua_State *L, int idx);
+const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
+void *(*dll_lua_touserdata) (lua_State *L, int idx);
+void (*dll_lua_pushnil) (lua_State *L);
+void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
+void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
+void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
+void (*dll_lua_pushstring) (lua_State *L, const char *s);
+const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
+void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
+void (*dll_lua_pushboolean) (lua_State *L, int b);
+void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
+void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
+void (*dll_lua_rawget) (lua_State *L, int idx);
+void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
+void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
+int (*dll_lua_getmetatable) (lua_State *L, int objindex);
+void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
+void (*dll_lua_rawset) (lua_State *L, int idx);
+void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
+int (*dll_lua_setmetatable) (lua_State *L, int objindex);
+void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
+int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
+/* libs */
+int (*dll_luaopen_base) (lua_State *L);
+int (*dll_luaopen_table) (lua_State *L);
+int (*dll_luaopen_string) (lua_State *L);
+int (*dll_luaopen_math) (lua_State *L);
+int (*dll_luaopen_os) (lua_State *L);
+int (*dll_luaopen_package) (lua_State *L);
+int (*dll_luaopen_debug) (lua_State *L);
+
+typedef void **luaV_function;
+typedef struct {
+    const char *name;
+    luaV_function func;
+} luaV_Reg;
+
+static const luaV_Reg luaV_dll[] = {
+    /* lauxlib */
+    {"luaL_register", (luaV_function) &dll_luaL_register},
+    {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
+    {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
+    {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
+    {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
+    {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
+    {"luaL_error", (luaV_function) &dll_luaL_error},
+    {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
+    {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
+    {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
+    {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
+    {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
+    {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
+    {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
+    /* lua */
+    {"lua_close", (luaV_function) &dll_lua_close},
+    {"lua_gettop", (luaV_function) &dll_lua_gettop},
+    {"lua_settop", (luaV_function) &dll_lua_settop},
+    {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
+    {"lua_replace", (luaV_function) &dll_lua_replace},
+    {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
+    {"lua_isstring", (luaV_function) &dll_lua_isstring},
+    {"lua_type", (luaV_function) &dll_lua_type},
+    {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
+    {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
+    {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
+    {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
+    {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
+    {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
+    {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
+    {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
+    {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
+    {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
+    {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
+    {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
+    {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
+    {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
+    {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
+    {"lua_getfield", (luaV_function) &dll_lua_getfield},
+    {"lua_rawget", (luaV_function) &dll_lua_rawget},
+    {"lua_createtable", (luaV_function) &dll_lua_createtable},
+    {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
+    {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
+    {"lua_setfield", (luaV_function) &dll_lua_setfield},
+    {"lua_rawset", (luaV_function) &dll_lua_rawset},
+    {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
+    {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
+    {"lua_call", (luaV_function) &dll_lua_call},
+    {"lua_pcall", (luaV_function) &dll_lua_pcall},
+    /* libs */
+    {"luaopen_base", (luaV_function) &dll_luaopen_base},
+    {"luaopen_table", (luaV_function) &dll_luaopen_table},
+    {"luaopen_string", (luaV_function) &dll_luaopen_string},
+    {"luaopen_math", (luaV_function) &dll_luaopen_math},
+    {"luaopen_os", (luaV_function) &dll_luaopen_os},
+    {"luaopen_package", (luaV_function) &dll_luaopen_package},
+    {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
+    {NULL, NULL}
+};
+
+static HINSTANCE hinstLua = 0;
+
+static void end_dynamic_lua (void) {
+    if (hinstLua) {
+        FreeLibrary(hinstLua);
+        hinstLua = 0;
+    }
+}
+
+static int lua_link_init (char *libname, int verbose) {
+    const luaV_Reg *reg;
+    if (hinstLua) return OK;
+    hinstLua = LoadLibrary(libname);
+    if (!hinstLua) {
+        if (verbose) EMSG2(_(e_loadlib), libname);
+        return FAIL;
+    }
+    for (reg = luaV_dll; reg->func; reg++) {
+	if ((*reg->func = GetProcAddress(hinstLua, reg->name)) == NULL) {
+            FreeLibrary(hinstLua);
+            hinstLua = 0;
+            if (verbose) EMSG2(_(e_loadfunc), reg->name);
+            return FAIL;
+        }
+    }
+    return OK;
+}
+
+int lua_enabled (int verbose) {
+    return lua_link_init(DYNAMIC_LUA_DLL, verbose) == OK;
+}
+
+#endif /* DYNAMIC_LUA */
+
+
+/* =======   Internal   ======= */
+
+static void luaV_newmetatable (lua_State *L, const char *tname) {
+    lua_newtable(L);
+    lua_pushlightuserdata(L, (void *) tname);
+    lua_pushvalue(L, -2);
+    lua_rawset(L, LUA_REGISTRYINDEX);
+}
+
+static void *luaV_toudata (lua_State *L, int ud, const char *tname) {
+    void *p = lua_touserdata(L, ud);
+    if (p != NULL) { /* value is userdata? */
+        if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
+            luaV_getfield(L, tname); /* get metatable */
+            if (lua_rawequal(L, -1, -2)) { /* MTs match? */
+                lua_pop(L, 2); /* MTs */
+                return p;
+            }
+        }
+    }
+    return NULL;
+}
+
+static void *luaV_checkudata (lua_State *L, int ud, const char *tname) {
+    void *p = luaV_toudata(L, ud, tname);
+    if (p == NULL) luaL_typerror(L, ud, tname);
+    return p;
+}
+
+static void luaV_pushtypval (lua_State *L, typval_T *tv) {
+    if (tv == NULL) luaL_error(L, "null type");
+    switch (tv->v_type) {
+        case VAR_STRING:
+            lua_pushstring(L, (char *) tv->vval.v_string);
+            break;
+        case VAR_NUMBER:
+            lua_pushinteger(L, (int) tv->vval.v_number);
+            break;
+#ifdef FEAT_FLOAT
+        case VAR_FLOAT:
+            lua_pushnumber(L, (lua_Number) tv->vval.v_float);
+            break;
+#endif
+        case VAR_LIST: {
+            list_T *l = tv->vval.v_list;
+            if (l != NULL) {
+                /* check cache */
+                lua_pushlightuserdata(L, (void *) l);
+                lua_rawget(L, LUA_ENVIRONINDEX);
+                if (lua_isnil(L, -1)) { /* not interned? */
+                    listitem_T *li;
+                    int n = 0;
+                    lua_pop(L, 1); /* nil */
+                    lua_newtable(L);
+                    lua_pushlightuserdata(L, (void *) l);
+                    lua_pushvalue(L, -2);
+                    lua_rawset(L, LUA_ENVIRONINDEX);
+                    for (li = l->lv_first; li != NULL; li = li->li_next) {
+                        luaV_pushtypval(L, &li->li_tv);
+                        lua_rawseti(L, -2, ++n);
+                    }
+                }
+            }
+            else lua_pushnil(L);
+            break;
+        }
+        case VAR_DICT: {
+            dict_T *d = tv->vval.v_dict;
+            if (d != NULL) {
+                /* check cache */
+                lua_pushlightuserdata(L, (void *) d);
+                lua_rawget(L, LUA_ENVIRONINDEX);
+                if (lua_isnil(L, -1)) { /* not interned? */
+                    hashtab_T *ht = &d->dv_hashtab;
+                    hashitem_T *hi;
+                    int n = ht->ht_used; /* remaining items */
+                    lua_pop(L, 1); /* nil */
+                    lua_newtable(L);
+                    lua_pushlightuserdata(L, (void *) d);
+                    lua_pushvalue(L, -2);
+                    lua_rawset(L, LUA_ENVIRONINDEX);
+                    for (hi = ht->ht_array; n > 0; hi++) {
+                        if (!HASHITEM_EMPTY(hi)) {
+                            dictitem_T *di = dict_lookup(hi);
+                            luaV_pushtypval(L, &di->di_tv);
+                            lua_setfield(L, -2, (char *) hi->hi_key);
+                            n--;
+                        }
+                    }
+                }
+            }
+            else lua_pushnil(L);
+            break;
+        }
+        default:
+            luaL_error(L, "invalid type");
+    }
+}
+
+/* similar to luaL_addlstring, but replaces \0 with \n if toline and
+ * \n with \0 otherwise */
+static void luaV_addlstring (luaL_Buffer *b, const char *s, size_t l,
+        int toline) {
+    while (l--) {
+        if (*s == '\0' && toline) luaL_addchar(b, '\n');
+        else if (*s == '\n' && !toline) luaL_addchar(b, '\0');
+        else luaL_addchar(b, *s);
+        s++;
+    }
+}
+
+static void luaV_pushline (lua_State *L, buf_T *buf, linenr_T n) {
+    const char *s = (const char *) ml_get_buf(buf, n, FALSE);
+    luaL_Buffer b;
+    luaL_buffinit(L, &b);
+    luaV_addlstring(&b, s, strlen(s), 0);
+    luaL_pushresult(&b);
+}
+
+static char_u *luaV_toline (lua_State *L, int pos) {
+    size_t l;
+    const char *s = lua_tolstring(L, pos, &l);
+    luaL_Buffer b;
+    luaL_buffinit(L, &b);
+    luaV_addlstring(&b, s, l, 1);
+    luaL_pushresult(&b);
+    return (char_u *) lua_tostring(L, -1);
+}
+
+/* pops a string s from the top of the stack and calls mf(t) for pieces t of
+ * s separated by newlines */
+static void luaV_msgfunc (lua_State *L, msgfunc_T mf) {
+    luaL_Buffer b;
+    size_t l;
+    const char *p, *s = lua_tolstring(L, -1, &l);
+    luaL_buffinit(L, &b);
+    luaV_addlstring(&b, s, l, 0);
+    luaL_pushresult(&b);
+    /* break string */
+    p = s = lua_tolstring(L, -1, &l);
+    while (l--) {
+        if (*p++ == '\0') { /* break? */
+            mf((char_u *) s);
+            s = p;
+        }
+    }
+    mf((char_u *) s);
+    lua_pop(L, 2); /* original and modified strings */
+}
+
+
+/* =======   Buffer type   ======= */
+
+static luaV_Buffer *luaV_newbuffer (lua_State *L, buf_T *buf) {
+    luaV_Buffer *b = (luaV_Buffer *) lua_newuserdata(L, sizeof(luaV_Buffer));
+    *b = buf;
+    lua_pushlightuserdata(L, (void *) buf);
+    lua_pushvalue(L, -2);
+    lua_rawset(L, LUA_ENVIRONINDEX); /* env[buf] = udata */
+    /* to avoid GC, store as key in env */
+    lua_pushvalue(L, -1);
+    lua_pushboolean(L, 1);
+    lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
+    /* set metatable */
+    luaV_getfield(L, LUAVIM_BUFFER);
+    lua_setmetatable(L, -2);
+    return b;
+}
+
+static luaV_Buffer *luaV_pushbuffer (lua_State *L, buf_T *buf) {
+    luaV_Buffer *b = NULL;
+    if (buf == NULL)
+        lua_pushnil(L);
+    else {
+        lua_pushlightuserdata(L, (void *) buf);
+        lua_rawget(L, LUA_ENVIRONINDEX);
+        if (lua_isnil(L, -1)) { /* not interned? */
+            lua_pop(L, 1);
+            b = luaV_newbuffer(L, buf);
+        }
+        else b = (luaV_Buffer *) lua_touserdata(L, -1);
+    }
+    return b;
+}
+
+/* Buffer metamethods */
+
+static int luaV_buffer_tostring (lua_State *L) {
+    lua_pushfstring(L, "%s: %p", LUAVIM_BUFFER, lua_touserdata(L, 1));
+    return 1;
+}
+
+static int luaV_buffer_len (lua_State *L) {
+    luaV_Buffer *b = lua_touserdata(L, 1);
+    lua_pushinteger(L, (*b)->b_ml.ml_line_count);
+    return 1;
+}
+
+static int luaV_buffer_call (lua_State *L) {
+    luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
+    lua_settop(L, 1);
+    set_curbuf(*b, DOBUF_SPLIT);
+    return 1;
+}
+
+static int luaV_buffer_index (lua_State *L) {
+    luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
+    linenr_T n = (linenr_T) lua_tointeger(L, 2);
+    if (n > 0 && n <= (*b)->b_ml.ml_line_count)
+        luaV_pushline(L, *b, n);
+    else if (lua_isstring(L, 2)) {
+        const char *s = lua_tostring(L, 2);
+        if (strncmp(s, "name", 4) == 0)
+            lua_pushstring(L, (char *) (*b)->b_sfname);
+        else if (strncmp(s, "fname", 5) == 0)
+            lua_pushstring(L, (char *) (*b)->b_ffname);
+        else if (strncmp(s, "number", 6) == 0)
+            lua_pushinteger(L, (*b)->b_fnum);
+        /* methods */
+        else if (strncmp(s,   "insert", 6) == 0
+                || strncmp(s, "next", 4) == 0
+                || strncmp(s, "previous", 8) == 0
+                || strncmp(s, "isvalid", 7) == 0) {
+            lua_getmetatable(L, 1);
+            lua_getfield(L, -1, s);
+        }
+        else lua_pushnil(L);
+    }
+    else lua_pushnil(L);
+    return 1;
+}
+
+static int luaV_buffer_newindex (lua_State *L) {
+    luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
+    linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
+#ifdef HAVE_SANDBOX
+    luaV_checksandbox(L);
+#endif
+    if (n < 1 || n > (*b)->b_ml.ml_line_count)
+        luaL_error(L, "invalid line number");
+    if (lua_isnil(L, 3)) { /* delete line */
+        buf_T *buf = curbuf;
+        curbuf = *b;
+        if (u_savedel(n, 1L) == FAIL) {
+            curbuf = buf;
+            luaL_error(L, "cannot save undo information");
+        }
+        else if (ml_delete(n, FALSE) == FAIL) {
+            curbuf = buf;
+            luaL_error(L, "cannot delete line");
+        }
+        else {
+            deleted_lines_mark(n, 1L);
+            if (*b == curwin->w_buffer) { /* fix cursor in current window? */
+                if (curwin->w_cursor.lnum >= n) {
+                    if (curwin->w_cursor.lnum > n) {
+                        curwin->w_cursor.lnum -= 1;
+                        check_cursor_col();
+                    }
+                    else check_cursor();
+                    changed_cline_bef_curs();
+                }
+                invalidate_botline();
+            }
+        }
+        curbuf = buf;
+    }
+    else if (lua_isstring(L, 3)) { /* update line */
+        buf_T *buf = curbuf;
+        curbuf = *b;
+        if (u_savesub(n) == FAIL) {
+            curbuf = buf;
+            luaL_error(L, "cannot save undo information");
+        }
+        else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL) {
+            curbuf = buf;
+            luaL_error(L, "cannot replace line");
+        }
+        else changed_bytes(n, 0);
+        curbuf = buf;
+        if (*b == curwin->w_buffer)
+            check_cursor_col();
+    }
+    else
+        luaL_error(L, "wrong argument to change line");
+    return 0;
+}
+
+static int luaV_buffer_insert (lua_State *L) {
+    luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
+    linenr_T last = (*b)->b_ml.ml_line_count;
+    linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
+    buf_T *buf;
+    luaL_checktype(L, 2, LUA_TSTRING);
+#ifdef HAVE_SANDBOX
+    luaV_checksandbox(L);
+#endif
+    /* fix insertion line */
+    if (n < 0) n = 0;
+    if (n > last) n = last;
+    /* insert */
+    buf = curbuf;
+    curbuf = *b;
+    if (u_save(n, n + 1) == FAIL) {
+        curbuf = buf;
+        luaL_error(L, "cannot save undo information");
+    }
+    else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL) {
+        curbuf = buf;
+        luaL_error(L, "cannot insert line");
+    }
+    else
+        appended_lines_mark(n, 1L);
+    curbuf = buf;
+    update_screen(VALID);
+    return 0;
+}
+
+static int luaV_buffer_next (lua_State *L) {
+    luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
+    luaV_pushbuffer(L, (*b)->b_next);
+    return 1;
+}
+
+static int luaV_buffer_previous (lua_State *L) {
+    luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
+    luaV_pushbuffer(L, (*b)->b_prev);
+    return 1;
+}
+
+static int luaV_buffer_isvalid (lua_State *L) {
+    luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
+    lua_pushlightuserdata(L, (void *) (*b));
+    lua_rawget(L, LUA_ENVIRONINDEX);
+    lua_pushboolean(L, !lua_isnil(L, -1));
+    return 1;
+}
+
+static const luaL_Reg luaV_Buffer_mt[] = {
+    {"__tostring", luaV_buffer_tostring},
+    {"__len", luaV_buffer_len},
+    {"__call", luaV_buffer_call},
+    {"__index", luaV_buffer_index},
+    {"__newindex", luaV_buffer_newindex},
+    {"insert", luaV_buffer_insert},
+    {"next", luaV_buffer_next},
+    {"previous", luaV_buffer_previous},
+    {"isvalid", luaV_buffer_isvalid},
+    {NULL, NULL}
+};
+
+
+/* =======   Window type   ======= */
+
+static luaV_Window *luaV_newwindow (lua_State *L, win_T *win) {
+    luaV_Window *w = (luaV_Window *) lua_newuserdata(L, sizeof(luaV_Window));
+    *w = win;
+    lua_pushlightuserdata(L, (void *) win);
+    lua_pushvalue(L, -2);
+    lua_rawset(L, LUA_ENVIRONINDEX); /* env[win] = udata */
+    /* to avoid GC, store as key in env */
+    lua_pushvalue(L, -1);
+    lua_pushboolean(L, 1);
+    lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
+    /* set metatable */
+    luaV_getfield(L, LUAVIM_WINDOW);
+    lua_setmetatable(L, -2);
+    return w;
+}
+
+static luaV_Window *luaV_pushwindow (lua_State *L, win_T *win) {
+    luaV_Window *w = NULL;
+    if (win == NULL)
+        lua_pushnil(L);
+    else {
+        lua_pushlightuserdata(L, (void *) win);
+        lua_rawget(L, LUA_ENVIRONINDEX);
+        if (lua_isnil(L, -1)) { /* not interned? */
+            lua_pop(L, 1);
+            w = luaV_newwindow(L, win);
+        }
+        else w = (luaV_Window *) lua_touserdata(L, -1);
+    }
+    return w;
+}
+
+/* Window metamethods */
+
+static int luaV_window_tostring (lua_State *L) {
+    lua_pushfstring(L, "%s: %p", LUAVIM_WINDOW, lua_touserdata(L, 1));
+    return 1;
+}
+
+static int luaV_window_call (lua_State *L) {
+    luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
+    lua_settop(L, 1);
+    win_goto(*w);
+    return 1;
+}
+
+static int luaV_window_index (lua_State *L) {
+    luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
+    const char *s = luaL_checkstring(L, 2);
+    if (strncmp(s, "buffer", 6) == 0)
+        luaV_pushbuffer(L, (*w)->w_buffer);
+    else if (strncmp(s, "line", 4) == 0)
+        lua_pushinteger(L, (*w)->w_cursor.lnum);
+    else if (strncmp(s, "col", 3) == 0)
+        lua_pushinteger(L, (*w)->w_cursor.col + 1);
+#ifdef FEAT_VERTSPLIT
+    else if (strncmp(s, "width", 5) == 0)
+        lua_pushinteger(L, W_WIDTH((*w)));
+#endif
+    else if (strncmp(s, "height", 6) == 0)
+        lua_pushinteger(L, (*w)->w_height);
+    /* methods */
+    else if (strncmp(s,   "next", 4) == 0
+            || strncmp(s, "previous", 8) == 0
+            || strncmp(s, "isvalid", 7) == 0) {
+        lua_getmetatable(L, 1);
+        lua_getfield(L, -1, s);
+    }
+    else
+        lua_pushnil(L);
+    return 1;
+}
+
+static int luaV_window_newindex (lua_State *L) {
+    luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
+    const char *s = luaL_checkstring(L, 2);
+    int v = luaL_checkinteger(L, 3);
+    if (strncmp(s, "line", 4) == 0) {
+#ifdef HAVE_SANDBOX
+        luaV_checksandbox(L);
+#endif
+        if (v < 1 || v > (*w)->w_buffer->b_ml.ml_line_count)
+            luaL_error(L, "line out of range");
+        (*w)->w_cursor.lnum = v;
+        update_screen(VALID);
+    }
+    else if (strncmp(s, "col", 3) == 0) {
+#ifdef HAVE_SANDBOX
+        luaV_checksandbox(L);
+#endif
+        (*w)->w_cursor.col = v - 1;
+        update_screen(VALID);
+    }
+#ifdef FEAT_VERTSPLIT
+    else if (strncmp(s, "width", 5) == 0) {
+        win_T *win = curwin;
+#ifdef FEAT_GUI
+        need_mouse_correct = TRUE;
+#endif
+        curwin = *w;
+        win_setwidth(v);
+        curwin = win;
+    }
+#endif
+    else if (strncmp(s, "height", 6) == 0) {
+        win_T *win = curwin;
+#ifdef FEAT_GUI
+        need_mouse_correct = TRUE;
+#endif
+        curwin = *w;
+        win_setheight(v);
+        curwin = win;
+    }
+    else
+        luaL_error(L, "invalid window property: `%s'", s);
+    return 0;
+}
+
+static int luaV_window_next (lua_State *L) {
+    luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
+    luaV_pushwindow(L, (*w)->w_next);
+    return 1;
+}
+
+static int luaV_window_previous (lua_State *L) {
+    luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
+    luaV_pushwindow(L, (*w)->w_prev);
+    return 1;
+}
+
+static int luaV_window_isvalid (lua_State *L) {
+    luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
+    lua_pushlightuserdata(L, (void *) (*w));
+    lua_rawget(L, LUA_ENVIRONINDEX);
+    lua_pushboolean(L, !lua_isnil(L, -1));
+    return 1;
+}
+
+static const luaL_Reg luaV_Window_mt[] = {
+    {"__tostring", luaV_window_tostring},
+    {"__call", luaV_window_call},
+    {"__index", luaV_window_index},
+    {"__newindex", luaV_window_newindex},
+    {"next", luaV_window_next},
+    {"previous", luaV_window_previous},
+    {"isvalid", luaV_window_isvalid},
+    {NULL, NULL}
+};
+
+
+/* =======   Vim module   ======= */
+
+static int luaV_print (lua_State *L) {
+    int i, n = lua_gettop(L); /* nargs */
+    const char *s;
+    size_t l;
+    luaL_Buffer b;
+    luaL_buffinit(L, &b);
+    lua_getglobal(L, "tostring");
+    for (i = 1; i <= n; i++) {
+        lua_pushvalue(L, -1); /* tostring */
+        lua_pushvalue(L, i); /* arg */
+        lua_call(L, 1, 1);
+        s = lua_tolstring(L, -1, &l);
+        if (s == NULL)
+            return luaL_error(L, "cannot convert to string");
+        if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
+        luaV_addlstring(&b, s, l, 0);
+        lua_pop(L, 1);
+    }
+    luaL_pushresult(&b);
+    luaV_msg(L);
+    return 0;
+}
+
+static int luaV_command (lua_State *L) {
+    do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
+    update_screen(VALID);
+    return 0;
+}
+
+static int luaV_eval (lua_State *L) {
+    typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
+    if (tv == NULL) luaL_error(L, "invalid expression");
+    luaV_pushtypval(L, tv);
+    return 1;
+}
+
+static int luaV_beep (lua_State *L) {
+    vim_beep();
+    return 0;
+}
+
+static int luaV_line (lua_State *L) {
+    luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
+    return 1;
+}
+
+static int luaV_buffer (lua_State *L) {
+    buf_T *buf;
+    if (lua_isstring(L, 1)) { /* get by number or name? */
+        if (lua_isnumber(L, 1)) { /* by number? */
+            int n = lua_tointeger(L, 1);
+            for (buf = firstbuf; buf != NULL; buf = buf->b_next)
+                if (buf->b_fnum == n) break;
+        }
+        else { /* by name */
+            size_t l;
+            const char *s = lua_tolstring(L, 1, &l);
+            for (buf = firstbuf; buf != NULL; buf = buf->b_next) {
+                if (buf->b_ffname == NULL || buf->b_sfname == NULL) {
+                    if (l == 0) break;
+                }
+                else if (strncmp(s, buf->b_ffname, l) == 0
+                        || strncmp(s, buf->b_sfname, l) == 0)
+                    break;
+            }
+        }
+        if (buf == NULL) /* not found? */
+            lua_pushnil(L);
+        else
+            luaV_pushbuffer(L, buf);
+    }
+    else {
+        buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
+        luaV_pushbuffer(L, buf);
+    }
+    return 1;
+}
+
+static int luaV_window (lua_State *L) {
+    win_T *win;
+    if (lua_isnumber(L, 1)) { /* get by number? */
+        int n = lua_tointeger(L, 1);
+        for (win = firstwin; win != NULL; win = win->w_next, n--)
+            if (n == 1) break;
+        if (win == NULL) /* not found? */
+            lua_pushnil(L);
+        else
+            luaV_pushwindow(L, win);
+    }
+    else {
+        win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
+        luaV_pushwindow(L, win);
+    }
+    return 1;
+}
+
+static int luaV_open (lua_State *L) {
+    luaV_Buffer *b;
+    char_u *s = NULL;
+#ifdef HAVE_SANDBOX
+    luaV_checksandbox(L);
+#endif
+    if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
+    b = luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
+    return 1;
+}
+
+static int luaV_isbuffer (lua_State *L) {
+    lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_BUFFER) != NULL);
+    return 1;
+}
+
+static int luaV_iswindow (lua_State *L) {
+    lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_WINDOW) != NULL);
+    return 1;
+}
+
+/* for freeing buffer and window objects; lightuserdata as arg */
+static luaV_free (lua_State *L) {
+    lua_pushvalue(L, 1); /* lightudata */
+    lua_rawget(L, LUA_ENVIRONINDEX);
+    if (!lua_isnil(L, -1)) {
+        lua_pushnil(L);
+        lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = nil */
+        lua_pushnil(L);
+        lua_rawset(L, LUA_ENVIRONINDEX); /* env[lightudata] = nil */
+    }
+    return 0;
+}
+
+static const luaL_Reg luaV_module[] = {
+    {"command", luaV_command},
+    {"eval", luaV_eval},
+    {"beep", luaV_beep},
+    {"line", luaV_line},
+    {"buffer", luaV_buffer},
+    {"window", luaV_window},
+    {"open", luaV_open},
+    {"isbuffer", luaV_isbuffer},
+    {"iswindow", luaV_iswindow},
+    {NULL, NULL}
+};
+
+static int luaopen_vim (lua_State *L) {
+    /* set environment */
+    lua_newtable(L);
+    lua_newtable(L);
+    lua_pushliteral(L, "v");
+    lua_setfield(L, -2, "__mode");
+    lua_setmetatable(L, -2);
+    lua_replace(L, LUA_ENVIRONINDEX);
+    /* print */
+    lua_pushcfunction(L, luaV_print);
+    lua_setglobal(L, "print");
+    /* free */
+    lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
+    lua_pushcfunction(L, luaV_free);
+    lua_rawset(L, LUA_REGISTRYINDEX);
+    /* register */
+    luaV_newmetatable(L, LUAVIM_BUFFER);
+    luaL_register(L, NULL, luaV_Buffer_mt);
+    luaV_newmetatable(L, LUAVIM_WINDOW);
+    luaL_register(L, NULL, luaV_Window_mt);
+    luaL_register(L, LUAVIM_NAME, luaV_module);
+    return 0;
+}
+
+static lua_State *luaV_newstate (void) {
+    lua_State *L = luaL_newstate();
+    const luaL_Reg luaV_core_libs[] = {
+        {"", luaopen_base},
+        {LUA_TABLIBNAME, luaopen_table},
+        {LUA_STRLIBNAME, luaopen_string},
+        {LUA_MATHLIBNAME, luaopen_math},
+        {LUA_OSLIBNAME, luaopen_os}, /* restricted */
+        {LUA_LOADLIBNAME, luaopen_package},
+        {LUA_DBLIBNAME, luaopen_debug},
+        {NULL, NULL}
+    };
+    const char *os_funcs[] = {
+        "date", "clock", "time", "difftime", "getenv", NULL
+    };
+    const luaL_Reg *reg = luaV_core_libs;
+    const char **s = os_funcs;
+    /* core libs */
+    for ( ; reg->func; reg++) {
+        lua_pushcfunction(L, reg->func);
+        lua_pushstring(L, reg->name);
+        lua_call(L, 1, 0);
+    }
+    /* restricted os lib */
+    lua_getglobal(L, LUA_OSLIBNAME);
+    lua_newtable(L);
+    for ( ; *s; s++) {
+        lua_getfield(L, -2, *s);
+        lua_setfield(L, -2, *s);
+    }
+    lua_setglobal(L, LUA_OSLIBNAME);
+    lua_pop(L, 1); /* os table */
+    /* vim */
+    lua_pushcfunction(L, luaopen_vim);
+    lua_call(L, 0, 0);
+    return L;
+}
+
+static void luaV_setrange (lua_State *L, int line1, int line2) {
+    lua_getglobal(L, LUAVIM_NAME);
+    lua_pushinteger(L, line1);
+    lua_setfield(L, -2, "firstline");
+    lua_pushinteger(L, line2);
+    lua_setfield(L, -2, "lastline");
+    lua_pop(L, 1); /* vim table */
+}
+
+
+/* =======   Interface   ======= */
+
+static lua_State *L = NULL;
+
+static int lua_init (void) {
+    if (L == NULL) {
+#ifdef DYNAMIC_LUA
+        if (!lua_enabled(TRUE)) {
+            EMSG(_("Lua library cannot be loaded."));
+            return FAIL;
+        }
+#endif
+        L = luaV_newstate();
+    }
+    return OK;
+}
+
+void lua_end (void) {
+    if (L != NULL) {
+        lua_close(L);
+        L = NULL;
+#ifdef DYNAMIC_LUA
+        end_dynamic_lua();
+#endif
+    }
+}
+
+/* ex commands */
+void ex_lua (exarg_T *eap) {
+    char *script;
+    if (lua_init() == FAIL) return;
+    script = (char *) script_get(eap, eap->arg);
+    if (!eap->skip) {
+        char *s = (script) ? script :  (char *) eap->arg;
+        luaV_setrange(L, eap->line1, eap->line2);
+        if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
+                || lua_pcall(L, 0, 0, 0))
+            luaV_emsg(L);
+    }
+    if (script != NULL) vim_free(script);
+}
+
+void ex_luado (exarg_T *eap) {
+    linenr_T l;
+    const char *s = (const char *) eap->arg;
+    luaL_Buffer b;
+    size_t len;
+    if (lua_init() == FAIL) return;
+    if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) {
+        EMSG(_("cannot save undo information"));
+        return;
+    }
+    luaV_setrange(L, eap->line1, eap->line2);
+    luaL_buffinit(L, &b);
+    luaL_addlstring(&b, "return function(line) ", 22); /* header */
+    luaL_addlstring(&b, s, strlen(s));
+    luaL_addlstring(&b, " end", 4); /* footer */
+    luaL_pushresult(&b);
+    s = lua_tolstring(L, -1, &len);
+    if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME)) {
+        luaV_emsg(L);
+        lua_pop(L, 1); /* function body */
+        return;
+    }
+    lua_call(L, 0, 1);
+    lua_replace(L, -2); /* function -> body */
+    for (l = eap->line1; l <= eap->line2; l++) {
+        lua_pushvalue(L, -1); /* function */
+        luaV_pushline(L, curbuf, l); /* current line as arg */
+        if (lua_pcall(L, 1, 1, 0)) {
+            luaV_emsg(L);
+            break;
+        }
+        if (lua_isstring(L, -1)) { /* update line? */
+#ifdef HAVE_SANDBOX
+            luaV_checksandbox(L);
+#endif
+            ml_replace(l, luaV_toline(L, -1), TRUE);
+            changed_bytes(l, 0);
+            lua_pop(L, 1); /* result from luaV_toline */
+        }
+        lua_pop(L, 1); /* line */
+    }
+    lua_pop(L, 1); /* function */
+    check_cursor();
+    update_screen(NOT_VALID);
+}
+
+void ex_luafile (exarg_T *eap) {
+    if (lua_init() == FAIL) return;
+    if (!eap->skip) {
+        luaV_setrange(L, eap->line1, eap->line2);
+        if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
+            luaV_emsg(L);
+    }
+}
+
+/* buffer */
+void lua_buffer_free (buf_T *buf) {
+    if (lua_init() == FAIL) return;
+    luaV_getfield(L, LUAVIM_FREE);
+    lua_pushlightuserdata(L, (void *) buf);
+    lua_call(L, 1, 0);
+}
+
+/* window */
+void lua_window_free (win_T *win) {
+    if (lua_init() == FAIL) return;
+    luaV_getfield(L, LUAVIM_FREE);
+    lua_pushlightuserdata(L, (void *) win);
+    lua_call(L, 1, 0);
+}
+
+#endif
--- a/src/main.aap
+++ b/src/main.aap
@@ -243,13 +243,13 @@ EVIEWTARGET     = $EVIEWNAME$LNKSUF
 
 
 PRE_DEFS = -Iproto -I. $DEFS $GUI_DEFS $GUI_IPATH $CPPFLAGS $?(EXTRA_IPATHS)
-POST_DEFS = $X_CFLAGS $MZSCHEME_CFLAGS $PERL_CFLAGS $PYTHON_CFLAGS $TCL_CFLAGS $RUBY_CFLAGS $?(EXTRA_DEFS)
+POST_DEFS = $X_CFLAGS $LUA_CFLAGS $MZSCHEME_CFLAGS $PERL_CFLAGS $PYTHON_CFLAGS $TCL_CFLAGS $RUBY_CFLAGS $?(EXTRA_DEFS)
 CFLAGS = $PRE_DEFS $CONF_CFLAGS $?(PROFILE_CFLAGS) $POST_DEFS
 CPPFLAGS =
 
 ALL_LIB_DIRS = $GUI_LIBS_DIR $X_LIBS_DIR
 LDFLAGS = $ALL_LIB_DIRS $CONF_LDFLAGS
-LIBS = $GUI_LIBS1 $GUI_X_LIBS $GUI_LIBS2 $X_PRE_LIBS $X_LIBS $X_EXTRA_LIBS $CONF_LIBS $?(EXTRA_LIBS) $MZSCHEME_LIBS $PERL_LIBS $PYTHON_LIBS $TCL_LIBS $RUBY_LIBS $?(PROFILE_LIBS)
+LIBS = $GUI_LIBS1 $GUI_X_LIBS $GUI_LIBS2 $X_PRE_LIBS $X_LIBS $X_EXTRA_LIBS $CONF_LIBS $?(EXTRA_LIBS) $LUA_LIBS $MZSCHEME_LIBS $PERL_LIBS $PYTHON_LIBS $TCL_LIBS $RUBY_LIBS $?(PROFILE_LIBS)
 
 Target = $VIMNAME
 
@@ -340,6 +340,7 @@ Source =
         $OS_EXTRA_SRC
         $GUI_SRC
         $HANGULIN_SRC
+        $LUA_SRC
         $MZSCHEME_SRC
         $PERL_SRC
         $NETBEANS_SRC
--- a/src/main.c
+++ b/src/main.c
@@ -1390,6 +1390,9 @@ getout(exitval)
 	windgoto((int)Rows - 1, 0);
 #endif
 
+#ifdef FEAT_LUA
+    lua_end();
+#endif
 #ifdef FEAT_MZSCHEME
     mzscheme_end();
 #endif
--- a/src/proto.h
+++ b/src/proto.h
@@ -174,6 +174,10 @@ void qsort __ARGS((void *base, size_t el
 # include "version.pro"
 # include "window.pro"
 
+# ifdef FEAT_LUA
+#  include "if_lua.pro"
+# endif
+
 # ifdef FEAT_MZSCHEME
 #  include "if_mzsch.pro"
 # endif
new file mode 100644
--- /dev/null
+++ b/src/proto/if_lua.pro
@@ -0,0 +1,8 @@
+/* if_lua.c */
+void lua_end __ARGS((void));
+void ex_lua __ARGS((exarg_T *eap));
+void ex_luado __ARGS((exarg_T *eap));
+void ex_luafile __ARGS((exarg_T *eap));
+void lua_buffer_free __ARGS((buf_T *buf));
+void lua_window_free __ARGS((win_T *win));
+/* vim: set ft=c : */
--- a/src/version.c
+++ b/src/version.c
@@ -314,6 +314,15 @@ static char *(features[]) =
 #else
 	"-localmap",
 #endif
+#ifdef FEAT_LUA
+# ifdef DYNAMIC_LUA
+	"+lua/dyn",
+# else
+	"+lua",
+# endif
+#else
+	"-lua",
+#endif
 #ifdef FEAT_MENU
 	"+menu",
 #else
--- a/src/window.c
+++ b/src/window.c
@@ -4370,6 +4370,10 @@ win_free(wp, tp)
     block_autocmds();
 #endif
 
+#ifdef FEAT_LUA
+    lua_window_free(wp);
+#endif
+
 #ifdef FEAT_MZSCHEME
     mzscheme_window_free(wp);
 #endif