Class: History

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

Class Method Summary collapse

Class Method Details

.backup_fileObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/xiki/history.rb', line 236

def self.backup_file
  bm = Bookmarks['$bak']
  return View.beep "- First, set the 'bak' bookmark to a dir!" if bm == "$bak"

  unless bm.any?   # If no bookmark, just show error
    View.beep
    return View.message("Error: create a bookmark named 'bak' first, in a dir where you backups will go.")
  end

  path = Keys.prefix_u? ?
    View.file :
    FileTree.tree_path_or_this_file

  name = path.sub(/.+\//, '')

  # Copy file xx
  $el.copy_file path, "#{bm}#{name}.#{Time.now.strftime('%Y-%m-%d.%H-%M')}"

  message = "backed up to $bak: '#{name}'"
  View.flash "- #{message}", :times=>3
  View.message "Successfully #{message}"
end

.diff_with_backupObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/xiki/history.rb', line 259

def self.diff_with_backup

  if Keys.prefix_u
    $el.ediff_files Dir["#{Bookmarks['$bak']}#{View.file_name}*"].last, View.file
    return
  end

  backup = Dir["#{Bookmarks['$bak']}#{View.file_name}*"].last

  return View.beep("- No backup exists in $bak/") if ! backup

  diff = Console.run "diff -U 0 \"#{backup}\" \"#{$el.buffer_file_name}\"", :sync=>true

  return Launcher.show "- No Differences!" if diff.blank?

  diff = DiffLog.format diff

  View.to_buffer("*diff with saved*")
  View.clear
  Notes.mode

  $el.insert diff.count("\n") > 2 ?
    diff :
    "| Alert\n- ~No Differences~\n"

end

.enter_historyObject



117
118
119
120
121
122
123
# File 'lib/xiki/history.rb', line 117

def self.enter_history
  orig = Location.new
  self.insert_history self.prefix_times
  right = $el.point
  orig.go
  Tree.search :recursive => true, :left => $el.point, :right => right
end

.enter_viewingObject



131
132
133
134
135
136
137
# File 'lib/xiki/history.rb', line 131

def self.enter_viewing
  orig = Location.new
  self.insert_viewing self.prefix_times
  right = $el.point
  orig.go
  Tree.search :recursive => true, :left => $el.point, :right => right
end

.insert_history(times) ⇒ Object



113
114
115
# File 'lib/xiki/history.rb', line 113

def self.insert_history times
  View.insert Tree.paths_to_tree($el.elvar.recentf_list.to_a[0..(times-1)])
end

.insert_viewing(times) ⇒ Object



125
126
127
128
129
# File 'lib/xiki/history.rb', line 125

def self.insert_viewing times
  paths = ( $el.buffer_list.map { |b| $el.buffer_file_name(b) }.select{|path| path})
  paths = paths[0..(times-1)] if times  # Limit to number if prefix passed
  View.insert Tree.paths_to_tree(paths)
end

FILENAME = File.expand_path “~/.emacs.d/menu_log.notes” Unused



5
6
7
8
9
10
11
12
13
14
# File 'lib/xiki/history.rb', line 5

def self.menu
  "
  - menus) @log/

  > Files
  - .unsaved files/
  - @edited/
  - @files/history/
  "
end

.open_current(options = {}) ⇒ Object



23
24
25
26
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
84
85
86
87
88
# File 'lib/xiki/history.rb', line 23

def self.open_current options={}
  if options[:paths]
    paths = options[:paths]
  elsif options[:prompt_for_bookmark]
    bm = Keys.input(:timed => true, :prompt => "Enter bookmark to show content for: ")
    path = Bookmarks.expand(bm, :just_bookmark => true)
    return View.beep("- Bookmark '#{bm}' not found!") if ! path
    path = File.expand_path(path)

    if ! options[:all] && View.files.member?(path)
      View.open path
      FileTree.to_outline
      return
    end

    paths = [path]
  elsif options[:outline] || options[:all]
    paths = [$el.buffer_file_name($el.buffer_list[0])]
  else  # No options passed
    times = Keys.prefix
    paths = ( $el.buffer_list.map { |b| $el.buffer_file_name(b) }.select{|path| path})
    paths = paths[0..(times-1)] if times  # Limit to number if prefix passed
  end

  if options[:enter_here]  # If entering in current file
    path = paths.to_s
    # If it's a dir, delegate to Open Tree
    if path =~ /\/$/
      FileTree.ls :here => true, :dir => path
      return
    end
    View.insert "- " + FileTree.filename_to_next_line(paths.to_s)
    $el.open_line 1
    FileTree.enter_lines

  else  # If entering in new buffer

    # By default happen in same view
    View.bar if options[:bar]  # If to go to bar

    # If only one path, go to it's buffer if already open
    if paths.size == 1
      View.open paths[0]
    end

    View.to_buffer("*tree of current")
    View.clear;  Notes.mode

    raise "Thought this wouldn't happen :(" if paths.length > 1

    dir, file = paths[0].match(/(.+\/)(.+)/)[1..2]
    View << "- #{dir}\n  - #{file}\n"

    View.to_top
    Keys.clear_prefix
    FileTree.select_next_file
    if options[:all]
      FileTree.enter_lines(//)
    elsif options[:outline] || options[:prompt_for_bookmark]
      FileTree.enter_lines
    else
      Tree.search :recursive => true
    end
  end

end

.open_editedObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/xiki/history.rb', line 90

def self.open_edited
  times = self.prefix_times
  View.to_buffer("*tree of edited")
  View.clear;  notes_mode
  View.insert Tree.paths_to_tree($el.elvar.editedhistory_history.to_a[0..(times-1)])
  View.to_top
  Keys.clear_prefix
  FileTree.select_next_file
  Tree.search :recursive => true
end

.open_historyObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/xiki/history.rb', line 101

def self.open_history
  times = self.prefix_times
  View.to_buffer("*tree of history")
  View.clear;  notes_mode

  self.insert_history times
  View.to_top
  Keys.clear_prefix
  FileTree.select_next_file
  Tree.search :recursive => true
end

.prefix_timesObject



16
17
18
19
20
21
# File 'lib/xiki/history.rb', line 16

def self.prefix_times
  prefix = Keys.prefix
  prefix ||= 20
  prefix = 20 if prefix == :u
  prefix
end

.setup_editedhistoryObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
# File 'lib/xiki/history.rb', line 174

def self.setup_editedhistory

  return if ! $el

  $el.el4r_lisp_eval %q<
    (progn
      ; Settings
      (setq editedhistory-log "~/.editedhistory")

      ; Runs upon startup.  Load log file into memory if it exists.  Run this manually if you edit the log file.
      (defun editedhistory-load ()
        (if (file-readable-p editedhistory-log)
          ; Read from file into memory
          (with-temp-buffer
            (insert-file-contents editedhistory-log nil nil nil t)
            (setq editedhistory-history
              (car (read-from-string (buffer-substring (point-min) (point-max))) )))
          ; Otherwise, initialize var
          (set 'editedhistory-history nil)))

      ; Saves on exit: 'editedhistory-history' to the file 'editedhistory-log'
      (add-hook 'kill-emacs-hook 'editedhistory-save)
      (defun editedhistory-save () (interactive)
        (with-temp-buffer
          (erase-buffer)
          (insert (pp-to-string editedhistory-history) )
          (if (file-writable-p editedhistory-log)
            (write-region (point-min) (point-max) editedhistory-log)
          )
          (kill-buffer (current-buffer)))
        nil)

      ; Runs upon save: Track modified files
      (defun editedhistory-remember-file ()
        ; Remove from list in case it's already there (we want to add to beginning)
        (when (boundp 'editedhistory-history)
          (setq editedhistory-history (delete buffer-file-name editedhistory-history) )
          ; Add to list
          (add-to-list 'editedhistory-history buffer-file-name))
        nil)   ; Return nil so we won't block writing
      (add-hook 'write-file-hooks 'editedhistory-remember-file)

      ; Load upon startup, to add hooks
      (define-minor-mode editedhistory-mode
        "Toggle editedhistory mode"
        :global t
        :group 'editedhistory

        ; Load if not yet loaded
        (pp (boundp 'editedhistory-loaded-p))
        (unless (boundp 'editedhistory-loaded-p)
          (setq editedhistory-loaded-p t)
          (pp editedhistory-loaded-p)
          ; Load log file into memory
          (editedhistory-load)))

      ; Load mode
      (editedhistory-mode t)
    )
  >
end

.unsaved_filesObject



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
165
166
167
168
169
170
171
172
# File 'lib/xiki/history.rb', line 139

def self.unsaved_files

  # Narrow down to modified buffer only
  buffers = $el.buffer_list.to_a.
    select{|b| $el.buffer_modified_p(b)}.
    select{|b| $el.buffer_file_name(b)}

  if (buffers.size == 0)
    return "- No files unsaved!\n"
  end

  txt = ""

  buffers.each do |b|
    path = $el.buffer_file_name(b)
    $el.with(:save_excursion) do
      $el.set_buffer b

      diffs = DiffLog.save_diffs :dont_log=>1

      if ! diffs
        diffs = "    | File no longer exists?\n" if ! File.exists?(path)
        diffs ||= "    | no changes\n"
      end
      diffs = diffs.sub(/^.+\n.+\n/, '').gsub /^  /, ''

      txt << "@#{path}\n"
      txt << diffs

    end
  end

  txt
end