comparison libinterp/corefcn/pr-output.cc @ 22898:9baa19102908

refactor display and disp functions (bug #49794) * pr-output.cc (Fdisp, Ffdisp): Tag with dispatch classes. (Fdisplay): New function. * ov-class.cc (octave_class::print_with_name): Simply call octave_base_value::print_with_name. * ov-classdef.cc (octave_classdef::print): Simply call print_raw. (octave_classdef::print_with_name): Simply call octave_base_value::print_with_name. * variables.cc (bind_ans): Call display function to print result. * pt-assign.cc (tree_simple_assignment::rvalue1, tree_multi_assignment::rvalue): Likewise. * pt-id.cc (tree_identifier::rvalue): Likewise. * display.m: Delete. * scripts/general/module.mk: Update.
author John W. Eaton <jwe@octave.org>
date Fri, 16 Dec 2016 00:10:27 -0500
parents f84aa17075d4
children b28801182c08
comparison
equal deleted inserted replaced
22897:4090c32fccf8 22898:9baa19102908
51 #include "errwarn.h" 51 #include "errwarn.h"
52 #include "ovl.h" 52 #include "ovl.h"
53 #include "oct-stream.h" 53 #include "oct-stream.h"
54 #include "octave-preserve-stream-state.h" 54 #include "octave-preserve-stream-state.h"
55 #include "pager.h" 55 #include "pager.h"
56 #include "parse.h"
56 #include "pr-output.h" 57 #include "pr-output.h"
57 #include "sysdep.h" 58 #include "sysdep.h"
58 #include "unwind-prot.h" 59 #include "unwind-prot.h"
59 #include "utils.h" 60 #include "utils.h"
60 #include "variables.h" 61 #include "variables.h"
3481 3482
3482 return ovl (string_vector (lst)); 3483 return ovl (string_vector (lst));
3483 } 3484 }
3484 3485
3485 DEFUN (disp, args, nargout, 3486 DEFUN (disp, args, nargout,
3487 classes: cell char double function_handle int8 int16 int32 int64 logical single struct uint8 uint16 uint32 uint64
3486 doc: /* -*- texinfo -*- 3488 doc: /* -*- texinfo -*-
3487 @deftypefn {} {} disp (@var{x}) 3489 @deftypefn {} {} disp (@var{x})
3488 Display the value of @var{x}. 3490 Display the value of @var{x}.
3489 3491
3490 For example: 3492 For example:
3524 3526
3525 return retval; 3527 return retval;
3526 } 3528 }
3527 3529
3528 DEFUN (fdisp, args, , 3530 DEFUN (fdisp, args, ,
3531 classes: cell char double function_handle int8 int16 int32 int64 logical single struct uint8 uint16 uint32 uint64
3529 doc: /* -*- texinfo -*- 3532 doc: /* -*- texinfo -*-
3530 @deftypefn {} {} fdisp (@var{fid}, @var{x}) 3533 @deftypefn {} {} fdisp (@var{fid}, @var{x})
3531 Display the value of @var{x} on the stream @var{fid}. 3534 Display the value of @var{x} on the stream @var{fid}.
3532 3535
3533 For example: 3536 For example:
3588 %! format compact; 3591 %! format compact;
3589 %! compact = disp (foo.(fields{f})); 3592 %! compact = disp (foo.(fields{f}));
3590 %! expected = strrep (loose, "\n\n", "\n"); 3593 %! expected = strrep (loose, "\n\n", "\n");
3591 %! assert (expected, compact); 3594 %! assert (expected, compact);
3592 %! endfor 3595 %! endfor
3596 */
3597
3598 DEFUN (display, args, ,
3599 classes: cell char double function_handle int8 int16 int32 int64 logical single struct uint8 uint16 uint32 uint64
3600 doc: /* -*- texinfo -*-
3601 @deftypefn {} {} display (@var{obj})
3602 Display the contents of the object @var{obj}.
3603
3604 The Octave interpreter calls the @code{display} function whenever it needs
3605 to present a class on-screen. Typically, this would be a statement which
3606 does not end in a semicolon to suppress output. For example:
3607
3608 @example
3609 myobj = myclass (@dots{})
3610 @end example
3611
3612 User-defined classes should overload the @code{display} method so that
3613 something useful is printed for a class object. Otherwise, Octave will
3614 report only that the object is an instance of its class.
3615
3616 @example
3617 @group
3618 myobj = myclass (@dots{})
3619 @result{} myobj = <class myclass>
3620 @end group
3621 @end example
3622
3623 @seealso{class, subsref, subsasgn}
3624 @end deftypefn */)
3625 {
3626 int nargin = args.length ();
3627
3628 if (nargin < 1 || nargin > 2)
3629 print_usage ();
3630
3631 std::string name;
3632
3633 if (nargin == 2)
3634 name = args(1).xstring_value ("CALLER must be a string");
3635 else
3636 {
3637 string_vector names = args.name_tags ();
3638 std::string tmp = names(0);
3639 name = valid_identifier (tmp) ? tmp : "ans";
3640 }
3641
3642 // Only reason we got here is that there was no overloaded display
3643 // function. Rely on built-in functions to display whatever obj is.
3644
3645 octave_value value = args(0);
3646 bool is_scalar = value.is_scalar_type ();
3647
3648 octave_stdout << name << (is_scalar ? " = " : " =\n\n");
3649
3650 // Use feval so that dispatch will also work for disp.
3651
3652 feval ("disp", ovl (value));
3653
3654 if (! is_scalar)
3655 octave_stdout << std::endl;
3656
3657 return ovl ();
3658 }
3659
3660 /*
3661 %!test
3662 %! str = evalc ("x = 1.1; display (x)");
3663 %! assert (str, "x = 1.1000\n");
3664
3665 %!test
3666 %! str = evalc ("display (1.1)");
3667 %! assert (str, " 1.1000\n");
3668
3669 ## Test input validation
3670 %!error display ()
3671 %!error display (1,2)
3593 */ 3672 */
3594 3673
3595 static void 3674 static void
3596 init_format_state (void) 3675 init_format_state (void)
3597 { 3676 {