Keyword Merge Add-in for Visual Studio

Introduction

One of the nice things about Visual Studio's syntax highlighting is that you can define your own keywords. Visual Studio, both 5.0 and 6.0, support this feature. To define a new keyword, you merely need to edit the file USERTYPE.DAT (hereafter known as the DAT file) located in the same directory as MSDEV.EXE. The file is a simple text file where each keyword is on its own line. In order for Visual Studio to recognize the new keyword, you have to restart it.

The immediate benefit of this syntax highlighting is that you can tell whether you misspelled a function, or method, or whatever. Of course with the advent of Visual Studio 6.0 and Intellisense, this is not so useful. However, I found it a pain to have to go to the dat file and modify it everytime I found another keyword I wanted to add. Furthermore, sometimes I don't want all the keywords I have defined to highlight. For example, when I'm not using MFC, I don't want the MFC keywords to be highlighted.

To get around some of these problems, I wrote a simple add-in that permits you to add keywords to the DAT file from within Visual Studio. Furthermore, you can divide the keywords up into different files. Each file can represent different areas, such as MFC or Win32. Whenever you want to add or remove files from the DAT file, you merely click a button and the DAT file is rebuilt. You still have to restart Visual Studio, but most of the work is already done.

Keyword Files

Each keyword file is a simple text file containing a list of keywords. Each keyword file has a description that you provide. To add a new keyword file to the list of files that you can choose from you use the MergeKW add-in. The add-in will display a list of currently defined keyword files, and their descriptions. You can choose to add or remove keyword files. Additionally, each keyword file is either checked or unchecked depending on whether it was used the last time you merged the keyword files into a new DAT file. You can check or uncheck keyword files and then use the MERGE button to build a new USERTYPE.DAT file.

The following picture shows the dialog that you use to add and remove keyword files and to merge them into a new DAT file. Each entry in the list box consists of a descriptive name of the keyword file followed by the path to the keyword file in brackets. In the example, the Debug, MFC, and Process Status Helper keyword files are not currently being used. Therefore, any keywords contained in these files will not be highlighted by Visual Studio.

Important Note Sometimes Visual Studio locks the DAT file in memory, especially with Visual Studio 5.0. In this case, the add-in will be unable to replace the file. To get around this, the add-in will tell you that the DAT file will be replaced when Visual Studio shuts down. When you shut down Visual Studio, you will receive a message telling you the DAT file was replaced properly. In either case, you must restart Visual Studio before the changes will take effect. It would be nice if Visual Studio could be forced to automatically update itself when the DAT file changed.

Adding Keywords

One of the problems of using custom keywords is having to manually add the keyword to a file. To get around this, the add-in adds another command, 'Add Keyword' (ADD). ADD will take the currently selected text from the active editor window and add it to one of the keyword files. You will be prompted for the keyword file to use. It will also show you what text will be added, so you can ensure you aren't making a mistake. This is far faster than manually adding it to a file. Furthermore, you can continually add new words to a keyword file and then whenever your ready, rebuild the DAT file. It is harmless to add a keyword more than once, or even adding the same keyword to multiple files.

The following picture shows the dialog that appears when you select a word and then choose the ADD command on the toolbar. In this example, the keyword WIN32_FIND_DATA is going to be added to the keyword file Win32.txt.

Changes in Version 2.0.4

The following features are new to this version:

The add-in now remembers the last directory used when adding a keyword to a TXT file, or adding a new TXT file.

Auto-merge has been added. By enabling this option in the add-in dialog, the add-in will automatically merge the files selected in the dialog whenever MSDev shuts down. This saves you the effort of having to explicitly merge any changes you have made.

Create TXT files when needed. You no longer have to create the TXT files before being able to use them in the add-in. The Open File/Save File dialogs will prompt you to create any file that doesn't exist.

Installation

To install the add-in, do the following :
1. Copy MergeKW.dll to any directory you like. If you insert the DLL into the Addins directory (under the directory where MSDEV.EXE resides, then Visual Studio will automatically load it the next time it starts.
2. Start Visual Studio and go to the 'Tools/Customize' menu item. Click on the menu. The Customize dialog will appear.
3. In the Customize dialog, go to the 'Add-ins and Macro Files' tab. If you placed the DLL in the Addins directory, then it will show up here, otherwise use the 'Browse' button to locate the DLL.
4. Once the DLL is registered in Visual Studio, click the add-in from within the list to show its buttons.
5. Installation is complete. You can now use the add-ins toolbar buttons.

Conclusion

I hope you find this add-in useful. If you have any suggestions or comments then please let me know. To assist you in getting started, I have provided in the Data directory of the ZIP file a copy of the keyword files that I have defined. You can use these to get started.

Downloads

The zipped file contains all the source code and several example keyword files. The add-in works under UNICODE. It will compile under ANSI, but I have not done any extensive tests to ensure it works.
Download source - 61 KB


Comments

  • Why VS ignores KW in lines begining with @ ??? (0)

    Posted by Legacy on 03/26/2002 12:00am

    Originally posted by: Roman Mukhin

    (0)

    Reply
  • Another way to speed up adding keywords

    Posted by Legacy on 01/04/2001 12:00am

    Originally posted by: rolf draether

    this addin is great! THX! but if U have to add many
    
    keywords to the same file much time is wasted selecting
    every time the filename in the FileOpen-dialog. so I
    modified the CCommands::AddKWCommandMethod. now it
    remembers not only the last used directory but the last
    used filename too. that makes adding keywords much faster.

    that are the modifications I made in the file Commands.cpp:

    ...

    //Cache for the last directory used to save the keywords
    TCHAR g_lpszLastDir[MAX_PATH] = TEXT("");

    //---> here I declared and initialised a new global value

    //Cache for the last file used to save the keywords
    TCHAR g_lpszLastFile[MAX_PATH] = TEXT("");

    ...

    ---> and in the method CCommands::AddKWCommandMethod() I changed the following:

    ...

    //If the selected text is empty, then done
    if (!Sel.IsEmpty())
    {
    //Display a browse dialog so the user can pick the TXT file
    //to add it to
    CString szExt, szFilter, szTitle;
    DWORD dwFlags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
    OFN_HIDEREADONLY | OFN_NOREADONLYRETURN |
    OFN_CREATEPROMPT;

    szExt.LoadString(IDS_DEFAULTEXT);
    szFilter.LoadString(IDS_EXTFILTER);
    CFileDialog FileDlg(TRUE, szExt, NULL, dwFlags, szFilter, NULL);
    szTitle.Format(IDS_FMTFILE_SELECT, Sel);
    FileDlg.m_ofn.lpstrTitle = szTitle;
    FileDlg.m_ofn.lpstrInitialDir = g_lpszLastDir;

    //---> here I set last used filename as initial value --->
    FileDlg.m_ofn.lpstrFile = g_lpszLastFile;

    if (FileDlg.DoModal() == IDOK)
    {
    CString szDir = FileDlg.GetPathName();
    AppendToFile(szDir, Sel);
    //Save the directory
    CString szFile = FileDlg.GetFileName();

    //---> here I save the last used filename
    lstrcpy(g_lpszLastFile, szFile);

    int iPos = szDir.Find(szFile);
    if (iPos != -1)
    szDir = szDir.Left(iPos - 1);
    lstrcpy(g_lpszLastDir, szDir);
    };
    }; //Sel !Empty

    ...


    That's all. now U must only select a file, if U restarted
    DevStudio or U want save the keyword to another file. :-)


    greetings
    rolf:o)


    Reply
  • color custom blocks like comments?

    Posted by Legacy on 10/02/2000 12:00am

    Originally posted by: Brad Pirtle

    We have our own C++ codfe generator that uses comment blocks to mark where code should be inserted.
    
    Like:
    MyObject::MyObject()(
    {

    //<<GENERATED_ATTRIBUTE_INITIALIZATION
    // Dont hand modify this code - it is generated
    for (int i = 0; i < 10; i++)
    {
    // Initialize the generated object model code
    }
    //>>GENERATED_ATTRIBUTE_INITIALIZATION

    }

    Is there a way to define a color block that starts with
    //<<
    and end with
    //>>
    (just like a comment /* and */ does)??
    This would keep my from accidentally manualy changing this code.
    Thanks for any advice!!
    Brad

    Reply
  • Keyword Merge Add In

    Posted by Legacy on 11/20/1999 12:00am

    Originally posted by: Jud Valeski

    This thing is great! Thanks for distributing it. Is there anyway to control the color used for highlighting? I noticed the keyword file format is pretty generic and it doesn't look like this is possible, but I thought I'd ask.

    Thanks again,
    Jud Valeski

    Reply
  • Quick & Dirty speed up to this great tool

    Posted by Legacy on 11/03/1999 12:00am

    Originally posted by: Tameem Antoniades

    I wanted to press a hotkey and have the word under the cursor added to a prespecified file.
    
    

    No selecting involved, no file dialogs. Here's the petty macro I used:

    Sub SelectAddtoMayaFncs()
    'DESCRIPTION: selects the word under cursor and adds it to Maya_fncs.txt

    ExecuteCommand "WordSelect"
    ExecuteCommand "AddKWCommand"

    ActiveDocument.Selection.Cancel
    ActiveDocument.Selection.Cancel
    End Sub


    Note that it makes use of the WordSelect macro by Brian Sturk (http://codeguru.developer.com/devstudio_macros/wordselect.shtml)

    To circumvent the dialog box I just changed the CCommands::AddKWCommandMethod() in Commands.cpp to read:


    CCommands::AddKWCommandMethod()
    if (!Sel.IsEmpty())
    {
    //Display a browse dialog so the user can pick the TXT file
    //to add it to
    // CString szExt, szFilter, szTitle;
    // DWORD dwFlags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
    // OFN_HIDEREADONLY | OFN_NOREADONLYRETURN |
    // OFN_CREATEPROMPT;
    //
    // szExt.LoadString(IDS_DEFAULTEXT);
    // szFilter.LoadString(IDS_EXTFILTER);
    // CFileDialog FileDlg(TRUE, szExt, NULL, dwFlags, szFilter, NULL);
    // szTitle.Format(IDS_FMTFILE_SELECT, Sel);
    // FileDlg.m_ofn.lpstrTitle = szTitle;
    // FileDlg.m_ofn.lpstrInitialDir = g_lpszLastDir;

    // if (FileDlg.DoModal() == IDOK)
    // {
    // CString szDir = FileDlg.GetPathName();
    AppendToFile("d:\\Program Files\\Microsoft Visual Studio\\Common\\MSDev98\\Bin\\Maya_fncs.txt", Sel);

    //Save the directory
    // CString szFile = FileDlg.GetFileName();
    // int iPos = szDir.Find(szFile);
    // if (iPos != -1)
    // szDir = szDir.Left(iPos - 1);
    // lstrcpy(g_lpszLastDir, szDir);
    // };
    }; //Sel !Empty


    It's not brilliant but I don't care - it does what I want it to do. Cheerio!

    Tameem

    Reply
  • usertypes.dat -- defined types not showing up as highlighted

    Posted by Legacy on 05/04/1999 12:00am

    Originally posted by: Mason Deming

    Using VC6.0, I've got a usertypes.dat file in my msdev98/bin folder, it's got one word per line - in fact i used one of the text files from the source to get a bunch of the common datatypes. I'm pretty confident i put things in their proper places.

    I've also gone into the Tools->Options->Format tab and made sure that User defined types have a highlight color, as well as keywords. I've restarted msdev, i've rebooted since then, and the keywords are still normal text color.

    Are there any other settings i should change? Is there a flag somewhere controlling whether or not to use the usertypes.dat file? Before reading your article, I had no desire for user defined hilight strings ... Now i really really want them!

    Thanks in advance,

    ~ Mason

    Reply
  • Can't find USERTYPES.DAT

    Posted by Legacy on 03/06/1999 12:00am

    Originally posted by: Carl Letourneau

    I can't find USERTYPES.DAT.... using VC++ 5.0 on a computer, and 6.0 on another one...

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds