passing selected text as a parameter in slickC
-
27-02-2021 - |
Question
I am trying to get text in a selection to be passed to the function find_max_eq_filter originally. The function I am trying to modify had an "s" parameter which I assume is the selection text. I want to change the function to allow a parameter that is passed by the user. Could someone clue me in how to do this?
_command void align_equals() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
if ( _select_type() != "LINE" && _select_type() != "BLOCK" ) {
message("A LINE or BLOCK selection is required for this function");
return;
}
gMaxEqCol = 0;
_str character = prompt(prmt1, 'What do you wish to align?'); /* I want to ask the user for an input string */
filter_selection(find_max_eq_filter(s, character)); /* Here I want to pass the selection with the string taken from the user. */
if (gMaxEqCol == 0) {
// no equal signs
return;
}
filter_selection(align_eq_filter);
_free_selection("");
}
static _str find_max_eq_filter(s, _str toalign)
{
_str inputStr = expand_tabs(s)
int equalsPos = pos(toalign, inputStr, 1, "");
if ( equalsPos > gMaxEqCol ) {
gMaxEqCol = equalsPos;
}
return s;
}
filter_selection(find_max_eq_filter);
Any help appreciated,
Ted
PS. you want to reproduce what I am doing so I'll have to paste the whole thing:
#include "slick.sh"
static int gMaxEqCol;
static _str gUsrStr;
static _str find_max_char_filter(s)
{
_str inputStr = expand_tabs(s);
int equalsPos = pos(gUsrStr, inputStr, 1, "");
if ( equalsPos > gMaxEqCol ) {
gMaxEqCol = equalsPos;
}
return s;
}
static _str align_char_filter(s)
{
if (gMaxEqCol <= 0 || length(s) == 0) {
return s;
}
_str expandedStr = expand_tabs(s);
int equalsPos = pos(gUsrStr, expandedStr, 1, "");
if (equalsPos == 0) {
return s;
}
_str prefix = substr(expandedStr, 1, equalsPos - 1);
_str postfix = substr(expandedStr,equalsPos);
while (equalsPos < gMaxEqCol ) {
prefix = prefix :+ ' ';
++equalsPos;
}
return prefix :+ postfix;
}
_command void align_chars() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
if ( _select_type() != "LINE" && _select_type() != "BLOCK" ) {
message("A LINE or BLOCK selection is required for this function");
return;
}
gMaxEqCol = 0;
_str prmpt1 = "";
gUsrStr = prompt(prmpt1, "Please enter a string: ");
filter_selection(find_max_char_filter);
if (gMaxEqCol == 0) {
// no equal signs
return;
}
filter_selection(align_char_filter);
_free_selection("");
}
So this is my problem, I want to change gUsrStr to be a parameter of the helper functions instead of having a global variable for it. However, when I try to do that it breaks. Also, I do not quite understand the first argument of the prompt function. What is it suppposed to be? Is it a default value in case the user does not enter anything?
Solution
You are not posting the relevant part, I think you should post the header of the calling function ...
But I'll try my hand at a partial answer, then update if you update the question ...
Does it have any of these tags?
VSARG2_REQUIRES_AB_SELECTION
, or VSARG2_REQUIRES_SELECTION
(or really anything with REQUIRE
and SELECTION
- if so try to remove them
Are there any select_active()
checks? follow those or disable them
Also are you trying to invoke it interactively or just from another slick-c code?
Many years ago I figured some of this out when I wrote narrow.e
, allowing me to emulate emacs' narrow-to-region
. I used it to base my answer (narrow.e
is posted here for the full example).