C スタイルのものとキャメルケースのもの (つまり、c_style <-> cStyle) の間で ID を変換する単純な Emacs 関数を作成しようとしています。しかし、何らかの理由で、Emacs の組み込みdowncase
関数は単語をそのまま残します。M-x downcase-word
正常に動作するので、完全に負けました。どんなアイデアでも大歓迎です。
(defun toggle-id-style ()
"Toggle between C-style ids and camel Case ones (i.e. c_style_id -> cStyleId and back)."
(interactive)
(save-excursion
(progn
(re-search-forward "[^A-Za-z0-9_]" nil t)
(let ((end (point))
(case-fold-search nil))
(progn
(re-search-backward "[^A-Za-z0-9_]" nil t)
(let* ((cstyle (if (string-match "_" (buffer-substring-no-properties (point) end)) t nil))
(regexp (if cstyle "_\\(\\w+\\)" "\\([A-Z][a-z0-9]+\\)") )
(func (if cstyle 'capitalize (lambda (s) (concat "_" (downcase s) ) ))))
(progn
(while (re-search-forward regexp end t)
(replace-match (funcall func (match-string 1)) nil nil)))))))))
;;M-x replace-regexp _\(\w+\) -> \,(capitalize \1) ;; c_style -> cStyle
;;M-x replace-regexp \([A-Z][a-z0-9]+\) -> _\,(downcase \1) ;;cStyle -> c_style
変換すると問題なく動作しますc_style
が、変換しようとするcStyle
と結果が得c_Style
られます。はい、これがdowncase
動作によるものであることを確認しました。