diff options
| author | john muhl | 2025-03-20 15:48:05 -0500 |
|---|---|---|
| committer | Po Lu | 2025-08-25 09:59:04 +0800 |
| commit | 2e454bea03ed7186141e9e1d9b3eb4e888699149 (patch) | |
| tree | 45d4b013c39db0b36e833150309476a1c9c60793 | |
| parent | bf9e502e706de5c6d8815254c857fe5cbd844d7f (diff) | |
| download | emacs-2e454bea03ed7186141e9e1d9b3eb4e888699149.tar.gz emacs-2e454bea03ed7186141e9e1d9b3eb4e888699149.zip | |
; Adjust code style in 'lua-mode'
* lisp/progmodes/lua-mode.el (lua-prefix-key)
(lua-goto-matching-block): Replace 'let...if' with 'if-let*'.
(lua--fill-paragraph, lua-goto-matching-block-token): Replace
'let...when' with 'when-let*'.
(lua-comment-start-pos): Replace 'unless..and' with 'if-let*'.
(lua-skip-ws-and-comments-backward)
(lua-skip-ws-and-comments-forward): Remove unnecessary 'setq'.
(lua-indent-line): Replace 'when...not' with 'unless'.
(lua-calculate-string-or-comment-indentation): Formatting.
(lua-find-matching-token-word): Remove a level of 'let' nesting,
replace 'when...not' with 'unless'.
(lua-indent-line): Combine 'setq's.
| -rw-r--r-- | lisp/progmodes/lua-mode.el | 162 |
1 files changed, 76 insertions, 86 deletions
diff --git a/lisp/progmodes/lua-mode.el b/lisp/progmodes/lua-mode.el index 790b3feaac9..dcb8a27da64 100644 --- a/lisp/progmodes/lua-mode.el +++ b/lisp/progmodes/lua-mode.el | |||
| @@ -229,8 +229,7 @@ Should be a list of strings." | |||
| 229 | :type 'string | 229 | :type 'string |
| 230 | :set 'lua--customize-set-prefix-key | 230 | :set 'lua--customize-set-prefix-key |
| 231 | :get (lambda (sym) | 231 | :get (lambda (sym) |
| 232 | (let ((val (eval sym))) | 232 | (if-let* ((val (eval sym))) (single-key-description val) "")) |
| 233 | (if val (single-key-description (eval sym)) ""))) | ||
| 234 | :version "31.1") | 233 | :version "31.1") |
| 235 | 234 | ||
| 236 | (defvar lua-prefix-mode-map | 235 | (defvar lua-prefix-mode-map |
| @@ -603,17 +602,15 @@ index of respective Lua reference manuals.") | |||
| 603 | (fill-paragraph justify region)))) | 602 | (fill-paragraph justify region)))) |
| 604 | 603 | ||
| 605 | (defun lua-prefix-key-update-bindings () | 604 | (defun lua-prefix-key-update-bindings () |
| 606 | (let (old-cons) | 605 | (if (eq lua-prefix-mode-map (keymap-parent lua-mode-map)) |
| 607 | (if (eq lua-prefix-mode-map (keymap-parent lua-mode-map)) | 606 | ;; if prefix-map is a parent, delete the parent |
| 608 | ;; if prefix-map is a parent, delete the parent | 607 | (set-keymap-parent lua-mode-map nil) |
| 609 | (set-keymap-parent lua-mode-map nil) | 608 | ;; otherwise, look for it among children |
| 610 | ;; otherwise, look for it among children | 609 | (when-let* ((old-cons (rassoc lua-prefix-mode-map lua-mode-map))) |
| 611 | (when (setq old-cons (rassoc lua-prefix-mode-map lua-mode-map)) | 610 | (delq old-cons lua-mode-map))) |
| 612 | (delq old-cons lua-mode-map))) | 611 | (if (null lua-prefix-key) |
| 613 | 612 | (set-keymap-parent lua-mode-map lua-prefix-mode-map) | |
| 614 | (if (null lua-prefix-key) | 613 | (define-key lua-mode-map (vector lua-prefix-key) lua-prefix-mode-map))) |
| 615 | (set-keymap-parent lua-mode-map lua-prefix-mode-map) | ||
| 616 | (define-key lua-mode-map (vector lua-prefix-key) lua-prefix-mode-map)))) | ||
| 617 | 614 | ||
| 618 | (defun lua-set-prefix-key (new-key-str) | 615 | (defun lua-set-prefix-key (new-key-str) |
| 619 | "Changes `lua-prefix-key' properly and updates keymaps | 616 | "Changes `lua-prefix-key' properly and updates keymaps |
| @@ -642,13 +639,11 @@ consider point as inside comment when it is between the two hyphens" | |||
| 642 | "Return position of comment containing current point. | 639 | "Return position of comment containing current point. |
| 643 | 640 | ||
| 644 | If point is not inside a comment, return nil." | 641 | If point is not inside a comment, return nil." |
| 645 | (unless parsing-state (setq parsing-state (syntax-ppss))) | 642 | (if-let* ((parsing-state (or parsing-state (syntax-ppss))) |
| 646 | (and | 643 | ((not (nth 3 parsing-state))) ; Not a string. |
| 647 | ;; Not a string | 644 | ((nth 4 parsing-state))) ; Syntax-based comment. |
| 648 | (not (nth 3 parsing-state)) | 645 | (nth 8 parsing-state) |
| 649 | ;; Syntax-based comment | 646 | (lua--containing-double-hyphen-start-pos))) |
| 650 | (or (and (nth 4 parsing-state) (nth 8 parsing-state)) | ||
| 651 | (lua--containing-double-hyphen-start-pos)))) | ||
| 652 | 647 | ||
| 653 | (defun lua-comment-or-string-p (&optional pos) | 648 | (defun lua-comment-or-string-p (&optional pos) |
| 654 | "Returns true if the point is in a comment or string." | 649 | "Returns true if the point is in a comment or string." |
| @@ -744,24 +739,26 @@ Return the amount the indentation changed by." | |||
| 744 | ;; save point as a distance to eob - it's invariant w.r.t indentation | 739 | ;; save point as a distance to eob - it's invariant w.r.t indentation |
| 745 | (pos (- (point-max) (point)))) | 740 | (pos (- (point-max) (point)))) |
| 746 | (back-to-indentation) | 741 | (back-to-indentation) |
| 747 | (if (lua-comment-or-string-p) | 742 | (setq indent (if (lua-comment-or-string-p) |
| 748 | (setq indent (lua-calculate-string-or-comment-indentation)) ;; just restore point position | 743 | ;; Just restore point posistion. |
| 749 | (setq indent (max 0 (lua-calculate-indentation)))) | 744 | (lua-calculate-string-or-comment-indentation) |
| 745 | (max 0 (lua-calculate-indentation)))) | ||
| 750 | 746 | ||
| 751 | (when (not (equal indent (current-column))) | 747 | (unless (equal indent (current-column)) |
| 752 | (delete-region (line-beginning-position) (point)) | 748 | (delete-region (line-beginning-position) (point)) |
| 753 | (indent-to indent)) | 749 | (indent-to indent)) |
| 754 | 750 | ||
| 755 | ;; If initial point was within line's indentation, | 751 | ;; If initial point was within line's indentation, |
| 756 | ;; position after the indentation. Else stay at same point in text. | 752 | ;; position after the indentation. Else stay at same point in text. |
| 757 | (when (> (- (point-max) pos) (point)) ; 03e991 | 753 | (when (> (- (point-max) pos) (point)) |
| 758 | (goto-char (- (point-max) pos))) ; 03e991 | 754 | (goto-char (- (point-max) pos))) |
| 759 | 755 | ||
| 760 | indent)) | 756 | indent)) |
| 761 | 757 | ||
| 762 | (defun lua-calculate-string-or-comment-indentation () | 758 | (defun lua-calculate-string-or-comment-indentation () |
| 763 | "This function should be run when point at (current-indentation) is inside string" | 759 | "This function should be run when point at (current-indentation) is inside string" |
| 764 | (if (and (lua-string-p) (not lua-indent-string-contents)) | 760 | (if (and (lua-string-p) |
| 761 | (not lua-indent-string-contents)) | ||
| 765 | ;; if inside string and strings aren't to be indented, return current indentation | 762 | ;; if inside string and strings aren't to be indented, return current indentation |
| 766 | (current-indentation) | 763 | (current-indentation) |
| 767 | 764 | ||
| @@ -936,7 +933,7 @@ DIRECTION has to be either \\='forward or \\='backward." | |||
| 936 | ;; (i.e. for a closing token), need to step one character forward | 933 | ;; (i.e. for a closing token), need to step one character forward |
| 937 | ;; first, or the regexp will match the opening token. | 934 | ;; first, or the regexp will match the opening token. |
| 938 | (when (eq search-direction 'forward) (forward-char 1)) | 935 | (when (eq search-direction 'forward) (forward-char 1)) |
| 939 | (catch 'found ; 03e991 | 936 | (catch 'found |
| 940 | ;; If we are attempting to find a matching token for a terminating token | 937 | ;; If we are attempting to find a matching token for a terminating token |
| 941 | ;; (i.e. a token that starts a statement when searching back, or a token | 938 | ;; (i.e. a token that starts a statement when searching back, or a token |
| 942 | ;; that ends a statement when searching forward), then we don't need to look | 939 | ;; that ends a statement when searching forward), then we don't need to look |
| @@ -948,43 +945,42 @@ DIRECTION has to be either \\='forward or \\='backward." | |||
| 948 | (throw 'found nil)) | 945 | (throw 'found nil)) |
| 949 | (while (lua-find-regexp search-direction lua-indentation-modifier-regexp) ; 03e991 | 946 | (while (lua-find-regexp search-direction lua-indentation-modifier-regexp) ; 03e991 |
| 950 | ;; have we found a valid matching token? | 947 | ;; have we found a valid matching token? |
| 951 | (let ((found-token (match-string 0)) | 948 | (let* ((found-token (match-string 0)) |
| 952 | (found-pos (match-beginning 0))) | 949 | (found-pos (match-beginning 0)) |
| 953 | (let ((found-type (lua-get-token-type | 950 | (found-type (lua-get-token-type |
| 954 | (lua-get-block-token-info found-token)))) | 951 | (lua-get-block-token-info found-token)))) |
| 955 | (if (not (and match (string-match match found-token))) | 952 | (if (not (and match (string-match match found-token))) |
| 956 | ;; no - then there is a nested block. If we were looking for | 953 | ;; no - then there is a nested block. If we were looking for |
| 957 | ;; a block begin token, found-token must be a block end | 954 | ;; a block begin token, found-token must be a block end |
| 958 | ;; token; likewise, if we were looking for a block end token, | 955 | ;; token; likewise, if we were looking for a block end token, |
| 959 | ;; found-token must be a block begin token, otherwise there | 956 | ;; found-token must be a block begin token, otherwise there |
| 960 | ;; is a grammatical error in the code. | 957 | ;; is a grammatical error in the code. |
| 961 | (when (not (and ; 03e991 | 958 | (unless (and (or (eq match-type 'middle) |
| 962 | (or (eq match-type 'middle) | 959 | (eq found-type 'middle) |
| 963 | (eq found-type 'middle) | 960 | (eq match-type 'middle-or-open) |
| 964 | (eq match-type 'middle-or-open) | 961 | (eq found-type 'middle-or-open) |
| 965 | (eq found-type 'middle-or-open) | 962 | (eq match-type found-type)) |
| 966 | (eq match-type found-type)) | 963 | (goto-char found-pos) |
| 967 | (goto-char found-pos) | 964 | (lua-find-matching-token-word |
| 968 | (lua-find-matching-token-word found-token | 965 | found-token search-direction)) |
| 969 | search-direction))) | 966 | (when maybe-found-pos |
| 970 | (when maybe-found-pos | 967 | (goto-char maybe-found-pos) |
| 971 | (goto-char maybe-found-pos) | 968 | (throw 'found maybe-found-pos))) |
| 972 | (throw 'found maybe-found-pos))) | 969 | ;; yes. |
| 973 | ;; yes. | 970 | ;; if it is a not a middle kind, report the location |
| 974 | ;; if it is a not a middle kind, report the location | 971 | (unless (or (eq found-type 'middle) |
| 975 | (when (not (or (eq found-type 'middle) | 972 | (eq found-type 'middle-or-open)) |
| 976 | (eq found-type 'middle-or-open))) | 973 | (throw 'found found-pos)) |
| 977 | (throw 'found found-pos)) | 974 | ;; if it is a middle-or-open type, record location, but keep searching. |
| 978 | ;; if it is a middle-or-open type, record location, but keep searching. | 975 | ;; If we fail to complete the search, we'll report the location |
| 979 | ;; If we fail to complete the search, we'll report the location | 976 | (when (eq found-type 'middle-or-open) |
| 980 | (when (eq found-type 'middle-or-open) | 977 | (setq maybe-found-pos found-pos)) |
| 981 | (setq maybe-found-pos found-pos)) | 978 | ;; Cannot use tail recursion. too much nesting on long chains of |
| 982 | ;; Cannot use tail recursion. too much nesting on long chains of | 979 | ;; if/elseif. Will reset variables instead. |
| 983 | ;; if/elseif. Will reset variables instead. | 980 | (setq token found-token) |
| 984 | (setq token found-token) | 981 | (setq token-info (lua-get-block-token-info token)) |
| 985 | (setq token-info (lua-get-block-token-info token)) | 982 | (setq match (lua-get-token-match-re token-info search-direction)) |
| 986 | (setq match (lua-get-token-match-re token-info search-direction)) | 983 | (setq match-type (lua-get-token-type token-info))))) |
| 987 | (setq match-type (lua-get-token-type token-info)))))) | ||
| 988 | maybe-found-pos))) | 984 | maybe-found-pos))) |
| 989 | 985 | ||
| 990 | (defun lua-goto-matching-block-token (&optional parse-start direction) | 986 | (defun lua-goto-matching-block-token (&optional parse-start direction) |
| @@ -997,11 +993,10 @@ Optional PARSE-START is a position to which the point should be moved first. | |||
| 997 | DIRECTION has to be \\='forward or \\='backward (\\='forward by default)." | 993 | DIRECTION has to be \\='forward or \\='backward (\\='forward by default)." |
| 998 | (when parse-start (goto-char parse-start)) | 994 | (when parse-start (goto-char parse-start)) |
| 999 | (let ((case-fold-search nil)) | 995 | (let ((case-fold-search nil)) |
| 1000 | (when (looking-at lua-indentation-modifier-regexp) ; 03e991 | 996 | (when-let* (((looking-at lua-indentation-modifier-regexp)) |
| 1001 | (let ((position (lua-find-matching-token-word (match-string 0) ; 03e991 | 997 | (position (lua-find-matching-token-word |
| 1002 | direction))) | 998 | (match-string 0) direction))) |
| 1003 | (and position ; 03e991 | 999 | (goto-char position)))) |
| 1004 | (goto-char position)))))) ; 03e991 | ||
| 1005 | 1000 | ||
| 1006 | (defun lua-goto-matching-block (&optional noreport) | 1001 | (defun lua-goto-matching-block (&optional noreport) |
| 1007 | "Go to the keyword balancing the one under the point. | 1002 | "Go to the keyword balancing the one under the point. |
| @@ -1015,11 +1010,9 @@ is no block open/close open." | |||
| 1015 | (when (and (eq (char-syntax (following-char)) ?w) | 1010 | (when (and (eq (char-syntax (following-char)) ?w) |
| 1016 | (not (looking-at "\\_<"))) | 1011 | (not (looking-at "\\_<"))) |
| 1017 | (re-search-backward "\\_<" nil t)) | 1012 | (re-search-backward "\\_<" nil t)) |
| 1018 | (let ((position (lua-goto-matching-block-token))) | 1013 | (if-let* ((position (lua-goto-matching-block-token))) |
| 1019 | (if (and (not position) | 1014 | position |
| 1020 | (not noreport)) | 1015 | (unless noreport (error "Not on a block control keyword or brace")))) |
| 1021 | (error "Not on a block control keyword or brace") | ||
| 1022 | position))) | ||
| 1023 | 1016 | ||
| 1024 | (defun lua-skip-ws-and-comments-backward (&optional limit) | 1017 | (defun lua-skip-ws-and-comments-backward (&optional limit) |
| 1025 | "Move point back skipping all whitespace and comments. | 1018 | "Move point back skipping all whitespace and comments. |
| @@ -1030,14 +1023,12 @@ Return non-nil if moved point." | |||
| 1030 | (interactive) | 1023 | (interactive) |
| 1031 | (unless (lua-string-p) | 1024 | (unless (lua-string-p) |
| 1032 | (let ((start-pos (point)) | 1025 | (let ((start-pos (point)) |
| 1033 | (comment-start-pos (lua-comment-start-pos))) | 1026 | (comment-start-pos (lua-comment-start-pos)) |
| 1034 | (setq limit (min (point) (or limit (point-min)))) | 1027 | (limit (min (point) (or limit (point-min))))) |
| 1035 | (when comment-start-pos | 1028 | (when comment-start-pos (goto-char (max limit comment-start-pos))) |
| 1036 | (goto-char (max limit comment-start-pos))) | ||
| 1037 | (when (< limit (point)) (forward-comment (- limit (point)))) | 1029 | (when (< limit (point)) (forward-comment (- limit (point)))) |
| 1038 | (when (< (point) limit) (goto-char limit)) | 1030 | (when (< (point) limit) (goto-char limit)) |
| 1039 | (when (/= start-pos (point)) | 1031 | (when (/= start-pos (point)) (point))))) |
| 1040 | (point))))) | ||
| 1041 | 1032 | ||
| 1042 | (defun lua-skip-ws-and-comments-forward (&optional limit) | 1033 | (defun lua-skip-ws-and-comments-forward (&optional limit) |
| 1043 | "Move point forward skipping all whitespace and comments. | 1034 | "Move point forward skipping all whitespace and comments. |
| @@ -1048,8 +1039,8 @@ Return non-nil if moved point." | |||
| 1048 | (interactive) | 1039 | (interactive) |
| 1049 | (unless (lua-string-p) | 1040 | (unless (lua-string-p) |
| 1050 | (let ((start-pos (point)) | 1041 | (let ((start-pos (point)) |
| 1051 | (comment-start-pos (lua-comment-start-pos))) | 1042 | (comment-start-pos (lua-comment-start-pos)) |
| 1052 | (setq limit (max (point) (or limit (point-max)))) | 1043 | (limit (max (point) (or limit (point-max))))) |
| 1053 | ;; Escape from current comment. It is necessary to use "while" because | 1044 | ;; Escape from current comment. It is necessary to use "while" because |
| 1054 | ;; luadoc parameters have non-comment face, and parse-partial-sexp with | 1045 | ;; luadoc parameters have non-comment face, and parse-partial-sexp with |
| 1055 | ;; 'syntax-table flag will stop on them. | 1046 | ;; 'syntax-table flag will stop on them. |
| @@ -1058,8 +1049,7 @@ Return non-nil if moved point." | |||
| 1058 | (forward-comment 1)) | 1049 | (forward-comment 1)) |
| 1059 | (when (< (point) limit) (forward-comment (- limit (point)))) | 1050 | (when (< (point) limit) (forward-comment (- limit (point)))) |
| 1060 | (when (< limit (point)) (goto-char limit)) | 1051 | (when (< limit (point)) (goto-char limit)) |
| 1061 | (when (/= start-pos (point)) | 1052 | (when (/= start-pos (point)) (point))))) |
| 1062 | (point))))) | ||
| 1063 | 1053 | ||
| 1064 | (defun lua-forward-line-skip-blanks (&optional back) | 1054 | (defun lua-forward-line-skip-blanks (&optional back) |
| 1065 | "Move 1 line forward/backward and skip all insignificant ws/comment lines. | 1055 | "Move 1 line forward/backward and skip all insignificant ws/comment lines. |
| @@ -1261,7 +1251,7 @@ This true is when the line : | |||
| 1261 | * starts with a 1+ block-closer tokens, an top-most block opener is on a continuation line | 1251 | * starts with a 1+ block-closer tokens, an top-most block opener is on a continuation line |
| 1262 | " | 1252 | " |
| 1263 | (save-excursion | 1253 | (save-excursion |
| 1264 | (when parse-start (goto-char parse-start)) ; 03e991 | 1254 | (when parse-start (goto-char parse-start)) |
| 1265 | 1255 | ||
| 1266 | ;; If line starts with a series of closer tokens, whether or not the line | 1256 | ;; If line starts with a series of closer tokens, whether or not the line |
| 1267 | ;; is a continuation line is decided by the opener line, e.g. | 1257 | ;; is a continuation line is decided by the opener line, e.g. |