|
| 1 | +;;; git-attr.el --- Git attributes of buffer file -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2018 Arne Jørgensen |
| 4 | +;; URL: https://github.com/arnested/emacs-git-attr |
| 5 | +;; Version: 0.0.1 |
| 6 | +;; Package-Requires: ((emacs "24.3")) |
| 7 | + |
| 8 | +;; Author: Arne Jørgensen <arne@arnested.dk> |
| 9 | +;; Keywords: vc |
| 10 | + |
| 11 | +;; This program is free software; you can redistribute it and/or modify |
| 12 | +;; it under the terms of the GNU General Public License as published by |
| 13 | +;; the Free Software Foundation, either version 3 of the License, or |
| 14 | +;; (at your option) any later version. |
| 15 | + |
| 16 | +;; This program is distributed in the hope that it will be useful, |
| 17 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +;; GNU General Public License for more details. |
| 20 | + |
| 21 | +;; You should have received a copy of the GNU General Public License |
| 22 | +;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + |
| 24 | +;;; Commentary: |
| 25 | + |
| 26 | +;; This tool will let you use git attributes |
| 27 | +;; (https://git-scm.com/docs/gitattributes) in Emacs buffers. |
| 28 | + |
| 29 | +;; In example the following will get the value of a `foo' git |
| 30 | +;; attribute for the file associated with the current buffer: |
| 31 | + |
| 32 | +;; (git-attr-get "foo") |
| 33 | + |
| 34 | +;; The `git-attr-get' function will return |
| 35 | + |
| 36 | +;; * t for git attributes with the value `set' |
| 37 | +;; * nil for git attributes with the value `unset' |
| 38 | +;; * 'undecided for git attributes that are `unspecified' |
| 39 | +;; * and the value if the git attribute is set to a value |
| 40 | + |
| 41 | +;;; Code: |
| 42 | + |
| 43 | +(defvar-local git-attr 'undecided "Git attributtes for current buffer.") |
| 44 | + |
| 45 | +(defun git-attr-check () |
| 46 | + "Get git attributes for current buffer file." |
| 47 | + (let ((file buffer-file-name) |
| 48 | + (git (executable-find "git"))) |
| 49 | + (when (and file |
| 50 | + git) |
| 51 | + (let ((attr-list (split-string (with-output-to-string |
| 52 | + (with-current-buffer standard-output |
| 53 | + (call-process git nil (list t nil) nil "check-attr" "-z" "-a" file) |
| 54 | + ) |
| 55 | + ) "\000" t)) |
| 56 | + result) |
| 57 | + (while attr-list |
| 58 | + (push `(,(cadr attr-list) . ,(caddr attr-list)) result) |
| 59 | + (setq attr-list (cdddr attr-list))) |
| 60 | + result)))) |
| 61 | + |
| 62 | +(defun git-attr () |
| 63 | + "Get git attributes for current buffer file and set in buffer local variable `git-attr'." |
| 64 | + (interactive) |
| 65 | + (if (eq git-attr 'undecided) |
| 66 | + (setq git-attr (git-attr-check)) |
| 67 | + git-attr)) |
| 68 | + |
| 69 | +(defun git-attr-raw (attr) |
| 70 | + "Get the raw git attribute named ATTR for the file in current buffer. |
| 71 | +
|
| 72 | +This is the raw value as returned from `git check-attr -a' (if specified). |
| 73 | +
|
| 74 | +You probably want to use `git-attr-get' instead." |
| 75 | + (cdr (assoc attr (git-attr)))) |
| 76 | + |
| 77 | +(defun git-attr-get (attr) |
| 78 | + "Get the git attribute named ATTR for the file in current buffer. |
| 79 | +
|
| 80 | + * t for git attributes with the value `set' |
| 81 | + * nil for git attributes with the value `unset' |
| 82 | + * 'undecided for git attributes that are `unspecified' |
| 83 | + * and the value if the git attribute is set to a value" |
| 84 | + (let ((value (git-attr-raw attr))) |
| 85 | + (cond ((string= value "set") t) |
| 86 | + ((string= value "unset") nil) |
| 87 | + ((eq nil value) 'undecided) |
| 88 | + (t value)))) |
| 89 | + |
| 90 | +(provide 'git-attr) |
| 91 | +;;; git-attr.el ends here |
0 commit comments