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

#copybufObject (readonly)

Copy buffer.



25
26
27
# File 'lib/rupat.rb', line 25

def copybuf
  @copybuf
end

#linesObject (readonly)

Array of lines including current content.



22
23
24
# File 'lib/rupat.rb', line 22

def lines
  @lines
end

#newFileObject

Created file reference.



31
32
33
# File 'lib/rupat.rb', line 31

def newFile
  @newFile
end

#orgFileObject

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.

Parameters:

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

    Content for the appended line.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • content (Object)

    Content (See: #manyLineContent).

Returns:

  • (Object)

    Self.



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

Parameters:

  • count (Integer) (defaults to: 1)

    Number of lines to step backwards.

Returns:

  • (Object)

    Self.



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.

Parameters:

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

    File to save edits to.

Returns:

  • (String, NilClass)

    Backup file name if backup made, otherwise nil.



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.

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.



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

Parameters:

  • orgFile (String, File)

    Source file.

  • newFile (String) (defaults to: nil)

    Destination file.



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.

Parameters:

  • l1 (Integer) (defaults to: nil)

    Line marker 1 (See: #linePairRef).

  • l2 (Integer) (defaults to: nil)

    Line marker 2 (See: #linePairRef).

Returns:

  • (Object)

    Self.



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

#deleteObject

Delete current line.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • cnt (Integer) (defaults to: length)

    Number of lines to delete.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • file (String, File)

    Source file.

  • backup (Boolean) (defaults to: true)

    Create backup from the source (original) file.



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.

Yields:

  • blk Proc to execute on file content.

Returns:

  • (Object)

    Value of the block.



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.

Parameters:

  • re (Regexp)

    Search pattern regexp.

Returns:

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

Parameters:

  • re (Regexp)

    Search pattern regexp.

Returns:

  • (Boolean)

    Self if pattern found, nil otherwise.



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.

Parameters:

  • re1 (Regexp)

    Range start marking Regexp.

  • re2 (Regexp)

    Range end marking Regexp.

Returns:

  • (Integer, Integer)

    Range start/end tuplet.



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.

Parameters:

  • re (Regexp)

    Search pattern regexp.

Returns:

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

Parameters:

  • re (Regexp)

    Search pattern regexp.

Returns:

  • (Boolean)

    Self if pattern found, nil otherwise.



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

Parameters:

  • count (Integer) (defaults to: 1)

    Number of lines to step forwards.

Returns:

  • (Object)

    Self.



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.

Parameters:

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

    Indexed or current line.

Returns:

  • (String)

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

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.



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.

Parameters:

  • line (Integer) (defaults to: 0)

    Line to access.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • line (Integer) (defaults to: 1)

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

Returns:

  • (Object)

    Self.



117
118
119
120
121
# File 'lib/rupat.rb', line 117

def goto1( line = 1 )
    @curline = line-1
    normalizeCurline
    self
end

#gotoEndObject Also known as: jumpEnd

Goto line after last line. User can append content starting with this line.

Returns:

  • (Object)

    Self.



157
158
159
160
# File 'lib/rupat.rb', line 157

def gotoEnd
    @curline = @lines.length
    self
end

#gotoFirstObject Also known as: jumpFirst

Goto first line.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • line (Integer) (defaults to: 0)

    Line to access.

Returns:

  • (Object)

    Self.



130
131
132
133
# File 'lib/rupat.rb', line 130

def gotoForce( line = 0 )
    @curline = line
    self
end

#gotoLastObject Also known as: jumpLast

Goto last line.

Returns:

  • (Object)

    Self.



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.

Parameters:

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

    Content for the inserted line.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • file (String)

    Name of file to insert.

Returns:

  • (Object)

    Self.



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.

Parameters:

  • content (Object)

    Content (See: #manyLineContent).

Returns:

  • (Object)

    Self.



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

#lastObject

Return last line number.



231
232
233
# File 'lib/rupat.rb', line 231

def last
    length-1
end

#lengthObject

Return lines length.



516
517
518
# File 'lib/rupat.rb', line 516

def length
    @lines.length
end

#lineObject

Return current line number. First line is 0.



218
219
220
# File 'lib/rupat.rb', line 218

def line
    @curline
end

#line1Object

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

#nextObject

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

Parameters:

  • file (String, File)

    Source file.



67
68
69
# File 'lib/rupat.rb', line 67

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

#paste(err = true) ⇒ Object

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

Parameters:

  • err (Boolean) (defaults to: true)

    Generated exception for empty copybuf if true.



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

#prevObject

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 file content.

Parameters:

  • fh (IO) (defaults to: STDOUT)

    IO stream for printout.



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

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.



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.

Parameters:

  • re (Regexp)

    Regexp to match replace lines to.

  • str (String)

    New content.

Returns:

  • (Object)

    Self.



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.

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.



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.

Parameters:

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

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

Raises:



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.

Parameters:

  • str (String)

    New content for line.

  • line (Integer) (defaults to: nil)

    Index to modified line.

Returns:

  • (Object)

    Self.



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.

Yields:

  • blk Proc to execute on file content.



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.

Parameters:

  • lines (Array<String>)

    Lines to use.



75
76
77
78
# File 'lib/rupat.rb', line 75

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