回 帖 发 新 帖 刷新版面

主题:[原创]吐血奉献:CVF注释多行的宏命令

不止一位问道可不可以在CVF中为多行代码加注释。
CVF本身无这样的功能,但是可以用macro功能自己添!而且是添加工具钮!
俺初步试验成功,虽然在容错性方面还可以改进,但已经基本够用,特奉献给大家。

[color=FF0000]做法:[/color]

(1) 在..\Microsoft Visual Studio\Common\MSDEV98\MACROS文件夹下生成文件GrpComment.dsm
(2) 用文本编辑器打开该文件,将以下所附的代码贴在其中,保存(注意保留.dsm后缀)
(3) 启动CVF,选Tools=>Customize=>Add-ins and Macro Files
(4) 在GrpComment前打勾,去掉其他的勾
(5) 在同一对话框中选Commands=>Macros,此时在右边可以看见CommentDel和CommentOut
(6) 选中CommentOut,拖到CVF的工具栏上去(添加工具钮),会弹出Button Appearance对话框
(7) 选Image and text,在下边Button text框中输入名称(默认是CommentOut),如“加注释”
(8) 类似的方法再将CommentDel命令以工具钮的形式添加到工具栏上,名称可取为“去注释”

这时,工具栏上应该多了两个工具钮:“加注释”和“去注释”。

[color=FF0000]用法:[/color]

加注释:选择要加注释的多行代码,点击“加注释”按钮即可;
去注释:选择已经注释的多行代码,点击“去注释”按钮即可。

[color=FF0000]适用:[/color]后缀为f90或f77的代码文件。

[color=0000FF]Enjoy!!![/color]


[color=FF0000]VBscript代码:[/color]

Function FileType (ByVal doc)
    ext = doc.Name
    FileType = 0
    pos = Instr(ext, ".")
    if pos > 0 then
        Do While pos <> 1
            ext = Mid(ext, pos, Len(ext) - pos + 1)
            pos = Instr(ext, ".")
        Loop
        ext = LCase(ext)
    end if
    If ext = ".f90" Then
        FileType = 8
    ElseIf ext = ".for" Then
        FileType = 9
    Else
        FileType = 0
    End If
End Function


Sub CommentOut ()
'DESCRIPTION: 为所选的多行代码加注释
    Dim win
    set win = ActiveWindow
    if win.type <> "Text" Then
      MsgBox "This macro can only be run when a text editor window is active."
    else
        TypeOfFile = FileType(ActiveDocument)  
        If TypeOfFile = 8 Or TypeOfFile = 9 Then

            If TypeOfFile = 8 Then
                CommentType = "! "  ' Fortran 90 file
            Else
                CommentType = "C "  ' Fortran 77 file
            End If
     
            StartLine = ActiveDocument.Selection.TopLine
            EndLine = ActiveDocument.Selection.BottomLine
            If EndLine < StartLine Then
                Temp = StartLine
                StartLine = EndLine
                EndLine = Temp
            End If

            If EndLine = StartLine Then
                ActiveDocument.Selection.SelectLine
                ActiveDocument.Selection = CommentType + ActiveDocument.Selection

            Else
                For i = StartLine To EndLine
                    ActiveDocument.Selection.GoToLine i
                    ActiveDocument.Selection.SelectLine
                    ActiveDocument.Selection = CommentType + _
                        ActiveDocument.Selection
                Next
            End If
        else
            MsgBox("Unable to comment out the highlighted text" + vbLf + _
                    "because the file type was unrecognized." + vbLf + _
                    "If the file has not yet been saved, " + vbLf + _
                    "please save it and try again.")
        End If
    End If
End Sub


Sub CommentDel ()
'DESCRIPTION: 去除所选的多行代码的注释
    Dim win
    set win = ActiveWindow
    if win.type <> "Text" Then
        MsgBox "This macro can only be run when a text editor window is active."
    else
        TypeOfFile = FileType(ActiveDocument)  
        If TypeOfFile = 8 Or TypeOfFile = 9 Then

            StartLine = ActiveDocument.Selection.TopLine
            EndLine = ActiveDocument.Selection.BottomLine
            If EndLine < StartLine Then
                Temp = StartLine
                StartLine = EndLine
                EndLine = Temp
            End If

            If EndLine = StartLine Then
                ActiveDocument.Selection.SelectLine
                ActiveDocument.Selection = mid(ActiveDocument.Selection, 3)
            Else
                For i = StartLine To EndLine
                    ActiveDocument.Selection.GoToLine i
                    ActiveDocument.Selection.SelectLine
                    ActiveDocument.Selection = mid(ActiveDocument.Selection, 3)
                Next
            End If
        else
            MsgBox("Unable to comment out the highlighted text" + vbLf + _
                    "because the file type was unrecognized." + vbLf + _
                    "If the file has not yet been saved, " + vbLf + _
                    "please save it and try again.")
        End If
    End If
End Sub

回复列表 (共45个回复)

沙发

很好用,谢谢mltx老师!请问这是您的原创么?

板凳

谢谢老师的无私!

3 楼

CVF原有一个Macro样例文件(sample.dsm),里边有CommentOut函数,可惜只能对C,html,VBscript等语言进行多行注释,唯独没有fortran语言,而且没有去除多行注释的功能。

该样例给了我启发。我参考了该样例,编写了上述代码,可对f90和f77文件操作,其中特别编写了CommontDel子过程。

然后,还是觉得不够方便,就琢磨出添加按钮的办法(Studio有这个功能)。

这种技术,就是窗户纸,一捅就破。只要熟悉VBScript,谁都可以做。

确实很好用,我现在也用了。:)

4 楼

真是牛人啊!佩服sincerely!
那个fast fortran toolbar实现也就主要是这个功能!
[em12]

5 楼

好东西!呵呵!以前我是一排一排的加,傻的啦!
呵呵!
谢谢MLTX!
呵呵!好人啊!


6 楼

发信人: hansom (胖胖熊), 信区: NumComp       
标  题: Visual Fortran中注释若干行的实现
发信站: BBS 水木清华站 (Sun Oct 22 20:42:09 2000)

    我用VB Script写了两个宏,可以实现用“!”注释free form程序的若干行,或删
除位于行首的“!”。
    可以把下面的代码存为Commentline.dsm,放到Visual Fortran的Common\msdev98\
macros目录下面。在Visual Fortran集成编译环境下,首先选择菜单Tools-> Customiz
e-> Add-ins and Macro Files,选择Commentline,再点击close按钮。然后用鼠标选择
若干行文本,选择菜单Tools->Macro,选择Macro Name为CommentLine或UnCommentLine
,再点击Run按钮,就可以分别实现注释所选择的文本行和删除所选择的文本行首的“!
”了。在UnCommentLine中,如果有的文本行行首没有“!”,则该行不受影响。
    另外,可以为这两个宏定义快捷键,以方便使用。方法如下:选择菜单Tools->Cus
tomize->Keyboard,在Category中选择Macros,在Commands中会出现CommentLine和UnC
ommentLine,首先选择CommentLine,点击Press new shortcut框,再按下希望定义的快
捷键,最后点击Assign按钮;UnCommentLine的快捷键的定义方法相同。
    Enjoy it!

'-----------------------------------------------------------------------
'FILE DESCRIPTION: 注释若干行;去掉若干行的注释
'-----------------------------------------------------------------------
Sub CommentLine()
'DESCRIPTION: 注释若干行
    Dim EndLine, CurrLine
    EndLine=ActiveDocument.Selection.BottomLine
    ActiveDocument.Selection.StartOfLine
    CurrLine=ActiveDocument.Selection.CurrentLine
    while ( CurrLine<=EndLine )
        ActiveDocument.Selection.SelectLine
        ActiveDocument.Selection = "!" + ActiveDocument.Selection
        CurrLine=ActiveDocument.Selection.CurrentLine
    wend
End Sub

Sub UnCommentLine()
'DESCRIPTION: 去掉若干行的注释
    Dim EndLine, CurrLine, CurrText
    EndLine=ActiveDocument.Selection.BottomLine
    ActiveDocument.Selection.StartOfLine
    CurrLine=ActiveDocument.Selection.CurrentLine
    while ( CurrLine<=EndLine )
        ActiveDocument.Selection.SelectLine
        CurrText = ActiveDocument.Selection
        if ( Left(CurrText, 1) = "!" ) then
            CurrText = Right(CurrText, Len(CurrText)-1)
            ActiveDocument.Selection = CurrText
            CurrLine=ActiveDocument.Selection.CurrentLine
        else
            CurrLine=CurrLine+1
            ActiveDocument.Selection.LineDown
            ActiveDocument.Selection.LineUp
        end if
    wend
End Sub

7 楼

哈哈。。。

如3楼所说:这种技术,就是窗户纸,一捅就破。只要熟悉VBScript,谁都可以做。

稍有不同的是:我对f90,for文件类型进行判断,分别处理;另我用工具钮,他用快捷键。

8 楼

请问mltx老师,如何在一个文件里同时使用两个命令: 一个是"direc",一个是"append",请告诉我,急等!

9 楼

谢谢mltx老师!很好用的说!

另外,问一下,可否把CommentOut和CommentDel两个命令合在一个toolbar中,这样更美观些。:)

10 楼

把第2个按钮拖到第1个的工具条上就行了。

我来回复

您尚未登录,请登录后再回复。点此登录或注册