1

Often I am involved with transforming text with one system of word separation (e.g., camelcase) into another system (e.g., hyphen separation, space separation, underscore separation. For example, if I am transforming from camelcase separation to hyphen separatio, and there's a phrase of words separated by camelcase boundaries, e.g., TimeOfDay, I will may have to change it to time-of-day, or search for time-of-day, etc. Or I might have to go back and forth, or deal with multiple systems together.

What does Emacs have in terms of packages or modes or tools for dealing with these systems. It seems like a triviality, but it's kind of pain that something I take so for granted, like Meta-F moving to forward from the beginning of "time-of-day" to the end of the word "time", is totally useless to moving past the first word in "TimeOfDay", since it would move you to the end of the word "day".

2

1 Answer 1

1

You can use the elisp functions s-upper-camel-case or s-lower-camel-case like so:

(s-lower-camel-case "camel-case")
(s-upper-camel-case "camel-case")

enter image description here enter image description here

@Tyler's comment above provides a way to un-camelize strings.

(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)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.