Ver código fonte

Implement Case Conversion and Commenting (M-u, M-l, M-c, M-;)

- Add case conversion commands to init.lua
- Add comment-dwim command with smart toggling
- Add helper functions for pattern escaping
Bernardo Magri 1 mês atrás
pai
commit
dc9e0b0146
1 arquivos alterados com 163 adições e 0 exclusões
  1. 163 0
      init.lua

+ 163 - 0
init.lua

@@ -790,6 +790,169 @@ bind_key("C-x C-b", function()
     message(string.format("Buffer list (%d buffers)", #buffer_info))
 end)
 
+-- ============================================================================
+-- CASE CONVERSION (M-u, M-l, M-c)
+-- ============================================================================
+
+-- M-u (upcase-word)
+bind_key("M-u", function()
+    local start_pos = editor.cursor
+    editor:move_forward_word()
+    local end_pos = editor.cursor
+    
+    local range = lumacs.Range(start_pos, end_pos)
+    local text = editor.buffer:get_text_in_range(range)
+    
+    if #text > 0 then
+        editor.buffer:replace(range, string.upper(text))
+    end
+end)
+
+-- M-l (downcase-word)
+bind_key("M-l", function()
+    local start_pos = editor.cursor
+    editor:move_forward_word()
+    local end_pos = editor.cursor
+    
+    local range = lumacs.Range(start_pos, end_pos)
+    local text = editor.buffer:get_text_in_range(range)
+    
+    if #text > 0 then
+        editor.buffer:replace(range, string.lower(text))
+    end
+end)
+
+-- M-c (capitalize-word)
+bind_key("M-c", function()
+    local start_pos = editor.cursor
+    editor:move_forward_word()
+    local end_pos = editor.cursor
+    
+    local range = lumacs.Range(start_pos, end_pos)
+    local text = editor.buffer:get_text_in_range(range)
+    
+    if #text > 0 then
+        local cap_text = text:sub(1,1):upper() .. text:sub(2):lower()
+        editor.buffer:replace(range, cap_text)
+    end
+end)
+
+-- C-x C-u (upcase-region)
+bind_key("C-x C-u", function()
+    local range = editor.buffer:get_region(editor.cursor)
+    if not range then
+        message("No active region")
+        return
+    end
+    
+    local text = editor.buffer:get_text_in_range(range)
+    if #text > 0 then
+        editor.buffer:replace(range, string.upper(text))
+        editor.buffer:deactivate_mark()
+    end
+end)
+
+-- C-x C-l (downcase-region)
+bind_key("C-x C-l", function()
+    local range = editor.buffer:get_region(editor.cursor)
+    if not range then
+        message("No active region")
+        return
+    end
+    
+    local text = editor.buffer:get_text_in_range(range)
+    if #text > 0 then
+        editor.buffer:replace(range, string.lower(text))
+        editor.buffer:deactivate_mark()
+    end
+end)
+
+-- ============================================================================
+-- COMMENTING (M-;)
+-- ============================================================================
+
+function escape_pattern(text)
+    return text:gsub("([^%w])", "%%%1")
+end
+
+function comment_dwim()
+    local mode_name = current_major_mode()
+    local mode = major_modes[mode_name]
+    local prefix = mode and mode.comment_syntax or "--"
+    local escaped_prefix = escape_pattern(prefix)
+    
+    local range = editor.buffer:get_region(editor.cursor)
+    
+    if range then
+        -- Region handling
+        local start_line = range.start.line
+        local end_line = range.end.line
+        
+        -- Check if all lines are commented
+        local all_commented = true
+        for i = start_line, end_line do
+            local line = editor.buffer:line(i)
+            -- Ignore empty lines for decision
+            if #line > 0 and not string.match(line, "^%s*" .. escaped_prefix) then
+                all_commented = false
+                break
+            end
+        end
+        
+        -- Apply change
+        for i = start_line, end_line do
+            local line = editor.buffer:line(i)
+            local line_range = lumacs.Range(lumacs.Position(i, 0), lumacs.Position(i, #line))
+            
+            if #line > 0 then
+                if all_commented then
+                    -- Uncomment
+                    local s, e = string.find(line, escaped_prefix)
+                    if s then
+                        local suffix = line:sub(e+1)
+                        if suffix:sub(1,1) == " " then suffix = suffix:sub(2) end
+                        local new_line = line:sub(1, s-1) .. suffix
+                        editor.buffer:replace(line_range, new_line)
+                    end
+                else
+                    -- Comment (if not already commented)
+                    if not string.match(line, "^%s*" .. escaped_prefix) then
+                        local indent = string.match(line, "^%s*")
+                        local content = line:sub(#indent + 1)
+                        local new_line = indent .. prefix .. " " .. content
+                        editor.buffer:replace(line_range, new_line)
+                    end
+                end
+            end
+        end
+        
+    else
+        -- Single line
+        local line_num = editor.cursor.line
+        local line = editor.buffer:line(line_num)
+        local line_range = lumacs.Range(lumacs.Position(line_num, 0), lumacs.Position(line_num, #line))
+        
+        if string.match(line, "^%s*" .. escaped_prefix) then
+            -- Uncomment
+            local s, e = string.find(line, escaped_prefix)
+            if s then
+                local suffix = line:sub(e+1)
+                if suffix:sub(1,1) == " " then suffix = suffix:sub(2) end
+                local new_line = line:sub(1, s-1) .. suffix
+                editor.buffer:replace(line_range, new_line)
+            end
+        else
+            -- Comment
+            local indent = string.match(line, "^%s*")
+            local content = line:sub(#indent + 1)
+            local new_line = indent .. prefix .. " " .. content
+            editor.buffer:replace(line_range, new_line)
+        end
+    end
+end
+
+bind_key("M-;", comment_dwim)
+
 -- Test control keys (ncurses versions)
 bind_key("C-k", function()
     message("C-k pressed! (Control key working with ncurses)")