Allan Felipe
Allan Felipe

Reputation: 201

Emacs: eval-after-load code always being executed

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?

Upvotes: 1

Views: 780

Answers (3)

Stefan
Stefan

Reputation: 28541

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)

Upvotes: 0

phils
phils

Reputation: 73274

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.

Upvotes: 2

Andreas Röhler
Andreas Röhler

Reputation: 4804

Maybe introduce some boolean

(defvar my-action-done nil)

Put them non-nil following action and

(unless my-action-done do ...

Upvotes: 0

Related Questions