2

I have the following structure of list and want to prettify it.

The list is composed of elements as in:

((p1 . q1) . text) ((p1 . q2) . text) 
((p2 . q3) . text) ((p2 . q4) . text) 

And want to isolate each list with identical p.

Thusly, some structure similar to:

((p1 . q1) . text) 
((p1 . q2) . text) 

((p2 . q3) . text) 
((p2 . q4) . text) 

Here is the actual list

'((((1 . 3) . "This buffer is for text") ((1 . 5) . "This buffer is for text")) (((1 . 3) . "buffer is for text that") ((1 . 5) . "buffer is for text that")) (((1 . 3) . "is for text that is") ((1 . 5) . "is for text that is")) (((1 . 3) . "for text that is not") ((1 . 5) . "for text that is not")) (((1 . 3) . "text that is not saved") ((1 . 5) . "text that is not saved")) (((1 . 3) . "that is not saved and") ((1 . 5) . "that is not saved and")) (((1 . 3) . "is not saved and for") ((1 . 5) . "is not saved and for")) (((1 . 3) . "not saved and for Lisp") ((1 . 5) . "not saved and for Lisp")) (((1 . 3) . "saved and for Lisp evaluation") ((1 . 5) . "saved and for Lisp evaluation")) (((2 . 3) . "and for Lisp evaluation To") ((2 . 5) . "and for Lisp evaluation To")) (((2 . 3) . "for Lisp evaluation To create") ((2 . 5) . "for Lisp evaluation To create")) (((2 . 3) . "Lisp evaluation To create a") ((2 . 5) . "Lisp evaluation To create a")) (((2 . 3) . "evaluation To create a file") ((2 . 5) . "evaluation To create a file")) (((2 . 4) . "To create a file visit") ((2 . 6) . "To create a file visit")) (((2 . 4) . "create a file visit it") ((2 . 6) . "create a file visit it")) (((2 . 4) . "a file visit it with") ((2 . 6) . "a file visit it with")) (((2 . 4) . "file visit it with C") ((2 . 6) . "file visit it with C")) (((2 . 4) . "visit it with C x") ((2 . 6) . "visit it with C x")) (((2 . 4) . "it with C x C") ((2 . 6) . "it with C x C")) (((2 . 4) . "with C x C f") ((2 . 6) . "with C x C f")) (((2 . 4) . "C x C f and") ((2 . 6) . "C x C f and")) (((2 . 4) . "x C f and enter") ((2 . 6) . "x C f and enter")) (((2 . 4) . "C f and enter text") ((2 . 6) . "C f and enter text")) (((2 . 4) . "f and enter text in") ((2 . 6) . "f and enter text in")) (((2 . 4) . "and enter text in its") ((2 . 6) . "and enter text in its")) (((2 . 4) . "enter text in its buffer") ((2 . 6) . "enter text in its buffer")) (((3 . 4) . "text in its buffer This")) (((3 . 4) . "in its buffer This buffer")) (((3 . 4) . "its buffer This buffer is")) (((3 . 4) . "buffer This buffer is for")) (((3 . 5) . "This buffer is for text")) (((3 . 5) . "buffer is for text that")) (((3 . 5) . "is for text that is")) (((3 . 5) . "for text that is not")) (((3 . 5) . "text that is not saved")) (((3 . 5) . "that is not saved and")) (((3 . 5) . "is not saved and for")) (((3 . 5) . "not saved and for Lisp")) (((3 . 5) . "saved and for Lisp evaluation")) (((4 . 5) . "and for Lisp evaluation To")) (((4 . 5) . "for Lisp evaluation To create")) (((4 . 5) . "Lisp evaluation To create a")) (((4 . 5) . "evaluation To create a file")) (((4 . 6) . "To create a file visit")) (((4 . 6) . "create a file visit it")) (((4 . 6) . "a file visit it with")) (((4 . 6) . "file visit it with C")) (((4 . 6) . "visit it with C x")) (((4 . 6) . "it with C x C")) (((4 . 6) . "with C x C f")) (((4 . 6) . "C x C f and")) (((4 . 6) . "x C f and enter")) (((4 . 6) . "C f and enter text")) (((4 . 6) . "f and enter text in")) (((4 . 6) . "and enter text in its")) (((4 . 6) . "enter text in its buffer")))
dalanicolai
  • 6,108
  • 7
  • 23
Dilna
  • 1,173
  • 3
  • 10
  • 1
    Your example list is a list of lists, each containing two conses, and it is not clear how you'd like to have them printed. Should it only print the group and print the individual conses? Or should it become a flat list (like your example seems to suggest)? Or should it become a nested list? Please improve (clarify) the question... – dalanicolai Oct 24 '22 at 22:21
  • 1
    I can actually keep it as a nested list. With the suggestion of `pp-buffer`, I have found `(pp LIST)`. Are there other possibilities, interteresting but still quite simple? – Dilna Oct 24 '22 at 22:34
  • 1
    Pretty-printing reformats the list but does not change the order, so if you are trying to bring all the `p1` things togethere, `pp` is not enough. – NickD Oct 25 '22 at 01:05
  • 1
    I *think* you may be asking how to sort a list using sublist elements as key -- please clarify. – Phil Hudson Oct 26 '22 at 18:32

1 Answers1

1

One quick way to do this that doesn't require any programming is to put the actual list in an elisp buffer all by itself and run M-x pp-buffer on it. The end result I received looked like this.

((((1 . 3)
   . This buffer is for text)
  ((1 . 5)
   . This buffer is for text))
 (((1 . 3)
   . buffer is for text that)
  ((1 . 5)
   . buffer is for text that))
 (((1 . 3)
   . is for text that is)
  ((1 . 5)
   . is for text that is))
 (((1 . 3)
   . for text that is not)
  ((1 . 5)
   . for text that is not))
 (((1 . 3)
   . text that is not saved)
  ((1 . 5)
   . text that is not saved))
 (((1 . 3)
   . that is not saved and)
  ((1 . 5)
   . that is not saved and))
 (((1 . 3)
   . is not saved and for)
  ((1 . 5)
   . is not saved and for))
 (((1 . 3)
   . not saved and for Lisp)
  ((1 . 5)
   . not saved and for Lisp))
 (((1 . 3)
   . saved and for Lisp evaluation)
  ((1 . 5)
   . saved and for Lisp evaluation))
 (((2 . 3)
   . and for Lisp evaluation To)
  ((2 . 5)
   . and for Lisp evaluation To))
 (((2 . 3)
   . for Lisp evaluation To create)
  ((2 . 5)
   . for Lisp evaluation To create))
 (((2 . 3)
   . Lisp evaluation To create a)
  ((2 . 5)
   . Lisp evaluation To create a))
 (((2 . 3)
   . evaluation To create a file)
  ((2 . 5)
   . evaluation To create a file))
 (((2 . 4)
   . To create a file visit)
  ((2 . 6)
   . To create a file visit))
 (((2 . 4)
   . create a file visit it)
  ((2 . 6)
   . create a file visit it))
 (((2 . 4)
   . a file visit it with)
  ((2 . 6)
   . a file visit it with))
 (((2 . 4)
   . file visit it with C)
  ((2 . 6)
   . file visit it with C))
 (((2 . 4)
   . visit it with C x)
  ((2 . 6)
   . visit it with C x))
 (((2 . 4)
   . it with C x C)
  ((2 . 6)
   . it with C x C))
 (((2 . 4)
   . with C x C f)
  ((2 . 6)
   . with C x C f))
 (((2 . 4)
   . C x C f and)
  ((2 . 6)
   . C x C f and))
 (((2 . 4)
   . x C f and enter)
  ((2 . 6)
   . x C f and enter))
 (((2 . 4)
   . C f and enter text)
  ((2 . 6)
   . C f and enter text))
 (((2 . 4)
   . f and enter text in)
  ((2 . 6)
   . f and enter text in))
 (((2 . 4)
   . and enter text in its)
  ((2 . 6)
   . and enter text in its))
 (((2 . 4)
   . enter text in its buffer)
  ((2 . 6)
   . enter text in its buffer))
 (((3 . 4)
   . text in its buffer This))
 (((3 . 4)
   . in its buffer This buffer))
 (((3 . 4)
   . its buffer This buffer is))
 (((3 . 4)
   . buffer This buffer is for))
 (((3 . 5)
   . This buffer is for text))
 (((3 . 5)
   . buffer is for text that))
 (((3 . 5)
   . is for text that is))
 (((3 . 5)
   . for text that is not))
 (((3 . 5)
   . text that is not saved))
 (((3 . 5)
   . that is not saved and))
 (((3 . 5)
   . is not saved and for))
 (((3 . 5)
   . not saved and for Lisp))
 (((3 . 5)
   . saved and for Lisp evaluation))
 (((4 . 5)
   . and for Lisp evaluation To))
 (((4 . 5)
   . for Lisp evaluation To create))
 (((4 . 5)
   . Lisp evaluation To create a))
 (((4 . 5)
   . evaluation To create a file))
 (((4 . 6)
   . To create a file visit))
 (((4 . 6)
   . create a file visit it))
 (((4 . 6)
   . a file visit it with))
 (((4 . 6)
   . file visit it with C))
 (((4 . 6)
   . visit it with C x))
 (((4 . 6)
   . it with C x C))
 (((4 . 6)
   . with C x C f))
 (((4 . 6)
   . C x C f and))
 (((4 . 6)
   . x C f and enter))
 (((4 . 6)
   . C f and enter text))
 (((4 . 6)
   . f and enter text in))
 (((4 . 6)
   . and enter text in its))
 (((4 . 6)
   . enter text in its buffer)))

I could not find an interactive version of this that just worked on a region. That's the kind of function I would have liked to use.

g-gundam
  • 1,096
  • 1
  • 3
  • 13
  • 1
    You can make an itteractive function that calls `pp`. – Dilna Oct 24 '22 at 22:42
  • Someone should write an interactive `pp-region` function as a companion to `pp-buffer` and contribute it to upstream Emacs. I'm really surprised it doesn't already exist. – g-gundam Oct 24 '22 at 22:45
  • 2
    @g-gundam You can use [narrowing](https://www.gnu.org/software/emacs/manual/html_node/emacs/Narrowing.html) for that. Also see [here](https://www.gnu.org/software/emacs/manual/html_node/emacs/Narrowing.html). – dalanicolai Oct 25 '22 at 05:31
  • 2
    @dalanicolai Thanks for the tip. I've never used narrowing, because I didn't have a feel for when it would be useful, but now I have a better idea. – g-gundam Oct 25 '22 at 06:25