Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3979918
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:13:44+00:00 2026-05-20T05:13:44+00:00

I use the Emacs desktop module to save my open buffers between sessions. However

  • 0

I use the Emacs desktop module to save my open buffers between sessions. However I found that this accumulates more buffers than I want, so I wrote a small function to clean up the buffer list immediately before saving to the desktop file. This works as expected, but for strange reasons the .emacs.desktop gets scrambled occasionally, i.e. it contains a part of another buffer at its start, then the intended contents and then the result of the other buffer. I don’t have the slightest idea, why this happens. Here is an excerpt from my .emacs file:

(defun kill-old-buffers ()
  "Kill buffers from end of buffer list (not used recently) until no more than 50 buffers are left. Remove temporary buffers first"
  (interactive)
  (let* (desktop-buffer (current-buffer))
   (dolist (buffer (buffer-list))
     (if (or (string-match "^\*" (buffer-name buffer)) (string-match "\.hpp$" (buffer-name buffer)))
         (kill-buffer buffer)
       )
     )

  (setq all-buffers (reverse (buffer-list)))
  (while (> (safe-length all-buffers) 50)
    (setq buffer (pop all-buffers))
    (if (not (string-equal (buffer-name buffer) (buffer-name (current-buffer))))
        (kill-buffer buffer)
      )
    )
  (switch-to-buffer desktop-buffer)
))

;; Kill old rarely-used buffers before saving
(add-hook 'desktop-save-hook
  '(lambda () (kill-old-buffers)))

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:13:45+00:00Added an answer on May 20, 2026 at 5:13 am

    I’m not sure if your function is really the cause of your problem. If it should be the case, the wrong usage of the let* that scottfrazer pointed out might be the cause. But you don’t even need that let* (and switch-to-buffer) at all, because

    1. what you’re trying to do is better done with Emacs’ built in save-excursion, and
    2. you aren’t actually switching the buffer ever.

    OTOH, you should have used let instead of the setqs in the lower half of your function, because setq will otherwise change a variable from an enclosing lexical scope. In this case you might very well have stomped over a buffer variable from the function that’s executing the desktop-save-hook which is another potential cause of your problem.

    But you don’t need those lets either because you can do the second loop with another dolist. You can get rid of those first 50 buffers that you don’t want to loop over with nthcdr.

    Here’s my improved version of kill-old-buffers:

    (defun kill-old-buffers ()
      "Kill buffers from end of buffer list (not used recently) until
    no more than 50 buffers are left. Remove temporary buffers first."
      (interactive)
      (save-excursion
        (dolist (buffer (buffer-list))
          (if (or (string-match "^\*" (buffer-name buffer))
                  (string-match "\.hpp$" (buffer-name buffer)))
              (kill-buffer buffer)))
    
        (dolist (buffer (reverse (nthcdr 50 (buffer-list))))
          (unless (eq buffer (current-buffer))
            (kill-buffer buffer)))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two sessions open in Emacs-ESS: one on my desktop and one on
I've started to use emacs, and I've found out that while the editor-specific features
I like to use emacsclient to open new emacs frames on different virtual desktops.
I'm giving ErgoEmacs mode a try to see if I can use Emacs more
This question is, as indicated, for those who use Emacs. When you do, do
I want to index clojure files, using etags so that i can use Emacs's
I use emacs-server so that I can set emacsclient as the default texteditor and
I use Emacs and am currently working on projects that use different versions of
I have a TODO file that I load emacs up to use 90% of
When I use emacs from a terminal (xterm-color; a putty ssh session in this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.