]> git.lizzy.rs Git - micro.git/blobdiff - runtime/plugins/comment/comment.lua
Add autoretab
[micro.git] / runtime / plugins / comment / comment.lua
index 4c3032c6088bd490e1f55d175006551a3ef0c422..bcf91c6c910f3ef8287dd9c364787af7ddeb32be 100644 (file)
@@ -35,6 +35,7 @@ ft["markdown"] = "<!-- %s -->"
 ft["nginx"] = "# %s"
 ft["nim"] = "# %s"
 ft["objc"] = "// %s"
+ft["ocaml"] = "(* %s *)"
 ft["pascal"] = "{ %s }"
 ft["perl"] = "# %s"
 ft["php"] = "// %s"
@@ -59,13 +60,11 @@ ft["zig"] = "// %s"
 ft["zscript"] = "// %s"
 ft["zsh"] = "# %s"
 
-function onBufferOpen(buf)
-    if buf.Settings["commenttype"] == nil then
-        if ft[buf.Settings["filetype"]] ~= nil then
-            buf.Settings["commenttype"] = ft[buf.Settings["filetype"]]
-        else
-            buf.Settings["commenttype"] = "# %s"
-        end
+function updateCommentType(buf)
+    if ft[buf.Settings["filetype"]] ~= nil and ft[buf.Settings["filetype"]] ~= nil then
+        buf.Settings["commenttype"] = ft[buf.Settings["filetype"]]
+    elseif buf.Settings["commenttype"] == nil then
+        buf.Settings["commenttype"] = "# %s"
     end
 end
 
@@ -78,6 +77,8 @@ function isCommented(bp, lineN, commentRegex)
 end
 
 function commentLine(bp, lineN)
+    updateCommentType(bp.Buf)
+
     local line = bp.Buf:Line(lineN)
     local commentType = bp.Buf.Settings["commenttype"]
     local sel = -bp.Cursor.CurSelection
@@ -99,6 +100,8 @@ function commentLine(bp, lineN)
 end
 
 function uncommentLine(bp, lineN, commentRegex)
+    updateCommentType(bp.Buf)
+
     local line = bp.Buf:Line(lineN)
     local commentType = bp.Buf.Settings["commenttype"]
     local sel = -bp.Cursor.CurSelection
@@ -148,6 +151,8 @@ function toggleCommentSelection(bp, startLine, endLine, commentRegex)
 end
 
 function comment(bp, args)
+    updateCommentType(bp.Buf)
+
     local commentType = bp.Buf.Settings["commenttype"]
     local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)"):gsub("%s+", "%s*")