Skip to content

Commit 6033cf1

Browse files
committed
💥 Support \directlua{}
1 parent a8abf9e commit 6033cf1

File tree

5 files changed

+100
-38
lines changed

5 files changed

+100
-38
lines changed

README.md

+29-7
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ Refer [luatex](#luatex).
102102
luatex use `kpse` to search lua modules and binary lua modules.
103103

104104
```sh
105-
kpsewhich -progname=luatex -var-value=LUAINPUTS
106-
kpsewhich -progname=luatex -var-value=CLUAINPUTS
107-
kpsewhich -progname=luajittex -var-value=LUAINPUTS
108-
kpsewhich -progname=luajittex -var-value=CLUAINPUTS
105+
$ kpsewhich -progname=luatex -format=clua prompt
106+
/run/current-system/sw/lib/lua/5.1/prompt.so
107+
$ kpsewhich -progname=luatex -format=lua prompt/utils
108+
/home/wzy/.local/share/lua/5.1/prompt/utils.lua
109109
```
110110

111111
You can edit web2c config to modify its default values. Such as, default config
@@ -130,6 +130,31 @@ export LUAINPUTS="$HOME/.local/share/lua/5.3"
130130
export CLUAINPUTS="$HOME/.local/lib/lua/5.3"
131131
```
132132

133+
Check your changes:
134+
135+
```sh
136+
kpsewhich -progname=luatex -var-value=LUAINPUTS
137+
kpsewhich -progname=luatex -var-value=CLUAINPUTS
138+
kpsewhich -progname=luajittex -var-value=LUAINPUTS
139+
kpsewhich -progname=luajittex -var-value=CLUAINPUTS
140+
```
141+
142+
If you want to use lua REPL in `\directlua{}`, you can insert:
143+
144+
```tex
145+
\directlua{require'prompt.tex'}
146+
```
147+
148+
As [example](examples/main.tex):
149+
150+
```sh
151+
luajittex --lua=/the/path/of/prompt/tex.lua main.tex
152+
```
153+
154+
Then you will get:
155+
156+
![directlua](https://github.com/user-attachments/assets/45279ebe-76aa-4ff4-826e-03c00e9e79a8)
157+
133158
#### [neomutt](https://neomutt.org)
134159

135160
Broken in [upstream](https://github.com/neomutt/neomutt/issues/4328)
@@ -173,6 +198,3 @@ luarocks install prompt-style
173198
- `xmake l /the/path/xmake.lua`
174199
- doesn't support shebang
175200
- xmake doesn't provide `require()`
176-
- luatex's `\directlua{}`:
177-
- weird. Even if `kpse.lookup('prompt.so')` return correct path,
178-
`require'prompt'` still throw `module 'prompt' not found`.

examples/main.tex

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
\directlua{require'prompt.tex'}
2+
\bye

lua/prompt/style.lua

+36-30
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ local function wakatime(cmd)
1515
"--entity-type=app --entity=lua --alternate-language=lua --project=%s &")
1616
local s, _ = cmd:find("%s")
1717
if s ~= nil then
18-
local project = io.popen("git rev-parse --show-toplevel 2> /dev/null"):read()
18+
local f = io.popen("git rev-parse --show-toplevel 2> /dev/null")
19+
-- specific command execution disabled
20+
if f == nil then
21+
return ""
22+
end
23+
local project = f:read()
24+
f:close()
1925
if project == nil then
2026
project = lfs.currentdir()
2127
end
@@ -48,63 +54,63 @@ local function get_distribution()
4854
end
4955
f:close()
5056
if result:match("arch") then
51-
return "arch"
57+
return "arch"
5258
elseif result:match("debian") then
53-
return "debian"
59+
return "debian"
5460
elseif result:match("raspbian") then
55-
return "raspbian"
61+
return "raspbian"
5662
elseif result:match("ubuntu") then
57-
return "ubuntu"
63+
return "ubuntu"
5864
elseif result:match("elementary") then
59-
return "elementary"
65+
return "elementary"
6066
elseif result:match("fedora") then
61-
return "fedora"
67+
return "fedora"
6268
elseif result:match("coreos") then
63-
return "coreos"
69+
return "coreos"
6470
elseif result:match("kali") then
65-
return "kali"
71+
return "kali"
6672
elseif result:match("gentoo") then
67-
return "gentoo"
73+
return "gentoo"
6874
elseif result:match("mageia") then
69-
return "mageia"
75+
return "mageia"
7076
elseif result:match("centos") then
71-
return "centos"
77+
return "centos"
7278
elseif result:match("opensuse") then
73-
return "opensuse"
79+
return "opensuse"
7480
elseif result:match("tumbleweed") then
75-
return "opensuse"
81+
return "opensuse"
7682
elseif result:match("sabayon") then
77-
return "sabayon"
83+
return "sabayon"
7884
elseif result:match("slackware") then
79-
return "slackware"
85+
return "slackware"
8086
elseif result:match("linuxmint") then
81-
return "mint"
87+
return "mint"
8288
elseif result:match("alpine") then
83-
return "alpine"
89+
return "alpine"
8490
elseif result:match("aosc") then
85-
return "aosc"
91+
return "aosc"
8692
elseif result:match("nixos") then
87-
return "nixos"
93+
return "nixos"
8894
elseif result:match("devuan") then
89-
return "devuan"
95+
return "devuan"
9096
elseif result:match("manjaro") then
91-
return "manjaro"
97+
return "manjaro"
9298
elseif result:match("void") then
93-
return "void"
99+
return "void"
94100
elseif result:match("artix") then
95-
return "artix"
101+
return "artix"
96102
elseif result:match("rhel") then
97-
return "rhel"
103+
return "rhel"
98104
elseif result:match("amzn") then
99-
return "amzn"
105+
return "amzn"
100106
elseif result:match("endeavouros") then
101-
return "endeavouros"
107+
return "endeavouros"
102108
elseif result:match("rocky") then
103-
return "rocky"
109+
return "rocky"
104110
elseif result:match("guix") then
105-
return "guix"
111+
return "guix"
106112
elseif result:match("dock") then
107-
return "docker"
113+
return "docker"
108114
end
109115
end
110116
return "linux"

lua/prompt/tex.lua

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- luacheck: ignore 111 112 113
2+
---@diagnostic disable: undefined-global
3+
if not texconfig.prompt_init then
4+
-- FIXME: no format given, quitting
5+
-- kpse.set_program_name(status.list().luatex_engine)
6+
local configs = { os.getenv('HOME') .. '/.luaprc.lua',
7+
os.getenv('HOME') .. '/.config/luaprc.lua' }
8+
for _, name in ipairs(configs) do
9+
local f = io.open(name, "r")
10+
if f ~= nil then
11+
io.close(f)
12+
local chunk, message = loadfile(name)
13+
if chunk then
14+
chunk()
15+
else
16+
print(message)
17+
end
18+
break
19+
end
20+
end
21+
22+
require 'prompt'
23+
texconfig.prompt_init = true
24+
return
25+
end
26+
local prompt = require 'prompt'
27+
local utils = require "prompt.utils"
28+
29+
utils.init()
30+
print()
31+
prompt.enter()

lua/prompt/utils.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---prompt utils.
22
local argparse = require "argparse"
33
local prompt = require "prompt"
4+
local style = require "prompt.style"
45
local M = {}
56

67
---get a parser with same command line options as lua
@@ -212,7 +213,7 @@ function M.init()
212213
prompt.name = arg[0]:gsub(".*/", ""):gsub("p$", "")
213214
end
214215

215-
prompt.prompts = prompt.prompts or { '> ', '>> ' }
216+
prompt.prompts = { style.generate_ps1(), " " }
216217
if prompt.colorize == nil then
217218
prompt.colorize = true
218219
end

0 commit comments

Comments
 (0)