##// END OF EJS Templates
Aliases fixes: ls alias improvements and frontend-dependent fixes....
Fernando Perez -
Show More
@@ -10,10 +10,11 b' Authors:'
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
13 # Copyright (C) 2008-2010 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License.
16 # the file COPYING, distributed as part of this software.
16 #
17 # The full license is in the file COPYING.txt, distributed with this software.
17 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
18
19
19 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
@@ -41,50 +42,61 b' from IPython.utils.warn import warn, error'
41 shell_line_split = re.compile(r'^(\s*)(\S*\s*)(.*$)')
42 shell_line_split = re.compile(r'^(\s*)(\S*\s*)(.*$)')
42
43
43 def default_aliases():
44 def default_aliases():
44 # Make some aliases automatically
45 """Return list of shell aliases to auto-define.
45 # Prepare list of shell aliases to auto-define
46 """
47 # Note: the aliases defined here should be safe to use on a kernel
48 # regardless of what frontend it is attached to. Frontends that use a
49 # kernel in-process can define additional aliases that will only work in
50 # their case. For example, things like 'less' or 'clear' that manipulate
51 # the terminal should NOT be declared here, as they will only work if the
52 # kernel is running inside a true terminal, and not over the network.
53
46 if os.name == 'posix':
54 if os.name == 'posix':
47 default_aliases = ('mkdir mkdir', 'rmdir rmdir',
55 default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
48 'mv mv -i','rm rm -i','cp cp -i',
56 ('mv', 'mv -i'), ('rm', 'rm -i'), ('cp', 'cp -i'),
49 'cat cat','less less','clear clear',
57 ('cat', 'cat'),
50 # a better ls
58 ]
51 'ls ls -F',
59 # Useful set of ls aliases. The GNU and BSD options are a little
60 # different, so we make aliases that provide as similar as possible
61 # behavior in ipython, by passing the right flags for each platform
62 if sys.platform.startswith('linux'):
63 ls_aliases = [('ls', 'ls -F --color'),
52 # long ls
64 # long ls
53 'll ls -lF')
65 ('ll', 'ls -F -o --color'),
54 # Extra ls aliases with color, which need special treatment on BSD
55 # variants
56 ls_extra = ( # color ls
57 'lc ls -F -o --color',
58 # ls normal files only
66 # ls normal files only
59 'lf ls -F -o --color %l | grep ^-',
67 ('lf', 'ls -F -o --color %l | grep ^-'),
60 # ls symbolic links
68 # ls symbolic links
61 'lk ls -F -o --color %l | grep ^l',
69 ('lk', 'ls -F -o --color %l | grep ^l'),
62 # directories or links to directories,
70 # directories or links to directories,
63 'ldir ls -F -o --color %l | grep /$',
71 ('ldir', 'ls -F -o --color %l | grep /$'),
64 # things which are executable
72 # things which are executable
65 'lx ls -F -o --color %l | grep ^-..x',
73 ('lx', 'ls -F -o --color %l | grep ^-..x'),
66 )
74 ]
67 # The BSDs don't ship GNU ls, so they don't understand the
75 else:
68 # --color switch out of the box
76 # BSD, OSX, etc.
69 if 'bsd' in sys.platform:
77 ls_aliases = [('ls', 'ls -F'),
70 ls_extra = ( # ls normal files only
78 # long ls
71 'lf ls -lF | grep ^-',
79 ('ll', 'ls -F -l'),
80 # ls normal files only
81 ('lf', 'ls -F -l %l | grep ^-'),
72 # ls symbolic links
82 # ls symbolic links
73 'lk ls -lF | grep ^l',
83 ('lk', 'ls -F -l %l | grep ^l'),
74 # directories or links to directories,
84 # directories or links to directories,
75 'ldir ls -lF | grep /$',
85 ('ldir', 'ls -F -l %l | grep /$'),
76 # things which are executable
86 # things which are executable
77 'lx ls -lF | grep ^-..x',
87 ('lx', 'ls -F -l %l | grep ^-..x'),
78 )
88 ]
79 default_aliases = default_aliases + ls_extra
89 default_aliases = default_aliases + ls_aliases
80 elif os.name in ['nt','dos']:
90 elif os.name in ['nt', 'dos']:
81 default_aliases = ('ls dir /on',
91 default_aliases = [('ls', 'dir /on'),
82 'ddir dir /ad /on', 'ldir dir /ad /on',
92 ('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'),
83 'mkdir mkdir','rmdir rmdir','echo echo',
93 ('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
84 'ren ren','cls cls','copy copy')
94 ('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'),
95 ]
85 else:
96 else:
86 default_aliases = ()
97 default_aliases = []
87 return [s.split(None,1) for s in default_aliases]
98
99 return default_aliases
88
100
89
101
90 class AliasError(Exception):
102 class AliasError(Exception):
@@ -94,12 +106,10 b' class AliasError(Exception):'
94 class InvalidAliasError(AliasError):
106 class InvalidAliasError(AliasError):
95 pass
107 pass
96
108
97
98 #-----------------------------------------------------------------------------
109 #-----------------------------------------------------------------------------
99 # Main AliasManager class
110 # Main AliasManager class
100 #-----------------------------------------------------------------------------
111 #-----------------------------------------------------------------------------
101
112
102
103 class AliasManager(Configurable):
113 class AliasManager(Configurable):
104
114
105 default_aliases = List(default_aliases(), config=True)
115 default_aliases = List(default_aliases(), config=True)
@@ -113,10 +123,7 b' class AliasManager(Configurable):'
113 self.init_aliases()
123 self.init_aliases()
114
124
115 def __contains__(self, name):
125 def __contains__(self, name):
116 if name in self.alias_table:
126 return name in self.alias_table
117 return True
118 else:
119 return False
120
127
121 @property
128 @property
122 def aliases(self):
129 def aliases(self):
@@ -231,7 +238,6 b' class AliasManager(Configurable):'
231 then:
238 then:
232
239
233 baz huhhahhei -> bar /tmp huhhahhei
240 baz huhhahhei -> bar /tmp huhhahhei
234
235 """
241 """
236 line = fn + " " + rest
242 line = fn + " " + rest
237
243
@@ -115,6 +115,28 b' class TerminalInteractiveShell(InteractiveShell):'
115 toggle_set_term_title(False)
115 toggle_set_term_title(False)
116
116
117 #-------------------------------------------------------------------------
117 #-------------------------------------------------------------------------
118 # Things related to aliases
119 #-------------------------------------------------------------------------
120
121 def init_alias(self):
122 # The parent class defines aliases that can be safely used with any
123 # frontend.
124 super(TerminalInteractiveShell, self).init_alias()
125
126 # Now define aliases that only make sense on the terminal, because they
127 # need direct access to the console in a way that we can't emulate in
128 # GUI or web frontend
129 if os.name == 'posix':
130 aliases = [('clear', 'clear'), ('more', 'more'), ('less', 'less'),
131 ('man', 'man')]
132 elif os.name == 'nt':
133 aliases = [('cls', 'cls')]
134
135
136 for name, cmd in aliases:
137 self.alias_manager.define_alias(name, cmd)
138
139 #-------------------------------------------------------------------------
118 # Things related to the banner and usage
140 # Things related to the banner and usage
119 #-------------------------------------------------------------------------
141 #-------------------------------------------------------------------------
120
142
General Comments 0
You need to be logged in to leave comments. Login now