├── .github └── workflows │ └── test.yml ├── .gitignore ├── LICENSE ├── Makefile ├── README.org ├── ffip-screenshot-nq8.png ├── find-file-in-project.el ├── pkg.sh └── tests ├── dummy.diff ├── emacs-init.el ├── ffip-tests.el ├── git-diff.diff ├── my-byte-compile.el ├── test1.ts └── test2.js /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | fail-fast: false 10 | matrix: 11 | emacs_version: 12 | - 25.3 13 | - 26.3 14 | - 27.1 15 | - snapshot 16 | steps: 17 | - uses: purcell/setup-emacs@master 18 | with: 19 | version: ${{ matrix.emacs_version }} 20 | 21 | - uses: actions/checkout@v2 22 | 23 | - name: Print emacs version 24 | run: emacs --version 25 | 26 | - name: Run tests 27 | run: make test 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.tar 2 | 3 | ### https://raw.github.com/github/gitignore/073727ccd9b0ec0b39440fb7673034f04f5d1302/Global/Emacs.gitignore 4 | 5 | # -*- mode: gitignore; -*- 6 | *~ 7 | \#*\# 8 | /.emacs.desktop 9 | /.emacs.desktop.lock 10 | *.elc 11 | auto-save-list 12 | tramp 13 | .\#* 14 | 15 | # Org-mode 16 | .org-id-locations 17 | *_archive 18 | 19 | # flymake-mode 20 | *_flymake.* 21 | 22 | # eshell files 23 | /eshell/history 24 | /eshell/lastdir 25 | 26 | # elpa packages 27 | /elpa/ 28 | 29 | # reftex files 30 | *.rel 31 | 32 | # AUCTeX auto folder 33 | /auto/ 34 | 35 | # cask packages 36 | .cask/ 37 | 38 | 39 | ### https://raw.github.com/github/gitignore/073727ccd9b0ec0b39440fb7673034f04f5d1302/Global/Vim.gitignore 40 | 41 | [._]*.s[a-w][a-z] 42 | [._]s[a-w][a-z] 43 | *.un~ 44 | Session.vim 45 | .netrwhist 46 | *~ 47 | 48 | deps/ 49 | 50 | TAGS 51 | tags 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SHELL = /bin/sh 2 | EMACS ?= emacs 3 | PROFILER = 4 | EMACS_GENERIC_OPTS=-Q -L . -L deps/ivy-0.13.4 5 | EMACS_BATCH_OPTS:=--batch $(EMACS_GENERIC_OPTS) 6 | RM = @rm -rf 7 | 8 | .PHONY: test clean test compile 9 | 10 | # Delete byte-compiled files etc. 11 | clean: 12 | $(RM) *~ 13 | $(RM) \#*\# 14 | $(RM) *.elc 15 | 16 | compile: clean 17 | @$(EMACS) $(EMACS_BATCH_OPTS) -l tests/my-byte-compile.el 2>&1 | grep -E "([Ee]rror|[Ww]arning):" && exit 1 || exit 0 18 | 19 | # Run tests. 20 | test: compile 21 | @$(EMACS) $(EMACS_BATCH_OPTS) -l tests/ffip-tests.el 22 | 23 | runemacs: 24 | @mkdir -p deps; 25 | @if [ ! -f deps/ivy-0.13.4/ivy.el ]; then curl -L https://stable.melpa.org/packages/ivy-0.13.4.tar | tar x -C deps/; fi; 26 | @$(EMACS) $(EMACS_GENERIC_OPTS) -l ./tests/emacs-init.el 27 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | * find-file-in-project 2 | 3 | [[https://github.com/redguardtoo/find-file-in-project/actions/workflows/test.yml][https://github.com/redguardtoo/find-file-in-project/actions/workflows/test.yml/badge.svg]] 4 | [[http://melpa.org/#/find-file-in-project][file:http://melpa.org/packages/find-file-in-project-badge.svg]] [[http://stable.melpa.org/#/find-file-in-project][file:http://stable.melpa.org/packages/find-file-in-project-badge.svg]] 5 | 6 | Find file/directory and review Diff/Patch/Commit quickly everywhere. 7 | 8 | *User Case One:* 9 | Find file/directory quickly in current project. The project root is detected *automatically* if Git/Subversion/Mercurial is used. 10 | 11 | *User Case Two:* 12 | Diff/patch files. Target files could be under any Version Control Software (VCS) or no VCS at all. Please check =ffip-diff-*= commands. 13 | 14 | *Features:* 15 | - Works on Windows64/Linux/macOS with minimum setup. Only dependency is =BSD/GNU Find= 16 | - Fast. Tested with 50,000+ files 17 | - Works flawlessly with [[https://www.emacswiki.org/emacs/TrampMode][Tramp Mode]] 18 | - Uses native API =completing-read= and supports [[https://www.gnu.org/software/emacs/manual/html_mono/ido.html][ido]]/[[https://github.com/emacs-helm/helm][helm]]/[[https://github.com/abo-abo/swiper][ivy]]/[[https://github.com/minad/consult][consult]]/[[https://github.com/raxod502/selectrum][selectrum]] out of box 19 | 20 | *Screenshot:* 21 | 22 | [[https://raw.githubusercontent.com/redguardtoo/find-file-in-project/master/ffip-screenshot-nq8.png]] 23 | * Try it 24 | Run =make runemacs= in this project's root directory. The demo works out of box. 25 | * Install 26 | Place [[https://raw.githubusercontent.com/redguardtoo/find-file-in-project/master/find-file-in-project.el][find-file-in-project.el]] under [[https://www.emacswiki.org/emacs/LoadPath][Load Path]]. Then add =(require 'find-file-in-project)= to your configuration. 27 | 28 | It is also possible to use [[http://stable.melpa.org/#/find-file-in-project][melpa]]; however be aware that as of the time of this writing installation using =package.el= is [[https://glyph.twistedmatrix.com/2015/11/editor-malware.html][not recommended]] due to flaws in Emacs's TLS implementation. 29 | 30 | Since =v3.7=, =Emacs 24.3= is required. 31 | 32 | Since =v5.7.5=, =Emacs 24.4= is required. 33 | 34 | Since =v6=, =Emacs 25.1= is required. 35 | 36 | Users of Debian ≥10 and derivatives can install this program with the following command: 37 | =sudo apt install elpa-find-file-in-project= 38 | * Setup 39 | Since v6, ffip gives you freedom to choose your favorite completion framework. 40 | 41 | It only uses builtin api =completing-read=. 42 | 43 | Ido setup, 44 | #+begin_src elisp 45 | (setq ffip-prefer-ido-mode t) 46 | #+end_src 47 | 48 | Helm setup, 49 | #+begin_src elisp 50 | (helm-mode 1) 51 | #+end_src 52 | 53 | Ivy setup, 54 | #+begin_src elisp 55 | (ivy-mode 1) 56 | #+end_src 57 | ** Windows 58 | Windows setup is as easy as *installing [[http://cygwin.com][Cygwin]] or [[https://msys2.github.io/][MYSYS2]] at default directory of any driver*. =GNU Find= executable is detected automatically. 59 | 60 | You can also manually specify the executable path, 61 | #+begin_src elisp 62 | (when (eq system-type 'windows-nt) (setq ffip-find-executable "c:\\\\cygwin64\\\\bin\\\\find")) 63 | #+end_src 64 | 65 | ** Linux and OS X 66 | NO setup needed. 67 | * Usage 68 | Project root is *automatically* detected if Git/Mercurial/Subversion is used. 69 | 70 | You can override the default root directory by setting =ffip-project-root=, 71 | #+begin_src elisp 72 | (setq ffip-project-root "~/projs/PROJECT_DIR") 73 | #+end_src 74 | 75 | Per-project and per-directory setup is easy. Check "Tips" section for details. 76 | ** =find-file-in-project-at-point= 77 | Guess the file path at point and try to find file. The path could contain environment variables. 78 | ** =find-file-in-project-by-selected= 79 | Use the selected region as keyword to search file. If no region is active, you could provide the keyword which could contain wildcard. 80 | 81 | If keyword contains line number like "hello.txt:32" or "hello.txt:32:", we will move to that line in opened file. 82 | 83 | If parameter is passed , file will be opened in new window. 84 | 85 | If =(setq ffip-match-path-instead-of-filename t)= is placed before =M-x find-file-in-project-by-selected=, we try to match selected text with any part of full path before displaying candidates. 86 | 87 | It could replace old command =find-file-in-project= (or =ffip=) because it's faster. It was tested searching in 50K+ files without any performance issue. 88 | ** =find-file-with-similar-name= 89 | Find file with similar name to current opened file. 90 | 91 | The regular expression =ffip-strip-file-name-regex= is also used by =find-file-with-similar-name=. 92 | ** =find-directory-in-project-by-selected= 93 | Use the selected region as keyword to find directory. If no region is active, you could provide the keyword. Keyword could contain wildcard character which passed to Find as value of =-iwholename= option 94 | 95 | If parameter is passed , directory will be opened in new window. 96 | ** =ffip-fix-file-path-at-point= 97 | It replaces file path at point with correct relative/absolute path. File path could contain environment variables. 98 | ** =find-file-in-project= 99 | Starts search immediately. This command is slow if there 10K+ files because it use ONLY Emacs Lisp to filter candidates. You should always use =find-file-in-project-by-selected= in big project.. 100 | ** =ffip-find-files-resume= 101 | File/directory searching actions are automatically stored into =ffip-find-files-history=. 102 | 103 | Use =ffip-find-files-resume= to replay any previous action. 104 | 105 | The maximum number of items of the history is set in =ffip-find-files-history-max-items=. 106 | ** =ffip-lisp-find-file-in-project= 107 | By default it finds file in project. f its parameter is not nil, it find directory instead. 108 | 109 | It's written in pure Lisp and does not use any third party command line program. So it works in all environments. 110 | ** =ffip-create-project-file= 111 | Create =.dir-locals.el= which "[[http://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html][defines the same set of local variables to all the files in a certain directory and its subdirectory]]". 112 | 113 | You can setup variables like =ffip-project-root= in this file. 114 | 115 | The original setup in =.dir-locals.el= is respected. This command will *merge* new setup with old content. 116 | 117 | See [[http://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html][Emacs manual]] for technical details. 118 | ** =find-file-in-current-directory= 119 | Like =find-file-in-project= but find file in current directory. 120 | ** =find-file-in-current-directory-by-selected= 121 | Like =find-file-in-project-by-selected= but find file in current directory. 122 | ** =ffip-show-diff= 123 | Execute backend from =ffip-diff-backends=. 124 | 125 | The output of backend execution is in [[http://www.gnu.org/software/diffutils/manual/html_node/Unified-Format.html][Unified Diff Format]] and is inserted into =*ffip-diff*= buffer where you can press =o=, =C-c C-c=, =ENTER= , =M-x ffip-diff-find-file= to open the corresponding file. 126 | 127 | =ffip-diff-find-file-before-hook= is called in =ffip-diff-find-file=. Two file names are passed to it as parameters. One name is returned by the hook as the file searching keyword. 128 | 129 | For example, you can =M-x ffip-show-diff= to view the git commit and open file inside patch. 130 | 131 | , =M-x 5 ffip-show-diff= executes 5th backend from =ffip-diff-backends=. 132 | 133 | Please press =C-h v ffip-diff-backends= to view available back-ends. 134 | 135 | Other key bindings defined in =*ffip-diff*= buffer, 136 | | key binding | command | 137 | |-------------+----------------| 138 | | p | diff-hunk-prev | 139 | | n | diff-hunk-next | 140 | | P | diff-file-prev | 141 | | N | diff-file-next | 142 | 143 | Insert below code into =.emacs= if you use =evil-mode=, 144 | #+begin_src elisp 145 | (defun ffip-diff-mode-hook-setup () 146 | (evil-local-set-key 'normal "K" 'diff-hunk-prev) 147 | (evil-local-set-key 'normal "J" 'diff-hunk-next) 148 | (evil-local-set-key 'normal "P" 'diff-file-prev) 149 | (evil-local-set-key 'normal "N" 'diff-file-next) 150 | (evil-local-set-key 'normal (kbd "RET") 'ffip-diff-find-file) 151 | (evil-local-set-key 'normal "o" 'ffip-diff-find-file)) 152 | (add-hook 'ffip-diff-mode-hook 'ffip-diff-mode-hook-setup) 153 | #+end_src 154 | 155 | You can customize the =ffip-diff-backends=, 156 | #+begin_src elisp 157 | (setq ffip-diff-backends 158 | '(ffip-diff-backend-git-show-commit 159 | "cd $(git rev-parse --show-toplevel) && git diff" 160 | "cd $(git rev-parse --show-toplevel) && git diff --cached" 161 | ffip-diff-backend-hg-show-commit 162 | ("Diff from `kill-ring'" . (car kill-ring)) 163 | "cd $(hg root) && hg diff" 164 | "svn diff")) 165 | #+end_src 166 | 167 | Please note some backends assume that the git cli program is added into environment variable PATH. 168 | ** =find-relative-path= 169 | Find file/directory and copy its relative path into `kill-ring'. 170 | 171 | File's path is copied by default. =C-u M-x find-relative-path= copy directory's path. 172 | 173 | You can set =ffip-find-relative-path-callback= to format the string before copying. 174 | #+begin_src elisp 175 | ;; (setq ffip-find-relative-path-callback 'ffip-copy-reactjs-import) 176 | (setq ffip-find-relative-path-callback 'ffip-copy-org-file-link) 177 | #+end_src 178 | ** =ffip-diff-apply-hunk= 179 | Similar to =diff-apply-hunk=, it applies current hunk on the target file (please note =ffip-diff-mode= inherits from =diff-mode=). 180 | 181 | The target file could be found by searching =(ffip-project-root)=. You can also apply extra operation on the file in =ffip-diff-apply-hunk-hook= before hunk applying happens. 182 | 183 | For example, for files under [[https://www.perforce.com/][Perforce]] control, 184 | #+begin_src elisp 185 | (defun p4-edit-file-and-make-buffer-writable(file) 186 | "p4 edit FILE and make corresponding buffer writable." 187 | (shell-command (format "p4 edit %s" file)) 188 | ;; make sure the buffer is readable 189 | (let* ((buf (get-file-buffer file))) 190 | (if buf 191 | (with-current-buffer buf 192 | ;; turn off read-only since we've already `p4 edit' 193 | (read-only-mode -1))))) 194 | (defun ffip-diff-apply-hunk-hook-setup (file) 195 | (unless (featurep 'init-perforce) (require 'init-perforce)) 196 | (if (string-match-p "/myproject/" file) 197 | (p4-edit-file-and-make-buffer-writable file))) 198 | (add-hook 'ffip-diff-apply-hunk-hook 'ffip-diff-apply-hunk-hook-setup) 199 | #+end_src 200 | ** =ffip-diff-filter-hunks-by-file-name= 201 | It can filter hunks by their file names. 202 | 203 | For example, user input pattern "regex !exclude1 exclude1" means the hunk's file name does match "regex", but does not match "exclude1" or "exclude2". 204 | 205 | Please note in "regex", space represents any string. 206 | ** =ffip-insert-file= 207 | Insert file content into current buffer. 208 | * Tips 209 | All tips are OPTIONAL. =find-file-in-project= works out of box in 99% cases. 210 | ** Use fd (A simple, fast and user-friendly alternative to 'find') 211 | Please insert =(setq ffip-use-rust-fd t)= into =.emacs= to use [[https://github.com/sharkdp/fd][fd]] (alternative to GNU Find). 212 | ** APIs 213 | - =ffip-get-project-root-directory= return the full path of current project 214 | ** Per-project setup using Emacs lisp 215 | Here is complete setup you could insert into =.emacs==, 216 | #+begin_src elisp 217 | ;; if the full path of current file is under SUBPROJECT1 or SUBPROJECT2 218 | ;; OR if I'm reading my personal issue track document, 219 | (defun my-setup-develop-environment () 220 | (interactive) 221 | (when (ffip-current-full-filename-match-pattern-p "\\(PROJECT_DIR\\|issue-track.org\\)") 222 | ;; Though PROJECT_DIR is team's project, I care only its sub-directory "subproj1"" 223 | (setq-local ffip-project-root "~/projs/PROJECT_DIR/subproj1") 224 | ;; well, I'm not interested in concatenated BIG js file or file in dist/ 225 | (setq-local ffip-find-options "-not -size +64k -not -iwholename '*/dist/*'") 226 | ;; for this project, I'm only interested certain types of files 227 | (setq-local ffip-patterns '("*.html" "*.js" "*.css" "*.java" "*.xml" "*.js")) 228 | ;; ignore files whose name match certain glob pattern 229 | (setq-local ffip-ignore-filenames '("*.bmp" "*.jpg")) 230 | ;; exclude `dist/' directory 231 | (push "*/dist" ffip-prune-patterns)) 232 | ;; insert more WHEN statements below this line for other projects 233 | ) 234 | ;; most major modes inherit from prog-mode, so below line is enough 235 | (add-hook 'prog-mode-hook 'my-setup-develop-environment) 236 | #+end_src 237 | ** Per-directory setup using =.dir-locals.el= 238 | All variables may be overridden on a per-directory basis in your =.dir-locals.el=. See (info "(Emacs) Directory Variables") for details. 239 | 240 | You can place =.dir-locals.el= into your project root directory. 241 | 242 | A sample =.dir-locals.el=, 243 | #+begin_src elisp 244 | ((nil . ((ffip-project-root . "~/projs/PROJECT_DIR") 245 | ;; ignore files bigger than 64k and directory "dist/" when searching 246 | (ffip-find-options . "-not -size +64k -not -iwholename '*/dist/*'") 247 | ;; only search files with following extensions 248 | (ffip-patterns . ("*.html" "*.js" "*.css" "*.java" "*.xml" "*.js")) 249 | (eval . (progn 250 | (require 'find-file-in-project) 251 | ;; ignore directory ".tox/" when searching 252 | (setq ffip-prune-patterns `("*/.tox" ,@ffip-prune-patterns)) 253 | ;; Do NOT ignore directory "bin/" when searching 254 | (setq ffip-prune-patterns `(delete "*/bin" ,@ffip-prune-patterns)))) 255 | ))) 256 | #+end_src 257 | 258 | As mentioned, =ffip-create-project-file= could create a minimum =.dir-locals.el=. 259 | 260 | BTW, please use either per-directory setup or per-project setup, *NOT* both. 261 | ** Specify root directory on Windows 262 | #+begin_src elisp 263 | (if (eq system-type 'windows-nt) 264 | ;; Native Windows 265 | (setq ffip-project-root "C:/Users/myname/projs/myproj1") 266 | ;; Cygwin 267 | (setq ffip-project-root "~/projs/myprojs1")) 268 | #+end_src 269 | ** Search and grep files under Git control 270 | Install [[https://github.com/abo-abo/swiper][counsel]]. 271 | 272 | Use =counsel-git= to find file and =counsel-git-grep= to grep. 273 | * Bug Report 274 | Check [[https://github.com/redguardtoo/find-file-in-project]]. 275 | * License 276 | This program is free software: you can redistribute it and/or modify it under the terms of the [[file:LICENSE][GNU General Public License]] as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 277 | 278 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the [[file:LICENSE][GNU General Public License]] for more details. 279 | -------------------------------------------------------------------------------- /ffip-screenshot-nq8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/redguardtoo/find-file-in-project/108cb496b1c37090add6fc13e975e572381fe50b/ffip-screenshot-nq8.png -------------------------------------------------------------------------------- /find-file-in-project.el: -------------------------------------------------------------------------------- 1 | ;;; find-file-in-project.el --- Find file/directory and review Diff/Patch/Commit efficiently -*- lexical-binding: t -*- 2 | 3 | ;; Copyright (C) 2006-2009, 2011-2012, 2015-2018 4 | ;; Phil Hagelberg, Doug Alcorn, Will Farrington, Chen Bin 5 | ;; 6 | ;; Version: 6.2.3 7 | ;; Author: Phil Hagelberg, Doug Alcorn, and Will Farrington 8 | ;; Maintainer: Chen Bin 9 | ;; URL: https://github.com/redguardtoo/find-file-in-project 10 | ;; Package-Requires: ((emacs "25.1")) 11 | ;; Created: 2008-03-18 12 | ;; Keywords: project, convenience 13 | ;; EmacsWiki: FindFileInProject 14 | 15 | ;; This file is NOT part of GNU Emacs. 16 | 17 | ;;; License: 18 | 19 | ;; This program is free software; you can redistribute it and/or modify 20 | ;; it under the terms of the GNU General Public License as published by 21 | ;; the Free Software Foundation; either version 3, or (at your option) 22 | ;; any later version. 23 | ;; 24 | ;; This program is distributed in the hope that it will be useful, 25 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 26 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27 | ;; GNU General Public License for more details. 28 | ;; 29 | ;; You should have received a copy of the GNU General Public License 30 | ;; along with GNU Emacs. If not, see . 31 | 32 | ;;; Commentary: 33 | 34 | ;; This program provides methods to find file in project. 35 | ;; 36 | ;; Features, 37 | ;; - Only dependency is BSD/GNU find 38 | ;; - Works on Windows with minimum setup 39 | ;; - Works on Tramp Mode (https://www.emacswiki.org/emacs/TrampMode) 40 | ;; - fd (faster alternative of find, see https://github.com/sharkdp/fd) is supported 41 | ;; - Uses native API `completing-read' and supports helm/ivy/consult/selectrum out of box. 42 | ;; 43 | ;; Helm setup, 44 | ;; (helm-mode 1) 45 | ;; 46 | ;; Ivy setup, 47 | ;; (ivy-mode 1) 48 | ;; 49 | ;; Ido setup, 50 | ;; (setq ffip-prefer-ido-mode t) 51 | ;; 52 | ;; Usage, 53 | ;; - You can insert "(setq ffip-use-rust-fd t)" into ".emacs" to use fd (alternative of find) 54 | ;; - `find-file-in-project-at-point' guess the file path at point and 55 | ;; find file 56 | ;; - `find-file-in-project-by-selected' uses the selected region 57 | ;; as the keyword to search file. You can provide the keyword 58 | ;; if no region is selected. 59 | ;; - `find-directory-in-project-by-selected' uses the select region 60 | ;; to find directory. You can provide the keyword if no region 61 | ;; is selected. 62 | ;; - `find-file-in-project' starts search file immediately 63 | ;; - `ffip-create-project-file' creates ".dir-locals.el" 64 | ;; - `ffip-lisp-find-file-in-project' finds file in project. 65 | ;; If its parameter is not nil, it find directory. 66 | ;; This command is written in pure Lisp and does not use any third party 67 | ;; command line program. So it works in all environments. 68 | ;; 69 | ;; A project is found by searching up the directory tree until a file 70 | ;; is found that matches `ffip-project-file'. 71 | ;; You can set `ffip-project-root-function' to provide an alternate 72 | ;; function to search for the project root. By default, it looks only 73 | ;; for files whose names match `ffip-patterns', 74 | 75 | ;; If you have so many files that it becomes unwieldy, you can set 76 | ;; `ffip-find-options' to a string which will be passed to the `find' 77 | ;; invocation in order to exclude irrelevant subdirectories/files. 78 | ;; For instance, in a Ruby on Rails project, you are interested in all 79 | ;; .rb files that don't exist in the "vendor" directory. In that case 80 | ;; you could set `ffip-find-options' to "-not -regex \".*vendor.*\"". 81 | 82 | ;; `ffip-insert-file' insert file content into current buffer. 83 | 84 | ;; `find-file-with-similar-name' find file with similar name to current 85 | ;; opened file. The regular expression `ffip-strip-file-name-regex' is 86 | ;; also used by `find-file-with-similar-name'. 87 | ;; 88 | ;; all these variables may be overridden on a per-directory basis in 89 | ;; your ".dir-locals.el". See (info "(Emacs) Directory Variables") for 90 | ;; details. 91 | ;; 92 | ;; Sample ".dir-locals.el", 93 | ;; 94 | ;; ((nil . ((ffip-project-root . "~/projs/PROJECT_DIR") 95 | ;; ;; ignore files bigger than 64k and directory "dist/" when searching 96 | ;; (ffip-find-options . "-not -size +64k -not -iwholename '*/dist/*'") 97 | ;; ;; only search files with following extensions 98 | ;; (ffip-patterns . ("*.html" "*.js" "*.css" "*.java" "*.xml" "*.js")) 99 | ;; (eval . (progn 100 | ;; (require 'find-file-in-project) 101 | ;; ;; ignore directory ".tox/" when searching 102 | ;; (setq ffip-prune-patterns `("*/.tox" ,@ffip-prune-patterns)) 103 | ;; ignore BMP image file 104 | ;; (setq ffip-ignore-filenames `("*.bmp" ,@ffip-ignore-filenames)) 105 | ;; ;; Do NOT ignore directory "bin/" when searching 106 | ;; (setq ffip-prune-patterns `(delete "*/bin" ,@ffip-prune-patterns)))) 107 | ;; ))) 108 | ;; 109 | ;; To find in current directory, use `find-file-in-current-directory' 110 | ;; and `find-file-in-current-directory-by-selected'. 111 | ;; 112 | ;; `ffip-fix-file-path-at-point' replaces path at point with correct relative/absolute path. 113 | ;; 114 | ;; File/directory searching actions are automatically stored into `ffip-find-files-history'. 115 | ;; Use `ffip-find-files-resume' to replay any previous action. 116 | ;; The maximum number of items of the history is set in `ffip-find-files-history-max-items'. 117 | ;; 118 | ;; `ffip-show-diff' execute the backend from `ffip-diff-backends'. 119 | ;; The output is in Unified Diff Format and inserted into *ffip-diff* buffer. 120 | ;; Press "o" or "C-c C-c" or "ENTER" or `M-x ffip-diff-find-file' in the 121 | ;; buffer to open corresponding file. Please note some backends assume that the git cli program 122 | ;; is added into environment variable PATH. 123 | ;; 124 | ;; `ffip-diff-find-file-before-hook' is called in `ffip-diff-find-file'. 125 | ;; Two file names are passed to it as parameters. One name is returned by the hook 126 | ;; as the file searching keyword. 127 | ;; 128 | ;; `ffip-diff-apply-hunk' applies current hunk in `diff-mode' (please note 129 | ;; `ffip-diff-mode' inherits from `diff-mode') to the target. 130 | ;; file. The target file could be located by searching `recentf-list'. 131 | ;; Except this extra feature, `ffip-diff-apply-hunk' is same as `diff-apply-hunk'. 132 | ;; So `diff-apply-hunk' can be replaced by `ffip-diff-apply-hunk'. 133 | ;; 134 | ;; `ffip-diff-filter-hunks-by-file-name' can filter hunks by their file names. 135 | ;; User input pattern "regex !exclude1 exclude1" means the hunk's file name does match "regex". 136 | ;; But does not match "exclude1" or "exclude2".; 137 | ;; Please note in "regex", space represents any string. 138 | ;; 139 | ;; If you use `evil-mode', insert below code into ~/.emacs, 140 | ;; (defun ffip-diff-mode-hook-setup () 141 | ;; (evil-local-set-key 'normal "K" 'diff-hunk-prev) 142 | ;; (evil-local-set-key 'normal "J" 'diff-hunk-next) 143 | ;; (evil-local-set-key 'normal "P" 'diff-file-prev) 144 | ;; (evil-local-set-key 'normal "N" 'diff-file-next) 145 | ;; (evil-local-set-key 'normal (kbd "RET") 'ffip-diff-find-file) 146 | ;; (evil-local-set-key 'normal "o" 'ffip-diff-find-file)) 147 | ;; (add-hook 'ffip-diff-mode-hook 'ffip-diff-mode-hook-setup) 148 | 149 | ;; `find-relative-path' find file/directory and copy its relative path 150 | ;; into `kill-ring'. You can customize `ffip-find-relative-path-callback' 151 | ;; to format the relative path, 152 | ;; (setq ffip-find-relative-path-callback 'ffip-copy-reactjs-import) 153 | ;; (setq ffip-find-relative-path-callback 'ffip-copy-org-file-link) 154 | ;; 155 | ;; BSD/GNU Find can be installed through Cygwin or MYSYS2 on Windows. 156 | ;; Executable is automatically detected. But you can manually specify 157 | ;; the executable location by insert below code into ".emacs", 158 | ;; 159 | ;; (if (eq system-type 'windows-nt) 160 | ;; (setq ffip-find-executable "c:\\\\cygwin64\\\\bin\\\\find")) 161 | ;; 162 | ;; This program works on Windows/Cygwin/Linux/macOS 163 | ;; 164 | ;; See https://github.com/redguardtoo/find-file-in-project for advanced tips. 165 | 166 | ;;; Code: 167 | 168 | (require 'find-file) 169 | (require 'find-lisp) 170 | (require 'diff-mode) 171 | (require 'windmove) 172 | (require 'subr-x) 173 | (require 'ido) 174 | 175 | (defgroup ffip nil 176 | "Find File in Project." 177 | :group 'convenience) 178 | 179 | (defcustom ffip-use-rust-fd nil 180 | "Use rust fd instead of GNU find." 181 | :link '(url-link :tag "fd @ GitHub" 182 | "https://github.com/sharkdp/fd") 183 | :group 'ffip 184 | :type 'boolean 185 | :safe #'booleanp) 186 | 187 | (defcustom ffip-rust-fd-executable-name "fd" 188 | "Rust fd executable name." 189 | :group 'ffip 190 | :type 'string) 191 | 192 | (defcustom ffip-gnu-find-executable-name "find" 193 | "GNU find executable name." 194 | :group 'ffip 195 | :type 'string) 196 | 197 | (defcustom ffip-rust-fd-respect-ignore-files t 198 | "Don't show search results from '.*ignore' files." 199 | :group 'ffip 200 | :type 'boolean 201 | :safe #'booleanp) 202 | 203 | (defcustom ffip-project-search-function 'ffip-project-search-default-function 204 | "Function to execute find program in shell." 205 | :group 'ffip 206 | :type 'function 207 | :safe #'functionp) 208 | 209 | (defcustom ffip-prefer-ido-mode nil 210 | "Prefer `ido-completing-read' to filter file candidates." 211 | :group 'ffip 212 | :type 'boolean 213 | :safe #'booleanp) 214 | 215 | (defcustom ffip-rust-fd-extra-opts "" 216 | "Rust fd extra options passed to cli." 217 | :group 'ffip 218 | :type 'string) 219 | 220 | (defcustom ffip-diff-find-file-by-file-name-p nil 221 | "When find file in diff hunk, only search by file name." 222 | :group 'ffip 223 | :type 'boolean 224 | :safe #'booleanp) 225 | 226 | (defcustom ffip-strip-file-name-regex 227 | "\\(\\.mock\\|\\.test\\|\\.mockup\\)" 228 | "Strip file name to get minimum keyword with this regex. 229 | It's used by `find-file-with-similar-name'." 230 | :group 'ffip 231 | :type 'regexp) 232 | 233 | (defcustom ffip-find-files-history-max-items 4 234 | "Maximum number of items stored in `ffip-find-files-history'. 235 | If this number is zero, no item is saved into `ffip-find-files-history'." 236 | :group 'ffip 237 | :type 'integer) 238 | 239 | (defvar ffip-filename-history nil 240 | "History of file names provided by users.") 241 | 242 | (defvar ffip-find-files-history nil 243 | "History generated by `ffip-find-files'.") 244 | 245 | (defvar ffip-diff-find-file-before-hook nil 246 | "Hook before `ffip-diff-find-file' move focus out of *ffip-diff* buffer.") 247 | 248 | (defvar ffip-read-file-name-hijacked-p nil 249 | "Internal flag used by `ffip-diff-apply-hunk'.") 250 | 251 | (defvar ffip-diff-apply-hunk-hook nil 252 | "Hook when `ffip-diff-apply-hunk' find the file to apply hunk. 253 | The file path is passed to the hook as the first argument.") 254 | 255 | (defvar ffip-relative-path-pattern "^\\(\\.\\.*/\\)+" 256 | "Pattern of relative path.") 257 | 258 | (defun ffip-nonempty-lines (str) 259 | "Return non empty lines from STR." 260 | (split-string str "[\r\n]+" t)) 261 | 262 | (defun ffip-diff-git-versions () 263 | "List all versions of code under Git." 264 | (let* ((cmd1 "git branch --no-color --all") 265 | (cmd2 (concat "git --no-pager log --date=short --pretty=format:'%h|%ad|%s|%an'" buffer-file-name))) 266 | (nconc (ffip-nonempty-lines (shell-command-to-string cmd1)) 267 | (ffip-nonempty-lines (shell-command-to-string cmd2))))) 268 | 269 | (defun ffip-diff-select-version () 270 | "Select a version from git history." 271 | (let* ((line (completing-read "Select from git history: " (ffip-diff-git-versions))) 272 | (version (replace-regexp-in-string "^ *\\*? *" 273 | "" 274 | (car (split-string line "|" t))))) 275 | version)) 276 | 277 | ;;;###autoload 278 | (defun ffip-git-diff-current-file () 279 | "Compare another version of current file." 280 | (let* ((default-directory (locate-dominating-file default-directory ".git"))) 281 | (shell-command-to-string (format "git --no-pager diff %s:%s %s" 282 | (ffip-diff-select-version) 283 | (file-relative-name buffer-file-name default-directory) 284 | buffer-file-name)))) 285 | 286 | (defun ffip-git-diff-project() 287 | "Compare another version of project." 288 | (let* ((default-directory (locate-dominating-file default-directory ".git"))) 289 | (shell-command-to-string (format "git --no-pager diff %s" 290 | (ffip-diff-select-version))))) 291 | 292 | (defun ffip-git-diff-directory() 293 | "Compare another version of current directory." 294 | (when buffer-file-name 295 | (let* ((dir (read-directory-name "Directory: " (file-name-directory buffer-file-name))) 296 | (default-directory (locate-dominating-file default-directory ".git"))) 297 | (shell-command-to-string (format "git --no-pager diff %s -- \"%s\"" 298 | (ffip-diff-select-version) 299 | dir))))) 300 | 301 | (defun ffip-git-diff-file-extension() 302 | "Compare another version of files matching file extensions from user input. 303 | File extensions are separated by space character." 304 | (let* ((patterns (read-string "File extensions (e.g., \"cpp py\" matches C++ and Python code files): " 305 | (and buffer-file-name (file-name-extension buffer-file-name))))) 306 | (when patterns 307 | (let* ((default-directory (locate-dominating-file default-directory ".git"))) 308 | (setq patterns 309 | (mapconcat (lambda (s) (format "\"*.%s\"" s)) (split-string patterns " +") " ")) 310 | (shell-command-to-string (format "git --no-pager diff %s -- %s" 311 | (ffip-diff-select-version) 312 | patterns)))))) 313 | 314 | (defvar ffip-diff-backends 315 | '(ffip-git-diff-current-file 316 | ffip-git-diff-project 317 | ffip-git-diff-directory 318 | ffip-git-diff-file-extension 319 | ("`git diff HEAD^` in project" . "cd $(git rev-parse --show-toplevel) && git diff HEAD^") 320 | ("`git diff --cached` in project" . "cd $(git rev-parse --show-toplevel) && git diff --cached") 321 | ("`git diff` in project" . "cd $(git rev-parse --show-toplevel) && git diff") 322 | ("`git diff` current file" . (shell-command-to-string (format "cd $(git rev-parse --show-toplevel) && git diff \"%s\"" 323 | (buffer-file-name)))) 324 | ;; git option "--cc" shows changes in merge commits 325 | ("`git log -p` current file" . (shell-command-to-string (format "cd $(git rev-parse --show-toplevel) && git --no-pager log --date=short -p --cc \"%s\"" 326 | (buffer-file-name)))) 327 | ("`git log -S keyword -p` in project" . (shell-command-to-string (format "cd $(git rev-parse --show-toplevel) && git --no-pager log --date=short -S\"%s\" -p --cc" 328 | (read-string "Git search string: ")))) 329 | ("Diff from `kill-ring'" . (car kill-ring))) 330 | "The list of back-ends. 331 | If back-end is string, it's run in `shell-command-to-string'. 332 | If it's a function or expression, it'll be executed and return a string. 333 | 334 | The output is inserted into *ffip-diff* buffer.") 335 | 336 | (defcustom ffip-find-executable nil 337 | "Path of GNU find. If nil we will guess." 338 | :group 'ffip 339 | :type 'string) 340 | 341 | (defcustom ffip-project-file '(".svn" ".hg" ".git") 342 | "The file/directory used to locate project root. 343 | May be set using .dir-locals.el. Checks each entry if set to a list." 344 | :group 'ffip 345 | :type '(repeat string)) 346 | 347 | (defcustom ffip-patterns nil 348 | "List of glob patterns to look for with `find-file-in-project'." 349 | :group 'ffip 350 | :type '(repeat string)) 351 | 352 | (defvar ffip-match-path-instead-of-filename nil 353 | "Match full path instead of file name.") 354 | 355 | ;; For "GNU/BSD Find", "*/test/*" matches "./test/" and "./dir/test/" 356 | ;; 357 | ;; But for "rust fd", only "test/*" matches "./test/" and "./dir/test/"; 358 | ;; "*/test/*" won't match "./test/" but matches "./dir/test/" 359 | ;; Maybe it's a fd bug. 360 | (defcustom ffip-prune-patterns 361 | '(;; VCS 362 | "*/.git" 363 | "*/.svn" 364 | "*/.cvs" 365 | "*/.tox" 366 | "*/.bzr" 367 | "*/.hg" 368 | "*/.DS_Store" 369 | "*/.sass-cache" 370 | "*/elpy" 371 | "*/dcache" 372 | "*/.npm" 373 | "*/.tmp" 374 | "*/.idea" 375 | "*/node_modules" 376 | "*/bower_components" 377 | "*/.gradle" 378 | "*/.cask") 379 | "Ignored directories(prune patterns)." 380 | :group 'ffip 381 | :type '(repeat string)) 382 | 383 | (defcustom ffip-ignore-filenames 384 | '(;; VCS 385 | ;; project misc 386 | "*.log" 387 | ;; Ctags 388 | "tags" 389 | "TAGS" 390 | ;; compressed 391 | "*.tgz" 392 | "*.gz" 393 | "*.xz" 394 | "*.zip" 395 | "*.tar" 396 | "*.rar" 397 | ;; Global/Cscope 398 | "GTAGS" 399 | "GPATH" 400 | "GRTAGS" 401 | "cscope.files" 402 | ;; html/javascript/css 403 | "*bundle.js" 404 | "*min.js" 405 | "*min.css" 406 | ;; Images 407 | "*.png" 408 | "*.jpg" 409 | "*.jpeg" 410 | "*.gif" 411 | "*.bmp" 412 | "*.tiff" 413 | "*.ico" 414 | ;; documents 415 | "*.doc" 416 | "*.docx" 417 | "*.xls" 418 | "*.ppt" 419 | "*.odt" 420 | ;; C/C++ 421 | "*.obj" 422 | "*.so" 423 | "*.o" 424 | "*.a" 425 | "*.ifso" 426 | "*.tbd" 427 | "*.dylib" 428 | "*.lib" 429 | "*.d" 430 | "*.dll" 431 | "*.exe" 432 | ;; Java 433 | ".metadata*" 434 | "*.class" 435 | "*.war" 436 | "*.jar" 437 | ;; Emacs/Vim 438 | "*flymake" 439 | "#*#" 440 | ".#*" 441 | "*.swp" 442 | "*~" 443 | "*.elc" 444 | ;; Python 445 | "*.pyc") 446 | "Ignored file names. Wildcast is supported." 447 | :group 'ffip 448 | :type '(repeat string)) 449 | 450 | (defcustom ffip-find-options "" 451 | "Extra options to pass to `find' when using `find-file-in-project'. 452 | 453 | Use this to exclude portions of your project: \"-not -regex \\\".*svn.*\\\"\"." 454 | :group 'ffip 455 | :type 'string) 456 | 457 | (defcustom ffip-find-pre-path-options "" 458 | "Options for find program. 459 | 460 | GNU Find requires '-H', '-L', '-P', '-D' and `-O' appear before first path '.'. 461 | For example, use '-L' to follow symbolic links." 462 | :group 'ffip 463 | :type 'string) 464 | 465 | (defcustom ffip-project-root nil 466 | "If non-nil, overrides the project root directory location." 467 | :group 'ffip 468 | :type 'string) 469 | 470 | (defcustom ffip-project-root-function nil 471 | "If non-nil, this function is called to determine the project root. 472 | This overrides variable `ffip-project-root' when set." 473 | :group 'ffip 474 | :type 'function) 475 | 476 | (defvar ffip-debug nil "Print debug information.") 477 | 478 | ;;;###autoload 479 | (defun ffip-copy-without-change (p) 480 | "Copy P without change." 481 | (kill-new p) 482 | (message "%s => kill-ring" p)) 483 | 484 | ;;;###autoload 485 | (defun ffip-copy-reactjs-import(p) 486 | "Create ReactJS link from P and copy the result." 487 | (setq p (format "import str from '%s';" p)) 488 | (kill-new p) 489 | (message "%s => kill-ring" p)) 490 | 491 | ;;;###autoload 492 | (defun ffip-copy-org-file-link(p) 493 | "Create org link from P and copy the result." 494 | (setq p (format "[[file:%s]]" p)) 495 | (kill-new p) 496 | (message "%s => kill-ring" p)) 497 | 498 | ;;;###autoload 499 | (defcustom ffip-find-relative-path-callback 'ffip-copy-without-change 500 | "The callback after calling `find-relative-path'." 501 | :group 'ffip 502 | :type 'function) 503 | 504 | (defun ffip--some (predicate seq) 505 | "Return if PREDICATE is t for any element of SEQ." 506 | (let* (elem rlt) 507 | (while (and (setq elem (car seq)) 508 | (not rlt)) 509 | (setq seq (cdr seq)) 510 | (setq rlt (funcall predicate elem))) 511 | rlt)) 512 | 513 | ;;;###autoload 514 | (defun ffip-project-root () 515 | "Return project root or `default-directory'." 516 | (let* ((project-root (or ffip-project-root 517 | (cond 518 | ((functionp ffip-project-root-function) 519 | (funcall ffip-project-root-function)) 520 | ((listp ffip-project-file) 521 | (ffip--some (apply-partially 'locate-dominating-file 522 | default-directory) 523 | ffip-project-file)) 524 | (t 525 | (locate-dominating-file default-directory 526 | ffip-project-file)))))) 527 | (or (and project-root (file-name-as-directory project-root)) 528 | default-directory))) 529 | 530 | (defun ffip--read-file-text (file) 531 | "Read text from FILE." 532 | (read (decode-coding-string 533 | (with-temp-buffer 534 | (set-buffer-multibyte nil) 535 | (setq buffer-file-coding-system 'binary) 536 | (insert-file-contents-literally file) 537 | (buffer-substring-no-properties (point-min) (point-max))) 'utf-8))) 538 | 539 | ;;;###autoload 540 | (defun ffip-get-project-root-directory () 541 | "Get the full path of project root directory." 542 | (if ffip-project-root (file-name-as-directory ffip-project-root) 543 | (ffip-project-root))) 544 | 545 | ;;;###autoload 546 | (defun ffip-filename-identity (keyword) 547 | "Return identical KEYWORD." 548 | keyword) 549 | 550 | ;;;###autoload 551 | (defun ffip-filename-camelcase-to-dashes (keyword &optional check-only) 552 | "Convert KEYWORD from camel cased to dash separated. 553 | If CHECK-ONLY is true, only do the check." 554 | (let* (rlt) 555 | (cond 556 | (check-only 557 | (setq rlt (string-match "^[a-z0-9]+[A-Z][A-Za-z0-9]+$" keyword)) 558 | (if ffip-debug (message "ffip-filename-camelcase-to-dashes called. check-only keyword=%s rlt=%s" keyword rlt))) 559 | (t 560 | (let* ((case-fold-search nil)) 561 | ;; case sensitive replace 562 | (setq rlt (downcase (replace-regexp-in-string "\\([a-z]\\)\\([A-Z]\\)" "\\1-\\2" keyword)))) 563 | 564 | (if (string= rlt (downcase keyword)) (setq rlt nil)) 565 | 566 | (if (and rlt ffip-debug) (message "ffip-filename-camelcase-to-dashes called. rlt=%s" rlt)))) 567 | rlt)) 568 | 569 | ;;;###autoload 570 | (defun ffip-filename-dashes-to-camelcase (keyword &optional check-only) 571 | "Convert KEYWORD from dash separated to camel cased. 572 | If CHECK-ONLY is true, only do the check." 573 | (let* (rlt) 574 | (cond 575 | (check-only 576 | (setq rlt (string-match "^[A-Za-z0-9]+\\(-[A-Za-z0-9]+\\)+$" keyword)) 577 | (if ffip-debug (message "ffip-filename-dashes-to-camelcase called. check-only keyword=%s rlt=%s" keyword rlt))) 578 | (t 579 | (setq rlt (mapconcat (lambda (s) (capitalize s)) (split-string keyword "-") "")) 580 | 581 | (let ((first-char (substring rlt 0 1))) 582 | (setq rlt (concat "[" first-char (downcase first-char) "]" (substring rlt 1)))) 583 | (if (and rlt ffip-debug) (message "ffip-filename-dashes-to-camelcase called. rlt=%s" rlt)))) 584 | rlt)) 585 | 586 | (defun ffip--create-filename-pattern-for-gnufind (keyword) 587 | "Create search pattern from KEYWORD." 588 | (let* ((rlt "")) 589 | (cond 590 | ((not keyword) 591 | (setq rlt "")) 592 | (t 593 | (setq rlt (concat (if ffip-match-path-instead-of-filename "-iwholename" "-iname") 594 | " \"*" 595 | keyword 596 | "*\"" )))) 597 | (if ffip-debug (message "ffip--create-filename-pattern-for-gnufind called. rlt=%s" rlt)) 598 | rlt)) 599 | 600 | (defun ffip--win-executable-find (exe) 601 | "Find EXE on windows." 602 | (let* ((drivers '("c" "d" "e" "g" "h" "i" "j" "k")) 603 | (i 0) 604 | j 605 | (dirs '(":\\\\cygwin64\\\\bin\\\\" 606 | ":\\\\cygwin-portable\\\\cygwin\\\\bin\\\\" 607 | ":\\\\msys64\\\\usr\\\\bin\\\\")) 608 | rlt) 609 | (while (and (not rlt) 610 | (< i (length dirs))) 611 | (setq j 0) 612 | (while (and (not rlt) 613 | (< j (length drivers))) 614 | (setq rlt (executable-find (concat (nth j drivers) (nth i dirs) exe))) 615 | (setq j (1+ j))) 616 | (setq i (1+ i))) 617 | (unless rlt 618 | ;; nothing found, fall back to exe 619 | (setq rlt exe)) 620 | rlt)) 621 | 622 | (defun ffip--executable-find () 623 | "Find EXE on all environments." 624 | (let* ((exe (if ffip-use-rust-fd ffip-rust-fd-executable-name 625 | ffip-gnu-find-executable-name)) 626 | rlt) 627 | (cond 628 | ((file-remote-p default-directory) 629 | ;; In tramp mode and local windows, remote nix-like, 630 | ;; the `ffip-find-executable' with windows path can't be applied. 631 | ;; Assume remote server has already added EXE into $PATH! 632 | ;; Thanks for ShuguangSun for the fix 633 | (setq rlt exe)) 634 | ((setq rlt ffip-find-executable)) 635 | ((eq system-type 'windows-nt) 636 | ;; in case PATH is not setup properly 637 | (cond 638 | (ffip-use-rust-fd 639 | (setq rlt (concat (getenv "USERPROFILE") 640 | "\\\\.cargo\\\\bin\\\\" 641 | exe 642 | ".exe")) 643 | (unless (file-exists-p rlt) 644 | (setq rlt exe))) 645 | (t 646 | (setq rlt (ffip--win-executable-find exe))))) 647 | ((setq rlt (executable-find exe))) 648 | (t 649 | ;; well, `executable-find' failed 650 | (setq rlt exe))) 651 | rlt)) 652 | 653 | (defun ffip--join-patterns (patterns) 654 | "Convert PATTERNS into cli arguments." 655 | (cond 656 | ((and ffip-patterns (not ffip-use-rust-fd)) 657 | (format "\\( %s \\)" (mapconcat (lambda (pat) (format "-iwholename \"%s\"" pat)) 658 | patterns " -or "))) 659 | (t 660 | ;; rust fd only supports ONE pattern (and it's regular expression) 661 | ;; which is precious resource to waste here 662 | ""))) 663 | 664 | (defun ffip--prune-patterns () 665 | "Turn `ffip-prune-patterns' into a string that `find' can use." 666 | ;; Both fd and find use "glob pattern" 667 | ;; @see https://en.wikipedia.org/wiki/Glob_%28programming%29 668 | (cond 669 | (ffip-use-rust-fd 670 | ;; fd match relative path 671 | (mapconcat (lambda (p) 672 | (format "-E \"%s\"" (replace-regexp-in-string "^\*/" "" p))) 673 | ffip-prune-patterns " ")) 674 | (t 675 | ;; find match whole path 676 | (mapconcat (lambda (p) 677 | (format "-iwholename \"%s\"" p)) 678 | ffip-prune-patterns " -or ")))) 679 | 680 | (defun ffip--ignore-file-names () 681 | "Turn `ffip-ignore-filenames' into a string that `find' can use." 682 | ;; @see `ffip-prune-patterns' for fd vs find. 683 | (cond 684 | (ffip-use-rust-fd 685 | (mapconcat (lambda (p) 686 | (format "-E \"%s\"" p)) 687 | ffip-ignore-filenames " ")) 688 | (t 689 | (mapconcat (lambda (n) (format "-not -name \"%s\"" n)) 690 | ffip-ignore-filenames " ")))) 691 | 692 | (defun ffip--file-completion-table (all-files) 693 | ;; direct copy of project--file-completion-table 694 | (lambda (string pred action) 695 | (cond 696 | ((eq action 'metadata) 697 | '(metadata . ((category . project-file)))) 698 | (t 699 | (complete-with-action action all-files string pred))))) 700 | 701 | ;;;###autoload 702 | (defun ffip-completing-read (prompt collection &optional action) 703 | "Read a string in minibuffer, with completion. 704 | 705 | PROMPT is a string with same format parameters in `completing-read'. 706 | COLLECTION is a list of strings. 707 | 708 | ACTION is a lambda function to call after selecting a result. 709 | 710 | This function returns the selected candidate or nil." 711 | (let* (selected) 712 | (cond 713 | ((= 1 (length collection)) 714 | ;; select the only candidate immediately 715 | (setq selected (car collection))) 716 | 717 | (ffip-prefer-ido-mode 718 | ;; ido can only handle list of strings 719 | (setq selected (ido-completing-read prompt (mapcar 'car collection)))) 720 | 721 | (t 722 | (setq selected 723 | (completing-read prompt (ffip--file-completion-table collection))) 724 | (setq selected (or (assoc selected collection) selected)))) 725 | 726 | (when selected 727 | ;; make sure only the string/file is passed to action 728 | (let* ((default-directory (ffip-get-project-root-directory)) 729 | (result (if (consp selected) (cdr selected) selected))) 730 | (if action (funcall action result) result))))) 731 | 732 | (defun ffip-create-shell-command (keyword find-directory-p) 733 | "Produce command to search KEYWORD. 734 | If FIND-DIRECTORY-P is t, we look up directory instead of file. 735 | Rust fd use regular expression. 736 | BSD/GNU Find use glob pattern." 737 | (let* (cmd fmt tgt) 738 | (cond 739 | (ffip-use-rust-fd 740 | ;; `-H` => search hidden files 741 | ;; `-E` => exclude pattern 742 | ;; `-c` => color 743 | ;; `-i` => case insensitive 744 | ;; `-t` => directory (d) or file (f) 745 | ;; `-p` => match full path 746 | (setq fmt (concat "%s %s -c never -H -i -t %s %s %s %s" 747 | (if ffip-rust-fd-respect-ignore-files "" " -I") 748 | (if ffip-match-path-instead-of-filename " -p" "") 749 | " " 750 | ffip-rust-fd-extra-opts 751 | " %s")) 752 | ;; fd use regular expression for target pattern (but glob pattern when excluding, sigh) 753 | (setq tgt (if keyword (format "\".*%s\"" keyword) ""))) 754 | (t 755 | (setq tgt 756 | (if find-directory-p (format "-iwholename \"*%s\"" keyword) 757 | (ffip--create-filename-pattern-for-gnufind keyword))) 758 | (setq fmt (concat "%s " 759 | ffip-find-pre-path-options 760 | " . \\( %s \\) -prune -o -type %s %s %s %s %s -print")))) 761 | 762 | (setq cmd (format fmt 763 | (ffip--executable-find) 764 | (ffip--prune-patterns) 765 | (if find-directory-p "d" "f") 766 | (ffip--ignore-file-names) 767 | ffip-find-options 768 | (ffip--join-patterns ffip-patterns) 769 | tgt)) 770 | cmd)) 771 | 772 | (defun ffip-glob-to-regex (s) 773 | "Convert glob pattern S into regular expression." 774 | (setq s (replace-regexp-in-string "\\." "\\\\." s)) 775 | (setq s (replace-regexp-in-string "\*" ".*" s)) 776 | s) 777 | 778 | (defmacro ffip-push-one-candidate (file result) 779 | "Push FILE into RESULT." 780 | ;; @see https://www.murilopereira.com/how-to-open-a-file-in-emacs/ 781 | ;; also @see #15 improving handling of directories containing space 782 | `(push (cons (replace-regexp-in-string "^\./" "" ,file) ,file) ,result)) 783 | 784 | (defun ffip-project-search-default-function (find-command) 785 | "Execute FIND-COMMAND in shell and split its output into lines." 786 | (if ffip-debug "ffip-project-search-default-function => find-command=%s" find-command) 787 | (split-string (shell-command-to-string find-command) "[\r\n]+" t)) 788 | 789 | ;;;###autoload 790 | (defun ffip-project-search (keyword &optional find-directory-p) 791 | "Return an alist of all filenames in the project and their path. 792 | 793 | Files with duplicate filenames are suffixed with the name of the 794 | directory they are found in so that they are unique. 795 | 796 | If KEYWORD is string, it's the file name or file path to find file. 797 | If KEYWORD is list, it's the list of file names. 798 | IF FIND-DIRECTORY-P is t, we are searching directories, else files." 799 | (let* ((default-directory (ffip-get-project-root-directory)) 800 | (cmd (ffip-create-shell-command keyword find-directory-p)) 801 | (collection (funcall ffip-project-search-function cmd)) 802 | rlt) 803 | 804 | (if ffip-debug (message "run command at %s: %s" default-directory cmd)) 805 | 806 | ;; use simple loop statement for clean code 807 | (cond 808 | ((and ffip-use-rust-fd ffip-patterns) 809 | (let* ((fd-file-pattern (concat "^" 810 | (mapconcat 'ffip-glob-to-regex ffip-patterns "\\|") 811 | "$"))) 812 | (dolist (file collection) 813 | ;; filter result with Lisp because fd does NOT support multiple patterns 814 | (if (string-match fd-file-pattern file) (ffip-push-one-candidate file rlt))))) 815 | 816 | (t 817 | (dolist (file collection) 818 | (ffip-push-one-candidate file rlt)))) 819 | 820 | (nreverse rlt))) 821 | 822 | (defun ffip--forward-line (lnum) 823 | "Forward LNUM lines." 824 | (if ffip-debug (message "ffip--forward-line called => %s" lnum)) 825 | (when (and lnum (> lnum 0)) 826 | (goto-char (point-min)) 827 | (forward-line (1- lnum)))) 828 | 829 | (defun ffip-hint () 830 | "Hint." 831 | (let ((root (ffip-get-project-root-directory))) 832 | (format "Find in %s/: " 833 | (file-name-nondirectory (directory-file-name root))))) 834 | 835 | (defun ffip-select-and-open-file (files new-window-p directory-p fn lnum) 836 | "Select and open file from FILES. 837 | If NEW-WINDOW-P is t, create a new window for opened file. 838 | If DIRECTORY-P is t, open directory instead of file. 839 | IF the function FN is not nil, call it after opening the selected file. 840 | After opening the file, forward LNUM lines." 841 | (ffip-completing-read 842 | (ffip-hint) 843 | files 844 | `(lambda (file) 845 | ;; only one item in project files 846 | (if ,directory-p 847 | (if (quote ,new-window-p) 848 | (dired-other-window file) 849 | (switch-to-buffer (dired file))) 850 | ;; open file 851 | (if (quote ,new-window-p) 852 | (find-file-other-window file) 853 | (find-file file)) 854 | ;; goto line if needed 855 | (ffip--forward-line ,lnum) 856 | (if ,fn (funcall ,fn file)))))) 857 | 858 | ;;;###autoload 859 | (defun ffip-find-files (keyword open-another-window &optional find-directory-p fn) 860 | "Use KEYWORD to find files. 861 | If OPEN-ANOTHER-WINDOW is t, the results are displayed in a new window. 862 | If FIND-DIRECTORY-P is t, only search directories. FN is callback. 863 | This function is the API to find files." 864 | (let (cands lnum) 865 | ;; extract line num if exists 866 | (when (and keyword (stringp keyword) 867 | (string-match "^\\(.*\\):\\([0-9]+\\):?$" keyword)) 868 | (setq lnum (string-to-number (match-string 2 keyword))) 869 | (setq keyword (match-string 1 keyword))) 870 | 871 | (setq cands (ffip-project-search keyword find-directory-p)) 872 | (cond 873 | ((> (length cands) 0) 874 | 875 | (unless (eq ffip-find-files-history-max-items 0) 876 | ;; save the history 877 | (when (>= (length ffip-find-files-history) 878 | ffip-find-files-history-max-items) 879 | ;; kick out the oldest item 880 | (setq ffip-find-files-history (butlast ffip-find-files-history))) 881 | ;; add the latest item 882 | (push (list :files cands 883 | :keyword keyword 884 | :directory-p find-directory-p 885 | :function fn 886 | :forward-lines lnum 887 | :default-directory (ffip-get-project-root-directory)) 888 | ffip-find-files-history)) 889 | 890 | (ffip-select-and-open-file cands 891 | open-another-window 892 | find-directory-p 893 | fn 894 | lnum)) 895 | 896 | (t 897 | (message "Nothing found!"))))) 898 | 899 | ;;;###autoload 900 | (defun ffip-find-files-resume (&optional n) 901 | "Resume the last Nth `ffip-find-file' operation. 902 | Please note N is zero originated." 903 | (interactive "P") 904 | (unless n (setq n 0)) 905 | (cond 906 | ((>= n ffip-find-files-history-max-items) 907 | (message "There are only %d items in `ffip-find-files-history'." 908 | ffip-find-files-history-max-items)) 909 | (t 910 | (let* ((item (nth n ffip-find-files-history)) 911 | (default-directory (plist-get item :default-directory))) 912 | (ffip-select-and-open-file (plist-get item :files) 913 | nil 914 | (plist-get item :directory-p) 915 | (plist-get item :function) 916 | (plist-get item :forward-lines)))))) 917 | 918 | (defun ffip--prepare-root-data-for-project-file (root) 919 | "Prepare data for ROOT." 920 | (cons 'ffip-project-root root)) 921 | 922 | (defun ffip--read-selected () 923 | "Read select string." 924 | (buffer-substring-no-properties (region-beginning) (region-end))) 925 | 926 | (defun ffip-read-keyword () 927 | "Read keyword from selected text or user input." 928 | (let* ((hint (if ffip-use-rust-fd "Enter regex (or press ENTER): " 929 | "Enter keyword (or press ENTER): ")) 930 | rlt) 931 | (cond 932 | ((region-active-p) 933 | (push (ffip--read-selected) ffip-filename-history) 934 | (setq rlt (ffip--read-selected))) 935 | (t 936 | (setq rlt (read-from-minibuffer hint nil nil nil 'ffip-filename-history)))) 937 | (if rlt (string-trim rlt) rlt))) 938 | 939 | ;;;###autoload 940 | (defun ffip-create-project-file () 941 | "Create or Append .dir-locals.el to set up per directory. 942 | You can move .dir-locals.el to root directory. 943 | See (info \"(Emacs) Directory Variables\") for details." 944 | (interactive) 945 | (let* ((root (read-directory-name "Project root directory: " default-directory)) 946 | (file (if (and root (file-exists-p root)) 947 | (concat (file-name-as-directory root) ".dir-locals.el")))) 948 | (when file 949 | (with-temp-buffer 950 | (let ((print-level nil) (print-length nil) sexp) 951 | (cond 952 | ;; modify existing .dir-locals.el 953 | ((file-exists-p file) 954 | (let (sub-sexp new-sub-sexp) 955 | (setq sexp (ffip--read-file-text file)) 956 | ;; valid .dir-locals.el 957 | (when sexp 958 | ;; the list for nil 959 | (setq sub-sexp (assoc nil sexp)) 960 | (cond 961 | ;; `(nil (prop1 . val1) (prop2 . val2))' exists 962 | (sub-sexp 963 | ;; remove (ffip-project-root . "/path/file") 964 | (if (assoc 'ffip-project-root sub-sexp) 965 | (setq new-sub-sexp (delete (assoc 'ffip-project-root sub-sexp) sub-sexp)) 966 | (setq new-sub-sexp sub-sexp)) 967 | (push (ffip--prepare-root-data-for-project-file root) new-sub-sexp) 968 | ;; update sexp 969 | (setq sexp (delete sub-sexp sexp)) 970 | (push new-sub-sexp sexp)) 971 | (t 972 | ;; add `(nil (ffip-project-root . "path/file"))' 973 | (push (list nil (ffip--prepare-root-data-for-project-file root)) sexp)))) 974 | )) 975 | (t 976 | ;; a new .dir-locals.el 977 | (setq sexp (list (list nil (ffip--prepare-root-data-for-project-file root)))))) 978 | (when sexp 979 | (insert (format "%S" sexp)) 980 | (write-file file) 981 | (message "%s created." file))))))) 982 | 983 | ;;;###autoload 984 | (defun ffip-current-full-filename-match-pattern-p (regex) 985 | "Is current full file name (including directory) match the REGEX?" 986 | (let* ((dir (if (buffer-file-name) (buffer-file-name) ""))) 987 | (string-match-p regex dir))) 988 | 989 | ;;;###autoload 990 | (defun find-file-in-project (&optional open-another-window) 991 | "More powerful and efficient `find-file-in-project-by-selected' is recommended. 992 | 993 | Prompt with a completing list of all files in the project to find one. 994 | If OPEN-ANOTHER-WINDOW is not nil, the file will be opened in new window. 995 | The project's scope is defined as the first directory containing 996 | a `ffip-project-file' whose value is \".git\" by default. 997 | You can override this by setting the variable `ffip-project-root'." 998 | (interactive "P") 999 | (ffip-find-files nil open-another-window)) 1000 | 1001 | (defun ffip-file-name-relative-p (filename) 1002 | "Is FILENAME relative?" 1003 | (if (string-match-p ffip-relative-path-pattern filename) t)) 1004 | 1005 | (defun ffip-guess-file-name-at-point () 1006 | "Guess file name at point. File name could contain environment variables." 1007 | (let* ((file (or (and (region-active-p) (ffip--read-selected)) 1008 | (thing-at-point 'filename) 1009 | (thing-at-point 'symbol) 1010 | (read-string "No file name at point. Please provide one: ")))) 1011 | (when file 1012 | ;; replace environment variable in file name 1013 | (car (ff-list-replace-env-vars (list file)))))) 1014 | 1015 | (defun ffip--guess-physical-path (file) 1016 | "Return physical full path of FILE which does exist." 1017 | (let* (rlt tmp) 1018 | ;; only deal with file path 1019 | (when (or (file-name-absolute-p file) 1020 | (ffip-file-name-relative-p file)) 1021 | (cond 1022 | ;; file already exists 1023 | ((and (file-exists-p file) 1024 | ;; not directory 1025 | (not (car (file-attributes file)))) 1026 | (setq rlt (file-truename file))) 1027 | 1028 | ;; extra effort for javascript like language 1029 | ;; "./lib/A" could mean "./lib/A.js" or "./lib/A/index.js" 1030 | ((and (or (derived-mode-p 'js-mode) 1031 | (memq major-mode '(typescript-mode)))) 1032 | (dolist (ext '(".ts" ".tsx" ".js" ".jsx")) 1033 | ;; guess physical path 1034 | (cond 1035 | ;; "./lib/A.js" or "./lib/A.ts" 1036 | ((file-exists-p (setq tmp (concat file ext))) 1037 | (setq rlt (file-truename tmp))) 1038 | 1039 | ;; "./lib/A/index.js" or "./lib/A/index.ts" 1040 | ((file-exists-p (setq tmp (concat (file-name-as-directory file) "index" ext))) 1041 | (setq rlt (file-truename tmp)))))))) 1042 | 1043 | rlt)) 1044 | 1045 | ;;;###autoload 1046 | (defun find-file-in-project-at-point (&optional open-another-window) 1047 | "Find file at point. File path could contain environment variables. 1048 | If OPEN-ANOTHER-WINDOW is not nil, the file will be opened in new window." 1049 | (interactive "P") 1050 | (let* ((fn (ffip-guess-file-name-at-point)) 1051 | ;; could be a path 1052 | (ffip-match-path-instead-of-filename t) 1053 | full-path) 1054 | (cond 1055 | (fn 1056 | (cond 1057 | ;; is relative/full path and path is real 1058 | ((setq full-path (ffip--guess-physical-path fn)) 1059 | (if open-another-window (find-file-other-window full-path) 1060 | (find-file full-path))) 1061 | 1062 | ;; absolute path which does not exist 1063 | ((file-name-absolute-p fn) 1064 | ;; search file name only 1065 | (let* ((ffip-match-path-instead-of-filename nil)) 1066 | (ffip-find-files (file-name-nondirectory fn) open-another-window))) 1067 | 1068 | (t 1069 | ;; strip prefix "../../" or "././" from file name 1070 | (ffip-find-files (replace-regexp-in-string ffip-relative-path-pattern "" fn) 1071 | open-another-window)))) 1072 | (t 1073 | (message "No file name is provided."))))) 1074 | 1075 | (defun ffip-parent-directory (level directory) 1076 | "Return LEVEL up parent directory of DIRECTORY." 1077 | (let* ((rlt directory)) 1078 | (while (and (> level 0) (not (string= "" rlt))) 1079 | (setq rlt (file-name-directory (directory-file-name rlt))) 1080 | (setq level (1- level))) 1081 | (if (string= "" rlt) (setq rlt nil)) 1082 | rlt)) 1083 | 1084 | ;;;###autoload 1085 | (defun find-file-in-current-directory (&optional level) 1086 | "Search file in current directory or LEVEL up parent directory." 1087 | (interactive "P") 1088 | (unless level (setq level 0)) 1089 | (let* ((ffip-project-root (ffip-parent-directory level default-directory))) 1090 | (find-file-in-project nil))) 1091 | 1092 | ;;;###autoload 1093 | (defun find-file-in-project-by-selected (&optional open-another-window) 1094 | "Same as `find-file-in-project' but more powerful and faster. 1095 | It use string from selected region to search files in the project. 1096 | If no region is selected, you could provide a keyword. 1097 | 1098 | Keyword could be ANY part of the file's full path and support wildcard. 1099 | For example, to find /home/john/proj1/test.js, below keywords are valid: 1100 | - test.js 1101 | - roj1/tes 1102 | - john*test 1103 | 1104 | If keyword contains line number like \"hello.txt:32\" or \"hello.txt:32:\", 1105 | we will move to that line in opened file. 1106 | 1107 | If keyword is empty, it behaves same as `find-file-in-project'. 1108 | 1109 | If OPEN-ANOTHER-WINDOW is not nil, the file will be opened in new window." 1110 | (interactive "P") 1111 | (ffip-find-files (ffip-read-keyword) open-another-window)) 1112 | 1113 | ;;;###autoload 1114 | (defun ffip-insert-file () 1115 | "Insert contents of file in current buffer. 1116 | The file name is selected interactively from candidates in project." 1117 | (interactive) 1118 | (let* ((cands (ffip-project-search (ffip-read-keyword)))) 1119 | (when (> (length cands) 0) 1120 | (ffip-completing-read (ffip-hint) 1121 | cands 1122 | 'insert-file)))) 1123 | 1124 | ;;;###autoload 1125 | (defun find-file-with-similar-name (&optional open-another-window) 1126 | "Use base name of current file as keyword which could be further stripped. 1127 | by `ffip-strip-file-name-regex'. 1128 | If OPEN-ANOTHER-WINDOW is not nil, the file will be opened in new window." 1129 | (interactive "P") 1130 | (when buffer-file-name 1131 | (let* ((keyword (concat (file-name-base buffer-file-name) ".*") )) 1132 | (if ffip-strip-file-name-regex 1133 | (setq keyword (replace-regexp-in-string ffip-strip-file-name-regex 1134 | "" 1135 | keyword))) 1136 | (ffip-find-files keyword open-another-window)))) 1137 | 1138 | ;;;###autoload 1139 | (defun find-file-in-current-directory-by-selected (&optional open-another-window) 1140 | "Like `find-file-in-project-by-selected' but search current directory. 1141 | If OPEN-ANOTHER-WINDOW is not nil, the file will be opened in new window." 1142 | (interactive "P") 1143 | (let* ((ffip-project-root default-directory)) 1144 | (find-file-in-project-by-selected open-another-window))) 1145 | 1146 | ;;;###autoload 1147 | (defun ffip-find-relative-path(&optional find-directory-p) 1148 | "Find file/directory and copy its relative path into `kill-ring'. 1149 | If FIND-DIRECTORY-P is t, copy the directory path. 1150 | Set `ffip-find-relative-path-callback' to format the result." 1151 | (interactive "P") 1152 | (let* ((cands (ffip-project-search (ffip-read-keyword) find-directory-p))) 1153 | (cond 1154 | ((> (length cands) 0) 1155 | (ffip-completing-read 1156 | (ffip-hint) 1157 | cands 1158 | `(lambda (file) 1159 | ;; only one item in project files 1160 | (if ,find-directory-p 1161 | (setq file (file-name-as-directory file))) 1162 | (setq file (file-relative-name file 1163 | (if buffer-file-name 1164 | (file-name-directory buffer-file-name) 1165 | (expand-file-name default-directory)))) 1166 | (funcall ffip-find-relative-path-callback file)))) 1167 | (t 1168 | (message "Nothing found!"))))) 1169 | 1170 | ;;;###autoload 1171 | (defun find-directory-in-project-by-selected (&optional open-another-window) 1172 | "Similar to `find-file-in-project-by-selected'. 1173 | Use string from selected region to find directory in the project. 1174 | If no region is selected, you need provide keyword. 1175 | 1176 | Keyword could be directory's base-name only or parent-directory+base-name 1177 | For example, to find /home/john/proj1/test, below keywords are valid: 1178 | - test 1179 | - roj1/test 1180 | - john*test 1181 | 1182 | If OPEN-ANOTHER-WINDOW is not nil, the file will be opened in new window." 1183 | (interactive "P") 1184 | (ffip-find-files (ffip-read-keyword) open-another-window t)) 1185 | 1186 | (defun ffip--prune-patterns-regex () 1187 | "Convert `ffip--prune-patterns-regex to regex." 1188 | (let* ((rlt (mapconcat 'identity ffip-prune-patterns "\\|"))) 1189 | (setq rlt (replace-regexp-in-string "\\." "\\\\." rlt)) 1190 | (setq rlt (replace-regexp-in-string "\\*" ".*" rlt)) 1191 | ;; file name or directory name 1192 | (concat rlt "\\($\\|/\\)" ))) 1193 | 1194 | ;;;###autoload 1195 | (defun ffip-lisp-find-file-in-project (&optional directory-p) 1196 | "If DIRECTORY-P is nil, find file in project, or else find directory. 1197 | This command works in any environment (Windows, etc) out of box." 1198 | (interactive "P") 1199 | (let* ((root (ffip-get-project-root-directory)) 1200 | (input-regex (read-string "Input regex (or press ENTER): ")) 1201 | (find-lisp-regexp (if (string= input-regex "") ".*" input-regex)) 1202 | cands 1203 | (ignored-regex (ffip--prune-patterns-regex))) 1204 | (cond 1205 | (directory-p 1206 | (setq cands (find-lisp-find-files-internal 1207 | root 1208 | 'find-lisp-file-predicate-is-directory 1209 | 'find-lisp-default-directory-predicate))) 1210 | (t 1211 | (setq cands (find-lisp-find-files-internal 1212 | root 1213 | 'find-lisp-default-file-predicate 1214 | 'find-lisp-default-directory-predicate)))) 1215 | (setq cands 1216 | (delq nil 1217 | (mapcar `(lambda (c) 1218 | (unless (string-match ,ignored-regex c) c)) 1219 | cands))) 1220 | (ffip-completing-read 1221 | (format "%s %s: " (if directory-p "directories" "files") root) 1222 | cands 1223 | `(lambda (file) 1224 | (if ,directory-p 1225 | (switch-to-buffer (dired file)) 1226 | (find-file file)))))) 1227 | 1228 | ;;;###autoload 1229 | (defalias 'ffip 'find-file-in-project) 1230 | ;;;###autoload 1231 | (defalias 'find-relative-path 'ffip-find-relative-path) 1232 | 1233 | (defun ffip-path (candidate) 1234 | "Get path from CANDIDATE." 1235 | (let* ((default-directory (ffip-project-root))) 1236 | (file-truename (if (consp candidate) (cdr candidate) candidate)))) 1237 | 1238 | ;;;###autoload 1239 | (defun ffip-diff-quit () 1240 | "Quit." 1241 | (interactive) 1242 | ;; kill buffer instead of bury it 1243 | (quit-window t)) 1244 | 1245 | ;;;###autoload 1246 | (defun ffip-diff-find-file (&optional open-another-window) 1247 | "File file(s) in current hunk. 1248 | If OPEN-ANOTHER-WINDOW is not nil, the file will be opened in new window." 1249 | (interactive "P") 1250 | (let* ((files (mapcar (lambda (f) (replace-regexp-in-string "^[^/]*/" "" f)) (diff-hunk-file-names))) 1251 | (alnum 0) 1252 | (blnum 0) 1253 | (regex "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?") 1254 | (ffip-match-path-instead-of-filename (not ffip-diff-find-file-by-file-name-p)) 1255 | keyword) 1256 | 1257 | (save-excursion 1258 | (diff-beginning-of-hunk t) 1259 | (when (looking-at regex) 1260 | (setq alnum (string-to-number (match-string 1))) 1261 | (setq blnum (string-to-number (match-string 3))))) 1262 | 1263 | (cond 1264 | ((or (null files) (eq (length files) 0)) 1265 | (message "No file is found!")) 1266 | 1267 | ;; file A and file B have the same name 1268 | ((or (and (> (length files) 1) 1269 | (string= (nth 0 files) (nth 1 files))) 1270 | (eq (length files) 1)) 1271 | (setq keyword (nth 0 files)) 1272 | (when ffip-diff-find-file-by-file-name-p 1273 | (setq keyword (file-name-nondirectory keyword))) 1274 | 1275 | (ffip-find-files keyword 1276 | open-another-window 1277 | nil 1278 | `(lambda (opened-file) 1279 | ;; use line number in new file since there 1280 | ;; is only one file name candidate 1281 | (ffip--forward-line ,blnum)))) 1282 | 1283 | (t 1284 | (unless (setq keyword (run-hook-with-args 'ffip-diff-find-file-before-hook 1285 | (nth 0 files) 1286 | (nth 1 files))) 1287 | ;; pick a file name from A and B 1288 | (setq keyword (cond 1289 | ((string= (nth 0 files) "null") 1290 | (nth 1 files)) 1291 | (t 1292 | (nth 0 files))))) 1293 | (when ffip-diff-find-file-by-file-name-p 1294 | (setq keyword (file-name-nondirectory keyword))) 1295 | 1296 | (ffip-find-files keyword 1297 | open-another-window 1298 | nil 1299 | (lambda (opened-file) 1300 | (cond 1301 | ((string= (file-name-nondirectory opened-file) (nth 0 files)) 1302 | (ffip--forward-line alnum)) 1303 | (t 1304 | (ffip--forward-line blnum))))))))) 1305 | 1306 | (defvar ffip-diff-mode-map 1307 | (let ((map (make-sparse-keymap))) 1308 | (set-keymap-parent map diff-mode-map) 1309 | (define-key map (kbd "C-c C-k") 'ffip-diff-filter-hunks-by-file-name) 1310 | (define-key map [remap diff-goto-source] 'ffip-diff-find-file) 1311 | map) 1312 | "Mode map based on `diff-mode-map'.") 1313 | 1314 | ;;;###autoload 1315 | (define-derived-mode ffip-diff-mode diff-mode "ffip" 1316 | "Show diff/patch." 1317 | (setq buffer-read-only t) 1318 | (setq truncate-lines t) 1319 | (use-local-map ffip-diff-mode-map)) 1320 | 1321 | (defun ffip-show-content-in-diff-mode (content) 1322 | "Insert CONTENT into *ffip-diff* buffer." 1323 | (cond 1324 | ((and content (not (string= content ""))) 1325 | (let (rlt-buf) 1326 | (if (get-buffer "*ffip-diff*") 1327 | (kill-buffer "*ffip-diff*")) 1328 | (setq rlt-buf (get-buffer-create "*ffip-diff*")) 1329 | (save-current-buffer 1330 | (switch-to-buffer-other-window rlt-buf) 1331 | (set-buffer rlt-buf) 1332 | (erase-buffer) 1333 | (insert content) 1334 | (ffip-diff-mode) 1335 | (goto-char (point-min))))) 1336 | (t 1337 | (message "Output is empty!")))) 1338 | 1339 | (defun ffip-diff-execute-backend (backend) 1340 | "Execute BACKEND." 1341 | (if backend 1342 | (cond 1343 | ;; shell command 1344 | ((stringp backend) 1345 | (ffip-show-content-in-diff-mode (shell-command-to-string backend))) 1346 | ;; command 1347 | ((functionp backend) 1348 | (ffip-show-content-in-diff-mode (funcall backend))) 1349 | ;; lisp expression 1350 | ((consp backend) 1351 | (ffip-show-content-in-diff-mode (funcall `(lambda () ,backend))))))) 1352 | 1353 | (defun ffip-backend-description (backend) 1354 | "Get BACKEND description." 1355 | (let* (rlt) 1356 | (cond 1357 | ;; shell command 1358 | ((stringp backend) 1359 | (setq rlt backend)) 1360 | ;; command 1361 | ((functionp backend) 1362 | (setq rlt (symbol-name backend))) 1363 | ;; lisp expression 1364 | ((consp backend) 1365 | ;; (cons "description" actual-backend) 1366 | (if (stringp (car backend)) 1367 | (setq rlt (car backend)) 1368 | (setq rlt "unknown")))) 1369 | rlt)) 1370 | 1371 | ;;;###autoload 1372 | (defun ffip-show-diff-internal (&optional num) 1373 | "Show the diff output by executing selected `ffip-diff-backends'. 1374 | NUM is the index selected backend from `ffip-diff-backends'. 1375 | NUM is zero based whose default value is zero." 1376 | (interactive "P") 1377 | (cond 1378 | ((or (not num) (< num 0)) 1379 | (setq num 0)) 1380 | ((> num (length ffip-diff-backends)) 1381 | (setq num (1- (length ffip-diff-backends))))) 1382 | 1383 | (let* ((backend (nth num ffip-diff-backends))) 1384 | (if (and (consp backend) 1385 | (stringp (car backend))) 1386 | (setq backend (cdr backend))) 1387 | (ffip-diff-execute-backend backend))) 1388 | 1389 | ;;;###autoload 1390 | (defun ffip-show-diff-by-description (&optional num) 1391 | "Show the diff output by executing selected `ffip-diff-backends'. 1392 | NUM is the backend index of `ffip-diff-backends'. 1393 | If NUM is not nil, the corresponding backend is executed directly." 1394 | (interactive "P") 1395 | (cond 1396 | (num 1397 | (ffip-show-diff-internal num)) 1398 | (t 1399 | (let* (descriptions 1400 | (i 0)) 1401 | ;; format backend descriptions 1402 | (dolist (b ffip-diff-backends) 1403 | (push (format "%s: %s" i (ffip-backend-description b)) descriptions) 1404 | (setq i (+ 1 i))) 1405 | (ffip-completing-read 1406 | "Run diff backend: " 1407 | (nreverse descriptions) 1408 | (lambda (file) 1409 | (if (string-match "^\\([0-9]+\\): " file) 1410 | (ffip-show-diff-internal (string-to-number (match-string 1 file)))))))))) 1411 | 1412 | ;;;###autoload 1413 | (defalias 'ffip-show-diff 'ffip-show-diff-by-description) 1414 | 1415 | (defun ffip-read-file-name-hack (orig-func &rest args) 1416 | "Advice `read-file-name' with ORIG-FUNC and ARGS for `ffip-diff-apply-hunk'." 1417 | (cond 1418 | (ffip-read-file-name-hijacked-p 1419 | ;; only hack read-file-name once 1420 | (setq ffip-read-file-name-hijacked-p nil) 1421 | (let* ((file-name (file-name-nondirectory (nth 2 args))) 1422 | (default-directory (ffip-project-root)) 1423 | (cands (ffip-project-search file-name)) 1424 | (rlt (if cands (ffip-completing-read "Files: " cands)))) 1425 | (message "file-name=%s" file-name) 1426 | (when rlt 1427 | (setq rlt (file-truename rlt)) 1428 | (run-hook-with-args 'ffip-diff-apply-hunk-hook rlt) 1429 | rlt))) 1430 | (t 1431 | (apply orig-func args)))) 1432 | (advice-add 'read-file-name :around #'ffip-read-file-name-hack) 1433 | 1434 | ;;;###autoload 1435 | (defun ffip-diff-apply-hunk (&optional reverse) 1436 | "Apply current hunk in `diff-mode'. Try to locate the file to patch. 1437 | Similar to `diff-apply-hunk' but smarter. 1438 | Please read documentation of `diff-apply-hunk' to get more details. 1439 | If REVERSE is t, applied patch is reverted." 1440 | (interactive "P") 1441 | (cond 1442 | ((derived-mode-p 'diff-mode) 1443 | (setq ffip-read-file-name-hijacked-p t) 1444 | (diff-apply-hunk reverse) 1445 | (setq ffip-read-file-name-hijacked-p nil)) 1446 | (t 1447 | (message "This command only run in `diff-mode' and `ffip-diff-mode'.")))) 1448 | 1449 | (defun ffip-diff-hunk-file-name-match-p (keyword neg-keywords) 1450 | "Current hunk's file name does match KEYWORD and doesn't NEG-KEYWORDS." 1451 | (let* ((filenames (diff-hunk-file-names)) 1452 | (f0 (nth 0 filenames)) 1453 | (f1 (nth 1 filenames)) 1454 | rlt) 1455 | (when (and filenames (> (length filenames) 1)) 1456 | (setq rlt (or (string-match keyword f0) (string-match keyword f1))) 1457 | 1458 | (let ((i 0) nk) 1459 | (while (and rlt (< i (length neg-keywords))) 1460 | (setq nk (nth i neg-keywords)) 1461 | (setq rlt (not (or (string-match nk f0) (string-match nk f1)))) 1462 | (setq i (1+ i))))) 1463 | rlt)) 1464 | 1465 | ;;;###autoload 1466 | (defun ffip-diff-filter-hunks-by-file-name () 1467 | "Filter hunks by file names which are generated from user input patterns. 1468 | E.g., \"regex !exclude1 exclude1\" means the hunk's file name should 1469 | match \"regex\", but should not match \"exclude1\" or \"exclude2\". 1470 | Please note in \"regex\", space represents any string." 1471 | (interactive) 1472 | (cond 1473 | ((derived-mode-p 'diff-mode) 1474 | (let* ((pattern (read-string "File pattern (e.g., \"regex !exclude1 exclude2\"): ")) 1475 | arr 1476 | keyword 1477 | neg-keywords 1478 | (first-hunk-position (save-excursion 1479 | (goto-char (point-min)) 1480 | (re-search-forward diff-hunk-header-re) 1481 | (line-end-position)))) 1482 | (cond 1483 | ((and pattern (not (string= pattern ""))) 1484 | (setq arr (split-string pattern "!")) 1485 | (setq keyword (string-trim (nth 0 arr))) 1486 | (when (> (length arr) 1) 1487 | (setq neg-keywords (split-string (string-trim (nth 1 arr)) " +"))) 1488 | 1489 | ;; kill from the bottom to the top 1490 | (goto-char (point-max)) 1491 | (condition-case nil 1492 | (while (not (and (<= (point) first-hunk-position) 1493 | (ffip-diff-hunk-file-name-match-p keyword neg-keywords))) 1494 | (cond 1495 | ((not (ffip-diff-hunk-file-name-match-p keyword neg-keywords)) 1496 | (diff-file-kill)) 1497 | (t 1498 | (diff-file-prev)))) 1499 | (error nil))) 1500 | (t 1501 | (message "File kill pattern should not be empty"))))) 1502 | (t 1503 | (message "This command only run in `diff-mode' and `ffip-diff-mode'.")))) 1504 | 1505 | ;;;###autoload 1506 | (defun ffip-fix-file-path-at-point (&optional absolute-path-p) 1507 | "Fix file path at point. File path could contain environment variables. 1508 | If ABSOLUTE-PATH-P is t, old path is replaced by correct absolute path. 1509 | Or else it's replaced by relative path." 1510 | (interactive "P") 1511 | (let* ((filename (ffip-guess-file-name-at-point)) 1512 | full-path 1513 | cands) 1514 | (cond 1515 | ((not filename) 1516 | (message "There is no file path at point.")) 1517 | 1518 | ;; path at point is a path of physical file 1519 | ((setq full-path (ffip--guess-physical-path filename)) 1520 | nil) 1521 | 1522 | ;; find a file 1523 | ((setq cands 1524 | (ffip-project-search (replace-regexp-in-string ffip-relative-path-pattern 1525 | "" 1526 | filename))) 1527 | (cond 1528 | ((eq (length cands) 1) 1529 | (setq full-path (nth 0 cands))) 1530 | (t 1531 | (setq full-path (completing-read "Find file: " cands)))))) 1532 | 1533 | (when full-path 1534 | (if (consp full-path) (setq full-path (cdr full-path))) 1535 | 1536 | ;; convert to actual full path 1537 | (let* ((default-directory (ffip-get-project-root-directory))) 1538 | (setq full-path (file-truename full-path))) 1539 | 1540 | (let* ((bounds (bounds-of-thing-at-point 'filename)) 1541 | (path (if absolute-path-p full-path 1542 | (file-relative-name full-path 1543 | ;; compare with current file's directory if possible 1544 | (and buffer-file-name (file-name-directory buffer-file-name)))))) 1545 | (goto-char (car bounds)) 1546 | (delete-region (car bounds) (cdr bounds)) 1547 | (insert (replace-regexp-in-string "/index\\.[jt]s$" "" path)))))) 1548 | 1549 | ;; safe locals 1550 | (progn 1551 | (put 'ffip-diff-backends 'safe-local-variable 'listp) 1552 | (put 'ffip-patterns 'safe-local-variable 'listp) 1553 | (put 'ffip-prune-patterns 'safe-local-variable 'listp) 1554 | (put 'ffip-ignore-filenames 'safe-local-variable 'listp) 1555 | (put 'ffip-match-path-instead-of-filename 'safe-local-variable 'booleanp) 1556 | (put 'ffip-project-file 'safe-local-variable 'stringp) 1557 | (put 'ffip-strip-file-name-regex 'safe-local-variable 'stringp) 1558 | (put 'ffip-project-root 'safe-local-variable 'stringp)) 1559 | 1560 | (provide 'find-file-in-project) 1561 | ;;; find-file-in-project.el ends here 1562 | -------------------------------------------------------------------------------- /pkg.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | name=find-file-in-project 3 | version=6.2.3 4 | pkg=$name-$version 5 | mkdir $pkg 6 | cp *.el $pkg 7 | cat << EOF > $pkg/$name-pkg.el 8 | (define-package "$name" "$version" 9 | "whatever") 10 | EOF 11 | if [[ `uname -s` == *Darwin* ]]; then 12 | COPYFILE_DISABLE="" tar cvf $pkg.tar $pkg/ 13 | else 14 | tar cvf $pkg.tar $pkg/ 15 | fi 16 | rm -rf $pkg/ 17 | -------------------------------------------------------------------------------- /tests/dummy.diff: -------------------------------------------------------------------------------- 1 | commit 52274e6001545bdf45c6477ba21bfaa8eca04755 2 | Author: Chen Bin 3 | Date: Sun Sep 26 18:51:07 2021 +1000 4 | 5 | fixed ci 6 | 7 | diff --git a/tests/ffip-tests.el b/tests/ffip-tests.el 8 | index a32d6bf..0e073bd 100644 9 | --- a/tests/ffip-tests.el 10 | +++ b/tests/ffip-tests.el 11 | @@ -145,7 +145,4 @@ 12 | ;; relative path 13 | (should (string= "tests/git-diff.diff" (buffer-string)))))) 14 | 15 | -(ert-deftest ffip-test-git-commands () 16 | - (should (string-match "master" (car (ffip-diff-git-versions))))) 17 | - 18 | (ert-run-tests-batch-and-exit) 19 | -------------------------------------------------------------------------------- /tests/emacs-init.el: -------------------------------------------------------------------------------- 1 | (require 'find-file-in-project) 2 | (require 'ivy) 3 | (ivy-mode 1) 4 | (setq ffip-match-path-instead-of-filename t) 5 | (run-with-idle-timer 6 | 1 7 | nil 8 | (lambda () 9 | (erase-buffer) 10 | (goto-char (point-min)) 11 | (insert 12 | ";; Setup of this demo,\n" 13 | "(setq ffip-match-path-instead-of-filename t)\n\n\n" 14 | ";; Run \"M-x find-file-in-project-by-selected\" and input search keyword \"el\" or \"tests\".\n\n\n" 15 | ";; Move cursor above below paths and run \"M-x find-file-in-project-at-point\",\n\n" 16 | ";; tests/ffip-tests.el ; open file directly \n" 17 | ";; find-file-in-project.el:50 ; open file and jump to line 50\n"))) 18 | -------------------------------------------------------------------------------- /tests/ffip-tests.el: -------------------------------------------------------------------------------- 1 | ;; ffip-tests.el --- unit tests for find-file-in-project -*- coding: utf-8 -*- 2 | 3 | ;; Author: Chen Bin 4 | 5 | ;;; License: 6 | 7 | ;; This file is not part of GNU Emacs. 8 | 9 | ;; This program is free software; you can redistribute it and/or modify 10 | ;; it under the terms of the GNU General Public License as published by 11 | ;; the Free Software Foundation; either version 2, or (at your option) 12 | ;; any later version. 13 | 14 | ;; This program is distributed in the hope that it will be useful, 15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | ;; GNU General Public License for more details. 18 | 19 | ;; You should have received a copy of the GNU General Public License 20 | ;; along with this program; if not, write to the Free Software 21 | ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 | 23 | ;;; Commentary: 24 | 25 | ;;; Code: 26 | 27 | (require 'ert) 28 | (require 'find-file-in-project) 29 | 30 | (defun get-full-path (filename) 31 | "Get full path of FILENAME in current directory." 32 | (concat 33 | (if load-file-name (file-name-directory load-file-name) default-directory) 34 | filename)) 35 | 36 | (ert-deftest ffip-test-find-by-selected () 37 | (let (files) 38 | (setq ffip-project-root default-directory) 39 | (setq files (mapcar 'car (ffip-project-search "git-diff"))) 40 | ;; (message "files=%s" files) 41 | (should (string-match-p "git-diff.diff" (car files))))) 42 | 43 | 44 | (ert-deftest ffip-test-ffip () 45 | (let (files) 46 | (setq ffip-project-root default-directory) 47 | (setq files (mapcar 'car (ffip-project-search nil))) 48 | (should (> (length files) 1)) 49 | (should (not (active-minibuffer-window))))) 50 | 51 | (ert-deftest ffip-test-ffip-open-another () 52 | (let (files 53 | (prefix-args '(4 (4)))) 54 | (dolist (open-another-arg prefix-args) 55 | (setq ffip-project-root default-directory) 56 | (setq files (mapcar 'car (ffip-project-search "git-diff"))) 57 | (should (= (length files) 1)) 58 | (should (not (active-minibuffer-window)))))) 59 | 60 | (ert-deftest ffip-test-ffip-show-diff () 61 | (let* (files 62 | (ffip-diff-backends '((with-temp-buffer 63 | (insert-file-contents (get-full-path "git-diff.diff")) 64 | (buffer-string)))) 65 | ;; see https://github.com/redguardtoo/find-file-in-project/issues/137 66 | ;; debian package creates some extra diff in parent directory "tests/" 67 | ;; So root directory should be set to "tests/" 68 | (ffip-diff-find-file-by-file-name-p t) 69 | (ffip-project-root (file-name-directory load-file-name))) 70 | (ffip-show-diff) 71 | (switch-to-buffer "*ffip-diff*") 72 | (goto-char (point-min)) 73 | (diff-file-next) 74 | ;; find now 75 | (ffip-diff-find-file) 76 | (should (string= (file-name-nondirectory (buffer-file-name)) "ffip-tests.el")) 77 | 78 | ;; move to the second file hunk 79 | (switch-to-buffer "*ffip-diff*") 80 | (diff-file-next) 81 | ;; find file in the first diff hunk now 82 | (ffip-diff-find-file) 83 | (should (string= (file-name-nondirectory (buffer-file-name)) "git-diff.diff")) 84 | ;; cleanup 85 | (kill-buffer "*ffip-diff*"))) 86 | 87 | (ert-deftest ffip-test-ffip-parent-directory () 88 | (let* ((dir "/home/cb/projs/find-file-in-project/")) 89 | (should (string= (ffip-parent-directory 0 dir) dir)) 90 | (should (string= (ffip-parent-directory 1 dir) "/home/cb/projs/")) 91 | (should (string= (ffip-parent-directory 2 dir) "/home/cb/")) 92 | (should (string= (ffip-parent-directory 3 dir) "/home/")) 93 | (should (string= (ffip-parent-directory 4 dir) "/")) 94 | (should (string= (ffip-parent-directory 999 dir) "/")))) 95 | 96 | (ert-deftest ffip-test-guess-physical-path () 97 | (let* (fn 98 | (default-directory (file-name-directory (or load-file-name buffer-file-name)))) 99 | (with-temp-buffer 100 | (insert "import './test1';\n") 101 | (insert "import './test2';\n") 102 | (js-mode) ; javascript 103 | 104 | ;; detect "test1.ts" 105 | (goto-char (point-min)) 106 | (search-forward "test1") 107 | (setq fn (ffip-guess-file-name-at-point)) 108 | (should (string= fn "./test1")) 109 | (should (string= (ffip--guess-physical-path fn) (file-truename "./test1.ts"))) 110 | 111 | ;; detect "test2.js" 112 | (goto-char (point-min)) 113 | (search-forward "test2") 114 | (setq fn (ffip-guess-file-name-at-point)) 115 | (should (string= fn "./test2")) 116 | (should (string= (ffip--guess-physical-path fn) (file-truename "./test2.js")))) 117 | )) 118 | 119 | (ert-deftest ffip-test-completing-read () 120 | (should (eq (ffip-completing-read "hint:" '(a)) 'a)) 121 | (should (eq (ffip-completing-read "hint:" '((a . b))) 'b)) 122 | (ffip-completing-read "hint:" 123 | '("a") 124 | (lambda (selected) (should (string= selected "a"))))) 125 | 126 | (ert-deftest ffip-test-ido () 127 | (should (boundp 'ffip-prefer-ido-mode)) 128 | (should (not ffip-prefer-ido-mode))) 129 | 130 | (ert-deftest ffip-test-windows () 131 | (if (eq system-type 'windows-nt) 132 | (should (executable-find (ffip--guess-gnu-find))) 133 | (message "NOT windows native Emacs, nothing to test.") 134 | (should t))) 135 | 136 | (ert-deftest ffip-test-relative-path-commands () 137 | (with-temp-buffer 138 | (let* (orig-pos) 139 | (insert (get-full-path "git-diff.diff")) 140 | (goto-char 5) 141 | (should (file-exists-p (buffer-string))) 142 | ;; absolute path 143 | (should (not (string= "tests/git-diff.diff" (buffer-string)))) 144 | (ffip-fix-file-path-at-point) 145 | ;; relative path 146 | (should (string= "tests/git-diff.diff" (buffer-string)))))) 147 | 148 | (ert-run-tests-batch-and-exit) 149 | -------------------------------------------------------------------------------- /tests/git-diff.diff: -------------------------------------------------------------------------------- 1 | diff --git a/tests/ffip-tests.el b/tests/ffip-tests.el 2 | index a54fd6d..0c4738d 100644 3 | --- a/tests/ffip-tests.el 4 | +++ b/tests/ffip-tests.el 5 | @@ -37,6 +37,20 @@ 6 | (setq files (mapcar 'car (ffip-project-search nil nil))) 7 | (should (> (length files) 1)) 8 | (should (not (active-minibuffer-window))) 9 | + 10 | + (setq ivy-read-called nil) 11 | + ;; ffip will call ivy by default 12 | + (ffip-find-files nil nil) 13 | + (should ivy-read-called))) 14 | + 15 | +(ert-deftest ffip-ffip-show-diff () 16 | + (let (files 17 | + (ffip-diff-backends '((let (str "") 18 | + str)))) 19 | + (setq ffip-project-root default-directory) 20 | + (setq files (mapcar 'car (ffip-project-search nil nil))) 21 | + (should (> (length files) 1)) 22 | + (should (not (active-minibuffer-window))) 23 | (setq ivy-read-called nil) 24 | ;; ffip will call ivy by default 25 | (ffip-find-files nil nil) 26 | diff --git a/tests/git-diff.diff b/tests/git-diff.diff 27 | index bacd2be..3a4d83d 100644 28 | --- a/tests/git-diff.diff 29 | +++ b/tests/git-diff.diff 30 | @@ -9,6 +9,6 @@ 31 | history preselect keymap update-fn sort 32 | action unwind re-builder matcher dynamic-collection caller) 33 | (setq ivy-read-called t) 34 | - (message "ivy-read mockup is called")) 35 | + (message "`ivy-read' mockup is called")) 36 | (provide 'ivy) 37 | \ No newline at end of file 38 | -------------------------------------------------------------------------------- /tests/my-byte-compile.el: -------------------------------------------------------------------------------- 1 | ;;; my-byte-compile.el --- syntax check the code -*- lexical-binding: t -*- 2 | 3 | ;; Copyright (C) 2022 Chen Bin 4 | ;; 5 | ;; Author: Chen Bin 6 | 7 | ;; This file is NOT part of GNU Emacs. 8 | 9 | ;; This program is free software; you can redistribute it and/or modify 10 | ;; it under the terms of the GNU General Public License as published by 11 | ;; the Free Software Foundation; either version 3, or (at your option) 12 | ;; any later version. 13 | 14 | ;; This program is distributed in the hope that it will be useful, 15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | ;; GNU General Public License for more details. 18 | 19 | ;; You should have received a copy of the GNU General Public License 20 | ;; along with this program; if not, see . 21 | 22 | ;;; Commentary: 23 | ;; Syntax check the code. It's used in Emacs cli. 24 | ;; 25 | 26 | ;;; Code: 27 | 28 | (require 'find-lisp) 29 | 30 | (let ((files (find-lisp-find-files-internal 31 | "." 32 | (lambda (file dir) 33 | (and (not (file-directory-p (expand-file-name file dir))) 34 | (string-match "\\.el$" file) 35 | (not (string-match "\\.dir-locals\\.el" file)))) 36 | (lambda (dir parent) 37 | (not (or (member dir '("." ".." ".git" ".svn" "deps" "tests")) 38 | (file-symlink-p (expand-file-name dir parent)))))))) 39 | (dolist (file files) 40 | (byte-compile-file file))) 41 | 42 | ;;; my-byte-compile.el ends here 43 | -------------------------------------------------------------------------------- /tests/test1.ts: -------------------------------------------------------------------------------- 1 | console.log('hello world'); 2 | -------------------------------------------------------------------------------- /tests/test2.js: -------------------------------------------------------------------------------- 1 | console.log('hello world'); 2 | --------------------------------------------------------------------------------