summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Baz <git@maximbaz.com>2018-06-28 23:43:00 +0200
committerMaxim Baz <git@maximbaz.com>2018-06-28 23:47:48 +0200
commit5d689858df4d182a8ddf2188896ff23df81e2eb3 (patch)
tree12e27c9895d031a3d34e74f75291f0681d7427d1
parent43b72770ec5ef15723b0ee5c1901a39e23039de8 (diff)
Improve comment-line command
-rw-r--r--rc/core/comment.kak26
-rw-r--r--test/indent/comment/comment-multiple-lines-indented/cmd1
-rw-r--r--test/indent/comment/comment-multiple-lines-indented/in4
-rw-r--r--test/indent/comment/comment-multiple-lines-indented/out4
-rw-r--r--test/indent/comment/comment-multiple-lines-indented/rc4
-rw-r--r--test/indent/comment/comment-multiple-lines-unindented/cmd1
-rw-r--r--test/indent/comment/comment-multiple-lines-unindented/in4
-rw-r--r--test/indent/comment/comment-multiple-lines-unindented/out4
-rw-r--r--test/indent/comment/comment-multiple-lines-unindented/rc4
-rw-r--r--test/indent/comment/comment-multiple-lines-when-partially-commented/cmd1
-rw-r--r--test/indent/comment/comment-multiple-lines-when-partially-commented/in3
-rw-r--r--test/indent/comment/comment-multiple-lines-when-partially-commented/out3
-rw-r--r--test/indent/comment/comment-multiple-lines-when-partially-commented/rc4
13 files changed, 58 insertions, 5 deletions
diff --git a/rc/core/comment.kak b/rc/core/comment.kak
index 3e017ed0..433f38bd 100644
--- a/rc/core/comment.kak
+++ b/rc/core/comment.kak
@@ -150,16 +150,32 @@ define-command comment-line -docstring '(un)comment selected lines using line co
try %{
# Keep non-empty lines
execute-keys <a-K>\A\s*\z<ret>
+ } catch %{
+ }
+
+ try %{
+ set-register / "\A\Q%opt{comment_line}\E\h?"
try %{
- # Select the comment characters and remove them
- set-register / "\A\Q%opt{comment_line}\E\h*"
- execute-keys s<ret>d
- } catch %{
- # Comment the line
+ # See if there are any uncommented lines in the selection
+ execute-keys -draft <a-K><ret>
+
+ # There are uncommented lines, so comment everything
set-register '"' "%opt{comment_line} "
+ align-cursors-left
execute-keys P
+ } catch %{
+ # All lines were commented, so uncomment everything
+ execute-keys s<ret>d
}
}
}
}
+
+define-command align-cursors-left -docstring 'set all cursor (and anchor) columns to the column of the leftmost cursor' %{
+ %sh{
+ leftmost_column=$(echo "$kak_selections_desc" | tr ':' '\n' | cut -d',' -f1 | cut -d'.' -f2 | sort -n | head -n1)
+ aligned_selections=$(echo "$kak_selections_desc" | sed "s/\.[0-9]\+,/.$leftmost_column,/g")
+ echo "select $aligned_selections"
+ }
+}
diff --git a/test/indent/comment/comment-multiple-lines-indented/cmd b/test/indent/comment/comment-multiple-lines-indented/cmd
new file mode 100644
index 00000000..fbad60f2
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-indented/cmd
@@ -0,0 +1 @@
+jXXX:comment-line<ret>
diff --git a/test/indent/comment/comment-multiple-lines-indented/in b/test/indent/comment/comment-multiple-lines-indented/in
new file mode 100644
index 00000000..d81dbd16
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-indented/in
@@ -0,0 +1,4 @@
+a:
+ b: 1
+
+ c: 2
diff --git a/test/indent/comment/comment-multiple-lines-indented/out b/test/indent/comment/comment-multiple-lines-indented/out
new file mode 100644
index 00000000..46f89cd5
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-indented/out
@@ -0,0 +1,4 @@
+a:
+ # b: 1
+
+ # c: 2
diff --git a/test/indent/comment/comment-multiple-lines-indented/rc b/test/indent/comment/comment-multiple-lines-indented/rc
new file mode 100644
index 00000000..4808655d
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-indented/rc
@@ -0,0 +1,4 @@
+source "%val{runtime}/colors/default.kak"
+source "%val{runtime}/rc/base/yaml.kak"
+source "%val{runtime}/rc/core/comment.kak"
+set buffer filetype yaml
diff --git a/test/indent/comment/comment-multiple-lines-unindented/cmd b/test/indent/comment/comment-multiple-lines-unindented/cmd
new file mode 100644
index 00000000..f67b66d6
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-unindented/cmd
@@ -0,0 +1 @@
+%:comment-line<ret>
diff --git a/test/indent/comment/comment-multiple-lines-unindented/in b/test/indent/comment/comment-multiple-lines-unindented/in
new file mode 100644
index 00000000..d81dbd16
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-unindented/in
@@ -0,0 +1,4 @@
+a:
+ b: 1
+
+ c: 2
diff --git a/test/indent/comment/comment-multiple-lines-unindented/out b/test/indent/comment/comment-multiple-lines-unindented/out
new file mode 100644
index 00000000..9405433d
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-unindented/out
@@ -0,0 +1,4 @@
+# a:
+# b: 1
+
+# c: 2
diff --git a/test/indent/comment/comment-multiple-lines-unindented/rc b/test/indent/comment/comment-multiple-lines-unindented/rc
new file mode 100644
index 00000000..4808655d
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-unindented/rc
@@ -0,0 +1,4 @@
+source "%val{runtime}/colors/default.kak"
+source "%val{runtime}/rc/base/yaml.kak"
+source "%val{runtime}/rc/core/comment.kak"
+set buffer filetype yaml
diff --git a/test/indent/comment/comment-multiple-lines-when-partially-commented/cmd b/test/indent/comment/comment-multiple-lines-when-partially-commented/cmd
new file mode 100644
index 00000000..f67b66d6
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-when-partially-commented/cmd
@@ -0,0 +1 @@
+%:comment-line<ret>
diff --git a/test/indent/comment/comment-multiple-lines-when-partially-commented/in b/test/indent/comment/comment-multiple-lines-when-partially-commented/in
new file mode 100644
index 00000000..e048df42
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-when-partially-commented/in
@@ -0,0 +1,3 @@
+a:
+ # b: 1
+ c: 2
diff --git a/test/indent/comment/comment-multiple-lines-when-partially-commented/out b/test/indent/comment/comment-multiple-lines-when-partially-commented/out
new file mode 100644
index 00000000..f4bed302
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-when-partially-commented/out
@@ -0,0 +1,3 @@
+# a:
+# # b: 1
+# c: 2
diff --git a/test/indent/comment/comment-multiple-lines-when-partially-commented/rc b/test/indent/comment/comment-multiple-lines-when-partially-commented/rc
new file mode 100644
index 00000000..4808655d
--- /dev/null
+++ b/test/indent/comment/comment-multiple-lines-when-partially-commented/rc
@@ -0,0 +1,4 @@
+source "%val{runtime}/colors/default.kak"
+source "%val{runtime}/rc/base/yaml.kak"
+source "%val{runtime}/rc/core/comment.kak"
+set buffer filetype yaml