unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Add json-sexp-mode.
@ 2015-10-17 16:27 Taylan Ulrich Bayırlı/Kammer
  2015-10-17 17:19 ` Artur Malabarba
  0 siblings, 1 reply; 10+ messages in thread
From: Taylan Ulrich Bayırlı/Kammer @ 2015-10-17 16:27 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 19 bytes --]

This is for ELPA.


[-- Attachment #2: 0001-Add-json-sexp-mode.patch --]
[-- Type: text/x-diff, Size: 5092 bytes --]

From 50ec911133b23aaa959795e80e09283794bafd19 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Taylan=20Ulrich=20Bay=C4=B1rl=C4=B1/Kammer?=
 <taylanbayirli@gmail.com>
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ırlı/Kammer <taylanbayirli@gmail.com>
+;; 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 <http://www.gnu.org/licenses/>.
+
+;;; 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 into 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 for
+;; 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 (= 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 (= 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
-- 
2.5.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] Add json-sexp-mode.
  2015-10-17 16:27 [PATCH] Add json-sexp-mode Taylan Ulrich Bayırlı/Kammer
@ 2015-10-17 17:19 ` Artur Malabarba
  2015-10-17 18:10   ` Taylan Ulrich Bayırlı/Kammer
  0 siblings, 1 reply; 10+ messages in thread
From: Artur Malabarba @ 2015-10-17 17:19 UTC (permalink / raw)
  To: Taylan Ulrich Bayırlı/Kammer; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 523 bytes --]

> +;; Note that `json-sexp-mode' inherits from `emacs-lisp-mode' since it's
for
> +;; editing JSON data as Elisp s-expressions.

It might be more appropriate to inherit from lisp-mode, which is parent to
emacs-lisp-mode. It should still provide all of the syntax definitions you
need without carrying a lot of key binds that don't make sense for your
mode (like eval-defun or eval-last-sexp).

I'm not sure which version of emacs added this mode, so you might have to
add a dependency on emacs 24.3 or something like that.

[-- Attachment #2: Type: text/html, Size: 612 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Add json-sexp-mode.
  2015-10-17 17:19 ` Artur Malabarba
@ 2015-10-17 18:10   ` Taylan Ulrich Bayırlı/Kammer
  2015-10-17 21:47     ` Artur Malabarba
  0 siblings, 1 reply; 10+ messages in thread
From: Taylan Ulrich Bayırlı/Kammer @ 2015-10-17 18:10 UTC (permalink / raw)
  To: Artur Malabarba; +Cc: emacs-devel

Artur Malabarba <bruce.connor.am@gmail.com> writes:

>> +;; Note that `json-sexp-mode' inherits from `emacs-lisp-mode' since
> it's for 
>> +;; editing JSON data as Elisp s-expressions. 
>
> It might be more appropriate to inherit from lisp-mode, which is
> parent to emacs-lisp-mode. It should still provide all of the syntax
> definitions you need without carrying a lot of key binds that don't
> make sense for your mode (like eval-defun or eval-last-sexp). 
>
> I'm not sure which version of emacs added this mode, so you might have
> to add a dependency on emacs 24.3 or something like that. 

json.el yields Elisp s-expressions though.  The closest approximation to
an "Elisp s-expression editing mode" is emacs-lisp-mode.

Taylan



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Add json-sexp-mode.
  2015-10-17 18:10   ` Taylan Ulrich Bayırlı/Kammer
@ 2015-10-17 21:47     ` Artur Malabarba
  2015-10-17 22:08       ` Taylan Ulrich Bayırlı/Kammer
  0 siblings, 1 reply; 10+ messages in thread
From: Artur Malabarba @ 2015-10-17 21:47 UTC (permalink / raw)
  To: Taylan Ulrich Bayırlı/Kammer; +Cc: emacs-devel

2015-10-17 19:10 GMT+01:00 Taylan Ulrich Bayırlı/Kammer
<taylanbayirli@gmail.com>:
>
> json.el yields Elisp s-expressions though.  The closest approximation to
> an "Elisp s-expression editing mode" is emacs-lisp-mode.

I've got something even better for you then. You can change your mode
to inherit from prog-mode and add this line:
    (lisp-mode-variables nil nil 'elisp)

This should make sure it has all the syntax definitions of
emacs-lisp-mode, without carrying its keybinds, eldoc, completion
functions, xref definitions, search-path, menu-bars, etc, which (IIUC)
wouldn't make sense for this json-sexp-mode.

Of course, it's up to you. I'm just recommending what I would do.


Cheers,
Artur



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Add json-sexp-mode.
  2015-10-17 21:47     ` Artur Malabarba
@ 2015-10-17 22:08       ` Taylan Ulrich Bayırlı/Kammer
  2015-10-19 12:36         ` Taylan Ulrich Bayırlı/Kammer
  0 siblings, 1 reply; 10+ messages in thread
From: Taylan Ulrich Bayırlı/Kammer @ 2015-10-17 22:08 UTC (permalink / raw)
  To: Artur Malabarba; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1078 bytes --]

Artur Malabarba <bruce.connor.am@gmail.com> writes:

> 2015-10-17 19:10 GMT+01:00 Taylan Ulrich Bayırlı/Kammer
> <taylanbayirli@gmail.com>:
>>
>> json.el yields Elisp s-expressions though.  The closest approximation to
>> an "Elisp s-expression editing mode" is emacs-lisp-mode.
>
> I've got something even better for you then. You can change your mode
> to inherit from prog-mode and add this line:
>     (lisp-mode-variables nil nil 'elisp)
>
> This should make sure it has all the syntax definitions of
> emacs-lisp-mode, without carrying its keybinds, eldoc, completion
> functions, xref definitions, search-path, menu-bars, etc, which (IIUC)
> wouldn't make sense for this json-sexp-mode.
>
> Of course, it's up to you. I'm just recommending what I would do.

Ooh, thanks, that seems to be a good way to do it.

(Some people might have to add paredit-mode to json-sexp-mode-hook when
previously emacs-lisp-mode-hook would enable it for them, but that
should be fine.  I personally enable Paredit in prog-mode-hook.)

Here's the updated patch.


[-- Attachment #2: 0001-Add-json-sexp-mode.patch --]
[-- Type: text/x-diff, Size: 5000 bytes --]

From ff35fbca2ff8b818ce47efe2bb1d07f5f70a6051 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Taylan=20Ulrich=20Bay=C4=B1rl=C4=B1/Kammer?=
 <taylanbayirli@gmail.com>
Date: Sat, 17 Oct 2015 17:15:23 +0200
Subject: [PATCH 1/2] Add json-sexp-mode.

---
 packages/json-sexp-mode/json-sexp-mode.el | 110 ++++++++++++++++++++++++++++++
 1 file changed, 110 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..689ba8c
--- /dev/null
+++ b/packages/json-sexp-mode/json-sexp-mode.el
@@ -0,0 +1,110 @@
+;;; json-sexp-mode.el --- Edit JSON in s-expression form.
+
+;; Copyright (C) 2015  Free Software Foundation, Inc.
+
+;; Author: Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com>
+;; 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 <http://www.gnu.org/licenses/>.
+
+;;; 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 into 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.)
+;;
+;;
+;; 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 (= 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 (= 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 prog-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."
+  (lisp-mode-variables nil nil 'elisp)
+  (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
-- 
2.5.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] Add json-sexp-mode.
  2015-10-17 22:08       ` Taylan Ulrich Bayırlı/Kammer
@ 2015-10-19 12:36         ` Taylan Ulrich Bayırlı/Kammer
  2015-10-19 13:02           ` Artur Malabarba
  0 siblings, 1 reply; 10+ messages in thread
From: Taylan Ulrich Bayırlı/Kammer @ 2015-10-19 12:36 UTC (permalink / raw)
  To: Artur Malabarba; +Cc: emacs-devel

taylanbayirli@gmail.com (Taylan Ulrich "Bayırlı/Kammer") writes:

> Here's the updated patch.
>
> [...]

Can we push this?  Or do I get push access?

Taylan



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Add json-sexp-mode.
  2015-10-19 12:36         ` Taylan Ulrich Bayırlı/Kammer
@ 2015-10-19 13:02           ` Artur Malabarba
  2015-10-19 17:09             ` Taylan Ulrich Bayırlı/Kammer
  0 siblings, 1 reply; 10+ messages in thread
From: Artur Malabarba @ 2015-10-19 13:02 UTC (permalink / raw)
  To: Taylan Ulrich Bayırlı/Kammer; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 391 bytes --]

I can push it for you if someone else confirms to me that your copyright
paperwork is on file (I can't check).

2015-10-19 13:36 GMT+01:00 Taylan Ulrich Bayırlı/Kammer <
taylanbayirli@gmail.com>:

> taylanbayirli@gmail.com (Taylan Ulrich "Bayırlı/Kammer") writes:
>
> > Here's the updated patch.
> >
> > [...]
>
> Can we push this?  Or do I get push access?
>
> Taylan
>

[-- Attachment #2: Type: text/html, Size: 852 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Add json-sexp-mode.
  2015-10-19 13:02           ` Artur Malabarba
@ 2015-10-19 17:09             ` Taylan Ulrich Bayırlı/Kammer
  2015-10-19 18:48               ` Artur Malabarba
  0 siblings, 1 reply; 10+ messages in thread
From: Taylan Ulrich Bayırlı/Kammer @ 2015-10-19 17:09 UTC (permalink / raw)
  To: Artur Malabarba; +Cc: emacs-devel

Artur Malabarba <bruce.connor.am@gmail.com> writes:

> I can push it for you if someone else confirms to me that your
> copyright paperwork is on file (I can't check).

Thanks but please don't bother.  I won't be contributing to Emacs
upstream until there's a change in the community of core developers.

Taylan



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Add json-sexp-mode.
  2015-10-19 17:09             ` Taylan Ulrich Bayırlı/Kammer
@ 2015-10-19 18:48               ` Artur Malabarba
  2015-10-19 19:59                 ` Taylan Ulrich Bayırlı/Kammer
  0 siblings, 1 reply; 10+ messages in thread
From: Artur Malabarba @ 2015-10-19 18:48 UTC (permalink / raw)
  To: Taylan Ulrich Bayırlı/Kammer; +Cc: emacs-devel

2015-10-19 18:09 GMT+01:00 Taylan Ulrich Bayırlı/Kammer
<taylanbayirli@gmail.com>:
> Thanks but please don't bother.  I won't be contributing to Emacs
> upstream until there's a change in the community of core developers.

That's a shame. Everyone here is just trying to help improve the code
that goes into Emacs.
I hope you'll reconsider after sleeping on it.


Artur



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Add json-sexp-mode.
  2015-10-19 18:48               ` Artur Malabarba
@ 2015-10-19 19:59                 ` Taylan Ulrich Bayırlı/Kammer
  0 siblings, 0 replies; 10+ messages in thread
From: Taylan Ulrich Bayırlı/Kammer @ 2015-10-19 19:59 UTC (permalink / raw)
  To: Artur Malabarba; +Cc: emacs-devel

Artur Malabarba <bruce.connor.am@gmail.com> writes:

> Everyone here is just trying to help improve the code that goes into
> Emacs.

Don't make me laugh. :-)

Some people seem only interested in keeping out new contributors.

It's not that I believe that is their conscious intent, but it's the
effect anyhow.

Taylan



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-10-19 19:59 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-17 16:27 [PATCH] Add json-sexp-mode Taylan Ulrich Bayırlı/Kammer
2015-10-17 17:19 ` Artur Malabarba
2015-10-17 18:10   ` Taylan Ulrich Bayırlı/Kammer
2015-10-17 21:47     ` Artur Malabarba
2015-10-17 22:08       ` Taylan Ulrich Bayırlı/Kammer
2015-10-19 12:36         ` Taylan Ulrich Bayırlı/Kammer
2015-10-19 13:02           ` Artur Malabarba
2015-10-19 17:09             ` Taylan Ulrich Bayırlı/Kammer
2015-10-19 18:48               ` Artur Malabarba
2015-10-19 19:59                 ` Taylan Ulrich Bayırlı/Kammer

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).