Module: RupatMod
- Included in:
- Rupat, RupatAlias
- Defined in:
- lib/rupat.rb
Overview
Defined Under Namespace
Classes: RupatError, RupatInvalidLineError, RupatPasteError, RupatSearchError, RupatTypeError
Instance Attribute Summary (collapse)
-
- (Object) copybuf
readonly
Copy buffer.
-
- (Object) lines
readonly
Array of lines including current content.
-
- (Object) newFile
Created file reference.
-
- (Object) orgFile
Original file reference.
Instance Method Summary (collapse)
-
- (Object) [](range)
Return lines content.
-
- (Object) abort(file = @newFile)
Don't save changes, just close the file.
-
- (Object) append(str = "")
Append line to current position.
-
- (Object) appendMany(content)
Append lines to current position.
-
- (Object) backward(count = 1)
(also: #up, #back, #dec)
Backwards line (or more).
-
- (String, NilClass) close(file = @newFile)
Save edits and close file.
-
- (Array<String>) copy(l1 = nil, l2 = nil)
Store content of lines bounded by start/end markers (inclusive) to copy-buffer.
-
- (Object) create(orgFile, newFile = nil)
Create new file based on given name (or IO stream).
-
- (Object) cut(l1 = nil, l2 = nil)
Delete specified lines (inclusive).
-
- (Object) delete
Delete current line.
-
- (Object) deleteMany(cnt = length)
Delete current line N times.
-
- (Object) edit(file, backup = true)
Create new file based on old.
-
- (Object) excursion { ... }
Perform operations in block, but revert back to original position after block completion.
-
- (Object) findBackward(re)
(also: #bfind)
Find regexp backwards.
-
- (Boolean) findBackward?(re)
(also: #bfind?)
Find regexp backward.
-
- (Integer) findBlock(re1, re2)
Return pair of line numbers that match the start/end regexps.
-
- (Object) findForward(re)
(also: #find, #ffind)
Find regexp forwards.
-
- (Boolean) findForward?(re)
(also: #find?, #ffind?)
Find regexp forward.
-
- (Object) forward(count = 1)
(also: #step, #down, #inc)
Forward line (or more).
-
- (String) get(line = nil)
Return line content.
-
- (Array<String>) getMany(l1 = nil, l2 = nil)
Return content of lines bounded by start/end markers (inclusive).
-
- (Object) goto(line = 0)
(also: #jump)
Goto line.
-
- (Object) goto1(line = 1)
Goto (text editor) line.
-
- (Object) gotoEnd
(also: #jumpEnd)
Goto line after last line.
-
- (Object) gotoFirst
(also: #jumpFirst)
Goto first line.
-
- (Object) gotoForce(line = 0)
Goto line without checking for line number validity.
-
- (Object) gotoLast
(also: #jumpLast)
Goto last line.
-
- (Object) insert(str = "")
Insert a line at current position.
-
- (Object) insertFile(file)
Insert file content to current position.
-
- (Object) insertMany(content)
Insert multiple lines at current position.
-
- (Object) last
Return last line number.
-
- (Object) length
Return lines length.
-
- (Object) line
Return current line number.
-
- (Object) line1
Return current line number + one, i.e.
-
- (Object) next
Return next line number from current.
-
- (Object) open(file)
Open a file in source mode (read only).
- - (Object) paste(err = true)
-
- (Object) prev
Return prev line number from current.
-
- (Object) print(fh = STDOUT)
Print file content.
-
- (Object) replace {|get| ... }
(also: #subs)
Replace the current line content (i.e. get&set).
-
- (Object) replaceAll(re, str)
Replace all occurance of re with str.
-
- (Object) replaceWithin(re, str, l1 = nil, l2 = nil)
Replace all occurance of re with str within given range.
-
- (Object) save(file = @newFile)
Save edits.
-
- (Object) set(str, line = nil)
Set line content.
-
- (Object) update { ... }
Update the content using proc block.
-
- (Object) use(lines)
Use set of lines for editing.
Instance Attribute Details
- (Object) copybuf (readonly)
Copy buffer.
25 26 27 |
# File 'lib/rupat.rb', line 25 def copybuf @copybuf end |
- (Object) lines (readonly)
Array of lines including current content.
22 23 24 |
# File 'lib/rupat.rb', line 22 def lines @lines end |
- (Object) newFile
Created file reference.
31 32 33 |
# File 'lib/rupat.rb', line 31 def newFile @newFile end |
- (Object) orgFile
Original file reference.
28 29 30 |
# File 'lib/rupat.rb', line 28 def orgFile @orgFile end |
Instance Method Details
- (Object) [](range)
Return lines content.
297 298 299 |
# File 'lib/rupat.rb', line 297 def []( range ) @lines[ range ] end |
- (Object) abort(file = @newFile)
Don't save changes, just close the file.
518 519 520 |
# File 'lib/rupat.rb', line 518 def abort( file = @newFile ) file.close if file.class == File end |
- (Object) append(str = "")
Append line to current position.
412 413 414 415 416 |
# File 'lib/rupat.rb', line 412 def append( str = "" ) forward @lines.insert( @curline, str ) self end |
- (Object) appendMany(content)
Append lines to current position.
423 424 425 426 427 428 429 |
# File 'lib/rupat.rb', line 423 def appendMany( content ) lines = manyLineContent( content ) lines.each do |s| append( s ) end self end |
- (Object) backward(count = 1) Also known as: up, back, dec
Backwards line (or more).
180 181 182 183 184 |
# File 'lib/rupat.rb', line 180 def backward( count = 1 ) @curline -= count normalizeCurline self end |
- (String, NilClass) close(file = @newFile)
Save edits and close file.
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
# File 'lib/rupat.rb', line 528 def close( file = @newFile ) # Backup original file: if @backup time = Time.now.strftime( "%y%m%d_%H%M_%S" ) p = @orgFile.split( '/' ) backupFile = "#{p[0..-2].join('/')}/rupat_#{time}_#{p[-1]}" File.rename( @orgFile, backupFile ) @orgFile = backupFile end save( file ) file.close if file.class == File if @backup backupFile else nil end end |
- (Array<String>) copy(l1 = nil, l2 = nil)
Store content of lines bounded by start/end markers (inclusive) to copy-buffer.
329 330 331 332 333 |
# File 'lib/rupat.rb', line 329 def copy( l1 = nil, l2 = nil ) l1, l2 = linePairRef( l1, l2 ) @copybuf = @lines[ lineRef( l1 )..lineRef( l2 ) ] self end |
- (Object) create(orgFile, newFile = nil)
Create new file based on given name (or IO stream).
38 39 40 41 42 43 44 45 46 |
# File 'lib/rupat.rb', line 38 def create( orgFile, newFile = nil ) @orgFile = orgFile @newFile = newFile @lines = readclean( @orgFile ) @curline = 0 end |
- (Object) cut(l1 = nil, l2 = nil)
Delete specified lines (inclusive). Save content to copy-buffer.
458 459 460 461 462 463 464 465 466 |
# File 'lib/rupat.rb', line 458 def cut( l1 = nil, l2 = nil ) l1, l2 = linePairRef( l1, l2 ) @copybuf = [] cnt = l2-l1+1 cnt.times do @copybuf.push @lines.delete_at( l1 ) end self end |
- (Object) delete
Delete current line.
435 436 437 438 |
# File 'lib/rupat.rb', line 435 def delete @lines.delete_at( @curline ) self end |
- (Object) deleteMany(cnt = length)
Delete current line N times.
445 446 447 448 449 450 |
# File 'lib/rupat.rb', line 445 def deleteMany( cnt = length ) cnt.times do @lines.delete_at( @curline ) end self end |
- (Object) edit(file, backup = true)
Create new file based on old. Original file is backupped.
54 55 56 57 |
# File 'lib/rupat.rb', line 54 def edit( file, backup = true ) @backup = backup create( file, file ) end |
- (Object) excursion { ... }
Perform operations in block, but revert back to original position after block completion.
90 91 92 93 94 95 |
# File 'lib/rupat.rb', line 90 def excursion( &blk ) startline = @curline ret = instance_eval( &blk ) @curline = startline ret end |
- (Object) findBackward(re) Also known as: bfind
Find regexp backwards. Exception is generated, if pattern is not found.
263 264 265 |
# File 'lib/rupat.rb', line 263 def findBackward( re ) findCommon( re, false ) end |
- (Boolean) findBackward?(re) Also known as: bfind?
Find regexp backward.
272 273 274 |
# File 'lib/rupat.rb', line 272 def findBackward?( re ) findCommon( re, false, false ) end |
- (Integer) findBlock(re1, re2)
Return pair of line numbers that match the start/end regexps.
287 288 289 290 291 292 293 |
# File 'lib/rupat.rb', line 287 def findBlock( re1, re2 ) findCommon( re1, true ) l1 = line findCommon( re2, true ) l2 = line [ l1, l2 ] end |
- (Object) findForward(re) Also known as: find, ffind
Find regexp forwards. Exception is generated, if pattern is not found.
237 238 239 |
# File 'lib/rupat.rb', line 237 def findForward( re ) findCommon( re, true ) end |
- (Boolean) findForward?(re) Also known as: find?, ffind?
Find regexp forward.
246 247 248 |
# File 'lib/rupat.rb', line 246 def findForward?( re ) findCommon( re, true, false ) end |
- (Object) forward(count = 1) Also known as: step, down, inc
Forward line (or more).
169 170 171 172 173 |
# File 'lib/rupat.rb', line 169 def forward( count = 1 ) @curline += count normalizeCurline self end |
- (String) get(line = nil)
Return line content.
306 307 308 |
# File 'lib/rupat.rb', line 306 def get( line = nil ) @lines[ lineRef( line ) ] end |
- (Array<String>) getMany(l1 = nil, l2 = nil)
Return content of lines bounded by start/end markers (inclusive).
317 318 319 320 |
# File 'lib/rupat.rb', line 317 def getMany( l1 = nil, l2 = nil ) l1, l2 = linePairRef( l1, l2 ) @lines[ lineRef( l1 )..lineRef( l2 ) ] end |
- (Object) goto(line = 0) Also known as: jump
Goto line.
102 103 104 105 106 |
# File 'lib/rupat.rb', line 102 def goto( line = 0 ) @curline = line normalizeCurline self end |
- (Object) goto1(line = 1)
Goto (text editor) line.
113 114 115 116 117 |
# File 'lib/rupat.rb', line 113 def goto1( line = 1 ) @curline = line-1 normalizeCurline self end |
- (Object) gotoEnd Also known as: jumpEnd
Goto line after last line. User can append content starting with this line.
153 154 155 156 |
# File 'lib/rupat.rb', line 153 def gotoEnd @curline = @lines.length self end |
- (Object) gotoFirst Also known as: jumpFirst
Goto first line.
135 136 137 |
# File 'lib/rupat.rb', line 135 def gotoFirst goto end |
- (Object) gotoForce(line = 0)
Goto line without checking for line number validity.
NOTE: User has to be aware of out-of-bound indexing issues.
126 127 128 129 |
# File 'lib/rupat.rb', line 126 def gotoForce( line = 0 ) @curline = line self end |
- (Object) gotoLast Also known as: jumpLast
Goto last line.
143 144 145 146 |
# File 'lib/rupat.rb', line 143 def gotoLast @curline = @lines.length-1 self end |
- (Object) insert(str = "")
Insert a line at current position.
386 387 388 389 |
# File 'lib/rupat.rb', line 386 def insert( str = "" ) @lines.insert( @curline, str ) self end |
- (Object) insertFile(file)
Insert file content to current position.
473 474 475 476 477 478 479 |
# File 'lib/rupat.rb', line 473 def insertFile( file ) excursion do lines = readclean( file ) insertMany( lines ) end self end |
- (Object) insertMany(content)
Insert multiple lines at current position.
396 397 398 399 400 401 402 403 404 405 |
# File 'lib/rupat.rb', line 396 def insertMany( content ) lines = manyLineContent( content ) excursion do insert( lines[0] ) lines[1..-1].each do |s| append( s ) end end self end |
- (Object) last
Return last line number.
227 228 229 |
# File 'lib/rupat.rb', line 227 def last length-1 end |
- (Object) length
Return lines length.
512 513 514 |
# File 'lib/rupat.rb', line 512 def length @lines.length end |
- (Object) line
Return current line number. First line is 0.
214 215 216 |
# File 'lib/rupat.rb', line 214 def line @curline end |
- (Object) line1
Return current line number + one, i.e. matches what for example an text editor would show. First line is 1.
221 222 223 |
# File 'lib/rupat.rb', line 221 def line1 @curline + 1 end |
- (Object) next
Return next line number from current. Current line is not changed.
201 202 203 |
# File 'lib/rupat.rb', line 201 def next @curline+1 end |
- (Object) open(file)
Open a file in source mode (read only).
63 64 65 |
# File 'lib/rupat.rb', line 63 def open( file ) create( file, nil ) end |
- (Object) paste(err = true)
341 342 343 344 345 346 347 348 349 |
# File 'lib/rupat.rb', line 341 def paste( err = true ) if err unless @copybuf raise RupatPasteError, "Copy buffer was empty!" end end insertMany( @copybuf ) if @copybuf self end |
- (Object) prev
Return prev line number from current. Current line is not changed.
208 209 210 |
# File 'lib/rupat.rb', line 208 def prev @curline-1 end |
- (Object) print(fh = STDOUT)
Print file content.
591 592 593 594 595 596 |
# File 'lib/rupat.rb', line 591 def print( fh = STDOUT ) @lines.each do |l| fh.puts l end self end |
- (Object) replace {|get| ... } Also known as: subs
Replace the current line content (i.e. get&set).
373 374 375 376 |
# File 'lib/rupat.rb', line 373 def replace( &action ) set( yield( get ) ) self end |
- (Object) replaceAll(re, str)
Replace all occurance of re with str.
487 488 489 490 491 492 |
# File 'lib/rupat.rb', line 487 def replaceAll( re, str ) @lines.each_index do |idx| @lines[idx].gsub!( re, str ) end self end |
- (Object) replaceWithin(re, str, l1 = nil, l2 = nil)
Replace all occurance of re with str within given range.
502 503 504 505 506 507 508 |
# File 'lib/rupat.rb', line 502 def replaceWithin( re, str, l1 = nil, l2 = nil ) l1, l2 = linePairRef( l1, l2 ) @lines[l1..l2].each_index do |idx| @lines[idx].gsub!( re, str ) end self end |
- (Object) save(file = @newFile)
Save edits. File handle is not closed. Use #close for complete file closing.
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'lib/rupat.rb', line 558 def save( file = @newFile ) raise RupatTypeError, "Can't use \"nil\" for file save!" unless file close = true case file when String if @orgFile.class == String # Open with original file permissions. fh = File.open( file, "w", File.stat( @orgFile ).mode ) else # Open with default file permissions. fh = File.open( file, "w" ) end when File fh = file close = false else raise RupatTypeError, "Can't use \"#{file.class}\" type for file save!" end @lines.each do |l| fh.syswrite( l + "\n" ) end fh.close if close end |
- (Object) set(str, line = nil)
Set line content.
357 358 359 360 |
# File 'lib/rupat.rb', line 357 def set( str, line = nil ) @lines[ lineRef( line ) ] = str self end |
- (Object) update { ... }
Update the content using proc block.
80 81 82 |
# File 'lib/rupat.rb', line 80 def update( &blk ) instance_eval &blk end |
- (Object) use(lines)
Use set of lines for editing.
71 72 73 74 |
# File 'lib/rupat.rb', line 71 def use( lines ) @lines = lines @curline = 0 end |