Created
April 2, 2012 14:28
-
-
Save takumikinjo/2283840 to your computer and use it in GitHub Desktop.
snake-case/camel-case elisp functions
This file contains 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
;; Usage: | |
;; | |
;; (defun snake-camel () (interactive)(kill-new(upcase-first(snake-to-camel(car kill-ring))))) | |
;; (defun camel-snake () (interactive)(kill-new(upcase(camel-to-snake(car kill-ring))))) | |
(defun snake-to-camel (s) | |
"" | |
(setq s (downcase s)) | |
(if (string-match "_" s) | |
(concat (let ((fs (substring s 0 (match-beginning 0)))) | |
(concat (upcase (substring fs 0 1)) (substring fs 1 (length fs)))) | |
(snake-to-camel (substring s (match-end 0) (length s)))) | |
(concat (upcase (substring s 0 1)) (substring s 1 (length s))))) | |
(defun camel-to-snake (s) | |
"" | |
(save-match-data | |
(let ((case-fold-search nil)) | |
(camel-to-snake-recursively s)))) | |
(defun camel-to-snake-recursively (s) | |
"" | |
(let ((ss (downcase-first s))) | |
(if (string-match "[A-Z]" ss) | |
(concat (substring ss 0 (match-beginning 0)) "_" (camel-to-snake-recursively (substring ss (match-beginning 0) (length ss)))) | |
ss))) | |
(defun upcase-first (s) | |
"" | |
(concat | |
(upcase (substring s 0 1)) | |
(substring s 1 (length s)))) | |
(defun downcase-first (s) | |
"" | |
(concat | |
(downcase (substring s 0 1)) | |
(substring s 1 (length s)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment