spacemacs/layers/+source-control/git/funcs.el
Roman Gonzalez 28f3b6289d Add spacemacs/git-permalink functionality
Context:

When using org files, one normally tends to add Github code links to an
algorithm/bug explanation so that readers are able to follow along with a PR
change.

The current `<SPC> g l L` command gets us the URL to the current branch; given
the code is in flux, the link becomes non-sensical rather quickly, loosing value
when trying to build a document with explanations about the code.

This PR allows us to get the Permalink (e.g. link associated to the commit, not
the branch); using this, the URL contents will never change with new commits
submitted to the repository.

Changes:

  * Add two new functions with associated keybindings:

    - `<SPC> g l p` maps to `spacemacs/git-permalink`

    - `<SPC> g l P` maps to `spacemacs/git-permalink-copy-url-only`

  * The above changes rely on mechanisms of `git-link`, so no new code needs to
    be introduced
2019-10-05 19:19:41 +02:00

103 lines
3.3 KiB
EmacsLisp
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; funcs.el --- Colors Layer functions File
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
;; magit
(defun spacemacs/magit-toggle-whitespace ()
"Toggle whitespace in `magit-diff-mode'."
(interactive)
(if (member "-w" (if (derived-mode-p 'magit-diff-mode)
magit-refresh-args
magit-diff-section-arguments))
(spacemacs//magit-dont-ignore-whitespace)
(spacemacs//magit-ignore-whitespace)))
(defun spacemacs//magit-ignore-whitespace ()
"Ignore whitespace in `magit-diff-mode'"
(add-to-list (if (derived-mode-p 'magit-diff-mode)
'magit-refresh-args 'magit-diff-section-arguments) "-w")
(magit-refresh))
(defun spacemacs//magit-dont-ignore-whitespace ()
"Don't ignore whitespace in `magit-diff-mode'"
(setq magit-diff-options
(remove "-w"
(if (derived-mode-p 'magit-diff-mode)
magit-refresh-args
magit-diff-section-arguments)))
(magit-refresh))
(defun spacemacs/git-permalink ()
"Allow the user to get a permalink via git-link in a git-timemachine buffer."
(interactive)
(let ((git-link-use-commit t))
(call-interactively 'spacemacs/git-link-commit)))
(defun spacemacs/git-permalink-copy-url-only ()
"Allow the user to get a permalink via git-link in a git-timemachine buffer."
(interactive)
(let (git-link-open-in-browser
(git-link-use-commit t))
(call-interactively 'spacemacs/git-link-commit)))
(defun spacemacs/git-link-copy-url-only ()
"Only copy the generated link to the kill ring."
(interactive)
(let (git-link-open-in-browser)
(call-interactively 'spacemacs/git-link)))
(defun spacemacs/git-link-commit-copy-url-only ()
"Only copy the generated link to the kill ring."
(interactive)
(let (git-link-open-in-browser)
(call-interactively 'spacemacs/git-link-commit)))
(defun spacemacs/git-link ()
"Allow the user to run git-link in a git-timemachine buffer."
(interactive)
(require 'git-link)
(if (and (boundp 'git-timemachine-revision)
git-timemachine-revision)
(cl-letf (((symbol-function 'git-link--branch)
(lambda ()
(car git-timemachine-revision))))
(call-interactively 'git-link))
(call-interactively 'git-link)))
(defun spacemacs/git-link-commit ()
"Allow the user to run git-link-commit in a git-timemachine buffer."
(interactive)
(require 'git-link)
(if (and (boundp 'git-timemachine-revision)
git-timemachine-revision)
(cl-letf (((symbol-function 'word-at-point)
(lambda ()
(car git-timemachine-revision))))
(call-interactively 'git-link-commit))
(call-interactively 'git-link-commit)))
(defun spacemacs//support-evilified-buffer-p (style)
"Return non-nil if evil navigation should be enabled for STYLE."
(or (eq style 'vim)
(and (eq style 'hybrid)
hybrid-style-enable-evilified-state)))
(defun spacemacs//magit-evil-magit-bindings (style)
"Set `evil-magit' bindings for the given editing STYLE."
(cond
((spacemacs//support-evilified-buffer-p style)
(evil-magit-init))
(t
(when (featurep 'evil-magit)
(evil-magit-revert)))))