Combining python-mode with auto-insert mode

Asked by Marcin

Hi,

I have been using auto-insert mode to insert the shebang and some common imports into empty Python scripts. That worked fine with the default python.el mode but with python-mode it gives me a "Wrong type argument: inserted chars, 24" error. Any ideas how this could be avoided?

Cheers,
Marcin

Question information

Language:
English Edit question
Status:
Answered
For:
python-mode.el Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Andreas Roehler (a-roehler) said :
#1

Need some more info: Which command/key which triggered the error? May you tell where the code to insert is defined?

Revision history for this message
Marcin (antyfilidor) said :
#2

This is what I have in my init file:
(auto-insert-mode)
(setq auto-insert-directory "~/.emacs.d/autoinsert-templates")
(setq auto-insert-query t) ; Ask before insertion
(setq auto-insert-alist '(("\.py" . "autoinsert-python-template.py")
                            ("\.[Rr]nw" . "autoinsert-sweave-template.Rnw")))

The inserted text is stored in ~/.emacs.d/autoinsert-templates/autoinsert-python-template.py, which at the moment contains only a shebang (#!/usr/bin/env python3).

The thing is that in spite of the error message the text is in fact inserted but I have to manually switch to the buffer associated with the file.

All this is with python-mode 6.1.3 and Emacs 24.3.1.

Revision history for this message
Marcin (antyfilidor) said :
#3

I have just tried it with the previous version of pyton-mode (6.1.2) and it works fine there.

Cheers,
M.

Revision history for this message
Andreas Roehler (a-roehler) said :
#4

Hmm, when opening an empty py-file, buffer isn't current any more? Which one then?

Revision history for this message
Marcin (antyfilidor) said :
#5

Whichever buffer is current at the time.

Revision history for this message
Andreas Roehler (a-roehler) said :
#6

Sorry, don't understand the proceeding yet. Should it work in connection with opening a file or is there a command M-x auto-insert?

Revision history for this message
Marcin (antyfilidor) said :
#7

There is such a command but turning auto-insert-mode globally will insert the text automatically (it uses find-file-hook, I think).

So the procedure in my case consists in running find-file and pointing it to an empty or non-existent file. What happens then is the error message shows up in the minibuffer and the current buffer does not change. I can then switch to the buffer holding the empty file and the inserted text is there.

As for the M-x command, running it on an empty file produces the same error and inserts the text. Running it on a non-empty file is fine.

Revision history for this message
Andreas Roehler (a-roehler) said :
#8

On 19.06.2014 20:02, Marcin wrote:
> Question #250467 on python-mode.el changed:
> https://answers.launchpad.net/python-mode/+question/250467
>
> Marcin gave more information on the question:
> There is such a command but turning auto-insert-mode globally will
> insert the text automatically (it uses find-file-hook, I think).
>
> So the procedure in my case consists in running find-file and pointing
> it to an empty or non-existent file. What happens then is the error
> message shows up in the minibuffer and the current buffer does not
> change. I can then switch to the buffer holding the empty file and the
> inserted text is there.
>
> As for the M-x command, running it on an empty file produces the same
> error and inserts the text. Running it on a non-empty file is fine.
>

With `define-auto-insert' instead of `setq' example below works for me:

(auto-insert-mode)
(setq auto-insert-directory "~/insert/")
(setq auto-insert-query t)
(define-auto-insert "\.py" "my-python-template.py")

AFAICS it's not a python-mode issue.

Best,

Andreas

Can you help with this problem?

Provide an answer of your own, or ask Marcin for more information if necessary.

To post a message you must log in.