2016-11-28 7 views
2

MATLABではコードボディの開始と終了を自動的に挿入できますか? 例:classdefおよびend;機能と終了;方法と終了。MATLABは自動的にコード本体の開始および終了ステートメントを完了しますか?

+0

あなたは、MATLABエディタを意味しますか?例えばNotepad ++のように? –

+0

@RodyOldenhuisはい。はい。ちょうどそれのように。 –

+0

私はそうは思わない...しかし、私はこれがMATLABの最新バージョンでどのように動作するのかよくわかりません。私はデフォルトのエディタをNotepad ++に設定しましたが、デバッグやコードアナライザーメッセージのチェックのためにMATLABエディタに戻ることがあります –

答えて

2

私は非常に多くの異なるエディタで定期的に働いているので、私はどのエディタの機能にも依存しません。これらのすべてを学び、これらすべてのエディタの設定をすべてのマシンで同期させておくのは苦労です。しばしば、私が以前使っていたのと同じ機能を持っていると便利です。MS Wordやスタックオーバーフローのようなコードではないエディタでも役に立ちます。

したがって、私は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 
関連する問題