From d98dd19c2f0f4a84675d07eff34c641949a40019 Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Wed, 29 Dec 2010 17:21:04 -0500 Subject: The filter-through module handles selected text as stdin better. --- doc/manual/10_Advanced.md | 14 ++++++++++---- doc/manual/14_Appendix.md | 2 +- doc/manual/9_Preferences.md | 1 + modules/textadept/filter_through.lua | 23 ++++++++++++++--------- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/doc/manual/10_Advanced.md b/doc/manual/10_Advanced.md index 3295110a..74b56ddc 100644 --- a/doc/manual/10_Advanced.md +++ b/doc/manual/10_Advanced.md @@ -42,10 +42,16 @@ buffer (or a selection). You could do the following from the command entry: 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. +The standard input (stdin) for shell commands is determined as follows: + +* 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 the + beginning of a line, only the EOL (end of line) characters from the previous + line are included as input. The rest of the line is excluded. +* If text is selected and spans a single line, only the selected text is used. +* If no text is selected, the entire buffer is used. + +The input text is replaced with the standard output (stdout) of the command. ## File Encoding diff --git a/doc/manual/14_Appendix.md b/doc/manual/14_Appendix.md index ff9791cb..c77ee9f0 100644 --- a/doc/manual/14_Appendix.md +++ b/doc/manual/14_Appendix.md @@ -68,7 +68,7 @@ F2F2Focus Lua command entry Ctrl+RCtrl+RRun file Ctrl+Shift+RCtrl+Shift+RCompile file - Alt+RCtrl+Alt+RFilter through shell command + Alt+RCtrl+Apple+RFilter through shell command TabTabExpand snippet or next placeholder or indent text Shift+TabShift+TabPrevious snippet placeholder or dedent text Ctrl+Alt+ICtrl+Apple+ICancel current snippet diff --git a/doc/manual/9_Preferences.md b/doc/manual/9_Preferences.md index 89abc852..12240863 100644 --- a/doc/manual/9_Preferences.md +++ b/doc/manual/9_Preferences.md @@ -27,6 +27,7 @@ with 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/filter_through.lua b/modules/textadept/filter_through.lua index 4d029d8a..a3094c0c 100644 --- a/modules/textadept/filter_through.lua +++ b/modules/textadept/filter_through.lua @@ -12,13 +12,17 @@ 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. +-- through. +-- The standard input (stdin) for shell commands is determined 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 +-- the beginning of a line, only the EOL (end of line) characters from the +-- previous line are included as input. The rest of the line is excluded. (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. function filter_through() filter_through_active = true - gui.command_entry.entry_text = '' gui.command_entry.focus() end @@ -36,11 +40,12 @@ events.connect('command_entry_command', 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) + 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:get_sel_text() + input = buffer:text_range(s, e) else -- use whole buffer as input input = buffer:get_text() end -- cgit v1.2.3