Search code examples
pluginsgedit

Use commands onto the Manage Snippets plugin from Gedit


I have gedit and the Manage Snippets plugin and I would like to create a snippets to comment a line (it's easy, just add : "# $GEDIT_CURRENT_LINE", for python code for example) but, if the line is ever commented, I would like to uncomment it. Is there a special syntax to use, I don't know, python or c++ statements like an if with a condition on $GEDIT_CURRENT_LINE ? Because any of code write on the snippets will be only print.


Solution

  • Ok, actually, I just find how a way to do it. To use bash commands onto the manage snippets plugin, just use `. For example, in my case, to know what is the first caracter on the line : `${GEDIT_CURRENT_LINE:0:1}` and, with the if statement :

    ``if [ ${GEDIT_CURRENT_LINE:0:1} == "#" ]; then  echo ${GEDIT_CURRENT_LINE:2}; else GEDIT_CURRENT_LINE_STR=$GEDIT_CURRENT_LINE; echo "# $GEDIT_CURRENT_LINE_STR"; fi``
    

    this snippets will comment the line or uncomment it if it is ever commented. (Just as Notepad++ do it).

    Edit : you have to create a new variable with the line to rewrite it : GEDIT_CURRENT_LINE_STR=$GEDIT_CURRENT_LINE;. Without that new variable, the current line text can be interpreted as a command and then, just erase the line because of an error. (I updated the code above)

    Edit2 : here a snippets to comment/uncomment all the selected lines with python (and not just on as before) :

    $<
    AllLines = $GEDIT_SELECTED_TEXT.split('\n')
    if AllLines == ['']:
        AllLines = $GEDIT_CURRENT_LINE.split('\n')
    NewLines = []
    for line in AllLines:
        if line[0:2] == '# ':
            NewLines += [line.replace('# ', '')]
        elif line[0] == '#':
            NewLines += [line.replace('#', '')]
        elif line[0] != '#':
            NewLines += ['# ' + line]
    return '\n'.join(NewLines)
    >