From 8441de0b3b95ff0ec591e8ec3134531f6d369c7c Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Mon, 10 Mar 2025 11:08:33 -0400 Subject: [PATCH 01/17] feat(babel): add shebang support for tangled code blocks Add the ability to include a shebang line at the beginning of tangled code files by supporting the `:shebang` header argument. The shebang value is cleaned of quotes and inserted as the first line of the tangled content. --- lua/orgmode/babel/tangle.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index 0d317b65a..612b70781 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -60,6 +60,13 @@ function Tangle:tangle() vim.fn.mkdir(path, 'p') end + local shebang = info.header_args[':shebang'] + if shebang then + shebang = shebang:gsub('[\'"]', '') + utils.echo_info(('shebang: %s'):format(shebang)) + table.insert(parsed_content, 1, shebang) + end + if info.name then block_content_by_name[info.name] = parsed_content end From cc2a38b146b090aa805563ab55f927923cd0d4d2 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Tue, 11 Mar 2025 01:13:40 -0400 Subject: [PATCH 02/17] feat(babel): Support the tangle-mode source block header This header allow the creator to change the generated file permissions. --- lua/orgmode/babel/tangle.lua | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index 612b70781..60e5ff645 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -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 @@ -63,19 +64,33 @@ function Tangle:tangle() local shebang = info.header_args[':shebang'] if shebang then shebang = shebang:gsub('[\'"]', '') - utils.echo_info(('shebang: %s'):format(shebang)) 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.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) + end end Promise.all(promises):wait() utils.echo_info(('Tangled %d blocks from %s'):format(#valid_blocks, vim.fn.fnamemodify(self.file.filename, ':t'))) From 47a3b11cdb9c49adce07229df086d445f0d50954 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Tue, 11 Mar 2025 01:21:52 -0400 Subject: [PATCH 03/17] feat(babel): add directory creation support with :mkdirp header argument When tangling code blocks to files, automatically create parent directories when the :mkdirp header argument is set to 'yes'. --- lua/orgmode/babel/tangle.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index 60e5ff645..a57b93c65 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -75,6 +75,11 @@ function Tangle:tangle() 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") + end + if info.name then block_content_by_name[info.name] = parsed_content end From 9288b8658f63078815c2491bb3182152b1f41e77 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Tue, 11 Mar 2025 00:11:01 -0400 Subject: [PATCH 04/17] fix(babel): replace double quotes with single quotes. Coding style requires single quotes --- lua/orgmode/babel/tangle.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index a57b93c65..f38c0af7e 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -77,7 +77,7 @@ function Tangle:tangle() if info.header_args[':mkdirp'] == 'yes' then local path = vim.fn.fnamemodify(info.filename, ':h') - vim.fn.mkdir(path, "p") + vim.fn.mkdir(path, 'p') end if info.name then From 8d63b6f5d9dfded1f067cd63daa7afde2618e4d0 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Mon, 10 Mar 2025 11:08:33 -0400 Subject: [PATCH 05/17] feat(babel): add shebang support for tangled code blocks Add the ability to include a shebang line at the beginning of tangled code files by supporting the `:shebang` header argument. The shebang value is cleaned of quotes and inserted as the first line of the tangled content. --- lua/orgmode/babel/tangle.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index f38c0af7e..566e73e4b 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -80,6 +80,13 @@ function Tangle:tangle() vim.fn.mkdir(path, 'p') end + local shebang = info.header_args[':shebang'] + if shebang then + shebang = shebang:gsub('[\'"]', '') + utils.echo_info(('shebang: %s'):format(shebang)) + table.insert(parsed_content, 1, shebang) + end + if info.name then block_content_by_name[info.name] = parsed_content end From 9e022f5e3b5c2fe2816da34b03af58c5d03f31ca Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Tue, 11 Mar 2025 01:30:11 -0400 Subject: [PATCH 06/17] fix(babel): remove debug messages They keep showing up. --- lua/orgmode/babel/tangle.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index 566e73e4b..b6ce9877b 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -83,7 +83,6 @@ function Tangle:tangle() local shebang = info.header_args[':shebang'] if shebang then shebang = shebang:gsub('[\'"]', '') - utils.echo_info(('shebang: %s'):format(shebang)) table.insert(parsed_content, 1, shebang) end From 482959202719dc980b19a8ef8ef0c08c9feaccad Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Wed, 19 Mar 2025 13:19:47 -0400 Subject: [PATCH 07/17] fix: lint error, replace double quotes with single quotes --- lua/orgmode/babel/tangle.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index b6ce9877b..dbf445108 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -66,7 +66,7 @@ function Tangle:tangle() shebang = shebang:gsub('[\'"]', '') table.insert(parsed_content, 1, shebang) if filemode == nil then - filemode = "o755" + filemode = 'o755' end end From 64d4fb73388aee5764cb3c37ae75546871b0fac1 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Wed, 19 Mar 2025 13:51:47 -0400 Subject: [PATCH 08/17] fix: linter errors --- lua/orgmode/babel/tangle.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index dbf445108..647dd3c77 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -37,7 +37,7 @@ function Tangle:tangle() if tangle_info[info.filename] then table.insert(tangle_info[info.filename]['content'], '') else - tangle_info[info.filename] = {content = {}} + tangle_info[info.filename] = { content = {} } end local filemode = tangle_info[info.filename]['mode'] @@ -95,7 +95,11 @@ function Tangle:tangle() local promises = {} for filename, block in pairs(tangle_info) do - table.insert(promises, utils.writefile(filename, table.concat(self:_remove_obsolete_indent(block['content']), '\n'))) + 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) From 0dbea990c7ad558051f9dc38c9d8703d1fe90470 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Wed, 19 Mar 2025 13:54:12 -0400 Subject: [PATCH 09/17] fix: remove trailing spaces --- lua/orgmode/babel/tangle.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index 647dd3c77..f6119d073 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -96,7 +96,7 @@ function Tangle:tangle() local promises = {} for filename, block in pairs(tangle_info) do table.insert( - promises, + promises, utils.writefile(filename, table.concat(self:_remove_obsolete_indent(block['content']), '\n')) ) From d424528814046574595466ebcd9fc5b5c872fb50 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Mon, 14 Apr 2025 23:29:54 -0400 Subject: [PATCH 10/17] fix(babel): converting file mode to string for writefile --- lua/orgmode/babel/tangle.lua | 52 +++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index f6119d073..cad306d57 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -14,6 +14,44 @@ function Tangle:new(opts) }, self) end +function mode_to_string(mode) + local permissions = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"} + local result = "" + + -- File type + local file_type = bit.band(bit.rshift(mode, 12), 15) + local type_char = { + [0] = "-", -- regular file + [1] = "p", -- named pipe (fifo) + [2] = "c", -- character special + [4] = "d", -- directory + [6] = "b", -- block special + [8] = "f", -- regular file (rarely used) + [10] = "l", -- symbolic link + [12] = "s" -- socket + } + result = result .. (type_char[file_type] or "-") + + -- Owner, group, others permissions + for i = 2, 0, -1 do + local perm = bit.band(bit.rshift(mode, i * 3), 7) + result = result .. permissions[perm + 1] + end + + -- Special bits + if bit.band(mode, 0x800) ~= 0 then -- sticky bit + result = result:sub(1, 9) .. (result:sub(10, 10) == "-" and "T" or "t") + end + if bit.band(mode, 0x400) ~= 0 then -- setgid + result = result:sub(1, 6) .. (result:sub(7, 7) == "-" and "S" or "s") + end + if bit.band(mode, 0x200) ~= 0 then -- setuid + result = result:sub(1, 3) .. (result:sub(4, 4) == "-" and "S" or "s") + end + + return result +end + function Tangle:tangle() local block_content_by_name = {} ---@type OrgBlockTangleInfo[] @@ -95,17 +133,17 @@ function Tangle:tangle() local promises = {} for filename, block in pairs(tangle_info) do + local mode_str = block['mode'] + if mode_str and mode_str:sub(1, 1) == 'o' then + mode_str[1] = 0 + mode_str = mode_to_string(mode_str) + end + table.insert( promises, - utils.writefile(filename, table.concat(self:_remove_obsolete_indent(block['content']), '\n')) + utils.writefile(filename, table.concat(self:_remove_obsolete_indent(block['content']), '\n'), mode_str) ) - 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) - end end Promise.all(promises):wait() utils.echo_info(('Tangled %d blocks from %s'):format(#valid_blocks, vim.fn.fnamemodify(self.file.filename, ':t'))) From a146841dd2bb63d615261fbf0e3a35829fc3f3d3 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Mon, 14 Apr 2025 23:31:11 -0400 Subject: [PATCH 11/17] fix(babel): removed extra shebang parsing --- lua/orgmode/babel/tangle.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index cad306d57..e7c79da02 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -118,12 +118,6 @@ 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) - end - if info.name then block_content_by_name[info.name] = parsed_content end From 3d3857693f6d6bb162e2792393aa9f72f02c52c4 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Wed, 16 Apr 2025 00:22:52 -0400 Subject: [PATCH 12/17] fix(bable) tangle-mode support ls-style, chmod-style and octal style file mode --- lua/orgmode/babel/tangle.lua | 98 +++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 40 deletions(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index e7c79da02..291998206 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -14,44 +14,51 @@ function Tangle:new(opts) }, self) end -function mode_to_string(mode) - local permissions = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"} - local result = "" - - -- File type - local file_type = bit.band(bit.rshift(mode, 12), 15) - local type_char = { - [0] = "-", -- regular file - [1] = "p", -- named pipe (fifo) - [2] = "c", -- character special - [4] = "d", -- directory - [6] = "b", -- block special - [8] = "f", -- regular file (rarely used) - [10] = "l", -- symbolic link - [12] = "s" -- socket - } - result = result .. (type_char[file_type] or "-") - - -- Owner, group, others permissions - for i = 2, 0, -1 do - local perm = bit.band(bit.rshift(mode, i * 3), 7) - result = result .. permissions[perm + 1] - end - - -- Special bits - if bit.band(mode, 0x800) ~= 0 then -- sticky bit - result = result:sub(1, 9) .. (result:sub(10, 10) == "-" and "T" or "t") - end - if bit.band(mode, 0x400) ~= 0 then -- setgid - result = result:sub(1, 6) .. (result:sub(7, 7) == "-" and "S" or "s") - end - if bit.band(mode, 0x200) ~= 0 then -- setuid - result = result:sub(1, 3) .. (result:sub(4, 4) == "-" and "S" or "s") +function ls_style_to_octal(rwx_string) + local result = 0 + local value = 0 + + for i = 1, #rwx_string, 3 do + local chunk = rwx_string:sub(i, i+2) + value = 0 + + if chunk:sub(1, 1) == 'r' then value = value + 4 end + if chunk:sub(2, 2) == 'w' then value = value + 2 end + if chunk:sub(3, 3) == 'x' then value = value + 1 end + + result = result * 8 + value + utils.echo_info(("ls style mode: %o"):format(result)) end - + return result end + +function chmod_style_to_octal(chmod_string) + local owner, group, other = 0, 0, 0 + + for part in chmod_string:gmatch('[^,]+') do + utils.echo_info(('part: %s'):format(part)) + local who, what = part:match('(%a+)[=+](.+)') + utils.echo_info(('who: %s what: %s'):format(who, what)) + if not who or not what then + return nil + end + + + local perm = 0 + if what:find('r') then perm = perm + 4 end + if what:find('w') then perm = perm + 2 end + if what:find('x') then perm = perm + 1 end + + if who:find('u') or who:find('a') then owner = bit.bor(owner, perm) end + if who:find('g') or who:find('a') then group = bit.bor(group, perm) end + if who:find('o') or who:find('a') then other = bit.bor(other, perm) end + end + + return owner * 64 + group * 8 + other +end + function Tangle:tangle() local block_content_by_name = {} ---@type OrgBlockTangleInfo[] @@ -127,17 +134,28 @@ function Tangle:tangle() local promises = {} for filename, block in pairs(tangle_info) do - local mode_str = block['mode'] - if mode_str and mode_str:sub(1, 1) == 'o' then - mode_str[1] = 0 - mode_str = mode_to_string(mode_str) - end table.insert( promises, - utils.writefile(filename, table.concat(self:_remove_obsolete_indent(block['content']), '\n'), mode_str) + utils.writefile(filename, table.concat(self:_remove_obsolete_indent(block['content']), '\n')) ) + local mode_str = block['mode'] + local mode = nil + + if mode_str and mode_str:sub(1, 1) == 'o' then + mode = tonumber(mode_str:sub(2), 8) + else + mode = chmod_style_to_octal(mode_str) + if mode == nil then + mode = ls_style_to_octal(mode_str) + end + end + + if mode then + utils.echo_info(('change mode %s mode %o'):format(filename, mode)) + vim.loop.fs_chmod(filename, mode) + end end Promise.all(promises):wait() utils.echo_info(('Tangled %d blocks from %s'):format(#valid_blocks, vim.fn.fnamemodify(self.file.filename, ':t'))) From fdfb3c8fe29c06cd14bf0d11f6b4a2c4241fcb27 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Wed, 16 Apr 2025 00:29:53 -0400 Subject: [PATCH 13/17] fix(bable): remove debugging output --- lua/orgmode/babel/tangle.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index 291998206..b728db2dd 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -27,7 +27,6 @@ function ls_style_to_octal(rwx_string) if chunk:sub(3, 3) == 'x' then value = value + 1 end result = result * 8 + value - utils.echo_info(("ls style mode: %o"):format(result)) end return result @@ -38,9 +37,7 @@ function chmod_style_to_octal(chmod_string) local owner, group, other = 0, 0, 0 for part in chmod_string:gmatch('[^,]+') do - utils.echo_info(('part: %s'):format(part)) local who, what = part:match('(%a+)[=+](.+)') - utils.echo_info(('who: %s what: %s'):format(who, what)) if not who or not what then return nil end From 537ceb7433e221298660296138297e23b3b8a021 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Wed, 16 Apr 2025 00:37:49 -0400 Subject: [PATCH 14/17] chore(style): fix lua style linter errors. --- lua/orgmode/babel/tangle.lua | 52 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index b728db2dd..3f0248594 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -15,45 +15,45 @@ function Tangle:new(opts) end function ls_style_to_octal(rwx_string) - local result = 0 - local value = 0 + local result = 0 + local value = 0 - for i = 1, #rwx_string, 3 do - local chunk = rwx_string:sub(i, i+2) - value = 0 + for i = 1, #rwx_string, 3 do + local chunk = rwx_string:sub(i, i+2) + value = 0 - if chunk:sub(1, 1) == 'r' then value = value + 4 end - if chunk:sub(2, 2) == 'w' then value = value + 2 end - if chunk:sub(3, 3) == 'x' then value = value + 1 end + if chunk:sub(1, 1) == 'r' then value = value + 4 end + if chunk:sub(2, 2) == 'w' then value = value + 2 end + if chunk:sub(3, 3) == 'x' then value = value + 1 end - result = result * 8 + value - end + result = result * 8 + value + end - return result + return result end function chmod_style_to_octal(chmod_string) - local owner, group, other = 0, 0, 0 + local owner, group, other = 0, 0, 0 - for part in chmod_string:gmatch('[^,]+') do - local who, what = part:match('(%a+)[=+](.+)') - if not who or not what then - return nil - end + for part in chmod_string:gmatch('[^,]+') do + local who, what = part:match('(%a+)[=+](.+)') + if not who or not what then + return nil + end - local perm = 0 - if what:find('r') then perm = perm + 4 end - if what:find('w') then perm = perm + 2 end - if what:find('x') then perm = perm + 1 end + local perm = 0 + if what:find('r') then perm = perm + 4 end + if what:find('w') then perm = perm + 2 end + if what:find('x') then perm = perm + 1 end - if who:find('u') or who:find('a') then owner = bit.bor(owner, perm) end - if who:find('g') or who:find('a') then group = bit.bor(group, perm) end - if who:find('o') or who:find('a') then other = bit.bor(other, perm) end - end + if who:find('u') or who:find('a') then owner = bit.bor(owner, perm) end + if who:find('g') or who:find('a') then group = bit.bor(group, perm) end + if who:find('o') or who:find('a') then other = bit.bor(other, perm) end + end - return owner * 64 + group * 8 + other + return owner * 64 + group * 8 + other end function Tangle:tangle() From 1286de0061be2b70fe31c3604be2223dd5a984f0 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Wed, 16 Apr 2025 00:44:20 -0400 Subject: [PATCH 15/17] chore(style): more lua style fixes --- lua/orgmode/babel/tangle.lua | 41 ++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index 3f0248594..bd9cba2de 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -19,12 +19,18 @@ function ls_style_to_octal(rwx_string) local value = 0 for i = 1, #rwx_string, 3 do - local chunk = rwx_string:sub(i, i+2) + local chunk = rwx_string:sub(i, i + 2) value = 0 - if chunk:sub(1, 1) == 'r' then value = value + 4 end - if chunk:sub(2, 2) == 'w' then value = value + 2 end - if chunk:sub(3, 3) == 'x' then value = value + 1 end + if chunk:sub(1, 1) == 'r' then + value = value + 4 + end + if chunk:sub(2, 2) == 'w' then + value = value + 2 + end + if chunk:sub(3, 3) == 'x' then + value = value + 1 + end result = result * 8 + value end @@ -32,7 +38,6 @@ function ls_style_to_octal(rwx_string) return result end - function chmod_style_to_octal(chmod_string) local owner, group, other = 0, 0, 0 @@ -42,15 +47,26 @@ function chmod_style_to_octal(chmod_string) return nil end - local perm = 0 - if what:find('r') then perm = perm + 4 end - if what:find('w') then perm = perm + 2 end - if what:find('x') then perm = perm + 1 end + if what:find('r') then + perm = perm + 4 + end + if what:find('w') then + perm = perm + 2 + end + if what:find('x') then + perm = perm + 1 + end - if who:find('u') or who:find('a') then owner = bit.bor(owner, perm) end - if who:find('g') or who:find('a') then group = bit.bor(group, perm) end - if who:find('o') or who:find('a') then other = bit.bor(other, perm) end + if who:find('u') or who:find('a') then + owner = bit.bor(owner, perm) + end + if who:find('g') or who:find('a') then + group = bit.bor(group, perm) + end + if who:find('o') or who:find('a') then + other = bit.bor(other, perm) + end end return owner * 64 + group * 8 + other @@ -131,7 +147,6 @@ function Tangle:tangle() local promises = {} for filename, block in pairs(tangle_info) do - table.insert( promises, utils.writefile(filename, table.concat(self:_remove_obsolete_indent(block['content']), '\n')) From f7cbb2281afd2a3a3fdf5d9029fe754c14330215 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Wed, 16 Apr 2025 09:51:03 -0400 Subject: [PATCH 16/17] fix(tangle-mode): handling case were we don't have an empty mode_str --- lua/orgmode/babel/tangle.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index bd9cba2de..64a6a0fe3 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -157,7 +157,7 @@ function Tangle:tangle() if mode_str and mode_str:sub(1, 1) == 'o' then mode = tonumber(mode_str:sub(2), 8) - else + elseif mode_str then mode = chmod_style_to_octal(mode_str) if mode == nil then mode = ls_style_to_octal(mode_str) From 717bb800abab4378506de38b127bfd7ad8e15b53 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Wed, 16 Apr 2025 09:55:10 -0400 Subject: [PATCH 17/17] fix(tangle-mode): refactor if statement with no duplicate test --- lua/orgmode/babel/tangle.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lua/orgmode/babel/tangle.lua b/lua/orgmode/babel/tangle.lua index 64a6a0fe3..207feed80 100644 --- a/lua/orgmode/babel/tangle.lua +++ b/lua/orgmode/babel/tangle.lua @@ -155,12 +155,14 @@ function Tangle:tangle() local mode_str = block['mode'] local mode = nil - if mode_str and mode_str:sub(1, 1) == 'o' then - mode = tonumber(mode_str:sub(2), 8) - elseif mode_str then - mode = chmod_style_to_octal(mode_str) - if mode == nil then - mode = ls_style_to_octal(mode_str) + if mode_str then + if mode_str:sub(1, 1) == 'o' then + mode = tonumber(mode_str:sub(2), 8) + else + mode = chmod_style_to_octal(mode_str) + if mode == nil then + mode = ls_style_to_octal(mode_str) + end end end