Automatically rewrite elisp-style variable name to camelCase/snake_case reddit / emacs ---------------------------------------------------------------------- CommanderThunderous · 2022-03-03 19:10 Is there a package which automatically and intelligently rewrites `variable-name` to `variableName` or `variable_name` depending on the current major language mode? It should be intelligent enough to account for cases where you're typing within the bounds of string quotes. The more I think about this problem the more difficult I think it would be to write such a package --- perhaps none exists. As an example, let's suppose we're writing Python (with `|` as the cursor): Basic: 1. `variable-name|` 1. `variable-name |` 1. `variable_name |` (conversion performed) In quotes: 1. `variable_name 'foo-bar|` 1. `variable_name 'foo-bar |` 1. `variable_name 'foo-bar |` (no conversion as the user is typing within quotes) In f-string: 1. `variable_name f'foo {bar-baz|` 1. `variable_name f'foo {bar-baz |` 1. `variable_name f'foo {bar_baz |` (conversion performed) ---------------------------------------------------------------------- crawsphi · 2022-03-03 19:35 I would never tell someone not to hack on their Emacs if they wanted to, but, yes: https://github.com/zk-phi/electric-case Also https://www.emacswiki.org/emacs/CamelCase, https://github.com/akicho8/string-inflection, and the recently-introduced https://gitlab.com/ideasman42/emacs-cycle-at-point might have something for you. ---------------------------------------------------------------------- ieure · 2022-03-03 20:12 Don't know if there's a package, but I wrote this ages ago and still use it fairly often: https://gist.github.com/ieure/2d8fad2252a3aba406eb6700f758e49a ---------------------------------------------------------------------- nandryshak · 2022-03-03 22:13 https://github.com/akicho8/string-inflection In order to discover this package, you could search for `case` on melpa.org and sort by # of downloads. ---------------------------------------------------------------------- nnreddit-user · 2022-03-04 20:56 This makes an interesting use of the tree-sitter integration in Commercial Emacs: src/emacs -Q \ --eval "(custom-set-variables \ (quote (font-lock-support-mode (quote tree-sitter- lock-mode))))" \ --eval "(defun camelot (beg _end old-len) \ (let ((prev (tree-sitter-node-at (max (point-min) (1- beg)))) \ (cur (tree-sitter-node-at beg))) \ (when (and (zerop old-len) \ (equal \"identifier\" (tree-sitter-node- type prev)) \ (not (equal \"identifier\" (tree-sitter- node-type cur)))) \ (let* ((start (tree-sitter-node-start prev)) \ (end (tree-sitter-node-end prev)) \ (identifier (buffer-substring-no-properties start end)) \ (rep (replace-regexp-in-string \"_\\\\(\\\\w\\\\)\" \ (lambda (m) (upcase (match-string 1 m))) \ identifier))) \ (unless (equal rep identifier) \ (save-excursion \ (goto-char start) \ (delete-char (- end start)) \ (insert rep)))))))" \ --eval "(c-mode)" \ --eval "(add-hook (quote after-change-functions) (function camelot) t t))" \ --eval "(dolist (c (split-string \"camel_this_now \" \"\")) \ (insert c) (redisplay) (sleep-for 0 200))" \ --eval "(insert \"(\")" \ --eval "(dolist (c (split-string \"\\\"dont_camel_me_bro \\\"\" \"\")) \ (insert c) (redisplay) (sleep-for 0 200))" \ --eval "(insert \")\n\")"