diff options
author | 2010-12-23 23:06:21 -0500 | |
---|---|---|
committer | 2010-12-23 23:06:21 -0500 | |
commit | dd0d3ab4eec12d9de43297cac504b6b589788d75 (patch) | |
tree | e03b99a1df068663c2e7b671e7f8f94d6a2cd695 | |
parent | 65b71f8a364740f7497efa7ec01709f35b314cfa (diff) | |
download | textadept-dd0d3ab4eec12d9de43297cac504b6b589788d75.tar.gz textadept-dd0d3ab4eec12d9de43297cac504b6b589788d75.zip |
Added filter-through module for shell commands.
-rw-r--r-- | core/locale.conf | 1 | ||||
-rw-r--r-- | doc/manual/10_Advanced.md | 17 | ||||
-rw-r--r-- | doc/manual/14_Appendix.md | 1 | ||||
-rw-r--r-- | doc/manual/1_Introduction.md | 4 | ||||
-rw-r--r-- | modules/textadept/filter_through.lua | 67 | ||||
-rw-r--r-- | modules/textadept/init.lua | 1 | ||||
-rw-r--r-- | modules/textadept/keys.lua | 6 | ||||
-rw-r--r-- | modules/textadept/menu.lua | 1 |
8 files changed, 94 insertions, 4 deletions
diff --git a/core/locale.conf b/core/locale.conf index 17e6cbab..0325aed1 100644 --- a/core/locale.conf +++ b/core/locale.conf @@ -129,6 +129,7 @@ gtk-jump-to = gtk-jump-to Command _Entry = Command _Entry _Run = _Run _Compile = _Compile +Fi_lter Through = Fi_lter Through _Snippets = _Snippets _Insert = _Insert _Previous Placeholder = _Previous Placeholder diff --git a/doc/manual/10_Advanced.md b/doc/manual/10_Advanced.md index 86387fca..3295110a 100644 --- a/doc/manual/10_Advanced.md +++ b/doc/manual/10_Advanced.md @@ -30,6 +30,23 @@ example of this is [incremental search](../modules/gui.find.html#find_incremental). See `modules/textadept/find.lua` for the implementation. +## Shell Commands and Filtering Text + +Sometimes it is easier to use an existing shell command to manipulate text +instead of using the command entry. An example would be sorting all text in a +buffer (or a selection). You could do the following from the command entry: + + ls={}; for l in buffer:get_text():gmatch('[^\n]+') do ls[#ls+1]=l end; + table.sort(ls); buffer:set_text(table.concat(ls, '\n')) + +A simpler way would be to press `Alt+R` (`Ctrl+Apple+R` on Mac OSX), enter the +shell command `sort`, and hit `Enter`. + +For shell commands, if text is selected, all text on the lines containing the +selection is used as the standard input (stdin) to the command. Otherwise the +entire buffer is used. Either the selected text or buffer is replaced with the +standard output (stdout) of the command. + ## File Encoding Textadept represents all characters and strings internally as UTF-8. You will diff --git a/doc/manual/14_Appendix.md b/doc/manual/14_Appendix.md index 67a5feb7..ff9791cb 100644 --- a/doc/manual/14_Appendix.md +++ b/doc/manual/14_Appendix.md @@ -68,6 +68,7 @@ <tr><td>F2</td><td>F2</td><td>Focus Lua command entry</td></tr> <tr><td>Ctrl+R</td><td>Ctrl+R</td><td>Run file</td></tr> <tr><td>Ctrl+Shift+R</td><td>Ctrl+Shift+R</td><td>Compile file</td></tr> + <tr><td>Alt+R</td><td>Ctrl+Alt+R</td><td>Filter through shell command</td></tr> <tr><td>Tab</td><td>Tab</td><td>Expand snippet or next placeholder or indent text</td></tr> <tr><td>Shift+Tab</td><td>Shift+Tab</td><td>Previous snippet placeholder or dedent text</td></tr> <tr><td>Ctrl+Alt+I</td><td>Ctrl+Apple+I</td><td>Cancel current snippet</td></tr> diff --git a/doc/manual/1_Introduction.md b/doc/manual/1_Introduction.md index 7affdd30..f3fb61f9 100644 --- a/doc/manual/1_Introduction.md +++ b/doc/manual/1_Introduction.md @@ -30,8 +30,8 @@ surprised to find you can write the same commands in Lua, from moving the caret to replacing text, performing searches, and much more! Worried that your existing shell scripts for transforming text in other editors -will not be compatible with Lua or Textadept? No need to be. You can tell Lua to -run them in your shell. +will not be compatible with Lua or Textadept? No need to be. You can run those +scripts from within the editor or Lua. These are just some of Textadept's strengths. Textadept is not about constraining the user to a certain set of features while allowing minimal diff --git a/modules/textadept/filter_through.lua b/modules/textadept/filter_through.lua new file mode 100644 index 00000000..59dd1a42 --- /dev/null +++ b/modules/textadept/filter_through.lua @@ -0,0 +1,67 @@ +-- Copyright 2007-2010 Mitchell mitchell<att>caladbolg.net. See LICENSE. + +local L = _G.locale.localize + +--- +-- Filter-Through for the textadept module. +module('_m.textadept.filter_through', package.seeall) + +local cat = not WIN32 and 'cat' or 'type' +local tmpfile = _USERHOME..'/.ft' +local filter_through_active = false + +--- +-- Prompts for a Linux, Mac OSX, or Windows shell command to filter text +-- through. If text is selected, all text on the lines containing the selection +-- is used as the standard input (stdin) to the command. Otherwise the entire +-- buffer is used. Either the selected text or buffer is replaced with the +-- standard output (stdout) of the command. +function filter_through() + filter_through_active = true + gui.command_entry.entry_text = '' + gui.command_entry.focus() +end + +events.connect('command_entry_keypress', + function(code) + if filter_through_active and code == 0xff1b then -- escape + filter_through_active = false + end + end, 1) -- place before command_entry.lua's handler (if necessary) + +events.connect('command_entry_command', + function(text) -- filter through + if filter_through_active then + local buffer = buffer + local s, e = buffer.selection_start, buffer.selection_end + local input + if s ~= e then -- use selected lines as input + s = buffer:position_from_line(buffer:line_from_position(s)) + if buffer.column[e] > 0 then + e = buffer:position_from_line(buffer:line_from_position(e) + 1) + end + input = buffer:get_sel_text() + else -- use whole buffer as input + input = buffer:get_text() + end + local f = io.open(tmpfile, 'wb') + f:write(input) + f:close() + local cmd = table.concat({ cat, '"'..tmpfile..'"', '|', text }, ' ') + if WIN32 then cmd = cmd:gsub('/', '\\') end + local p = io.popen(cmd) + if s ~= e then + buffer.target_start, buffer.target_end = s, e + buffer:replace_target(p:read('*all')) + buffer:set_sel(buffer.target_start, buffer.target_end) + else + buffer:set_text(p:read('*all')) + buffer:goto_pos(s) + end + p:close() + os.remove(tmpfile) + filter_through_active = false + return true + end + end, 1) -- place before command_entry.lua's handler (if necessary) + diff --git a/modules/textadept/init.lua b/modules/textadept/init.lua index 6bcfbf5d..fbf5afe8 100644 --- a/modules/textadept/init.lua +++ b/modules/textadept/init.lua @@ -9,6 +9,7 @@ require 'textadept.bookmarks' require 'textadept.command_entry' require 'textadept.editing' require 'textadept.find' +require 'textadept.filter_through' require 'textadept.mime_types' require 'textadept.run' require 'textadept.session' diff --git a/modules/textadept/keys.lua b/modules/textadept/keys.lua index 1b79e9ae..490b31c8 100644 --- a/modules/textadept/keys.lua +++ b/modules/textadept/keys.lua @@ -184,7 +184,7 @@ if not OSX then --[[ C: D I J K M U - A: A B C D E F G H J K L M N P R S T U V W X Y Z + A: A B C D E F G H J K L M N P S T U V W X Y Z CS: A B C D G H I J K L M N O Q T U V X Y Z SA: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z CA: A B C D E F G H J K L M N O Q R S T U V W X Y Z @@ -270,6 +270,7 @@ if not OSX then local m_run = _m.textadept.run keys.cr = { m_run.run } keys.cR = { m_run.compile } + keys.ar = { _m.textadept.filter_through.filter_through } -- Snippets local m_snippets = _m.textadept.snippets keys['\t'] = { m_snippets._insert } @@ -317,7 +318,7 @@ else A: D E H J K L U Y CS: C D G H I J K L M O Q S T U V W X Y Z SA: A B C D H I J K L M N O Q R T U V X Y - CA: A C E J K L M N O Q R S U V W X Y Z + CA: A C E J K L M N O Q S U V W X Y Z CSA: A C D E H J K L M N O P Q R S T U V W X Y Z ]]-- @@ -403,6 +404,7 @@ else local m_run = _m.textadept.run keys.cr = { m_run.run } keys.cR = { m_run.compile } + keys.car = { _m.textadept.filter_through.filter_through } -- Snippets local m_snippets = _m.textadept.snippets keys['\t'] = { m_snippets._insert } diff --git a/modules/textadept/menu.lua b/modules/textadept/menu.lua index 85835cfe..96d82dc8 100644 --- a/modules/textadept/menu.lua +++ b/modules/textadept/menu.lua @@ -192,6 +192,7 @@ menubar = { { SEPARATOR }, { L('_Run'), { m_run.run } }, { L('_Compile'), { m_run.compile } }, + { L('Fi_lter Through'), { _m.textadept.filter_through.filter_through } }, { SEPARATOR }, { title = L('_Snippets'), { L('_Insert'), { m_snippets._insert } }, |