Example #1
0
    def _reaugment_word(self, word_end_offset):
        if not self.app.config['augmented_text']:
            return

        word_start_offset = word_end_offset

        # TODO: do purely with iters?
        while widgets.get_uslice(
                  self.buffer.get_iter_at_offset(word_start_offset-1),
                  self.buffer.get_iter_at_offset(word_start_offset)
              ).isalpha():
            word_start_offset -= 1

        if word_start_offset == word_end_offset:
            return

        iter1 = self.buffer.get_iter_at_offset(word_start_offset) 
        iter2 = self.buffer.get_iter_at_offset(word_end_offset) 

        word_start_offset -= self.prompt_end
        word_end_offset -= self.prompt_end
        length = word_end_offset - word_start_offset

        sum = 0
        for char in self.augmentation[word_start_offset:word_end_offset]:
            sum += ord(char)

        average = sum / length
        tag = "aug" + str(average/16)

        self.augmentation = self.augmentation[:word_start_offset] + \
                            chr(average) * length + \
                            self.augmentation[word_end_offset:]
        
        self.buffer.remove_all_tags(iter1,iter2)
        self.buffer.apply_tag_by_name(tag,iter1,iter2)
Example #2
0
    def on_key_press(self, _w,_e):
        self.key_presses[_e.keyval] = (_e.time, self.get_insertion_offset() - self.prompt_end)
        

        if _e.string and not _e.string.isalpha():
            self._reaugment_word(self.get_insertion_offset())

        if _e.keyval == 65383: # Menu
            field= self.last_field
            while field:
                if field.start <= self.get_insertion_offset() < field.end:
                    if field.active:
                        def pos_func(x):
                            mark = self.buffer.get_insert()
                            iter = self.buffer.get_iter_at_mark(mark)
                            r    = self.view.get_iter_location(iter)
                            x,y  = self.view.buffer_to_window_coords(gtk.TEXT_WINDOW_WIDGET,r.x,r.y)
                            xx,yy = self.handler.app.window.get_position()
                            xxx,yyy = self.view.window.get_position()
                            return xxx+xx+x,yyy+yy+y+30,gtk.TRUE
                        field.popup_menu(_w,_e,self.handler.app,pos_func)
                        return gtk.TRUE
                    else:
                        return gtk.FALSE
                field=field.prev
            return gtk.FALSE

            
        if _e.keyval == 65293: # Enter

            if self.mode == 'edit_mode':
                self.mode = 'command_mode'
                text = widgets.get_uslice(
                    self.buffer.get_iter_at_offset(self.edited_field.start),
                    self.buffer.get_iter_at_offset(self.edited_field.end))

                self.edited_field.on_edit(text)
                self.view.emit_stop_by_name("key-press-event")
                return

            if self.completion_field:
                self.completion_field.show_gtk('')
                self.completion_field = None
            offset = self.get_insertion_offset()
            if offset < self.prompt_end:
                self.buffer.place_cursor(self.buffer.get_end_iter())
                self.view.emit_stop_by_name("key-press-event")
                return

            if widgets.get_uslice(
                 self.buffer.get_iter_at_offset(offset-1),
                 self.buffer.get_iter_at_offset(offset)
               ) == '\\':
                return

            command = widgets.get_uslice(self.buffer.get_iter_at_offset(self.prompt_end),
                                         self.buffer.get_end_iter())
            command_aug = self.augmentation
            if command:
                self.history[-1] = command
                self.history.append('')
                self.history_position = len(self.history)-1

	    self.prompt_start = self.buffer.get_end_iter().get_offset()
	    self.prompt_end = self.prompt_start
            self.augmentation = ''
            self.show('\n') # note: gtk_show was needed here
	    self.buffer.place_cursor(self.buffer.get_end_iter())
            
            #command = string.replace(command,'\\\n','\n')
            min_index = 0
            while 1:
                index = command.find('\\\n')
                if index < min_index: break
                min_index = index
                command = command[:index] + command[index+1:]
                command_aug = command_aug[:index] + command_aug[index+1:]
            
            self.view.emit_stop_by_name("key-press-event")

            #handler might be chat, or a shell...
            self.handler.handle_user_entry(command, command_aug)
            return
 
        if _e.state & 5: # Ctrl

            prefix = widgets.get_uslice(
              self.buffer.get_iter_at_offset(self.prompt_end),
              self.buffer.get_end_iter())
            self.handler.handle_control_key(prefix,_e)
            #return gtk.TRUE
            
        if _e.keyval == 65289:    #tabulation
          
            # auto-completions
            # todo: the info used here should not belong to the tfield object
            # completions should be registered, like commands
            
            prefix = widgets.get_uslice(self.buffer.get_iter_at_offset(self.prompt_end),
                                        self.buffer.get_end_iter())
            return self.handler.handle_tabulation_key(prefix,_e)


        
        position = self.get_insertion_offset() - self.prompt_end
        if position >= 0 and _e.keyval in [65362, 65364]:
            command = widgets.get_uslice(self.buffer.get_iter_at_offset(self.prompt_end),
                                        self.buffer.get_end_iter())
	    
            if string.find(command[:position],'\n') == -1 and \
               _e.keyval == 65362: # Up arrow
                if self.history_position > 0:
                    self.history[self.history_position] = command
                    self.history_position = self.history_position - 1
		    str = self.history[self.history_position]
                    self.buffer.delete(
                        self.buffer.get_iter_at_offset(self.prompt_end),
                        self.buffer.get_end_iter())
                    self.buffer.insert(self.buffer.get_end_iter(), str)
		    self.force_cursor_onscreen()
                return 1

            if string.find(command[position:],'\n') == -1 and \
                 _e.keyval == 65364: # Down arrow
                if self.history_position < len(self.history)-1:
                    self.history[self.history_position] = command
                    self.history_position = self.history_position + 1
                    str = self.history[self.history_position]
                    self.buffer.delete(
                        self.buffer.get_iter_at_offset(self.prompt_end),
                        self.buffer.get_end_iter())
                    self.buffer.insert(self.buffer.get_end_iter(), str)
                    self.buffer.place_cursor(self.buffer.get_iter_at_offset(self.prompt_end))
		    self.force_cursor_onscreen()
                return 1