Method: Color.colorize

Defined in:
lib/xiki/color.rb

.colorize(char = nil) ⇒ Object



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
# File 'lib/xiki/color.rb', line 29

def self.colorize char=nil
  char ||= Keys.input(:chars=>1, :prompt=>'Enter first letter of color: ')
  char = char.to_s
  # If h, just show all colors
  case char
  when "l"
    # We want there to be only one "light" line per file, so delete existing
    overlays = $el.overlays_in(View.top, View.bottom)   # Get all overlays
    overlays.to_a.reverse.each do |o|   # Loop through and copy all
      if $el.overlay_get(o, :face).to_s == "color-rb-light"
        $el.delete_overlay(o)
      end
    end

  when "s"   # Search in all buffers for marked lines

    prefix = Keys.prefix

    if prefix.nil?
      light = $el.overlays_in(View.top, View.bottom).to_a.find{|o| $el.overlay_get(o, :face).to_s == "color-rb-light"}
      return View.to($el.overlay_start(light)) if light
      # Else, contine on and do Color.search
    end

    return Launcher.open("- Color.search/") if Keys.prefix == 8

    # Presumably :u
    Launcher.open("- Color.search 'light'/")
    return

  when "o"   # Outline of marked lines
    res = self.get_marked_lines
    res.gsub! /^/, "    | "

    file = View.file
    path = file ?
      "- #{File.expand_path(file).sub(/(.+)\//, "\\1/\n  - ")}\n" :
      "- buffer #{View.name}/\n"

    View.to_buffer("*outline of marked in #{path}")
    View.clear;  Notes.mode
    View.insert path
    if res == "    | "
      return View.insert "    - Nothing was marked in this file!"
    end
    View.insert res
    Keys.clear_prefix
    View.to_line 3
    Line.to_beginning

    Tree.search :left=> Line.left, :number_means_enter=>true

    return

  when "c"   # Copy marked lines

    res = self.get_marked_lines

    Clipboard['0'] = res

  when "h"   # Hilight
    prefix = Keys.prefix :clear=>1

    ignore, left, right = View.txt_per_prefix prefix, :just_positions=>1, :default_is_line=>1

    prefix == :u ?
      Effects.glow(:color=>:rainbow, :times=>4) :
      Effects.glow(:what=>[left, right], :times=>4)

    #     when "h"   # Hide
    #       Hide.hide_unless_block { |l, bol, eol|
    #         # Whether current line contains an overlay of this color
    #         $el.overlays_in(bol, eol).to_a.find{ |o|
    #           $el.overlay_get(o, :face).to_s =~ /^color-rb-/
    #         }
    #       }
    #       recenter(-3)
    #       Hide.search
    #       return
  when "n"   # to next marker
    #       Keys.prefix_times do
    pos = $el.next_overlay_change(View.cursor)
    #       end
    # If no overlay, may be at end, so continue on
    pos = $el.next_overlay_change(pos) unless $el.overlays_at(pos)
    return View.to(pos)
  when "p"   # to next marker
    pos = $el.previous_overlay_change(View.cursor)
    pos = $el.previous_overlay_change(pos-2) if $el.overlays_at(pos-2)
    return View.to pos
  when "d"
    overlays = $el.overlays_at($el.next_overlay_change($el.point_at_bol - 1))
    return View.beep "- No highlights after cursor!" if ! overlays
    return $el.delete_overlay(overlays[0])
  when "k"

    if Keys.prefix_u   # Don't delete map mark
      return $el.remove_overlays
    end
    overlays = $el.overlays_in(View.top, View.bottom)   # Get all overlays
    overlays.to_a.reverse.each do |o|   # Loop through and copy all
      if $el.overlay_get(o, :face).to_s != "color-rb-light"
        $el.delete_overlay(o)
      end
    end
    return

  when "a"
    return self.alternating
  end

  case Keys.prefix
  when :u   # If C-u, use region
    left, right = View.range
  when nil   # If nothing, do line
    left, right = Line.left, Line.right+1
  else   # Else, get N lines
    txt, left, right = View.txt_per_prefix
  end

  if ! @@colors[char]
    return View.message "No char color for '#{char}'"
  end

  over = $el.make_overlay(left, right)
  $el.overlay_put over, :face, @@colors[char]
end