Changeset 6460a27fda60372c9a6f37ce5f654de90a0eebaf

Show
Ignore:
Timestamp:
01/06/07 23:32:16 (6 years ago)
Author:
Philipp Kern <phil@…>
Parents:
1d79f148669cfa7e22a001b0c897a60553bd3487
Children:
ca555a305ba7567b0f0270ad9a1180fb2ab928e2
git-committer:
Philipp Kern <phil@0x539.de> / 2007-01-06T22:32:16Z+0000
Message:

[project @ Code highlighting by file extension]

Original author: Armin Burgmeier <armin@…>
Date: 2005-04-22 20:52:29+00:00

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • inc/document.hpp

    r75296ef r6460a27  
    2626#include "features.hpp" 
    2727#ifdef WITH_GTKSOURCEVIEW 
     28#include "sourceview/sourcelanguagesmanager.hpp" 
    2829#include "sourceview/sourceview.hpp" 
    2930#endif 
     
    3536{ 
    3637public: 
     38        typedef std::map<Glib::ustring, Glib::ustring> MimeMap; 
     39 
    3740        Document(obby::document& doc); 
    3841        virtual ~Document(); 
     
    5457#ifdef WITH_GTKSOURCEVIEW 
    5558        Gtk::SourceView m_view; 
     59        Glib::RefPtr<Gtk::SourceLanguagesManager> m_lang_manager; 
    5660#else 
    5761        Gtk::TextView m_view; 
    5862#endif 
    5963        bool m_editing; 
     64 
     65public: 
     66#ifdef WITH_GTKSOURCEVIEW 
     67        static const MimeMap& create_mime_map(); 
     68        static const MimeMap& m_mime_map; 
     69#endif 
     70 
    6071}; 
    6172 
  • src/document.cpp

    r9f3f191 r6460a27  
    1717 */ 
    1818 
     19#include "sourceview/sourcelanguagesmanager.hpp" 
    1920#include "document.hpp" 
    2021 
     22#ifdef WITH_GTKSOURCEVIEW 
     23const Gobby::Document::MimeMap& Gobby::Document::m_mime_map = 
     24        Gobby::Document::create_mime_map(); 
     25#endif 
     26 
    2127Gobby::Document::Document(obby::document& doc) 
    22  : Gtk::ScrolledWindow(), m_doc(doc), m_editing(true) 
     28 : Gtk::ScrolledWindow(), m_doc(doc), m_editing(true), 
     29   m_lang_manager(Gtk::SourceLanguagesManager::create() ) 
    2330{ 
    2431#ifdef WITH_GTKSOURCEVIEW 
    2532        m_view.set_show_line_numbers(true); 
     33        Glib::RefPtr<Gtk::SourceBuffer> buf = m_view.get_buffer(); 
     34#else 
     35        Glib::RefPtr<Gtk::TextBuffer> buf = m_view.get_buffer(); 
    2636#endif 
    27         Glib::RefPtr<Gtk::TextBuffer> buf = m_view.get_buffer(); 
    2837 
     38        // Set monospaced font 
    2939        Pango::FontDescription desc; 
    3040        desc.set_family("monospace"); 
    3141        m_view.modify_font(desc); 
     42 
     43 
     44#ifdef WITH_GTKSOURCEVIEW 
     45        // Set source language by file extension 
     46        Glib::ustring title = doc.get_title(); 
     47        Glib::ustring::size_type pos = title.rfind('.'); 
     48        if(pos != Glib::ustring::npos) 
     49        { 
     50                Glib::ustring extension = title.substr(pos + 1); 
     51                MimeMap::const_iterator iter = m_mime_map.find(extension); 
     52                if(iter != m_mime_map.end() ) 
     53                { 
     54                        Glib::ustring mime = iter->second; 
     55                        Glib::RefPtr<Gtk::SourceLanguage> language =  
     56                                m_lang_manager->get_language_from_mime_type( 
     57                                        mime 
     58                                ); 
     59                        if(language) 
     60                        { 
     61                                buf->set_language(language); 
     62                        } 
     63                        else 
     64                        { 
     65                                g_warning("Could not find syntax file for file " 
     66                                          "extension %s (mime-type %s)", 
     67                                          extension.c_str(), mime.c_str() ); 
     68                        } 
     69                } 
     70                else 
     71                { 
     72                        g_warning("Could not detect file type of file " 
     73                                  "extension '%s'", extension.c_str() ); 
     74                } 
     75        } 
     76 
     77        buf->set_highlight(true); 
     78#endif 
    3279 
    3380        // Textbuffer signal handlers 
     
    99146} 
    100147 
     148#ifdef WITH_GTKSOURCEVIEW 
     149const Gobby::Document::MimeMap& Gobby::Document::create_mime_map() 
     150{ 
     151        static MimeMap map; 
     152 
     153        // Translates file extension to mime type 
     154        map["ada"] = "text/x-ada"; 
     155        map["c"] = "text/x-c"; 
     156        map["h"] = "text/x-c++"; 
     157        map["hh"] = "text/c-c++"; 
     158        map["cpp"] = "text/x-c++"; 
     159        map["hpp"] = "text/x-c++"; 
     160        map["cc"] = "text/x-c++"; 
     161        map["css"] = "text/css"; 
     162        map["diff"] = "text/x-diff"; 
     163        map["f"] = "text/x-fortran"; 
     164        map["f77"] = "text/x-fortran"; 
     165        // Wi geth haskell? 
     166        map["htm"] = "text/html"; 
     167        map["html"] = "text/html"; 
     168        map["xhtml"] = "text/html"; 
     169        // Wi geth IDL? 
     170        map["java"] = "text/x-java"; 
     171        map["js"] = "text/x-javascript"; 
     172        map["tex"] = "text/x-tex"; 
     173        map["latex"] = "text/x-tex"; 
     174        map["lua"] = "text/x-lua"; 
     175        // Wi geth MSIL? 
     176        map["dpr"] = "text/x-pascal"; 
     177        // TODO: Pascal hat noch mehr Krams, oder? 
     178        map["pl"] = "text/x-perl"; 
     179        map["pm"] = "text/x-perl"; 
     180        map["php"] = "text/x-php"; 
     181        map["php3"] = "text/x-php"; 
     182        map["php4"] = "text/x-php"; 
     183        map["php5"] = "text/x-php"; 
     184        map["po"] = "text/x-gettext-translation"; 
     185        map["py"] = "text/x-python"; 
     186        map["rb"] = "text/x-ruby"; 
     187        map["sql"] = "text/x-sql"; 
     188        // Wi geth texinfo? 
     189        // Wi geth vb.NET? 
     190        // Wi geth verilog? 
     191        map["xml"] = "text/xml"; 
     192 
     193        return map; 
     194} 
     195#endif