Hatena::ブログ(Diary)

わからん

2011.04.22

[] Emacsで快適にgit blameしたい(できてない) の回答例のひとつ

お疲れ様です。トラブル対応中の森と申します。id:hitode909 さんのトラブルもカイケツしようと考えてみました。ゼロから考える元気がなかったので、hitode さんのコードをできるだけそのまま使いました。ポイント位置のコミットメッセージをポップアップで表示し、ウィンドウを上下分割してポイント位置を中心に git blame した結果を表示するバッファを表示します。このバッファ/ウィンドウは q で削除できます。でも、僕は M-x vc-annotate の方が好きかも。


f:id:kitokitoki:20110422021518p:image


▼ ヒトデさんの日記にある git-blame-oneline を ~/bin に置いていることが前提で、auto-complete.el 周辺が入っていれば、M-x g で実行できるはず。

(defun git-blame-current-buffer () ;; shell-command を使うより、vc-annotete を利用した方がさらに過去にさかのぼれるので良いのでは?
  (interactive)
  (let ((result-buf "*git blame*")
        (popup-context (git-blame-current-line))
        (hoge-line (line-number-at-pos)))
    (shell-command (concat "git blame " (buffer-file-name (current-buffer))) result-buf)
    (view-buffer-other-window result-buf t
                              (lambda (buf)
                                (kill-buffer-and-window)))
  (switch-to-buffer result-buf)
  (goto-line hoge-line)
  (recenter)
  (popup-tip popup-context)))

(defalias 'g 'git-blame-current-buffer)

(defun git-blame-current-line ()
  (interactive)
  (let ((old-buf (current-buffer))
    (blame-buf (get-buffer-create "*blame*"))
    (line-num (number-to-string (line-number-at-pos))))
    (set-buffer blame-buf)
    (erase-buffer)
    (call-process "git-blame-oneline" nil "*blame*" t (buffer-file-name old-buf) line-num)
    (setq content (buffer-string))
    (set-buffer old-buf)
    (when (not (eq (length content) 0)) ;;この when に入らない場合は想定していないです
      content)))
Google