Skip to content

Commit bcb8bc4

Browse files
committed
feat(babel): Support the tangle-mode source block header
This header allow the creator to change the generated file permissions.
1 parent 3d13709 commit bcb8bc4

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

lua/orgmode/babel/tangle.lua

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ function Tangle:tangle()
3535

3636
for _, info in ipairs(valid_blocks) do
3737
if tangle_info[info.filename] then
38-
table.insert(tangle_info[info.filename], '')
38+
table.insert(tangle_info[info.filename]['content'], '')
3939
else
40-
tangle_info[info.filename] = {}
40+
tangle_info[info.filename] = {content = {}}
4141
end
4242

43+
local filemode = tangle_info[info.filename]['mode']
4344
local do_noweb = info.header_args[':noweb'] == 'yes' or info.header_args[':noweb'] == 'tangle'
4445
local parsed_content = info.content
4546

@@ -63,19 +64,33 @@ function Tangle:tangle()
6364
local shebang = info.header_args[':shebang']
6465
if shebang then
6566
shebang = shebang:gsub('[\'"]', '')
66-
utils.echo_info(('shebang: %s'):format(shebang))
6767
table.insert(parsed_content, 1, shebang)
68+
if filemode == nil then
69+
filemode = "o755"
70+
end
71+
end
72+
73+
local tangle_mode = info.header_args[':tangle-mode']
74+
if tangle_mode then
75+
filemode = tangle_mode:gsub('[\'"]', '')
6876
end
6977

7078
if info.name then
7179
block_content_by_name[info.name] = parsed_content
7280
end
73-
vim.list_extend(tangle_info[info.filename], parsed_content)
81+
vim.list_extend(tangle_info[info.filename]['content'], parsed_content)
82+
tangle_info[info.filename]['mode'] = filemode
7483
end
7584

7685
local promises = {}
77-
for filename, content in pairs(tangle_info) do
78-
table.insert(promises, utils.writefile(filename, table.concat(self:_remove_obsolete_indent(content), '\n')))
86+
for filename, block in pairs(tangle_info) do
87+
table.insert(promises, utils.writefile(filename, table.concat(self:_remove_obsolete_indent(block['content']), '\n')))
88+
local mode_str = block['mode']
89+
if mode_str and mode_str:sub(1, 1) == 'o' then
90+
mode_str = mode_str:sub(2)
91+
local mode_num = tonumber(mode_str, 8)
92+
vim.loop.fs_chmod(filename, mode_num)
93+
end
7994
end
8095
Promise.all(promises):wait()
8196
utils.echo_info(('Tangled %d blocks from %s'):format(#valid_blocks, vim.fn.fnamemodify(self.file.filename, ':t')))

0 commit comments

Comments
 (0)