module cubeedit_abort
  use cubetools_parameters
  use cubetools_structure
  use cubeedit_messaging
  !
  public :: abort
  private
  !
  type :: abort_comm_t
     type(option_t), pointer :: comm
   contains
     procedure, public  :: register => cubeedit_abort_register
     procedure, private :: parse    => cubeedit_abort_parse
     procedure, private :: main     => cubeedit_abort_main
  end type abort_comm_t
  type(abort_comm_t) :: abort
  !
  type abort_user_t
     character(len=varn_l) :: edid
  end type abort_user_t
  !
contains
  !
  subroutine cubeedit_abort_command(line,error)
    !-------------------------------------------------------------------
    ! Support routine for command ABORT
    !-------------------------------------------------------------------
    character(len=*), intent(in)    :: line
    logical,          intent(inout) :: error
    !
    type(abort_user_t) :: user
    character(len=*), parameter :: rname='ABORT>COMMAND'
    !
    call cubeedit_message(edseve%trace,rname,'Welcome')
    !
    call abort%parse(line,user,error)
    if (error) return
    call abort%main(user,error)
    if (error) return
  end subroutine cubeedit_abort_command
  !
  !----------------------------------------------------------------------
  !
  subroutine cubeedit_abort_register(abort,error)
    !-------------------------------------------------------------------
    ! Register EDIT\ABORT and its options
    !-------------------------------------------------------------------
    class(abort_comm_t), intent(inout) :: abort
    logical,             intent(inout) :: error
    !
    !
    type(standard_arg_t) :: stdarg
    character(len=*), parameter :: rname='ABORT>REGISTER'
    !
    call cubeedit_message(edseve%trace,rname,'Welcome')
    !
    call cubetools_register_command(&
         'ABORT','[bufferid]',&
         'Abort the editing of an opened buffer',&
         'Abort an opened edited buffer and discard its pending&
         & edits.'//strg_bl//&
         'Behavior:'//strg_cr//&
         '- The selected edited buffer is destroyed and not registered in&
         & the DAG. Any previous editing work on that buffer will be lost.'//strg_bl//&
         'Notes:'//strg_cr//&
         '- The edited buffer is registered in the DAG only after CLOSE.'&
         //strg_cr//&
         '- Use CLOSE to register updates in the DAG instead of&
         & discarding them.'//strg_bl//&
         'Examples:'//strg_cr//&
         'CUBE> ABORT'//strg_cr//&
         'CUBE> ABORT work'//strg_bl//&
         'Typical workflow:'//strg_cr//&
         'CUBE> OPEN work /LIKE refcube'//strg_cr//&
         'CUBE> PUT work ...'//strg_cr//&
         'CUBE> ABORT work'//strg_bl//&
         'After ABORT, the edited buffer no longer exists.',&
         cubeedit_abort_command,&
         abort%comm,&
         error)
    if (error) return
    call stdarg%register(&
         'BUFFERID',&
         'Edited buffer identifier',&
         'ABORT resolves bufferid. If omitted, it targets the most&
         & recently opened edited buffer.',&
         code_arg_optional,&
         error)
    if (error) return
    !
  end subroutine cubeedit_abort_register
  !
  subroutine cubeedit_abort_parse(abort,line,user,error)
    !-------------------------------------------------------------------
    ! Parse routine for command ABORT
    !-------------------------------------------------------------------
    class(abort_comm_t), intent(in)    :: abort
    character(len=*),    intent(in)    :: line
    type(abort_user_t),  intent(out)   :: user
    logical,             intent(inout) :: error
    !
    character(len=*), parameter :: rname='ABORT>PARSE'
    !
    call cubeedit_message(edseve%trace,rname,'Welcome')
    !
    user%edid = strg_star
    call cubetools_getarg(line,abort%comm,1,user%edid,.not.mandatory,error)
    if (error) return
    !
  end subroutine cubeedit_abort_parse
  !
  subroutine cubeedit_abort_main(abort,user,error)
    use cubeedit_opened_types
    !-------------------------------------------------------------------
    ! Main routine for command ABORT
    !-------------------------------------------------------------------
    class(abort_comm_t), intent(in)    :: abort
    type(abort_user_t),  intent(in)    :: user
    logical,             intent(inout) :: error
    !
    character(len=*), parameter :: rname='ABORT>MAIN'
    !
    call cubeedit_message(edseve%trace,rname,'Welcome')
    !
    call opened%abort(user%edid,error)
    if (error) return
  end subroutine cubeedit_abort_main
end module cubeedit_abort
