version-control-lexical.el 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ;;; version-control-lexical.el --- Functions for version control -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2020 Free Software Foundation, Inc.
  3. ;; Author: Oleg Pykhalov <go.wigust@gmail.com>
  4. ;; Version: 0.0.1
  5. ;; Package-Requires: ((ffap "1.0") ((magit "1.0")))
  6. ;; Keywords: browse-url, magit, vc
  7. ;; URL: https://github.com/kitnil/dotfiles/
  8. ;;; Commentary:
  9. ;; This package provides functions to work with version control systems.
  10. ;;; Code:
  11. (require 'ffap)
  12. ;; (require 'magit-diff)
  13. ;;;###autoload
  14. (defun wi-define-browse-url-git-commit (name directory url->commit)
  15. "Define browse-url-* function.
  16. NAME part of function name browse-url-NAME-git-commit.
  17. DIRECTORY Git project directory.
  18. URL->COMMIT function to parse commit from url.
  19. The following example defines `browse-url-guix-git-commit'
  20. function, which opens
  21. https://git.savannah.gnu.org/cgit/guix.git/commit/?id=df05842332be80e…
  22. with `magit-show-commit' function in /home/oleg/src/git.savannah.gnu.org/git/guix directory.
  23. \(wi-define-browse-url-git-commit
  24. \"guix\"
  25. (expand-file-name \"/home/oleg/src/git.savannah.gnu.org/git/guix\")
  26. (lambda (url) (car (last (split-string url \"=\")))))"
  27. (defalias (intern (concat "browse-url-" name "-git-commit"))
  28. (function
  29. (lambda (url &optional _new-window)
  30. (interactive (list (read-string "Commit: " nil nil (word-at-point))))
  31. (let ((default-directory directory)
  32. (commit (funcall url->commit url)))
  33. (when (eq (cdr (assoc 'major-mode
  34. (buffer-local-variables (current-buffer))))
  35. 'elfeed-search-mode)
  36. (let* ((entry (elfeed-search-selected t))
  37. (title (elfeed-entry-title entry)))
  38. (elfeed-untag entry 'unread)
  39. (elfeed-search-update-entry entry)
  40. (unless (or elfeed-search-remain-on-entry (use-region-p))
  41. (forward-line))))
  42. (condition-case user-error
  43. (magit-show-commit commit)
  44. (error (magit-status default-directory))))))
  45. (format "Show a Git `commit' from the %s checkout.
  46. If no commit hash provides, show a commit from hash at current point."
  47. name)))
  48. (defun wi-magit-show-commit ()
  49. (interactive)
  50. (call-interactively #'magit-show-commit)
  51. (call-interactively #'crux-transpose-windows)
  52. (call-interactively #'other-window))
  53. (with-eval-after-load 'magit
  54. (let ((map magit-log-mode-map))
  55. (define-key map (kbd "RET") 'wi-magit-show-commit)))
  56. ;;; version-control-lexical.el ends here