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
-
#copybuf ⇒ Object
readonly
Copy buffer.
-
#lines ⇒ Object
readonly
Array of lines including current content.
-
#newFile ⇒ Object
Created file reference.
-
#orgFile ⇒ Object
Original file reference.
Instance Method Summary collapse
-
#[](range) ⇒ Object
Return lines content.
-
#append(str = "") ⇒ Object
Append line to current position.
-
#appendMany(content) ⇒ Object
Append lines to current position.
-
#backward(count = 1) ⇒ Object
(also: #up, #back, #dec)
Backwards line (or more).
-
#close(file = @newFile) ⇒ String, NilClass
Save edits and close file.
-
#copy(l1 = nil, l2 = nil) ⇒ Array<String>
Store content of lines bounded by start/end markers (inclusive) to copy-buffer.
-
#create(orgFile, newFile = nil) ⇒ Object
Create new file based on given name (or IO stream).
-
#cut(l1 = nil, l2 = nil) ⇒ Object
Delete specified lines (inclusive).
-
#delete ⇒ Object
Delete current line.
-
#deleteMany(cnt = length) ⇒ Object
Delete current line N times.
-
#edit(file, backup = true) ⇒ Object
Create new file based on old.
-
#excursion { ... } ⇒ Object
Perform operations in block, but revert back to original position after block completion.
-
#findBackward(re) ⇒ Object
(also: #bfind)
Find regexp backwards.
-
#findBackward?(re) ⇒ Boolean
(also: #bfind?)
Find regexp backward.
-
#findBlock(re1, re2) ⇒ Integer
Return pair of line numbers that match the start/end regexps.
-
#findForward(re) ⇒ Object
(also: #find, #ffind)
Find regexp forwards.
-
#findForward?(re) ⇒ Boolean
(also: #find?, #ffind?)
Find regexp forward.
-
#forward(count = 1) ⇒ Object
(also: #step, #down, #inc)
Forward line (or more).
-
#get(line = nil) ⇒ String
Return line content.
-
#getMany(l1 = nil, l2 = nil) ⇒ Array<String>
Return content of lines bounded by start/end markers (inclusive).
-
#goto(line = 0) ⇒ Object
(also: #jump)
Goto line.
-
#goto1(line = 1) ⇒ Object
Goto (text editor) line.
-
#gotoEnd ⇒ Object
(also: #jumpEnd)
Goto line after last line.
-
#gotoFirst ⇒ Object
(also: #jumpFirst)
Goto first line.
-
#gotoForce(line = 0) ⇒ Object
Goto line without checking for line number validity.
-
#gotoLast ⇒ Object
(also: #jumpLast)
Goto last line.
-
#insert(str = "") ⇒ Object
Insert a line at current position.
-
#insertFile(file) ⇒ Object
Insert file content to current position.
-
#insertMany(content) ⇒ Object
Insert multiple lines at current position.
-
#last ⇒ Object
Return last line number.
-
#length ⇒ Object
Return lines length.
-
#line ⇒ Object
Return current line number.
-
#line1 ⇒ Object
Return current line number + one, i.e.
-
#next ⇒ Object
Return next line number from current.
-
#open(file) ⇒ Object
Open a file in source mode (read only).
- #paste(err = true) ⇒ Object
-
#prev ⇒ Object
Return prev line number from current.
-
#print(fh = STDOUT) ⇒ Object
Print file content.
-
#replace {|get| ... } ⇒ Object
(also: #subs)
Replace the current line content (i.e. get&set).
-
#replaceAll(re, str) ⇒ Object
Replace all occurance of re with str.
-
#replaceWithin(re, str, l1 = nil, l2 = nil) ⇒ Object
Replace all occurance of re with str within given range.
-
#save(file = @newFile) ⇒ Object
Save edits.
-
#set(str, line = nil) ⇒ Object
Set line content.
-
#update { ... } ⇒ Object
Update the content using proc block.
-
#use(lines) ⇒ Object
Use set of lines for editing.
Instance Attribute Details
#copybuf ⇒ Object (readonly)
Copy buffer.
25 26 27 |
# File 'lib/rupat.rb', line 25 def copybuf @copybuf end |
#lines ⇒ Object (readonly)
Array of lines including current content.
22 23 24 |
# File 'lib/rupat.rb', line 22 def lines @lines end |
#newFile ⇒ Object
Created file reference.
31 32 33 |
# File 'lib/rupat.rb', line 31 def newFile @newFile end |
#orgFile ⇒ Object
Original file reference.
28 29 30 |
# File 'lib/rupat.rb', line 28 def orgFile @orgFile end |
Instance Method Details
#[](range) ⇒ Object
Return lines content.
301 302 303 |
# File 'lib/rupat.rb', line 301 def []( range ) @lines[ range ] end |
#append(str = "") ⇒ Object
Append line to current position.
416 417 418 419 420 |
# File 'lib/rupat.rb', line 416 def append( str = "" ) forward @lines.insert( @curline, str ) self end |
#appendMany(content) ⇒ Object
Append lines to current position.
427 428 429 430 431 432 433 |
# File 'lib/rupat.rb', line 427 def appendMany( content ) lines = manyLineContent( content ) lines.each do |s| append( s ) end self end |
#backward(count = 1) ⇒ Object Also known as: up, back, dec
Backwards line (or more).
184 185 186 187 188 |
# File 'lib/rupat.rb', line 184 def backward( count = 1 ) @curline -= count normalizeCurline self end |
#close(file = @newFile) ⇒ String, NilClass
Save edits and close file.
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/rupat.rb', line 526 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 |
#copy(l1 = nil, l2 = nil) ⇒ Array<String>
Store content of lines bounded by start/end markers (inclusive) to copy-buffer.
333 334 335 336 337 |
# File 'lib/rupat.rb', line 333 def copy( l1 = nil, l2 = nil ) l1, l2 = linePairRef( l1, l2 ) @copybuf = @lines[ lineRef( l1 )..lineRef( l2 ) ] self end |
#create(orgFile, newFile = nil) ⇒ Object
Create new file based on given name (or IO stream).
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rupat.rb', line 38 def create( orgFile, newFile = nil ) @orgFile = orgFile @newFile = newFile if @orgFile.class == String @lines = cleanlines( File.open( @orgFile ) ) else @lines = cleanlines( @orgFile ) end @curline = 0 end |
#cut(l1 = nil, l2 = nil) ⇒ Object
Delete specified lines (inclusive). Save content to copy-buffer.
462 463 464 465 466 467 468 469 470 |
# File 'lib/rupat.rb', line 462 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 |
#delete ⇒ Object
Delete current line.
439 440 441 442 |
# File 'lib/rupat.rb', line 439 def delete @lines.delete_at( @curline ) self end |
#deleteMany(cnt = length) ⇒ Object
Delete current line N times.
449 450 451 452 453 454 |
# File 'lib/rupat.rb', line 449 def deleteMany( cnt = length ) cnt.times do @lines.delete_at( @curline ) end self end |
#edit(file, backup = true) ⇒ Object
Create new file based on old. Original file is backupped.
58 59 60 61 |
# File 'lib/rupat.rb', line 58 def edit( file, backup = true ) @backup = backup create( file, file ) end |
#excursion { ... } ⇒ Object
Perform operations in block, but revert back to original position after block completion.
94 95 96 97 98 99 |
# File 'lib/rupat.rb', line 94 def excursion( &blk ) startline = @curline ret = instance_eval( &blk ) @curline = startline ret end |
#findBackward(re) ⇒ Object Also known as: bfind
Find regexp backwards. Exception is generated, if pattern is not found.
267 268 269 |
# File 'lib/rupat.rb', line 267 def findBackward( re ) findCommon( re, false ) end |
#findBackward?(re) ⇒ Boolean Also known as: bfind?
Find regexp backward.
276 277 278 |
# File 'lib/rupat.rb', line 276 def findBackward?( re ) findCommon( re, false, false ) end |
#findBlock(re1, re2) ⇒ Integer
Return pair of line numbers that match the start/end regexps.
291 292 293 294 295 296 297 |
# File 'lib/rupat.rb', line 291 def findBlock( re1, re2 ) findCommon( re1, true ) l1 = line findCommon( re2, true ) l2 = line [ l1, l2 ] end |
#findForward(re) ⇒ Object Also known as: find, ffind
Find regexp forwards. Exception is generated, if pattern is not found.
241 242 243 |
# File 'lib/rupat.rb', line 241 def findForward( re ) findCommon( re, true ) end |
#findForward?(re) ⇒ Boolean Also known as: find?, ffind?
Find regexp forward.
250 251 252 |
# File 'lib/rupat.rb', line 250 def findForward?( re ) findCommon( re, true, false ) end |
#forward(count = 1) ⇒ Object Also known as: step, down, inc
Forward line (or more).
173 174 175 176 177 |
# File 'lib/rupat.rb', line 173 def forward( count = 1 ) @curline += count normalizeCurline self end |
#get(line = nil) ⇒ String
Return line content.
310 311 312 |
# File 'lib/rupat.rb', line 310 def get( line = nil ) @lines[ lineRef( line ) ] end |
#getMany(l1 = nil, l2 = nil) ⇒ Array<String>
Return content of lines bounded by start/end markers (inclusive).
321 322 323 324 |
# File 'lib/rupat.rb', line 321 def getMany( l1 = nil, l2 = nil ) l1, l2 = linePairRef( l1, l2 ) @lines[ lineRef( l1 )..lineRef( l2 ) ] end |
#goto(line = 0) ⇒ Object Also known as: jump
Goto line.
106 107 108 109 110 |
# File 'lib/rupat.rb', line 106 def goto( line = 0 ) @curline = line normalizeCurline self end |
#goto1(line = 1) ⇒ Object
Goto (text editor) line.
117 118 119 120 121 |
# File 'lib/rupat.rb', line 117 def goto1( line = 1 ) @curline = line-1 normalizeCurline self end |
#gotoEnd ⇒ Object Also known as: jumpEnd
Goto line after last line. User can append content starting with this line.
157 158 159 160 |
# File 'lib/rupat.rb', line 157 def gotoEnd @curline = @lines.length self end |
#gotoFirst ⇒ Object Also known as: jumpFirst
Goto first line.
139 140 141 |
# File 'lib/rupat.rb', line 139 def gotoFirst goto end |
#gotoForce(line = 0) ⇒ Object
Goto line without checking for line number validity.
NOTE: User has to be aware of out-of-bound indexing issues.
130 131 132 133 |
# File 'lib/rupat.rb', line 130 def gotoForce( line = 0 ) @curline = line self end |
#gotoLast ⇒ Object Also known as: jumpLast
Goto last line.
147 148 149 150 |
# File 'lib/rupat.rb', line 147 def gotoLast @curline = @lines.length-1 self end |
#insert(str = "") ⇒ Object
Insert a line at current position.
390 391 392 393 |
# File 'lib/rupat.rb', line 390 def insert( str = "" ) @lines.insert( @curline, str ) self end |
#insertFile(file) ⇒ Object
Insert file content to current position.
477 478 479 480 481 482 483 |
# File 'lib/rupat.rb', line 477 def insertFile( file ) excursion do lines = cleanlines( File.open( file ) ) insertMany( lines ) end self end |
#insertMany(content) ⇒ Object
Insert multiple lines at current position.
400 401 402 403 404 405 406 407 408 409 |
# File 'lib/rupat.rb', line 400 def insertMany( content ) lines = manyLineContent( content ) excursion do insert( lines[0] ) lines[1..-1].each do |s| append( s ) end end self end |
#last ⇒ Object
Return last line number.
231 232 233 |
# File 'lib/rupat.rb', line 231 def last length-1 end |
#length ⇒ Object
Return lines length.
516 517 518 |
# File 'lib/rupat.rb', line 516 def length @lines.length end |
#line ⇒ Object
Return current line number. First line is 0.
218 219 220 |
# File 'lib/rupat.rb', line 218 def line @curline end |
#line1 ⇒ Object
Return current line number + one, i.e. matches what for example an text editor would show. First line is 1.
225 226 227 |
# File 'lib/rupat.rb', line 225 def line1 @curline + 1 end |
#next ⇒ Object
Return next line number from current. Current line is not changed.
205 206 207 |
# File 'lib/rupat.rb', line 205 def next @curline+1 end |
#open(file) ⇒ Object
Open a file in source mode (read only).
67 68 69 |
# File 'lib/rupat.rb', line 67 def open( file ) create( file, nil ) end |
#paste(err = true) ⇒ Object
345 346 347 348 349 350 351 352 353 |
# File 'lib/rupat.rb', line 345 def paste( err = true ) if err unless @copybuf raise RupatPasteError, "Copy buffer was empty!" end end insertMany( @copybuf ) if @copybuf self end |
#prev ⇒ Object
Return prev line number from current. Current line is not changed.
212 213 214 |
# File 'lib/rupat.rb', line 212 def prev @curline-1 end |
#print(fh = STDOUT) ⇒ Object
Print file content.
589 590 591 592 593 594 |
# File 'lib/rupat.rb', line 589 def print( fh = STDOUT ) @lines.each do |l| fh.puts l end self end |
#replace {|get| ... } ⇒ Object Also known as: subs
Replace the current line content (i.e. get&set).
377 378 379 380 |
# File 'lib/rupat.rb', line 377 def replace( &action ) set( yield( get ) ) self end |
#replaceAll(re, str) ⇒ Object
Replace all occurance of re with str.
491 492 493 494 495 496 |
# File 'lib/rupat.rb', line 491 def replaceAll( re, str ) @lines.each_index do |idx| @lines[idx].gsub!( re, str ) end self end |
#replaceWithin(re, str, l1 = nil, l2 = nil) ⇒ Object
Replace all occurance of re with str within given range.
506 507 508 509 510 511 512 |
# File 'lib/rupat.rb', line 506 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 |
#save(file = @newFile) ⇒ Object
Save edits. File handle is not closed. Use #close for complete file closing.
556 557 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 |
# File 'lib/rupat.rb', line 556 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 |
#set(str, line = nil) ⇒ Object
Set line content.
361 362 363 364 |
# File 'lib/rupat.rb', line 361 def set( str, line = nil ) @lines[ lineRef( line ) ] = str self end |
#update { ... } ⇒ Object
Update the content using proc block.
84 85 86 |
# File 'lib/rupat.rb', line 84 def update( &blk ) instance_eval &blk end |
#use(lines) ⇒ Object
Use set of lines for editing.
75 76 77 78 |
# File 'lib/rupat.rb', line 75 def use( lines ) @lines = lines @curline = 0 end |