Running a lua script as a gel script - lua

I have the following lua script that separates a string in its proper words:
names = "aa bb cc dd"
words = {}
for word in names:gmatch("%w+") do table.insert(words, word) end
for k,v in pairs(words) do print(k,v) end
I am using a program called Graphite, which is a platform for computer graphics and more. In its readme it is written:
You can write a LUA script and run it with Graphite. Just load the
script using GEL -> execute file.
Having very small experience with LUA and zero knowledge about what GEL is, I am having trouble running a script. After some googling I found this: http://gema.sourceforge.net/new/gel.shtml#3_1 ,
but it is still not so clear to me what the connection between gel and lua is. In this website it is described as: a Lua binding for Gema, a general purpose text processing utility.
The above script for example works as intended in the lua interpreter. On the other hand, when I try to execute it as a .gel script (because the software mentioned above requires a .gel script) it returns a syntax error.
Any idea on how to make it run as a .gel script? Or any other comment that might help?

If you read the documentation more closely, you'll notice:
Embed the lua functions in the gema file using the new syntax "![" "!]"
They also have an example provided:
![
function inflog(x)
if x>0 then return math.log(x)
else return "-infty"
end
end
!]
<N>=#lua{return inflog($0)}
Following the same example, the following should work:
![
function split(names)
words = {}
for word in names:gmatch("%w+") do table.insert(words, word) end
for k, v in ipairs(words) do print(k, v) end
return words
end
!]
<N>=#lua{return split($0)}

Related

PandocLuaError "all choices failed" in custom pandoc writer

I'm trying to develop a pandoc (v2.18) lua custom writer for kramdown. Kramdown uses $$ as delimiter for display and inline math and so my writer looks like:
function Writer (doc, opts)
local filter = {
Math = function(elem)
local math = elem
if elem.mathtype == 'DisplayMath' then
local delimited = '\n$$' .. elem.text ..'$$\n'
math = pandoc.RawBlock('markdown', delimited)
end
if elem.mathtype == 'InlineMath' then
local delimited = '$$' .. elem.text ..'$$'
math = pandoc.RawInline('markdown', delimited)
end
return math
end
}
return pandoc.write(doc:walk(filter), 'markdown', opts)
end
Now when trying to convert a latex test file called vector.tex this fails with the error message
$ pandoc -t kramdown.lua vector.tex -o vector.md --wrap=preserve
Error running Lua:
PandocLuaError "all choices failed"
stack traceback:
kramdown.lua:21: in function 'Writer'
I realized that it works and I get the output I want by replacing RawBlock with RawInline like
math = pandoc.RawInline('markdown', delimited .. '\n')
So there seems to be a problem with my usage of RawBlock. I am new to pandoc and lua so maybe I'm missing something basic here. Can someone give me a hint what might be the issue here?
Using RawInline works, as Math elements are inline elements. Display math may look like a block, but internally it's still an inline. Filters must replace inline elements with other inlines, and blocks with blocks.
A "Block" is something like a paragraph, list, or block quote, while an "Inline" is text, emphasis, an image, or a link.
Sorry for the abysmal error message, I'll try to improve that.

Why doesn't love2d use my custom-defined pairs metamethod?

I have the following code in the file main.lua:
local table = {data={a=1,b=2,c=3}}
setmetatable(table, table)
function table:__pairs()
return pairs(self.data)
end
function table:__tostring()
return "a table"
end
print(table)
for e in pairs(table) do
print(e)
end
When I run lua main.lua I get the output
a table
a
b
c
When I run love ~/path/to/project I get the output
a table
__tostring
data
__pairs
Why does love use other metamethods correctly, but not pairs?
I have LOVE 11.3 (Mysterious Mysteries) and Lua 5.3.5
Love2D uses LuaJIT as its default interpreter, which is fixed to Lua 5.1. And while you can rebuild Love2D for the standard Lua 5.1 interpreter, making it use modern versions of the standard Lua interpreter would require substantial code hacking, since 5.2+ aren't backwards compatible.
And Lua 5.1 doesn't have the pairs metamethod.

abstract syntax tree of Lua function

I am new to Lua and I was reading about the AST (abstract syntax tree) but didn't quite get it yet.
I wrote a simple "Hello-World" function in Lua:
function foo()
value = 10
num = "to the"
if value > 2 then
print("Hello World")
end
return value
end
print(foo())
Simple program which it's output is:
Hello World
10
I want to build an abstract syntax tree but not sure where and how to start.
I read about the syntax here, alought didn't figure out how to "design"/"draw" the wanted tree.
I'd start by studying the output of existing modules that build ASTs, as it will make it more clear what the elements of the tree are and how they correspond to the code you write. You can start with Metalua.

Lua print on the same line

In Pascal, I have write and writeln. Apparently Lua's print is similar to writeln of Pascal. Do we have something similar to write of Pascal? How can consecutive print commands send their output to the same line?
print("Hello")
print("World")
Output:
Hello
world
I want to have this:
Hello world
Use io.write instead print, which is meant for simple uses, like debugging, anyway.
Expanding on lhf's correct answer, the io library is preferred for production use.
The print function in the base library is implemented as a primitive capability. It allows for quick and dirty scripts that compute something and print an answer, with little control over its presentation. Its principle benefits are that it coerces all arguments to string and that it separates each argument in the output with tabs and supplies a newline.
Those advantages quickly become defects when detailed control of the output is required. For that, you really need to use io.write. If you mix print and io.write in the same program, you might trip over another defect. print uses the C stdout file handle explicitly. This means that if you use io.output to change the output file handle, io.write will do what you expect but print won't.
A good compromise can be to implement a replacement for print in terms of io.write. It could look as simple as this untested sample where I've tried to write clearly rather than optimally and still handle nil arguments "correctly":
local write = io.write
function print(...)
local n = select("#",...)
for i = 1,n do
local v = tostring(select(i,...))
write(v)
if i~=n then write'\t' end
end
write'\n'
end
Once you are implementing your own version of print, then it can be tempting to improve it in other ways for your application. Using something with more formatting control than offered by tostring() is one good idea. Another is considering a separator other than a tab character.
As an alternative, just build up your string then write it out with a single print
You may not always have access to the io library.
You could use variables for "Hello" and "World". Then concatenate them later. Like this:
local h = "Hello"
local w = "World"
print(h..w)
It will be display, in this case, as "HelloWorld". But that's easy to fix. Hope this helped!
Adding on to #Searous's answer, try the following.
local h = "hello"
local w = "world"
print(h.." "..w)
You can concatenate both together, just concatenate a space between both variables.
local h = "Hello"
local w = "World!"
print(h, w)

How can I "URL decode" a string in Emacs Lisp?

I've got a string like "foo%20bar" and I want "foo bar" out of it.
I know there's got to be a built-in function to decode a URL-encoded string (query string) in Emacs Lisp, but for the life of me I can't find it today, either in my lisp/ folder or with Google.
What is it called?
url-unhex-string
In my case I needed to do this interactively. The previous answers gave me the right functions to call, then it was just a matter of wrapping it a little to make them interactive:
(defun func-region (start end func)
"run a function over the region between START and END in current buffer."
(save-excursion
(let ((text (delete-and-extract-region start end)))
(insert (funcall func text)))))
(defun hex-region (start end)
"urlencode the region between START and END in current buffer."
(interactive "r")
(func-region start end #'url-hexify-string))
(defun unhex-region (start end)
"de-urlencode the region between START and END in current buffer."
(interactive "r")
(func-region start end #'url-unhex-string))
Add salt, I mean bind to keys according to taste.
Emacs is shipped with a URL library that provides a bunch of URL parsing functions—as huaiyuan and Charlie Martin already pointed out. Here is a small example that'd give you an idea how to use it:
(let ((url "http://www.google.hu/search?q=elisp+decode+url&btnG=Google+keres%E9s&meta="))
;; Return list of arguments and values
(url-parse-query-string
;; Decode hexas
(url-unhex-string
;; Retrieve argument list
(url-filename
;; Parse URL, return a struct
(url-generic-parse-url url)))))
=> (("meta" "") ("btnG" "Google+keresés") ("/search?q" "elisp+decode+url"))
I think is better to rely on it rather than Org-mode as it is its main purpose to parse a URL.
org-link-unescape does the job for very simple cases ... w3m-url-decode-string is better, but it isn't built in and the version I have locally isn't working with Emacs 23.
You can grab urlenc from MELPA and use urlenc:decode-region for a region or urlenc:decode-insert to insert your text interactively.
I think you're making it a little too hard: split-string will probably do most of what you want. For fancier stuff, have a look at the functions in url-expand.el; unfortunately, many of them don't have doc-strings, so you may have to read code.
url-generic-parse-url looks like a potential winner.

Resources