From 264720be8ea9bde2ac8c8f40aa1708fecd9051d0 Mon Sep 17 00:00:00 2001 From: Howard Abrams Date: Wed, 5 Jul 2023 10:18:18 -0700 Subject: [PATCH] Wrote functions to change from camelCase to snake_case. Need to also do something for dashed case (Lisp case?). --- ha-config.org | 2 +- ha-programming.org | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/ha-config.org b/ha-config.org index 4408a96..468aa47 100644 --- a/ha-config.org +++ b/ha-config.org @@ -562,7 +562,7 @@ Can’t remember all the shortcuts on the ~g~ key, and =which-key= displays the ;; These go into operator mode, so the key sequence, g U i o ;; upper cases the symbol at point: "g u" '("downcase" . evil-downcase) - "g U" '("upcase" . evil-case) + "g U" '("upcase" . evil-upcase) "g ~" '("invert case" . evil-invert-case) ;; Use this ALL the time: diff --git a/ha-programming.org b/ha-programming.org index 6f1daca..6b5718b 100644 --- a/ha-programming.org +++ b/ha-programming.org @@ -557,6 +557,63 @@ While there are language-specific ways to rename variables and functions, [[http :config (ha-leader "s e" '("iedit" . iedit-mode))) #+end_src +** Case Conversion +Sure we can upper– and lower–case stuff, but can we change from Camel to Snake case and back? For instance, =fooBarBaz=, would with ~z s~ keybinding, change to =foo_bar_baz= (and repeating that sequence would return it to =fooBarBaz=). + +#+begin_src emacs-lisp + (defun get-bounds-of-symbol () + "Return a list of the start and end of the current symbol at point. + Return the start and end of region, if the region is active." + (if (region-active-p) + (cons (region-beginning) (region-end)) + (bounds-of-thing-at-point 'symbol))) + + (defun camel-to-snake-case () + "Convert the active region or current symbol to snake case. + For instance, fooBarBaz will be foo_bar_baz." + (interactive) + (cl-destructuring-bind (start . end) (get-bounds-of-symbol) + (save-excursion + (goto-char start) + (narrow-to-region start end) + (let ((case-fold-search nil)) + (while (re-search-forward (rx (group (one-or-more upper))) nil t) + (replace-match (concat "_" (downcase (match-string 1))) t nil))) + (widen)))) + + (defun snake-to-camel-case () + "Convert the active region or current symbol to camel case. + For instance, foo_bar_baz will be fooBarBaz." + (interactive) + (cl-destructuring-bind (start . end) (get-bounds-of-symbol) + (save-excursion + (goto-char start) + (narrow-to-region start end) + (let ((case-fold-search nil)) + (while (re-search-forward (rx "_" (group alpha)) nil t) + (replace-match (upcase (match-string 1)) t nil))) + (widen)))) + + (defun toggle-snake-to-camel-case () + "Toggle current symbol from snake to camel, and visa-versa." + (interactive) + (cl-destructuring-bind (start . end) (get-bounds-of-symbol) + (save-excursion + (goto-char start) + (if (looking-at (rx (zero-or-more alphanumeric) "_")) + (snake-to-camel-case) + (camel-to-snake-case))))) +#+end_src +And we can put this on the ~z~ keybinding: +#+begin_src emacs-lisp + (use-package evil + :general + (:states '(normal visual motion operator) + "z s" '("toggle snake/camel" . toggle-snake-to-camel-case) + "z S" '("to snake case" . camel-to-snake-case) + "z C" '("to camel case" . snake-to-camel-case))) +#+end_src +I would like to have this bound on the ~g~ sequence, but that is crowded. ** Commenting I like =comment-dwim= (~M-;~), and I like =comment-box=, but I have an odd personal style that I like to codify: