From 36d363c7bd3afbc1263beec4ee53a1417c118bd0 Mon Sep 17 00:00:00 2001 From: moculus Date: Wed, 2 Apr 2008 23:06:52 +0000 Subject: [PATCH] tab width stuff --HG-- branch : pmacs2 --- buffer.py | 1 + method/__init__.py | 21 ++++++++++++++------- mode/c.py | 12 +++++++----- mode/java.py | 12 +++++++----- mode/javascript.py | 3 ++- mode/perl.py | 10 ++++++---- mode/python.py | 11 ++++++----- mode/sh.py | 5 +++-- mode/sql.py | 17 +++++++++-------- tab.py | 1 - 10 files changed, 55 insertions(+), 38 deletions(-) diff --git a/buffer.py b/buffer.py index 669eb9e..ce0cd96 100644 --- a/buffer.py +++ b/buffer.py @@ -58,6 +58,7 @@ class Buffer(object): self.nl = nl self.modified = False self.highlights = {} + self.indentlvl = 4 # basic file operation stuff def _open_file_r(self, path): diff --git a/method/__init__.py b/method/__init__.py index 2137212..7e1f75e 100644 --- a/method/__init__.py +++ b/method/__init__.py @@ -261,10 +261,10 @@ class DeleteRight(Method): '''Delete the character under the cursor''' def _execute(self, w, **vargs): cursor = w.logical_cursor() - line = w.buffer.lines[cursor.y] - if len(line[cursor.x:]) >= 4 and line[:cursor.x + 4].isspace(): - w.kill(Point(cursor.x, cursor.y), - Point(cursor.x + 4, cursor.y)) + line = w.buffer.lines[cursor.y] + lvl = w.mode.tabwidth + if len(line[cursor.x:]) >= lvl and line[:cursor.x + lvl].isspace(): + w.kill(Point(cursor.x, cursor.y), Point(cursor.x + lvl, cursor.y)) else: w.right_delete() class DeleteLeftWord(Method): @@ -723,8 +723,9 @@ class Redo(Method): w.set_error("%s" % (e)) class UnindentBlock(Method): - '''Prepend 4 spaces to each line in region''' + '''Prepend a tab of space to each line in region''' def _execute(self, w, **vargs): + lvl = w.mode.tabwidth cursor = w.logical_cursor() if cursor < w.mark: p1 = cursor @@ -738,11 +739,11 @@ class UnindentBlock(Method): lines = w.buffer.lines[p1.y:p2.y] for i in range(0, len(lines)): if lines[i].startswith(' '): - lines[i] = lines[i][4:] + lines[i] = lines[i][lvl:] w.buffer.delete(Point(0, p1.y), Point(0, p2.y)) w.buffer.insert_string(Point(0, p1.y), '\n'.join(lines) + '\n') class IndentBlock(Method): - '''Add 4 spaces to each line in region''' + '''Prepend a tab of space to each line in region''' def _execute(self, w, **vargs): cursor = w.logical_cursor() if cursor < w.mark: @@ -954,3 +955,9 @@ class SetConfigVariable(Method): w.set_error("param %r set to %r" % (name, value)) else: w.set_error("previously unset param %r set to %r" % (name, value)) + +class SetTabWidth(Method): + args = [arg('width', t=type(0), p="Tab Width: ", h='New tab width for buffer')] + def _execute(self, w, **vargs): + w.mode.tabwidth = vargs['width'] + w.set_error('Tab width set to %d' % w.mode.tabwidth) diff --git a/mode/c.py b/mode/c.py index 7c1ad05..eac3248 100644 --- a/mode/c.py +++ b/mode/c.py @@ -104,6 +104,7 @@ class CTabber(tab.StackTabber): tab.StackTabber._handle_open_token(self, currlvl, y, i) return currlvl def _handle_close_token(self, currlvl, y, i): + w = self.mode.tabwidth self._opt_pop('cont') currlvl = tab.StackTabber._handle_close_token(self, currlvl, y, i) token = self.get_token(y, i) @@ -115,9 +116,10 @@ class CTabber(tab.StackTabber): pass else: if token.fqname() != 'macro.delimiter': - self._opt_append('cont', currlvl + 4) + self._opt_append('cont', currlvl + w) return currlvl def _handle_other_token(self, currlvl, y, i): + w = self.mode.tabwidth token = self.get_token(y, i) fqname = token.fqname() if fqname == 'delimiter' and token.string == ';': @@ -128,7 +130,7 @@ class CTabber(tab.StackTabber): elif fqname == 'keyword': if token.string in ('do', 'else', 'for', 'if', 'while'): - self._append('cond', currlvl + 4) + self._append('cond', currlvl + w) elif token.string == 'break': self._opt_pop('case', 'while', 'for') elif token.string == 'continue': @@ -136,14 +138,14 @@ class CTabber(tab.StackTabber): elif token.string == 'case': self._opt_pop('case') currlvl = self.get_curr_level() - self._opt_append('case', currlvl + 4) + self._opt_append('case', currlvl + w) elif fqname == 'string.start': self._opt_append('string', None) elif fqname == 'string.end': self._opt_pop('string') if self.is_rightmost_token(y, i): - self._opt_append('cont', currlvl + 4) + self._opt_append('cont', currlvl + w) # TODO: this could be a lot better elif fqname == 'macro': @@ -172,7 +174,7 @@ class CTabber(tab.StackTabber): not fqname == 'spaces' and not fqname == 'eol' and token.string not in ('}', ';', '(', '{', '[', ',')): - self._opt_append('cont', currlvl + 4) + self._opt_append('cont', currlvl + w) return currlvl class C(mode.Fundamental): diff --git a/mode/java.py b/mode/java.py index ece8be2..a50d9a8 100644 --- a/mode/java.py +++ b/mode/java.py @@ -85,6 +85,7 @@ class JavaTabber(CTabber): currlvl = tab.StackTabber._handle_open_token(self, currlvl, y, i) return currlvl def _handle_close_token(self, currlvl, y, i): + w = self.mode.tabwidth self._opt_pop('cont') currlvl = tab.StackTabber._handle_close_token(self, currlvl, y, i) token = self.get_token(y, i) @@ -95,9 +96,10 @@ class JavaTabber(CTabber): elif self._peek_name() == 'cond': pass else: - self._opt_append('cont', currlvl + 4) + self._opt_append('cont', currlvl + w) return currlvl def _handle_other_token(self, currlvl, y, i): + w = self.mode.tabwidth token = self.get_token(y, i) fqname = token.fqname() if fqname == 'delimiter' and token.string == ';': @@ -107,7 +109,7 @@ class JavaTabber(CTabber): elif fqname == 'keyword': if token.string in ('do', 'else', 'for', 'if', 'while'): - self._append('cond', currlvl + 4) + self._append('cond', currlvl + w) elif token.string == 'break': self._opt_pop('case', 'while', 'for') elif token.string == 'continue': @@ -115,14 +117,14 @@ class JavaTabber(CTabber): elif token.string == 'case': self._opt_pop('case') currlvl = self.get_curr_level() - self._opt_append('case', currlvl + 4) + self._opt_append('case', currlvl + w) elif fqname == 'string.start': self._opt_append('string', None) elif fqname == 'string.end': self._opt_pop('string') if self.is_rightmost_token(y, i): - self._opt_append('cont', currlvl + 4) + self._opt_append('cont', currlvl + w) # TODO: this could be a lot better elif fqname == 'macro': @@ -150,7 +152,7 @@ class JavaTabber(CTabber): not fqname == 'null' and not fqname == 'eol' and token.string not in ('}', ';', '(', '{', '[', ',')): - self._opt_append('cont', currlvl + 4) + self._opt_append('cont', currlvl + w) return currlvl class Java(mode.Fundamental): diff --git a/mode/javascript.py b/mode/javascript.py index e0be60f..b77ae09 100644 --- a/mode/javascript.py +++ b/mode/javascript.py @@ -44,10 +44,11 @@ class JavascriptTabber(tab.StackTabber): t = highlighter.tokens[y][0] return t.name == 'reserved' and t.string == 'function' def _handle_other_token(self, currlvl, y, i): + w = self.mode.tabwidth token = self.get_token(y, i) fqname = token.fqname() if token.name == 'operator' and token.string == '=': - self._opt_append("cont", currlvl + 4) + self._opt_append("cont", currlvl + w) elif token.name == 'delimiter' and token.string == ";": self._opt_pop("cont") return currlvl diff --git a/mode/perl.py b/mode/perl.py index 1206678..001d5af 100644 --- a/mode/perl.py +++ b/mode/perl.py @@ -142,6 +142,7 @@ class PerlTabber(tab.StackTabber): currlvl = tab.StackTabber._handle_open_token(self, currlvl, y, i) return currlvl def _handle_close_token(self, currlvl, y, i): + w = self.mode.tabwidth self._opt_pop('cont') currlvl = tab.StackTabber._handle_close_token(self, currlvl, y, i) token = self.get_token(y, i) @@ -149,9 +150,10 @@ class PerlTabber(tab.StackTabber): if token.string == '}': self._opt_pop('cont') else: - self._opt_append('cont', currlvl + 4) + self._opt_append('cont', currlvl + w) return currlvl def _handle_other_token(self, currlvl, y, i): + w = self.mode.tabwidth token = self.get_token(y, i) fqname = token.fqname() if fqname == 'delimiter' and token.string == ';': @@ -162,7 +164,7 @@ class PerlTabber(tab.StackTabber): self._opt_pop('heredoc') self._opt_pop('cont') elif fqname == 'quoted.start': - self._opt_append('quoted', currlvl + 4) + self._opt_append('quoted', currlvl + w) elif fqname == 'quoted.end': self._opt_pop('cont') self._opt_pop('quoted') @@ -181,7 +183,7 @@ class PerlTabber(tab.StackTabber): elif fqname == 'perl_string.end': self._opt_pop('string') if self.is_rightmost_token(y, i): - self._opt_append('cont', currlvl + 4) + self._opt_append('cont', currlvl + w) if self.is_rightmost_token(y, i): if(not fqname.startswith('pod') and not fqname.startswith('heredoc') and @@ -191,7 +193,7 @@ class PerlTabber(tab.StackTabber): not fqname == 'comment' and not fqname == 'null' and token.string not in ('}', ';', '(', '{', '[', ',')): - self._opt_append('cont', currlvl + 4) + self._opt_append('cont', currlvl + w) return currlvl class Perl(mode.Fundamental): diff --git a/mode/python.py b/mode/python.py index 1f35971..3dbeaf6 100644 --- a/mode/python.py +++ b/mode/python.py @@ -124,6 +124,7 @@ class PythonTabber(tab.StackTabber): return currlvl def _handle_other_token(self, currlvl, y, i): + w = self.mode.tabwidth token = self.get_token(y, i) fqname = token.fqname() if fqname == 'continuation': @@ -132,7 +133,7 @@ class PythonTabber(tab.StackTabber): if self.continued: self._opt_append('cont', currlvl) else: - self._opt_append('cont', currlvl + 4) + self._opt_append('cont', currlvl + w) elif fqname == 'string.start': # while inside of a string, there is no indention leve self._opt_append('string', None) @@ -157,25 +158,25 @@ class PythonTabber(tab.StackTabber): self.popped = True elif token.string in self.startlevel_names and self.is_leftmost_token(y, i): # we know we will indent exactly once - self._append(token.string, currlvl + 4) + self._append(token.string, currlvl + w) elif token.string in ('elif', 'else') and self.is_leftmost_token(y, i): # we know we'll unindent at least to the first if/elif if not self.popped and not self.last_popped: self._pop_until('if', 'elif') currlvl = self.get_curr_level() - self._append(token.string, currlvl + 4) + self._append(token.string, currlvl + w) elif token.string == 'except' and self.is_leftmost_token(y, i): # we know we'll unindent at least to the first try if not self.popped and not self.last_popped: self._pop_until('try') currlvl = self.get_curr_level() - self._append(token.string, currlvl + 4) + self._append(token.string, currlvl + w) elif token.string == 'finally' and self.is_leftmost_token(y, i): # we know we'll unindent at least to the first try/except if not self.popped and not self.last_popped: self._pop_until('try', 'except') currlvl = self.get_curr_level() - self._append(token.string, currlvl + 4) + self._append(token.string, currlvl + w) return currlvl class Python(mode.Fundamental): diff --git a/mode/sh.py b/mode/sh.py index a12aac0..74718ae 100644 --- a/mode/sh.py +++ b/mode/sh.py @@ -93,12 +93,13 @@ class ShTabber(tab.StackTabber): else: return tab.StackTabber._handle_close_token(self, currlvl, y, i) def _handle_other_token(self, currlvl, y, i): + w = self.mode.tabwidth token = self.get_token(y, i) fqname = token.fqname() if token.name == 'continuation': - self._opt_append("cont", currlvl + 4) + self._opt_append("cont", currlvl + w) elif token.name == 'sh_reserved' and token.string == 'else': - currlvl -= 4 + currlvl -= w elif token.name == 'eol': self._opt_pop("cont") return currlvl diff --git a/mode/sql.py b/mode/sql.py index b6de3db..1a11bc3 100644 --- a/mode/sql.py +++ b/mode/sql.py @@ -74,15 +74,16 @@ class SqlTabber(tab.StackTabber): t = highlighter.tokens[y][0] return t.name == 'function' def _handle_other_token(self, currlvl, y, i): - token = self.get_token(y, i) - s = token.string.lower() + w = self.mode.tabwidth + token = self.get_token(y, i) + s = token.string.lower() if token.name == 'delimiter' and s == ';': self._opt_pop('cont') elif token.name == 'sql_keyword': if s == 'declare': - self._append('declare', currlvl + 4) + self._append('declare', currlvl + w) elif s == 'begin': - currlvl -= 4 + currlvl -= w elif s == 'end': self._opt_pop('declare') currlvl = self.get_curr_level() @@ -93,15 +94,15 @@ class SqlTabber(tab.StackTabber): self._opt_pop('loop') currlvl = self.get_curr_level() elif s == 'else': - currlvl -= 4 + currlvl -= w elif s == 'if': - self._append('if', currlvl + 4) + self._append('if', currlvl + w) elif s in ('while', 'for'): - self._append('loop', currlvl + 4) + self._append('loop', currlvl + w) if self.is_rightmost_token(y, i): if not self._empty() and token.name == 'continuation': - self._append('cont', currlvl + 4) + self._append('cont', currlvl + w) elif token.name == 'eol' and not self.markers: self._opt_pop("cont") return currlvl diff --git a/tab.py b/tab.py index ec6670e..eed2d23 100644 --- a/tab.py +++ b/tab.py @@ -155,7 +155,6 @@ class StackTabber(Tabber): token = self.get_token(y, i) rtoken = self.get_next_right_token(y, i) if rtoken is None: - #level = self.get_curr_level() + 4 level = self.get_curr_level() + self.mode.tabwidth else: level = rtoken.x