Answers to: automatically run an (emacs-)command on startup/shutdown of emacshttp://linuxexchange.org/questions/2365/automatically-run-an-emacs-command-on-startupshutdown-of-emacs<p>I use <a href="http://mobileorg.ncogni.to/">MobileOrg</a> to synchronize my <code>emacs</code> org-files with my mobile. For correct synchronization, mobile-org requires me to run <code>org-mobile-push</code> and <code>org-mobile-pull</code> from within emacs.</p> <p>I can automatically run <code>org-mobile-pull</code> on startup by inserting</p> <pre><code>(org-mobile-pull) </code></pre> <p>into my <code>.emacs</code> file, but how do I automatically run an emacs command when <strong>closing</strong> emacs?</p>enMon, 18 Apr 2011 09:39:04 -0400Answer by Jazzhttp://linuxexchange.org/questions/2365/automatically-run-an-emacs-command-on-startupshutdown-of-emacs/2369<p>Apparently, a way to solve this is to <strong>rebind <code>C-c C-x</code></strong> (default close emacs keyboard shortcut) to another function. Upon further research, I found the <a href="http://www.elliotglaysher.org/emacs/"><code>.emacs</code> of Elliot Glaysher</a>, who bound a more intelligent function to <code>C-c C-x</code>:</p><pre><code>(defun intelligent-close () (interactive) (org-mobile-push) ;; &lt;== MOBILE == (if (eq (car (visible-frame-list)) (selected-frame)) (if (&gt; (length (visible-frame-list)) 1) (delete-frame (selected-frame)) (save-buffers-kill-emacs)) (delete-frame (selected-frame))) ) (global-set-key "C-xC-c" 'intelligent-close) </code></pre><p></p> <p>As you can see, I simply added <code>org-mobile-push</code> to the function. Works like a charm. :)</p> <p>I'd be very interested in further solutions for this problem!</p>JazzMon, 18 Apr 2011 09:39:04 -0400http://linuxexchange.org/questions/2365/automatically-run-an-emacs-command-on-startupshutdown-of-emacs/2369Comment by Jazz on Ron's answerhttp://linuxexchange.org/questions/2365/automatically-run-an-emacs-command-on-startupshutdown-of-emacs#2368<p>This is a good idea, but is dependend on the fact that the commands are executable from outside of emacs. I updated my question to clarify that org-mobile-push and org-mobile-pull must be run from within emacs.</p>JazzMon, 18 Apr 2011 09:25:59 -0400http://linuxexchange.org/questions/2365/automatically-run-an-emacs-command-on-startupshutdown-of-emacs#2368Answer by Ronhttp://linuxexchange.org/questions/2365/automatically-run-an-emacs-command-on-startupshutdown-of-emacs/2367<p>Well, I don't have any experience with that, however, and this may or may not work, but my idea would be to setup a cron job to run a bash script that would look and see if emacs is open, mark the time stamp and then compare it to when emacs closes, and if it's more than X time, do Y. If it's less than X time, do Z.</p> <p>Now this idea is strictly conceptual, but something like that may (or may not) work. I wish I had a more concrete idea for you with code, but alas, I do not.</p>RonMon, 18 Apr 2011 09:00:33 -0400http://linuxexchange.org/questions/2365/automatically-run-an-emacs-command-on-startupshutdown-of-emacs/2367