-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagit-nix3.el
122 lines (100 loc) · 4.13 KB
/
magit-nix3.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
;;; magit-nix3.el --- Nix3.el integration for Magit -*- lexical-binding: t -*-
;; Copyright (C) 2022 Akira Komamura
;; Author: Akira Komamura <[email protected]>
;; Version: 0.1
;; Package-Requires: ((emacs "28.1") (nix3 "0.1") (promise "1.1"))
;; Keywords: processes
;; URL: https://github.com/emacs-twist/nix3.el
;; This file is not part of GNU Emacs.
;;; License:
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
;; License as published by the Free Software Foundation; either
;; version 2.1 of the License, or (at your option) any later version.
;;
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with this library; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;; Commentary:
;; This library integrates magit with Nix.
;;; Code:
(require 'map)
(require 'magit-section)
(declare-function nix3-flake-lock-diff-section "ext:nix3-flake-lock")
(declare-function nix3-flake-run-section "ext:nix3-flake")
(declare-function nix3-normalize-path "ext:nix3-core")
(defgroup magit-nix3 nil
"Nix flake integration for Magit."
:prefix "magit-nix3-"
:group 'magit-status
:group 'nix3)
;;;; magit-status-mode integration
(defcustom magit-nix3-sections
'(nix3-flake-insert-outputs
nix3-flake-insert-inputs)
"Sections inserted into the magit buffer."
:type 'hook)
(defcustom magit-nix3-insert-positions nil
"Position at which nix flake sections are inserted.
The value should be either nil or one of the existing members of
`magit-status-sections-hook'."
:type '(choice function
(const nil)))
(defcustom magit-nix3-append t
"Whether to insert the section after the position."
:type 'boolean)
(defun magit-nix3-flake-sections ()
(require 'promise)
;; Load the library without adding autoloads
(require 'nix3-flake)
(when (file-exists-p "flake.nix")
(nix3-flake-run-section 'magit-nix3-sections
(nix3-normalize-path default-directory)
nil)))
;;;###autoload
(define-minor-mode magit-nix3-flake-mode
"Turn on flake features in `magit-status-mode' buffers."
:global t
(cond
(magit-nix3-flake-mode
(magit-add-section-hook 'magit-status-sections-hook
#'magit-nix3-flake-sections
magit-nix3-insert-positions
magit-nix3-append))
(t
(remove-hook 'magit-status-sections-hook #'magit-nix3-flake-sections))))
;;;; magit-diff-mode integration
(defun magit-nix3-diff-section ()
(require 'nix3-flake-lock)
;; When the user edits a commit message, magit displays diffs via
;; `magit-revision-sections-hook', so this section will be called as well.
;; However, it seems to block creating an actual commit, so I want to prevent
;; this function from being called when creating a commit as a workaround. It
;; seems that `this-command' is nil during hook call after a commit is made
;; with a message, so use it as a guard.
(when this-command
(nix3-flake-lock-diff-section)))
;;;###autoload
(define-minor-mode magit-nix3-flake-lock-mode
"Minor mode to show flake.lock diffs in `magit-diff-mode' buffers."
:global t
(cond
(magit-nix3-flake-lock-mode
(magit-add-section-hook 'magit-diff-sections-hook
#'magit-nix3-diff-section
'magit-insert-diff
'append)
(magit-add-section-hook 'magit-revision-sections-hook
#'magit-nix3-diff-section
'magit-insert-revision-diff
'append))
(t
(remove-hook 'magit-diff-sections-hook #'magit-nix3-diff-section)
(remove-hook 'magit-revision-sections-hook #'magit-nix3-diff-section))))
(provide 'magit-nix3)
;;; magit-nix3.el ends here