Example #1
0
    def history_up(self):
        """Called when the history up command is required"""
        if self.textview.is_focus():
            tb = self.textbuffer
            command = tb.get_tag_table().lookup(COMMAND)
            insert = tb.get_insert()
            it = tb.get_iter_at_mark(insert)
            it.backward_to_tag_toggle(command)
            if it.ends_tag(command):
                it.backward_to_tag_toggle(command)
            self.textbuffer.place_cursor(it)
            self.textview.scroll_mark_onscreen(insert)

        elif self.sourceview.is_focus():
            tb = self.textbuffer
            sb = self.sourcebuffer
            command = tb.get_tag_table().lookup(COMMAND)
            if self.sb_changed:
                if sb.get_end_iter().get_line() != 0:
                    # Don't allow prefixes of more than one line
                    beep()
                    return
                self.hist_prefix = get_text(sb, sb.get_start_iter(),
                                            sb.get_end_iter())
                self.hist_count = {}
                self._track_change()
                tb.move_mark(self.hist_mark, tb.get_end_iter())
            it = tb.get_iter_at_mark(self.hist_mark)
            if it.is_start():
                beep()
                return
            while True:
                it.backward_to_tag_toggle(command)
                if it.ends_tag(command):
                    it.backward_to_tag_toggle(command)
                if not it.begins_tag(command):
                    beep()
                    break
                first_line = self.iter_get_command(
                    it, only_first_line=True).strip()
                if (first_line and first_line.startswith(self.hist_prefix) and
                    (len(first_line) > 2 or self.recall_1_char_commands)):

                    cmd = self.iter_get_command(it).strip()
                    cmd_hash = hash_cmd(cmd)
                    tb.move_mark(self.hist_mark, it)
                    count = self.hist_count.get(cmd_hash, 0) + 1
                    self.hist_count[cmd_hash] = count
                    if count == 1:
                        sb.set_text(cmd)
                        self._track_change()
                        sb.place_cursor(sb.get_end_iter())
                        break
                if it.is_start():
                    beep()
                    return

        else:
            beep()
Example #2
0
    def history_down(self):
        """Called when the history down command is required"""
        if self.textview.is_focus():
            tb = self.textbuffer
            command = tb.get_tag_table().lookup(COMMAND)
            insert = tb.get_insert()
            it = tb.get_iter_at_mark(insert)
            it.forward_to_tag_toggle(command)
            if it.ends_tag(command):
                it.forward_to_tag_toggle(command)
            self.textbuffer.place_cursor(it)
            self.textview.scroll_mark_onscreen(insert)

        elif self.sourceview.is_focus():
            tb = self.textbuffer
            sb = self.sourcebuffer
            command = tb.get_tag_table().lookup(COMMAND)
            if self.sb_changed:
                beep()
                return
            it = tb.get_iter_at_mark(self.hist_mark)
            passed_one = False
            while True:
                if not it.begins_tag(command):
                    # Return the source buffer to the prefix and everything
                    # to initial state.
                    sb.set_text(self.hist_prefix)
                    sb.place_cursor(sb.get_end_iter())
                    # Since we change the text and not called _track_change,
                    # it's like the user did it and hist_prefix is not longer
                    # meaningful.
                    break
                first_line = self.iter_get_command(
                    it, only_first_line=True).strip()
                if (first_line and first_line.startswith(self.hist_prefix) and
                    (len(first_line) > 2 or self.recall_1_char_commands)):

                    cmd = self.iter_get_command(it).strip()
                    cmd_hash = hash_cmd(cmd)
                    tb.move_mark(self.hist_mark, it)
                    if self.hist_count[cmd_hash] == 1:
                        if passed_one:
                            sb.set_text(cmd)
                            self._track_change()
                            sb.place_cursor(sb.get_end_iter())
                            break
                        else:
                            passed_one = True
                    self.hist_count[cmd_hash] -= 1

                it.forward_to_tag_toggle(command)
                it.forward_to_tag_toggle(command)

        else:
            beep()