Class: Bookmarks

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

Class Method Summary collapse

Class Method Details

.[](path) ⇒ Object

Expand $foo paths in strings. Expects strings to bee bookmark names, and replaces with corresponding paths.



201
202
203
# File 'lib/xiki/bookmarks.rb', line 201

def self.[] path
  self.expand(path)
end

.[]=(bookmark, path) ⇒ Object



81
82
83
84
85
86
# File 'lib/xiki/bookmarks.rb', line 81

def self.[]= bookmark, path
  $el.with(:save_window_excursion) do
    View.open path
    Bookmarks.set bookmark
  end
end

.buffer_bookmark(name) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/xiki/bookmarks.rb', line 133

def self.buffer_bookmark name
  # Load bookmarks.yml
  bookmarks_yml = File.expand_path("~/bookmarks.yml")
  return nil unless File.exists?(bookmarks_yml)
  bookmarks = YAML::load IO.read(bookmarks_yml)
  bookmarks = [] unless bookmarks

  # Get buffer name
  found = bookmarks.find {|bm| bm[0] == name}
  return nil unless found

  unless View.buffer_open?(found[1])
    return :buffer_not_open
  end
  # Do nothing if already open

  found[1]
end

.collapse(path) ⇒ Object

Insert $bookmark into to path if it contains a bookmarked path



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/xiki/bookmarks.rb', line 258

def self.collapse path
  if ! @bookmarks_cache
    @bookmarks_cache = []
    # TODO: pull this list out and make configurable
    %w[a tr p n x 18].each do |name|

      bmpath = $el.bookmark_get_filename(name)
      next unless bmpath
      bmpath.sub!(/[^\/]+$/, "")

      @bookmarks_cache << [name, bmpath]
    end
  end

  @bookmarks_cache.each do |name, bmpath|
    next unless path =~ /^#{bmpath}/
    return path.sub(bmpath, "$#{name}/")
  end
  return path
end

.dir_only(path) ⇒ Object

Remove file from the end (if not dir)



280
281
282
# File 'lib/xiki/bookmarks.rb', line 280

def self.dir_only path
  path.sub(/\/[^\/]+\.[^\/]+$/, '')
end

.expand(path, options = {}) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
250
251
252
253
254
255
# File 'lib/xiki/bookmarks.rb', line 205

def self.expand path, options={}
  if options[:just_bookmark]   # If only a bookmark, just expand it
    return $el.bookmark_get_filename(path)
  end

  # If $xxx found
  if path =~ /^\$([._a-zA-Z0-9-]+)([\\\/]?)(.*)/
    bm, slash, rest = $1, $2, $3

    bm_orig = bm
    # Expand bookmark
    if ["x", "xiki"].member? bm
      bm = Xiki.dir
    else
      bm = $el.bookmark_get_filename(bm)
    end

    if bm.nil?
      bm =
        if bm_orig == "t"
          "#{File.expand_path("~")}/todo.notes"
        elsif bm_orig == "f"
          "#{File.expand_path("~")}/files.notes"
        end
    end
    return path if bm.nil?
    # If a slash, cut off filename if there is one (only dir is wanted)
    if options[:file_ok]   # Put slash back if there was one
      bm << "/" if bm !~ /\/$/ && slash.any?
    elsif slash.any?
      bm.sub! /[^\\\/]+$/, ""
    end

    path = "#{bm}#{rest}"

    # Expand ~/ if it has it

    path = View.expand_path(path)  if path =~ /^~/

    path

  elsif path =~ /^~\//  # If home dir, expand
    # Expand ~/ if it has it
    View.expand_path(path)

  elsif options[:absolute] || path =~ /^\.+(\/|$)/  # If relative path, expand
    View.expand_path(path)
  else
    path
  end
end

.go(bookmark = nil, options = {}) ⇒ Object

If string passed, go to file+point of bookmark. If nothing passed, go to file of user-prompted text If no args passed, get bookmark from user and jump there



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/xiki/bookmarks.rb', line 157

def self.go bookmark=nil, options={}
  #el4r_lisp_eval "(require 'bookmark)(bookmark-maybe-load-default-file)"
  # If arg is a symbol, use it as the prefix
  prefix_to_bm = ""
  if bookmark && bookmark.class == Symbol
    prefix_to_bm = bookmark.to_s
  elsif bookmark && bookmark.class == String
    keys = bookmark
    #       return self.jump(bookmark.sub(/^\$/, ""))
  end

  # Use input from user or "default"
  keys ||= Keys.input(:timed => true, :prompt => "Enter bookmark to jump to: ") || "0"

  # Open file or jump to if already open
  path = $el.bookmark_get_filename( "#{prefix_to_bm}#{keys}" )

  if path.nil?   # If not found, try buffer in bookmarks.yml

    return true if self.jump( "#{prefix_to_bm}#{keys}" )
    View.beep
    $el.message("Bookmark not found!")
    return :not_found
  end

  prefix = Keys.prefix
  if prefix==9  # If 9, open in bar
    View.bar
    self.jump "#{prefix_to_bm}#{keys}"
  else
    Location.go path, :stay_in_bar => true
  end

  if options[:point] or prefix == :u   # Go to actual point
    return self.jump("#{prefix_to_bm}#{keys}")
  end
end

.input(options = {}) ⇒ Object

Prompt user for bookmark name, and return bookmark



285
286
287
288
289
290
# File 'lib/xiki/bookmarks.rb', line 285

def self.input options={}
  self.expand(
    Keys.input(options.merge(:timed => true)),
    :just_bookmark => true
    )
end

.jump(name) ⇒ Object

Like bookmark-jump, but accepts buffers



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/xiki/bookmarks.rb', line 117

def self.jump name

  # If normal bookmark found, use it
  return $el.bookmark_jump(name) if $el.bookmark_get_filename(name)

  buffer = self.buffer_bookmark name

  if buffer == :buffer_not_open
    return $el.message "Buffer '#{buffer}' not currently open."
  end
  return nil if buffer.nil?

  View.to_buffer buffer
  return true
end

.keysObject



195
196
197
# File 'lib/xiki/bookmarks.rb', line 195

def self.keys
  # TODO: put newest keys here
end

.list(path = nil) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/xiki/bookmarks.rb', line 297

def self.list path=nil

  result = ""
  if ! path   # Print all bookmarks
    all = $el.elvar.bookmark_alist.collect { |bm|
      item = bm.to_a

      second = item[1]   # Either (filename . "/path") or ((filename . "/path") ...)
      second = second[1].is_a?(String) ? second[1] : second[0][1]

      [item[0], second]
    }
    all.each do |l|
      n, p = l
      result << "- #{n}) @#{p.sub(/\/$/,'')}\n"
    end
    return result
  end

  # Open single path
  View.open path[/ ([~\/].+)/, 1]
  nil
end


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xiki/bookmarks.rb', line 7

def self.menu
  %`
  - .list/
  - .tree/
  - .api/
    | > Summary
    | Some of the common ways to use the Bookmarks class programatically.
    |
    | > Get path for a bookmark
    @p Bookmarks["$t"]
    |
    | > Expand bookmark in a path
    @p Bookmarks["$tm/hi.txt"]
    |
    > Where Emacs stores bookmarks
    @ ~/.emacs.bmk
    |
    - .elisp/
      | > Save unsaved bookmarks to ~/.emacs.bmk
      | (bookmark-save)
      |
      | > Delete a bookmark
      | (bookmark-delete "foo")
  - docs/
    > Summary
    | Xiki uses standard emacs bookmarks, while adding several keyboard shortcuts
    | and the ability to bookmark buffers in addition to just files.
    |
    > Keys
    | All of these prompt for a bookmark name:
    |
    | as+bookmark: bookmark the current file
    | open+bookmark: jumps to bookmark file
    | open+point: jumps to bookmark file and cursor position
    |
    > See Also
    @files/docs/
  `
end

.open_quickObject



342
343
344
345
# File 'lib/xiki/bookmarks.rb', line 342

def self.open_quick
  bookmark = Keys.input :timed=>1, :prompt=>"Enter a quick bookmark to jump to:"
  Bookmarks.go("q#{bookmark}")
end

.read(name) ⇒ Object

Read a file



293
294
295
# File 'lib/xiki/bookmarks.rb', line 293

def self.read name
  File.read(self.expand(name))
end

.save(arg = nil) ⇒ Object



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

def self.save arg=nil
  in_a_file_tree = FileTree.handles? rescue nil

  # If we're in a file tree, use file in tree (unless C-u over-rides it)
  if ! Keys.prefix_u? && in_a_file_tree && ! Line[/^ *\|/]
    path = Tree.construct_path
    keys = Keys.input(:timed=>true, :prompt=>"Name of bookmark for #{path.sub(/.+\/(.)/, "\\1")}: ") || "0"
    $el.with(:save_window_excursion) do
      $el.find_file path
      self.set keys
    end
    View.flash "- bookmarked as: $#{keys}", :times=>3
    return
  end

  # If arg is a symbol, use it as the prefix
  prefix = ""
  if arg && arg.class == Symbol
    prefix = arg.to_s
  elsif arg && arg.class == String
    self.set(arg.sub(/^\$/, ""))
    return
  end
  # Use input from user or "default"
  keys = Keys.input(:timed=>true, :prompt=>"Name of bookmark for #{View.file_name}: ") || "0"
  self.set "#{prefix}#{keys}"

  # Append to bookmark file
  log_path = "/temp/bookmark_log.notes"
  if File.exists?(log_path)
    File.open(log_path, "a") { |f| f << "| $#{prefix}#{keys}\n#{View.file}\n\n" }
  end
end

.set(name) ⇒ Object

Like bookmark-set, but accepts buffers



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

def self.set name
  # Just create normal bookmark if file
  return $el.bookmark_set(name) if View.file || $el.elvar.dired_directory

  # Must be buffer

  $el.bookmark_delete name   # Delete real bookmark

  # Load bookmarks.yml
  bookmarks_yml = File.expand_path("~/bookmarks.yml")
  if File.exists?(bookmarks_yml)
    bookmarks = YAML::load IO.read(bookmarks_yml)
  end
  bookmarks = [] unless bookmarks

  # Delete if there
  already_here = bookmarks.find {|bm| bm[0] == name}
  bookmarks.delete(already_here) if already_here

  # Add to end
  bookmarks << [name, View.name]

  # Save file
  File.open(bookmarks_yml, "w") { |f| f << bookmarks.to_yaml }

end

.treeObject



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/xiki/bookmarks.rb', line 321

def self.tree
  #     paths = elvar.bookmark_alist.collect {|bm|
  #       bm.to_a[1].to_a[0][1]
  #     }.select{|path| path =~ /^\/.+\w$/}

  paths = $el.elvar.bookmark_alist.collect {|bm|
    ary = bm.to_a
    key = ary[0]
    path = ary[1].to_a[0][1]
    [key, path]
  }
  paths = paths.select{|a| a[1] =~ /^\/.+\w$/}   # Remove bm's to dirs
  paths.collect! {|bm|
    path = bm[1]
    path.sub! /(.+\/)/, "\\1#{bm[0]}: "
    path
  }

  Tree.paths_to_tree(paths)
end