我想通过分析 git diff的结果,生成一些内容放到 magit 的commit-msg里面,现在第一步就没想明白, 如果把shell-command的结果放到一个buffer里,我能想到的是用switch-to-buffer来切换,处理完结果后再切换原来的状态,这样UI界面应该会闪烁吧,体验肯定不好,如果用shell-command-to-string的话,处理字符串好像比较麻烦,求赐教
with-current-buffer is a macro defined in subr.el.gz.
Signature
(with-current-buffer BUFFER-OR-NAME &rest BODY)
Documentation
Execute the forms in BODY with BUFFER-OR-NAME temporarily current.
BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
The value returned is the value of the last form in BODY. See
also with-temp-buffer.
其实可以用process-lines
(let ((default-directory "/home/chino/.emacs.d/lib/site-lisp/ace-window"))
(process-lines "git" "diff"))
;; =>
;; ("diff --git a/ace-window.el b/ace-window.el"
;; "index 3a19324..306dd26 100644"
;; "--- a/ace-window.el"
;; "+++ b/ace-window.el"
;; "@@ -1,3 +1,4 @@"
;; "+FOOBAR"
;; " ;;; ace-window.el --- Quickly switch windows. -*- lexical-binding: t -*-"
;; " "
;; " ;; Copyright (C) 2015 Free Software Foundation, Inc.")
哇,神速啊,谢谢,我尝试下再来汇报
目前可以提取出内容了,但还没有找到方法自动插入到 COMMIT_EDITMSG
(defun my-insert-change-log ()
" Inter change log from the result of `my-parse-git-diff'
"
(interactive)
(let* ((changes (my-parse-git-diff)))
(dolist (file-changes changes)
(insert (car file-changes))
(insert "\n")
(dolist (change (cadr file-changes))
(insert (format " %s\n" change))))))
(defun my-parse-git-diff ()
"Parse change log from output of \"git diff\"
the change log should be style with :
// 2020-12-14 comment string
or
// 2020-12-14 12:12:12 comment string
Return alist with structure: '( (fn1 (log-str log-str)) (fn2 (log-str log-str)) )
"
(let* ((fn nil)
(log-strings)
(file-result);; '(fn (log-str1 ...))
(alist (process-lines "git" "--no-pager" "diff" "--no-color" "-U0" "--cached")))
(dolist (line alist)
(and (string-prefix-p "diff --git a/" line)
(or (and fn
(add-to-list 'file-result (list fn log-strings)))
t)
;; save filename
(setq fn (substring (nth 2 (split-string line)) 2))
(setq log-strings '()))
(and (string-match (concat "\\(//\\s-?+"
(format-time-string "%Y-%m-%d");; date 2020-12-14
"\\)"
"\\(\\s-?+[0-9:]+\\)?";; time h:m:s
"\\s-?\\(.*$\\)") line)
(add-to-list 'log-strings (match-string 3 line))))
(add-to-list 'file-result (list fn log-strings))))