Hi all,
I have been working on a macro that allows the user to select a word, sentence .. etc, and when the user types bracket, for example, then the selected text will be surrounded by brackets. If they type quotes, it will be surrounded by quotes, and so on. This is a functionality I saw on textmate a while ago and considered it pretty handy. The code I have implemented for this macro so far, I have pasted here:
#include "slick.sh"
_command void type_single_quote() name_info(','EDITORCTL_ARG2|MARK_ARG2)
{
if (!select_active()) {
keyin("'");
}
else {
lock_selection();
_begin_select();
_insert_text("'");
_end_select();
cursor_right();
_insert_text("'");
deselect();
}
}
_command type_double_quote() name_info(','EDITORCTL_ARG2|MARK_ARG2)
{
if (!select_active()) {
keyin("\"");
}
else {
lock_selection();
_begin_select();
keyin("\"");
_end_select();
cursor_right();
keyin("\"");
deselect();
}
}
_command type_parenthesis() name_info(','EDITORCTL_ARG2|MARK_ARG2)
{
if (!select_active()) {
keyin("(");
return (0);
}
else {
lock_selection();
_begin_select();
_insert_text("(");
_end_select();
cursor_right();
_insert_text(")");
deselect();
}
}
_command type_bracket() name_info(','EDITORCTL_ARG2|MARK_ARG2)
{
_str l_event = last_event();
if (!select_active()) {
keyin(l_event);
return (0);
}
else {
lock_selection();
_begin_select();
_insert_text("{");
_end_select();
cursor_right();
_insert_text("}");
deselect();
}
}
_command type_square_bracket() name_info(','EDITORCTL_ARG2|MARK_ARG2|VSARG2_LASTKEY)
{
_str l_event = last_event();
if (!select_active()) {
keyin(l_event);
}
else {
// lock_selection();
_begin_select();
_insert_text("[");
_end_select();
cursor_right();
_insert_text("]");
deselect();
}
}
defeventtab default_keys;
def "'" = type_single_quote;
def '"' = type_double_quote;
def '(' = type_parenthesis;
def '{' = type_bracket;
def '[' = type_square_bracket;
I would also like to simplify the code a little bit, but i don't know how to do it. Notice that the functions {type_square_bracket, type_bracket, type_parenthesis, ... etc } are all the same, and they only differ in the symbol being used. If you know how to reduce it to one function, please let me know.
Also, please let me know of other improvements I can make on it.
Thanks,
Ed