# -*- coding:utf-8 -*-
#-----------------------------------------------------------------------------
#  Copyright (c) 2015, ROOT Team.
#  Authors: Omar Zapata <Omar.Zapata@cern.ch> http://oproject.org
#           Danilo Piparo <Danilo.Piparo@cern.ch> CERN
#           Enric Tejedor enric.tejedor.saavedra@cern.ch> CERN
#  website: http://oproject.org/ROOT+Jupyter+Kernel (information only for ROOT kernel)
#  Distributed under the terms of the Modified LGPLv3 License.
#
#  The full license is in the file COPYING.rst, distributed with this software.
#-----------------------------------------------------------------------------
from metakernel import Magic, option

import sys

#NOTE:actually JupyROOT is not capturing the error on %%cpp -d if the function is wrong
class CppMagics(Magic):
    def __init__(self, kernel):
        super(CppMagics, self).__init__(kernel)
    @option(
        '-a', '--aclic', action='store', default="default", help='Compile code with ACLiC.'
    )
    @option(
        '-d', '--declare', action='store', default=None, help='Declare functions and/or classes.'
    )
    def cell_cpp(self, args):
        '''Executes the content of the cell as C++ code.'''
        if self.code.strip():
             self.kernel.ioHandler.clear()
             self.kernel.ioHandler.InitCapture()

             if args=='-a':
                 self.kernel.ACLiC(self.code)
             elif args=='-d':
                 self.kernel.Declarer.Run(str(self.code))
             else:
                 self.kernel.Executor.Run(str(self.code))
             self.kernel.ioHandler.EndCapture()
             std_out = self.kernel.ioHandler.GetStdout()
             std_err = self.kernel.ioHandler.GetStderr()
             if std_out != "":
                stream_content_stdout = {'name': 'stdout', 'text': std_out}
                self.kernel.send_response(self.kernel.iopub_socket, 'stream', stream_content_stdout)
             if std_err != "":
                stream_content_stderr = {'name': 'stderr', 'text': std_err}
                self.kernel.send_response(self.kernel.iopub_socket, 'stream', stream_content_stderr)

        self.evaluate = False

def register_magics(kernel):
    kernel.register_magics(CppMagics)


@@ -1231,87 +1231,23 @@
     
-self.kernel.ioHandler.clear()%0A             self.kernel.ioHandler.InitCapture()%0A
+execFunc = None
 %0A   
@@ -1263,18 +1263,20 @@
  if args
+ 
 ==
+ 
 '-a':%0A  
@@ -1281,32 +1281,43 @@
                 
+ execFunc =
  self.kernel.ACL
@@ -1318,27 +1318,16 @@
 el.ACLiC
-(self.code)
 %0A       
@@ -1341,18 +1341,20 @@
 lif args
+ 
 ==
+ 
 '-d':%0A  
@@ -1359,32 +1359,43 @@
                 
+ execFunc =
  self.kernel.Dec
@@ -1403,32 +1403,16 @@
 arer.Run
-(str(self.code))
 %0A       
@@ -1422,18 +1422,31 @@
    else:
-%0A
  
+# normal flow%0A
         
@@ -1469,20 +1469,25 @@
 nel.
-E
+do_e
 xecut
-or.Run
+e_direct
 (str
@@ -1516,65 +1516,69 @@
     
+   
 self.
-kernel.ioHandler.EndCapture()%0A             std_out =
+evaluate = False%0A                return%0A%0A            
  sel
@@ -1600,17 +1600,13 @@
 ler.
-GetStdout
+Clear
 ()%0A 
@@ -1620,18 +1620,8 @@
     
- std_err =
  sel
@@ -1643,17 +1643,19 @@
 ler.
-GetStderr
+InitCapture
 ()%0A 
@@ -1670,313 +1670,81 @@
     
-if std_out != %22%22:%0A                stream_content_stdout = %7B'name': 'stdout', 'text': std_out%7D%0A                self.kernel.send_response(self.kernel.iopub_socket, 'stream', stream_content_stdout)%0A             if std_err != %22%22:%0A                stream_content_stderr = %7B'name': 'stderr', 'text': std_err%7D%0A   
+execFunc(str(self.code))%0A             self.kernel.ioHandler.EndCapture()%0A
     
@@ -1768,21 +1768,20 @@
 nel.
-send_response
+print_output
 (sel
@@ -1795,50 +1795,14 @@
 l.io
-pub_socket, 'stream', stream_content_stder
+Handle
 r)%0A%0A

Refactor and adapt to the new system