Skip to content

Instantly share code, notes, and snippets.

@mrvdb
Created July 14, 2012 15:25
Show Gist options
  • Save mrvdb/3111823 to your computer and use it in GitHub Desktop.
Save mrvdb/3111823 to your computer and use it in GitHub Desktop.
Automatic asynchronous org-mobile-push in idle time.
;; Show a notification when a push has been completed
(require 'notifications)
(defun notify-push (result)
(notifications-notify
:title "Push complete"
:body (format "Org-mobile-push: %s" result)
)
)
;; Fork the work of pushing to mobile
(require 'async)
(defun fork-org-push-mobile ()
(async-start
;; What to do in the child process
`(lambda ()
,(async-inject-variables "org-\\(mobile-\\|directory\\)")
(org-mobile-push))
; What to do when it finishes
(lambda (result)
(notify-push result))))
;; 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 'fork-org-push-mobile)))
;; After saving files, start a 30 seconds idle timer after which we
;; are going to push
(add-hook 'after-save-hook
(lambda ()
(when (eq major-mode 'org-mode)
(dolist (file (org-mobile-files-alist))
(if (string= (expand-file-name (car file)) (buffer-file-name))
(org-mobile-push-with-delay 30)))
)))
;; At least run it once a day, but no need for a delay this time
(run-at-time "00:05" 86400 '(lambda () (org-mobile-push-with-delay 1)))
@gabriel4649
Copy link

This is great! Thanks for sharing. I'm running into an issue with the after save hook, I'm getting the error "Symbol's function definition is void: org-mobile-files-alist". Do you have any suggestions?

@dylan-conlin
Copy link

Nice work--up and running like a charm!

@codemac
Copy link

codemac commented Mar 9, 2017

@tatsuhirosatou and for others reading this page. If you get the error "Symbol's function definiton is void: org-mobile-files-alist", that means you haven't require'd the org-mobile.el library. It only defines org-mobile-push and org-mobile-pull as autoload functions, so either define org-mobile-files-alist as another autoload, or just add (require 'org-mobile) at the top of this snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment