Emacs: eval-after-load code always being executed - python

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?

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.

Maybe introduce some boolean
(defvar my-action-done nil)
Put them non-nil following action and
(unless my-action-done
do ...

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)

Related

emacs ipython autoclear buffer after executing

I'm fairly new to Emacs and finally figured out how to set up python environment. I'm using elpy and iPython as python shell interpreter. My question is as follows:
After I press C-c C-c, the buffer on the right shows the executed result. The red box is the code being executed and the blue box is the executed result returned from python. My questions are:
Is there a way only showing the result?
And every time after the script being executed, how to set the buffer on the right only showing the current result, ie, clear previous executed results? Thanks.
Is there a way only showing the result?
elpy already has a custom variable for this:
(setq elpy-shell-echo-input nil)
And every time after the script being executed, how to set the buffer
on the right only showing the current result, ie, clear previous
executed results? Thanks.
elpy doesn't have this functionality. But you can create a wrapper function and bind it to the same keys:
(define-key elpy-mode-map (kbd "C-c C-c")
(lambda (p)
(interactive "P")
(ignore-errors
(with-current-buffer (process-buffer (python-shell-get-process-or-error))
(let ((comint-buffer-maximum-size 0))
(comint-clear-buffer))))
(elpy-shell-send-region-or-buffer p)))

Is there a function like eval-print-last-sexp for Comint-Mode?

Is there an equivalent function to eval-print-last-sexp for comint derived modes in emacs?
Specifically, I'm using python-mode (with elpy) and am looking for a way to send the contents of a region to the Python process and then print the results on the next line of the python script I'm working in.
Printing the results to the Messages buffer would be acceptable as well, however the behavior of eval-print-last-sexp would be preferred.
I'm running Emacs 25.1.1 and elpy 1.13.0.
It would depend on the comint-derived mode, since you would need to redirect the process output. Different modes have different methods of interacting with inferior processes. Python mode already has a function to do this, python-shell-send-string-no-output (other modes have similar functions, but you would need to search for them).
I'm not sure how exactly you want to define a sexp for python, but here is an example of sending the current line, with output like eval-print-last-sexp.
(defun python-eval-print-last-sexp ()
"Print result of evaluating current line into current buffer."
(interactive)
(let ((res (python-shell-send-string-no-output
;; modify to get a different sexp
(buffer-substring (line-beginning-position)
(line-end-position))))
(standard-output (current-buffer)))
(when res
(terpri)
(princ res)
(terpri))))
elpy-shell-send-statement from the Emacs Python Development Environment.

Running interactive python script from emacs

I am a fairly proficient vim user, but friends of mine told me so much good stuff about emacs that I decided to give it a try -- especially after finding about the aptly-named evil mode...
Anyways, I am currently working on a python script that requires user input (a subclass of cmd.Cmd). In vim, if I wanted to try it, I could simply do :!python % and then could interact with my script, until it quits. In emacs, I tried M-! python script.py, which would indeed run the script in a separate buffer, but then RETURNs seems not to be sent back to the script, but are caught by the emacs buffer instead. I also tried to have a look at python-mode's C-c C-c, but this runs the script in some temporary directory, whereas I just want to run it in (pwd).
So, is there any canonical way of doing that?
I don't know about canonical, but if I needed to interact with a script I'd do M-xshellRET and run the script from there.
There's also M-xterminal-emulator for more serious terminal emulation, not just shell stuff.
I like to use the Emacs "compile" command to test/run my python scripts. M-XcompileRET will pull up the default "make -k" but if you delete that and put in the command line for your script (including options), subsequent "compiles" will provide the new "compile" command automatically. All the output from your script will appear in the compile buffer. (As opposed to the shell, this provides a nice clean buffer each time it is invoked. Good for searching and such. If you forget to save your script before your run, compile will ask you if you would like to save the file.)
You will lose your the command line when you restart Emacs. But you can get Emacs to set the compile-command for the buffer holding your script by putting at the bottom of the python script this sort of code (actually a python comment):
# Trigger emacs to run this script using the "compile" command
# ;;; Local Variables: ***
# ;;; compile-command: "my_cool_script.py --complicated_option some_filename.txt" ***
# ;;; end: ***
This is handy for scripts with complicated invocations.
Note: The python comment character '#' protects this from the python interpreter while Emacs knows to set these variables because it looks at the bottom of every file when it opens them.
I'd love to be able to jump to 'compile errors' in my python script the way the compile command does when you use it for compiling C code but I'm too lazy to create the Emacs regular expression to make this work. Perhaps that would make another great question for stack overflow!
I currently use these hook to define my compilation commands:
(defun convert-filename-to-executable (file)
(if (eq system-type 'windows-nt)
(concat (file-name-sans-extension file) ".exe")
;; linux
(concat "./" (file-name-sans-extension file))))
(add-hook 'c++-mode-hook
(lambda ()
(unless (file-exists-p "Makefile")
(set (make-local-variable 'compile-command)
(let* ((file (file-name-nondirectory buffer- file-name))
(executable (convert-filename-to-executable file)))
(concat "g++ -g -Wall -o "
(file-name-sans-extension file)
" "
file
" && "
executable))))))
(add-hook 'c-mode-hook
(lambda ()
(unless (file-exists-p "Makefile")
(set (make-local-variable 'compile-command)
(let* ((file (file-name-nondirectory buffer-file-name))
(executable (convert-filename-to-executable file)))
(concat "gcc -g -ansi -Wall -Wpedantic -Wextra -Wc++-compat -Wconversion -o "
(file-name-sans-extension file)
" "
file
" && "
executable))))))
(add-hook 'python-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(concat "python " buffer-file-name))))
(add-hook 'perl-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(concat "python " buffer-file-name))))
Together with this lambda set to call the compile function interactively:
(global-set-key (kbd "<f4>") (lambda () (interactive) (setq current-prefix-arg '(4)) (call-interactively 'compile)))
One button to rule them all!
If you press F4 (in my case, you can set the key yourself in the lambda for global-set-key), then a file opened in C++ or C mode will be compiled and a file in python or perl mode will be run (interactively)
The I think ansi-term has the most faithful emulation of a terminal. But I don't see a way to pass arguments to the process. You can of course just launch it from a shell inside the ansi-term buffer.
But I think the best thing to do is to not use python-send-buffer, but instead to use a new function which does it "right", that is by sending the path to the current file instead of making a temp file. There are some slight differences of course in that you have to save the current file first, but the following should at least get you on the right track.
(defun python-send-file ()
(interactive)
(save-buffer)
(python-send-string (concat "execfile('" (buffer-file-name) "')")))
;; This overwrites the `python-send-buffer' binding so you may want to pick another key
(eval-after-load "python"
(define-key python-mode-map "\C-c\C-c" 'python-send-file))
I checked and this allows you to interact. To get tabs you have a few options.
C-qTAB will always give you a literal tab
You can rebind tab to be a literal tab in inferior-python-mode-map:
(define-key inferior-python-mode-map "\C-i" 'self-insert-command)
I'm sure there are others that I can't think of
If you use C-c C-c a buffer is created (look for inferior-python). Try changing to that buffer*, every time you hit C-c C-c the result is shown there, you need to see that buffer to get the results. Use C-x 2 so you can see both buffers at the same time.
Also try C-c C-z (switch to shell).
*I use Ibuffer to manage buffers, is very good.
(btw, this http://tuhdo.github.io/index.html is an excelent place to learn some emacs)
EDIT: Have you tried C-c C-p ?
Yet another option:
Using C-c C-c works nicely with fgallina's python.el -- pwd will be the location of the buffer's file.

aquamacs: windows swapping on C-c C-c (python mode)

I apologize if I use the incorrect terminology here, I've only been using emacs for a few months.
I just reinstalled Aquamacs on a macbook that I reformatted and am having the oddest problem.
I open a .py file, and use C-c ! to open a python shell. So I have (as expected), the .py file in the top window and the python shell in the bottom window.
If I then run C-c C-c (py-execute-buffer) in the .py file, the two windows swap positions. I mean, the .py file buffer opens in the bottom window in a new buffer, while the python shell opens in the top window in a new buffer. So basically, they swap positions. Repeatedly using C-c C-c swaps the windows back again... so they're shuffling positions. Also, both windows (top and bottom) have both buffers (.py file and python shell) in tabs.
I've not made any modifications to the default settings yet, and I've gotten the problem with both 2.3a and 2.3 (2.3 was on the machine previously and didn't have this problem, so I tried rolling back... to no avail).
Does anyone know how to stop this behavior? Thanks in advance!
Add the following to your Emacs init file in Aquamacs to prevent it from swapping the buffers around:
(defadvice py-execute-buffer
(around keep-buffers-same activate)
"Don't swap buffers in Aquamacs."
(save-window-excursion
ad-do-it))
You can also try adding the following to your emacs init file:
(setq py-split-windows-on-execute-p nil)
This will prevent the current window from splitting after you run any py-execute-*. (This also means that the python shell won't show up if it isn't already in one of your windows.)
i don't use Aquamacs and couldn't reproduce your described behaviour, however, try this code to toggle either of the windows as 'dedicated'. locking windows to buffers was the first thing i wanted to do when getting up and running with emacs. maybe this will help you here.
add the code to your '.emacs', then either 'mark' (select) the region 'S-< key-down >' and then 'M-x eval-region' to evaluate it ..or save and restart emacs.
(global-set-key [pause] 'window-dedication-toggle)
(defun window-dedication-toggle (&optional window force quiet)
"toggle or ensure the 'dedicated' state for a window"
(interactive)
(let* ((toggle t) (window (if window window (selected-window))) (dedicated (window-dedicated-p window)))
(cond ((equal force "on") (setq toggle (eq dedicated nil)))
((equal force "off") (setq toggle (eq dedicated t))))
(if toggle (progn
(setq dedicated (not dedicated))
(set-window-dedicated-p window dedicated)
(if (not quiet)
(message "window %sdedicated to %s" (if (not dedicated) "no longer " "") (buffer-name)))))))

Emacs: Set/Reset python debug breakpoint

I use python debugger pdb. I use emacs for python programming. I use python-mode.el. My idea is to make emacs intuitive. So I need the following help for python programs (.py)
Whenever I press 'F9' key, the emacs should put "import pdb; pdb.set_trace();" statements in the current line and move the current line to one line below.
Sentence to be in same line. smart indentation may help very much.
Wherever "import pdb; pdb.set_trace();" statement presents in the python code, emacs should display left indicator and highlight that line.
When I press 'Alt-F9' keys at the current line and emacs found the "import pdb; pdb.set_trace();" statement then, emacs should remove the "import pdb; pdb.set_trace();" line and move the current line to one up.
Whenever I press "F8" key, emacs to jump to "import pdb; pdb.set_trace();" in the same buffer.
I am trying to learn elisp and catch up lisp soon to customize emacs myself. I will appreciate your answers.
The answer shall be great enough for me and others who find this solution is very useful.
to do 1)
(defun add-py-debug ()
"add debug code and move line down"
(interactive)
(move-beginning-of-line 1)
(insert "import pdb; pdb.set_trace();\n"))
(local-set-key (kbd "<f9>") 'add-py-debug)
to do 2) you probably have to change the syntax highlighting of the python mode, or write you own minor mode. You'd have to look into font-lock to get more. Sorry.
to do 3) though I've set this to be C-c F9 instead of Alt-F9
(defun remove-py-debug ()
"remove py debug code, if found"
(interactive)
(let ((x (line-number-at-pos))
(cur (point)))
(search-forward-regexp "^[ ]*import pdb; pdb.set_trace();")
(if (= x (line-number-at-pos))
(let ()
(move-beginning-of-line 1)
(kill-line 1)
(move-beginning-of-line 1))
(goto-char cur))))
(local-set-key (kbd "C c <f9>") 'remove-py-debug)
and to do 4)
(local-set-key (kbd "<f3>") '(lambda ()
(interactive)
(search-forward-regexp "^[ ]*import pdb; pdb.set_trace();")
(move-beginning-of-line 1)))
Note, this is not the best elisp code in the world, but I've tried to make it clear to you what's going on rather than make it totally idiomatic. The GNU Elsip book is a great place to start if you want to do more with elisp.
HTH
I've found that Xah's Elisp Tutorial is an excellent starting point in figuring out the basics of Emacs Lisp programming. There are also some SteveY articles from a while ago that go through techniques you might find useful for learning the basics.
If you're serious about making an amended Python mode, you'll do well to take a look at Writing GNU Emacs Extensions, which is available as a PDF.
Finally, the most useful resource for me is actually Emacs itself. I make frequent use of M-x apropos and M-x describe-key to figure out how built-in functions work, and whether there's something already in place to do what I want.
The specific things you want to look like they can be done through some simple use of insert, and a few search/replace functions, so that'll be a good starting point.

Categories