Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sheband and tangle mode header tag #939

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
47 changes: 42 additions & 5 deletions lua/orgmode/babel/tangle.lua
Original file line number Diff line number Diff line change
@@ -35,11 +35,12 @@ function Tangle:tangle()

for _, info in ipairs(valid_blocks) do
if tangle_info[info.filename] then
table.insert(tangle_info[info.filename], '')
table.insert(tangle_info[info.filename]['content'], '')
else
tangle_info[info.filename] = {}
tangle_info[info.filename] = { content = {} }
end

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

@@ -60,15 +61,51 @@ function Tangle:tangle()
vim.fn.mkdir(path, 'p')
end

local shebang = info.header_args[':shebang']
if shebang then
shebang = shebang:gsub('[\'"]', '')
table.insert(parsed_content, 1, shebang)
if filemode == nil then
filemode = 'o755'
end
end

local tangle_mode = info.header_args[':tangle-mode']
if tangle_mode then
filemode = tangle_mode:gsub('[\'"]', '')
end

if info.header_args[':mkdirp'] == 'yes' then
local path = vim.fn.fnamemodify(info.filename, ':h')
vim.fn.mkdir(path, 'p')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does tangle-mode also apply to directories? I can't find anything in the documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me look in the orgmode source and see what they do.

end

local shebang = info.header_args[':shebang']
if shebang then
shebang = shebang:gsub('[\'"]', '')
table.insert(parsed_content, 1, shebang)
end
Comment on lines +83 to +87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is already done above on line 64. Is there a reason why it's needed twice?


if info.name then
block_content_by_name[info.name] = parsed_content
end
vim.list_extend(tangle_info[info.filename], parsed_content)
vim.list_extend(tangle_info[info.filename]['content'], parsed_content)
tangle_info[info.filename]['mode'] = filemode
end

local promises = {}
for filename, content in pairs(tangle_info) do
table.insert(promises, utils.writefile(filename, table.concat(self:_remove_obsolete_indent(content), '\n')))
for filename, block in pairs(tangle_info) do
table.insert(
promises,
utils.writefile(filename, table.concat(self:_remove_obsolete_indent(block['content']), '\n'))
)

local mode_str = block['mode']
if mode_str and mode_str:sub(1, 1) == 'o' then
mode_str = mode_str:sub(2)
local mode_num = tonumber(mode_str, 8)
vim.loop.fs_chmod(filename, mode_num)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing chmod after the file is created, lets pass down the mode as an argument to utils.writefile and pass it there when creating the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sound good, I'm make the changes

end
end
Promise.all(promises):wait()
utils.echo_info(('Tangled %d blocks from %s'):format(#valid_blocks, vim.fn.fnamemodify(self.file.filename, ':t')))