New context menu shell extension to rename Delphi / Pascal units.

Published 21 December 10 1:9 PM | Phil Gilmore

nametag212/21/2010

Phil Gilmore

Renaming a Pascal file isn’t as simple as pressing F2 in Windows Explorer and giving it a new name.  The name of the unit inside the file’s content must match the file’s name or the compiler can’t find it when you build your projects.

Rather than bring up Delphi to rename the file for you (which can take a long time), you could associate .pas, .dpr and .dpk files with a smaller editor like Notepad++, or just drag the file from Windows Explorer into notepad to change the file.  But all of this leaves an acre of space for human error.  Typos, confusion while keeping track of which file you’re editing, multiple copies, etc. can all cause problems of varying sizes, with all of which I am fed up. 

Therefore, I have written a Windows Explorer context menu shell extension to do this for you.  Just right-click on a file, select Rename Pascal Unit from the menu, enter a new name and it will handle the whole thing for you.

 

Instructions

Right-click on any file with a .dpr, .dpk or .pas file extension.  Click the menu item with the caption “Rename Pascal Unit”.

RenamePascalunit_Menu

 

Once invoked, it will prompt you for a new filename for the unit.

 

RenamePascalunit_NewName .

 

 

The file will be immediately renamed.  Next, it will parse the Pascal content to find the Program, Package, Library or Unit token in the source.  Then it compares it to the filename.  If they match, the content will be updated to match the new filename.  If they do not match, you will be prompted before proceeding.

 

RenamePascalunit_ConflictDialog

 

 

 

This is most likely to occur when working with a file which was improperly renamed in the past.  The old unit name will be inside the file but after it was renamed, the current filename doesn’t match.  Now that we’re renaming, we see that this has occurred. 

However, rather than assume that this is the case, you will be shown enough information to make the decision to proceed or abort.  You will be able to see if the token found is what is actually designated at the right place in the file, or if the parser has failed due to errors in the unit source or due to bugs in the parser. 

I have also tried it on non-Pascal files which could not be parsed by a Pascal parser to make sure it behaves.  If you try this, you will be informed.

 

RenamePascalunit_ParseError

 

If there is a parsing error or if there is a conflict and the user opts to cancel the operation then the file rename operation will be rolled back and the file will retain its original filename. 

 

Known issues

Parsing packages

There is a known bug in the parser.  Package unit names are not definitively found.  If the package heading is found and the next token matches the filename, it is probably safe to assume that the correct token was found and the operation will be successful.  If they do not match, pay particular attention to the details in the conflict dialog when renaming a package.  If the “found unit name” does not look right, it may be best to abort and fix the content by hand for that unit.  All of my tests, however, have been successful.

Broken units

Due to the nature of broken code, it is, by definition, impossible to say what will happen to a parser when trying to parser broken code.  An infinite number of ways to break code yields an uncertain number of ways to confuse a parser.  If you try to rename a file which cannot be compiled, the results are uncertain.  It will probably work.  If it can’t work, it will probably give you a parsing error.  I recommend having a backup of your files when using this program unless you decide for yourself that it is stable enough.

Limited rollback

If an unexpected exception occurs during the file rename operation, it will be trapped (keeping Windows Explorer intact) and the rename operation will not roll back.  In this instance, you are left with a renamed file whose unit name in the content is probably not updated to match.  This is a last-resort behavior only for unexpected errors, so no recovery is available in this situation.  When this happens, the exception class and message will be displayed.

32-bit only

Context menu shell extensions are plugins for Windows Explorer.  This means that if Windows Explorer is 32-bit, its plugins must also be 32-bit to run in the same process space.  If Windows Explorer is 64-bit, it must use 64-bit plugins in its process space.  Because Delphi is a 32-bit compiler only, I cannot build a 64-bit version of this library.  Therefore, this library only works in 32-bit versions of Windows for now.

 

Obtaining it

You can download the binaries from interactiveasp.net in the media / utils section.

http://interactiveasp.net/media/p/6702.aspx

 

Installing it

To install the shell extension, you need to register it as an an ActiveX library.  It is a self-registering library and will install itself upon registration.  Instructions for installing from the command prompt are included in the download.  Here are alternative instructions, with screen shots for the command-prompt impaired.

 

Copy the DLL to your C:\windows\system32\shellext folder.  If the folder doesn’t exist, you can put it anywhere, but don’t forget where you put it.

Open the run box (Start –> Run) or an elevated  command prompt and issue this command:

regsvr32 c:\windows\system32\shellext\RenameUnitMenu.dll

 

Here is a screen shot of the run box method:

RenamePascalunit_RunboxRegistration

 

Here is a screen shot of the command prompt method:

RenamePascalunit_CommandPromptRegistration

 

If successful, you should see a popup window like the one below.

RenamePascalunit_RegisterSucceeded

 

 

 

To uninstall it, repeat the above steps and add a “/u” (without the quotes) to the end of the commmand.

regsvr32 c:\windows\system32\shellext\RenameUnitMenu.dll /u

In response, you should see a popup like the one below.

 

RenamePascalunit_UnregisterSucceeded