如何自动执行org-mobile-push org-mobile拉入emacs

时间:2011-12-08 13:59:25

标签: emacs elisp org-mode

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

但问题出在这里:

我必须通过dropbox手动org-mobile-将我的更改从本地文件推送到手机,然后org-mobile-拉回手机所做的更改。

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

7 个答案:

答案 0 :(得分:22)

将这两行添加到dot 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

答案 1 :(得分:14)

我的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)"

答案 2 :(得分:2)

作为一个侧面解决方案,类似于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)        
       )
     )
   )
)

答案 3 :(得分:2)

此代码取自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.")

答案 4 :(得分:1)

你也可以在保存笔记后立即按下,如下所示:

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

答案 5 :(得分:1)

我在init.el上使用this elisp code from gist并且它运行良好,除了它没有内置的org-mobile-pull。

答案 6 :(得分:0)

我选择在保存时简单推送,因此我将其添加到我的emacs init文件中:

(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。

有关代码的更多信息:

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