我正在尝试编写简单的 Emacs 函数来在 C 样式和 camelCase 之间转换 id(即 c_style <-> cStyle)。但是出于某种原因,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
行为造成的。