Skip to Content
Writing GNU Emacs Extensions
book

Writing GNU Emacs Extensions

by Bob Glickstein
April 1997
Intermediate to advanced
240 pages
5h 56m
English
O'Reilly Media, Inc.
Content preview from Writing GNU Emacs Extensions

Circular Lists?!

Because we can destructively modify lists after they're created, we're not limited to building lists only out of pre-existing parts. A list can be made to refer to part of itself! Consider:

(setq x '(a b c))
(nthcdr 2 x) ⇒ (c)
(setcdr (nthcdr 2 x) x)      ;don't try this yet!

What's happening in this example? First we create a three-element list and place it in x. Next, we find the last cons cell with nthcdr . Finally, we replace that cell's cdr with x—which is the first cons cell in the list. The list is now circular: the former end of the list now points back to the beginning.

What does this list look like? Well, it starts out like this:

(a b c a b c a b c a b c a b c a b c a b c a b c ...

and it never stops. The reason I wrote don't try this yet! above is that if you executed the setcdr in the *scratch* buffer, Emacs would try to display the result—and would never finish. It would get caught in an infinite loop, albeit one that you can interrupt with C-g. Go ahead and try it now, but press C-g as soon as Emacs locks up. The longer you wait, the more it fills up the *scratch* buffer with repetitions of a b c.

Obviously, printing isn't the only thing you can do to a circular data structure that can make Emacs loop forever. Any operation that iterates over all the elements in a list will never terminate. Here's an important illustration:

(eq x (nthcdr 3 x)) ⇒ t       ;3rd cdr is same object as x
(equal x (nthcdr 3 x)) ⇒ never terminates!

If circular lists throw Emacs for a loop (pun ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GNU Emacs Pocket Reference

GNU Emacs Pocket Reference

Debra Cameron
Learning GNU Emacs, Second Edition

Learning GNU Emacs, Second Edition

Debra Cameron, Bill Rosenblatt, Eric S. Raymond

Publisher Resources

ISBN: 9781449395056Errata Page