« get me outta code hell

st4: hsmusic: bold 2.0, line break - dotfiles - Miscellaneous configuration files of my personal use
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-01-20 10:18:21 -0400
committer(quasar) nebula <qznebula@protonmail.com>2025-01-20 10:24:22 -0400
commitc45d2cd07a8f02e7ae6a43963e17fd72a9100e95 (patch)
treeabd281c827822e81ca9200a7d9190562e11c285c
parentce07094014be6cfdba4d1b6ad335d21ca7856486 (diff)
st4: hsmusic: bold 2.0, line break
this implementation from cc101
-rw-r--r--apps/Sublime Text 4/Packages/User/Default (OSX).sublime-keymap15
-rw-r--r--apps/Sublime Text 4/Packages/User/HSMusic Editing.py40
2 files changed, 44 insertions, 11 deletions
diff --git a/apps/Sublime Text 4/Packages/User/Default (OSX).sublime-keymap b/apps/Sublime Text 4/Packages/User/Default (OSX).sublime-keymap
index bcf66c0..7426757 100644
--- a/apps/Sublime Text 4/Packages/User/Default (OSX).sublime-keymap
+++ b/apps/Sublime Text 4/Packages/User/Default (OSX).sublime-keymap
@@ -52,9 +52,20 @@
 	},
 
 	{ "keys": ["super+b"],
-		"command": "wrap_text_bold",
+		"command": "bold_wiki_text",
 		"context": [
-			{ "key": "project_name", "operator": "regex_contains", "operand": "HSMusic" }
+			{ "key": "project_name", "operator": "regex_contains", "operand": "HSMusic" },
+			{ "key": "panel_has_focus", "operator": "not_equal" },
+			{ "key": "overlay_has_focus", "operator": "not_equal" },
+		],
+	},
+
+	{ "keys": ["super+enter"],
+		"command": "insert_line_break_in_wiki_text",
+		"context": [
+			{ "key": "project_name", "operator": "regex_contains", "operand": "HSMusic" },
+			{ "key": "panel_has_focus", "operator": "not_equal" },
+			{ "key": "overlay_has_focus", "operator": "not_equal" },
 		],
 	},
 ]
diff --git a/apps/Sublime Text 4/Packages/User/HSMusic Editing.py b/apps/Sublime Text 4/Packages/User/HSMusic Editing.py
index 6daa032..41c6408 100644
--- a/apps/Sublime Text 4/Packages/User/HSMusic Editing.py
+++ b/apps/Sublime Text 4/Packages/User/HSMusic Editing.py
@@ -174,10 +174,20 @@ def _match_tag_in_progress(view, region):
   if match:
     return match.group(1)
 
-def _match_rest_of_tag_in_progress(view, region):
+def _match_regex_ahead_same_line(view, region, regex):
   region = _normalize_region(region)
   to_end = sublime.Region(region.a, view.line(region).b)
-  match = re.search('^((?:(?!\[\[).)*]])', view.substr(to_end))
+  return re.search(regex, view.substr(to_end))
+
+def _match_rest_of_tag_in_progress(view, region):
+  regex = '^((?:(?!\[\[).)*]])'
+  match = _match_regex_ahead_same_line(view, region, regex)
+  if match:
+    return match.group(1)
+
+def _match_through_closing_html_tag(view, region, tag):
+  regex = f'^(.*?</{tag}>)'
+  match = _match_regex_ahead_same_line(view, region, regex)
   if match:
     return match.group(1)
 
@@ -241,13 +251,6 @@ class EnterExitWikiTagCommand(_CursorAdaptiveCommand):
     else:
       return ('wrap', '[[', ']]', 'left')
 
-class WrapTextBoldCommand(_CursorAdaptiveCommand):
-  def handle_region(self, region):
-    if region.a == region.b:
-      return ('insert', 'right', '<b></b>', 4)
-    else:
-      return ('wrap', '<b>', '</b>', 'right')
-
 class ExitWikiTagCommand(_CursorAdaptiveCommand):
   def handle_region(self, region):
     rest_of_tag = _match_rest_of_tag_in_progress(self.view, region)
@@ -298,3 +301,22 @@ class SpaceInWikiTagCommand(_CursorAdaptiveCommand):
 
   def no_action_fallback(self, edit):
     self.view.run_command('insert', {'characters': ' '})
+
+class BoldWikiTextCommand(_CursorAdaptiveCommand):
+  def handle_region(self, region):
+    rest_of_tag = _match_through_closing_html_tag(self.view, region, 'b')
+    if rest_of_tag:
+      return ('move', len(rest_of_tag))
+    elif region.a == region.b:
+      return ('insert', 'right', '<b></b>', 4)
+    else:
+      return ('wrap', '<b>', '</b>', 'left')
+
+class InsertLineBreakInWikiTextCommand(_CursorAdaptiveCommand):
+  def handle_region(self, region):
+    region = _normalize_region(region)
+    to_end = sublime.Region(region.a, self.view.line(region).b)
+    if region.a == to_end.b:
+      return ('insert', 'right', '<br>', 0)
+    else:
+      return ('move', len(self.view.substr(to_end)))