【发布时间】:2018-05-19 13:41:41
【问题描述】:
我正在尝试在 elisp 中编写一个命令,以自动对给定文件中的单元测试重新编号。为了帮助我轻松定位失败的测试,我通常使用以下语法(使用 GoogleTest):
TEST(testCaseName, T0XX_Test_Description)
我已经能够使用带有 re-search-forward / replace-match 的 while 循环编写工作命令:
(defun renumber-tests-auto(&optional num)
"Automatically renumber the tests from the current location in
the active buffer. Optional argument sets the current test
number (instead of 1). This function automatically updates
all test numbers from the current location until the end of
the buffer without querying the user for each test."
(interactive "NStarting test number: ")
(save-excursion
(setq num (or num 1 ))
(while (re-search-forward ", +T0[0-9]+" nil t )
(replace-match
(concat ", T" (format "%03d" num )))
(setq num (+ 1 num))
)
)
)
但是,我也非常希望有这个函数的交互式版本,使用 perform-replace 交互式地查询用户的每个测试。当然,我可以简单地在我的代码中手动处理查询行为,但是,鉴于此功能已经存在,我真的不想重新实现它。此外,我想确保此命令与其他内置查询替换函数具有相同的接口。
我最近失败的尝试如下:
(defun renumber-tests(&optional num)
(interactive "NStarting test number: ")
(save-excursion
(setq num (or num 1 ))
(perform-replace ", +T0[0-9]+"
(concat ", T" (format "%03d" (+ 1 num )
))
t t nil)
)
)
但是,这不会在每次运行时更新 num 的值(我也尝试过 (setq num (+ 1 num) )并得到相同的结果。
我非常感谢那些在 elisp 方面更有经验的人的帮助 - 如果有任何方法可以使它按我的意图工作。
【问题讨论】: