├── License.txt ├── README.md ├── README.org ├── rdd.gif ├── repl-driven-development.el └── snippets ├── EmacsConf2023.org ├── FizzBuzz.py ├── ImageGallery.java ├── RepoGif.java ├── YoutubeShorts.java ├── growing-a-webserver.js └── testing.el /License.txt: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 |

Editor Integrated REPLs for all languages

4 | 5 | MELPA 6 | 7 | 8 | 9 | 10 | 11 | This library provides the Emacs built-in C─x C─e behaviour for arbitrary languages, provided they have a REPL shell command. 12 | 13 | | | 14 | |------------------------------------------------------------------------- | 15 | | **It provides a “send line to REPL process” command, for your language.** | 16 | 17 |
18 | 19 |
20 | 21 | 22 | 23 | ![img](rdd.gif) 24 | 25 |
26 | 27 | 28 | #   Ξ   29 | 30 | 1. [Motivation](#motivation) 31 | 2. [Mini-Tutorial](#mini-tutorial) 32 | 3. [Installation & Usage Instructions](#orgb9da7df) 33 | 4. [Videos](#videos) 34 | 1. [REPL Driven Development :: Teaching a JavaScript runtime, incrementally, to be a web server 🍽️ 🔁 🤖](#teaching-a-javascript-runtime-incrementally-to-be-a-web-server-) 35 | 5. [Bye!](#Bye) 36 | 37 | 38 | # Motivation 39 | 40 | Whenever reading/refactoring some code, if you can make some of it self-contained, then you can immediately try it out! No need to load your entire program; nor copy-paste into an external REPL. The benefits of Emacs' built-in C─x C─e for Lisp, and Lisp's Repl Driven Development philosophy, are essentially made possible for arbitrary languages (to some approximate degree, but not fully). 41 | 42 |
43 | 44 | 45 | 46 |
47 | 48 | Just as C-u C-x C-e inserts the resulting expression at the current cursour position, so too all `repl-driven-development` commands allow for a C─u prefix which inserts the result. This allows for a nice scripting experience where results are kept for future use. 49 | 50 | Finally, just as C─h e shows you the `*Messages*` buffer where you can see the evaluations of your Emacs Lisp via C─x C─e; likewise, C─h e shows you the output results of any REPL command created by `repl-driven-development`. 51 | 52 | 53 | # Mini-Tutorial 54 | 55 | Often, while reading a README file, we will (1) copy a shell command, (2) open a terminal, and (3) paste the shell command to run it. We can evaluate arbitrary regions in a shell in one step via C─x C─t with: 56 | 57 | ```emacs-lisp 58 | (repl-driven-development [C-x C-t] "bash") 59 | ``` 60 | 61 | For example, execute C─x C─t anywhere on each line below and see results in an overlay, right by your cursor. 62 | 63 | ```shell 64 | echo "It is $(date) and I am at $PWD, my name is $(whoami) and I have: $(ls)" 65 | 66 | say "My name is $(whoami) and I like Emacs" 67 | ``` 68 | 69 | Notice as each line is sent to the Bash process, the line is highlighted briefly in yellow. Moreover, you can hover over the text to see a tooltip with the resulting shell output. Finally, if you invoke C-h k C-x C-t you get help about this new C─x C─t command, such as inserting results at point via C-u C-x C-t or to reset/refresh the current Bash process with C-u -1 C-x C-t. 70 | 71 | This also works for any command-line REPL; for example, for Python: 72 | 73 | ```emacs-lisp 74 | (repl-driven-development [C-x C-p] "python3") 75 | ``` 76 | 77 | Then, we can submit the following Python snippets with C─x C─p on each line. 78 | 79 | ```python 80 | sum([1, 2, 3, 4]) 81 | 82 | list(map(lambda i: 'Fizz'*(not i%3)+'Buzz'*(not i%5) or i, range(1,101))) 83 | ``` 84 | 85 | These work fine, however there are some shortcomings of this REPL. For example, echoing results could be prettier and it doesn't handle multi-line input very well. You can address these issues using the various hooks / keyword arguments of the `repl-driven-development` macro. 86 | 87 | However, this package comes with preconfigured REPLS for: `python, terminal, java, javascript`. 88 | 89 | Simply use the name of these configurations: 90 | 91 | ```emacs-lisp 92 | (repl-driven-development [C-x C-p] python) 93 | ``` 94 | 95 | Now we can submit the following, with C─x C─p, with no issues: 96 | 97 | ```python 98 | def square(x): 99 | return x * x 100 | 101 | square(5) 102 | ``` 103 | 104 | Since these new REPL commands are just Emacs functions, we can use several at the time, alternating between them. For example: 105 | 106 | ```emacs-lisp 107 | ;; C-x C-e on the next two lines 108 | (repl-driven-development [C-x C-t] terminal) 109 | (repl-driven-development [C-x C-p] python) 110 | ``` 111 | 112 | ```shell 113 | echo Hello... > /tmp/o # C-x C-t here 114 | ``` 115 | 116 | ```python 117 | print(open("/tmp/o").read()) # C-x C-p here 118 | ``` 119 | 120 | ```shell 121 | echo ...and bye >> /tmp/o # C-x C-t again 122 | ``` 123 | 124 | ```python 125 | print(open("/tmp/o").read()) # C-x C-p again 126 | ``` 127 | 128 | Let's conclude with a GUI example in Java. 129 | 130 | ```emacs-lisp 131 | ;; Set “C-x C-j” to evaluate Java code in a background REPL. 132 | (repl-driven-development [C-x C-j] "jshell") 133 | ``` 134 | 135 | ```java 136 | // Select this Java snippet, then press “C-x C-j” to evaluate it 137 | import javax.swing.*; 138 | JOptionPane.showMessageDialog(new JFrame(){{setAlwaysOnTop(true);}}, "Super nice!") 139 | ``` 140 | 141 | We can use a preconfigured Java REPL, to remove the annoying “jshell>” prompt from overlay echos, handle multi-line input, and more. 142 | 143 | ```emacs-lisp 144 | (repl-driven-development [C-x C-j] java) 145 | ``` 146 | 147 | ```java 148 | // REPL result values are shown as overlays: 149 | // See a list of 23 numbers, which are attached as a tooltip to this text. 150 | IntStream.range(0, 23).forEach(x -> System.out.println(x)) 151 | ``` 152 | 153 | For more documentation, and examples, see 154 | 155 | 156 | # Installation & Usage Instructions 157 | 158 | This package is on Melpa, MELPA, so you can install it with [use-package](https://github.com/alhassy/emacs.d#use-package-the-start-of-initel): 159 | 160 | ```emacs-lisp 161 | (use-package repl-driven-development 162 | :ensure t 163 | :config 164 | (repl-driven-development [C-x C-j] java) ;; e“X”ecute “j”ava 165 | (repl-driven-development [C-x C-n] javascript) ;; e“X”ecute “n”odejs 166 | (repl-driven-development [C-x C-p] python) ;; e“X”ecute “p”ython 167 | (repl-driven-development [C-x C-t] terminal)) ;; e“X”ecute “t”erminal 168 | ``` 169 | 170 | The above mentions the four pre-configured REPLs that the package comes with: These are like their CLI equivalents, but offer more bells and whistles. 171 | 172 | - For example, the pre-configured `java` REPL is like `(repl-driven-development [C-x C-j] "jshell")` but it supports multi-line input: JShell eagerly inserts semicolons onto expressions, so, say, a multi-line Stream pipeline would be interpreted as multiple distinct statements by JShell, whereas our `java` configuration handles this by stripping the newlines (and any intermediary comments). 173 | 174 | You can use any process, for example let's use the `tclsh` command line process: 175 | 176 | ```emacs-lisp 177 | (repl-driven-development [s-t] "tclsh" ;; Make “⌘-t” e“X”ecute “T”cl code, 178 | :blink 'pulsar-blue ;; and highlight submitted lines blue 179 | :prompt "%") ;; and don't show me the tclsh prompt, which is “%”. 180 | ``` 181 | 182 | The `repl-driven-development` macro has been tried at least with the following processes. 183 | 184 | | | 185 | |----------------------------------------------------------------------- | 186 | | *Please make a Pull Request, or Issue, to increase the following list!* | 187 | 188 |
🔗
JavaScript ---and a minimal server 189 | 190 | | | 191 | |----------------------------------------------------------------------------------------------- | 192 | | *🤔 We suggest using the preconfigured `javascript` configuration that ships with this package.* | 193 | 194 |
195 |

196 | 197 |

198 |
199 | 200 | We can set up a JavaScript REPL in the background as follows… 201 | 202 | ```emacs-lisp 203 | ;; C-x C-j now evaluates arbitrary JavaScript code 204 | (repl-driven-development [C-x C-j] "node -i") 205 | ``` 206 | 207 | That's it! Press C-x C-e on the above line so that C-x C-j will now evaluate a selection, or the entire line, as if it were JavaScript code. 208 | 209 | - Why C-x C-j  ?  Well, C-x C-“e” for Emacs Lisp code, and C-x C-“j” for JavaScript code! 210 | - For instance, copy-paste the following examples into a JavaScript file —or just press C-x C-j *in any buffer* to evaluate them! 211 | 212 | ```javascript 213 | 1 + 2 // ⮕ 3 214 | 215 | 1 + '2' // ⮕ '12' 216 | 217 | let me = {name: 'Jasim'}; Object.keys(me) // ⮕ ['name'] 218 | 219 | me.doesNotExist('whoops') // ⮕ Uncaught TypeError 220 | ``` 221 | 222 | All of these results are echoed inline in an overlay, by default. Moreover, there is a **REPL** buffer created for your REPL so you can see everything you've sent to it, and the output it sent back. This is particularly useful for lengthy error messages, such as those of Java, which cannot be rendered nicely within an overlay. 223 | 224 | How this works is that Emacs spawns a new “node -i” process, then C-x C-j sends text to that process. Whenever the process emits any output —on stdout or stderr— then we emit that to the user via an overlay starting with “⮕”. 225 | 226 | Finally, “C-h k C-x C-j” will show you the name of the function that is invoked when you press C-x C-j, along with minimal docs. 227 | 228 | A useful example would be a minimal server, and requests for it. 229 | 230 | ```javascript 231 | // First get stuff with C-x C-e: 232 | // (async-shell-command "npm install -g express axios") 233 | 234 | let app = require('express')() 235 | let clicked = 1 236 | app.get('/hi', (req, res) => res.send(`Hello World × ${clicked++}`)) 237 | 238 | let server = app.listen(3000) 239 | // Now visit http://localhost:3000/hi a bunch of times! 240 | 241 | // Better yet, see the output programmatically... 242 | let axios = require('axios') 243 | // Press C-x C-j a bunch of times on the following expression ♥‿♥ 244 | console.log((await axios.get('http://localhost:3000/hi')).data) 245 | 246 | // Consider closing the server when you're done with it. 247 | server.close() 248 | ``` 249 | 250 | Just as “Emacs is a Lisp Machine”, one can use “VSCodeJS” to use “VSCode as a JS Machine”. See . 251 | 252 |
253 | 254 |
🔗
Python 255 | 256 | | | 257 | |------------------------------------------------------------------------------------------- | 258 | | *🤔 We suggest using the preconfigured `python` configuration that ships with this package.* | 259 | 260 |
261 |

262 | 263 |

264 |
265 | 266 | We can set up a Python REPL in the background as follows… 267 | 268 | ```emacs-lisp 269 | ;; C-x C-p now evaluates arbitrary Python code 270 | (repl-driven-development [C-x C-p] "python3 -i") 271 | ``` 272 | 273 | Example use… 274 | 275 | ```python 276 | 1 + 2 # ⮕ 3 277 | 278 | hello = 'world!' # (No output; this is an effectful operation) 279 | 280 | print(hello) # ⮕ world! 281 | 282 | 2 + 'hi' # 🚫 TypeError: unsupported operand type(s) for + 283 | ``` 284 | 285 | Learn more by reading… [Python: A Gentle Introduction to Socket Programming](https://cs.lmu.edu/~ray/notes/pythonnetexamples/) 286 | 287 |
288 | 289 |
🔗
Java 290 | 291 | | | 292 | |----------------------------------------------------------------------------------------- | 293 | | *🤔 We suggest using the preconfigured `java` configuration that ships with this package.* | 294 | 295 |
296 |

297 | 298 |

299 |
300 | 301 | We can set up a Java REPL in the background as follows… 302 | 303 | ```emacs-lisp 304 | (repl-driven-development [C-x C-j] "jshell --enable-preview" :prompt "jshell>") 305 | ``` 306 | 307 | Now, we can select the following and press `C-x C-j` to evaluate the Java code: 308 | 309 | ```java 310 | // Ensure you're not fullscreen, and you'll see a dialog window appear. 311 | import javax.swing.*; 312 | JOptionPane.showMessageDialog(new JFrame(), "Super nice!"); 313 | ``` 314 | 315 | Or doing algebraic datatypes in Java: 316 | 317 | ```java 318 | sealed interface Maybe { 319 | record None() implements Maybe {} 320 | record Just(int x) implements Maybe {} 321 | } 322 | 323 | var thisPrettyPrintsNicelyInTheREPL = new Maybe.Just(3); 324 | 325 | new Maybe.Just(3).equals(new Maybe.Just(3)) // yay 326 | ``` 327 | 328 |
329 | 330 |
🔗
Ruby 331 | 332 |
333 |

334 | 335 |

336 |
337 | 338 | We can set up a REPL in the background as follows… 339 | 340 | ```emacs-lisp 341 | ;; “C-x e r” now “e”valuates arbitrary “r”uby code 342 | (repl-driven-development [C-x e r] "irb --inf-ruby-mode" :prompt "irb(main):.*>") 343 | ``` 344 | 345 | For example… 346 | 347 | ```ruby 348 | 2 + 2 349 | 350 | 33 + 4 351 | 352 | 5.times { print "Odelay!" } # ⮕ Odelay! Odelay! Odelay! Odelay! Odelay! 5 353 | 354 | ['ruby', 'is', 'readable'].map { | food | food.capitalize } # ⮕ ["Ruby", "Is", "Readable"] 355 | ``` 356 | 357 |
358 | 359 |
🔗
Clojure 360 | 361 |
362 |

363 | 364 |

365 |
366 | 367 | We can set up a REPL in the background as follows… 368 | 369 | ```emacs-lisp 370 | ;; “C-x C-k” now evaluates arbitrary Clojure code 371 | (repl-driven-development [C-x C-k] "clojure" :prompt "user=>") 372 | ``` 373 | 374 | For example… 375 | 376 | ```clojure 377 | (+ 1 2) ;; ⮕ 3 378 | 379 | (defn square [x] (* x x)) ;; ⮕ #'user/square 380 | (square 3) ;; ⮕ 9 381 | ``` 382 | 383 |
384 | 385 |
🔗
TypeScript 386 | 387 | We can set up a REPL in the background as follows… 388 | 389 | ```emacs-lisp 390 | ;; C-x C-j now evaluates arbitrary JavaScript code 391 | (repl-driven-development [C-x C-t] "npx ts-node") 392 | ``` 393 | 394 | Then we can use it as follows: 395 | 396 | ```typescript 397 | 22 + 2 398 | 399 | "hello" 400 | ``` 401 | 402 | However, the output is ugly since it mentions the `^M` character. 403 | 404 | - Look at the `python` configuration that ships with this package for a starting point on how to address this issue. 405 | 406 |
407 | 408 |
🔗
Haskell 409 | 410 |
411 |

412 | 413 |

414 |
415 | 416 | We can set up a REPL in the background as follows… 417 | 418 | ```emacs-lisp 419 | ;; C-x C-h now evaluates arbitrary Haskell code 420 | (repl-driven-development [C-x C-h] "ghci" :prompt "ghci>") 421 | ``` 422 | 423 | For example… 424 | 425 | ```haskell 426 | -- Sum of the first 100 squares 427 | sum [ x ** 2 | x <- [1..100]] -- ⇒ 338350.0 428 | 429 | -- The positive evens at-most 12 430 | [x | x <- [1..12], x `mod` 2 == 0] -- [2,4,6,8,10,12] 431 | 432 | -- Define a function... 433 | myLast = head . reverse 434 | 435 | -- Then use it... 436 | myLast [1, 2, 3] -- ⇒ 3 437 | ``` 438 | 439 | Note that Haskell has “typed holes” with the syntax `_A`: 440 | 441 | ```haskell 442 | 1 + _A -- ⇒ Found hole: _A::a; it :: forall {a}. Num a = a 443 | ``` 444 | 445 | Another language with typed holes is Arend… 446 | 447 |
448 | 449 |
🔗
Arend: Quickly making a terse Emacs interface for a language without one 450 | 451 | The [Arend Theorem Prover](https://arend-lang.github.io/download#console-application) has an IntelliJ interface (since it's a JetBrains proof assistant), but no Emacs counterpart —which may be annoying for Agda/Coq programmers accustomed to Emacs but want to experiment with Arend. 452 | 453 | We can set up an Arend REPL in the background as follows… 454 | 455 | ```emacs-lisp 456 | ;; C-x C-a now evaluates arbitrary Arend code 457 | (repl-driven-development [C-x C-a] 458 | (format "java -jar %s -i" 459 | (f-expand "~/Downloads/Arend.jar"))) 460 | ``` 461 | 462 | Then, 463 | 464 | ```arend 465 | 1 Nat.+ 1 -- ⇒ 2 466 | :type 4 -- ⇒ Fin 5 467 | 468 | -- Declare a constant 469 | \\func f => 1 470 | :type f -- ⇒ Nat 471 | f -- ⇒ 1 472 | 473 | -- Declare a polymorphic identity function, then use it 474 | \\func id {A : \\Type} (a : A) => a 475 | id 12 -- ⇒ 12 476 | 477 | -- Arend has “typed holes” 478 | 1 Nat.+ {?} -- ⇒ Nat.+{?}: Goal: Expectedtype: Nat 479 | ``` 480 | 481 |
482 | 483 |
🔗
PureScript 484 | 485 | First `brew install spago`, then we can set up a PureScript REPL in the background as follows… 486 | 487 | ```emacs-lisp 488 | ;; C-x C-p now evaluates arbitrary PureScript code 489 | (repl-driven-development [C-x C-p] "spago repl") 490 | ``` 491 | 492 | For example…. 493 | 494 | ```purescript 495 | import Prelude 496 | 497 | -- Define a function 498 | add1 = (\x -> x + 1) 499 | 500 | -- Use the function 501 | add1 2 -- ⇒ 3 502 | 503 | -- Experiment with a typed hole 504 | 1 + ?A -- ⇒ Hole ?A has the inferred type Int 505 | ``` 506 | 507 |
508 | 509 |
🔗
Idris 510 | 511 | First `brew install idris2`, then we can set up an Idris REPL in the background as follows… 512 | 513 | ```emacs-lisp 514 | ;; C-x C-i now evaluates arbitrary Idris code 515 | (repl-driven-development [C-x C-i] "idris2") 516 | ``` 517 | 518 | Here's some random code… 519 | 520 | ```purescript 521 | -- Like Lisp, Idris uses “the” for type annotations 522 | the Nat 4 -- ⇒ 4 : Nat 523 | 524 | with List sum [1,2,3] -- ⇒ 6 525 | 526 | -- defining a new type (REPL specific notation) 527 | :let data Foo : Type where Bar : Foo 528 | 529 | :t Bar -- ⇒ Foo 530 | 531 | -- Experiment with a typed hole [Same notation as Haskell] 532 | 1 + ?A -- prim__add_Integer 1 ?A 533 | ``` 534 | 535 |
536 | 537 |
🔗
Racket 538 | 539 | | | 540 | |------------------------------------------------------------------ | 541 | | Racket is a modern programming language in the Lisp/Scheme family. | 542 | 543 | First `brew install --cask racket`, then we can set up an Racket REPL in the background as follows… 544 | 545 | ```emacs-lisp 546 | ;; C-x C-i now evaluates arbitrary Racket code 547 | (repl-driven-development [C-x C-r] "racket -I slideshow") 548 | ``` 549 | 550 | Here's some random code… 551 | 552 | ```racket 553 | (define (series mk) (hc-append 4 (mk 5) (mk 10) (mk 20))) 554 | 555 | ;; Shows 3 circles of increasing radius, in an external window 556 | (show-pict (series circle)) 557 | ``` 558 | 559 | Meeting Racket for the first time is probably best done with *DrRacket*. 560 | 561 |
562 | 563 |
🔗
Common Lisp 564 | 565 | We can set up a REPL in the background as follows… 566 | 567 | ```emacs-lisp 568 | ;; “C-x e p” now “e”valuates arbitrary “c”ommon-lisp code 569 | (repl-driven-development [C-x e c] "sbcl" :prompt "\\*") 570 | ``` 571 | 572 | For example… 573 | 574 | ```common-lisp 575 | (print "hello world") 576 | ``` 577 | 578 |
579 | 580 |
🔗
Perl 581 | 582 | We can set up a REPL in the background as follows… 583 | 584 | ```emacs-lisp 585 | ;; “C-x e p” now “e”valuates arbitrary “p”erl code 586 | (repl-driven-development [C-x e p] "perl -de0") 587 | ``` 588 | 589 | For example… 590 | 591 | ```perl 592 | print(1..5) 593 | ``` 594 | 595 |
596 | 597 |
🔗
Julia 598 | 599 | We can set up a REPL in the background as follows… 600 | 601 | ```emacs-lisp 602 | ;; “C-x e j” now “e”valuates arbitrary “j”uila code 603 | (repl-driven-development [C-x e j] "julia" :prompt "julia>") 604 | ``` 605 | 606 | For example… Let's get a random 2×2 matrix… 607 | 608 | ```julia 609 | rand(2, 2) 610 | ``` 611 | 612 |
613 | 614 |
🔗
GNU Smalltalk 615 | 616 | We can set up a REPL in the background as follows… 617 | 618 | ```emacs-lisp 619 | ;; “C-x e j” now “e”valuates arbitrary “s”malltalk code 620 | (repl-driven-development [C-x e s] "gst" :prompt "st") 621 | ``` 622 | 623 | For example… 624 | 625 | ```smalltalk 626 | 'Hello World!' printNl ! 627 | ``` 628 | 629 |
630 | 631 |
🔗
Tcl 632 | 633 | We can set up a REPL in the background as follows… 634 | 635 | ```emacs-lisp 636 | ;; “C-x e t” now “e”valuates arbitrary “t”cl code 637 | (repl-driven-development [C-x e t] "tclsh" :prompt "%") 638 | ``` 639 | 640 | For example… 641 | 642 | ```tcl 643 | set a 1 644 | set b 2 645 | puts $a$b[expr 2 + 3]{bye} 646 | ``` 647 | 648 |
649 | 650 |
🔗
Lua 651 | 652 | We can set up a REPL in the background as follows… 653 | 654 | ```emacs-lisp 655 | ;; “C-x e l” now “e”valuates arbitrary “l”ua code 656 | (repl-driven-development [C-x e l] "lua") 657 | ``` 658 | 659 | For example… 660 | 661 | ```lua 662 | print("Hello, world") 663 | ``` 664 | 665 |
666 | 667 | 668 | # Videos 669 | 670 | 671 | ## REPL Driven Development :: Teaching a JavaScript runtime, incrementally, to be a web server 🍽️ 🔁 🤖 672 | 673 |
674 | 675 | 676 | 677 | 678 | 679 |
680 | 681 | 682 | # Bye! 683 | 684 | 685 | 686 | 687 | 688 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | #+export_file_name: ~/repl-driven-development/README.md 2 | #+title: Editor Integrated REPLs for all languages 3 | #+author: Musa Al-hassy 4 | #+email: alhassy@gmail.com 5 | #+options: toc:nil d:nil broken-links:t 6 | #+property: header-args :results none :eval never-export :exports code 7 | 8 | # M-x package-install ox-gfm 9 | # (let ((org-export-use-babel nil) (org-export-with-broken-links t)) (load-file "~/blog/AlBasmala.el") (org-mode) (org-gfm-export-to-markdown)) 10 | # *Then* use =grip= to see that this looks reasonable. 11 | 12 | 13 | * Top Matter :ignore: 14 | :PROPERTIES: 15 | :CUSTOM_ID: Top-Matter 16 | :END: 17 | #+html:
18 | # badge:repl-driven-development|1.0.8|informational|https://github.com/alhassy/repl-driven-development|Gnu-Emacs 19 | 20 | #+html:

Editor Integrated REPLs for all languages

21 | 22 | melpa:repl-driven-development 23 | 24 | tweet:https://github.com/alhassy/repl-driven-development 25 | badge:contributions|welcome|green|https://github.com/alhassy/repl-driven-development/issues 26 | 27 | badge:author|musa_al-hassy|purple|https://alhassy.com/|nintendo-3ds 28 | badge:|buy_me_a coffee|gray|https://www.buymeacoffee.com/alhassy|buy-me-a-coffee 29 | 30 | This library provides the Emacs built-in kbd:C─x_C─e behaviour for 31 | arbitrary languages, provided they have a REPL shell command. 32 | | *It provides a “send line to REPL process” command, for your language.* | 33 | #+html:
34 | 35 | #+html:
36 | #+html: 37 | #+attr_html: :width 400px 38 | file:rdd.gif 39 | #+html:
40 | 41 | #+TOC: headlines 2 42 | 43 | * Motivation 44 | :PROPERTIES: 45 | :CUSTOM_ID: motivation 46 | :END: 47 | 48 | Whenever reading/refactoring some code, if you can make some of it 49 | self-contained, then you can immediately try it out! No need to 50 | load your entire program; nor copy-paste into an external REPL. The 51 | benefits of Emacs' built-in kbd:C─x_C─e for Lisp, and Lisp's Repl 52 | Driven Development philosophy, are essentially made possible for 53 | arbitrary languages (to some approximate degree, but not fully). 54 | 55 | #+html:
56 | #+html: 57 | #+html:
58 | 59 | Just as kbd:C-u_C-x_C-e inserts the resulting expression at the 60 | current cursour position, so too all =repl-driven-development= 61 | commands allow for a kbd:C─u prefix which inserts the result. 62 | This allows for a nice scripting experience where results 63 | are kept for future use. 64 | 65 | Finally, just as kbd:C─h_e shows you the =*Messages*= buffer 66 | where you can see the evaluations of your Emacs Lisp via 67 | kbd:C─x_C─e; likewise, kbd:C─h_e shows you the output results 68 | of any REPL command created by =repl-driven-development=. 69 | 70 | * COMMENT Official Manual 71 | :PROPERTIES: 72 | :CUSTOM_ID: official-manual 73 | :END: 74 | 75 | See http://alhassy.com/repl-driven-development 76 | 77 | kbd:C-h_o_repl-driven-development also has extensive docs, 78 | via a JavaScript server example. 79 | 80 | * Mini-Tutorial 81 | :PROPERTIES: 82 | :CUSTOM_ID: mini-tutorial 83 | :END: 84 | 85 | Often, while reading a README file, we will (1) copy a shell command, (2) open a 86 | terminal, and (3) paste the shell command to run it. We can evaluate arbitrary 87 | regions in a shell in one step via kbd:C─x_C─t with: 88 | 89 | #+begin_src emacs-lisp :exports code :eval never-export 90 | (repl-driven-development [C-x C-t] "bash") 91 | #+end_src 92 | 93 | 94 | For example, execute kbd:C─x_C─t anywhere on each line below and see results in an 95 | overlay, right by your cursor. 96 | 97 | #+begin_src shell 98 | echo "It is $(date) and I am at $PWD, my name is $(whoami) and I have: $(ls)" 99 | 100 | say "My name is $(whoami) and I like Emacs" 101 | #+end_src 102 | 103 | Notice as each line is sent to the Bash process, the line is highlighted briefly 104 | in yellow. Moreover, you can hover over the text to see a tooltip with the 105 | resulting shell output. Finally, if you invoke kbd:C-h_k_C-x_C-t you get help 106 | about this new kbd:C─x_C─t command, such as inserting results at point via 107 | kbd:C-u_C-x_C-t or to reset/refresh the current Bash process with kbd:C-u_-1_C-x_C-t. 108 | 109 | This also works for any command-line REPL; for example, for Python: 110 | 111 | #+begin_src emacs-lisp 112 | (repl-driven-development [C-x C-p] "python3") 113 | #+end_src 114 | 115 | Then, we can submit the following Python snippets with kbd:C─x_C─p on each line. 116 | 117 | #+begin_src python 118 | sum([1, 2, 3, 4]) 119 | 120 | list(map(lambda i: 'Fizz'*(not i%3)+'Buzz'*(not i%5) or i, range(1,101))) 121 | #+end_src 122 | 123 | These work fine, however there are some shortcomings of this REPL. 124 | For example, echoing results could be prettier and it doesn't handle 125 | multi-line input very well. You can address these issues using the various 126 | hooks / keyword arguments of the =repl-driven-development= macro. 127 | 128 | However, this package comes with preconfigured REPLS for: =python, terminal, java, javascript=. 129 | 130 | Simply use the name of these configurations: 131 | 132 | #+begin_src emacs-lisp 133 | (repl-driven-development [C-x C-p] python) 134 | #+end_src 135 | 136 | Now we can submit the following, with kbd:C─x_C─p, with no issues: 137 | 138 | #+begin_src python 139 | def square(x): 140 | return x * x 141 | 142 | square(5) 143 | #+end_src 144 | 145 | Since these new REPL commands are just Emacs functions, we can use 146 | several at the time, alternating between them. For example: 147 | 148 | #+begin_src emacs-lisp 149 | ;; C-x C-e on the next two lines 150 | (repl-driven-development [C-x C-t] terminal) 151 | (repl-driven-development [C-x C-p] python) 152 | #+end_src 153 | #+begin_src shell 154 | echo Hello... > /tmp/o # C-x C-t here 155 | #+end_src 156 | #+begin_src python 157 | print(open("/tmp/o").read()) # C-x C-p here 158 | #+end_src 159 | #+begin_src shell 160 | echo ...and bye >> /tmp/o # C-x C-t again 161 | #+end_src 162 | #+begin_src python 163 | print(open("/tmp/o").read()) # C-x C-p again 164 | #+end_src 165 | 166 | Let's conclude with a GUI example in Java. 167 | #+begin_src emacs-lisp 168 | ;; Set “C-x C-j” to evaluate Java code in a background REPL. 169 | (repl-driven-development [C-x C-j] "jshell") 170 | #+end_src 171 | #+begin_src java 172 | // Select this Java snippet, then press “C-x C-j” to evaluate it 173 | import javax.swing.*; 174 | JOptionPane.showMessageDialog(new JFrame(){{setAlwaysOnTop(true);}}, "Super nice!") 175 | #+end_src 176 | 177 | We can use a preconfigured Java REPL, to remove the annoying “jshell>” prompt 178 | from overlay echos, handle multi-line input, and more. 179 | #+begin_src emacs-lisp 180 | (repl-driven-development [C-x C-j] java) 181 | #+end_src 182 | #+begin_src java 183 | // REPL result values are shown as overlays: 184 | // See a list of 23 numbers, which are attached as a tooltip to this text. 185 | IntStream.range(0, 23).forEach(x -> System.out.println(x)) 186 | #+end_src 187 | 188 | For more documentation, and examples, 189 | see http://alhassy.com/repl-driven-development 190 | 191 | * Installation & Usage Instructions 192 | :PROPERTIES: 193 | :CUSTOM_ID: installation 194 | :END: 195 | 196 | 197 | This package is on Melpa, melpa:repl-driven-development, so you can install it 198 | with [[https://github.com/alhassy/emacs.d#use-package-the-start-of-initel][use-package]]: 199 | 200 | #+BEGIN_SRC emacs-lisp :tangle no 201 | (use-package repl-driven-development 202 | :ensure t 203 | :config 204 | (repl-driven-development [C-x C-j] java) ;; e“X”ecute “j”ava 205 | (repl-driven-development [C-x C-n] javascript) ;; e“X”ecute “n”odejs 206 | (repl-driven-development [C-x C-p] python) ;; e“X”ecute “p”ython 207 | (repl-driven-development [C-x C-t] terminal)) ;; e“X”ecute “t”erminal 208 | #+END_SRC 209 | 210 | The above mentions the four pre-configured REPLs that the package comes with: 211 | These are like their CLI equivalents, but offer more bells and whistles. 212 | - For example, the pre-configured =java= REPL is like 213 | =(repl-driven-development [C-x C-j] "jshell")= but it supports multi-line input: 214 | JShell eagerly inserts semicolons onto expressions, so, say, a multi-line Stream 215 | pipeline would be interpreted as multiple distinct statements by JShell, 216 | whereas our =java= configuration handles this by stripping the newlines (and any 217 | intermediary comments). 218 | 219 | You can use any process, for example let's use the =tclsh= command line process: 220 | #+begin_src emacs-lisp 221 | (repl-driven-development [s-t] "tclsh" ;; Make “⌘-t” e“X”ecute “T”cl code, 222 | :blink 'pulsar-blue ;; and highlight submitted lines blue 223 | :prompt "%") ;; and don't show me the tclsh prompt, which is “%”. 224 | #+end_src 225 | 226 | The =repl-driven-development= macro has been tried at least with the following 227 | processes. 228 | | /Please make a Pull Request, or Issue, to increase the following list!/ | 229 | 230 | ** TODO COMMENT bash 231 | | /🤔 We suggest using the preconfigured =javascript= configuration that ships with this package./ | 232 | 233 | ** JavaScript ---and a minimal server :details_javascript: 234 | :PROPERTIES: 235 | :CUSTOM_ID: JavaScript-and-a-minimal-server 236 | :END: 237 | 238 | | /🤔 We suggest using the preconfigured =javascript= configuration that ships with this package./ | 239 | 240 | #+begin_center 241 | badge:JavaScript|Colourful PDF CheatSheet|success|https://alhassy.github.io/JavaScriptCheatSheet/CheatSheet.pdf|javascript 242 | #+end_center 243 | 244 | We can set up a JavaScript REPL in the background as follows... 245 | #+begin_src emacs-lisp :tangle nil 246 | ;; C-x C-j now evaluates arbitrary JavaScript code 247 | (repl-driven-development [C-x C-j] "node -i") 248 | #+end_src 249 | 250 | That's it! Press kbd:C-x_C-e on the above line so that kbd:C-x C-j will now 251 | evaluate a selection, or the entire line, as if it were JavaScript code. 252 | - Why kbd:C-x C-j  ?  Well, kbd:C-x_C-“e” for Emacs Lisp code, and kbd:C-x_C-“j” 253 | for JavaScript code! 254 | - For instance, copy-paste the following examples into a JavaScript file ---or just 255 | press kbd:C-x C-j /in any buffer/ to evaluate them! 256 | 257 | #+begin_src javascript 258 | 1 + 2 // ⮕ 3 259 | 260 | 1 + '2' // ⮕ '12' 261 | 262 | let me = {name: 'Jasim'}; Object.keys(me) // ⮕ ['name'] 263 | 264 | me.doesNotExist('whoops') // ⮕ Uncaught TypeError 265 | #+end_src 266 | 267 | All of these results are echoed inline in an overlay, by default. 268 | Moreover, there is a *REPL* buffer created for your REPL so you 269 | can see everything you've sent to it, and the output it sent 270 | back. This is particularly useful for lengthy error messages, 271 | such as those of Java, which cannot be rendered nicely within an 272 | overlay. 273 | 274 | How this works is that Emacs spawns a new “node -i” process, then 275 | kbd:C-x_C-j sends text to that process. Whenever the process emits 276 | any output ---on stdout or stderr--- then we emit that to the 277 | user via an overlay starting with “⮕”. 278 | 279 | Finally, “C-h k C-x C-j” will show you the name of the function 280 | that is invoked when you press C-x C-j, along with minimal docs. 281 | 282 | A useful example would be a minimal server, and requests for it. 283 | 284 | #+begin_src javascript 285 | // First get stuff with C-x C-e: 286 | // (async-shell-command "npm install -g express axios") 287 | 288 | let app = require('express')() 289 | let clicked = 1 290 | app.get('/hi', (req, res) => res.send(`Hello World × ${clicked++}`)) 291 | 292 | let server = app.listen(3000) 293 | // Now visit http://localhost:3000/hi a bunch of times! 294 | 295 | // Better yet, see the output programmatically... 296 | let axios = require('axios') 297 | // Press C-x C-j a bunch of times on the following expression ♥‿♥ 298 | console.log((await axios.get('http://localhost:3000/hi')).data) 299 | 300 | // Consider closing the server when you're done with it. 301 | server.close() 302 | #+end_src 303 | 304 | # (use-package plz) ;; An HTTP library for Emacs 305 | # (plz 'get \"http://localhost:3000/hi\") 306 | 307 | Just as “Emacs is a Lisp Machine”, one can use “VSCodeJS” to use 308 | “VSCode as a JS Machine”. 309 | See http://alhassy.com/vscode-is-itself-a-javascript-repl. 310 | 311 | ** Python :details_python_#add8e6: 312 | :PROPERTIES: 313 | :CUSTOM_ID: Python 314 | :END: 315 | 316 | | /🤔 We suggest using the preconfigured =python= configuration that ships with this package./ | 317 | 318 | #+begin_center 319 | badge:Python|Colourful PDF CheatSheet|success|https://alhassy.github.io/PythonCheatSheet/CheatSheet.pdf|python 320 | #+end_center 321 | 322 | We can set up a Python REPL in the background as follows... 323 | #+begin_src emacs-lisp :tangle nil 324 | ;; C-x C-p now evaluates arbitrary Python code 325 | (repl-driven-development [C-x C-p] "python3 -i") 326 | #+end_src 327 | 328 | Example use... 329 | #+begin_src python 330 | 1 + 2 # ⮕ 3 331 | 332 | hello = 'world!' # (No output; this is an effectful operation) 333 | 334 | print(hello) # ⮕ world! 335 | 336 | 2 + 'hi' # 🚫 TypeError: unsupported operand type(s) for + 337 | #+end_src 338 | 339 | Learn more by reading... [[https://cs.lmu.edu/~ray/notes/pythonnetexamples/][Python: A Gentle Introduction to Socket Programming]] 340 | 341 | ** Java :details_java: 342 | :PROPERTIES: 343 | :CUSTOM_ID: Java 344 | :END: 345 | 346 | | /🤔 We suggest using the preconfigured =java= configuration that ships with this package./ | 347 | 348 | #+begin_center 349 | badge:Java|Colourful PDF CheatSheet|success|https://alhassy.com/java-cheat-sheet.pdf|coffeescript 350 | #+end_center 351 | 352 | We can set up a Java REPL in the background as follows... 353 | #+begin_src emacs-lisp 354 | (repl-driven-development [C-x C-j] "jshell --enable-preview" :prompt "jshell>") 355 | #+end_src 356 | 357 | Now, we can select the following and press =C-x C-j= to evaluate the Java code: 358 | #+begin_src java :tangle no 359 | // Ensure you're not fullscreen, and you'll see a dialog window appear. 360 | import javax.swing.*; 361 | JOptionPane.showMessageDialog(new JFrame(), "Super nice!"); 362 | #+end_src 363 | 364 | Or doing algebraic datatypes in Java: 365 | #+begin_src java :tangle no 366 | sealed interface Maybe { 367 | record None() implements Maybe {} 368 | record Just(int x) implements Maybe {} 369 | } 370 | 371 | var thisPrettyPrintsNicelyInTheREPL = new Maybe.Just(3); 372 | 373 | new Maybe.Just(3).equals(new Maybe.Just(3)) // yay 374 | #+end_src 375 | 376 | ** TODO COMMENT Java MWE 377 | :PROPERTIES: 378 | :CUSTOM_ID: COMMENT-Java-MWE 379 | :END: 380 | 381 | #+begin_center 382 | badge:Java|Colourful PDF CheatSheet|success|https://alhassy.com/java-cheat-sheet.pdf|coffeescript 383 | #+end_center 384 | 385 | We can set up a Java REPL in the background as follows... 386 | #+begin_src emacs-lisp 387 | (repl-driven-development [C-x C-j] "jshell --enable-preview" :prompt "jshell>") 388 | #+end_src 389 | 390 | Now, we can select the following and press =C-x C-j= to evaluate the Java code: 391 | #+begin_src java :tangle no 392 | // Ensure you're not fullscreen, and you'll see a dialog window appear. 393 | import javax.swing.*; 394 | JOptionPane.showMessageDialog(new JFrame(), "Super nice!"); 395 | #+end_src 396 | 397 | Or doing algebraic datatypes in Java: 398 | #+begin_src java :tangle no 399 | sealed interface Maybe { 400 | record None() implements Maybe {} 401 | record Just(int x) implements Maybe {} 402 | } 403 | 404 | var thisPrettyPrintsNicelyInTheREPL = new Maybe.Just(3); 405 | 406 | new Maybe.Just(3).equals(new Maybe.Just(3)) // yay 407 | #+end_src 408 | 409 | ** TODO COMMENT Kotlin with ki 410 | ** TODO COMMENT Scala with scala 411 | ** COMMENT PHP 412 | ** Ruby :details_ruby: 413 | 414 | #+begin_center 415 | badge:Ruby|Colourful PDF CheatSheet|success|https://alhassy.github.io/RubyCheatSheet/CheatSheet.pdf|ruby 416 | #+end_center 417 | 418 | We can set up a REPL in the background as follows... 419 | #+begin_src emacs-lisp :tangle nil 420 | ;; “C-x e r” now “e”valuates arbitrary “r”uby code 421 | (repl-driven-development [C-x e r] "irb --inf-ruby-mode" :prompt "irb(main):.*>") 422 | #+end_src 423 | 424 | For example... 425 | #+begin_src ruby 426 | 2 + 2 427 | 428 | 33 + 4 429 | 430 | 5.times { print "Odelay!" } # ⮕ Odelay! Odelay! Odelay! Odelay! Odelay! 5 431 | 432 | ['ruby', 'is', 'readable'].map { | food | food.capitalize } # ⮕ ["Ruby", "Is", "Readable"] 433 | #+end_src 434 | 435 | ** Clojure :details_clojure: 436 | :PROPERTIES: 437 | :CUSTOM_ID: Clojure 438 | :END: 439 | 440 | #+begin_center 441 | badge:Clojure|Colourful PDF CheatSheet|success|https://alhassy.github.io/ClojureCheatSheet/CheatSheet.pdf|awslambda 442 | #+end_center 443 | 444 | We can set up a REPL in the background as follows... 445 | #+begin_src emacs-lisp 446 | ;; “C-x C-k” now evaluates arbitrary Clojure code 447 | (repl-driven-development [C-x C-k] "clojure" :prompt "user=>") 448 | #+end_src 449 | 450 | For example... 451 | #+begin_src clojure 452 | (+ 1 2) ;; ⮕ 3 453 | 454 | (defn square [x] (* x x)) ;; ⮕ #'user/square 455 | (square 3) ;; ⮕ 9 456 | #+end_src 457 | 458 | ** TypeScript :details_typescript: 459 | 460 | We can set up a REPL in the background as follows... 461 | #+begin_src emacs-lisp :tangle nil 462 | ;; C-x C-j now evaluates arbitrary JavaScript code 463 | (repl-driven-development [C-x C-t] "npx ts-node") 464 | #+end_src 465 | 466 | Then we can use it as follows: 467 | #+begin_src typescript 468 | 22 + 2 469 | 470 | "hello" 471 | #+end_src 472 | 473 | However, the output is ugly since it mentions the =^M= character. 474 | - Look at the =python= configuration that ships with this package for a starting 475 | point on how to address this issue. 476 | 477 | ** Haskell :details_haskell_#add8e6: 478 | :PROPERTIES: 479 | :CUSTOM_ID: Haskell 480 | :END: 481 | 482 | #+begin_center 483 | badge:Haskell|Colourful PDF CheatSheet|success|https://alhassy.github.io/HaskellCheatSheet/CheatSheet.pdf|awslambda 484 | #+end_center 485 | 486 | We can set up a REPL in the background as follows... 487 | #+begin_src emacs-lisp :tangle nil 488 | ;; C-x C-h now evaluates arbitrary Haskell code 489 | (repl-driven-development [C-x C-h] "ghci" :prompt "ghci>") 490 | #+end_src 491 | 492 | For example... 493 | #+begin_src haskell 494 | -- Sum of the first 100 squares 495 | sum [ x ** 2 | x <- [1..100]] -- ⇒ 338350.0 496 | 497 | -- The positive evens at-most 12 498 | [x | x <- [1..12], x `mod` 2 == 0] -- [2,4,6,8,10,12] 499 | 500 | -- Define a function... 501 | myLast = head . reverse 502 | 503 | -- Then use it... 504 | myLast [1, 2, 3] -- ⇒ 3 505 | #+end_src 506 | 507 | Note that Haskell has “typed holes” with the syntax =_A=: 508 | #+begin_src haskell :tangle nil 509 | 1 + _A -- ⇒ Found hole: _A::a; it :: forall {a}. Num a = a 510 | #+end_src 511 | 512 | Another language with typed holes is Arend... 513 | 514 | ** Arend: Quickly making a terse Emacs interface for a language without one :details_arend: 515 | 516 | The [[https://arend-lang.github.io/download#console-application][Arend Theorem Prover]] has an IntelliJ interface (since it's a JetBrains proof 517 | assistant), but no Emacs counterpart ---which may be annoying for Agda/Coq 518 | programmers accustomed to Emacs but want to experiment with Arend. 519 | 520 | We can set up an Arend REPL in the background as follows... 521 | #+begin_src emacs-lisp :tangle nil 522 | ;; C-x C-a now evaluates arbitrary Arend code 523 | (repl-driven-development [C-x C-a] 524 | (format "java -jar %s -i" 525 | (f-expand "~/Downloads/Arend.jar"))) 526 | #+end_src 527 | 528 | Then, 529 | #+begin_src arend :tangle nil 530 | 1 Nat.+ 1 -- ⇒ 2 531 | :type 4 -- ⇒ Fin 5 532 | 533 | -- Declare a constant 534 | \\func f => 1 535 | :type f -- ⇒ Nat 536 | f -- ⇒ 1 537 | 538 | -- Declare a polymorphic identity function, then use it 539 | \\func id {A : \\Type} (a : A) => a 540 | id 12 -- ⇒ 12 541 | 542 | -- Arend has “typed holes” 543 | 1 Nat.+ {?} -- ⇒ Nat.+{?}: Goal: Expectedtype: Nat 544 | #+end_src 545 | 546 | ** PureScript :details_purescript_#add8e6: 547 | :PROPERTIES: 548 | :CUSTOM_ID: PureScript 549 | :END: 550 | 551 | First =brew install spago=, then we can set up a PureScript REPL in the background 552 | as follows... 553 | #+begin_src emacs-lisp :tangle nil 554 | ;; C-x C-p now evaluates arbitrary PureScript code 555 | (repl-driven-development [C-x C-p] "spago repl") 556 | #+end_src 557 | 558 | For example.... 559 | #+begin_src purescript :tangle nil 560 | import Prelude 561 | 562 | -- Define a function 563 | add1 = (\x -> x + 1) 564 | 565 | -- Use the function 566 | add1 2 -- ⇒ 3 567 | 568 | -- Experiment with a typed hole 569 | 1 + ?A -- ⇒ Hole ?A has the inferred type Int 570 | #+end_src 571 | 572 | ** Idris :details_idris: 573 | :PROPERTIES: 574 | :CUSTOM_ID: Idris 575 | :END: 576 | 577 | First =brew install idris2=, then we can set up an Idris REPL in the background as 578 | follows... 579 | #+begin_src emacs-lisp :tangle nil 580 | ;; C-x C-i now evaluates arbitrary Idris code 581 | (repl-driven-development [C-x C-i] "idris2") 582 | #+end_src 583 | 584 | Here's some random code... 585 | #+begin_src purescript :tangle nil 586 | -- Like Lisp, Idris uses “the” for type annotations 587 | the Nat 4 -- ⇒ 4 : Nat 588 | 589 | with List sum [1,2,3] -- ⇒ 6 590 | 591 | -- defining a new type (REPL specific notation) 592 | :let data Foo : Type where Bar : Foo 593 | 594 | :t Bar -- ⇒ Foo 595 | 596 | -- Experiment with a typed hole [Same notation as Haskell] 597 | 1 + ?A -- prim__add_Integer 1 ?A 598 | #+end_src 599 | 600 | ** Racket :details_racket_#add8e6: 601 | :PROPERTIES: 602 | :CUSTOM_ID: Racket 603 | :END: 604 | 605 | | Racket is a modern programming language in the Lisp/Scheme family. | 606 | 607 | First =brew install --cask racket=, then we can set up an Racket REPL in the 608 | background as follows... 609 | #+begin_src emacs-lisp :tangle nil 610 | ;; C-x C-i now evaluates arbitrary Racket code 611 | (repl-driven-development [C-x C-r] "racket -I slideshow") 612 | #+end_src 613 | 614 | Here's some random code... 615 | #+begin_src racket :tangle nil 616 | (define (series mk) (hc-append 4 (mk 5) (mk 10) (mk 20))) 617 | 618 | ;; Shows 3 circles of increasing radius, in an external window 619 | (show-pict (series circle)) 620 | #+end_src 621 | 622 | Meeting Racket for the first time is probably best done with /DrRacket/. 623 | # - Learning it can be done with a nice Emacs-integrated REPL ---see [[https://beautifulracket.com/][Beautiful Racket by Matthew Butterick]]. 624 | 625 | ** Common Lisp :details_common_lisp: 626 | 627 | We can set up a REPL in the background as follows... 628 | #+begin_src emacs-lisp :tangle nil 629 | ;; “C-x e p” now “e”valuates arbitrary “c”ommon-lisp code 630 | (repl-driven-development [C-x e c] "sbcl" :prompt "\\*") 631 | #+end_src 632 | 633 | For example... 634 | #+begin_src common-lisp 635 | (print "hello world") 636 | #+end_src 637 | ** Perl :details_perl: 638 | 639 | We can set up a REPL in the background as follows... 640 | #+begin_src emacs-lisp :tangle nil 641 | ;; “C-x e p” now “e”valuates arbitrary “p”erl code 642 | (repl-driven-development [C-x e p] "perl -de0") 643 | #+end_src 644 | 645 | For example... 646 | #+begin_src perl 647 | print(1..5) 648 | #+end_src 649 | ** Julia :details_julia: 650 | 651 | We can set up a REPL in the background as follows... 652 | #+begin_src emacs-lisp :tangle nil 653 | ;; “C-x e j” now “e”valuates arbitrary “j”uila code 654 | (repl-driven-development [C-x e j] "julia" :prompt "julia>") 655 | #+end_src 656 | 657 | For example... Let's get a random 2×2 matrix... 658 | #+begin_src julia 659 | rand(2, 2) 660 | #+end_src 661 | ** GNU Smalltalk :details_smalltalk: 662 | 663 | We can set up a REPL in the background as follows... 664 | #+begin_src emacs-lisp :tangle nil 665 | ;; “C-x e j” now “e”valuates arbitrary “s”malltalk code 666 | (repl-driven-development [C-x e s] "gst" :prompt "st") 667 | #+end_src 668 | 669 | For example... 670 | #+begin_src smalltalk 671 | 'Hello World!' printNl ! 672 | #+end_src 673 | ** Tcl :details_tcl: 674 | 675 | We can set up a REPL in the background as follows... 676 | #+begin_src emacs-lisp :tangle nil 677 | ;; “C-x e t” now “e”valuates arbitrary “t”cl code 678 | (repl-driven-development [C-x e t] "tclsh" :prompt "%") 679 | #+end_src 680 | 681 | For example... 682 | #+begin_src tcl 683 | set a 1 684 | set b 2 685 | puts $a$b[expr 2 + 3]{bye} 686 | #+end_src 687 | ** Lua :details_lua: 688 | 689 | We can set up a REPL in the background as follows... 690 | #+begin_src emacs-lisp :tangle nil 691 | ;; “C-x e l” now “e”valuates arbitrary “l”ua code 692 | (repl-driven-development [C-x e l] "lua") 693 | #+end_src 694 | 695 | For example... 696 | #+begin_src lua 697 | print("Hello, world") 698 | #+end_src 699 | ** TODO COMMENT tcl with tclsh 700 | ** COMMENT C 701 | # TODO: https://github.com/asvitkine/ccons 702 | # [Featurful!] Interactive Console for the C Programming Language 703 | 704 | ** COMMENT F# :Does_not_work:fsharpi_requires_ansi_term__not_eshell: 705 | :PROPERTIES: 706 | :CUSTOM_ID: COMMENT-F 707 | :END: 708 | 709 | badge:F#|Colourful PDF CheatSheet|success|https://alhassy.github.io/FSharpCheatSheet/CheatSheet.pdf|f-sharp 710 | 711 | #+begin_center 712 | badge:OCaml|Colourful PDF CheatSheet|success|https://alhassy.github.io/OCamlCheatSheet/CheatSheet.pdf|OCaml 713 | #+end_center 714 | 715 | First =brew install mono=, then we can set up an F# REPL in the background as 716 | follows... 717 | #+begin_src emacs-lisp :tangle nil 718 | ;; C-x C-j now evaluates arbitrary F#Script code 719 | (repl-driven-development [C-x C-j] "fsharpi") 720 | #+end_src 721 | 722 | #+begin_src fsharp :tangle nil 723 | -- F# has “ranges with a step” 724 | [0..3..14];; -- ⇒ [0; 3; 6; 9] 725 | 726 | -- Which are a shorthand for guarded comprehensions 727 | [for i in 0..14 do if i % 3 = 0 then yield i];; 728 | 729 | -- Experiment with a typed hole 730 | 1 + __;; -- ⇒ The type 'obj' does not match the type 'int' 731 | #+end_src 732 | 733 | If you like F#, take a look at [[https://fstar-lang.org/][F*: A Proof-Oriented Programming Language]]. 734 | 735 | ** COMMENT Ocaml 736 | * Videos 737 | :PROPERTIES: 738 | :CUSTOM_ID: videos 739 | :END: 740 | 741 | ** REPL Driven Development :: Teaching a JavaScript runtime, incrementally, to be a web server 🍽️ 🔁 🤖 742 | :PROPERTIES: 743 | :CUSTOM_ID: teaching-a-javascript-runtime-incrementally-to-be-a-web-server- 744 | :END: 745 | 746 | #+html:
747 | #+html: 748 | #+html: 749 | #+html:
750 | * Bye! 751 | :PROPERTIES: 752 | :CUSTOM_ID: Bye 753 | :END: 754 | 755 | badge:thanks|for_reading 756 | 757 | tweet:https://github.com/alhassy/repl-driven-development 758 | 759 | badge:|buy_me_a coffee|gray|https://www.buymeacoffee.com/alhassy|buy-me-a-coffee 760 | -------------------------------------------------------------------------------- /rdd.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alhassy/repl-driven-development/2ffa5368a6db602a9f220935cd985999c60845ba/rdd.gif -------------------------------------------------------------------------------- /repl-driven-development.el: -------------------------------------------------------------------------------- 1 | ;;; repl-driven-development.el --- Send arbitrary code to a REPL in the background 2 | 3 | ;; Copyright (c) 2023 Musa Al-hassy 4 | 5 | ;; Author: Musa Al-hassy 6 | ;; Version: 1.0.13 7 | ;; Package-Requires: ((s "1.12.0") (f "0.20.0") (lf "1.0") (dash "2.16.0") (eros "0.1.0") (bind-key "2.4.1") (emacs "29") (f "0.20.0") (devdocs "0.5") (pulsar "1.0.1") (peg "1.0.1") (json-navigator "1.0.0")) 8 | ;; Keywords: repl-driven-development, rdd, repl, lisp, eval, java, python, ruby, programming, convenience 9 | ;; Repo: https://github.com/alhassy/repl-driven-development 10 | ;; Homepage: http://alhassy.com/repl-driven-development 11 | 12 | ;; This program is free software; you can redistribute it and/or modify 13 | ;; it under the terms of the GNU General Public License as published by 14 | ;; the Free Software Foundation, either version 3 of the License, or 15 | ;; (at your option) any later version. 16 | 17 | ;; This program is distributed in the hope that it will be useful, 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 | ;; GNU General Public License for more details. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with this program. If not, see . 24 | 25 | ;;; Commentary: 26 | 27 | ;; This library provides the Emacs built-in “C-x C-e” behaviour for 28 | ;; arbitrary languages, provided they have a REPL shell command. 29 | ;; 30 | ;; ============================Benefits=============================== 31 | ;; 32 | ;; Whenever reading/refactoring some code, if you can make some of it 33 | ;; self-contained, then you can immediately try it out! No need to 34 | ;; load your entire program; nor copy-paste into an external REPL. The 35 | ;; benefits of Emacs' built-in “C-x C-e” for Lisp, and Lisp's Repl 36 | ;; Driven Development philosophy, are essentially made possible for 37 | ;; arbitrary languages (to some approximate degree, but not fully). 38 | ;; 39 | ;; Just as “C-u C-x C-e” inserts the resulting expression at the 40 | ;; current cursour position, so too all repl-driven-development 41 | ;; commands allow for a C-u prefix which inserts the result. 42 | ;; This allows for a nice scripting experience where results 43 | ;; are kept for future use. 44 | ;; 45 | ;; 46 | ;; ===============================Official Manual======================== 47 | ;; 48 | ;; See http://alhassy.com/repl-driven-development 49 | ;; 50 | ;; “C-h o repl-driven-development” also has extensive docs, 51 | ;; via a JavaScript server example. 52 | ;; 53 | ;; ===============================Mini-Tutorial========================== 54 | ;; 55 | ;; Often, while reading a README file, we will (1) copy a shell command, 56 | ;; (2) open a terminal, and (3) paste the shell command to run it. 57 | ;; We can evaluate arbitrary regions in a shell in one step via “C-x C-t” 58 | ;; with: 59 | ;; 60 | ;; (repl-driven-development [C-x C-t] "bash") 61 | ;; 62 | ;; For example, execute “C-x C-t” anywhere on each line below and see results in an 63 | ;; overlay, right by your cursor. 64 | ;; 65 | ;; echo "It is $(date) and I am at $PWD, my name is $(whoami) and I have: $(ls)" 66 | ;; 67 | ;; say "My name is $(whoami) and I like Emacs" 68 | ;; 69 | ;; Notice as each line is sent to the Bash process, the line is highlighted briefly in yellow. 70 | ;; Moreover, you can hover over the text to see a tooltip with the resulting shell output. 71 | ;; Finally, if you invoke “C-h k C-x C-t” you get help about this new “C-x C-t” command, 72 | ;; such as inserting results at point via “C-u C-x C-t” or to reset/refresh the current 73 | ;; Bash process with “C-u -1 C-x C-t”. 74 | ;; 75 | ;; This also works for any command-line REPL; for example, for Python: 76 | ;; 77 | ;; (repl-driven-development [C-x C-p] "python3") 78 | ;; 79 | ;; Then, we can submit the following Python snippets with “C-x C-p” on each line. 80 | ;; 81 | ;; sum([1, 2, 3, 4]) 82 | ;; 83 | ;; list(map(lambda i: 'Fizz'*(not i%3)+'Buzz'*(not i%5) or i, range(1,101))) 84 | ;; 85 | ;; These work fine, however there are some shortcomings of this REPL. 86 | ;; For example, echoing results could be prettier and it doesn't handle 87 | ;; multi-line input very well. You can address these issues using the various 88 | ;; hooks / keyword arguments of the “repl-driven-development” macro. 89 | ;; 90 | ;; However, this package comes with preconfigured REPLS for: python, terminal, 91 | ;; java, javascript. 92 | ;; 93 | ;; Simply use the name of these configurations: 94 | ;; 95 | ;; (repl-driven-development [C-x C-p] python) 96 | ;; 97 | ;; Now we can submit the following, with “C-x C-p”, with no issues: 98 | ;; 99 | ;; def square(x): 100 | ;; return x * x 101 | ;; 102 | ;; square(5) 103 | ;; 104 | ;; Since these new REPL commands are just Emacs functions, we can use 105 | ;; several at the time, alternating between them. For example: 106 | ;; 107 | ;; ;; C-x C-e on the next two lines 108 | ;; (repl-driven-development [C-x C-t] terminal) 109 | ;; (repl-driven-development [C-x C-p] python) 110 | ;; 111 | ;; echo Hello... > /tmp/o # C-x C-t here 112 | ;; 113 | ;; print(open("/tmp/o").read()) # C-x C-p here 114 | ;; 115 | ;; echo ...and bye >> /tmp/o # C-x C-t again 116 | ;; 117 | ;; print(open("/tmp/o").read()) # C-x C-p again 118 | ;; 119 | ;; Let's conclude with a GUI example in Java. 120 | ;; 121 | ;; ;; Set “C-x C-j” to evaluate Java code in a background REPL. 122 | ;; (repl-driven-development [C-x C-j] "jshell") 123 | ;; 124 | ;; // Select this Java snippet, then press “C-x C-j” to evaluate it 125 | ;; import javax.swing.*; 126 | ;; JOptionPane.showMessageDialog(new JFrame(){{setAlwaysOnTop(true);}}, "Super nice!") 127 | ;; 128 | ;; We can use a preconfigured Java REPL, to remove the annoying “jshell>” prompt 129 | ;; from overlay echos, handle multi-line input, and more. 130 | ;; 131 | ;; (repl-driven-development [C-x C-j] java) 132 | ;; 133 | ;; // REPL result values are shown as overlays: 134 | ;; // See a list of 23 numbers, which are attached as a tooltip to this text. 135 | ;; IntStream.range(0, 23).forEach(x -> System.out.println(x)) 136 | ;; 137 | ;; For more documentation, and examples, 138 | ;; see http://alhassy.com/repl-driven-development 139 | ;; 140 | ;;; Code: 141 | 142 | ;; TODO[Low Priority]: Implement pretty printing for Python. 143 | ;; TODO[Low Priority]: Implement a simple Read Protocol for JS. (eg JSON.parse) 144 | ;; TODO: terminal-eval-insert-last-output, 145 | ;; terminal-eval-copy-last-output-to-clipboard, 146 | ;; terminal-eval-show-last-input-and-output 147 | ;; [An Org mode buffer with the last-input and last output, headings] 148 | ;; TODO: Add precondition checks to each method. 149 | ;; (cl-assert (symbolp repl)) 150 | ;; (cl-assert (stringp (rdd@ repl input))) 151 | ;; TODO: Make eval-last-sexp, but allow rdd macro to consume key :backward-sexp. 152 | ;; For example, in Java backward-sexp is not that helpful, whereas 153 | ;; M-a in java-mode is bound to the useful c-beginning-of-statement. 154 | ;; TODO: Make *-eval-expression 155 | 156 | ;;; requires and package preamble 157 | 158 | (require 's) ;; “The long lost Emacs string manipulation library” 159 | (require 'f) 160 | (require 'dash) ;; “A modern list library for Emacs” 161 | (require 'cl-lib) ;; New Common Lisp library; ‘cl-???’ forms. 162 | (require 'eros) ;; Simple Emacs Overlays 163 | (require 'bind-key) ;; Bind keys 164 | (require 'lf) ;; Template strings with lf-string 165 | ;; [lf requires nil lexical binding!] 166 | (require 'peg) ;; Parsing expression grammars 167 | (require 'json) 168 | (require 'seq) 169 | (require 'devdocs) 170 | 171 | (defconst repl-driven-development-version (package-get-version)) 172 | (defun repl-driven-development-version () 173 | "Print the current `repl-driven-development' version in the minibuffer." 174 | (interactive) 175 | (message repl-driven-development-version)) 176 | 177 | (defmacro rdd@ (name property) 178 | "Get/set PROPERTY under namespace NAME. 179 | 180 | Usage: 181 | (rdd@ \"foo\" name) ;; ⇒ nil 182 | (setf (rdd@ \"foo\" name) 'Jasim) 183 | (rdd@ \"foo\" name) ;; ⇒ 'Jasim" 184 | `(get (intern (format "%s" ,name)) (quote ,property))) 185 | 186 | ;;; defvars 187 | 188 | (defvar repl-driven-development-echo-duration 5 189 | "Amount of seconds to show the result overlay.") 190 | 191 | (defvar repl-driven-development-echo-output-in-modeline nil 192 | "In addition to the overlays, should REPL output be emitted in the modeline? 193 | 194 | You can always use `C-h e' to see output in the *Messages* buffer.") 195 | 196 | ;;; main entry point 197 | 198 | ;;;###autoload 199 | (cl-defmacro repl-driven-development 200 | (keys cli 201 | &key (prompt ">") docs (init "") (blink ''pulsar-yellow) name 202 | (input-rewrite-fn ''identity) (echo-rewrite-fn ''identity)) 203 | "Make Emacs itself a REPL for your given language of choice. 204 | 205 | Suppose you're exploring a Python/Ruby/Java/JS/TS/Haskell/Lisps/etc 206 | API, or experimenting with an idea and want immediate feedback. 207 | You could open a terminal and try things out there; with no editor 208 | support, and occasionally copy-pasting things back into your editor 209 | for future use. Better yet, why not use your editor itself as a REPL. 210 | 211 | Implementation & behavioural notes can be found in the JavaScript 212 | Example below. 213 | 214 | ###################################################################### 215 | ### JavaScript Example ---Basic usage, and a minimal server ########## 216 | ###################################################################### 217 | 218 | ;; C-x C-j now evaluates arbitrary JavaScript code 219 | (repl-driven-development [C-x C-j] \"node\") 220 | 221 | That's it! Press “C-x C-e” on the above line so that “C-x C-j” 222 | will now evaluate a selection, or the entire line, as if it were 223 | JavaScript code. ⟦Why C-x C-j? C-x C-“e” for Emacs Lisp code, and C-x 224 | C-“j” for JavaScript code!⟧ For instance, copy-paste the 225 | following examples into a JS file ---or just press “C-x C-j” to 226 | evaluate them! 227 | 228 | 1 + 2 // ⮕ 3 229 | 1 + '2' // ⮕ '12' 230 | let me = {name: 'Jasim'}; Object.keys(me) // ⮕ ['name'] 231 | me.doesNotExist('whoops') // ⮕ Uncaught TypeError 232 | [ ...Array(45).keys() ] // ⮕ Multi-line overlay of 0..44 233 | 234 | All of these results are echoed inline in an overlay, by default. 235 | Moreover, there is a *REPL* buffer created for your REPL so you 236 | can see everything you've sent to it, and the output it sent 237 | back. This is particularly useful for lengthy error messages, 238 | such as those of Java, which cannot be rendered nicely within an 239 | overlay. 240 | 241 | How this works is that Emacs spawns a new “node” process, then 242 | C-x C-j sends text to that process. Whenever the process emits 243 | any output ---on stdout or stderr--- then we emit that to the 244 | user via an overlay. 245 | 246 | Finally, “C-h k C-x C-j” will show you the name of the function 247 | that is invoked when you press C-x C-j, along with minimal docs. 248 | 249 | A useful example would be a minimal server, and requests for it. 250 | 251 | // First get stuff with C-x C-e: 252 | // (async-shell-command \"npm install -g express axios\") 253 | 254 | let app = require('express')() 255 | let clicked = 1 256 | app.get('/hi', (req, res) => res.send(`Hello World × ${clicked++}`)) 257 | 258 | let server = app.listen(3000) 259 | // Now visit http://localhost:3000/hi a bunch of times! 260 | 261 | // Better yet, see the output programmatically... 262 | let axios = require('axios') 263 | // Press C-x C-j a bunch of times on the following expression ♥‿♥ 264 | console.log((await axios.get('http://localhost:3000/hi')).data) 265 | 266 | // Consider closing the server when you're done with it. 267 | server.close() 268 | 269 | Just as “Emacs is a Lisp Machine”, one can use “VSCodeJS” to use 270 | “VSCode as a JS Machine”. 271 | See http://alhassy.com/vscode-is-itself-a-javascript-repl. 272 | 273 | ###################################################################### 274 | ### Description of Arguments ######################################### 275 | ###################################################################### 276 | 277 | - KEYS [Vector]: A vector such as [C-x C-p] that declares the keybindings for 278 | the new REPL evaluator. 279 | 280 | - CLI [String|Symbol]: A string denoting the terminal command to start your 281 | repl; you may need an “-i” flag to force it to be interactive even though 282 | we use it from a child process rather than a top-level shell. 283 | 284 | This argument may also be one of the following unquoted symbols: 285 | 286 | java, python, terminal, javascript 287 | 288 | These are preconfigured REPLs; e.g., see the docs of 289 | function `repl-driven-development-preconfiguration:python'. 290 | 291 | - PROMPT [Regular Expression]: 292 | What is the prompt that your REPL shows, e.g., “>”. 293 | We try to ignore showing it in an overlay that would otherwise hide 294 | useful output. 295 | 296 | - DOCS [String]: A space-seperated string denoting a list of language 297 | documents you'd like to associate with your repl. 298 | Invoking your repl with “C-u C-u” will show the documentation 299 | of the word at point. This is done using `devdocs'. 300 | 301 | For example, 302 | (repl-driven-development [C-x C-j] \"node\" :docs \"javascript express\") 303 | Would allow us to invoke “C-u C-u C-x C-j” with the cursor on the 304 | word, say, “listen” and we'll see some useful docs (along with 305 | example uses) of this Express library method “listen”. 306 | 307 | Visit https://devdocs.io/ to see the list of documented languages 308 | and libraries. 309 | 310 | - INIT [String | List]: Any initial code you'd like your 311 | repl to be initiated with. For example, imports of standard libraries 312 | is probably something you'd always like to have on-hand; or perhaps 313 | some useful variables/declarations/functions. 314 | 315 | - BLINK [Face]: Any face with a background. It is used to briefly highlight 316 | the current line that is being sent to the REPL process. 317 | 318 | - INPUT-REWRITE-FN [1-arg function]: A function called to rewrite text 319 | before submitting it to the repl. For example usage, see the docs of 320 | function `repl-driven-development-preconfiguration:python'. 321 | 322 | - ECHO-REWRITE-FN [1-arg function]: A function called to rewrite repl 323 | output before echoing it to the user. For example usage, see the docs 324 | of function `repl-driven-development-preconfiguration:python'. 325 | 326 | Intentionally meant for human friendly pretty-printing, not for 327 | a READ protocol. Those serve different goals. 328 | The default READ protocol is this echo-rewrite-fn. 329 | Enter “M-x .*-read” to see the docs of the READ protocol 330 | for any REPL defined with this macro. 331 | 332 | - NAME [Symbol]: The name of the function associated to the keybinding 333 | KEYS. By default, the name is “CLI-eval”. This is used to namespace 334 | almost all other functions created by this macro. 335 | 336 | ### Misc Remarks ##################################################### 337 | For more documentation, and examples, 338 | see URL `http://alhassy.com/repl-driven-development'. 339 | 340 | VSCode has a similar utility for making in-editor REPLs, by the 341 | same author: See URL `http://alhassy.com/making-vscode-itself-a-java-repl'." 342 | (if (symbolp cli) 343 | `(or (ignore-errors (funcall (intern (format "repl-driven-development-preconfiguration:%s" (quote ,cli))) ,keys)) 344 | (message "It seems there is no preconfigured setup for “%s”; consider using a string with the name of a CLI process." (quote ,cli))) 345 | `(-let* (((repl . args) (s-split " " ,cli))) 346 | ;; (repl-fun-name string) 347 | (setf (rdd@ repl cmd) repl) ;; String 348 | (setf (rdd@ repl prompt) ,prompt) ;; String (Regular Expression) 349 | (setf (rdd@ repl keybinding) 350 | (s-join " " (mapcar #'pp-to-string ,keys))) ;; String 351 | ;; String: Space separated list 352 | (setf (rdd@ repl docs) ,docs) 353 | ;; The following is set to “nil” so that consecutive 354 | ;; calls to this method, but possibly with differing docs, 355 | ;; results in those docs being installed as needed. 356 | (setf (rdd@ repl docs-installed?) nil) 357 | ;; Used to avoid scenarios where input is echoed thereby 358 | ;; accidentally treating it as a repl output 359 | (setf (rdd@ repl input) "") ;; String 360 | (setf (rdd@ repl input/start) 0) 361 | (setf (rdd@ repl input/end) 0) 362 | (setf (rdd@ repl input-rewrite-fn) ,input-rewrite-fn) 363 | (setf (rdd@ repl echo-rewrite-fn) ,echo-rewrite-fn) 364 | (setf (rdd@ repl fun-name) 365 | (or ,name (intern (format "%s-eval" (rdd@ repl cmd))))) 366 | 367 | (setf (rdd@ repl init) ,init) 368 | (cl-assert (or (stringp ,init) (listp ,init))) 369 | (when (listp ,init) (setq ,init (s-join "\n" ,init))) 370 | (cl-assert (stringp ,init)) 371 | 372 | (setf (rdd@ repl blink) ,blink) 373 | ;; Identifier "repl-driven-development" is made unique by 374 | ;; start-process. 375 | (setf (rdd@ repl process) 376 | (apply #'start-process "repl-driven-development" 377 | nil repl args)) 378 | 379 | (eval (repl-driven-development--make-eval-function repl)) 380 | 381 | (process-send-string (rdd@ repl process) ,init) 382 | (process-send-string (rdd@ repl process) "\n") 383 | 384 | ;; Callback: Write the actual output to the REPL buffer 385 | ;; and emit overlay. 386 | (set-process-filter 387 | (rdd@ repl process) 388 | (repl-driven-development--main-callback (intern repl))) 389 | 390 | ;; Return the REPL symbol to the user (whose symbol-plist can be used 391 | ;; to get various information!) 392 | repl))) 393 | 394 | ;;; main-callback: insert or echo 395 | (defun repl-driven-development--main-callback (repl) 396 | "Return the callback that works on REPL." 397 | `(lambda (process output) 398 | ;; This is done to provide a richer, friendlier, interaction. 399 | ;; ^M at the end of line in Emacs is indicating a carriage return (\r) 400 | ;; followed by a line feed (\n). 401 | (setq output (s-trim (s-replace-regexp ,(rdd@ repl prompt) "" 402 | (s-replace "\r\n" "" output)))) 403 | ;; Output is always non-empty 404 | (unless (s-blank? (s-trim output)) 405 | (setf (rdd@ (quote ,repl) output) output)) 406 | 407 | (repl-driven-development--insert-or-echo (quote ,repl) output))) 408 | 409 | (defun repl-driven-development--insert-or-echo (repl output) 410 | "If there's a C-u, then insert the OUTPUT; else echo it in overlay. 411 | 412 | The echo only happens when OUTPUT differs from REPL's input." 413 | (cl-assert (stringp output)) 414 | (cl-assert (symbolp repl)) 415 | (cl-assert (stringp (rdd@ repl input))) 416 | (pcase current-prefix-arg 417 | ('(4) (unless (equal output (s-trim (rdd@ repl input))) 418 | (insert " " (funcall 419 | (intern (format "%s-read" 420 | (rdd@ repl fun-name))) output)))) 421 | ;; All other prefixes are handled by repl-fun-name, above. 422 | (_ 423 | ;; Show output as an overlay at the current cursor position 424 | 425 | ;; For some reason, sometimes Emacs emit the input as part of the 426 | ;; output, so let's chop it off. For example, I've noticed this 427 | ;; with Python on *my* machine, but others have noticed it with Java[0], 428 | ;; but I couldn't reproduce this with Java. 429 | ;; [0] https://github.com/alhassy/repl-driven-development/issues/5 430 | (setq output (s-chop-prefix (rdd@ repl input) output)) 431 | 432 | ;; ﴾ Since eros is intended to be used with ELisp, not arbitrary langs, 433 | ;; it does some sexp look-about, which may not mix well with, say, JS 434 | ;; arrow functions, so we freeze such movements, locally. ﴿ 435 | (setq output 436 | (apply (rdd@ repl echo-rewrite-fn) 437 | (list (repl-driven-development--ignore-ansi-color-codes 438 | output)))) 439 | (unless (s-blank? (s-trim output)) 440 | (unless (equal output (s-trim (rdd@ repl input))) 441 | ;; Tooltips ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 442 | (mapc #'delete-overlay 443 | (overlays-at (rdd@ repl input/start))) 444 | (let ((overlay (make-overlay (rdd@ repl input/start) 445 | (rdd@ repl input/end)))) 446 | (overlay-put overlay 'help-echo output)) 447 | ;; Messages ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 448 | ;; I want “C-h e” to show eval result ---just as “C-x C-e” does. 449 | (let ((inhibit-message 450 | (not repl-driven-development-echo-output-in-modeline))) 451 | (message "﴾%s﴿⇒ %s" (rdd@ repl fun-name) output)) 452 | ;; Overlays ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 453 | (require 'eros) 454 | (cl-letf (((symbol-function 'backward-sexp) (lambda (&rest _) 0))) 455 | (eros--make-result-overlay output 456 | :format " ⮕ %s" 457 | :duration repl-driven-development-echo-duration))))))) 458 | 459 | ;;; make-eval-function 460 | (defun repl-driven-development--make-eval-function (repl) 461 | "Constructs code denoting a function that sends a region to a REPL process." 462 | `(progn 463 | ;; TODO: Make a defun with a callback for repl testing a la 464 | ;; set-process-filter. 465 | 466 | ;; restart repl, [then send to repl --does not work since REPLs take a sec 467 | ;; to load. That's OK, not a deal-breaker!] 468 | (defun ,(intern (format "%s-restart" (rdd@ repl fun-name))) () 469 | "Restart the REPL process." 470 | (interactive) 471 | (kill-buffer (process-buffer (rdd@ ,repl process))) 472 | (repl-driven-development (rdd@ ,repl keybinding) 473 | (rdd@ ,repl cmd) 474 | :prompt (rdd@ ,repl prompt) 475 | :docs (rdd@ ,repl docs) 476 | :init (rdd@ ,repl init) 477 | :blink (rdd@ ,repl blink))) 478 | 479 | (defun ,(intern (format "%s-docs-at-point" (rdd@ repl fun-name))) () 480 | "Documentation at point." 481 | (interactive) 482 | (unless (rdd@ ,repl docs-installed?) 483 | (repl-driven-development--install-any-not-yet-installed-docs 484 | (rdd@ ,repl docs)) 485 | (setf (rdd@ ,repl docs-installed?) t)) 486 | (repl-driven-development--docs-at-point (rdd@ ,repl docs))) 487 | 488 | (defun ,(intern (format "%s-buffer" (rdd@ repl fun-name))) () 489 | ,(format "Execute the accessible portion of current buffer as %s code. 490 | 491 | You can use \\[narrow-to-region] to limit the part of buffer to be evaluated." 492 | (rdd@ repl cmd)) 493 | (interactive) 494 | (mark-whole-buffer) 495 | (call-interactively (rdd@ ,repl fun-name))) 496 | 497 | (defun ,(intern (format "%s-defun" (rdd@ repl fun-name))) () 498 | ,(format "Evaluate innermost defun at point. 499 | 500 | This method is only useful in a buffer whose major mode supports “%s” code. 501 | 502 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 503 | ﴾ Top-Level Definitions, or “Defuns” ﴿ 504 | 505 | In Emacs, a major definition at the top level in a buffer, such 506 | as a function or class, is called a “defun”. 507 | 508 | Just as “M-a, M-e, M-h” move to the start of a paragraph, end of a 509 | paragraph, and select a paragraph, so to the “C-M-𝓍” forms are for 510 | defuns: 511 | 512 | ⇒ \\[beginning-of-defun] and \\[end-of-defun] to move to the start/end of a defun. 513 | ⇒ \\[mark-defun] to highlight the current defun/class 514 | ↣ If mark is active, it extends the region until the end of the next defun. 515 | ↣ This selects the current class, or “topmost defun”, if point is outside 516 | a function but inside a class (e.g., in Java). 517 | ⇒ \\[imenu] gives you an interactive menu to see all defuns and to jump to them. 518 | 519 | Note: Programming modes generally bind “M-a” and “M-e” to move forward and 520 | backward between semantic units of code, e.g., statements. 521 | 522 | Useful tips: 523 | 1. \\[comment-dwim] inserts a new comment, or toggles commenting a selection. 524 | 2. \\[mark-word] selects the current word; repeated calls select more words. 525 | 526 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 527 | ﴾ Subexpressions, or “Sexps” ﴿ 528 | 529 | Emacs calls “sexp” any subexpression that is enclosed in matching delimiters, 530 | such as (parentheses), [brackets], {braces}, \"quotes\", or is a number, or a 531 | single identifer. 532 | 533 | We can use \\[backward-sexp], \\[forward-sexp], \\[kill-sexp], \\[mark-sexp] / “C-M-SPC” to move backward, forward, kill, 534 | and highlight sexps. 535 | 536 | For example, in front of a {braced code fragment}, press \\[mark-sexp] to highlight 537 | that entire fragment. In front of a word, \\[mark-sexp] highlights just that word." 538 | (rdd@ repl cmd)) 539 | (interactive) 540 | (mark-defun) (call-interactively (rdd@ ,repl fun-name))) 541 | 542 | (defun ,(intern (format "%s-read" (rdd@ repl fun-name))) (str) 543 | "Read STR into code executable by the REPL. 544 | 545 | This is intended to result in executable code, from a possibly prettified string. 546 | 547 | The read protocol is the “R” of “REPL”; it is fundamental if you want to insert 548 | the result of an evaluation into the current buffer, say, for forming tests, 549 | and so require the inserted text to also be executable. 550 | 551 | By default, this method returns the human pretty-printing that the overlay echo 552 | mechanism uses. 553 | 554 | YOU SHOULD REDEFINE THIS METHOD, TO BE AN APPROPRIATE READ PROTOCOL. 555 | (If you care about how inserted code looks.) 556 | 557 | For an example, see `repl-driven-development--java-read'." 558 | (interactive "sRead: ") 559 | (apply (rdd@ ,repl echo-rewrite-fn) (list str))) 560 | 561 | (defun ,(intern (format "%s-string" (rdd@ repl fun-name))) (string) 562 | ,(format "Send STRING to the %s process, followed by a newline, to be executed. 563 | 564 | To submit a region, use `%s'." 565 | (rdd@ repl cmd) 566 | (rdd@ repl fun-name)) 567 | (setf (rdd@ ,repl input) string) 568 | (process-send-string (rdd@ ,repl process) 569 | (apply (rdd@ ,repl input-rewrite-fn) (list string))) 570 | (process-send-string (rdd@ ,repl process) "\n")) 571 | 572 | (defun 573 | ,(intern (format "%s-display-output" (rdd@ repl fun-name))) 574 | () 575 | "Show most recent REPL result. With C-u prefix, result is shown in its \ 576 | own buffer. 577 | 578 | If you see “...” then chances are that you have exceeded the default truncation 579 | threshold for your REPL. Consider increasing the threshold, if possible, by 580 | reading the docs of your REPL. For an example, see 581 | `repl-driven-development-preconfiguration:java'." 582 | (interactive) 583 | (if (not current-prefix-arg) 584 | (display-message-or-buffer (rdd@ ,repl output)) 585 | (switch-to-buffer (format "*%s-most-recent-result*" 586 | (rdd@ ,repl fun-name))) 587 | (insert (rdd@ ,repl output)))) 588 | 589 | (bind-key* 590 | (rdd@ ,repl keybinding) 591 | (defun ,(rdd@ repl fun-name) (region-beg region-end) 592 | ,(repl-driven-development--make-eval-function-docstring repl) 593 | (interactive "r") 594 | 595 | (require 'pulsar) 596 | (-let [pulsar-face (rdd@ ,repl blink)] 597 | (pulsar-mode +1) 598 | (pulsar-pulse-line)) 599 | 600 | (pcase current-prefix-arg 601 | (0 (,(intern (format "%s-display-output" 602 | (rdd@ repl fun-name))))) 603 | (-1 (,(intern (format "%s-restart" (rdd@ repl fun-name))))) 604 | ;; ('(4) (insert " " output)) ;; C-u ;; handled when we actually have 605 | ;; the output; see the process filter below 606 | ('(16) ;; C-u C-u ⇒ documentation lookup 607 | (,(intern (format "%s-docs-at-point" (rdd@ repl fun-name))))) 608 | (_ 609 | (when (called-interactively-p 'interactive) 610 | (if (use-region-p) 611 | (deactivate-mark) 612 | (beginning-of-line) 613 | (setq region-beg (point)) 614 | (end-of-line) 615 | (setq region-end (point)))) 616 | (setf (rdd@ ,repl input/start) region-beg) 617 | (setf (rdd@ ,repl input/end) region-end) 618 | (,(intern (format "%s-string" (rdd@ repl fun-name))) 619 | (s-trim-left (buffer-substring-no-properties 620 | region-beg 621 | region-end))))))))) 622 | 623 | ;;; docs-at-point 624 | 625 | (defun repl-driven-development--install-any-not-yet-installed-docs (docs) 626 | "Install any not-yet-installed DOCS; return a List of the installed \ 627 | docs. 628 | 629 | DOCS is a space seperated sequence of identifiers for dev-docs." 630 | (when docs 631 | (cl-assert (stringp docs)) 632 | (setq docs (--reject (s-blank? it) (s-split " " docs))) 633 | (cl-assert (listp docs)) 634 | (-let [installed (mapcar #'f-base (f-entries devdocs-data-dir))] 635 | (mapc (lambda (it) (unless (member it installed) 636 | (devdocs-install (list (cons 'slug it))))) docs)) 637 | docs)) 638 | 639 | (defun repl-driven-development--docs-at-point (docs) 640 | "Lookup documentation at point using the given DOCS." 641 | ;; Test this by writing a word such as “IntStream.range(0, 44)” 642 | ;; then M-: (repl-driven-development--docs-at-point '("openjdk~19")) 643 | ;; anywhere on the phrase 644 | ;; 645 | ;; devdocs-lookup will ask to setup current docs when there's a 646 | ;; current-prefix, so we null it. 647 | ;; If user does have it setup, we want to temporarily change its value for 648 | ;; use with the current repl. 649 | (cl-assert (stringp docs)) 650 | (let ((devdocs-history nil) 651 | (current-prefix-arg nil) 652 | (devdocs-current-docs (s-split " " docs)) 653 | (word (or (thing-at-point 'symbol) ""))) 654 | ;; (devdocs-lookup nil word) ⇒ Quits abruptly when keyword is not a 655 | ;; valid candidate! 656 | (minibuffer-with-setup-hook 657 | `(lambda () (insert ,word)) 658 | (call-interactively #'devdocs-lookup)))) 659 | 660 | ;;; docstrings 661 | 662 | (defun repl-driven-development--make-eval-function-docstring (repl) 663 | "Make the docstring for a REPL function working with command CLI." 664 | (let ((keys (rdd@ repl keybinding)) 665 | (cmd (rdd@ repl cmd))) 666 | (setq repl (rdd@ repl fun-name)) 667 | (lf-string 668 | "Executes the selected region, if any or otherwise the entire current line, 669 | and evaluates it with the command-line tool “${cmd}”. 670 | 671 | Output is shown as an overlay at the current cursor position. 672 | It is shown for `repl-driven-development-echo-duration' many seconds. 673 | 674 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 675 | ﴾ Familiar Workflow ﴿ 676 | 677 | You can execute arbitrary Lisp anywhere by pressing \\[eval-last-sexp], you can 678 | insert the result with “C-u C-x C-e”, and see the output echoed in the 679 | mode-line and in the *Messages* buffer with \\[view-echo-area-messages]. 680 | Likewise, you can execute “${cmd}” code by pressing “${keys}”, insert output 681 | with “C-u ${keys}”, and see the output echoed near your cursor and in the 682 | *Messages* buffer with “C-h e”. 683 | 684 | Other familiar functions include `${repl}-defun', `${repl}-buffer', and `${repl}-string'. 685 | 686 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 687 | ﴾ C-u ${keys} ≈ Insert result ﴿ 688 | 689 | With a “C-u” prefix, the output is inserted at point 690 | (and not echoed in an overlay). 691 | 692 | Since ${repl} may pretty-print its output, inserting it may result in 693 | non-executable code. If you want executable code, you must specify 694 | how pretty-printed output must be converted into ${repl}-executable code. 695 | Do so by redefining `${repl}-read'. 696 | 697 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 698 | ﴾ C-u 0 ${keys} ≈ `${repl}-display-output' ﴿ 699 | 700 | Sometimes it may be useful to look at a large output in a dedicated buffer. 701 | However, the output of a command is also attached to the input via a 702 | tooltip: Hover to see it! See also `tooltip-delay'. 703 | Moreover, “C-h e” shows you the output in the *Messages* buffer. 704 | See also `repl-driven-development-echo-output-in-modeline'. 705 | 706 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 707 | ﴾ C-u C-u ${keys} ≈ `${repl}-docs-at-point' ﴿ 708 | 709 | With a “C-u C-u” prefix, documentation is looked-up for the word at point. 710 | 711 | This is done using `devdocs', and so the documentation generally provides 712 | example uses as well. Visit https://devdocs.io/ to see the list of documented 713 | languages and libraries. 714 | 715 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 716 | ﴾ C-u -1 ${keys} ≈ `${repl}-restart' ﴿ 717 | 718 | In the event you've messed-up your REPL, starting from a blank slate may be 719 | helpful. 720 | 721 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 722 | ﴾ Implementation Notes ﴿ 723 | 724 | The interactive method is asynchronous: Whenever you send text for 725 | evaluation, you immediately regain control in Emacs; you may send more text 726 | and it will be queued for evaluation. For example, evaluating a sleep 727 | command for 3 seconds does not block Emacs. 728 | 729 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 730 | ﴾ See also ﴿ 731 | 732 | See `repl-driven-development' for more useful docs. 733 | 734 | See URL `http://www.alhassy.com/repl-driven-development' to learn more about 735 | RDD see examples and many gifs."))) 736 | 737 | (defun repl-driven-development--ignore-ansi-color-codes (string-with-codes) 738 | "Ignore ANSI color codes in STRING-WITH-CODES." 739 | (with-temp-buffer 740 | (insert string-with-codes) 741 | (ansi-color-apply-on-region (point-min) (point-max)) 742 | (buffer-string))) 743 | 744 | ;;; pre-configured repls 745 | (defun repl-driven-development-preconfiguration:terminal (keys) 746 | "A Bash REPL configuration, bound to keybinding KEYS." 747 | ;; Disable “bash” intro message 748 | (setenv "BASH_SILENCE_DEPRECATION_WARNING" "1") 749 | (repl-driven-development 750 | keys 751 | "bash" 752 | :name 'terminal-eval 753 | :docs "bash" 754 | :prompt "^bash.*?\\$" 755 | :init "echo \"Enjoy the Terminal with Emacs (。◕‿◕。)\"")) 756 | 757 | (defun repl-driven-development-preconfiguration:applescript (keys) 758 | "An AppleScript REPL configuration, bound to keybinding KEYS. 759 | 760 | ✔ Supports multi-line output. 761 | ⚠ Each snippet must be a self-contained program; i.e., snippets do not share state." 762 | ;; Disable “bash” intro message 763 | (setenv "BASH_SILENCE_DEPRECATION_WARNING" "1") 764 | (repl-driven-development 765 | keys "bash" 766 | :name 'applescript-eval 767 | :blink 'pulsar-blue 768 | :prompt "\\(bash.*?\\$\\)\\|>" 769 | :init "echo \"Enjoy AppleScript with Emacs (。◕‿◕。)\" 770 | while true; do 771 | read input 772 | osa_script=\"\" 773 | while [ \"$input\" != \";;\" ]; do 774 | osa_script=\"$osa_script$input\"$'\n' 775 | read input 776 | done 777 | echo $(osascript -e \"$osa_script\") && echo \"Done (◕‿◕)\" 778 | done" 779 | :input-rewrite-fn (lambda (in) (format "%s\n;;" in)))) 780 | 781 | (defun repl-driven-development-preconfiguration:javascript (keys) 782 | "A NodeJS REPL configuration, bound to keybinding KEYS. 783 | 784 | ✔ Defines variable “axios”, if the library is installed, 785 | e.g., “npm i -g axios” 786 | 787 | ✔ Docs: Vanialla JS, Web APIs (e.g., “fetch”), Axios, Express. 788 | " 789 | (setenv "NODE_DISABLE_COLORS" "1") ;; So we don't display funky colour code upon insert 790 | (repl-driven-development 791 | keys "node" 792 | :name 'javascript-eval 793 | :prompt ">" 794 | :docs "javascript dom axios express" 795 | :input-rewrite-fn 796 | #'repl-driven-development--strip-out-C-style-comments&newlines 797 | ;; Load “axios”, if it's present. 798 | ;; By keeping everything in “one line”, there is only one output emitted. 799 | ;; Namely, I don't want users to see the output of loading axios. 800 | :init (s-join ";" '("var axios = null" 801 | "try { axios = require('axios') } catch (error) { }" 802 | "shell = command => require('child_process').execSync(command).toString().trim()" 803 | ;; Escape quotes/etc by going to base64 via JS built-in `btoa`, then decode base64 on the Lisp side 804 | "emacs = { eval: sexp => shell(`emacsclient -e '(eval (read (base64-decode-string \"${btoa(sexp)}\")))'`) }" 805 | "\"Enjoy JavaScript with Emacs (。◕‿◕。)!\"")))) 806 | 807 | (defun repl-driven-development--strip-out-C-style-comments&newlines (str) 808 | "Strip out C-style single-line and multi-line comments from STR. 809 | 810 | Do not touch the tokens “https://” nor “http://”." 811 | (let ((http (pp-to-string (gensym))) 812 | (https (pp-to-string (gensym)))) 813 | (thread-last 814 | str 815 | (s-replace-regexp "/\\*.\\*/" "") 816 | (s-replace-regexp "http://" http) 817 | (s-replace-regexp "https://" https) 818 | (s-replace-regexp "//.*$" "") 819 | (s-replace-regexp "\n" "") 820 | (s-replace-regexp http "http://") 821 | (s-replace-regexp https "https://")))) 822 | 823 | (defun repl-driven-development-preconfiguration:python (keys) 824 | "A Python REPL configuration, bound to keybinding KEYS. 825 | 826 | This configuration fixes the following shortcomings of the default Python CLI 827 | repl: 828 | 829 | ❌ The Python repl abruptly terminates def|class definitions when there is an 830 | empty new line in their definition. 831 | ✔ This configuration strips out all empty newlines. 832 | 833 | ❌ The Python repl requires an extra new line after a def|class definition to 834 | confirm that the definition has concluded. 835 | ✔ This configuration automatically adds such extra new lines. 836 | 837 | ❌ The Python repl emits nothing when a def|class declaration is submitted. 838 | ✔ This configuration emits a “Defined ⋯” message, along with the declaration's 839 | body. 840 | 841 | NOTE: Get a nice IDE experience with “sudo npm install -g pyright”, then 842 | open a Python file and invoke “M-x eglot”." 843 | (repl-driven-development 844 | keys 845 | "python3 -q" ;; “-q” to avoid showing intro message 846 | :prompt ">>>" 847 | :name 'python-eval 848 | :docs "python~3.5" 849 | :blink 'pulsar-red 850 | ;; Remove empty lines: In the middle of a def|class, they abruptly terminate 851 | ;; the def|class! 852 | :input-rewrite-fn (lambda (in) (concat (s-replace-regexp "^\s*\n" "" in) "\n\n\r")) 853 | ;; For some reason, Python (in Emacs shells) emits the input as part of the 854 | ;; output, so let's chop it off. 855 | ;; Default Python repl emits nothing on def|class declarations, 856 | ;; let's change that. 857 | :echo-rewrite-fn 858 | (lambda (echo) 859 | (let* ((input (rdd@ "python3" input)) 860 | (result (s-chop-prefix input echo))) 861 | (cond ((s-starts-with? "def" input) 862 | (s-replace-regexp " *def \\([^(]*\\).*" "Defined “\\1”" input)) 863 | ((s-starts-with? "class" input) 864 | (s-replace-regexp " *class \\([^(:]*\\).*" "Defined “\\1”:" input)) 865 | (t result)))) 866 | :init "print(\"Enjoy Python with Emacs (。◕‿◕。)\")")) 867 | 868 | (defun repl-driven-development-preconfiguration:java (keys) 869 | "A Java REPL configuration, bound to keybinding KEYS. 870 | 871 | ✔ This REPL is aware of all JARs in the ~/.m2/repository; the location of 872 | external JARs installed via the mvn tool. 873 | 874 | ✔ Notify me when imports succeed. 875 | 876 | ✔ Show types of results. 877 | 878 | ✔ JShell has a truncation for outputs exceeding 1k chars, we increase the 879 | threshold to size 40k chars. 880 | 881 | Implementation details below. 882 | 883 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 884 | ﴾ Setting a Classpath ﴿ 885 | 886 | The classpath lets jshell access non-standard-library code. 887 | 888 | % jshell --class-path .:myJar:myOtherJar:dir/to/compiled/classes 889 | 890 | Paths can be to Java Archives (JARs) or to directories of *compiled* class files 891 | ---which must be in a *named package*. 892 | 893 | You also use the '/env' command to set the classpath: 894 | 895 | jshell> /env --class-path myOwnClassPath 896 | | Setting new options and restoring state. 897 | jshell> import my.cool.code.* 898 | 899 | Note that this command resets the execution state, reloading any 900 | current snippets with the new classpath setting (or other 901 | environment setting). 902 | 903 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 904 | ﴾ JShell Feedback Modes ﴿ 905 | 906 | The built-in feedback modes cannot have their 907 | prompt|truncation|format changed, but we can inheirt from a 908 | built-in mode, then change those properties. 909 | 910 | ⟅ ✔ Notify me when imports succeed ⟆ 911 | 912 | In the format, inherited from normal mode, an import doesn't give 913 | any feedback, and the type of a value is not shown. 914 | 915 | import java.beans.* // Submit this twice and both times see nothing :-( 916 | 917 | /set mode myNewMode normal -command 918 | /set feedback myNewMode 919 | /help /set format // There is extensive help on this command 920 | // with /help /set format. 921 | 922 | /set format myNewMode display \"{pre}added import {name}{post}\" \ 923 | import-added 924 | /set format myNewMode display \"{pre}re-added import {name}{post}\" \ 925 | import-modified,replaced 926 | 927 | import java.awt.Graphics // Submit this twice and see two distinct messages 928 | 929 | This is automatically part of the Emacs Java REPL. 930 | 931 | ⟅ ✔ Show types of results ⟆ 932 | 933 | Let's also change the default “var ==> value” output to include the type of the 934 | resulting value. 935 | 936 | // The payload does not indicate the type of this thing, 937 | // I'd like to know what kind of data I'm working with! 938 | Locale.CANADA.getUnicodeLocaleAttributes() // $10 ==> [] 939 | 940 | /set format myNewMode result \"{type} {name} = {value}{post}\" \ 941 | added,modified,replaced-primary-ok 942 | 943 | 2 + 4 // int $11 = 6 944 | String me = \"hello\" // Emits: String me ⇒ \"hello\" 945 | Locale.CANADA.getUnicodeLocaleAttributes() // Set $12 = [] 946 | 947 | ⟅ ✔ Extended Truncation Limit ⟆ 948 | 949 | We can increase the truncation as follows, 950 | this is done automatically for the Java REPL. 951 | 952 | /set truncation myNewMode 40000 953 | IntStream.range(0, 40000).mapToObj(x -> x).toList() 954 | 955 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 956 | ﴾ Useful Reading ﴿ 957 | 958 | - The excellent JShell Tutorial by Robert Field at URL 959 | `https://cr.openjdk.org/~rfield/tutorial/JShellTutorial.html'. 960 | - A Gentle Intro to JShell at URL 961 | `https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Java-JShell-Online-Commands-How-to-Tutorial-Exit'. 962 | 963 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 964 | ﴾ Implementation Notes ﴿ 965 | 966 | We do not use the “--startup JAVASE” option to import all Java SE packages by 967 | default; since the simplest call “List.of(1)” results in an ambiguity error: 968 | java.util.List and java.awt.List match." 969 | (repl-driven-development 970 | keys 971 | ;; enable assertions, and add everything installed, via `mvn', in scope. 972 | (format "jshell --class-path %s --enable-preview -R -ea --feedback silent" 973 | (concat ".:" (shell-command-to-string "find ~/.m2/repository -name \"*.jar\" -type f 2>/dev/null | tr '\n' ':'"))) 974 | :name 'java-eval 975 | :prompt "jshell>" 976 | :input-rewrite-fn 977 | #'repl-driven-development--strip-out-C-style-comments&newlines 978 | :init "/set mode EmacsJavaMode normal -command 979 | /set format EmacsJavaMode display \"{pre}added import {name}{post}\" import-added 980 | /set format EmacsJavaMode display \"{pre}re-added import {name}{post}\" import-modified,replaced 981 | /set format EmacsJavaMode result \"{type} {name} = {value}{post}\" added,modified,replaced-primary-ok 982 | /set truncation EmacsJavaMode 40000 983 | /set feedback EmacsJavaMode 984 | import javax.swing.*; 985 | System.out.println(\"Enjoy Java with Emacs (。◕‿◕。))\")") 986 | 987 | (defalias 'java-eval-read #'repl-driven-development--java-read)) 988 | 989 | ;; TODO Consider using my own overlays, like I do for tooltips, instead of using 990 | ;; eros. Then, for example, I don't need to worry about this truncation 991 | ;; limitation: https://github.com/xiongtx/eros/blob/master/eros.el#L202 992 | ;; Then again, this truncation is fine 993 | ;; (since we have repl-display-output) and eros is lovely. 994 | 995 | ;;; READ Protocol for Java 996 | 997 | (defun repl-driven-development--java-read (a-pretty-printed-record) 998 | "Read executable Java code from pretty-printed record representations. 999 | 1000 | A-PRETTY-PRINTED-RECORD is a a string denoting pretty printed Java output. 1001 | 1002 | If called interactively via M-x, copies executable Java to clipboard; otherwise 1003 | returns the executable Java as a string. 1004 | 1005 | Using PEGs; see URL `https://elpa.gnu.org/packages/peg.html'." 1006 | (interactive "sJava Read: ") 1007 | (let ((result (thread-last 1008 | a-pretty-printed-record ;; Has shape “type name = value”. 1009 | (s-replace-regexp "^[^ ]* [^ ]* = " "") 1010 | repl-driven-development--parse-pretty-printed-java 1011 | repl-driven-development--lisp-to-java))) 1012 | (if (called-interactively-p 'interactive) 1013 | (progn (kill-new result) 1014 | (message "Copied: %s" result)) 1015 | (insert "\n") 1016 | result))) 1017 | 1018 | (defun repl-driven-development--parse-pretty-printed-java (&optional str) 1019 | "If called interactively, run the parser at point. 1020 | If called from Lisp, run the parse on the given string STR. 1021 | 1022 | Return a sexp with info about the parsed item. 1023 | 1024 | ⦉𝒯𝒾𝓅⦊ When actually implementing this parser, I enclosed the 1025 | defun in (bind-key [M-p] ⋯). Now I could quickly iterate on the 1026 | parser: Make a small modification to the parser, have one of my 1027 | test cases in the same buffer, and invoke M-p to parse it. This 1028 | let me see “how far” the parsing got and where it got stuck." 1029 | (interactive) 1030 | (with-peg-rules 1031 | ((expr (or structured-expr literal)) 1032 | (structured-expr (or map list record number)) 1033 | (list (or empty-list non-empty-list)) 1034 | (empty-list "[]" 1035 | `(val -- (list :type :list :items nil))) 1036 | (non-empty-list "[" expr (list (* "," (* [space]) structured-expr)) "]" 1037 | `(first rest -- (list :type :list 1038 | :items (cons first rest)))) 1039 | (map "{" (list (opt payloads)) "}" 1040 | `(entries -- (cons :type (cons :map (cons :entries entries))))) 1041 | (literal (substring (* [a-z A-Z 0-9 " \"-;!#%&'*+,./:;<=>?@[]^_`{|}~"]))) 1042 | (record identifier "[" (opt payloads) "]" 1043 | `(k vs -- (list :type :record 1044 | :name (plist-get k :value) 1045 | :fields vs))) 1046 | (identifier (substring (+ (or [alpha] comma "-" [space]))) 1047 | `(val -- (list :type :string :value val))) 1048 | (comma "," (not (and [space] (+ [alpha]) "="))) 1049 | (payloads (list payload (* (and "," (opt (+ [space])) payload)))) 1050 | (payload identifier "=" (or structured-expr identifier) 1051 | `(k v -- (list :name (plist-get k :value) :value v))) 1052 | (number (substring (and (+ [digit]) (opt (and "." (+ [digit]))))) 1053 | `(val -- (list :type :number :value (string-to-number val))))) 1054 | (if (called-interactively-p 'interactive) 1055 | (peg-run (peg expr) 1056 | #'ignore 1057 | (lambda (args) (insert " ;; ⇒ " 1058 | (pp-to-string (car (funcall args)))))) 1059 | (with-temp-buffer 1060 | (insert str) 1061 | (goto-char (point-min)) 1062 | (let (result) 1063 | ;; peg-run ⇒ Parse with matcher at point, 1064 | ;; & run success|failure function. 1065 | (peg-run (peg expr) #'ignore 1066 | (lambda (args) (setq result (car (funcall args))))) 1067 | result))))) 1068 | 1069 | (defun repl-driven-development--lisp-to-java (data) 1070 | "Convert the given Lisp sexp DATA into a JShell executable snippet." 1071 | (s-replace-regexp 1072 | "\n\\(,\\|)\\)" "\\1" 1073 | (pcase (plist-get data :type) 1074 | (:number (format "%s" (plist-get data :value))) 1075 | (:string (-let [it (plist-get data :value)] 1076 | (if (equal it "null") 1077 | it 1078 | ;; Escapes quotes. 1079 | (pp-to-string it)))) 1080 | (:list (thread-last (plist-get data :items) 1081 | (mapcar #'repl-driven-development--lisp-to-java) 1082 | (s-join ", ") 1083 | (format "List.of(%s)"))) 1084 | ;; Invariant: A :record type have :value in all fields. 1085 | (:record (thread-last 1086 | (plist-get data :fields) 1087 | (--map (repl-driven-development--lisp-to-java (plist-get it :value))) 1088 | (s-join ", ") 1089 | (format "new %s(%s)" (plist-get data :name)))) 1090 | (:map (thread-last 1091 | (plist-get data :entries) 1092 | (--map (format "%s, %s" 1093 | (plist-get it :name) 1094 | (repl-driven-development--lisp-to-java 1095 | (plist-get it :value)))) 1096 | (s-join ", ") 1097 | (format "Map.of(%s)"))) 1098 | (else (error "lisp-to-java: Unknown data type “%s”" else))))) 1099 | 1100 | ;;; navigate-most-recent-result 1101 | 1102 | (cl-defun java-eval-navigate-output (&optional (str (rdd@ 'jshell output))) 1103 | "Render STR, the last JShell output, as a clickable interactive hierarchy. 1104 | 1105 | For example, 1106 | 1107 | ;; See a JSON dropdown of three objects, key-value pairs. 1108 | (java-eval-navigate-output \"[Person[name=Jasim, age=72], Person[name=Kathy, age=82], Person[name=Jaafar, age=31]]\") 1109 | 1110 | ;; See a deeply-nested object that you can inspect 1111 | (java-eval-navigate-output \"Person[name=Jasim, age=72, child=Person[name=Hassan, age=22, child=Person[name=Abbas, age=31, child=null]]]\") 1112 | 1113 | ﴾ Remark: We are not limited to textual output ﴿ 1114 | We can use the full power of Emacs to render data in any kind of format 1115 | that is useful for the domain at hand. For example, rendering tabular data 1116 | in an Org buffer; HTML data into a xwidget-webkit browser; or any kind of 1117 | suitable major mode; or even opening an external program." 1118 | (interactive) 1119 | (if (and 1120 | (require 'hierarchy) ;; Interactive, clickable, views of hierarchical data 1121 | (require 'json-navigator)) ;; hierarchy.el specifically for JSON data 1122 | (with-temp-buffer 1123 | (thread-last 1124 | str ;; Has shape “type name = value”. 1125 | (s-replace-regexp "^[^ ]* [^ ]* = " "") 1126 | repl-driven-development--parse-pretty-printed-java 1127 | repl-driven-development--java-lisp-to-json-lisp 1128 | json-insert) 1129 | (goto-char (point-min)) 1130 | (json-navigator-navigate-after-point)) 1131 | (message "Please delete ~/.emacs.d/elpa/hierarchy-*/hierarchy.elc"))) 1132 | 1133 | (defun repl-driven-development--java-lisp-to-json-lisp (data) 1134 | "Convert the given Lisp sexp DATA into a JSON Lisp representation." 1135 | (cl-assert (plist-member data :type)) 1136 | (pcase (plist-get data :type) 1137 | (:number (plist-get data :value)) 1138 | (:string (plist-get data :value)) 1139 | (:list (thread-last (plist-get data :items) 1140 | (mapcar #'repl-driven-development--java-lisp-to-json-lisp) 1141 | (seq--into-vector))) 1142 | (:record (thread-last 1143 | (plist-get data :fields) 1144 | (--map (list (intern (plist-get it :name)) 1145 | (repl-driven-development--java-lisp-to-json-lisp (plist-get it :value)))) 1146 | (apply #'-concat) 1147 | (-cons* :type (plist-get data :name)))) 1148 | (else (error "java-lisp-to-json-lisp: Unknown data type “%s”" else)))) 1149 | 1150 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1151 | 1152 | (provide 'repl-driven-development) 1153 | 1154 | ;;; repl-driven-development.el ends here 1155 | -------------------------------------------------------------------------------- /snippets/EmacsConf2023.org: -------------------------------------------------------------------------------- 1 | # -*- eval: (my/execute-startup-blocks) 2 | 3 | #+title: Editor Integrated REPL Driven Development for all Languages 4 | #+author: Musa Al-hassy 5 | # 🗣 “moo saw”; and “al-hassy” rhymes with “classy” 6 | 7 | * The Wonders of “C-x C-e” ---“A uniform REPL interface for all languages” 8 | 9 | ** /Emacs can interpret Lisp anywhere it appears!/ 10 | 11 | ;; Press on the next line 12 | (message-box "Hello, %s" user-full-name) 13 | 14 | ** /Emacs can interpet Java/Bash/Python/⋯ anywhere it appears!/ 15 | 16 | // Press on the next two lines 17 | String username = System.getProperty("user.name"); 18 | JOptionPane.showMessageDialog(null, "Hello, " + username); 19 | 20 | ** /This means you can keep code in regular text files that does things 😲/ 21 | 22 | *** We can embed “hyperlinks” anywhere... 23 | 24 | ;; Learn more about the nifty “tldr” command with... 25 | (man "tldr") 26 | 27 | ;; Remember to edit this file, to keep things in-sync ... 28 | (find-file-other-window "../repl-driven-development.el") 29 | 30 | *** Or sanity checks, without using external tools: 31 | 32 | // Ensure this end-point works as intended by doing a request ... 33 | (await axios.get("https://jsonplaceholder.typicode.com/posts/1")).data 34 | 35 | *** Or, say, modify our current workflow on-the-fly 36 | 37 | -- Get your head in the game! ... with 38 | -- (Hear a motivation message, use exciting desktop wallpaper!) 39 | say "You can do it, buddy!" 40 | tell application "System Events" 41 | tell current desktop 42 | set picture to (POSIX file "~/Downloads/random-images/love-1.jpg") 43 | end tell 44 | end tell 45 | 46 | Exercise: Get a random image via an API call, say via Lisp, then set that to the 47 | background with AppleScript (Hint: ). 48 | 49 | ** /Your code immediately takes form!/ 50 | - It's a fun, and useful, way to learn coding! 51 | - Write code that gets things done for you, /now/. 52 | 53 | ;; Change the current Emacs theme, by pressing 54 | (my/load-theme 'spacemacs-light) 55 | 56 | // Change the current Emacs theme, by pressing 57 | emacs.eval(`(load-theme 'spacemacs-dark)`) 58 | 59 | -- Toggle the current operating system theme, with 60 | tell application "System Events" 61 | tell appearance preferences 62 | set dark mode to not dark mode 63 | end tell 64 | end tell 65 | 66 | (NOTE: Have a terminal app present, and notice its colours change. Notice the 67 | Emacs title pane change colour. Etc.) 68 | 69 | ** /Output is shown in an overlay at point/ 70 | 71 | For Emacs Lisp, 72 | 73 | ;; , then 74 | (s-collapse-whitespace (shell-command-to-string "scutil --get ComputerName")) 75 | 76 | For Python, 77 | 78 | ♯ Press on the next line 79 | list(map(lambda i: 'Fizz'*(not i%3) + 'Buzz'*(not i%5) or i, range(1,101))) 80 | 81 | - *Use ~C-u 0~ prefix to avoid truncation.* 82 | - *See the output echoed in the ~*Messages*~ buffer with ~C-h e~.* 83 | 84 | ** /Use the ~C-u~ prefix to insert results/ 85 | 86 | ;; Change all references to Jacob with Yacoub, via 87 | (subst 'yacoub 'jacob '(Abraham :wives (hagar sarah) :sons (ishameal jacob))) 88 | 89 | /This is useful for regression testing ---or to get the value for future use./ 90 | 91 | // Get the keys of a person, via 92 | Object.keys({ name: "Ibrahim", age: 175, city: "Ur" }) 93 | 94 | // Save a JSON payload, from an API request, for future use 95 | (await axios.get('https://jsonplaceholder.typicode.com/posts/23')).data 96 | 97 | + *Wait, how does ~subst~ work?* Press ~C-h o~ on it! 98 | + *Wait, how does ~axios~ work?* Use a ~C-u C-u~ prefix on it! 99 | ** Summary ---extending the familiar “C-x C-e” workflow to all languages 100 | 101 | | Familiar Lisp Workflow | | Mimicked workflow for your language 𝑳 | 102 | | Evaluate code selection | | ~𝑳-eval~ | 103 | | See result echoed | | In an overlay, by your cursour | 104 | | See result in ~*Messages*~ | | ~C-h e~ | 105 | | Insert result | | Use ~C-u~ prefix | 106 | | Evaluate top-level “defun” | | ~𝑳-eval-defun~ | 107 | | Evaluate all code in buffer | | ~𝑳-eval-buffer~ | 108 | | See docs at point | | ~C-u C-u~ prefix | 109 | | Restart evaluation process | | ~C-u -1~ prefix | 110 | 111 | * An overview of REPL Driven Development 112 | ** Modifying A BallPit Runtime, at runtime 113 | 114 | Since we can see the balls... 115 | 116 | 117 | [[~/blog/images/rdd-benefits.png]] 118 | 119 | # The ball pit code does not matter, the live change is what we want to discuss 120 | balls.get(2).diameter = 100; 121 | balls.get(2).color = Color.blue; 122 | balls = IntStream.range(0, 50).mapToObj(__ -> new Ball()).toList(); 123 | 124 | ** Interactively Solving FizzBuzz with Python 125 | 126 | A game to teach children division... 127 | 128 | /Write numbers 1..100 and replace multiples of 3 with “Fizz”, multiples of 5 129 | with “Buzz”, and multiples of both with “FizzBuzz”./ 130 | 131 | [[elisp:(progn (execute-kbd-macro (kbd "M-p")) (when nil I include the call twice so I can see the command name appear in the keycast, that is all) (execute-kbd-macro (kbd "M-p")))][﴾𝒮𝓉𝒶𝓇𝓉﴿]] 132 | 133 | ** Rapidly growing a Java runtime to be a photo gallery application, from scratch 134 | 135 | [[elisp:(progn (execute-kbd-macro (kbd "M-j")) (when nil I include the call twice so I can see the command name appear in the keycast, that is all) (execute-kbd-macro (kbd "M-j")))][﴾𝒮𝓉𝒶𝓇𝓉﴿]] 136 | 137 | * Implementing read protocols to use RDD to insert results of evaluation, for growing a test suite 138 | ** Demo 139 | [[elisp:(switch-to-buffer "YoutubeShorts.java")][﴾𝒮𝓉𝒶𝓇𝓉﴿]] 140 | 141 | #+begin_src latex-as-png :file rdd-print.pdf :resolution "520" :results raw value replace :exports none 142 | \begin{center} 143 | \smartdiagram[constellation diagram]{{\sc Print} \centerline{\footnotesize “Human friendly”}, 144 | \centerline{HTML} \centerline{in} \centerline{Browser}, 145 | \centerline{JSON} \centerline{in} \centerline{clickable tree}, 146 | \centerline{Table} \centerline{in} \centerline{Org-mode}, 147 | \centerline{OOP Objects} \centerline{as} \centerline{JSON}, 148 | \centerline{Hierarchical} \centerline{Text} \centerline{in} \centerline{PDF or Org}} 149 | {\newline \color{gray}\texttt{www.alhassy.com/repl-driven-development}} 150 | \end{center} 151 | #+end_src 152 | ** How to /show/ program output? 153 | 154 | [[file:rdd-print.png]] 155 | 156 | NOTE: JSON ⇒ doc:json-navigator-navigate-region 157 | browser ⇒ doc:xwidget-webkit-browse-url 158 | 159 | ** REPL: “R” and “P” are data interchange protocols 160 | ** Languages without them invent data interchange formats (e.g., JSON, XML, Protobuf) 161 | ** Specification: ~(𝓅𝓇𝒾𝓃𝓉 (𝓇ℯ𝒶𝒹 string)) ≈ string~ and ~(𝓇ℯ𝒶𝒹 (𝓅𝓇𝒾𝓃𝓉 data)) ≈ data~ 162 | 163 | # For example, in Emacs Lisp, 164 | #+name: startup-code 165 | #+begin_src emacs-lisp 166 | (setq string "(Abraham :wives (hagar sarah) :sons (ishameal yacoub))") ;; all green, one string 167 | (setq data '(Abraham :wives (hagar sarah) :sons (ishameal yacoub))) ;; somethings are blue; data; code! 168 | 169 | (defun 𝓅𝓇𝒾𝓃𝓉 (d) (s-trim (s-collapse-whitespace (pp-to-string d)))) 170 | (defun 𝓇ℯ𝒶𝒹 (s) (read s)) 171 | #+end_src 172 | 173 | #+RESULTS: startup-code 174 | : EmacsConf:\ Let\'s\ see\ some\ more... 175 | 176 | ** /“BRORP”: Bring your own Read Protocol!/ 177 | 178 | #+begin_src latex-as-png :file rdd-read.pdf :resolution "520" :results raw value replace :exports none 179 | \begin{center} 180 | \smartdiagram[sequence diagram]{ 181 | String (denoting OOP object), 182 | % JSON, 183 | Lisp \texttt{plist}, 184 | Constructor Methods} 185 | {\newline \color{gray}\texttt{www.alhassy.com/repl-driven-development} \newline} 186 | \smartdiagram[priority descriptive diagram]{ 187 | \texttt{Person@5e9f23b4}, 188 | % \mbox{\scriptsize \texttt{\hspace{-.5em}\{name: Jasim,} 189 | % \texttt{age: 78,} 190 | % \texttt{$\mathsf{\_{}\_{}type\_{}\_{}}$: "Person"\}}}, 191 | \texttt{'(:type Person :fields [ \mbox{{\scriptsize (:name "name" :value "Jasim" :type string) }} 192 | \mbox{{\scriptsize (:name "age" :value 78 :type number) }} 193 | ])}, 194 | \texttt{new Person("Jasim", 78)} 195 | } 196 | 197 | \end{center} 198 | #+end_src 199 | 200 | #+RESULTS: 201 | [[file:rdd-read.png]] 202 | 203 | + Red → Blue :: Pretty-printing and /Parsing Expression Grammars/ (e.g., ~peg.el~) 204 | - Why Parsing and not regular expressions? /To handle arbitraryly nested data-strutures!/ 205 | - See doc:repl-driven-development--parse-pretty-printed-java 206 | - Currently only supporting Java ~record~-s, for arbitrary classes we could 207 | convert to JSON first (using a 3ʳᵈ-party library like ~GSON~). 208 | [[file:rdd-read.png]] 209 | 210 | + Blue → Purple :: Custom Lisp algorithms ---e.g., to handle lists, ~null~, maps, etc. 211 | - See doc:repl-driven-development--lisp-to-java 212 | 🔑 /Parsing Expression Grammars/ (i.e., ~peg.el~) 213 | 214 | * Use Case: RDD & Job Interviews 215 | 216 | *Last year,* when I was changing jobs, I would share screen and show the 217 | interviewer what I was doing: 218 | a. I spoke my thought process as I typed, 219 | b. and did test-driven-development with the example input-output provided to me 220 | (if any), 221 | c. and ran my little algorithms as soon as I typed them ---or wrote my algorithms 222 | with *concrete example arguments then abstracted them to functions later on* 223 | d. I also had a loop of input-to-expected-output pairs which I ran occasionally 224 | to ensure my code would pass any tests I had created along the way. 225 | 226 | [[~/blog/images/rdd-workflow.png]] 227 | 228 | * Metadata 229 | 230 | #+SEQ_TODO: Metadata 231 | Presentation Setup Code 232 | 233 | #+name: startup-code 234 | #+begin_src emacs-lisp 235 | (setq display-time-format "Sat Dec 2, 2023") 236 | (display-time-mode) 237 | (require 'java-ts-mode) 238 | ;; Remove icon in title bar ---may take effect for /new/ frames, not current frame. 239 | ;; https://emacs.stackexchange.com/questions/33680/how-to-remove-the-icon-in-the-titlebar 240 | (shell-command "defaults write org.gnu.Emacs HideDocumentIcon YES") 241 | (set-frame-parameter (selected-frame) 'cursor-color "LightPink1") 242 | ;; (setq frame-title-format "    𝓔𝓶𝓪𝓬𝓼 𝓒𝓸𝓷𝓯 𝟚𝟘𝟚𝟛   ~   Editor Integrated REPL Driven Development for all Languages   ~   ⟅10 minutes⟆") 243 | (setq frame-title-format "    𝓔𝓶𝓪𝓬𝓼 𝓒𝓸𝓷𝓯 𝟚𝟘𝟚𝟛   ~   Editor Integrated REPL Driven Development for all Languages") 244 | (org-toggle-inline-images) 245 | (repl-driven-development [C-x C-j] java) 246 | (repl-driven-development [C-x C-n] javascript) 247 | (repl-driven-development [C-x C-p] python) 248 | (repl-driven-development [C-x C-t] terminal) 249 | (repl-driven-development [C-x C-a] applescript) 250 | (eros-mode -1) ;; I don't want to see the ELisp outputs of executing the above commands. 251 | (writegood-mode -1) (flyspell-mode -1) ;; Don't highlight intentional spelling errors, like BRORP. 252 | 253 | ;; M-x org-tree-slide 254 | ;; 255 | ;; https://github.com/takaxp/org-tree-slide#control-functions 256 | ;; org-tree-slide-move-next-tree (C->) 257 | ;; org-tree-slide-move-previous-tree (C-<) 258 | ;; org-tree-slide-content (C-x s c) 259 | (setq org-tree-slide-header nil) ;; Don't show title, author, date. 260 | (bind-key "M-n" #'org-tree-slide-move-next-tree) 261 | (bind-key "M-p" #'org-tree-slide-move-previous-tree) 262 | (bind-key "M-k" (defun keycast-on () (interactive) (doom-modeline-mode -1) (keycast-mode +1))) 263 | 264 | ;; Ensure Emacs is not full screen, to see java/applescript animations 265 | (defun my/maximize-frame () 266 | "Maximize the Emacs frame to take up all available screen space. 267 | 268 | This is not the same as fullscreen mode." 269 | (interactive) 270 | (set-frame-parameter nil 'fullscreen 'maximized)) 271 | ;; Actually call it! 272 | (my/maximize-frame) 273 | 274 | ;; Quickly jump to my Java code example 275 | ;; Use an informative name, since it'll show-up via keycast when I press M-j. 276 | (bind-key* "M-s" (lambda () (interactive) 277 | (org-tree-slide-mode) 278 | ;; To have keycast working with doom-modeline, I first need to turn off the latter then turn it on. 279 | (doom-modeline-mode -1) 280 | (keycast-mode +1) 281 | (keycast-mode-line-mode +1))) 282 | 283 | (org-tree-slide-mode) 284 | (bind-key* "M-j" 285 | (defun EmacsConf:\ Teaching\ a\ 𝒥𝒶𝓋𝒶\ runtime\ to\ be\ an\ image\ gallery\ application () 286 | (interactive) 287 | (find-file "./ImageGallery.java") 288 | (java-ts-mode) 289 | ;; (my/maximize-frame) 290 | (eglot-java-mode +1) ;; Inline overlays of arg names, syntax/type checks 291 | (company-mode +1) ;; Completions for methods 292 | (flyspell-mode -1) ;; Don't check my spelling 293 | (rainbow-delimiters-mode +1) ;; Colourful rainbow parens 294 | (rainbow-identifiers-mode +1) ;; Name-based/semantic highlighting 295 | (re-search-forward "END SETUP") 296 | (re-search-forward "public static void main.String.. args.") 297 | (mark-sexp) (narrow-to-region (region-beginning) (region-end)) 298 | (pop-mark) 299 | (scroll-down -2) 300 | ;; Ensure Emacs is not full screen, to see java/applescript animations 301 | ;; (set-frame-parameter nil 'fullscreen nil) 302 | (my/load-theme 'spacemacs-light))) 303 | 304 | (bind-key* "M-p" 305 | (defun EmacsConf:\ Interactively\ Solving\ FizzBuzz\ with\ 𝓟𝔂𝓽𝓱𝓸𝓷 () 306 | (interactive) 307 | (find-file "./FizzBuzz.py") 308 | (python-mode) 309 | ;; (my/maximize-frame) 310 | (call-interactively #'eglot) ;; Inline overlays of arg names, syntax/type checks 311 | (company-mode +1) ;; Completions for methods 312 | (flyspell-mode -1) ;; Don't check my spelling 313 | (rainbow-delimiters-mode +1) ;; Colourful rainbow parens 314 | (rainbow-identifiers-mode +1) ;; Name-based/semantic highlighting 315 | (my/load-theme 'spacemacs-light))) 316 | 317 | ;; Wait, this is just C-x C-o; i.e., delete-blank-lines 318 | (bind-key "M-SPC" 319 | (cl-defun EmacsConf:\ Let\'s\ see\ some\ more... () 320 | (interactive) 321 | (delete-all-space) 322 | (insert "\n\n") 323 | (indent-for-tab-command)) 324 | prog-mode-map) 325 | #+end_src 326 | -------------------------------------------------------------------------------- /snippets/FizzBuzz.py: -------------------------------------------------------------------------------- 1 | # (repl-driven-development [C-x C-p] python) 2 | # 3 | # Let's test the above works... 4 | "Hello, world".upper() 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | # Write numbers 1..100 and replace multiples of 3 with “Fizz”, multiples of 5 45 | # with “Buzz”, and multiples of both with “FizzBuzz”. 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | range(1, 100) 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | # Huh, nothing's showing up with “C-x C-p”! 130 | # Let's get the docs of “range” with “C-u C-u” … 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | # Hey, look at that, the docs show exactly *how to use range*: 173 | # It's a generator and so should be used with “list”. 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | list(range(1, 100)) 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | # Huh, we're missing ‘100’! 258 | # Take a look at the docs again... 259 | # “range”'s second argument is excluded! 260 | list(range(1, 101)) 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | # Let's use a “map” to get at each element 303 | list(map(lambda i: i, range(1,101))) 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | # Multiples of 3 ... 346 | list(map(lambda i: 'Fizz'*(not i%3), range(1,101))) 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | # Huh, we're seeing empty strings: 'Fizz' * 0 ≈ '' 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | list(map(lambda i: 'Fizz'*(not i%3) or i, range(1,101))) 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | # Multiples of 5... 473 | list(map(lambda i: 'Fizz'*(not i%3) or 'Buzz'*(not i%5) or i, range(1,101))) 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | # Huh, index 15 should be “FizzBuzz” not just “Fizz”! 516 | # 15 is a multiple of both 3 and 5, so “or”/∨ is not appropriate, we should concat/“+” instead! 517 | list(map(lambda i: 'Fizz'*(not i%3) + 'Buzz'*(not i%5) or i, range(1,101))) 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | # All done, bye! :wave: 560 | -------------------------------------------------------------------------------- /snippets/ImageGallery.java: -------------------------------------------------------------------------------- 1 | /** 2 | * “Teaching a Java runtime to be an image gallery application” 3 | * 4 | * I keep my code within a class and within a method, so that I can have access 5 | * to Eglot's IDE features for Java: Code completion, docs, argument labels, 6 | * tooltips, etc. In the main() method, I generally “C-M-SPC C-x C-n”, since 7 | * that's what I actually want to see / work with. 8 | * 9 | *** Gif Setup ***************************************************************** 10 | (progn 11 | ;; Auto-format on save;; For example, for Java, I need to: 12 | ;; brew install clang-format 13 | ;; (format-all-mode +1) ;; or: M-x format-all 14 | ;; Don't prompt me which formatter to use; just use the defaults. 15 | ;; For example, for Java, I need to: brew install clang-format 16 | ;; (setq format-all-formatters format-all-default-formatters) 17 | (setq frame-title-format 18 | "   Teaching a 𝒥𝒶𝓋𝒶 runtime to be an image gallery application") 19 | (rename-buffer " 🚀 https://github.com/alhassy/repl-driven-development 👀\t") 20 | (setq mode-line-format '(("%e" mode-line-buffer-identification) keycast-mode-line)) 21 | (eglot-java-mode +1) ;; Inline overlays of arg names, syntax/type checks 22 | (company-mode +1) ;; Completions for methods 23 | (flyspell-mode -1) ;; Don't check my spelling 24 | (rainbow-delimiters-mode +1) ;; Colourful rainbow parens 25 | (rainbow-identifiers-mode +1) ;; Name-based/semantic highlighting 26 | (keycast-header-line-mode +1) ;; Show my keybindings 27 | (re-search-forward "public static void main.String.. args.") 28 | (mark-sexp) (narrow-to-region (region-beginning) (region-end)) 29 | (pop-mark) 30 | (scroll-down -1) 31 | ;; (my/load-theme 'shanty-themes-light) 32 | (my/load-theme 'spacemacs-light) 33 | (message "See more code with C-x C-o; i.e., delete-blank-lines")) 34 | 35 | *** Screen Capture Misc. *************************** 36 | 37 | ;; Pearl - Mirror in your menubar 38 | (applescript-eval-string "tell application \"Pearl\" to activate") 39 | 40 | (applescript-eval-string " 41 | tell application \"QuickTime Player\" 42 | activate 43 | start (new screen recording) 44 | end tell") 45 | 46 | *** Setup the directory of photos! ****************************************** 47 | // rm -rf ~/Downloads/random-images/; cp -r ~/Documents/unsplash-images 48 | ~/Downloads/random-images/; cd ~/Downloads/random-images; mogrify -resize 49 | 300x300 ./*.jpg; cp pouring-coffee---photo-by-mhrezaa-on-unsplash.jpg 50 | love-0.jpg; cp pouring-water---photo-by-mhrezaa-on-unsplash.jpg love-1.jpg 51 | */ 52 | 53 | import java.awt.*; 54 | import java.awt.event.ActionListener; 55 | import java.io.File; 56 | import java.util.Arrays; 57 | 58 | import javax.swing.*; 59 | 60 | class ImageGallery { 61 | public static void main(String[] args) { 62 | /* 𝓖𝓸𝓪𝓵: 𝑺𝒆𝒆 𝒕𝒉𝒊𝒏𝒈𝒔 𝑨𝑺𝑨𝑷, 𝒂𝒏𝒅 𝒄𝒓𝒆𝒂𝒕𝒆 𝒃𝒚 𝒓𝒆𝒂𝒄𝒕𝒊𝒏𝒈 (。◕‿◕。) 63 | 64 | ;; Set “C-x C-j” to evaluate arbitrary Java code 65 | (repl-driven-development [C-x C-j] java) 66 | 67 | // See the solution (echoed in a tooltip) to FizzBuzz by selecting and evaluating: 68 | IntStream.range(0, 31) 69 | .mapToObj(i -> i % 15 == 0 ? "FizzBuzz" 70 | : i % 3 == 0 ? "Fizz" 71 | : i % 5 == 0 ? "Buzz" 72 | : String.valueOf(i)).toList() 73 | */ 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | /* 𝓖𝓸𝓪𝓵: 𝑺𝒆𝒆 𝒕𝒉𝒊𝒏𝒈𝒔 𝑨𝑺𝑨𝑷, 𝒂𝒏𝒅 𝒄𝒓𝒆𝒂𝒕𝒆 𝒃𝒚 𝒓𝒆𝒂𝒄𝒕𝒊𝒏𝒈 (。◕‿◕。) */ 114 | var frame = new JFrame(); // ⇒ Your cursor loses control: There's a JFrame created! 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | frame.setVisible(true); // It's invisible by default, lame! ﴾“x” button hides frame, not kills it﴿ 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | frame.setAlwaysOnTop(true); // I wanna see it even when I'm in my editor. 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | // We see a super tiny new dialog; let's enlarge it 240 | frame.setSize(500, 500); 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | // Whoops we forgot to set the title! 283 | frame.setTitle("Hola¡"); 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | // Nice; let's try another title 326 | frame.setTitle("Photo Gallery ~ Designed via REPL driven development"); 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | // Let's add some colour to our lives! 369 | frame.getContentPane().setBackground(Color.CYAN); 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | // Let's “add” some content 412 | File directory = new File("/Users/musa/Downloads/random-images/"); 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | var allImages = Arrays.stream(directory.listFiles()) 455 | .filter(it -> it.getName().endsWith(".jpg")) 456 | .map(it -> new ImageIcon(it.getAbsolutePath())) 457 | .toList(); 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | var image = new JLabel(allImages.get(0)); 500 | frame.add(image); 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | // Wait... nothing changed?! 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | // We need to re-trigger the repainting of our GUI; e.g., by changing its 585 | // size (either manually or via code, to a new size): 586 | frame.revalidate(); 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | // Can we change the image easily? 629 | image.setIcon(allImages.get(1)); 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | // Yay, it worked! 672 | // Let's change get(𝒾) for 𝒾:0..8 to see this in-action a few times! 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | // Let's add a button for user interactivity! 715 | var button = new JButton("Next Image"); 716 | frame.add(button); 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | // Now let's experiment to find the ideal position of the button ;-] 759 | frame.setLayout(new FlowLayout()); 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | // Let's hook a callback onto it 802 | int clicks = 0; 803 | ActionListener watDo = e -> frame.setTitle("You did the thing! x " + clicks++); 804 | button.addActionListener(watDo); 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | // We can also remove the listener: 847 | button.removeActionListener(watDo); 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | // Let's add modify the listener: Change the existing image! 890 | // (In the video, I would not copy-paste, but instead modify the above 891 | // snippet!) 892 | watDo = e -> image.setIcon(allImages.get(clicks++ % allImages.size())); 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | // I'd like to see the name of the image, whenever we click on a new image. 935 | watDo = e -> { 936 | var icon = allImages.get(clicks++ % allImages.size()); 937 | image.setIcon(icon); 938 | frame.setTitle(icon.toString()); 939 | }; 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 | 979 | 980 | 981 | // Neato! We have a photo gallery app xD 982 | 983 | // This was fun! Bye 👋 984 | } 985 | } 986 | 987 | /** 988 | * To “update” an action listener, we first need to remove it, then redefine it, then register it. 989 | * 990 | * Why? I'm not sure. 991 | * It'd be nice if we could just update the binding, but under-the-hood it may be the case that 992 | * the binding is copied and not used by reference. 993 | * 994 | * TLDR: All lines of the form “watDo = ⋯” should really be of the form: 995 | * 996 | * button.removeActionListener(watDo); 997 | * watDo = ⋯; 998 | * button.addActionListener(watDo); 999 | * 1000 | * In the video, I take care to run the remove, update, add lines via C-x C-j. 1001 | * 1002 | * 1003 | * ** “clicks” should really be an atomic integer / AtomicReference ************************** 1004 | * 1005 | * import java.util.concurrent.atomic.AtomicInteger; 1006 | * AtomicInteger clicks = new AtomicInteger(10); 1007 | * System.out.println(clicks.addAndGet(2)); 1008 | * 1009 | * ** Final Program ************************************************************************ 1010 | * 1011 | 1012 | import java.awt.*; 1013 | import java.awt.event.ActionListener; 1014 | import java.io.File; 1015 | import java.util.Arrays; 1016 | import javax.swing.*; 1017 | 1018 | var frame = new JFrame(); 1019 | frame.setVisible(true); 1020 | frame.setAlwaysOnTop(true); 1021 | frame.setSize(500, 500); 1022 | frame.getContentPane().setBackground(Color.CYAN); 1023 | // Let's add an image first, so that we have an initial image that can be 1024 | // mutated 1025 | var image = new JLabel(); 1026 | var button = new JButton("Next Image"); 1027 | frame.add(image); 1028 | frame.add(button); 1029 | frame.setLayout(new FlowLayout()); 1030 | int clicks = 0; 1031 | File directory = new File("/Users/musa/Downloads/random-images/"); 1032 | var allFiles = Arrays.stream(directory.listFiles()) 1033 | .filter(it -> it.getName().endsWith(".jpg")) 1034 | .map(it -> new ImageIcon(it.getAbsolutePath())) 1035 | .toList(); 1036 | button.addActionListener(e -> { 1037 | var icon = allFiles.get(clicks++ % allFiles.size()); 1038 | image.setIcon(icon); 1039 | frame.setTitle(icon.toString()); 1040 | }); 1041 | */ 1042 | -------------------------------------------------------------------------------- /snippets/RepoGif.java: -------------------------------------------------------------------------------- 1 | /** 2 | * I keep my code within a class and within a method, so that I can have access 3 | * to Eglot's IDE features for Java: Code completion, docs, argument labels, 4 | * tooltips, etc. In the main() method, I generally “C-M-SPC C-x C-n”, since 5 | * that's what I actually want to see / work with. 6 | * 7 | *** Gif Setup ***************************************************************** 8 | (progn 9 | ;; Make the M-RET command insert “* ” when I'm in a JavaDoc 10 | (bind-key* "M-RET" #'c-indent-new-comment-line 'java-mode-map) 11 | ;; Auto-format on save;; For example, for Java, I need to: 12 | ;; brew install clang-format 13 | (format-all-mode +1) ;; or: M-x format-all 14 | ;; Don't prompt me which formatter to use; just use the defaults. 15 | ;; For example, for Java, I need to: brew install clang-format 16 | (setq format-all-formatters format-all-default-formatters) 17 | (setq frame-title-format 18 | "   “Send region to process” for all Languages    ᕦ( ᴼ ڡ ᴼ )ᕤ") 19 | (defun doom-modeline-buffer-file-name () " 20 | https://github.com/alhassy/repl-driven-development ") 21 | (eglot-java-mode +1) ;; Inline overlays of arg names, syntax/type checks 22 | (company-mode +1) ;; Completions for methods 23 | (flyspell-mode -1) ;; Don't check my spelling 24 | (rainbow-delimiters-mode +1) ;; Colourful rainbow parens 25 | (rainbow-identifiers-mode +1) ;; Name-based/semantic highlighting 26 | (keycast-header-line-mode +1) ;; Show my keybindings 27 | (re-search-forward "public static void main()") 28 | (mark-sexp) (narrow-to-region (region-beginning) (region-end)) 29 | (pop-mark) 30 | (scroll-down -2) 31 | ;; (my/load-theme 'shanty-themes-light) 32 | (my/load-theme 'spacemacs-light) 33 | (message "Fold specific regions, then click on them to show/discuss them.")) 34 | 35 | *** Gif maker; needs privileges to capture screen *************************** 36 | * 37 | * ⇒ Move the screen capture frame while recording. 38 | * ⇒ Pause and restart recording, with optional inserted text messages. 39 | * ⇒ Global hotkey (shift+space) to toggle pausing while recording 40 | * (system-packages-ensure "licecap") ;; Use: ⌘-SPACE licecap 41 | * 42 | * Finally, ⌘-SPACE screenbrush. Use M-TAB to toggle it on/off; SHIFT for 43 | * flashlight focus. 44 | */ 45 | import java.util.*; 46 | class RepoGif { 47 | public static void main() { 48 | /** 49 | In Emacs, you can execute any line of Lisp 𝒶𝓃𝓎𝓌𝒽ℯ𝓇ℯ by pressing C-x C-e: 50 | (message-box "Hello, world") 51 | 52 | This package gives you the same “evaluate line” for any language. 53 | 54 | ;; This lets us use “C-x C-⋯” to send lines to the declared process 55 | (repl-driven-development [C-x C-t] "bash") 56 | (repl-driven-development [C-x C-p] "python3") 57 | (repl-driven-development [C-x C-j] "jshell") 58 | 59 | For example, when reading a README, you can now just execute shell 60 | commands with “C-x C-t”, rather than copy/paste into a terminal: 61 | 62 | echo "$(whoami) says “hello” at $(date) from $PWD" 63 | 64 | ⇒ Results are overlayed next to cursor, in a tooltip, and via “C-h e”. 65 | ⇒ “C-h k C-x C-t” gives extensive help about this new “C-x C-t” command. 66 | ↣ Ex: Insert the result of the above shell command with “C-u C-x C-t”. 67 | 68 | There are also “preconfigured REPLs” with helpful bells & whistels … 69 | 70 | ;; “C-x C-j” now evaluates arbitrary Java code ♥‿♥ 71 | (repl-driven-development [C-x C-j] java) 72 | 73 | For example, 74 | 75 | JOptionPane.showMessageDialog( 76 | new JFrame(){{ setAlwaysOnTop(true); }}, 77 | "Super nice!") 78 | */ 79 | 80 | // Let's make a type to model stuff we're working with … 81 | record Person(String name, int age) { 82 | 83 | // … along with a super duper complex algorithm 84 | static List foo(String... names) { 85 | return Arrays.stream(names) 86 | .map(n -> new Person(n, n.length())) 87 | .toList(); 88 | } 89 | } 90 | 91 | // “C-x C-j” shows me “human readable” results 92 | Person.foo("Hamid", "Jaafar"); 93 | 94 | // “C-u C-x C-j” shows me “java readable” code that can be used for 95 | // regression tests 96 | Person.foo("Hamid", "Jaafar"); 97 | 98 | // We can also interactively navigate large data-dumps… 99 | // …when overlay output is cramped 100 | Person.foo("Hamid", "Jaafar", "Musa", "Montather"); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /snippets/YoutubeShorts.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Resulting Youtube Short :: https://www.youtube.com/watch?v=y1BA3GJsWEE 3 | * 4 | * I keep my code within a class and within a method, so that I can have access 5 | * to Eglot's IDE features for Java: Code completion, docs, argument labels, 6 | * tooltips, etc. In the main() method, I generally “C-M-SPC C-x C-n”, since 7 | * that's what I actually want to see / work with. 8 | * 9 | *** Gif Setup ***************************************************************** 10 | (progn 11 | ;; Make the M-RET command insert “* ” when I'm in a JavaDoc 12 | (bind-key* "M-RET" #'c-indent-new-comment-line 'java-mode-map) 13 | ;; Auto-format on save;; For example, for Java, I need to: 14 | ;; brew install clang-format 15 | (format-all-mode +1) ;; or: M-x format-all 16 | ;; Don't prompt me which formatter to use; just use the defaults. 17 | ;; For example, for Java, I need to: brew install clang-format 18 | (setq format-all-formatters format-all-default-formatters) 19 | (setq frame-title-format 20 | "   Java: REPL Driven Development with  𝓔𝓶𝓪𝓬𝓼   ") 21 | (defun doom-modeline-buffer-file-name () " 22 | https://github.com/alhassy/repl-driven-development ") 23 | (eglot-java-mode +1) ;; Inline overlays of arg names, syntax/type checks 24 | (company-mode +1) ;; Completions for methods 25 | (flyspell-mode -1) ;; Don't check my spelling 26 | (rainbow-delimiters-mode +1) ;; Colourful rainbow parens 27 | (rainbow-identifiers-mode +1) ;; Name-based/semantic highlighting 28 | (keycast-header-line-mode +1) ;; Show my keybindings 29 | (re-search-forward "public static void main()") 30 | (mark-sexp) (narrow-to-region (region-beginning) (region-end)) 31 | (pop-mark) 32 | (scroll-down -2) 33 | ;; (my/load-theme 'shanty-themes-light) 34 | (my/load-theme 'spacemacs-light) 35 | (message "Fold specific regions, then click on them to show/discuss them.")) 36 | 37 | *** Gif maker; needs privileges to capture screen *************************** 38 | * 39 | * ⇒ Move the screen capture frame while recording. 40 | * ⇒ Pause and restart recording, with optional inserted text messages. 41 | * ⇒ Global hotkey (shift+space) to toggle pausing while recording 42 | * (system-packages-ensure "licecap") ;; Use: ⌘-SPACE licecap 43 | * 44 | * Finally, ⌘-SPACE screenbrush. Use M-TAB to toggle it on/off; SHIFT for 45 | * flashlight focus. 46 | */ 47 | import java.util.*; 48 | import javax.swing.JFrame; 49 | import javax.swing.JOptionPane; 50 | class YoutubeShorts { 51 | public static void main() { 52 | /** 53 | ;; “C-x C-j” now evaluates arbitrary Java code ♥‿♥ 54 | (repl-driven-development [C-x C-j] java) 55 | */ 56 | 57 | // See a message box 58 | JOptionPane.showMessageDialog(new JFrame() { 59 | { setAlwaysOnTop(true); } 60 | }, "Super nice!"); 61 | 62 | // Let's make a type to model stuff we're working with … 63 | record Person(String name, int age) { 64 | 65 | // … along with a super duper complex algorithm 66 | static List foo(String... names) { 67 | return Arrays.stream(names) 68 | .map(n -> new Person(n, n.length())) 69 | .toList(); 70 | } 71 | } 72 | 73 | // “C-x C-j” shows me “human readable” results 74 | Person.foo("Hamid", "Jaafar"); 75 | 76 | // “C-u C-x C-j” shows me “java readable” code that can be used for 77 | // regression tests 78 | Person.foo("Hamid", "Jaafar"); 79 | 80 | // We can also interactively navigate large data-dumps… 81 | // …when overlay output is cramped 82 | Person.foo("Hamid", "Jaafar", "Musa", "Montather"); 83 | 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /snippets/growing-a-webserver.js: -------------------------------------------------------------------------------- 1 | // C-x C-j now evaluates arbitrary JavaScript code ♥‿♥ 2 | // (repl-driven-development [C-x C-j] javascript) 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | [...Array(40).keys()] 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | .map(x => x % 3 == 0 ? "Fizz" : x) 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | .map(x => x % 3 == 5 ? "Buzz" : x) 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | .map(x => x % 3 == 0 && x % 5 == 0 ? "FizzBuzz" : x) 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | // C-x C-j now evaluates arbitrary JavaScript code, I'd also like docs for JS and Express 246 | // (repl-driven-development [C-x C-j] "node" :docs "javascript express") 247 | 248 | // npm install -g express axios@0.21.1 249 | 250 | let app = require('/usr/local/lib/node_modules/express')() 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | let server = app.listen(3030) // 📚 307 | 308 | // Now visit http://localhost:3030/ 309 | // ... and see “Cannot GET /” 310 | // ... Neat, it works but it does nothing! Importantly it works! 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | // Let's add a route... 368 | let visited = 1 369 | app.get('/hi', (_req, res) => res.send(`Hello × ${visited++}`)) 370 | 371 | // Now visit: http://localhost:3030/hi 372 | // Refresh the page a few times 😉 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | // Notice the local values of “visited” has changed, for each refresh 431 | visited 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | // Excellent; let's add an end-point to return info about the user 487 | app.get('/about', (_req, res) => res.send(html())) 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | // Whoops, there's no “html”! So we see an error! 545 | // Let's define that! 546 | let html = _ => "
" + info() + "
" 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | // Whoops, there's no “info”! So we see an error! 604 | // Let's define that! 605 | let info = function() { return { visited, user: process.env.USER, time: new Date() } } 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | // Uh-oh, we see “[object Object]” since we didn't convert the 663 | // JS object into a JSON string, so let's fix that! 664 | html = _ => "
" + JSON.stringify(info(), null, 3 /* indentation */) + "
" 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | // Uh-oh, the output doesn't look good; let's redefine `html` using
 tags.
 722 | html = _ => `

Welcome, visitor ${visited++}!

723 |
 724 |                ${ JSON.stringify(info(), null, 3 /* indentation */) }
 725 |             
` 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | // 🤩 Notice how we built this end-point from the top-down: 785 | // 🥰 We knew what we wanted, and saw some errors 786 | // ---on the client side--- then fixed them right here, 787 | // with no reloading! 🥳 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | // Actually, let's get info about a JS cheat sheet repo 844 | var axios = require('axios') 845 | let repo = (await axios 846 | .get('https://api.github.com/repos/alhassy/JavaScriptCheatSheet')).data 847 | info = _ => ({ url: repo.html_url, 848 | description: repo.description, 849 | stars: repo.watchers, 850 | forks: repo.forks, 851 | topics: repo.topics 852 | }) 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | // So cool! 915 | 916 | // Eventually, consider closing the server! 917 | server.close() 918 | 919 | // Bye! ᕦ( ᴼ ڡ ᴼ )ᕤ 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 | 979 | 980 | 981 | 982 | 983 | 984 | 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | 1029 | 1030 | 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | /* 1039 | (setq frame-title-format "REPL Driven Development with JavaScript:    FizzBuzz    ᕦ( ᴼ ڡ ᴼ )ᕤ 1040 | ") 1041 | 1042 | M-x goto-address-mode ;; makes all URLs clickable 1043 | http://localhost:3030/ 1044 | 1045 | 1046 | 1047 | (defun doom-modeline-buffer-file-name () "⇒ www.alhassy.com/repl-driven-development ⇐") 1048 | 1049 | 1050 | (flymake-mode -1) 1051 | (minions-mode 1) 1052 | (flyspell-mode -1) 1053 | 1054 | (bind-key "M-SPC" (cl-defun my/delete-vertical-space () 1055 | (interactive) 1056 | (delete-all-space) 1057 | (insert "\n") 1058 | (indent-for-tab-command))) 1059 | 1060 | Note: If you forget to close the sever, run “lso -i :3030” to find the pid 1061 | then run “kill -9 PROCESS_ID”. 1062 | 1063 | 1064 | (setq prettify-symbols-alist '(("=>" . 8658))) 1065 | (prettify-symbols-mode -1) 1066 | (prettify-symbols-mode +1) 1067 | */ 1068 | -------------------------------------------------------------------------------- /snippets/testing.el: -------------------------------------------------------------------------------- 1 | (when nil ⨾⨾ Rich Comment consisting of executable code to try things out. 2 | 3 | ⨾⨾ Testing setup ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 4 | 5 | (load-file "./testing-setup.el") ;; See my init.el 6 | (eval-buffer "repl-driven-development.el") 7 | ;; Style errors, package errors 8 | (my/show-errors) 9 | ;; Byte-compiles the file with all warnings enabled. 10 | (elisp-lint--byte-compile (buffer-file-name)) 11 | ;; Show me references to unbound symbols 12 | (elint-current-buffer) 13 | (my/load-file-in-new-emacs) 14 | (progn (outshine-mode) (outline-minor-mode)) 15 | 16 | ⨾⨾ A simple terminal REPL works as expected ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 17 | 18 | (repl-driven-development [C-x C-t] "bash" :blink 'pulsar-green) 19 | echo -e "$(whoami): On $(date), I sit at $(pwd) pondering ... \n $(ls)" 20 | 21 | ;; Nice docs 22 | (documentation #'bash-eval) 23 | 24 | ;; The output is echoed via an overlay; however on the source to see it 25 | ;; in a tooltip; invoke C-h e to see it in the *Messages* buffer; 26 | ;; Or see it in its own buffer with M-x ... 27 | (bash-eval-display-output) ;; i.e., (rdd@ "bash" output) 28 | 29 | ;; Insert the result of the above shell command with C-u C-x C-t. 30 | 31 | ;; We can also restart the repl... let's set some state 32 | export X=123 33 | echo $X 34 | ;; Now restart it with C-u -1 C-x C-t 35 | echo $X 36 | ;; C-x C-t on the above line emits no value 37 | 38 | ;; Init code works upon initialisation, neato! 39 | (repl-driven-development [C-x C-t] "bash" :init "echo $(fortune)") 40 | 41 | ;; We can get rid of the prompt at the end with :prompt 42 | (repl-driven-development [C-x C-t] "bash" :prompt "^bash.*?\\$") 43 | pwd 44 | echo "bye$" 45 | 46 | (repl-driven-development [C-x C-t] terminal) 47 | 48 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 49 | ;; We can change the blinking colours via rdd@. 50 | (repl-driven-development [C-x C-n] "node" :blink 'pulsar-blue) 51 | [...Array(14).keys()].map(x => x % 3 == 0 ? "Fizz" : x) 52 | ;; Change colour with C-x C-e, then C-x C-n on the line after. 53 | (setf (rdd@ "node" blink) 'pulsar-green) 54 | Object.keys({name: "mikle", 1: "one"}) 55 | 56 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 57 | ;; We can use a preconfigured REPL. 58 | ;; 59 | ;; Notice associated buffer's name involves only the command "jshell", 60 | ;; not the args. See it via C-u 0 C-x C-j. 61 | (repl-driven-development [C-x C-j] java) 62 | ;; 63 | ;; This allows us to submit multi-line input seamlessly. 64 | ;; Select the following 6 lines, then submit this region with C-x C-j 65 | IntStream 66 | /* a multi-line 67 | * comment */ 68 | .range(0, 23) 69 | // Now print it out 70 | .forEach(x -> System.out.println(x)) 71 | 72 | ;; There is no repetation of input via output 73 | ;; https://github.com/alhassy/repl-driven-development/issues/5 74 | System.out.println("Hi") 75 | 76 | IntStream.range(0, 40) .mapToObj(i -> i % 15 == 0 ? "FizzBuzz" : i % 3 == 0 ? "Fizz" : i % 5 == 0 ? "Buzz" : String.valueOf(i)).toList() 77 | 78 | // Let's make a type to model stuff we're working with 79 | record Person(String name, int age) { } 80 | 81 | // Let's write a super duper complex algorithm 82 | List foo(String... names) { return Arrays.stream(names).map(n -> new Person(n, n.length())).toList(); } 83 | 84 | // Let's run our algorithm and get *executable* outputs that can then be used for regression tests 85 | foo("musa", "hamid") // C-x C-j shows me “human readable” results 86 | // C-u C-x C-j shows me “java readable” code that can be used for regression tests 87 | foo("musa", "hamid") 88 | // ⇒ List.of(new Person("musa", 4), new Person("hamid", 5)) 89 | 90 | 91 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 92 | ;; Likewise for NodeJS 93 | (repl-driven-development [C-x C-n] javascript) 94 | ;; Then submit: 95 | [...Array(40).keys()] 96 | // yay, a comment in the middle 97 | .map(x => x % 3 == 0 ? "Fizz" : x) 98 | 99 | ⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾⨾ 100 | ;; Likewise for Python 101 | (repl-driven-development [C-x C-p] python) 102 | ;; Send each line, one at a time. 103 | " 104 | 1 + 2 * 3 105 | 106 | def foo(x): return x*x 107 | 108 | foo(5) 109 | 110 | list(map(lambda i: 'Fizz'*(not i%3)+'Buzz'*(not i%5) or i, range(1,101))) 111 | 112 | # (Above shows Result Truncated due to my use of eros, which has this limit. 113 | # TODO[Low Priority]: Fix this.) 114 | # 115 | # We can do multi-line def ---The quotes are to allow me to indent, 116 | # otherwise my aggressive-formatter strips the whitespace away. 117 | 118 | def square(x): 119 | return x * x 120 | 121 | square(5) 122 | 123 | # Likewise for class-es: 124 | 125 | class MyClass(): 126 | i = 12345 127 | 128 | def f(self): 129 | return 'hello world' 130 | 131 | 132 | x = MyClass() 133 | x.i 134 | x.f() 135 | " 136 | ;; Notice that the code is indented nicely. 137 | ) 138 | 139 | 140 | (cl-flet ((java-read (str) 141 | (thread-last 142 | str 143 | repl-driven-development--parse-pretty-printed-java 144 | repl-driven-development--lisp-to-java))) 145 | (require 'ert) 146 | ;; Non-record *values* are read literally 147 | (should (equal (java-read "123") "123")) 148 | ;; Records with a single field become constructor calls on that field: As a number (if possible), otherwise as a string. 149 | (should (equal (java-read "Person[name=Jaafar]") "new Person(\"Jaafar\")")) 150 | (should (equal (java-read "Person[age=31.2]") "new Person(31.2)")) 151 | ;; Record payloads can include spaces and commas 152 | (should (equal (java-read "Person[name=AsSaddiq, Jaafar, the first, age=thirty and one, years=31]") 153 | "new Person(\"AsSaddiq, Jaafar, the first\", \"thirty and one\", 31)")) 154 | ;; We can read nested records. 155 | ;; For arbitrarly deep nesting, we cannot use regular expressions, and so we need to move to using PEGs. 156 | (should (equal (java-read "Person[name=Jaafar, child=Person[name=Yacoub]]") "new Person(\"Jaafar\", new Person(\"Yacoub\"))")) 157 | (should (equal (java-read "Person[name=Jaafar, child=Person[name=Yacoub], child=Person[name=Jasim]]") 158 | "new Person(\"Jaafar\", new Person(\"Yacoub\"), new Person(\"Jasim\"))")) 159 | (should (equal (java-read "Person[name=Hamid, child=Person[name=Jaafar, child=Person[name=Yacoub]]]") 160 | "new Person(\"Hamid\", new Person(\"Jaafar\", new Person(\"Yacoub\")))")) 161 | (should (equal (java-read "Person[name=hamid, child=Person[name=Jaafar, age=12]]") "new Person(\"hamid\", new Person(\"Jaafar\", 12))")) 162 | ;; We can read lists. 163 | (should (equal (java-read "[1, 2, 3]") "List.of(1, 2, 3)")) 164 | (should (equal (java-read "[]") "List.of()")) 165 | (should (equal (java-read "[Person[name=Jasim, age=72, zindex=0.5], Person[name=Kathy, age=82, zindex=2.78], Person[name=Jaafar, age=31, zindex=3]]") 166 | "List.of(new Person(\"Jasim\", 72, 0.5), new Person(\"Kathy\", 82, 2.78), new Person(\"Jaafar\", 31, 3))")) 167 | (should (equal (java-read "[[1], [2, 3], [4, 5, 6]]") "List.of(List.of(1), List.of(2, 3), List.of(4, 5, 6))")) 168 | ;; We can mix the various structures (records & lists) 169 | ;; TODO: (java-read "Person[children=[]]") 170 | (should (equal (java-read "A[a=[B[b=[C[c=D[d=[E[e=hello]]]]]]]]") 171 | "new A(List.of(new B(List.of(new C(new D(List.of(new E(\"hello\"))))))))")) 172 | ;; Null is known 173 | (should (equal (java-read "Person[name=null]") "new Person(null)")) 174 | ;; Maps are known 175 | (should (equal (java-read "{}") "Map.of()")) 176 | (should (equal (java-read "{a=1, b=2, c=3}") "Map.of(a, 1, b, 2, c, 3)")) 177 | (should (equal (java-read "{a=Alice, b=Bob, c=Kathy}") "Map.of(a, \"Alice\", b, \"Bob\", c, \"Kathy\")")) 178 | ;; TODO: Allow numbers as keys. 179 | ;; {1=hello, 2=world} 180 | ;; TODO: (java-read "Person[]") 181 | ) 182 | 183 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 184 | ;; TODO: Document the `emacs' function in the javascript repl. 185 | ;; 186 | ;; emacs.eval(`user-full-name`) 187 | ;; emacs.eval(`(load-theme 'spacemacs-dark)`) 188 | ;; emacs.eval(`(load-theme 'spacemacs-light)`) 189 | ;; emacs.eval(`(message-box "hi")`) 190 | ;; // Multi-line also works fine 191 | ;; emacs.eval(`(progn 192 | ;; (message-box "Hello") 193 | ;; (message-box "World") 194 | ;; )`) 195 | 196 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 197 | ;; 198 | ;; TODO: Add to main rdd.el macro: Mention connecting to JS Server via C-x C-t telnet. 199 | 200 | ;; Something to consider using 201 | ;; 202 | ;; Current bug: It makes C-x C-t insert unexpected text, the docstring of C-x C-t! 203 | 204 | (defmacro rdd-defun (repl name args-list doc-string &rest body) 205 | "Defun NAME with ARGS_LIST and DOC-STRING and BODY, in the REPL namespace. 206 | 207 | This is like `defun' but the source code, excluding DOC-STRING, 208 | is appended to the function's docstring." 209 | ;; TODO: fun-name keyword of main rdd.el macro should be replace by a new :name keyword, 210 | ;; then fun-name is just “⟨name⟩-eval”, and derived methods are “⟨name⟩-eval-⟨derivation⟩”. 211 | (let* ( (name-sym (intern (format "%s" name))) 212 | (fun-name-sym (intern (format "%s" (rdd@ repl fun-name)))) 213 | (command-name (if (equal name-sym fun-name-sym) name-sym (intern (format "%s-%s" fun-name-sym name-sym))))) 214 | `(progn 215 | (setf (rdd@ ,repl ,command-name) 216 | ;; restart repl, [then send to repl --does not work since REPLs take a sec 217 | ;; to load. That's OK, not a deal-breaker!] 218 | (-cons* 'defun (quote ,command-name) (quote ,args-list) (quote ,body))) 219 | 220 | (eval (rdd@ ,repl ,command-name)) 221 | 222 | ;; TODO: Add to requires 223 | (require 'cl-extra) 224 | 225 | 226 | 227 | (put (quote ,command-name) 228 | 'function-documentation 229 | (concat 230 | ,doc-string 231 | (format "\n\n\t\t﴾Source Code of “%s”﴿\n\n" (quote ,command-name)) 232 | (with-temp-buffer (cl-prettyprint (rdd@ ,repl ,command-name)) (buffer-substring-no-properties (point-min) (point-max))) 233 | ))))) 234 | ;; MA: For testing. 235 | ;; (insert (pp-to-string (macroexpand '(rdd-defun "bash" restart-NEW1 (x y) "6 hola amigos ~ Restart the REPL process." (interactive) 'BYE)))) 236 | 237 | ;; Intended usage sites: 238 | 239 | - ;; restart repl, [then send to repl --does not work since REPLs take a sec 240 | - ;; to load. That's OK, not a deal-breaker!] 241 | - (defun ,(intern (format "%s-restart" (rdd@ repl fun-name))) () 242 | "Restart the REPL process." 243 | (interactive) 244 | (kill-buffer (process-buffer (rdd@ ,repl process))) 245 | (repl-driven-development (rdd@ ,repl keybinding) 246 | (rdd@ ,repl cmd) 247 | :prompt (rdd@ ,repl prompt) 248 | :docs (rdd@ ,repl docs) 249 | :init (rdd@ ,repl init) 250 | :blink (rdd@ ,repl blink))) 251 | 252 | ;; and 253 | 254 | (eval (rdd-defun 255 | ,repl 256 | ,(rdd@ repl fun-name) 257 | (region-beg region-end) 258 | ,(repl-driven-development--make-eval-function-docstring repl) 259 | (interactive "r") 260 | 261 | (require 'pulsar) 262 | (-let [pulsar-face (rdd@ ,repl blink)] 263 | (pulsar-mode +1) 264 | (pulsar-pulse-line)) 265 | 266 | (pcase current-prefix-arg 267 | (0 (,(intern (format "%s-display-output" 268 | (rdd@ repl fun-name))))) 269 | (-1 (,(intern (format "%s-restart" (rdd@ repl fun-name))))) 270 | ;; ('(4) (insert " " output)) ;; C-u ;; handled when we actually have 271 | ;; the output; see the process filter below 272 | ('(16) ;; C-u C-u ⇒ documentation lookup 273 | (,(intern (format "%s-docs-at-point" (rdd@ repl fun-name))))) 274 | (_ 275 | (when (called-interactively-p 'interactive) 276 | (if (use-region-p) 277 | (deactivate-mark) 278 | (beginning-of-line) 279 | (setq region-beg (point)) 280 | (end-of-line) 281 | (setq region-end (point)))) 282 | (setf (rdd@ ,repl input/start) region-beg) 283 | (setf (rdd@ ,repl input/end) region-end) 284 | (,(intern (format "%s-string" (rdd@ repl fun-name))) 285 | (s-trim-left (buffer-substring-no-properties 286 | region-beg 287 | region-end)))))))))) 288 | --------------------------------------------------------------------------------