|
| 1 | +;; |
| 2 | +;; Закомментировать или раскомментировать выделенный блок |
| 3 | +;; (c) Max Rusov |
| 4 | +;; |
| 5 | + |
| 6 | +macro Descr="Comment/uncomment selected block" Area="Editor" Keys="Ctrl-:Release" |
| 7 | +{{ |
| 8 | + #include_macro_once "CommentFunc" |
| 9 | + _G.ToggleComment(nil, _G.FindComment( Editor.FileName ) ) |
| 10 | +}} |
| 11 | + |
| 12 | + |
| 13 | +macro Descr="Uncomment selected block" Area="Editor" Keys="CtrlShift-:Release" |
| 14 | +{{ |
| 15 | + #include_macro_once "CommentFunc" |
| 16 | + local Comment1 = _G.FindComment( Editor.FileName ) |
| 17 | + if type(Comment1) == "string" then |
| 18 | + _G.UnCommentLineBlock(nil, Comment1 ) |
| 19 | + end |
| 20 | +}} |
| 21 | + |
| 22 | + |
| 23 | +macro Descr="Comment/uncomment selected block (alternative)" Area="Editor" Keys="Ctrl-:Hold" |
| 24 | +{{ |
| 25 | + #include_macro_once "CommentFunc" |
| 26 | + local Comment1, Comment2 = _G.FindComment( Editor.FileName ) |
| 27 | + if Comment2 then |
| 28 | + _G.ToggleComment(Id, Comment2 ) |
| 29 | + else |
| 30 | + mf.beep() |
| 31 | + end |
| 32 | +}} |
| 33 | + |
| 34 | + |
| 35 | +//----------------------------------------------------------------------------- |
| 36 | +// Глобальные функции |
| 37 | + |
| 38 | +macro Name="CommentFunc" |
| 39 | +{{ |
| 40 | + local Settings = { |
| 41 | + {"pas,pp,dpr", "//", {"(*", "*)"} }, |
| 42 | + {"lua,fmlua", "--", {"--[[", "--]]"} }, |
| 43 | + {"bat", "rem " }, |
| 44 | + {"htm,html,xml", {"<!--", "-->"} } |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + function _G.FindComment(AName) |
| 49 | + local vExt = mf.fsplit(AName, 8):sub(2):lower() |
| 50 | + for i, vRow in ipairs(Settings) do |
| 51 | + for iExt in vRow[1]:gmatch("[^%s,]+") do |
| 52 | + if iExt == vExt then |
| 53 | + return vRow[2], vRow[3] |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + return "//", {"/*", "*/"} |
| 58 | + end |
| 59 | + |
| 60 | +-- function _G.FindComment(AName) |
| 61 | +-- local vExt = mf.fsplit(AName, 8):sub(2) |
| 62 | +-- local vReg = regex.new("^(.*,)*\\s*"..vExt.."\\s*(,.*)*$") |
| 63 | +-- for i = 1, table.maxn(Settings) do |
| 64 | +-- local vRow = Settings[i] |
| 65 | +-- if vReg:find(vRow[1]) then |
| 66 | +-- return vRow[2], vRow[3] |
| 67 | +-- end |
| 68 | +-- end |
| 69 | +-- return "//", {"/*", "*/"} |
| 70 | +-- end |
| 71 | + |
| 72 | + |
| 73 | + function CorrectBlockWidth(ADelta) |
| 74 | + local vInfo = editor.GetSelection(Id) |
| 75 | + editor.Select(Id, vInfo.BlockType, vInfo.StartLine, vInfo.StartPos, |
| 76 | + vInfo.EndPos - vInfo.StartPos + ADelta, |
| 77 | + vInfo.EndLine - vInfo.StartLine + 1) |
| 78 | + end |
| 79 | + |
| 80 | + ------------------------------------------------------------------------------ |
| 81 | + |
| 82 | + function _G.CommentLineBlock(Id, AComment) |
| 83 | + local vInfo = editor.GetSelection(Id) |
| 84 | + if not vInfo then return end |
| 85 | + editor.UndoRedo(Id, "EUR_BEGIN") |
| 86 | + for i = vInfo.StartLine, vInfo.EndLine do |
| 87 | + local vRow = editor.GetString(Id, i) |
| 88 | + local vStr = vRow.StringText |
| 89 | + vStr = vStr:sub(0, vRow.SelStart) .. AComment .. vStr:sub(vRow.SelStart+1) |
| 90 | + editor.SetString(Id, i, vStr, vRow.StringEOL) |
| 91 | + end |
| 92 | + editor.UndoRedo(Id, "EUR_END") |
| 93 | + end |
| 94 | + |
| 95 | + function _G.UnCommentLineBlock(Id, AComment) |
| 96 | + local vLen = AComment:len() |
| 97 | + local vInfo = editor.GetSelection(Id) |
| 98 | + if not vInfo then return end |
| 99 | + editor.UndoRedo(Id, "EUR_BEGIN") |
| 100 | + for i = vInfo.StartLine, vInfo.EndLine do |
| 101 | + local vRow = editor.GetString(Id, i) |
| 102 | + local vStr = vRow.StringText |
| 103 | + local vPos = vRow.SelStart |
| 104 | + if vStr:sub(vPos+1, vPos+vLen) == AComment then |
| 105 | + vStr = vStr:sub(0, vPos) .. vStr:sub(vPos+1+vLen) |
| 106 | + editor.SetString(Id, i, vStr, vRow.StringEOL) |
| 107 | + end |
| 108 | + end |
| 109 | + editor.UndoRedo(Id, "EUR_END") |
| 110 | + end |
| 111 | + |
| 112 | + function _G.IsBlockLineComment(Id, AComment) |
| 113 | + local vLen = AComment:len() |
| 114 | + local vInfo = editor.GetSelection(Id) |
| 115 | + if not vInfo then return false end |
| 116 | + for i = vInfo.StartLine, vInfo.EndLine do |
| 117 | + local vRow = editor.GetString(Id, i) |
| 118 | + local vStr = vRow.StringText |
| 119 | + local vPos = vRow.SelStart |
| 120 | + if vStr:sub(vPos+1, vPos+vLen) ~= AComment then |
| 121 | + return false |
| 122 | + end |
| 123 | + end |
| 124 | + return true |
| 125 | + end |
| 126 | + |
| 127 | + function _G.ToggleCommentLineBlock(Id, AComment) |
| 128 | + if not _G.IsBlockLineComment(Id, AComment) then |
| 129 | + _G.CommentLineBlock(Id, AComment) |
| 130 | + else |
| 131 | + _G.UnCommentLineBlock(Id, AComment) |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | +------------------------------------------------------------------------------ |
| 136 | + |
| 137 | + function _G.CommentStreamBlock(Id, AComment) |
| 138 | + local vRow, vStr, vCol, vCor |
| 139 | + local vInfo = editor.GetSelection(Id) |
| 140 | + if not vInfo then return end |
| 141 | + editor.UndoRedo(Id, "EUR_BEGIN") |
| 142 | + |
| 143 | + vRow = editor.GetString(Id, vInfo.EndLine) |
| 144 | + vStr = vRow.StringText |
| 145 | + vCor = vRow.SelEnd >= 0 |
| 146 | + vCol = vRow.SelEnd >= 0 and vRow.SelEnd or vRow.StringLength |
| 147 | + vStr = vStr:sub(0, vCol) .. AComment[2] .. vStr:sub(vCol+1) |
| 148 | + editor.SetString(Id, vInfo.EndLine, vStr, vRow.StringEOL) |
| 149 | + |
| 150 | + vRow = editor.GetString(Id, vInfo.StartLine) |
| 151 | + vStr = vRow.StringText |
| 152 | + vStr = vStr:sub(0, vRow.SelStart) .. AComment[1] .. vStr:sub(vRow.SelStart+1) |
| 153 | + editor.SetString(Id, vInfo.StartLine, vStr, vRow.StringEOL) |
| 154 | + |
| 155 | + editor.UndoRedo(Id, "EUR_END") |
| 156 | + |
| 157 | + if vCor then |
| 158 | + CorrectBlockWidth( AComment[2]:len() + (vInfo.StartLine == vInfo.EndLine and AComment[1]:len() or 0) ) |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + |
| 163 | + function _G.UnCommentStreamBlock(Id, AComment) |
| 164 | + local vRow, vStr, vCol, vCor |
| 165 | + local vInfo = editor.GetSelection(Id) |
| 166 | + if not vInfo then return end |
| 167 | + |
| 168 | + editor.UndoRedo(Id, "EUR_BEGIN") |
| 169 | + |
| 170 | + vRow = editor.GetString(Id, vInfo.EndLine) |
| 171 | + vCor = vRow.SelEnd >= 0 |
| 172 | + vCol = vRow.SelEnd >= 0 and vRow.SelEnd or vRow.StringLength |
| 173 | + vStr = vRow.StringText |
| 174 | + vStr = vStr:sub(0, vCol - AComment[2]:len()) .. vStr:sub(vCol + 1) |
| 175 | + editor.SetString(Id, vInfo.EndLine, vStr, vRow.StringEOL) |
| 176 | + |
| 177 | + vRow = editor.GetString(Id, vInfo.StartLine) |
| 178 | + vStr = vRow.StringText |
| 179 | + vStr = vStr:sub(0, vRow.SelStart) .. vStr:sub(vRow.SelStart + AComment[1]:len() + 1) |
| 180 | + editor.SetString(Id, vInfo.StartLine, vStr, vRow.StringEOL) |
| 181 | + |
| 182 | + editor.UndoRedo(Id, "EUR_END") |
| 183 | + |
| 184 | + if vCor then |
| 185 | + CorrectBlockWidth( -( AComment[2]:len() + (vInfo.StartLine == vInfo.EndLine and AComment[1]:len() or 0) ) ) |
| 186 | + end |
| 187 | + |
| 188 | + return true |
| 189 | + end |
| 190 | + |
| 191 | + |
| 192 | + function _G.IsBlockStreamComment(Id, AComment) |
| 193 | + local vInfo = editor.GetSelection(Id) |
| 194 | + if not vInfo then return false end |
| 195 | + |
| 196 | + local vRow1 = editor.GetString(Id, vInfo.StartLine) |
| 197 | + local vRow2 = editor.GetString(Id, vInfo.EndLine) |
| 198 | + |
| 199 | + if (vRow1.StringText:sub(vRow1.SelStart + 1, vRow1.SelStart + AComment[1]:len()) ~= AComment[1]) or |
| 200 | + (vRow2.StringText:sub(vRow2.SelEnd - AComment[2]:len() + 1, vRow2.SelEnd) ~= AComment[2]) then |
| 201 | + return false |
| 202 | + end |
| 203 | + |
| 204 | + return true |
| 205 | + end |
| 206 | + |
| 207 | + |
| 208 | + function _G.ToggleCommentStreamBlock(Id, AComment) |
| 209 | + if not _G.IsBlockStreamComment(Id, AComment) then |
| 210 | + _G.CommentStreamBlock(Id, AComment) |
| 211 | + else |
| 212 | + _G.UnCommentStreamBlock(Id, AComment) |
| 213 | + end |
| 214 | + end |
| 215 | + |
| 216 | + |
| 217 | + function _G.ToggleComment(Id, AComment) |
| 218 | + if type(AComment) == "string" then |
| 219 | + _G.ToggleCommentLineBlock(Id, AComment) |
| 220 | + else |
| 221 | + _G.ToggleCommentStreamBlock(Id, AComment) |
| 222 | + end |
| 223 | + end |
| 224 | + |
| 225 | +}} |
0 commit comments