Commits

Alfredo Di Napoli committed fee556f

Fixed visual bug, added indent through ocamlp4o

Comments (0)

Files changed (1)

ftplugin/ocaml.vim

 
 def write_to_buffer():
   beduino_buff = vim.eval("g:beduino_buffer_location") 
-  text_to_write = vim.eval("g:selected_text")
-  print text_to_write
+  text_to_write = "\n".join(vim.eval("g:selected_text"))
   f = open(beduino_buff, "w")
   f.write(text_to_write)
   f.write(";;")
   f.write(os.linesep)
   f.close()
 
+def write_to_buffer_raw():
+  #Write without trailing \n and ;;
+  beduino_buff = vim.eval("g:beduino_buffer_location") 
+  text_to_write = "\n".join(vim.eval("g:selected_text"))
+  f = open(beduino_buff, "w")
+  f.write(text_to_write)
+  f.close()
+
 def beduino_eval_visual():
   beduino_buff = vim.eval("g:beduino_buffer_location") 
   #First write the content of the visual selection in a buffer
   subprocess.call(["tmux", "load-buffer", beduino_buff ])
   subprocess.call(["tmux", "pasteb", "-t", "beduino"])
 
+def indent_selection_into_buffer():
+  write_to_buffer_raw()
+  beduino_buff = vim.eval("g:beduino_buffer_location") 
+  subprocess.call(["camlp4o", "-impl", beduino_buff, "-o", beduino_buff])
+  
+  #Strip the trailing blank lines that camlp4o adds
+  f = open(beduino_buff, "a+")
+  lines = f.readlines()[0:-2]
+  f.seek(0)
+  f.truncate()
+  f.writelines(lines)
+  f.close()
+
 def beduino_kill():
   subprocess.call(["tmux", "kill-session", "-t", "beduino"])
 
   let [lnum1, col1] = getpos("'<")[1:2]
   let [lnum2, col2] = getpos("'>")[1:2]
   let lines = getline(lnum1, lnum2)
-  "let lines[-1] = lines[-1][: col2 - 2]
-  "let lines[0] = lines[0][col1 - 1:]
-  return join(lines)
+  return lines
 endfunction
 
-" BUGGED: The problem is that is executed for
-" every bloody line in the visual selection,
-" which is no good!
 fun! BeduinoEvalVisual()
   if BeduinoSessionExists()
     let g:selected_text = s:GetVisualSelection()
   endif
 endfun
 
+fun! BeduinoIndentVisualSelection()
+  let g:selected_text = s:GetVisualSelection()
+  python indent_selection_into_buffer()
+  normal gvd
+  let @x = system("cat ". g:beduino_buffer_location)
+  normal "xP
+endfun
+
 fun! BeduinoCloseSession()
   if BeduinoSessionExists()
     if g:beduino_owner == getpid()
       python beduino_kill()
-      ""call system("tmux kill-session -t beduino")
     endif
   endif
 endfun
 map <LocalLeader>bb :call BeduinoEvalBuffer()<RETURN>
 
 "Mnemonic: Beduino (Eval) Visual (Selection)
-vmap <LocalLeader>bv :call BeduinoEvalVisual()<RETURN>
+vmap <LocalLeader>bv :<BS><BS><BS><BS><BS>call BeduinoEvalVisual()<RETURN>
+
+vmap = :<BS><BS><BS><BS><BS>call BeduinoIndentVisualSelection()<RETURN>
 
 "Kill Beduino before exiting Vim
 autocmd VimLeavePre * call BeduinoCloseSession()<RETURN>