From: Leon W. <moo...@us...> - 2005-04-14 12:01:55
|
Update of /cvsroot/anyedit/AnyEditv2 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25364 Modified Files: AnyEditDoc.cpp AnyEditDoc.h FileTypeManager.cpp FileTypeManager.h Log Message: Added functionality to determine the language of a document by it's first line, if the extension isn't recognized. On startup a list of regular expressions is read from the config file (StartExrpressions). If one of these regex's matches, the language it belonged to is set on the document. Index: FileTypeManager.h =================================================================== RCS file: /cvsroot/anyedit/AnyEditv2/FileTypeManager.h,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** FileTypeManager.h 11 Apr 2005 06:56:12 -0000 1.10 --- FileTypeManager.h 14 Apr 2005 12:01:43 -0000 1.11 *************** *** 103,106 **** --- 103,109 ---- int GetLanguageNrFromExtension( CString szExtension ); + /// Get the LanguageNr of line. It is checked against StartExpressions. + int GetLanguageNrFromLine( CString szLine ); + /// Set properties for a given langugage to a scintilla control. void SetScintillaProperties(int iLanguage, CScintillaEx* pScintilla, BOOL bSyntaxHighlighting); *************** *** 113,116 **** --- 116,125 ---- void FillLanguageNames(CConfigFile *pConfigFile); + /// Fill start expressions of languages + void FillLanguageStartExpressions( CConfigFile* pConfigFile ); + + /// Map of the Start Expressions + CMapStringToString m_mapStartExpressions; + /// Array of language filters CStringArray m_arrFilters; Index: FileTypeManager.cpp =================================================================== RCS file: /cvsroot/anyedit/AnyEditv2/FileTypeManager.cpp,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** FileTypeManager.cpp 11 Apr 2005 06:56:12 -0000 1.28 --- FileTypeManager.cpp 14 Apr 2005 12:01:42 -0000 1.29 *************** *** 33,36 **** --- 33,37 ---- #include "FileTypeManager.h" #include "AnyEditDoc.h" + #include "Regex.h" #ifdef _DEBUG *************** *** 233,236 **** --- 234,258 ---- } + /// Get the LanguageNr of line. It is checked against StartExpressions. + int CFileTypeManager::GetLanguageNrFromLine( CString szLine ) + { + CRegex rx; + POSITION pos; + CString szExpression; + CString szLanguageNr; + pos = m_mapStartExpressions.GetStartPosition(); + while( pos != NULL ) + { + m_mapStartExpressions.GetNextAssoc( pos, szExpression, szLanguageNr ); + + rx.compile( szExpression ); + if( !rx.compiled() ) continue; + + if( rx.match( szLine ) ) return atoi( szLanguageNr ); + } + + return 0; + } + /// Find the SyntaxFile object by it's language number CSyntaxFile* CFileTypeManager::GetSyntaxFile( int iLanguage ) *************** *** 246,249 **** --- 268,272 ---- FillExtensionMap(conf); FillLanguageNames(conf); + FillLanguageStartExpressions(conf); LoadSyntaxFiles(conf); *************** *** 553,556 **** --- 576,590 ---- } + void CFileTypeManager::FillLanguageStartExpressions( CConfigFile* pConfigFile ) + { + int nCountLanguages = pConfigFile->GetLanguageCount(); + m_mapStartExpressions.RemoveAll(); + while( nCountLanguages > 0 ) + { + -- nCountLanguages; + pConfigFile->GetLanguageStartExpressions( nCountLanguages, &m_mapStartExpressions ); + } + } + /// Create filter string for file open dialog. CString CFileTypeManager::GetFileDialogFilter() Index: AnyEditDoc.h =================================================================== RCS file: /cvsroot/anyedit/AnyEditv2/AnyEditDoc.h,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** AnyEditDoc.h 3 Dec 2004 06:46:12 -0000 1.26 --- AnyEditDoc.h 14 Apr 2005 12:01:42 -0000 1.27 *************** *** 102,105 **** --- 102,106 ---- void CleanProjectFile(); void UpdateProjectFile(); + int DetermineLanguageNumber(); // Generated message map functions Index: AnyEditDoc.cpp =================================================================== RCS file: /cvsroot/anyedit/AnyEditv2/AnyEditDoc.cpp,v retrieving revision 1.52 retrieving revision 1.53 diff -C2 -d -r1.52 -r1.53 *** AnyEditDoc.cpp 11 Apr 2005 06:34:48 -0000 1.52 --- AnyEditDoc.cpp 14 Apr 2005 12:01:42 -0000 1.53 *************** *** 222,225 **** --- 222,257 ---- } + int CAnyEditDoc::DetermineLanguageNumber() + { + CString szTemp; + int iIndex; + int iLanguage = 0; + char* text = NULL; + + szTemp = GetPathName(); + iIndex = szTemp.ReverseFind( '.' ); + if( iIndex >= 0 ) + { + szTemp = szTemp.Mid( iIndex + 1 ); + szTemp.MakeUpper(); + iLanguage = theApp.GetFileTypeManager()->GetLanguageNrFromExtension( szTemp ); + } + if( iLanguage == 0 ) + { + CAnyEditView* pFirstView = GetFirstEditView(); + if( pFirstView ) + { + text = (char*)realloc( text, pFirstView->GetScintillaControl()->GetLineEndPosition(0) + 3 ); + if( NULL == text ) return 0; + iIndex = pFirstView->GetScintillaControl()->GetLine(0, text); + text[iIndex] = '\0'; + iLanguage = theApp.GetFileTypeManager()->GetLanguageNrFromLine( text ); + } + } + if( text ) free( text ); + + return iLanguage; + } + /*********************************************************************\ ** *************** *** 237,246 **** RemoveTempCopy(TRUE); - // Set the language number from the extension - CMisc misc; - m_iLanguage = theApp.GetFileTypeManager()->GetLanguageNrFromExtension( misc.GetFileExtension( lpszPathName ) ); - m_bBraceMatching = theApp.GetFileTypeManager()->GetBraceMatching(m_iLanguage); - m_bSyntaxHighlighting = theApp.GetFileTypeManager()->GetSyntaxHighlighting(m_iLanguage); - CConfigFile* pConfigFile = theApp.GetConfigFile(); if( NULL == pConfigFile ) --- 269,272 ---- *************** *** 253,259 **** } ! // Use Scintilla to open the file. CAnyEditView* pFirstView = GetFirstEditView(); ! if (!pFirstView->GetScintillaControl()->OpenFile(lpszPathName)) return FALSE; --- 279,285 ---- } ! // Use Scintilla to open the file. CAnyEditView* pFirstView = GetFirstEditView(); ! if (!pFirstView->GetScintillaControl()->OpenFile(lpszPathName)) return FALSE; *************** *** 268,272 **** UpdateProjectFile(); ! return TRUE; } --- 294,303 ---- UpdateProjectFile(); ! // Set the language number from the extension ! m_iLanguage = DetermineLanguageNumber(); ! m_bBraceMatching = theApp.GetFileTypeManager()->GetBraceMatching(m_iLanguage); ! m_bSyntaxHighlighting = theApp.GetFileTypeManager()->GetSyntaxHighlighting(m_iLanguage); ! ! return TRUE; } *************** *** 299,307 **** return FALSE; // Check if we need to update the scintilla properties // We only do this if we have non-default value for the iTempLanguage, // because we don't want to switch Default when we did a manual override. ! CMisc misc; ! int iTempLanguage = theApp.GetFileTypeManager()->GetLanguageNrFromExtension(misc.GetFileExtension(lpszPathName)); if(iTempLanguage != 0 && m_iLanguage != iTempLanguage) { --- 330,344 ---- return FALSE; + SetPathName(lpszPathName); + DWORD dwFileAttributes = ::GetFileAttributes(lpszPathName); + m_bFileReadOnly = (dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0; + pView->GetScintillaControl()->SetReadOnly(m_bFileReadOnly); + UpdateProjectFile(); + SetLastAccessTime(); + // Check if we need to update the scintilla properties // We only do this if we have non-default value for the iTempLanguage, // because we don't want to switch Default when we did a manual override. ! int iTempLanguage = DetermineLanguageNumber(); if(iTempLanguage != 0 && m_iLanguage != iTempLanguage) { *************** *** 313,323 **** } - SetPathName(lpszPathName); - DWORD dwFileAttributes = ::GetFileAttributes(lpszPathName); - m_bFileReadOnly = (dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0; - pView->GetScintillaControl()->SetReadOnly(m_bFileReadOnly); - UpdateProjectFile(); - SetLastAccessTime(); - return TRUE; } --- 350,353 ---- |