├── .gitignore ├── error.png ├── typeinfo.png ├── Cask ├── test ├── resources │ ├── ModulesAndTypes.res │ ├── Simple.res │ └── Exceptions.res ├── rescript-mode-test.el └── test-helper.el ├── rescript-mode.el ├── README.md ├── rescript-indent.el └── COPYING /.gitignore: -------------------------------------------------------------------------------- 1 | *.elc 2 | *.cask 3 | dist/ 4 | -------------------------------------------------------------------------------- /error.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jjlee/rescript-mode/HEAD/error.png -------------------------------------------------------------------------------- /typeinfo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jjlee/rescript-mode/HEAD/typeinfo.png -------------------------------------------------------------------------------- /Cask: -------------------------------------------------------------------------------- 1 | (source gnu) 2 | (source melpa) 3 | 4 | (package-file "rescript-mode.el") 5 | 6 | (files "*.el"(:exclude ".dir-locals.el")) 7 | 8 | (development 9 | (depends-on "ert-runner")) 10 | -------------------------------------------------------------------------------- /test/resources/ModulesAndTypes.res: -------------------------------------------------------------------------------- 1 | type spam<'a> = { 2 | ham: list<'a>, 3 | } 4 | 5 | module Play = { 6 | open Js.Array2 7 | type apples = { 8 | mutable spam: string, 9 | ham: bool, 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/rescript-mode-test.el: -------------------------------------------------------------------------------- 1 | ;;; rescript-mode-test.el --- Tests for rescript-mode 2 | 3 | (require 'ert) 4 | (require 'rescript-mode) 5 | 6 | (ert-deftest rescript-mode-hmm () 7 | (should t)) 8 | 9 | (rescript-mode-deftest-indent "Simple.res") 10 | (rescript-mode-deftest-indent "ModulesAndTypes.res") 11 | (rescript-mode-deftest-indent "Exceptions.res") 12 | 13 | ;;; rescript-mode-test.el ends here 14 | -------------------------------------------------------------------------------- /test/resources/Simple.res: -------------------------------------------------------------------------------- 1 | @bs.val external document: {..} = "document" 2 | 3 | // Comment 4 | 5 | let style = document["createElement"]("style") 6 | document["head"]["appendChild"](style) 7 | style["innerHTML"] = ExampleStyles.style 8 | 9 | let makeContainer = _ => { 10 | let container = document["createElement"]("div") 11 | let () = document["body"]["appendChild"](container) 12 | container 13 | } 14 | -------------------------------------------------------------------------------- /test/resources/Exceptions.res: -------------------------------------------------------------------------------- 1 | // Obviously ideally this would be indented two spaces not four, have not 2 | // investigated... 3 | 4 | let getItem = (items) => 5 | if callSomeFunctionThatThrows() { 6 | // return the found item here 7 | 1 8 | } else { 9 | raise(Not_found) 10 | } 11 | 12 | let result = 13 | try { 14 | getItem([1, 2, 3]) 15 | } catch { 16 | | Not_found => 0 // Default value if getItem throws 17 | } 18 | -------------------------------------------------------------------------------- /test/test-helper.el: -------------------------------------------------------------------------------- 1 | ;;; test-helper.el --- Helpers for rescript-mode-test.el 2 | 3 | ;; This resource file stuff is from ert-x.el and macroexp.el from Emacs git 6dabbdd 4 | (defun rescript-mode-tests--file-name () 5 | ;; `eval-buffer' binds `current-load-list' but not `load-file-name', 6 | ;; so prefer using it over using `load-file-name'. 7 | (let ((file (car (last current-load-list)))) 8 | (or (if (stringp file) file) 9 | (bound-and-true-p byte-compile-current-file)))) 10 | 11 | ;; Has to be a macro for `load-file-name'. 12 | (defmacro rescript-mode-tests--resource-directory () 13 | `(let* ((testfile ,(or (rescript-mode-tests--file-name) 14 | buffer-file-name)) 15 | (default-directory (file-name-directory testfile))) 16 | (file-truename 17 | (if (file-accessible-directory-p "resources/") 18 | (expand-file-name "resources/") 19 | (expand-file-name 20 | (format "%s-resources/" 21 | (string-trim testfile 22 | "" 23 | "\\(-tests?\\)?\\.el"))))))) 24 | 25 | (defmacro rescript-mode-tests--resource-file (file) 26 | `(expand-file-name ,file (rescript-mode-tests--resource-directory))) 27 | 28 | 29 | ;; This is from js.el's own tests 30 | (defun rescript-mode-tests--remove-indentation () 31 | "Remove all indentation in the current buffer." 32 | (goto-char (point-min)) 33 | (while (re-search-forward (rx bol (+ (in " \t"))) nil t) 34 | (let ((syntax (save-match-data (syntax-ppss)))) 35 | (unless (nth 3 syntax) ; Avoid multiline string literals. 36 | (replace-match ""))))) 37 | 38 | (defmacro rescript-mode-deftest-indent (file) 39 | `(ert-deftest ,(intern (format "rescript-mode-indent-test/%s" file)) () 40 | :tags '(:expensive-test) 41 | (let ((buf (find-file-noselect (rescript-mode-tests--resource-file ,file)))) 42 | (unwind-protect 43 | (with-current-buffer buf 44 | (let ((orig (buffer-string))) 45 | (rescript-mode-tests--remove-indentation) 46 | ;; Indent and check that we get the original text. 47 | (indent-region (point-min) (point-max)) 48 | (should (equal (buffer-string) orig)) 49 | ;; Verify idempotency. 50 | (indent-region (point-min) (point-max)) 51 | (should (equal (buffer-string) orig)))) 52 | (kill-buffer buf))))) 53 | 54 | ;;; test-helper.el ends here 55 | -------------------------------------------------------------------------------- /rescript-mode.el: -------------------------------------------------------------------------------- 1 | ;;; rescript-mode.el --- A major mode for editing ReScript -*-lexical-binding: t-*- 2 | ;; Portions Copyright (c) 2015-present, Facebook, Inc. All rights reserved. 3 | ;; Copyright (C) 2021 John Lee 4 | 5 | ;; Version: 0.1.0 6 | ;; Author: Karl Landstrom 7 | ;; Daniel Colascione 8 | ;; John Lee 9 | ;; Maintainer: John Lee 10 | ;; Url: https://github.com/jjlee/rescript-mode 11 | ;; Keywords: languages, rescript 12 | ;; Package-Requires: ((emacs "26.1")) 13 | 14 | ;; This file is NOT part of GNU Emacs. 15 | 16 | ;; This program is free software: you can redistribute it and/or modify 17 | ;; it under the terms of the GNU General Public License as published by 18 | ;; the Free Software Foundation, either version 3 of the License, or 19 | ;; (at your option) any later version. 20 | 21 | ;; This program is distributed in the hope that it will be useful, 22 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 | ;; GNU General Public License for more details. 25 | 26 | ;; You should have received a copy of the GNU General Public License 27 | ;; along with this program. If not, see . 28 | 29 | ;;; Commentary: 30 | ;; This project provides useful functions and helpers for developing code 31 | ;; using the ReScript programming language 32 | ;; 33 | ;; The indentation code comes from js.el. The rest is based on reason-mode.el. 34 | ;; 35 | ;; Exported names start with "rescript-"; private names start with 36 | ;; "rescript--". 37 | 38 | ;;; Code: 39 | 40 | (eval-when-compile (require 'rx) 41 | (require 'compile) 42 | (require 'url-vars)) 43 | 44 | (defgroup rescript nil 45 | "Support for ReScript code." 46 | :link '(url-link "https://rescript-lang.org/") 47 | :group 'languages) 48 | 49 | (require 'rescript-indent) 50 | 51 | (defconst rescript--re-ident "[[:word:][:multibyte:]_][[:word:][:multibyte:]_[:digit:]]*") 52 | 53 | ;; Syntax definitions and helpers 54 | (defvar rescript-mode-syntax-table 55 | (let ((table (make-syntax-table))) 56 | 57 | ;; Operators 58 | (dolist (i '(?+ ?- ?* ?/ ?& ?| ?^ ?! ?< ?> ?~ ?@)) 59 | (modify-syntax-entry i "." table)) 60 | 61 | ;; Strings 62 | (modify-syntax-entry ?\" "\"" table) 63 | (modify-syntax-entry ?\` "\"" table) 64 | (modify-syntax-entry ?\\ "\\" table) 65 | (modify-syntax-entry ?\' "_" table) 66 | 67 | ;; Comments 68 | (modify-syntax-entry ?/ ". 124b" table) 69 | (modify-syntax-entry ?* ". 23n" table) 70 | (modify-syntax-entry ?\n "> b" table) 71 | (modify-syntax-entry ?\^m "> b" table) 72 | 73 | table)) 74 | 75 | ;; in rescript-vscode grammar file, what are they? 76 | ;; import 77 | ;; library 78 | ;; export 79 | 80 | ;; Font-locking definitions and helpers 81 | (defconst rescript--keywords 82 | '("and" "as" "assert" "async" "await" 83 | "constraint" 84 | "else" "exception" "export" "external" 85 | "false" "for" 86 | "if" "import" "in" "include" 87 | "lazy" "let" "list{" 88 | "module" "mutable" 89 | "of" "open" 90 | "private" 91 | "rec" 92 | "switch" 93 | "true" "try" "type" 94 | "when" "while")) 95 | 96 | 97 | (defconst rescript--consts 98 | '("true" "false")) 99 | 100 | (defconst rescript--special-types 101 | '("int" "float" "string" "char" 102 | "bool" "unit" "list" "array" "exn" 103 | "option" "ref")) 104 | 105 | (defconst rescript--camel-case 106 | (rx symbol-start 107 | (group upper (0+ (any word nonascii digit "_"))) 108 | symbol-end)) 109 | 110 | (eval-and-compile 111 | (defconst rescript--char-literal-rx 112 | (rx (seq (group "'") 113 | (or (seq "\\" anything) 114 | (not (any "'\\"))) 115 | (group "'"))))) 116 | 117 | (defun rescript--re-grab (inner) 118 | "Build a grab regexp given INNER." 119 | (concat "\\(" inner "\\)")) 120 | 121 | ;;; Syntax highlighting for Rescript 122 | (defvar rescript--font-lock-keywords 123 | `((,(regexp-opt rescript--keywords 'symbols) . font-lock-keyword-face) 124 | (,(regexp-opt rescript--special-types 'symbols) . font-lock-builtin-face) 125 | (,(regexp-opt rescript--consts 'symbols) . font-lock-constant-face) 126 | 127 | (,rescript--camel-case 1 font-lock-type-face) 128 | 129 | ;; Field names like `foo:`, highlight excluding the : 130 | (,(concat (rescript--re-grab rescript--re-ident) ":[^:]") 1 font-lock-variable-name-face) 131 | ;; Module names like `foo::`, highlight including the :: 132 | (,(rescript--re-grab (concat rescript--re-ident "::")) 1 font-lock-type-face) 133 | ;; Name punned labeled args like ::foo 134 | (,(concat "[[:space:]]+" (rescript--re-grab (concat "::" rescript--re-ident))) 1 font-lock-type-face) 135 | 136 | ;; TODO jsx attribs? 137 | (, 138 | (concat "<[/]?" (rescript--re-grab rescript--re-ident) "[^>]*" ">") 139 | 1 font-lock-type-face))) 140 | 141 | (defvar rescript-mode-map 142 | (let ((map (make-sparse-keymap))) 143 | ;; (define-key map "\C-c\C-a" #'rescript-mode-find-alternate-file) 144 | map)) 145 | 146 | ;;;###autoload 147 | (define-derived-mode rescript-mode prog-mode "ReScript" 148 | "Major mode for ReScript code. 149 | 150 | \\{rescript-mode-map}" 151 | :keymap rescript-mode-map 152 | 153 | ;; Indentation 154 | (setq-local indent-line-function #'rescript-indent-line) 155 | (setq-local comment-start "/* ") 156 | (setq-local comment-end " */") 157 | (setq-local indent-tabs-mode nil) 158 | ;; Allow paragraph fills for comments 159 | (setq-local comment-start-skip "/\\*+[ \t]*") 160 | (setq-local paragraph-start 161 | (concat "^[ \t]*$\\|\\*)$\\|" page-delimiter)) 162 | (setq-local paragraph-separate paragraph-start) 163 | (setq-local require-final-newline t) 164 | (setq-local normal-auto-fill-function nil) 165 | (setq-local comment-multi-line t) 166 | (setq-local comment-start "// ") 167 | (setq-local comment-start-skip "\\(//+\\|/\\*+\\)\\s *") 168 | (setq-local comment-end "") 169 | ;; Fonts 170 | (setq-local font-lock-defaults '(rescript--font-lock-keywords))) 171 | 172 | 173 | ;;; Compilation Error Regexs 174 | (eval-and-compile 175 | (defconst rescript--compilation-error-rx 176 | (rx-to-string 177 | '(seq 178 | (or 179 | "We've found a bug for you!" 180 | "Syntax error!" 181 | (seq 182 | "Warning number" 183 | (* space) 184 | (+ digit) 185 | (* space) 186 | "(configured as error)")) 187 | (* (or space control)) 188 | line-start 189 | (* space) 190 | (group (group (seq (* any) (or ".res" ".resi"))) 191 | ":" 192 | (group (+ digit)) 193 | ":" 194 | (group (+ digit)) 195 | (? "-" (+ digit) (? ":" (+ digit)))) 196 | line-end))) 197 | 198 | 199 | (defconst rescript--compilation-warning-rx 200 | (rx-to-string 201 | '(seq 202 | "Warning number" 203 | (+ space) 204 | (+ digit) 205 | (* (or space control)) 206 | line-start 207 | (* space) 208 | (group (group (seq (* any) (or ".res" ".resi"))) 209 | ":" 210 | (group (+ digit)) 211 | ":" 212 | (group (+ digit)) 213 | (? "-" (+ digit))) 214 | line-end)))) 215 | 216 | 217 | (add-to-list 'compilation-error-regexp-alist 'rescript-error) 218 | (add-to-list 'compilation-error-regexp-alist 'rescript-warning) 219 | 220 | (add-to-list 221 | 'compilation-error-regexp-alist-alist 222 | (cons 'rescript-error (cons rescript--compilation-error-rx '(2 3 4 2 1)))) 223 | 224 | (add-to-list 225 | 'compilation-error-regexp-alist-alist 226 | (cons 'rescript-warning (cons rescript--compilation-warning-rx '(2 3 4 1 1)))) 227 | 228 | 229 | 230 | ;;;###autoload 231 | (add-to-list 'auto-mode-alist '("\\.resi?\\'" . rescript-mode)) 232 | 233 | (provide 'rescript-mode) 234 | ;;; rescript-mode.el ends here 235 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Emacs major mode for ReScript, part of [experimental](#support) Emacs support 2 | for the [ReScript Language](https://rescript-lang.org/). 3 | 4 | New ReScript users are advised to use one of the [editors with official support 5 | from the ReScript 6 | team](https://rescript-lang.org/docs/manual/latest/editor-plugins). 7 | 8 | ## How to Get it Working 9 | 10 | Apart from a working [ReScript install](https://rescript-lang.org/docs/manual/latest/installation) and this code, for a full setup you need: 11 | 12 | * Strongly recommended: Emacs 27.0 or newer built with native JSON support, for LSP performance 13 | * The [ReScript language server](https://github.com/rescript-lang/rescript-vscode/tree/master/server), for type information, compiler errors, completion, and jump to definition/find references 14 | * Currently, [a way to format ReScript code](#formatting) (LSP also provides this, but there is an lsp-mode bug I need to report/fix before this works) 15 | 16 | ### ReScript language server 17 | 18 | TODO: bundle this or provide a way of auto-installing it 19 | 20 | Install the language server globally with: 21 | 22 | ```sh 23 | npm i -g @rescript/language-server 24 | ``` 25 | 26 | ### Vanilla Emacs 27 | 28 | #### [LSP mode](https://emacs-lsp.github.io/lsp-mode/) 29 | 30 | `lsp-rescript` provides configuration code for `lsp-mode` and depends on `rescript-mode`. 31 | 32 | Install the following packages (e.g. using `M-x package-install` -- you can use 33 | things like use-package if you like of course): 34 | 35 | * lsp-rescript 36 | * lsp-ui 37 | 38 | Add the following to your Emacs configuration code (for example to `~/.emacs`): 39 | 40 | ```elisp 41 | ;; Tell `rescript-mode` how to run the language server 42 | (customize-set-variable 43 | 'lsp-rescript-server-command 44 | '("rescript-language-server" "--stdio")) 45 | (with-eval-after-load 'rescript-mode 46 | ;; Tell `lsp-mode` about the language server 47 | (require 'lsp-rescript) 48 | ;; Enable `lsp-mode` in rescript-mode buffers 49 | (add-hook 'rescript-mode-hook 'lsp-deferred) 50 | ;; Enable display of type information in rescript-mode buffers 51 | (require 'lsp-ui) 52 | (add-hook 'rescript-mode-hook 'lsp-ui-doc-mode)) 53 | ``` 54 | 55 | For now: ensure that `reason-mode` is not installed, e.g. using `package-delete` 56 | (I guess `reason-mode` needs to change so that `.re` is considered Reason code 57 | but not `.res`). 58 | 59 | Restart Emacs and open a ReScript `.res` file and you should have all the 60 | features working. 61 | 62 | Note that vanilla Emacs handles the LSP prompt for `"Start a build for this 63 | project to get the freshest data?"` on opening a ReScript file in a rather 64 | unfriendly way: you have to hit `TAB` to see the single possible `Start a Build` 65 | response, then hit return. 66 | 67 | #### [eglot](https://github.com/joaotavora/eglot) 68 | 69 | Install the following packages (e.g. using `M-x package-install`: 70 | 71 | * rescript-mode 72 | * eglot 73 | 74 | This configuration uses `use-package`, which you also need to install, but it 75 | shouldn't be too hard to rewrite this to not use `use-package`. 76 | 77 | ```elisp 78 | (use-package rescript-mode 79 | :hook ((rescript-mode . (lambda () (electric-indent-local-mode -1)))) 80 | :config 81 | (add-to-list 'eglot-server-programs 82 | '(rescript-mode . ("rescript-language-server" "--stdio")))) 83 | ``` 84 | 85 | ### Doom Emacs 86 | 87 | #### Default LSP 88 | 89 | Ensure `lsp` is enabled in `init.el`. 90 | 91 | In `packages.el` add: 92 | 93 | ```elisp 94 | (package! rescript-mode) 95 | (package! lsp-rescript) 96 | ``` 97 | 98 | Then in `config.el` add: 99 | 100 | ```elisp 101 | (after! rescript-mode 102 | (setq lsp-rescript-server-command 103 | '("rescript-language-server" "--stdio")) 104 | ;; Tell `lsp-mode` about the `rescript-vscode` LSP server 105 | (require 'lsp-rescript) 106 | ;; Enable `lsp-mode` in rescript-mode buffers 107 | (add-hook 'rescript-mode-hook 'lsp-deferred) 108 | ;; Enable display of type information in rescript-mode buffers 109 | (require 'lsp-ui) 110 | (add-hook 'rescript-mode-hook 'lsp-ui-doc-mode)) 111 | ``` 112 | 113 | #### [eglot](https://github.com/joaotavora/eglot) 114 | 115 | In `init.el`, ensure you have the `+eglot` option for `lsp`: 116 | 117 | ```elisp 118 | (lsp +eglot) 119 | ``` 120 | 121 | In `packages.el` add: 122 | 123 | ```elisp 124 | (package! rescript-mode) 125 | ``` 126 | 127 | Then in `config.el` add: 128 | 129 | ```elisp 130 | (after! eglot 131 | (add-to-list 'eglot-server-programs 132 | '(rescript-mode . ("rescript-language-server" "--stdio"))) 133 | ) 134 | 135 | (add-hook 'rescript-mode-hook (lambda () (eglot-ensure))) 136 | ``` 137 | 138 | 139 | ### Spacemacs 140 | 141 | #### [LSP mode](https://emacs-lsp.github.io/lsp-mode/) 142 | 143 | TODO: make a configuration layer 144 | 145 | Add `lsp` to the `dotspacemacs-configuration-layers` section of your spacemacs 146 | configuration file (`SPC f e d` to find that file) -- it should look something 147 | like this: 148 | 149 | ```elisp 150 | dotspacemacs-configuration-layers 151 | '( 152 | lsp 153 | ) 154 | ``` 155 | 156 | Add `rescript-mode` and `lsp-rescript` to the `dotspacemacs-additional-packages` 157 | section of your spacemacs configuration file -- it should look something like 158 | this: 159 | 160 | ```elisp 161 | dotspacemacs-additional-packages 162 | '( 163 | lsp-rescript 164 | rescript-mode 165 | ) 166 | ``` 167 | 168 | Add this to the `dotspacemacs/user-config` section of your spacemacs 169 | configuration file: 170 | 171 | ```elisp 172 | ;; Tell `rescript-mode` how to run the language server 173 | (customize-set-variable 174 | 'lsp-rescript-server-command 175 | '("rescript-language-server" "--stdio")) 176 | (with-eval-after-load 'rescript-mode 177 | ;; Tell `lsp-mode` about the lamguage server 178 | (require 'lsp-rescript) 179 | ;; All I remember is something weird happened if this wasn't there :-) 180 | (spacemacs|define-jump-handlers rescript-mode) 181 | ;; Enable `lsp-mode` in rescript-mode buffers 182 | (add-hook 'rescript-mode-hook 'lsp-deferred) 183 | ;; Enable display of type information in rescript-mode buffers 184 | (require 'lsp-ui) 185 | (add-hook 'rescript-mode-hook 'lsp-ui-doc-mode)) 186 | ``` 187 | 188 | For now: ensure that `reasonml` layer is not listed in 189 | `dotspacemacs-configuration-layers` and `reason-mode` is not listed in 190 | `dotspacemacs-additional-packages` (I guess `reason-mode` needs to change so 191 | that `.re` is considered Reason code but not `.res`). 192 | 193 | Restart spacemacs (`SPC q r`) and open a ReScript `.res` file and you should 194 | have all the features working. 195 | 196 | 197 | ### Formatting and indentation 198 | 199 | #### Formatting vs. Indentation 200 | 201 | In case the distinction is unclear: 202 | 203 | Formatting: This means that you run an Emacs command, and your whole buffer (or 204 | some section of it that you specify maybe) is magically formatted correctly -- 205 | that is, in the way that `rescript format` formats it. 206 | 207 | Indentation: This means that hitting the tab key or the return key (depending 208 | how you have things configured I guess) gives you an approximation of the 209 | “official” formatting of a tool like `rescript format`. It’s never identical to 210 | proper formatting, but stops you having to pay attention to formatting when 211 | writing code. 212 | 213 | #### Formatting 214 | 215 | You can use a package like 216 | [`format-all`](https://github.com/lassik/emacs-format-all-the-code) or 217 | [`reformatter`](https://github.com/purcell/reformatter.el) to get your code 218 | formatted correctly (i.e. as `rescript format`, soon to be renamed `rescript 219 | format`, formats it -- this is like `gofmt` for ReScript). See [this 220 | thread](https://forum.rescript-lang.org/t/rescript-emacs-support-with-rescript-vscode/1056/14) 221 | (I've not tried either of these). 222 | 223 | `lsp-mode` will make this part unnecessary when I get around to submitting a fix 224 | for an `lsp-mode` bug that currently causes `lsp-format-buffer` not to work with 225 | rescript-vscode. 226 | 227 | ### Indentation 228 | 229 | This is a terrible hack: it's lifted straight from `js-mode` (`js.el`) with 230 | little effort to adapt it to ReScript, and without any JSX support. 231 | Nevertheless, aside from JSX it seems to work OK. 232 | 233 | For more predictability, you may prefer to use something like `indent-relative` 234 | or `indent-relative-first-indent-point`, by adding something like this in your 235 | `(with-eval-after-load 'rescript mode ...`: 236 | 237 | ```elisp 238 | (define my/rescript-mode-hook () 239 | (setq-local indent-line-function #'indent-relative)) 240 | (add-hook 'rescript-mode-hook #'my/rescript-mode-hook) 241 | ``` 242 | 243 | ## Features 244 | 245 | Aside from the usual font-lock and indentation provided by any language major 246 | mode, this is what is provided by LSP: 247 | 248 | ### Builds and Errors 249 | 250 | You should see any errors show up via flycheck -- for me they look like this: 251 | 252 | ![Flycheck error](./error.png) 253 | 254 | These errors only show up when you save. 255 | 256 | If you don't see that, `rescript build *` may not be running on your project. 257 | 258 | To provide these UI for these errors, LSP mode falls back to `flymake` if 259 | `flycheck` is not installed, so it's recommended to install the latter. 260 | 261 | When you open a `.res` file in your project, you should see a prompt in Emacs in 262 | the minibuffer `"Start a build for this project to get the freshest data?"`. 263 | You can either hit return on `Start Build` to say yes to that and the LSP server 264 | will start a build for you, or `C-g` out of that and run `rescript build *` yourself however 265 | you usually do that in your rescript project (in my project I run `npm start`). 266 | 267 | You may find the UI here (how the `Start Build` option is presented) is a bit 268 | different from how I describe it depending if you're using vanilla emacs or some 269 | configuration that uses a package like `ivy` or `helm` that overrides the 270 | behaviour of `completing-read`. 271 | 272 | If you never want to see this prompt you can put this in your configuration: 273 | 274 | ```elisp 275 | (custom-set-variables '(lsp-rescript-prompt-for-build nil)) 276 | ``` 277 | 278 | If you don't see the `"Start a build for this project to get the freshest 279 | data?"` prompt, that may be because a build is already running somehow, or you 280 | may have a stale `.bsb.lock` lock file in your project. 281 | 282 | ### Type Information 283 | 284 | The configuration above enables `lsp-ui-doc-mode`. Hovering with the mouse or 285 | moving point to some code should give a popup like this: 286 | 287 | ![Type information](./typeinfo.png) 288 | 289 | Here are some other ways to see type information if you don't like it popping up 290 | automatically: 291 | 292 | * You can leave `lsp-ui-doc-mode` off and just use `lsp-ui-doc-glance` every 293 | time you want to see it. 294 | * You can use `lsp-describe-thing-at-point` to see the type in a window instead 295 | of in a popup overlay. 296 | 297 | ### Completion 298 | 299 | `lsp-mode`'s completion UI is provided by `company-mode`, so take a look at the 300 | docs for the latter for more about that. 301 | 302 | ### Jump to Definition / Find References 303 | 304 | You can use functions like `lsp-find-definition` and `lsp-find-references`. 305 | 306 | I believe functions like `xref-find-definitions` and `xref-find-references` also 307 | end up using LSP and seem equivalent to the LSP functions for ReScript purposes. 308 | `lsp-ui-peek-find-definitions` also seems equivalent to `lsp-find-definition`. 309 | 310 | `lsp-ui-peek-find-references` is a fancier more GUI-fied version of 311 | `lsp-find-references` which seems to only sometimes work for me (when it does, 312 | you get a complicated overlay -- it looks like a collection of windows but is 313 | not -- in which you can preview references and navigate through them; other 314 | times it seems to jump me, sometimes inaccurately, to the definition). 315 | `lsp-find-references` itself seems to not find references in other files for me, 316 | I haven't yet tested to see what the behaviour is in VS code. 317 | 318 | In spacemacs `, g g` (`spacemacs/jump-to-definition`) ends up rather indirectly 319 | using `lsp-ui-peek-find-definitions` (which is also bound directly to `, G d`). 320 | `, g r` is `xref-find-references`. `, G r` is `lsp-ui-peek-find-references` 321 | (when it shows the overlay, `j` and `k` or `n` and `p` navigate the list of 322 | references, I think `h` and `l` would navigate the file list if I ever saw one, 323 | and `q` quits). 324 | 325 | ## Problems 326 | 327 | If you don't see type information and errors: the ReScript compiler should be 328 | running, and in order to run it, you need the JavaScript dependencies of the 329 | project you're editing. Often those are fetched by running `npm install`. When 330 | you've done that, you can `revert-buffer` (or `SPC b R` in spacemacs) to reload 331 | the `.res` file you're looking at, which should prompt you to ask if you'd like 332 | to start a build (i.e. run the ReScript compiler). 333 | 334 | If you run into problems with display of compilation errors 335 | (`flycheck`/`flymake` errors), try this to get rid of any stale ReScript build: 336 | 337 | * Kill any `rescript` or `bsb` processes 338 | * Remove any .bsb.lock file in your project 339 | * `M-x revert-buffer` on the .res file you're trying to edit 340 | 341 | If you run into problems with other things, you can try killing the language server, 342 | and then if LSP doesn't automatically prompt you to restart the server, `M-x lsp`. 343 | 344 | 345 | ## Known Issues 346 | 347 | I've barely used this yet, so probably a lot of things are very broken! 348 | 349 | See the github issues, but notably: 350 | 351 | 352 | Emacs support 353 | * [Indentation](#indentation) is a terrible hack and should very likely be 354 | replaced with 355 | [tree-sitter-rescript](https://github.com/nkrkv/tree-sitter-rescript/) and 356 | [elisp-tree-sitter](https://github.com/emacs-tree-sitter/elisp-tree-sitter) 357 | and [tree-sitter-indent](https://github.com/emacsmirror/tree-sitter-indent) -- 358 | probably very easy? Perhaps this will also improve font-lock etc! 359 | * Font lock and indentation are broken for things like `let \"try" = true`. 360 | * Formatting with `lsp-format-buffer` is broken because it does not correctly 361 | handle the response from rescript-vscode because it uses a range like 362 | `"end":{"line":1.7976931348623157e+308,"character":1.7976931348623157e+308}` 363 | -- this should be easy to fix and [you can use other means](#formatting) to do 364 | this. 365 | 366 | Packaging issues: 367 | 368 | * Teach lsp-mode how to install rescript-vscode, or bundle it 369 | * Add spacemacs layer 370 | 371 | ## Development 372 | 373 | To run the tests, install [Cask](https://github.com/cask/cask) and run this in 374 | the project root directory: 375 | 376 | cask exec ert-runner 377 | 378 | ## Support 379 | 380 | Please do not report issues related to editor support with Emacs upstream to the 381 | ReScript or rescript-vscode projects (neither on the forum nor the github 382 | issues). For now please use github issues or github discussions on this project 383 | as a place to discuss Emacs ReScript support. 384 | 385 | Emacs is NOT SUPPORTED by the ReScript core team, nor rescript-vscode. The core 386 | ReScript team’s focus for editor support is currently on supporting VS Code and 387 | Sublime Text well. So, if you want something that you can be confident is going 388 | to work smoothly and will not go away, use one of the editors listed as 389 | supported by the core ReScript team (currently VS Code and Sublime Text). In 390 | particular, the Emacs support here depends on the LSP server from 391 | rescript-vscode and its `--stdio` switch, neither of which are officially 392 | supported and could be removed in a later version. 393 | 394 | So if you have problems with Emacs and ReScript, please report your issues here, 395 | not upstream with ReScript or rescript-vscode and please don’t complain if you 396 | used ReScript with Emacs and had a bad time – if you did that, you’re going it 397 | alone and you really didn’t try the official ReScript experience – that’s unfair 398 | and a good way to annoy everybody. 399 | -------------------------------------------------------------------------------- /rescript-indent.el: -------------------------------------------------------------------------------- 1 | ;;; rescript-indent.el --- Indentation functions for ReScript -*-lexical-binding: t-*- 2 | 3 | ;; Copyright (C) 2008-2020 Free Software Foundation, Inc. 4 | ;; Copyright (C) 2021 John Lee 5 | 6 | ;; This file is NOT part of GNU Emacs. 7 | 8 | ;; This program is free software: you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | 22 | ;;; Commentary: 23 | 24 | ;; Based heavily (in fact entirely, right now) on js.el from GNU Emacs 25 | ;; There is very likely still a lot of code in here that makes little sense for 26 | ;; ReScript, and sometimes the indentation it supplies will be just plain wrong. 27 | ;; 28 | ;; The goal for now is just to get vaguely sensible most of the time when you 29 | ;; hit return or tab. To format your ReScript code properly, use `rescript format`. 30 | ;; 31 | ;; Exported names start with "rescript-"; private names start with 32 | ;; "rescript--". 33 | 34 | 35 | ;;; Code: 36 | 37 | ;;; Constants 38 | 39 | (require 'cc-mode) 40 | 41 | (defconst rescript--name-start-re "[[:alpha:]_$]" 42 | "Regexp matching the start of a ReScript identifier, without grouping.") 43 | 44 | (defconst rescript--name-re (concat rescript--name-start-re 45 | "\\(?:\\s_\\|\\sw\\)*") 46 | "Regexp matching a ReScript identifier, without grouping.") 47 | 48 | (defconst rescript--objfield-re (concat rescript--name-re ":") 49 | "Regexp matching the start of a ReScript object field.") 50 | 51 | (defconst rescript--dotted-name-re 52 | (concat rescript--name-re "\\(?:\\." rescript--name-re "\\)*") 53 | "Regexp matching a dot-separated sequence of ReScript names.") 54 | 55 | (defconst rescript--opt-cpp-start "^\\s-*#\\s-*\\([[:alnum:]]+\\)" 56 | "Regexp matching the prefix of a cpp directive. 57 | This includes the directive name, or nil in languages without 58 | preprocessor support. The first submatch surrounds the directive 59 | name.") 60 | 61 | (defun rescript--regexp-opt-symbol (list) 62 | "Like `regexp-opt', but surround the result with `\\\\_<' and `\\\\_>'." 63 | (concat "\\_<" (regexp-opt list t) "\\_>")) 64 | 65 | ;;; User Customization 66 | 67 | (defcustom rescript-indent-level 2 68 | "Number of spaces for each indentation step in `rescript-mode'." 69 | :type 'integer 70 | :safe 'integerp 71 | :group 'rescript) 72 | 73 | (defcustom rescript-expr-indent-offset 0 74 | "Number of additional spaces for indenting continued expressions. 75 | The value must be no less than minus `rescript-indent-level'." 76 | :type 'integer 77 | :safe 'integerp 78 | :group 'rescript) 79 | 80 | (defcustom rescript-paren-indent-offset 0 81 | "Number of additional spaces for indenting expressions in parentheses. 82 | The value must be no less than minus `rescript-indent-level'." 83 | :type 'integer 84 | :safe 'integerp 85 | :group 'rescript 86 | :version "24.1") 87 | 88 | (defcustom rescript-square-indent-offset 0 89 | "Number of additional spaces for indenting expressions in square braces. 90 | The value must be no less than minus `rescript-indent-level'." 91 | :type 'integer 92 | :safe 'integerp 93 | :group 'rescript 94 | :version "24.1") 95 | 96 | (defcustom rescript-curly-indent-offset 0 97 | "Number of additional spaces for indenting expressions in curly braces. 98 | The value must be no less than minus `rescript-indent-level'." 99 | :type 'integer 100 | :safe 'integerp 101 | :group 'rescript 102 | :version "24.1") 103 | 104 | (defcustom rescript-switch-indent-offset 0 105 | "Number of additional spaces for indenting the contents of a switch block. 106 | The value must not be negative." 107 | :type 'integer 108 | :safe 'integerp 109 | :group 'rescript 110 | :version "24.4") 111 | 112 | (defcustom rescript-flat-functions nil 113 | "Treat nested functions as top-level functions in `rescript-mode'. 114 | This applies to function movement, marking, and so on." 115 | :type 'boolean 116 | :group 'rescript) 117 | 118 | (defcustom rescript-indent-align-list-continuation t 119 | "Align continuation of non-empty ([{ lines in `rescript-mode'." 120 | :version "26.1" 121 | :type 'boolean 122 | :safe 'booleanp 123 | :group 'rescript) 124 | 125 | (defcustom rescript-comment-lineup-func #'c-lineup-C-comments 126 | "Lineup function for `cc-mode-style', for C comments in `rescript-mode'." 127 | :type 'function 128 | :group 'rescript) 129 | 130 | (defcustom rescript-chain-indent nil 131 | "Use \"chained\" indentation. 132 | Chained indentation applies when the current line starts with \".\". 133 | If the previous expression also contains a \".\" at the same level, 134 | then the \".\"s will be lined up: 135 | 136 | let x = svg.mumble() 137 | .chained; 138 | " 139 | :version "26.1" 140 | :type 'boolean 141 | :safe 'booleanp 142 | :group 'rescript) 143 | 144 | (defun rescript--re-search-forward-inner (regexp &optional bound count) 145 | "Helper function for `rescript--re-search-forward'." 146 | (let ((parse) 147 | str-terminator 148 | (orig-macro-end (save-excursion 149 | (when (rescript--beginning-of-macro) 150 | (c-end-of-macro) 151 | (point))))) 152 | (while (> count 0) 153 | (re-search-forward regexp bound) 154 | (setq parse (syntax-ppss)) 155 | (cond ((setq str-terminator (nth 3 parse)) 156 | (when (eq str-terminator t) 157 | (setq str-terminator ?/)) 158 | (re-search-forward 159 | (concat "\\([^\\]\\|^\\)" (string str-terminator)) 160 | (point-at-eol) t)) 161 | ((nth 7 parse) 162 | (forward-line)) 163 | ((or (nth 4 parse) 164 | (and (eq (char-before) ?\/) (eq (char-after) ?\*))) 165 | (re-search-forward "\\*/")) 166 | ((and (not (and orig-macro-end 167 | (<= (point) orig-macro-end))) 168 | (rescript--beginning-of-macro)) 169 | (c-end-of-macro)) 170 | (t 171 | (setq count (1- count)))))) 172 | (point)) 173 | 174 | 175 | (defun rescript--re-search-forward (regexp &optional bound noerror count) 176 | "Search forward, ignoring strings, cpp macros, and comments. 177 | This function invokes `re-search-forward', but treats the buffer 178 | as if strings, cpp macros, and comments have been removed. 179 | 180 | If invoked while inside a macro, it treats the contents of the 181 | macro as normal text." 182 | (unless count (setq count 1)) 183 | (let ((saved-point (point)) 184 | (search-fun 185 | (cond ((< count 0) (setq count (- count)) 186 | #'rescript--re-search-backward-inner) 187 | ((> count 0) #'rescript--re-search-forward-inner) 188 | (t #'ignore)))) 189 | (condition-case err 190 | (funcall search-fun regexp bound count) 191 | (search-failed 192 | (goto-char saved-point) 193 | (unless noerror 194 | (signal (car err) (cdr err))))))) 195 | 196 | 197 | (defun rescript--re-search-backward-inner (regexp &optional bound count) 198 | "Auxiliary function for `rescript--re-search-backward'." 199 | (let ((parse) 200 | (orig-macro-start 201 | (save-excursion 202 | (and (rescript--beginning-of-macro) 203 | (point))))) 204 | (while (> count 0) 205 | (re-search-backward regexp bound) 206 | (when (and (> (point) (point-min)) 207 | (save-excursion (backward-char) (looking-at "/[/*]"))) 208 | (forward-char)) 209 | (setq parse (syntax-ppss)) 210 | (cond ((nth 8 parse) 211 | (goto-char (nth 8 parse))) 212 | ((or (nth 4 parse) 213 | (and (eq (char-before) ?/) (eq (char-after) ?*))) 214 | (re-search-backward "/\\*")) 215 | ((and (not (and orig-macro-start 216 | (>= (point) orig-macro-start))) 217 | (rescript--beginning-of-macro))) 218 | (t 219 | (setq count (1- count)))))) 220 | (point)) 221 | 222 | 223 | (defun rescript--re-search-backward (regexp &optional bound noerror count) 224 | "Search backward, ignoring strings, preprocessor macros, and comments. 225 | 226 | This function invokes `re-search-backward' but treats the buffer 227 | as if strings, preprocessor macros, and comments have been 228 | removed. 229 | 230 | If invoked while inside a macro, treat the macro as normal text." 231 | (rescript--re-search-forward regexp bound noerror (if count (- count) -1))) 232 | 233 | (defun rescript--beginning-of-macro (&optional lim) 234 | (let ((here (point))) 235 | (save-restriction 236 | (if lim (narrow-to-region lim (point-max))) 237 | (beginning-of-line) 238 | (while (eq (char-before (1- (point))) ?\\) 239 | (forward-line -1)) 240 | (back-to-indentation) 241 | (if (and (<= (point) here) 242 | (looking-at rescript--opt-cpp-start)) 243 | t 244 | (goto-char here) 245 | nil)))) 246 | 247 | (defun rescript--backward-syntactic-ws (&optional lim) 248 | "Simple implementation of `c-backward-syntactic-ws' for `rescript-mode'." 249 | (save-restriction 250 | (when lim (narrow-to-region lim (point-max))) 251 | 252 | (let ((in-macro (save-excursion (rescript--beginning-of-macro))) 253 | (pos (point))) 254 | 255 | (while (progn (unless in-macro (rescript--beginning-of-macro)) 256 | (forward-comment most-negative-fixnum) 257 | (/= (point) 258 | (prog1 259 | pos 260 | (setq pos (point))))))))) 261 | 262 | (defun rescript--forward-syntactic-ws (&optional lim) 263 | "Simple implementation of `c-forward-syntactic-ws' for `rescript-mode'." 264 | (save-restriction 265 | (when lim (narrow-to-region (point-min) lim)) 266 | (let ((pos (point))) 267 | (while (progn 268 | (forward-comment most-positive-fixnum) 269 | (when (eq (char-after) ?#) 270 | (c-end-of-macro)) 271 | (/= (point) 272 | (prog1 273 | pos 274 | (setq pos (point))))))))) 275 | 276 | ;;; Indentation 277 | 278 | (defconst rescript--possibly-braceless-keyword-re 279 | (rescript--regexp-opt-symbol 280 | '("catch" "else" "finally" "for" "if" "try" "while")) 281 | "Regexp matching keywords optionally followed by an opening brace.") 282 | 283 | (defconst rescript--declaration-keyword-re 284 | (regexp-opt '("let") 'words) 285 | "Regular expression matching variable declaration keywords.") 286 | 287 | (defconst rescript--indent-operator-re 288 | (concat "[-+*/%<>&^|?:.]\\([^-+*/.]\\|$\\)\\|!?=\\|" 289 | (rescript--regexp-opt-symbol '("in" "to" "downto"))) 290 | "Regexp matching operators that affect indentation of continued expressions.") 291 | 292 | (defun rescript--looking-at-operator-p () 293 | "Return non-nil if point is on a ReScript operator, other than a comma." 294 | (save-match-data 295 | (and (looking-at rescript--indent-operator-re) 296 | (or (not (eq (char-after) ?:)) 297 | (save-excursion 298 | (rescript--backward-syntactic-ws) 299 | (when (= (char-before) ?\)) (backward-list)) 300 | (and (rescript--re-search-backward "[?:{]\\|\\_" nil t) 301 | (eq (char-after) ??)))) 302 | (not (and 303 | (eq (char-after) ?/) 304 | (save-excursion 305 | (eq (nth 3 (syntax-ppss)) ?/)))) 306 | (not (and 307 | (eq (char-after) ?*) 308 | ;; Generator method (possibly using computed property). 309 | (looking-at (concat "\\* *\\(?:\\[\\|" rescript--name-re " *(\\)")) 310 | (save-excursion 311 | (rescript--backward-syntactic-ws) 312 | ;; We might misindent some expressions that would 313 | ;; return NaN anyway. Shouldn't be a problem. 314 | (memq (char-before) '(?, ?} ?{)))))))) 315 | 316 | (defun rescript--find-newline-backward () 317 | "Move backward to the nearest newline that is not in a block comment." 318 | (let ((continue t) 319 | (result t)) 320 | (while continue 321 | (setq continue nil) 322 | (if (search-backward "\n" nil t) 323 | (let ((parse (syntax-ppss))) 324 | ;; We match the end of a // comment but not a newline in a 325 | ;; block comment. 326 | (when (nth 4 parse) 327 | (goto-char (nth 8 parse)) 328 | ;; If we saw a block comment, keep trying. 329 | (unless (nth 7 parse) 330 | (setq continue t)))) 331 | (setq result nil))) 332 | result)) 333 | 334 | (defun rescript--continued-expression-p () 335 | "Return non-nil if the current line continues an expression." 336 | (save-excursion 337 | (back-to-indentation) 338 | (if (rescript--looking-at-operator-p) 339 | (if (eq (char-after) ?/) 340 | (prog1 341 | (not (nth 3 (syntax-ppss (1+ (point))))) 342 | (forward-char -1)) 343 | (or 344 | (not (memq (char-after) '(?- ?+))) 345 | (progn 346 | (forward-comment (- (point))) 347 | (not (memq (char-before) '(?, ?\[ ?\()))))) 348 | (and (rescript--find-newline-backward) 349 | (progn 350 | (skip-chars-backward " \t") 351 | (or (bobp) (backward-char)) 352 | (and (> (point) (point-min)) 353 | (save-excursion 354 | (backward-char) 355 | (not (looking-at "[/*]/\\|=>"))) 356 | (rescript--looking-at-operator-p) 357 | (and (progn (backward-char) 358 | (not (looking-at "\\+\\+\\|--\\|/[/*]")))))))))) 359 | 360 | (defun rescript--skip-term-backward () 361 | "Skip a term before point; return t if a term was skipped." 362 | (let ((term-skipped nil)) 363 | ;; Skip backward over balanced parens. 364 | (let ((progress t)) 365 | (while progress 366 | (setq progress nil) 367 | ;; First skip whitespace. 368 | (skip-syntax-backward " ") 369 | ;; Now if we're looking at closing paren, skip to the opener. 370 | ;; This doesn't strictly follow JS syntax, in that we might 371 | ;; skip something nonsensical like "()[]{}", but it is enough 372 | ;; if it works ok for valid input. 373 | (when (memq (char-before) '(?\] ?\) ?\})) 374 | (setq progress t term-skipped t) 375 | (backward-list)))) 376 | ;; Maybe skip over a symbol. 377 | (let ((save-point (point))) 378 | (if (and (< (skip-syntax-backward "w_") 0) 379 | (looking-at rescript--name-re)) 380 | ;; Skipped. 381 | (progn 382 | (setq term-skipped t) 383 | (skip-syntax-backward " ")) 384 | ;; Did not skip, so restore point. 385 | (goto-char save-point))) 386 | (when (and term-skipped (> (point) (point-min))) 387 | (backward-char) 388 | (eq (char-after) ?.)))) 389 | 390 | (defun rescript--skip-terms-backward () 391 | "Skip any number of terms backward. 392 | Move point to the earliest \".\" without changing paren levels. 393 | Returns t if successful, nil if no term was found." 394 | (when (rescript--skip-term-backward) 395 | ;; Found at least one. 396 | (let ((last-point (point))) 397 | (while (rescript--skip-term-backward) 398 | (setq last-point (point))) 399 | (goto-char last-point) 400 | t))) 401 | 402 | (defun rescript--chained-expression-p () 403 | "A helper for rescript--proper-indentation that handles chained expressions. 404 | A chained expression is when the current line starts with '.' and the 405 | previous line also has a '.' expression. 406 | This function returns the indentation for the current line if it is 407 | a chained expression line; otherwise nil. 408 | This should only be called while point is at the start of the line's content, 409 | as determined by `back-to-indentation'." 410 | (when rescript-chain-indent 411 | (save-excursion 412 | (when (and (eq (char-after) ?.) 413 | (rescript--continued-expression-p) 414 | (rescript--find-newline-backward) 415 | (rescript--skip-terms-backward)) 416 | (current-column))))) 417 | 418 | (defun rescript--ctrl-statement-indentation () 419 | "Helper function for `rescript--proper-indentation'. 420 | Return the proper indentation of the current line if it starts 421 | the body of a control statement without braces; otherwise, return 422 | nil." 423 | (save-excursion 424 | (back-to-indentation) 425 | (when (save-excursion 426 | (and (not (eq (point-at-bol) (point-min))) 427 | (not (looking-at "[{]")) 428 | (rescript--re-search-backward "[[:graph:]]" nil t) 429 | (progn 430 | (or (eobp) (forward-char)) 431 | (when (= (char-before) ?\)) (backward-list)) 432 | (skip-syntax-backward " ") 433 | (skip-syntax-backward "w_") 434 | (looking-at rescript--possibly-braceless-keyword-re)) 435 | (memq (char-before) '(?\s ?\t ?\n ?\})))) 436 | (save-excursion 437 | (goto-char (match-beginning 0)) 438 | (+ (current-indentation) rescript-indent-level))))) 439 | 440 | (defun rescript--get-c-offset (symbol anchor) 441 | (let ((c-offsets-alist 442 | (list (cons 'c rescript-comment-lineup-func)))) 443 | (c-get-syntactic-indentation (list (cons symbol anchor))))) 444 | 445 | (defun rescript--same-line (pos) 446 | (and (>= pos (point-at-bol)) 447 | (<= pos (point-at-eol)))) 448 | 449 | (defun rescript--multi-line-declaration-indentation () 450 | "Helper function for `rescript--proper-indentation'. 451 | Return the proper indentation of the current line if it belongs to a declaration 452 | statement spanning multiple lines; otherwise, return nil." 453 | (let (forward-sexp-function ; Use Lisp version. 454 | at-opening-bracket) 455 | (save-excursion 456 | (back-to-indentation) 457 | (when (not (looking-at rescript--declaration-keyword-re)) 458 | (let ((pt (point))) 459 | (when (looking-at rescript--indent-operator-re) 460 | (goto-char (match-end 0))) 461 | ;; The "operator" is probably a regexp literal opener. 462 | (when (nth 3 (syntax-ppss)) 463 | (goto-char pt))) 464 | (while (and (not at-opening-bracket) 465 | (not (bobp)) 466 | (let ((pos (point))) 467 | (save-excursion 468 | (rescript--backward-syntactic-ws) 469 | (or (eq (char-before) ?,) 470 | (and (not (eq (char-before) ?\;)) 471 | (prog2 472 | (skip-syntax-backward ".") 473 | (looking-at rescript--indent-operator-re) 474 | (rescript--backward-syntactic-ws)) 475 | (not (eq (char-before) ?\;))) 476 | (rescript--same-line pos))))) 477 | (condition-case nil 478 | (backward-sexp) 479 | (scan-error (setq at-opening-bracket t)))) 480 | (when (looking-at rescript--declaration-keyword-re) 481 | (goto-char (match-end 0)) 482 | (1+ (current-column))))))) 483 | 484 | (defconst rescript--line-terminating-arrow-re "=>\\s-*\\(/[/*]\\|$\\)" 485 | "Regexp matching the last \"=>\" (arrow) token on a line. 486 | Whitespace and comments around the arrow are ignored.") 487 | 488 | (defun rescript--broken-arrow-terminates-line-p () 489 | "Helper function for `rescript--proper-indentation'. 490 | Return non-nil if the last non-comment, non-whitespace token of the 491 | current line is the \"=>\" token (of an arrow function)." 492 | (let ((from (point))) 493 | (end-of-line) 494 | (re-search-backward rescript--line-terminating-arrow-re from t))) 495 | 496 | (defun rescript--proper-indentation (parse-status) 497 | "Return the proper indentation for the current line." 498 | (save-excursion 499 | (back-to-indentation) 500 | (cond ((nth 4 parse-status) ; inside comment 501 | (rescript--get-c-offset 'c (nth 8 parse-status))) 502 | ((nth 3 parse-status) 0) ; inside string 503 | ((eq (char-after) ?#) 0) 504 | ((save-excursion (rescript--beginning-of-macro)) 4) 505 | ((rescript--chained-expression-p)) 506 | ((rescript--ctrl-statement-indentation)) 507 | ((rescript--multi-line-declaration-indentation)) 508 | ((nth 1 parse-status) 509 | ;; A single closing paren/bracket should be indented at the 510 | ;; same level as the opening statement. Same goes for 511 | ;; "case" and "default". 512 | (let ((same-indent-p (looking-at "[]})]")) 513 | (switch-keyword-p (looking-at "default\\_>\\|case\\_>[^:]")) 514 | (continued-expr-p (rescript--continued-expression-p))) 515 | (goto-char (nth 1 parse-status)) ; go to the opening char 516 | (if (or (not rescript-indent-align-list-continuation) 517 | (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)") 518 | (save-excursion (forward-char) (rescript--broken-arrow-terminates-line-p))) 519 | (progn ; nothing following the opening paren/bracket 520 | (skip-syntax-backward " ") 521 | (when (eq (char-before) ?\)) (backward-list)) 522 | (back-to-indentation) 523 | (let* ((in-switch-p (unless same-indent-p 524 | (looking-at "\\_"))) 525 | (same-indent-p (or same-indent-p 526 | (and switch-keyword-p 527 | in-switch-p))) 528 | (indent 529 | (+ 530 | (current-column) 531 | (cond (same-indent-p 0) 532 | (continued-expr-p 533 | (+ (* 2 rescript-indent-level) 534 | rescript-expr-indent-offset)) 535 | (t 536 | (+ rescript-indent-level 537 | (pcase (char-after (nth 1 parse-status)) 538 | (?\( rescript-paren-indent-offset) 539 | (?\[ rescript-square-indent-offset) 540 | (?\{ rescript-curly-indent-offset)))))))) 541 | (if in-switch-p 542 | (+ indent rescript-switch-indent-offset) 543 | indent))) 544 | ;; If there is something following the opening 545 | ;; paren/bracket, everything else should be indented at 546 | ;; the same level. 547 | (unless same-indent-p 548 | (forward-char) 549 | (skip-chars-forward " \t")) 550 | (current-column)))) 551 | 552 | ((rescript--continued-expression-p) 553 | (+ rescript-indent-level rescript-expr-indent-offset)) 554 | (t (prog-first-column))))) 555 | 556 | (defun rescript-indent-line () 557 | "Indent the current line as JavaScript." 558 | (interactive) 559 | (let* ((parse-status 560 | (save-excursion (syntax-ppss (point-at-bol)))) 561 | (offset (- (point) (save-excursion (back-to-indentation) (point))))) 562 | (unless (nth 3 parse-status) 563 | (indent-line-to (rescript--proper-indentation parse-status)) 564 | (when (> offset 0) (forward-char offset))))) 565 | 566 | (provide 'rescript-indent) 567 | 568 | ;;; rescript-indent.el ends here 569 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------