1

I want to create a function to copy VHDL component.

I can get the component start and end line number as returned value from other functions.

But don't know how to use the returns to copy the component inside a function.

function! VHDL_Yank_Component()  
   let current_line = line('.')  
   let component_start  = VHDL_Find_Component_Start()  
   let component_end    = VHDL_Find_Component_End()  
   if (component_start != -1) && (component_end != -1)  
      normal ":".component_start",".component_end"Y"
      echo "Component copied"    
   else  
      normal :.current_line<CR>  
      echo "Can't find valid component to copy"  
   endif  
endfunction   
  • 2
    > normal ":".component_start",".component_end"Y" Make this :exe ":\<C-U>".component_start.",".component_end."y" same for the other. You do not want :normal, you want to use : exe to dynamically execute strings. Also make sure you concatenate strings correctly using . – Christian Brabandt Feb 14 '18 at 12:21
  • @ChristianBrabandt Yes, exe is working. Thanks! – Fisher Feb 14 '18 at 13:54
2

Below is fixed code.

function! VHDL_Yank_Component()  
   let current_line = line('.')  
   let component_start = VHDL_Find_Component_Start()  

   if (component_start == -1)  
      exe ":".current_line  
      echo "Can't find valid component start."  
      return -1  
   endif  

   let component_end = VHDL_Find_Component_End()  
   if (component_end ==  -1) || (component_start > component_end)  
      exe ":".current_line  
      echo "Can't find valid component end."  
      return -1  
   endif  

   if (component_start != -1) && (component_end != -1)  
      exe ":".component_start.",".component_end."y"  
      echo "Component copied: " .g:component_name  
   else  
      exe ":".current_line  
      echo "Can't find valid component to copy"  
      return -1  
   endif  
endfunction   
  • Why are you returning something (-1) sometimes, and nothing (hence 0) other times? If you just want to to exit early, you can just execute :return. If you want to abort, because the function is incorrectly used, and because you should not continue, you could either :throw an error message, or use the old :echoerr. – Luc Hermitte Feb 14 '18 at 15:50
  • @LucHermitte Thanks for the suggestion. Just want to exit early and pass execution state to the caller. For example, VHDL_Find_Component_Start may not able to find a valid start, it returns -1, then caller can check whether use the search result. It's simple and it's working. I'll check the two commend you advised. May find other decent way to do the same job. :) – Fisher Feb 14 '18 at 16:01
  • The actual question that'll help us decide: "Is it a programming error? Or is it a possible scenario?" In that later case, "Is there a lot to abort?" (=> exceptions will be nice), "Or shall we do other things when the boundaries aren't found?" (in that case, return-codes could more "fluid"). For instance, in a plugin, I have a find_function_boundaries that throws (at different levels), when we are not within a function scope. – Luc Hermitte Feb 14 '18 at 17:11
  • @LucHermitte Yes, you are correct. There is no need to add "return -1". Thanks a lot for your example and details, I'll go through it. – Fisher Feb 15 '18 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.