diff options
| author | Stefan Monnier | 2017-10-08 15:44:49 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2017-10-08 15:44:49 -0400 |
| commit | 85b4e88194cae541a0093a9166f4306e6fd3109e (patch) | |
| tree | 01ffc5d5431963185294a5cad22bec538c7f6ec1 | |
| parent | 9613690f6e51e2f2aa2bcbbede3e209d08cfaaad (diff) | |
| download | emacs-85b4e88194cae541a0093a9166f4306e6fd3109e.tar.gz emacs-85b4e88194cae541a0093a9166f4306e6fd3109e.zip | |
* lisp/emacs-lisp/checkdoc.el: cl-defstruct + minor simplifications
(checkdoc-make-overlay, checkdoc-overlay-put, checkdoc-delete-overlay)
(checkdoc-overlay-start, checkdoc-overlay-end, checkdoc-char=)
(checkdoc-mode-line-update): Remove old compatibility aliases.
(checkdoc, checkdoc-interactive-loop):
Consolidate common code in if branches.
(checkdoc-error): New struct type.
(checkdoc-error-text, checkdoc-error-start, checkdoc-error-end)
(checkdoc-error-unfixable): Now defined by cl-defstruct.
| -rw-r--r-- | lisp/emacs-lisp/checkdoc.el | 132 |
1 files changed, 52 insertions, 80 deletions
diff --git a/lisp/emacs-lisp/checkdoc.el b/lisp/emacs-lisp/checkdoc.el index 72f82f26f6f..fe6cd4160ed 100644 --- a/lisp/emacs-lisp/checkdoc.el +++ b/lisp/emacs-lisp/checkdoc.el | |||
| @@ -171,6 +171,7 @@ | |||
| 171 | (defvar checkdoc-version "0.6.1" | 171 | (defvar checkdoc-version "0.6.1" |
| 172 | "Release version of checkdoc you are currently running.") | 172 | "Release version of checkdoc you are currently running.") |
| 173 | 173 | ||
| 174 | (eval-when-compile (require 'cl-lib)) | ||
| 174 | (require 'help-mode) ;; for help-xref-info-regexp | 175 | (require 'help-mode) ;; for help-xref-info-regexp |
| 175 | (require 'thingatpt) ;; for handy thing-at-point-looking-at | 176 | (require 'thingatpt) ;; for handy thing-at-point-looking-at |
| 176 | 177 | ||
| @@ -436,23 +437,6 @@ be re-created.") | |||
| 436 | st) | 437 | st) |
| 437 | "Syntax table used by checkdoc in document strings.") | 438 | "Syntax table used by checkdoc in document strings.") |
| 438 | 439 | ||
| 439 | ;;; Compatibility | ||
| 440 | ;; | ||
| 441 | (defalias 'checkdoc-make-overlay | ||
| 442 | (if (featurep 'xemacs) #'make-extent #'make-overlay)) | ||
| 443 | (defalias 'checkdoc-overlay-put | ||
| 444 | (if (featurep 'xemacs) #'set-extent-property #'overlay-put)) | ||
| 445 | (defalias 'checkdoc-delete-overlay | ||
| 446 | (if (featurep 'xemacs) #'delete-extent #'delete-overlay)) | ||
| 447 | (defalias 'checkdoc-overlay-start | ||
| 448 | (if (featurep 'xemacs) #'extent-start #'overlay-start)) | ||
| 449 | (defalias 'checkdoc-overlay-end | ||
| 450 | (if (featurep 'xemacs) #'extent-end #'overlay-end)) | ||
| 451 | (defalias 'checkdoc-mode-line-update | ||
| 452 | (if (featurep 'xemacs) #'redraw-modeline #'force-mode-line-update)) | ||
| 453 | (defalias 'checkdoc-char= | ||
| 454 | (if (featurep 'xemacs) #'char= #'=)) | ||
| 455 | |||
| 456 | ;;; User level commands | 440 | ;;; User level commands |
| 457 | ;; | 441 | ;; |
| 458 | ;;;###autoload | 442 | ;;;###autoload |
| @@ -475,32 +459,31 @@ the users will view as each check is completed." | |||
| 475 | tmp) | 459 | tmp) |
| 476 | (checkdoc-display-status-buffer status) | 460 | (checkdoc-display-status-buffer status) |
| 477 | ;; check the comments | 461 | ;; check the comments |
| 478 | (if (not buffer-file-name) | 462 | (setf (nth 0 status) |
| 479 | (setcar status "Not checked") | 463 | (cond |
| 480 | (if (checkdoc-file-comments-engine) | 464 | ((not buffer-file-name) "Not checked") |
| 481 | (setcar status "Errors") | 465 | ((checkdoc-file-comments-engine) "Errors") |
| 482 | (setcar status "Ok"))) | 466 | (t "Ok"))) |
| 483 | (setcar (cdr status) "Checking...") | 467 | (setf (nth 1 status) "Checking...") |
| 484 | (checkdoc-display-status-buffer status) | 468 | (checkdoc-display-status-buffer status) |
| 485 | ;; Check the documentation | 469 | ;; Check the documentation |
| 486 | (setq tmp (checkdoc-interactive nil t)) | 470 | (setq tmp (checkdoc-interactive nil t)) |
| 487 | (if tmp | 471 | (setf (nth 1 status) |
| 488 | (setcar (cdr status) (format "%d Errors" (length tmp))) | 472 | (if tmp (format "%d Errors" (length tmp)) "Ok")) |
| 489 | (setcar (cdr status) "Ok")) | 473 | (setf (nth 2 status) "Checking...") |
| 490 | (setcar (cdr (cdr status)) "Checking...") | ||
| 491 | (checkdoc-display-status-buffer status) | 474 | (checkdoc-display-status-buffer status) |
| 492 | ;; Check the message text | 475 | ;; Check the message text |
| 493 | (if (setq tmp (checkdoc-message-interactive nil t)) | 476 | (setf (nth 2 status) |
| 494 | (setcar (cdr (cdr status)) (format "%d Errors" (length tmp))) | 477 | (if (setq tmp (checkdoc-message-interactive nil t)) |
| 495 | (setcar (cdr (cdr status)) "Ok")) | 478 | (format "%d Errors" (length tmp)) |
| 496 | (setcar (cdr (cdr (cdr status))) "Checking...") | 479 | "Ok")) |
| 480 | (setf (nth 3 status) "Checking...") | ||
| 497 | (checkdoc-display-status-buffer status) | 481 | (checkdoc-display-status-buffer status) |
| 498 | ;; Rogue spacing | 482 | ;; Rogue spacing |
| 499 | (if (condition-case nil | 483 | (setf (nth 3 status) |
| 500 | (checkdoc-rogue-spaces nil t) | 484 | (if (ignore-errors (checkdoc-rogue-spaces nil t)) |
| 501 | (error t)) | 485 | "Errors" |
| 502 | (setcar (cdr (cdr (cdr status))) "Errors") | 486 | "Ok")) |
| 503 | (setcar (cdr (cdr (cdr status))) "Ok")) | ||
| 504 | (checkdoc-display-status-buffer status))) | 487 | (checkdoc-display-status-buffer status))) |
| 505 | 488 | ||
| 506 | (defun checkdoc-display-status-buffer (check) | 489 | (defun checkdoc-display-status-buffer (check) |
| @@ -592,16 +575,16 @@ style." | |||
| 592 | (while err-list | 575 | (while err-list |
| 593 | (goto-char (cdr (car err-list))) | 576 | (goto-char (cdr (car err-list))) |
| 594 | ;; The cursor should be just in front of the offending doc string | 577 | ;; The cursor should be just in front of the offending doc string |
| 595 | (if (stringp (car (car err-list))) | 578 | (setq cdo (if (stringp (car (car err-list))) |
| 596 | (setq cdo (save-excursion (checkdoc-make-overlay | 579 | (save-excursion (make-overlay |
| 597 | (point) (progn (forward-sexp 1) | 580 | (point) (progn (forward-sexp 1) |
| 598 | (point))))) | 581 | (point)))) |
| 599 | (setq cdo (checkdoc-make-overlay | 582 | (make-overlay |
| 600 | (checkdoc-error-start (car (car err-list))) | 583 | (checkdoc-error-start (car (car err-list))) |
| 601 | (checkdoc-error-end (car (car err-list)))))) | 584 | (checkdoc-error-end (car (car err-list)))))) |
| 602 | (unwind-protect | 585 | (unwind-protect |
| 603 | (progn | 586 | (progn |
| 604 | (checkdoc-overlay-put cdo 'face 'highlight) | 587 | (overlay-put cdo 'face 'highlight) |
| 605 | ;; Make sure the whole doc string is visible if possible. | 588 | ;; Make sure the whole doc string is visible if possible. |
| 606 | (sit-for 0) | 589 | (sit-for 0) |
| 607 | (if (and (= (following-char) ?\") | 590 | (if (and (= (following-char) ?\") |
| @@ -627,10 +610,10 @@ style." | |||
| 627 | (if (not (integerp c)) (setq c ??)) | 610 | (if (not (integerp c)) (setq c ??)) |
| 628 | (cond | 611 | (cond |
| 629 | ;; Exit condition | 612 | ;; Exit condition |
| 630 | ((checkdoc-char= c ?\C-g) (signal 'quit nil)) | 613 | ((eq c ?\C-g) (signal 'quit nil)) |
| 631 | ;; Request an auto-fix | 614 | ;; Request an auto-fix |
| 632 | ((or (checkdoc-char= c ?y) (checkdoc-char= c ?f)) | 615 | ((memq c '(?y ?f)) |
| 633 | (checkdoc-delete-overlay cdo) | 616 | (delete-overlay cdo) |
| 634 | (setq cdo nil) | 617 | (setq cdo nil) |
| 635 | (goto-char (cdr (car err-list))) | 618 | (goto-char (cdr (car err-list))) |
| 636 | ;; `automatic-then-never' tells the autofix function | 619 | ;; `automatic-then-never' tells the autofix function |
| @@ -659,7 +642,7 @@ style." | |||
| 659 | "No Additional style errors. Continuing...") | 642 | "No Additional style errors. Continuing...") |
| 660 | (sit-for 2)))))) | 643 | (sit-for 2)))))) |
| 661 | ;; Move to the next error (if available) | 644 | ;; Move to the next error (if available) |
| 662 | ((or (checkdoc-char= c ?n) (checkdoc-char= c ?\s)) | 645 | ((memq c '(?n ?\s)) |
| 663 | (let ((ne (funcall findfunc nil))) | 646 | (let ((ne (funcall findfunc nil))) |
| 664 | (if (not ne) | 647 | (if (not ne) |
| 665 | (if showstatus | 648 | (if showstatus |
| @@ -671,7 +654,7 @@ style." | |||
| 671 | (sit-for 2)) | 654 | (sit-for 2)) |
| 672 | (setq err-list (cons ne err-list))))) | 655 | (setq err-list (cons ne err-list))))) |
| 673 | ;; Go backwards in the list of errors | 656 | ;; Go backwards in the list of errors |
| 674 | ((or (checkdoc-char= c ?p) (checkdoc-char= c ?\C-?)) | 657 | ((memq c '(?p ?\C-?)) |
| 675 | (if (/= (length err-list) 1) | 658 | (if (/= (length err-list) 1) |
| 676 | (progn | 659 | (progn |
| 677 | (setq err-list (cdr err-list)) | 660 | (setq err-list (cdr err-list)) |
| @@ -680,10 +663,10 @@ style." | |||
| 680 | (message "No Previous Errors.") | 663 | (message "No Previous Errors.") |
| 681 | (sit-for 2))) | 664 | (sit-for 2))) |
| 682 | ;; Edit the buffer recursively. | 665 | ;; Edit the buffer recursively. |
| 683 | ((checkdoc-char= c ?e) | 666 | ((eq c ?e) |
| 684 | (checkdoc-recursive-edit | 667 | (checkdoc-recursive-edit |
| 685 | (checkdoc-error-text (car (car err-list)))) | 668 | (checkdoc-error-text (car (car err-list)))) |
| 686 | (checkdoc-delete-overlay cdo) | 669 | (delete-overlay cdo) |
| 687 | (setq err-list (cdr err-list)) ;back up the error found. | 670 | (setq err-list (cdr err-list)) ;back up the error found. |
| 688 | (beginning-of-defun) | 671 | (beginning-of-defun) |
| 689 | (let ((ne (funcall findfunc nil))) | 672 | (let ((ne (funcall findfunc nil))) |
| @@ -695,7 +678,7 @@ style." | |||
| 695 | (sit-for 2)) | 678 | (sit-for 2)) |
| 696 | (setq err-list (cons ne err-list))))) | 679 | (setq err-list (cons ne err-list))))) |
| 697 | ;; Quit checkdoc | 680 | ;; Quit checkdoc |
| 698 | ((checkdoc-char= c ?q) | 681 | ((eq c ?q) |
| 699 | (setq returnme err-list | 682 | (setq returnme err-list |
| 700 | err-list nil | 683 | err-list nil |
| 701 | begin (point))) | 684 | begin (point))) |
| @@ -723,7 +706,7 @@ style." | |||
| 723 | "C-h - Toggle this help buffer."))) | 706 | "C-h - Toggle this help buffer."))) |
| 724 | (shrink-window-if-larger-than-buffer | 707 | (shrink-window-if-larger-than-buffer |
| 725 | (get-buffer-window "*Checkdoc Help*")))))) | 708 | (get-buffer-window "*Checkdoc Help*")))))) |
| 726 | (if cdo (checkdoc-delete-overlay cdo))))) | 709 | (if cdo (delete-overlay cdo))))) |
| 727 | (goto-char begin) | 710 | (goto-char begin) |
| 728 | (if (get-buffer "*Checkdoc Help*") (kill-buffer "*Checkdoc Help*")) | 711 | (if (get-buffer "*Checkdoc Help*") (kill-buffer "*Checkdoc Help*")) |
| 729 | (message "Checkdoc: Done.") | 712 | (message "Checkdoc: Done.") |
| @@ -1147,6 +1130,15 @@ Prefix argument is the same as for `checkdoc-defun'" | |||
| 1147 | ;; features and behaviors, so we need some ways of specifying | 1130 | ;; features and behaviors, so we need some ways of specifying |
| 1148 | ;; them, and making them easier to use in the wacked-out interfaces | 1131 | ;; them, and making them easier to use in the wacked-out interfaces |
| 1149 | ;; people are requesting | 1132 | ;; people are requesting |
| 1133 | |||
| 1134 | (cl-defstruct (checkdoc-error | ||
| 1135 | (:constructor nil) | ||
| 1136 | (:constructor checkdoc--create-error (text start end &optional unfixable))) | ||
| 1137 | (text nil :read-only t) | ||
| 1138 | (start nil :read-only t) | ||
| 1139 | (end nil :read-only t) | ||
| 1140 | (unfixable nil :read-only t)) | ||
| 1141 | |||
| 1150 | (defvar checkdoc-create-error-function #'checkdoc--create-error-for-checkdoc | 1142 | (defvar checkdoc-create-error-function #'checkdoc--create-error-for-checkdoc |
| 1151 | "Function called when Checkdoc encounters an error. | 1143 | "Function called when Checkdoc encounters an error. |
| 1152 | Should accept as arguments (TEXT START END &optional UNFIXABLE). | 1144 | Should accept as arguments (TEXT START END &optional UNFIXABLE). |
| @@ -1155,7 +1147,7 @@ TEXT is the descriptive text of the error. START and END define the region | |||
| 1155 | it is sensible to highlight when describing the problem. | 1147 | it is sensible to highlight when describing the problem. |
| 1156 | Optional argument UNFIXABLE means that the error has no auto-fix available. | 1148 | Optional argument UNFIXABLE means that the error has no auto-fix available. |
| 1157 | 1149 | ||
| 1158 | A list of the form (TEXT START END UNFIXABLE) is returned if we are not | 1150 | An object of type `checkdoc-error' is returned if we are not |
| 1159 | generating a buffered list of errors.") | 1151 | generating a buffered list of errors.") |
| 1160 | 1152 | ||
| 1161 | (defun checkdoc-create-error (text start end &optional unfixable) | 1153 | (defun checkdoc-create-error (text start end &optional unfixable) |
| @@ -1171,27 +1163,7 @@ TEXT, START, END and UNFIXABLE conform to | |||
| 1171 | (if checkdoc-generate-compile-warnings-flag | 1163 | (if checkdoc-generate-compile-warnings-flag |
| 1172 | (progn (checkdoc-error start text) | 1164 | (progn (checkdoc-error start text) |
| 1173 | nil) | 1165 | nil) |
| 1174 | (list text start end unfixable))) | 1166 | (checkdoc--create-error text start end unfixable))) |
| 1175 | |||
| 1176 | (defun checkdoc-error-text (err) | ||
| 1177 | "Return the text specified in the checkdoc ERR." | ||
| 1178 | ;; string-p part is for backwards compatibility | ||
| 1179 | (if (stringp err) err (car err))) | ||
| 1180 | |||
| 1181 | (defun checkdoc-error-start (err) | ||
| 1182 | "Return the start point specified in the checkdoc ERR." | ||
| 1183 | ;; string-p part is for backwards compatibility | ||
| 1184 | (if (stringp err) nil (nth 1 err))) | ||
| 1185 | |||
| 1186 | (defun checkdoc-error-end (err) | ||
| 1187 | "Return the end point specified in the checkdoc ERR." | ||
| 1188 | ;; string-p part is for backwards compatibility | ||
| 1189 | (if (stringp err) nil (nth 2 err))) | ||
| 1190 | |||
| 1191 | (defun checkdoc-error-unfixable (err) | ||
| 1192 | "Return the t if we cannot autofix the error specified in the checkdoc ERR." | ||
| 1193 | ;; string-p part is for backwards compatibility | ||
| 1194 | (if (stringp err) nil (nth 3 err))) | ||
| 1195 | 1167 | ||
| 1196 | ;;; Minor Mode specification | 1168 | ;;; Minor Mode specification |
| 1197 | ;; | 1169 | ;; |
| @@ -1342,7 +1314,7 @@ See the style guide in the Emacs Lisp manual for more details." | |||
| 1342 | (if (and (not (nth 1 fp)) ; not a variable | 1314 | (if (and (not (nth 1 fp)) ; not a variable |
| 1343 | (or (nth 2 fp) ; is interactive | 1315 | (or (nth 2 fp) ; is interactive |
| 1344 | checkdoc-force-docstrings-flag) ;or we always complain | 1316 | checkdoc-force-docstrings-flag) ;or we always complain |
| 1345 | (not (checkdoc-char= (following-char) ?\"))) ; no doc string | 1317 | (not (eq (following-char) ?\"))) ; no doc string |
| 1346 | ;; Sometimes old code has comments where the documentation should | 1318 | ;; Sometimes old code has comments where the documentation should |
| 1347 | ;; be. Let's see if we can find the comment, and offer to turn it | 1319 | ;; be. Let's see if we can find the comment, and offer to turn it |
| 1348 | ;; into documentation for them. | 1320 | ;; into documentation for them. |
| @@ -1471,9 +1443,9 @@ regexp short cuts work. FP is the function defun information." | |||
| 1471 | (if (> (point) e) (goto-char e)) ;of the form (defun n () "doc" nil) | 1443 | (if (> (point) e) (goto-char e)) ;of the form (defun n () "doc" nil) |
| 1472 | (forward-char -1) | 1444 | (forward-char -1) |
| 1473 | (cond | 1445 | (cond |
| 1474 | ((and (checkdoc-char= (following-char) ?\") | 1446 | ((and (eq (following-char) ?\") |
| 1475 | ;; A backslashed double quote at the end of a sentence | 1447 | ;; A backslashed double quote at the end of a sentence |
| 1476 | (not (checkdoc-char= (preceding-char) ?\\))) | 1448 | (not (eq (preceding-char) ?\\))) |
| 1477 | ;; We might have to add a period in this case | 1449 | ;; We might have to add a period in this case |
| 1478 | (forward-char -1) | 1450 | (forward-char -1) |
| 1479 | (if (looking-at "[.!?]") | 1451 | (if (looking-at "[.!?]") |
| @@ -1796,7 +1768,7 @@ function,command,variable,option or symbol." ms1)))))) | |||
| 1796 | (let ((lim (save-excursion | 1768 | (let ((lim (save-excursion |
| 1797 | (end-of-line) | 1769 | (end-of-line) |
| 1798 | ;; check string-continuation | 1770 | ;; check string-continuation |
| 1799 | (if (checkdoc-char= (preceding-char) ?\\) | 1771 | (if (eq (preceding-char) ?\\) |
| 1800 | (line-end-position 2) | 1772 | (line-end-position 2) |
| 1801 | (point)))) | 1773 | (point)))) |
| 1802 | (rs nil) replace original (case-fold-search t)) | 1774 | (rs nil) replace original (case-fold-search t)) |
| @@ -2593,12 +2565,12 @@ This function returns non-nil if the text was replaced. | |||
| 2593 | This function will not modify `match-data'." | 2565 | This function will not modify `match-data'." |
| 2594 | (if (and checkdoc-autofix-flag | 2566 | (if (and checkdoc-autofix-flag |
| 2595 | (not (eq checkdoc-autofix-flag 'never))) | 2567 | (not (eq checkdoc-autofix-flag 'never))) |
| 2596 | (let ((o (checkdoc-make-overlay start end)) | 2568 | (let ((o (make-overlay start end)) |
| 2597 | (ret nil) | 2569 | (ret nil) |
| 2598 | (md (match-data))) | 2570 | (md (match-data))) |
| 2599 | (unwind-protect | 2571 | (unwind-protect |
| 2600 | (progn | 2572 | (progn |
| 2601 | (checkdoc-overlay-put o 'face 'highlight) | 2573 | (overlay-put o 'face 'highlight) |
| 2602 | (if (or (eq checkdoc-autofix-flag 'automatic) | 2574 | (if (or (eq checkdoc-autofix-flag 'automatic) |
| 2603 | (eq checkdoc-autofix-flag 'automatic-then-never) | 2575 | (eq checkdoc-autofix-flag 'automatic-then-never) |
| 2604 | (and (eq checkdoc-autofix-flag 'semiautomatic) | 2576 | (and (eq checkdoc-autofix-flag 'semiautomatic) |
| @@ -2615,9 +2587,9 @@ This function will not modify `match-data'." | |||
| 2615 | (insert replacewith) | 2587 | (insert replacewith) |
| 2616 | (if checkdoc-bouncy-flag (sit-for 0)) | 2588 | (if checkdoc-bouncy-flag (sit-for 0)) |
| 2617 | (setq ret t))) | 2589 | (setq ret t))) |
| 2618 | (checkdoc-delete-overlay o) | 2590 | (delete-overlay o) |
| 2619 | (set-match-data md)) | 2591 | (set-match-data md)) |
| 2620 | (checkdoc-delete-overlay o) | 2592 | (delete-overlay o) |
| 2621 | (set-match-data md)) | 2593 | (set-match-data md)) |
| 2622 | (if (eq checkdoc-autofix-flag 'automatic-then-never) | 2594 | (if (eq checkdoc-autofix-flag 'automatic-then-never) |
| 2623 | (setq checkdoc-autofix-flag 'never)) | 2595 | (setq checkdoc-autofix-flag 'never)) |