]> code.delx.au - gnu-emacs-elpa/blobdiff - validate.el
Add validate-setq
[gnu-emacs-elpa] / validate.el
index 1ded67b0dd49d6d12572a1edcc1d54130ef7e1d6..7dfe45b01d15d61271dc91d03a250ab57f624840 100644 (file)
@@ -1,11 +1,11 @@
 ;;; validate.el --- Schema validation for Emacs-lisp  -*- lexical-binding: t; -*-
 
-;; Copyright (C) 2016  Artur Malabarba
+;; Copyright (C) 2016  Free Software Foundation, Inc.
 
 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
 ;; Keywords: lisp
 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
-;; Version: 0.1
+;; Version: 0.3
 
 ;;; Commentary:
 ;;
@@ -175,5 +175,17 @@ with `validate-value'. NOERROR is passed to `validate-value'."
       (if noerror val
         (error "Variable `%s' has no custom-type." symbol)))))
 
+;;;###autoload
+(defun validate-mark-safe-local (symbol)
+  "Mark SYMBOL as a safe local if its custom type is obeyed."
+  (put symbol 'safe-local-variable
+       (lambda (val)
+         (validate-value val (custom-variable-type symbol) 'noerror))))
+
+(defmacro validate-setq (symbol value)
+  "Like `setq', but throw an error if validation fails.
+VALUE is validated against SYMBOL's custom type."
+  `(setq ,symbol (validate-value ,value (custom-variable-type ',symbol))))
+
 (provide 'validate)
 ;;; validate.el ends here