Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

""" 

Readline 

  

This is the library behind the command line input, it takes keypresses 

until you hit Enter and then returns it as a string to Python. We hook 

into it so we can make it redraw the input area. 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import * 

sage: replace_line('foobar', 0) 

sage: set_point(3) 

sage: print('current line: ' + repr(copy_text(0, get_end()))) 

current line: 'foobar' 

sage: print('cursor position: {}'.format(get_point())) 

cursor position: 3 

  

When printing with :class:`interleaved_output` the prompt and current 

line is removed:: 

  

sage: with interleaved_output(): 

....: print('output') 

....: print('current line: ' + repr(copy_text(0, get_end()))) 

....: print('cursor position: {}'.format(get_point())) 

output 

current line: '' 

cursor position: 0 

  

After the interleaved output, the line and cursor is restored to the 

old value:: 

  

sage: print('current line: ' + repr(copy_text(0, get_end()))) 

current line: 'foobar' 

sage: print('cursor position: {}'.format(get_point())) 

cursor position: 3 

  

Finally, clear the current line for the remaining doctests:: 

  

sage: replace_line('', 1) 

""" 

  

#***************************************************************************** 

# Copyright (C) 2013 Volker Braun <vbraun.name@gmail.com> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function 

  

  

cdef extern from 'readline/readline.h': 

int rl_forced_update_display() 

int rl_redisplay() 

int rl_message(char* msg) 

int rl_clear_message() 

void rl_replace_line(char* line, int pos) 

char* rl_copy_text(int begin, int end) 

void rl_save_prompt() 

void rl_restore_prompt() 

int rl_read_key() 

int rl_set_signals() 

int rl_clear_signals() 

int rl_crlf() 

int rl_initialize() 

int rl_catch_signals 

int rl_catch_sigwinch 

int rl_point 

int rl_end 

  

  

def print_status(): 

""" 

Print readline status for debug purposes 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import print_status 

sage: print_status() 

catch_signals: 1 

catch_sigwinch: 1 

""" 

print('catch_signals:', rl_catch_signals) 

print('catch_sigwinch:', rl_catch_sigwinch) 

  

def set_signals(): 

""" 

Install the readline signal handlers 

  

Install Readline's signal handler for SIGINT, SIGQUIT, SIGTERM, 

SIGALRM, SIGTSTP, SIGTTIN, SIGTTOU, and SIGWINCH, depending on the 

values of rl_catch_signals and rl_catch_sigwinch. 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import set_signals 

sage: set_signals() 

0 

""" 

return rl_set_signals() 

  

  

def clear_signals(): 

""" 

Remove the readline signal handlers 

  

Remove all of the Readline signal handlers installed by 

:func:`set_signals` 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import clear_signals 

sage: clear_signals() 

0 

""" 

return rl_clear_signals() 

  

def get_point(): 

""" 

Get the cursor position 

  

OUTPUT: 

  

Integer 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import get_point, set_point 

sage: get_point() 

0 

sage: set_point(5) 

sage: get_point() 

5 

sage: set_point(0) 

""" 

return rl_point 

  

def get_end(): 

""" 

Get the end position of the current input 

  

OUTPUT: 

  

Integer 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import get_end 

sage: get_end() 

0 

""" 

return rl_end 

  

def set_point(point): 

""" 

Set the cursor position 

  

INPUT: 

  

- ``point`` -- integer. The new cursor position. 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import get_point, set_point 

sage: get_point() 

0 

sage: set_point(5) 

sage: get_point() 

5 

sage: set_point(0) 

""" 

global rl_point 

rl_point = point 

  

def forced_update_display(): 

""" 

Force the line to be updated and redisplayed, whether or not 

Readline thinks the screen display is correct. 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import forced_update_display 

sage: forced_update_display() 

0 

""" 

return rl_forced_update_display() 

  

def copy_text(pos_start, pos_end): 

""" 

Return a copy of the text between start and end in the current line. 

  

INPUT: 

  

- ``pos_start``, ``pos_end`` -- integer. Start and end position. 

  

OUTPUT: 

  

String. 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import copy_text, replace_line 

sage: replace_line('foobar', 0) 

sage: copy_text(1, 5) 

'ooba' 

""" 

return rl_copy_text(pos_start, pos_end) 

  

def replace_line(text, clear_undo): 

""" 

Replace the contents of rl_line_buffer with text. 

  

The point and mark are preserved, if possible. 

  

INPUT: 

  

- ``text`` -- the new content of the line. 

  

- ``clear_undo`` -- integer. If non-zero, the undo list associated 

with the current line is cleared. 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import copy_text, replace_line 

sage: replace_line('foobar', 0) 

sage: copy_text(1, 5) 

'ooba' 

""" 

rl_replace_line(text, clear_undo) 

  

def initialize(): 

""" 

Initialize or re-initialize Readline’s internal state. It’s not 

strictly necessary to call this; readline() calls it before 

reading any input. 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import initialize 

sage: initialize() 

0 

""" 

return rl_initialize() 

  

  

  

class interleaved_output: 

  

def __init__(self): 

""" 

Context manager for asynchronous output 

  

This allows you to show output while at the readline 

prompt. When the block is left, the prompt is restored even if 

it was clobbered by the output. 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import interleaved_output 

sage: with interleaved_output(): 

....: print('output') 

output 

""" 

pass 

  

def __enter__(self): 

""" 

Called when entering the with block 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import interleaved_output 

sage: with interleaved_output(): 

....: print('output') 

output 

""" 

self._saved_point = rl_point; 

self._saved_line = rl_copy_text(0, rl_end) 

rl_save_prompt() 

rl_replace_line('', 0) 

rl_redisplay() 

rl_clear_signals() 

  

def __exit__(self, exc_type, exc_val, exc_tb): 

""" 

Called when entering the with block 

  

EXAMPLES:: 

  

sage: from sage.libs.readline import interleaved_output 

sage: with interleaved_output(): 

....: print('output') 

output 

""" 

rl_set_signals() 

rl_replace_line(self._saved_line, 0) 

global rl_point 

rl_point = self._saved_point 

rl_restore_prompt() 

rl_forced_update_display() 

return False