Class: Hide

Inherits:
Object show all
Defined in:
lib/xiki/hide.rb

Constant Summary collapse

@@visible =
{}

Class Method Summary collapse

Class Method Details

.already_hiddenObject



23
24
25
# File 'lib/xiki/hide.rb', line 23

def self.already_hidden
  @@already_hidden
end

.char_from_userObject



85
86
87
88
89
90
# File 'lib/xiki/hide.rb', line 85

def self.char_from_user
  c = $el.read_char_exclusive
  # Treat C-0 - C-9 the same as 0-9
  c -= 67108864 if(c >= 67108912 && c <= 67108921)
  "#{c.chr}"
end

.hide_by_indent(indent = nil) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/xiki/hide.rb', line 205

def self.hide_by_indent indent=nil
  indent ||= Keys.prefix
  indent = -1 if indent == :u

  # If no prefix, use indent of current line
  if indent.nil?
    indent = Line.matches(/^ */).size
    # If currently indented to that level, go one deeper
    if $el.elvar.selective_display && indent == ($el.elvar.selective_display - 1)
      indent += 2
    end
  end

  $el.set_selective_display(indent + 1)
end

.hide_unless(search, options = {}) ⇒ Object

self.reset



13
14
15
16
17
# File 'lib/xiki/hide.rb', line 13

def self.hide_unless search, options={}
  self.hide_unless_block(options) do |l, bol, eol|
    l =~ search
  end
end

.hide_unless_block(options = {}, &block) ⇒ Object



27
28
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
# File 'lib/xiki/hide.rb', line 27

def self.hide_unless_block options={}, &block

  #    elvar.line_move_ignore_invisible = true
  #     @@already_hidden = {}
  #     @@visible = []

  @@visible = []

  # Unless we can optimize by assuming hidden regions are ok
  unless options[:optimize]
    self.reset
    # Lisp funtion defined below
    l = $el.hide_find_overlays.to_a
    l.each { |i| @@already_hidden[i] = true }
  end

  t = $el.buffer_string
  lines = t.split "\n"
  bol = 1
  left = 1
  in_non_match = false

  if options[:include_first]   # Optionally skip top line
    l = lines.shift
    eol = bol + l.size + 1
    left = eol
    bol = eol
  end

  lines.each do |l|   # For each line
    eol = bol + l.size + 1
    #      if (l =~ search) && (! @@already_hidden[bol])
    if (yield(l, bol, eol)) && (! @@already_hidden[bol])
      @@visible << bol
      # If we're at the end of non-match area, hide
      if in_non_match
        @@areas_to_hide << [left, bol]
        in_non_match = false
      end
      left = eol
    else
      in_non_match = true
      @@already_hidden[bol] = true
    end
    bol = eol
  end

  @@areas_to_hide << [left, bol] if in_non_match

  # Construct and run lisp to hide it
  elisp = "(progn\n"
  @@areas_to_hide.each do |i|
    elisp += " (overlay-put (make-overlay #{i[0]} #{i[1]}) 'invisible 'hide)\n"
  end
  $el.el4r_lisp_eval elisp + ")"

end

.initObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/xiki/hide.rb', line 221

def self.init

  return if ! $el

  # Define lisp function to get list of displayed lines
  # In case something has been done to change them
  $el.el4r_lisp_eval %`
    (defun hide-find-overlays ()
      (save-excursion
        (beginning-of-buffer)
        (let (found (res ()))
          (while (not (eq (point) (point-max)))
            (setq found nil)
            ; Save if not hidden
            (dolist (o (overlays-at (point)))
              (if (overlay-get o 'invisible)
                (setq found t)
              )
            )
            (if found
              (push (point) res))
            (forward-line)
          )
          (reverse res)
        )
      )
    )
  `
end

.keysObject



166
167
# File 'lib/xiki/hide.rb', line 166

def self.keys
end


5
6
7
8
9
# File 'lib/xiki/hide.rb', line 5

def self.menu
  "
  | TODO: add docs about hiding.
  "
end

.resetObject



195
196
197
198
199
# File 'lib/xiki/hide.rb', line 195

def self.reset
  @@areas_to_hide = []
  # Make this be stored per page, not globally
  @@already_hidden = {}
end

.revealObject

Reveals all hidden



252
253
254
255
256
# File 'lib/xiki/hide.rb', line 252

def self.reveal
  $el.widen
  self.show
  self.hide_by_indent :u   # If hidden by indent
end

.search(search = nil, options = {}) ⇒ Object



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
156
157
158
159
160
161
162
163
164
# File 'lib/xiki/hide.rb', line 92

def self.search search=nil, options={}
  if search
    if search.class == String
      search = /#{Regexp.escape(search)}/i
    end
    self.hide_unless search, options
  else
    search = ""
  end

  $el.message "Hide.search for: #{search}"
  ch = self.char_from_user

  first_search = true

  while true
    break unless ch =~ /[-a-zA-Z ._=@\/\\#,;*<>-]/
    search += ch
    $el.message "hide-search for: #{search}"

    # If a tab, clear search and keep going
    if ch == ","
      search = ""
      ch = self.char_from_user
      next
    end

    self.hide_unless /#{Regexp.escape(search)}/i, options
    $el.recenter -3

    # Don't re-check visible regions from now on
    if first_search
      options.merge!(:optimize => true)
      first_search = false
    end

    # If only one left, go to it
    if @@visible.size == 1 && options[:expand_when_one]
      ch = "1"
      break
    end
    ch = self.char_from_user
  end

  ch = 1 if ch == "\t"   # If a tab, treat it like C-1

  # If num
  if 1 <= ch.to_i && ch.to_i <= 9
    if @@visible.size > 0
      if @@visible[ch.to_i - 1]
        $el.goto_char @@visible[ch.to_i - 1]
      else
        $el.goto_char @@visible[-1]
      end
    else
      $el.goto_line ch.to_i
    end
    #      next_line -1
    self.show
    $el.recenter 0
    # Run enter if option was passed
    if options[:press_enter]
      $el.command_execute "\C-m"
    end

  # Show all if they typed C-a
  elsif ch == "\001"
    self.show
  # Else, if control character, execute it and exit
  elsif "\C-a" <= ch && ch <= "\C-z"
    $el.command_execute ch unless ch == "\C-m"
  end
end

.showObject

Shows things hidden by .hide



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/xiki/hide.rb', line 170

def self.show

  # Move down to visible line (this doesn't seem to happen on its own)
  while( @@already_hidden[$el.point] )
    $el.forward_line
  end

  #    @@already_hidden = {}
  # Delete hidden overlays
  $el.el4r_lisp_eval %`
    (dolist (over (overlays-in (point-min) (point-max) ))
      (when
        (overlay-get over 'invisible)
        (delete-overlay over)
      )
    )
  `
  self.reset
  @@visible = []
end

.visibleObject



19
20
21
# File 'lib/xiki/hide.rb', line 19

def self.visible
  @@visible
end