├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── antifennel.el ├── changelog.md ├── fennel-ls-flymake.el ├── fennel-mode.el ├── fennel-proto-repl.el ├── fennel-scratch.el ├── ob-fennel.el ├── syntax.fnl └── test ├── fennel-mode-test.el ├── fennel-proto-repl-test.el └── ob-fennel-test.el /.gitignore: -------------------------------------------------------------------------------- 1 | /*.elc 2 | /scratch.fnl 3 | /scratch.lua 4 | /.cask 5 | /coverage 6 | /test/*.elc 7 | /fennel-mode-autoloads.el 8 | /fennel-mode-pkg.el 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | EMACS ?= emacs 2 | TESTS ?= test/fennel-mode-test.el 3 | ALL_TESTS ?= $(wildcard test/*-test.el) 4 | 5 | test: clean 6 | $(EMACS) -Q -batch -L . -l $(TESTS) \ 7 | -f ert-run-tests-batch-and-exit 8 | 9 | testall: $(ALL_TESTS) 10 | @$(foreach test,$?,make test TESTS=$(test) || exit;) 11 | 12 | clean: ; rm -f *.elc 13 | 14 | .PHONY: test testall clean 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fennel Mode 2 | 3 | Font-lock, indentation, navigation, documentation, and REPL support for the 4 | [Fennel](https://fennel-lang.org) programming language. 5 | 6 | Supports `M-x imenu` for quick navigation to local definitions. 7 | 8 | ## Installation 9 | 10 | Add this to your config: 11 | 12 | ```lisp 13 | (autoload 'fennel-mode "/path/to/fennel-mode/fennel-mode" nil t) 14 | (add-to-list 'auto-mode-alist '("\\.fnl\\'" . fennel-mode)) 15 | ``` 16 | 17 | Note that `fennel-mode` doesn't inherit from `lisp-mode` since 0.4.0, and 18 | instead switched to `prog-mode`. This means that there is no longer any way to 19 | declare shared functionality (such as paredit) that you want to be applied to every 20 | lisp you use; you have to add hooks specifically to `fennel-mode-hook`. 21 | 22 | You'll also probably want to use [fennel-ls][8], which works great 23 | with [eglot][9]. In Emacs 30+, it will work automatically once 24 | `fennel-ls` is installed; in earlier versions you need to add this to 25 | your config: 26 | 27 | ```lisp 28 | (with-eval-after-load 'eglot 29 | (add-to-list 'eglot-server-programs '(fennel-mode . ("fennel-ls")))) 30 | ``` 31 | 32 | ## Interactivity 33 | 34 | Run `M-x fennel-repl RET` to open a REPL buffer. Once a REPL is open, 35 | you can send code from a `fennel-mode` buffer to be evaluated. 36 | 37 | ### Bindings for `fennel-mode` 38 | 39 | * `M-.` - Jump to the definition of a globally-visible function (xref) 40 | * `M-,` - Jump back to where you were before jumping to definition 41 | * `M-'` - Jump to the definition of a function in a module 42 | * `C-c C-k` - Reload the module for the current file (requires `fennel.seacher`) 43 | * `C-c C-l` - Display compilation output for the current file 44 | * `C-c C-z` - Start or switch to REPL buffer 45 | * `C-c C-t` - Reformat current buffer with [fnlfmt][1] (separate install) 46 | * `C-c C-d` - Ask for a value and show its docstring in the REPL (also `C-c C-f`) 47 | * `C-c C-v` - Show docstring of variable at point 48 | * `C-c C-p` - Print macro expansion of expression at point in the REPL 49 | * `C-x C-e` - Evaluate last expression before the point 50 | * `C-c C-e` - Evaluate current top-level form (also `C-M-x`) 51 | * `C-c C-n` - Evaluate current top-level form, then move to the next 52 | * `C-c C-r` - Evaluate the region 53 | * `C-c C-z` - Start or switch to REPL buffer 54 | 55 | ### Bindings for `fennel-repl-mode` 56 | 57 | * `TAB` - Completion at point 58 | * `C-c M-o` - Clear the REPL output 59 | * `C-c C-d` - Ask for a value and show its docstring in the REPL (also `C-c C-f`) 60 | * `C-c C-v` - Show docstring of variable at point 61 | * `M-.` - Jump to the definition of a globally-visible function 62 | * `C-c C-z` - Toggle back to previous `fennel-mode` buffer 63 | * `C-c C-q` - Quit the REPL 64 | 65 | These functions assume a `fennel` executable is present on your 66 | path. You can override the location by setting `inferior-lisp-program` 67 | or invoking `C-u M-x fennel-repl`. For instance, if you have [a stdio 68 | REPL][2] in a [LÖVE][3] game, you can set this to `love .`. 69 | 70 | Note that finding the definition of a function with `M-.` only works when the 71 | function is in scope for the REPL, which means it's usually best to 72 | load a module and set it as a global if you want to use it this way. 73 | 74 | ## Antifennel 75 | 76 | This repo also contains `antifennel.el` which allows you to compile 77 | Lua code to Fennel straight from a `lua-mode` buffer. It requires 78 | installing [antifennel][6] first. Install it with: 79 | 80 | ```lisp 81 | (autoload 'antifennel-mode "/path/to/fennel-mode/antifennel.el" nil t) 82 | (add-hook 'lua-mode-hook 'antifennel-mode) 83 | ``` 84 | 85 | Now when `antifennel-mode` is active in a Lua buffer, you can press 86 | `C-c C-f` to open a new buffer containing the Fennel equivalent of the 87 | Lua code. 88 | 89 | ## Protocol-based REPL 90 | 91 | A separate REPL integration is provided via the `fennel-proto-repl` 92 | module. This module provides a client that can connect to a regular 93 | Fennel REPL, *upgrade* it with the [protocol][7] code, and provides a 94 | more robust interactive experience. Advantages over the default 95 | `fennel-repl` are: 96 | 97 | * IO and evaluation results are separated. 98 | * Stack traces in Errors can be used to jump to the error location. 99 | * Evaluation results are shown in the echo area - no need to keep the 100 | REPL window open. 101 | * Running multiple REPLs is easier, and different buffers can be 102 | linked to different REPLs. 103 | * Synchronous and Asynchronous API. 104 | * Support for Eldoc and Xref. 105 | * Configurable dynamic font-locking of globals and macros 106 | * Project integration via `project.el` or `projectile`. 107 | 108 | Installation is similar to the other modules: 109 | 110 | ```lisp 111 | (autoload 'fennel-proto-repl "/path/to/fennel-mode/fennel-proto-repl.el" nil t) 112 | (add-hook 'fennel-mode-hook 'fennel-proto-repl-minor-mode) 113 | ``` 114 | 115 | The `fennel-proto-repl-minor-mode` re-binds all of the default REPL 116 | interaction keys available in `fennel-mode` to use `fennel-proto-repl` 117 | instead of a regular `fennel-repl`. In addition to the usual 118 | `fennel-mode` mappings listed above, additional commands are 119 | available: 120 | 121 | * `C-c C-S-l` - link the current buffer to a specific REPL session 122 | * `C-c C-b` - Evaluate the whole buffer 123 | * `C-c C-a` - Ask for a function and show its argument list in the REPL 124 | 125 | Starting the REPL or switching to the existing one is done via the 126 | same `C-c C-z` shortcut, and the new REPL session can be started at 127 | any moment with the `fennel-proto-repl` command. The buffer is 128 | automatically linked to the newly created REPL. 129 | 130 | Note that `fennel-proto-repl` requires a Fennel version recent enough 131 | to include the `___repl___` variable. If the Fennel doesn't have the 132 | `___repl___` variable the Proto REPL won't be started. 133 | 134 | ## Org Babel support 135 | 136 | Installation is similar to the other modules: 137 | 138 | ```lisp 139 | (autoload 'ob-fennel "/path/to/fennel-mode/ob-fennel.el" nil t) 140 | ``` 141 | 142 | Then, the support for Fennel in Org buffers can be activated by adding 143 | the following code to the Emacs init file: 144 | 145 | ```lisp 146 | (with-eval-after-load 'org 147 | (require 'ob-fennel)) 148 | ``` 149 | 150 | After that, the `#+begin_src fennel` code blocks can be executed in 151 | Org buffers. The integration requires the `fennel-proto-repl` module 152 | to be available and operational. 153 | 154 | The `ob-fennel` module supports evaluating code from the regular "src 155 | blocks", and "inline src blocks" (`src_fennel[]{...}`), with the 156 | ability to access variables and tables defined elsewhere in the file. 157 | Multiple sessions can be used, and a different command to start the 158 | REPL process can be passed via the `:fennel-cmd` header argument. 159 | 160 | ## Flymake support 161 | 162 | The Flymake backend depends on the [fennel-ls][8] for linting. 163 | 164 | Installation is similar to the other modules: 165 | 166 | ```lisp 167 | (autoload 'fennel-ls-flymake "/path/to/fennel-mode/fennel-ls-flymake.el" nil t) 168 | ``` 169 | 170 | Enabling the Flymake integration for Fennel buffers can be done via a hook: 171 | 172 | ```lisp 173 | (add-hook 'fennel-mode-hook 'fennel-ls-flymake) 174 | ``` 175 | 176 | ## Contributing 177 | 178 | Send patches to the [Fennel mailing list][4]. If you prefer not to 179 | use email you can send a pull request on the [Codeberg mirror][5]. 180 | Please byte-compile to check for warnings. 181 | 182 | ### Testing 183 | 184 | This package has tests stored in the `test/` directory. When 185 | developing new functionality consider writing a test for it. 186 | 187 | You can run the tests with `make`. By default this runs the tests for 188 | `fennel-mode` alone; for `fennel-proto-repl` and `ob-fennel` you can 189 | override the `TESTS` argument or run tests interactively via 190 | `ert-run-tests-interactively` after loading the respecting test file. 191 | 192 | Use `make EMACS=emacs29` to test against a different Emacs version. 193 | 194 | During development it may be more convenient to use `M-x 195 | ert-run-tests-interactively` but try to make sure the tests pass with 196 | a fresh Emacs using `make` before committing. 197 | 198 | ## Copyright 199 | 200 | Copyright © 2018-2025 Phil Hagelberg and contributors 201 | 202 | Licensed under the same license as Emacs (GPL v3 or later); see LICENSE 203 | 204 | [1]: https://git.sr.ht/~technomancy/fnlfmt 205 | [2]: https://gitlab.com/alexjgriffith/min-love2d-fennel/blob/master/lib/stdio.fnl 206 | [3]: https://love2d.org 207 | [4]: https://lists.sr.ht/%7Etechnomancy/fennel 208 | [5]: https://codeberg.org/technomancy/fennel-mode 209 | [6]: https://git.sr.ht/~technomancy/antifennel 210 | [7]: https://gitlab.com/andreyorst/fennel-proto-repl-protocol 211 | [8]: https://git.sr.ht/~xerool/fennel-ls 212 | [9]: https://www.gnu.org/software/emacs/manual/html_mono/eglot.html 213 | -------------------------------------------------------------------------------- /antifennel.el: -------------------------------------------------------------------------------- 1 | ;;; antifennel.el --- Turn Lua code into Fennel -*- lexical-binding: t -*- 2 | 3 | ;; Copyright © 2022 Phil Hagelberg and contributors 4 | 5 | ;; Author: Phil Hagelberg 6 | ;; URL: https://git.sr.ht/~technomancy/fennel-mode 7 | ;; Version: 0.0.1 8 | ;; Created: 2022-03-27 9 | ;; Keywords: languages, tools 10 | ;; Package-Requires: ((emacs "26.1")) 11 | 12 | ;;; Commentary: 13 | 14 | ;; Turn Lua code into Fennel code from Emacs. 15 | ;; Requires install of antifennel: https://git.sr.ht/~technomancy/antifennel 16 | 17 | ;; Recommended usage: 18 | 19 | ;; (add-hook 'lua-mode-hook 'antifennel-mode) 20 | 21 | ;; This will make it so that C-c C-f in a lua-mode buffer will display 22 | ;; the Fennel equivalent. You can also invoke it with M-x antifennel-buffer. 23 | 24 | ;;; License: 25 | 26 | ;; This program is free software; you can redistribute it and/or 27 | ;; modify it under the terms of the GNU General Public License 28 | ;; as published by the Free Software Foundation; either version 3 29 | ;; of the License, or (at your option) any later version. 30 | ;; 31 | ;; This program is distributed in the hope that it will be useful, 32 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 33 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 34 | ;; GNU General Public License for more details. 35 | ;; 36 | ;; You should have received a copy of the GNU General Public License 37 | ;; along with GNU Emacs; see the file COPYING. If not, write to the 38 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 39 | ;; Boston, MA 02110-1301, USA. 40 | 41 | ;;; Code: 42 | 43 | (require 'fennel-mode) 44 | 45 | (defvar antifennel-program "antifennel") 46 | 47 | (defvar antifennel-mode-map 48 | (let ((map (make-sparse-keymap))) 49 | (define-key map (kbd "C-c C-f") 'antifennel-buffer) 50 | map)) 51 | 52 | ;;;###autoload 53 | (define-minor-mode antifennel-mode 54 | "Quickly turn Lua code into Fennel." 55 | :keymap antifennel-mode-map) 56 | 57 | ;;;###autoload 58 | (defun antifennel-buffer () 59 | "Compile the contents of the current buffer's file from Lua to Fennel." 60 | (interactive) 61 | (antifennel-region (point-min) (point-max))) 62 | 63 | ;;;###autoload 64 | (defun antifennel-region (beg end) 65 | "Compile the region of the current buffer's file from Lua to Fennel. 66 | The region is specified via BEG and END." 67 | (interactive "r") 68 | (save-excursion 69 | (when (get-buffer "*antifennel*") 70 | (kill-buffer "*antifennel*")) 71 | (shell-command-on-region beg end "antifennel -" "*antifennel*")) 72 | (switch-to-buffer "*antifennel*") 73 | (read-only-mode) 74 | (fennel-mode) 75 | (goto-char (point-min))) 76 | 77 | (provide 'antifennel) 78 | ;;; antifennel.el ends here 79 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Summary of user-visible changes 2 | 3 | ## 0.9.3 / ??? 4 | 5 | * Support `.fnlm` files. 6 | * Remove old reloading mechanism in favor of the `,reload` REPL 7 | command 8 | * Obsolete `fennel-mode-annotate-completion` in favor of 9 | `fennel-proto-repl` 10 | * Highlight backquoted (``; `sym` ``) symbols in comments 11 | * Remove old find-definition mechanism in favor of the `,find` REPL 12 | command 13 | * Guess module name based on project structure in `fennel-proto-repl` 14 | 15 | ## 0.9.2 / 2025-03-13 16 | 17 | * Switch `M-.` find definition to using `xref`, allowing graceful 18 | fallback to eglot when the repl can't find the target. 19 | * Avoid binding `q` in all Lua buffers when viewving compilation. 20 | * Provide a customization variable to change what Lua major mode to 21 | use in a compilation view. 22 | * Better handling of ANSI colors in Fennel Proto REPL. 23 | * `ob-fennel` now can send Emacs Lisp hash tables to Fennel. 24 | * major update to the `fennel-proto-repl` - new protocol version, 25 | changes in user-input handling, ability to extend with custom 26 | operations. 27 | * Add Flymake support via `fennel-ls` 28 | * Add dynamic font-locking support to the `fennel-proto-repl` mode 29 | * Add project integration to the `fennel-proto-repl` mode 30 | * Remove `M-'` in favor of `C-u M-.` 31 | 32 | ## 0.9.1 / 2024-04-24 33 | 34 | * Add `fennel-proto-repl-read-handler` for custom `io.read` handling 35 | * Fix bug with discarding undo history in proto-repl buffers 36 | * Move Fennel Proto REPL settings to a subgroup 37 | * Provide `fennel-proto-repl-fennel-module-name` for customizing 38 | Fennel module location for `fennel-proto-repl` to work with apps 39 | that embed Fennel. 40 | * Update protocol to 0.5.0 41 | * Add support to the `,return` command in `fennel-proto-repl`. 42 | * Fix bug with `poly-org` overriding `compile-command` in 43 | `fennel-view-compilation`. 44 | 45 | ## 0.9.0 / 2023-08-14 46 | 47 | * Added support for Org Babel via proto-repl 48 | 49 | ## 0.8.1 / 2023-04-23 50 | 51 | * Log results and errors to the *Messages* buffer. 52 | * Indicate REPL state in the mode-line. 53 | * Add a binding to link the current buffer to the REPL. 54 | * Allow using proto-repl from fennel-scratch via custom variable. 55 | 56 | ## 0.8.0 / 2023-04-17 57 | 58 | * Remove `fennel-eldoc` module and Eldoc integration with 59 | `fennel-repl`. Eldoc is now a feature of the `fennel-proto-repl`. 60 | * Add `fennel-mode-repl-prompt` and `fennel-mode-repl-subprompt` 61 | variables that are now strings that are automatically escaped. 62 | * Obsolete `fennel-mode-repl-prompt-regexp` and 63 | `fennel-mode-repl-subprompt-regexp` variables. 64 | * Fix duplicate prompt bug in the `fennel-repl` REPL. 65 | 66 | ## 0.7.0 / 2023-04-03 67 | 68 | * New protocol-based REPL integration via `fennel-proto-repl` command 69 | 70 | ## 0.6.0 / 2023-02-15 71 | 72 | * Add fennel-mode to `interpreter-mode-alist` 73 | * Add `fennel-macroexpand` command and a shortcut 74 | * Add separate `antifennel.el` file 75 | * Add `fennel-mode-repl-subprompt-regexp` for REPL subprompt recognition 76 | * Add `fennel-mode-repl-prompt-readonly` custom 77 | * Add experimental `fennel-repl-minify-code` custom that defines how 78 | to collapse code before sending it to the REPL 79 | * Add new forms from Fennel 1.3.0 80 | * Support the `with-*` and `def*` naming convention for body macros 81 | * Add `fennel-format-region` command 82 | 83 | ## 0.5.0 / 2022-04-03 84 | 85 | * Add optional Eldoc support when REPL is connected 86 | * Add `fennel-mode-repl-prompt-regexp` to define a regular expression 87 | to recognize prompt 88 | 89 | ## 0.4.1 / 2021-11-22 90 | 91 | * Add `fennel-scratch` command, that opens scratch buffer for Fennel evaluation 92 | * Make REPL prompt readonly 93 | * Add missing highlighting for names defined with `macro` 94 | 95 | ## 0.4.0 / 2021-10-26 96 | 97 | * Add completion annotation and improve syntax highlighting consistency 98 | * Highlight local definitions 99 | * Highlight function definitions with `font-lock-function-name-face` 100 | * Fix REPL prompt duplication when completing symbols 101 | * Fix multiline input breaking the REPL interaction 102 | * Fix `fennel-find-definition` to use `symbol-at-point` as default value 103 | * Allow restarting and reusing fennel-repl buffer if present with C-c C-z 104 | * Implement more content aware table indentation 105 | * Inherit major mode from `prog-mode` instead of `lisp-mode` 106 | * Bump required Emacs version to 26.1 107 | 108 | ## 0.3.1 / 2021-07-04 109 | 110 | * Fix `M-.` to use the symbol at point by default 111 | * Improve arglist and docstring lookup 112 | * Fix several bugs around completion 113 | * Fix a bug where the `fennel-program` setting was ignored 114 | * Don't default to `--correlate` mode in repl 115 | 116 | ## 0.3.0 / 2021-06-18 117 | 118 | * Add support for `completion-at-point` 119 | * Add `fennel-format` command 120 | * Extend jump-to-definition to working on functions in modules 121 | * Add font-lock and indentation for new forms up to Fennel 0.9.3 122 | 123 | ## 0.2.0 / 2020-06-17 124 | 125 | * Add font-lock for new fennel forms up to 0.4.1 126 | * Add docstring lookup with `C-c C-d` 127 | * Fail gracefully during compilation errors in fennel-reload 128 | 129 | ## 0.1.0 / 2018-11-01 130 | 131 | * Fix imenu support 132 | * Add fennel-view-compilation function 133 | * Make fennel-reload safe for compiler errors 134 | * Fixed a bug where indentation wouldn't work unless lisp-mode was loaded 135 | * Add a setting to disable switching to repl on reload 136 | 137 | ## 0.0.2 / 2018-05-09 138 | 139 | * Add fennel-reload 140 | * Add imenu support 141 | * Add find-definition 142 | * Lots more keywords 143 | 144 | ## 0.0.1 / 2018-02-18 145 | 146 | * Initial commit 147 | -------------------------------------------------------------------------------- /fennel-ls-flymake.el: -------------------------------------------------------------------------------- 1 | ;;; fennel-ls-flymake.el --- Flymake backend for the Fennel language server -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright © 2023 Phil Hagelberg and contributors 4 | 5 | ;; Author: Andrey Listopadov 6 | ;; URL: https://git.sr.ht/~technomancy/fennel-mode 7 | ;; Version: 0.1.0 8 | ;; Created: 2025-03-05 9 | ;; Package-Requires: ((emacs "26.1")) 10 | ;; Keywords: languages, tools 11 | 12 | ;;; Commentary: 13 | ;; 14 | ;; This package uses fennel-ls --lint as a flymake backend. 15 | 16 | ;;; Code: 17 | 18 | (require 'flymake) 19 | 20 | (defconst fennel-ls-flymake--diagnositc-regexp 21 | "^.+:\\([[:digit:]]+\\):\\([[:digit:]]+\\):[[:space:]]*\\([[:space:][:alpha:]]+\\):[[:space:]]+\\(.*\\)$" 22 | "A regex for matching fennel-ls diagnostics.") 23 | 24 | (defvar-local fennel-ls-flymake--proc nil 25 | "Active fennel-ls process.") 26 | 27 | (defun fennel-ls-flymake-backend (report-fn &rest _args) 28 | "Build the Flymake backend for fennel-ls linter with REPORT-FN." 29 | (unless (executable-find "fennel-ls") 30 | (user-error "Executable fennel-ls not found on PATH")) 31 | 32 | (when (process-live-p fennel-ls-flymake--proc) 33 | (kill-process fennel-ls-flymake--proc)) 34 | 35 | (let ((default-directory 36 | (or (locate-dominating-file (buffer-file-name) "flsproject.fnl") 37 | (file-name-directory (buffer-file-name)))) 38 | (source (current-buffer))) 39 | (save-restriction 40 | (widen) 41 | (setq 42 | fennel-ls-flymake--proc 43 | (make-process 44 | :name "fennel-ls-flymake" :noquery t :connection-type 'pipe 45 | :buffer (generate-new-buffer " *fennel-ls-flymake*") 46 | :command '("fennel-ls" "--lint" "-") 47 | :sentinel 48 | (lambda (proc _event) 49 | (when (memq (process-status proc) '(exit signal)) 50 | (unwind-protect 51 | (if (with-current-buffer source (eq proc fennel-ls-flymake--proc)) 52 | (with-current-buffer (process-buffer proc) 53 | (goto-char (point-min)) 54 | (cl-loop while (search-forward-regexp fennel-ls-flymake--diagnositc-regexp nil t) 55 | for lnum = (string-to-number (match-string 1)) 56 | for lcol = (string-to-number (match-string 2)) 57 | for type = (let ((severity (match-string 3))) 58 | (pcase severity 59 | ("error" :error) 60 | ("warning" :warning) 61 | (_ :note))) 62 | for msg = (match-string 4) 63 | for (beg . end) = (flymake-diag-region source lnum lcol) 64 | collect (flymake-make-diagnostic source beg end type msg) 65 | into diags 66 | finally (funcall report-fn diags))) 67 | (flymake-log :warning "Canceling obsolete check %s" proc)) 68 | (kill-buffer (process-buffer proc))))))) 69 | (process-send-region fennel-ls-flymake--proc (point-min) (point-max)) 70 | (process-send-eof fennel-ls-flymake--proc)))) 71 | 72 | ;;;###autoload 73 | (defun fennel-ls-flymake () 74 | "Setup fennel-ls integration with Flymake." 75 | (interactive) 76 | (if (< emacs-major-version 26) 77 | (error "Fennel-ls-flymake requires Emacs 26 or later") 78 | (add-hook 'flymake-diagnostic-functions #'fennel-ls-flymake-backend nil t) 79 | (flymake-mode))) 80 | 81 | (provide 'fennel-ls-flymake) 82 | ;;; fennel-ls-flymake.el ends here 83 | -------------------------------------------------------------------------------- /fennel-mode.el: -------------------------------------------------------------------------------- 1 | ;;; fennel-mode.el --- A major-mode for editing Fennel code -*- lexical-binding: t -*- 2 | 3 | ;; Copyright © 2018-2025 Phil Hagelberg and contributors 4 | 5 | ;; Author: Phil Hagelberg 6 | ;; URL: https://git.sr.ht/~technomancy/fennel-mode 7 | ;; Version: 0.9.3 8 | ;; Created: 2018-02-18 9 | ;; Package-Requires: ((emacs "26.1")) 10 | ;; Keywords: languages, tools 11 | 12 | ;;; Commentary: 13 | 14 | ;; Provides font-lock, indentation, navigation, and REPL for Fennel code. 15 | 16 | ;;; License: 17 | 18 | ;; This program is free software; you can redistribute it and/or 19 | ;; modify it under the terms of the GNU General Public License 20 | ;; as published by the Free Software Foundation; either version 3 21 | ;; of the License, or (at your option) any later version. 22 | ;; 23 | ;; This program is distributed in the hope that it will be useful, 24 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 25 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 | ;; GNU General Public License for more details. 27 | ;; 28 | ;; You should have received a copy of the GNU General Public License 29 | ;; along with GNU Emacs; see the file COPYING. If not, write to the 30 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 31 | ;; Boston, MA 02110-1301, USA. 32 | 33 | ;;; Code: 34 | 35 | (require 'lisp-mode) 36 | (require 'prog-mode) 37 | (require 'inf-lisp) 38 | (require 'thingatpt) 39 | (require 'xref) 40 | 41 | (eval-when-compile 42 | (defvar paredit-space-for-delimiter-predicates)) 43 | 44 | (declare-function paredit-open-curly "ext:paredit") 45 | (declare-function paredit-close-curly "ext:paredit") 46 | (declare-function lua-mode "ext:lua-mode") 47 | (declare-function lua-ts-mode "ext:lua-ts-mode") 48 | 49 | (defcustom fennel-mode-switch-to-repl-after-reload t 50 | "If the focus should switch to the REPL after a module reload." 51 | :group 'fennel-mode 52 | :type 'boolean 53 | :package-version '(fennel-mode "0.1.0")) 54 | 55 | (defcustom fennel-program "fennel --repl" 56 | "Command to run the fennel REPL." 57 | :group 'fennel-mode 58 | :type 'string 59 | :package-version '(fennel-mode "0.3.0")) 60 | 61 | ;; TODO: remove all obsolete variables in the 1.0.0 release 62 | (define-obsolete-variable-alias 63 | 'fennel-mode-annotate-completion 64 | 'fennel-proto-repl-annotate-completion 65 | "fennel-mode 0.9.3" 66 | "Completion annotation is now a feature of `fennel-proto-repl' mode. 67 | This variable will be removed in the 1.0.0 release of `fennel-mode'.") 68 | 69 | (define-obsolete-variable-alias 70 | 'fennel-eldoc-fontify-markdown 71 | 'fennel-proto-repl-eldoc-fontify-markdown 72 | "fennel-mode 0.7.0" 73 | "Eldoc is now a feature of `fennel-proto-repl' mode. 74 | This variable will be removed in the 1.0.0 release of `fennel-mode'.") 75 | 76 | (define-obsolete-variable-alias 77 | 'fennel-mode-repl-prompt-regexp 78 | 'fennel-mode-repl-prompt 79 | "fennel-mode 0.8.0" 80 | "This variable will be removed in the 1.0.0 release of `fennel-mode'.") 81 | 82 | (define-obsolete-variable-alias 83 | 'fennel-mode-repl-subprompt-regexp 84 | 'fennel-mode-repl-subprompt 85 | "fennel-mode 0.8.0" 86 | "This variable will be removed in the 1.0.0 release of `fennel-mode'.") 87 | 88 | (define-obsolete-variable-alias 89 | 'fennel-mode-repl-log-communications 90 | 'fennel-proto-repl-log-communication 91 | "fennel-mode 0.8.0" 92 | "Logging is now a feature of `fennel-proto-repl' mode. 93 | This variable will be removed in the 1.0.0 release of `fennel-mode'.") 94 | 95 | (define-obsolete-variable-alias 96 | 'fennel-repl-minify-code 97 | nil 98 | "fennel-mode 0.7.0" 99 | "Minification is no longer preformed; variable value is ignored. 100 | If you exerience problems with sending expressions to the REPL, try the 101 | `fennel-proto-repl' mode. 102 | 103 | This variable will be removed in the 1.0.0 release of `fennel-mode'.") 104 | 105 | (defcustom fennel-mode-repl-prompt ">>" 106 | "String that matches REPL prompt. 107 | The string is automatically escaped and used as a regular 108 | expression to match both prompt and subpromt." 109 | :group 'fennel-mode 110 | :type 'string 111 | :set (lambda (sym val) 112 | (set-default-toplevel-value sym (string-trim-right val))) 113 | :package-version '(fennel-mode "0.8.0")) 114 | 115 | (defcustom fennel-mode-repl-subprompt ".." 116 | "String that matches REPL subprompt. 117 | The string is automatically escaped and used as a regular 118 | expression to match both prompt and subpromt." 119 | :group 'fennel-mode 120 | :type 'string 121 | :set (lambda (sym val) 122 | (set-default-toplevel-value sym (string-trim-right val))) 123 | :package-version '(fennel-mode "0.8.0")) 124 | 125 | (defcustom fennel-mode-repl-prompt-readonly t 126 | "Whether to use read-only prompt in the Fennel REPL buffer." 127 | :group 'fennel-mode 128 | :type 'boolean 129 | :package-version '(fennel-mode "0.6.0")) 130 | 131 | (defcustom fennel-view-compilation-mode 132 | (cond ((fboundp 'lua-mode) #'lua-mode) 133 | ((fboundp 'lua-ts-mode) #'lua-ts-mode) 134 | (t #'fundamental-mode)) 135 | "The major mode that should be used to view the compilation output." 136 | :group 'fennel-mode 137 | :type 'function 138 | :package-version '(fennel-mode "0.9.2")) 139 | 140 | (defun fennel-show-documentation (symbol) 141 | "Show SYMBOL documentation in the REPL." 142 | (interactive (lisp-symprompt "Documentation" (lisp-fn-called-at-pt))) 143 | (comint-proc-query 144 | (inferior-lisp-proc) 145 | (format ",doc %s\n" symbol))) 146 | 147 | (defun fennel-show-variable-documentation (symbol) 148 | "Show SYMBOL documentation in the REPL. 149 | 150 | Main difference from `fennel-show-documentation' is that rather 151 | than a function, a variable at point is picked automatically." 152 | (interactive (lisp-symprompt "Documentation" (lisp-var-at-pt))) 153 | (fennel-show-documentation symbol)) 154 | 155 | (defvar fennel-mode-syntax-table 156 | (let ((table (make-syntax-table))) 157 | ;; Initialize ASCII charset as symbol syntax 158 | (modify-syntax-entry '(0 . 127) "_" table) 159 | 160 | ;; Word syntax 161 | (modify-syntax-entry '(?0 . ?9) "w" table) 162 | (modify-syntax-entry '(?a . ?z) "w" table) 163 | (modify-syntax-entry '(?A . ?Z) "w" table) 164 | (modify-syntax-entry '(?! . ?&) "w" table) 165 | 166 | ;; Whitespace 167 | (modify-syntax-entry ?\s " " table) 168 | (modify-syntax-entry ?\xa0 " " table) ; non-breaking space 169 | (modify-syntax-entry ?\t " " table) 170 | (modify-syntax-entry ?\f " " table) 171 | 172 | ;; Delimiters 173 | (modify-syntax-entry ?\( "()" table) 174 | (modify-syntax-entry ?\) ")(" table) 175 | (modify-syntax-entry ?\[ "(]" table) 176 | (modify-syntax-entry ?\] ")[" table) 177 | (modify-syntax-entry ?\{ "(}" table) 178 | (modify-syntax-entry ?\} "){" table) 179 | 180 | ;; Prefix chars 181 | (modify-syntax-entry ?` "'" table) 182 | (modify-syntax-entry ?~ "'" table) 183 | (modify-syntax-entry ?^ "'" table) 184 | (modify-syntax-entry ?@ "'" table) 185 | (modify-syntax-entry ?? "_ p" table) 186 | 187 | ;; Others 188 | (modify-syntax-entry ?\; "<" table) ; comment start 189 | (modify-syntax-entry ?\n ">" table) ; comment end 190 | (modify-syntax-entry ?\" "\"" table) ; string 191 | (modify-syntax-entry ?\\ "\\" table) ; escape 192 | 193 | table) 194 | "Syntax table for Fennel mode.") 195 | 196 | (defun fennel--redefine-multisym-syntax (f &rest args) 197 | "Redefine multisym separators as punctuation during completion. 198 | Without this, completion of table fields doesn't work. But if we do 199 | this outside the context of completion, it results in broken 200 | indentation. Argument F is a completion function, such as 201 | `completion-at-point'. ARGS are optional arguments passed to the 202 | specified function F." 203 | (modify-syntax-entry ?. "." fennel-mode-syntax-table) 204 | (modify-syntax-entry ?: "." fennel-mode-syntax-table) 205 | (apply f args) 206 | (modify-syntax-entry ?. "_" fennel-mode-syntax-table) 207 | (modify-syntax-entry ?: "_" fennel-mode-syntax-table)) 208 | 209 | ;; TODO: this doesn't actually work; debug why 210 | ;; (advice-add 'completion-at-point :around #'fennel--redefine-multisym-syntax) 211 | 212 | (defvar-local fennel-module-name nil 213 | "Buffer-local value for storing the current file's module name.") 214 | 215 | ;; see syntax.fnl to generate these next two forms: 216 | (defvar fennel-keywords 217 | '("#" "%" "*" "+" "-" "->" "->>" "-?>" "-?>>" "." ".." "/" "//" ":" "<" 218 | "<=" "=" ">" ">=" "?." "^" "accumulate" "and" "band" "bnot" "bor" "bxor" 219 | "collect" "comment" "do" "doto" "each" "eval-compiler" "fcollect" "fn" 220 | "for" "global" "hashfn" "icollect" "if" "import-macros" "include" 221 | "lambda" "length" "let" "local" "lshift" "lua" "macro" "macrodebug" 222 | "macros" "match" "match-try" "not" "not=" "or" "partial" "pick-args" 223 | "pick-values" "quote" "require-macros" "rshift" "set" "set-forcibly!" 224 | "tset" "values" "var" "when" "while" "with-open" "~=" "λ" 225 | "faccumulate" "case" "case-try")) 226 | 227 | (defvar fennel-builtin-functions 228 | '("assert" "collectgarbage" "dofile" "error" "getmetatable" "ipairs" "load" 229 | "loadfile" "next" "pairs" "pcall" "print" "rawequal" "rawget" "rawlen" 230 | "rawset" "require" "select" "setmetatable" "tonumber" "tostring" "type" 231 | "warn" "xpcall")) 232 | 233 | (defvar fennel-local-fn-pattern 234 | (rx (syntax open-parenthesis) 235 | (or "fn" "lambda" "λ" "macro") (1+ space) 236 | (group (1+ (or (syntax word) (syntax symbol) "-" "_"))))) 237 | 238 | (defvar fennel-local-var-pattern 239 | (rx (syntax open-parenthesis) 240 | (or "global" "var" "local") (1+ space) 241 | (group (1+ (or (syntax word) (syntax symbol)))))) 242 | 243 | (defvar fennel-font-lock-keywords 244 | `((,fennel-local-fn-pattern 1 font-lock-function-name-face) 245 | (,fennel-local-var-pattern 1 font-lock-variable-name-face) 246 | (,(regexp-opt fennel-keywords 'symbols) . font-lock-keyword-face) 247 | (,(regexp-opt fennel-builtin-functions 'symbols) . font-lock-builtin-face) 248 | (,(rx bow "$" (optional digit) eow) . font-lock-keyword-face) 249 | (,(rx (group ":" (1+ word))) 0 font-lock-builtin-face) 250 | (,(rx (group letter (0+ word) "." (1+ word))) 0 font-lock-type-face) 251 | (,(rx bow "&" (optional "as") eow) . font-lock-keyword-face) 252 | (,(rx "`" (group-n 1 (optional "#'") 253 | (+ (or (syntax symbol) (syntax word)))) "`") 254 | (1 'font-lock-constant-face prepend)))) 255 | 256 | (defvar fennel-doc-string-elt-property 'doc-string-elt 257 | "The symbol property that holds the doc string position info.") 258 | 259 | (defun fennel-string-in-doc-position-p (listbeg startpos) 260 | "Return non-nil if a doc string may occur at STARTPOS inside a list. 261 | LISTBEG is the position of the start of the innermost list 262 | containing STARTPOS." 263 | (let* ((firstsym (and listbeg 264 | (save-excursion 265 | (goto-char listbeg) 266 | (and (looking-at "([ \t\n]*\\(\\(?:\\sw\\|\\s_\\|\\\\.\\)+\\)") 267 | (match-string 1))))) 268 | (docelt (and firstsym 269 | (function-get (intern-soft firstsym) 270 | lisp-doc-string-elt-property)))) 271 | (and docelt 272 | (save-excursion 273 | (when (functionp docelt) 274 | (goto-char (match-end 1)) 275 | (setq docelt (funcall docelt))) 276 | (goto-char listbeg) 277 | (forward-char 1) 278 | (condition-case nil 279 | (while (and (> docelt 0) (< (point) startpos) 280 | (progn (forward-sexp 1) t)) 281 | (setq docelt (1- docelt))) 282 | (error nil)) 283 | (and (zerop docelt) (<= (point) startpos) 284 | (progn (forward-comment (point-max)) t) 285 | (= (point) startpos)))))) 286 | 287 | (defun fennel-font-lock-syntactic-face-function (state) 288 | "Return syntactic face function for the position represented by STATE. 289 | STATE is a `parse-partial-sexp' state, and the returned function is the 290 | Lisp font lock syntactic face function." 291 | (if (nth 3 state) 292 | (let ((startpos (nth 8 state))) 293 | (let ((listbeg (nth 1 state))) 294 | (if (fennel-string-in-doc-position-p listbeg startpos) 295 | 'font-lock-doc-face 296 | 'font-lock-string-face))) 297 | 'font-lock-comment-face)) 298 | 299 | (defun fennel-font-lock-setup () 300 | "Setup font lock for keywords." 301 | (setq font-lock-defaults 302 | '(fennel-font-lock-keywords 303 | nil nil 304 | (("+-*/.<>=!?$%_&:" . "w")) 305 | nil 306 | (font-lock-mark-block-function . mark-defun) 307 | (font-lock-syntactic-face-function 308 | . fennel-font-lock-syntactic-face-function)))) 309 | 310 | (defvar calculate-lisp-indent-last-sexp) 311 | 312 | (defun fennel-indent-function (indent-point state) 313 | "Simplified version of function `lisp-indent-function'. 314 | 315 | INDENT-POINT is the position at which the line being indented begins. 316 | Point is located at the point to indent under (for default indentation); 317 | STATE is the `parse-partial-sexp' state for that position." 318 | (let ((normal-indent (current-column))) 319 | (goto-char (1+ (elt state 1))) 320 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) 321 | (let* ((fn (buffer-substring (point) (progn (forward-sexp 1) (point)))) 322 | (open-paren (elt state 1)) 323 | (method (get (intern-soft fn) 'fennel-indent-function))) 324 | (cond ((member (char-after open-paren) '(?\[ ?\{)) 325 | (goto-char open-paren) 326 | (skip-chars-forward (string (char-after open-paren))) 327 | (if (looking-at-p "[ \t\n]") 328 | (progn 329 | (skip-chars-forward "[ \t\n]") 330 | (current-column)) 331 | (goto-char open-paren) 332 | (+ 1 (current-column)))) 333 | ((eq method 'defun) 334 | (lisp-indent-defform state indent-point)) 335 | ((integerp method) 336 | (lisp-indent-specform method state indent-point normal-indent)) 337 | (method 338 | (funcall method indent-point state)) 339 | ((string-match (rx (or line-start ".") (or "with-" "def")) fn) 340 | (lisp-indent-defform state indent-point)))))) 341 | 342 | (defun fennel-space-for-delimiter-p (endp _delim) 343 | "Prevent paredit from inserting useless spaces. 344 | See `paredit-space-for-delimiter-predicates' for the meaning of 345 | ENDP and DELIM." 346 | (and (not endp) 347 | ;; don't insert after opening quotes, auto-gensym syntax 348 | (not (looking-back "\\_<#" (if (fboundp 'line-beginning-position) 349 | (line-beginning-position) 350 | (with-no-warnings 351 | (point-at-bol))))))) 352 | 353 | ;;;###autoload 354 | (define-derived-mode fennel-mode prog-mode "Fennel" 355 | "Major mode for editing Fennel code. 356 | 357 | \\{fennel-mode-map}" 358 | (setq-local comment-add 1) ; default to `;;' in comment-region 359 | (setq-local comment-column 40) 360 | (setq-local comment-end "") 361 | (setq-local comment-start ";") 362 | (setq-local comment-start-skip ";+ *") 363 | (setq-local comment-use-syntax t) 364 | (setq-local electric-pair-open-newline-between-pairs nil) 365 | (setq-local electric-pair-skip-whitespace 'chomp) 366 | (setq-local fill-paragraph-function #'lisp-fill-paragraph) 367 | (setq-local indent-line-function #'lisp-indent-line) 368 | (setq-local indent-tabs-mode nil) 369 | (setq-local lisp-doc-string-elt-property 'fennel-doc-string-elt) 370 | (setq-local lisp-indent-function #'fennel-indent-function) 371 | (setq-local multibyte-syntax-as-symbol t) 372 | (setq-local normal-auto-fill-function #'do-auto-fill) 373 | (setq-local open-paren-in-column-0-is-defun-start nil) 374 | (setq-local outline-level 'lisp-outline-level) 375 | (setq-local outline-regexp ";;;;* [^ \t\n]\\|(") 376 | (setq-local paragraph-ignore-fill-prefix t) 377 | (setq-local parse-sexp-ignore-comments t) 378 | (setq-local inferior-lisp-program fennel-program) 379 | (setq-local comint-prompt-regexp 380 | (format "^\\(?:%s\\|%s\\) " 381 | (regexp-quote fennel-mode-repl-prompt) 382 | (regexp-quote fennel-mode-repl-subprompt))) 383 | (setq-local comint-use-prompt-regexp t) 384 | ;; NOTE: won't work if the fennel module name has changed but beats nothing 385 | (setq-local inferior-lisp-load-command "((. (require :fennel) :dofile) %s)") 386 | (when (version<= "26.1" emacs-version) 387 | (setq-local adaptive-fill-function #'lisp-adaptive-fill)) 388 | 389 | (add-to-list 'imenu-generic-expression `(nil ,fennel-local-fn-pattern 1)) 390 | (make-local-variable 'completion-at-point-functions) 391 | (add-to-list 'completion-at-point-functions 'fennel-complete) 392 | (add-hook 'xref-backend-functions #'fennel--xref-backend nil t) 393 | (set-syntax-table fennel-mode-syntax-table) 394 | (fennel-font-lock-setup) 395 | (add-hook 'paredit-mode-hook #'fennel-paredit-setup nil t)) 396 | 397 | (put 'fn 'fennel-doc-string-elt 3) 398 | (put 'lambda 'fennel-doc-string-elt 3) 399 | (put 'λ 'fennel-doc-string-elt 3) 400 | (put 'macro 'fennel-doc-string-elt 3) 401 | 402 | (defun fennel--paredit-setup (mode-map) 403 | "Setup paredit keys for given MODE-MAP." 404 | (define-key mode-map "{" #'paredit-open-curly) 405 | (define-key mode-map "}" #'paredit-close-curly) 406 | (make-local-variable 'paredit-space-for-delimiter-predicates) 407 | (add-to-list 'paredit-space-for-delimiter-predicates #'fennel-space-for-delimiter-p)) 408 | 409 | (defun fennel-paredit-setup () 410 | "Setup paredit keys." 411 | (fennel--paredit-setup fennel-mode-map)) 412 | 413 | (defun fennel-get-module (ask? last-module) 414 | "Ask for the name of a module for the current file; return keyword. 415 | 416 | If ASK? or LAST-MODULE were not supplied, asks for the name of a module." 417 | (let ((module (if (or ask? (not last-module)) 418 | (read-string "Module: " (or last-module (file-name-base nil)) 'fennel-mode-module-names) 419 | last-module))) 420 | (setq fennel-module-name module) ; remember for the next reload 421 | module)) 422 | 423 | (defun fennel-reload (ask?) 424 | "Reload the module for the current file. 425 | 426 | ASK? forces module name prompt. 427 | 428 | Tries to reload in a way that makes it retroactively visible; if 429 | the module returns a table, then existing references to the same 430 | module will have their contents updated with the new 431 | value. Requires installing `fennel.searcher'. 432 | 433 | Queries the user for a module name upon first run for a given 434 | buffer, or when given a prefix arg." 435 | (interactive "P") 436 | (when buffer-file-name 437 | (comint-check-source buffer-file-name) 438 | (when (and (file-exists-p (concat (file-name-base nil) ".lua")) 439 | (yes-or-no-p "Lua file for module exists; delete it first?")) 440 | (delete-file (concat (file-name-base nil) ".lua")))) 441 | (let ((module (fennel-get-module ask? fennel-module-name))) 442 | (comint-send-string (inferior-lisp-proc) (format ",reload %s\n" module))) 443 | (when fennel-mode-switch-to-repl-after-reload 444 | (switch-to-lisp t))) 445 | 446 | (defun fennel-completions (input) 447 | "Query completions for the INPUT from the `inferior-lisp-proc'." 448 | (condition-case nil 449 | (when input 450 | (let* ((command (format ",complete %s" input)) 451 | (proc (inferior-lisp-proc)) 452 | (buf (fennel-repl-redirect-one proc command " *fennel-completion*"))) 453 | (when buf 454 | (with-current-buffer buf 455 | (let ((contents (buffer-substring-no-properties (point-min) (point-max)))) 456 | ;; strip ANSI escape codes added by readline 457 | (split-string (ansi-color-apply contents))))))) 458 | (error nil))) 459 | 460 | (defun fennel-complete () 461 | "Return a list of completion data for `completion-at-point'. 462 | 463 | Requires Fennel 0.9.3+." 464 | (interactive) 465 | (let ((bounds (bounds-of-thing-at-point 'symbol)) 466 | (completions (fennel-completions (symbol-at-point)))) 467 | (when completions 468 | (list (car bounds) 469 | (cdr bounds) 470 | completions)))) 471 | 472 | ;;; xref 473 | 474 | (defun fennel--xref-backend () 475 | "Xref backend for Fennel." 476 | 'fennel) 477 | 478 | (cl-defmethod xref-backend-identifier-at-point ((_backend (eql fennel))) 479 | "Return the relevant identifier at point." 480 | (thing-at-point 'symbol)) 481 | 482 | (defun fennel-mode--find-definition-for (identifier) 483 | "Find the definition of IDENTIFIER." 484 | (let* ((command (format ",find %s\n" identifier)) 485 | (proc (inferior-lisp-proc)) 486 | (buf (fennel-repl-redirect-one proc command " *fennel-find*"))) 487 | (when buf 488 | (with-current-buffer buf 489 | (buffer-substring-no-properties (point-min) (point-max)))))) 490 | 491 | (define-obsolete-function-alias 492 | 'fennel-find-definition-for 493 | 'fennel-mode--find-definition-for 494 | "fennel-mode 0.9.3" 495 | "This functionality was moved to `xref'.") 496 | 497 | (cl-defmethod xref-backend-definitions ((_backend (eql fennel)) identifier) 498 | "Find definitions of IDENTIFIER." 499 | (let ((location (fennel-mode--find-definition-for identifier))) 500 | (when (string-match "^@?\\([^:]+\\):\\([[:digit:]]+\\)" location) 501 | (let ((file (match-string 1 location)) 502 | (line (1+ (string-to-number (match-string 2 location))))) 503 | (message "found %s %s" file line) 504 | (list (xref-make nil (xref-make-file-location file line 0))))))) 505 | 506 | (defvar fennel-module-history nil) 507 | (defvar fennel-field-history nil) 508 | 509 | ;; TODO: remove/obsolete? 510 | (defun fennel-find-module-field (module fields-string) 511 | "Find FIELDS-STRING in the MODULE." 512 | (let ((tempfile (make-temp-file "fennel-module-")) 513 | (fields (mapcar (apply-partially 'concat ":") 514 | (split-string fields-string "\\.")))) 515 | (comint-send-string 516 | (inferior-lisp-proc) 517 | (format "%s\n" 518 | `(with-open [f (io.open ,(format "\"%s\"" tempfile) :w)] 519 | (match (-?> ("." (require ,(format "\"%s\"" module)) ,@fields) 520 | (debug.getinfo)) 521 | {:what :Lua 522 | : source : linedefined} (f:write source :! linedefined))))) 523 | (sit-for 0.1) 524 | (when (file-exists-p tempfile) 525 | (with-temp-buffer 526 | (insert-file-contents tempfile) 527 | (delete-file tempfile) 528 | (buffer-substring-no-properties (point-min) (point-max)))))) 529 | 530 | (define-minor-mode fennel-view-compilation-minor-mode 531 | "Minor mode to hook into `fennel-view-compilation'. 532 | 533 | \\{fennel-view-compilation-minor-mode-map}" 534 | :lighter "" 535 | :keymap (make-sparse-keymap) 536 | (if fennel-view-compilation-minor-mode 537 | (progn 538 | (funcall fennel-view-compilation-mode) 539 | (read-only-mode 1)) 540 | (fundamental-mode) 541 | (read-only-mode -1))) 542 | 543 | (define-key fennel-view-compilation-minor-mode-map (kbd "q") 'bury-buffer) 544 | 545 | (defun fennel-view-compilation () 546 | "Compile the current buffer contents and view the output. 547 | Arguments for the fennel executable are taken from the `fennel-program' 548 | variable." 549 | (interactive) 550 | (let* ((file (or (buffer-file-name) (make-temp-file "fennel-compile"))) 551 | (tmp? (not (buffer-file-name))) 552 | (fennel-program (replace-regexp-in-string "--repl" "" fennel-program)) 553 | (command (format "%s --compile %S" fennel-program file)) 554 | (buffer-name (format "*fennel %s*" (buffer-name)))) 555 | (when tmp? 556 | (let ((inhibit-message t)) 557 | (write-region (point-min) (point-max) file))) 558 | (with-current-buffer (switch-to-buffer buffer-name) 559 | (save-mark-and-excursion 560 | (fennel-view-compilation-minor-mode -1) 561 | (delete-region (point-min) (point-max)) 562 | (insert (shell-command-to-string command)) 563 | (fennel-view-compilation-minor-mode 1) 564 | (when tmp? 565 | (delete-file file)))))) 566 | 567 | (defun fennel-macroexpand () 568 | "Display macro expansion of expression at point in the REPL." 569 | (interactive) 570 | (when (not (inferior-lisp-proc)) 571 | (error "Need a running repl to expand macros")) 572 | (comint-proc-query 573 | (inferior-lisp-proc) 574 | (format "(macrodebug %s)\n" (thing-at-point 'sexp)))) 575 | 576 | (defalias 'fennel-macrodebug 'fennel-macroexpand) 577 | (defalias 'fennel-repl-clear-buffer 'comint-clear-buffer) 578 | (defalias 'fennel-eval-last-sexp 'lisp-eval-last-sexp) 579 | (defalias 'fennel-eval-toplevel-form 'lisp-eval-defun) 580 | (defalias 'fennel-eval-form-and-next 'lisp-eval-form-and-next) 581 | (defalias 'fennel-eval-region 'lisp-eval-region) 582 | 583 | (defun fennel-format-region (start end) 584 | "Run fnlfmt on the region from START to END." 585 | (interactive "r") 586 | (if (executable-find "fnlfmt") 587 | (shell-command-on-region start end "fnlfmt -" nil t) 588 | (message "fnlfmt not found"))) 589 | 590 | (defun fennel-format () 591 | "Run fnlfmt on the current buffer." 592 | (interactive) 593 | (fennel-format-region (point-min) (point-max))) 594 | 595 | (define-key fennel-mode-map (kbd "C-c C-k") 'fennel-reload) 596 | (define-key fennel-mode-map (kbd "C-c C-l") 'fennel-view-compilation) 597 | (define-key fennel-mode-map (kbd "C-c C-z") 'fennel-repl) 598 | (define-key fennel-mode-map (kbd "C-c C-t") 'fennel-format) 599 | (define-key fennel-mode-map (kbd "C-c C-f") 'fennel-show-documentation) 600 | (define-key fennel-mode-map (kbd "C-c C-d") 'fennel-show-documentation) 601 | (define-key fennel-mode-map (kbd "C-c C-v") 'fennel-show-variable-documentation) 602 | (define-key fennel-mode-map (kbd "C-c C-p") 'fennel-macroexpand) 603 | ;; lisp-mode function aliases 604 | (define-key fennel-mode-map (kbd "C-x C-e") 'fennel-eval-last-sexp) 605 | (define-key fennel-mode-map (kbd "C-c C-e") 'fennel-eval-toplevel-form) 606 | (define-key fennel-mode-map (kbd "C-M-x") 'fennel-eval-toplevel-form) 607 | (define-key fennel-mode-map (kbd "C-c C-n") 'fennel-eval-form-and-next) 608 | (define-key fennel-mode-map (kbd "C-c C-r") 'fennel-eval-region) 609 | 610 | (put 'lambda 'fennel-indent-function 'defun) 611 | (put 'λ 'fennel-indent-function 'defun) 612 | (put 'fn 'fennel-indent-function 'defun) 613 | (put 'while 'fennel-indent-function 'defun) 614 | (put 'do 'fennel-indent-function 0) 615 | (put 'let 'fennel-indent-function 1) 616 | (put 'when 'fennel-indent-function 1) 617 | (put 'for 'fennel-indent-function 1) 618 | (put 'each 'fennel-indent-function 1) 619 | (put 'eval-compiler 'fennel-indent-function 'defun) 620 | (put 'macro 'fennel-indent-function 'defun) 621 | (put 'doto 'fennel-indent-function 1) 622 | (put 'match 'fennel-indent-function 1) 623 | (put 'match-try 'fennel-indent-function 1) 624 | (put 'case 'fennel-indent-function 1) 625 | (put 'case-try 'fennel-indent-function 1) 626 | (put 'with-open 'fennel-indent-function 1) 627 | (put 'collect 'fennel-indent-function 1) 628 | (put 'icollect 'fennel-indent-function 1) 629 | (put 'fcollect 'fennel-indent-function 1) 630 | (put 'accumulate 'fennel-indent-function 1) 631 | (put 'faccumulate 'fennel-indent-function 1) 632 | 633 | ;;;###autoload 634 | (add-to-list 'auto-mode-alist '("\\.fnl\\'" . fennel-mode)) 635 | ;;;###autoload 636 | (add-to-list 'auto-mode-alist '("\\.fnlm\\'" . fennel-mode)) 637 | ;;;###autoload 638 | (add-to-list 'interpreter-mode-alist '("fennel" . fennel-mode)) 639 | 640 | ;;; REPL support 641 | 642 | (defvar fennel-repl--buffer-name "*Fennel REPL*") 643 | (defvar fennel-repl--buffer nil) 644 | (defvar fennel-repl--last-buffer nil) 645 | 646 | (defun fennel-repl--input-filter (body) 647 | "Input filter for the comint process. 648 | Checks if BODY is a balanced expression." 649 | (with-temp-buffer 650 | (set-syntax-table fennel-mode-syntax-table) 651 | (insert body) 652 | (condition-case nil 653 | (check-parens) 654 | (error (user-error "[Fennel REPL] Input not complete"))))) 655 | 656 | (defun fennel-repl--start (cmd &optional buffer) 657 | "Create the REPL buffer for CMD or select an existing one. 658 | If BUFFER is supplied, make it the last buffer." 659 | (let* ((cmdlist (if (fboundp 'split-string-shell-command) 660 | (split-string-shell-command cmd) 661 | (split-string cmd))) 662 | (repl-buffer (apply #'make-comint-in-buffer 663 | fennel-repl--buffer-name 664 | fennel-repl--buffer-name 665 | (car cmdlist) nil (cdr cmdlist)))) 666 | (setq fennel-repl--last-buffer (or buffer (current-buffer))) 667 | (setq fennel-repl--buffer repl-buffer) 668 | (setq inferior-lisp-buffer repl-buffer) 669 | (setq-local fennel-program cmd) 670 | (with-current-buffer repl-buffer 671 | (fennel-repl-mode) 672 | (setq-local fennel-program cmd)) 673 | (pop-to-buffer repl-buffer))) 674 | 675 | ;;;###autoload 676 | (defun fennel-repl (command &optional buffer) 677 | "Switch to the fennel REPL, or start a new one if needed. 678 | 679 | If there was a REPL buffer but its REPL process is dead, 680 | a new one is started in the same buffer. 681 | 682 | If invoked interactively with a prefix argument, asks for COMMAND 683 | to start the REPL. If optional BUFFER is supplied it is used as 684 | the last buffer before starting the REPL. 685 | 686 | The command is persisted as a buffer-local variable, the REPL 687 | buffer remembers the command that was used to start it. 688 | Resetting the command to another value can be done by invoking 689 | using a prefix argument. 690 | 691 | Return the REPL buffer." 692 | (interactive 693 | (list (if current-prefix-arg 694 | (read-string "Fennel command: " fennel-program) 695 | fennel-program))) 696 | (if (and (eq major-mode 'fennel-repl-mode) 697 | (not current-prefix-arg) 698 | (get-buffer-process (current-buffer))) 699 | (switch-to-buffer-other-window fennel-repl--last-buffer) 700 | (fennel-repl--start command buffer))) 701 | 702 | (defun fennel-repl-quit () 703 | "Kill the Fennel REPL buffer." 704 | (interactive) 705 | (if (not (eq 'fennel-repl-mode major-mode)) 706 | (user-error "Not a Fennel REPL buffer") 707 | (let ((kill-buffer-query-functions 708 | (delq 'process-kill-buffer-query-function 709 | kill-buffer-query-functions))) 710 | (kill-buffer)))) 711 | 712 | ;;;###autoload 713 | (defun fennel-repl-redirect-one (proc expr &optional outbuf) 714 | "Redirect the result of one EXPR to OUTBUF, return the redirection buffer. 715 | 716 | PROC must be an active Fennel REPL process. If OUTBUF is not 717 | provided a default fennel redirection buffer is created. 718 | 719 | Can block Emacs if redirected command takes too long to execute. 720 | Handles redirection cleanup in case of quit, and waits for the 721 | result." 722 | (when expr 723 | (with-current-buffer (process-buffer proc) 724 | (let ((buf (get-buffer-create (or outbuf " *fennel-redirect*")))) 725 | (with-current-buffer buf 726 | (erase-buffer)) 727 | (let ((inhibit-quit t) 728 | comint-preoutput-filter-functions) 729 | (with-local-quit 730 | (comint-redirect-send-command-to-process expr buf proc nil t) 731 | (while (and (null quit-flag) (null comint-redirect-completed)) 732 | (accept-process-output proc 0.1 nil t)) 733 | (when quit-flag 734 | (comint-redirect-cleanup)))) 735 | buf)))) 736 | 737 | (defun fennel-repl--get-old-input () 738 | "Return a string containing the sexp ending at point." 739 | (save-excursion 740 | (let ((end (point))) 741 | (backward-sexp) 742 | (replace-regexp-in-string 743 | (format "^\\(?:%s\\|%s\\) " 744 | (regexp-quote fennel-mode-repl-prompt) 745 | (regexp-quote fennel-mode-repl-subprompt)) 746 | "" 747 | (buffer-substring (point) end))))) 748 | 749 | ;;;###autoload 750 | (define-derived-mode fennel-repl-mode inferior-lisp-mode "Fennel REPL" 751 | "Major mode for Fennel REPL. 752 | 753 | \\{fennel-repl-mode-map}" 754 | :group 'fennel-mode 755 | (let ((prompt-re (format "^\\(?:%s\\|%s\\) " 756 | (regexp-quote fennel-mode-repl-prompt) 757 | (regexp-quote fennel-mode-repl-subprompt)))) 758 | (setq-local comint-prompt-regexp prompt-re) 759 | (setq-local inferior-lisp-prompt prompt-re)) 760 | (setq-local comint-prompt-read-only fennel-mode-repl-prompt-readonly) 761 | (add-hook 'comint-input-filter-functions 'fennel-repl--input-filter nil t) 762 | (setq-local lisp-indent-function 'fennel-indent-function) 763 | (setq-local lisp-doc-string-elt-property 'fennel-doc-string-elt) 764 | (setq-local comint-get-old-input #'fennel-repl--get-old-input) 765 | (setq-local comment-end "") 766 | (fennel-font-lock-setup) 767 | (set-syntax-table fennel-mode-syntax-table) 768 | (add-hook 'xref-backend-functions #'fennel--xref-backend nil t) 769 | (add-hook 'completion-at-point-functions 'fennel-complete nil t) 770 | (add-hook 'paredit-mode-hook #'fennel-repl-paredit-setup nil t)) 771 | 772 | (defun fennel-repl-paredit-setup () 773 | "Setup paredit keys in `fennel-repl-mode'." 774 | (fennel--paredit-setup fennel-repl-mode-map)) 775 | 776 | (define-key fennel-repl-mode-map (kbd "TAB") 'completion-at-point) 777 | (define-key fennel-repl-mode-map (kbd "C-c C-z") 'fennel-repl) 778 | (define-key fennel-repl-mode-map (kbd "C-c M-o") 'fennel-repl-clear-buffer) 779 | (define-key fennel-repl-mode-map (kbd "C-c C-f") 'fennel-show-documentation) 780 | (define-key fennel-repl-mode-map (kbd "C-c C-d") 'fennel-show-documentation) 781 | (define-key fennel-repl-mode-map (kbd "C-c C-v") 'fennel-show-variable-documentation) 782 | (define-key fennel-repl-mode-map (kbd "M-.") 'fennel-find-definition) 783 | (define-key fennel-repl-mode-map (kbd "C-c C-q") 'fennel-repl-quit) 784 | 785 | (provide 'fennel-mode) 786 | ;;; fennel-mode.el ends here 787 | -------------------------------------------------------------------------------- /fennel-scratch.el: -------------------------------------------------------------------------------- 1 | ;;; fennel-scratch.el --- A scratch buffer for Fennel -*- lexical-binding: t -*- 2 | 3 | ;; Copyright © 2018-2021 Phil Hagelberg and contributors 4 | ;; 5 | ;; Author: Andrey Listopadov 6 | ;; URL: https://git.sr.ht/~technomancy/fennel-mode 7 | ;; Version: 0.1.1 8 | ;; Created: 2021-11-22 9 | ;; Keywords: languages, tools 10 | ;; Package-Requires: ((emacs "26.1")) 11 | 12 | ;;; Commentary: 13 | 14 | ;; Provides a scratch buffer for Fennel evaluation, much like 15 | ;; `lisp-interaction-mode' does. 16 | 17 | ;;; License: 18 | 19 | ;; This program is free software; you can redistribute it and/or 20 | ;; modify it under the terms of the GNU General Public License 21 | ;; as published by the Free Software Foundation; either version 3 22 | ;; of the License, or (at your option) any later version. 23 | ;; 24 | ;; This program is distributed in the hope that it will be useful, 25 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 26 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27 | ;; GNU General Public License for more details. 28 | ;; 29 | ;; You should have received a copy of the GNU General Public License 30 | ;; along with GNU Emacs; see the file COPYING. If not, write to the 31 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 32 | ;; Boston, MA 02110-1301, USA. 33 | 34 | ;;; Code: 35 | 36 | (require 'fennel-mode) 37 | 38 | (declare-function fennel-proto-repl "ext:fennel-proto-repl") 39 | (declare-function fennel-proto-repl-send-message-sync "ext:fennel-proto-repl") 40 | 41 | (defcustom fennel-scratch-message ";; This buffer is for Fennel evaluation. 42 | ;; Use \\[fennel-scratch-eval-print-last-sexp] after the expression to evaluate it and insert the result. 43 | 44 | " 45 | "Initial documentation displayed in *fennel-scratch* buffer. 46 | If this is nil, no message will be displayed." 47 | :group 'fennel-mode 48 | :package-version '(fennel-mode "0.4.1") 49 | :type '(choice (text :tag "Message") 50 | (const :tag "none" nil))) 51 | 52 | (defcustom fennel-scratch-use-proto-repl nil 53 | "Whether to use the `fennel-proto-repl' as a backend for the `fennel-scratch'." 54 | :group 'fennel-mode 55 | :package-version '(fennel-mode "0.8.1") 56 | :type 'boolean) 57 | 58 | (defvar fennel-scratch-mode-map 59 | (let ((map (make-sparse-keymap))) 60 | (set-keymap-parent map fennel-mode-map) 61 | (define-key map "\C-j" 'fennel-scratch-eval-print-last-sexp) 62 | map) 63 | "Keymap for *fennel-scratch* buffer. 64 | All commands in `fennel-mode-map' are inherited by this map.") 65 | 66 | (defun fennel-scratch--get-buffer (cmd) 67 | "Return the scratch buffer, create it if necessary. 68 | CMD is used to start the REPL process." 69 | (or (get-buffer "*fennel-scratch*") 70 | (with-current-buffer (get-buffer-create "*fennel-scratch*") 71 | (prog1 (current-buffer) 72 | (fennel-mode) 73 | (use-local-map fennel-scratch-mode-map) 74 | (when fennel-scratch-message 75 | (insert (substitute-command-keys fennel-scratch-message))) 76 | (if fennel-scratch-use-proto-repl 77 | (fennel-proto-repl cmd) 78 | (fennel-repl--start cmd)))))) 79 | 80 | (defun fennel-scratch--eval-to-string (sexp) 81 | "Send SEXP to the Fennel process, return result as a string. 82 | May block indefinitely." 83 | (let ((sexp (string-trim (substring-no-properties sexp)))) 84 | (if fennel-scratch-use-proto-repl 85 | (let (string) 86 | (if-let* ((res (fennel-proto-repl-send-message-sync 87 | :eval sexp 88 | (lambda (_ message traceback) 89 | (setq string (string-join (list message (or traceback "")) "\n"))) 90 | nil 91 | most-positive-fixnum))) 92 | (string-join res "\t") 93 | (or string ""))) 94 | (let ((buf (get-buffer-create " *fennel-eval*")) 95 | (proc (inferior-lisp-proc))) 96 | (fennel-repl-redirect-one proc sexp buf) 97 | (with-current-buffer buf 98 | (string-trim (buffer-substring-no-properties (point-min) (point-max)))))))) 99 | 100 | (defun fennel-scratch-eval-print-last-sexp () 101 | "Evaluate s-expression before point and print value into current buffer." 102 | (interactive) 103 | (let* ((sexp (buffer-substring (save-excursion (backward-sexp) (point)) (point))) 104 | (result (fennel-scratch--eval-to-string sexp))) 105 | (newline) 106 | (unless (string-empty-p result) 107 | (insert (ansi-color-apply result)) 108 | (newline)))) 109 | 110 | ;;;###autoload 111 | (defun fennel-scratch (&optional ask-for-command?) 112 | "Create or open an existing scratch buffer for Fennel evaluation. 113 | With prefix argument ASK-FOR-COMMAND? asks for the command to 114 | start the REPL process." 115 | (interactive 116 | (list (if current-prefix-arg 117 | (read-string "Fennel command: " fennel-program) 118 | fennel-program))) 119 | (set-buffer (fennel-scratch--get-buffer ask-for-command?)) 120 | (unless (eq (current-buffer) (window-buffer)) 121 | (pop-to-buffer (current-buffer) t))) 122 | 123 | (provide 'fennel-scratch) 124 | ;;; fennel-scratch.el ends here 125 | -------------------------------------------------------------------------------- /ob-fennel.el: -------------------------------------------------------------------------------- 1 | ;;; ob-fennel.el --- Org Babel Functions for Fennel evaluation -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright © 2023 Phil Hagelberg and contributors 4 | 5 | ;; Author: Andrey Listopadov 6 | ;; URL: https://git.sr.ht/~technomancy/fennel-mode 7 | ;; Keywords: tools, literate programming, reproducible research 8 | ;; Prefix: ob-fennel 9 | ;; Version: 0.0.10 10 | ;; Package-Requires: ((emacs "26.1")) 11 | 12 | ;;; Commentary: 13 | 14 | ;; Support for evaluating fennel code 15 | 16 | ;; Requirements: 17 | ;; 18 | ;; - fennel (at least 1.4.0) 19 | ;; - fennel-mode 20 | 21 | ;;; Code: 22 | 23 | (require 'ob) 24 | (require 'ansi-color) 25 | (require 'fennel-proto-repl) 26 | 27 | (defvar org-babel-tangle-lang-exts) 28 | (add-to-list 'org-babel-tangle-lang-exts '("fennel" . "fnl")) 29 | 30 | (defvar org-babel-default-header-args:fennel '()) 31 | 32 | (defvar ob-fennel--hline-to "(setmetatable [] {:__fennelview #:hline})" 33 | "Replace hlines in incoming tables with this when translating to Fennel.") 34 | 35 | (defvar ob-fennel--buffer-fmt "*OB Fennel REPL:%s*" 36 | "Template for naming session buffers.") 37 | 38 | (defun ob-fennel--check-fennel-proc (buffer) 39 | "Check if BUFFER has a running `inferior-lisp-proc'." 40 | (car (memq (get-buffer buffer) (fennel-proto-repl-live-repls)))) 41 | 42 | (defun ob-fennel--initialize-repl (name params) 43 | "Create a Fennel REPL buffer with given NAME according to PARAMS." 44 | (let* ((cmd (or (cdr (assq :fennel-cmd params)) 45 | fennel-program)) 46 | (buffer (get-buffer-create name))) 47 | (save-window-excursion 48 | (with-current-buffer buffer 49 | (fennel-proto-repl cmd buffer) 50 | (rename-buffer name))) 51 | buffer)) 52 | 53 | (defun ob-fennel--get-create-repl-buffer (session params) 54 | "Get or create Fennel REPL buffer for SESSION according to PARAMS. 55 | 56 | Raises a `user-error' in case there was no REPL buffer." 57 | (cond ((and (or (string= "none" session) 58 | (null session)) 59 | (ob-fennel--check-fennel-proc 60 | (format ob-fennel--buffer-fmt "default"))) 61 | (get-buffer (format ob-fennel--buffer-fmt "default"))) 62 | ((or (string= "none" session) 63 | (null session)) 64 | (ob-fennel--initialize-repl 65 | (format ob-fennel--buffer-fmt "default") params)) 66 | ((ob-fennel--check-fennel-proc (format ob-fennel--buffer-fmt session)) 67 | (get-buffer (format ob-fennel--buffer-fmt session))) 68 | (t (ob-fennel--initialize-repl 69 | (format ob-fennel--buffer-fmt session) 70 | params)))) 71 | 72 | (defun ob-fennel--eval-to-string (body params) 73 | "Evaluate BODY according to PARAMS." 74 | (let* ((result-params (cdr (assq :result-params params))) 75 | (result-type (cdr (assq :result-type params))) 76 | (pretty? (or (member "code" result-params) 77 | (member "pp" result-params))) 78 | string done) 79 | (pcase result-type 80 | (`output 81 | (fennel-proto-repl-send-message 82 | :eval body 83 | (lambda (_values) 84 | (setq done t)) 85 | (lambda (_type message traceback) 86 | (setq done t) 87 | (setq string (string-join (list message (or traceback "")) "\n"))) 88 | (lambda (data) 89 | (setq string (concat (or string "") data))))) 90 | (`value 91 | (fennel-proto-repl-send-message 92 | :eval body 93 | (lambda (values) 94 | (setq done t) 95 | (setq string 96 | (if pretty? (string-join values "\n") 97 | (fennel-proto-repl--minify-body (string-join values "\t") t)))) 98 | (lambda (_type message traceback) 99 | (setq done t) 100 | (setq string (string-join (list message (or traceback "")) "\n")))))) 101 | (while (not done) 102 | (accept-process-output nil 0.01)) 103 | (ansi-color-apply (or string "")))) 104 | 105 | (defun ob-fennel--send-to-repl (repl-buffer body params) 106 | "Send BODY to the REPL-BUFFER and retrieve the result." 107 | (with-current-buffer repl-buffer 108 | (ob-fennel--eval-to-string body params))) 109 | 110 | (defun ob-fennel-var-to-fennel (var) 111 | "Convert an elisp value to a fennel variable. 112 | Convert an elisp value, VAR, into a string of fennel source code 113 | specifying a variable of the same value." 114 | (cond ((hash-table-p var) 115 | (let (results) 116 | (maphash 117 | (lambda (key value) 118 | (push (format "%s %s" 119 | (ob-fennel-var-to-fennel key) 120 | (ob-fennel-var-to-fennel value)) 121 | results)) 122 | var) 123 | (concat "{" (mapconcat #'identity results " ") "}"))) 124 | ((null var) "nil") 125 | ((listp var) 126 | (concat "[" (mapconcat #'ob-fennel-var-to-fennel var " ") "]")) 127 | ((eq var 'hline) 128 | ob-fennel--hline-to) 129 | ((keywordp var) 130 | (format "%S" (substring (symbol-name var) 1))) 131 | ((eq var t) 132 | "true") 133 | (t (format 134 | (if (stringp var) "%S" "%s") 135 | (if (stringp var) (substring-no-properties var) var))))) 136 | 137 | (defun ob-fennel-table-or-string (results) 138 | "Convert RESULTS into an appropriate elisp value. 139 | If the results look like a list or tuple, then convert them into an 140 | Emacs-lisp table, otherwise return the result as a string." 141 | (org-babel-script-escape results)) 142 | 143 | (defun org-babel-variable-assignments:fennel (params) 144 | "Return a list of Fennel let bindings assigning the block's PARAMS." 145 | (mapcar 146 | (lambda (pair) 147 | (format "%s %s" 148 | (car pair) 149 | (ob-fennel-var-to-fennel (cdr pair)))) 150 | (org-babel--get-vars params))) 151 | 152 | (defun org-babel-expand-body:fennel (body params) 153 | "Expand BODY according to PARAMS, return the expanded body." 154 | (let* ((vars (org-babel-variable-assignments:fennel params)) 155 | (body 156 | (with-temp-buffer 157 | (insert (if (null vars) body 158 | (format "(let [%s]\n %s)" (string-join vars "\n") body))) 159 | (buffer-substring-no-properties (point-min) (point-max))))) 160 | (string-trim-right body))) 161 | 162 | (defun org-babel-execute:fennel (body params) 163 | "Evaluate a block of Fennel code with Babel. 164 | 165 | Sends BODY to the `fennel-repl' process according to PARAMS. 166 | Supports `:session' parameter, and automatically creates a new 167 | REPL buffer, named after the session. Includes an additional 168 | parameter, exclusive to Fennel src blocks `:fennel-cmd' used to 169 | specify how to start the REPL process. 170 | 171 | For example: 172 | 173 | #+begin_src fennel :session foo :fennel-cmd \"fennel --lua luajit --repl\" 174 | \(+ 1 2 3) 175 | #+end_src 176 | 177 | automatically creates buffer \"foo\", starts Fennel REPL in it, 178 | by using \"fennel --lua luajit --repl\" as a command. Sessions 179 | are isolated, and repl-local variables with the same names can 180 | co-exist in different sessions, since they're different 181 | processes." 182 | (let* ((repl-buffer (ob-fennel--get-create-repl-buffer 183 | (cdr (assq :session params)) params)) 184 | (body (org-babel-expand-body:fennel body params)) 185 | (eval-result (ob-fennel--send-to-repl 186 | repl-buffer body params))) 187 | (org-babel-result-cond (cdr (assq :result-params params)) 188 | eval-result 189 | (ob-fennel-table-or-string (org-trim eval-result))))) 190 | 191 | (provide 'ob-fennel) 192 | ;;; ob-fennel.el ends here 193 | -------------------------------------------------------------------------------- /syntax.fnl: -------------------------------------------------------------------------------- 1 | ;; print fennel-keywords and fennel-built-in definitions for fennel-mode.el 2 | ;; requires fennel 0.9.3 or newer 3 | (local fennel (require :fennel)) 4 | 5 | (var column 5) 6 | 7 | (local keywords (doto (icollect [name {: global?} (pairs (fennel.syntax))] 8 | (if (not global?) name)) 9 | (table.sort))) 10 | 11 | (local builtin-modules (doto (icollect [name {: global? : function?} (pairs (fennel.syntax))] 12 | (if (and global? (not function?)) 13 | name)) 14 | (table.sort))) 15 | 16 | (local builtin-functions (doto (icollect [name {: global? : function?} (pairs (fennel.syntax))] 17 | (if (and global? function? (not (name:match "%."))) 18 | name)) 19 | (table.sort))) 20 | 21 | (local module-functions (doto (icollect [name {: global? : function?} (pairs (fennel.syntax))] 22 | (if (and global? function? (name:match "%.")) 23 | name)) 24 | (table.sort))) 25 | 26 | (fn write-name [name] 27 | (let [out (string.format "%q" name) 28 | next-column (+ column 1 (length out))] 29 | (if (= column 5) 30 | (do (set column (- next-column 1)) 31 | (io.write out)) 32 | (< next-column 79) 33 | (do (set column next-column) 34 | (io.write (.. " " out))) 35 | (do (set column (+ 5 (length out))) 36 | (io.write (.. "\n " out)))))) 37 | 38 | (io.write "(defvar fennel-keywords 39 | '(") 40 | 41 | (each [_ name (pairs keywords)] 42 | (write-name name)) 43 | 44 | (set column 5) 45 | 46 | (io.write "))\n 47 | (defvar fennel-builtin-functions 48 | '(") 49 | 50 | (each [_ name (pairs builtin-functions)] 51 | (write-name name)) 52 | 53 | (io.write "))\n") 54 | -------------------------------------------------------------------------------- /test/fennel-mode-test.el: -------------------------------------------------------------------------------- 1 | ;;; -*- lexical-binding: t; -*- 2 | (require 'ert) 3 | (require 'compat nil t) 4 | (require 'fennel-mode) 5 | (require 'outline) 6 | 7 | (defmacro with-fennel-repl (&rest body) 8 | "Starts the Fennel REPL and evals BODY in its context." 9 | (declare (indent 0) (debug t)) 10 | `(let ((inhibit-message t)) 11 | (with-current-buffer (fennel-repl "fennel --repl") 12 | (while (save-excursion 13 | (goto-char (point-min)) 14 | (not (re-search-forward 15 | (format "^%s" (regexp-quote fennel-mode-repl-prompt)) 16 | nil t))) 17 | (accept-process-output)) 18 | (unwind-protect 19 | (progn ,@body) 20 | (fennel-repl-quit))))) 21 | 22 | (ert-deftest fennel-mode-start-repl-test () 23 | "Starting the Proto REPL and checking its buffers and processes." 24 | (with-temp-buffer 25 | (let* ((current (current-buffer)) 26 | (inhibit-message t) 27 | (repl (fennel-repl "fennel --repl"))) 28 | (unwind-protect 29 | (progn 30 | (should-not (eq current (current-buffer))) 31 | (should (bufferp repl)) 32 | (should (eq repl (current-buffer))) 33 | (should (bufferp fennel-repl--last-buffer)) 34 | (should (eq repl fennel-repl--buffer)) 35 | (should (process-live-p (get-buffer-process repl))) 36 | (with-current-buffer repl 37 | (should (string= fennel-program "fennel --repl")))) 38 | (with-current-buffer repl 39 | (fennel-repl-quit)))))) 40 | 41 | (ert-deftest fennel-mode-kill-repl-error-test () 42 | (with-fennel-repl 43 | (with-temp-buffer 44 | (should-error (fennel-repl-quit))))) 45 | 46 | (ert-deftest fennel-mode-eval-test () 47 | (with-fennel-repl 48 | (let ((proc (inferior-lisp-proc))) 49 | (with-temp-buffer 50 | (fennel-repl-redirect-one proc "(+ 1 2 3)" (current-buffer)) 51 | (should (equal (string-trim (buffer-substring-no-properties (point-min) (point-max))) "6")))))) 52 | 53 | (ert-deftest fennel-mode-view-compilation-test () 54 | (let ((code "(+ 1 2 3)")) 55 | (with-temp-buffer 56 | (let ((name (buffer-name))) 57 | (insert code) 58 | (fennel-view-compilation) 59 | (with-current-buffer (format "*fennel %s*" name) 60 | (should (equal (string-trim (buffer-substring-no-properties (point-min) (point-max))) 61 | (string-trim (shell-command-to-string (format "fennel -c <(echo '%s')" code)))))))))) 62 | 63 | (defun fennel-mode--remove-leading-spaces (s) 64 | (replace-regexp-in-string "^[[:space:]]+" "" s)) 65 | 66 | (ert-deftest fennel-mode-indentation-test () 67 | (dolist (expected '(" 68 | (do 1 69 | 2 70 | 3)" " 71 | (do 72 | 1 73 | 2 74 | 3)" " 75 | (do 76 | 1 77 | 2 78 | 3 79 | )" " 80 | (+ 81 | 1 82 | 2 3 83 | )" " 84 | (doto 85 | {} 86 | (tset :a 1) 87 | (tset :b 2))" " 88 | (doto {} 89 | (tset :a 1) 90 | (tset :b 2))" " 91 | (let [a 1 92 | b 2] 93 | (+ 94 | a 95 | b))" " 96 | (let 97 | [a 1 98 | b 99 | 2] 100 | (+ a 101 | b))" " 102 | [1 103 | 2 104 | 3]" " 105 | [ 106 | 1 107 | 2 108 | 3 109 | ]" " 110 | {:a 111 | 1 112 | :b 113 | 2}" " 114 | { 115 | :a 1 116 | :b 2 117 | }" " 118 | {:a 119 | [1 120 | 2 121 | 3 122 | ] 123 | :bcd [ 124 | 1 125 | 2 126 | 3 127 | ]}" " 128 | (case {:a 1} 129 | {:a 1} 130 | 1 131 | (where {:a 2}) 132 | 2)" " 133 | (case 134 | {:a 1} 135 | {:a 1} 136 | 1 137 | (where {:a 2}) 138 | 2)" 139 | )) 140 | (with-temp-buffer 141 | (let ((inhibit-message t) 142 | (code (fennel-mode--remove-leading-spaces expected))) 143 | (insert code) 144 | (fennel-mode) 145 | (should (indent-region (point-min) (point-max))) 146 | (should (equal (string-trim expected) 147 | (string-trim (buffer-substring-no-properties (point-min) (point-max))))))))) 148 | 149 | (ert-deftest fennel-mode-outline-level-1-test () 150 | (with-temp-buffer 151 | (insert " 152 | ;;; Heading A 153 | 154 | (local x 1) 155 | 156 | (fn f [] 157 | (and 1 ; Any long body. 158 | 2 159 | 3 160 | 4 161 | 5)) 162 | 163 | ;;; Heading B 164 | 165 | (local y 1) 166 | 167 | (fn g [] 168 | (and 1 ; Any long body. 169 | 2 170 | 3 171 | 4 172 | 5))") 173 | (fennel-mode) 174 | (outline-cycle-buffer) 175 | (outline-headers-as-kill (point-min) (point-max)) 176 | (should (equal (current-kill 0) 177 | ";;; Heading A 178 | 179 | ;;; Heading B 180 | 181 | ")))) 182 | 183 | (ert-deftest fennel-mode-outline-level-2-test () 184 | (with-temp-buffer 185 | (insert " 186 | ;;; Heading A 187 | 188 | (local x 1) 189 | 190 | (fn f [] 191 | (and 1 ; Any long body. 192 | 2 193 | 3 194 | 4 195 | 5)) 196 | 197 | ;;; Heading B 198 | 199 | (local y 1) 200 | 201 | (fn g [] 202 | (and 1 ; Any long body. 203 | 2 204 | 3 205 | 4 206 | 5))") 207 | (fennel-mode) 208 | (outline-cycle-buffer) 209 | (outline-cycle-buffer) 210 | (outline-headers-as-kill (point-min) (point-max)) 211 | (should (equal (current-kill 0) 212 | ";;; Heading A 213 | 214 | (local x 1) 215 | 216 | (fn f [] 217 | 218 | ;;; Heading B 219 | 220 | (local y 1) 221 | 222 | (fn g [] 223 | 224 | ")))) 225 | -------------------------------------------------------------------------------- /test/fennel-proto-repl-test.el: -------------------------------------------------------------------------------- 1 | ;;; -*- lexical-binding: t; -*- 2 | (require 'ert) 3 | (require 'compat nil t) 4 | (require 'fennel-proto-repl) 5 | 6 | (setq fennel-proto-repl-loud nil) 7 | 8 | (defmacro with-fennel-proto-repl (&rest body) 9 | "Starts the Fennel Proto REPL and evals BODY in its context." 10 | (declare (indent 0) (debug t)) 11 | (let ((buf (gensym "buf"))) 12 | `(let ((inhibit-message t) 13 | (,buf (fennel-proto-repl "fennel --repl"))) 14 | (with-current-buffer ,buf 15 | (unwind-protect 16 | (progn ,@body) 17 | (fennel-proto-repl-quit) 18 | (kill-buffer ,buf)))))) 19 | 20 | (defmacro fpr--send-input (string) 21 | (declare (debug t)) 22 | `(progn 23 | (goto-char (point-max)) 24 | (fennel-proto-repl--input-sender nil ,string) 25 | (while (fennel-proto-repl-callbacks-pending) 26 | (accept-process-output nil 0.01)) 27 | (fennel-proto-repl--display-prompt))) 28 | 29 | (defmacro fpr--with-mocks (bindings &rest body) 30 | (declare (indent 1)) 31 | `(cl-letf ,(cl-loop for pair in bindings 32 | collect (list `(symbol-function ',(car pair)) (cadr pair))) 33 | ,@body)) 34 | 35 | (ert-deftest fpr-start-repl-test () 36 | "Starting the Proto REPL and checking its buffers and processes." 37 | (with-temp-buffer 38 | (let ((repl (fennel-proto-repl "fennel --repl"))) 39 | (unwind-protect 40 | (let ((inhibit-message t)) 41 | (should (bufferp repl)) 42 | (should (bufferp fennel-proto-repl--buffer)) 43 | (should (eq repl fennel-proto-repl--buffer)) 44 | (should (process-live-p (get-buffer-process repl))) 45 | (with-current-buffer repl 46 | (should (bufferp fennel-proto-repl--process-buffer)) 47 | (should (process-live-p (get-buffer-process fennel-proto-repl--process-buffer))) 48 | (fennel-proto-repl-quit) 49 | (should-not (buffer-live-p fennel-proto-repl--process-buffer)) 50 | (should-not (process-live-p (get-buffer-process repl))))) 51 | (with-current-buffer repl 52 | (kill-buffer)))))) 53 | 54 | (ert-deftest fpr-start-repl-in-custom-buffer-test () 55 | "Starting the Proto REPL and checking its buffers and processes." 56 | (with-temp-buffer 57 | (let* ((inhibit-message t) 58 | (buffer (get-buffer-create "repl")) 59 | (repl (fennel-proto-repl "fennel --repl" buffer))) 60 | (unwind-protect 61 | (should (eq repl buffer)) 62 | (with-current-buffer repl 63 | (fennel-proto-repl-quit) 64 | (kill-buffer)))))) 65 | 66 | (ert-deftest fpr-multiple-live-repls-test () 67 | (with-temp-buffer 68 | (let* ((inhibit-message t) 69 | (repl1 (fennel-proto-repl "fennel --repl")) 70 | (repl2 (fennel-proto-repl "fennel --repl")) 71 | (repl3 (fennel-proto-repl "fennel --repl"))) 72 | (should-not (or (eq repl1 repl2) (eq repl1 repl3) (eq repl2 repl3))) 73 | (with-current-buffer repl1 (rename-buffer "repl1")) 74 | (with-current-buffer repl2 (rename-buffer "repl2")) 75 | (with-current-buffer repl3 (rename-buffer "repl3")) 76 | (should (memq repl1 (fennel-proto-repl-live-repls))) 77 | (should (memq repl2 (fennel-proto-repl-live-repls))) 78 | (should (memq repl3 (fennel-proto-repl-live-repls))) 79 | (with-current-buffer repl2 (fennel-proto-repl-quit)) 80 | (should-not (memq repl2 (fennel-proto-repl-live-repls))) 81 | (kill-buffer repl2) 82 | (with-current-buffer repl1 (fennel-proto-repl-quit)) 83 | (should-not (memq repl1 (fennel-proto-repl-live-repls))) 84 | (kill-buffer repl1) 85 | (with-current-buffer repl3 (fennel-proto-repl-quit)) 86 | (should-not (memq repl3 (fennel-proto-repl-live-repls))) 87 | (kill-buffer repl3) 88 | (should-not (fennel-proto-repl-live-repls))))) 89 | 90 | (ert-deftest fpr-link-test () 91 | "Linking current buffer to a specific REPL" 92 | (with-temp-buffer 93 | (let* ((inhibit-message t) 94 | (repl1 (fennel-proto-repl "fennel --repl")) 95 | (repl2 (fennel-proto-repl "fennel --repl"))) 96 | (unwind-protect 97 | (with-temp-buffer 98 | (should-not (eq repl1 repl2)) 99 | (fennel-proto-repl-link-buffer repl1) 100 | (should (eq repl1 fennel-proto-repl--buffer)) 101 | (should (eq (with-current-buffer repl1 fennel-proto-repl--process-buffer) 102 | (fennel-proto-repl--process-buffer))) 103 | (fennel-proto-repl-link-buffer repl2) 104 | (should (eq repl2 fennel-proto-repl--buffer)) 105 | (should (eq (with-current-buffer repl2 fennel-proto-repl--process-buffer) 106 | (fennel-proto-repl--process-buffer)))) 107 | (with-current-buffer repl1 (fennel-proto-repl-quit) (kill-buffer)) 108 | (with-current-buffer repl2 (fennel-proto-repl-quit) (kill-buffer)))))) 109 | 110 | (ert-deftest fpr-interactive-link-test () 111 | (with-temp-buffer 112 | (let* ((inhibit-message t) 113 | (repl1 (fennel-proto-repl "fennel --repl")) 114 | (repl2 (fennel-proto-repl "fennel --repl"))) 115 | (unwind-protect 116 | (with-temp-buffer 117 | (with-current-buffer repl1 (rename-buffer "repl-a")) 118 | (with-current-buffer repl2 (rename-buffer "repl-b")) 119 | (fpr--with-mocks ((completing-read (lambda (&rest _) "repl-a"))) 120 | (fennel-proto-repl-link-buffer)) 121 | (should (eq repl1 fennel-proto-repl--buffer)) 122 | (should (eq (with-current-buffer repl1 fennel-proto-repl--process-buffer) 123 | (fennel-proto-repl--process-buffer))) 124 | (fpr--with-mocks ((completing-read (lambda (&rest _) "repl-b"))) 125 | (fennel-proto-repl-link-buffer)) 126 | (should (eq repl2 fennel-proto-repl--buffer)) 127 | (should (eq (with-current-buffer repl2 fennel-proto-repl--process-buffer) 128 | (fennel-proto-repl--process-buffer)))) 129 | (with-current-buffer repl1 (fennel-proto-repl-quit) (kill-buffer)) 130 | (with-current-buffer repl2 (fennel-proto-repl-quit) (kill-buffer)))))) 131 | 132 | (ert-deftest fpr-send-message-test () 133 | "Sending a successful asynchronous request." 134 | (with-temp-buffer 135 | (with-fennel-proto-repl 136 | (let (res done) 137 | (fennel-proto-repl-send-message 138 | :eval "(+ 1 2 3)" 139 | (lambda (values) (setq done t) (setq res values))) 140 | (while (not done) (accept-process-output nil 0.01)) 141 | (should (equal '("6") res)))))) 142 | 143 | (ert-deftest fpr-send-message-error-test () 144 | "Sending a errornous asynchronous request." 145 | (with-temp-buffer 146 | (with-fennel-proto-repl 147 | (let (res done) 148 | (fennel-proto-repl-send-message 149 | :eval "x" 150 | #'ignore 151 | (lambda (&rest _) (setq res "ok" done t)) 152 | #'ignore) 153 | (while (not done) (accept-process-output nil 0.01)) 154 | (should 155 | (equal "ok" res)))))) 156 | 157 | (ert-deftest fpr-send-message-io-test () 158 | "Sending a successful asynchronous request with IO." 159 | (with-temp-buffer 160 | (with-fennel-proto-repl 161 | (let (res done) 162 | (fennel-proto-repl-send-message 163 | :eval "(print 1234)" 164 | (lambda (_) (setq done t)) 165 | #'ignore 166 | (lambda (data) (setq res data))) 167 | (while (not done) (accept-process-output nil 0.01)) 168 | (should (equal "1234\n" res)))))) 169 | 170 | (ert-deftest fpr-send-message-sync-test () 171 | "Sending a successful synchronous request." 172 | (with-temp-buffer 173 | (with-fennel-proto-repl 174 | (should 175 | (equal '("6") (fennel-proto-repl-send-message-sync :eval "(+ 1 2 3)")))))) 176 | 177 | (ert-deftest fpr-send-message-sync-error-test () 178 | "Signaling an error from a synchronous request." 179 | (with-temp-buffer 180 | (with-fennel-proto-repl 181 | (let (res done) 182 | (fennel-proto-repl-send-message-sync 183 | :eval "x" 184 | (lambda (&rest _) (setq res "ok" done t))) 185 | (while (not done) (accept-process-output nil 0.01)) 186 | (should (equal "ok" res )))))) 187 | 188 | (ert-deftest fpr-send-message-sync-io-test () 189 | "Sending a successful asynchronous request." 190 | (with-temp-buffer 191 | (with-fennel-proto-repl 192 | (let (res) 193 | (fennel-proto-repl-send-message-sync 194 | :eval "(print 1234)" 195 | #'ignore 196 | (lambda (data) (setq res data))) 197 | (should (equal "1234\n" res)))))) 198 | 199 | (ert-deftest fpr-completion-test () 200 | (with-temp-buffer 201 | (with-fennel-proto-repl 202 | (let ((res (with-temp-buffer 203 | (insert "f") 204 | (fennel-proto-repl-complete)))) 205 | (should (pcase res 206 | ((seq 1 2 207 | _ 208 | :annotation-function fennel-proto-repl--completion-annotate 209 | :company-kind fennel-proto-repl--completion-candidate-kind 210 | :company-doc-buffer fennel-proto-repl--eldoc-get-doc-buffer) 211 | t))) 212 | (let ((completions (funcall (nth 2 res) "f" #'identity t))) 213 | (should (listp completions)) 214 | (should (equal '("faccumulate" "fcollect" "fn" "for") 215 | (sort completions #'string<)))) 216 | (should (equal " keyword" (funcall (nth 4 res) "fn"))) 217 | (should (equal 'keyword (funcall (nth 6 res) "fn"))) 218 | (should (equal 'field (funcall (nth 6 res) "a.b"))) 219 | (should (equal 'variable (funcall (nth 6 res) "a"))))))) 220 | 221 | (ert-deftest fpr-no-live-repl-test () 222 | (should-not (fennel-proto-repl-send-message-sync :eval "(+ 1 2)"))) 223 | 224 | (ert-deftest fpr-double-quit-test () 225 | (with-temp-buffer 226 | (let* ((inhibit-message t) 227 | (repl (fennel-proto-repl "fennel --repl"))) 228 | (fennel-proto-repl-quit) 229 | (should-error (fennel-proto-repl-quit))))) 230 | 231 | (ert-deftest fpr-restarting-in-repl-test () 232 | (with-temp-buffer 233 | (with-fennel-proto-repl 234 | (let ((repl (current-buffer))) 235 | (fennel-proto-repl-quit) 236 | (should-not (memq repl (fennel-proto-repl-live-repls))) 237 | (fennel-proto-repl--check-for-repl) 238 | (should (memq repl (fennel-proto-repl-live-repls))))))) 239 | 240 | (ert-deftest fpr-restarting-outside-repl-test () 241 | (with-temp-buffer 242 | (with-fennel-proto-repl 243 | (let ((repl (current-buffer))) 244 | (with-temp-buffer 245 | (fennel-proto-repl-link-buffer) 246 | (fennel-proto-repl-quit) 247 | (should-not (memq repl (fennel-proto-repl-live-repls))) 248 | (fennel-proto-repl--check-for-repl) 249 | (should (memq repl (fennel-proto-repl-live-repls)))))))) 250 | 251 | (ert-deftest fpr-switching-to-repl-test () 252 | (with-temp-buffer 253 | (with-fennel-proto-repl 254 | (let ((repl (current-buffer))) 255 | (with-temp-buffer 256 | (fennel-proto-repl-link-buffer repl) 257 | (fennel-proto-repl-switch-to-repl) 258 | (should (eq repl (current-buffer)))))))) 259 | 260 | (ert-deftest fpr-switching-from-repl-test () 261 | ;; TODO: For some reason fails if run interactively, but passes in the batch mode 262 | :expected-result (if noninteractive :passed :failed) 263 | (with-temp-buffer 264 | (let ((buf (get-buffer-create "test-buffer1"))) 265 | (unwind-protect 266 | (with-current-buffer buf 267 | (with-fennel-proto-repl 268 | (should (eq buf fennel-proto-repl--last-buffer)) 269 | (fennel-proto-repl-switch-to-repl) 270 | (should (eq buf (current-buffer))))) 271 | (kill-buffer buf))))) 272 | 273 | (ert-deftest fpr-switching-from-dead-repl-test () 274 | (with-temp-buffer 275 | (let ((buf (get-buffer-create "test-buffer2"))) 276 | (unwind-protect 277 | (with-current-buffer buf 278 | (with-fennel-proto-repl 279 | (let ((repl (current-buffer))) 280 | (fennel-proto-repl-quit) 281 | (fennel-proto-repl-switch-to-repl) 282 | (should (eq repl (current-buffer))) 283 | (should (memq repl (fennel-proto-repl-live-repls)))))) 284 | (kill-buffer buf))))) 285 | 286 | (ert-deftest fpr-disabling-fpr-interaction-test () 287 | (with-temp-buffer 288 | (let ((buf (get-buffer-create "test-buffer3"))) 289 | (unwind-protect 290 | (with-current-buffer buf 291 | (with-fennel-proto-repl nil) 292 | (should fennel-proto-repl-minor-mode) 293 | (should (memq 'fennel-proto-repl-complete completion-at-point-functions)) 294 | (should (memq 'fennel-proto-repl--xref-backend xref-backend-functions)) 295 | (when (boundp 'eldoc-documentation-functions) 296 | (should (memq 'fennel-proto-repl-eldoc-fn-docstring eldoc-documentation-functions)) 297 | (should (memq 'fennel-proto-repl-eldoc-var-docstring eldoc-documentation-functions))) 298 | (fennel-proto-repl-minor-mode -1) 299 | (should-not fennel-proto-repl-minor-mode) 300 | (should-not (memq 'fennel-proto-repl-complete completion-at-point-functions)) 301 | (should-not (memq 'fennel-proto-repl--xref-backend xref-backend-functions)) 302 | (when (boundp 'eldoc-documentation-functions) 303 | (should-not (memq 'fennel-proto-repl-eldoc-fn-docstring eldoc-documentation-functions)) 304 | (should-not (memq 'fennel-proto-repl-eldoc-var-docstring eldoc-documentation-functions)))) 305 | (kill-buffer buf))))) 306 | 307 | (ert-deftest fpr-output-test () 308 | (with-temp-buffer 309 | (with-fennel-proto-repl 310 | (fennel-proto-repl-clear-buffer) 311 | (should (equal ">> " (buffer-substring-no-properties (point-min) (point-max)))) 312 | (fennel-proto-repl-send-message-sync :eval "(io.write 123)") 313 | (should (equal "123\n>> " (buffer-substring-no-properties (point-min) (point-max)))) 314 | (fennel-proto-repl-send-message-sync :eval "(io.write 456)") 315 | (should (equal "123456\n>> " (buffer-substring-no-properties (point-min) (point-max)))) 316 | (fennel-proto-repl-send-message-sync :eval "(print 789)") 317 | (should (equal "123456789\n\n>> " (buffer-substring-no-properties (point-min) (point-max))))))) 318 | 319 | (ert-deftest fpr-error-capture-test () 320 | (with-temp-buffer 321 | (with-fennel-proto-repl 322 | (fennel-proto-repl-clear-buffer) 323 | (should (equal ">> " (buffer-substring-no-properties (point-min) (point-max)))) 324 | (fennel-proto-repl-send-message-sync :eval "(error \"%s\")") 325 | (should (equal "%s\n>> " (buffer-substring-no-properties (point-min) (point-max))))))) 326 | 327 | (ert-deftest fpr-clean-output-test () 328 | (with-temp-buffer 329 | (with-fennel-proto-repl 330 | (fennel-proto-repl-clear-output) 331 | (should (equal "*** output flushed ***\n>> " (buffer-substring-no-properties (point-min) (point-max)))) 332 | (fennel-proto-repl-send-message-sync :eval "(print 123)") 333 | (should (equal "*** output flushed ***\n123\n>> " (buffer-substring-no-properties (point-min) (point-max))))))) 334 | 335 | (ert-deftest fpr-eval-print-test () 336 | (with-temp-buffer 337 | (with-fennel-proto-repl 338 | (with-temp-buffer 339 | (fennel-proto-repl-link-buffer) 340 | (insert "[\"a\nb\" 1 2]") 341 | (goto-char (point-max)) 342 | (fennel-proto-repl-eval-print-last-sexp) 343 | (while (fennel-proto-repl-callbacks-pending) 344 | (accept-process-output nil 0.01)) 345 | (should (equal "[\"a\nb\" 1 2]\n[\"a\\nb\" 1 2]" 346 | (buffer-substring-no-properties (point-min) (point-max))))))) 347 | (with-temp-buffer 348 | (with-fennel-proto-repl 349 | (with-temp-buffer 350 | (fennel-proto-repl-link-buffer) 351 | (insert "[\"a\nb\" 1 2]") 352 | (goto-char (point-max)) 353 | (fennel-proto-repl-eval-print-last-sexp 'pp) 354 | (while (fennel-proto-repl-callbacks-pending) 355 | (accept-process-output nil 0.01)) 356 | (should (equal "[\"a\nb\" 1 2]\n[\"a\nb\"\n 1\n 2]" 357 | (buffer-substring-no-properties (point-min) (point-max)))))))) 358 | 359 | (ert-deftest fpr-eval-last-sexp-test () 360 | (with-temp-buffer 361 | (with-fennel-proto-repl 362 | (let ((repl (current-buffer))) 363 | (with-temp-buffer 364 | (fennel-proto-repl-link-buffer repl) 365 | (insert "(+ 1 2 3)") 366 | (goto-char (point-max)) 367 | (let (res done) 368 | (fpr--with-mocks ((fennel-proto-repl--display-result 369 | (lambda (values) 370 | (setq done t) 371 | (setq res values)))) 372 | (fennel-proto-repl-eval-last-sexp) 373 | (while (not done) (accept-process-output nil 0.01)) 374 | (should (equal '("6") res)))))))) 375 | (with-temp-buffer 376 | (with-fennel-proto-repl 377 | (let ((repl (current-buffer))) 378 | (with-temp-buffer 379 | (fennel-proto-repl-link-buffer) 380 | (insert "(+ 1 2 3)") 381 | (goto-char (point-max)) 382 | (let (res done) 383 | (fpr--with-mocks ((fennel-proto-repl--display-result 384 | (lambda (values) 385 | (setq done t) 386 | (setq res values)))) 387 | (fennel-proto-repl-eval-last-sexp 'and-go) 388 | (should (eq repl (current-buffer))) 389 | (while (not done) (accept-process-output nil 0.01)) 390 | (should (equal '("6") res))))))))) 391 | 392 | (ert-deftest fpr-eval-buffer-test () 393 | (with-temp-buffer 394 | (with-fennel-proto-repl 395 | (let ((repl (current-buffer))) 396 | (with-temp-buffer 397 | (fennel-proto-repl-link-buffer repl) 398 | (insert "(+ 1 2 3 4)") 399 | (goto-char (point-max)) 400 | (let (res done) 401 | (fpr--with-mocks ((fennel-proto-repl--display-result 402 | (lambda (values) 403 | (setq done t) 404 | (setq res values)))) 405 | (fennel-proto-repl-eval-buffer) 406 | (while (not done) (accept-process-output nil 0.01)) 407 | (should (equal '("10") res))))))))) 408 | 409 | (ert-deftest fpr-eval-defun-test () 410 | (with-temp-buffer 411 | (with-fennel-proto-repl 412 | (let ((repl (current-buffer))) 413 | (with-temp-buffer 414 | (fennel-proto-repl-link-buffer repl) 415 | (insert "(+ 1 2)") 416 | (goto-char (point-min)) 417 | (forward-char 2) 418 | (let (res done) 419 | (fpr--with-mocks ((fennel-proto-repl--display-result 420 | (lambda (values) 421 | (setq done t) 422 | (setq res values)))) 423 | (fennel-proto-repl-eval-defun) 424 | (while (not done) (accept-process-output nil 0.01)) 425 | (should (equal '("3") res))))))))) 426 | 427 | (ert-deftest fpr-eval-form-and-next-test () 428 | (with-temp-buffer 429 | (with-fennel-proto-repl 430 | (let ((repl (current-buffer))) 431 | (with-temp-buffer 432 | (fennel-proto-repl-link-buffer repl) 433 | (insert "(+ 1 2 3)\n(+ 4 5)") 434 | (goto-char (point-min)) 435 | (forward-char 2) 436 | (let (res done) 437 | (fpr--with-mocks ((fennel-proto-repl--display-result 438 | (lambda (values) 439 | (setq done t) 440 | (setq res values)))) 441 | (fennel-proto-repl-eval-form-and-next) 442 | (while (not done) (accept-process-output nil 0.01)) 443 | (should (equal '("6") res)) 444 | (should (equal 18 (point)))))))))) 445 | 446 | (ert-deftest fpr-eval-paragraph-test () 447 | (with-temp-buffer 448 | (with-fennel-proto-repl 449 | (let ((repl (current-buffer))) 450 | (with-temp-buffer 451 | (fennel-proto-repl-link-buffer repl) 452 | (insert "(+ 1 2 3)\n(+ 4 5)") 453 | (goto-char (point-min)) 454 | (forward-char 2) 455 | (let (res done) 456 | (fpr--with-mocks ((fennel-proto-repl--display-result 457 | (lambda (values) 458 | (setq done t) 459 | (setq res values)))) 460 | (fennel-proto-repl-eval-paragraph) 461 | (while (not done) (accept-process-output nil 0.01)) 462 | (should (equal '("9") res))))))))) 463 | 464 | (ert-deftest fpr-sync-request-timeout-test () 465 | (with-temp-buffer 466 | (with-fennel-proto-repl 467 | (should-error (fennel-proto-repl-send-message-sync :eval "(while true nil)") 468 | :type 'fennel-proto-repl-timeout)))) 469 | 470 | (ert-deftest fpr-user-input-test () 471 | (with-temp-buffer 472 | (with-fennel-proto-repl 473 | (fpr--with-mocks ((read-string (lambda (&rest _) "123"))) 474 | (should (equal '("123") (fennel-proto-repl-send-message-sync :eval "(io.read :n)"))))))) 475 | 476 | (ert-deftest fpr-delete-output-test () 477 | (with-temp-buffer 478 | (with-fennel-proto-repl 479 | (fennel-proto-repl-clear-buffer) 480 | (should (equal ">> " (buffer-substring-no-properties (point-min) (point-max)))) 481 | (fpr--send-input "(print 123)") 482 | (should (equal ">> 123\nnil\n>> " (buffer-substring-no-properties (point-min) (point-max)))) 483 | (fennel-proto-repl-clear-output) 484 | (should (equal "*** output flushed ***\n>> " (buffer-substring-no-properties (point-min) (point-max))))))) 485 | 486 | (ert-deftest fpr-comma-commands () 487 | (should (equal '("apropos" "apropos-doc" "apropos-show-docs" "compile" 488 | "complete" "doc" "exit" "find" "help" "reload" "reset" "return") 489 | (sort (fennel-proto-repl--available-comma-commands) #'string<))) 490 | (with-fennel-proto-repl 491 | (should-error 492 | (fpr--with-mocks ((completing-read (lambda (&rest _) ""))) 493 | (fennel-proto-repl-comma-command)))) 494 | (with-fennel-proto-repl 495 | (let (res) 496 | (fpr--with-mocks ((fennel-proto-repl--print (lambda (data) (setq res data))) 497 | (completing-read (lambda (&rest _) "complete")) 498 | (read-string (lambda (&rest _) "f"))) 499 | (fennel-proto-repl-comma-command) 500 | (while (fennel-proto-repl-callbacks-pending) 501 | (accept-process-output nil 0.01))) 502 | (should (stringp res)) 503 | (should (equal '("faccumulate" "fcollect" "fn" "for") 504 | (sort (split-string res "\n" t) #'string<))))) 505 | (with-fennel-proto-repl 506 | (let (res) 507 | (fpr--with-mocks ((fennel-proto-repl--print (lambda (data) (setq res data))) 508 | (completing-read (lambda (&rest _) "help"))) 509 | (fennel-proto-repl-comma-command) 510 | (while (fennel-proto-repl-callbacks-pending) 511 | (accept-process-output nil 0.01))) 512 | (should (stringp res))))) 513 | -------------------------------------------------------------------------------- /test/ob-fennel-test.el: -------------------------------------------------------------------------------- 1 | ;;; -*- lexical-binding: t -*- 2 | (require 'ert) 3 | (require 'compat nil t) 4 | (require 'ob-fennel) 5 | 6 | (defvar params 7 | '((:colname-names) 8 | (:rowname-names) 9 | (:result-params "replace") 10 | (:result-type . value) 11 | (:results . "replace") 12 | (:exports . "code") 13 | (:session . "none") 14 | (:cache . "no") 15 | (:noweb . "no") 16 | (:hlines . "no") 17 | (:tangle . "no"))) 18 | 19 | (defmacro with-session-teardown (name &rest body) 20 | "Teardown the Babel session NAME after executing BODY." 21 | (declare (indent 1)) 22 | `(let ((inhibit-message t)) 23 | (unwind-protect 24 | (progn ,@body) 25 | (with-current-buffer 26 | ,(format ob-fennel--buffer-fmt (or name "default")) 27 | (fennel-proto-repl-quit) 28 | (kill-buffer))))) 29 | 30 | (ert-deftest ob-fennel-evaluation-test () 31 | (with-session-teardown nil 32 | (should 33 | (equal 6 (org-babel-execute:fennel "(+ 1 2 3)" params))) 34 | (should 35 | (equal "foo" (org-babel-execute:fennel ":foo" params))) 36 | (should 37 | (equal '(1 2 3) (org-babel-execute:fennel "[1 2 3]" params))))) 38 | 39 | (ert-deftest ob-fennel-session-test () 40 | (with-session-teardown "a" 41 | (with-session-teardown "b" 42 | (should 43 | (equal "nil" (org-babel-execute:fennel 44 | "(local a 10)" 45 | (cons '(:session . "a") params)))) 46 | (should 47 | (equal "nil" (org-babel-execute:fennel 48 | "(local a 20)" 49 | (cons '(:session . "b") params)))) 50 | (should 51 | (equal 10 (org-babel-execute:fennel 52 | "a" 53 | (cons '(:session . "a") params)))) 54 | (should 55 | (equal 20 (org-babel-execute:fennel 56 | "a" 57 | (cons '(:session . "b") params))))))) 58 | 59 | (ert-deftest ob-fennel-variable-test () 60 | (with-session-teardown nil 61 | (should 62 | (equal 30 (org-babel-execute:fennel 63 | "(+ x y)" 64 | (append '((:var x . 10) (:var y . 20)) params)))))) 65 | 66 | (ert-deftest ob-fennel-table-test () 67 | (with-session-teardown nil 68 | (should 69 | (equal '(("low" "high" "average") 70 | hline 71 | (0 6 3) 72 | (3 7 5) 73 | (8 42 25)) 74 | (org-babel-execute:fennel 75 | "(let [[column-names 76 | separator 77 | & rows] samples] 78 | (table.insert column-names :average) 79 | (icollect [_ row (ipairs rows) 80 | :into [column-names separator]] 81 | (doto row 82 | (table.insert 83 | (/ (accumulate [res 0 _ val (ipairs row)] 84 | (+ res val)) 85 | (length row))))))" 86 | (append '((:var samples ("low" "high") hline (0 6) (3 7) (8 42)) 87 | (:result-params replace table) 88 | (:hlines . yes)) 89 | params)))))) 90 | 91 | (ert-deftest ob-fennel-output-test () 92 | (with-session-teardown nil 93 | (should 94 | (equal "vaiv" 95 | (org-babel-execute:fennel 96 | "(print :vaiv) 42" 97 | (append '((:result-type . output)) params)))))) 98 | 99 | (ert-deftest ob-fennel-output-with-error-test () 100 | (with-session-teardown nil 101 | (should 102 | (not (string-match-p 103 | "vaiv" 104 | (org-babel-execute:fennel 105 | "(print :vaiv) x" 106 | (append '((:result-type . output)) params))))))) 107 | 108 | (ert-deftest ob-fennel-pretty-printing-test () 109 | (with-session-teardown nil 110 | (should 111 | (equal '("a\\nb" 1 2) 112 | (org-babel-execute:fennel 113 | "[\"a\nb\" 1 2]" 114 | (append '((:result-paramss "replace" "scalar")) params)))) 115 | (should 116 | (equal "[\"a\nb\"\n 1\n 2]" 117 | (org-babel-execute:fennel 118 | "[\"a\nb\" 1 2]" 119 | (append '((:result-params "replace" "scalar" "pp")) params)))))) 120 | 121 | (ert-deftest ob-fennel-error-test () 122 | (with-session-teardown nil 123 | (should 124 | (string-match-p "error" (org-babel-execute:fennel "x" params))))) 125 | --------------------------------------------------------------------------------