Replace elisp函数将字符串大小写从camel更改为upcase snake大小写

Replace elisp函数将字符串大小写从camel更改为upcase snake大小写,replace,emacs,uppercase,Replace,Emacs,Uppercase,我花了3个小时试图找出在不同情况下修改字符串的方法,例如isFailedUpgrade到isFailedUpgrade 我已经到了可以在vartext处获取字符串的地步,但不知道如何将字符串text更新为所需的大小写 (defun change-case () (interactive) (let* ((bounds (if (use-region-p) (cons (region-beginning) (region-end))

我花了3个小时试图找出在不同情况下修改字符串的方法,例如
isFailedUpgrade
isFailedUpgrade

我已经到了可以在var
text
处获取字符串的地步,但不知道如何将字符串
text
更新为所需的大小写

(defun change-case ()
  (interactive)
  (let* ((bounds (if (use-region-p)
                     (cons (region-beginning) (region-end))
                   (bounds-of-thing-at-point 'symbol)))
         (text   (buffer-substring-no-properties (car bounds) (cdr bounds))))
    (when bounds
      (delete-region (car bounds) (cdr bounds))
      (insert (change-case-helper text)))))

# the following code is rubbish 
(defun change-case-helper (text)
  (let ((output ""))
    (dotimes (i (length text))
      (concat output (char-to-string (aref text i))))
    output))
因为我自己正在学习一点emacs函数,所以我更喜欢自己编写这个函数,而不是使用现有的魔法函数


好了,再过两个小时,我想我已经明白了:

(defun change-case ()
  (interactive)
  (let* ((bounds (if (use-region-p)
                     (cons (region-beginning) (region-end))
                   (bounds-of-thing-at-point 'symbol)))
         (text   (buffer-substring-no-properties (car bounds) (cdr bounds))))
    (when bounds
      (delete-region (car bounds) (cdr bounds))
      (insert (change-case-helper text)))))

(defun change-case-helper (text)
  (when (and text (> (length text) 0))
    (let ((first-char (string-to-char (substring text 0 1)))
          (rest-str (substring text 1)))
      (concat (if (upcasep first-char) (string ?_ first-char) (string (upcase first-char)))
              (change-case-helper rest-str))))
  )

(defun upcasep (c) (and (= ?w (char-syntax c)) (= c (upcase c))))

仍然觉得这很尴尬,请评论,让我知道是否有更好的方法编写此函数。

请每个问题一个问题。(我删除了你的后续问题,不管怎样,这鼓励了基于意见的回答。)@drew感谢你编辑我的问题。我想知道你对哪一种更为惯用的编写elisp函数的风格有什么看法;很抱歉我建议你在讨论网站上问类似的问题,比如Reddit.check-out
s.el
,它的功能你可以从中学习。您可以使用更高阶的函数,使用一个谓词分割单词,然后使用另一个谓词进行连接——请参见
s-split-words
,了解它们的用法version@Rorschach很好,是的,高阶字符串函数是我这里缺少的东西。