bashgeditexternal-tools

Can I write some output to the current document and some to shell output?


I'm using gedit and trying to wrestle it into a real IDE. What I want is to map ctrlshift| to run my tidy tool when the file type is "text/html" and my autopep8 tool when the file type is "text/x-python".

As it turns out (and I think this is a bug), gedit doesn't care what filetype you've specified. If you have a key combo set, it will run a tool whether or not the filetype matches. Related, but maybe not a bug, I can only set the keyboard shortcut to one external tool.

So I wrote one external tool that runs on ctrlshift| and runs autopep8 if the document is Python and tidy if the document is HTML:

#!/bin/sh
# [Gedit Tool]
# Save-files=document
# Shortcut=<Primary><Shift>bar
# Output=replace-document
# Name=Tidy by Filetype
# Applicability=all
# Input=document

if [ $GEDIT_CURRENT_DOCUMENT_TYPE = "text/x-python" ]; then
    autopep8 - -v -a
elif [ $GEDIT_CURRENT_DOCUMENT_TYPE = "text/html" ]; then 
    #-i auto indents, -w 80 wrap at 80 chars, -c replace font tags w/CSS
    exec tidy -utf8 -i -w 80 -c "$GEDIT_CURRENT_DOCUMENT_NAME"
elif [ $GEDIT_CURRENT_DOCUMENT_TYPE = "text/css" ]; then
    #TK CSS tidy
else 
    echo "This seems to be " $GEDIT_CURRENT_DOCUMENT_TYPE " I don't know how to tidy that."
fi

That second to last line is the one that is breaking my heart. If I don't define any action for that last else it just deletes my existing document. If I run ctrlshift| and the file-type isn't one that I've accounted for, I'd like it to report the file type to the shell output, not replace the document contents with

This seems to be application/x-shellscript I don't know how to tidy that.

Is there a way to write my tool so that I write some output to the shell and some to the document?


Solution

  • Having no experience with trying to make gedit more usable, I did find this: https://wiki.gnome.org/Apps/Gedit/Plugins/ExternalTools?action=AttachFile&do=view&target=external-tools-manager-with-gedit-3.png

    The key construct here is

    echo "..." > /dev/stderr
    

    At least that should stop you from clobbering the contents of your file if it the mime-type doesn't match, but I'm not sure where it'll print out (hopefully somewhere sane).

    Edit (for a more complete answer):

    You will also need to cat $GEDIT_CURRENT_DOCUMENT_NAME to replace the file contents with itself. This was pointed out by @markku-k (thanks!)