Class: Redwood::EditMessageMode

Inherits:
LineCursorMode show all
Defined in:
lib/sup/modes/edit-message-mode.rb

Direct Known Subclasses

ComposeMode, ForwardMode, ReplyMode, ResumeMode

Constant Summary collapse

FORCE_HEADERS =
%w(From To Cc Bcc Subject)
MULTI_HEADERS =
%w(To Cc Bcc)
NON_EDITABLE_HEADERS =
%w(Message-Id Date)

Constants inherited from ScrollMode

ScrollMode::COL_JUMP

Instance Attribute Summary collapse

Attributes inherited from LineCursorMode

#curpos

Attributes inherited from ScrollMode

#botline, #leftcol, #topline

Attributes inherited from Mode

#buffer

Instance Method Summary collapse

Methods inherited from LineCursorMode

#draw

Methods inherited from ScrollMode

#at_bottom?, #at_top?, #cancel_search!, #col_left, #col_right, #continue_search_in_buffer, #draw, #ensure_mode_validity, #in_search?, #jump_to_col, #jump_to_end, #jump_to_left, #jump_to_line, #jump_to_start, #line_down, #line_up, #page_down, #page_up, #resize, #rightcol, #search_goto_line, #search_in_buffer, #search_start_line

Methods inherited from Mode

#blur, #cancel_search!, #cleanup, #draw, #focus, #handle_input, #help_text, #in_search?, load_all_modes, make_name, #name, register_keymap, #resize, #resolve_input, #save_to_file

Constructor Details

#initialize(opts = {}) ⇒ EditMessageMode

Returns a new instance of EditMessageMode.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sup/modes/edit-message-mode.rb', line 53

def initialize opts={}
  @header = opts.delete(:header) || {} 
  @header_lines = []

  @body = opts.delete(:body) || []
  @body += sig_lines if $config[:edit_signature]

  @attachments = []
  @message_id = "<#{Time.now.to_i}-sup-#{rand 10000}@#{Socket.gethostname}>"
  @edited = false
  @skip_top_rows = opts[:skip_top_rows] || 0
  
  HookManager.run "before-edit", :header => @header, :body => @body

  super opts
  regen_text
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



38
39
40
# File 'lib/sup/modes/edit-message-mode.rb', line 38

def body
  @body
end

#headerObject

Returns the value of attribute header.



38
39
40
# File 'lib/sup/modes/edit-message-mode.rb', line 38

def header
  @header
end

#statusObject (readonly)

Returns the value of attribute status.



37
38
39
# File 'lib/sup/modes/edit-message-mode.rb', line 37

def status
  @status
end

Instance Method Details

#[](i) ⇒ Object



72
# File 'lib/sup/modes/edit-message-mode.rb', line 72

def [] i; @text[i] end

#attach_fileObject



116
117
118
119
120
121
# File 'lib/sup/modes/edit-message-mode.rb', line 116

def attach_file
  fn = BufferManager.ask_for_filename :attachment, "File name (enter for browser): "
  return unless fn
  @attachments << Pathname.new(fn)
  update
end

#delete_attachmentObject



123
124
125
126
127
128
129
# File 'lib/sup/modes/edit-message-mode.rb', line 123

def delete_attachment
  i = (curpos - @skip_top_rows) - @attachment_lines_offset
  if i >= 0 && i < @attachments.size && BufferManager.ask_yes_or_no("Delete attachment #{@attachments[i]}?")
    @attachments.delete_at i
    update
  end
end

#edit_ccObject



86
# File 'lib/sup/modes/edit-message-mode.rb', line 86

def edit_cc; edit_field "Cc" end

#edit_messageObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sup/modes/edit-message-mode.rb', line 89

def edit_message
  @file = Tempfile.new "sup.#{self.class.name.gsub(/.*::/, '').camel_to_hyphy}"
  @file.puts format_headers(@header - NON_EDITABLE_HEADERS).first
  @file.puts
  @file.puts @body
  @file.close

  editor = $config[:editor] || ENV['EDITOR'] || "/usr/bin/vi"

  mtime = File.mtime @file.path
  BufferManager.shell_out "#{editor} #{@file.path}"
  @edited = true if File.mtime(@file.path) > mtime

  return @edited unless @edited

  header, @body = parse_file @file.path
  @header = header - NON_EDITABLE_HEADERS
  handle_new_text @header, @body
  update

  @edited
end

#edit_message_or_fieldObject



77
78
79
80
81
82
83
# File 'lib/sup/modes/edit-message-mode.rb', line 77

def edit_message_or_field
  if (curpos - @skip_top_rows) >= @header_lines.length
    edit_message
  else
    edit_field @header_lines[curpos - @skip_top_rows]
  end
end

#edit_subjectObject



87
# File 'lib/sup/modes/edit-message-mode.rb', line 87

def edit_subject; edit_field "Subject" end

#edit_toObject



85
# File 'lib/sup/modes/edit-message-mode.rb', line 85

def edit_to; edit_field "To" end

#handle_new_text(header, body) ⇒ Object

a hook



75
# File 'lib/sup/modes/edit-message-mode.rb', line 75

def handle_new_text header, body; end

#killable?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/sup/modes/edit-message-mode.rb', line 112

def killable?
  !edited? || BufferManager.ask_yes_or_no("Discard message?")
end

#linesObject



71
# File 'lib/sup/modes/edit-message-mode.rb', line 71

def lines; @text.length end