MATLAB自动完成代码体的开始和结束语句?

时间:2016-11-28 15:24:44

标签: matlab

MATLAB中是否可以自动插入代码体的开始和结束? 例如:classdef和end;功能和结束;方法和结束。

 classdef Circle
        properties
            radius
        end
        methods
            function dia = FindDia(obj)
                dia = [obj.radius]*2;
            end
            function %no automatic insertion of 'end'
        end    
    end

1 个答案:

答案 0 :(得分:2)

由于我经常在这么多不同的编辑中工作,所以我不会依赖任何一个编辑器的功能。在我的所有机器上学习它们并使所有这些编辑器的配置保持同步是一件痛苦的事。通常情况下,拥有相同的功能集非常有用,在编辑器中甚至不代表代码(如MS Word或此处的Stack Overflow)。

因此,我使用AutoHotkey进行此类操作(以及Linux上的Autokey)。

对于函数和类,我使用粘贴函数来粘贴特定的模板文件,具体取决于我是在工作还是在家,以及我正在处理哪个项目。然后一个小GUI会询问我的函数或类名,然后用它填充模板。如果你愿意,我也可以分享。

以下是我用于包含end关键字的一些AutoHotkey功能和热字符串。请注意,这可能看起来过于复杂只是为了放置end,在这种情况下,它可能就是这样。但是clipCopyclipPastegetIndent函数在我的其余编程片段中证明了它们的用处,我希望它们也可能适合您。

我也错过了错误函数,只是因为。

; Copy selected text
; More robust than C-c/C-x/C-v; see  
; https://autohotkey.com/board/topic/111817-robust-copy-and-paste-routine-function/
clipCopy(dontRestoreClipboard := 0)
{
    prevClipboard := Clipboard
    Clipboard := ""

    copyKey := "vk43sc02E" ; Copy

    SendInput, {Shift Down}{Shift Up}{Ctrl Down}{%copyKey% Down}
    ClipWait, 0.25, 1
    SendInput, {%copyKey% Up}{Ctrl Up}

    str := Clipboard

    if (dontRestoreClipboard == 0)
        Clipboard := prevClipboard 

    return str
}

clipPaste(ByRef txt, dontBackupClipboard := 0)
{
    if (txt != "")
    {
        prevClipboard := ""

        pasteKey := "vk56sc02F" ; Paste


        if (dontBackupClipboard == 0) {
            prevClipboard := Clipboard
            Clipboard := ""
        }

        Clipboard := txt
        ClipWait, 1.00, 1

        ; Start pressing paste key
        SendInput, {Shift Down}{Shift Up}{Ctrl Down}{%pasteKey% Down}

        ; Wait until clipboard is ready
        startTime := A_TickCount
        while (DllCall("GetOpenClipboardWindow") && (A_TickCount - startTime < 1000)) {
            Sleep, 50
        }

        ; Release paste key
        SendInput, {%pasteKey% Up}{Ctrl Up}

        ; TODO: a manual delay still seems necessary...this vlaue needs to be this large, to also have
        ; correct behavior in superslow apps like MS Office, Outlook, etc. Sadly, the SetTimer approach
        ; does not seem to work (doesn't correctly restore the clipboard); to be investigated.
        Sleep 333

        ; Put previous clipboard content back onto the clipboard
        Clipboard := prevClipboard 
    }
}

; Get current indentation level in an editor-independent way
getIndent(dontRestoreClipboard := 0)
{
    ; Select text from cursor to start of line
    SendInput, +{Home}

    indent := clipCopy(dontRestoreClipboard)
    numsp  := 0
    if (indent != "")
        indent := RegExReplace(indent, ".", " ", numsp)

    ; Undo selection (this is tricky; different editors often have
    ; different behavior for Home/End keys while text is selected
    SendInput, {Right}{Left}{Home}

    ; NOTE: don't use {End}, because we might be in the middle of a sentence
    ; Use the "right" key, repeatedly
    Loop, %numsp% {
        SendInput, {Right}
    }

    return indent
}

mBasic(str)
{
    current_indent := getIndent()
    SendInput, %str% (){Enter}+{Home}%current_indent%{Space 4}{Enter}+{Home}%current_indent%end{Up 2}{End}{Left}
}

mErrfcn(str)
{
    current_indent := getIndent()
    spaces := RegExReplace(str, ".", " ")
    clipPaste(str . "([mfilename ':default_errorID'],...`r`n" . current_indent . spaces . " 'Default error string.');")

    return current_indent
}



; MATLAB Hotstrings for basic control structures
:o:mfor::
    mBasic("for")
return

:o:mpar::
    mBasic("parfor")
return

:o:mwhile::
    mBasic("while")
return

:o:spmd::
    mBasic("spmd")
return

:o:mif::
    mBasic("if")
return

; ...etc.


; error, warning, assert
:o:merr::
:o:merror::
    mErrfcn("error")
    SendInput, {Up}{End}{Left 21}
return

:o:mwarn::
:o:mwarning::
    mErrfcn("warning")
    SendInput, {Up}{End}{Left 21}
return

_mlAssert()
{
    current_indent := mErrfcn("assert")
    SendInput, {Up}{End}{Left 34}true,...{Enter}+{Home}%current_indent%{Space 7}{Up}{End}{Left 8}
}
:o:massert::
    _mlAssert()
return