This repository was archived by the owner on Nov 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
[DO NOT PULL] Issue #7 Selection and Copy #389
Open
Eugene-msc
wants to merge
1
commit into
p-e-w:master
Choose a base branch
from
Eugene-msc:copypaste
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -373,9 +373,13 @@ public class FinalTerm : Gtk.Application { | |
return; | ||
|
||
case Command.CommandType.COPY_TO_CLIPBOARD: | ||
if (command.parameters.is_empty) | ||
return; | ||
Utilities.set_clipboard_text(command.parameters.get(0)); | ||
string text = active_terminal_widget.get_selected_text(); | ||
if (text == "") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem here is that if any text is selected, clicking a "copy to clipboard" entry in a semantic text menu (e.g. for an IP address) will copy the selection rather than the text menu caption, which is not what the user expects. A separate |
||
text = command.parameters.get(0); | ||
} | ||
if (text != "") { | ||
Utilities.set_clipboard_text(text); | ||
} | ||
return; | ||
|
||
case Command.CommandType.PASTE_FROM_CLIPBOARD: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,13 @@ public class LineView : Clutter.Actor { | |
private Mx.Button collapse_button = null; | ||
private Clutter.Text text_container; | ||
|
||
private struct LineSelection { | ||
int start; | ||
int end; | ||
} | ||
|
||
private LineSelection selection; | ||
|
||
public bool is_prompt_line { get {return original_output_line.is_prompt_line;}} | ||
|
||
public LineView(TerminalOutput.OutputLine output_line, LineContainer line_container) { | ||
|
@@ -57,6 +64,8 @@ public class LineView : Clutter.Actor { | |
|
||
on_settings_changed(null); | ||
Settings.get_default().changed.connect(on_settings_changed); | ||
|
||
selection = LineSelection(); | ||
} | ||
|
||
public void get_character_coordinates(int character_index, out int x, out int y) { | ||
|
@@ -75,6 +84,44 @@ public class LineView : Clutter.Actor { | |
y = (int)(character_y + text_container.allocation.get_y()); | ||
} | ||
|
||
public int get_coordinates_character(float x, float y) { | ||
// TODO: coords_to_position seems to be buggy when working with non-latin characters | ||
// return text_container.coords_to_position(x - text_container.get_x(), y - text_container.get_y()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
int byte_index; | ||
int trailing; | ||
|
||
float text_container_x; | ||
float text_container_y; | ||
text_container.get_transformed_position(out text_container_x, out text_container_y); | ||
|
||
text_container.get_layout().xy_to_index( | ||
(int)(x - text_container_x) * Pango.SCALE, | ||
(int)(y - text_container_y) * Pango.SCALE, | ||
out byte_index, out trailing); | ||
|
||
return Utilities.byte_index_to_character_index(output_line.get_text(), byte_index) + 1; | ||
} | ||
|
||
public void set_selection(int start, int end, Selection.SelectionMode mode) { | ||
selection.start = start; | ||
selection.end = end; | ||
|
||
if (mode == Selection.SelectionMode.WORD && | ||
(selection.start != 0 || selection.end != 0)) { | ||
string text = output_line.get_text(); | ||
|
||
while (selection.start > 0 && | ||
!" \t\n\r-:\'\"".contains(text.substring(selection.start-1, 1))) { | ||
selection.start--; | ||
} | ||
while (selection.end < text.char_count() && | ||
selection.end > 0 && | ||
!" \t\n\r-:\'\"".contains(text.substring(selection.end, 1))) { | ||
selection.end++; | ||
} | ||
} | ||
} | ||
|
||
private bool on_text_container_motion_event(Clutter.MotionEvent event) { | ||
// Apparently, motion event coordinates are relative to the stage | ||
// (the Clutter documentation does not specify this) | ||
|
@@ -167,6 +214,35 @@ public class LineView : Clutter.Actor { | |
text_container.set_markup(get_markup(output_line)); | ||
} | ||
|
||
public string get_selected_text() { | ||
int element_offset = 0; | ||
int len = 0; | ||
int offset = 0; | ||
string text = ""; | ||
foreach (var text_element in output_line) { | ||
// TODO: make this block of code less ugly | ||
len = offset = 0; | ||
if ((selection.end >= element_offset || selection.end == -1)&& | ||
selection.start < element_offset + text_element.text.char_count()) { | ||
offset = int.max(selection.start - element_offset, 0); | ||
if (selection.end == -1 || | ||
selection.end > element_offset + text_element.text.char_count()) { | ||
len = text_element.text.char_count() - offset; | ||
} else | ||
len = selection.end - offset - element_offset; | ||
} | ||
|
||
var offset_index = text_element.text.index_of_nth_char(offset); | ||
var len_index = text_element.text.index_of_nth_char(offset + len); | ||
|
||
text += text_element.text.substring(offset_index, len_index - offset_index); | ||
|
||
element_offset += text_element.text.char_count(); | ||
} | ||
|
||
return text; | ||
} | ||
|
||
private void update_collapse_button() { | ||
collapse_button.style = Settings.get_default().theme.style; | ||
|
||
|
@@ -179,20 +255,60 @@ public class LineView : Clutter.Actor { | |
private string get_markup(TerminalOutput.OutputLine output_line) { | ||
var markup_builder = new StringBuilder(); | ||
|
||
int element_offset = 0; | ||
int len = 0; | ||
int offset = 0; | ||
|
||
foreach (var text_element in output_line) { | ||
// TODO: make this block of code less ugly | ||
len = offset = 0; | ||
if ((selection.end >= element_offset || selection.end == -1)&& | ||
selection.start < element_offset + text_element.text.char_count()) { | ||
offset = int.max(selection.start - element_offset, 0); | ||
if (selection.end == -1 || | ||
selection.end > element_offset + text_element.text.char_count()) { | ||
len = text_element.text.char_count() - offset; | ||
} else { | ||
len = selection.end - offset - element_offset; | ||
} | ||
} | ||
|
||
var offset_index = text_element.text.index_of_nth_char(offset); | ||
var len_index = text_element.text.index_of_nth_char(offset + len); | ||
|
||
var text_attributes = text_element.attributes.get_text_attributes( | ||
Settings.get_default().color_scheme, Settings.get_default().dark); | ||
var markup_attributes = text_attributes.get_markup_attributes( | ||
Settings.get_default().color_scheme, Settings.get_default().dark); | ||
|
||
var pre_selection_text = Markup.escape_text(text_element.text.substring(0, offset_index)); | ||
var selection_text = Markup.escape_text(text_element.text.substring(offset_index, len_index - offset_index)); | ||
var post_selection_text = Markup.escape_text(text_element.text.substring(len_index)); | ||
|
||
// TODO: make selection stylable | ||
if (markup_attributes.length > 0) { | ||
markup_builder.append( | ||
"<span" + markup_attributes + ">" + | ||
Markup.escape_text(text_element.text) + | ||
"</span>"); | ||
pre_selection_text + | ||
"</span>" + | ||
"<span background='#ffffff' foreground='#000000'>" + | ||
selection_text + | ||
"</span>" + | ||
"<span" + markup_attributes + ">" + | ||
post_selection_text + | ||
"</span>" | ||
); | ||
} else { | ||
markup_builder.append(Markup.escape_text(text_element.text)); | ||
markup_builder.append( | ||
pre_selection_text + | ||
"<span background='#ffffff' foreground='#000000'>" + | ||
selection_text + | ||
"</span>" + | ||
post_selection_text | ||
); | ||
|
||
} | ||
element_offset += text_element.text.char_count(); | ||
} | ||
|
||
return markup_builder.str; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright © 2013–2014 Philipp Emanuel Weidmann <pew@worldwidemann.com> | ||
* | ||
* Nemo vir est qui mundum non reddat meliorem. | ||
* | ||
* | ||
* This file is part of Final Term. | ||
* | ||
* Final Term is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Final Term is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Final Term. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
public class Selection { | ||
private TerminalOutput.CursorPosition start; | ||
private TerminalOutput.CursorPosition finish; | ||
|
||
public enum SelectionMode { | ||
NORMAL, | ||
WORD, | ||
LINE, | ||
COLUMN // Does not work | ||
} | ||
|
||
public SelectionMode mode {get; set;} | ||
|
||
public Selection(TerminalOutput.CursorPosition position, SelectionMode mode) { | ||
start = position; | ||
this.mode = mode; | ||
if(this.mode == SelectionMode.LINE) { | ||
start.column = 0; | ||
finish = start; | ||
finish.column = 1; | ||
} | ||
} | ||
|
||
public void update(TerminalOutput.CursorPosition position) { | ||
finish = position; | ||
} | ||
|
||
public void get_line_range(int line, out int from, out int to) { | ||
TerminalOutput.CursorPosition beginning = TerminalOutput.CursorPosition(); | ||
TerminalOutput.CursorPosition end = TerminalOutput.CursorPosition(); | ||
|
||
get_range(out beginning, out end); | ||
|
||
from = to = 0; | ||
|
||
if (mode == SelectionMode.COLUMN) { | ||
if (line >= beginning.line && line <= end.line) { | ||
from = int.min(beginning.column, end.column); | ||
to = int.max(beginning.column, end.column); | ||
} | ||
} else if (mode == SelectionMode.WORD) { | ||
if (line == beginning.line) { | ||
from = beginning.column; | ||
} | ||
if (line >= beginning.line && line < end.line) { | ||
to = -1; | ||
} else if (line == end.line) { | ||
to = end.column; | ||
} | ||
} else if (mode == SelectionMode.LINE) { | ||
if (line >= beginning.line && line <= end.line) { | ||
from = 0; | ||
to = -1; | ||
} | ||
} else { | ||
if (line == beginning.line) { | ||
from = beginning.column; | ||
} | ||
if (line >= beginning.line && line < end.line) { | ||
to = -1; | ||
} else if (line == end.line) { | ||
to = end.column; | ||
} | ||
} | ||
} | ||
|
||
// end position can be before or after start position | ||
// this function returns them in correct order | ||
public void get_range(out TerminalOutput.CursorPosition beginning, out TerminalOutput.CursorPosition end) { | ||
if (start.compare(finish) > 0) { | ||
beginning = finish; | ||
end = start; | ||
} else { | ||
beginning = start; | ||
end = finish; | ||
} | ||
} | ||
}< 45C0 /span> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do these lines have to be commented? The "Shift+Insert" shortcut works on my system even with these lines enabled – it's the "Ctrl+Shift+V" shortcut I can't get to work.