Module: RupatMod

Included in:
Rupat, RupatAlias
Defined in:
lib/rupat.rb

Overview

RupatMod contains the core functionality of Rupat.

Defined Under Namespace

Classes: RupatError, RupatInvalidLineError, RupatPasteError, RupatSearchError, RupatTypeError

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

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.

Parameters:

  • str (String) (defaults to: "")

    Content for the appended line.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • content (Object)

    Content (See: #manyLineContent).

Returns:

  • (Object)

    Self.



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).

Parameters:

  • count (Integer) (defaults to: 1)

    Number of lines to step backwards.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • file (String) (defaults to: @newFile)

    File to save edits to.

Returns:

  • (String, NilClass)

    Backup file name if backup made, otherwise nil.



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.

Parameters:

  • l1 (Integer) (defaults to: nil)

    Line marker 1 (See: #linePairRef).

  • l2 (Integer) (defaults to: nil)

    Line marker 2 (See: #linePairRef).

Returns:

  • (Array<String>)

    Line(s) content.



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).

Parameters:

  • orgFile (String, File)

    Source file.

  • newFile (String) (defaults to: nil)

    Destination file.



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.

Parameters:

  • l1 (Integer) (defaults to: nil)

    Line marker 1 (See: #linePairRef).

  • l2 (Integer) (defaults to: nil)

    Line marker 2 (See: #linePairRef).

Returns:

  • (Object)

    Self.



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.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • cnt (Integer) (defaults to: length)

    Number of lines to delete.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • file (String, File)

    Source file.

  • backup (Boolean) (defaults to: true)

    Create backup from the source (original) file.



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.

Yields:

  • blk Proc to execute on file content.

Returns:

  • (Object)

    Value of the block.



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.

Parameters:

  • re (Regexp)

    Search pattern regexp.

Returns:

  • Self if 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.

Parameters:

  • re (Regexp)

    Search pattern regexp.

Returns:

  • (Boolean)

    Self if pattern found, nil otherwise.



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.

Parameters:

  • re1 (Regexp)

    Range start marking Regexp.

  • re2 (Regexp)

    Range end marking Regexp.

Returns:

  • (Integer, Integer)

    Range start/end tuplet.



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.

Parameters:

  • re (Regexp)

    Search pattern regexp.

Returns:

  • Self if 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.

Parameters:

  • re (Regexp)

    Search pattern regexp.

Returns:

  • (Boolean)

    Self if pattern found, nil otherwise.



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).

Parameters:

  • count (Integer) (defaults to: 1)

    Number of lines to step forwards.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • line (Integer, NilClass) (defaults to: nil)

    Indexed or current line.

Returns:

  • (String)

    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).

Parameters:

  • l1 (Integer) (defaults to: nil)

    Line marker 1 (See: #linePairRef).

  • l2 (Integer) (defaults to: nil)

    Line marker 2 (See: #linePairRef).

Returns:

  • (Array<String>)

    Line(s) content.



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.

Parameters:

  • line (Integer) (defaults to: 0)

    Line to access.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • line (Integer) (defaults to: 1)

    Line to access (updated to <line>-1).

Returns:

  • (Object)

    Self.



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.

Returns:

  • (Object)

    Self.



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.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • line (Integer) (defaults to: 0)

    Line to access.

Returns:

  • (Object)

    Self.



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.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • str (String) (defaults to: "")

    Content for the inserted line.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • file (String)

    Name of file to insert.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • content (Object)

    Content (See: #manyLineContent).

Returns:

  • (Object)

    Self.



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).

Parameters:

  • file (String, File)

    Source file.



63
64
65
# File 'lib/rupat.rb', line 63

def open( file )
    create( file, nil )
end

- (Object) paste(err = true)

Insert content from #copybuf (i.e. from #copy) to current position.

Parameters:

  • err (Boolean) (defaults to: true)

    Generated exception for empty copybuf if 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

Print file content.

Parameters:

  • fh (IO) (defaults to: STDOUT)

    IO stream for printout.



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).

Examples:

r.replace do |c|
   c.gsub( /foo/, 'bar'
end

Yields:

  • (get)

    Actions to perform for the content. Current content is given as argument for the block.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • re (Regexp)

    Regexp to match replace lines to.

  • str (String)

    New content.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • re (Regexp)

    Regexp to match replace lines to.

  • str (String)

    New content.

  • l1 (Integer) (defaults to: nil)

    Line marker 1 (See: #linePairRef).

  • l2 (Integer) (defaults to: nil)

    Line marker 2 (See: #linePairRef).

Returns:

  • (Object)

    Self.



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.

Parameters:

  • file (String, File) (defaults to: @newFile)

    File to save edits to. If type is File, stream is not closed.

Raises:



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.

Parameters:

  • str (String)

    New content for line.

  • line (Integer) (defaults to: nil)

    Index to modified line.

Returns:

  • (Object)

    Self.



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.

Yields:

  • blk Proc to execute on file content.



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.

Parameters:

  • lines (Array<String>)

    Lines to use.



71
72
73
74
# File 'lib/rupat.rb', line 71

def use( lines )
    @lines = lines
    @curline = 0
end