1

My .emacs file (emacs 23.4.1) contains python and latex related code. For both there is eval-after-load (code that I want to be executed just once when emacs initiates) and hooks. A relevant part of it is:

(setq py-install-directory "~/.emacs.d/python-mode.el-6.1.3")
(add-to-list 'load-path py-install-directory)
(require 'python-mode)

(defun my-eval-after-load-python()
  (setq initial-frame-alist '((top . 48) (left . 45) (width . 142) (height . 57)))
  (split-window-horizontally (floor (* 0.49 (window-width)))))

(eval-after-load "python-mode" '(my-eval-after-load-python))

All hooks work fine, but my-eval-after-load-python doesn't, which causes the frame to be split into two windows everytime emacs initiates for every extension (for example: emacs file.py, emacs file.tex, emacs file). I tried to change it to:

(eval-after-load "python-mode" 
  '(progn  
     (setq initial-frame-alist '((top . 48) (left . 45) (width . 142) (height . 57)))
     (split-window-horizontally (floor (* 0.49 (window-width))))

, but it still doesn't work. There's probably a beginner mistake going on here, but I'm unable to find it. How would I split the window just the first time a python script is opened (emacs file.py) and not every time I open a new buffer file2.py?

Allan Felipe
  • 201
  • 2
  • 8
  • *Get rid of all of the above code that is not specifically related to the problem* - all of the latex stuff and most of the python stuff. Just provide one, simple, narrowed down, minimal bit of code and a complete recipe to repro the problem, **starting from `emacs -Q`**. – Drew Dec 05 '14 at 06:10
  • 1
    My wild guess: You do not want to run the stuff from `my-eval-after-load-python` after loading the python-package but after loading the first python file. Therefore, you should hook `my-eval-after-load-python` into `python-mode-hook`. Maybe, you should eval it conditionally depending on whether you have already called it (via setting a flag and testing it). – Tobias Dec 05 '14 at 07:21
  • I wouldn't like the window to be resized everytime I open a buffer with a different .py file. Your second solution is fine, but it seems that autoload is what I was looking for. – Allan Felipe Dec 06 '14 at 03:50

3 Answers3

2

It sounds like something is causing (load "python-mode") to happen "everytime emacs initiates for every extension" (I'm not sure what you actually mean by that).

Your code is also strange in that you are forcibly loading python-mode with require, and then subsequently evaluating eval-after-load for that same library, even though you know that it's definitely already loaded. This will still work, but it's odd. One tends to use eval-after-load to avoid loading something up front (letting autoloading deal with it on demand, but still having the custom code running at that time).

Edit: Oh, do you just mean that when you start Emacs it evaluates your eval-after-load code? That's because you've told it to -- you loaded python mode, and then told Emacs that if/when python mode is loaded, split the screen in two.

phils
  • 71,335
  • 11
  • 153
  • 198
  • That was perfect! You understood it correctly, it was silly. The use of `autoload` instead of `require` saves my code, thanks. – Allan Felipe Dec 06 '14 at 03:25
0

Maybe introduce some boolean

(defvar my-action-done nil)

Put them non-nil following action and

(unless my-action-done do ...

Andreas Röhler
  • 4,804
  • 14
  • 18
0

You do (require 'python-mode) right at the beginning, so python-mode is always loaded even before you get to the (eval-after-load "python-mode" ...) part. I thinkg your my-eval-after-load-python is not meant to be loaded when python-mode is loaded but when python-mode is entered. So you want to use

(add-hook 'python-mode-hook #'my-eval-after-load-python)
Stefan
  • 27,908
  • 4
  • 53
  • 82