Next / Previous / Contents / Shipman's homepage

5.7. ibp-classify-line: What kind of line is this?

This function looks at the first character of a line and returns a symbol describing what kind of line it is. The return value will be one of:

Here's the prologue:

ibp.el
;; - - -   i b p - c l a s s i f y - l i n e   - - -

(defun ibp-classify-line (beg end)
  "Examines the given line and returns a symbol describing its type.

Return values:
    'non-trans          Not a transaction record (empty, @, or #)
    'lost-destroyed     Lost or destroyed band transaction
    'short-head         Transaction with 3-character head: u or n
    'short-recap        Recap with no prefix, 9-character head
    'long-head          Transaction with 10-character head: r or g
----------------------------------------------------------------"

First we start a let scope, and within that scope, define a variable line-text containing the text of the line.

ibp.el
  (let ((line-text (buffer-substring beg end)))

If the line is empty, it's a non-transaction line, and we are done.

ibp.el
    (if (string= line-text "")
        'non-trans

Now that we know there's at least one character on the line, look at it, start another let scope, and set variable prefix to that first character.

ibp.el
      (let ((prefix (string-to-char (substring line-text 0 1))))

The classification of the line type is done in a cond, which contains a series of tests, each with a corresponding result. For example, the second line here returns the 'short-head symbol if prefix is equal to the character n. The expression “?n” means the character “n”.

ibp.el
        (cond
         ((= prefix ?n) 'short-head)       ;; New band (short form)
         ((= prefix ?u) 'short-head)       ;; Unbanded

If the first character is a space, this is a recap line without the encounter code.

ibp.el
         ((= prefix ? ) 'short-recap)     ;; Short recaps start w/space

The next case is somewhat more complex. A short recap line can start with any character that can legally occur in a band number. This includes digits and question mark. (Letters can also occur in band numbers, but never as the first character.)

ibp.el
         ((and (<= ?0 prefix)                 ;; ...or a digit
               (<= prefix ?9)) 'short-recap)
         ((= prefix ??)                       ;; ...or a question mark
          'short-recap)

The remaining line types are straightforwardly related to encounter codes.

ibp.el
         ((= prefix ?r) 'long-head)           ;; Recap with 'r' prefix
         ((= prefix ?c) 'long-head)           ;; Recap, changed band
         ((= prefix ?a) 'long-head)           ;; Recap, double-banded
         ((= prefix ?g) 'long-head)           ;; New band (long form)
         ((= prefix ?l) 'lost-destroyed)      ;; Lost band
         ((= prefix ?d) 'lost-destroyed)      ;; Destroyed band

If all the above conditions fail, we'll just call it a non-transaction line.

ibp.el
         (t 'non-trans))))))