Mercurial > octave-nkf
comparison liboctave/cmd-hist.cc @ 11486:a1deab9a6e71
bash-like history control
author | Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 12 Jan 2011 03:40:19 -0500 |
parents | 944b7e20fc5a |
children | 7aeb4eb7403f |
comparison
equal
deleted
inserted
replaced
11485:571bfa4fc295 | 11486:a1deab9a6e71 |
---|---|
115 { | 115 { |
116 if (s.empty () | 116 if (s.empty () |
117 || (s.length () == 1 && (s[0] == '\r' || s[0] == '\n'))) | 117 || (s.length () == 1 && (s[0] == '\r' || s[0] == '\n'))) |
118 return; | 118 return; |
119 | 119 |
120 ::octave_add_history (s.c_str ()); | 120 lines_this_session += ::octave_add_history (s.c_str (), history_control); |
121 | |
122 lines_this_session++; | |
123 } | 121 } |
124 } | 122 } |
125 | 123 |
126 void | 124 void |
127 gnu_history::do_remove (int n) | 125 gnu_history::do_remove (int n) |
419 #endif | 417 #endif |
420 } | 418 } |
421 | 419 |
422 void | 420 void |
423 command_history::initialize (bool read_history_file, | 421 command_history::initialize (bool read_history_file, |
424 const std::string& f_arg, int sz) | 422 const std::string& f_arg, int sz, |
425 { | 423 const std::string & control_arg) |
426 if (instance_ok ()) | 424 { |
427 instance->do_initialize (read_history_file, f_arg, sz); | 425 if (instance_ok ()) |
426 instance->do_initialize (read_history_file, f_arg, sz, control_arg); | |
428 } | 427 } |
429 | 428 |
430 bool | 429 bool |
431 command_history::is_initialized (void) | 430 command_history::is_initialized (void) |
432 { | 431 { |
452 return (instance_ok ()) | 451 return (instance_ok ()) |
453 ? instance->do_file () : std::string (); | 452 ? instance->do_file () : std::string (); |
454 } | 453 } |
455 | 454 |
456 void | 455 void |
456 command_history::process_histcontrol (const std::string& control_arg) | |
457 { | |
458 if (instance_ok ()) | |
459 instance->do_process_histcontrol(control_arg); | |
460 } | |
461 | |
462 std::string | |
463 command_history::histcontrol (void) | |
464 { | |
465 return (instance_ok ()) | |
466 ? instance->do_histcontrol () : std::string (); | |
467 } | |
468 | |
469 void | |
457 command_history::set_size (int n) | 470 command_history::set_size (int n) |
458 { | 471 { |
459 if (instance_ok ()) | 472 if (instance_ok ()) |
460 instance->do_set_size (n); | 473 instance->do_set_size (n); |
461 } | 474 } |
641 instance->do_clean_up_and_save (f, n); | 654 instance->do_clean_up_and_save (f, n); |
642 } | 655 } |
643 | 656 |
644 void | 657 void |
645 command_history::do_initialize (bool read_history_file, | 658 command_history::do_initialize (bool read_history_file, |
646 const std::string& f_arg, int sz) | 659 const std::string& f_arg, int sz, |
660 const std::string & control_arg) | |
647 { | 661 { |
648 command_history::set_file (f_arg); | 662 command_history::set_file (f_arg); |
649 command_history::set_size (sz); | 663 command_history::set_size (sz); |
664 command_history::process_histcontrol (control_arg); | |
650 | 665 |
651 if (read_history_file) | 666 if (read_history_file) |
652 command_history::read (false); | 667 command_history::read (false); |
653 | 668 |
654 initialized = true; | 669 initialized = true; |
663 void | 678 void |
664 command_history::do_set_file (const std::string& f) | 679 command_history::do_set_file (const std::string& f) |
665 { | 680 { |
666 xfile = f; | 681 xfile = f; |
667 } | 682 } |
683 | |
684 void | |
685 command_history::do_process_histcontrol (const std::string& control_arg) | |
686 { | |
687 history_control = 0; | |
688 | |
689 size_t len = control_arg.length (); | |
690 size_t beg = 0; | |
691 | |
692 while (beg < len) | |
693 { | |
694 if (control_arg[beg] == ':') | |
695 beg++; | |
696 else | |
697 { | |
698 size_t end = control_arg.find (":", beg); | |
699 | |
700 if (end == std::string::npos) | |
701 end = len; | |
702 | |
703 std::string tmp = control_arg.substr (beg, end-beg); | |
704 | |
705 if (tmp == "erasedups") | |
706 history_control |= HC_ERASEDUPS; | |
707 else if (tmp == "ignoreboth") | |
708 history_control |= HC_IGNDUPS|HC_IGNSPACE; | |
709 else if (tmp == "ignoredups") | |
710 history_control |= HC_IGNDUPS; | |
711 else if (tmp == "ignorespace") | |
712 history_control |= HC_IGNSPACE; | |
713 else | |
714 (*current_liboctave_warning_handler) | |
715 ("unknown histcontrol directive %s", tmp.c_str ()); | |
716 | |
717 if (end != std::string::npos) | |
718 beg = end + 1; | |
719 } | |
720 } | |
721 } | |
722 | |
723 std::string | |
724 command_history::do_histcontrol (void) const | |
725 { | |
726 // FIXME -- instead of reconstructing this value, should we just save | |
727 // the string we were given when constructing the command_history | |
728 // object? | |
729 | |
730 std::string retval; | |
731 | |
732 if (history_control & HC_IGNSPACE) | |
733 retval.append ("ignorespace"); | |
734 | |
735 if (history_control & HC_IGNDUPS) | |
736 { | |
737 if (retval.length() > 0) | |
738 retval.append (":"); | |
739 | |
740 retval.append ("ignoredups"); | |
741 } | |
742 | |
743 if (history_control & HC_ERASEDUPS) | |
744 { | |
745 if (retval.length() > 0) | |
746 retval.append (":"); | |
747 | |
748 retval.append ("erasedups"); | |
749 } | |
750 | |
751 return retval; | |
752 } | |
753 | |
668 | 754 |
669 std::string | 755 std::string |
670 command_history::do_file (void) | 756 command_history::do_file (void) |
671 { | 757 { |
672 return xfile; | 758 return xfile; |