pyclewn.vim 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. " pyclewn run time file
  2. " Maintainer: <xdegaye at users dot sourceforge dot net>
  3. "
  4. " Configure VIM to be used with pyclewn and netbeans
  5. "
  6. if exists("s:did_pyclewn")
  7. finish
  8. endif
  9. let s:did_pyclewn = 1
  10. let s:start_err = "Error: pyclewn failed to start, "
  11. let s:start_err .= "run the 'pyclewn' program to get the cause of the problem."
  12. " The following variables define how pyclewn is started when
  13. " the ':Pyclewn' vim command is run.
  14. " They may be changed to match your preferences.
  15. let s:pgm = "pyclewn"
  16. if exists("pyclewn_args")
  17. let s:args = pyclewn_args
  18. else
  19. let s:args = "--window=top --maxlines=10000 --background=Cyan,Green,Magenta"
  20. endif
  21. if exists("pyclewn_connection")
  22. let s:connection = pyclewn_connection
  23. else
  24. let s:connection = "localhost:3219:changeme"
  25. endif
  26. " Uncomment the following line to print full traces in a file named 'logfile'
  27. " for debugging purpose.
  28. " let s:args .= " --level=nbdebug --file=logfile"
  29. " The 'Pyclewn' command starts pyclewn and vim netbeans interface.
  30. let s:fixed = "--daemon --editor= --netbeans=" . s:connection . " --cargs="
  31. " Run the 'Cinterrupt' command to open the console
  32. function s:start_pdb(args)
  33. let argl = split(a:args)
  34. if index(argl, "--pdb") != -1
  35. " find the prefix
  36. let prefix = "C"
  37. let idx = index(argl, "-x")
  38. if idx == -1
  39. let idx = index(argl, "--prefix")
  40. if idx == -1
  41. for item in argl
  42. if stridx(item, "--prefix") == 0
  43. let pos = stridx(item, "=")
  44. if pos != -1
  45. let prefix = strpart(item, pos + 1)
  46. endif
  47. endif
  48. endfor
  49. endif
  50. endif
  51. if idx != -1 && len(argl) > idx + 1
  52. let prefix = argl[idx + 1]
  53. endif
  54. " hack to prevent Vim being stuck in the command line with '--More--'
  55. echohl WarningMsg
  56. echo "About to run the 'interrupt' command."
  57. call inputsave()
  58. call input("Press the <Enter> key to continue.")
  59. call inputrestore()
  60. echohl None
  61. exe prefix . "interrupt"
  62. endif
  63. endfunction
  64. " Check wether pyclewn successfully wrote the script file
  65. function s:pyclewn_ready(filename)
  66. let l:cnt = 1
  67. let l:max = 20
  68. echohl WarningMsg
  69. while l:cnt < l:max
  70. echon "."
  71. let l:cnt = l:cnt + 1
  72. if filereadable(a:filename)
  73. break
  74. endif
  75. sleep 200m
  76. endwhile
  77. echohl None
  78. if l:cnt == l:max
  79. throw s:start_err
  80. endif
  81. call s:info("Creation of vim script file \"" . a:filename . "\": OK.\n")
  82. endfunction
  83. " Start pyclewn and vim netbeans interface.
  84. function s:start(args)
  85. if !exists(":nbstart")
  86. throw "Error: the ':nbstart' vim command does not exist."
  87. endif
  88. if has("netbeans_enabled")
  89. throw "Error: netbeans is already enabled and connected."
  90. endif
  91. if !executable(s:pgm)
  92. throw "Error: '" . s:pgm . "' cannot be found or is not an executable."
  93. endif
  94. let l:tmpfile = tempname()
  95. " remove console and dbgvar buffers from previous session
  96. if bufexists("(clewn)_console")
  97. bwipeout (clewn)_console
  98. endif
  99. if bufexists("(clewn)_dbgvar")
  100. bwipeout (clewn)_dbgvar
  101. endif
  102. " start pyclewn and netbeans
  103. call s:info("Starting pyclewn.\n")
  104. exe "silent !" . s:pgm . " " . a:args . " " . s:fixed . l:tmpfile . " &"
  105. call s:info("Running nbstart, <C-C> to interrupt.\n")
  106. call s:pyclewn_ready(l:tmpfile)
  107. exe "nbstart :" . s:connection
  108. " source vim script
  109. if has("netbeans_enabled")
  110. if !filereadable(l:tmpfile)
  111. nbclose
  112. throw s:start_err
  113. endif
  114. " the pyclewn generated vim script is sourced only once
  115. if ! exists("s:source_once")
  116. let s:source_once = 1
  117. exe "source " . l:tmpfile
  118. endif
  119. call s:info("The netbeans socket is connected.\n")
  120. call s:start_pdb(a:args)
  121. else
  122. throw "Error: the netbeans socket could not be connected."
  123. endif
  124. endfunction
  125. function pyclewn#StartClewn(...)
  126. " command to start pdb: Pyclewn pdb foo.py arg1 arg2 ....
  127. let l:args = s:args
  128. if a:0 != 0
  129. if a:1 == "pdb"
  130. if a:0 == 2 && filereadable(a:2) == 0
  131. call s:error("File '" . a:2 . "' is not readable.")
  132. return
  133. endif
  134. let l:args .= " --pdb"
  135. if a:0 > 1
  136. let l:args .= " --args \"" . join(a:000[1:], ' ') . "\""
  137. endif
  138. else
  139. call s:error("Invalid optional first argument: must be 'pdb'.")
  140. return
  141. endif
  142. endif
  143. try
  144. call s:start(l:args)
  145. catch /.*/
  146. call s:info("The 'Pyclewn' command has been aborted.\n")
  147. call s:error(v:exception)
  148. " vim console screen is garbled, redraw the screen
  149. if !has("gui_running")
  150. redraw!
  151. endif
  152. " clear the command line
  153. echo "\n"
  154. endtry
  155. endfunction
  156. function s:info(msg)
  157. echohl WarningMsg
  158. echo a:msg
  159. echohl None
  160. endfunction
  161. function s:error(msg)
  162. echohl ErrorMsg
  163. echo a:msg
  164. call inputsave()
  165. call input("Press the <Enter> key to continue.")
  166. call inputrestore()
  167. echohl None
  168. endfunction