home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / packages / field.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  3.7 KB  |  114 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;;
  3. ;;;  Copyright (c) 1988 Microelectronics and Computer Technology Corporation
  4. ;;;
  5. ;;; File:     field.el
  6. ;;; Created:     Sun Jan 10 10:16:17 1988
  7. ;;; Author:     Frank Halasz (halasz@babyhalasz)
  8. ;;;
  9. ;;; Description: Implementation of fields which are markers that span regions.
  10. ;;; 
  11. ;;;
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13.  
  14.  
  15. ;;;  A field is like a marker but it defines a region rather than a
  16. ;;;  point.  Like a marker, a field is aasocated with a buffer.
  17. ;;;  The field mechanism uses the marker mechanism in the
  18. ;;;  sense that its start and end points are maintained as markers
  19. ;;;  updated in the usual way as the buffer changes.
  20. ;;;  A field can be protected or unprotected.  If it is protected,
  21. ;;;  no modifications can be made that affect the field in its buffer 
  22. ;;;  (see insdel.c and field.c).  Finally, an arbitrary data object
  23. ;;;  can be associated with a field.
  24.  
  25. ;;;  Fields are currently implemented using Lisp vectors.  This is to
  26. ;;;  avoid having to make a new lisp type and deal with all the
  27. ;;;  garbage collection issues, etc.  At some later point, it may make
  28. ;;;  sense to make them into a lisp data type of their own.
  29.  
  30. (defvar buffer-fields nil)
  31.  
  32. (defun add-field (start end &optional buffer protected data)
  33.   "Add a new field to BUFFER that starts at START (inclusive)and ends at
  34. END (exclusive).
  35. START and END can be character numbers or markers.
  36. If PROTECTED is non-nil, then the field
  37. will be protected from insertion and deletion.
  38. ALIST (optional) is the initial value for the field's alist.
  39. Returns the field object (which is actually a vector)."
  40.  
  41.   ;; nil buffer means current buffer
  42.   (if (null buffer)
  43.       (setq buffer (current-buffer)))
  44.  
  45.   ;; transform marker args (if any) in character positions
  46.   (if (markerp start)
  47.       (setq start (marker-position start)))
  48.   (if (markerp end)
  49.       (setq end (marker-position end)))
  50.  
  51.   ;; Reverse start and end if necessary
  52.   (if (> start end)
  53.       (let (temp)
  54.     (setq temp start
  55.           start end
  56.           end temp)))
  57.    
  58.   ;; Make the field, fill in the slots, hook the field into buffer's
  59.   ;; field chain.
  60.   (let ((field (vector 'field buffer
  61.                ;; markers stick to the preceeding character!!!!!
  62.                (set-marker (make-marker) (1+ start) buffer)
  63.                (set-marker (make-marker) (+ 0 end) buffer)
  64.                protected
  65.                data)))
  66.     (save-excursion
  67.       (set-buffer buffer)
  68.       (setq buffer-fields (cons field buffer-fields)))
  69.   
  70.     field))
  71.  
  72. (defun region-field ()
  73.   (add-field (region-beginning) (region-end)))
  74.  
  75. (defun protected-region-field ()
  76.   (add-field (region-beginning) (region-end) (current-buffer) t nil))
  77.  
  78. (defmacro field-buffer (field)
  79.   "Return the buffer that FIELD is associated with."
  80.   (list 'aref field 1))
  81.   
  82. (defmacro field-start (field)
  83.   "Return the character number of the current starting point of FIELD"
  84.   ;; markers stick to the preceeding character
  85.   (list '1- (list 'aref field 2)))
  86.  
  87. (defmacro field-end (field)
  88.   "Return the character number of the current end point of FIELD"
  89.   (list 'aref field 3))
  90.  
  91. (defmacro field-protected (field)
  92.   "Return t is FIELD is protected, nil otherwise."
  93.   (list 'aref field 4))
  94.  
  95. (defmacro field-alist (field)
  96.   "Return the data associated with FIELD."
  97.   (list 'aref field 5))
  98.  
  99. (defmacro set-field-protected (field protected)
  100.   "Set the protection status of FIELD to PROTECTED."
  101.   (list 'aset field 4 protected))
  102.   
  103. (defmacro set-field-alist (field alist)
  104.   "Set the data associate with FIELD to be ALIST."
  105.   (list 'aset field 5 alist))
  106.  
  107. (defun delete-field (field)
  108.   "Delete field FIELD, in whichever buffer it belongs to."
  109.   (save-excursion
  110.     (set-buffer (field-buffer field))
  111.     (setq buffer-field-list
  112.       (delq field buffer-field-list))))
  113.     
  114.