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

Add example scripts #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions examples/base64_2_jpg.lua
Original file line number Diff line number Diff line change
@@ -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)
113 changes: 113 additions & 0 deletions examples/fix_console.lua
Original file line number Diff line number Diff line change
@@ -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)
48 changes: 48 additions & 0 deletions examples/open_source.lua
Original file line number Diff line number Diff line change
@@ -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)
112 changes: 112 additions & 0 deletions examples/quoting.lua
Original file line number Diff line number Diff line change
@@ -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)