0

I have a file, "/path/to/token-file.el", which contains an alist:

((access
   ((access_token 123abc456)
    (timestamp
     ((43 5 21 29 12 2021 3 nil 0)
      (Wed, 29 Dec 2021 21:05:43 GMT))))))

I am extracting the alist from the token-file.el with the following Lisp code:

(let ((alist (with-temp-buffer
               (insert-file-contents-literally "/path/to/token-file.el")
               (read (current-buffer)))))
  (message "alist:  %s" alist))

The human readable timestamp is being modified by read:

BEFORE: (Wed, 29 Dec 2021 21:05:43 GMT)

AFTER: (Wed (, 29) Dec 2021 21:05:43 GMT)

How can I transfer the alist from the file to a let-bound variable without modifying the timestamp?

1 Answer 1

2

You know from your recent Q&A why the comma is being handled that way.

The fact is that read is for reading lisp syntax into lisp objects, and so you can only expect the results to be as they would be if the input was lisp.

(Bearing in mind that Emacs Lisp does not support user-defined reader macros, so we have no ability to manipulate how things will be read.)

I think you should make that part of the input file a double-quoted string (you would in general still need to ensure that such strings adhered to the read syntax for strings), and then it will be read as a string.


FYI if you backquote the list before reading it, then the printed representation is different to what you're seeing now, but it still represents the objects created by the lisp reader in a different format to the original input string (while still being equivalent as lisp).

(let ((alist (with-temp-buffer
               (insert "`")
               (insert-file-contents-literally "~/token-file.el")
               (goto-char (point-min))
               (read (current-buffer)))))
  (message "%s" alist))
"`((access ((access_token 123abc456)
            (timestamp ((43 5 21 29 12 2021 3 nil 0)
                        (Wed ,29 Dec 2021 21:05:43 GMT))))))"

Note the shift from Wed, 29 to Wed ,29 (which is again familiar from the earlier Q&A).

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.