1

In Emacs I can evaluate the following to get a list of holidays:

(list-holidays 2020 2021)

But I need to have the list written to a file. I have tried this:

(progn (list-holidays 2020 2021) (write-file \"myholidays.txt\"))

But no luck. How can I write the holiday list to a file?

1
1

list-holidays opens a new buffer called *Holidays* and makes it visible, but leaves the current buffer unchanged. Meanwhile write-file writes the contents of the current buffer to a file. Thus your code writes the wrong thing to the file. You can call switch-to-buffer to change which buffer is current, but you probably also want to restore the window configuration afterwards.

You probably want to do something like this:

(save-window-excursion
  (list-holidays 2020 2021)
  (switch-to-buffer "*Holidays*")
  (write-file "~/temp/myholidays.txt"))

save-window-excursion saves the current window configuration, runs the code you give it just like progn would, then restores the window configuration. It also saves the state of the buffer you are in as well (point and mark and suchlike).

The code inside opens the holidays window, switches to it, and then writes it to a file.

Edit: Incidentally, I took a peek at the list-holidays function before writing my answer, because as lawlist says there could be a better function to use. Ideally there would be one that returns a string, or just puts the text in the current buffer. Sadly no such function exists, though it wouldn’t be terribly hard to split the current list-holidays in half if you wanted.

0

I do not know if there is a built-in function similar to list-holidays that returns the list as a string. If no such function exists, then I would be inclined to take the function list-holidays and modify it to suit my needs ... beginning by typing M-x find-function RET list-holidays RET and copying the function to a *scratch* buffer and modifying it accordingly. However, I saw what makes it tick and see that the output is to a buffer defined as the holiday-buffer -- as such, the following may suffice to meet the needs of the O.P. without further ado:

(save-window-excursion
  (require 'holidays) ;; just in case it is not already loaded 
  (when (get-buffer holiday-buffer)
    (with-current-buffer holiday-buffer
      (let ((inhibit-read-only t))
        (erase-buffer))))
  (list-holidays 2020 2021)
  (with-temp-file "/Users/HOME/Desktop/myholidays.txt"
    (insert-buffer holiday-buffer))
  (kill-buffer holiday-buffer))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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