├── .gitignore ├── .gitmodules ├── COPYING ├── README.md ├── athame.c ├── athame.h ├── athame_patcher.sh ├── athame_readline.h ├── athame_util.h ├── athame_zsh.h ├── athamerc ├── bash_readline_setup.sh ├── readline.patch ├── readline_athame_setup.sh ├── test ├── bash │ ├── expected1 │ └── inst1.sh ├── charread.sh ├── prefix.sh ├── runtests.sh ├── shell │ ├── expected1 │ ├── expected2 │ ├── expected3 │ ├── expected4 │ ├── expected5 │ ├── inst1.sh │ ├── inst2.sh │ ├── inst3.sh │ ├── inst4.sh │ └── inst5.sh └── vim │ ├── expected1 │ ├── expected2 │ ├── expected3 │ ├── expected4 │ ├── expected5 │ ├── expected6 │ ├── inst1.sh │ ├── inst2.sh │ ├── inst3.sh │ ├── inst4.sh │ ├── inst5.sh │ └── inst6.sh ├── zsh.patch └── zsh_athame_setup.sh /.gitignore: -------------------------------------------------------------------------------- 1 | test/build 2 | test/testrun 3 | *.tar.bz2 4 | *.tar.gz 5 | *.tar.xz 6 | *_patches 7 | *_tmp 8 | *.sw? 9 | Makefile 10 | *.so* 11 | *.o 12 | *.a 13 | config.* 14 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "vimbed"] 2 | path = vimbed 3 | url = http://github.com/ardagnir/vimbed 4 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Athame 2 | ====== 3 | 4 | Athame patches your shell to add full Vim support by routing your keystrokes through an actual Vim process. Athame can currently be used to patch readline (used by bash, gdb, python, etc) and/or zsh (which doesn't use readline). 5 | 6 | ![Demo](https://i.imgur.com/74EoF4X.gif) 7 | 8 | **Don't most shells already come with a vi-mode?** 9 | 10 | Yes, and if you're fine with basic vi imitations designed by a bunch of Emacs users, feel free to use them. ...but for the true Vim fanatics who sacrifice goats to the modal gods, Athame gives you the full power of Vim. 11 | 12 | ## Requirements 13 | - Athame works best on GNU/Linux. 14 | - Athame also works on Windows (with WSL), and usually works on OSX. (I don't have a Mac, so I can't always test changes on one) 15 | - Athame needs at least *one* of the following: 16 | - Vim 8.0+ with `+job` support 17 | - Vim 7.4+ with `+clientserver` support 18 | - Vim 7.4.928+ is strongly recommended. 19 | - The clientserver version requires X and will fall back to a normal shell if X isn't running. 20 | - Neovim 0.2.2+ 21 | - Neovim support is currently experimental. 22 | - The setup scripts will look for vim by default. You can append `--vimbin=$(which nvim)` to have them use nvim. 23 | 24 | ## Setting up Athame Readline 25 | ### Option 1: (Arch Linux only) Use the AUR 26 | Use `readline-athame-git` from the AUR. 27 | - If you are missing the readline gpg key, you can get it with `gpg --recv-keys BB5869F064EA74AB` 28 | 29 | ### Option 2: (The safest method) Install a local copy of Readline/Bash 30 | git clone --recursive http://github.com/ardagnir/athame 31 | cd athame 32 | mkdir -p ~/local 33 | ./readline_athame_setup.sh --prefix=$HOME/local/ 34 | ./bash_readline_setup.sh --prefix=$HOME/local/ --use_readline=$HOME/local/ 35 | cp athamerc ~/.athamerc 36 | 37 | You can now run ~/local/bin/bash to run bash with Athame. 38 | 39 | ### Option 3: Install Athame as your default Readline 40 | *For Ubuntu/Debian:* 41 | 42 | git clone --recursive http://github.com/ardagnir/athame 43 | cd athame 44 | ./readline_athame_setup.sh --libdir=/lib/x86_64-linux-gnu --use_sudo 45 | 46 | *Otherwise:* 47 | 48 | git clone --recursive http://github.com/ardagnir/athame 49 | cd athame 50 | ./readline_athame_setup.sh --use_sudo 51 | 52 | You may need to rebuild bash if your installed version doesn't use your system readline *(this is usually the case in Ubuntu and OSX)*: 53 | 54 | ./bash_readline_setup.sh --use_sudo 55 | 56 | #### Additional Notes 57 | - You can add the `--nobuild` flag to the setup script if you want to configure/build/install yourself. 58 | - You can change what Vim binary is used by passing `--vimbin=/path/to/vim` to the setup script. 59 | 60 | ## Setting up Athame Zsh 61 | ### Option 1: (Arch Linux only) Use the AUR 62 | Use `zsh-athame-git` from the AUR. 63 | - If you are missing the zsh gpg key, you can get it with `gpg --recv-keys A71D9A9D4BDB27B3` 64 | - Add "unset zle_bracketed_paste" to the end of your ~/.zshrc 65 | 66 | ### Option 2: (The safest method) Install a local copy of Zsh 67 | *Windows Note: zsh tests don't work in Windows because zsh gets stuck in the background. Add `--notest` to the setup script line if installing on Windows.* 68 | 69 | git clone --recursive http://github.com/ardagnir/athame 70 | cd athame 71 | mkdir -p ~/local 72 | ./zsh_athame_setup.sh --prefix=$HOME/local/ 73 | cp athamerc ~/.athamerc 74 | 75 | Add "unset zle_bracketed_paste" to the end of your ~/.zshrc 76 | 77 | You can now run ~/local/bin/zsh to run zsh with Athame. 78 | 79 | ### Option 3: Install Athame as your default Zsh 80 | *Windows Note: zsh tests don't work in Windows because zsh gets stuck in the background. Add `--notest` to the setup script line if installing on Windows.* 81 | 82 | git clone --recursive http://github.com/ardagnir/athame 83 | cd athame 84 | ./zsh_athame_setup.sh --use_sudo 85 | 86 | Add "unset zle_bracketed_paste" to the end of your ~/.zshrc 87 | 88 | #### Additional Notes 89 | - You can add the `--nobuild` flag to the setup script if you want to configure/build/install yourself. 90 | - You can change what Vim binary is used by passing `--vimbin=/path/to/vim` to the setup script. 91 | 92 | 93 | ## Configuration 94 | Athame can be configured through the following environment variables. They can be set on the fly or you can add them to your ~/.bashrc or ~/.zshrc. Make sure you use `export` if you add them to your ~/.zshrc. 95 | 96 | - **ATHAME_ENABLED:** Set to 0 to disable Athame. 97 | - **ATHAME_SHOW_MODE:** Set to 0 to hide mode display at the bottom of the screen. To display it yourself, read the ATHAME_VIM_MODE variable. 98 | - **ATHAME_SHOW_COMMAND:** Set to 0 to hide :, /, and ? commands at the bottom of the screen. To display them yourself, read the ATHAME_VIM_COMMAND variable. 99 | - **ATHAME_SHOW_ERROR:** Set to 0 to hide athame errors. If you want to display these yourself, read the ATHAME_ERROR variable. 100 | - **ATHAME_VIM_PERSIST:** Normally Athame spawns a new short-lived Vim instance for each readline/zle usage (in practice, a separate Vim instance each line). If you set this to 1, you will get 1 Vim instance per shell instead, so your iabbrevs, etc, are preserved from line to line. The tradeoff is that it will keep Vim open until the shell closes, whether or not it is using readline. For example: if you open your terminal and type bash 10 times, with the default ATHAME_VIM_PERSIST of 0 you will have one instance of Vim running, because only the inner shell is using readline, but with ATHAME_VIM_PERSIST set to 1, you will have 10. 101 | - **ATHAME_USE_JOBS:** If non-zero, Athame will use jobs for async behavior. Otherwise, it will use clientserver. 102 | 103 | ## FAQ 104 | #### How do I use this? 105 | It's just like Vim. Imagine your history is stored inside a Vim buffer (because it is!) with a blank line at the bottom. Your cursor starts on that blank line each time readline is called. 106 | Unless you're in command mode, some special chars (such as carriage return) are handled by readline/zsh. 107 | 108 | Some example commands (there's no specific code for these, it's just Vim): 109 | 110 | - j: go back a line in history 111 | - k: go forward a line in history 112 | - ?: search history (and current line) backwards (better if you have :set incsearch enabled) 113 | - /: search history (and current line) forwards (better if you have :set incsearch enabled) 114 | - cc: clear current line and go to insert mode 115 | 116 | The default athamerc modifies up/down arrows in insert mode to jump to the end of the line, like in a normal shell, but you can change this. 117 | 118 | You can also enable history searching arrows in the athamerc. (If you enable this and you use arrows, it searches for text that begins with what you've typed. It's like Vim's command mode.) 119 | 120 | 121 | #### ? and / are reversed from bash's vi-mode. Why? 122 | Bash's vi-mode has ? go down and / go up. In actual Vim (and thus, Athame) they go the other way. 123 | 124 | #### Why does Athame start in insert/normal mode? 125 | The default athamerc includes "startinsert" to make Athame start in insert mode. If you remove that line, it will start in normal mode. 126 | 127 | #### Why is my Athame slow? 128 | Athame should be very fast, but it will slow down if your Vim setup is slow or if you have invalid vimscript in either your athamerc or vimrc. Try using a clean vimrc or athamerc and see if it speeds up. If not, file an issue. 129 | 130 | If Athame is only slow in command mode (:?/), you should be able to fix it by updating Vim to version 8.1+. 131 | 132 | #### I installed Athame for Readline, but it isn't doing anything! 133 | Are you using the system readline? 134 | Type: 135 | 136 | ldd $(which bash) 137 | This should include libreadline. If it doesn't, you need to build bash to use the system readline. You can do this by running: 138 | 139 | ./bash_readline_setup.sh 140 | 141 | #### Which chars are sent to vim and which to readline/zsh? 142 | 143 | Control-C usually sends a SIGNINT signal that is handled by zsh, readline, or the program that calls readline. For other keys: 144 | 145 | - In readline, chars marked as rl_delete (usually Control-D), rl_newline (usually return), rl_complete (usually tab), and rl_clear_screen (usually Control-L) are sent to readline. All other keys are sent to vim. This means that if you use readline's built-in vi-mode, Control-L and Control-D probably won't be sent to the shell. 146 | - In zsh, Athame is hardcoded to send tab, Control-D, carriage return, new line, and Control-L to zsh. All other keys are sent to vim. This behavior will likely change in the future. 147 | 148 | #### I got the error "Couldn't load vim path" 149 | Is Vim at the correct path? You can change the path Athame looks at with the --vimbin flag. 150 | 151 | #### How do I disable/uninstall Athame? 152 | To temporarily disable Athame, `export ATHAME_ENABLED=0` 153 | 154 | To compile readline/zsh without the Athame patches, run the setup script with a `--noathame` flag. 155 | 156 | To get rid of Athame completly, you should probably just replace it with the non-patched version of readline/zsh from your distro. 157 | 158 | Or, if you're cool with not having a shell, you can uninstall it by cding into the readline or zsh directory and typing `sudo make uninstall`. 159 | 160 | Depending on your approach, you may want to manually remove `/usr/lib/athame*` and `/etc/athamerc` as well. 161 | 162 | #### Why do the Up/Down arrows make the cursor jump to the end of the line? 163 | This is done through vimscript in the athamerc to make Athame more like a normal shell. Feel free to comment it out. 164 | 165 | #### How do I disable/change the way Athame shows Vim's mode? 166 | By default Athame shows the Vim mode at the bottom of the screen. This can be disabled using `export ATHAME_SHOW_MODE=0` 167 | 168 | Athame stores the current mode in the `ATHAME_VIM_MODE` environment variable. You can use this to display the Vim mode yourself. See https://github.com/ardagnir/athame/issues/21 for an example using powerline. 169 | 170 | Similarly, Vim commands are displayed at the bottom of the screen by default. These can be disabled using `export ATHAME_SHOW_COMMAND=0` and accessed using `ATHAME_VIM_COMMAND` 171 | 172 | #### Why don't arrow keys work when I add Athame to my convoluted Zsh setup? 173 | Because arrow keys are evil! Just kidding. :P 174 | 175 | Ohmyzsh and some other zsh setups put your terminal into application mode to help their keymaps. Athame doesn't like this, and besides, you're using Vim as your keymap. You can override this by adding the following to the end of your ~/.zshrc: 176 | 177 | function zle-line-init(){ 178 | echoti rmkx 179 | } 180 | zle -N zle-line-init 181 | 182 | #### Why isn't there an Athame package for my favorite distro? 183 | ...because you haven't made one yet. The Athame setup script comes with a --nobuild flag so that you can build it however you want or your package can just apply the Athame patches itself. 184 | 185 | #### This is awesome! Can I help? 186 | The best way to help is to look at the issue section and submit patches to fix bugs. 187 | 188 | If you have a shell that I'm missing, you can also try making a patch to communicate between it and Athame (see athame_readline.h and athame_zsh.h for the functions you need to implement). 189 | 190 | #### What about donations? 191 | I'm not accepting donations, but you should consider donating to the [EFF](https://supporters.eff.org/donate/) so that we don't end up living in a scary dystopian future where everyone is forced to use emacs. 192 | 193 | 194 | ## Bugs 195 | - See [issues](https://github.com/ardagnir/athame/issues) 196 | - If you see a bug without an issue, please create one. 197 | 198 | ## License 199 | GPL v3 200 | -------------------------------------------------------------------------------- /athame.c: -------------------------------------------------------------------------------- 1 | /* athame.c -- Full vim integration for your shell.*/ 2 | 3 | /* Copyright (C) 2015 James Kolb 4 | 5 | This file is part of Athame. 6 | 7 | Athame 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 | Athame 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 Athame. If not, see . 19 | */ 20 | 21 | #define _GNU_SOURCE 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #ifdef BSD 36 | // Includes OSX 37 | #include 38 | typedef void (*sighandler_t) (int); 39 | #else 40 | #include 41 | #endif 42 | 43 | #include "athame.h" 44 | #include "athame_intermediary.h" 45 | #include "athame_util.h" 46 | 47 | /* Forward declarations used in this file. */ 48 | void athame_init(int instream, FILE* outstream) { 49 | if (!ap_is_catching_signals()) { 50 | steal_signal_handler(); 51 | } 52 | athame_init_sig(instream, outstream); 53 | if (!ap_is_catching_signals()) { 54 | return_signal_handler(); 55 | } 56 | } 57 | 58 | static void athame_init_sig(int instream, FILE* outstream) { 59 | athame_outstream = outstream ? outstream : stderr; 60 | cleaned = 0; 61 | expr_pid = 0; 62 | athame_dirty = 0; 63 | updated = 1; 64 | time_to_poll = -1; 65 | vim_started = -1; 66 | stale_polls = 0; 67 | change_since_key = 0; 68 | keys_since_change = 0; 69 | athame_mode[0] = 'n'; 70 | athame_mode[1] = '\0'; 71 | athame_command[0] = '\0'; 72 | command_cursor = 0; 73 | bottom_display[0] = '\0'; 74 | last_athame_mode[0] = '\0'; 75 | bottom_color = 0; 76 | bottom_style = 0; 77 | bottom_cursor = 0; 78 | cs_confirmed = 0; 79 | athame_failure = 0; 80 | msg_sent = 0; 81 | 82 | dev_null = 0; 83 | fifo = 0; 84 | 85 | servername = 0; 86 | dir_name = 0; 87 | slice_file_name = 0; 88 | contents_file_name = 0; 89 | update_file_name = 0; 90 | messages_file_name = 0; 91 | vimbed_file_name = 0; 92 | fifo_name = 0; 93 | msg_count_file_name = 0; 94 | 95 | if (!athame_is_set("ATHAME_ENABLED", 1)) { 96 | athame_failure = strdup("Athame was disabled on init"); 97 | return; 98 | } 99 | if (!athame_is_set("ATHAME_USE_JOBS", ATHAME_USE_JOBS_DEFAULT) && !getenv("DISPLAY")) { 100 | athame_set_failure("Vim with +job required to use Athame without X"); 101 | return; 102 | } 103 | 104 | asprintf(&servername, "athame_%d", getpid()); 105 | 106 | char* tmpdir = getenv(temp_dir_loc()); 107 | if (tmpdir == NULL) { 108 | char* failure; 109 | asprintf(&failure, "%s environment variable not set", temp_dir_loc()); 110 | athame_set_failure(failure); 111 | free(failure); 112 | return; 113 | } 114 | char* parent_name; 115 | asprintf(&parent_name, "%s/athame_vimbed", tmpdir); 116 | asprintf(&dir_name, "%s/%s", parent_name, servername); 117 | mkdir(parent_name, S_IRWXU); 118 | mkdir(dir_name, S_IRWXU); 119 | free(parent_name); 120 | 121 | asprintf(&slice_file_name, "%s/slice.txt", dir_name); 122 | asprintf(&contents_file_name, "%s/contents.txt", dir_name); 123 | asprintf(&update_file_name, "%s/update.txt", dir_name); 124 | asprintf(&messages_file_name, "%s/messages.txt", dir_name); 125 | asprintf(&msg_count_file_name, "%s/messageCount.txt", dir_name); 126 | if (getenv("ATHAME_VIMBED_LOCATION")) { 127 | asprintf(&vimbed_file_name, "%s/vimbed.vim", 128 | getenv("ATHAME_VIMBED_LOCATION")); 129 | } else { 130 | asprintf(&vimbed_file_name, "%s/vimbed.vim", VIMBED_LOCATION); 131 | } 132 | 133 | asprintf(&fifo_name, "%s/exprPipe", dir_name); 134 | mkfifo(fifo_name, S_IRWXU); 135 | 136 | dev_null = fopen("/dev/null", "w"); 137 | 138 | if (athame_setup_history()) { 139 | return; 140 | } 141 | 142 | if (is_vim_alive() && athame_is_set("ATHAME_USE_JOBS", ATHAME_USE_JOBS_DEFAULT)) { 143 | if (athame_setup_fifo()) { 144 | // Vim is still alive but the fifo isn't so we have to kill it. 145 | kill(vim_pid, SIGTERM); 146 | close(vim_term); 147 | wait_then_kill(vim_pid); 148 | } 149 | } 150 | 151 | if (is_vim_alive()) { 152 | vim_stage = VIM_NEEDS_RESET; 153 | } else { 154 | vim_stage = VIM_NOT_STARTED; 155 | } 156 | 157 | athame_ensure_vim(1, instream); 158 | } 159 | 160 | // Cleanup everything athame related. 161 | // If locked is set, make sure not to let control leave athame during cleanup. 162 | // (This is useful if the shell wants to shutdown as soon as it gets any control) 163 | void athame_cleanup(int locked) { 164 | if (cleaned) { 165 | return; 166 | } 167 | cleaned = 1; 168 | int persist = athame_is_set("ATHAME_VIM_PERSIST", 0) && 169 | vim_stage == VIM_RUNNING && is_vim_alive(); 170 | 171 | if (expr_pid > 0) { 172 | kill(expr_pid, SIGTERM); 173 | } 174 | if (vim_pid && !persist) { 175 | // forkpty will keep vim open on OSX if we don't close the fd 176 | kill(vim_pid, SIGTERM); 177 | athame_sleep(20, 0, 0); 178 | close(vim_term); 179 | } 180 | if (dev_null) { 181 | fclose(dev_null); 182 | } 183 | if (vimbed_file_name) { 184 | free(vimbed_file_name); 185 | } 186 | if (servername) { 187 | free(servername); 188 | } 189 | if (athame_failure) { 190 | if(!locked) { 191 | athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0); 192 | } 193 | free((char*)athame_failure); 194 | } else if (athame_is_set("ATHAME_SHOW_MODE", 1)) { 195 | if (!locked) { 196 | athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0); 197 | } 198 | } 199 | if (expr_pid > 0) { 200 | wait_then_kill(expr_pid); 201 | } 202 | if (vim_pid && !persist) { 203 | wait_then_kill(vim_pid); 204 | vim_pid = 0; 205 | } 206 | if (fifo) { 207 | close(fifo); 208 | } 209 | if (fifo_name) { 210 | unlink(fifo_name); 211 | free(fifo_name); 212 | } 213 | if (slice_file_name) { 214 | unlink(slice_file_name); 215 | free(slice_file_name); 216 | } 217 | if (contents_file_name) { 218 | unlink(contents_file_name); 219 | free(contents_file_name); 220 | } 221 | if (update_file_name) { 222 | unlink(update_file_name); 223 | free(update_file_name); 224 | } 225 | if (messages_file_name) { 226 | unlink(messages_file_name); 227 | free(messages_file_name); 228 | } 229 | if (msg_count_file_name) { 230 | unlink(msg_count_file_name); 231 | free(msg_count_file_name); 232 | } 233 | if (dir_name) { 234 | rmdir(dir_name); 235 | free(dir_name); 236 | } 237 | } 238 | 239 | int athame_enabled() { 240 | if (athame_is_set("ATHAME_ENABLED", 1)) { 241 | if (athame_failure) { 242 | athame_draw_failure(); 243 | return 0; 244 | } 245 | unsetenv("ATHAME_ERROR"); 246 | return !ap_temp_novim(); 247 | } else { 248 | return 0; 249 | } 250 | } 251 | 252 | char athame_loop(int instream) { 253 | if (!ap_is_catching_signals()) { 254 | steal_signal_handler(); 255 | } 256 | char r = athame_loop_sig(instream); 257 | if (!ap_is_catching_signals()) { 258 | return_signal_handler(); 259 | } 260 | return r; 261 | } 262 | 263 | static char athame_loop_sig(int instream) { 264 | char returnVal = 0; 265 | sent_to_vim = 0; 266 | time_to_sync = get_time(); 267 | last_athame_mode[0] = '\0'; 268 | bottom_display[0] = '\0'; 269 | 270 | // This is a performance step that allows us to bypass starting up (or resetting) vim if we 271 | // aren't going to talk to it. 272 | char first_char = 273 | (vim_stage != VIM_RUNNING) ? athame_get_first_char(instream) : 0; 274 | if (first_char && strchr(ap_nl, first_char)) { 275 | return first_char; 276 | } 277 | athame_ensure_vim(0, 0); 278 | 279 | // If we have a character already, assume Vim hasn't had time to sync. 280 | vim_sync = first_char ? VIM_SYNC_NO : VIM_SYNC_YES; 281 | 282 | if (!updated) { 283 | athame_update_vimline(athame_row, ap_get_cursor()); 284 | } else { 285 | athame_redisplay(); 286 | } 287 | 288 | if (first_char && !athame_failure) { 289 | returnVal = athame_process_char(first_char); 290 | first_char = 0; 291 | } 292 | 293 | while (!returnVal && !athame_failure) { 294 | int selected = 0; 295 | long timeout_msec = get_timeout_msec(); 296 | selected = athame_select(instream, vim_term, 0, timeout_msec, 0); 297 | 298 | // nvim doesn't end the process when vim quits cleanly 299 | int nvim_checked_quit = athame_nvim && athame_has_clean_quit(); 300 | 301 | if (!athame_failure && (!is_vim_alive() || nvim_checked_quit)) { 302 | if (!nvim_checked_quit && !athame_has_clean_quit()) { 303 | athame_set_failure("Vim quit unexpectedly"); 304 | break; 305 | } 306 | if (!sent_to_vim) { 307 | // We never want to kill the user's shell without giving 308 | // them a chance to type anything. 309 | athame_set_failure("Vim quit"); 310 | break; 311 | } 312 | ap_set_line_buffer(""); 313 | athame_mode[0] = 'n'; 314 | athame_mode[1] = '\0'; 315 | athame_redisplay(); 316 | return ap_delete; 317 | } 318 | int sr = process_signals(); 319 | if (sr != ATHAME_CONTINUE) { 320 | return (char)sr; 321 | } 322 | switch (selected) { 323 | case 0: // Timeout 324 | if (vim_sync >= VIM_SYNC_CHAR_BEHIND) { 325 | vim_sync = VIM_SYNC_NEEDS_POLL; 326 | } 327 | break; 328 | case 1: // Input received 329 | returnVal = athame_process_input(instream); 330 | break; 331 | case 2: // Vim changed 332 | read(vim_term, athame_buffer, DEFAULT_BUFFER_SIZE - 1); 333 | if (!athame_get_vim_info()) { 334 | request_poll(); 335 | } 336 | break; 337 | case -1: // Error (possibly from a signal) 338 | // Make sure we keep the mode drawn on a resize. 339 | // This must happen after process_signals so we don't change the 340 | // value of the caught signal 341 | athame_bottom_mode(); 342 | } 343 | 344 | if (ap_needs_to_leave()) { 345 | if (athame_is_set("ATHAME_SHOW_MODE", 1)) { 346 | athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0); 347 | } 348 | return '\0'; 349 | } 350 | 351 | if (time_to_poll >= 0 && time_to_poll < get_time()) { 352 | athame_poll_vim(0); 353 | } 354 | 355 | // If vim isn't doing anything after pressing 20 keys, failover. 356 | // This allows the user to get back to a normal shell if everything 357 | // is working, but vim always starts up in a weird state 358 | // (Infinte, loop/ex mode, etc) 359 | // TODO: use an env variable instead of 20 360 | if (keys_since_change > 20) { 361 | athame_force_vim_sync(); 362 | if (keys_since_change > 0) { 363 | athame_set_failure("20 keys pressed without change. To disable Athame use: AHTAME_ENABLED=0"); 364 | } 365 | } 366 | } // end while 367 | 368 | if (athame_failure) { 369 | // If we ate the first_char, we should return it. 370 | return returnVal || first_char; 371 | } 372 | 373 | if (sent_to_vim) { 374 | // Should already be synced here, but this is a no-op in that case 375 | // and it makes the code less fragile to make sure. 376 | athame_force_vim_sync(); 377 | if (strcmp(athame_mode, "i") == 0) { 378 | athame_send_to_vim('\x1d'); // Finish abbrevs/kill mappings 379 | athame_force_vim_sync(); 380 | } 381 | } 382 | if (athame_is_set("ATHAME_SHOW_MODE", 1)) { 383 | athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0); 384 | } 385 | updated = 0; 386 | return returnVal; 387 | } 388 | 389 | void athame_after_bypass() { 390 | if (athame_failure && athame_is_set("ATHAME_SHOW_ERROR", 1)) { 391 | athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0); 392 | } 393 | } 394 | 395 | // Redraw bottom stuff before handing off to a process that might block, 396 | // like python. 397 | void athame_char_handled() { 398 | if (athame_is_set("ATHAME_ENABLED", 1) && !ap_needs_to_leave()) { 399 | if (athame_failure) { 400 | athame_draw_failure(); 401 | } else { 402 | athame_bottom_mode(); 403 | } 404 | } 405 | } 406 | -------------------------------------------------------------------------------- /athame.h: -------------------------------------------------------------------------------- 1 | /* athame.h -- Full vim integration for your shell.*/ 2 | 3 | /* Copyright (C) 2015 James Kolb 4 | 5 | This file is part of Athame. 6 | 7 | Athame 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 | Athame 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 Athame. If not, see . 19 | */ 20 | 21 | #ifndef _ATHAME_H_ 22 | #define _ATHAME_H_ 23 | 24 | #include 25 | 26 | #define ATHAME_NORMAL 0 27 | #define ATHAME_BOLD 1 28 | 29 | #define ATHAME_DEFAULT 0 30 | #define ATHAME_RED 31 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | // Initialize athame. 37 | extern void athame_init(int instream, FILE* outstream); 38 | static void athame_init_sig(int instream, FILE* outstream); 39 | 40 | // Loop over input and do vimy stuff until a special key is pressed. 41 | extern char athame_loop(int instream); 42 | static char athame_loop_sig(int instream); 43 | 44 | // Is athame currently enabled? 45 | extern int athame_enabled(); 46 | 47 | // Cleanup all the memory allocated by athame and shut vim down. 48 | extern void athame_cleanup(int); 49 | 50 | // Run after bypassing athame. 51 | extern void athame_after_bypass(); 52 | 53 | // Called after we are done with the char returned by athame. 54 | // This is really a hack to display mode while an external process is waiting for char input that it will probably send to readline and thus vim. 55 | extern void athame_char_handled(); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /* !_ATHAME_H_ */ 62 | -------------------------------------------------------------------------------- /athame_patcher.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # athame_patcher.sh -- Full vim integration for your shell. 3 | # 4 | # Copyright (C) 2015 James Kolb 5 | # 6 | # This file is part of Athame. 7 | # 8 | # Athame 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 # (at your option) any later version. 11 | # 12 | # Athame 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 Athame. If not, see . 19 | 20 | if [ "$1" == "--help" ]; then 21 | echo "Usage athame_patcher.sh [ --dirty ] ( readline | zsh ) athame_dir" 22 | exit 0 23 | fi 24 | 25 | if [ "$1" == "--dirty" ]; then 26 | dirty=1 27 | ptype="$2" 28 | adir="$3" 29 | else 30 | dirty=0 31 | ptype="$1" 32 | adir="$2" 33 | fi 34 | 35 | if [ "$ptype" != "zsh" ] && [ "$ptype" != "readline" ]; then 36 | echo Invalid patch "$ptype" >&2 37 | exit 1 38 | fi 39 | 40 | if [ -z "$adir" ]; then 41 | echo No directory provided 42 | exit 1 43 | fi 44 | 45 | if [ $dirty = 0 ]; then 46 | echo "Patching with Athame patch" 47 | patch -p1 < "$adir/$ptype.patch" 48 | fi 49 | 50 | sdir="." 51 | if [ "$ptype" = zsh ]; then 52 | sdir="./Src/Zle" 53 | fi 54 | 55 | rm -rf "$sdir/vimbed" 56 | cp -r "$adir/vimbed" "$sdir" 57 | echo "Copying Athame files" 58 | cp "$adir/athame".* "$sdir" 59 | cp "$adir/athame_util.h" "$sdir" 60 | cp "$adir/athame_$ptype.h" "$sdir/athame_intermediary.h" 61 | -------------------------------------------------------------------------------- /athame_readline.h: -------------------------------------------------------------------------------- 1 | /* athame_readline.h -- Full vim integration for your shell.*/ 2 | 3 | /* Copyright (C) 2015 James Kolb 4 | 5 | This file is part of Athame. 6 | 7 | Athame 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 | Athame 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 Athame. If not, see . 19 | */ 20 | 21 | #define READLINE_LIBRARY 22 | #include 23 | 24 | #include "history.h" 25 | #include "readline.h" 26 | #include "rlprivate.h" 27 | #include "xmalloc.h" 28 | 29 | char* athame_buffer_store = 0; 30 | static char* ap_get_line_buffer() { 31 | if (rl_end == 0) { 32 | return ""; 33 | } 34 | if (athame_buffer_store) { 35 | free(athame_buffer_store); 36 | } 37 | return athame_buffer_store = strndup(rl_line_buffer, rl_end); 38 | } 39 | 40 | static int ap_get_line_buffer_length() { return rl_end; } 41 | 42 | static int helper_ap_char_length(char* buffer) { 43 | int ret; 44 | int len = mbstowcs(NULL, buffer, 0); 45 | return len; 46 | } 47 | 48 | static int ap_get_line_char_length() { 49 | return helper_ap_char_length(ap_get_line_buffer()); 50 | } 51 | 52 | static void ap_set_line_buffer(char* newText) { rl_replace_line(newText, 0); } 53 | 54 | static int ap_get_cursor() { 55 | int ret; 56 | char* temp = strndup(rl_line_buffer, rl_point); 57 | ret = helper_ap_char_length(temp); 58 | free(temp); 59 | return ret; 60 | } 61 | 62 | static void ap_set_cursor(int c) { 63 | rl_point = 0; 64 | if (c == 0) { 65 | return; 66 | } 67 | rl_forward_char(c, 'l'); 68 | } 69 | 70 | static void ap_set_cursor_end() { rl_point = rl_end; } 71 | 72 | static int ap_temp_novim() { return 0; } 73 | 74 | static void ap_redraw_prompt() { 75 | // Substitutions are already performed before readline gets the prompt. 76 | } 77 | 78 | static void ap_display() { rl_redisplay(); } 79 | 80 | static void ap_get_term_size(int* height, int* width) { 81 | _rl_sigwinch_resize_terminal(); // Incase the terminal changed size while 82 | // readline wasn't looking. 83 | rl_get_screen_size(height, width); 84 | } 85 | 86 | static int ap_get_prompt_length() { return rla_prompt_phys_length(); } 87 | 88 | HISTORY_STATE* hs; 89 | int hs_counter; 90 | static void ap_get_history_start() { 91 | hs = history_get_history_state(); 92 | hs_counter = 0; 93 | } 94 | 95 | static char* ap_get_history_next() { 96 | if (hs->entries && hs->entries[hs_counter]) { 97 | return hs->entries[hs_counter++]->line; 98 | } else { 99 | return NULL; 100 | } 101 | } 102 | 103 | static void ap_get_history_end() { xfree(hs); } 104 | 105 | static int ap_needs_to_leave() { 106 | return rl_done || rl_num_chars_to_read > 0 && rl_end >= rl_num_chars_to_read; 107 | } 108 | 109 | static char* ap_get_substr(char* text, int start, int end) { 110 | int mbchars; 111 | int pos_s = 0; 112 | int pos = 0; 113 | for (mbchars = 0; mbchars < end; mbchars++) { 114 | if (mbchars == start) { 115 | pos_s = pos; 116 | } 117 | int l = mblen(text + pos, MB_CUR_MAX); 118 | if (l >= 0) { 119 | pos += l; 120 | } else { 121 | if (mbchars < start) { 122 | return strdup(""); 123 | } 124 | break; 125 | } 126 | } 127 | return strndup(text + pos_s, pos - pos_s); 128 | } 129 | 130 | static char ap_handle_signals() { 131 | if (_rl_caught_signal == SIGINT || _rl_caught_signal == SIGHUP) { 132 | athame_cleanup(_rl_caught_signal == SIGHUP); 133 | _rl_signal_handler(_rl_caught_signal); 134 | if (rl_signal_event_hook) { 135 | (*rl_signal_event_hook)(); 136 | } 137 | return '\x03'; // 138 | } 139 | return 0; 140 | } 141 | 142 | static char ap_delete; 143 | static char ap_special[KEYMAP_SIZE]; 144 | static char ap_nl[KEYMAP_SIZE]; 145 | 146 | static void ap_set_control_chars() { 147 | // In default readline these are: tab, , , and all the newline keys. 148 | int specialLen = 0; 149 | int nlLen = 0; 150 | ap_delete = '\x04'; 151 | for (int key = 0; key < KEYMAP_SIZE; key++) { 152 | if (_rl_keymap[key].type == ISFUNC) { 153 | if (_rl_keymap[key].function == rl_delete) { 154 | ap_delete = key; 155 | ap_special[specialLen++] = key; 156 | } else if (_rl_keymap[key].function == rl_newline) { 157 | ap_special[specialLen++] = key; 158 | ap_nl[nlLen++] = key; 159 | } else if (_rl_keymap[key].function == rl_complete || 160 | _rl_keymap[key].function == rl_menu_complete || 161 | _rl_keymap[key].function == rl_backward_menu_complete || 162 | _rl_keymap[key].function == rl_clear_screen) { 163 | ap_special[specialLen++] = key; 164 | } 165 | } 166 | } 167 | ap_special[specialLen] = '\0'; 168 | } 169 | 170 | // Tells readline that we weren't in the middle of tab completion, search, etc. 171 | static void ap_set_nospecial() { rl_last_func = rl_insert; } 172 | 173 | static int ap_is_catching_signals() { 174 | return rl_catch_signals; 175 | } 176 | -------------------------------------------------------------------------------- /athame_util.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void athame_send_to_vim(char input); 5 | static int athame_get_vim_info(); 6 | static void athame_set_failure(char* fail_str); 7 | static char athame_process_input(int instream); 8 | static char athame_process_char(char instream); 9 | static int athame_setup_history(); 10 | static int athame_sleep(int msec, int char_break, int instream); 11 | static int athame_get_vim_info_inner(); 12 | static void athame_update_vimline(int row, int col); 13 | static int athame_setup_fifo(); 14 | static int athame_remote_expr(char* expr, int block); 15 | static int athame_remote_expr_cs(char* expr, int block); 16 | static int athame_remote_expr_v8(char* expr, int block); 17 | static char athame_get_first_char(int instream); 18 | static void athame_highlight(int start, int end); 19 | static void athame_redisplay(); 20 | static void athame_bottom_display(char* string, int style, int color, 21 | int cursor, int force); 22 | static void athame_bottom_mode(); 23 | static void athame_poll_vim(int block); 24 | static void athame_draw_failure(); 25 | static int athame_has_clean_quit(); 26 | static int athame_wait_for_file(char* file_name, int sanity, int char_break, 27 | int instream); 28 | static int athame_select(int file_desc1, int file_desc2, int timeout_sec, 29 | int timeout_ms, int no_signals); 30 | static int athame_is_set(char* env, int def); 31 | static char* athame_tok(char** pointer, char delim); 32 | static long get_time(); 33 | static void athame_force_vim_sync(); 34 | 35 | #define ATHAME_CONTINUE 256 36 | #define DEFAULT_BUFFER_SIZE 2048 37 | 38 | static char athame_buffer[DEFAULT_BUFFER_SIZE]; 39 | static char bottom_display[DEFAULT_BUFFER_SIZE]; 40 | static char athame_command[DEFAULT_BUFFER_SIZE]; 41 | static int athame_nvim = 0; 42 | static int cleaned = 0; 43 | static int command_cursor; 44 | static int bottom_color; 45 | static int bottom_style; 46 | static int bottom_cursor; 47 | static const char* athame_failure; 48 | static int msg_sent = 0; 49 | static int vim_pid = 0; 50 | static int expr_pid; 51 | static int vim_term; 52 | static int cs_confirmed; 53 | static FILE* dev_null; 54 | static int fifo; 55 | static int athame_row; 56 | static int updated; 57 | static char* slice_file_name; 58 | static char* contents_file_name; 59 | static char* update_file_name; 60 | static char* messages_file_name; 61 | static char* vimbed_file_name; 62 | static char* dir_name; 63 | static char* fifo_name; 64 | static char* msg_count_file_name; 65 | static char* servername; 66 | #define VIM_NOT_STARTED 0 67 | #define VIM_TRIED_START 1 68 | #define VIM_CONFIRMED_START 2 69 | #define VIM_RUNNING 3 70 | #define VIM_NEEDS_RESET 4 71 | static int vim_stage = VIM_NOT_STARTED; 72 | 73 | // Have we sent any keys to vim since readline started. 74 | static int sent_to_vim = 0; 75 | 76 | // How closely is vim synced with athame? 77 | #define VIM_SYNC_YES 0 78 | #define VIM_SYNC_NEEDS_INFO_READ 1 79 | #define VIM_SYNC_WAITING_POLL_DONE 2 80 | #define VIM_SYNC_NEEDS_POLL 3 81 | #define VIM_SYNC_CHAR_BEHIND 4 82 | #define VIM_SYNC_NO 5 83 | static int vim_sync = VIM_SYNC_YES; 84 | static long time_to_sync = 0; 85 | 86 | // Measured in ms since epoch 87 | static long time_to_poll = -1; 88 | static long vim_started = -1; 89 | 90 | // Number of stale polls since last keypress or change 91 | static int stale_polls = 0; 92 | 93 | static int change_since_key = 0; 94 | 95 | // For figure out if vim got stuck 96 | static int keys_since_change = 0; 97 | 98 | static FILE* athame_outstream = 0; 99 | 100 | static char athame_mode[3]; 101 | static char last_athame_mode[3]; 102 | static int end_col; // For visual mode 103 | static int end_row; // For visual mode 104 | 105 | static int athame_dirty; 106 | 107 | static int start_vim(int char_break, int instream) { 108 | if (char_break && athame_select(instream, -1, 0, 0, 0) > 0) { 109 | return 2; 110 | } 111 | 112 | char* etcrc = "/etc/athamerc"; 113 | char homerc[256]; 114 | char* athamerc = getenv("ATHAME_TEST_RC"); 115 | int testrc = athamerc != NULL; 116 | if (!testrc) { 117 | snprintf(homerc, 255, "%s/.athamerc", getenv("HOME")); 118 | if (!access(homerc, R_OK)) { 119 | athamerc = homerc; 120 | } else if (!access(etcrc, R_OK)) { 121 | athamerc = etcrc; 122 | } else { 123 | athame_set_failure("No athamerc found"); 124 | return 1; 125 | } 126 | } 127 | 128 | int pid = forkpty(&vim_term, NULL, NULL, NULL); 129 | if (pid == 0) { 130 | int cursor = ap_get_cursor(); 131 | snprintf(athame_buffer, DEFAULT_BUFFER_SIZE - 1, 132 | "+call Vimbed_UpdateText(%d, %d, %d, %d, 1, 'StartLine')", 133 | athame_row + 1, cursor + 1, athame_row + 1, cursor + 1); 134 | int vim_error = 0; 135 | char* setup_str; 136 | asprintf(&setup_str, "+call Vimbed_SetupVimbed('', '%s', 'slice')", dir_name); 137 | if (athame_is_set("ATHAME_USE_JOBS", ATHAME_USE_JOBS_DEFAULT)) { 138 | if (testrc) { 139 | vim_error = 140 | execl(ATHAME_VIM_BIN, "vim", "-u", "NONE", "-S", vimbed_file_name, 141 | "-S", athamerc, setup_str, athame_buffer, NULL); 142 | } else { 143 | vim_error = execl(ATHAME_VIM_BIN, "vim", "-S", vimbed_file_name, "-S", 144 | athamerc, setup_str, athame_buffer, NULL); 145 | } 146 | free(setup_str); 147 | } else { 148 | if (testrc) { 149 | vim_error = execl(ATHAME_VIM_BIN, "vim", "--servername", servername, "-u", 150 | "NONE", "-S", vimbed_file_name, "-S", athamerc, 151 | setup_str, athame_buffer, NULL); 152 | } else { 153 | vim_error = execl(ATHAME_VIM_BIN, "vim", "--servername", servername, "-S", 154 | vimbed_file_name, "-S", athamerc, setup_str, 155 | athame_buffer, NULL); 156 | } 157 | } 158 | free(setup_str); 159 | if (vim_error != 0) { 160 | printf("Error: %d", errno); 161 | exit(EXIT_FAILURE); 162 | } 163 | return 1; 164 | } else if (pid == -1) { 165 | athame_set_failure("Failure starting Vim"); 166 | return 1; 167 | } else { 168 | vim_pid = pid; 169 | ap_set_control_chars(); 170 | return 0; 171 | } 172 | } 173 | 174 | static int confirm_vim_start(int char_break, int instream) { 175 | int selected = athame_select(vim_term, char_break ? instream : -1, 176 | char_break ? 5 : 1, 0, 1); 177 | if (selected == -1) { 178 | athame_set_failure("Select interupted"); 179 | return 1; 180 | } 181 | if (selected == 0) { 182 | athame_set_failure("Vim timed out"); 183 | return 1; 184 | } else if (selected == 2) { 185 | return 2; 186 | } 187 | 188 | int amount = read(vim_term, athame_buffer, 5); 189 | athame_buffer[amount] = '\0'; 190 | if (strncmp(athame_buffer, "Error", 5) == 0) { 191 | char* error; 192 | asprintf(&error, "Couldn't load vim path: %s", ATHAME_VIM_BIN); 193 | athame_set_failure(error); 194 | free(error); 195 | return 1; 196 | } else if (strncmp(athame_buffer, "VIM", 3) == 0) 197 | // Vim should dump termcap stuff before this if things are working, so only 198 | // bother checking for errors if we start with VIM. 199 | // If Vim changes to not start errors with "VIM", we will still catch the 200 | // error later with the vague "Vimbed failure" label 201 | { 202 | athame_sleep(20, 0, 0); 203 | amount = read(vim_term, athame_buffer, 200); 204 | athame_buffer[amount] = '\0'; 205 | if (strstr(athame_buffer, "--servername")) { 206 | char* error; 207 | asprintf(&error, "%s was not compiled with clientserver support.", 208 | ATHAME_VIM_BIN); 209 | 210 | athame_set_failure(error); 211 | free(error); 212 | return 1; 213 | } //--servername 214 | } // VIM 215 | return 0; 216 | } 217 | 218 | static int athame_wait_for_vim(int char_break, int instream) { 219 | int error = athame_wait_for_file(slice_file_name, 50, char_break, instream); 220 | if (error == 1) { 221 | if (athame_wait_for_file(contents_file_name, 1, 0, 0) == 0) { 222 | athame_set_failure("Using incompatible vimbed version"); 223 | } else { 224 | athame_set_failure("Vimbed failure"); 225 | } 226 | } 227 | if (error > 0) { 228 | return error; 229 | } 230 | 231 | snprintf(athame_buffer, DEFAULT_BUFFER_SIZE-1, "%s/nvim", dir_name); 232 | athame_nvim = !athame_wait_for_file(athame_buffer, 0, 0, 0); 233 | 234 | vim_started = get_time(); 235 | 236 | if (char_break) { 237 | // Grab the mode now because the program might block (like gdb). 238 | int i; 239 | for (i = 0; i < 6; i++) { 240 | if (athame_mode[0] != 'n' || athame_sleep(10, char_break, instream)) { 241 | break; 242 | } 243 | athame_get_vim_info(); 244 | } 245 | } else { 246 | athame_get_vim_info(); 247 | } 248 | athame_redisplay(); 249 | 250 | return 0; 251 | } 252 | 253 | // Make sure vim is running, 254 | // or set athame_failure, 255 | // or (if char_break) break on char press 256 | static void athame_ensure_vim(int char_break, int instream) { 257 | // These will fallthrough if not interrupted. 258 | if (vim_stage == VIM_NOT_STARTED) { 259 | if (!start_vim(char_break, instream)) { 260 | vim_stage = VIM_TRIED_START; 261 | } 262 | } 263 | if (vim_stage == VIM_TRIED_START) { 264 | if (!confirm_vim_start(char_break, instream)) { 265 | vim_stage = VIM_CONFIRMED_START; 266 | } 267 | } 268 | if (vim_stage == VIM_CONFIRMED_START) { 269 | if (!athame_wait_for_vim(char_break, instream)) { 270 | vim_stage = VIM_RUNNING; 271 | } 272 | } 273 | if (vim_stage == VIM_NEEDS_RESET) { 274 | if (char_break && athame_select(instream, -1, 0, 0, 0) > 0) { 275 | return; 276 | } 277 | athame_remote_expr("Vimbed_Reset()", 1); 278 | int cursor = ap_get_cursor(); 279 | snprintf(athame_buffer, DEFAULT_BUFFER_SIZE - 1, 280 | "Vimbed_UpdateText(%d, %d, %d, %d, 1, 'StartLine')", 281 | athame_row + 1, cursor + 1, athame_row + 1, cursor + 1); 282 | athame_remote_expr(athame_buffer, 1); 283 | athame_poll_vim(1); 284 | athame_get_vim_info(); 285 | athame_redisplay(); 286 | vim_stage = VIM_RUNNING; 287 | } 288 | } 289 | 290 | static int athame_wait_for_file(char* file_name, int sanity, int char_break, 291 | int instream) { 292 | // Check for existance of a file to see if we have advanced that far 293 | FILE* theFile = 0; 294 | theFile = fopen(file_name, "r"); 295 | while (!theFile) { 296 | if (sanity-- < 0) { 297 | return 1; 298 | } 299 | if (athame_sleep(20, char_break, instream)) { 300 | return 2; 301 | } 302 | theFile = fopen(file_name, "r"); 303 | } 304 | fclose(theFile); 305 | return 0; 306 | } 307 | 308 | static char athame_get_first_char(int instream) { 309 | char return_val = '\0'; 310 | if (athame_select(instream, -1, 0, 0, 0) > 0) { 311 | read(instream, &return_val, 1); 312 | } 313 | return return_val; 314 | } 315 | 316 | static int athame_sleep(int msec, int char_break, int instream) { 317 | if (char_break) { 318 | return athame_select(instream, -1, 0, msec, 1) > 0; 319 | } else { 320 | struct timespec timeout, timeout2; 321 | timeout.tv_sec = 0; 322 | timeout.tv_nsec = msec * 1000000; 323 | while (nanosleep(&timeout, &timeout2) < 0) { 324 | timeout = timeout2; 325 | } 326 | return 0; 327 | } 328 | } 329 | 330 | /* Write history file and store number of lines in athame_row */ 331 | static int athame_setup_history() { 332 | FILE* updateFile = fopen(update_file_name, "w+"); 333 | 334 | if (!updateFile) { 335 | char* fail_str; 336 | asprintf(&fail_str, "Couldn't create temporary file in %s", dir_name); 337 | athame_set_failure(fail_str); 338 | free(fail_str); 339 | return 1; 340 | } 341 | 342 | ap_get_history_start(); 343 | int total_lines = 0; 344 | char* history_line; 345 | while (history_line = ap_get_history_next()) { 346 | fwrite(history_line, 1, strlen(history_line), updateFile); 347 | // Count newlines in history 348 | while (history_line = strstr(history_line, "\n")) { 349 | history_line++; 350 | total_lines++; 351 | } 352 | fwrite("\n", 1, 1, updateFile); 353 | total_lines++; 354 | } 355 | fwrite(ap_get_line_buffer(), 1, ap_get_line_buffer_length(), updateFile); 356 | fwrite("\n", 1, 1, updateFile); 357 | athame_row = total_lines; 358 | ap_get_history_end(); 359 | 360 | fclose(updateFile); 361 | return 0; 362 | } 363 | 364 | static int athame_remote_expr(char* expr, int block) { 365 | if (athame_failure) { 366 | return 1; 367 | } 368 | 369 | if (athame_is_set("ATHAME_USE_JOBS", ATHAME_USE_JOBS_DEFAULT)){ 370 | return athame_remote_expr_v8(expr, block); 371 | } else { 372 | return athame_remote_expr_cs(expr, block); 373 | } 374 | }; 375 | 376 | // Sends a message to vim 377 | // 378 | // All the use_pipe stuff is for checking that clientserver actually works. By 379 | // now Vim has told us that it does, but we actually make sure since some 380 | // versions (like MacVim) lie about it. 381 | static int athame_remote_expr_cs(char* expr, int block) { 382 | int stdout_to_readline[2]; 383 | int stderr_to_readline[2]; 384 | int use_pipe = 0; 385 | if (block && !cs_confirmed) { 386 | use_pipe = 1; 387 | pipe(stdout_to_readline); 388 | pipe(stderr_to_readline); 389 | } 390 | // wait for last remote_expr to finish 391 | if (expr_pid > 0) { 392 | if (waitpid(expr_pid, NULL, block ? 0 : WNOHANG) == 0) { 393 | return -1; 394 | } 395 | } 396 | 397 | expr_pid = fork(); 398 | if (expr_pid == 0) { 399 | if (use_pipe) { 400 | dup2(stdout_to_readline[1], STDOUT_FILENO); 401 | dup2(stderr_to_readline[1], STDERR_FILENO); 402 | close(stdout_to_readline[0]); 403 | close(stderr_to_readline[0]); 404 | } else { 405 | dup2(fileno(dev_null), STDOUT_FILENO); 406 | dup2(fileno(dev_null), STDERR_FILENO); 407 | } 408 | 409 | execl(ATHAME_VIM_BIN, "vim", "--servername", servername, "--remote-expr", 410 | expr, NULL); 411 | printf("Expr Error:%d", errno); 412 | exit(EXIT_FAILURE); 413 | } else if (expr_pid == -1) { 414 | // TODO: error handling 415 | if (use_pipe) { 416 | close(stdout_to_readline[0]); 417 | close(stderr_to_readline[0]); 418 | close(stdout_to_readline[1]); 419 | close(stderr_to_readline[1]); 420 | } 421 | athame_set_failure("Clientserver error"); 422 | return -1; 423 | } else { 424 | if (block) { 425 | waitpid(expr_pid, NULL, 0); 426 | expr_pid = 0; 427 | } 428 | if (use_pipe) { 429 | close(stdout_to_readline[1]); 430 | close(stderr_to_readline[1]); 431 | int selected = athame_select(stdout_to_readline[0], stderr_to_readline[0], 432 | -1, -1, 0); 433 | if (selected > 0) { 434 | char error[80]; 435 | if (selected == 2 || read(stdout_to_readline[0], error, 1) < 1) { 436 | error[read(stderr_to_readline[0], error, 437 | sizeof(error) / sizeof(char))] = '\0'; 438 | snprintf(athame_buffer, DEFAULT_BUFFER_SIZE - 1, 439 | "Clientserver error for %s:%s", expr, error); 440 | athame_set_failure(athame_buffer); 441 | close(stdout_to_readline[0]); 442 | close(stderr_to_readline[0]); 443 | return -1; 444 | } 445 | cs_confirmed = 1; 446 | } 447 | close(stdout_to_readline[0]); 448 | close(stderr_to_readline[0]); 449 | } 450 | } 451 | return 0; 452 | } 453 | 454 | 455 | //If block, blocks while an expr is in flight (gives up after ~3 sec for safety) 456 | //Otherwise returns 1 if an expr is in flight. 457 | static int check_expr_in_flight(int block) { 458 | char msgCount[256]; 459 | for (int sanity = 300; sanity > 0; sanity--) { 460 | FILE* msgCountFile = fopen(msg_count_file_name, "r"); 461 | int count; 462 | if (msgCountFile == 0) { 463 | count = 0; 464 | } else { 465 | fread(msgCount, 1, 255, msgCountFile); 466 | fclose(msgCountFile); 467 | msgCount[255]='\0'; 468 | } 469 | count = strtol(msgCount, NULL, 10); 470 | if (count >= msg_sent) { 471 | return 0; 472 | } 473 | if (!block) { 474 | return 1; 475 | } 476 | athame_sleep(10, 0, 0); 477 | } 478 | athame_set_failure("Failed to sync with vim job"); 479 | return 1; 480 | } 481 | 482 | 483 | char fifo_buffer[DEFAULT_BUFFER_SIZE]; 484 | 485 | static int athame_setup_fifo() { 486 | int sanity = 40; 487 | while (fifo <= 0) { 488 | sanity--; 489 | if (sanity < 1) { 490 | return 1; 491 | } 492 | fifo = open(fifo_name, O_WRONLY | O_NONBLOCK); 493 | if (fifo < 0) { 494 | athame_sleep(25, 0, 0); 495 | } 496 | } 497 | return 0; 498 | } 499 | 500 | static int athame_remote_expr_v8(char* expr, int block) { 501 | int ret = check_expr_in_flight(block); 502 | 503 | if (ret) { 504 | return ret; 505 | } 506 | 507 | msg_sent++; 508 | 509 | if (athame_setup_fifo()) { 510 | athame_set_failure("Vim not reading from fifo"); 511 | return 1; 512 | } 513 | 514 | snprintf(fifo_buffer, DEFAULT_BUFFER_SIZE-1, "%d:%s\n", msg_sent, expr); 515 | write(fifo, fifo_buffer, strlen(fifo_buffer)); 516 | 517 | if (block) { 518 | check_expr_in_flight(1); 519 | } 520 | return 0; 521 | } 522 | 523 | static void athame_update_vimline(int row, int col) { 524 | FILE* updateFile = fopen(update_file_name, "w+"); 525 | 526 | fwrite(ap_get_line_buffer(), 1, ap_get_line_buffer_length(), updateFile); 527 | fwrite("\n", 1, 1, updateFile); 528 | 529 | fclose(updateFile); 530 | 531 | snprintf(athame_buffer, DEFAULT_BUFFER_SIZE - 1, 532 | "Vimbed_UpdateText(%d, %d, %d, %d, 0, '')", row + 1, col + 1, 533 | row + 1, col + 1); 534 | 535 | athame_remote_expr(athame_buffer, 1); 536 | updated = 1; 537 | } 538 | 539 | static long get_timeout_msec() { 540 | if (vim_sync >= VIM_SYNC_CHAR_BEHIND) { 541 | time_to_sync = 100 + get_time(); 542 | return 100; 543 | } 544 | if (time_to_poll == -1) { 545 | return -1; 546 | } 547 | return MAX(30, time_to_poll - get_time() + 5); 548 | } 549 | 550 | static void request_poll() { 551 | if (stale_polls > 2) { 552 | return; 553 | } 554 | long request_time = 555 | get_time() + (change_since_key || stale_polls > 0 ? 500 : 100); 556 | time_to_poll = 557 | time_to_poll < 0 ? request_time : MIN(time_to_poll, request_time); 558 | } 559 | 560 | static void athame_poll_vim(int block) { 561 | time_to_poll = -1; 562 | 563 | // Poll Vim. If we fail, postpone the poll by requesting a new poll 564 | if (athame_remote_expr("Vimbed_Poll()", block) == 0) { 565 | if (vim_sync == VIM_SYNC_WAITING_POLL_DONE) { 566 | vim_sync = VIM_SYNC_NEEDS_INFO_READ; 567 | } else if (vim_sync == VIM_SYNC_NEEDS_POLL) { 568 | if (block) { 569 | vim_sync = VIM_SYNC_NEEDS_INFO_READ; 570 | } else { 571 | vim_sync = VIM_SYNC_WAITING_POLL_DONE; 572 | } 573 | } 574 | stale_polls++; 575 | } else { 576 | request_poll(); 577 | } 578 | } 579 | 580 | int last_bdisplay_top = 0; 581 | int last_bdisplay_bottom = 0; 582 | int text_lines_for_bdisplay = 0; 583 | 584 | void athame_bottom_display(char* string, int style, int color, int cursor, 585 | int force) { 586 | int term_height, term_width; 587 | ap_get_term_size(&term_height, &term_width); 588 | 589 | int new_text_lines = 590 | (ap_get_line_char_length() + ap_get_prompt_length()) / term_width; 591 | 592 | if (!force && term_height == last_bdisplay_bottom) { 593 | int changed = 0; 594 | changed |= (strcmp(bottom_display, string)); 595 | if (string[0] != '\0') { 596 | changed |= (style != bottom_style); 597 | changed |= (color != bottom_color); 598 | changed |= (cursor != bottom_cursor); 599 | changed |= (new_text_lines != text_lines_for_bdisplay); 600 | } 601 | if (!changed) { 602 | return; 603 | } 604 | } 605 | text_lines_for_bdisplay = new_text_lines; 606 | 607 | strncpy(bottom_display, string, DEFAULT_BUFFER_SIZE - 1); 608 | bottom_display[DEFAULT_BUFFER_SIZE - 1] = '\0'; 609 | bottom_style = style; 610 | bottom_color = color; 611 | bottom_cursor = cursor; 612 | 613 | if (!last_bdisplay_bottom) { 614 | last_bdisplay_top = term_height; 615 | last_bdisplay_bottom = term_height; 616 | } 617 | 618 | int temp = ap_get_cursor(); 619 | if (!athame_dirty) { 620 | ap_set_cursor_end(); 621 | ap_display(); 622 | } 623 | 624 | int extra_lines = ((int)strlen(string)) / term_width; 625 | int i; 626 | for (i = 0; i < extra_lines; i++) { 627 | fprintf(athame_outstream, "\e[B"); 628 | } 629 | 630 | char colorstyle[64]; 631 | if (color) { 632 | sprintf(colorstyle, "\e[%d;%dm", style, color); 633 | } else { 634 | sprintf(colorstyle, "\e[%dm", style); 635 | } 636 | 637 | char erase[64]; 638 | if (term_height != last_bdisplay_bottom) { 639 | // We've been resized and have no idea where the last bottom display is. 640 | // Clear everything after the current text. 641 | sprintf(erase, "\e[J"); 642 | } else if (!athame_dirty && 643 | ap_get_line_char_length() + ap_get_prompt_length() >= term_width) { 644 | // Delete text in the way on my row and bottom display but leave everything 645 | // else alone 646 | sprintf(erase, "\e[K\e[%d;1H\e[J", last_bdisplay_top); 647 | } else { 648 | sprintf(erase, "\e[%d;1H\e[J", last_bdisplay_top); 649 | } 650 | 651 | char cursor_code[64]; 652 | if (cursor) { 653 | char cursor_char = string[MIN(cursor, strlen(string))]; 654 | if (!cursor_char) { 655 | cursor_char = ' '; 656 | } 657 | sprintf(cursor_code, "\e[%d;%dH\e[7m%c", 658 | term_height - extra_lines + cursor / term_width, 659 | cursor % term_width + 1, cursor_char); 660 | } else { 661 | cursor_code[0] = '\0'; 662 | } 663 | 664 | //\e7\n\e8\e[B\e[A Add a line underneath if at bottom 665 | //\e7 Save cursor position 666 | //%s(erase) Delete old athame_bottom_display 667 | //\e[%d;1H Go to position for new athame_bottom_display 668 | //%s(colorstyle)%s(string) Write bottom display using given color/style 669 | //%s Draw cursor for command mode 670 | //\e[0m Reset style 671 | //\e8 Return to saved position 672 | fprintf(athame_outstream, "\e7\n\e8\e[B\e[A\e7%s\e[%d;1H%s%s%s\e[0m\e8", 673 | erase, term_height - extra_lines, colorstyle, string, cursor_code); 674 | 675 | for (i = 0; i < extra_lines; i++) { 676 | fprintf(athame_outstream, "\e[A"); 677 | } 678 | 679 | last_bdisplay_bottom = term_height; 680 | last_bdisplay_top = term_height - extra_lines; 681 | 682 | fflush(athame_outstream); 683 | if (!athame_dirty) { 684 | ap_set_cursor(temp); 685 | ap_display(); 686 | } 687 | } 688 | 689 | static int athame_clear_dirty() { 690 | if (!athame_dirty) { 691 | return 0; 692 | } 693 | 694 | int count = athame_dirty; 695 | while (athame_dirty) { 696 | if (athame_dirty == count) { 697 | // Avoid deleting prompt 698 | fprintf(athame_outstream, "\e[%dG\e[K\n", ap_get_prompt_length() + 1); 699 | } else { 700 | fprintf(athame_outstream, "\e[2K\n"); 701 | } 702 | athame_dirty--; 703 | } 704 | while (count) { 705 | fprintf(athame_outstream, "\e[A"); 706 | count--; 707 | } 708 | fflush(athame_outstream); 709 | return 1; 710 | } 711 | 712 | static void athame_redisplay() { 713 | if (strcmp(athame_mode, "v") == 0 || strcmp(athame_mode, "V") == 0 || 714 | strcmp(athame_mode, "s") == 0 || strcmp(athame_mode, "c") == 0) { 715 | athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0); 716 | 717 | // Athame_highlight assumes the cursor is on the current terminal row. If we 718 | // came from normal readline display, that might not be the case 719 | if (!athame_clear_dirty()) { 720 | int temp = ap_get_cursor(); 721 | ap_set_cursor(0); 722 | ap_display(); 723 | ap_set_cursor(temp); 724 | } 725 | athame_highlight(ap_get_cursor(), end_col); 726 | } else { 727 | if (athame_clear_dirty()) { 728 | fprintf(athame_outstream, "\e[%dG", ap_get_prompt_length() + 1); 729 | fflush(athame_outstream); 730 | ap_display(); 731 | } else { 732 | ap_display(); 733 | } 734 | } 735 | athame_bottom_mode(); 736 | } 737 | 738 | static char* athame_copy_w_space(char* text) { 739 | int len = strlen(text); 740 | text[len] = ' '; 741 | char* ret = strndup(text, len + 1); 742 | text[len] = '\0'; 743 | return ret; 744 | } 745 | 746 | static int athame_draw_line_with_highlight(char* text, int start, int end) { 747 | int prompt_len = ap_get_prompt_length(); 748 | int term_width; 749 | ap_get_term_size(NULL, &term_width); 750 | // How much more than one line does the text take up (if it wraps) 751 | int extra_lines = ((int)strlen(text) + prompt_len - 1) / term_width; 752 | // How far down is the start of the highlight (if text wraps) 753 | int extra_lines_s = (start + prompt_len) / term_width; 754 | // How far down is the end of the highlight (if text wraps) 755 | int extra_lines_e = (end + prompt_len - 1) / term_width; 756 | 757 | char* with_space = athame_copy_w_space(text); 758 | char* highlighted = ap_get_substr(with_space, start, end); 759 | free(with_space); 760 | 761 | fprintf(athame_outstream, "\e[%dG%s", prompt_len + 1, text); 762 | if (extra_lines - extra_lines_s) { 763 | fprintf(athame_outstream, "\e[%dA", extra_lines - extra_lines_s); 764 | } 765 | 766 | fprintf(athame_outstream, "\e[%dG\e[7m%s\e[0m", 767 | (prompt_len + start) % term_width + 1, highlighted); 768 | 769 | free(highlighted); 770 | 771 | if (extra_lines - extra_lines_e) { 772 | fprintf(athame_outstream, "\e[%dB", extra_lines - extra_lines_e); 773 | } 774 | fprintf(athame_outstream, "\n"); 775 | return extra_lines + 1; 776 | } 777 | 778 | static void athame_highlight(int start, int end) { 779 | char* highlight_buffer = strdup(ap_get_line_buffer()); 780 | char* hi_loc = highlight_buffer; 781 | int cursor = ap_get_cursor(); 782 | ap_set_line_buffer(""); 783 | ap_display(); 784 | ap_set_line_buffer(highlight_buffer); 785 | ap_set_cursor(cursor); 786 | char* new_string = athame_tok(&hi_loc, '\n'); 787 | while (new_string) { 788 | char* next_string; 789 | int this_start; 790 | int this_end; 791 | next_string = athame_tok(&hi_loc, '\n'); 792 | 793 | if (new_string == highlight_buffer) { 794 | this_start = start; 795 | } else { 796 | this_start = 0; 797 | } 798 | if (next_string || end == 0 || 799 | (end <= start && new_string == highlight_buffer)) { 800 | this_end = strlen(new_string) + 1; 801 | } else { 802 | this_end = end; 803 | } 804 | athame_dirty += 805 | athame_draw_line_with_highlight(new_string, this_start, this_end); 806 | new_string = next_string; 807 | } 808 | free(highlight_buffer); 809 | if (athame_dirty) { 810 | fprintf(athame_outstream, "\e[%dA", athame_dirty); 811 | } 812 | fflush(athame_outstream); 813 | } 814 | 815 | static int athame_has_clean_quit() { 816 | FILE* sliceFile = fopen(slice_file_name, "r"); 817 | if (!sliceFile) { 818 | return 0; 819 | } 820 | int bytes_read = fread(athame_buffer, 1, 4, sliceFile); 821 | fclose(sliceFile); 822 | return strncmp(athame_buffer, "quit", bytes_read) == 0; 823 | } 824 | 825 | static char* athame_get_mode_text(char* mode) { 826 | switch (mode[0]) { 827 | case 'i': return "INSERT"; 828 | case 'n': return "NORMAL"; 829 | case 'v': return "VISUAL"; 830 | case 'V': return "VISUAL LINE"; 831 | case 's': return "SELECT"; 832 | case 'S': return "SELECT LINE"; 833 | case 'R': return "REPLACE"; 834 | case 'c': return "COMMAND"; 835 | default: return ""; 836 | } 837 | } 838 | 839 | static int athame_set_mode(char* mode) { 840 | if (strcmp(athame_mode, mode) != 0) { 841 | char* mode_string = athame_get_mode_text(mode); 842 | strcpy(athame_mode, mode); 843 | setenv("ATHAME_VIM_MODE", mode_string, 1); 844 | ap_redraw_prompt(); 845 | return 1; 846 | } 847 | return 0; 848 | } 849 | 850 | static void athame_bottom_mode() { 851 | if (athame_failure) { 852 | return; 853 | } 854 | char* mode_string = athame_get_mode_text(athame_mode); 855 | { 856 | if (athame_mode[0] == 'c' && athame_is_set("ATHAME_SHOW_COMMAND", 1)) { 857 | athame_bottom_display(athame_command, ATHAME_NORMAL, ATHAME_DEFAULT, 858 | command_cursor, 0); 859 | } else if (athame_is_set("ATHAME_SHOW_MODE", 1)) { 860 | if (athame_mode[0] == 'n') { 861 | athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0); 862 | } else if (mode_string[0]) { 863 | sprintf(athame_buffer, "-- %s --", mode_string); 864 | athame_bottom_display(athame_buffer, ATHAME_BOLD, ATHAME_DEFAULT, 0, 0); 865 | } 866 | } 867 | } 868 | } 869 | 870 | static void athame_draw_failure() { 871 | setenv("ATHAME_ERROR", athame_failure, 1); 872 | if (athame_is_set("ATHAME_SHOW_ERROR", 1)) { 873 | snprintf(athame_buffer, DEFAULT_BUFFER_SIZE - 1, "Athame Failure: %s", 874 | athame_failure); 875 | athame_bottom_display(athame_buffer, ATHAME_BOLD, ATHAME_RED, 0, 0); 876 | } 877 | } 878 | 879 | static void athame_set_failure(char* fail_str) { 880 | if (athame_failure == 0) { 881 | kill(vim_pid, SIGTERM); 882 | } 883 | athame_failure = strdup(fail_str); 884 | athame_draw_failure(); 885 | } 886 | 887 | static char athame_process_input(int instream) { 888 | char result; 889 | int chars_read = read(instream, &result, 1); 890 | if (chars_read == 1) { 891 | return athame_process_char(result); 892 | } else { 893 | return EOF; 894 | } 895 | } 896 | 897 | // Should we send the char to readline? 898 | static int athame_is_special_char(char char_read) { 899 | if (athame_failure) { 900 | return 1; 901 | } 902 | if (strchr(ap_special, char_read)) { 903 | // Make sure our mode is up to date. 904 | athame_force_vim_sync(); 905 | // Most specials trigger only on inserty modes. 906 | if (strchr("iR", athame_mode[0])) { 907 | return 1; 908 | } 909 | // Newlines trigger on all non command modes. 910 | if (strchr(ap_nl, char_read)) { 911 | if (athame_mode[0] != 'c') { 912 | return 1; 913 | } 914 | } 915 | } 916 | ap_set_nospecial(); 917 | return 0; 918 | } 919 | 920 | static char athame_process_char(char char_read) { 921 | if (athame_is_special_char(char_read)) { 922 | return char_read; 923 | } else { 924 | sent_to_vim = 1; 925 | stale_polls = 0; 926 | change_since_key = 0; 927 | 928 | // Backspace 929 | if (char_read == '\177') { 930 | char_read = '\b'; 931 | } 932 | athame_send_to_vim(char_read); 933 | keys_since_change++; 934 | return 0; 935 | } 936 | } 937 | 938 | static void athame_send_to_vim(char input) { 939 | if (vim_sync < VIM_SYNC_CHAR_BEHIND) { 940 | vim_sync = VIM_SYNC_CHAR_BEHIND; 941 | } else { 942 | vim_sync = VIM_SYNC_NO; 943 | } 944 | write(vim_term, &input, 1); 945 | } 946 | 947 | static int athame_get_vim_info() { 948 | if (vim_sync == VIM_SYNC_NEEDS_INFO_READ) { 949 | vim_sync = VIM_SYNC_YES; 950 | } 951 | if (athame_get_vim_info_inner()) { 952 | time_to_poll = -1; 953 | stale_polls = 0; 954 | change_since_key = 1; 955 | keys_since_change = 0; 956 | athame_redisplay(); 957 | return 1; 958 | } 959 | return 0; 960 | } 961 | 962 | static int athame_get_col_row(char* string, int* col, int* row) { 963 | if (string && athame_tok(&string, ',')) { 964 | char* colStr = athame_tok(&string, ','); 965 | if (!colStr) { 966 | return 0; 967 | } 968 | *col = strtol(colStr, NULL, 10); 969 | if (row) { 970 | char* rowStr = athame_tok(&string, ','); 971 | if (!rowStr) { 972 | return 0; 973 | } 974 | *row = strtol(rowStr, NULL, 10); 975 | } 976 | return 1; 977 | } 978 | return 0; 979 | } 980 | 981 | static int athame_get_vim_info_inner() { 982 | int changed = 0; 983 | ssize_t bytes_read; 984 | if (athame_failure) { 985 | return 1; 986 | } 987 | 988 | FILE* sliceFile = fopen(slice_file_name, "r"); 989 | if (!sliceFile) { 990 | return 0; 991 | } 992 | bytes_read = fread(athame_buffer, 1, DEFAULT_BUFFER_SIZE - 1, sliceFile); 993 | fclose(sliceFile); 994 | 995 | athame_buffer[bytes_read] = 0; 996 | char* buffer_loc = athame_buffer; 997 | char* mode = athame_tok(&buffer_loc, '\n'); 998 | if (!mode || *mode==0) { 999 | return changed; 1000 | } 1001 | 1002 | if (mode[0] == 'c') { 1003 | changed |= athame_set_mode("c"); 1004 | char* command = athame_tok(&buffer_loc, '\n'); 1005 | int cmd_pos = 0; 1006 | if (mode[1] == ',') { 1007 | cmd_pos = strtol(mode + 2, NULL, 10); 1008 | } 1009 | if (command) { 1010 | if (strcmp(athame_command, command) != 0 || command_cursor != cmd_pos) { 1011 | setenv("ATHAME_VIM_COMMAND", command, 1); 1012 | strcpy(athame_command, command); 1013 | command_cursor = cmd_pos; 1014 | changed = 1; 1015 | } 1016 | } 1017 | } else { 1018 | if (athame_mode[0] == 'c' && athame_is_set("ATHAME_SHOW_COMMAND", 1)) { 1019 | athame_bottom_display("", ATHAME_NORMAL, ATHAME_DEFAULT, 0, 0); 1020 | } 1021 | if (strcmp(mode, "quit") == 0) { 1022 | // Don't do work if we're quitting. 1023 | return 0; 1024 | } 1025 | changed |= athame_set_mode(mode); 1026 | } 1027 | char* location = athame_tok(&buffer_loc, '\n'); 1028 | if (!location) { 1029 | return changed; 1030 | } 1031 | char* location2 = athame_tok(&buffer_loc, '\n'); 1032 | 1033 | int col = -1; 1034 | int row = -1; 1035 | if (athame_get_col_row(location, &col, &row)) { 1036 | if (col < 0 || row < 0) { 1037 | col = 0; 1038 | location2 = NULL; 1039 | } 1040 | 1041 | int new_end_col; 1042 | int new_end_row; 1043 | if (athame_get_col_row(location2, &new_end_col, &new_end_row)) { 1044 | if (new_end_col != end_col || new_end_row != end_row) { 1045 | end_col = new_end_col; 1046 | end_row = new_end_row; 1047 | changed = 1; 1048 | } 1049 | } else { 1050 | end_col = col + 1; 1051 | end_row = row; 1052 | } 1053 | 1054 | if (athame_row != row) { 1055 | athame_row = row; 1056 | changed = 1; 1057 | } 1058 | } 1059 | 1060 | if (!buffer_loc) { 1061 | ap_set_line_buffer(""); 1062 | return changed; 1063 | } 1064 | 1065 | if (strcmp(ap_get_line_buffer(), buffer_loc) != 0) { 1066 | changed = 1; 1067 | ap_set_line_buffer(buffer_loc); 1068 | } 1069 | if (col > -1 && ap_get_cursor() != col) { 1070 | ap_set_cursor(col); 1071 | changed = 1; 1072 | } 1073 | 1074 | return changed; 1075 | } 1076 | 1077 | static int athame_select(int file_desc1, int file_desc2, int timeout_sec, 1078 | int timeout_ms, int no_signals) { 1079 | fd_set files; 1080 | FD_ZERO(&files); 1081 | if (file_desc1 > -1) { 1082 | FD_SET(file_desc1, &files); 1083 | } 1084 | if (file_desc2 > -1) { 1085 | FD_SET(file_desc2, &files); 1086 | } 1087 | sigset_t block_signals; 1088 | int use_timeout = timeout_sec >= 0 && timeout_ms >= 0; 1089 | int results; 1090 | if (no_signals) { 1091 | struct timespec timeout; 1092 | timeout.tv_sec = timeout_sec; 1093 | timeout.tv_nsec = timeout_ms * 1000000; 1094 | sigfillset(&block_signals); 1095 | results = pselect(MAX(file_desc1, file_desc2) + 1, &files, NULL, NULL, 1096 | use_timeout ? &timeout : NULL, &block_signals); 1097 | } else { 1098 | struct timeval timeout; 1099 | timeout.tv_sec = timeout_sec; 1100 | timeout.tv_usec = timeout_ms * 1000; 1101 | results = select(MAX(file_desc1, file_desc2) + 1, &files, NULL, NULL, 1102 | use_timeout ? &timeout : NULL); 1103 | } 1104 | if (results > 0) { 1105 | if (file_desc1 >= 0 && FD_ISSET(file_desc1, &files)) { 1106 | return 1; 1107 | } 1108 | if (file_desc2 >= 0 && FD_ISSET(file_desc2, &files)) { 1109 | return 2; 1110 | } 1111 | } 1112 | return results; 1113 | } 1114 | 1115 | int athame_is_set(char* env, int def) { 1116 | char* env_val = getenv(env); 1117 | if (!env_val) { 1118 | setenv(env, def ? "1" : "0", 0); 1119 | return def; 1120 | } else { 1121 | return env_val[0] == '1'; 1122 | } 1123 | } 1124 | 1125 | // Unlike normal strtok, this 1126 | // - moves pointer to point after delim 1127 | // - gives the empty strings between consecutive tokens. 1128 | // - has no external state 1129 | static char* athame_tok(char** pointer, char delim) { 1130 | if (*pointer == 0) { 1131 | return 0; 1132 | } 1133 | char* original = *pointer; 1134 | char* loc = strchr(original, delim); 1135 | if (loc == 0) { 1136 | *pointer = 0; 1137 | } else { 1138 | *loc = '\0'; 1139 | *pointer = loc + 1; 1140 | } 1141 | return original; 1142 | } 1143 | 1144 | static long get_time() { 1145 | // We need to check __MACH__ too because OSX claims it has this functionality 1146 | // but doesn't. 1147 | #if _POSIX_MONOTONIC_CLOCK && !__MACH__ 1148 | struct timespec t; 1149 | #if CLOCK_MONOTONIC_COARSE 1150 | clock_gettime(CLOCK_MONOTONIC_COARSE, &t); 1151 | #else 1152 | clock_gettime(CLOCK_MONOTONIC, &t); 1153 | #endif 1154 | return t.tv_sec * 1000L + t.tv_nsec / 1000 / 1000; 1155 | #else 1156 | struct timeval t; 1157 | gettimeofday(&t, NULL); 1158 | return t.tv_sec * 1000L + t.tv_usec / 1000; 1159 | #endif 1160 | } 1161 | 1162 | static void wait_then_kill(int pid) { 1163 | int i; 1164 | for (i = 0; i < 10; i++) { 1165 | if (waitpid(pid, NULL, WNOHANG) != 0) { 1166 | break; 1167 | } 1168 | athame_sleep(5, 0, 0); 1169 | } 1170 | if (waitpid(pid, NULL, WNOHANG) == 0) { 1171 | kill(pid, SIGKILL); 1172 | waitpid(pid, NULL, 0); 1173 | } 1174 | } 1175 | 1176 | static void athame_force_vim_sync() { 1177 | if (vim_sync == VIM_SYNC_YES) { 1178 | return; 1179 | } 1180 | if (vim_sync == VIM_SYNC_CHAR_BEHIND) { 1181 | athame_sleep(20, 0, 0); 1182 | vim_sync = VIM_SYNC_NEEDS_POLL; 1183 | } else if (vim_sync == VIM_SYNC_NO) { 1184 | if (athame_select(vim_term, -1, 0, MAX(1, time_to_sync - get_time()), 1) != 1185 | 0) { 1186 | int sanity = 100; 1187 | while (athame_select(vim_term, -1, 0, 100, 1) != 0 && sanity-- > 0) { 1188 | athame_sleep(5, 0, 0); 1189 | read(vim_term, athame_buffer, DEFAULT_BUFFER_SIZE - 1); 1190 | } 1191 | } 1192 | vim_sync = VIM_SYNC_NEEDS_POLL; 1193 | } 1194 | if (vim_sync == VIM_SYNC_WAITING_POLL_DONE) { 1195 | if (athame_is_set("ATHAME_USE_JOBS", ATHAME_USE_JOBS_DEFAULT)){ 1196 | check_expr_in_flight(1); 1197 | } else { 1198 | waitpid(expr_pid, NULL, 0); 1199 | } 1200 | vim_sync = VIM_SYNC_NEEDS_INFO_READ; 1201 | } 1202 | if (vim_sync >= VIM_SYNC_NEEDS_POLL) { 1203 | athame_poll_vim(1); 1204 | } 1205 | athame_get_vim_info(); 1206 | } 1207 | 1208 | static int is_vim_alive() { 1209 | return vim_pid && waitpid(vim_pid, NULL, WNOHANG) == 0; 1210 | } 1211 | 1212 | static char* temp_dir_loc() { 1213 | #ifdef BSD 1214 | return "TMPDIR"; 1215 | #else 1216 | return "XDG_RUNTIME_DIR"; 1217 | #endif 1218 | } 1219 | 1220 | static volatile int athame_sigint = 0; 1221 | sighandler_t old_sigint = 0; 1222 | 1223 | static void sigint_handler(int signum) { 1224 | signal(SIGINT, old_sigint); 1225 | athame_sigint = 1; 1226 | } 1227 | 1228 | static void steal_signal_handler() { 1229 | athame_sigint = 0; 1230 | old_sigint = signal(SIGINT, sigint_handler); 1231 | } 1232 | 1233 | static void return_signal_handler() { 1234 | signal(SIGINT, old_sigint); 1235 | athame_sigint = 0; 1236 | } 1237 | 1238 | // Processes signals and returns either a valid char or -1. 1239 | static int process_signals() { 1240 | if(ap_is_catching_signals()) { 1241 | int sig_result; 1242 | if (sig_result = (int)ap_handle_signals()) { 1243 | return sig_result; 1244 | } 1245 | return ATHAME_CONTINUE; 1246 | } 1247 | 1248 | if(athame_sigint) { 1249 | // Python shell has a race where it only handles signint correctly if 1250 | // it's triggered during a specific select in python code. We start a 1251 | // new process to send the signal so that python can reach the select. 1252 | // This is far from ideal and still racy in theory, but seems to work 1253 | // in practice. 1254 | int athame_pid = getpid(); 1255 | int pid = fork(); 1256 | if (pid == 0) { 1257 | // Child process 1258 | kill(athame_pid, SIGINT); 1259 | exit(0); 1260 | } 1261 | return 0; 1262 | } 1263 | return ATHAME_CONTINUE; 1264 | } 1265 | -------------------------------------------------------------------------------- /athame_zsh.h: -------------------------------------------------------------------------------- 1 | /* athame_zsh.h -- Full vim integration for your shell.*/ 2 | 3 | /* Copyright (C) 2015 James Kolb 4 | 5 | This file is part of Athame. 6 | 7 | Athame 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 | Athame 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 Athame. If not, see . 19 | 20 | This file incorporates work under the following copyright and permission 21 | notice: 22 | The Z Shell is copyright (c) 1992-2009 Paul Falstad, Richard Coleman, 23 | Zoltán Hidvégi, Andrew Main, Peter Stephenson, Sven Wischnowsky, and 24 | others. All rights reserved. Individual authors, whether or not 25 | specifically named, retain copyright in all changes; in what follows, they 26 | are referred to as `the Zsh Development Group'. This is for convenience 27 | only and this body has no legal status. The Z shell is distributed under 28 | the following licence; any provisions made in individual files take 29 | precedence. 30 | 31 | Permission is hereby granted, without written agreement and without 32 | licence or royalty fees, to use, copy, modify, and distribute this 33 | software and to distribute modified versions of this software for any 34 | purpose, provided that the above copyright notice and the following 35 | two paragraphs appear in all copies of this software. 36 | 37 | In no event shall the Zsh Development Group be liable to any party for 38 | direct, indirect, special, incidental, or consequential damages arising out 39 | of the use of this software and its documentation, even if the Zsh 40 | Development Group have been advised of the possibility of such damage. 41 | 42 | The Zsh Development Group specifically disclaim any warranties, including, 43 | but not limited to, the implied warranties of merchantability and fitness 44 | for a particular purpose. The software provided hereunder is on an "as is" 45 | basis, and the Zsh Development Group have no obligation to provide 46 | maintenance, support, updates, enhancements, or modifications. 47 | 48 | */ 49 | #include "zle.mdh" 50 | 51 | // Note: Most of this code is from zle_params.c 52 | 53 | static char* unmetafy_athame(char* text) { 54 | int length; 55 | char* unmeta = unmetafy(text, &length); 56 | for (int i = 0; i < length; i++) { 57 | if (unmeta[i] == NULL) { 58 | // Display nulls as spaces. This isn't perfect, but this is mostly 59 | // just to avoid memory errors. 60 | unmeta[i] = ' '; 61 | } 62 | } 63 | return unmeta; 64 | } 65 | 66 | static char* ap_get_line_buffer() { 67 | if (zlemetaline != 0) { 68 | return unmetafy_athame(dupstring(zlemetaline)); 69 | } 70 | return unmetafy_athame(zlelineasstring(zleline, zlell, 0, NULL, NULL, 1)); 71 | } 72 | 73 | static int ap_get_line_buffer_length() { return strlen(ap_get_line_buffer()); } 74 | 75 | static int ap_get_line_char_length() { return zlell; } 76 | 77 | static void ap_set_line_buffer(char* newText) { setline(newText, ZSL_COPY); } 78 | 79 | static int ap_get_cursor() { 80 | if (zlemetaline != NULL) { 81 | /* A lot of work for one number, but still... */ 82 | ZLE_STRING_T tmpline; 83 | int tmpcs, tmpll, tmpsz; 84 | char* tmpmetaline = ztrdup(zlemetaline); 85 | tmpline = stringaszleline(tmpmetaline, zlemetacs, &tmpll, &tmpsz, &tmpcs); 86 | free(tmpmetaline); 87 | free(tmpline); 88 | return tmpcs; 89 | } 90 | return zlecs; 91 | } 92 | 93 | static void ap_set_cursor(int x) { 94 | if (x < 0) { 95 | zlecs = 0; 96 | } else if (x > zlell) { 97 | zlecs = zlell; 98 | } else { 99 | zlecs = x; 100 | } 101 | } 102 | 103 | static void ap_set_cursor_end() { zlecs = zlell; } 104 | 105 | /* Temporarily disable vim (used by zsh for completion).*/ 106 | static int ap_temp_novim() { 107 | return menucmp != 0 || suffixlen > 0 || showinglist != 0; 108 | } 109 | 110 | static void ap_redraw_prompt() { resetprompt(NULL); } 111 | 112 | static void ap_display() { zrefresh(); } 113 | 114 | static void ap_get_term_size(int* height, int* width) { 115 | adjustwinsize(1); 116 | if (width) { 117 | *width = zterm_columns; 118 | } 119 | if (height) { 120 | *height = zterm_lines; 121 | } 122 | } 123 | 124 | static int ap_get_prompt_length() { 125 | int length; 126 | countprompt(lpromptbuf, &length, NULL, -1); 127 | return length; 128 | } 129 | 130 | Histent he; 131 | static void ap_get_history_start() { he = hist_ring->down; } 132 | 133 | #define GETZLETEXT(ent) ((ent)->zle_text ? (ent)->zle_text : (ent)->node.nam) 134 | static char* ap_get_history_next() { 135 | if (he == hist_ring) { 136 | return 0; 137 | } 138 | char* ret = GETZLETEXT(he); 139 | he = down_histent(he); 140 | return unmetafy_athame(dupstring(ret)); 141 | } 142 | 143 | static void ap_get_history_end() {} 144 | 145 | static int ap_needs_to_leave() { return 0; } 146 | 147 | static char* ap_get_substr(char* text, int start, int end) { 148 | int mbchars; 149 | int pos_s = 0; 150 | int pos = 0; 151 | for (mbchars = 0; mbchars < end; mbchars++) { 152 | if (mbchars == start) { 153 | pos_s = pos; 154 | } 155 | int l = mblen(text + pos, MB_CUR_MAX); 156 | if (l >= 0) { 157 | pos += l; 158 | } else { 159 | if (mbchars < start) { 160 | return strdup(""); 161 | } 162 | break; 163 | } 164 | } 165 | return strndup(text + pos_s, pos - pos_s); 166 | } 167 | 168 | static char ap_handle_signals() { 169 | int q = queue_signal_level(); 170 | 171 | // Calling dont_queue_signals() here would make zsh process all queued signals. 172 | // This is a modified version of dont_queue_signals() that calls athame_cleanup 173 | // before processing any SIGHUP. The SIGHUP would cause zsh to close and we need 174 | // to make sure we cleanup athame first. 175 | queueing_enabled = 0; 176 | while (queue_front != queue_rear) { /* while signals in queue */ 177 | sigset_t oset; 178 | queue_front = (queue_front + 1) % MAX_QUEUE_SIZE; 179 | oset = signal_setmask(signal_mask_queue[queue_front]); 180 | if (signal_queue[queue_front] == SIGHUP) { 181 | athame_cleanup(1); 182 | } 183 | zhandler(signal_queue[queue_front]); /* handle queued signal */ 184 | signal_setmask(oset); 185 | } 186 | 187 | restore_queue_signals(q); 188 | 189 | if (errflag & ERRFLAG_INT) { 190 | return EOF; 191 | } 192 | return 0; 193 | } 194 | 195 | static char* ap_nl = "\r\n"; 196 | static char* ap_special = "\t\x04\r\n\x0c"; 197 | static char ap_delete = '\x04'; 198 | 199 | static void ap_set_control_chars() { 200 | // TODO: Lookup zsh control chars instead of assuming defaults. 201 | } 202 | 203 | static void ap_set_nospecial() { 204 | // We don't care about this in zsh. 205 | } 206 | 207 | static int ap_is_catching_signals() { 208 | return 1; 209 | } 210 | -------------------------------------------------------------------------------- /athamerc: -------------------------------------------------------------------------------- 1 | " WARNING: By default /etc/athamerc will get rewritten on updates. You should 2 | " create a ~/.athamerc that sources this file and make changes there. 3 | " 4 | " /etc/athamerc is only read if no ~/.athamerc is found 5 | " 6 | " If you don't want /etc/athamerc overidden, supply the --norc flag to the setup 7 | " script. 8 | 9 | set nocompatible 10 | 11 | set ttimeoutlen=10 "Otherwise, you have to wait for the escape key. 12 | 13 | set backspace-=eol "For more traditional shell behavior 14 | set backspace+=start "Without this, you can't delete shell completions 15 | 16 | set textwidth=0 "Don't try to wrap text 17 | 18 | "Start each line in insert mode. Most shell vi-modes do this: 19 | autocmd User Vimbed_StartLine call feedkeys("\\i","n") 20 | 21 | " These make arrows more shell like. Feel free to comment them out: 22 | if v:version>=800 || has("patch928") 23 | " These maps can segfault in earlier vim versions. 24 | inoremap A 25 | inoremap A 26 | endif 27 | 28 | " Uncomment these maps to have the up and down arrow 29 | " only match lines that share text before cursor 30 | " (Similar to how arrows work for vim ex expressions) 31 | " 32 | "inoremap :silent call HistorySearchBackward() 33 | "inoremap :silent call HistorySearchForward() 34 | "nnoremap :silent call HistorySearchBackward() 35 | "nnoremap :silent call HistorySearchForward() 36 | 37 | "Similar to bash's history-search-backward. 38 | function! HistorySearchBackward() 39 | let curcol = col('.') 40 | if curcol > 1 41 | let searchText = getline('.')[0:curcol - 2] 42 | for line in range(line('.') - 1, 1, -1) 43 | if getline(line)[0:curcol - 2] == searchText 44 | call cursor(line, curcol) 45 | break 46 | endif 47 | endfor 48 | elseif line('.') > 1 49 | call cursor(line('.') - 1, 1) 50 | endif 51 | endfunction 52 | 53 | "Similar to bash's history-search-forward. 54 | function! HistorySearchForward() 55 | let curcol = col('.') 56 | if curcol > 1 57 | let searchText = getline('.')[0:curcol - 2] 58 | for line in range(line('.') + 1, line('$')) 59 | if getline(line)[0:curcol - 2] == searchText 60 | call cursor(line, curcol) 61 | break 62 | endif 63 | endfor 64 | else 65 | call cursor(line('.') + 1, 1) 66 | endif 67 | endfunction 68 | -------------------------------------------------------------------------------- /bash_readline_setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # bash_readline_setup.sh -- Full vim integration for your shell. 3 | # 4 | # Copyright (C) 2015 James Kolb 5 | # 6 | # This file is part of Athame. 7 | # 8 | # Athame 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 | # Athame 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 Athame. If not, see . 20 | 21 | patches=19 22 | redownload=0 23 | build=1 24 | dirty=0 25 | sudo="" 26 | destdir="" 27 | prefix="/" 28 | use_readline="" 29 | for arg in "$@" 30 | do 31 | case "$arg" in 32 | "--redownload" ) redownload=1;; 33 | "--nobuild" ) build=0;; 34 | "--dirty" ) dirty=1;; 35 | "--use_sudo" ) sudo="sudo ";; 36 | --destdir=*) destdir="${arg#*=}";; 37 | --prefix=*) prefix="${arg#*=}";; 38 | --use_readline=*) use_readline="${arg#*=}";; 39 | "--help" ) echo -e " --redownload: redownload bash and patches\n" \ 40 | "--nobuild: stop before actually building src\n" \ 41 | "--prefix: set prefix for configure\n"\ 42 | "--use_readline: set where to look for readline\n"\ 43 | "--destdir: set DESTDIR for install\n"\ 44 | "--dirty: don't run the whole build process,\n" \ 45 | " just make and install changes\n" \ 46 | " (only use after a successful build)\n" \ 47 | "--help: display this message"; exit;; 48 | * ) echo Unknown flag "$arg" >&2; exit 1;; 49 | esac 50 | done 51 | 52 | #Download bash 53 | if [ $redownload = 1 ]; then 54 | rm -r bash-4.4.tar.gz 55 | fi 56 | if [ ! -f bash-4.4.tar.gz ]; then 57 | curl -O https://ftp.gnu.org/gnu/bash/bash-4.4.tar.gz 58 | fi 59 | 60 | mkdir -p bash_patches 61 | cd bash_patches 62 | for (( patch=1; patch <= patches; patch++ )); do 63 | if [ $redownload = 1 ]; then 64 | rm -r bash44-$(printf "%03d" $patch) 65 | fi 66 | if [ ! -f bash44-$(printf "%03d" $patch) ]; then 67 | curl -O https://ftp.gnu.org/gnu/bash/bash-4.4-patches/bash44-$(printf "%03d" $patch) 68 | fi 69 | done 70 | cd .. 71 | 72 | if [ ! -d bash-4.4_tmp ]; then 73 | dirty=0 74 | fi 75 | 76 | #Unpack bash dir 77 | if [ $dirty = 0 ]; then 78 | rm -rf bash-4.4_tmp 79 | tar -xf bash-4.4.tar.gz 80 | mv bash-4.4 bash-4.4_tmp 81 | fi 82 | 83 | #Move into bash directory 84 | cd bash-4.4_tmp 85 | 86 | if [ $dirty = 0 ]; then 87 | #Patch bash with bash patches 88 | for (( patch=1; patch <= patches; patch++ )); do 89 | echo Patching with standard bash patch $patch 90 | patch -p0 < ../bash_patches/bash44-$(printf "%03d" $patch) 91 | done 92 | fi 93 | 94 | readline_configure_flag="--with-installed-readline" 95 | readline_make_flag="" 96 | if [ -n "$use_readline" ]; then 97 | readline_configure_flag="--with-installed-readline=$use_readline" 98 | readline_make_flag="READLINE_LDFLAGS=-Wl,-rpath,$use_readline/lib/" 99 | fi 100 | 101 | #Build and install bash 102 | if [ $build = 1 ]; then 103 | if [ ! -f Makefile ]; then 104 | ac_cv_rl_version=8.0 ./configure \ 105 | "--prefix=$prefix" \ 106 | --docdir=${prefix}/usr/share/doc/bash-4.4 \ 107 | --without-bash-malloc \ 108 | --enable-readline \ 109 | "$readline_configure_flag" \ 110 | || exit 1 111 | fi 112 | if [ -n "$use_readline" ]; then 113 | make LOCAL_LIBS=-lutil "$readline_make_flag" 114 | else 115 | make LOCAL_LIBS=-lutil 116 | fi 117 | if [ $? != 0 ]; then 118 | printf "\n\e[1;31mMake failed:\e[0m Are you sure you have readline 8 installed? readline_athame_setup.sh installs readline 8 patched with athame. You may want to run it first.\nThis may also fail if you have a versionless libreadline.so symlinked to libreadline.so.7\n" 119 | exit 1 120 | fi 121 | if [ -n "$destdir" ]; then 122 | mkdir -p "$destdir" 123 | fi 124 | ${sudo}make install DESTDIR="$destdir" 125 | fi 126 | 127 | #Leave bash dir 128 | cd .. 129 | -------------------------------------------------------------------------------- /readline.patch: -------------------------------------------------------------------------------- 1 | diff -ru a/display.c b/display.c 2 | --- a/display.c 2015-10-28 00:00:00.000000000 - 0400 3 | --- b/display.c 2015-10-28 00:00:00.000000000 - 0400 4 | @@ -255,6 +255,11 @@ 5 | static int prompt_last_screen_line; 6 | 7 | static int prompt_physical_chars; 8 | + 9 | +int 10 | +rla_prompt_phys_length() { 11 | + return prompt_physical_chars; 12 | +} 13 | 14 | /* An array of indexes into the prompt string where we will break physical 15 | screen lines. It's easier to compute in expand_prompt and use later in 16 | diff -ru a/Makefile.in b/Makefile.in 17 | --- a/Makefile.in 2015-07-15 00:03:32.894897704 -0400 18 | +++ b/Makefile.in 2015-07-14 23:36:13.208157503 -0400 19 | @@ -75,7 +75,7 @@ 20 | CTAGS = ctags -tw 21 | 22 | CFLAGS = @CFLAGS@ 23 | -LOCAL_CFLAGS = @LOCAL_CFLAGS@ -DRL_LIBRARY_VERSION='"$(RL_LIBRARY_VERSION)"' 24 | +LOCAL_CFLAGS = @LOCAL_CFLAGS@ -DRL_LIBRARY_VERSION='"$(RL_LIBRARY_VERSION)"' -DVIMBED_LOCATION='"${libdir}/athame_readline"' -DATHAME_VIM_BIN='"${ATHAME_VIM_BIN}"' -DATHAME_USE_JOBS_DEFAULT='${ATHAME_USE_JOBS_DEFAULT}' 25 | CPPFLAGS = @CPPFLAGS@ 26 | 27 | DEFS = @DEFS@ @CROSS_COMPILE@ 28 | @@ -120,7 +120,7 @@ 29 | $(srcdir)/histfile.c $(srcdir)/nls.c $(srcdir)/search.c \ 30 | $(srcdir)/shell.c $(srcdir)/savestring.c $(srcdir)/tilde.c \ 31 | $(srcdir)/text.c $(srcdir)/misc.c $(srcdir)/compat.c \ 32 | - $(srcdir)/mbutil.c 33 | + $(srcdir)/mbutil.c $(srcdir)/athame.c 34 | 35 | # The header files for this library. 36 | HSOURCES = $(srcdir)/readline.h $(srcdir)/rldefs.h $(srcdir)/chardefs.h \ 37 | @@ -130,7 +130,7 @@ 38 | $(srcdir)/ansi_stdlib.h $(srcdir)/tcap.h $(srcdir)/rlstdc.h \ 39 | $(srcdir)/xmalloc.h $(srcdir)/rlprivate.h $(srcdir)/rlshell.h \ 40 | $(srcdir)/rltypedefs.h $(srcdir)/rlmbutil.h \ 41 | - $(srcdir)/colors.h $(srcdir)/parse-colors.h 42 | + $(srcdir)/colors.h $(srcdir)/parse-colors.h $(srcdir)/athame.h 43 | 44 | HISTOBJ = history.o histexpand.o histfile.o histsearch.o shell.o mbutil.o 45 | TILDEOBJ = tilde.o 46 | @@ -153,7 +153,7 @@ 47 | CREATED_TAGS = TAGS tags 48 | 49 | INSTALLED_HEADERS = readline.h chardefs.h keymaps.h history.h tilde.h \ 50 | - rlstdc.h rlconf.h rltypedefs.h 51 | + rlstdc.h rlconf.h rltypedefs.h athame.h 52 | 53 | OTHER_DOCS = $(srcdir)/CHANGES $(srcdir)/INSTALL $(srcdir)/README 54 | OTHER_INSTALLED_DOCS = CHANGES INSTALL README 55 | @@ -230,6 +230,13 @@ 56 | 57 | install: $(INSTALL_TARGETS) 58 | 59 | +install-vimbed: 60 | + $(RM) $(DESTDIR)$(libdir)/athame_readline/vimbed.vim && $(RM) -r $(DESTDIR)$(libdir)/athame_readline 61 | + $(CP) -r $(srcdir)/vimbed/plugin $(DESTDIR)$(libdir)/athame_readline 62 | + 63 | +uninstall-vimbed: 64 | + $(RM) -r $(DESTDIR)$(libdir)/athame_readline 65 | + 66 | install-headers: installdirs ${INSTALLED_HEADERS} 67 | for f in ${INSTALLED_HEADERS}; do \ 68 | $(INSTALL_DATA) $(srcdir)/$$f $(DESTDIR)$(includedir)/readline ; \ 69 | @@ -250,7 +257,7 @@ 70 | 71 | maybe-uninstall-pc: uninstall-pc 72 | 73 | -install-static: installdirs $(STATIC_LIBS) install-headers install-doc ${install_examples} install-pc 74 | +install-static: installdirs $(STATIC_LIBS) install-headers install-doc ${install_examples} install-pc install-vimbed 75 | -$(MV) $(DESTDIR)$(libdir)/libreadline.a $(DESTDIR)$(libdir)/libreadline.old 76 | $(INSTALL_DATA) libreadline.a $(DESTDIR)$(libdir)/libreadline.a 77 | -test -n "$(RANLIB)" && $(RANLIB) $(DESTDIR)$(libdir)/libreadline.a 78 | @@ -264,7 +281,7 @@ 79 | $(DESTDIR)$(infodir) $(DESTDIR)$(man3dir) $(DESTDIR)$(docdir) \ 80 | $(DESTDIR)$(pkgconfigdir) 81 | 82 | -uninstall: uninstall-headers uninstall-doc uninstall-examples uninstall-pc 83 | +uninstall: uninstall-headers uninstall-doc uninstall-examples uninstall-pc uninstall-vimbed 84 | -test -n "$(DESTDIR)$(libdir)" && cd $(DESTDIR)$(libdir) && \ 85 | ${RM} libreadline.a libreadline.old libhistory.a libhistory.old $(SHARED_LIBS) 86 | -( cd shlib; ${MAKE} ${MFLAGS} DESTDIR=${DESTDIR} uninstall ) 87 | @@ -412,7 +419,7 @@ 88 | readline.o: readline.h keymaps.h rltypedefs.h chardefs.h tilde.h 89 | readline.o: rldefs.h ${BUILD_DIR}/config.h rlconf.h 90 | readline.o: history.h rlstdc.h 91 | -readline.o: posixstat.h ansi_stdlib.h posixjmp.h 92 | +readline.o: posixstat.h ansi_stdlib.h posixjmp.h athame.h 93 | rltty.o: rldefs.h ${BUILD_DIR}/config.h rlconf.h 94 | rltty.o: rltty.h 95 | rltty.o: readline.h keymaps.h rltypedefs.h chardefs.h tilde.h rlstdc.h 96 | @@ -532,6 +539,7 @@ 97 | text.o: rlmbutil.h 98 | vi_mode.o: rlmbutil.h 99 | 100 | +athame.o: $(srcdir)/athame.c 101 | bind.o: $(srcdir)/bind.c 102 | callback.o: $(srcdir)/callback.c 103 | compat.o: $(srcdir)/compat.c 104 | @@ -570,6 +578,7 @@ 105 | history.o: $(srcdir)/history.c 106 | histsearch.o: $(srcdir)/histsearch.c 107 | 108 | +athame.o: athame.c 109 | bind.o: bind.c 110 | callback.o: callback.c 111 | compat.o: compat.c 112 | diff -ru a/readline.c b/readline.c 113 | --- a/readline.c 2015-07-15 00:03:32.898231038 -0400 114 | +++ b/readline.c 2015-07-15 00:07:56.201576180 -0400 115 | @@ -67,6 +67,7 @@ 116 | /* Some standard library routines. */ 117 | #include "readline.h" 118 | #include "history.h" 119 | +#include "athame.h" 120 | 121 | #include "rlprivate.h" 122 | #include "rlshell.h" 123 | @@ -450,6 +451,8 @@ 124 | if (rl_pre_input_hook) 125 | (*rl_pre_input_hook) (); 126 | 127 | + athame_init(fileno(rl_instream), rl_outstream); 128 | + 129 | RL_CHECK_SIGNALS (); 130 | } 131 | 132 | @@ -461,6 +464,8 @@ 133 | 134 | RL_CHECK_SIGNALS (); 135 | 136 | + athame_cleanup(0); 137 | + 138 | /* Restore the original of this history line, iff the line that we 139 | are editing was originally in the history, AND the line has changed. */ 140 | entry = current_history (); 141 | @@ -570,7 +575,16 @@ 142 | } 143 | 144 | RL_SETSTATE(RL_STATE_READCMD); 145 | - c = rl_read_key (); 146 | + int use_athame = athame_enabled(); 147 | + if (use_athame) 148 | + { 149 | + c = athame_loop(fileno(rl_instream)); 150 | + } 151 | + else { 152 | + c = rl_read_key(); 153 | + athame_after_bypass(); 154 | + } 155 | + if (!use_athame || c) { 156 | RL_UNSETSTATE(RL_STATE_READCMD); 157 | 158 | /* look at input.c:rl_getc() for the circumstances under which this will 159 | @@ -630,6 +644,7 @@ 160 | 161 | lastc = c; 162 | r = _rl_dispatch ((unsigned char)c, _rl_keymap); 163 | + athame_char_handled(); 164 | RL_CHECK_SIGNALS (); 165 | 166 | /* If there was no change in _rl_last_command_was_kill, then no kill 167 | @@ -638,6 +653,7 @@ 168 | if (rl_pending_input == 0 && lk == _rl_last_command_was_kill) 169 | _rl_last_command_was_kill = 0; 170 | 171 | +} 172 | _rl_internal_char_cleanup (); 173 | 174 | #if defined (READLINE_CALLBACKS) 175 | diff -ru a/shlib/Makefile.in b/shlib/Makefile.in 176 | --- a/shlib/Makefile.in 2015-07-15 00:03:32.898231038 -0400 177 | +++ b/shlib/Makefile.in 2015-07-14 23:37:04.674826478 -0400 178 | @@ -64,7 +64,7 @@ 179 | DESTDIR = 180 | 181 | CFLAGS = @CFLAGS@ 182 | -LOCAL_CFLAGS = @LOCAL_CFLAGS@ -DRL_LIBRARY_VERSION='"$(RL_LIBRARY_VERSION)"' 183 | +LOCAL_CFLAGS = @LOCAL_CFLAGS@ -DRL_LIBRARY_VERSION='"$(RL_LIBRARY_VERSION)"' -DVIMBED_LOCATION='"${libdir}/athame_readline"' -DATHAME_VIM_BIN='"${ATHAME_VIM_BIN}"' -DATHAME_USE_JOBS_DEFAULT='${ATHAME_USE_JOBS_DEFAULT}' 184 | CPPFLAGS = @CPPFLAGS@ 185 | LDFLAGS = @LDFLAGS@ @LOCAL_LDFLAGS@ @CFLAGS@ 186 | 187 | @@ -135,7 +135,7 @@ 188 | $(topdir)/shell.c $(topdir)/savestring.c $(topdir)/tilde.c \ 189 | $(topdir)/text.c $(topdir)/misc.c $(topdir)/compat.c \ 190 | $(topdir)/colors.c $(topdir)/parse-colors.c \ 191 | - $(topdir)/mbutil.c 192 | + $(topdir)/mbutil.c $(topdir)/athame.c 193 | 194 | # The header files for this library. 195 | HSOURCES = $(topdir)/readline.h $(topdir)/rldefs.h $(topdir)/chardefs.h \ 196 | @@ -145,7 +145,7 @@ 197 | $(topdir)/ansi_stdlib.h $(topdir)/tcap.h $(topdir)/rlstdc.h \ 198 | $(topdir)/xmalloc.h $(topdir)/rlprivate.h $(topdir)/rlshell.h \ 199 | $(topdir)/rltypedefs.h $(topdir)/rlmbutil.h \ 200 | - $(topdir)/colors.h $(topdir)/parse-colors.h 201 | + $(topdir)/colors.h $(topdir)/parse-colors.h $(topdir)/athame.h 202 | 203 | SHARED_HISTOBJ = history.so histexpand.so histfile.so histsearch.so shell.so \ 204 | mbutil.so 205 | @@ -156,7 +156,7 @@ 206 | util.so kill.so undo.so macro.so input.so callback.so terminal.so \ 207 | text.so nls.so misc.so \ 208 | $(SHARED_HISTOBJ) $(SHARED_TILDEOBJ) $(SHARED_COLORSOBJ) \ 209 | - xmalloc.so xfree.so compat.so 210 | + xmalloc.so xfree.so compat.so athame.so 211 | 212 | ########################################################################## 213 | 214 | @@ -191,7 +191,14 @@ 215 | -$(SHELL) $(topdir)/support/mkdirs $(DESTDIR)$(libdir) 216 | -$(SHELL) $(topdir)/support/mkdirs $(DESTDIR)$(bindir) 217 | 218 | -install-supported: installdirs $(SHLIB_STATUS) 219 | +install-athame: 220 | + $(RM) $(DESTDIR)$(libdir)/athame_readline/vimbed.vim && $(RM) -r $(DESTDIR)$(libdir)/athame_readline 221 | + $(CP) -r $(topdir)/vimbed/plugin $(DESTDIR)$(libdir)/athame_readline 222 | + 223 | +uninstall-athame: 224 | + $(RM) -r $(DESTDIR)$(libdir)/athame_readline 225 | + 226 | +install-supported: installdirs $(SHLIB_STATUS) install-athame 227 | $(SHELL) $(topdir)/support/shlib-install -O $(host_os) -V $(host_vendor) -d $(DESTDIR)$(libdir) -b $(DESTDIR)$(bindir) -i "$(INSTALL_DATA)" $(SHARED_HISTORY) 228 | $(SHELL) $(topdir)/support/shlib-install -O $(host_os) -V $(host_vendor) -d $(DESTDIR)$(libdir) -b $(DESTDIR)$(bindir) -i "$(INSTALL_DATA)" $(SHARED_READLINE) 229 | @echo install: you may need to run ldconfig 230 | @@ -201,7 +208,7 @@ 231 | 232 | install: install-$(SHLIB_STATUS) 233 | 234 | -uninstall-supported: 235 | +uninstall-supported: uninstall-athame 236 | $(SHELL) $(topdir)/support/shlib-install -O $(host_os) -V $(host_vendor) -d $(DESTDIR)$(libdir) -b $(DESTDIR)$(bindir) -U $(SHARED_HISTORY) 237 | $(SHELL) $(topdir)/support/shlib-install -O $(host_os) -V $(host_vendor) -d $(DESTDIR)$(libdir) -b $(DESTDIR)$(bindir) -U $(SHARED_READLINE) 238 | @echo uninstall: you may need to run ldconfig 239 | @@ -341,6 +348,8 @@ 240 | xmalloc.so: ${BUILD_DIR}/config.h 241 | xmalloc.so: $(topdir)/ansi_stdlib.h 242 | 243 | +readline.so: $(topdir)/athame.h 244 | + 245 | bind.so: $(topdir)/rlshell.h 246 | histfile.so: $(topdir)/rlshell.h 247 | nls.so: $(topdir)/rlshell.h 248 | @@ -427,6 +436,7 @@ 249 | colors.so: $(topdir)/rlmbutil.h 250 | parse-colors.so: $(topdir)/rlmbutil.h 251 | 252 | +athame.so: $(topdir)/athame.c 253 | bind.so: $(topdir)/bind.c 254 | callback.so: $(topdir)/callback.c 255 | compat.so: $(topdir)/compat.c 256 | @@ -462,6 +472,7 @@ 257 | history.so: $(topdir)/history.c 258 | histsearch.so: $(topdir)/histsearch.c 259 | 260 | +athame.so: athame.c 261 | bind.so: bind.c 262 | callback.so: callback.c 263 | comapt.so: compat.c 264 | diff -ru a/readline.h b/readline.h 265 | --- a/readline.h 2015-10-29 00:00:00.000000000 -0400 266 | +++ b/readline.h 2015-10-29 00:00:00.000000000 -0400 267 | @@ -295,6 +295,7 @@ 268 | 269 | extern int rl_set_prompt PARAMS((const char *)); 270 | extern int rl_expand_prompt PARAMS((char *)); 271 | +extern int rla_prompt_phys_length PARAMS((void)); 272 | 273 | extern int rl_initialize PARAMS((void)); 274 | 275 | diff -ru a/callback.c b/callback.c 276 | --- a/callback.c 2015-10-29 00:00:00.000000000 -0400 277 | +++ b/callback.c 2015-10-29 00:00:00.000000000 -0400 278 | @@ -26,6 +26,7 @@ 279 | #endif 280 | 281 | #include "rlconf.h" 282 | +#include "athame.h" 283 | 284 | #if defined (READLINE_CALLBACKS) 285 | 286 | @@ -311,6 +312,8 @@ 287 | rl_clear_signals (); 288 | #endif 289 | } 290 | + // If readline exits early because of a SIGINT, it still needs to cleanup athame. 291 | + athame_cleanup(1); 292 | } 293 | 294 | _rl_callback_generic_arg * 295 | -------------------------------------------------------------------------------- /readline_athame_setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # readline_athame_setup.sh -- Full vim integration for your shell. 3 | # 4 | # Copyright (C) 2017 James Kolb 5 | # 6 | # This file is part of Athame. 7 | # 8 | # Athame 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 # (at your option) any later version. 11 | # 12 | # Athame 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 Athame. If not, see . 19 | 20 | shopt -s extglob 21 | 22 | patches=0 23 | redownload=0 24 | build=1 25 | runtest=1 26 | athame=1 27 | dirty=0 28 | rc=1 29 | submodule=1 30 | vimbin="" 31 | destdir="" 32 | sudo="" 33 | prefix_flag="--prefix=/usr" 34 | libdir_flag="" 35 | for arg in "$@" 36 | do 37 | case "$arg" in 38 | "--redownload" ) redownload=1;; 39 | "--nobuild" ) build=0;; 40 | "--notest" ) runtest=0;; 41 | "--noathame" ) athame=0;; 42 | "--dirty" ) dirty=1;; 43 | "--norc" ) rc=0;; 44 | "--nosubmodule" ) submodule=0;; 45 | "--use_sudo" ) sudo="sudo ";; 46 | --vimbin=*) vimbin="${arg#*=}";; 47 | --destdir=*) destdir="${arg#*=}";; 48 | --prefix=*) prefix_flag='--prefix='"${arg#*=}";; 49 | --libdir=*) libdir="${arg#*=}";libdir_flag="--libdir=$libdir";; 50 | "--help" ) echo -e " --redownload: redownload readline and patches\n" \ 51 | "--nobuild: stop before actually building src\n" \ 52 | "--notest: don't run tests\n" \ 53 | "--noathame: setup normal readline without athame\n" \ 54 | "--vimbin=path/to/vim: set a path to the vim binary\n"\ 55 | " you want athame to use\n" \ 56 | "--prefix: set prefix for configure\n"\ 57 | "--libdir: set libdir for configure\n"\ 58 | "--destdir: set DESTDIR for install\n"\ 59 | "--dirty: don't run the whole patching/configure process,\n" \ 60 | " just make and install changes\n" \ 61 | "--norc: don't copy the rc file to /etc/athamerc\n" \ 62 | "--nosubmodule: don't update submodules\n" \ 63 | "--use_sudo: use sudo for installation and copying athamerc\n" \ 64 | "--help: display this message"; exit;; 65 | * ) echo Unknown flag "$arg" >&2; exit 1;; 66 | esac 67 | done 68 | 69 | #Get vim binary 70 | if [ -z "$vimbin" ]; then 71 | vimmsg="Please provide a vim binary by running this script with --vimbin=/path/to/vim at the end. (replace with the actual path to vim)" 72 | testvim="$(which vim)" 73 | if [ -z "$testvim" ]; then 74 | echo "Could not find a vim binary using 'which'" 75 | echo $vimmsg 76 | exit 77 | fi 78 | echo "No vim binary provided. Trying $testvim" 79 | if [ "$($testvim --version | grep -E '(\+job|nvim)')" ]; then 80 | vimbin="$testvim" 81 | echo "$vimbin probably has job support. Using $vimbin as vim binary." 82 | ATHAME_USE_JOBS_DEFAULT=1 83 | elif [ "$($testvim --version | grep +clientserver)" ]; then 84 | vimbin="$testvim" 85 | echo "$vimbin probably has clientserver support. Using $vimbin as vim binary." 86 | ATHAME_USE_JOBS_DEFAULT=0 87 | else 88 | echo "$testvim does not appear to have job or clientserver support." 89 | echo $vimmsg 90 | exit 91 | fi 92 | else 93 | if [ "$($vimbin --version | grep -E '(\+job|nvim)')" ]; then 94 | ATHAME_USE_JOBS_DEFAULT=1 95 | else 96 | ATHAME_USE_JOBS_DEFAULT=0 97 | fi 98 | fi 99 | 100 | if [ $submodule = 1 ]; then 101 | git submodule update --init 102 | fi 103 | 104 | #Download Readline 105 | if [ $redownload = 1 ]; then 106 | rm -r readline-8.0.tar.gz 107 | fi 108 | if [ ! -f readline-8.0.tar.gz ]; then 109 | curl -O https://ftp.gnu.org/gnu/readline/readline-8.0.tar.gz 110 | fi 111 | 112 | mkdir -p readline_patches 113 | cd readline_patches 114 | for (( patch=1; patch <= patches; patch++ )); do 115 | if [ $redownload = 1 ]; then 116 | rm -r readline80-$(printf "%03d" $patch) 117 | fi 118 | if [ ! -f readline80-$(printf "%03d" $patch) ]; then 119 | curl -O https://ftp.gnu.org/gnu/readline/readline-8.0-patches/readline80-$(printf "%03d" $patch) 120 | fi 121 | done 122 | cd .. 123 | 124 | if [ ! -d readline-8.0_tmp ]; then 125 | dirty=0 126 | fi 127 | 128 | #Unpack readline dir 129 | if [ $dirty = 0 ]; then 130 | rm -rf readline-8.0_tmp 131 | tar -xf readline-8.0.tar.gz 132 | mv readline-8.0 readline-8.0_tmp 133 | fi 134 | 135 | #Move into readline directory 136 | cd readline-8.0_tmp 137 | 138 | if [ $dirty = 0 ]; then 139 | #Patch readline with readline patches 140 | for (( patch=1; patch <= patches; patch++ )); do 141 | echo Patching with standard readline patch $patch 142 | patch -p0 < ../readline_patches/readline80-$(printf "%03d" $patch) 143 | done 144 | fi 145 | 146 | if [ $athame = 1 ]; then 147 | if [ $dirty = 0 ]; then 148 | ../athame_patcher.sh readline .. || exit 1 149 | else 150 | ../athame_patcher.sh --dirty readline .. || exit 1 151 | fi 152 | fi 153 | 154 | if [ $build != 1 ]; then 155 | exit 0 156 | fi 157 | 158 | #Build and install Readline 159 | if [ ! -f Makefile ]; then 160 | ./configure "$prefix_flag" "$libdir_flag" || exit 1 161 | fi 162 | make CFLAGS=-std=c99 SHLIB_LIBS="-lncurses -lutil" ATHAME_VIM_BIN="$vimbin" ATHAME_USE_JOBS_DEFAULT="$ATHAME_USE_JOBS_DEFAULT" || exit 1 163 | if [ $runtest = 1 ]; then 164 | rm -rf $(pwd)/../test/build 165 | mkdir -p $(pwd)/../test/build 166 | make install DESTDIR=$(pwd)/../test/build || exit 1 167 | 168 | cd ../test 169 | 170 | export LD_LIBRARY_PATH="$(dirname $(find $(pwd)/build -name libreadline* | head -n 1))" 171 | export ATHAME_VIMBED_LOCATION="$(find $(pwd)/build -name athame_readline | head -n 1)" 172 | 173 | if [ "$($vimbin --version | grep nvim)" ]; then 174 | nvim="nvim" 175 | fi 176 | if [ "$(uname)" == "Darwin" ]; then 177 | export DYLD_LIBRARY_PATH="$LD_LIBRARY_PATH" 178 | otool -L "$(which bash)" | grep libreadline.8.dylib >/dev/null 179 | else 180 | ldd "$(which bash)" | grep libreadline.so.8 >/dev/null 181 | fi 182 | if [ $? -eq 1 ]; then 183 | echo "Bash isn't set to use system readline or is not using readline 8. Setting up local bash for testing." 184 | cd .. 185 | ./bash_readline_setup.sh --destdir="$(pwd)/test/build" --use_readline="${LD_LIBRARY_PATH%+(/lib|/lib/*)}" 186 | cd test 187 | ./runtests.sh "$(pwd)/build/bin/bash -i" bash $nvim || exit 1 188 | else 189 | ./runtests.sh "bash -i" bash $nvim || exit 1 190 | fi 191 | cd ../readline-8.0_tmp 192 | fi 193 | echo "Installing Readline with Athame..." 194 | if [ -n "$destdir" ]; then 195 | mkdir -p "$destdir" 196 | fi 197 | ${sudo}make install DESTDIR="$destdir" || exit 1 198 | 199 | if [ $rc = 1 ]; then 200 | ${sudo}cp ../athamerc /etc/athamerc 201 | if [ $? -ne 0 ]; then 202 | printf "\e[0;31mThe athamerc was not copied. You should copy athamerc to /etc/athamerc or ~/.athamerc.\e[0;0m\n" 203 | fi 204 | fi 205 | -------------------------------------------------------------------------------- /test/bash/expected1: -------------------------------------------------------------------------------- 1 | good 2 | yay 3 | -------------------------------------------------------------------------------- /test/bash/inst1.sh: -------------------------------------------------------------------------------- 1 | input=0 2 | read -e input 3 | good 4 | echo $input > out1 5 | read -en3 input 6 | yayecho $input >> out1 7 | -------------------------------------------------------------------------------- /test/charread.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | text=$(cat $2) 3 | for (( i=0; i<${#text}; i++ )); do 4 | if [ "${text:$i:1}" == "" ]; then 5 | # Type arrows without pauses 6 | echo -n "${text:$i:3}" 7 | i=$((i+2)) 8 | else 9 | echo -n "${text:$i:1}" 10 | fi 11 | sleep $1 12 | done 13 | echo "" 14 | -------------------------------------------------------------------------------- /test/prefix.sh: -------------------------------------------------------------------------------- 1 | #The q\b is for bypassing zsh's newuser script 2 | qunset HISTFILE 3 | -------------------------------------------------------------------------------- /test/runtests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # runtests.sh 3 | # 4 | # Copyright (C) 2015 James Kolb 5 | # 6 | # This file is part of Athame. 7 | # 8 | # Athame 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 | # Athame 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 Athame. If not, see . 20 | 21 | first_test=1 22 | slow=0 23 | sanity=0 24 | 25 | function runtest () { 26 | sanity=$((sanity-1)) 27 | export ATHAME_TEST_RC=$(pwd)/../athamerc 28 | echo "Testing Athame $3..." 29 | mkdir -p testrun 30 | rm -rf testrun/* 31 | cp "$2"/inst* testrun 32 | failures="" 33 | cd testrun 34 | for t in inst*.sh; do 35 | i=${t:4:${#t}-7} 36 | cat ../prefix.sh inst$i.sh | grep -v '^\#' > input_text 37 | if [ "$using_nvim" ]; then 38 | # Skip tests containing "no_neovim" 39 | grep no_neovim $t > /dev/null && continue 40 | # nvim filters out all bare escapes pressed before vim starts , so let's 41 | # map Π to escape 42 | sed -Ei "s/([^\[]|$)/Π\1/g" input_text 43 | cp ../../athamerc neovim_athamerc 44 | echo "inoremap Π " >> neovim_athamerc 45 | export ATHAME_TEST_RC=$(pwd)/neovim_athamerc 46 | fi 47 | 48 | echo "Test $i:" 49 | printf "exit\n\x04" >> input_text 50 | 51 | milli=1 52 | # Do we have millisecond precision in date? 53 | if [ $(date +%s%3N | grep N) ]; then 54 | milli=0 55 | fi 56 | if [ $milli != 0 ]; then 57 | start_time=$(date +%s%3N) 58 | else 59 | start_time=$(date +%s999) 60 | fi 61 | script -c "cat input_text | $1" failure > /dev/null 2> /dev/null 62 | if [ $? -ne 0 ]; then 63 | # Linux version failed. Try bsd version: 64 | script failure bash -c "cat input_text | $1" > /dev/null 65 | fi 66 | if [ $milli != 0 ]; then 67 | end_time=$(date +%s%3N) 68 | else 69 | end_time=$(date +%s000) 70 | if [ $end_time -lt $start_time ]; then 71 | end_time=$((start_time+1)) 72 | fi 73 | fi 74 | keys_typed=$(($(wc -c < input_text))) 75 | speed=$((keys_typed * 1000 / $((end_time-start_time)))) 76 | # Make sure we can handle at least 27 keys per second. This is about 294 77 | # words per minute for English text, faster than world record typists. 78 | # 79 | # We don't count the first test for speed. We already know that vim can take 80 | # a while to load off disk on the first run and we don't want to mark the 81 | # test as slow just because the user hasn't run vim yet. 82 | if [ $milli != 0 ]; then 83 | echo speed=$speed 84 | fi 85 | if [ $first_test -ne 1 ] && [ $speed -lt 27 ]; then 86 | slow=1 87 | if [ $milli == 0 ]; then 88 | echo speed=$speed 89 | fi 90 | fi 91 | diff "../$2/expected$i" out$i >>failure 2>&1 92 | if [ $? -eq 0 ]; then 93 | echo "Success!" 94 | else 95 | echo "Failed." 96 | cat failure >>failures 97 | failures="$failures $i" 98 | fi 99 | echo "" 100 | first_test=0 101 | done 102 | cd .. 103 | if [[ -n $failures ]]; then 104 | echo "Test Failed" 105 | echo "Failed tests:$failures" 106 | while true; do 107 | printf "\n\e[1mWhat now?\e[0m\n\e[1mv:\e[0m view failures\n\e[1mC:\e[0m \e[0;31m*DANGEROUS*\e[0;0m continue anyway\n\e[1mx:\e[0m exit\n" 108 | read -rn 1 109 | echo "" 110 | if [[ $REPLY =~ ^[Vv]$ ]]; then 111 | cat testrun/failures 112 | elif [[ $REPLY =~ ^[C]$ ]]; then 113 | sanity=$((sanity+1)) 114 | return 1 115 | elif [[ $REPLY =~ ^[Xx]$ ]]; then 116 | exit 1 117 | else 118 | echo "Invalid option" 119 | fi 120 | done 121 | fi 122 | sanity=$((sanity+1)) 123 | return 0 124 | } 125 | 126 | if [ "$2" == "bash" ] || [ "$3" == "bash" ]; then 127 | using_bash=1 128 | fi 129 | if [ "$2" == "nvim" ] || [ "$3" == "nvim" ]; then 130 | using_nvim=1 131 | fi 132 | runtest "$1" shell "Shell" 133 | if [ "$using_bash" ]; then 134 | runtest "$1" bash "Bash Shell" 135 | fi 136 | runtest "$1" vim "Vim Integration" 137 | temp=$DISPLAY 138 | unset DISPLAY 139 | runtest "$1" shell "Shell without X" 140 | if [ $using_bash ]; then 141 | runtest "$1" bash "Bash Shell without X" 142 | fi 143 | DISPLAY=$temp 144 | 145 | if [ $slow -eq 1 ]; then 146 | echo "Athame is running slow on this computer. Install anyway?" 147 | read -p "Install anyway? (y:yes, other:no)? " -rn 1 148 | if ! [[ $REPLY =~ ^[Yy]$ ]]; then 149 | echo "" 150 | exit 1 151 | fi 152 | echo "" 153 | fi 154 | 155 | # Bash errors can cause us to skip code. Make extra sure we've run the tests if 156 | # we're saying we passed. 157 | if [ $sanity -lt 0 ]; then 158 | exit 1 159 | fi 160 | -------------------------------------------------------------------------------- /test/shell/expected1: -------------------------------------------------------------------------------- 1 | basic test 2 | -------------------------------------------------------------------------------- /test/shell/expected2: -------------------------------------------------------------------------------- 1 | tab test 2 | -------------------------------------------------------------------------------- /test/shell/expected3: -------------------------------------------------------------------------------- 1 | g 2 | g 3 | g 4 | g 5 | g 6 | g 7 | g 8 | g 9 | g 10 | g 11 | -------------------------------------------------------------------------------- /test/shell/expected4: -------------------------------------------------------------------------------- 1 | a 2 | b 3 | b 4 | 1 5 | 2 6 | 3 7 | 2 8 | 2 9 | -------------------------------------------------------------------------------- /test/shell/expected5: -------------------------------------------------------------------------------- 1 | long 2 | short 3 | short 4 | -------------------------------------------------------------------------------- /test/shell/inst1.sh: -------------------------------------------------------------------------------- 1 | echo "basic test" > out1 2 | -------------------------------------------------------------------------------- /test/shell/inst2.sh: -------------------------------------------------------------------------------- 1 | echo "tab test" > out2_extended 2 | touch out2_exxtended 3 | #This will fail on readline without the last_tab code 4 | mv out2_ t out2 5 | rm out2_exxtended 6 | -------------------------------------------------------------------------------- /test/shell/inst3.sh: -------------------------------------------------------------------------------- 1 | # Test for flaky tab failures 2 | rm -rf 3* 3 | echo b > 3ob 4 | echo g > 3og 5 | echo 'g' >> 3 g 6 | echo 'g' >> 3 g 7 | echo g >> 3 g 8 | echo g >> 3 g 9 | echo g >> 3 g 10 | echo 'g' >> 3 g 11 | echo 'g' >> 3 g 12 | echo 'g' >> 3 g 13 | echo 'g' >> 3 g 14 | rm 3 b 15 | mv 3 out3 16 | -------------------------------------------------------------------------------- /test/shell/inst4.sh: -------------------------------------------------------------------------------- 1 | # Test history 2 | rm -rf out4 3 | echo "a" >> out4 4 | echo "b" >> out4 5 |  6 | echo "1" >> out4 7 | echo "2" >> out4 8 | echo "3" >> out4 9 |  10 |  11 | -------------------------------------------------------------------------------- /test/shell/inst5.sh: -------------------------------------------------------------------------------- 1 | # Make sure quik 1 line commands work. 2 | echo long > out5 3 | alias a="echo short >> out5" 4 | a 5 | a 6 | -------------------------------------------------------------------------------- /test/vim/expected1: -------------------------------------------------------------------------------- 1 | basic test 2 | basic test 3 | -------------------------------------------------------------------------------- /test/vim/expected2: -------------------------------------------------------------------------------- 1 | TesT 2 2 | -------------------------------------------------------------------------------- /test/vim/expected3: -------------------------------------------------------------------------------- 1 | vim is awesome 2 | -------------------------------------------------------------------------------- /test/vim/expected4: -------------------------------------------------------------------------------- 1 | test 4 2 | -------------------------------------------------------------------------------- /test/vim/expected5: -------------------------------------------------------------------------------- 1 | xx 2 | mapped 3 | xx 4 | xx 5 | mapped 6 | mapped 7 | mapped mapped x 8 | z 9 | xx 10 | -------------------------------------------------------------------------------- /test/vim/expected6: -------------------------------------------------------------------------------- 1 | we made it 2 | -------------------------------------------------------------------------------- /test/vim/inst1.sh: -------------------------------------------------------------------------------- 1 | echo "basic xestbrtA" > out1 2 | echo "basic xest" >> out14brt 3 | -------------------------------------------------------------------------------- /test/vim/inst2.sh: -------------------------------------------------------------------------------- 1 | :set nocompatible 2 | :inoremap kj 3 | iecho "test 2kj2h~b~A" > out2 4 | -------------------------------------------------------------------------------- /test/vim/inst3.sh: -------------------------------------------------------------------------------- 1 | echo 'vim' 2 | echo 'is awesome' > out3 3 | kf'vkf'c 4 | -------------------------------------------------------------------------------- /test/vim/inst4.sh: -------------------------------------------------------------------------------- 1 | echo "test 4hlhlhlhlhlhlhlhlhlhlhlhlhlhlhlhlhlhlhlhlhlhlA" >> out4 2 | -------------------------------------------------------------------------------- /test/vim/inst5.sh: -------------------------------------------------------------------------------- 1 | export ATHAME_VIM_PERSIST=0 2 | echo xx > out5 3 | :inoremap xx mapped 4 | iecho xx >> out5 5 | echo xx >> out5 6 | export ATHAME_VIM_PERSIST=1 7 | echo xx >> out5 8 | :inoremap xx mapped 9 | iecho xx >> out5 10 | echo xx >> out5 11 | echo xx xx x >> out5 12 | export ATHAME_VIM_PERSIST=0 13 | # The env isn't set until after readline ends, so don't check on next line. 14 | echo z >> out5 15 | echo xx >> out5 16 | -------------------------------------------------------------------------------- /test/vim/inst6.sh: -------------------------------------------------------------------------------- 1 | # Enter ex mode and make sure Athame pulls us out after enough keys. 2 | #no_neovim because neovim jobs behave weirdly in ex mode 3 | rm out6 4 | echo Qohno 5 | ex mode we made it > out6 6 | -------------------------------------------------------------------------------- /zsh.patch: -------------------------------------------------------------------------------- 1 | diff -ru a/Config/defs.mk.in 2 | --- a/Config/defs.mk.in 2014-12-13 18:55:54.000000000 -0500 3 | +++ b/Config/defs.mk.in 2015-07-15 22:06:56.455125410 -0400 4 | @@ -41,7 +41,7 @@ 5 | exec_prefix = @exec_prefix@ 6 | bindir = @bindir@ 7 | libdir = @libdir@ 8 | -MODDIR = $(libdir)/$(tzsh)/$(VERSION) 9 | +MODDIR = $(ATHAME_TESTDIR)$(libdir)/$(tzsh)/$(VERSION) 10 | infodir = @infodir@ 11 | mandir = @mandir@ 12 | datarootdir = @datarootdir@ 13 | @@ -67,7 +67,7 @@ 14 | DLLDFLAGS = @DLLDFLAGS@ 15 | LIBLDFLAGS = @LIBLDFLAGS@ 16 | EXELDFLAGS = @EXELDFLAGS@ 17 | -LIBS = @LIBS@ 18 | +LIBS = @LIBS@ -lutil 19 | DL_EXT = @DL_EXT@ 20 | DLLD = @DLLD@ 21 | EXPOPT = @EXPOPT@ 22 | 23 | diff -ru a/Src/Makefile.in b/Src/Makefile.in 24 | --- a/Src/Makefile.in 2014-12-13 18:55:54.000000000 -0500 25 | +++ b/Src/Makefile.in 2015-07-15 22:06:56.455125410 -0400 26 | @@ -142,8 +142,15 @@ 27 | uninstall: uninstall.bin uninstall.modules 28 | .PHONY: install uninstall 29 | 30 | -install.bin: install.bin-here 31 | -uninstall.bin: uninstall.bin-here 32 | +install.vimbed: 33 | + if [ -d $(DESTDIR)$(libdir)/athame_zsh ]; then rm -r $(DESTDIR)$(libdir)/athame_zsh; fi 34 | + mkdir -p $(DESTDIR)/$(libdir) 35 | + cp -r $(dir_top)/$(subdir)/Zle/vimbed/plugin $(DESTDIR)/$(libdir)/athame_zsh 36 | +uninstall.vimbed: 37 | + rm -r $(DESTDIR)$(libdir)/athame_zsh 38 | + 39 | +install.bin: install.bin-here install.vimbed 40 | +uninstall.bin: uninstall.bin-here uninstall.vimbed 41 | .PHONY: install.bin uninstall.bin 42 | 43 | # install binary, creating install directory if necessary 44 | diff -ru a/Src/Makemod.in.in b/Src/Makemod.in.in 45 | --- a/Src/Makemod.in.in 2014-12-13 18:55:54.000000000 -0500 46 | +++ b/Src/Makemod.in.in 2015-07-15 21:52:18.235086024 -0400 47 | @@ -42,13 +42,14 @@ 48 | 49 | sdir_src = $(sdir_top)/Src 50 | dir_src = $(dir_top)/Src 51 | +athame = -DVIMBED_LOCATION='"$(ATHAME_TESTDIR)$(libdir)/athame_zsh"' -DATHAME_VIM_BIN='"${ATHAME_VIM_BIN}"' -DATHAME_USE_JOBS_DEFAULT='${ATHAME_USE_JOBS_DEFAULT}' 52 | 53 | # ========== COMPILATION RULES ========== 54 | 55 | DNCFLAGS = 56 | 57 | -COMPILE = $(CC) -c -I. -I$(dir_top)/Src -I$(sdir_top)/Src -I$(sdir_top)/Src/Zle -I$(sdir) $(CPPFLAGS) $(DEFS) $(CFLAGS) $(D@L@CFLAGS) 58 | -DLCOMPILE = $(CC) -c -I. -I$(dir_top)/Src -I$(sdir_top)/Src -I$(sdir_top)/Src/Zle -I$(sdir) $(CPPFLAGS) $(DEFS) -DMODULE $(CFLAGS) $(DLCFLAGS) 59 | +COMPILE = $(CC) -c -I. -I$(dir_top)/Src -I$(sdir_top)/Src -I$(sdir_top)/Src/Zle -I$(sdir) $(CPPFLAGS) $(DEFS) $(CFLAGS) $(D@L@CFLAGS) $(athame) 60 | +DLCOMPILE = $(CC) -c -I. -I$(dir_top)/Src -I$(sdir_top)/Src -I$(sdir_top)/Src/Zle -I$(sdir) $(CPPFLAGS) $(DEFS) -DMODULE $(CFLAGS) $(DLCFLAGS) $(athame) 61 | LINK = $(CC) $(LDFLAGS) $(EXELDFLAGS) $(EXTRA_LDFLAGS) -o $@ 62 | DLLINK = $(DLLD) $(LDFLAGS) $(LIBLDFLAGS) $(DLLDFLAGS) -o $@ 63 | 64 | diff -ru a/Src/Zle/zle_main.c b/Src/Zle/zle_main.c 65 | --- a/Src/Zle/zle_main.c 2015-05-22 18:16:13.000000000 -0400 66 | +++ b/Src/Zle/zle_main.c 2015-07-15 21:09:04.778303048 -0400 67 | @@ -28,6 +28,7 @@ 68 | */ 69 | 70 | #include "zle.mdh" 71 | +#include "athame.h" 72 | #include "zle_main.pro" 73 | 74 | #ifdef HAVE_POLL_H 75 | @@ -1097,6 +1098,8 @@ 76 | void 77 | zlecore(void) 78 | { 79 | + athame_init(SHTTY, NULL); 80 | + 81 | Keymap km; 82 | #if !defined(HAVE_POLL) && defined(HAVE_SELECT) 83 | struct timeval tv; 84 | @@ -1186,6 +1189,7 @@ 85 | } 86 | 87 | popheap(); 88 | + athame_cleanup(0); 89 | } 90 | 91 | /* Read a line. It is returned metafied. 92 | diff -ru a/Src/Zle/zle_keymap.c b/Src/Zle/zle_keymap.c 93 | --- a/Src/Zle/zle_keymap.c 2015-05-22 18:16:13.000000000 -0400 94 | +++ b/Src/Zle/zle_keymap.c 2015-08-23 00:04:00.000000000 -0400 95 | @@ -28,6 +28,7 @@ 96 | */ 97 | 98 | #include "zle.mdh" 99 | +#include "athame.h" 100 | 101 | /* 102 | * Keymap structures: 103 | @@ -1673,7 +1674,20 @@ 104 | static int 105 | getkeybuf(int w) 106 | { 107 | - int c = getbyte((long)w, NULL, 1); 108 | + int c; 109 | + if (athame_enabled()) 110 | + { 111 | +#ifdef MULTIBYTE_SUPPORT 112 | + lastchar_wide_valid = 0; 113 | +#endif 114 | + c = lastchar = STOUC(athame_loop(SHTTY)); 115 | + if(c == STOUC(EOF)) { 116 | + return EOF; 117 | + } 118 | + } 119 | + else { 120 | + c = getbyte((long)w, NULL, 1); 121 | + } 122 | 123 | if(c < 0) 124 | return EOF; 125 | diff -ru a/Src/Zle/zle.mdd b/Src/Zle/zle.mdd 126 | --- a/Src/Zle/zle.mdd 2014-12-13 18:55:54.000000000 -0500 127 | +++ b/Src/Zle/zle.mdd 2015-07-15 21:12:11.711644764 -0400 128 | @@ -8,7 +8,7 @@ 129 | objects="zle_bindings.o zle_hist.o zle_keymap.o zle_main.o \ 130 | zle_misc.o zle_move.o zle_params.o zle_refresh.o \ 131 | zle_thingy.o zle_tricky.o zle_utils.o zle_vi.o zle_word.o \ 132 | -textobjects.o" 133 | +textobjects.o athame.o" 134 | 135 | headers="zle.h zle_things.h" 136 | -------------------------------------------------------------------------------- /zsh_athame_setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # zsh_athame_setup.sh -- Full vim integration for your shell. 3 | # 4 | # Copyright (C) 2017 James Kolb 5 | # 6 | # This file is part of Athame. 7 | # 8 | # Athame 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 | # Athame 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 Athame. If not, see . 20 | 21 | redownload=0 22 | build=1 23 | runtest=1 24 | athame=1 25 | dirty=0 26 | rc=1 27 | submodule=1 28 | sudo="" 29 | vimbin="" 30 | destdir="" 31 | docdir="" 32 | htmldir="" 33 | prefix="/usr" 34 | for arg in "$@" 35 | do 36 | case "$arg" in 37 | "--redownload" ) redownload=1;; 38 | "--nobuild" ) build=0;; 39 | "--noathame" ) athame=0;; 40 | "--notest" ) runtest=0;; 41 | "--dirty" ) dirty=1;; 42 | "--norc" ) rc=0;; 43 | "--nosubmodule" ) submodule=0;; 44 | "--use_sudo") sudo="sudo ";; 45 | --vimbin=*) vimbin="${arg#*=}";; 46 | --destdir=*) destdir="${arg#*=}";; 47 | --prefix=*) prefix="${arg#*=}";; 48 | --libdir=*) libdir="${arg#*=}";; 49 | --docdir=*) docdir="${arg#*=}";; 50 | --htmldir=*) htmldir="${arg#*=}";; 51 | "--help" ) echo -e " --redownload: redownload zsh\n" \ 52 | "--nobuild: stop before actually building src\n" \ 53 | "--notest: don't run tests\n" \ 54 | "--noathame: setup normal zsh without athame\n" \ 55 | "--vimbin=path/to/vim: set a path to the vim binary\n"\ 56 | " you want athame to use\n" \ 57 | "--destdir: set DESTDIR for install\n"\ 58 | "--dirty: don't run the whole patching/configure process,\n" \ 59 | " just make and install changes\n" \ 60 | "--norc: don't copy the rc file to /etc/athamerc\n" \ 61 | "--nosubmodule: don't update submodules\n" \ 62 | "--use_sudo: use sudo for installation and copying athamerc\n" \ 63 | "additional flags: these flags are passed to configure\n" \ 64 | " --prefix=\n" \ 65 | " --libdir=\n" \ 66 | " --docdir=\n" \ 67 | " --htmldir=\n" \ 68 | "--help: display this message"; exit;; 69 | * ) echo Unknown flag "$arg" >&2; exit 1;; 70 | esac 71 | done 72 | 73 | prefix_flag="--prefix=$prefix" 74 | libdir_flag="${libdir:+--libdir=$libdir}" 75 | docdir_flag="--docdir=${docdir:-$prefix/share/doc/zsh}" 76 | htmldir_flag="--htmldir=${htmldir:-$docdir/html}" 77 | 78 | #Get vim binary 79 | if [ -z $vimbin ]; then 80 | vimmsg="Please provide a vim binary by running this script with --vimbin=/path/to/vim at the end. (replace with the actual path to vim)" 81 | testvim=$(which vim) 82 | if [ -z $testvim ]; then 83 | echo "Could not find a vim binary using 'which'" 84 | echo $vimmsg 85 | exit 86 | fi 87 | echo "No vim binary provided. Trying $testvim" 88 | if [ "$($testvim --version | grep -E '(\+job|nvim)')" ]; then 89 | vimbin="$testvim" 90 | echo "$vimbin probably has job support. Using $vimbin as vim binary." 91 | ATHAME_USE_JOBS_DEFAULT=1 92 | elif [ "$($testvim --version | grep +clientserver)" ]; then 93 | vimbin=$testvim 94 | echo "$vimbin probably has clientserver support. Using $vimbin as vim binary." 95 | ATHAME_USE_JOBS_DEFAULT=0 96 | else 97 | echo "$testvim does not appear to have clientserver support." 98 | echo $vimmsg 99 | exit 100 | fi 101 | else 102 | if [ "$($vimbin --version | grep -E '(\+job|nvim)')" ]; then 103 | ATHAME_USE_JOBS_DEFAULT=1 104 | else 105 | ATHAME_USE_JOBS_DEFAULT=0 106 | fi 107 | fi 108 | 109 | if [ $submodule = 1 ]; then 110 | git submodule update --init 111 | fi 112 | 113 | #Download zsh 114 | if [ $redownload = 1 ]; then 115 | rm -r zsh-5.7.1.tar.xz 116 | fi 117 | if [ ! -f zsh-5.7.1.tar.xz ]; then 118 | curl -O https://www.zsh.org/pub/zsh-5.7.1.tar.xz 119 | head -n 3 zsh-5.7.1.tar.xz | grep "404 Not Found" > /dev/null 120 | if [ $? -eq 0 ]; then 121 | curl -O https://www.zsh.org/pub/old/zsh-5.7.1.tar.xz 122 | fi 123 | fi 124 | if [ "$(md5sum zsh-5.7.1.tar.xz 2>/dev/null)" != "374f9fdd121b5b90e07abfcad7df0627 zsh-5.7.1.tar.xz" ] && [ "$(md5 zsh-5.7.1.tar.xz 2>/dev/null)" != "MD5 (zsh-5.7.1.tar.xz) = 374f9fdd121b5b90e07abfcad7df0627" ]; then 125 | #rm zsh-5.7.1.tar.xz 126 | echo "FAILED: Incorrect md5 hash" >&2 127 | exit 1 128 | fi 129 | 130 | if [ ! -d zsh-5.7.1_tmp ]; then 131 | dirty=0 132 | fi 133 | 134 | #Unpack zsh dir 135 | if [ $dirty = 0 ]; then 136 | rm -rf zsh-5.7.1_tmp 137 | tar -xf zsh-5.7.1.tar.xz 138 | mv zsh-5.7.1 zsh-5.7.1_tmp 139 | fi 140 | 141 | #Patch Zsh with Athame 142 | cd zsh-5.7.1_tmp 143 | if [ $athame = 1 ]; then 144 | if [ $dirty = 0 ]; then 145 | ../athame_patcher.sh zsh .. || exit 1 146 | else 147 | ../athame_patcher.sh --dirty zsh .. || exit 1 148 | fi 149 | fi 150 | 151 | if [ $build != 1 ]; then 152 | exit 0 153 | fi 154 | 155 | #Build and install zsh 156 | if [ ! -f Makefile ]; then 157 | ./configure "$prefix_flag" \ 158 | "$libdir_flag" \ 159 | "$docdir_flag" \ 160 | "$htmldir_flag" \ 161 | --enable-etcdir=/etc/zsh \ 162 | --enable-zshenv=/etc/zsh/zshenv \ 163 | --enable-zlogin=/etc/zsh/zlogin \ 164 | --enable-zlogout=/etc/zsh/zlogout \ 165 | --enable-zprofile=/etc/zsh/zprofile \ 166 | --enable-zshrc=/etc/zsh/zshrc \ 167 | --enable-maildir-support \ 168 | --enable-multibyte \ 169 | --enable-function-subdirs \ 170 | --enable-fndir="$prefix/share/zsh/functions" \ 171 | --enable-scriptdir="$prefix/share/zsh/scripts" \ 172 | --with-tcsetpgrp \ 173 | --enable-pcre \ 174 | --enable-cap \ 175 | --enable-zsh-secure-free \ 176 | || exit 1 177 | fi 178 | if [ $runtest = 1 ]; then 179 | rm -rf "$(pwd)/../test/build" 180 | mkdir -p "$(pwd)/../test/build" 181 | 182 | # make sure the files affected by ATHAME_TESTDIR are updated to use test settings 183 | rm -f Src/zshpaths.h && touch Src/Zle/athame.c 184 | 185 | make CFLAGS=-std=c99 ATHAME_VIM_BIN="$vimbin" ATHAME_USE_JOBS_DEFAULT="$ATHAME_USE_JOBS_DEFAULT" ATHAME_TESTDIR="$(pwd)/../test/build" || exit 1 186 | make install DESTDIR="$(pwd)/../test/build" || exit 1 187 | 188 | # make sure the files affected by ATHAME_TESTDIR are updated to not use test settings 189 | rm -f Src/zshpaths.h && touch Src/Zle/athame.c 190 | 191 | cd ../test 192 | zsh_bin="$(find $(pwd)/build -name zsh -type f | head -n 1)" 193 | if [ "$($vimbin --version | grep nvim)" ]; then 194 | nvim="nvim" 195 | fi 196 | if [ "$(uname)" == "Darwin" ]; then 197 | ./runtests.sh "script /dev/null $zsh_bin" $nvim || exit 1 198 | else 199 | ./runtests.sh "script -c $zsh_bin" $nvim || exit 1 200 | fi 201 | cd - 202 | fi 203 | make CFLAGS=-std=c99 ATHAME_VIM_BIN="$vimbin" ATHAME_USE_JOBS_DEFAULT="$ATHAME_USE_JOBS_DEFAULT" || exit 1 204 | echo "Installing Zsh with Athame..." 205 | if [ -n "$destdir" ]; then 206 | mkdir -p "$destdir" 207 | fi 208 | ${sudo}make install DESTDIR="$destdir" || exit 1 209 | 210 | if [ $rc = 1 ]; then 211 | ${sudo}cp ../athamerc /etc/athamerc 212 | if [ $? -ne 0 ]; then 213 | printf "\e[0;31mThe athamerc was not copied. You should copy athamerc to /etc/athamerc or ~/.athamerc.\e[0;0m\n" 214 | fi 215 | fi 216 | --------------------------------------------------------------------------------