Re: Tabs (or spaces) in snippets

From: Robert <ro....at.web.de>
Date: Wed, 7 Apr 2010 09:02:03 -0700 (PDT)

On Mar 29, 10:12 pm, mitchell <mforal.n....at.gmail.com> wrote:
> Robert,
>
> > does anyone know if there is an easy way to get a "tab" inserted in a
> > snippet that respects that currently settabsize and type?
>
> >tab= "\t",
> > Will be alway be atabstop even if spaces are set.
>
> > btab = "if %1:\n%(buffer:tab())",
> > indents the entire block onetab(respecting the current setting)
>
> > atab = "if %1:\n%2(#(buffer:tab())) #end"
> > adds atab(respecting the current setting) add the end of the entire
> > snippet
>
> The snippet text could be preprocessed toreplace'\t' with
> string.rep(' ', buffer.indent_size). I think it's indent_size anyway.
> I'll accept a patch.
>
> Mitchell
>
Sorry for the delay :-)
I hope this is readable... I replaced the double spaces in the build-
in module as well.
Robert

diff -r 9e37b7c405f8 modules/cpp/snippets.lua
--- a/modules/cpp/snippets.lua Sun Apr 04 21:27:21 2010 +0200
+++ b/modules/cpp/snippets.lua Wed Apr 07 17:37:54 2010 +0200
@@ -13,8 +13,8 @@
     cc = 'const_cast<%1>(%2(%(selected_text)))',

     -- Lua snippets
- lf = 'static int %1(function)(lua_State *%2(lua)) {\n %0\n
return 0;\n}',
- lff = 'LF %1(function)(lua_State *%2(lua)) {\n %0\n return
0;\n}',
+ lf = 'static int %1(function)(lua_State *%2(lua)) {\n\t%0\n
\treturn 0;\n}',
+ lff = 'LF %1(function)(lua_State *%2(lua)) {\n\t%0\n\treturn
0;\n}',
     ls = 'lua_State',
     lgf = 'lua_getfield(%1(lua), %2(-1), %3(field));',
     lgg = 'lua_getglobal(%1(lua), %2(global));',
diff -r 9e37b7c405f8 modules/lua/snippets.lua
--- a/modules/lua/snippets.lua Sun Apr 04 21:27:21 2010 +0200
+++ b/modules/lua/snippets.lua Wed Apr 07 17:37:54 2010 +0200
@@ -10,14 +10,14 @@
   snippets.lua = {
     l = "local %1(expr)%2( = %3(value))",
     p = "print(%0)",
- f = "function %1(name)(%2(args))\n %0\nend",
- fori = "for %1(i), %2(val) in ipairs(%3(table)) do\n %0\nend",
- ['for'] = "for i=%1(1), %2(10)%3(, -1) do\n %0\nend",
- forp = "for %1(k), %2(v) in pairs(%3(table)) do\n %0\nend",
+ f = "function %1(name)(%2(args))\n\t%0\nend",
+ fori = "for %1(i), %2(val) in ipairs(%3(table)) do\n\t%0\nend",
+ ['for'] = "for i=%1(1), %2(10)%3(, -1) do\n\t%0\nend",
+ forp = "for %1(k), %2(v) in pairs(%3(table)) do\n\t%0\nend",
     find = "string.find(%1(str), %2(pattern))",
     len = "string.len(%1(str))",
     gsub = "string.gsub(%1(str), %2(pattern), %3(repl))",
- gfind = "for %1(match) in string.gfind(%2(str), %3(pattern)) do
\n %0\nend",
+ gfind = "for %1(match) in string.gfind(%2(str), %3(pattern)) do\n
\t%0\nend",
     c = "-- ",

     tc = "local %1(tc) = lunit.TestCase('%2(description)')",
diff -r 9e37b7c405f8 modules/textadept/lsnippets.lua
--- a/modules/textadept/lsnippets.lua Sun Apr 04 21:27:21 2010 +0200
+++ b/modules/textadept/lsnippets.lua Wed Apr 07 17:37:54 2010 +0200
@@ -344,6 +344,11 @@
     buffer:begin_undo_action()
     s_text = handle_escapes(s_text)

+ -- Replace tabs with spaces if their use is set in the buffer
+ if not buffer.use_tabs then
+ s_text = s_text:gsub('\t', string.rep(' ', buffer.tab_width))
+ end
+
     -- Execute Lua and shell code.
     s_text = s_text:gsub('%%(%b())', run_lua_code)
     s_text =
Received on Wed 07 Apr 2010 - 12:02:03 EDT

This archive was generated by hypermail 2.2.0 : Thu 08 Mar 2012 - 11:41:41 EST