From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: taylanbayirli@gmail.com (Taylan Ulrich =?utf-8?Q?Bay=C4=B1rl=C4=B1?= =?utf-8?Q?=2FKammer?=) Newsgroups: gmane.emacs.devel Subject: [PATCH] Add json-sexp-mode. Date: Sat, 17 Oct 2015 18:27:01 +0200 Message-ID: <87wpulwje2.fsf@T420.taylan> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1445099232 6506 80.91.229.3 (17 Oct 2015 16:27:12 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 17 Oct 2015 16:27:12 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Oct 17 18:27:12 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZnUK6-00043o-O5 for ged-emacs-devel@m.gmane.org; Sat, 17 Oct 2015 18:27:11 +0200 Original-Received: from localhost ([::1]:59026 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZnUK5-0005iP-KC for ged-emacs-devel@m.gmane.org; Sat, 17 Oct 2015 12:27:09 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54871) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZnUK1-0005iB-Hx for emacs-devel@gnu.org; Sat, 17 Oct 2015 12:27:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZnUK0-0004gp-7e for emacs-devel@gnu.org; Sat, 17 Oct 2015 12:27:05 -0400 Original-Received: from mail-wi0-x231.google.com ([2a00:1450:400c:c05::231]:35015) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZnUJz-0004fs-UI for emacs-devel@gnu.org; Sat, 17 Oct 2015 12:27:04 -0400 Original-Received: by wicll6 with SMTP id ll6so45229954wic.0 for ; Sat, 17 Oct 2015 09:27:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:user-agent:mime-version :content-type; bh=BofuL8Y8ap5yY6Ok/lBFv1IyYKUnMTN4/m1Sq9QiDU0=; b=O8vk/xxxhRkCq988zGaFQc6gJcwAbFOgOZ09WMryzldiAjTHRkVFAjk6lPzfwMlriu hu3b1yVJs5bkyGwacNu68SZL5phcsBjWG2oFDSjCbdvyntiPPQDMeQdWl5PGNbR6ojk2 lNR3oFFEFB69/2zIFO0lyi14TZUPk+r3bHiN9j8OLTYEkQa9QKvFP9Drl9KmBbqlwF/F f2+6ILqPEXWlwX1pWsnVaVvvBGm6dw9rAp5gQ1ma0uT0qExexz9QvgXku39O3EoJJAiD hpQMotzMlHXs5bN+HT0EyyrWGx1UixFbyb2rt7Z3zR60hG3o4stUu/6qh9njVx37wZfa ZsDQ== X-Received: by 10.180.10.101 with SMTP id h5mr11100087wib.22.1445099223460; Sat, 17 Oct 2015 09:27:03 -0700 (PDT) Original-Received: from T420.taylan ([2a02:908:c32:4740:221:ccff:fe66:68f0]) by smtp.gmail.com with ESMTPSA id lf10sm29163331wjb.23.2015.10.17.09.27.02 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Oct 2015 09:27:02 -0700 (PDT) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c05::231 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:191848 Archived-At: --=-=-= Content-Type: text/plain This is for ELPA. --=-=-= Content-Type: text/x-diff; charset=utf-8 Content-Disposition: inline; filename=0001-Add-json-sexp-mode.patch Content-Transfer-Encoding: quoted-printable >From 50ec911133b23aaa959795e80e09283794bafd19 Mon Sep 17 00:00:00 2001 From: =3D?UTF-8?q?Taylan=3D20Ulrich=3D20Bay=3DC4=3DB1rl=3DC4=3DB1/Kammer?=3D Date: Sat, 17 Oct 2015 17:15:23 +0200 Subject: [PATCH] Add json-sexp-mode. --- packages/json-sexp-mode/json-sexp-mode.el | 112 ++++++++++++++++++++++++++= ++++ 1 file changed, 112 insertions(+) create mode 100644 packages/json-sexp-mode/json-sexp-mode.el diff --git a/packages/json-sexp-mode/json-sexp-mode.el b/packages/json-sexp= -mode/json-sexp-mode.el new file mode 100644 index 0000000..0d21995 --- /dev/null +++ b/packages/json-sexp-mode/json-sexp-mode.el @@ -0,0 +1,112 @@ +;;; json-sexp-mode.el --- Edit JSON in s-expression form. + +;; Copyright (C) 2015 Free Software Foundation, Inc. + +;; Author: Taylan Ulrich Bay=C4=B1rl=C4=B1/Kammer +;; Keywords: data, files + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; Activating `json-sexp-mode' will turn the JSON value in the buffer into= an +;; s-expression, and arrange for it to be transparently converted back int= o a +;; (pretty-printed) JSON value whenever the buffer is saved. Adding +;; `json-sexp-mode' to `auto-mode-alist' can provide a fully transparent +;; experience for editing JSON files as s-expressions. +;; +;; The symbol `false' is used to represent JSON's false value, because nil +;; stands for an empty object. (However, t is used for JSON's true value.) +;; +;; Note that `json-sexp-mode' inherits from `emacs-lisp-mode' since it's f= or +;; editing JSON data as Elisp s-expressions. +;; +;; +;; The following commands can be used to manually convert a region or whole +;; buffer from a JSON value to an s-expression and from an s-expression to= a +;; JSON value: +;; +;; `json-sexp-convert-region-to-sexp' +;; `json-sexp-convert-region-to-json' +;; `json-sexp-convert-buffer-to-sexp' +;; `json-sexp-convert-buffer-to-json' + +;;; Code: +(require 'json) + +(defun json-sexp-convert-region-to-sexp (start end) + "Convert region from JSON to sexps." + (interactive "r") + (unless (=3D start end) + (let ((data (let ((json-object-type 'alist) + (json-false 'false) + (json-null 'null)) + (json-read-from-string (buffer-substring start end)))) + (point (point))) + (delete-region start end) + (goto-char start) + (insert (pp-to-string data)) + ;; Only an approximation of our position in the JSON. + (goto-char point)))) + +(defun json-sexp-convert-region-to-json (start end) + "Convert region from sexps to JSON." + (interactive "r") + (unless (=3D start end) + (let ((data (car (read-from-string (buffer-substring start end)))) + (point (point))) + (delete-region start end) + (goto-char start) + (insert (let ((json-encoding-pretty-print t) + (json-false 'false) + (json-null 'null) + (json-nil 'object)) + (json-encode data))) + ;; Only an approximation of our position in the s-expression. + (goto-char point)))) + +(defun json-sexp-convert-buffer-to-sexp () + "Convert buffer from JSON to sexps." + (interactive) + (json-sexp-convert-region-to-sexp (point-min) (point-max))) + +(defun json-sexp-convert-buffer-to-json () + "Convert buffer from sexps to JSON." + (interactive) + (json-sexp-convert-region-to-json (point-min) (point-max))) + +(defvar json-sexp--saved-point nil) + +(define-derived-mode json-sexp-mode emacs-lisp-mode "JSON-sexp" + "Major mode for editing JSON in s-expression form. +The buffer-contents, which must be JSON, are transformed to +s-expressions when this mode is started, and transformed back +temporarily to JSON whenever the buffer is saved." + (let ((was-modified (buffer-modified-p))) + (json-sexp-convert-buffer-to-sexp) + (set-buffer-modified-p was-modified)) + (add-hook 'before-save-hook 'json-sexp--before-save nil t) + (add-hook 'after-save-hook 'json-sexp--after-save nil t)) + +(defun json-sexp--before-save () + (setq json-sexp--saved-point (point)) + (json-sexp-convert-buffer-to-json)) + +(defun json-sexp--after-save () + (json-sexp-convert-buffer-to-sexp) + (set-buffer-modified-p nil) + (goto-char json-sexp--saved-point)) + +(provide 'json-sexp-mode) +;;; json-sexp-mode.el ends here --=20 2.5.0 --=-=-=--