diff options
| author | Stefan Monnier | 2004-05-02 17:17:57 +0000 |
|---|---|---|
| committer | Stefan Monnier | 2004-05-02 17:17:57 +0000 |
| commit | 4f9ae122650926b107b8b6bd4e5b0cf97abeacd9 (patch) | |
| tree | a905913f5a2fc29267a9f4a2e9a5f2f560cee1b9 | |
| parent | 6cbc951e92e9a915268c205628a698599e409ad8 (diff) | |
| download | emacs-4f9ae122650926b107b8b6bd4e5b0cf97abeacd9.tar.gz emacs-4f9ae122650926b107b8b6bd4e5b0cf97abeacd9.zip | |
(bibtex-sort-buffer): Remove error message.
(bibtex-clean-entry): Disentangle code.
(bibtex-realign): New function.
(bibtex-reformat): Use mapcar and bibtex-realign. Do not use
bibtex-beginning-of-first-entry and bibtex-skip-to-valid-entry.
Remove undocumented optional arg called-by-convert-alien.
(bibtex-convert-alien): Use bibtex-realign. Use bibtex-reformat
for sorting instead of bibtex-sort-buffer.
| -rw-r--r-- | lisp/textmodes/bibtex.el | 158 |
1 files changed, 77 insertions, 81 deletions
diff --git a/lisp/textmodes/bibtex.el b/lisp/textmodes/bibtex.el index 82b15cf4eb5..1926661bbab 100644 --- a/lisp/textmodes/bibtex.el +++ b/lisp/textmodes/bibtex.el | |||
| @@ -3017,12 +3017,10 @@ The predicate depends on the variable `bibtex-maintain-sorted-entries'." | |||
| 3017 | (defun bibtex-sort-buffer () | 3017 | (defun bibtex-sort-buffer () |
| 3018 | "Sort BibTeX buffer alphabetically by key. | 3018 | "Sort BibTeX buffer alphabetically by key. |
| 3019 | The predicate for sorting is defined via `bibtex-maintain-sorted-entries'. | 3019 | The predicate for sorting is defined via `bibtex-maintain-sorted-entries'. |
| 3020 | Text outside of BibTeX entries is not affected. If | 3020 | If its value is nil use plain sorting. Text outside of BibTeX entries is not |
| 3021 | `bibtex-sort-ignore-string-entries' is non-nil, @String entries will be | 3021 | affected. If `bibtex-sort-ignore-string-entries' is non-nil, @String entries |
| 3022 | ignored." | 3022 | will be ignored." |
| 3023 | (interactive) | 3023 | (interactive) |
| 3024 | (unless bibtex-maintain-sorted-entries | ||
| 3025 | (error "You must choose a sorting scheme")) | ||
| 3026 | (save-restriction | 3024 | (save-restriction |
| 3027 | (narrow-to-region (bibtex-beginning-of-first-entry) | 3025 | (narrow-to-region (bibtex-beginning-of-first-entry) |
| 3028 | (save-excursion (goto-char (point-max)) | 3026 | (save-excursion (goto-char (point-max)) |
| @@ -3523,27 +3521,30 @@ At end of the cleaning process, the functions in | |||
| 3523 | (match-end bibtex-key-in-head))) | 3521 | (match-end bibtex-key-in-head))) |
| 3524 | (insert key)) | 3522 | (insert key)) |
| 3525 | ;; sorting | 3523 | ;; sorting |
| 3526 | (let* ((start (bibtex-beginning-of-entry)) | 3524 | (unless called-by-reformat |
| 3527 | (end (progn (bibtex-end-of-entry) | 3525 | (let* ((start (bibtex-beginning-of-entry)) |
| 3528 | (if (re-search-forward | 3526 | (end (progn (bibtex-end-of-entry) |
| 3529 | bibtex-entry-maybe-empty-head nil 'move) | 3527 | (if (re-search-forward |
| 3530 | (goto-char (match-beginning 0))) | 3528 | bibtex-entry-maybe-empty-head nil 'move) |
| 3531 | (point))) | 3529 | (goto-char (match-beginning 0))) |
| 3532 | (entry (buffer-substring start end)) | 3530 | (point))) |
| 3533 | (index (progn (goto-char start) | 3531 | (entry (buffer-substring start end)) |
| 3534 | (bibtex-entry-index)))) | 3532 | (index (progn (goto-char start) |
| 3535 | (delete-region start end) | 3533 | (bibtex-entry-index))) |
| 3536 | (unless (prog1 (or called-by-reformat | 3534 | no-error) |
| 3537 | (if (and bibtex-maintain-sorted-entries | 3535 | (if (and bibtex-maintain-sorted-entries |
| 3538 | (not (and bibtex-sort-ignore-string-entries | 3536 | (not (and bibtex-sort-ignore-string-entries |
| 3539 | (equal entry-type "string")))) | 3537 | (equal entry-type "string")))) |
| 3540 | (bibtex-prepare-new-entry index) | 3538 | (progn |
| 3541 | (not (bibtex-find-entry (car index))))) | 3539 | (delete-region start end) |
| 3542 | (insert entry) | 3540 | (setq no-error (bibtex-prepare-new-entry index)) |
| 3543 | (forward-char -1) | 3541 | (insert entry) |
| 3544 | (bibtex-beginning-of-entry) ; moves backward | 3542 | (forward-char -1) |
| 3545 | (re-search-forward bibtex-entry-head)) | 3543 | (bibtex-beginning-of-entry) ; moves backward |
| 3546 | (error "New inserted entry yields duplicate key"))) | 3544 | (re-search-forward bibtex-entry-head)) |
| 3545 | (setq no-error (bibtex-find-entry (car index)))) | ||
| 3546 | (unless no-error | ||
| 3547 | (error "New inserted entry yields duplicate key")))) | ||
| 3547 | ;; final clean up | 3548 | ;; final clean up |
| 3548 | (unless called-by-reformat | 3549 | (unless called-by-reformat |
| 3549 | (save-excursion | 3550 | (save-excursion |
| @@ -3621,91 +3622,89 @@ If `bibtex-align-at-equal-sign' is non-nil, align equal signs, too." | |||
| 3621 | (indent-to-column bibtex-entry-offset) | 3622 | (indent-to-column bibtex-entry-offset) |
| 3622 | (goto-char pnt))) | 3623 | (goto-char pnt))) |
| 3623 | 3624 | ||
| 3624 | (defun bibtex-reformat (&optional additional-options called-by-convert-alien) | 3625 | (defun bibtex-realign () |
| 3626 | "Realign BibTeX entries such that they are separated by one blank line." | ||
| 3627 | (goto-char (point-min)) | ||
| 3628 | (let ((case-fold-search t)) | ||
| 3629 | (when (looking-at bibtex-valid-entry-whitespace-re) | ||
| 3630 | (replace-match "\\1")) | ||
| 3631 | (while (re-search-forward bibtex-valid-entry-whitespace-re nil t) | ||
| 3632 | (replace-match "\n\n\\1")))) | ||
| 3633 | |||
| 3634 | (defun bibtex-reformat (&optional read-options) | ||
| 3625 | "Reformat all BibTeX entries in buffer or region. | 3635 | "Reformat all BibTeX entries in buffer or region. |
| 3626 | With prefix argument, read options for reformatting from minibuffer. | 3636 | With prefix argument, read options for reformatting from minibuffer. |
| 3627 | With \\[universal-argument] \\[universal-argument] prefix argument, reuse previous answers (if any) again. | 3637 | With \\[universal-argument] \\[universal-argument] prefix argument, reuse previous answers (if any) again. |
| 3628 | If mark is active it reformats entries in region, if not in whole buffer." | 3638 | If mark is active reformat entries in region, if not in whole buffer." |
| 3629 | (interactive "*P") | 3639 | (interactive "*P") |
| 3630 | (let* ((pnt (point)) | 3640 | (let* ((pnt (point)) |
| 3631 | (use-previous-options | 3641 | (use-previous-options |
| 3632 | (and (equal (prefix-numeric-value additional-options) 16) | 3642 | (and (equal (prefix-numeric-value read-options) 16) |
| 3633 | (or bibtex-reformat-previous-options | 3643 | (or bibtex-reformat-previous-options |
| 3634 | bibtex-reformat-previous-reference-keys))) | 3644 | bibtex-reformat-previous-reference-keys))) |
| 3635 | (bibtex-entry-format | 3645 | (bibtex-entry-format |
| 3636 | (if additional-options | 3646 | (if read-options |
| 3637 | (if use-previous-options | 3647 | (if use-previous-options |
| 3638 | bibtex-reformat-previous-options | 3648 | bibtex-reformat-previous-options |
| 3639 | (setq bibtex-reformat-previous-options | 3649 | (setq bibtex-reformat-previous-options |
| 3640 | (delq nil (list | 3650 | (mapcar (lambda (option) |
| 3641 | (if (or called-by-convert-alien | 3651 | (if (y-or-n-p (car option)) (cdr option))) |
| 3642 | (y-or-n-p "Realign entries (recommended)? ")) | 3652 | `(("Realign entries (recommended)? " . 'realign) |
| 3643 | 'realign) | 3653 | ("Remove empty optional and alternative fields? " . 'opts-or-alts) |
| 3644 | (if (y-or-n-p "Remove empty optional and alternative fields? ") | 3654 | ("Remove delimiters around pure numerical fields? " . 'numerical-fields) |
| 3645 | 'opts-or-alts) | 3655 | (,(concat (if bibtex-comma-after-last-field "Insert" "Remove") |
| 3646 | (if (y-or-n-p "Remove delimiters around pure numerical fields? ") | 3656 | " comma at end of entry? ") . 'last-comma) |
| 3647 | 'numerical-fields) | 3657 | ("Replace double page dashes by single ones? " . 'page-dashes) |
| 3648 | (if (y-or-n-p (concat (if bibtex-comma-after-last-field "Insert" "Remove") | 3658 | ("Force delimiters? " . 'delimiters) |
| 3649 | " comma at end of entry? ")) | 3659 | ("Unify case of entry types and field names? " . 'unify-case))))) |
| 3650 | 'last-comma) | ||
| 3651 | (if (y-or-n-p "Replace double page dashes by single ones? ") | ||
| 3652 | 'page-dashes) | ||
| 3653 | (if (y-or-n-p "Force delimiters? ") | ||
| 3654 | 'delimiters) | ||
| 3655 | (if (y-or-n-p "Unify case of entry types and field names? ") | ||
| 3656 | 'unify-case))))) | ||
| 3657 | '(realign))) | 3660 | '(realign))) |
| 3658 | (reformat-reference-keys (if additional-options | 3661 | (reformat-reference-keys |
| 3659 | (if use-previous-options | 3662 | (if read-options |
| 3660 | bibtex-reformat-previous-reference-keys | 3663 | (if use-previous-options |
| 3661 | (setq bibtex-reformat-previous-reference-keys | 3664 | bibtex-reformat-previous-reference-keys |
| 3662 | (y-or-n-p "Generate new reference keys automatically? "))))) | 3665 | (setq bibtex-reformat-previous-reference-keys |
| 3663 | bibtex-autokey-edit-before-use | 3666 | (y-or-n-p "Generate new reference keys automatically? "))))) |
| 3664 | (bibtex-sort-ignore-string-entries t) | ||
| 3665 | (start-point (if (bibtex-mark-active) | 3667 | (start-point (if (bibtex-mark-active) |
| 3666 | (region-beginning) | 3668 | (region-beginning) |
| 3667 | (bibtex-beginning-of-first-entry) | 3669 | (point-min))) |
| 3668 | (bibtex-skip-to-valid-entry) | ||
| 3669 | (point))) | ||
| 3670 | (end-point (if (bibtex-mark-active) | 3670 | (end-point (if (bibtex-mark-active) |
| 3671 | (region-end) | 3671 | (region-end) |
| 3672 | (point-max)))) | 3672 | (point-max))) |
| 3673 | (bibtex-sort-ignore-string-entries t) | ||
| 3674 | bibtex-autokey-edit-before-use) | ||
| 3675 | |||
| 3673 | (save-restriction | 3676 | (save-restriction |
| 3674 | (narrow-to-region start-point end-point) | 3677 | (narrow-to-region start-point end-point) |
| 3675 | (when (memq 'realign bibtex-entry-format) | 3678 | (if (memq 'realign bibtex-entry-format) |
| 3676 | (goto-char (point-min)) | 3679 | (bibtex-realign)) |
| 3677 | (while (re-search-forward bibtex-valid-entry-whitespace-re nil t) | ||
| 3678 | (replace-match "\n\\1"))) | ||
| 3679 | (goto-char start-point) | 3680 | (goto-char start-point) |
| 3680 | (bibtex-progress-message "Formatting" 1) | 3681 | (bibtex-progress-message "Formatting" 1) |
| 3681 | (bibtex-map-entries (lambda (key beg end) | 3682 | (bibtex-map-entries (lambda (key beg end) |
| 3682 | (bibtex-progress-message) | 3683 | (bibtex-progress-message) |
| 3683 | (bibtex-clean-entry reformat-reference-keys t) | 3684 | (bibtex-clean-entry reformat-reference-keys t))) |
| 3684 | (when (memq 'realign bibtex-entry-format) | 3685 | (when (memq 'realign bibtex-entry-format) |
| 3685 | (goto-char end) | 3686 | (bibtex-delete-whitespace) |
| 3686 | (bibtex-delete-whitespace) | 3687 | (open-line (if (eobp) 1 2))) |
| 3687 | (open-line 2)))) | ||
| 3688 | (bibtex-progress-message 'done)) | 3688 | (bibtex-progress-message 'done)) |
| 3689 | (when (and reformat-reference-keys | 3689 | (when (and reformat-reference-keys |
| 3690 | bibtex-maintain-sorted-entries | 3690 | bibtex-maintain-sorted-entries) |
| 3691 | (not called-by-convert-alien)) | 3691 | (bibtex-progress-message "Sorting" 1) |
| 3692 | (bibtex-sort-buffer) | 3692 | (bibtex-sort-buffer) |
| 3693 | (kill-local-variable 'bibtex-reference-keys)) | 3693 | (kill-local-variable 'bibtex-reference-keys) |
| 3694 | (bibtex-progress-message 'done)) | ||
| 3694 | (goto-char pnt))) | 3695 | (goto-char pnt))) |
| 3695 | 3696 | ||
| 3696 | (defun bibtex-convert-alien (&optional do-additional-reformatting) | 3697 | (defun bibtex-convert-alien (&optional read-options) |
| 3697 | "Convert an alien BibTeX buffer to be fully usable by BibTeX mode. | 3698 | "Convert an alien BibTeX buffer to be fully usable by BibTeX mode. |
| 3698 | If a file does not conform with some standards used by BibTeX mode, | 3699 | If a file does not conform with all standards used by BibTeX mode, |
| 3699 | some of the high-level features of BibTeX mode will not be available. | 3700 | some of the high-level features of BibTeX mode will not be available. |
| 3700 | This function tries to convert current buffer to conform with these standards. | 3701 | This function tries to convert current buffer to conform with these standards. |
| 3701 | With prefix argument DO-ADDITIONAL-REFORMATTING | 3702 | With prefix argument READ-OPTIONS non-nil, read options for reformatting |
| 3702 | non-nil, read options for reformatting entries from minibuffer." | 3703 | entries from minibuffer." |
| 3703 | (interactive "*P") | 3704 | (interactive "*P") |
| 3704 | (message "Starting to validate buffer...") | 3705 | (message "Starting to validate buffer...") |
| 3705 | (sit-for 1 nil t) | 3706 | (sit-for 1 nil t) |
| 3706 | (goto-char (point-min)) | 3707 | (bibtex-realign) |
| 3707 | (while (re-search-forward "[ \t\n]+@" nil t) | ||
| 3708 | (replace-match "\n@")) | ||
| 3709 | (message | 3708 | (message |
| 3710 | "If errors occur, correct them and call `bibtex-convert-alien' again") | 3709 | "If errors occur, correct them and call `bibtex-convert-alien' again") |
| 3711 | (sit-for 5 nil t) | 3710 | (sit-for 5 nil t) |
| @@ -3714,10 +3713,7 @@ non-nil, read options for reformatting entries from minibuffer." | |||
| 3714 | (bibtex-validate)) | 3713 | (bibtex-validate)) |
| 3715 | (message "Starting to reformat entries...") | 3714 | (message "Starting to reformat entries...") |
| 3716 | (sit-for 2 nil t) | 3715 | (sit-for 2 nil t) |
| 3717 | (bibtex-reformat do-additional-reformatting t) | 3716 | (bibtex-reformat read-options) |
| 3718 | (when bibtex-maintain-sorted-entries | ||
| 3719 | (message "Starting to sort buffer...") | ||
| 3720 | (bibtex-sort-buffer)) | ||
| 3721 | (goto-char (point-max)) | 3717 | (goto-char (point-max)) |
| 3722 | (message "Buffer is now parsable. Please save it."))) | 3718 | (message "Buffer is now parsable. Please save it."))) |
| 3723 | 3719 | ||