1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
|
;;; db-types.el --- part of EDB, the Emacs database
;; See database.el for copyright notice, distribution conditions, etc.
;; Author: Michael Ernst <mernst@theory.lcs.mit.edu>
;; Keywords: EDB
;;; Commentary:
;; Library of types for EDB database fields.
;; This file contains predefined types. For efficiency, they're defined
;; in terms of the displayspec abstraction instead of via format strings.
;; Improvements and additions are welcome. Predefined types for dates and
;; times can be found in db-time.el.
;;; Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Contents
;;;
;;; Contents
;;; To Do
;;; Variables
;;; Displaytype
;;; Enumeration Displaytypes
;;; Numbers
;;; Booleans
;;; Strings
;;; Last Names
;;; First Names
;;; Full Names
;;; Places
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; To Do
;;;
;; Some of this information might be appropriate in the displayspec rather
;; than in the type. Pros and cons:
;; In displayspec:
;; * no need for extra indirection to get the information
;; * easier to customize (eg to provide more specific help information).
;; In datatype:
;; * less repetition of information, slightly more space-efficient.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Variables
;;;
(defvar db-use-completing-read t
"*Non-nil if EDB should use `completing-read' when reading enumerated types.
Otherwise, on erroneous enumeration input, EDB shows the possible completions
but doesn't correct the input.")
(defvar db-enum-ignore-case t
"If non-nil, assume that any association lists created from
enumeration types input names are to include both upper and lower case
versions of the name, if distinct.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Displaytype
;;;
;; WARNING! We may want the type field of the displayspec to be different
;; from the argument here; this implies we want to be able to specify a
;; type either elsewhere or in optstring. Easier for me if it's not in
;; optstring, of course. Now, why make the user deal with optstring at
;; all? Why not let him use an alist or something if he wants? Answer:
;; because I'm too lazy just now for that. [Uh, who uses the type field?
;; Wouldn't we want the type field to reflect the first argument? Do we
;; just want to permit inheritance here?]
;; Perhaps permit inheritance based on inheritedtype; perhaps permit
;; setting of type field dependent on actualtype
;; Perhaps permit typename to be different from an abbreviation permitted
;; in field specifications.
(defun define-displaytype-from-optstring (typename optstring)
"Define a displaytype named TYPENAME according to OPTSTRING.
TYPENAME is a symbol or string and OPTSTRING is the optional parameters
part of a display specification string."
(if (stringp typename)
(setq typename (intern typename)))
(let ((displayspec (make-displayspec-from-type-and-options nil optstring t)))
(define-displaytype-from-displayspec typename displayspec)))
;; Typename must be a symbol. This is an internal function.
(defun define-displaytype-from-displayspec (typename displayspec)
"Define a displaytype named TYPENAME (a symbol) with the default DISPLAYSPEC.
DISPLAYSPEC may also be a typename symbol itself."
(setq db-displaytypes (cons (cons typename displayspec) db-displaytypes)))
(put 'define-displaytype-from-displayspec 'lisp-indent-hook 1)
(defun define-recordfieldtype-from-recordfieldspec (typename recordfieldspec)
"Define a recordfieldtype named TYPENAME (a symbol) with the default RECORDFIELDSPEC.
DISPLAYSPEC may also be a typename symbol itself.
After this call, `recordfieldtype->recordfieldspec' called with argument
TYPENAME returns the proper record field specification."
(setq db-recordfieldtypes (cons (cons typename recordfieldspec) db-recordfieldtypes)))
(put 'define-recordfieldtype-from-recordfieldspec 'lisp-indent-hook 1)
;; Provide functions to easily create aliases to existing types
(fset 'define-displaytype-alias 'define-displaytype-from-displayspec)
(make-obsolete 'define-displaytype-alias 'define-displaytype-from-displayspec)
(fset 'define-recordfieldtype-alias 'define-recordfieldtype-from-recordfieldspec)
(make-obsolete 'define-recordfieldtype-alias 'define-recordfieldtype-from-recordfieldspec)
(defun define-type-alias (alias typename)
"Make symbol ALIAS refer to the same displaytype and recordfieldtype as TYPENAME."
(define-displaytype-from-displayspec alias typename)
(define-recordfieldtype-from-recordfieldspec alias typename))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Enumeration displaytypes
;;;
;; An enumeration displaytype is used for fields whose values are one of a
;; fixed set of alternatives. There are two types of alternatives: those
;; consisting of a single character (say, M or F for gender) and those
;; consisting of a whole string. The types are nicknamed one-char-enum and
;; (for the multicharacter alternative type) enum.
;;;
;;; One-character alternatives
;;;
;; One-char-enum types don't necessarily get displayed as a single
;; character, but only a single character need be typed in order to set the
;; value.
(defun define-one-char-enum-displaytype (typename alternatives optstring)
"Not yet implemented."
(error "define-one-char-enum-displaytype isn't defined yet")
)
;;;
;;; Multi-character alternatives
;;;
;; The user can specify for each alternative four pieces of information
;; (see documentation for `define-enum-type' for details). We have the
;; following options for storing the relationships between those pieces of
;; information, which must be used by such functions as the displayspec's
;; display->actual function:
;; * in global variables keyed by enum-type name and looked up when needed,
;; * in database-local variables similar to the above, or
;; * in the functions themselves (the functions are created when the
;; enum-type is defined and must in any event contain some specific
;; information such as the enum-type name).
;; Keeping the information in functions is faster at run-time (I suspect
;; this will be most noticable in stored->actual and actual->stored; it
;; might not be noticable elsewhere) but more difficult to debug and to
;; modify by hand. If any of the information is kept in database-local
;; variables, all of it should be, because database-local variables are
;; saved when the database is stored in internal file layout and restored
;; when it is read in again. However, other information is kept in global
;; variables such as db-displaytypes, so it is problematic to keep any
;; information in database-local variables; it is especially bad to have
;; the type half-defined so that an error is produced if the type is
;; redefined but the existing information is insufficient to provide the
;; full enum type functionality. If all the information is stored in
;; database-local variables, then it is unnecessary for databases stored in
;; internal file layout to have calls to define-enum-type in their
;; auxiliary files.
(defun define-enum-type (typename alternatives &optional optstring)
"Make TYPENAME (a symbol or string) an enumerated type.
Both a displaytype and a recordfieldtype are created.
ALTERNATIVES is a list. Each alternative is a list of up to four components:
the internal representation, any constant Lisp object, often a string;
the input representation typed by the user to specify this alternative,
a string or list of strings (for multiple input representations);
the display representation, a string; and
the file storage representation, a string.
If the input representation is omitted and the internal representation is a
string, that string is used. If the display representation is omitted, it
defaults to the first input representation. The display representation is
automatically also a valid input representation. If the file storage
representation is omitted, it defaults to the display representation.
If all the other components are omitted, the internal representation string
may be used in place of a one-element list containing just it.
Optional argument OPTSTRING is a displayspec option string."
(if (stringp typename)
(setq typename (intern typename)))
(let (d->a a->d a->s
(processed-alternatives (db-enum-process-alternatives alternatives)))
(setq d->a (car processed-alternatives)
a->d (car (cdr processed-alternatives))
a->s (car (cdr (cdr processed-alternatives))))
(let ((ds (make-displayspec-from-type-and-options nil optstring t)))
(displayspec-set-indent ds nil)
(displayspec-set-display->actual ds (db-enum-make-display->actual
typename d->a))
(displayspec-set-actual->display ds (db-enum-make-actual->display
typename a->d))
(define-displaytype-from-displayspec typename ds))
(let ((rs (make-recordfieldspec)))
(recordfieldspec-set-type rs typename)
(recordfieldspec-set-default-value rs "")
(recordfieldspec-set-order-fn rs (make-enum-member-orderer typename a->d))
(recordfieldspec-set-sort-fn rs (make-enum-member-sorter typename a->d))
;; With this value, we should have
;; (displayspec-set-match-actual->display ds 'string-match-actual->display)
;; (displayspec-set-match-display->actual ds 'string-match-display->actual)
;; in the displayspec, I believe.
(recordfieldspec-set-match-function rs (function string-match-function))
(recordfieldspec-set-help-info rs (db-enum-make-help-info typename d->a))
(recordfieldspec-set-actual->stored rs (db-enum-make-actual->stored
typename a->s))
(recordfieldspec-set-stored->actual rs (db-enum-make-stored->actual
typename a->s))
(define-recordfieldtype-from-recordfieldspec typename rs))))
;; These names are provided for backward compatibility. (4/26/93)
(fset 'define-alternative-multi-char-displaytype 'define-enum-type)
;; "Takes an alternatives list of the form accepted by `define-enum-type'.
;; Returns a list of three alists appropriate for use by display->actual,
;; actual->display, and actual->stored (also stored->actual) functions."
(defun db-enum-process-alternatives (alternatives)
(let (d->a dinput->a a->d a->s a-s-differ
alternative internal input display storage)
(while alternatives
(setq alternative (car alternatives)
alternative (if (listp alternative) alternative (list alternative))
internal (car alternative)
input (or (car (cdr alternative)) internal)
input (if (listp input) input (list input))
display (or (car (cdr (cdr alternative))) (car input))
storage (or (nth 3 alternative) display)
d->a (nconc d->a (mapcar (function (lambda (irep)
(cons irep internal)))
input))
dinput->a (if (member display input)
dinput->a
(cons (cons display internal) dinput->a))
a->d (cons (cons internal display) a->d)
a->s (cons (cons internal storage) a->s)
a-s-differ (or a-s-differ (not (equal internal storage)))
alternatives (cdr alternatives)))
;; The order is significant in db-enum-make-help-info.
;; [It's not clear whether that's a feature or a bug.]
(setq d->a (nconc d->a (nreverse dinput->a))
a->d (nreverse a->d)
a->s (and a-s-differ (nreverse a->s)))
(list d->a a->d a->s)))
;; (defun db-enum-set-d->a (type alist &optional no-error)
;; )
;; (defun db-enum-set-a->d (type alist &optional no-error)
;; )
;; (defun db-enum-set-a->s (type alist &optional no-error)
;; )
;; (defun db-enum-get-d->a (type alist &optional no-error)
;; )
;; (defun db-enum-get-a->d (type alist &optional no-error)
;; )
;; (defun db-enum-get-a->s (type alist &optional no-error)
;; )
;;; make-enumerated-help [Added by AKS, 10/13/92].
;; "Return a string listing the input representations of TYPE."
(defun db-enum-make-help-info (type d->a)
(let* ((buf (get-buffer-create " *temp*"))
(standard-output buf))
(save-excursion
(set-buffer buf)
(erase-buffer)
(princ (format "%s: an enumerated type. " (symbol-name type)))
(display-completion-list (all-completions "" d->a))
(prog1 (buffer-string)
(kill-buffer buf)))))
;; "Return a function appropriate for a displayspec's display->actual slot.
;; The input string is converted into one of the alternatives of TYPE, and
;; if ambiguities exist, they are resolved interactively."
(defun db-enum-make-display->actual (type d->a)
(` (lambda (input-rep)
(db-enum-do-completions input-rep (quote (, type)) (quote (, d->a))))))
;; "Given a string INPUT-REP and an enum TYPE, return INPUT-REP if it is a
;; valid input representation, otherwise see if it completes to a valid one,
;; and show the possible completions."
(defun db-enum-do-completions (input-rep type d->a)
(let ((assoc-hit (assoc input-rep d->a)))
(if assoc-hit
(cdr assoc-hit)
(let* ((completion-ignore-case db-enum-ignore-case)
(try-comp (try-completion input-rep d->a)))
(if (and try-comp
(setq assoc-hit (assoc try-comp d->a)))
(cdr assoc-hit)
(progn
(dbf-set-this-field-text (or try-comp ""))
(if db-use-completing-read
(progn
(setq input-rep
(completing-read
(format "Enter \"%s\" enum value (? for list): " type)
d->a nil t (or try-comp ""))
assoc-hit (assoc input-rep d->a))
(cdr assoc-hit))
;; no completing read, so display the options
(progn
(with-electric-help-maybe
(display-completion-list
(all-completions (or try-comp "") d->a)))
(error (if try-comp
"Not unique."
"No match."))))
))))))
;; "Return a function appropriate for a displayspec's actual->display slot."
(defun db-enum-make-actual->display (typename a->d)
(` (lambda (enum-val)
(cdr (assoc enum-val (quote (, a->d)))))))
;; "Return nil or a function appropriate for a displayspec's actual->stored slot."
(defun db-enum-make-actual->stored (typename a->s)
(if a->s
(` (lambda (enum-val)
(cdr (assoc enum-val (quote (, a->s))))))))
;; "Return nil or a function appropriate for a displayspec's stored->actual slot."
(defun db-enum-make-stored->actual (typename a->s)
(if a->s
(` (lambda (stored-val)
(car (db-rassoc stored-val (quote (, a->s))))))))
;; "Return an ordering function for the enum type TYPE."
(defsubst make-enum-member-orderer (type a->d)
(make-enum-member-orderer-internal type a->d -1 0 1))
;; "Return a sorting function for the enum type TYPE."
(defsubst make-enum-member-sorter (type a->d)
(make-enum-member-orderer-internal type a->d t nil nil))
;; "Given an enum type TYPE, create an ordering function which compares two
;; items in the enum type's alternatives list. The resulting function returns
;; LESS-VALUE if its first argument precedes its second argument in the
;; alternative list, EQUAL-VALUE if they're equal or neither appears, and
;; GREATER-VALUE if the first follows the second.
;;
;; If one of the arguments doesn't appear in the alternatives list, the other
;; is considered to precede it."
(defun make-enum-member-orderer-internal (type a->d less-value equal-value greater-value)
(` (lambda (item1 item2)
(if (equal item1 item2)
(, equal-value)
(let ((alt-alist (quote (, a->d)))
(result (, equal-value))
alt-item)
(while alt-alist
(setq alt-item (car alt-alist))
(cond ((equal (car alt-item) item1)
(setq result (, less-value)
alt-alist nil))
((equal (car alt-item) item2)
(setq result (, greater-value)
alt-alist nil))
(t
(setq alt-alist (cdr alt-alist)))))
result)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Numbers
;;;
;; I may wish to permit a numeric type that's stored as a string, not a
;; number; not because that will make conversion to display and storage
;; types easier (actually, moot) but because it will allow searching the
;; number like a string.
;; Ought I provide special handling of unspecified numbers? Perhaps later;
;; that will require dealing with computation, too.
;;;
;;; Integers
;;;
(let ((ds (make-displayspec)))
(displayspec-set-indent ds nil)
(displayspec-set-actual->display ds (function int-to-string))
(displayspec-set-display->actual ds (function string-to-int))
(define-displaytype-from-displayspec 'integer ds))
(let ((rs (make-recordfieldspec)))
(recordfieldspec-set-type rs 'integer)
(recordfieldspec-set-default-value rs 0)
(recordfieldspec-set-actual->stored rs (function int-to-string))
(recordfieldspec-set-stored->actual rs (function string-to-int))
(recordfieldspec-set-order-fn rs (function number-order))
(recordfieldspec-set-sort-fn rs (function <))
(recordfieldspec-set-match-function rs (function =))
(recordfieldspec-set-help-info rs "An integer.")
(define-recordfieldtype-from-recordfieldspec 'integer rs))
(let ((ds (make-displayspec)))
(displayspec-set-indent ds nil)
(displayspec-set-actual->display ds (function number-or-nil->string))
(displayspec-set-display->actual ds (function string->integer-or-nil))
(define-displaytype-from-displayspec 'integer-or-nil ds))
(let ((rs (make-recordfieldspec)))
(recordfieldspec-set-type rs 'integer-or-nil)
(recordfieldspec-set-default-value rs 0)
(recordfieldspec-set-actual->stored rs (function number-or-nil->string))
(recordfieldspec-set-stored->actual rs (function string->integer-or-nil))
(recordfieldspec-set-order-fn rs (function number-or-nil-order-nil-greatest))
;; (recordfieldspec-set-sort-fn rs (function <))
(recordfieldspec-set-match-function rs (function equal))
(recordfieldspec-set-help-info rs "An integer, or nil.")
(define-recordfieldtype-from-recordfieldspec 'integer-or-nil rs))
(defun string-or-nil->number-or-nil (string-or-nil)
(and string-or-nil
(string->number-or-nil string-or-nil)))
(make-obsolete 'string-or-nil->integer-or-nil 'string-or-nil->number-or-nil)
;; ;; Deal with requirement to take extra arguments.
;; (defun db-integer->string (i &rest ignore)
;; (int-to-string i))
;; (defun db-string->integer (s &rest ignore)
;; (string->integer s))
;; (defun db-integer-or-nil->string (i &rest ignore)
;; (integer-or-nil->string i))
;; (defun db-string->integer-or-nil (s &rest ignore)
;; (string->integer-or-nil s))
;; (proclaim-inline db-integer->string db-string->integer
;; db-integer-or-nil->string db-string->integer-or-nil)
;;;
;;; Numbers (a number is an integer or a float)
;;;
;; Emacs 19.28 bug: isfloat_string fails when its argument has trailing spaces,
;; so (string-to-number "5.4") => 5.4 but (string-to-number "5.4 ") => 5 .
(defun string-to-number-trim (string)
(if (string-match " " string)
;; chop everything after the first space
(string-to-number (substring string 0 (match-beginning 0)))
(string-to-number string)))
(let ((ds (make-displayspec)))
(displayspec-set-indent ds nil)
(displayspec-set-actual->display ds (function number-to-string))
(displayspec-set-display->actual ds (function string-to-number-trim))
(define-displaytype-from-displayspec 'number ds))
(let ((rs (make-recordfieldspec)))
(recordfieldspec-set-type rs 'number)
(recordfieldspec-set-default-value rs 0)
(recordfieldspec-set-actual->stored rs (function number-to-string))
(recordfieldspec-set-stored->actual rs (function string-to-number-trim))
(recordfieldspec-set-order-fn rs (function number-order))
(recordfieldspec-set-sort-fn rs (function <))
(recordfieldspec-set-match-function rs (function =))
(recordfieldspec-set-help-info rs "A number.")
(define-recordfieldtype-from-recordfieldspec 'number rs))
(let ((ds (make-displayspec)))
(displayspec-set-indent ds nil)
(displayspec-set-actual->display ds (function number-or-nil->string))
(displayspec-set-display->actual ds (function string->number-or-nil))
(define-displaytype-from-displayspec 'number-or-nil ds))
(let ((rs (make-recordfieldspec)))
(recordfieldspec-set-type rs 'number-or-nil)
(recordfieldspec-set-default-value rs 0)
(recordfieldspec-set-actual->stored rs (function number-or-nil->string))
(recordfieldspec-set-stored->actual rs (function string->number-or-nil))
(recordfieldspec-set-order-fn rs (function number-or-nil-order-nil-greatest))
;; (recordfieldspec-set-sort-fn rs (function <))
(recordfieldspec-set-match-function rs (function equal))
(recordfieldspec-set-help-info rs "A number, or nil.")
(define-recordfieldtype-from-recordfieldspec 'number-or-nil rs))
;;;
;;; Sorting and ordering
;;;
(defsubst number-order (a b)
"Return -1, 0, or 1 depending on whether A < B, A = B, or A > B."
(cond ((= a b) 0)
((< a b) -1)
(t 1)))
(defun number-or-nil-order-nil-greatest (a b)
"Like number-order, but nil is treated as greater than any integer.
This puts nil after integers in an increasing list."
(cond ((and a b)
(number-order a b))
(a -1)
(b 1)
(t 0)))
(defun number-or-nil-order-nil-least (a b)
"Like number-order, but nil is treated as smaller than any integer.
This puts nil after integers in a decreasing list."
(cond ((and a b)
(number-order a b))
(a 1)
(b -1)
(t 0)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Booleans
;;;
;; There could be a one-char boolean as well; implement it later.
(let ((ds (make-displayspec)))
(displayspec-set-indent ds nil)
(displayspec-set-min-width ds 3)
(displayspec-set-max-width ds 3)
(displayspec-set-actual->display ds (function boolean->yes-no-string))
(displayspec-set-display->actual ds (function yes-no-string->boolean))
(define-displaytype-from-displayspec 'yes-no ds))
(let ((rs (make-recordfieldspec)))
(recordfieldspec-set-type rs 'boolean)
(recordfieldspec-set-default-value rs nil)
(recordfieldspec-set-actual->stored rs (function boolean->yn-string))
(recordfieldspec-set-stored->actual rs (function yn-string->boolean))
(recordfieldspec-set-order-fn rs (function boolean-order-function))
(recordfieldspec-set-sort-fn rs (function boolean-lessp))
(recordfieldspec-set-match-function rs (function eq))
(recordfieldspec-set-help-info rs "A boolean value.")
(define-recordfieldtype-from-recordfieldspec 'boolean rs))
(defsubst boolean->yes-no-string (boolean)
(if boolean "Yes" "No"))
(defun yes-no-string->boolean (yes-or-no)
(let ((downcased-yes-or-no (downcase yes-or-no)))
(cond ((string= "yes" downcased-yes-or-no)
t)
((string= "no" downcased-yes-or-no)
nil)
(t
(error "`%s' is not `Yes' or `No'." yes-or-no)))))
;; (defun db-boolean->yes-no-string (boolean &rest ignore)
;; (boolean->yes-no-string boolean))
;; (defun db-yes-no-string->boolean (yes-or-no)
;; (yes-no-string->boolean yes-or-no))
;; (proclaim-inline db-boolean->yes-no-string db-yes-no-string->boolean)
(defsubst boolean->yn-string (boolean)
(if boolean "Y" "N"))
(defsubst yn-string->boolean (y-or-n)
(string= "y" (downcase y-or-n)))
(defun string-is-yn-p (string)
(and (= 1 (length string))
(or (string= "Y" string)
(string= "y" string)
(string= "N" string)
(string= "n" string))))
;; t < nil so that in the "increasing" ordering "true" things occur before
;; false ones. (This is somewhat arbitrary.)
(defun boolean-order-function (bool1 bool2)
(cond ((or (and bool1 bool2)
(not (or bool1 bool2)))
0)
(bool1
-1)
(t
1)))
(defsubst boolean-lessp (bool1 bool2)
(and (not bool1) bool2))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Strings and variants
;;;
;; Type: string
(let ((ds (make-displayspec)))
(displayspec-set-max-height ds nil)
(displayspec-set-indent ds t)
;; use quote instead of function because of actual->display-call
(displayspec-set-match-actual->display ds 'string-match-actual->display)
(displayspec-set-match-display->actual ds 'string-match-display->actual)
(define-displaytype-from-displayspec 'string ds))
(let ((rs (make-recordfieldspec)))
(recordfieldspec-set-type rs 'string)
(recordfieldspec-set-default-value rs "")
(recordfieldspec-set-order-fn rs (function string-order-ci))
(recordfieldspec-set-sort-fn rs (function string-lessp-ci))
(recordfieldspec-set-match-function rs (function string-match-function))
;; String is the default type; I'm not sure I want
;; help-info for it.
;; (recordfieldspec-set-help-info rs "A string.")
(define-recordfieldtype-from-recordfieldspec 'string rs))
;; Type: one-line-string
(let ((ds (copy-displayspec (displaytype->displayspec 'string))))
(displayspec-set-min-height ds 1)
(displayspec-set-max-height ds 1)
(displayspec-set-indent ds nil)
(define-displaytype-from-displayspec 'one-line-string ds))
;; This recordfieldspec is gratuitous. Isn't it?
(let ((rs (copy-recordfieldspec (recordfieldtype->recordfieldspec 'string))))
(recordfieldspec-set-type rs 'one-line-string)
(define-recordfieldtype-from-recordfieldspec 'one-line-string rs))
;; Type: string-or-nil
(let ((ds (copy-displayspec (displaytype->displayspec 'string))))
(displayspec-set-actual->display ds (function string-or-nil->string))
(define-displaytype-from-displayspec 'string-or-nil ds))
(let ((rs (copy-recordfieldspec (recordfieldtype->recordfieldspec 'string))))
(recordfieldspec-set-type rs 'string-or-nil)
(recordfieldspec-set-order-fn rs (function string-or-nil-order-ci))
(recordfieldspec-set-sort-fn rs (function string-or-nil-lessp-ci))
(recordfieldspec-set-match-function rs (function string-or-nil-match-function))
(define-recordfieldtype-from-recordfieldspec 'string-or-nil rs))
;; Type: nil-or-string
(let ((ds (copy-displayspec (displaytype->displayspec 'string-or-nil))))
(displayspec-set-display->actual ds (function string->nil-or-string))
(define-displaytype-from-displayspec 'nil-or-string ds))
(let ((rs (copy-recordfieldspec (recordfieldtype->recordfieldspec 'string-or-nil))))
(recordfieldspec-set-type rs 'nil-or-string)
(define-recordfieldtype-from-recordfieldspec 'nil-or-string rs))
;; Type: one-line-string-or-nil
(let ((ds (copy-displayspec (displaytype->displayspec 'one-line-string))))
(displayspec-set-actual->display ds (function string-or-nil->string))
(define-displaytype-from-displayspec 'one-line-string-or-nil ds))
(let ((rs (copy-recordfieldspec (recordfieldtype->recordfieldspec 'one-line-string))))
(recordfieldspec-set-type rs 'one-line-string-or-nil)
(recordfieldspec-set-order-fn rs (function string-or-nil-order-ci))
(recordfieldspec-set-sort-fn rs (function string-or-nil-lessp-ci))
(recordfieldspec-set-match-function rs (function string-or-nil-match-function))
(define-recordfieldtype-from-recordfieldspec 'one-line-string-or-nil rs))
;; Helping functions for type string
(defsubst string-lessp-ci (string1 string2)
"Case-insensitive version of string-lessp."
(string-lessp (downcase string1) (downcase string2)))
(defun string-order-ci (string1 string2)
"Return -1, 0, or 1 depending on whether STRING1 is lexicographically less
than, equal to, or greater than STRING2. Case-insensitive."
(let ((s1 (downcase string1))
(s2 (downcase string2)))
(cond ((string= s1 s2) 0)
((string-lessp s1 s2) -1)
(t 1))))
;;; Matching strings
;; These variables used to be in db-search, but they're not used there.
(defvar dbm-string-regexp-prefix "^[ \t]*\\(/\\|regexp[ \t]+\\)")
(defvar dbm-string-regexp-prefix-string "/")
;; Pattern is a list of 'regexp and a regexp or a list of 'string, a
;; regexp, and a string.
(defun make-regexp-pattern (regexp)
(list 'regexp regexp))
(defun regexp-pattern-regexp (regexp-pattern)
(car (cdr regexp-pattern)))
(defun make-string-pattern (string &optional regexp)
(list 'string (or regexp (regexp-quote string)) string))
(defun string-pattern-regexp (string-pattern)
(car (cdr string-pattern)))
(defun string-pattern-string (string-pattern)
(car (cdr (cdr string-pattern))))
(defsubst string-match-function (pattern string)
;; The second element of pattern is a regexp whether the pattern is a
;; string or a regexp.
;; However, I also deal with the case where pattern is incorrectly a string.
(string-match (if (stringp pattern) pattern (car (cdr pattern)))
string))
;; Return a pattern
(defun string-match-display->actual (string)
(if (string-match dbm-string-regexp-prefix string)
(make-regexp-pattern (substring string (match-end 0)))
(make-string-pattern string)))
(defun string-match-actual->display (pattern)
(cond ((eq (car pattern) 'string)
(string-pattern-string pattern))
((eq (car pattern) 'regexp)
(concat dbm-string-regexp-prefix-string
(regexp-pattern-regexp pattern)))
(t
(error "string-match-actual->display: bad pattern %s" pattern))))
;; Helping functions for type string-or-nil
(defsubst string-or-nil->string (s-o-n)
(or s-o-n ""))
(defsubst string-or-nil-lessp-ci (s-o-n-1 s-o-n-2)
(string-lessp-ci (string-or-nil->string s-o-n-1)
(string-or-nil->string s-o-n-2)))
(defsubst string-or-nil-order-ci (s-o-n-1 s-o-n-2)
(string-order-ci (or s-o-n-1 "") (or s-o-n-2 "")))
(defsubst string-or-nil-match-function (pattern s-o-n)
(string-match (if (stringp pattern)
pattern
(if pattern (car (cdr pattern)) ""))
(or s-o-n "")))
;; Helping functions for type nil-or-string
(defsubst string->nil-or-string (string)
(if (equal "" string)
nil
string))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Last names
;;;
;; Note stringency of equality test: capitalization and spacing matter.
(defun order-last-names (last-name-1 last-name-2)
"Return -1, 0, or 1 depending on whether LAST-NAME-1 is lexographically
less than, equal to, or greater than LAST-NAME-2."
(cond ((equal last-name-1 last-name-2)
0)
((string-lessp (canonicalize-name last-name-1)
(canonicalize-name last-name-2))
-1)
(t
1)))
;; Remove spaces and quotation marks, and ignore capitalization.
(defun canonicalize-name (last-name)
(let ((result ""))
(while (string-match "[ ']+" last-name)
(setq result (concat result (substring last-name 0 (match-beginning 0)))
last-name (substring last-name (match-end 0))))
(downcase (concat result last-name))))
(make-obsolete 'make-name-comparable 'canonicalize-name)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; First names
;;;
(defconst nicknames
'(
("Abraham" "Abe")
("Charles" "Charlie")
("David" "Dave")
("Elizabeth" "Beth" "Liz")
("Gerald" "Gerry")
("James" "Jamey" "Jamie")
("Leonard" "Lenny")
("Michael" "Mike")
("Richard" "Dick" "Dicky" "Rick" "Ricky" "Rico")
("Robert" "Bob")
("Theodore" "Ted")
)
"List of nicknames which are not prefixes of the full name.
It is by no means comprehensive.")
(defun same-first-name-p (fname1 fname2)
(or (nicknamep fname1 fname2)
(nicknamep fname2 fname1)))
(defun nicknamep (nickname fullname)
(or (string-match (concat "^" (regexp-quote nickname)) fullname)
(member nickname
;; I think this db-string-split is utterly unnecessary.
;; [That was for a particular application, I believe.]
(cdr (assoc (car (db-string-split-first-word fullname))
nicknames)))))
(defun order-first-names (first-name-1 first-name-2)
(let ((first-middle-1 (db-string-split-first-word first-name-1))
(first-middle-2 (db-string-split-first-word first-name-2)))
(cond ((or
;; This first test isn't necessary.
;; [Is it?]
(same-first-name-p (car first-middle-1) (car first-middle-2))
(same-first-name-p first-name-1 first-name-2))
0)
((string-lessp (car first-middle-1) (car first-middle-2))
-1)
(t
1))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Full names
;;;
(defun standardize-name (name)
;; remove leading honorifics
(if (string-match (concat "^\\(\\(Dr\\|Mrs?\\|Prof\\|1?Lt\\)\\.?"
"\\|Miss\\|Ensign\\|Doctor\\) ") name)
(setq name (substring name (match-end 0))))
;; remove trailing gubbish
(setq name (substring name
0 (string-match ",? \\(PhD\\|MD\\|Esq\\.?\\|Esquire\\)$" name)))
;; add periods after initials
(while (string-match "\\(^\\| \\)[A-Z]\\([ ,]\\|$\\)" name)
(setq name (concat (substring name 0 (match-beginning 2))
"."
(substring name (match-beginning 2)))))
name)
;; preferred suffixes
(defvar jr-assoc-list
'(("3d" . "3rd")))
;; return list of (suffixless-name suffix)
(defun name->name-jr (name)
(if (string-match ",? +\\(jr\\.?\\|sr\\.?\\|3r?d\\|[45]th\\|I+\\)$" name)
(list (substring name 0 (match-beginning 0))
(let ((jr (substring name (match-beginning 1))))
(or (cdr (assoc (downcase jr) jr-assoc-list))
jr)))
(list name "")))
;; return list of (fname lname jr)
(defun name->first-last-jr (fullname)
(let* ((name-jr (name->name-jr (standardize-name fullname)))
(split-names (db-string-split-last-word (car name-jr)
"\\(La\\|de\\) [A-Z-]+")))
(append split-names (cdr name-jr))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Places
;;;
(defvar UK-postal-code-regexp
"[A-Z][0-9][A-Z] ?[0-9][A-Z][0-9]")
(defvar zip-code-regexp
"[0-9][0-9][0-9][0-9][0-9]\\(-?[0-9][0-9][0-9][0-9]\\)?")
(defvar postal-code-regexp
(mapconcat
(function identity)
(list
;; US zip code
zip-code-regexp
;; UK postal code
UK-postal-code-regexp
;; other
"[0-9][0-9A-Z]*")
"\\|"))
(defsubst postal-code-p (string)
(string-match postal-code-regexp string))
;; Full state-alist and associated functions added by
;; Alan Stebbens, UCSB, Sep 18 '92.
(defvar state-alist
'(
( "AK" . "Alaska" )
( "AL" . "Alabama" )
( "AR" . "Arkansas" )
( "AZ" . "Arizona" )
( "CA" . "California" )
( "CO" . "Colorado" )
( "CT" . "Conneticut" )
( "DC" . "District of Columbia" )
( "DE" . "Deleware" )
( "FL" . "Florida" )
( "GA" . "Georgia" )
( "GU" . "Guam" )
( "HI" . "Hawaii" )
( "ID" . "Idaho" )
( "IL" . "Illinois" )
( "IN" . "Indiana" )
( "IA" . "Iowa" )
( "KS" . "Kansas" )
( "KY" . "Kentucky" )
( "LA" . "Louisiana" )
( "MA" . "Massachusetts" )
( "MD" . "Maryland" )
( "ME" . "Maine" )
( "MI" . "Michigan" )
( "MN" . "Minnessota" )
( "MS" . "Mississippi" )
( "MO" . "Missouri" )
( "MT" . "Montana" )
( "NE" . "Nebraska" )
( "NH" . "New Hampshire" )
( "NC" . "North Carolina" )
( "ND" . "North Dakota" )
( "NJ" . "New Jersey" )
( "NM" . "New Mexico" )
( "NV" . "Nevada" )
( "NY" . "New York" )
( "OH" . "Ohio" )
( "OK" . "Oklahoma" )
( "OR" . "Oregon" )
( "PA" . "Pennsylvania" )
( "PR" . "Puerto Rico" )
( "RI" . "Rhode Island" )
( "SC" . "South Carolina" )
( "SD" . "South Dakota" )
( "TX" . "Texas" )
( "TN" . "Tennessee" )
( "UT" . "Utah" )
( "VA" . "Virginia" )
( "VT" . "Vermont" )
( "WA" . "Washington" )
( "WI" . "Wisconson" )
( "WV" . "West Virginia" )
( "WY" . "Wyoming" )
)
"An alist of ABBREV and FULLNAME of each of the United States, and its
territories. Used by \"full-state-name\" and \"abbreviate-state\"."
)
(defun statep (string)
"Return non-nil if STRING is a valid state abbreviation."
(full-state-name string))
(defun full-state-name (abbrev)
"Return the full state name for ABBREV, or nil if not found."
(and (= 2 (length abbrev))
;; (let ((elt (assoc (upcase abbrev) state-alist)))
;; (if elt (cdr elt)))
(cdr (assoc (upcase abbrev) state-alist))))
(defun abbreviate-state (state)
"Return the postal abbreviation for STATE, or nil if not found."
(car (db-rassoc (capitalize state) state-alist)))
;; (defun abbreviate-state (state)
;; "Return the postal abbreviation for STATE."
;; (let ((alist state-alist)
;; (key (capitalize state)))
;; (while (and alist
;; (not (equal key (cdr (car alist)))))
;; (setq alist (cdr alist)))
;; (if alist (car (car alist)))))
(defvar country-list
'("Canada"
"Israel"
"Switzerland"
"Japan"
"West Germany"
"United States"
"Zambia")
"List of country names. Far from complete.")
(defsubst countryp (string)
(member (capitalize string) country-list))
;; Dates and times moved to db-time.el
;;; db-types.el ends here
|