|
1 | | -"""hooks for IPython. |
| 1 | +"""Hooks for IPython. |
2 | 2 |
|
3 | 3 | In Python, it is possible to overwrite any method of any object if you really |
4 | | -want to. But IPython exposes a few 'hooks', methods which are _designed_ to |
| 4 | +want to. But IPython exposes a few 'hooks', methods which are *designed* to |
5 | 5 | be overwritten by users for customization purposes. This module defines the |
6 | 6 | default versions of all such hooks, which get used by IPython if not |
7 | 7 | overridden by the user. |
8 | 8 |
|
9 | | -hooks are simple functions, but they should be declared with 'self' as their |
| 9 | +Hooks are simple functions, but they should be declared with ``self`` as their |
10 | 10 | first argument, because when activated they are registered into IPython as |
11 | | -instance methods. The self argument will be the IPython running instance |
| 11 | +instance methods. The self argument will be the IPython running instance |
12 | 12 | itself, so hooks have full access to the entire IPython object. |
13 | 13 |
|
14 | | -If you wish to define a new hook and activate it, you need to put the |
15 | | -necessary code into a python file which can be either imported or execfile()'d |
16 | | -from within your profile's ipython_config.py configuration. |
| 14 | +If you wish to define a new hook and activate it, you can make an :doc:`extension |
| 15 | +</config/extensions/index>` or a :ref:`startup script <startup_files>`. For |
| 16 | +example, you could use a startup file like this:: |
17 | 17 |
|
18 | | -For example, suppose that you have a module called 'myiphooks' in your |
19 | | -PYTHONPATH, which contains the following definition: |
| 18 | + import os |
20 | 19 |
|
21 | | -import os |
22 | | -from IPython.core import ipapi |
23 | | -ip = ipapi.get() |
24 | | -
|
25 | | -def calljed(self,filename, linenum): |
26 | | - "My editor hook calls the jed editor directly." |
27 | | - print "Calling my own editor, jed ..." |
28 | | - if os.system('jed +%d %s' % (linenum,filename)) != 0: |
29 | | - raise TryNext() |
| 20 | + def calljed(self,filename, linenum): |
| 21 | + "My editor hook calls the jed editor directly." |
| 22 | + print "Calling my own editor, jed ..." |
| 23 | + if os.system('jed +%d %s' % (linenum,filename)) != 0: |
| 24 | + raise TryNext() |
30 | 25 |
|
31 | | -ip.set_hook('editor', calljed) |
| 26 | + def load_ipython_extension(ip): |
| 27 | + ip.set_hook('editor', calljed) |
32 | 28 |
|
33 | | -You can then enable the functionality by doing 'import myiphooks' |
34 | | -somewhere in your configuration files or ipython command line. |
35 | 29 | """ |
36 | 30 |
|
37 | 31 | #***************************************************************************** |
|
0 commit comments