How to automatically do org-mobile-push org-mobile pull in emacs - emacs

Since I am using org-mode to track my todo list in emacs, I like the iPhone app: MobileOrg, with it, I can access my todo list all day.
But here's the problem:
I have to manually org-mobile-push my changes from local file to mobile phone through dropbox, and org-mobile-pull the changes made by phone back.
How to make that automatically? Like adding some recipes in dotemacs file.

Add these two lines to dot emacs file:
(add-hook 'after-init-hook 'org-mobile-pull)
(add-hook 'kill-emacs-hook 'org-mobile-push)
With them, it automatically pulls the changes on emacs startup, and pushes the changes before emacs exits.
-- Update
If you never exit your Emacs, this solution might not work for you. So, another solution using idle timer
;; 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)
I just found out it is same as below answer, so, if you prefer the idle timer solution, please upvote tkf's answer.

I have something like this in my Emacs setting to do push and pull when I am away from computer.
(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)
Alternative is to put the following in cron job
(I found this here https://github.com/matburt/mobileorg-android/wiki/Scripting/):
emacs --batch --load ~/.emacs --eval "(org-mobile-pull)" --eval "(org-mobile-push)"

This code is taken from http://kenmankoff.com/2012/08/17/emacs-org-mode-and-mobileorg-auto-sync/, with a couple of details changed. You need to configure the variables in the beginning. This code will
Check every 30s whether MobileOrg has synced, and if so
Pull from MobileOrg.
Push to MobileOrg.
This is necessary to update the agenda views in MobileOrg.
With this behavior, you can be away from your computer, update some things in MobileOrg, sync, wait 30 seconds, sync again, and your mobile agenda view will be updated.
Whenever an org file is saved
Check whether the saved org file is supposed to be synced with MobileOrg, and if so
Wait for the user to become idle
Push to MobileOrg
Code for your .emacs file:
(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.")

you can also push right after saving a note, like this:
(add-hook
'after-save-hook
(lambda ()
(if (string= buffer-file-name "<path to my notes.org>")
(org-mobile-push)
)
))

I use this elisp code from gist on my init.el and it works pretty well, except it doesn't have org-mobile-pull built in.

As a side solution, similar to Sandeep C's
;; 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)
)
)
)
)

I opted to simply push when saving, so I added this to my emacs init file:
(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)))
In a nutshell, it adds an after-save-hook to org-mode buffers.
More info on the code:
https://emacs.stackexchange.com/a/14476/12694
https://stackoverflow.com/a/6141681/45881
For auto-pull, a timer as in other answers is probably a good way.

Related

Emacs: load-file .emacs when saved

I'm new to emacs and lisp.
I wanted to auto-load the dot file when it was saved. Meaning, when I save my .emacs file, it would automatically call load-file on it (thus letting me know right away if I messed up).
But I can't seen to be able to find a comprehensive tutorial on hooks in emacs.
This is what I've come up with:
(defun load-init-after-save ()
"After saving this file, load it"
(if (eq bname this) ('load-file this) (nil))
)
(add-hook 'after-save-hook 'load-init-after-save)
Of course, this is incorrect: bname and this are just placeholders. And I don't want this function to run on all saves, just when the .emacs file is saved.
Does anyone know how to do this? Is there a better, easier way?
The following code loads your .emacs or ~/.emacs.d/init.el file after save:
(defun my-load-user-init-file-after-save ()
(when (string= (file-truename user-init-file)
(file-truename (buffer-file-name)))
(let ((debug-on-error t))
(load (buffer-file-name)))))
(add-hook 'after-save-hook #'my-load-user-init-file-after-save)
Since your intended use is error-checking, the code also enables the debugger while loading the init file, so that you get a nice backtrace in case of errors.
For error checking of your init file you may also find Flycheck useful. It checks your init file on the fly with the byte compiler, highlights any errors and warnings in the buffer, and—optionally—gives you a list of all errors and warnings.
Disclaimer: I'm the maintainer of this library.
Way 1
One way of doing is to install auto-compile-mode from MELPA, and enable it:
(defun my-emacs-lisp-hook ()
(auto-compile-mode 1))
(add-hook 'emacs-lisp-mode-hook 'my-emacs-lisp-hook)
Now each time you save an Elisp file that's byte-compiled, it will be
re-compiled. Compilation will usually catch some bad errors.
To compile any Elisp file, select it in dired (C-x d)
and press B (dired-do-byte-compile).
Way 2
Use this custom code I wrote.
(defun test-emacs ()
(interactive)
(require 'async)
(async-start
(lambda () (shell-command-to-string "emacs --batch --eval \"(condition-case e (progn (load \\\"~/.emacs\\\") (message \\\"-OK-\\\")) (error (message \\\"ERROR!\\\") (signal (car e) (cdr e))))\""))
`(lambda (output)
(if (string-match "-OK-" output)
(when ,(called-interactively-p 'any)
(message "All is well"))
(switch-to-buffer-other-window "*startup error*")
(delete-region (point-min) (point-max))
(insert output)
(search-backward "ERROR!")))))
(defun auto-test-emacs ()
(when (eq major-mode 'emacs-lisp-mode))
(test-emacs))
(add-hook 'after-save-hook 'auto-test-emacs)
This will start a new Emacs instance in the background each time you
save a file. If something goes wrong, it will complain.
The second approach uses async.
If you really want to do this just for .emacs user this:
(defun auto-test-emacs ()
(when (and (eq major-mode 'emacs-lisp-mode)
(equal (file-truename user-init-file)
(expand-file-name
buffer-file-truename)))
(test-emacs)))

use .dir-locals.el to pick major mode [duplicate]

I have defined a .dir-locals.el file with the following content:
((python-mode . ((cr/virtualenv-name . "saas"))))
In my .emacs I have the following function to retrieve this value and provide a virtualenv path:
(defun cr/virtualenv ()
(cond (cr/virtualenv-name (format "%s/%s" virtualenv-base cr/virtualenv-name))
((getenv "EMACS_VIRTUAL_ENV") (getenv "EMACS_VIRTUAL_ENV"))
(t "~/.emacs.d/python")))
Finally, in my python-mode-hook list, I have this hook function:
(add-hook 'python-mode-hook 'cr/python-mode-shell-setup)
(defun cr/python-mode-shell-setup ()
(message "virtualenv-name is %s" cr/virtualenv-name)
(let ((python-base (cr/virtualenv)))
(cond ((and (fboundp 'ipython-shell-hook) (file-executable-p (concat python-base "/bin/ipython")))
(setq python-python-command (concat python-base "/bin/ipython"))
(setq py-python-command (concat python-base "/bin/ipython"))
(setq py-python-command-args '( "-colors" "NoColor")))
(t
(setq python-python-command (concat python-base "/bin/python"))
(setq py-python-command (concat python-base "/bin/python"))
(setq py-python-command-args nil)))))
When I open a new python file, the message logged by cr/python-mode-shell-setup indicates that cr/virtualenv-name is nil. However, when I C-h v the name, I get "saas" instead.
Obviously there's a load order issue here; is there a way to have my mode hook statements respond to directory-local variables?
This happens because normal-mode calls (set-auto-mode) and (hack-local-variables) in that order.
However hack-local-variables-hook is run after the local variables have been processed, which enables some solutions:
The first is to make Emacs run a new "local variables hook" for each major mode:
(add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
(defun run-local-vars-mode-hook ()
"Run a hook for the major-mode after the local variables have been processed."
(run-hooks (intern (concat (symbol-name major-mode) "-local-vars-hook"))))
(add-hook 'python-mode-local-vars-hook 'cr/python-mode-shell-setup)
(Your original function can be used unmodified, with that approach.)
A second option is to utilise the optional LOCAL argument to add-hook that makes the specified function buffer-local. With this approach you could write your hook as follows:
(add-hook 'python-mode-hook 'cr/python-mode-shell-setup)
(defun cr/python-mode-shell-setup ()
(add-hook 'hack-local-variables-hook
(lambda () (message "virtualenv-name is %s" cr/virtualenv-name)
(let ((python-base (cr/virtualenv)))
(cond ((and (fboundp 'ipython-shell-hook) (file-executable-p (concat python-base "/bin/ipython")))
(setq python-python-command (concat python-base "/bin/ipython"))
(setq py-python-command (concat python-base "/bin/ipython"))
(setq py-python-command-args '( "-colors" "NoColor")))
(t
(setq python-python-command (concat python-base "/bin/python"))
(setq py-python-command (concat python-base "/bin/python"))
(setq py-python-command-args nil)))))
nil t)) ; buffer-local hack-local-variables-hook
i.e. python-mode-hook runs first and registers the anonymous function with hack-local-variables-hook for the current buffer only; and that function is then called after the local variables have been processed.
Lindydancer's comment prompts a third approach. It's not nearly as clean as the other two, but proved interesting regardless. I didn't like the idea of causing (hack-local-variables) to be called twice, but I see that if you set the local-enable-local-variables buffer-locally, it prevents (hack-local-variables) from doing anything, so you could do this:
(defun cr/python-mode-shell-setup ()
(report-errors "File local-variables error: %s"
(hack-local-variables)))
(set (make-local-variable 'local-enable-local-variables) nil)
(let ((python-base (cr/virtualenv)))
...))
Obviously that modifies the normal sequence of execution a little, so side effects may be possible. I was worried that if the same major mode is set by a local variable comment in the file, this might cause infinite recursion, but that doesn't actually appear to be a problem.
Local variable header comments (e.g. -*- mode: foo -*-) are handled by (set-auto-mode), so those are fine; but a mode: foo Local Variables: comment seems like it would be an issue as it is handled by (hack-local-variables), and so if the mode is set that way I thought it would cause recursion.
In practice I was able to trigger the problem by using a simple function as a 'mode' which did nothing more than try to run its hooks; however testing with a 'proper' mode did not exhibit the problem, so it's probably safe in reality. I didn't look into this further (as the other two solutions are much cleaner than this), but I would guess the delayed mode hooks mechanism probably explains it?

How to create a hook which fires when I pause typing in Emacs?

I want to implement a hook which will work similar to Automatic LaTeX plugin for Vim: when I pause typing in emacs org mode, hook should fire and do (org-beamer-export-to-pdf t) in background.
How to create such a hook?
Thanks to #legoscia for the idea. Solved my problem using Real auto saving for emacs script with slightly modified real-auto-save function:
(defun real-auto-save()
(interactive)
(if real-auto-save-p
(progn
(save-excursion
(dolist (elem real-auto-save-alist)
(set-buffer elem)
(if (and (buffer-file-name) (buffer-modified-p))
(progn
(write-file (buffer-file-name))
(if (boundp 'org-beamer-mode)
(org-beamer-export-to-pdf t))
)))))))
In my init.el file:
(require 'real-auto-save)
(add-hook 'org-mode-hook 'turn-on-real-auto-save)
(setq real-auto-save-interval 2)
Everything is working perfect, no overhead or error messages.

In emacs, how do I save without running save hooks?

I have various things set up in my 'before-save-hook. For example, I run 'delete-trailing-whitespace. This is what I want in almost all occasions.
But sometimes, I'm working on files that are shared with other people, and the file already has a bunch of trailing whitespace. If I save the file, I'll get a big diff that's pretty confusing, as my change is buried in dozens or hundreds of meaningless changes. Yes, everyone could just tell their diff tool to not show whitespace changes, but that's something that everyone has to do every time they look at the diff. I'd rather not even have the whitespace change.
Is there anything I can do to save the file without the whitespace changes, short of starting a new instance of Emacs with no init.el file, or with a modified init.el that doesn't have the hook?
Here is how I save without triggering delete-trailing-whitespace:
C-x C-q C-x C-s C-x C-q: read-only, save, revert read-only
A simpler solution I came up with is that my fundamental-mode has no hooks installed, because I want it to be as plain as possible. Thus if I want to save a file without running hooks, I temporarily switch to fundamental-mode.
Based on a comment discussion with #Stefan, here are two possible (untested) solutions:
Use let:
(defun save-buffer-without-dtw ()
(interactive)
(let ((b (current-buffer))) ; memorize the buffer
(with-temp-buffer ; new temp buffer to bind the global value of before-save-hook
(let ((before-save-hook (remove 'delete-trailing-whitespace before-save-hook)))
(with-current-buffer b ; go back to the current buffer, before-save-hook is now buffer-local
(let ((before-save-hook (remove 'delete-trailing-whitespace before-save-hook)))
(save-buffer)))))))
Use unwind-protect:
(defun save-buffer-without-dtw ()
(interactive)
(let ((restore-global
(memq 'delete-trailing-whitespace (default-value before-save-hook)))
(restore-local
(and (local-variable-p 'before-save-hook)
(memq 'delete-trailing-whitespace before-save-hook))))
(unwind-protect
(progn
(when restore-global
(remove-hook 'before-save-hook 'delete-trailing-whitespace))
(when restore-local
(remove-hook 'before-save-hook 'delete-trailing-whitespace t))
(save-buffer))
(when restore-global
(add-hook 'before-save-hook 'delete-trailing-whitespace))
(when restore-local
(add-hook 'before-save-hook 'delete-trailing-whitespace nil t)))))
The problem with the second solution is that the order of functions in the before-save-hook may change.
Here's another solution:
(defvar my-inhibit-dtw nil)
(defun my-delete-trailing-whitespace ()
(unless my-inhibit-dtw (delete-trailing-whitespace)))
(add-hook 'before-save-hook 'my-delete-trailing-whitespace)
and then
(defun my-inhibit-dtw ()
(interactive)
(set (make-local-variable 'my-inhibit-dtw) t))
so you can M-x my-inhibit-dtw RET in the buffers where you don't want to trim whitespace.
I wrote a command inspired by Nicholas Douma's solution.
(defun olav-save-buffer-as-is ()
"Save file \"as is\", that is in read-only-mode."
(interactive)
(if buffer-read-only
(save-buffer)
(read-only-mode 1)
(save-buffer)
(read-only-mode 0)))
What we need to do is remove 'delete-trailing-whitespace from before-save-hook, save the buffer, then add it back.
This code will do that, but only remove and add it if it's there to begin with.
;; save the buffer, removing and readding the 'delete-trailing-whitespace function
;; to 'before-save-hook if it's there
(defun save-buffer-no-delete-trailing-whitespace ()
(interactive)
(let ((normally-should-delete-trailing-whitespace (memq 'delete-trailing-whitespace before-save-hook)))
(when normally-should-delete-trailing-whitespace
(remove-hook 'before-save-hook 'delete-trailing-whitespace))
(save-buffer)
(when normally-should-delete-trailing-whitespace
(add-hook 'before-save-hook 'delete-trailing-whitespace))))
(global-set-key (kbd "C-c C-s") 'save-buffer-no-delete-trailing-whitespace)
It also binds the command to (kbd C-c C-s), for convenience.

Persistent Undos in Emacs [duplicate]

Is there any way to have EMACS save your undo history between sessions?
I'm aware of the savehist lib, the saveplace lib, the desktop lib, and the windows lib, these all provide some session control but none seem to save the undo history.
From version 0.4 onwards, undo-tree supports persistent storage of undo-tree data between sessions "out of the box". (Note that there are significant bug-fixes related to this feature in more recent versions; the latest version at the time of writing is 0.6.3.)
Simply enable the undo-tree-auto-save-history customization option to automatically save and load undo history in undo-tree buffers. Or use the undo-tree-save/load-history commands to save and load undo history manually.
You need at least Emacs version 24.3 for this to work reliably, but with a recent enough Emacs it works very well.
Add the following to your .emacs file :
(global-undo-tree-mode)
(setq undo-tree-auto-save-history t)
(setq undo-tree-history-directory-alist '(("." . "~/.emacs.d/undo")))
Explanation
(global-undo-tree-mode) enables undo tree.
(setq undo-tree-auto-save-history t) enables auto save of undo history.
(setq undo-tree-history-directory-alist '(("." . "~/.emacs.d/undo"))) so that your project does not get littered with undo-history savefiles.
Here's some code I wrote which seems to do the trick. It isn't bullet-proof, as in, it doesn't handle all the file handling intricacies that Emacs does (e.g. overriding where auto-save files are put, symlink handling, etc.). But, it seemed to do the trick for some simple text files I manipulated.
(defun save-undo-filename (orig-name)
"given a filename return the file name in which to save the undo list"
(concat (file-name-directory orig-name)
"."
(file-name-nondirectory orig-name)
".undo"))
(defun save-undo-list ()
"Save the undo list to a file"
(save-excursion
(ignore-errors
(let ((undo-to-save `(setq buffer-undo-list ',buffer-undo-list))
(undo-file-name (save-undo-filename (buffer-file-name))))
(find-file undo-file-name)
(erase-buffer)
(let (print-level
print-length)
(print undo-to-save (current-buffer)))
(let ((write-file-hooks (remove 'save-undo-list write-file-hooks)))
(save-buffer))
(kill-buffer))))
nil)
(defvar handling-undo-saving nil)
(defun load-undo-list ()
"load the undo list if appropriate"
(ignore-errors
(when (and
(not handling-undo-saving)
(null buffer-undo-list)
(file-exists-p (save-undo-filename (buffer-file-name))))
(let* ((handling-undo-saving t)
(undo-buffer-to-eval (find-file-noselect (save-undo-filename (buffer-file-name)))))
(eval (read undo-buffer-to-eval))))))
(add-hook 'write-file-hooks 'save-undo-list)
(add-hook 'find-file-hook 'load-undo-list)
desktop-save-mode does not save buffer-undo-list by default. You just have to tell him!
(add-to-list 'desktop-locals-to-save 'buffer-undo-list)
Emacs Session appears to support this:
(add-to-list 'session-locals-include 'buffer-undo-list)
I have managed to get the undo history working by using the information provided here: http://emacs.stackexchange.com/q/3725/2287
Instead of patching the original file desktop.el.gz I created an advice that temporarily overrides (buffer-local-variables) then I use it together with the function that gathers information about the buffer.
(defun +append-buffer-undo-list-to-buffer-local-variables-advice (orig-fn &rest args)
"Override `buffer-local-variables' and call ORIG-FN with ARGS.
There is a bug in Emacs where the `buffer-undo-list' data is
missing from the output of `buffer-local-variables'. This
advice temporarily overrides the function and appends the
missing data."
(let ((orig-buffer-local-variables-fn (symbol-function 'buffer-local-variables)))
(cl-letf (((symbol-function 'buffer-local-variables)
#'(lambda () (append (funcall orig-buffer-local-variables-fn)
`(,(cons 'buffer-undo-list buffer-undo-list))))))
(apply orig-fn args))))
(advice-add #'desktop-buffer-info :around #'+append-buffer-undo-list-to-buffer-local-variables-advice)
(push 'buffer-undo-list desktop-locals-to-save)
(desktop-save-mode 1)
I hope this helps someone else.