From 90d26c400ff0baa7036b68d7b320c74ccc1b29e8 Mon Sep 17 00:00:00 2001 From: abashak Date: Fri, 25 Jun 2021 12:04:10 +0300 Subject: [PATCH] Add example scripts - base64_2_jpg.lua: open base64 encoded jpg images in external viewer - fix_console.lua: unfold lines which copied from windows console - open_source.lua: open corresponding header (.h, .hpp) or source (.c, .cpp) file - quoting.lua: Mark selected or all lines with quoting char '>' --- examples/base64_2_jpg.lua | 41 ++++++++++++++ examples/fix_console.lua | 113 ++++++++++++++++++++++++++++++++++++++ examples/open_source.lua | 48 ++++++++++++++++ examples/quoting.lua | 112 +++++++++++++++++++++++++++++++++++++ 4 files changed, 314 insertions(+) create mode 100644 examples/base64_2_jpg.lua create mode 100644 examples/fix_console.lua create mode 100644 examples/open_source.lua create mode 100644 examples/quoting.lua diff --git a/examples/base64_2_jpg.lua b/examples/base64_2_jpg.lua new file mode 100644 index 0000000..c43fa22 --- /dev/null +++ b/examples/base64_2_jpg.lua @@ -0,0 +1,41 @@ +-- open base64 encoded jpg images in external viewer + + +local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' + +local function base64_dec(data) + data = string.gsub(data, '[^'..b..'=]', '') + return (data:gsub('.', function(x) + if (x == '=') then return '' end + local r,f='',(b:find(x)-1) + for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end + return r; + end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x) + if (#x ~= 8) then return '' end + local c=0 + for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end + return string.char(c) + end)) +end + +npp.AddShortcut("base64 to Image", "", function() + local text = editor:GetSelText() -- Get any selected text + + if #text ==0 then + text = editor:GetText() -- read selected text or entire document + end + + local data = base64_dec(text) + + local path = os.tmpname() + if not string.find(path, ':') then + path = os.getenv("TEMP") .. path + end + path = path .. '.jpg' + + local f = assert(io.open(path, 'w+b')) + f:write(data) + f:close() + + os.execute(path) +end) diff --git a/examples/fix_console.lua b/examples/fix_console.lua new file mode 100644 index 0000000..10e4425 --- /dev/null +++ b/examples/fix_console.lua @@ -0,0 +1,113 @@ +-- Unfold lines which copied from windows console +-- обрабатывает вставленный вывод консоли, склеивая перенесенные строки в одну + +local function GetEOLchar() + local EOLchar + if editor.EOLMode == 0 then + EOLchar = "\r\n" + elseif editor.EOLMode == 1 then + EOLchar = "\r" + elseif editor.EOLMode == 2 then + EOLchar = "\n" + end + return EOLchar +end + +-- split without skip empty lines +local function split(str, delimiter) + local result = { } + local from = 1 + local delim_from, delim_to = string.find( str, delimiter, from ) + while delim_from do + table.insert( result, string.sub( str, from , delim_from-1 ) ) + from = delim_to + 1 + delim_from, delim_to = string.find( str, delimiter, from ) + end + table.insert( result, string.sub( str, from ) ) + + -- clean only space strings + for i = 1, #result do + if not string.find(result[i], '%S') then + result[i] = '' + end + end + return result +end + +local function get_max_line_len(lines) + local res + for _, line in ipairs(lines) do + if not res or res < #line then + res = #line + end + end + return res +end + + +local function merge_lines(line_len_max, lines) + local res = {} + local cur_line + for i = 1, #lines+1 do + local line = lines[i] or '' + --print(cur_line and #cur_line, #line, line) + + if #line+1 >= line_len_max then + if #line+1 == line_len_max then + line = line .. ' ' + end + cur_line = (cur_line or '') .. line + else + if cur_line then + line = cur_line .. line + cur_line = nil + end + table.insert(res, line) + end + end + + return res +end + + +-- ========================================= + +npp.AddShortcut("Fix Concole Output", "", function() + editor:BeginUndoAction() -- Preserve Undo Capability + + local text = editor:GetSelText() -- Get any selected text + + local SelectionStart, SelectionEnd + if #text >= 1 then + --read the selection points for restore after operation + SelectionStart = editor.SelectionStart + SelectionEnd = editor.SelectionEnd + else + text = editor:GetText() -- read selected text or entire document + end + + local EOLchar = GetEOLchar() -- Preserve EOL Mode when we send back the reordered lines + + -------------------- + -- process the text + -------------------- + + local rawtextlines = split(text, EOLchar) -- split the text into an array of lines + local max_len = get_max_line_len(rawtextlines) + print('max_len =', max_len) + rawtextlines = merge_lines(max_len, rawtextlines) + + + text = table.concat(rawtextlines, EOLchar) -- output to result var + + -- replace selected text or entire document + if SelectionStart then + editor:ReplaceSel(text) + -- restore selection + editor:SetSel(SelectionStart, SelectionEnd) + else + editor:SetText(text) + end + + editor:EndUndoAction() +end) diff --git a/examples/open_source.lua b/examples/open_source.lua new file mode 100644 index 0000000..a760594 --- /dev/null +++ b/examples/open_source.lua @@ -0,0 +1,48 @@ +-- open corresponding header (.h, .hpp) or source (.c, .cpp) file +-- скрипт для Notepad++, открывает соотвтествующий файл заголовка или исх. кода по названию + + +local function get_index(tbl, val) + for i, v in ipairs(tbl) do + if v == val then + return i + end + end +end + +local function split_ext(file_path) + return string.match(file_path, "^(.+)(%..+)$") +end + +local function file_exist(file_path) + local f = io.open(file_path, "r") + if f ~= nil then + io.close(f) + return true + else + return false + end +end + + +-- ====================================================== + +local KNOWN_EXTENSIONS = {'.h', '.hpp', '.cpp', '.c'} + + +npp.AddShortcut("Open Source", "", function() + local file_name, file_ext = split_ext(npp:GetFullCurrentPath()) + + if get_index(KNOWN_EXTENSIONS, file_ext) then + for i, ext in ipairs(KNOWN_EXTENSIONS) do + if ext ~= file_ext then + local tmp_path = file_name .. ext + -- print(tmp_path) + if file_exist(tmp_path) then + npp:DoOpen(tmp_path) + break + end + end + end + end +end) diff --git a/examples/quoting.lua b/examples/quoting.lua new file mode 100644 index 0000000..9474e30 --- /dev/null +++ b/examples/quoting.lua @@ -0,0 +1,112 @@ +-- Mark selected or all lines with quoting char '>' + + +-- helper function to split lines +local function split1(str, sep) + local result = {} + local regex = ("([^%s]+)"):format(sep) + for each in str:gmatch(regex) do + table.insert(result, each) + end + return result +end + +-- split without skip empty lines +local function split(str, delimiter) + local result = { } + local from = 1 + local delim_from, delim_to = string.find( str, delimiter, from ) + while delim_from do + table.insert( result, string.sub( str, from , delim_from-1 ) ) + from = delim_to + 1 + delim_from, delim_to = string.find( str, delimiter, from ) + end + table.insert( result, string.sub( str, from ) ) + + -- clean only space strings + for i = 1, #result do + if not string.find(result[i], '%S') then + result[i] = '' + end + end + return result +end + + +local function add_quote_char(lines) + local res = {} + for _, line in ipairs(lines) do + if #line > 0 then + line = '> ' .. line + end + table.insert(res, line) + end + return res +end + +local function GetEOLchar() + local EOLchar + if editor.EOLMode == 0 then + EOLchar = "\r\n" + elseif editor.EOLMode == 1 then + EOLchar = "\r" + elseif editor.EOLMode == 2 then + EOLchar = "\n" + end + return EOLchar +end + +-------------------------------------------------------------------- + + +npp.AddShortcut("Make Quoting", "", function() + + local SelectionStart, SelectionEnd + + -- Preserve Undo Capability + editor:BeginUndoAction() + + -- Get any selected text + local text = editor:GetSelText() + + -- read selected text or entire document + if #text >= 1 then + --read the selection points for restore after operation + SelectionStart = editor.SelectionStart + SelectionEnd = editor.SelectionEnd + else + text = editor:GetText() + end + + -- Preserve EOL Mode when we send back the reordered lines + local EOLchar = GetEOLchar() + + -------------------- + -- process the text + -------------------- + + -- split the text into an array of lines + local rawtextlines = split(text, EOLchar); + + -- randomize the lines + rawtextlines = add_quote_char(rawtextlines) + + -- output to result var + text = table.concat(rawtextlines, EOLchar) + + -------------------- + -- end processing + -------------------- + + -- replace selected text or entire document + if SelectionStart then + editor:ReplaceSel(text) + -- restore selection + editor:SetSel(SelectionStart, SelectionEnd) + else + editor:SetText(text) + end + + editor:EndUndoAction() + +end)