diff options
| author | Yuan Fu | 2022-11-22 00:49:04 -0800 |
|---|---|---|
| committer | Yuan Fu | 2022-11-22 00:56:28 -0800 |
| commit | 6fde1fcd0fb5111f3262e5ff829b19ec9f136d90 (patch) | |
| tree | 8b4b43dd41e5cfc3dfc5ca367435b863cef3bcc6 | |
| parent | 7144e38da828c885b6d11c96bbccbd72f527c162 (diff) | |
| download | emacs-feature/tree-sitter.tar.gz emacs-feature/tree-sitter.zip | |
Separate tree-sitter and non-tree-sitter variant of sh-modefeature/tree-sitter
Now there are three modes, sh-base-mode, sh-mode, bash-ts-mode.
The change I made: change sh-mode to sh-base-mode, remove docstring.
Below the new sh-base-mode, create a new definition for sh-mode, paste
the dostring, add setup for font-lock-defaults. Below sh-mode, add
bash-ts-mode.
* lisp/progmodes/sh-script.el (sh-mode): Moves all setup into
sh-base-mode, except for the setup for font-lock-defaults and the
docstring.
(sh-base-mode): New mode.
(bash-ts-mode): New mode.
| -rw-r--r-- | lisp/progmodes/sh-script.el | 142 |
1 files changed, 74 insertions, 68 deletions
diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index 54f005508ca..7fe31802c41 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el | |||
| @@ -1469,57 +1469,12 @@ When the region is active, send the region instead." | |||
| 1469 | (defvar sh-mode--treesit-settings) | 1469 | (defvar sh-mode--treesit-settings) |
| 1470 | 1470 | ||
| 1471 | ;;;###autoload | 1471 | ;;;###autoload |
| 1472 | (define-derived-mode sh-mode prog-mode "Shell-script" | 1472 | (define-derived-mode sh-base-mode prog-mode "Shell-script" |
| 1473 | "Major mode for editing shell scripts. | 1473 | "Generic major mode for editing shell scripts. |
| 1474 | This mode works for many shells, since they all have roughly the same syntax, | ||
| 1475 | as far as commands, arguments, variables, pipes, comments etc. are concerned. | ||
| 1476 | Unless the file's magic number indicates the shell, your usual shell is | ||
| 1477 | assumed. Since filenames rarely give a clue, they are not further analyzed. | ||
| 1478 | |||
| 1479 | This mode adapts to the variations between shells (see `sh-set-shell') by | ||
| 1480 | means of an inheritance based feature lookup (see `sh-feature'). This | ||
| 1481 | mechanism applies to all variables (including skeletons) that pertain to | ||
| 1482 | shell-specific features. Shell script files can use the `sh-shell' local | ||
| 1483 | variable to indicate the shell variant to be used for the file. | ||
| 1484 | |||
| 1485 | The default style of this mode is that of Rosenblatt's Korn shell book. | ||
| 1486 | The syntax of the statements varies with the shell being used. The | ||
| 1487 | following commands are available, based on the current shell's syntax: | ||
| 1488 | \\<sh-mode-map> | ||
| 1489 | \\[sh-case] case statement | ||
| 1490 | \\[sh-for] for loop | ||
| 1491 | \\[sh-function] function definition | ||
| 1492 | \\[sh-if] if statement | ||
| 1493 | \\[sh-indexed-loop] indexed loop from 1 to n | ||
| 1494 | \\[sh-while-getopts] while getopts loop | ||
| 1495 | \\[sh-repeat] repeat loop | ||
| 1496 | \\[sh-select] select loop | ||
| 1497 | \\[sh-until] until loop | ||
| 1498 | \\[sh-while] while loop | ||
| 1499 | |||
| 1500 | For sh and rc shells indentation commands are: | ||
| 1501 | \\[smie-config-show-indent] Show the rules controlling this line's indentation. | ||
| 1502 | \\[smie-config-set-indent] Change the rules controlling this line's indentation. | ||
| 1503 | \\[smie-config-guess] Try to tweak the indentation rules so the | ||
| 1504 | buffer indents as it currently is indented. | ||
| 1505 | |||
| 1506 | |||
| 1507 | \\[backward-delete-char-untabify] Delete backward one position, even if it was a tab. | ||
| 1508 | \\[sh-end-of-command] Go to end of successive commands. | ||
| 1509 | \\[sh-beginning-of-command] Go to beginning of successive commands. | ||
| 1510 | \\[sh-set-shell] Set this buffer's shell, and maybe its magic number. | ||
| 1511 | \\[sh-execute-region] Have optional header and region be executed in a subshell. | ||
| 1512 | |||
| 1513 | `sh-electric-here-document-mode' controls whether insertion of two | ||
| 1514 | unquoted < insert a here document. You can control this behavior by | ||
| 1515 | modifying `sh-mode-hook'. | ||
| 1516 | 1474 | ||
| 1517 | If you generally program a shell different from your login shell you can | 1475 | This is a generic major mode intended to be inherited by concrete |
| 1518 | set `sh-shell-file' accordingly. If your shell's file name doesn't correctly | 1476 | implementations. Currently there are two: `sh-mode' and |
| 1519 | indicate what shell it is use `sh-alias-alist' to translate. | 1477 | `bash-ts-mode'." |
| 1520 | |||
| 1521 | If your shell gives error messages with line numbers, you can use \\[executable-interpret] | ||
| 1522 | with your script for an edit-interpret-debug cycle." | ||
| 1523 | (make-local-variable 'sh-shell-file) | 1478 | (make-local-variable 'sh-shell-file) |
| 1524 | (make-local-variable 'sh-shell) | 1479 | (make-local-variable 'sh-shell) |
| 1525 | 1480 | ||
| @@ -1583,31 +1538,82 @@ with your script for an edit-interpret-debug cycle." | |||
| 1583 | nil nil) | 1538 | nil nil) |
| 1584 | (add-hook 'flymake-diagnostic-functions #'sh-shellcheck-flymake nil t) | 1539 | (add-hook 'flymake-diagnostic-functions #'sh-shellcheck-flymake nil t) |
| 1585 | (add-hook 'hack-local-variables-hook | 1540 | (add-hook 'hack-local-variables-hook |
| 1586 | #'sh-after-hack-local-variables nil t) | 1541 | #'sh-after-hack-local-variables nil t)) |
| 1587 | 1542 | ||
| 1588 | (cond | 1543 | ;;;###autoload |
| 1589 | ;; Tree-sitter. If the shell is bash, we can enable tree-sitter. | 1544 | (define-derived-mode sh-mode sh-base-mode "Shell-script" |
| 1590 | ((treesit-ready-p sh-shell) | 1545 | "Major mode for editing shell scripts. |
| 1546 | This mode works for many shells, since they all have roughly the same syntax, | ||
| 1547 | as far as commands, arguments, variables, pipes, comments etc. are concerned. | ||
| 1548 | Unless the file's magic number indicates the shell, your usual shell is | ||
| 1549 | assumed. Since filenames rarely give a clue, they are not further analyzed. | ||
| 1550 | |||
| 1551 | This mode adapts to the variations between shells (see `sh-set-shell') by | ||
| 1552 | means of an inheritance based feature lookup (see `sh-feature'). This | ||
| 1553 | mechanism applies to all variables (including skeletons) that pertain to | ||
| 1554 | shell-specific features. Shell script files can use the `sh-shell' local | ||
| 1555 | variable to indicate the shell variant to be used for the file. | ||
| 1556 | |||
| 1557 | The default style of this mode is that of Rosenblatt's Korn shell book. | ||
| 1558 | The syntax of the statements varies with the shell being used. The | ||
| 1559 | following commands are available, based on the current shell's syntax: | ||
| 1560 | \\<sh-mode-map> | ||
| 1561 | \\[sh-case] case statement | ||
| 1562 | \\[sh-for] for loop | ||
| 1563 | \\[sh-function] function definition | ||
| 1564 | \\[sh-if] if statement | ||
| 1565 | \\[sh-indexed-loop] indexed loop from 1 to n | ||
| 1566 | \\[sh-while-getopts] while getopts loop | ||
| 1567 | \\[sh-repeat] repeat loop | ||
| 1568 | \\[sh-select] select loop | ||
| 1569 | \\[sh-until] until loop | ||
| 1570 | \\[sh-while] while loop | ||
| 1571 | |||
| 1572 | For sh and rc shells indentation commands are: | ||
| 1573 | \\[smie-config-show-indent] Show the rules controlling this line's indentation. | ||
| 1574 | \\[smie-config-set-indent] Change the rules controlling this line's indentation. | ||
| 1575 | \\[smie-config-guess] Try to tweak the indentation rules so the | ||
| 1576 | buffer indents as it currently is indented. | ||
| 1577 | |||
| 1578 | |||
| 1579 | \\[backward-delete-char-untabify] Delete backward one position, even if it was a tab. | ||
| 1580 | \\[sh-end-of-command] Go to end of successive commands. | ||
| 1581 | \\[sh-beginning-of-command] Go to beginning of successive commands. | ||
| 1582 | \\[sh-set-shell] Set this buffer's shell, and maybe its magic number. | ||
| 1583 | \\[sh-execute-region] Have optional header and region be executed in a subshell. | ||
| 1584 | |||
| 1585 | `sh-electric-here-document-mode' controls whether insertion of two | ||
| 1586 | unquoted < insert a here document. You can control this behavior by | ||
| 1587 | modifying `sh-mode-hook'. | ||
| 1588 | |||
| 1589 | If you generally program a shell different from your login shell you can | ||
| 1590 | set `sh-shell-file' accordingly. If your shell's file name doesn't correctly | ||
| 1591 | indicate what shell it is use `sh-alias-alist' to translate. | ||
| 1592 | |||
| 1593 | If your shell gives error messages with line numbers, you can use \\[executable-interpret] | ||
| 1594 | with your script for an edit-interpret-debug cycle." | ||
| 1595 | (setq font-lock-defaults | ||
| 1596 | `((sh-font-lock-keywords | ||
| 1597 | sh-font-lock-keywords-1 sh-font-lock-keywords-2) | ||
| 1598 | nil nil | ||
| 1599 | ((?/ . "w") (?~ . "w") (?. . "w") (?- . "w") (?_ . "w")) nil | ||
| 1600 | (font-lock-syntactic-face-function | ||
| 1601 | . ,#'sh-font-lock-syntactic-face-function)))) | ||
| 1602 | |||
| 1603 | ;;;###autoload | ||
| 1604 | (defalias 'shell-script-mode 'sh-mode) | ||
| 1605 | |||
| 1606 | ;;;###autoload | ||
| 1607 | (define-derived-mode bash-ts-mode sh-base-mode "Bash" | ||
| 1608 | "Major mode for editing Bash shell scripts." | ||
| 1609 | (when (treesit-ready-p 'bash) | ||
| 1591 | (setq-local treesit-font-lock-feature-list | 1610 | (setq-local treesit-font-lock-feature-list |
| 1592 | '((comment function string heredoc) | 1611 | '((comment function string heredoc) |
| 1593 | (variable keyword command declaration-command) | 1612 | (variable keyword command declaration-command) |
| 1594 | (constant operator builtin-variable))) | 1613 | (constant operator builtin-variable))) |
| 1595 | (setq-local treesit-font-lock-settings | 1614 | (setq-local treesit-font-lock-settings |
| 1596 | sh-mode--treesit-settings) | 1615 | sh-mode--treesit-settings) |
| 1597 | (treesit-major-mode-setup)) | 1616 | (treesit-major-mode-setup))) |
| 1598 | ;; Elisp. | ||
| 1599 | (t | ||
| 1600 | (setq font-lock-defaults | ||
| 1601 | `((sh-font-lock-keywords | ||
| 1602 | sh-font-lock-keywords-1 sh-font-lock-keywords-2) | ||
| 1603 | nil nil | ||
| 1604 | ((?/ . "w") (?~ . "w") (?. . "w") (?- . "w") (?_ . "w")) nil | ||
| 1605 | (font-lock-syntactic-face-function | ||
| 1606 | . ,#'sh-font-lock-syntactic-face-function)))))) | ||
| 1607 | |||
| 1608 | ;;;###autoload | ||
| 1609 | (defalias 'shell-script-mode 'sh-mode) | ||
| 1610 | |||
| 1611 | 1617 | ||
| 1612 | (defun sh-font-lock-keywords (&optional keywords) | 1618 | (defun sh-font-lock-keywords (&optional keywords) |
| 1613 | "Function to get simple fontification based on `sh-font-lock-keywords'. | 1619 | "Function to get simple fontification based on `sh-font-lock-keywords'. |