snippets / emacs

All snippets tagged emacs (1)

  1. Convert CamelCase string to lowercase with specified separator.

    This is Emacs LISP code. Only tested on GNU Emacs 22.1, but should work on almost any Emacs version.

     1 (defun un-camelcase-string (s &optional sep start)
    2 "Convert CamelCase string S to lower case with word separator SEP.
    3 Default for SEP is a hyphen \"-\".
    4
    5 If third argument START is non-nil, convert words after that
    6 index in STRING."
    7 (let ((case-fold-search nil))
    8 (while (string-match "[A-Z]" s (or start 1))
    9 (setq s (replace-match (concat (or sep "-")
    10 (downcase (match-string 0 s)))
    11 t nil s)))
    12 (downcase s)))
    13
    14 (defun un-camelcase-string-recursively (s &optional sep start)
    15 "Convert CamelCase string S to lower case with word separator SEP.
    16 Default for SEP is a hyphen '-'.
    17
    18 If third argument START is non-nil, convert words after that
    19 index in STRING.
    20
    21 This is the same as `uncamelcase-string', but using a tail-recursive algorithm."
    22 (let ((case-fold-search nil))
    23 (if (not (string-match "[A-Z]" s (or start 1)))
    24 (downcase s)
    25 ;; substitute one match, then recurse
    26 (un-camelcase-string-recursively (replace-match (concat (or sep "-")
    27 (downcase (match-string 0 s)))
    28 t nil s)
    29 (or sep "-")
    30 (match-end 0)))))
    Posted by rmurri to scheme emacs lisp ... saved by 1 person ... 0 comments ... 1 year, 1 month
showing 10, 25, 50 items per pages

Pages : 1

Flux RSS friendsnippetLatest snippets


More...