Ang: GPS Hook question

Per Sandberg <[email protected]> Sun, 2 Mar 2008 07:54:49 +0100 (CET)
Newsgroups gmane.comp.ide.gps.user
Message-ID <29741449.344171204440889423.JavaMail.defaultUser@defaultHost>
Got a small sample that does that, hoever I dont know how to do 
a "gnat pretty" on save in a safe way.
Have a look in the attachment.
/Per

>----Ursprungligt meddelande----
>Från: [email protected]
>Datum: 29-02-2008 14:24
>Till: <[email protected]>
>Ärende: [gps-users]  GPS Hook question
>
>
>I've never created a hook for GPS.  I've read the docs but 
I'm still not sure
>how to proceed.
>
>Here's what I want to do:
>I want the pretty printer to run, using whatever the project 
defaults are,
>whenever I save an Ada file.  In other words, I always want 
Ada files
>pretty-printed as part of the save operation.  I'm guessing I 
need to add
>something to the file_saved Hook, but I need some help.
>
>Any Hook Masters want to, ehh, hook me up with a solution?
>Thanks!
>
>-- 
>View this message in context: http://www.nabble.com/GPS-Hook-
question-tp15744424p15744424.html
>Sent from the GNAT Programming Studio - User mailing list 
archive at Nabble.com.
>
>_______________________________________________
>gps-users mailing list
>[email protected]
>http://lists.adacore.com/mailman/listinfo/gps-users
>

_______________________________________________
gps-users mailing list
[email protected]
http://lists.adacore.com/mailman/listinfo/gps-users
PrettyPrintOnSave.py (text/plain, 1.3 KB)
"""
 This macro reformats a source file on save with the
 selected engine.
"""
import GPS
def reformat(hook,f):
    try:
        engine=GPS.Preference("format-on-save").get()
    except:
        GPS.Preference("format-on-save").set("GPS-Format")
        engine=GPS.Preference("format-on-save").get()
    if engine="None":
        pass   
    elif engine=="Gps-format":
        v=GPS.EditorBuffer.get().current_view()
        c=v.cursor()
        GPS.EditorBuffer.get(f).select()
        GPS.execute_action (action="Format Selection")
        v.goto(c)
    elif engine=="Gnatpp":
        # Think about:
        #  What if several files are edited.
        #  Dont think it is possible to do in a safe way.
        print "To be implemented"
    else:
        print "Unknown engine:" + engine

        
def initialize():
    GPS.parse_xml("""
   <preference name="format-on-save"
                    page="Editor"
                    tip ="Reformats the sourcefile before save"
                    label="Format on save"
                    default="1"
                    type="choices" >
          <choice>None</choice>
          <choice>GPS-Format</choice>  <!--  The default choice -->
<!--          <choice>gnatpp</choice> --> 
        </preference>""")
    GPS.Hook ("before_file_saved").add(reformat)
initialize()