X-Git-Url: https://code.delx.au/gnu-emacs-elpa/blobdiff_plain/de55883970f631196ae8c8f7b0531bd92151b74e..12e857ab9562320806fd0f7fe5fa19a1a395556a:/validate.el diff --git a/validate.el b/validate.el index 1ded67b0d..2d66a2785 100644 --- a/validate.el +++ b/validate.el @@ -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 ;; Keywords: lisp ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5")) -;; Version: 0.1 +;; Version: 0.3 ;;; Commentary: ;; @@ -64,6 +64,9 @@ "wrong number of elements" (seq-find #'identity (seq-mapn #'validate--check values schemas)))) +(defun validate--indent-by-2 (x) + (replace-regexp-in-string "^" " " x)) + (defun validate--check (value schema) "Return nil if VALUE matches SCHEMA. If they don't match, return an explanation." @@ -127,8 +130,8 @@ If they don't match, return an explanation." (error "`choice' needs at least one argument") (let ((gather (mapcar (lambda (x) (validate--check value x)) args))) (when (seq-every-p #'identity gather) - (concat "all of the options failed\n " - (mapconcat #'identity gather "\n ")))))) + (concat "all of the options failed\n" + (mapconcat #'validate--indent-by-2 gather "\n")))))) ;; TODO: `restricted-sexp' (set (or (wtype 'list) (let ((failed (list t))) @@ -145,7 +148,10 @@ If they don't match, return an explanation." (let ((print-length 4) (print-level 2)) (format "Looking for `%S' in `%S' failed because:\n%s" - schema value r)))))) + schema value + (if (string-match "\\`Looking" r) + r + (validate--indent-by-2 r)))))))) ;;; Exposed API @@ -175,5 +181,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