--- escape.py.orig	2008-04-24 12:36:46.000000000 +0300
+++ escape.py	2008-04-25 01:00:09.000000000 +0300
@@ -27,6 +27,7 @@
 import util
 import os
 import re
+import curses
 
 import encodings
 utf8decode = lambda s: encodings.codecs.utf_8_decode(s)[0]
@@ -106,6 +107,50 @@
 	('[M', 'mouse')
 ]
 
+_fkeys = [("kcuu1", "up"),
+		  ("kcud1", "down"),
+		  ("kcuf1", "right"),
+		  ("kcub1", "left"),
+		  ("kend", "end"),
+		  ("khome", "home"),
+		  ("kich1", "insert"),
+		  ("knp", "page down"),
+		  ("kpp", "page up"),
+		  ("kf1", "f1"),
+		  ("kf2", "f2"),
+		  ("kf3", "f3"),
+		  ("kf4", "f4"),
+		  ("kf5", "f5"),
+		  ("kf6", "f6"),
+		  ("kf7", "f7"),
+		  ("kf8", "f8"),
+		  ("kf9", "f9"),
+		  ("kf10", "f10"),
+		  ("kf11", "f11"),
+		  ("kf12", "f12"),
+		  ("kf13", "f13"),
+		  ("kf14", "f14"),
+		  ("kf15", "f15"),
+		  ("kf16", "f16"),
+		  ("kf17", "f17"),
+		  ("kf18", "f18"),
+		  ("kf19", "f19"),
+		  ("kf20", "f20"),
+		 ]
+
+curses.setupterm()
+
+terminfo_sequences = []
+for _cap, _name in _fkeys:
+	_seq = curses.tigetstr(_cap)
+
+	if _seq is None:
+		continue
+	elif _seq.startswith("\x1b"):
+		_seq = _seq[1:]
+
+	terminfo_sequences.append((_seq, _name))
+
 class KeyqueueTrie(object):
 	def __init__( self, sequences ):
 		self.data = {}
@@ -182,8 +227,12 @@
 
 
 #################################################
+# Build terminfo tree first, input_trie will serve as fallback
+terminfo_trie = KeyqueueTrie(terminfo_sequences)
+
 # Build the input trie from input_sequences list
 input_trie = KeyqueueTrie(input_sequences)
+
 #################################################
 
 _keyconv = {
@@ -277,8 +326,15 @@
 	if code != 27:
 		return ["<%d>"%code], codes[1:]
 
+	# First look at terminfo data
+	result = terminfo_trie.get(codes[1:], more_available)
+	if result is not None:
+		result, remaining_codes = result
+		return [result], remaining_codes
+
+	# Then hardcoded fallback
 	result = input_trie.get(codes[1:], more_available)
-	
+
 	if result is not None:
 		result, remaining_codes = result
 		return [result], remaining_codes
