Class Buffer

A Buffer represents the contents of an open text file as it is maintained in the computer's memory (as opposed to how it may be stored on a disk).

File attribute methods

  • public String getName(void);

  • public String getPath(void);

  • public File getFile(void);

    This method may return null if the file is stored on a remote file system (for example, if the FTP or Archive plugins are in use). This method should be avoided unless you really need to use the java.io.File APIs and you are sure the buffer in question is a local file.

  • public boolean isNewFile(void);

    Returns whether a buffer lacks a corresponding version on disk.

  • public boolean isDirty(void);

    Returns whether there have been unsaved changes to the buffer.

  • public boolean isReadOnly(void);

  • public boolean isUntitled(void);

Editing attribute methods

  • public Mode getMode(void);

  • public void setMode(Mode mode);

    Gets and sets the editing mode for the buffer.

  • public int getIndentSize(void);

  • public int getTabSize(void);

    These methods return the size of an initial indentation at the beginning of a line and the distance between tab stops, each measured in character columns. If these properties are not individually set for a specific buffer, they are inherited from the properties of the buffer's associated editing mode.

The Buffer object maintains a table of properties that describe a broad range of attributes. The value of each property is stored using a String that names the particular property. Most of these properties are documented in the section called "Buffer-Local Properties".

  • public Object getProperty(String name);

    Using this method is generally discouraged, because it returns an Object which must be cast to another type in order to be useful, and this can cause problems if the object is of a different type than what your plugin or macro expects.

    jEdit 4.0 added a number of "wrapper" methods, documented below; you are strongly encouraged to use them instead.

  • public void setProperty(String  name,
     Object  value);

    Sets the value of a property named by name to value.

These methods provide shortcuts for getting and setting string, integer and boolean properties.

  • public static String getStringProperty(String  name);

    Returns the value of the property named by name as a string.

  • public static void setStringProperty(String  name,
     String  value);

    Sets the property named by name to value.

  • public static boolean getBooleanProperty(String  name);

    Returns a boolean value of true or false for the property named by name by examining the contents of the property; returns false if the property cannot be found.

  • public static void setBooleanProperty(String  name,
     boolean  value);

    Sets the property named by name to value. The boolean value is stored internally as the string "true" or "false".

  • public static int getIntegerProperty(String  name,
     int  defaultValue);

    Returns the integer value of the property named by name. If the property value is not a valid numeric string, returns defaultValue instead.

  • public static void setIntegerProperty(String  name,
     int  value);

    Sets the property named by name to value.

Input/output methods

  • public void reload(View view);

    Reloads the buffer from disk into view, asking for confirmation if the buffer has unsaved changes.

  • public boolean save(View view, String path);

  • public boolean save(View  view,
     String  path,
     boolean  rename);

    The rename parameter causes a buffer's name to change if set to true; if false, a copy is saved to path.

  • public boolean saveAs(View  view,
     boolean  rename);

    Prompts the user for a new name for saving the file.

General editing methods

  • public String getText(int  offset,
     int  length);
  • public void getText(int  offset,
     int  length,
     Segment  text);

    These methods extract a portion of buffer text having length length beginning at offset position offset. The first method returns a newly created String containing the requested excerpt. The second version initializes an existing Segment object with the location of the requested excerpt. The Segment object represents array locations within the Buffer object's data and should be used on a read-only basis. Calling toString() on the Segment will create a new object suitable for manipulation.

    Using a Segment is generally more efficient than using a String because it results in less memory allocation and array copying. However, Segments are slightly harder to set up and use.

  • public String getLineText(int lineIndex);

  • public void getLineText(int  lineIndex,
     Segment  text);

    Returns the text of the given line. Just as with getText(), there are two forms of this method; the first returns a String, the second copies into a Segment.

  • public void beginCompoundEdit(void);

  • public void endCompoundEdit(void);

    Marks the beginning and end of a series of editing operations that will be dealt with by a single Undo command.

  • public void insert(int offset, String text);

    This method inserts the string text at offset offset in the buffer.

  • public void remove(int offset, int length);

    This method removes length characters of text starting from offset.

  • public void removeTrailingWhiteSpace(int[]  lines);

    Removes trailing whitespace in the lines referenced by the index numbers in lines array.

  • public int getLineOfOffset(int offset);

    Returns the line on which the given offset is found.

  • public int getLineStartOffset(int line);

  • public int getLineEndOffset(int line);

    Returns the offset of the beginning or end of the given line.

  • public int getLineLength(int line);

    Returns the length of the line number line (using a zero-based count).

  • public int getLineCount(void);

    Returns the number of lines in the buffer being edited.

  • public int getLength(void);

    Returns the number of characters in the buffer.

Marker methods

Buffers may have one or more markers which serve as textual bookmarks. A Marker has three key attributes: the Buffer to which it relates, the line number to which the marker refers, and an optional shortcut character. The shortcut identifies the the key that can be pressed with the Markers>Go To Marker command to move the editing caret to the marker line location.

The position and shortcut character of a Marker object can be retrieved with the methods getPosition() and getShortcut().

The Buffer class includes the following methods to set and retrieve markers:

  • public void addMarker(char  shortcut,
     int  pos);

    Adds a marker for the line indicated by pos using shortcut. Set shortcut to '\0' to indicate the absence of a shortcut.

  • public Vector getMarkers(void);

    Returns a Vector containing the buffer's current markers.

  • public Marker getMarkerAtLine(int line);

    Returns the first marker at the specified line, or null if no marker is present at the line.

  • public Marker getMarker(char shortcut);

    Returns the marker with the specified shortcut, or null if no such marker exists.

  • public void removeMarker(int line);

    Removes all markers at the specified line.

  • public void removeAllMarkers(void);

    Removes all markers in the buffer.