-
-
Save wu-lee/b266fedfa97093847958 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; FIXME work in progress | |
;; ---------------------------------------------------------------------- | |
;; Gets a cons cell indicating the bounds of the current region (if set) or word | |
(defun bounds-of-word-or-region () | |
(if (and transient-mark-mode mark-active) | |
(cons (region-beginning) (region-end)) | |
(bounds-of-thing-at-point 'symbol))) | |
;; ---------------------------------------------------------------------- | |
(defun camelcase-region (start end &optional upcase) | |
"Changes region from snake_case to camelCase (or CamelCase is upcase is t)" | |
(interactive "r") | |
(save-excursion | |
(when upcase | |
(goto-char start) | |
(while (re-search-forward "\\([^[:alnum:]_]\\)\\([[:lower:]]\\)" nil t) | |
(replace-match (concat (match-string 1) (upcase (match-string 2)))))) | |
(goto-char start) | |
(while (re-search-forward "_\\(.\\)" end t) | |
(replace-match (upcase (match-string 1))) | |
(setq end (1- end))))) | |
;; ---------------------------------------------------------------------- | |
;; Convert region (if set) or word at point to camel-case | |
(defun camelcase-word-or-region (&optional upcase) | |
"Changes word or region from snake_case to camelCase (or CamelCase if upcase is t)" | |
(interactive "P") | |
(let ((bounds (bounds-of-word-or-region))) | |
(camelcase-region (car bounds) (cdr bounds) upcase))) | |
;; ---------------------------------------------------------------------- | |
;; snakecase-region | |
(defun snakecase-region (start end &optional upcase) | |
"Changes region from camelCase to snake_case (or SNAKE_CASE if upcase is t)" | |
(interactive "rP") | |
(save-excursion | |
(let ((case-fold-search nil) | |
(last-case t)) | |
(when upcase | |
; uppercase word endings | |
(goto-char start) | |
(while (re-search-forward "\\([[:lower:][:digit:]_]+\\)\\([^[::]_]\\)" nil t) | |
(replace-match (concat (upcase (match-string 1)) (match-string 2))))) | |
; insert underscores between lower->uppercase transitions | |
(goto-char start) | |
(while (re-search-forward "\\([^[:upper:]]+\\)\\([[:upper:]]\\)" end t) | |
(replace-match (if upcase | |
(upcase (concat (match-string 1) "_" (match-string 2))) | |
(downcase (concat (match-string 1) "_" (match-string 2))))) | |
(setq end (1+ end)))))) | |
;; ---------------------------------------------------------------------- | |
(defun snakecase-word-or-region (&optional upcase) | |
"Changes word or region from camelCase to snake_case (or SNAKE_CASE if upcase is t)" | |
(interactive "P") | |
(let ((bounds (bounds-of-word-or-region))) | |
;;; Suggested key bindings | |
;;; (global-set-key (kbd "C-c C--") 'camelcase-word-or-region) | |
;;; (global-set-key (kbd "C-c C-_") 'snakecase-word-or-region) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment