gpt4 book ai didi

emacs - emacs lisp 中惯用的序列化方式

转载 作者:太空宇宙 更新时间:2023-11-03 18:39:49 24 4
gpt4 key购买 nike

目前我正在研究一种跨 session 使用哈希表的 elisp 主要模式。所以每次初始化主要模​​式时,表都会加载到内存中。在 session 期间和结束时,它们被写入文件。我当前的实现以下列方式写入数据:

(with-temp-buffer
(prin1 hash-table (current-buffer))
(write-file ("path/to/file.el"))))

在 session 开始时加载数据是通过读取完成的,是这样的:

(setq name-of-table (car
(read-from-string
(with-temp-buffer
(insert-file-contents path-of-file)
(buffer-substring-no-properties
(point-min)
(point-max))))))))

它有效,但我觉得这不是最漂亮的方法。我的目标是:我希望这个主要模式变成一个漂亮的干净包,将它自己的数据存储在存储包的其他数据的文件夹中。

最佳答案

我是这样实现的

写入文件:

(defun my-write (file data)
(with-temp-file file
(prin1 data (current-buffer))))

从文件中读取:

(defun my-read (file symbol)
(when (boundp symbol)
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(set symbol (read (current-buffer))))))

调用写入:

(my-write "~/test.txt" emacs-version)

调用阅读

(my-read "~/test.txt" 'my-emacs-version)

关于emacs - emacs lisp 中惯用的序列化方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36193866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com