在emacs中从camelcase转换为_
Posted
技术标签:
【中文标题】在emacs中从camelcase转换为_【英文标题】:Converting from camelcase to _ in emacs 【发布时间】:2012-03-06 11:42:11 【问题描述】:是否有一个 emacs 函数可以将驼峰大小写的单词转换为下划线?比如:
longVariableName
M-x
to-underscore
long_variable_name
【问题讨论】:
您可能还对M-x glasses-mode
感兴趣,它神奇地在缓冲区中插入下划线而不修改底层文件。
@tripleee - 结果表明 this 现在是 Google 的热门歌曲。而且那个emacswiki页面有一堆垃圾,其中一个是函数,但该函数在一般情况下并不真正起作用。这里的答案更好。无论如何,在问答网站上责骂人们提问似乎真的没有必要。
【参考方案1】:
使用 MELPA 或 https://github.com/akicho8/string-inflection 上提供的 string-inflection 包。
有用的键盘快捷键,复制自https://www.emacswiki.org/emacs/CamelCase:
;; Cycle between snake case, camel case, etc.
(require 'string-inflection)
(global-set-key (kbd "C-c i") 'string-inflection-cycle)
(global-set-key (kbd "C-c C") 'string-inflection-camelcase) ;; Force to CamelCase
(global-set-key (kbd "C-c L") 'string-inflection-lower-camelcase) ;; Force to lowerCamelCase
(global-set-key (kbd "C-c J") 'string-inflection-java-style-cycle) ;; Cycle through Java styles
【讨论】:
如果您希望以编程方式使用它,请使用string-inflection-underscore-function
。
@RadonRosborough 现在string-inflection-underscore
.【参考方案2】:
我使用以下方法在驼峰和下划线之间切换:
(defun toggle-camelcase-underscores ()
"Toggle between camelcase and underscore notation for the symbol at point."
(interactive)
(save-excursion
(let* ((bounds (bounds-of-thing-at-point 'symbol))
(start (car bounds))
(end (cdr bounds))
(currently-using-underscores-p (progn (goto-char start)
(re-search-forward "_" end t))))
(if currently-using-underscores-p
(progn
(upcase-initials-region start end)
(replace-string "_" "" nil start end)
(downcase-region start (1+ start)))
(replace-regexp "\\([A-Z]\\)" "_\\1" nil (1+ start) end)
(downcase-region start (cdr (bounds-of-thing-at-point 'symbol)))))))
【讨论】:
【参考方案3】:(progn (replace-regexp "\\([A-Z]\\)" "_\\1" nil (region-beginning) (region-end))
(downcase-region (region-beginning) (region-end)))
【讨论】:
分配to-underscore
(这样你就可以做到M-x to-underscore
):(defun to-underscore () (interactive) (progn (replace-regexp "\\([A-Z]\\)" "_\\1" nil (region-beginning) (region-end)) (downcase-region (region-beginning) (region-end))) )
【参考方案4】:
我在将 C# 代码转换为 php 时使用它。
(defun un-camelcase-word-at-point ()
"un-camelcase the word at point, replacing uppercase chars with
the lowercase version preceded by an underscore.
The first char, if capitalized (eg, PascalCase) is just
downcased, no preceding underscore.
"
(interactive)
(save-excursion
(let ((bounds (bounds-of-thing-at-point 'word)))
(replace-regexp "\\([A-Z]\\)" "_\\1" nil
(1+ (car bounds)) (cdr bounds))
(downcase-region (car bounds) (cdr bounds)))))
然后在我的 php-mode fn 中:
(local-set-key "\M-\C-C" 'un-camelcase-word-at-point)
【讨论】:
酷!让我们做我想要的。【参考方案5】:现在2018年还有另一种通用方式:magnars/s.el: The long lost Emacs string manipulation library. - github.com, 关于 OP 问题的一些例子:
无论大小写蛇形(下划线分隔):
(s-snake-case "some words") ;; => "some_words"
(s-snake-case "dashed-words") ;; => "dashed_words"
(s-snake-case "camelCasedWords") ;; => "camel_cased_words"
无论大小写驼峰式:
(s-lower-camel-case "some words") ;; => "someWords"
(s-lower-camel-case "dashed-words") ;; => "dashedWords"
(s-lower-camel-case "under_scored_words") ;; => "underScoredWords"
在其 repo 中查看更多示例。
【讨论】:
【参考方案6】:如果您想使用s.el获取完整代码:
(defun to-snake-case (start end)
"Change selected text to snake case format"
(interactive "r")
(if (use-region-p)
(let ((camel-case-str (buffer-substring start end)))
(delete-region start end)
(insert (s-snake-case camel-case-str)))
(message "No region selected")))
【讨论】:
【参考方案7】:@ens 的回答很接近,但在 Emacs 26.1 上对我来说有点小问题。我修复了这个错误并添加了通过 C-u 前缀 arg 指定是否希望驼峰大小写的第一个字母为小写的功能:
(defun toggle-camelcase-underscores (first-lower-p)
"Toggle between camelcase and underscore notation for the
symbol at point. If prefix arg, C-u, is supplied, then make first
letter of camelcase lowercase."
(interactive "P")
(save-excursion
(let* ((bounds (bounds-of-thing-at-point 'symbol))
(start (car bounds))
(end (cdr bounds))
(currently-using-underscores-p (progn (goto-char start)
(re-search-forward "_" end t))))
(if currently-using-underscores-p
(progn
(replace-string "_" " " nil start end)
(upcase-initials-region start end)
(replace-string " " "" nil start end)
(when first-lower-p
(downcase-region start (1+ start))))
(replace-regexp "\\([A-Z]\\)" "_\\1" nil (1+ start) end)
(downcase-region start (cdr (bounds-of-thing-at-point 'symbol)))))))
【讨论】:
以上是关于在emacs中从camelcase转换为_的主要内容,如果未能解决你的问题,请参考以下文章
用于在 java 中将 CamelCase 转换为 camel_case 的正则表达式
正则表达式在 Redshift 中将 CamelCase 转换为蛇形案例
如何在 R 中将 not.camel.case 转换为 CamelCase