(Re)generate: (find-lua-tutorial-intro)
Source code: (find-efunction 'find-lua-tutorial-intro)
More intros: (find-eev-quick-intro)
(find-eev-intro)
(find-eepitch-intro)
This buffer is _temporary_ and _editable_.
It is meant as both a tutorial and a sandbox.
This intro is a work in progress -
that is in a very early stage!!!
At this moment it only covers the material
that is in this video:
Page: http://anggtwu.net/find-luaso-links.html
Play: (find-2024luasovideo "00:00")
LSubs: (find-2024luasolsubs "00:00")
Info: (find-1stclassvideodef "2024luaso")
Note that this intro
(find-show2-intro)
explains how to install Lua and how to test the programs that I
presented in the EmacsConf2023, but it doesn't say where someone
who would like to learn Lua can learn the basics...
1. Installation
Run the instructions in this section:
(find-show2-intro "2. Dependencies")
Here are the instructions - in video - for how to run them:
(find-eev2023replsbvideo "05:23" "Then, to try the demo people have to")
(find-eev2023replsblsubs "05:23" "Then, to try the demo people have to")
and copy the `code-brappend's to your ~/.emacs (without the red stars).
2. LUA_INIT
Run this
(find-luainit-links "/tmp/")
to download my init file for Lua in /tmp/ and test it there. Then
run this
(find-luainit-links "~/LUA/")
to download it in ~/LUA/ and test it there.
Then copy these three lines to your ~/.emacs:
;; See: (find-lua-tutorial-intro "2. LUA_INIT")
;; (find-fline "~/LUA/lua50init.lua")
(setenv "LUA_INIT" (concat "@" (ee-expand "~/LUA/lua50init.lua")))
3. The C API
This is an example of how to define Lua functions in C:
(find-angg "CLUA/dummy2.c")
Note that its test block looks like this:
/*
* (eepitch-shell)
* (eepitch-kill)
* (eepitch-shell)
gcc -g -Wall -shared -I/usr/include/lua5.1 -o dummy2.so dummy2.c
ls -lAF dummy2*
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
Path.prependtocpath "~/CLUA/?.so"
require "dummy2"
print(foo(42))
*/
Its first part compiles the .c to a .so, and the second part:
1) runs Path.prependtocpath to add "~/CLUA/?.so" to the path.
This needs the class Path, that is defined in my init file -
so this will only work if you configured LUA_INIT correctly
using the instructions in the previous section,
2) loads "~/CLUA/dummy2.so",
3) calls `print(foo(42))' - where `foo' is a Lua function
defined in C in dummy2.{c,so}.
Note also that the header of dummy2.c starts with these lines:
// This file:
// http://anggtwu.net/CLUA/dummy2.c.html
// http://anggtwu.net/CLUA/dummy2.c
// (find-angg "CLUA/dummy2.c")
// Skel: (find-luaso-links "~/CLUA/dummy2.c" "foo")
// Author: Eduardo Ochs <eduardoochs@gmail.com>
//
// (defun e () (interactive) (find-angg "CLUA/dummy2.c"))
I will refer to those files as "angg-isms" - they only make
sense for C files in http://anggtwu.net/.
This file
(find-angg "CLUA/dummy2.c")
was generated by this call to a template-based function:
(find-luaso-links "~/CLUA/dummy2.c" "foo")
EXERCISE: run this,
(find-luaso-links "/tmp/dummy2.c" "foo")
and ignore - or delete - all the lines in the temporary buffer
that look like angg-isms; use the `ee-copy-rest' in the temporary
buffer to create a file /tmp/dummy2.c with the correct contents;
and run its test block. If everything goes right then the test
block will generate a file /tmp/dummy2.so, load it from Lua, and
test its function `foo'.
4. CLua1.lua
This file
http://anggtwu.net/LUA/CLua1.lua.html
http://anggtwu.net/LUA/CLua1.lua
(find-angg "LUA/CLua1.lua")
implements a way to do something similar to the elisp function
`find-luaso-links' of the last section, but using Lua to generate
all strings from templates. Here's how to test it:
** Download it into /tmp/CLua1/:
* (eepitch-shell)
* (eepitch-kill)
* (eepitch-shell)
rm -Rfv /tmp/CLua1/
mkdir /tmp/CLua1/
cd /tmp/CLua1/
wget http://anggtwu.net/LUA/lua50init.lua
wget http://anggtwu.net/LUA/Dang1.lua
wget http://anggtwu.net/LUA/CLua1.lua
** Make `find-clua' and LUA_{INIT,PATH} point to /tmp/CLua1/:
* (code-c-d "clua" "/tmp/CLua1/" :anchor)
* (setenv "LUA_INIT" "@/tmp/CLua1/lua50init.lua")
* (setenv "LUA_PATH" "/tmp/CLua1/?.lua;;")
** Now run these test blocks:
* (find-clua "CLua1.lua" "CLua-tests")
* (find-clua "CLua1.lua" "buildandload-tests")
5. CLua1.lua from the outside
In the previous section you ran the tests in two test blocks that
were _inside_ CLua1.lua; it is also possible to run the functions
in CLua1.lua "from the outside". Try this:
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
require "CLua1" -- (find-clua "CLua1.lua")
-- Choose one:
CLua.__index.compile = CLua.__index.mac
CLua.__index.compile = CLua.__index.debian
CLua.rm()
buildandload('minusdiv', [=[
lua_pushnumber(L, lua_tonumber(L, 1) - lua_tonumber(L, 2));
lua_pushnumber(L, lua_tonumber(L, 1) / lua_tonumber(L, 2));
return 2;
]=])
print(minusdiv(20, 2)) --> 18 10
print(minusdiv(20, 2, 42, 99, 300, 3)) --> 18 10
buildandload('minusdiv', [=[
lua_pushnumber(L, lua_tonumber(L, -2) - lua_tonumber(L, -1));
lua_pushnumber(L, lua_tonumber(L, -3) / lua_tonumber(L, -2));
return 2;
]=])
print(minusdiv(20, 2)) --> 18 10
print(minusdiv(20, 2, 42, 99, 300, 3)) --> 297 100
6. TODO
I have an old eev-based tutorial for Lua here:
(find-es "lua-intro" "how-to-use")
(find-es "lua-intro" "intro:types")
I need to clean it up and explain how to use it...