我开始为“git add -p”编写这个代码片段:
(add-hook
'diff-mode-hook
(lambda()
(mapc (lambda(k)
(lexical-let ((kk k))
(define-key diff-mode-map k
(lambda()(interactive)
(if (region-active-p)
(replace-regexp "^." kk
nil
(region-beginning)
(region-end))
(insert kk)))))) (list " " "-" "+"))))
它可以按我的意愿工作,只是中间的 'lexical-let 的丑陋困扰着我。我不得不添加它,因为嵌套的 lambda 没有看到变量 'k. 有没有更好的方法来编写这段代码?我更喜欢一些神奇的函数'lexic-lambda,它可以让我写:
(add-hook
'diff-mode-hook
(lambda()
(mapc (lexical-lambda(k)
(define-key diff-mode-map k
(lexical-lambda()(interactive)
(if (region-active-p)
(replace-regexp "^." k
nil
(region-beginning)
(region-end))
(insert k)))))) (list " " "-" "+")))