Is there a way to automatically remove the command around an argument?

Miguel
2015-01-28
2016-11-16
  • Miguel

    Miguel - 2015-01-28

    Imagine that I have the following text:
    This is a \st{very nice}\MG{not so great} sample text.

    Is there a way that having the cursor in any place of the string "\st{very nice}" or "\MG{not so great}" a macro could convert them to "" (deletes everything) or "not so great" (accepts the change)?

     
  • Benito van der Zander

    You can use indexOf/lastIndexOf to find the next {, } and \, replace everything between with selectColumns/replaceSelectedText:

    %SCRIPT
    
    var line = editor.text(cursor.lineNumber());
    var start = line.lastIndexOf("{", cursor.columnNumber());
    var startCmd = line.lastIndexOf("\\", start);
    var end = line.indexOf("}", cursor.columnNumber());
    
    cursor.selectColumns(startCmd, end+1);
    cursor.replaceSelectedText(line.substring(start+1, end))
    
     
  • Miguel

    Miguel - 2016-11-16

    Great! I tweeked the code a bit to:

    • be able to have nested commands.
    • be able to place the cursor inside nested commands.
    • behave differently depending on the command found.

    Basically it searches for a command that looks like \MG or \st and finds its matching }. Then it "accepts the change", i.e. it removes the content if the command was \st and it keeps the content if the command was \MG.

    Now I'm only missing spanning multiple lines and giving a warning if it fails.

    %SCRIPT
    
    var maxct;
    var ILN = cursor.lineNumber();
    var line = editor.text(ILN);
    
    //Find command and correct start
    maxct=50;
    var start = line.lastIndexOf("{", cursor.columnNumber());
    var startCmd = line.lastIndexOf("\\", start);
    var CmdName;
    var notfoundCmd = true; 
    while (maxct>0 && notfoundCmd) {
        maxct--;
        CmdName = line.substring(startCmd+1, start);
        if ((CmdName=="MG") || (CmdName=="st")) {
            notfoundCmd = false;
        }
        else {
            start = line.lastIndexOf("{", start-1);
            if (startCmd>start)
                startCmd = line.lastIndexOf("\\", start);
        }
    }
    
    //Find matching close parenthesis
    maxct=50;
    var cpos=start+1;
    var ctO=1;
    var ctC=0;
    var fndO;
    var fndC;
    while (maxct>0 && ctO>ctC) {
        maxct--;
        fndO=line.indexOf("{", cpos);
        if (fndO==-1)
            fndO=line.length+1;
        fndC=line.indexOf("}", cpos);
        if (fndC==-1)
            fndC=line.length+1;
    
        if (fndO<fndC) {
            cpos=fndO+1;
            ctO++;
        }
        else {
            cpos=fndC+1;
            ctC++;
        }
    }
    var end = cpos-1;
    
    //Replace Text with correct one
    if (CmdName=="st")
        Text="";
    else
        Text=line.substring(start+1, end);
    cursor.selectColumns(startCmd, end+1);
    cursor.replaceSelectedText(Text);
    
     
    Last edit: Miguel 2016-11-16

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks