Skip to content

Instantly share code, notes, and snippets.

@owensys
Last active December 13, 2021 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owensys/f486e31af7ff663e44721fadb8d614d2 to your computer and use it in GitHub Desktop.
Save owensys/f486e31af7ff663e44721fadb8d614d2 to your computer and use it in GitHub Desktop.
git operation function
(defun eye--get-region-line-number ()
"获取选中区域开始行号和结束行号。
参考:https://emacs.stackexchange.com/questions/62023/how-to-get-the-real-line-number-of-a-selection"
(let* ((rend (region-end))
(startl (line-number-at-pos (region-beginning)))
(endl (line-number-at-pos rend))
(real-endl (if (= rend (save-excursion (goto-char rend) (beginning-of-line) (point)))
(- endl 1)
endl)))
(cons startl real-endl)
))
(defun xah-copy-file-path (&optional @dir-path-only-p)
"Copy the current buffer's file path or dired path to `kill-ring'.
Result is full path.
If `universal-argument' is called first, copy only the dir path.
If in dired, copy the file/dir cursor is on, or marked files.
If a buffer is not file and not dired, copy value of `default-directory' (which is usually the “current” dir when that buffer was created)
URL `http://ergoemacs.org/emacs/emacs_copy_file_path.html'
Version 2018-06-18"
(interactive "P")
(let (($fpath
(if (string-equal major-mode 'dired-mode)
(progn
(let (($result (mapconcat 'identity (dired-get-marked-files) "\n")))
(if (equal (length $result) 0)
(progn default-directory )
(progn $result))))
(if (buffer-file-name)
(buffer-file-name)
(expand-file-name default-directory)))))
(kill-new
(if @dir-path-only-p
(progn
(message "Directory copied: %s" (file-name-directory $fpath))
(file-name-directory $fpath))
(progn
(message "File path copied: %s" $fpath)
$fpath )))))
(defun eye/git-blame-file-or-region ()
"显示当前文件git blame 记录,如果有选中区域,则显示选中区域历史记录: git blame -Lstart,end file"
(interactive)
(let* ((file-path (xah-copy-file-path))
(file-name (buffer-name))
(output-buffer (format "*blame-%s*" file-name))
(args "")
(command "git blame ")
p1 p2
)
(if (region-active-p)
(let ((region-lines (eye--get-region-line-number)))
(setq p1 (car region-lines))
(setq p2 (cdr region-lines))
(setq command (concat command (format "-L%s,%s " p1 p2)))
(deactivate-mark)
)
)
(setq command (concat command file-path))
(shell-command command output-buffer nil)
(switch-to-buffer output-buffer)
(delete-other-windows)
(if (string-equal "c" (f-ext file-name))
(c-mode))
(if (string-equal "h" (f-ext file-name))
(c-mode))
(if (string-equal "cpp" (f-ext file-name))
(c++-mode))
(if (string-equal "cc" (f-ext file-name))
(c++-mode))
)
)
(defun eye/git-show-commit (&optional commit-id)
"光标停在commit id上,显示一个commit id的记录"
(interactive)
(save-excursion
(let* ((cid (or (thing-at-point 'word) (read-string "commit id:") ))
(command (format "git show %s" cid))
(output-buffer (format "*commit-%s*" cid))
)
(shell-command command output-buffer nil)
(switch-to-buffer output-buffer)
(diff-mode)
(delete-other-windows)
)
))
(defun eye/git-log-current-file ()
"显示当前文件的git记录"
(interactive)
(let* ((file-path (xah-copy-file-path))
(file-name (buffer-name))
(output-buffer (format "*gitlog-%s*" file-name))
(args "")
(command "git log --date=iso -- ")
)
(setq command (concat command file-path))
(shell-command command output-buffer nil)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment