2016-11-28 133 views
2

是否有可能在MATLAB自動插入一個代碼體的開始和結束? 例如:classdef和end;功能和結束;方法和結束。MATLAB自動完成代碼體的開始和結束語句?

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

你的意思是,MATLAB編輯器?例如在記事本++中? –

+0

@RodyOldenhuis是的。是。就這樣。 –

+0

我不這麼認爲......但我不確定這是如何工作在最新版本的MATLAB。我剛剛設置我的默認編輯器記事本+ +,只是偶爾回去MATLAB編輯器,調試和檢查代碼分析器的消息 –

回答

2

因爲我經常在這麼多不同的編輯中工作,所以我不依賴任何一位編輯的功能。將所有這些編輯器全部學習並保持配置同步,在我所有的機器上都是一種痛苦。通常,在編輯器中使用相同的功能集對我來說是非常有用的,甚至不適用於代碼(例如MS Word或Stack Overflow)。

因此,我用AutoHotkey的對這種事情(和自動密鑰在Linux上)。

對於函數和類,我用粘貼功能貼上特定的模板文件,這取決於我是否在工作中還是在家裏,和項目我的工作。一個小GUI然後要求我輸入函數或類名,然後用它填充模板。如果你願意,我也可以分享。

下面是一些AutoHotkey的功能和熱字串我使用包括end關鍵字。請注意,這可能似乎過於複雜,只是爲了放置一個end,在這種情況下,它可能是。但是clipCopy,clipPastegetIndent函數已在其餘的編程片段中證明了它們的有用性,我希望它們也可能適合您。

我在錯誤的功能甩爲好,只是因爲。

; 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