That funky naming scheme where the first letter of every word is capitalized and all the words are joined together without spaces.
This scheme is also called StudlyCaps or “intercapping” by some people. GlassesMode is the StudlyCaps specific minor-mode that comes with Emacs.
For those runnning an up-to-date version of Emacs (22.1 or newer) an alternative to both CamelCase and GlassesMode is already built in c-mode. Just activate the c-subword-mode (subword-mode since 23.2) and it works out of the box. As usual you can activate this minor mode manually, typing (M-x c-subword-mode) or programmatically (using hooks). It also works in non-CC Mode buffers. Enjoy!
⇒>the references below all seem to be out of date. If anyone still has a copy…. I have to use this stuff.
⇒> The Wayback machine ( web.archive.org ) has them.
There’s a minor mode for CamelCase (M-x camelCase-mode), written by C. R. Manning, that allows for forward-word and backward-word to stop at each word within a CamelCase word. It’s now available from http://www.eecs.ucf.edu/~leavens/emacs/camelCase/camelCase-mode.el with instructions and commentary at http://www.eecs.ucf.edu/~leavens/emacs/camelCase/camelCase-mode.html
And http://www.sixfingeredman.net/proj/xemacs/camelCase-mode.el is a hacked version which actually replaces forward-word and backward-word so that all word-related functions work with camelCase (when the mode is on).
< ;;; camelCase.el --- quasi-minor mode for editing with camelCase words --- > ;;; camelCase-mode.el --- minor mode for editing with camelCase words 86c86,90 < (defvar camelCase-mode nil) --- > (defvar camelCase-modeline-indicator " camelCase" > "call (camelCase-install-mode) again if this is changed") > (defvar camelCase-mode nil) > (make-variable-buffer-local 'camelCase-mode) > (put 'camelCase-mode 'permanent-local t) 89c93 < "Quasi-minor mode which overrides word command keys for editing camelCase words. --- > "Minor mode which overrides word command keys for editing camelCase words. 108c112 < (* means only in Gnu Emacs, not in XEmacs; the original binding is not --- > (* means only in Gnu Emacs, not in XEMacs; the original binding is not 115c119,120 < (> (prefix-numeric-value arg) 0)))) --- > (> (prefix-numeric-value arg) 0))) > (force-mode-line-update)) 189c194 < nil --- > camelCase-modeline-indicator
--- camelCase-mode.el.orig 2007-03-16 23:08:39.078125000 +0300 +++ camelCase-mode.el 2007-03-16 23:07:01.640625000 +0300 @@ -142,7 +142,7 @@ "move point foward COUNT camelCase words" (interactive "p") (if (not camelCase-mode) - (camelCase-old-forward-word count buffer) + (camelCase-old-forward-word count) ;; search forward increments point until some match occurs; ;; extent of match is as large as possible at that point. ;; point is left at END of match. @@ -172,7 +172,7 @@ "move point backward COUNT camelCase words" (interactive "p") (if (not camelCase-mode) - (camelCase-old-backward-word count buffer) + (camelCase-old-backward-word count) ;; search backward decrements point until some match occurs; ;; extent of match is as large as possible at that point. ;; So once point is found, have to keep decrementing point until we cross
--ivan4th
For cycling text between various styles (CamelCase, snake_case, lowerCamelCase, etc) use the string-inflection package (available in MELPA, or at https://github.com/akicho8/string-inflection).
;; Cycle between snake case, camel case, etc. (require 'string-inflection) (global-set-key (kbd "C-c i") 'string-inflection-cycle) (global-set-key (kbd "C-c C") 'string-inflection-camelcase) ;; Force to CamelCase (global-set-key (kbd "C-c L") 'string-inflection-lower-camelcase) ;; Force to lowerCamelCase (global-set-key (kbd "C-c J") 'string-inflection-java-style-cycle) ;; Cycle through Java styles
This makes the suggestions below obsolete.
Sample EmacsLisp code to un-CamelCase a string (from http://www.friendsnippets.com/snippet/101/):
(defun un-camelcase-string (s &optional sep start) "Convert CamelCase string S to lower case with word separator SEP. Default for SEP is a hyphen \"-\".
If third argument START is non-nil, convert words after that index in STRING." (let ((case-fold-search nil)) (while (string-match "[A-Z]" s (or start 1)) (setq s (replace-match (concat (or sep "-") (downcase (match-string 0 s))) t nil s))) (downcase s)))
Sample EmacsLisp code to convert a string from underscore → CamelCase:
(defun mapcar-head (fn-head fn-rest list) "Like MAPCAR, but applies a different function to the first element." (if list (cons (funcall fn-head (car list)) (mapcar fn-rest (cdr list)))))
(defun camelize (s) "Convert under_score string S to CamelCase string." (mapconcat 'identity (mapcar '(lambda (word) (capitalize (downcase word))) (split-string s "_")) ""))
(defun camelize-method (s) "Convert under_score string S to camelCase string." (mapconcat 'identity (mapcar-head '(lambda (word) (downcase word)) '(lambda (word) (capitalize (downcase word))) (split-string s "_")) ""))
Has anyone else seen a problem with http://www.sixfingeredman.net/proj/xemacs/camelCase-mode.el where M-f and M-b work as expected, but C-left uses the original forward-word logic? C-right is fine for some reason. Overall this mode is very neat and helpful, thanks to all who have worked on it.
Ok, here’s my cycling variant:
(defun split-name (s) (split-string (let ((case-fold-search nil)) (downcase (replace-regexp-in-string "\\([a-z]\\)\\([A-Z]\\)" "\\1 \\2" s))) "[^A-Za-z0-9]+"))
(defun camelcase (s) (mapconcat 'capitalize (split-name s) "")) (defun underscore (s) (mapconcat 'downcase (split-name s) "_")) (defun dasherize (s) (mapconcat 'downcase (split-name s) "-")) (defun colonize (s) (mapconcat 'capitalize (split-name s) "::"))
(defun camelscore (s) (cond ((string-match-p "\\(?:[a-z]+_\\)+[a-z]+" s) (dasherize s)) ((string-match-p "\\(?:[a-z]+-\\)+[a-z]+" s) (camelcase s)) ((string-match-p "\\(?:[A-Z][a-z]+\\)+$" s) (colonize s)) (t (underscore s)) ))
(defun camelscore-word-at-point () (interactive) (let* ((case-fold-search nil) (beg (and (skip-chars-backward "[:alnum:]:_-") (point))) (end (and (skip-chars-forward "[:alnum:]:_-") (point))) (txt (buffer-substring beg end)) (cml (camelscore txt)) ) (if cml (progn (delete-region beg end) (insert cml))) ))
The cycling method above did not work for me, but I was able to redefine camelscore as:
(defun camelscore (s) (cond ((string-match-p "\:" s) (camelcase s)) ((string-match-p "-" s) (colonize s)) ((string-match-p "_" s) (dasherize s)) (t (underscore s)) ))
Bind the following to some-key, write as snake case, then press the key!
Dependency: camelize
(defun camelize-previous-snake (&optional beg end) "Camelize the previous snake cased string. If transient-mark-mode is active and a region is activated, camelize the region." (interactive "r") (unless (and (boundp 'transient-mark-mode) transient-mark-mode mark-active) (setq end (point) beg (+ (point) (skip-chars-backward "[:alnum:]_")))) (save-excursion (let ((c (camelize (buffer-substring-no-properties beg end)))) (delete-region beg end) (goto-char beg) (insert c))))
– tomykaira
— To check spelling of camel-cased words, you need to switch the default ispell program to aspell.
(setq ispell-program-name "aspell") ;; -C makes aspell accept run-together words ;; --run-together-limit is maximum number of words that can be strung together. (setq ispell-extra-args '("-C" "--sug-mode=ultra" "--run-together-limit=5"))
— A better solution to check spelling of camel-cased words is https://github.com/redguardtoo/wucuo
It supports both aspell and hunspell. No extra setup required. Just works out of box.