Elisp:如何将数据保存到文件中? [英] Elisp: How to save data in a file?

查看:113
本文介绍了Elisp:如何将数据保存到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据保存到elisp程序中的文件中.我有一个要保存到文件的多维列表,因此可以在下次程序运行时将其还原.最简单/最好的方法是什么?

I want to save data to a file in my elisp program. I have a multi-dimensional list that I want to save to a file, so I can restore it the next time my program runs. What's the easiest / best way to do this?

当然,我意识到我可以简单地将数据以自定义格式写入缓冲区,然后保存该缓冲区,但是当我想还原该数据格式时,我必须编写一个函数来解析该数据格式.我宁愿不必那样做.

I realise, of course, that I can simply write my data to a buffer in a custom format and then save the buffer, but then I'd have to write a function to parse that data format when I want to restore it. I'd rather not have to do that.

在Python中,有一个Pickle模块,可让您非常轻松地将对象转储"到磁盘并还原它们. elisp有类似的东西吗?

In Python, there's the Pickle module that lets you "dump" objects to disk and restore them, very easily. Is there something similar for elisp?

推荐答案

此'dump-vars-to-file例程将创建一些表达式,稍后可以通过简单地对表达式进行评估(通过'load命令或'read)来读取这些表达式:

This 'dump-vars-to-file routine will create some expressions that can be read by simply evaluating the expressions later (via a 'load command or 'read):

(defun dump-vars-to-file (varlist filename)
  "simplistic dumping of variables in VARLIST to a file FILENAME"
  (save-excursion
    (let ((buf (find-file-noselect filename)))
      (set-buffer buf)
      (erase-buffer)
      (dump varlist buf)
      (save-buffer)
      (kill-buffer))))

(defun dump (varlist buffer)
  "insert into buffer the setq statement to recreate the variables in VARLIST"
  (loop for var in varlist do
        (print (list 'setq var (list 'quote (symbol-value var)))
               buffer)))

我确定我缺少一些内置的例程,它们能做得更好或更灵活.

I'm sure I'm missing some built-in routine that does a nicer job or is more flexible.

我用这个小例程对其进行了测试:

I tested it with this little routine:

(defun checkit ()
  (let ((a '(1 2 3 (4 5)))
        (b '(a b c))
        (c (make-vector 3 'a)))
    (dump-vars-to-file '(a b c) "/some/path/to/file.el")))

哪个产生了输出:

(setq a (quote (1 2 3 (4 5))))
(setq b (quote (a b c)))
(setq c (quote [a a a]))

有关更多信息,请参见

For more information, see the info page on reading and printing lisp objects

这篇关于Elisp:如何将数据保存到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆