Skip to content
Browse files

convert-case textmate command (extract to own plugin?)

  • Loading branch information...
1 parent 6a7c24d commit b8010d830707bd54c44d84c4b156019b3a77807e @timcharper timcharper committed Dec 2, 2010
Showing with 72 additions and 0 deletions.
  1. +72 −0 initializers.available/textmate.el
View
72 initializers.available/textmate.el
@@ -193,3 +193,75 @@
(global-set-key (kbd "<s-left>") 'beginning-of-line)
(global-set-key (kbd "<s-right>") 'end-of-line)
+
+;; CamelCase related behavior
+
+(defun textmate-case/camelCase-p (word)
+ (let (case-fold-search)
+ (and (not (string-match "_" word))
+ (not (string-match "^[A-Z]" word)))))
+
+(defun textmate-case/snake_case-p (word)
+ (string-match "_" word))
+
+(defun textmate-case/PascalCase-p (word)
+ (let (case-fold-search)
+ (and (not (string-match "_" word))
+ (string-match "^[A-Z]" word))))
+
+(defun textmate-case/word-pieces (word)
+ "returns a list of the pieces of a word, separated by snake-case or camel-case boundaries"
+ (with-temp-buffer
+ (insert-string word)
+ (goto-char 0)
+ (let (case-fold-search
+ (pieces '())
+ (accumulate-piece (lambda ()
+ (setq pieces (append pieces (list (filter-buffer-substring 1 (point) t)))))))
+ (while (search-forward-regexp "_\\|[a-z][A-Z]" nil t)
+ (if (looking-back "_")
+ (backward-delete-char 1)
+ (backward-char))
+ (funcall accumulate-piece))
+
+ (goto-char (point-max))
+ (funcall accumulate-piece)
+
+ pieces)))
+
+(defun textmate-case/convert-case (case-format word)
+ "convert provided word to camelCase and return as string"
+ (with-temp-buffer
+ (let ((word-pieces (textmate-case/word-pieces word)))
+ (if (or (equal 'camelCase case-format)
+ (equal 'PascalCase case-format))
+ (progn
+ (mapcar (lambda (piece)
+ (save-excursion (insert piece))
+ (capitalize-word 1))
+ word-pieces)
+ (if (equal 'camelCase case-format) (downcase-region (point-min) (1+ (point-min)))))
+ (progn
+ (insert (mapconcat 'identity word-pieces "_"))
+ (downcase-region (point-min) (point-max)))))
+ (buffer-string)))
+
+
+(defconst textmate-case/boundary-regex "[^a-zA-Z0-9_]")
+
+(defun textmate-case/toggle (arg)
+ "Toggles between camelCase, PascalCase, and snake_case"
+ (interactive "p")
+ (let* ((start (progn (search-backward-regexp textmate-case/boundary-regex) (forward-char) (point)))
+ (end (progn (search-forward-regexp textmate-case/boundary-regex) (backward-char) (point)))
+ (word (filter-buffer-substring start end t))
+ (target-case-format (cond ((textmate-case/snake_case-p word) 'camelCase)
+ ((textmate-case/PascalCase-p word) 'snake_case)
+ ((textmate-case/camelCase-p word) 'PascalCase))))
+ (insert
+ (textmate-case/convert-case target-case-format
+ word)))
+ (if (or (= arg -1) (= arg 2)) (textmate-case/toggle 1)))
+
+(define-key *textmate-mode-map* (kbd "C-c t _") 'textmate-case/toggle)
+(define-key *textmate-mode-map* (kbd "C-c t -") 'textmate-case/toggle)

0 comments on commit b8010d8

Please sign in to comment.
Something went wrong with that request. Please try again.