diff --git a/mode/tal.py b/mode/tal.py index 560c509..7b199d1 100644 --- a/mode/tal.py +++ b/mode/tal.py @@ -109,6 +109,42 @@ class Taldoc(Method): '\'': 'raw character', '\"': 'raw word', } + def find_macro(self, name, w): + toks = w.get_highlighter().tokens + target = '%' + name + found = False + start = None + end = None + defn = None + for line in toks: + for t in line: + if not found: + found = t.name == "tal.defmacro" and t.string == target + elif start is None: + if t.name == 'delimiter' and t.string == '{': + start = (t.x + 1, t.y) + elif end is None: + if t.name == 'delimiter' and t.string == '}': + end = (t.x, t.y) + break + if start is None or end is None: + return None + (sx, sy) = start + (ex, ey) = end + lines = w.buffer.lines + if sy == ey: + # 1 lines + return lines[sy][sx:ex] + elif sy == ey + 1: + # 2 lines + return lines[sy][sx:] + ' ' + lines[ey][:e.x] + else: + # 3+ lines + out = [] + out.append(lines[sy][sx:]) + out.extend(lines[sy+1:ey]) + out.append(lines[ey][:ex]) + return ' '.join(out) def _execute(self, w, **vargs): tok = w.get_token() word = tok.string @@ -145,7 +181,11 @@ class Taldoc(Method): kind = self.addr_dict.get(word[0], 'unknown address') w.set_error('%r is a %s' % (word, kind)) else: - w.set_error('%r is not recognized' % word) + defn = self.find_macro(word, w) + if defn is None: + w.set_error('%r is not recognized' % word) + else: + w.set_error('%r is a macro: %s' % (word, defn)) class Tal(Fundamental):