我尝试过两种不同的方式来编写我的函数.我决定编写一个小函数来转换为驼峰大小写并返回使用这个elisp字符串库.首先通过搜索我发现这个教程在点上更换东西并实现了这个功能:
; use string manipulation library to switch between camel and snake (s.el)
(defun my_test ()
"test"
(interactive)
;; get current selection or word
(let (bds p1 p2 inputStr resultStr)
;; get boundary
(if (use-region-p)
(setq bds (cons (region-beginning) (region-end) ))
(setq bds (bounds-of-thing-at-point 'word)) )
(setq p1 (car bds) )
(setq p2 (cdr bds) )
;; grab the string
(setq inputStr (buffer-substring-no-properties p1 p2) )
(setq resultStr (s-lower-camel-case inputStr))
(message inputStr)
(delete-region p1 p2 ) ; delete the region
(insert resultStr) ; insert new string
)
)
Run Code Online (Sandbox Code Playgroud)
这不会resultStr按预期进行修改,只会inputStr在那里进行修改.
我不明白的是,当我得到eval(with M-:)时,(setq resultStr (s-lower-camel-case "other_string"))我得到了预期的结果("otherString")
我甚至尝试了一种不同的(并且更好地用于我的目的)编写受这个SO问题启发的功能的方式:
(defun change-word-at-point (fun)
(cl-destructuring-bind (beg . end)
(bounds-of-thing-at-point 'word)
(let ((str (buffer-substring-no-properties beg end)))
(delete-region beg end)
(insert (funcall fun str)))))
(defun my_test_camel ()
(interactive)
(change-word-at-point 's-lower-camel-case))
Run Code Online (Sandbox Code Playgroud)
它遇到了同样的问题.这让我觉得这个s-lower-camel-case函数有问题(或者我如何调用它)但是当从eval调用时如上所述
编辑:修改第一个函数以包含let语法,请参阅注释
编辑#2:这两个功能都正常工作,答案已被接受,因为它提供了一个更好的替代方案,包括符号信息和正确的编写方式.我的问题是测试是由于haskell模式.新问题在这里
这是另一种定义.评论是正确的,您需要通过本地绑定let.请注意,如果该区域处于活动状态,则此版本将使用该区域,或者bounds-of-thing-at-point如果没有区域处于活动状态,则使用该区域来获取该字词:
(defun word-or-region-to-lcc ()
"Convert word at point (or selected region) to lower camel 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 (s-lower-camel-case text)))))
Run Code Online (Sandbox Code Playgroud)
如果您不关心使用region的选项,则可以text在本地绑定(thing-at-point 'symbol)而不是调用buffer-substring-no-properties.
UPDATE. 事实证明你可以使用(thing-at-point 'symbol)而不是(thing-at-point 'word)获得蛇案的完整符号.
| 归档时间: |
|
| 查看次数: |
1787 次 |
| 最近记录: |