forked from juanjosegarciaripoll/project-cmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project-local.el
205 lines (171 loc) · 7.82 KB
/
project-local.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
;;; project-local.el --- Local variables for projects -*- lexical-binding: t; -*-
;; Version: 0.1
;; Author: Juan Jose Garcia-Ripoll
;; Maintainer: Juan Jose Garcia-Ripoll <[email protected]>
;; URL: https://github.com/juanjosegarciaripoll/project-cmake
;; Keywords: convenience, languages
;; Package-Requires: ((emacs "26.1") (project "0.3.0"))
;; MIT License
;; Copyright (c) 2022 Juan José García Ripoll
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Commentary:
;; This package extents `project` to define project-local variables,
;; much like dir-local variables already do. Those variables are
;; stored into a .project.el file at the root of the project, without
;; the cumbersome syntax of dir-local variables, but rather as a
;; series of `defvar` statements.
;;
(require 'project)
(require 'wid-edit) ;; widget-convert
(require 'cus-edit) ;; custom-variable-type
(defcustom project-local-confirm-save t
"Ask for confirmation before saving a project's variable file.")
(defvar project-local-cache nil
"Association between projects and assignments. This is an association
between projects and association lists, indexed by variable names that
take local values. A project's record may also contain values indexed
by keywords (e.g. :filename, :date, etc) which are not saved but are
used during the manipulation of a project.")
(defun project-local-map-records (fn)
(dolist (pair project-local-cache)
(let ((project (car pair))
(record (cdr pair)))
(funcall fn project record))))
(defun project-local-file-name (project)
"Return the canonical name for a project's local variable file."
(let ((root (project-root project)))
(expand-file-name ".project.el" root)))
(defun project-local-new-record (project)
(list (cons :filename (project-local-file-name project))))
(defun project-local-set-record (project record)
(let ((previous (assoc project project-local-cache)))
(if previous
(rplacd previous record)
(push (cons project record) project-local-cache)))
record)
(defun project-local-remove-record (project)
(setq project-local-cache (delq (project-local-cached-record project)
project-local-cache)))
(defun project-local-record-set (record variable value)
(let ((field (assq variable record)))
(if field
(setcdr field value)
(setcdr record (cons (cons variable value) (cdr record)))))
record)
(defun project-local-record-value (record variable &optional default)
(let* ((field (assq variable record)))
(if field
(cdr field)
default)))
(defun project-local-record-map-values (record function)
(dolist (pair record)
(let ((variable (car pair))
(value (cdr pair)))
(funcall function variable value))))
(defun project-local-record-mark-as-changed (record)
(project-local-record-set record :changed t))
(defun project-local-record (project)
"Return the record of variables associated to a project. It may be taken
from the cache or read from a project's local variables file. If it does
not exists, it creates a new record that is added to the cache."
(or (project-local-cached-record project)
(project-local-load-record project)))
(defun project-local-cached-record (project)
"Return the existing record of a project, if it exists, or NIL if it doesn't."
(assoc project project-local-cache))
(defun project-local-read-definitions (project)
"Read project-local definitions from the .project.el file, and return
a new record with those definitions -- or an empty one if such file does
not exist."
(let* ((file-name (project-local-file-name project))
(record (project-local-new-record project)))
(when (file-exists-p file-name)
(condition-case condition
(with-temp-buffer
(insert-file file-name)
(let ((buf (current-buffer))
sexp)
(while (setq sexp (read buf))
(cl-destructuring-bind (statement variable &optional value)
sexp
(when (eq statement 'defvar)
(project-local-record-set record variable (eval value)))))
output))
(end-of-file)
(error (message "Ill-formed project-local file %s.\nCondition: %S"
file-name condition))))
record))
(defun project-local-write-definitions (project record)
"Write all variables from a project into its project-local file"
(with-temp-buffer
(insert ";;; Project-local definitions.\n")
(insert ";;; Saved on " (format-time-string "%Y-%m-%dT%H:%M:%S") "\n")
(project-local-record-map-values
record
(lambda (variable value)
(unless (keywordp variable)
(prin1 `(defvar ,variable ',value) (current-buffer))
(insert "\n"))))
(write-region (point-min) (point-max)
(project-local-file-name project))))
(defun project-local-value (project variable)
"Return the project-local value assigned to VARIABLE or the globally
bound value for that symbol."
(project-local-record-value (project-local-record project)
variable
(and (boundp variable)
(symbol-value variable))))
(defun project-local-set (project variable value)
"Assign a project-local VARIABLE a VALUE. It may create a new record in the
project-local cache, that will have to be saved later on."
(let ((record (project-local-record project)))
(project-local-record-set record variable value)
(project-local-record-mark-as-changed record)
value))
(defun project-local-edit (project variable)
(let* ((prompt (format "Value of %s: " variable))
(widget (widget-convert (custom-variable-type variable)))
(value (project-local-value project variable))
(new-value-string (read-from-minibuffer prompt (format "%S" value)))
(new-value (car (read-from-string new-value-string))))
(if (widget-apply widget :match new-value)
(project-local-set (project-current t) variable new-value)
(error "The value does not match the expected type for %S"
variable))))
(defun project-local-save-project (project record)
(when (and (project-local-record-value record :changed)
(or (null project-local-confirm-save)
(y-or-n-p (format "Save project local file %s?"
(project-local-file-name project)))))
(project-local-write-definitions project record)))
(defun project-local-save-records (&optional all)
"Save all records that have been modified. If no argument is
provided, it only saves the variables from the current project.
Otherwise it saves variables from all projects."
(if all
(project-local-map-records 'project-local-save-project)
(let* ((project (project-current t))
(record (project-local-record project)))
(project-local-save-project project record))))
(defun project-local-load-record (project)
"Load a project's record from a .project.el, or return a new record for it."
(project-local-set-record project (project-local-read-definitions project)))
(defun project-local-clear (&optional project)
(interactive)
(project-local-remove-record (or project (project-current t))))
(add-to-list 'kill-emacs-hook '(lambda () (project-local-save-records 'all)))
(provide 'project-local)