Class: Relisp::Buffer

Inherits:
Proxy show all
Defined in:
lib/relisp/type_conversion/editing_types.rb

Overview

A proxy to an Emacs buffer.

Instance Attribute Summary

Attributes inherited from Proxy

#elisp_variable, #slave

Instance Method Summary collapse

Methods inherited from Proxy

elisp_alias, from_elisp, #makunbound, #to_elisp

Constructor Details

#initialize(*args) ⇒ Buffer

args can be any of these forms:

  • (symbol, slave = Relisp.default_slave)

  • (string, slave = Relisp.default_slave)

  • (slave = Relisp.default_slave)

When a symbol is given it is considered to be the name of a pre-existing buffer in the slave process. Otherwise a new buffer is created (generate-new-buffer). The name is string, if given, and a variant of “relisp-buffer” otherwise.



64
65
66
67
68
69
70
# File 'lib/relisp/type_conversion/editing_types.rb', line 64

def initialize(*args)
  super do |sargs|
    name = sargs[0] ? sargs[0] : "relisp-buffer"
    raise ArgumentError unless name.kind_of?(String)
    @slave.elisp_exec( "(setq #{@elisp_variable} (generate-new-buffer #{name.to_elisp}))" )
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



240
241
242
243
244
245
# File 'lib/relisp/type_conversion/editing_types.rb', line 240

def method_missing(method, *args)
  @slave.save_excursion do 
    set
    @slave.send(method, *args)
  end
end

Instance Method Details

#<<(object) ⇒ Object



227
228
229
230
231
232
233
234
# File 'lib/relisp/type_conversion/editing_types.rb', line 227

def <<(object)
  @slave.save_excursion do 
    set
    @slave.elisp_eval "(goto-char (point-max))"
    insert object
  end
  return self
end

#==(buffer2) ⇒ Object



236
237
238
# File 'lib/relisp/type_conversion/editing_types.rb', line 236

def ==(buffer2)
  @slave.elisp_eval "(equal #{to_elisp} #{buffer2.to_elisp})"
end

#buryObject



152
153
154
155
156
157
# File 'lib/relisp/type_conversion/editing_types.rb', line 152

def bury
  call_on_self :bury_buffer
  if @slave.elisp_eval "(equal (current-buffer) #{self.to_elisp})"
    @slave.elisp_exec "(switch-to-buffer (other-buffer))"
  end
end

#eraseObject



199
200
201
# File 'lib/relisp/type_conversion/editing_types.rb', line 199

def erase
  eval_in_buffer "(erase-buffer)"      
end

#filename=(newname, along_with_file = false) ⇒ Object

Change name of file visited in the Buffer to newname. This also renames the buffer to correspond to the new file. The next time the buffer is saved it will go in the newly specified file. newname nil or an empty string means mark buffer as not visiting any file.

The optional along_with_file, if non-nil, means that the old visited file has been renamed to newname (set-visited-file-name).



121
122
123
124
125
# File 'lib/relisp/type_conversion/editing_types.rb', line 121

def filename=(newname, along_with_file=false)
  # the second argument here inhibits confirmation in the case
  # where another buffer is already visiting _newname_.
  eval_in_buffer "(set-visited-file-name #{newname.to_elisp} t #{along_with_file.to_elisp})"
end

#insert(object) ⇒ Object Also known as: print



213
214
215
# File 'lib/relisp/type_conversion/editing_types.rb', line 213

def insert(object)
  eval_in_buffer "(insert #{object.to_elisp})"
end

#killObject



159
160
161
162
# File 'lib/relisp/type_conversion/editing_types.rb', line 159

def kill
  raise if modified?
  call_on_self :kill_buffer
end

#kill!Object



164
165
166
167
# File 'lib/relisp/type_conversion/editing_types.rb', line 164

def kill!
  set_modified(false)
  kill
end

#modified=(flag) ⇒ Object



131
132
133
# File 'lib/relisp/type_conversion/editing_types.rb', line 131

def modified=(flag)
  set_modified(flag)
end

#nameObject

Return the name of Buffer, as a string (buffer-name).



91
# File 'lib/relisp/type_conversion/editing_types.rb', line 91

elisp_alias :name, :buffer_name

#puts(object = "") ⇒ Object



219
220
221
222
223
224
225
# File 'lib/relisp/type_conversion/editing_types.rb', line 219

def puts(object="")
  line_number = @slave.line_number_at_pos
  insert object
  if line_number == @slave.line_number_at_pos
    insert "\n"
  end
end

#read_only=(flag) ⇒ Object



148
149
150
# File 'lib/relisp/type_conversion/editing_types.rb', line 148

def read_only=(flag)
  eval_in_buffer "(setq buffer-read-only #{flag.to_elisp})"
end

#read_only?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/relisp/type_conversion/editing_types.rb', line 144

def read_only?
  eval_in_buffer "buffer-read-only"
end

#rename(newname, unique = false) ⇒ Object Also known as: name=

Change current buffer’s name to newname (a string). If unique is nil, it is an error if a buffer named newname already exists. If unique is non-nil, come up with a new name using generate-new-buffer-name' (rename-buffer).



99
100
101
# File 'lib/relisp/type_conversion/editing_types.rb', line 99

def rename(newname, unique = false)
  eval_in_buffer "(rename-buffer #{newname.to_elisp} #{unique.to_elisp})"
end

#saveObject

Save the buffer in its visited file, if it has been modified (save-buffer).



175
176
177
178
# File 'lib/relisp/type_conversion/editing_types.rb', line 175

def save
  raise "Attempt to save buffer with no filename." unless filename
  eval_in_buffer "(with-output-to-string (save-buffer))"
end

#setObject



84
85
86
# File 'lib/relisp/type_conversion/editing_types.rb', line 84

def set
  @slave.set_buffer(@elisp_variable.value)
end

#set_modified(flag = true) ⇒ Object



127
128
129
# File 'lib/relisp/type_conversion/editing_types.rb', line 127

def set_modified(flag=true)
  eval_in_buffer "(set-buffer-modified-p #{flag.to_elisp})"
end

#substring(start_position, end_position) ⇒ Object



187
188
189
# File 'lib/relisp/type_conversion/editing_types.rb', line 187

def substring(start_position, end_position)
  eval_in_buffer "(buffer-substring #{start_position} #{end_position})"
end

#substring_no_properties(start_position, end_position) ⇒ Object



191
192
193
# File 'lib/relisp/type_conversion/editing_types.rb', line 191

def substring_no_properties(start_position, end_position)
  eval_in_buffer "(buffer-substring-no-properties #{start_position} #{end_position})"
end

#to_sObject



195
196
197
# File 'lib/relisp/type_conversion/editing_types.rb', line 195

def to_s
  eval_in_buffer "(buffer-string)"
end

#window=(new_window) ⇒ Object



206
207
208
# File 'lib/relisp/type_conversion/editing_types.rb', line 206

def window=(new_window)
  new_window.buffer = self
end

#write(newfile) ⇒ Object



180
181
182
# File 'lib/relisp/type_conversion/editing_types.rb', line 180

def write(newfile)
  eval_in_buffer "(with-output-to-string (write-file #{newfile.to_elisp}))"
end