├── .github ├── issue_template.md ├── pull_request_template.md └── workflows │ └── test.yml ├── .gitignore ├── COPYING ├── Makefile ├── NEWS ├── README ├── guix-snakemake-environment ├── snakemake-file-targets ├── snakemake-mode.el ├── snakemake-test.el └── snakemake.el /.github/issue_template.md: -------------------------------------------------------------------------------- 1 | **Note**: This repository is a mirror. The primary repository is at 2 | . 3 | 4 | Please send bug reports, patches, questions, and comments as 5 | plain-text email to . Messages that 6 | include this address are public and archived at 7 | . 8 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | issue_template.md -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | on: push 3 | jobs: 4 | test: 5 | runs-on: ubuntu-latest 6 | strategy: 7 | matrix: 8 | emacs_version: 9 | - 27.2 10 | - 28.2 11 | - 29.4 12 | - snapshot 13 | steps: 14 | - name: Install emacs 15 | uses: purcell/setup-emacs@master 16 | with: 17 | version: ${{ matrix.emacs_version }} 18 | - name: Check out seq 19 | if: matrix.emacs_version == '27.2' || matrix.emacs_version == '28.2' 20 | # Note: This relies on Transient's Makefile looking in this spot 21 | # (relative to Transient checkout) by default. 22 | run: | 23 | git clone --branch=externals/seq --depth=1 \ 24 | https://git.savannah.gnu.org/git/emacs/elpa.git seq 25 | echo "SEQ_DIR=$PWD/seq" >>$GITHUB_ENV 26 | - name: Check out compat 27 | uses: actions/checkout@v4 28 | with: 29 | repository: emacs-compat/compat 30 | path: compat 31 | - name: Check out transient 32 | uses: actions/checkout@v4 33 | with: 34 | repository: magit/transient 35 | path: transient 36 | - name: Check out snakemake-mode 37 | uses: actions/checkout@v4 38 | with: 39 | repository: kyleam/snakemake-mode 40 | path: snakemake-mode 41 | - name: Set up Python 42 | uses: actions/setup-python@v4 43 | with: 44 | python-version: 3.12 45 | - name: Install Snakemake 46 | run: | 47 | python -m pip install --upgrade pip 48 | pip install snakemake 49 | - name: Build compat 50 | run: make -C compat compile 51 | - name: Build transient 52 | run: make -C transient lisp 53 | - name: Set envvars for dependencies 54 | run: | 55 | echo "COMPAT_DIR=$PWD/compat" >>$GITHUB_ENV 56 | echo "TRANSIENT_DIR=$PWD/transient/lisp" >>$GITHUB_ENV 57 | - name: Build snakemake-mode 58 | run: make -C snakemake-mode all 59 | - name: Test snakemake-mode 60 | run: make -C snakemake-mode test 61 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *-autoloads.el 2 | *.elc 3 | /.deps 4 | /config.mk 5 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | -include config.mk 3 | 4 | SEQ_DIR ?= /dev/null 5 | COMPAT_DIR ?= /dev/null 6 | TRANSIENT_DIR ?= /dev/null 7 | 8 | LOAD_PATH = -L $(SEQ_DIR) -L $(COMPAT_DIR) -L $(TRANSIENT_DIR) -L . 9 | EMACS = emacs 10 | BATCH = $(EMACS) -Q --batch $(LOAD_PATH) 11 | 12 | els := snakemake.el snakemake-mode.el 13 | elcs := $(patsubst %.el, %.elc, $(els)) 14 | AUTOLOADS_FILE = snakemake-autoloads.el 15 | 16 | .PHONY: all 17 | all: $(elcs) $(AUTOLOADS_FILE) 18 | 19 | .PHONY: test 20 | test: 21 | @$(BATCH) -l snakemake-test \ 22 | --eval "(ert-run-tests-batch-and-exit '(not (tag interactive)))" 23 | 24 | $(AUTOLOADS_FILE): $(els) 25 | @$(BATCH) -l autoload --eval \ 26 | "(let ((make-backup-files nil) \ 27 | (generated-autoload-file \"$(CURDIR)/$@\")) \ 28 | (update-directory-autoloads \"$(CURDIR)/\"))" 29 | 30 | %.elc: %.el 31 | @$(BATCH) -f batch-byte-compile $< 32 | 33 | .PHONY: clean 34 | clean: 35 | $(RM) $(elcs) $(AUTOLOADS_FILE) 36 | 37 | .PHONY: sign-tar 38 | sign-tar: 39 | tag="$$(git describe --abbrev=0)"; \ 40 | object=$$(git archive --format tar \ 41 | --prefix "snakemake-mode-$${tag#v}/" "$$tag" | \ 42 | gpg --output - --armor --detach-sign | \ 43 | git hash-object -w --stdin); \ 44 | git notes --ref=refs/notes/signatures/tar add -C "$$object" "$$tag" 45 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | NEWS -- history of user-visible changes -*- mode: org; -*- 2 | 3 | * master (unreleased) 4 | 5 | - The minimum required Emacs is now 27.1. 6 | 7 | - The 'localrule' keyword (new in Snakemake v7.25.0) is now 8 | recognized. 9 | 10 | * v2.0.0 11 | 12 | - Support for Emacs versions older than 26.1 has been dropped. 13 | 14 | - The Magit Popup command ~snakemake-popup~ has been rewritten as a 15 | transient, ~snakemake~. snakemake.el now depends on Transient 16 | rather than Magit Popup. 17 | 18 | - The ~--jobs~ argument in the ~snakemake~ transient has been replaced 19 | by ~--cores~ to follow upstream changes. ~--cores~ is bound to 20 | ~-c~, and ~--use-conda~ has been moved from ~-c~ to ~-C~. 21 | 22 | - Snakemake's new module syntax (added in v6.0) is now supported. 23 | 24 | - Additional Snakemake keywords are now recognized: 'containerized' 25 | (new in Snakemake v6.0.0), 'default_target' (new in Snakemake 26 | v6.15.0), 'handover' (new in Snakemake v6.2.0), 'name' (new in 27 | Snakemake v5.31.0), and 'notebook' (new in Snakemake v5.10.0). 28 | 29 | - The ~snakemake-compile~ command, marked obsolete in v1.2.0 in favor 30 | of ~snakemake-build~, has been removed. 31 | 32 | * v1.8.0 33 | 34 | - Additional Snakemake keywords are now recognized: 'envmodules' (new 35 | in Snakemake v5.9.0), 'envvars' (new in v5.11.0), 'container' (new 36 | in v5.11.0), and 'cache' (new in v5.12.0). 37 | 38 | * v1.7.0 39 | 40 | - The 'multiext' built-in (new in Snakemake v5.8.2) is now recognized. 41 | 42 | - The font-lock handling has been updated to be compatible with 43 | python.el changes in Emacs 27. 44 | 45 | * v1.6.0 46 | 47 | - Checkpoints (new in Snakemake v5.4) are now recognized. 48 | 49 | * v1.5.0 50 | 51 | - ~snakemake-popup~ now includes ~--use-conda~. 52 | - Additional Snakemake keywords are now recognized: 'group' keyword 53 | (new in Snakemake v5.0), report (both the keyword and function; new 54 | in Snakemake v5.1), and 'localrules'. 55 | 56 | * v1.4.0 57 | 58 | - The 'cwl' keyword (new in Snakemake v4.8.0), 'pipe' (new in 59 | Snakemake v5.2.0), 'directory' (new in Snakemake v5.2.0), and 60 | 'ancient' built-in are now recognized. 61 | 62 | - The minimum Emacs version is now specified as 24.5. Although the 63 | previous specification was a looser "24", Snakemake's indentation 64 | function is incompatible with Emacs 24.4 and below. 65 | 66 | * v1.3.0 67 | 68 | - The 'singularity' keyword (new in Snakemake v4.2.0) is now 69 | recognized. 70 | 71 | - ".smk" has been added as a suffix for Snakemake files. 72 | 73 | * v1.2.0 74 | 75 | - ~snakemake-popup~ commands learned to call Snakemake through a 76 | terminal rather than through ~compile~. This allows a persistent 77 | environment to be maintained between Snakemake calls, which is 78 | useful for running Snakemake in isolated environments created by 79 | tools like Guix. 80 | 81 | * v1.1.0 82 | 83 | - The 'conda' keyword (new in Snakemake v3.9.0) is now recognized. 84 | 85 | * v1.0.0 86 | 87 | - The 'wildcard_constraints' keyword (new in Snakemake v3.8.0) is now 88 | recognized. 89 | 90 | * v0.5.0 91 | 92 | - New command ~snakemake-graph-this-file~ displays the graph for the 93 | first rule of the current file. Similar functionality is available 94 | from ~snakemake-graph~, which supports graphing any target in the 95 | file, but you may find the new command useful if your workflow 96 | involves frequently using Snakemake's ~--snakefile~ option. 97 | 98 | - New function ~snakemake-mode-setup-mmm~ configures MMM mode to 99 | highlight string-embedded R code. (Contributed by Endre Bakken 100 | Stovner.) 101 | 102 | - ".rules" has been added as a suffix for Snakemake files. 103 | 104 | - ~beginning-of-defun~ and ~end-of-defun~ implementations for rule 105 | blocks are now defined. 106 | 107 | - Rule blocks are now recognized by ~add-log-current-defun~. 108 | 109 | * v0.4.0 110 | 111 | - New library snakemake.el provides a popup interface for running 112 | Snakemake from Emacs. 113 | 114 | - New command ~snakemake-graph~ displays the graph of dependencies for 115 | a rule. (Thanks to Endre Bakken Stovner for the idea and initial 116 | implementation.) 117 | 118 | - ".snakefile" has been added as a suffix for Snakemake files. 119 | 120 | - Anonymous rule blocks are now supported. 121 | 122 | - The 'wrapper' keyword is now recognized. 123 | 124 | * v0.3.0 125 | 126 | - The keywords 'configfile', 'onsuccess', 'onerror', 'log', and 127 | 'benchmark', 'script', 'shadow', as well as the 'touch' function, 128 | are now recognized. 129 | 130 | - Indentation for field values starting on the line below a field key 131 | is now supported. New variable ~snakemake-indent-value-offset~ 132 | controls the offset for the value and replaces the variable 133 | ~snakemake-indent-run-offset~. If these values are continued on 134 | another line, indentation cycling now includes a step that indents 135 | according to Python mode. 136 | 137 | - ~snakemake-compile-rule~ can now pass the '--touch' flag to 138 | snakemake. 139 | 140 | - The Imenu index now includes Python mode items in addition to rule 141 | blocks. 142 | 143 | - Errors in Snakefiles are now highlighted in complilation buffers. 144 | 145 | * v0.2.0 146 | 147 | - Both the executable and flags for the Snakemake ~compile-command~ are 148 | now customizable. 149 | 150 | - New command ~snakemake-compile-rule~ runs Snakemake with the rule at 151 | point as the target. 152 | 153 | - Subworkflow blocks are now supported. 154 | 155 | - The 'ruleorder' keyword is now recognized. 156 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | 2 | Emacs support for Snakemake 3 | =========================== 4 | 5 | This repo contains two Emacs libraries for Snakemake [^1]. 6 | 7 | * snakemake-mode.el provides a major mode for editing Snakemake 8 | files. 9 | 10 | * snakemake.el defines a transient [^2] for calling Snakemake. 11 | 12 | See the header of each file for more information. 13 | 14 | Both of these are available on MELPA under the snakemake-mode package: 15 | 16 | https://melpa.org/#/snakemake-mode 17 | 18 | [^1]: https://snakemake.readthedocs.io/en/stable/ 19 | [^2]: https://magit.vc/manual/transient/ 20 | 21 | 22 | Contributing 23 | ------------ 24 | 25 | Bug reports, patches, questions, and other feedback are welcome. 26 | Please send a plain-text email to 27 | 28 | snakemake-mode@inbox.kyleam.com 29 | 30 | Messages that include this address are public and available as 31 | public-inbox [^3] archives at 32 | 33 | https://inbox.kyleam.com/snakemake-mode/ 34 | 35 | [^3]: https://public-inbox.org/README 36 | -------------------------------------------------------------------------------- /guix-snakemake-environment: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | ### Set up a pure environment for snakemake.el commands 4 | ### 5 | ### If `snakemake-shell-file-name' points to this file, 6 | ### `snakemake-term-start' will start a terminal with an environment 7 | ### that exposes snakemake and the dependencies defined in "manifest.scm", 8 | ### a file which should be located in the same directory as the 9 | ### current Snakefile. 10 | 11 | guix environment --pure -m manifest.scm --ad-hoc snakemake 12 | -------------------------------------------------------------------------------- /snakemake-file-targets: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Print all concrete file targets. 3 | 4 | Usage: snakemake-file-targets [SNAKEFILE] 5 | """ 6 | import sys 7 | from snakemake.workflow import Workflow 8 | 9 | if len(sys.argv) == 1: 10 | snakefile = "Snakefile" 11 | elif len(sys.argv) == 2 and sys.argv[1] not in ["-h", "--help"]: 12 | snakefile = sys.argv[1] 13 | else: 14 | sys.exit(__doc__) 15 | 16 | workflow = Workflow(snakefile=snakefile) 17 | workflow.include(snakefile) 18 | 19 | for rule in workflow.rules: 20 | for fname in rule.output: 21 | if not callable(fname) and not fname.contains_wildcard(): 22 | try: 23 | print(fname) 24 | except BrokenPipeError: 25 | sys.exit(0) 26 | -------------------------------------------------------------------------------- /snakemake-mode.el: -------------------------------------------------------------------------------- 1 | ;;; snakemake-mode.el --- Major mode for editing Snakemake files -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright all Snakemake mode contributors 4 | 5 | ;; Author: Kyle Meyer 6 | ;; URL: https://git.kyleam.com/snakemake-mode/about 7 | ;; Keywords: tools 8 | ;; Version: 2.0.0 9 | ;; Package-Requires: ((emacs "27.1") (transient "0.3.0")) 10 | 11 | ;; This program is free software; you can redistribute it and/or modify 12 | ;; it under the terms of the GNU General Public License as published by 13 | ;; the Free Software Foundation; either version 3, or (at your option) 14 | ;; any later version. 15 | ;; 16 | ;; This program is distributed in the hope that it will be useful, 17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | ;; GNU General Public License for more details. 20 | ;; 21 | ;; You should have received a copy of the GNU General Public License 22 | ;; along with GNU Emacs. If not, see . 23 | 24 | ;;; Commentary: 25 | 26 | ;; Snakemake mode provides support for editing Snakemake [1] files. It 27 | ;; builds on Python mode to provide fontification, indentation, and 28 | ;; imenu indexing for Snakemake's rule blocks. 29 | ;; 30 | ;; See also snakemake.el, which is packaged with snakemake-mode.el and 31 | ;; provides an interface for running Snakemake commands. 32 | ;; 33 | ;; If Snakemake mode is installed from MELPA, no additional setup is 34 | ;; required. It will be loaded the first time a file named 'Snakefile' 35 | ;; is opened. 36 | ;; 37 | ;; Otherwise, put snakemake-mode.el in your `load-path' and add 38 | ;; 39 | ;; (require 'snakemake-mode) 40 | ;; 41 | ;; to your initialization file. 42 | ;; 43 | ;; snakemake-mode.el also includes support for highlighting embedded R 44 | ;; code. See the snakemake-mode-setup-mmm function documentation for how. 45 | ;; 46 | ;; [1] https://snakemake.github.io/ 47 | 48 | ;;; Code: 49 | 50 | (require 'cl-lib) 51 | (require 'python) 52 | 53 | 54 | ;;; Customization 55 | 56 | ;;;###autoload 57 | (defgroup snakemake-mode nil 58 | "Support for Snakemake files" 59 | :group 'tools) 60 | 61 | (defcustom snakemake-mode-hook nil 62 | "Hook run when entering `snakemake-mode'." 63 | :type 'hook) 64 | 65 | (defcustom snakemake-indent-field-offset 4 66 | "Offset for field indentation." 67 | :type 'integer) 68 | 69 | (defcustom snakemake-indent-value-offset 4 70 | "Offset for field values that the line below the field key." 71 | :type 'integer) 72 | 73 | 74 | ;;; Regexp 75 | 76 | (defmacro snakemake-rx (&rest regexps) 77 | "Specialized `rx' for Snakemake mode." 78 | ;; Modified from `python-rx'. 79 | `(rx-let ((named-rule (and (group 80 | symbol-start 81 | (or "checkpoint" "module" "rule" "subworkflow")) 82 | " " 83 | (group (one-or-more 84 | (or (syntax word) (syntax symbol)))))) 85 | (anon-rule (and symbol-start "rule")) 86 | (field-key (and symbol-start 87 | (or "benchmark" 88 | "cache" 89 | "conda" 90 | "container" 91 | "cwl" 92 | "default_target" 93 | "envmodules" 94 | "group" 95 | "handover" 96 | "input" 97 | "localrule" 98 | "log" 99 | "message" 100 | "name" 101 | "notebook" 102 | "output" 103 | "params" 104 | "priority" 105 | "resources" 106 | "run" 107 | "script" 108 | "shadow" 109 | "shell" 110 | "singularity" 111 | "threads" 112 | "version" 113 | "wildcard_constraints" 114 | "wrapper" 115 | ;; Keys for subworkflow blocks 116 | "configfile" 117 | "snakefile" 118 | "workdir") 119 | symbol-end)) 120 | (sm-command (and symbol-start 121 | (or "configfile" 122 | "container" 123 | "containerized" 124 | "envvars" 125 | "include" 126 | "localrules" 127 | "onerror" 128 | "onsuccess" 129 | "report" 130 | "ruleorder" 131 | "singularity" 132 | "wildcard_constraints" 133 | "workdir") 134 | symbol-end)) 135 | (sm-builtin (and symbol-start 136 | (or "ancient" 137 | "checkpoints" 138 | "directory" 139 | "dynamic" 140 | "expand" 141 | "input" 142 | "multiext" 143 | "output" 144 | "params" 145 | "pipe" 146 | "protected" 147 | "report" 148 | "shell" 149 | "temp" 150 | "touch" 151 | "unpack" 152 | "wildcards") 153 | symbol-end))) 154 | (rx ,@regexps))) 155 | 156 | (defconst snakemake-rule-or-subworkflow-re 157 | (snakemake-rx line-start (zero-or-more space) 158 | (or named-rule (group anon-rule)) 159 | (zero-or-more space) ":") 160 | "Regexp matching a rule or subworkflow.") 161 | 162 | 163 | ;;; Info and navigation 164 | 165 | (defun snakemake-block-info () 166 | "Return description of rule or subworkflow block at point." 167 | (save-excursion 168 | (save-restriction 169 | (widen) 170 | (let ((pos (point))) 171 | (end-of-line) 172 | (and (re-search-backward snakemake-rule-or-subworkflow-re nil t) 173 | (let ((type (or (match-string-no-properties 1) 174 | "rule")) 175 | (name (match-string-no-properties 2)) 176 | (start (or (match-beginning 1) 177 | (match-beginning 3))) 178 | (rule-indent (current-indentation)) 179 | end) 180 | (beginning-of-line) 181 | (forward-line) 182 | (while (and (or (< rule-indent (current-indentation)) 183 | (looking-at-p "^\\s-*$")) 184 | (or (not (eobp)) 185 | (progn (setq end (point-max)) 186 | nil))) 187 | (setq end (line-end-position)) 188 | (forward-line)) 189 | (when (<= start pos end) 190 | (list type name start end)))))))) 191 | 192 | (defun snakemake-beginning-of-block (&optional arg) 193 | "Move to beginning of rule block. 194 | With ARG, do it that many times. Negative ARG signals to move 195 | forward rather than backward." 196 | (when (or (null arg) (zerop arg)) 197 | (setq arg 1)) 198 | (funcall (if (> arg 0) 199 | #'re-search-backward 200 | (lambda (&rest args) 201 | (end-of-line) 202 | (prog1 (apply #'re-search-forward args) 203 | (beginning-of-line)))) 204 | snakemake-rule-or-subworkflow-re 205 | nil 'move (abs arg)) 206 | (looking-at-p snakemake-rule-or-subworkflow-re)) 207 | 208 | (defun snakemake-end-of-block () 209 | "Move to end of rule or subworkflow block." 210 | (let ((end (nth 3 (snakemake-block-info)))) 211 | (when end (goto-char end)))) 212 | 213 | (defun snakemake-beginning-of-defun (&optional arg) 214 | "Move to beginning of current rule block or function. 215 | With ARG, do it that many times. Negative ARG signals to move 216 | forward rather than backward." 217 | (when (or (null arg) (zerop arg)) 218 | (setq arg 1)) 219 | (let ((choose-fn (if (> arg 0) #'max #'min)) 220 | (cands (delq nil 221 | (mapcar 222 | (lambda (f) 223 | (save-excursion (and (funcall f arg) (point)))) 224 | (list #'snakemake-beginning-of-block 225 | #'python-nav-beginning-of-defun))))) 226 | (cond (cands 227 | (goto-char (apply choose-fn cands))) 228 | ((> arg 0) 229 | (goto-char (point-min)) 230 | nil) 231 | (t 232 | (goto-char (point-max)) 233 | nil)))) 234 | 235 | (defun snakemake-end-of-defun () 236 | "Move to end of current rule block or function." 237 | (or (snakemake-end-of-block) 238 | (python-nav-end-of-defun))) 239 | 240 | (defun snakemake-block-or-defun-name () 241 | "Return name of current rule or function. 242 | This function is appropriate to use as the value of 243 | `add-log-current-defun-function'." 244 | (or (nth 1 (snakemake-block-info)) 245 | (python-info-current-defun))) 246 | 247 | 248 | ;;; Indentation 249 | 250 | (defun snakemake--calculate-indentation (&optional previous) 251 | "Return indentation offset for the current line. 252 | 253 | A non-nil value for PREVIOUS indicates that the previous command 254 | was an indentation command. 255 | 256 | When Python mode should handle the indentation, a nil value is 257 | returned." 258 | (when (memq (car (python-indent-context)) 259 | (list :after-line 260 | ;; If point is on a value line following a naked 261 | ;; field value, `python-indent-context' returns 262 | ;; :after-block-start. 263 | :after-block-start)) 264 | (let* ((initial-indent (current-indentation)) 265 | (goto-first-p (or (not previous) (zerop initial-indent)))) 266 | (save-excursion 267 | (save-restriction 268 | (widen) 269 | (beginning-of-line) 270 | (if (looking-at-p (snakemake-rx 271 | line-start (zero-or-more space) 272 | (or (and field-key (zero-or-more space) ":") 273 | (or "\"\"\"" "'''")))) 274 | (and goto-first-p 275 | (let (rule-indent) 276 | (while (not (or rule-indent (bobp))) 277 | (forward-line -1) 278 | (when (looking-at-p snakemake-rule-or-subworkflow-re) 279 | (setq rule-indent (current-indentation)))) 280 | (and rule-indent 281 | (+ rule-indent snakemake-indent-field-offset)))) 282 | ;; We need to look back to determine indentation. 283 | (skip-chars-backward " \t\n") 284 | (beginning-of-line) 285 | (cond 286 | ((looking-at-p (snakemake-rx 287 | line-start (zero-or-more space) 288 | (or named-rule anon-rule 289 | field-key sm-command) 290 | (zero-or-more space) ":" (zero-or-more space) 291 | (zero-or-one (and "#" (zero-or-more not-newline))) 292 | line-end)) 293 | (let ((above-indent (current-indentation))) 294 | (cond (goto-first-p 295 | (+ above-indent snakemake-indent-value-offset)) 296 | ((< above-indent initial-indent) 297 | above-indent)))) 298 | ((looking-at (snakemake-rx 299 | line-start (zero-or-more space) 300 | field-key 301 | (zero-or-more space) ":" (zero-or-more space))) 302 | (let ((above-indent (current-indentation))) 303 | (cond (goto-first-p 304 | (- (match-end 0) (line-beginning-position))) 305 | ((< above-indent initial-indent) 306 | above-indent)))) 307 | ((save-excursion 308 | (let ((above-indent (current-indentation)) 309 | field-indent) 310 | (when (> above-indent 0) 311 | (while (and (not (bobp)) 312 | (or (= above-indent 313 | (setq field-indent (current-indentation))) 314 | (looking-at-p "^\\s-*$"))) 315 | (forward-line -1))) 316 | (and (looking-at (snakemake-rx 317 | line-start (zero-or-more space) 318 | (group field-key) 319 | (zero-or-more space) ":" 320 | (zero-or-more space) 321 | (group (zero-or-more not-newline)))) 322 | (not (equal (match-string-no-properties 1) 323 | "run")) 324 | (cond (goto-first-p 325 | (let ((field-val (match-string-no-properties 2))) 326 | (if (or (equal field-val "") 327 | (string-match-p "\\`#" field-val)) 328 | above-indent 329 | (- (match-beginning 2) (line-beginning-position))))) 330 | ((< field-indent initial-indent) 331 | field-indent))))))))))))) 332 | 333 | (defun snakemake-indent-line (&optional previous) 334 | "Snakemake mode variant of `python-indent-line'." 335 | (let ((follow-indentation-p 336 | (and (<= (line-beginning-position) (point)) 337 | (>= (+ (line-beginning-position) 338 | (current-indentation)) 339 | (point))))) 340 | (save-excursion 341 | (indent-line-to 342 | (or (snakemake--calculate-indentation previous) 343 | (python-indent-calculate-indentation previous)))) 344 | (when follow-indentation-p 345 | (back-to-indentation)))) 346 | 347 | (defun snakemake-indent-line-function () 348 | "Snakemake mode variant of `python-indent-line-function'." 349 | (snakemake-indent-line 350 | (and (memq this-command python-indent-trigger-commands) 351 | (eq last-command this-command)))) 352 | 353 | 354 | ;;; Imenu 355 | 356 | (defun snakemake-imenu-create-index () 357 | "Create Imenu index for rule blocks. 358 | If `python-imenu-create-index' returns a non-nil value, also 359 | include these results and append a \"(rule)\" to the index 360 | label." 361 | (let ((py-index (python-imenu-create-index)) 362 | (sm-index (snakemake--imenu-build-rule-index))) 363 | (if py-index 364 | (append (mapcar (lambda (x) 365 | (cons (concat (car x) " (rule)") (cdr x))) 366 | sm-index) 367 | py-index) 368 | sm-index))) 369 | 370 | (defun snakemake--imenu-build-rule-index () 371 | (goto-char (point-min)) 372 | (let (index) 373 | (while (re-search-forward snakemake-rule-or-subworkflow-re nil t) 374 | (push (cons (match-string-no-properties 2) 375 | (save-excursion (beginning-of-line) 376 | (point-marker))) 377 | index)) 378 | (nreverse index))) 379 | 380 | 381 | ;;; Embedded language syntax-highlighting 382 | 383 | (declare-function mmm-add-classes "mmm-vars") 384 | (declare-function mmm-add-mode-ext-class "mmm-vars") 385 | 386 | (defun snakemake-mode-setup-mmm () 387 | "Set up MMM mode to highlight embedded R code. 388 | 389 | You must have the R-strings either within a R(\"\"\" \"\"\") function 390 | call or a code block delimited with \"\"\"#r and \"\"\". (Triple 391 | single-quotes also accepted.) 392 | 393 | For automatic highlighting of embedded regions, you need to set 394 | `mmm-global-mode' to `maybe'." 395 | (unless (require 'mmm-mode nil t) 396 | (user-error "You need to install mmm-mode")) 397 | 398 | (unless (bound-and-true-p mmm-global-mode) 399 | (display-warning 'snakemake-mode "To get automatic syntax highlighting of 400 | embedded R, you need to set mmm-global-mode to a non-nil value such as `maybe'.")) 401 | 402 | (mmm-add-classes 403 | '((snakemake-R-call-double 404 | :submode R-mode 405 | :front ".*R\(\"\"\"" 406 | :back ".*\"\"\"\)"))) 407 | 408 | (mmm-add-classes 409 | '((snakemake-R-call-regular 410 | :submode R-mode 411 | :front ".*R\('''" 412 | :back ".*'''\)"))) 413 | 414 | (mmm-add-classes 415 | '((snakemake-R-string-double 416 | :submode R-mode 417 | :front ".*\"\"\" *# *[rR]" 418 | :back ".*\"\"\""))) 419 | 420 | (mmm-add-classes 421 | '((snakemake-R-string-regular 422 | :submode R-mode 423 | :front ".*''' *# *[rR]" 424 | :back ".*'''"))) 425 | 426 | (mmm-add-mode-ext-class 'snakemake-mode nil 'snakemake-R-call-double) 427 | (mmm-add-mode-ext-class 'snakemake-mode nil 'snakemake-R-call-regular) 428 | (mmm-add-mode-ext-class 'snakemake-mode nil 'snakemake-R-string-double) 429 | (mmm-add-mode-ext-class 'snakemake-mode nil 'snakemake-R-string-regular)) 430 | 431 | 432 | ;;; Mode 433 | 434 | (defvar snakemake--font-lock-keywords 435 | `((,snakemake-rule-or-subworkflow-re 436 | (1 font-lock-keyword-face nil 'lax) 437 | (2 font-lock-function-name-face nil 'lax) 438 | (3 font-lock-keyword-face nil 'lax)) 439 | (,(snakemake-rx line-start (one-or-more space) 440 | (group field-key) 441 | (zero-or-more space) ":") 442 | 1 font-lock-type-face) 443 | (,(snakemake-rx line-start (zero-or-more space) 444 | (group sm-command) 445 | (zero-or-more space) ":") 446 | 1 font-lock-keyword-face) 447 | (,(rx line-start (group "use rule ")) 1 font-lock-keyword-face) 448 | (,(snakemake-rx (group sm-builtin)) 1 font-lock-builtin-face))) 449 | 450 | (defvar snakemake-font-lock-keywords) 451 | (if (bound-and-true-p python-font-lock-keywords-level-1) 452 | (with-no-warnings 453 | ;; In Emacs 27 `python-font-lock-keywords' was split up into 454 | ;; different decoration levels. 455 | (defvar snakemake-font-lock-keywords-level-1 456 | (append snakemake--font-lock-keywords 457 | python-font-lock-keywords-level-1)) 458 | (defvar snakemake-font-lock-keywords-level-2 459 | (append snakemake--font-lock-keywords 460 | python-font-lock-keywords-level-2)) 461 | (defvar snakemake-font-lock-keywords-maximum-decoration 462 | (append snakemake--font-lock-keywords 463 | python-font-lock-keywords-maximum-decoration)) 464 | (setq snakemake-font-lock-keywords 465 | ;; Mirrors `python-font-lock-keywords'. 466 | '(snakemake-font-lock-keywords-level-1 467 | snakemake-font-lock-keywords-level-1 468 | snakemake-font-lock-keywords-level-2 469 | snakemake-font-lock-keywords-maximum-decoration))) 470 | (setq snakemake-font-lock-keywords 471 | (append snakemake--font-lock-keywords python-font-lock-keywords))) 472 | 473 | ;;;###autoload 474 | (define-derived-mode snakemake-mode python-mode "Snakemake" 475 | "Mode for editing Snakemake files." 476 | (set (make-local-variable 'imenu-create-index-function) 477 | #'snakemake-imenu-create-index) 478 | (set (make-local-variable 'indent-line-function) 'snakemake-indent-line-function) 479 | (set (make-local-variable 'indent-region-function) nil) 480 | 481 | (set (make-local-variable 'beginning-of-defun-function) 482 | #'snakemake-beginning-of-defun) 483 | (set (make-local-variable 'end-of-defun-function) 484 | #'snakemake-end-of-defun) 485 | (set (make-local-variable 'add-log-current-defun-function) 486 | #'snakemake-block-or-defun-name) 487 | 488 | (set (make-local-variable 'font-lock-defaults) 489 | (cons snakemake-font-lock-keywords (cdr font-lock-defaults)))) 490 | 491 | ;;;###autoload 492 | (add-to-list 'auto-mode-alist '("Snakefile\\'" . snakemake-mode)) 493 | ;;;###autoload 494 | (add-to-list 'auto-mode-alist '("\\.\\(?:sm\\)?rules\\'" . snakemake-mode)) 495 | ;;;###autoload 496 | (add-to-list 'auto-mode-alist '("\\.smk\\'" . snakemake-mode)) 497 | ;;;###autoload 498 | (add-to-list 'auto-mode-alist '("\\.snakefile\\'" . snakemake-mode)) 499 | 500 | (provide 'snakemake-mode) 501 | ;;; snakemake-mode.el ends here 502 | -------------------------------------------------------------------------------- /snakemake-test.el: -------------------------------------------------------------------------------- 1 | ;;; snakemake-test.el --- Test snakemake{,-mode}.el 2 | 3 | ;; Copyright all Snakemake mode contributors 4 | 5 | ;; Author: Kyle Meyer 6 | 7 | ;; This program is free software; you can redistribute it and/or modify 8 | ;; it under the terms of the GNU General Public License as published by 9 | ;; the Free Software Foundation, either version 3 of the License, or 10 | ;; (at your option) any later version. 11 | 12 | ;; This program is distributed in the hope that it will be useful, 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | ;; GNU General Public License for more details. 16 | 17 | ;; You should have received a copy of the GNU General Public License 18 | ;; along with this program. If not, see . 19 | 20 | ;;; Code: 21 | 22 | (require 'cl-lib) 23 | (require 'snakemake-mode) 24 | (require 'snakemake) 25 | (require 'ert) 26 | 27 | ;; This is modified from `org-tests.el' (55c0708). 28 | (defmacro snakemake-with-temp-text (text &rest body) 29 | "Run body in a temporary Snakemake mode buffer. 30 | 31 | Fill the buffer with TEXT. If the string \"\" appears in 32 | TEXT then remove it and place the point there before running 33 | BODY, otherwise place the point at the beginning of the inserted 34 | text. 35 | 36 | Also, mute messages." 37 | (declare (indent 1)) 38 | `(cl-letf (((symbol-function 'message) (lambda (&rest args) nil))) 39 | (let ((inside-text (if (stringp ,text) ,text (eval ,text)))) 40 | (with-temp-buffer 41 | (snakemake-mode) 42 | (let ((point (string-match "" inside-text))) 43 | (if point 44 | (progn 45 | (insert (replace-match "" nil nil inside-text)) 46 | (goto-char (1+ (match-beginning 0)))) 47 | (insert inside-text) 48 | (goto-char (point-min)))) 49 | ,@body)))) 50 | (def-edebug-spec snakemake-with-temp-text (form body)) 51 | 52 | (defmacro snakemake-with-temp-dir (&rest body) 53 | "Run BODY in a temporary directory with Snakefile. 54 | `snakemake-test-dir' is bound to top-level directory." 55 | (declare (indent 0) (debug t)) 56 | `(cl-letf (((symbol-function 'message) (lambda (&rest args) nil))) 57 | (let* ((snakemake-test-dir (file-name-as-directory 58 | (make-temp-file "sm-test-dir" t))) 59 | (snakemake-root-dir-function `(lambda () ,snakemake-test-dir))) 60 | (unwind-protect 61 | (let ((default-directory snakemake-test-dir)) 62 | (mkdir "subdir") 63 | (write-region "\ 64 | 65 | rule aa: 66 | output: \"aa.out\" 67 | shell: \"echo aa.content > {output}\" 68 | 69 | rule bb: 70 | input: \"aa.out\" 71 | output: \"bb.out\" 72 | shell: \"cat {input} > {output}\" 73 | 74 | rule cc_wildcards: 75 | input: \"bb.out\" 76 | output: \"{name}.outwc\" 77 | shell: \"cat {input} > {output}\" 78 | 79 | rule dd_subdir: 80 | input: \"aa.out\" 81 | output: \"subdir/dd.out\" 82 | shell: \"cat {input} > {output}\" 83 | 84 | rule: 85 | input: \"anon.in\" 86 | output: \"anon.out\" 87 | shell: \"cat {input} > {output}\"" 88 | nil 89 | "Snakefile") 90 | ,@body) 91 | (delete-directory snakemake-test-dir t))))) 92 | (def-edebug-spec snakemake-with-temp-dir (body)) 93 | 94 | 95 | ;;; snakemake-mode.el 96 | 97 | ;;;; Indentation 98 | 99 | (ert-deftest snakemake-test-indent-line/at-rule-block () 100 | (should 101 | (string= 102 | "rule abc:" 103 | (snakemake-with-temp-text 104 | "rule abc:" 105 | (snakemake-indent-line) 106 | (buffer-string)))) 107 | (should 108 | (string= 109 | "rule abc:" 110 | (snakemake-with-temp-text 111 | " rule abc:" 112 | (snakemake-indent-line) 113 | (buffer-string)))) 114 | (should 115 | (string= 116 | "rule abc :" 117 | (snakemake-with-temp-text 118 | " rule abc :" 119 | (snakemake-indent-line) 120 | (buffer-string)))) 121 | (should 122 | (string= 123 | "rule abc: " 124 | (snakemake-with-temp-text 125 | " rule abc: " 126 | (snakemake-indent-line) 127 | (buffer-string)))) 128 | (should 129 | (string= 130 | " 131 | if True: 132 | rule abc:" 133 | (snakemake-with-temp-text 134 | " 135 | if True: 136 | rule abc:" 137 | (snakemake-indent-line) 138 | (buffer-string)))) 139 | (should 140 | (string= 141 | " 142 | if True: 143 | rule abc:" 144 | (snakemake-with-temp-text 145 | " 146 | if True: 147 | rule abc:" 148 | (snakemake-indent-line 'prev) 149 | (buffer-string))))) 150 | 151 | (ert-deftest snakemake-test-indent-line/outside-rule () 152 | (should 153 | (string= 154 | " 155 | def ok(): 156 | " 157 | (snakemake-with-temp-text 158 | " 159 | def ok(): 160 | " 161 | (snakemake-indent-line) 162 | (buffer-string))))) 163 | 164 | (ert-deftest snakemake-test-indent-line/toplevel-command () 165 | (should 166 | (string= 167 | "include: \"somefile\"" 168 | (snakemake-with-temp-text 169 | "include: \"somefile\"" 170 | (snakemake-indent-line) 171 | (buffer-string)))) 172 | (should 173 | (string= 174 | "include: \"somefile\"" 175 | (snakemake-with-temp-text 176 | " include: \"somefile\"" 177 | (snakemake-indent-line) 178 | (buffer-string)))) 179 | (should 180 | (string= 181 | " 182 | if True: 183 | include: \"somefile\" 184 | " 185 | (snakemake-with-temp-text 186 | " 187 | if True: 188 | include: \"somefile\" 189 | " 190 | (snakemake-indent-line) 191 | (buffer-string)))) 192 | (should 193 | (string= 194 | " 195 | include: 196 | \"somefile\" 197 | " 198 | (snakemake-with-temp-text 199 | " 200 | include: 201 | \"somefile\" 202 | " 203 | (snakemake-indent-line) 204 | (buffer-string))))) 205 | 206 | (ert-deftest snakemake-test-indent-line/field-key () 207 | (should 208 | (string= 209 | " 210 | rule abc: 211 | " 212 | (snakemake-with-temp-text 213 | " 214 | rule abc: 215 | " 216 | (snakemake-indent-line) 217 | (buffer-string)))) 218 | (should 219 | (string= 220 | " 221 | rule abc: 222 | 223 | " 224 | (snakemake-with-temp-text 225 | " 226 | rule abc: 227 | 228 | " 229 | (snakemake-indent-line) 230 | (buffer-string)))) 231 | (should 232 | (string= 233 | " 234 | rule abc: 235 | " 236 | (snakemake-with-temp-text 237 | " 238 | rule abc: 239 | " 240 | (snakemake-indent-line 'prev) 241 | (buffer-string)))) 242 | (should 243 | (string= 244 | " 245 | rule abc: 246 | " 247 | (snakemake-with-temp-text 248 | " 249 | rule abc: 250 | " 251 | (snakemake-indent-line 'prev) 252 | (buffer-string)))) 253 | (should 254 | (string= 255 | " 256 | rule abc: 257 | text" 258 | (snakemake-with-temp-text 259 | " 260 | rule abc: 261 | text" 262 | (snakemake-indent-line) 263 | (buffer-substring (point-min) (point))))) 264 | (should 265 | (string= 266 | " 267 | rule abc: 268 | 269 | text" 270 | (snakemake-with-temp-text 271 | " 272 | rule abc: 273 | 274 | text" 275 | (snakemake-indent-line) 276 | (buffer-substring (point-min) (point))))) 277 | (should 278 | (string= 279 | " 280 | rule abc: 281 | te" 282 | (snakemake-with-temp-text 283 | " 284 | rule abc: 285 | text" 286 | (snakemake-indent-line) 287 | (buffer-substring (point-min) (point))))) 288 | (should 289 | (string= 290 | " 291 | rule abc: 292 | input: 'infile' 293 | output:" 294 | (snakemake-with-temp-text 295 | " 296 | rule abc: 297 | input: 'infile' 298 | output:" 299 | (snakemake-indent-line) 300 | (buffer-string)))) 301 | (should 302 | (string= 303 | " 304 | rule abc: 305 | input: 'infile' 306 | output :" 307 | (snakemake-with-temp-text 308 | " 309 | rule abc: 310 | input: 'infile' 311 | output :" 312 | (snakemake-indent-line) 313 | (buffer-string)))) 314 | (should 315 | (string= 316 | " 317 | rule abc: 318 | input: 'infile' 319 | output:" 320 | (snakemake-with-temp-text 321 | " 322 | rule abc: 323 | input: 'infile' 324 | output:" 325 | (snakemake-indent-line 'prev) 326 | (buffer-string)))) 327 | (should 328 | (string= 329 | " 330 | rule abc: 331 | input: 'infile' 332 | output: " 333 | (snakemake-with-temp-text 334 | " 335 | rule abc: 336 | input: 'infile' 337 | output: " 338 | (snakemake-indent-line) 339 | (buffer-string)))) 340 | (should 341 | (string= 342 | " 343 | rule abc: 344 | input: 'infile' 345 | " 346 | (snakemake-with-temp-text 347 | " 348 | rule abc: 349 | input: 'infile' 350 | output:" 351 | (snakemake-indent-line) 352 | (buffer-substring (point-min) (point)))))) 353 | 354 | (ert-deftest snakemake-test-indent-line/field-value () 355 | (should 356 | (string= 357 | " 358 | rule abc: 359 | output: 360 | " 361 | (snakemake-with-temp-text 362 | " 363 | rule abc: 364 | output: 365 | " 366 | (snakemake-indent-line) 367 | (buffer-string)))) 368 | (should 369 | (string= 370 | " 371 | rule abc: 372 | output: 373 | " 374 | (snakemake-with-temp-text 375 | " 376 | rule abc: 377 | output: 378 | " 379 | (snakemake-indent-line 'prev) 380 | (buffer-string)))) 381 | (should 382 | (string= 383 | " 384 | rule abc: 385 | output: 386 | " 387 | (snakemake-with-temp-text 388 | " 389 | rule abc: 390 | output: 391 | " 392 | (snakemake-indent-line) 393 | (buffer-string)))) 394 | (should 395 | (string= 396 | " 397 | rule abc: 398 | output: # comment 399 | " 400 | (snakemake-with-temp-text 401 | " 402 | rule abc: 403 | output: # comment 404 | " 405 | (snakemake-indent-line) 406 | (buffer-string)))) 407 | (should 408 | (string= 409 | " 410 | rule abc: 411 | output: 412 | 'file{}{}'.format('one', 413 | 'two'" 414 | (snakemake-with-temp-text 415 | " 416 | rule abc: 417 | output: 418 | 'file{}{}'.format('one', 419 | 'two'" 420 | (snakemake-indent-line) 421 | (buffer-string)))) 422 | (should 423 | (string= 424 | " 425 | rule abc: 426 | output: 'file' 427 | " 428 | (snakemake-with-temp-text 429 | " 430 | rule abc: 431 | output: 'file' 432 | " 433 | (snakemake-indent-line) 434 | (buffer-string)))) 435 | (should 436 | (string= 437 | " 438 | rule abc: 439 | output: 'file' 440 | " 441 | (snakemake-with-temp-text 442 | " 443 | rule abc: 444 | output: 'file' 445 | " 446 | (snakemake-indent-line 'prev) 447 | (buffer-string)))) 448 | (should 449 | (string= 450 | " 451 | rule abc: 452 | output: 'file' 453 | 'text'" 454 | (snakemake-with-temp-text 455 | " 456 | rule abc: 457 | output: 'file' 458 | 'text'" 459 | (snakemake-indent-line) 460 | (buffer-string)))) 461 | (should 462 | (string= 463 | " 464 | rule abc: 465 | output: 'file' 466 | 'text' " 467 | (snakemake-with-temp-text 468 | " 469 | rule abc: 470 | output: 'file' 471 | 'text' " 472 | (snakemake-indent-line) 473 | (buffer-string)))) 474 | (should 475 | (string= 476 | " 477 | rule abc: 478 | output: 'file' 479 | " 480 | (snakemake-with-temp-text 481 | " 482 | rule abc: 483 | output: 'file' 484 | 'text'" 485 | (snakemake-indent-line) 486 | (buffer-substring (point-min) (point))))) 487 | (should 488 | (string= 489 | " 490 | rule abc: 491 | output: 'file' 492 | 'text'" 493 | (snakemake-with-temp-text 494 | " 495 | rule abc: 496 | output: 'file' 497 | 'text'" 498 | (snakemake-indent-line 'prev) 499 | (buffer-string)))) 500 | (should 501 | (string= 502 | " 503 | rule abc: 504 | output: 505 | 'file' 506 | 'text'" 507 | (snakemake-with-temp-text 508 | " 509 | rule abc: 510 | output: 511 | 'file' 512 | 'text'" 513 | (snakemake-indent-line) 514 | (buffer-string)))) 515 | (should 516 | (string= 517 | " 518 | rule abc: 519 | output: # comment 520 | 'file' 521 | 'text'" 522 | (snakemake-with-temp-text 523 | " 524 | rule abc: 525 | output: # comment 526 | 'file' 527 | 'text'" 528 | (snakemake-indent-line) 529 | (buffer-string)))) 530 | (should 531 | (string= 532 | " 533 | rule abc: 534 | run: 535 | with this: 536 | " 537 | (snakemake-with-temp-text 538 | " 539 | rule abc: 540 | run: 541 | with this: 542 | " 543 | (snakemake-indent-line) 544 | (buffer-string))))) 545 | 546 | (ert-deftest snakemake-test/indent-region () 547 | (should 548 | (string= 549 | " 550 | rule abc: 551 | input: 'infile' 552 | output:" 553 | (snakemake-with-temp-text 554 | " 555 | rule abc: 556 | input: 'infile' 557 | output:" 558 | (indent-region (point) (point-max)) 559 | (buffer-string)))) 560 | (should 561 | (string= 562 | " 563 | if True: 564 | rule abc: 565 | input: 'infile' 566 | output:" 567 | (snakemake-with-temp-text 568 | " 569 | if True: 570 | rule abc: 571 | input: 'infile' 572 | output:" 573 | (indent-region (point) (point-max)) 574 | (buffer-string)))) 575 | (should 576 | (string= 577 | " 578 | rule abc: 579 | input: 580 | one='one', two='two' 581 | output: 'out' 582 | run: 583 | with open(input.one) as ifh: 584 | with open(output.out, 'w') as ofh: 585 | ofh.write(ifh.read())" 586 | (snakemake-with-temp-text 587 | " 588 | rule abc: 589 | input: 590 | one='one', two='two' 591 | output: 'out' 592 | run: 593 | with open(input.one) as ifh: 594 | with open(output.out, 'w') as ofh: 595 | ofh.write(ifh.read())" 596 | (indent-region (point) (point-max)) 597 | (buffer-string)))) 598 | (should 599 | (string= 600 | " 601 | x = [1, 602 | 2, 603 | 3,]" 604 | (snakemake-with-temp-text 605 | " 606 | x = [1, 607 | 2, 608 | 3,]" 609 | (indent-region (point) (point-max)) 610 | (buffer-string))))) 611 | 612 | ;;;; Info and navigation 613 | 614 | (ert-deftest snakemake-test-block-info () 615 | (should-not 616 | (snakemake-with-temp-text 617 | " 618 | rule abc: 619 | output: 'file'" 620 | (snakemake-block-info)))) 621 | 622 | (ert-deftest snakemake-test-beginning-of-block () 623 | (should 624 | (string= " 625 | rule abc: 626 | output: 'file'" 627 | (snakemake-with-temp-text 628 | " 629 | rule abc: 630 | output: 'file'" 631 | (snakemake-beginning-of-block) 632 | (buffer-substring (point) (point-max))))) 633 | (should 634 | (string= "rule abc: 635 | output: 'file'" 636 | (snakemake-with-temp-text 637 | " 638 | rule abc: 639 | output: 'file'" 640 | (snakemake-beginning-of-block) 641 | (buffer-substring (point) (point-max))))) 642 | (should 643 | (string= "rule abc: 644 | output: 'file' 645 | 646 | " 647 | (snakemake-with-temp-text 648 | " 649 | rule abc: 650 | output: 'file' 651 | 652 | " 653 | (snakemake-beginning-of-block) 654 | (buffer-substring (point) (point-max))))) 655 | (should 656 | (string= "rule abc: 657 | \"\"\"docstring header 658 | 659 | docstring line 660 | \"\"\" 661 | output: 'file'" 662 | (snakemake-with-temp-text 663 | " 664 | rule abc: 665 | \"\"\"docstring header 666 | 667 | docstring line 668 | \"\"\" 669 | output: 'file'" 670 | (snakemake-beginning-of-block) 671 | (buffer-substring (point) (point-max))))) 672 | (should 673 | (string= "subworkflow otherworkflow: 674 | workdir: '../path/to/otherworkflow' 675 | snakefile: '../path/to/otherworkflow/Snakefile'" 676 | (snakemake-with-temp-text 677 | " 678 | subworkflow otherworkflow: 679 | workdir: '../path/to/otherworkflow' 680 | snakefile: '../path/to/otherworkflow/Snakefile'" 681 | (snakemake-beginning-of-block) 682 | (buffer-substring (point) (point-max)))))) 683 | 684 | (ert-deftest snakemake-test-end-of-block () 685 | (should 686 | (string= " 687 | rule abc: 688 | output: 'file' 689 | 690 | 691 | " 692 | (snakemake-with-temp-text 693 | " 694 | rule abc: 695 | output: 'file' 696 | 697 | 698 | " 699 | (snakemake-end-of-block) 700 | (buffer-substring (point-min) (point))))) 701 | (should 702 | (string= " 703 | rule abc: 704 | output: 'file' 705 | " 706 | (snakemake-with-temp-text 707 | " 708 | rule abc: 709 | output: 'file' 710 | 711 | rule xyz: 712 | input: 'file'" 713 | (snakemake-end-of-block) 714 | (buffer-substring (point-min) (point))))) 715 | (should 716 | (string= " 717 | rule abc: 718 | output: 'file'" 719 | (snakemake-with-temp-text 720 | " 721 | rule abc: 722 | output: 'file' 723 | rule xyz: 724 | input: 'file'" 725 | (snakemake-end-of-block) 726 | (buffer-substring (point-min) (point)))))) 727 | 728 | (ert-deftest snakemake-test-block-or-defun-name () 729 | (should 730 | (string= "abc" 731 | (snakemake-with-temp-text 732 | " 733 | rule abc: 734 | output: 'file' 735 | " 736 | (snakemake-block-or-defun-name)))) 737 | (should 738 | (string= "xyz" 739 | (snakemake-with-temp-text 740 | " 741 | rule abc: 742 | output: 'file' 743 | 744 | def xyz(): 745 | pass 746 | " 747 | (snakemake-block-or-defun-name)))) 748 | (should-not 749 | (snakemake-with-temp-text 750 | " 751 | rule abc: 752 | output: 'file' 753 | " 754 | (snakemake-block-or-defun-name)))) 755 | 756 | 757 | ;;; snakemake.el 758 | 759 | (ert-deftest snakemake-test-snakefile-directory () 760 | (snakemake-with-temp-dir 761 | (should (equal default-directory (snakemake-snakefile-directory))) 762 | (let ((topdir default-directory)) 763 | (should (equal topdir 764 | (let ((default-directory "subdir")) 765 | (snakemake-snakefile-directory))))))) 766 | 767 | (ert-deftest snakemake-test-split-lines () 768 | (should 769 | (equal '("abc" "123") 770 | (snakemake--split-lines "\ 771 | abc 772 | 123 773 | two words"))) 774 | (should 775 | (equal '("abc" "123") 776 | (snakemake--split-lines "\ 777 | abc 778 | 779 | 123 780 | 781 | two words"))) 782 | (should 783 | (equal '("abc") 784 | (snakemake--split-lines "\ 785 | abc 786 | 123 787 | two words" 788 | 'remove-num)))) 789 | 790 | (ert-deftest snakemake-test-rule-targets () 791 | (should 792 | (equal '("aa" "bb" "dd_subdir") 793 | (snakemake-with-temp-dir 794 | (snakemake-rule-targets))))) 795 | 796 | (ert-deftest snakemake-test-all-rules () 797 | (should 798 | (equal '("aa" "bb" "cc_wildcards" "dd_subdir") 799 | (snakemake-with-temp-dir 800 | (snakemake-all-rules))))) 801 | 802 | (ert-deftest snakemake-test-file-targets () 803 | (should 804 | (equal 805 | (and snakemake-file-target-program 806 | '("aa.out" "bb.out" "subdir/dd.out" "anon.out")) 807 | (snakemake-with-temp-dir 808 | (snakemake-file-targets))))) 809 | 810 | (ert-deftest snakemake-test-check-target () 811 | (should 812 | (snakemake-with-temp-dir 813 | (snakemake-check-target "aa.out"))) 814 | (should-not 815 | (snakemake-with-temp-dir 816 | (snakemake-check-target "aa.out.not-target"))) 817 | (should 818 | (snakemake-with-temp-dir 819 | (snakemake-check-target "aa"))) 820 | (should-not 821 | (snakemake-with-temp-dir 822 | (snakemake-check-target "cc_wildcards"))) 823 | ;; Errors with the Snakefile, like ambiguous rules and syntax 824 | ;; errors, should be reported as errors rather than treated as 825 | ;; invalid targets. 826 | (should-error 827 | (snakemake-with-temp-dir 828 | (write-region "\ndef incomplete_def:" 829 | nil 830 | "Snakefile" 831 | 'append) 832 | (snakemake-check-target "aa")) 833 | :type 'snakemake-error) 834 | (should-error 835 | (snakemake-with-temp-dir 836 | (write-region "\ 837 | 838 | rule aa: 839 | output: \"aa.ambig.out\" 840 | shell: \"echo aa.ambig.content > {output}\"" 841 | nil 842 | "Snakefile" 843 | 'append) 844 | (snakemake-check-target "aa")) 845 | :type 'snakemake-error) 846 | ;; Write-protected targets should be recognized as valid targets 847 | ;; despite Snakemake throwing an error. 848 | (should 849 | (snakemake-with-temp-dir 850 | (write-region "" nil "bb.out") 851 | (set-file-modes "bb.out" (file-modes-symbolic-to-number "u=r")) 852 | (write-region "" nil "aa.out") 853 | (snakemake-check-target "bb.out")))) 854 | 855 | (ert-deftest snakemake-test-org-link-file-targets () 856 | (should (equal '("/path/to/fname") 857 | (with-temp-buffer 858 | (org-mode) 859 | (insert "\n[[file:/path/to/fname][descr]]\n") 860 | (forward-line -1) 861 | (snakemake-org-link-file-targets))))) 862 | 863 | (ert-deftest snakemake-test-region-file-targets () 864 | (let ((files '("/path/to/fname" "fname2" "CAP"))) 865 | (should (equal (mapcar #'expand-file-name files) 866 | (with-temp-buffer 867 | (insert (mapconcat #'identity files "\n")) 868 | (snakemake-region-file-targets 869 | (point-min) (point-max))))) 870 | (should (equal (mapcar #'expand-file-name files) 871 | (with-temp-buffer 872 | (insert (mapconcat #'identity files ",")) 873 | (snakemake-region-file-targets 874 | (point-min) (point-max))))) 875 | (should (equal (mapcar #'expand-file-name files) 876 | (with-temp-buffer 877 | (insert (car files)) 878 | (insert ?\n) 879 | (insert (mapconcat #'identity (cdr files) " ")) 880 | (snakemake-region-file-targets 881 | (point-min) (point-max))))))) 882 | 883 | (ert-deftest snakemake-test-file-targets-at-point () 884 | (should 885 | (equal '("aa.out") 886 | (snakemake-with-temp-dir 887 | (with-temp-buffer 888 | (insert "aa.out") 889 | (beginning-of-line) 890 | (snakemake-file-targets-at-point 'check))))) 891 | (should-not 892 | (snakemake-with-temp-dir 893 | (with-temp-buffer 894 | (insert "aa.out.not-target") 895 | (beginning-of-line) 896 | (snakemake-file-targets-at-point 'check)))) 897 | (should 898 | (equal '("aa.out.not-target") 899 | (snakemake-with-temp-dir 900 | (with-temp-buffer 901 | (insert "aa.out.not-target") 902 | (beginning-of-line) 903 | (snakemake-file-targets-at-point)))))) 904 | 905 | (ert-deftest snakemake-test-rule-at-point () 906 | (should 907 | (equal '("aa") 908 | (snakemake-with-temp-dir 909 | (with-temp-buffer 910 | (snakemake-mode) 911 | (insert-file-contents "Snakefile") 912 | (re-search-forward "rule aa:") 913 | (snakemake-rule-at-point 'target))))) 914 | (should 915 | (equal '("cc_wildcards") 916 | (snakemake-with-temp-dir 917 | (with-temp-buffer 918 | (snakemake-mode) 919 | (insert-file-contents "Snakefile") 920 | (re-search-forward "rule cc_wildcards:") 921 | (snakemake-rule-at-point))))) 922 | (should-not 923 | (snakemake-with-temp-dir 924 | (with-temp-buffer 925 | (snakemake-mode) 926 | (insert-file-contents "Snakefile") 927 | (re-search-forward "rule cc_wildcards:") 928 | (snakemake-rule-at-point 'target)))) 929 | (should-not 930 | (snakemake-with-temp-dir 931 | (with-temp-buffer 932 | (snakemake-mode) 933 | (insert-file-contents "Snakefile") 934 | (re-search-forward "rule:") 935 | (snakemake-rule-at-point))))) 936 | 937 | (provide 'snakemake-test) 938 | ;;; snakemake-test.el ends here 939 | -------------------------------------------------------------------------------- /snakemake.el: -------------------------------------------------------------------------------- 1 | ;;; snakemake.el --- Call Snakemake from Emacs -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright all Snakemake mode contributors 4 | 5 | ;; Author: Kyle Meyer 6 | ;; URL: https://git.kyleam.com/snakemake-mode/about 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; This package provides a Transient interface [1] for calling 24 | ;; Snakemake [2]. The main entry point is the `snakemake' command, 25 | ;; which you should consider giving a global key binding. 26 | ;; 27 | ;; The `snakemake' transient currently includes four actions, all 28 | ;; which lead to a Snakemake call. What's different between the 29 | ;; actions is how targets are selected. 30 | ;; 31 | ;; snakemake-build-targets-at-point 32 | ;; 33 | ;; This command builds the file name or rule at point if it's a 34 | ;; valid build target. 35 | ;; 36 | ;; How the file name targets are retrieved depends on functions in 37 | ;; `snakemake-file-targets-hook'. The default functions grab file 38 | ;; names from Dired, Org links, and general text under point that 39 | ;; looks like a file name. 40 | ;; 41 | ;; snakemake-build 42 | ;; 43 | ;; This action allows you to edit the command before running it. 44 | ;; 45 | ;; It tries to guess what the target name should be, but it 46 | ;; doesn't verify if this target is actually a build target. This 47 | ;; lax behavior is useful to, for example, pull in an input file 48 | ;; name from Dired and then edit the extension to get the desired 49 | ;; output name. 50 | ;; 51 | ;; snakemake-build-rule-target 52 | ;; 53 | ;; This command prompts with a list of all target rules for the 54 | ;; current Snakefile and builds the selected one. 55 | ;; 56 | ;; snakemake-build-file-target 57 | ;; 58 | ;; Like `snakemake-build-rule-target', but this command prompts 59 | ;; for a file name, using the file name at point, if it's a valid 60 | ;; target, as the default completion. 61 | ;; 62 | ;; When `snakemake-file-target-program' is set to a program that 63 | ;; outputs a list of files, the user is prompted with that list 64 | ;; instead. snakemake.el's source repo contains a script, 65 | ;; "snakemake-file-targets", that can be used for this purpose. 66 | ;; It outputs a list of all concrete output files for the current 67 | ;; Snakefile. 68 | ;; 69 | ;; All these commands use the Snakefile contained in the 70 | ;; `default-directory' of the current buffer. If 71 | ;; `snakemake-root-dir-function' is non-nil, this function should 72 | ;; return a secondary directory (e.g., a project repository root) in 73 | ;; which to look for a Snakefile if there isn't one in the current 74 | ;; directory. 75 | ;; 76 | ;; By default, commands are executed through `compile'. However, a 77 | ;; terminal can be created with `snakemake-term-start', in which case 78 | ;; commands are sent there instead. 79 | ;; 80 | ;; In addition to the commands in the transient, there are commands 81 | ;; for showing and saving the dependency graph of a target. The 82 | ;; command `snakemake-graph' displays the graph in a buffer. From 83 | ;; this buffer, `snakemake-graph-save' can be called to save the graph 84 | ;; to an output file. 85 | ;; 86 | ;; [1] https://magit.vc/manual/transient/ 87 | ;; [2] https://snakemake.github.io/ 88 | 89 | ;;; Code: 90 | 91 | (require 'cl-lib) 92 | (require 'compile) 93 | (require 'term) 94 | (require 'transient) 95 | 96 | (require 'snakemake-mode) 97 | 98 | 99 | ;;; Customization 100 | 101 | ;;;###autoload 102 | (defgroup snakemake nil 103 | "Interface for running Snakemake" 104 | :group 'tools) 105 | 106 | (defcustom snakemake-program "snakemake" 107 | "Command to run Snakemake." 108 | :type 'string 109 | :package-version '(snakemake-mode . "0.4.0")) 110 | 111 | (defcustom snakemake-shell-file-name shell-file-name 112 | "Program used by `ansi-term' to start a terminal." 113 | :type 'string 114 | :package-version '(snakemake-mode . "1.2.0")) 115 | 116 | (defcustom snakemake-file-target-program 117 | (executable-find "snakemake-file-targets") 118 | "Program that returns newline-delimited list of output files." 119 | :type '(choice (const :tag "None" nil) 120 | (string :tag "Program")) 121 | :package-version '(snakemake-mode . "0.4.0")) 122 | 123 | (defcustom snakemake-always-use-term nil 124 | "Whether commands should be sent to the terminal only. 125 | 126 | When nil, commands are sent to the terminal if there is an active 127 | terminal for the current Snakefile directory. If there isn't an 128 | active terminal, commands are executed through `compile'. 129 | 130 | When this variable is non-nil, always try to send the command to 131 | the terminal. If no terminal is found, the command will be 132 | aborted with a message telling you to first run 133 | `\\[snakemake-term-start]'." 134 | :type 'boolean 135 | :safe #'booleanp 136 | :package-version '(snakemake-mode . "1.2.0")) 137 | 138 | (defcustom snakemake-root-dir-function nil 139 | "Function used to find root directory of the current \"project\". 140 | This function will be called with no arguments and should return 141 | an absolute path or, if no root is found, nil. When nil, only 142 | Snakefiles in the current directory will be detected." 143 | :type '(radio (const :tag "None" nil) 144 | (function-item vc-root-dir) 145 | (function-item projectile-project-root) 146 | function) 147 | :package-version '(snakemake-mode . "1.2.0")) 148 | 149 | (defcustom snakemake-file-targets-hook 150 | '(snakemake-region-file-targets 151 | snakemake-dired-file-targets 152 | snakemake-org-link-file-targets 153 | snakemake-thingatpt-file-targets) 154 | "Functions to return file targets at point. 155 | These will be called, with no arguments, until one of them 156 | signals success by returning non-nil. If non-nil, the return 157 | value should be a list of absolute paths." 158 | :type 'hook 159 | :package-version '(snakemake-mode . "0.4.0")) 160 | 161 | (defcustom snakemake-region-files-strip-re 162 | (concat (regexp-opt '("[" "]" "'" "\"" ";" ",")) "+") 163 | "Regexp matching text to be discarded when collecting region files. 164 | Used by `snakemake-region-file-targets'." 165 | :type 'regexp 166 | :package-version '(snakemake-mode . "0.4.0")) 167 | 168 | (defcustom snakemake-dot-program "dot" 169 | "Program used to save the graph with `snakemake-graph-save'. 170 | This program must have an option `-T' that can be used to specify 171 | the output type. See `man dot'." 172 | :type 'string 173 | :package-version '(snakemake-mode . "0.4.0")) 174 | 175 | (defcustom snakemake-graph-default-extension ".svg" 176 | "Extension used by `snakemake-graph-save'. 177 | This should be a valid value for the '-T' option of 178 | `snakemake-dot-program'." 179 | :type 'string 180 | :package-version '(snakemake-mode . "0.4.0")) 181 | 182 | 183 | ;;; Utilities 184 | 185 | (defun snakemake-snakefile-directory (&optional path) 186 | "Return current Snakefile's directory for PATH. 187 | 188 | If PATH is nil, it defaults to `default-directory'. 189 | 190 | Which Snakefile, if any, is current is determined by the value of 191 | `default-directory'. 192 | 193 | * A file named \"Snakefile\" in `default-directory' is always 194 | considered the current file. 195 | 196 | * If `default-directory' does not contain a file named 197 | \"Snakefile\", look in the directory given by 198 | `snakemake-root-dir-function'." 199 | (let ((default-directory 200 | (or (and path (file-name-directory (expand-file-name path))) 201 | default-directory))) 202 | (or (and (file-exists-p "Snakefile") default-directory) 203 | (and snakemake-root-dir-function 204 | (let ((topdir (ignore-errors 205 | (funcall snakemake-root-dir-function)))) 206 | (when topdir 207 | (and (file-exists-p (expand-file-name "Snakefile" topdir)) 208 | topdir)))) 209 | (user-error "No Snakefile found for %s" 210 | (abbreviate-file-name default-directory))))) 211 | 212 | (defvar snakemake--cache (make-hash-table :test #'equal)) 213 | (defmacro snakemake-with-cache (directory cache-info &rest body) 214 | "Execute BODY and cache result, with DIRECTORY's Snakefile as current. 215 | CACHE-INFO should uniquely identify the call when taken together 216 | with DIRECTORY and the Snakefile's modification time." 217 | (declare (indent defun) (debug (form form body))) 218 | (let ((cached (cl-gensym "cached")) 219 | (key (cl-gensym "key"))) 220 | `(let* ((default-directory (snakemake-snakefile-directory ,directory)) 221 | (,key (list default-directory 222 | (nth 5 (file-attributes 223 | (expand-file-name "Snakefile"))) 224 | ,@cache-info)) 225 | (,cached (gethash ,key snakemake--cache 'not-found))) 226 | (if (eq ,cached 'not-found) 227 | (let ((result ,(macroexp-progn body))) 228 | (puthash ,key result snakemake--cache) 229 | result) 230 | ,cached)))) 231 | 232 | (define-error 'snakemake-error "Snakemake process error") 233 | 234 | (defconst snakemake-error-buffer "*Snakemake process error*") 235 | 236 | (defun snakemake--display-error () 237 | (ignore-errors (kill-buffer snakemake-error-buffer)) 238 | (let ((buf (get-buffer-create snakemake-error-buffer))) 239 | (copy-to-buffer buf (point-min) (point-max)) 240 | (with-current-buffer buf (help-mode)) 241 | (display-buffer buf) 242 | (signal 'snakemake-error nil))) 243 | 244 | (defun snakemake-insert-output (&rest args) 245 | "Call `snakemake-program' with ARGS and insert output." 246 | (apply #'call-process snakemake-program nil t nil args)) 247 | 248 | (defun snakemake--split-lines (str &optional remove-num) 249 | (cl-remove-if 250 | (lambda (x) (or (and remove-num (string-match-p "\\`[0-9]+\\'" x)) 251 | ;; There is not a clean separation of stderr/stdout 252 | ;; streams for warnings, so this is a hacky way to 253 | ;; filter non-target lines from the output. 254 | (string-match-p " " x))) 255 | (split-string str "\n" t))) 256 | 257 | (defun snakemake--split-rules (type) 258 | "Return rules of TYPE. 259 | TYPE can be `all' or `target'." 260 | (snakemake--split-lines 261 | (with-temp-buffer 262 | (if (zerop (snakemake-insert-output 263 | "--nocolor" 264 | (cl-case type 265 | (all "--list") 266 | (target "--list-target-rules") 267 | (t (user-error "Invalid rule type: %s" type))))) 268 | (buffer-string) 269 | (snakemake--display-error))) 270 | t)) 271 | 272 | (defun snakemake-all-rules (&optional directory) 273 | "Return list of rules for DIRECTORY's Snakefile." 274 | (snakemake-with-cache directory ("all-rules") 275 | (snakemake--split-rules 'all))) 276 | 277 | (defun snakemake-rule-targets (&optional directory) 278 | "Return list of target rules for DIRECTORY's Snakefile." 279 | (snakemake-with-cache directory ("target-rules") 280 | (snakemake--split-rules 'target))) 281 | 282 | (defun snakemake-file-targets (&optional directory) 283 | "Return list of output files for DIRECTORY's Snakefile. 284 | The file list is determined by the output of 285 | `snakemake-file-target-program'." 286 | (when snakemake-file-target-program 287 | (snakemake-with-cache directory ("target-files") 288 | (snakemake--split-lines 289 | (with-temp-buffer 290 | (if (zerop (call-process snakemake-file-target-program nil t)) 291 | (buffer-string) 292 | (snakemake--display-error))))))) 293 | 294 | (defconst snakemake-valid-target-re "ProtectedOutputException" 295 | "Regular expression indicating valid target. 296 | If this matches, the target will be considered valid even if the 297 | exit status is non-zero.") 298 | 299 | (defun snakemake-check-target (target &optional directory) 300 | "Return non-nil if TARGET is a valid target for DIRECTORY's Snakefile." 301 | (snakemake-with-cache directory (target) 302 | (with-temp-buffer 303 | (let ((ex-code (snakemake-insert-output "--quiet" "--dryrun" target))) 304 | (or (zerop ex-code) 305 | (progn (goto-char (point-min)) 306 | (re-search-forward snakemake-valid-target-re nil t)) 307 | ;; A non-zero exit status could indicate that TARGET is 308 | ;; invalid, but it could also be the result of an issue 309 | ;; like a syntax error or an ambiguous rule. To check 310 | ;; this, see whether `snakemake-all-rules' signals a 311 | ;; `snakemake-error'. This avoids relying on parsing 312 | ;; Snakemake exception output, which isn't stable across 313 | ;; Snakemake versions. 314 | (progn (snakemake-all-rules) nil)))))) 315 | 316 | (declare-function org-element-context "org-element") 317 | (declare-function org-element-property "org-element") 318 | (defun snakemake-org-link-file-targets () 319 | "Return file path from Org link. 320 | This function returns a list for consistency with other 321 | target-returning functions, but any non-nil return value is 322 | currently limited to a single-item list." 323 | (when (derived-mode-p 'org-mode) 324 | (let ((el (org-element-context))) 325 | ;; Don't use `org-element-lineage' because it isn't available 326 | ;; until Org version 8.3. 327 | (while (and el (not (memq (car el) '(link)))) 328 | (setq el (org-element-property :parent el))) 329 | (when (eq (car el) 'link) 330 | (list (expand-file-name (org-element-property :path el))))))) 331 | 332 | (declare-function dired-get-marked-files "dired") 333 | (defun snakemake-dired-file-targets () 334 | "Return marked Dired files." 335 | (and (derived-mode-p 'dired-mode) 336 | (dired-get-marked-files))) 337 | 338 | (defun snakemake-region-file-targets (&optional beg end) 339 | "Return file targets in region. 340 | 341 | Before generating the list, characters that match 342 | `snakemake-region-files-strip-re' are discarded. 343 | 344 | If BEG or END is non-nil, use them in place of `region-beginning' 345 | or `region-end', respectively." 346 | (when (or (use-region-p) (and beg end)) 347 | (mapcar #'expand-file-name 348 | (split-string 349 | (replace-regexp-in-string 350 | snakemake-region-files-strip-re " " 351 | (buffer-substring-no-properties (or beg (region-beginning)) 352 | (or end (region-end)))))))) 353 | 354 | (defun snakemake-thingatpt-file-targets () 355 | "Return file at point according to `thing-at-point'. 356 | This function returns a list for consistency with other 357 | target-returning functions, but any non-nil return value is 358 | currently limited to a single-item list." 359 | (let ((fname (thing-at-point 'filename))) 360 | (and fname 361 | (list (expand-file-name fname))))) 362 | 363 | (defun snakemake-file-targets-at-point (&optional check) 364 | "Return list of file targets at point. 365 | If CHECK is non-nil, filter files to known targets of the current 366 | Snakefile." 367 | (let* ((dir (snakemake-snakefile-directory)) 368 | (fnames (run-hook-with-args-until-success 369 | 'snakemake-file-targets-hook)) 370 | (targets (mapcar (lambda (f) (file-relative-name f dir)) 371 | fnames))) 372 | (when targets 373 | (if check 374 | (let ((default-directory dir)) 375 | (cl-remove-if-not #'snakemake-check-target targets)) 376 | targets)))) 377 | 378 | (defun snakemake-rule-at-point (&optional targets-only) 379 | "Return name of rule at point. 380 | 381 | If TARGETS-ONLY is non-nil, verify that the rule is a valid 382 | target. 383 | 384 | This function returns a list for consistency with other 385 | target-returning functions, but any non-nil return value is 386 | currently limited to a single-item list." 387 | (when (derived-mode-p 'snakemake-mode) 388 | (let* ((info (snakemake-block-info)) 389 | (rule (and (equal (nth 0 info) "rule") 390 | (nth 1 info)))) 391 | (when rule 392 | (and (or (not targets-only) (snakemake-check-target rule)) 393 | (list rule)))))) 394 | 395 | (defun snakemake--prompt (prompt default) 396 | (concat prompt 397 | (and default (format " (default %s)" default)) 398 | ": ")) 399 | 400 | (defun snakemake-read-file-target () 401 | "Read a file target. 402 | If `snakemake-file-target-program' is non-nil, use it to generate 403 | a collection of file targets to prompt with. Otherwise, just 404 | read a file name, adjusting the returned file name's path 405 | relative to `snakemake-snakefile-directory'." 406 | (let ((default (car (snakemake-file-targets-at-point t)))) 407 | (if snakemake-file-target-program 408 | (completing-read 409 | (snakemake--prompt "File" default) 410 | (snakemake-file-targets) 411 | nil nil nil nil 412 | default) 413 | (let* ((sdir (snakemake-snakefile-directory)) 414 | (rel-default (and default 415 | (file-relative-name 416 | (expand-file-name default sdir))))) 417 | (file-relative-name 418 | (read-file-name 419 | (snakemake--prompt "File" rel-default) 420 | nil rel-default) 421 | sdir))))) 422 | 423 | (defun snakemake-read-rule (&optional targets-only) 424 | "Read a rule for the current Snakefile. 425 | If TARGETS-ONLY is non-nil, only prompt with rules that are valid 426 | targets." 427 | (let ((default (car (snakemake-rule-at-point targets-only)))) 428 | (completing-read 429 | (snakemake--prompt "Rule" default) 430 | (if targets-only (snakemake-rule-targets) (snakemake-all-rules)) 431 | nil nil nil nil 432 | default))) 433 | 434 | (defun snakemake-read-rules (&rest _) 435 | "Read multiple rules for the current Snakefile." 436 | (mapconcat #'identity 437 | (completing-read-multiple "Rules (comma-separated): " 438 | (snakemake-all-rules)) 439 | " ")) 440 | 441 | 442 | ;;; Graphing commands 443 | 444 | (defvar-local snakemake-graph-id nil) 445 | 446 | (defun snakemake-graph--display () 447 | (require 'image) 448 | (if (not (image-type-available-p 'imagemagick)) 449 | (find-file (snakemake-graph-save)) 450 | (image-mode) 451 | (pop-to-buffer (current-buffer)))) 452 | 453 | ;;;###autoload 454 | (defun snakemake-graph (rules &optional rule-graph) 455 | "Display graph for DAG of RULES. 456 | 457 | The graph will be displayed with `image-mode'. From this buffer, 458 | you can call \\\\[snakemake-graph-save] \ 459 | to save the graph. 460 | 461 | If prefix argument RULE-GRAPH is non-nil, pass --rulegraph 462 | instead of --dag to snakemake. 463 | 464 | $ snakemake --{dag,rulegraph} -- RULES | display" 465 | (interactive (list (or (snakemake-file-targets-at-point 'check) 466 | (snakemake-rule-at-point 'target) 467 | (snakemake-read-rule 'targets)) 468 | current-prefix-arg)) 469 | (unless (listp rules) 470 | (setq rules (list rules))) 471 | (let ((dir (snakemake-snakefile-directory))) 472 | (with-current-buffer (get-buffer-create "*Snakemake graph*") 473 | (setq default-directory dir) 474 | (let ((inhibit-read-only t)) 475 | (erase-buffer) 476 | (apply #'call-process snakemake-program nil t nil 477 | (if rule-graph "--rulegraph" "--dag") 478 | rules)) 479 | (snakemake-graph-mode) 480 | (setq snakemake-graph-id (mapconcat #'file-name-nondirectory rules "-")) 481 | (snakemake-graph--display)))) 482 | 483 | ;;;###autoload 484 | (defun snakemake-graph-this-file (&optional rule-graph directory) 485 | "Display graph of DAG for the first rule of the current file. 486 | 487 | The graph will be displayed with `image-mode'. From this buffer, 488 | you can call \\\\[snakemake-graph-save] \ 489 | to save the graph. 490 | 491 | If RULE-GRAPH is non-nil, pass --rulegraph instead of --dag to 492 | the Snakemake call. Snakemake is called from DIRECTORY or, if 493 | DIRECTORY is nil, from the current file's directory. 494 | 495 | Interactively, \\[universal-argument] indicates to use \ 496 | --rulegraph instead of --dag, 497 | whereas \\[universal-argument] \\[universal-argument] signals to \ 498 | prompt user for a directory from which 499 | to call Snakemake. With any other non-nil prefix value, both of the 500 | above actions are performed. 501 | 502 | $ snakemake -s --{dag,rulegraph} | display" 503 | (interactive 504 | (let ((read-dir (lambda () 505 | (read-directory-name "Call from directory: " 506 | nil nil t)))) 507 | (cond ((equal current-prefix-arg '(4)) 508 | (list t nil)) 509 | ((equal current-prefix-arg '(16)) 510 | (list nil (funcall read-dir))) 511 | (current-prefix-arg 512 | (list t (funcall read-dir))) 513 | (t 514 | (list nil nil))))) 515 | (unless (derived-mode-p 'snakemake-mode) 516 | (user-error "Current buffer is not in Snakemake mode")) 517 | (let* ((file (or (buffer-file-name (buffer-base-buffer)) 518 | (user-error "Buffer is not visiting a file"))) 519 | (dir (or directory (file-name-directory file))) 520 | ret-val) 521 | (with-current-buffer (get-buffer-create "*Snakemake graph*") 522 | (setq default-directory dir) 523 | (let ((inhibit-read-only t)) 524 | (erase-buffer) 525 | (setq ret-val (call-process snakemake-program nil t nil 526 | (if rule-graph "--rulegraph" "--dag") 527 | "--snakefile" file))) 528 | (if (zerop ret-val) 529 | (progn (snakemake-graph-mode) 530 | (setq snakemake-graph-id file) 531 | (snakemake-graph--display)) 532 | (goto-char (point-min)) 533 | (insert (format "Error in snakemake call from %s:\n\n" dir)) 534 | (pop-to-buffer (current-buffer)))))) 535 | 536 | (defun snakemake-graph-save () 537 | "Save graph in current buffer to file. 538 | 539 | The graph will be processed by `snakemake-dot-program'. The 540 | default extension of the output file is 541 | `snakemake-graph-default-extension', but you can enter any 542 | extension that the dot program supports. 543 | 544 | Return the name of the output file." 545 | (interactive) 546 | (unless snakemake-graph-id 547 | (user-error "Not in Snakemake graph buffer")) 548 | (let ((file (read-file-name "To file: " nil nil nil 549 | (concat snakemake-graph-id 550 | snakemake-graph-default-extension)))) 551 | (cond 552 | ((string-match-p "\\`\\s-*\\'" file) 553 | (user-error "No output file specified")) 554 | ((and (file-exists-p file) 555 | (not (y-or-n-p 556 | (concat file " already exists. Overwrite?")))) 557 | (user-error "Aborted")) 558 | (t 559 | (call-process-region (point-min) (point-max) 560 | snakemake-dot-program nil (list :file file) nil 561 | "-T" (file-name-extension file)) 562 | file)))) 563 | 564 | (defvar snakemake-graph-mode-map 565 | (let ((map (make-sparse-keymap))) 566 | (define-key map (kbd "C-c C-s") #'snakemake-graph-save) 567 | map) 568 | "Keymap for Snakemake-graph mode.") 569 | 570 | (define-minor-mode snakemake-graph-mode 571 | "Toggle Snakemake-graph mode. 572 | 573 | With a prefix argument ARG, enable Snakemake-graph mode if ARG is 574 | positive, and disable it otherwise. If called from Lisp, enable 575 | the mode if ARG is omitted or nil. 576 | 577 | Snakemake-graph mode is a minor mode that provides a key, 578 | \\\\[snakemake-graph-save], for saving the graph to an output file." 579 | :keymap snakemake-graph-mode-map) 580 | 581 | 582 | ;;; Commands for calling Snakemake 583 | 584 | (defun snakemake--make-command (targets args) 585 | (mapconcat #'identity 586 | `(,snakemake-program ,@args "--" ,@targets) 587 | " ")) 588 | 589 | ;;;; Compilation interface 590 | 591 | (add-to-list 'compilation-error-regexp-alist 'snakemake) 592 | (add-to-list 593 | 'compilation-error-regexp-alist-alist 594 | '(snakemake . ("^SyntaxError in line \\([0-9]+\\) of \\(.*[^A-z]Snakefile\\):$" 595 | 2 1))) 596 | 597 | (defun snakemake-compile-targets (targets args) 598 | "Run non-interactive `compile' with `snakemake [ARGS] -- TARGETS'." 599 | (let ((cmd (snakemake--make-command targets args))) 600 | (compile cmd) 601 | (push cmd compile-history))) 602 | 603 | ;;;; Terminal interface 604 | 605 | (defun snakemake-term--name () 606 | (concat "snakemake-terminal: " (abbreviate-file-name default-directory))) 607 | 608 | (defun snakemake-term-process () 609 | "Return the terminal process of the current directory." 610 | (get-process (concat "*" (snakemake-term--name) "*"))) 611 | 612 | (defun snakemake-term-use-p () 613 | (or snakemake-always-use-term (snakemake-term-process))) 614 | 615 | ;;;###autoload 616 | (defun snakemake-term-start () 617 | "Start a terminal session for the current Snakefile directory. 618 | 619 | The main advantage of using a terminal is that it allows for a 620 | persistent environment between Snakemake calls, which is useful 621 | for running Snakemake in isolated environments created by tools 622 | like Guix. 623 | 624 | To do so, `snakemake-shell-file-name' should be set to a script 625 | that starts a shell with the desired environment. For example, 626 | to set up an enviroment with Guix, `snakemake-shell-file-name' 627 | could point to a script that runs 628 | 629 | guix environment --pure -m manifest.scm --ad-hoc snakemake 630 | 631 | The file guix-snakemake-environment in Snakemake mode's source 632 | directory can be used for this purpose." 633 | (interactive) 634 | (let ((default-directory (snakemake-snakefile-directory))) 635 | (unless (snakemake-term-process) 636 | (ansi-term snakemake-shell-file-name (snakemake-term--name))))) 637 | 638 | (defun snakemake-term-send (string) 639 | "Send STRING to the terminal for the current directory." 640 | (let* ((proc (or (snakemake-term-process) 641 | (user-error "No active terminal. Start with %s" 642 | (substitute-command-keys 643 | "`\\[snakemake-term-start]'")))) 644 | (buf (process-buffer proc))) 645 | (unless (get-buffer-window buf) 646 | (display-buffer buf)) 647 | (term-send-string proc (concat string "\n")) 648 | (with-current-buffer buf 649 | (goto-char (process-mark proc))))) 650 | 651 | (defun snakemake-term-build-targets (targets args) 652 | "Send `snakemake [ARGS] -- TARGETS' to the terminal." 653 | (snakemake-term-send (snakemake--make-command targets args))) 654 | 655 | ;;;; General interface 656 | 657 | (defun snakemake-build-targets (targets args) 658 | "Run `snakemake [ARGS] -- TARGETS'. 659 | If a terminal is associated with the current Snakefile directory, 660 | send the command there. Otherwise, run the command with 661 | `compile'." 662 | (let ((default-directory (snakemake-snakefile-directory))) 663 | (funcall (if (snakemake-term-use-p) 664 | #'snakemake-term-build-targets 665 | #'snakemake-compile-targets) 666 | targets 667 | args))) 668 | 669 | ;;;###autoload 670 | (defun snakemake-build-targets-at-point (&optional args) 671 | "Build target(s) at point without any prompts. 672 | 673 | $ snakemake [ARGS] -- " 674 | (interactive (list (transient-args 'snakemake))) 675 | (snakemake-build-targets 676 | (or (snakemake-file-targets-at-point 'check) 677 | (snakemake-rule-at-point 'target) 678 | (user-error "No target found at point")) 679 | args)) 680 | 681 | ;;;###autoload 682 | (defun snakemake-build-file-target (&optional args) 683 | "Build target file. 684 | 685 | $ snakemake [ARGS] -- " 686 | (interactive (list (transient-args 'snakemake))) 687 | (snakemake-build-targets 688 | (list (snakemake-read-file-target)) 689 | args)) 690 | 691 | ;;;###autoload 692 | (defun snakemake-build-rule-target (&optional args) 693 | "Build target rule, prompting with known rules. 694 | 695 | $ snakemake [ARGS] -- " 696 | (interactive (list (transient-args 'snakemake))) 697 | (snakemake-build-targets 698 | (list (snakemake-read-rule 'targets)) 699 | args)) 700 | 701 | ;;;###autoload 702 | (defun snakemake-build (&optional args) 703 | "Read and run a Snakemake command for building targets. 704 | 705 | If a terminal is associated with the current Snakefile directory, 706 | send the command there. Otherwise, run the command with 707 | `compile'. 708 | 709 | To start a terminal for the current Snakefile directory, run 710 | `\\[snakemake-term-start]'. 711 | 712 | $ snakemake [ARGS] -- " 713 | (interactive (list (transient-args 'snakemake))) 714 | (let ((cmd (snakemake--make-command 715 | (or (snakemake-file-targets-at-point) 716 | (snakemake-rule-at-point) 717 | (list "")) 718 | args)) 719 | (default-directory (snakemake-snakefile-directory))) 720 | (if (snakemake-term-use-p) 721 | (snakemake-term-send 722 | (read-from-minibuffer "Command to send to terminal: " cmd)) 723 | (let ((compile-command cmd) 724 | (compilation-read-command t)) 725 | (call-interactively #'compile))))) 726 | 727 | (transient-define-argument snakemake:--allowed-rules () 728 | :description "Allowed rules" 729 | :class 'transient-option 730 | :key "-a" 731 | :argument "--allowed-rules=" 732 | :reader 'snakemake-read-rules) 733 | 734 | (transient-define-argument snakemake:--cores () 735 | :description "Number of cores" 736 | :class 'transient-option 737 | :key "-c" 738 | :argument "--cores=") 739 | 740 | ;;;###autoload (autoload 'snakemake "snakemake" nil t) 741 | (transient-define-prefix snakemake () 742 | "Transient for running Snakemake." 743 | :value '("--cores=all") 744 | ["Arguments" 745 | ("-C" "Use conda" "--use-conda") 746 | ("-f" "Force" "--force") 747 | ("-i" "Ignore temp()" "--notemp") 748 | ("-n" "Dry run" "--dryrun") 749 | ("-p" "Print shell commands" "-p") 750 | ("-r" "Print reason" "--reason") 751 | ("-t" "Touch files" "--touch") 752 | (snakemake:--allowed-rules) 753 | (snakemake:--cores)] 754 | ["Actions" 755 | [("c" "Edit and run command" snakemake-build)] 756 | [("p" "Build target at point" snakemake-build-targets-at-point) 757 | ("f" "Build file" snakemake-build-file-target) 758 | ("r" "Build rule" snakemake-build-rule-target)]]) 759 | 760 | ;;;###autoload 761 | (define-obsolete-function-alias 'snakemake-popup 'snakemake "2.0.0") 762 | 763 | (provide 'snakemake) 764 | ;;; snakemake.el ends here 765 | --------------------------------------------------------------------------------