| | 368 | ;;; Abbrev properties. |
|---|
| | 369 | |
|---|
| | 370 | (defun abbrev-table-get (table prop) |
|---|
| | 371 | "Get the PROP property of abbrev table TABLE." |
|---|
| | 372 | (let ((sym (intern-soft "" table))) |
|---|
| | 373 | (if sym (get sym prop)))) |
|---|
| | 374 | |
|---|
| | 375 | (defun abbrev-table-put (table prop val) |
|---|
| | 376 | "Set the PROP property of abbrev table TABLE to VAL." |
|---|
| | 377 | (let ((sym (intern "" table))) |
|---|
| | 378 | (set sym nil) ; Make sure it won't be confused for an abbrev. |
|---|
| | 379 | (put sym prop val))) |
|---|
| | 380 | |
|---|
| | 381 | (defalias 'abbrev-get 'get |
|---|
| | 382 | "Get the property PROP of abbrev ABBREV |
|---|
| | 383 | |
|---|
| | 384 | \(fn ABBREV PROP)") |
|---|
| | 385 | |
|---|
| | 386 | (defalias 'abbrev-put 'put |
|---|
| | 387 | "Set the property PROP of abbrev ABREV to value VAL. |
|---|
| | 388 | See `define-abbrev' for the effect of some special properties. |
|---|
| | 389 | |
|---|
| | 390 | \(fn ABBREV PROP VAL)") |
|---|
| | 391 | |
|---|
| | 392 | (defmacro abbrev-with-wrapper-hook (var &rest body) |
|---|
| | 393 | "Run BODY wrapped with the VAR hook. |
|---|
| | 394 | VAR is a special hook: its functions are called with one argument which |
|---|
| | 395 | is the \"original\" code (the BODY), so the hook function can wrap the |
|---|
| | 396 | original function, can call it several times, or even not call it at all. |
|---|
| | 397 | VAR is normally a symbol (a variable) in which case it is treated like a hook, |
|---|
| | 398 | with a buffer-local and a global part. But it can also be an arbitrary expression. |
|---|
| | 399 | This is similar to an `around' advice." |
|---|
| | 400 | (declare (indent 1) (debug t)) |
|---|
| | 401 | ;; We need those two gensyms because CL's lexical scoping is not available |
|---|
| | 402 | ;; for function arguments :-( |
|---|
| | 403 | (let ((funs (make-symbol "funs")) |
|---|
| | 404 | (global (make-symbol "global"))) |
|---|
| | 405 | ;; Since the hook is a wrapper, the loop has to be done via |
|---|
| | 406 | ;; recursion: a given hook function will call its parameter in order to |
|---|
| | 407 | ;; continue looping. |
|---|
| | 408 | `(labels ((runrestofhook (,funs ,global) |
|---|
| | 409 | ;; `funs' holds the functions left on the hook and `global' |
|---|
| | 410 | ;; holds the functions left on the global part of the hook |
|---|
| | 411 | ;; (in case the hook is local). |
|---|
| | 412 | (lexical-let ((funs ,funs) |
|---|
| | 413 | (global ,global)) |
|---|
| | 414 | (if (consp funs) |
|---|
| | 415 | (if (eq t (car funs)) |
|---|
| | 416 | (runrestofhook (append global (cdr funs)) nil) |
|---|
| | 417 | (funcall (car funs) |
|---|
| | 418 | (lambda () (runrestofhook (cdr funs) global)))) |
|---|
| | 419 | ;; Once there are no more functions on the hook, run |
|---|
| | 420 | ;; the original body. |
|---|
| | 421 | ,@body)))) |
|---|
| | 422 | (runrestofhook ,var |
|---|
| | 423 | ;; The global part of the hook, if any. |
|---|
| | 424 | ,(if (symbolp var) |
|---|
| | 425 | `(if (local-variable-p ',var) |
|---|
| | 426 | (default-value ',var))))))) |
|---|
| | 427 | |
|---|
| | 428 | |
|---|
| | 429 | ;;; Code that used to be implemented in src/abbrev.c |
|---|
| | 430 | |
|---|
| | 431 | (defvar abbrev-table-name-list '(fundamental-mode-abbrev-table |
|---|
| | 432 | global-abbrev-table) |
|---|
| | 433 | "List of symbols whose values are abbrev tables.") |
|---|
| | 434 | |
|---|
| | 435 | (defun make-abbrev-table (&optional props) |
|---|
| | 436 | "Create a new, empty abbrev table object. |
|---|
| | 437 | PROPS is a " |
|---|
| | 438 | ;; The value 59 is an arbitrary prime number. |
|---|
| | 439 | (let ((table (make-vector 59 0))) |
|---|
| | 440 | ;; Each abbrev-table has a `modiff' counter which can be used to detect |
|---|
| | 441 | ;; when an abbreviation was added. An example of use would be to |
|---|
| | 442 | ;; construct :regexp dynamically as the union of all abbrev names, so |
|---|
| | 443 | ;; `modiff' can let us detect that an abbrev was added and hence :regexp |
|---|
| | 444 | ;; needs to be refreshed. |
|---|
| | 445 | ;; The presence of `modiff' entry is also used as a tag indicating this |
|---|
| | 446 | ;; vector is really an abbrev-table. |
|---|
| | 447 | (abbrev-table-put table :abbrev-table-modiff 0) |
|---|
| | 448 | (while (consp props) |
|---|
| | 449 | (abbrev-table-put table (pop props) (pop props))) |
|---|
| | 450 | table)) |
|---|
| | 451 | |
|---|
| | 452 | (defun abbrev-table-p (object) |
|---|
| | 453 | (and (vectorp object) |
|---|
| | 454 | (numberp (abbrev-table-get object :abbrev-table-modiff)))) |
|---|
| | 455 | |
|---|
| | 456 | (defvar global-abbrev-table (make-abbrev-table) |
|---|
| | 457 | "The abbrev table whose abbrevs affect all buffers. |
|---|
| | 458 | Each buffer may also have a local abbrev table. |
|---|
| | 459 | If it does, the local table overrides the global one |
|---|
| | 460 | for any particular abbrev defined in both.") |
|---|
| | 461 | |
|---|
| | 462 | (defvar abbrev-minor-mode-table-alist nil |
|---|
| | 463 | "Alist of abbrev tables to use for minor modes. |
|---|
| | 464 | Each element looks like (VARIABLE . ABBREV-TABLE); |
|---|
| | 465 | ABBREV-TABLE is active whenever VARIABLE's value is non-nil.") |
|---|
| | 466 | |
|---|
| | 467 | (defvar fundamental-mode-abbrev-table |
|---|
| | 468 | (let ((table (make-abbrev-table))) |
|---|
| | 469 | ;; Set local-abbrev-table's default to be fundamental-mode-abbrev-table. |
|---|
| | 470 | (setq-default local-abbrev-table table) |
|---|
| | 471 | table) |
|---|
| | 472 | "The abbrev table of mode-specific abbrevs for Fundamental Mode.") |
|---|
| | 473 | |
|---|
| | 474 | (defvar abbrevs-changed nil |
|---|
| | 475 | "Set non-nil by defining or altering any word abbrevs. |
|---|
| | 476 | This causes `save-some-buffers' to offer to save the abbrevs.") |
|---|
| | 477 | |
|---|
| | 478 | (defcustom abbrev-all-caps nil |
|---|
| | 479 | "Non-nil means expand multi-word abbrevs all caps if abbrev was so." |
|---|
| | 480 | :type 'boolean |
|---|
| | 481 | :group 'abbrev-mode) |
|---|
| | 482 | |
|---|
| | 483 | (defvar abbrev-start-location nil |
|---|
| | 484 | "Buffer position for `expand-abbrev' to use as the start of the abbrev. |
|---|
| | 485 | When nil, use the word before point as the abbrev. |
|---|
| | 486 | Calling `expand-abbrev' sets this to nil.") |
|---|
| | 487 | |
|---|
| | 488 | (defvar abbrev-start-location-buffer nil |
|---|
| | 489 | "Buffer that `abbrev-start-location' has been set for. |
|---|
| | 490 | Trying to expand an abbrev in any other buffer clears `abbrev-start-location'.") |
|---|
| | 491 | |
|---|
| | 492 | (defvar last-abbrev nil |
|---|
| | 493 | "The abbrev-symbol of the last abbrev expanded. See `abbrev-symbol'.") |
|---|
| | 494 | |
|---|
| | 495 | (defvar last-abbrev-text nil |
|---|
| | 496 | "The exact text of the last abbrev expanded. |
|---|
| | 497 | nil if the abbrev has already been unexpanded.") |
|---|
| | 498 | |
|---|
| | 499 | (defvar last-abbrev-location 0 |
|---|
| | 500 | "The location of the start of the last abbrev expanded.") |
|---|
| | 501 | |
|---|
| | 502 | ;; (defvar local-abbrev-table fundamental-mode-abbrev-table |
|---|
| | 503 | ;; "Local (mode-specific) abbrev table of current buffer.") |
|---|
| | 504 | ;; (make-variable-buffer-local 'local-abbrev-table) |
|---|
| | 505 | |
|---|
| | 506 | (defcustom pre-abbrev-expand-hook nil |
|---|
| | 507 | "Function or functions to be called before abbrev expansion is done. |
|---|
| | 508 | This is the first thing that `expand-abbrev' does, and so this may change |
|---|
| | 509 | the current abbrev table before abbrev lookup happens." |
|---|
| | 510 | :type 'hook |
|---|
| | 511 | :group 'abbrev-mode) |
|---|
| | 512 | (make-obsolete-variable 'pre-abbrev-expand-hook 'abbrev-expand-functions "23.1") |
|---|
| | 513 | |
|---|
| | 514 | (defun clear-abbrev-table (table) |
|---|
| | 515 | "Undefine all abbrevs in abbrev table TABLE, leaving it empty." |
|---|
| | 516 | (setq abbrevs-changed t) |
|---|
| | 517 | (let* ((sym (intern-soft "" table))) |
|---|
| | 518 | (dotimes (i (length table)) |
|---|
| | 519 | (aset table i 0)) |
|---|
| | 520 | ;; Preserve the table's properties. |
|---|
| | 521 | (assert sym) |
|---|
| | 522 | (let ((newsym (intern "" table))) |
|---|
| | 523 | (set newsym nil) ; Make sure it won't be confused for an abbrev. |
|---|
| | 524 | (setplist newsym (symbol-plist sym))) |
|---|
| | 525 | (abbrev-table-put table :abbrev-table-modiff |
|---|
| | 526 | (1+ (abbrev-table-get table :abbrev-table-modiff))))) |
|---|
| | 527 | |
|---|
| | 528 | (defun define-abbrev (table name expansion &optional hook &rest props) |
|---|
| | 529 | "Define an abbrev in TABLE named NAME, to expand to EXPANSION and call HOOK. |
|---|
| | 530 | NAME must be a string, and should be lower-case. |
|---|
| | 531 | EXPANSION should usually be a string. |
|---|
| | 532 | To undefine an abbrev, define it with EXPANSION = nil. |
|---|
| | 533 | If HOOK is non-nil, it should be a function of no arguments; |
|---|
| | 534 | it is called after EXPANSION is inserted. |
|---|
| | 535 | If EXPANSION is not a string, the abbrev is a special one, |
|---|
| | 536 | which does not expand in the usual way but only runs HOOK. |
|---|
| | 537 | |
|---|
| | 538 | PROPS is a property list. The following properties are special: |
|---|
| | 539 | - `:count': the value for the abbrev's usage-count, which is incremented each time |
|---|
| | 540 | the abbrev is used (the default is zero). |
|---|
| | 541 | - `:system': if non-nil, says that this is a \"system\" abbreviation |
|---|
| | 542 | which should not be saved in the user's abbreviation file. |
|---|
| | 543 | Unless `:system' is `force', a system abbreviation will not |
|---|
| | 544 | overwrite a non-system abbreviation of the same name. |
|---|
| | 545 | - `:case-fixed': non-nil means that abbreviations are looked up without |
|---|
| | 546 | case-folding, and the expansion is not capitalized/upcased. |
|---|
| | 547 | - `:enable-function': a function of no argument which returns non-nil iff the |
|---|
| | 548 | abbrev should be used for a particular call of `expand-abbrev'. |
|---|
| | 549 | |
|---|
| | 550 | An obsolete but still supported calling form is: |
|---|
| | 551 | |
|---|
| | 552 | \(define-abbrev TABLE NAME EXPANSION &optional HOOK COUNT SYSTEM)." |
|---|
| | 553 | (when (and (consp props) (or (null (car props)) (numberp (car props)))) |
|---|
| | 554 | ;; Old-style calling convention. |
|---|
| | 555 | (setq props (list* :count (car props) |
|---|
| | 556 | (if (cadr props) (list :system (cadr props)))))) |
|---|
| | 557 | (unless (plist-get props :count) |
|---|
| | 558 | (setq props (plist-put props :count 0))) |
|---|
| | 559 | (let ((system-flag (plist-get props :system)) |
|---|
| | 560 | (sym (intern name table))) |
|---|
| | 561 | ;; Don't override a prior user-defined abbrev with a system abbrev, |
|---|
| | 562 | ;; unless system-flag is `force'. |
|---|
| | 563 | (unless (and (not (memq system-flag '(nil force))) |
|---|
| | 564 | (boundp sym) (symbol-value sym) |
|---|
| | 565 | (not (abbrev-get sym :system))) |
|---|
| | 566 | (unless (or system-flag |
|---|
| | 567 | (and (boundp sym) (fboundp sym) |
|---|
| | 568 | ;; load-file-name |
|---|
| | 569 | (equal (symbol-value sym) expansion) |
|---|
| | 570 | (equal (symbol-function sym) hook))) |
|---|
| | 571 | (setq abbrevs-changed t)) |
|---|
| | 572 | (set sym expansion) |
|---|
| | 573 | (fset sym hook) |
|---|
| | 574 | (setplist sym |
|---|
| | 575 | ;; Don't store the `force' value of `system-flag' into |
|---|
| | 576 | ;; the :system property. |
|---|
| | 577 | (if (eq 'force system-flag) (plist-put props :system t) props)) |
|---|
| | 578 | (abbrev-table-put table :abbrev-table-modiff |
|---|
| | 579 | (1+ (abbrev-table-get table :abbrev-table-modiff)))) |
|---|
| | 580 | name)) |
|---|
| | 581 | |
|---|
| | 582 | (defun abbrev--check-chars (abbrev global) |
|---|
| | 583 | "Check if the characters in ABBREV have word syntax in either the |
|---|
| | 584 | current (if global is nil) or standard syntax table." |
|---|
| | 585 | (with-syntax-table |
|---|
| | 586 | (cond ((null global) (standard-syntax-table)) |
|---|
| | 587 | ;; ((syntax-table-p global) global) |
|---|
| | 588 | (t (syntax-table))) |
|---|
| | 589 | (when (string-match "\\W" abbrev) |
|---|
| | 590 | (let ((badchars ()) |
|---|
| | 591 | (pos 0)) |
|---|
| | 592 | (while (string-match "\\W" abbrev pos) |
|---|
| | 593 | (pushnew (aref abbrev (match-beginning 0)) badchars) |
|---|
| | 594 | (setq pos (1+ pos))) |
|---|
| | 595 | (error "Some abbrev characters (%s) are not word constituents %s" |
|---|
| | 596 | (apply 'string (nreverse badchars)) |
|---|
| | 597 | (if global "in the standard syntax" "in this mode")))))) |
|---|
| | 598 | |
|---|
| | 599 | (defun define-global-abbrev (abbrev expansion) |
|---|
| | 600 | "Define ABBREV as a global abbreviation for EXPANSION. |
|---|
| | 601 | The characters in ABBREV must all be word constituents in the standard |
|---|
| | 602 | syntax table." |
|---|
| | 603 | (interactive "sDefine global abbrev: \nsExpansion for %s: ") |
|---|
| | 604 | (abbrev--check-chars abbrev 'global) |
|---|
| | 605 | (define-abbrev global-abbrev-table (downcase abbrev) expansion)) |
|---|
| | 606 | |
|---|
| | 607 | (defun define-mode-abbrev (abbrev expansion) |
|---|
| | 608 | "Define ABBREV as a mode-specific abbreviation for EXPANSION. |
|---|
| | 609 | The characters in ABBREV must all be word-constituents in the current mode." |
|---|
| | 610 | (interactive "sDefine mode abbrev: \nsExpansion for %s: ") |
|---|
| | 611 | (unless local-abbrev-table |
|---|
| | 612 | (error "Major mode has no abbrev table")) |
|---|
| | 613 | (abbrev--check-chars abbrev nil) |
|---|
| | 614 | (define-abbrev local-abbrev-table (downcase abbrev) expansion)) |
|---|
| | 615 | |
|---|
| | 616 | (defun abbrev--active-tables (&optional tables) |
|---|
| | 617 | "Return the list of abbrev tables currently active. |
|---|
| | 618 | TABLES if non-nil overrides the usual rules. It can hold |
|---|
| | 619 | either a single abbrev table or a list of abbrev tables." |
|---|
| | 620 | ;; We could just remove the `tables' arg and let callers use |
|---|
| | 621 | ;; (or table (abbrev--active-tables)) but then they'd have to be careful |
|---|
| | 622 | ;; to treat the distinction between a single table and a list of tables. |
|---|
| | 623 | (cond |
|---|
| | 624 | ((consp tables) tables) |
|---|
| | 625 | ((vectorp tables) (list tables)) |
|---|
| | 626 | (t |
|---|
| | 627 | (let ((tables (if (listp local-abbrev-table) |
|---|
| | 628 | (append local-abbrev-table |
|---|
| | 629 | (list global-abbrev-table)) |
|---|
| | 630 | (list local-abbrev-table global-abbrev-table)))) |
|---|
| | 631 | ;; Add the minor-mode abbrev tables. |
|---|
| | 632 | (dolist (x abbrev-minor-mode-table-alist) |
|---|
| | 633 | (when (and (symbolp (car x)) (boundp (car x)) (symbol-value (car x))) |
|---|
| | 634 | (setq tables |
|---|
| | 635 | (if (listp (cdr x)) |
|---|
| | 636 | (append (cdr x) tables) (cons (cdr x) tables))))) |
|---|
| | 637 | tables)))) |
|---|
| | 638 | |
|---|
| | 639 | |
|---|
| | 640 | (defun abbrev-symbol (abbrev &optional table) |
|---|
| | 641 | "Return the symbol representing abbrev named ABBREV. |
|---|
| | 642 | This symbol's name is ABBREV, but it is not the canonical symbol of that name; |
|---|
| | 643 | it is interned in an abbrev-table rather than the normal obarray. |
|---|
| | 644 | The value is nil if that abbrev is not defined. |
|---|
| | 645 | Optional second arg TABLE is abbrev table to look it up in. |
|---|
| | 646 | The default is to try buffer's mode-specific abbrev table, then global table." |
|---|
| | 647 | (let ((tables (abbrev--active-tables table)) |
|---|
| | 648 | sym) |
|---|
| | 649 | (while (and tables (not (symbol-value sym))) |
|---|
| | 650 | (let* ((table (pop tables)) |
|---|
| | 651 | (case-fold (not (abbrev-table-get table :case-fixed)))) |
|---|
| | 652 | (setq tables (append (abbrev-table-get table :parents) tables)) |
|---|
| | 653 | ;; In case the table doesn't set :case-fixed but some of the |
|---|
| | 654 | ;; abbrevs do, we have to be careful. |
|---|
| | 655 | (setq sym |
|---|
| | 656 | ;; First try without case-folding. |
|---|
| | 657 | (or (intern-soft abbrev table) |
|---|
| | 658 | (when case-fold |
|---|
| | 659 | ;; We didn't find any abbrev, try case-folding. |
|---|
| | 660 | (let ((sym (intern-soft (downcase abbrev) table))) |
|---|
| | 661 | ;; Only use it if it doesn't require :case-fixed. |
|---|
| | 662 | (and sym (not (abbrev-get sym :case-fixed)) |
|---|
| | 663 | sym))))))) |
|---|
| | 664 | (if (symbol-value sym) |
|---|
| | 665 | sym))) |
|---|
| | 666 | |
|---|
| | 667 | |
|---|
| | 668 | (defun abbrev-expansion (abbrev &optional table) |
|---|
| | 669 | "Return the string that ABBREV expands into in the current buffer. |
|---|
| | 670 | Optionally specify an abbrev table as second arg; |
|---|
| | 671 | then ABBREV is looked up in that table only." |
|---|
| | 672 | (symbol-value (abbrev-symbol abbrev table))) |
|---|
| | 673 | |
|---|
| | 674 | |
|---|
| | 675 | (defun abbrev--before-point () |
|---|
| | 676 | "Try and find an abbrev before point. Return it if found, nil otherwise." |
|---|
| | 677 | (unless (eq abbrev-start-location-buffer (current-buffer)) |
|---|
| | 678 | (setq abbrev-start-location nil)) |
|---|
| | 679 | |
|---|
| | 680 | (let ((tables (abbrev--active-tables)) |
|---|
| | 681 | (pos (point)) |
|---|
| | 682 | start end name res) |
|---|
| | 683 | |
|---|
| | 684 | (if abbrev-start-location |
|---|
| | 685 | (progn |
|---|
| | 686 | (setq start abbrev-start-location) |
|---|
| | 687 | (setq abbrev-start-location nil) |
|---|
| | 688 | ;; Remove the hyphen inserted by `abbrev-prefix-mark'. |
|---|
| | 689 | (if (and (< start (point-max)) |
|---|
| | 690 | (eq (char-after start) ?-)) |
|---|
| | 691 | (delete-region start (1+ start))) |
|---|
| | 692 | (skip-syntax-backward " ") |
|---|
| | 693 | (setq end (point)) |
|---|
| | 694 | (when (> end start) |
|---|
| | 695 | (setq name (buffer-substring start end)) |
|---|
| | 696 | (goto-char pos) ; Restore point. |
|---|
| | 697 | (list (abbrev-symbol name tables) name start end))) |
|---|
| | 698 | |
|---|
| | 699 | (while (and tables (not (car res))) |
|---|
| | 700 | (let* ((table (pop tables)) |
|---|
| | 701 | (enable-fun (abbrev-table-get table :enable-function))) |
|---|
| | 702 | (setq tables (append (abbrev-table-get table :parents) tables)) |
|---|
| | 703 | (setq res |
|---|
| | 704 | (and (or (not enable-fun) (funcall enable-fun)) |
|---|
| | 705 | (looking-back (or (abbrev-table-get table :regexp) |
|---|
| | 706 | "\\<\\(\\w+\\)\\W*") |
|---|
| | 707 | (line-beginning-position)) |
|---|
| | 708 | (setq start (match-beginning 1)) |
|---|
| | 709 | (setq end (match-end 1)) |
|---|
| | 710 | (setq name (buffer-substring start end)) |
|---|
| | 711 | (let ((abbrev (abbrev-symbol name table))) |
|---|
| | 712 | (when abbrev |
|---|
| | 713 | (setq enable-fun (abbrev-get abbrev :enable-function)) |
|---|
| | 714 | (and (or (not enable-fun) (funcall enable-fun)) |
|---|
| | 715 | ;; This will also look it up in parent tables. |
|---|
| | 716 | ;; This is not on purpose, but it seems harmless. |
|---|
| | 717 | (list abbrev name start end)))))) |
|---|
| | 718 | ;; Restore point. |
|---|
| | 719 | (goto-char pos))) |
|---|
| | 720 | res))) |
|---|
| | 721 | |
|---|
| | 722 | (defvar abbrev-expand-functions nil |
|---|
| | 723 | "Wrapper hook around `expand-abbrev'. |
|---|
| | 724 | The functions on this special hook are called with one argument: |
|---|
| | 725 | a function that performs the abbrev expansion. It should return |
|---|
| | 726 | the abbrev symbol if expansion took place.") |
|---|
| | 727 | |
|---|
| | 728 | (defun expand-abbrev () |
|---|
| | 729 | "Expand the abbrev before point, if there is an abbrev there. |
|---|
| | 730 | Effective when explicitly called even when `abbrev-mode' is nil. |
|---|
| | 731 | Returns the abbrev symbol, if expansion took place." |
|---|
| | 732 | (interactive) |
|---|
| | 733 | (run-hooks 'pre-abbrev-expand-hook) |
|---|
| | 734 | (abbrev-with-wrapper-hook abbrev-expand-functions |
|---|
| | 735 | (destructuring-bind (&optional sym name wordstart wordend) |
|---|
| | 736 | (abbrev--before-point) |
|---|
| | 737 | (when sym |
|---|
| | 738 | (let ((value sym)) |
|---|
| | 739 | (unless (or ;; executing-kbd-macro |
|---|
| | 740 | noninteractive |
|---|
| | 741 | (window-minibuffer-p (selected-window))) |
|---|
| | 742 | ;; Add an undo boundary, in case we are doing this for |
|---|
| | 743 | ;; a self-inserting command which has avoided making one so far. |
|---|
| | 744 | (undo-boundary)) |
|---|
| | 745 | ;; Now sym is the abbrev symbol. |
|---|
| | 746 | (setq last-abbrev-text name) |
|---|
| | 747 | (setq last-abbrev sym) |
|---|
| | 748 | (setq last-abbrev-location wordstart) |
|---|
| | 749 | ;; Increment use count. |
|---|
| | 750 | (abbrev-put sym :count (1+ (abbrev-get sym :count))) |
|---|
| | 751 | ;; If this abbrev has an expansion, delete the abbrev |
|---|
| | 752 | ;; and insert the expansion. |
|---|
| | 753 | (when (stringp (symbol-value sym)) |
|---|
| | 754 | (goto-char wordstart) |
|---|
| | 755 | ;; Insert at beginning so that markers at the end (e.g. point) |
|---|
| | 756 | ;; are preserved. |
|---|
| | 757 | (insert (symbol-value sym)) |
|---|
| | 758 | (delete-char (- wordend wordstart)) |
|---|
| | 759 | (let ((case-fold-search nil)) |
|---|
| | 760 | ;; If the abbrev's name is different from the buffer text (the |
|---|
| | 761 | ;; only difference should be capitalization), then we may want |
|---|
| | 762 | ;; to adjust the capitalization of the expansion. |
|---|
| | 763 | (when (and (not (equal name (symbol-name sym))) |
|---|
| | 764 | (string-match "[[:upper:]]" name)) |
|---|
| | 765 | (if (not (string-match "[[:lower:]]" name)) |
|---|
| | 766 | ;; Abbrev was all caps. If expansion is multiple words, |
|---|
| | 767 | ;; normally capitalize each word. |
|---|
| | 768 | (if (and (not abbrev-all-caps) |
|---|
| | 769 | (save-excursion |
|---|
| | 770 | (> (progn (backward-word 1) (point)) |
|---|
| | 771 | (progn (goto-char wordstart) |
|---|
| | 772 | (forward-word 1) (point))))) |
|---|
| | 773 | (upcase-initials-region wordstart (point)) |
|---|
| | 774 | (upcase-region wordstart (point))) |
|---|
| | 775 | ;; Abbrev included some caps. Cap first initial of expansion. |
|---|
| | 776 | (let ((end (point))) |
|---|
| | 777 | ;; Find the initial. |
|---|
| | 778 | (goto-char wordstart) |
|---|
| | 779 | (skip-syntax-forward "^w" (1- end)) |
|---|
| | 780 | ;; Change just that. |
|---|
| | 781 | (upcase-initials-region (point) (1+ (point))) |
|---|
| | 782 | (goto-char end)))))) |
|---|
| | 783 | ;; Now point is at the end of the expansion and the beginning is |
|---|
| | 784 | ;; in last-abbrev-location. |
|---|
| | 785 | (when (symbol-function sym) |
|---|
| | 786 | (let* ((hook (symbol-function sym)) |
|---|
| | 787 | (expanded |
|---|
| | 788 | ;; If the abbrev has a hook function, run it. |
|---|
| | 789 | (funcall hook))) |
|---|
| | 790 | ;; In addition, if the hook function is a symbol with |
|---|
| | 791 | ;; a non-nil `no-self-insert' property, let the value it |
|---|
| | 792 | ;; returned specify whether we consider that an expansion took |
|---|
| | 793 | ;; place. If it returns nil, no expansion has been done. |
|---|
| | 794 | (if (and (symbolp hook) |
|---|
| | 795 | (null expanded) |
|---|
| | 796 | (get hook 'no-self-insert)) |
|---|
| | 797 | (setq value nil)))) |
|---|
| | 798 | value))))) |
|---|
| | 799 | |
|---|
| | 800 | (defun unexpand-abbrev () |
|---|
| | 801 | "Undo the expansion of the last abbrev that expanded. |
|---|
| | 802 | This differs from ordinary undo in that other editing done since then |
|---|
| | 803 | is not undone." |
|---|
| | 804 | (interactive) |
|---|
| | 805 | (save-excursion |
|---|
| | 806 | (unless (or (< last-abbrev-location (point-min)) |
|---|
| | 807 | (> last-abbrev-location (point-max))) |
|---|
| | 808 | (goto-char last-abbrev-location) |
|---|
| | 809 | (when (stringp last-abbrev-text) |
|---|
| | 810 | ;; This isn't correct if last-abbrev's hook was used |
|---|
| | 811 | ;; to do the expansion. |
|---|
| | 812 | (let ((val (symbol-value last-abbrev))) |
|---|
| | 813 | (unless (stringp val) |
|---|
| | 814 | (error "value of abbrev-symbol must be a string")) |
|---|
| | 815 | (delete-region (point) (+ (point) (length val))) |
|---|
| | 816 | ;; Don't inherit properties here; just copy from old contents. |
|---|
| | 817 | (insert last-abbrev-text) |
|---|
| | 818 | (setq last-abbrev-text nil)))))) |
|---|
| | 819 | |
|---|
| | 820 | (defun abbrev--write (sym) |
|---|
| | 821 | "Write the abbrev in a `read'able form. |
|---|
| | 822 | Only writes the non-system abbrevs. |
|---|
| | 823 | Presumes that `standard-output' points to `current-buffer'." |
|---|
| | 824 | (unless (or (null (symbol-value sym)) (abbrev-get sym :system)) |
|---|
| | 825 | (insert " (") |
|---|
| | 826 | (prin1 (symbol-name sym)) |
|---|
| | 827 | (insert " ") |
|---|
| | 828 | (prin1 (symbol-value sym)) |
|---|
| | 829 | (insert " ") |
|---|
| | 830 | (prin1 (symbol-function sym)) |
|---|
| | 831 | (insert " ") |
|---|
| | 832 | (prin1 (abbrev-get sym :count)) |
|---|
| | 833 | (insert ")\n"))) |
|---|
| | 834 | |
|---|
| | 835 | (defun abbrev--describe (sym) |
|---|
| | 836 | (when (symbol-value sym) |
|---|
| | 837 | (prin1 (symbol-name sym)) |
|---|
| | 838 | (if (null (abbrev-get sym :system)) |
|---|
| | 839 | (indent-to 15 1) |
|---|
| | 840 | (insert " (sys)") |
|---|
| | 841 | (indent-to 20 1)) |
|---|
| | 842 | (prin1 (abbrev-get sym :count)) |
|---|
| | 843 | (indent-to 20 1) |
|---|
| | 844 | (prin1 (symbol-value sym)) |
|---|
| | 845 | (when (symbol-function sym) |
|---|
| | 846 | (indent-to 45 1) |
|---|
| | 847 | (prin1 (symbol-function sym))) |
|---|
| | 848 | (terpri))) |
|---|
| | 849 | |
|---|
| | 850 | (defun insert-abbrev-table-description (name &optional readable) |
|---|
| | 851 | "Insert before point a full description of abbrev table named NAME. |
|---|
| | 852 | NAME is a symbol whose value is an abbrev table. |
|---|
| | 853 | If optional 2nd arg READABLE is non-nil, a human-readable description |
|---|
| | 854 | is inserted. Otherwise the description is an expression, |
|---|
| | 855 | a call to `define-abbrev-table', which would |
|---|
| | 856 | define the abbrev table NAME exactly as it is currently defined. |
|---|
| | 857 | |
|---|
| | 858 | Abbrevs marked as \"system abbrevs\" are omitted." |
|---|
| | 859 | (let ((table (symbol-value name)) |
|---|
| | 860 | (symbols ())) |
|---|
| | 861 | (mapatoms (lambda (sym) (if (symbol-value sym) (push sym symbols))) table) |
|---|
| | 862 | (setq symbols (sort symbols 'string-lessp)) |
|---|
| | 863 | (let ((standard-output (current-buffer))) |
|---|
| | 864 | (if readable |
|---|
| | 865 | (progn |
|---|
| | 866 | (insert "(") |
|---|
| | 867 | (prin1 name) |
|---|
| | 868 | (insert ")\n\n") |
|---|
| | 869 | (mapc 'abbrev--describe symbols) |
|---|
| | 870 | (insert "\n\n")) |
|---|
| | 871 | (insert "(define-abbrev-table '") |
|---|
| | 872 | (prin1 name) |
|---|
| | 873 | (insert " '(") |
|---|
| | 874 | (mapc 'abbrev--write symbols) |
|---|
| | 875 | (insert " ))\n\n")) |
|---|
| | 876 | nil))) |
|---|
| | 877 | |
|---|
| | 878 | (defun define-abbrev-table (tablename definitions |
|---|
| | 879 | &optional docstring &rest props) |
|---|
| | 880 | "Define TABLENAME (a symbol) as an abbrev table name. |
|---|
| | 881 | Define abbrevs in it according to DEFINITIONS, which is a list of elements |
|---|
| | 882 | of the form (ABBREVNAME EXPANSION HOOK USECOUNT SYSTEMFLAG). |
|---|
| | 883 | \(If the list is shorter than that, omitted elements default to nil). |
|---|
| | 884 | PROPS is a property list to apply to the table. |
|---|
| | 885 | Properties with special meaning: |
|---|
| | 886 | - `:parents' contains a list of abbrev tables from which this table inherits |
|---|
| | 887 | abbreviations. |
|---|
| | 888 | - `:case-fixed' non-nil means that abbreviations are looked up without |
|---|
| | 889 | case-folding, and the expansion is not capitalized/upcased. |
|---|
| | 890 | - `:regexp' describes the form of abbrevs. It defaults to \\=\\<\\(\\w+\\)\\W* which |
|---|
| | 891 | means that an abbrev can only be a single word. The submatch 1 is treated |
|---|
| | 892 | as the potential name of an abbrev. |
|---|
| | 893 | - `:enable-function' can be set to a function of no argument which returns |
|---|
| | 894 | non-nil iff the abbrevs in this table should be used for this instance |
|---|
| | 895 | of `expand-abbrev'." |
|---|
| | 896 | ;; We used to manually add the docstring, but we also want to record this |
|---|
| | 897 | ;; location as the definition of the variable (in load-history), so we may |
|---|
| | 898 | ;; as well just use `defvar'. |
|---|
| | 899 | (eval `(defvar ,tablename nil ,@(if (stringp docstring) (list docstring)))) |
|---|
| | 900 | (let ((table (if (boundp tablename) (symbol-value tablename)))) |
|---|
| | 901 | (unless table |
|---|
| | 902 | (setq table (make-abbrev-table props)) |
|---|
| | 903 | (set tablename table) |
|---|
| | 904 | (push tablename abbrev-table-name-list)) |
|---|
| | 905 | (dolist (elt definitions) |
|---|
| | 906 | (apply 'define-abbrev table elt)))) |
|---|
| | 907 | |
|---|