I'm looking to automate my development and I would like emacs to execute a few commands automatically when I load any file called "project.clj"
Specifically I'd like it to check the open buffers, and if there isn't a buffer called "swank" execute the clojure-jack-in command as if it came from the "project.clj" buffer,
and then I'd like it to run shell and speedbar as well, but I imagine once I figure out how to do the above, those will be easy.
I am a complete elisp noob, but I'm familiar with lisp in general.
You could do something like this:
(defun my-project-hook (filename)
(when (string= (file-name-nondirectory filename) "project.clj")
(do-stuff)))
(add-hook 'after-load-functions 'my-project-hook)
Related
I'm running
/usr/bin/emacs -l ~/.emacs -eval '(org-icalendar-export-agenda-files)' --batch
in a cron job.
Orgmode adds the uuid to the properties of new todo items in the agenda files, but then prompts to save the agenda files (only when run from the command line in batch mode).
Save file /home/user/gtd/work.org? (y or n)
My cludgy work around is to echo y | /usr/bin/emacs.... But since there are actually 3 agenda files that are potentially modified, this is not a good solution. There must be some variable to set or something that will cause org-icalendar-export-agenda-files to just save the files it modifies.
Using defadvice is not a good practice in general1 but, if you want to fix this and can't wait your fix to get included in the next emacs release, try this:
(defadvice org-icalendar-create-uid (after org-icalendar-create-uid-after activate)
(save-buffer))
This advices org-icalendar-create-uid so that when a new UID is created, the current buffer gets saved2. You need to be really careful advising functions. I would recommend to create a export-icalendar.el script similar to this:
;; initialize org-mode
(require 'org)
;; pick your agenda files
(setq org-agenda-files '("~/org"))
;; store UIDs in properties drawer
(setq org-icalendar-store-UID t)
;; optional configuration
(setq org-icalendar-use-scheduled '(event-if-todo event-if-not-todo))
(setq org-agenda-default-appointment-duration 50)
;; avoid interactive prompts if UIDs are created
(defadvice org-icalendar-create-uid (after org-icalendar-create-uid-after activate)
(save-buffer))
(org-icalendar-combine-agenda-files)
To run it, do it like this3:
emacs --batch -l export-icalendar.el
1: http://emacswiki.org/emacs/AdvisingFunctions
2: org-icalendar-create-uid is called inside with-current-buffer, see ox-icalendar.el for details.
3: Never run org-icalendar-combine-agenda-files through eval using emacsclient, it causes problems if you were already visiting agenda files.
Would you be helped by
the function save-some-buffers
in emacs?
Like in
/usr/bin/emacs -l ~/.emacs -eval '(progn (org-icalendar-export-agenda-files) (save-some-buffers t))' --batch
Yes, it can been seen as a kludge.
I would like this to happen when I right click the launcher...icon..and select quit..and choose file exit. I would like this command to be run right before emacs exits....so when I right click the launcher icon and select quit then this command "wg-update-workgroup" is run then Emacs exits..I've tried learning about hooks but don't get how to add one....If someone can give me exact code I put into my initialization file I would be very grateful....tried binding a function to a key but get weird command p error.
Here's the code:
(add-hook 'kill-emacs-hook
(lambda ()
(wg-update-workgroup)))
UPD
It seems that the code isn't working for you since wg-update-workgroup needs an argument.
You have to test this yourself, since I don't really want to get familiar with the package.
Solution 1:
(add-hook 'kill-emacs-hook
(lambda ()
(wg-update-all-workgroups)))
Solution 2:
(add-hook 'kill-emacs-hook
(lambda ()
(call-interactively 'wg-update-workgroup)))
UPD: disregard everything from above:)
I'm pretty sure this is what you want:
(setq wg-query-for-save-on-emacs-exit nil)
(push (lambda()(or (ignore-errors
(wg-update-all-workgroups-and-save)) t))
kill-emacs-query-functions)
The first statement removes the extremely annoying y/n query about saving
workgroups on exit. The second statement saves everything unconditionally on exit.
Just to list my full configuration:
(require 'workgroups)
(workgroups-mode 1)
(setq wg-query-for-save-on-emacs-exit nil)
(wg-load "~/wg")
(push (lambda()(or (ignore-errors
(wg-update-all-workgroups-and-save)) t))
kill-emacs-query-functions)
You're right to be baffled. The issue is this: when Emacs begins exiting, workgroups.el cleans itself up earlier than 'kill-emacs-hook. What you want is this:
(add-hook 'kill-emacs-query-functions 'wg-update-all-workgroups)
The hook variable should then look something like this:
(wg-update-all-workgroups wg-emacs-exit-query)
I recommend using 'wg-update-all-workgroups rather than 'wg-update-workgroup if, like me, you have more than one workgroup saved in a given workgroups file.
IMO workgroups.el is the best Emacs session manager. Somebody new has just taken it over. I'm excited that a new version with even more features may be forthcoming:
https://github.com/pashinin/workgroups2/
I have a perltidy-mode.el lisp file, which is being loaded - I can call it manually by M-x perltidy-mode.
What would be the proper way to run it automatically after a file is opened (or emacs is loaded)?
(defalias 'perl-mode 'cperl-mode)
(defalias 'perl-mode 'perltidy-mode)
doesn't seem to work.
It seems I've forgotten a lot of lisp/emacs
If you would like to activate it for every opened perl file, you can add it to a hook:
(defun my-perl-hook ()
(perltidy-mode 1))
(add-hook 'perl-mode-hook 'my-perl-hook)
Note: I know nothing about this mode, or about the different perl modes, so you might to add your function to other hooks as well.
I'm trying to setup ipython.el in emacs23. I've successfully installed it (after putting python-mode.el in my load-path to supplant python.el which comes pre-installed with emacs). And I can even get it to run via M-x py-shell, etc.
The interface seems to be pretty poorly setup, and I was wondering if I was doing it wrong, or if I need to customize it to make it work the way I'd like.
In short, the workflow I'd like to have:
in one or more buffers, edit Python code
When I hit C-c C-c in that buffer, either execute the Python code in that buffer in the open IPython shell buffer (if there is one) or open up another buffer to do that.
But what happens right now is:
With the IPython shell in one buffer and the Python file in the other, if I hit C-c C-c in the Python file buffer, the file buffer switches to the IPython buffer (meaning I now have two, duplicated iPython buffers) and the file is executed.
This is annoying.
I'm pretty new to elisp, but my understanding of defadvice is that I could advise around python-execute-buffer to take note of the existing file buffer, run python-execute-buffer, and then switch back to the original file buffer as a workaround.
This seems pretty silly. Any suggestions for better ways to accomplish this would be appreciated!
If it matters: I'm on OS X 10.6.8 with IPython 0.10.1 running Emacs 24.0.50.
Thanks in advance!
Turns out that simply installing python-mode.el and anything-ipython.el and putting
(require 'python-mode)
(require 'ipython)
(require 'anything-ipython)
(add-hook 'python-mode-hook #'(lambda ()
(define-key py-mode-map (kbd "C-<tab>") 'anything-ipython-complete)))
(add-hook 'ipython-shell-hook #'(lambda ()
(define-key py-mode-map (kbd "C-<tab>")
'anything-ipython-complete)))
in my .emacs made everything work just how I wanted if py-shell is executed before py-execute-buffer (so that C-c C-c) executes the code in the shell instead of just opening up the *Python Output* buffer.
I am upgrading to emacs23. I find that my emacs.el loads much more slowly.
It's my own fault really... I have a lot of stuff in there.
So I am also trying to autoload everything possible that is currently "required" by my emacs.el.
I have a module that exposes 12 entry points - interactive functions I can call.
Is the correct approach to have 12 calls to autoload in order to insure that the module is loaded regardless of which function I call? Are there any problems with this approach? Will it present performance issues?
If not that approach, then what?
What you really want is to get the autoloads generated for you automatically, so that your .emacs file remains pristine. Most packages have the ;;;###autoload lines in them already, and if not, you can easily add them.
To manage this, you can put all the packages in a directory, say ~/emacs/lisp, and in there have a file named update-auto-loads.el which contains:
;; put this path into the load-path automatically
;;;###autoload
(progn
(setq load-path (cons (file-name-directory load-file-name) load-path)))
;;;###autoload
(defun update-autoloads-in-package-area (&optional file)
"Update autoloads for files in the diretory containing this file."
(interactive)
(let ((base (file-truename
(file-name-directory
(symbol-file 'update-autoloads-in-package-area 'defun)))))
(require 'autoload) ;ironic, i know
(let ((generated-autoload-file (concat base "loaddefs.el")))
(when (not (file-exists-p generated-autoload-file))
(with-current-buffer (find-file-noselect generated-autoload-file)
(insert ";;") ;; create the file with non-zero size to appease autoload
(save-buffer)))
(cd base)
(if file
(update-file-autoloads file)
(update-autoloads-from-directories base)))))
;;;###autoload
(defun update-autoloads-for-file-in-package-area (file)
(interactive "f")
(update-autoloads-in-package-area file))
If you add 'update-autoloads-in-package-area to your kill-emacs-hook, then the loaddefs.el will automatically be updated every time you exit Emacs.
And, to tie it all together, add this to your .emacs:
(load-file "~/emacs/lisp/loaddefs.el")
Now, when you download a new package, just save it in the ~/emacs/lisp directory, update the loaddefs via M-x update-autoloads-in-package-area (or exit emacs), and it'll be available the next time you run Emacs. No more changes to your .emacs to load things.
See this question for other alternatives to speeding up Emacs startup: How can I make Emacs start-up faster?
Well, who cares how slowly it starts?
Fire it up via emacs --daemon & and then connect using either one of
emacsclient -c /some/file.ext, or
emacsclient -nw
I created aliases for both these as emx and emt, respectively. Continuing once editing session is so much saner...
Ideally you shouldn't have any load or require in your .emacs file.
You should be using autoload instead...
e.g.
(autoload 'slime-selector "slime" t)
You will need to use eval-after-load to do any library specific config, but the upshot is that you won't need to wait for all this to load up front, or cause errors on versions of Emacs that don't have the same functionality. (e.g. Terminal based, or a different platform etc.)
While this may not affect you right now, chances are, in future you will want to use the same config on all machines / environments where you use Emacs, so it's a very good thing to have your config ready to fly.
Also use (start-server) and open external files into Emacs using emacsclient - So you avoid restarting Emacs.