[SciTE] 블록으로 지정한 구간 정렬하기 by tactlee

블록으로 지정한 구간을 정렬해준다. 자주 쓰이지는 않지만, 그래도 아쉬울 때가 있다. SciteSortSelection에서 가져와 extman에 등록해서 사용할 수 있도록 수정했다.

SciTE_SortSelection.lua
  1. -- Sort a selected text
  2. -- The functions below will sort the selected text in alphabetical or alphabetical reverse order.
  3. -- From http://lua-users.org/wiki/SciteSortSelection

  4. scite_Command {
  5.     'Sort Selection|sort_text',
  6.     'Sort Selection Reverse|sort_text_reverse',
  7. }

  8. function lines(str)
  9.     local t = {}
  10.     local i, lstr = 1, #str
  11.     while i <= lstr do
  12.         local x, y = string.find(str, "\r?\n", i)
  13.         if x then t[#t + 1] = string.sub(str, i, x - 1)
  14.         else break
  15.         end
  16.         i = y + 1
  17.     end
  18.     if i <= lstr then t[#t + 1] = string.sub(str, i) end
  19.     return t
  20. end

  21. function sort_text()
  22.     local sel = editor:GetSelText()
  23.     if #sel == 0 then return end
  24.     local eol = string.match(sel, "\n$")
  25.     local buf = lines(sel)
  26.     --table.foreach(buf, print) --used for debugging
  27.     table.sort(buf)
  28.     local out = table.concat(buf, "\n")
  29.     if eol then out = out.."\n" end
  30.     editor:ReplaceSel(out)
  31. end

  32. function sort_text_reverse()
  33.     local sel = editor:GetSelText()
  34.     if #sel == 0 then return end
  35.     local eol = string.match(sel, "\n$")
  36.     local buf = lines(sel)
  37.     --table.foreach(buf, print) --used for debugging
  38.     table.sort(buf, function(a, b) return a > b end)
  39.     local out = table.concat(buf, "\n")
  40.     if eol then out = out.."\n" end
  41.     editor:ReplaceSel(out)
  42. end

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://tactlee.egloos.com/tb/1646920 [도움말]

덧글

덧글 입력 영역


GirlsWatch