20

I am dealing with JSON files with multiple repeated blocks with just one field value (integer) changed - in fact, every changed value is just the previous corresponding value incremented. To make such files easy to read, I would like to be able to fold this entire set of repeated blocks into just one block with some indication of what the ranges of the changing value across these blocks is. Searching for code folding methods in Emacs, I found hs-minor-mode and outline-mode. However, I don't see how to go from the functionality they provide to the one I desire. Could someone provide a rough overview of how I could achieve this?

An example of what I am trying to do:

Given a JSON file that looks like this:

{
    "SomeArray": [
        {
        "ChangingField": 1,
        "ConstantField" : "X"
        },
        {
        "ChangingField": 2,
        "ConstantField" : "X"
        },
        {
        "ChangingField": 3,
        "ConstantField" : "X"
        },
        {
        "ChangingField": 4,
        "ConstantField" : "X"
        },
        {
        "ChangingField": 5,
        "ConstantField" : "X"
        }
    ]
}

I would like to have it displayed like this:

{
    "SomeArray": [
        {
        "ChangingField": 1...5,
        "ConstantField" : "X"
        }
    ]
}

Apologies for the very open-ended question.

1
  • 1
    Good question - that would be quite useful.
    – Drew
    Feb 13, 2015 at 14:59

2 Answers 2

9

There are more folding options for Emacs: http://wikemacs.org/wiki/Folding#Yafolding and Yafolding looks like the best for your case since it is based on indentation. It also includes a nice interface à-la Magit to discover the commands.

However, that doesn't fully answer your question since it won't show the range of the fields out of the box.

Yafolding is in MELPA:

M-x package-install yafolding
5

Use emacs' hideshow. You can keep your cursor anywhere & to hide blocks 'n' level below it, use the command

C-c @ C-l

Here, n is a numeric prefix argument that gets set by pressing

C-u 1 RET

Read more about hsmode here, hs-mode. More about prefix arguments here, prefix args hs mode doesn't work on Default mode. You need to have js2-mode (or) js-mode in place for this to work.

2
  • Out of the box, Emacs opens JSON files in Fundamental mode, which hs-minor-mode refuses to run in. It also rejects basic text mode. I worked around this by specifying c-mode (sic) for the JSON file where I wanted to use folding. While the workaround is admittedly horribly crude, it removes the need to use a third-party js-mode just for this. I guess whatever programming mode you are not currently using should be fine for the same purpose (java-mode works fine too; sadly, pascal-mode doesn't, or rather, it doesn't regard curly braces as a structure you can fold).
    – tripleee
    Aug 2, 2023 at 14:10
  • See also emacs.stackexchange.com/questions/41060/… for more options
    – tripleee
    Aug 21, 2023 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.