|
| 1 | +--------------------------------------------------------------- |
| 2 | +-- randomizelines.lua for Notepad++ LuaScript plugin |
| 3 | +-- |
| 4 | +-- This script will randomize the lines within a document. |
| 5 | +-- |
| 6 | +-- Activate it on all text in the editor by running without a |
| 7 | +-- selection, or select a group of lines to randomize only |
| 8 | +-- the selected lines. |
| 9 | +--------------------------------------------------------------- |
| 10 | + |
| 11 | +-- helper function to split lines |
| 12 | +local function split(str, sep) |
| 13 | + local result = {} |
| 14 | + local regex = ("([^%s]+)"):format(sep) |
| 15 | + for each in str:gmatch(regex) do |
| 16 | + table.insert(result, each) |
| 17 | + end |
| 18 | + return result |
| 19 | +end |
| 20 | + |
| 21 | +-- helper function to shuffle a table |
| 22 | +local function shuffle(t) |
| 23 | + math.randomseed( os.time() ) |
| 24 | + local rand = math.random |
| 25 | + local iterations = #t |
| 26 | + local j |
| 27 | + |
| 28 | + for i = iterations, 2, -1 do |
| 29 | + j = rand(i) |
| 30 | + t[i], t[j] = t[j], t[i] |
| 31 | + end |
| 32 | + return t |
| 33 | +end |
| 34 | + |
| 35 | +-------------------------------------------------------------------- |
| 36 | + |
| 37 | +npp.AddShortcut("Randomize Lines", "", function() |
| 38 | + |
| 39 | + local result --placeholder to store the processed text for return to editor |
| 40 | + local SelectionStart, SelectionEnd --placeholder to store selection points |
| 41 | + |
| 42 | + -- Preserve Undo Capability |
| 43 | + editor:BeginUndoAction() |
| 44 | + |
| 45 | + -- Get any selected text |
| 46 | + local seltext = editor:GetSelText() |
| 47 | + local seltextlen = #seltext |
| 48 | + |
| 49 | + -- read selected text or entire document |
| 50 | + local rawtext |
| 51 | + if seltextlen >= 1 then |
| 52 | + rawtext = seltext |
| 53 | + --read the selection points for restore after operation |
| 54 | + SelectionStart = editor.SelectionStart |
| 55 | + SelectionEnd = editor.SelectionEnd |
| 56 | + else |
| 57 | + rawtext = editor:GetText() |
| 58 | + end |
| 59 | + |
| 60 | + -- Preserve EOL Mode when we send back the reordered lines |
| 61 | + local EOLchar |
| 62 | + if editor.EOLMode == 0 then |
| 63 | + EOLchar = "\r\n" |
| 64 | + elseif editor.EOLMode == 1 then |
| 65 | + EOLchar = "\r" |
| 66 | + elseif editor.EOLMode == 2 then |
| 67 | + EOLchar = "\n" |
| 68 | + end |
| 69 | + -------------------- |
| 70 | + -- process the text |
| 71 | + -------------------- |
| 72 | + |
| 73 | + -- split the text into an array of lines |
| 74 | + local rawtextlines = split(rawtext,EOLchar); |
| 75 | + |
| 76 | + -- randomize the lines |
| 77 | + rawtextlines = shuffle(rawtextlines) |
| 78 | + |
| 79 | + -- output to result var |
| 80 | + result = table.concat(rawtextlines, EOLchar) |
| 81 | + |
| 82 | + -------------------- |
| 83 | + -- end processing |
| 84 | + -------------------- |
| 85 | + |
| 86 | + -- replace selected text or entire document |
| 87 | + if seltextlen >= 1 then |
| 88 | + editor:ReplaceSel(result) |
| 89 | + -- restore selection |
| 90 | + editor:SetSel(SelectionStart, SelectionEnd) |
| 91 | + else |
| 92 | + editor:SetText(result) |
| 93 | + end |
| 94 | + |
| 95 | + editor:EndUndoAction() |
| 96 | + |
| 97 | +end) |
0 commit comments