emacs - `with-temp-file' 与 `org-map-entries' 的行为

标签 emacs elisp org-mode

(2012-12-12:编辑以澄清问题

  • 添加了症状描述
  • 明确了第一段代码中temp-file的变量定义)

我正在尝试让这段代码正常工作。这个想法是将 org-map-entries 应用于由“with-temp-file”创建的新文件,并且仍在“with-temp-file sexp:

(let ((temp-file (expand-file-name "test-tmp-sandbox.org" dir)))
  (with-temp-file temp-file
    (insert-file (expand-file-name "my-org-file.org" dir))
    (org-mode)
    (org-map-entries '(org-entry-put nil "MY-READ-ONLY" ?t) t 'file)))

它不起作用。

  • 没有崩溃:好的
  • 创建并填充了临时文件:确定
  • 当我手动打开文件时,文件处于组织模式,组织标题正常
  • 但是我想用 org-map-entries 映射的函数 org-entry-put 不适用就好像没有 org-entries,而且确实有几个可能是(组织模式)未应用

我有以下解决方法:

(let ((temp-file (expand-file-name "test-tmp-sandbox.org" dir)))
  (with-temp-file temp-file
    (insert-file (expand-file-name "my-org-file.org" dir)))
  (find-file temp-file)
  (org-map-entries '(org-entry-put nil "MY-READ-ONLY" ?t) t 'file)
  )

哪个:

  • 填写文件:好的
  • 完成映射:好的
  • 但是很尴尬!

知道第一段代码出了什么问题吗?

最佳答案

  1. 你不要说什么“不起作用”。始终描述症状:您所期望的与您实际看到的。

  2. 将您的代码包装在 (macroexpand '...) 中,您将看到错误所在:temp-file 未定义。

 (let
     ((temp-file temp-file) ; TEMP-FILE on the right is undefined 
      (temp-buffer
       (get-buffer-create
        (generate-new-buffer-name " *temp file*"))))
   (unwind-protect
       (prog1
           (with-current-buffer temp-buffer
             (insert-file
              (expand-file-name "my-org-file.org" dir))
             (org-mode)
             (org-map-entries
              '(org-entry-put nil "MY-READ-ONLY" 116)
              t 'file))
         (with-current-buffer temp-buffer
           (write-region nil nil temp-file nil 0)))
     (and
      (buffer-name temp-buffer)
      (kill-buffer temp-buffer))))

更新

如果不研究 org-map-entries 是什么或做什么(没时间;抱歉),我建议这样做:with-temp-file 不写文件直到完成。如果 org-map-entries 期望该文件已经存在(即,已写入),那么这将解释为什么您的第一个代码无法工作。

同样,如果我所说的还不够充分,那么查看 org-map-entries 的实际作用和期望可能会让您更接近一个好的答案。

关于emacs - `with-temp-file' 与 `org-map-entries' 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20526956/

相关文章:

javascript - Emacs 有问题的 JavaScript 缩进

function - 使 emacs 模式中的函数在模式文件之外可用

emacs - 将 edebug 用于 elisp 失败,找不到库

emacs - 如何使用组织模式 TAB 自动完成?

emacs - 在 Emacs 组织模式中转义字符

emacs - 如何绑定(bind) key 以在 dired emacs 中运行 shell 命令

emacs - 如何在 Linux 中使用 Ctrl+M 关闭替代 Enter

debugging - 如何找到 ".emacs"或 "init.el"中的错误?

emacs - 比较 Markdown 和 org-mode

通过终端 ssh 支持 Emacs 鼠标