changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / emacs/lib/sxp.el

changeset 698: 96958d3eb5b0
parent: 7efdeaebaf22
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 ;;; sxp.el --- S-Expression Mode -*- lexical-binding:t -*-
2 
3 ;; Copyright (C) 2023 ellis
4 
5 ;; Author: ellis <ellis@rwest.io>
6 ;; Keywords: lisp
7 
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12 
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17 
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
20 
21 ;;; Commentary:
22 
23 ;; Emacs Support for S-Expression files (.sxp).
24 
25 ;; https://www.gnu.org/software/emacs/manual/html_node/elisp/Parsing-Expressions.html
26 
27 ;;; Code:
28 (define-derived-mode sxp-mode lisp-data-mode "S-Expr"
29  "Major mode for editing '.sxp' files.")
30 
31 (add-to-list 'auto-mode-alist '("\\.sxp" . sxp-mode))
32 
33 ;;; OOP
34 (require 'eieio)
35 
36 (cl-defgeneric wrap (sxp form))
37 (cl-defgeneric unwrap (sxp))
38 (cl-defgeneric from-sxp (sxp form)
39  "Update SXP using values from FORM.")
40 (cl-defgeneric to-sxp (sxp)
41  "Return SXP as a list.")
42 (cl-defgeneric read-sxp (sxp &optional stream)
43  "Read S-Expressions directly from STREAM (default =
44  `standard-input') and update SXP.")
45 (cl-defgeneric write-sxp (sxp &optional stream comment)
46  "Write S-Expressions directly to STREAM (default =
47  `standard-output') with optional COMMENT.")
48 
49 (defclass sxp ()
50  ((form :initarg :form :accessor form)))
51 
52 (cl-defmethod wrap ((sxp sxp) form)
53  (oset sxp :form form))
54 (cl-defmethod unwrap ((sxp sxp))
55  (slot-value sxp 'form))
56 
57 (cl-defmethod read-sxp ((sxp sxp) &optional stream)
58  (initialize-instance sxp (list :form (read stream)))
59  sxp)
60 (cl-defmethod write-sxp ((sxp sxp) &optional stream)
61  (print (to-sxp sxp) stream))
62 
63 ;; (defvar sxp-load-tests nil)
64 ;; (when sxp-load-tests
65 ;; (require 'ert)
66 ;; (defmacro deftest (name &rest body)
67 ;; "shorthand for `ert-deftest'."
68 ;; (declare (indent 1))
69 ;; `(ert-deftest ,name () :tags '(sxp) ,@body))
70 ;; (deftest 'sxp:read
71 ;; (should (read-sxp (sxp) "(hey stranger)")))
72 ;; (deftest 'sxp:write
73 ;; (should (write-sxp (sxp :form nil))))
74 ;; (deftest 'sxp:from
75 ;; (should (from-sxp (sxp) '(test 1 2 3))))
76 ;; (deftest 'sxp:to
77 ;; (should (to-sxp (sxp :form '("test" 'ing)))))
78 ;; (deftest 'sxp:fmt
79 ;; (should t))
80 ;; (deftest 'sxp:mode
81 ;; (should t)))
82 
83 (provide 'sxp)
84 ;;; sxp.el ends here