aboutsummaryrefslogtreecommitdiff
path: root/modules/textadept/filter_through.lua
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2013-04-11 23:58:33 -0400
committermitchell <70453897+667e-11@users.noreply.github.com>2013-04-11 23:58:33 -0400
commita65f9ac0c460fba64b2d8ddde2149661c025ea65 (patch)
tree9f1d3e7b73756b264974d0be272a722fc92e483e /modules/textadept/filter_through.lua
parent2a54e9fcfd955b8cf63fa42f41292d1176026721 (diff)
downloadtextadept-a65f9ac0c460fba64b2d8ddde2149661c025ea65.tar.gz
textadept-a65f9ac0c460fba64b2d8ddde2149661c025ea65.zip
Added key modes and changed the command entry to use them.
Removed obsoleted `events.COMMAND_ENTRY_COMMAND`.
Diffstat (limited to 'modules/textadept/filter_through.lua')
-rw-r--r--modules/textadept/filter_through.lua87
1 files changed, 35 insertions, 52 deletions
diff --git a/modules/textadept/filter_through.lua b/modules/textadept/filter_through.lua
index fbff074c..59cb77b0 100644
--- a/modules/textadept/filter_through.lua
+++ b/modules/textadept/filter_through.lua
@@ -9,11 +9,12 @@ module('_M.textadept.filter_through')]]
local cat = not WIN32 and 'cat' or 'type'
local tmpfile = _USERHOME..'/.ft'
-local filter_through_active = false
---
--- Prompts the user for a Linux, BSD, Mac OSX, or Windows shell command to
--- filter text through with standard input (stdin) as follows:
+-- Passes selected or all buffer text to string shell command *cmd* as standard
+-- input (stdin) and replaces the input text with the command's standard output
+-- (stdout).
+-- Standard input is as follows:
--
-- 1. If text is selected and spans multiple lines, all text on the lines
-- containing the selection is used. However, if the end of the selection is at
@@ -22,57 +23,39 @@ local filter_through_active = false
-- 2. If text is selected and spans a single line, only the selected text is
-- used.
-- 3. If no text is selected, the entire buffer is used.
---
--- The input text is replaced with the standard output (stdout) of the command.
+-- @param cmd The Linux, BSD, Mac OSX, or Windows shell command to filter text
+-- through.
-- @name filter_through
-function M.filter_through()
- filter_through_active = true
- gui.command_entry.focus()
-end
-
-local events = events
-
-events.connect(events.COMMAND_ENTRY_KEYPRESS, function(code)
- if filter_through_active and keys.KEYSYMS[code] == 'esc' then
- filter_through_active = false
- end
-end, 1) -- place before command_entry.lua's handler (if necessary)
-
--- Filter through.
-events.connect(events.COMMAND_ENTRY_COMMAND, function(text)
- 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
- local i, j = buffer:line_from_position(s), buffer:line_from_position(e)
- if i < j then
- s = buffer:position_from_line(i)
- if buffer.column[e] > 0 then e = buffer:position_from_line(j + 1) end
- end
- input = buffer:text_range(s, e)
- else -- use whole buffer as input
- input = buffer:get_text()
- end
- local f = io.open(tmpfile, 'wb')
- f:write(input)
- f:close()
- local cmd = 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)
+function M.filter_through(cmd)
+ local buffer = buffer
+ local s, e = buffer.selection_start, buffer.selection_end
+ local input
+ if s ~= e then -- use selected lines as input
+ local i, j = buffer:line_from_position(s), buffer:line_from_position(e)
+ if i < j then
+ s = buffer:position_from_line(i)
+ if buffer.column[e] > 0 then e = buffer:position_from_line(j + 1) end
end
- p:close()
- os.remove(tmpfile)
- filter_through_active = false
- return true
+ input = buffer:text_range(s, e)
+ else -- use whole buffer as input
+ input = buffer:get_text()
end
-end, 1) -- place before command_entry.lua's handler (if necessary)
+ local f = io.open(tmpfile, 'wb')
+ f:write(input)
+ f:close()
+ local cmd = cat..' "'..tmpfile..'" | '..cmd
+ 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)
+end
return M