【问题标题】:How to automatically do org-mobile-push org-mobile pull in emacs如何在 emacs 中自动执行 org-mobile-push org-mobile pull
【发布时间】:2011-12-08 13:59:25
【问题描述】:

由于我在 emacs 中使用 org-mode 来跟踪我的 todo 列表,我喜欢 iPhone 应用程序:MobileOrg,有了它,我可以整天访问我的 todo 列表。

但问题来了:

我必须手动 org-mobile-push 我的更改从本地文件通过 dropbox 到手机,并 org-mobile-pull 手机所做的更改。

如何自动制作?就像在 dotmacs 文件中添加一些食谱一样。

【问题讨论】:

  • 这不是stackoverflow.com/questions/4217599/…的复制品吗?
  • 其实是这样,虽然这个问题的表达方式我一眼就认不出来。
  • (run-with-timer 0 (* 5 60) 'org-mobile-pull) 是否适合自动拉(/推)?

标签: emacs elisp org-mode


【解决方案1】:

将这两行添加到点 emacs 文件中:

(add-hook 'after-init-hook 'org-mobile-pull)
(add-hook 'kill-emacs-hook 'org-mobile-push) 

有了它们,它会在 emacs 启动时自动拉取更改,并在 emacs 退出之前推送更改。

-- 更新

如果您从不退出 Emacs,则此解决方案可能不适合您。所以,另一种使用空闲计时器的解决方案

;; moble sync
(defvar org-mobile-sync-timer nil)
(defvar org-mobile-sync-idle-secs (* 60 10))
(defun org-mobile-sync ()
  (interactive)
  (org-mobile-pull)
  (org-mobile-push))
(defun org-mobile-sync-enable ()
  "enable mobile org idle sync"
  (interactive)
  (setq org-mobile-sync-timer
        (run-with-idle-timer org-mobile-sync-idle-secs t
                             'org-mobile-sync)));
(defun org-mobile-sync-disable ()
  "disable mobile org idle sync"
  (interactive)
  (cancel-timer org-mobile-sync-timer))
(org-mobile-sync-enable)

我刚刚发现它与以下答案相同,因此,如果您更喜欢空闲计时器解决方案,请投票tkf's answer

【讨论】:

  • 可以说,如果您经常杀死并重新启动您的 Emacs 以使其真正实用,那么您做错了什么。基于保存挂钩或空闲计时器的解决方案应该是更可取的。
  • 我写的是一种简单的方法,是的,空闲计时器更好,但需要更多脚本,我将编辑此答案以添加我现在正在使用的空闲计时器解决方案。
【解决方案2】:

当我离开电脑时,我的 Emacs 设置中有类似的东西可以推拉。

(defvar my-org-mobile-sync-timer nil)

(defvar my-org-mobile-sync-secs (* 60 20))

(defun my-org-mobile-sync-pull-and-push ()
  (org-mobile-pull)
  (org-mobile-push)
  (when (fboundp 'sauron-add-event)
    (sauron-add-event 'my 3 "Called org-mobile-pull and org-mobile-push")))

(defun my-org-mobile-sync-start ()
  "Start automated `org-mobile-push'"
  (interactive)
  (setq my-org-mobile-sync-timer
        (run-with-idle-timer my-org-mobile-sync-secs t
                             'my-org-mobile-sync-pull-and-push)))

(defun my-org-mobile-sync-stop ()
  "Stop automated `org-mobile-push'"
  (interactive)
  (cancel-timer my-org-mobile-sync-timer))

(my-org-mobile-sync-start)

替代方法是将以下内容放入 cron 作业中 (我在这里找到了这个https://github.com/matburt/mobileorg-android/wiki/Scripting/):

emacs --batch --load ~/.emacs --eval "(org-mobile-pull)" --eval "(org-mobile-push)"

【讨论】:

  • 我假设 60 是如果 emacs 空闲 60 秒,然后 20 秒是此后每 20 秒运行一次。它是否正确??如果没有,请告诉我。非常感谢。
  • 没有。 my-org-mobile-sync-secs 存储 1200,这意味着如果 Emacs 空闲 20 分钟,它会拉/推。如果它保持空闲,它什么也不做。如果它重新进入空闲状态(您在 Emacs 空闲超过 20 分钟后进行一些编辑,首先发生拉/推,您停止编辑,然后经过 20 分钟),它将再次执行拉/推。
【解决方案3】:

此代码取自http://kenmankoff.com/2012/08/17/emacs-org-mode-and-mobileorg-auto-sync/,更改了一些细节。您需要在开始时配置变量。此代码将

  • 每 30 秒检查一次 MobileOrg 是否已同步,如果已同步

    • 从 MobileOrg 拉取。
    • 推送到 MobileOrg。

      这是更新 MobileOrg 中的议程视图所必需的。 通过此行为,您可以离开计算机,在 MobileOrg 中更新一些内容,同步,等待 30 秒,再次同步,您的移动议程视图将被更新。

  • 每当保存组织文件时
    • 检查保存的组织文件是否应该与 MobileOrg 同步,如果是
      • 等待用户空闲
      • 推送到 MobileOrg

.emacs 文件的代码:

(require 'org-mobile)
;; Configure these two variables
(setq org-mobile-inbox-for-pull "~/Dropbox/org/mobile.org" 
      org-mobile-directory "~/Dropbox/MobileOrg")
(require 'gnus-async) 
;; Define a timer variable
(defvar org-mobile-push-timer nil
  "Timer that `org-mobile-push-timer' used to reschedule itself, or nil.")
;; Push to mobile when the idle timer runs out
(defun org-mobile-push-with-delay (secs)
   (when org-mobile-push-timer
    (cancel-timer org-mobile-push-timer))
  (setq org-mobile-push-timer
        (run-with-idle-timer
         (* 1 secs) nil 'org-mobile-push)))
;; After saving files, start an idle timer after which we are going to push 
(add-hook 'after-save-hook 
 (lambda () 
   (if (or (eq major-mode 'org-mode) (eq major-mode 'org-agenda-mode))
     (dolist (file (org-mobile-files-alist))
       (if (string= (expand-file-name (car file)) (buffer-file-name))
           (org-mobile-push-with-delay 10))))))
;; watch mobileorg.org for changes, and then call org-mobile-pull
(defun org-mobile-install-monitor (file secs)
  (run-with-timer
   0 secs
   (lambda (f p)
     (unless (< p (second (time-since (elt (file-attributes f) 5))))
       (org-mobile-pull)
       (org-mobile-push)))
   file secs))
(defvar monitor-timer (org-mobile-install-monitor (concat org-mobile-directory "/mobileorg.org") 30)
  "Check if file changed every 30 s.")

【讨论】:

  • 请注意,使用此设置,MobileOrg 同步会导致拉取,拉动会导致推送,而推送会导致保存。这通常应该不是问题,但如果您的工作流程包括恢复缓冲区(它不应该),则可能。
【解决方案4】:

您也可以在保存笔记后立即推送,如下所示:

(add-hook 
  'after-save-hook 
  (lambda () 
     (if (string= buffer-file-name "<path to my notes.org>") 
        (org-mobile-push)
     )
  ))

【讨论】:

    【解决方案5】:

    我在我的 init.el 上使用this elisp code from gist,它工作得很好,除了它没有内置 org-mobile-pull。

    【讨论】:

    【解决方案6】:

    作为辅助解决方案,类似于 Sandeep C 的

    ;; for Emacs 24.3.1 insert next line
    (require 'cl)  
    ;; automatically org-mobile-push on save of a file
    (add-hook 
     'after-save-hook 
     (lambda ()
       (let (
             (org-filenames (mapcar 'file-name-nondirectory (directory-files org-directory))) ; list of org file names (not paths)
             (filename (file-name-nondirectory buffer-file-name)) ; list of the buffers filename (not path)
             )
         (if (find filename org-filenames :test #'string=)
             (org-mobile-push)        
           )
         )
       )
    )
    

    【讨论】:

      【解决方案7】:

      我选择在保存时简单地推送,所以我将它添加到我的 emacs 初始化文件中:

      (defun org-mobile-push-on-save ()
        "Used in `after-save-hook'."
        (when (memq this-command '(save-buffer save-some-buffers))
          (org-mobile-push)))
      
      (add-hook 'org-mode-hook
                (lambda ()
                  (add-hook 'after-save-hook 'org-mobile-push-on-save nil 'make-local)))
      

      简而言之,它为 org-mode 缓冲区添加了一个 after-save-hook。

      有关代码的更多信息:

      对于自动拉动,其他答案中的计时器可能是一个好方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-20
        • 1970-01-01
        相关资源
        最近更新 更多