Repository: alphapapa/plz.el
Branch: master
Commit: e2d07838e3b6
Files: 9
Total size: 164.0 KB
Directory structure:
gitextract_vkmmx439/
├── .elpaignore
├── .github/
│ └── workflows/
│ └── test.yml
├── .gitignore
├── LICENSE
├── Makefile
├── README.org
├── makem.sh
├── plz.el
└── tests/
└── test-plz.el
================================================
FILE CONTENTS
================================================
================================================
FILE: .elpaignore
================================================
.elpaignore
Makefile
makem.sh
NOTES.org
.github/
images/
tests/
README.info
================================================
FILE: .github/workflows/test.yml
================================================
# * test.yml --- Test Emacs packages using makem.sh on GitHub Actions
# URL: https://github.com/alphapapa/makem.sh
# Version: 0.6-pre
# * Commentary:
# Based on Steve Purcell's examples at
# <https://github.com/purcell/setup-emacs/blob/master/.github/workflows/test.yml>,
# <https://github.com/purcell/package-lint/blob/master/.github/workflows/test.yml>.
# * License:
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 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
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# * Code:
name: "CI"
on:
pull_request:
push:
# Comment out this section to enable testing of all branches.
# branches:
# - master
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
emacs_version:
- 27.1
- 27.2
- 28.1
- 28.2
- 29.1
- 29.2
- 29.3
- snapshot
steps:
- uses: purcell/setup-emacs@master
with:
version: ${{ matrix.emacs_version }}
- uses: actions/checkout@v2
- name: Install Ispell
run: |
sudo apt-get install ispell
- name: Initialize sandbox
run: |
SANDBOX_DIR=$(mktemp -d) || exit 1
echo "SANDBOX_DIR=$SANDBOX_DIR" >> $GITHUB_ENV
./makem.sh -vv --sandbox=$SANDBOX_DIR --install-deps --install-linters
- name: Run httpbin with Docker
run: docker run -d -p 80:80 -P kennethreitz/httpbin
# The "all" rule is not used, because it treats compilation warnings
# as failures, so linting and testing are run as separate steps.
- name: Lint
# NOTE: Uncomment this line to treat lint failures as passing so
# the job doesn't show failure. (Enabled for now because
# Emacs 29 indents some cl- forms differently, which
# causes lint-indent to fail, and what matters most is
# that the tests pass.)
continue-on-error: true
run: ./makem.sh -vv --sandbox=$SANDBOX_DIR lint
- name: Test
if: always() # Run test even if linting fails.
run: ./makem.sh -vv --sandbox=$SANDBOX_DIR test
# Local Variables:
# eval: (outline-minor-mode)
# End:
================================================
FILE: .gitignore
================================================
*.elc
/scratch.el
.#*
/sandbox/
/.sandbox/
/worktrees/
================================================
FILE: LICENSE
================================================
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
================================================
FILE: Makefile
================================================
# * makem.sh/Makefile --- Script to aid building and testing Emacs Lisp packages
# URL: https://github.com/alphapapa/makem.sh
# Version: 0.5
# * Arguments
# For consistency, we use only var=val options, not hyphen-prefixed options.
# NOTE: I don't like duplicating the arguments here and in makem.sh,
# but I haven't been able to find a way to pass arguments which
# conflict with Make's own arguments through Make to the script.
# Using -- doesn't seem to do it.
ifdef install-deps
INSTALL_DEPS = "--install-deps"
endif
ifdef install-linters
INSTALL_LINTERS = "--install-linters"
endif
ifdef sandbox
ifeq ($(sandbox), t)
SANDBOX = --sandbox
else
SANDBOX = --sandbox=$(sandbox)
endif
endif
ifdef debug
DEBUG = "--debug"
endif
# ** Verbosity
# Since the "-v" in "make -v" gets intercepted by Make itself, we have
# to use a variable.
verbose = $(v)
ifneq (,$(findstring vvv,$(verbose)))
VERBOSE = "-vvv"
else ifneq (,$(findstring vv,$(verbose)))
VERBOSE = "-vv"
else ifneq (,$(findstring v,$(verbose)))
VERBOSE = "-v"
endif
# * Rules
# TODO: Handle cases in which "test" or "tests" are called and a
# directory by that name exists, which can confuse Make.
%:
@./makem.sh $(DEBUG) $(VERBOSE) $(SANDBOX) $(INSTALL_DEPS) $(INSTALL_LINTERS) $(@)
.DEFAULT: init
init:
@./makem.sh $(DEBUG) $(VERBOSE) $(SANDBOX) $(INSTALL_DEPS) $(INSTALL_LINTERS)
================================================
FILE: README.org
================================================
#+TITLE: plz.el
#+PROPERTY: LOGGING nil
# NOTE: It would be preferable to put these at the bottom of the file under the export options heading, but it seems that "TEXINFO_DIR_CATEGORY" only works at the top of the file.
#+EXPORT_FILE_NAME: plz.texi
#+TEXINFO_DIR_CATEGORY: Emacs
#+TEXINFO_DIR_TITLE: Plz: (plz)
#+TEXINFO_DIR_DESC: HTTP library using Curl as a backend
# Note: This readme works with the org-make-toc <https://github.com/alphapapa/org-make-toc> package, which automatically updates the table of contents.
[[http://elpa.gnu.org/packages/plz.html][file:http://elpa.gnu.org/packages/plz.svg]]
#+HTML: <img src="images/mascot.png" align="right">
~plz~ is an HTTP library for Emacs. It uses ~curl~ as a backend, which avoids some of the issues with using Emacs's built-in ~url~ library. It supports both synchronous and asynchronous requests. Its API is intended to be simple, natural, and expressive. Its code is intended to be simple and well-organized. Every feature is tested against [[https://httpbin.org/][httpbin]].
* Contents :noexport:
:PROPERTIES:
:TOC: :include siblings
:END:
:CONTENTS:
- [[#installation][Installation]]
- [[#usage][Usage]]
- [[#examples][Examples]]
- [[#functions][Functions]]
- [[#queueing][Queueing]]
- [[#changelog][Changelog]]
- [[#credits][Credits]]
- [[#development][Development]]
- [[#copyright-assignment][Copyright assignment]]
:END:
* Installation
:PROPERTIES:
:TOC: :depth 0
:END:
** GNU ELPA
~plz~ is available in [[http://elpa.gnu.org/packages/plz.html][GNU ELPA]]. It may be installed in Emacs using the ~package-install~ command.
** Manual
~plz~ has no dependencies other than Emacs and ~curl~. It's known to work on Emacs 26.3 or later. To install it manually, simply place =plz.el= in your ~load-path~ and ~(require 'plz)~.
* Usage
:PROPERTIES:
:TOC: :depth 1
:END:
The main public function is ~plz~, which sends an HTTP request and returns either the result of the specified type (for a synchronous request), or the ~curl~ process object (for asynchronous requests). For asynchronous requests, callback, error-handling, and finalizer functions may be specified, as well as various other options.
** Examples
Synchronously =GET= a URL and return the response body as a decoded string (here, raw JSON):
#+BEGIN_SRC elisp :exports both :results value code :cache yes
(plz 'get "https://httpbin.org/user-agent")
#+END_SRC
#+RESULTS[47fef7e4780e9fac6c99d7661c29de580bf0fa14]:
#+begin_src elisp
"{\n \"user-agent\": \"curl/7.35.0\"\n}\n"
#+end_src
Synchronously =GET= a URL that returns a JSON object, and parse and return it as an alist:
#+BEGIN_SRC elisp :exports both :results value code :cache yes
(plz 'get "https://httpbin.org/get" :as #'json-read)
#+END_SRC
#+RESULTS[a117174ba62b2be3ea3f23e5c43662047b81bccf]:
#+begin_src elisp
((args)
(headers
(Accept . "*/*")
(Accept-Encoding . "deflate, gzip")
(Host . "httpbin.org")
(User-Agent . "curl/7.35.0"))
(url . "https://httpbin.org/get"))
#+end_src
Asynchronously =POST= a JSON object in the request body, then parse a JSON object from the response body, and call a function with the result:
#+BEGIN_SRC elisp :exports both :cache yes
(plz 'post "https://httpbin.org/post"
:headers '(("Content-Type" . "application/json"))
:body (json-encode '(("key" . "value")))
:as #'json-read
:then (lambda (alist)
(message "Result: %s" (alist-get 'data alist))))
#+END_SRC
#+RESULTS[3f4fdd16c4980bf36c3930e91f69cc379cca4a35]:
: Result: {"key":"value"}
Synchronously download a JPEG file, then create an Emacs image object from the data:
#+BEGIN_SRC elisp :exports both :cache yes
(let ((jpeg-data (plz 'get "https://httpbin.org/image/jpeg" :as 'binary)))
(create-image jpeg-data nil 'data))
#+END_SRC
#+RESULTS[fbe8a6c8cb097ac08e992ea90bdbd50e7337a385]:
: (image :type jpeg :data ""ÿØÿà^@^PJFIF...")
** Functions
+ ~plz~ :: /(method url &key headers body else finally noquery (as 'string) (then 'sync) (body-type 'text) (decode t decode-s) (connect-timeout plz-connect-timeout) (timeout plz-timeout))/
Request ~METHOD~ from ~URL~ with curl. Return the curl process object or, for a synchronous request, the selected result.
~HEADERS~ may be an alist of extra headers to send with the request.
~BODY~ may be a string, a buffer, or a list like ~(file FILENAME)~ to upload a file from disk.
~BODY-TYPE~ may be ~text~ to send ~BODY~ as text, or ~binary~ to send it as binary.
~AS~ selects the kind of result to pass to the callback function ~THEN~, or the kind of result to return for synchronous requests. It may be:
- ~buffer~ to pass the response buffer, which will be narrowed to the response body and decoded according to ~DECODE~.
- ~binary~ to pass the response body as an un-decoded string.
- ~string~ to pass the response body as a decoded string.
- ~response~ to pass a ~plz-response~ structure.
- ~file~ to pass a temporary filename to which the response body has been saved without decoding.
- ~(file ~FILENAME)~ to pass ~FILENAME~ after having saved the response body to it without decoding. ~FILENAME~ must be a non-existent file; if it exists, it will not be overwritten, and an error will be signaled. ~FILENAME~ is passed through ~expand-file-name~, which see.
- A function, which is called in the response buffer with it narrowed to the response body (suitable for, e.g. ~json-read~).
If ~DECODE~ is non-nil, the response body is decoded automatically. For binary content, it should be nil. When ~AS~ is ~binary~, ~DECODE~ is automatically set to nil.
~THEN~ is a callback function, whose sole argument is selected above with ~AS~; if the request fails and no ~ELSE~ function is given (see below), the argument will be a ~plz-error~ structure describing the error. Or ~THEN~ may be ~sync~ to make a synchronous request, in which case the result is returned directly from this function.
~ELSE~ is an optional callback function called when the request fails (i.e. if curl fails, or if the ~HTTP~ response has a non-2xx status code). It is called with one argument, a ~plz-error~ structure. If ~ELSE~ is nil, a ~plz-curl-error~ or ~plz-http-error~ is signaled when the request fails, with a ~plz-error~ structure as the error data. For synchronous requests, this argument is ignored.
~NOTE~: In v0.8 of ~plz~, only one error will be signaled: ~plz-error~. The existing errors, ~plz-curl-error~ and ~plz-http-error~, inherit from ~plz-error~ to allow applications to update their code while using v0.7 (i.e. any ~condition-case~ forms should now handle only ~plz-error~, not the other two).
~FINALLY~ is an optional function called without argument after ~THEN~ or ~ELSE~, as appropriate. For synchronous requests, this argument is ignored.
~CONNECT-TIMEOUT~ and ~TIMEOUT~ are a number of seconds that limit how long it takes to connect to a host and to receive a response from a host, respectively.
~NOQUERY~ is passed to ~make-process~, which see.
~FILTER~ is an optional function to be used as the process filter for the curl process. It can be used to handle HTTP responses in a streaming way. The function must accept 2 arguments, the process object running curl, and a string which is output received from the process. The default process filter inserts the output of the process into the process buffer. The provided ~FILTER~ function should at least insert output up to the HTTP body into the process buffer.
** Queueing
~plz~ provides a simple system for queueing HTTP requests. First, make a ~plz-queue~ struct by calling ~make-plz-queue~. Then call ~plz-queue~ with the struct as the first argument, and the rest of the arguments being the same as those passed to ~plz~. Then call ~plz-run~ to run the queued requests.
All of the queue-related functions return the queue as their value, making them easy to use. For example:
#+begin_src elisp :exports code
(defvar my-queue (make-plz-queue :limit 2))
(plz-run
(plz-queue my-queue
'get "https://httpbin.org/get?foo=0"
:then (lambda (body) (message "%s" body))))
#+end_src
Or:
#+begin_src elisp :exports code
(let ((queue (make-plz-queue :limit 2
:finally (lambda ()
(message "Queue empty."))))
(urls '("https://httpbin.org/get?foo=0"
"https://httpbin.org/get?foo=1")))
(plz-run
(dolist (url urls queue)
(plz-queue queue 'get url
:then (lambda (body) (message "%s" body))))))
#+end_src
You may also clear a queue with ~plz-clear~, which cancels any active or queued requests and calls their ~:else~ functions. And ~plz-length~ returns the number of a queue's active and queued requests.
** Tips
:PROPERTIES:
:TOC: :ignore (this)
:END:
+ You can customize settings in the =plz= group, but this can only be used to adjust a few defaults. It's not intended that changing or binding global variables be necessary for normal operation.
* Changelog
:PROPERTIES:
:TOC: :depth 0
:END:
** 0.10-pre
*Additions*
+ Support for the HTTP PATCH method. ([[https://github.com/alphapapa/plz.el/pull/49][#49]]. Thanks to [[https://github.com/fpvmorais][Pedro Morais]].)
** 0.9.1
*Fixes*
+ Expand filenames when downloading to files (which was already applied to filenames passed for uploading). (Thanks to [[https://github.com/josephmturner][Joseph Turner]].)
** 0.9
*Compatibility*
+ The minimum supported Emacs version is now 27.1. (It is no longer practical to test ~plz~ with Emacs versions older than 27.1. For Emacs 26.3, an earlier version of ~plz~ may be used, or this version might be compatible, with or without minor changes, which the maintainer cannot offer support for.)
*Additions*
+ When downloading to a file, the ~--create-dirs~ option is passed to Curl so it will create any necessary subdirectories automatically. ([[https://github.com/alphapapa/plz.el/pull/64][#64]]. Thanks to [[https://github.com/josephmturner][Joseph Turner]] and [[https://ushin.org/][USHIN]].)
*Changes*
+ Option ~plz-timeout~ is removed. (It was the default value for ~plz~'s ~:timeout~ argument, which is passed to Curl as its ~--max-time~ argument, limiting the total duration of a request operation. This argument should be unset by default, because larger or slower downloads might not finish within a certain duration, and it is surprising to the user to have this option set by default, potentially causing requests to timeout unnecessarily.)
+ Using arguments ~:as 'file~ or ~:as '(file FILENAME)~ now passes the filename to Curl, allowing it to write the data to the file itself (rather than receiving the data into an Emacs buffer and then writing it to a file. This improves performance when downloading large files, significantly reducing Emacs's CPU and memory usage).
*Fixes*
+ Improve workaround for Emacs's process sentinel-related issues. (Don't try to process response a second time if Emacs calls the sentinel after ~plz~ has returned for a synchronous request. See [[https://github.com/alphapapa/plz.el/issues/53][#53]]. Thanks to [[https://github.com/josephmturner][Joseph Turner]] for extensive help debugging, and to [[https://ushin.org/][USHIN]] for sponsoring some of this work.)
+ Inhibit buffer hooks when calling ~generate-new-buffer~ (as extra protection against "kill buffer?" prompts in case of errors). (See [[https://github.com/alphapapa/plz.el/pull/52][#52]]. Thanks to [[https://github.com/mkcms][Michał Krzywkowski]].)
- Avoid "kill buffer?" prompts in case of errors on Emacs versions before 28. (See [[https://github.com/alphapapa/plz.el/pull/52][#52]] and [[https://github.com/alphapapa/plz.el/issues/57][#57]]. Thanks to [[https://github.com/mkcms][Michał Krzywkowski]].)
*Development*
+ ~plz~ is now automatically tested against Emacs versions 27.1, 27.2, 28.1, 28.2, 29.1, 29.2, 29.3, and a recent snapshot of the ~master~ branch (adding 29.2 and 29.3).
** 0.8
*Additions*
+ Function ~plz~ now accepts a ~:filter~ argument which can be used to override the default process filter (e.g. for streaming responses). ([[https://github.com/alphapapa/plz.el/pull/43][#43]], [[https://github.com/alphapapa/plz.el/pull/50][#50]]. Thanks to [[https://github.com/r0man][Roman Scherer]].)
** 0.7.3
*Fixes*
+ Info manual generation on GNU ELPA. (Also, the Info manual is no longer committed to Git.)
** 0.7.2
*Fixes*
+ Don't delete preexisting files when downloading to a file. ([[https://github.com/alphapapa/plz.el/pull/41][#41]]. Thanks to [[https://github.com/josephmturner][Joseph Turner]].)
** 0.7.1
*Fixes*
+ Handle HTTP 303 redirects. (Thanks to [[https://github.com/hubisan][Daniel Hubmann]] for reporting.)
** 0.7
*Changes*
+ A new error signal, ~plz-error~, is defined. The existing signals, ~plz-curl-error~ and ~plz-http-error~, inherit from it, so handling ~plz-error~ catches both.
*NOTE:* The existing signals, ~plz-curl-error~ and ~plz-http-error~, are hereby deprecated, and they will be removed in v0.8. Applications should be updated while using v0.7 to only expect ~plz-error~.
*Fixes*
+ Significant improvement in reliability by implementing failsafes and workarounds for Emacs's process-handling code. (See [[https://github.com/alphapapa/plz.el/issues/3][#3]].)
+ STDERR output from curl processes is not included in response bodies (which sometimes happened, depending on Emacs's internal race conditions). (Fixes [[https://github.com/alphapapa/plz.el/issues/23][#23]].)
+ Use ~with-local-quit~ for synchronous requests (preventing Emacs from complaining sometimes). (Fixes [[https://github.com/alphapapa/plz.el/issues/26][#26]].)
+ Various fixes for ~:as 'buffer~ result type: decode body when appropriate; unset multibyte for binary; narrow to body; don't kill buffer prematurely.
+ When clearing a queue, don't try to kill finished processes.
*Internal*
+ Response processing now happens outside the process sentinel, so any errors (e.g. in user callbacks) are not signaled from inside the sentinel. (This avoids the 2-second pause Emacs imposes in such cases.)
+ Tests run against a local instance of [[https://github.com/postmanlabs/httpbin][httpbin]] (since the ~httpbin.org~ server is often overloaded).
+ No buffer-local variables are defined anymore; process properties are used instead.
** 0.6
*Additions*
+ Function ~plz~'s ~:body~ argument now accepts a list like ~(file FILENAME)~ to upload a file from disk (by passing the filename to curl, rather than reading its content into Emacs and sending it to curl through the pipe).
*Fixes*
+ Function ~plz~'s docstring now mentions that the ~:body~ argument may also be a buffer (an intentional feature that was accidentally undocumented).
+ Handle HTTP 3xx redirects when using ~:as 'response~.
** 0.5.4
*Fixes*
+ Only run queue's ~finally~ function after queue is empty. (New features should not be designed and released on a Friday.)
** 0.5.3
*Fixes*
+ Move new slot in ~plz-queue~ struct to end to prevent invalid byte-compiler expansions for already-compiled applications (which would require them to be recompiled after upgrading ~plz~).
** 0.5.2
*Fixes*
+ When clearing a queue, only call ~plz-queue~'s ~finally~ function when specified.
** 0.5.1
*Fixes*
+ Only call ~plz-queue~'s ~finally~ function when specified. (Thanks to [[https://github.com/redchops][Dan Oriani]] for reporting.)
** 0.5
*Additions*
+ Struct ~plz-queue~'s ~finally~ slot, a function called when the queue is finished.
** 0.4
*Additions*
+ Support for HTTP ~HEAD~ requests. (Thanks to [[https://ushin.org/][USHIN]] for sponsoring.)
*Changes*
+ Allow sending ~POST~ and ~PUT~ requests without bodies. ([[https://github.com/alphapapa/plz.el/issues/16][#16]]. Thanks to [[https://github.com/josephmturner][Joseph Turner]] for reporting. Thanks to [[https://ushin.org/][USHIN]] for sponsoring.)
*Fixes*
+ All 2xx HTTP status codes are considered successful. ([[https://github.com/alphapapa/plz.el/issues/17][#17]]. Thanks to [[https://github.com/josephmturner][Joseph Turner]] for reporting. Thanks to [[https://ushin.org/][USHIN]] for sponsoring.)
+ Errors are signaled with error data correctly.
*Internal*
+ Test suite explicitly tests with both HTTP/1.1 and HTTP/2.
+ Test suite also tests with Emacs versions 27.2, 28.1, and 28.2.
** 0.3
*Additions*
+ Handle HTTP proxy headers from Curl. ([[https://github.com/alphapapa/plz.el/issues/2][#2]]. Thanks to [[https://github.com/alanthird][Alan Third]] and [[https://github.com/sawyerzheng][Sawyer Zheng]] for reporting.)
*Fixes*
+ Replaced words not in Ispell's default dictionaries (so ~checkdoc~ linting succeeds).
** 0.2.1
*Fixes*
+ Handle when Curl process is interrupted.
** 0.2
*Added*
+ Simple request queueing.
** 0.1
Initial release.
* Credits
+ Thanks to [[https://github.com/skeeto][Chris Wellons]], author of the [[https://github.com/skeeto/elfeed][Elfeed]] feed reader and the popular blog [[https://nullprogram.com/][null program]], for his invaluable advice, review, and encouragement.
* Development
Bug reports, feature requests, suggestions — /oh my/!
Note that ~plz~ is a young library, and its only client so far is [[https://github.com/alphapapa/ement.el][Ement.el]]. There are a variety of HTTP and ~curl~ features it does not yet support, since they have not been needed by the author. Patches are welcome, as long as they include passing tests.
** Copyright assignment
This package is part of [[https://www.gnu.org/software/emacs/][GNU Emacs]], being distributed in [[https://elpa.gnu.org/][GNU ELPA]]. Contributions to this project must follow GNU guidelines, which means that, as with other parts of Emacs, patches of more than a few lines must be accompanied by having assigned copyright for the contribution to the FSF. Contributors who wish to do so may contact [[mailto:emacs-devel@gnu.org][emacs-devel@gnu.org]] to request the assignment form.
* License
:PROPERTIES:
:TOC: :ignore (this)
:END:
GPLv3
* COMMENT Export setup :noexport:
:PROPERTIES:
:TOC: :ignore (this descendants)
:END:
# Copied from org-super-agenda's readme, in which much was borrowed from Org's =org-manual.org=.
#+OPTIONS: broken-links:t *:t
** Info export options
# NOTE: These are moved to the top of the file.
** File-local variables
# NOTE: Setting org-comment-string buffer-locally is a nasty hack to work around GitHub's org-ruby's HTML rendering, which does not respect noexport tags. The only way to hide this tree from its output is to use the COMMENT keyword, but that prevents Org from processing the export options declared in it. So since these file-local variables don't affect org-ruby, wet set org-comment-string to an unused keyword, which prevents Org from deleting this tree from the export buffer, which allows it to find the export options in it. And since org-export does respect the noexport tag, the tree is excluded from the info page.
# Local Variables:
# eval: (require 'org-make-toc)
# before-save-hook: org-make-toc
# org-export-with-properties: ()
# org-export-with-title: t
# org-export-initial-scope: buffer
# org-comment-string: "NOTCOMMENT"
# End:
================================================
FILE: makem.sh
================================================
#!/usr/bin/env bash
# * makem.sh --- Script to aid building and testing Emacs Lisp packages
# URL: https://github.com/alphapapa/makem.sh
# Version: 0.6-pre
# * Commentary:
# makem.sh is a script that helps to build, lint, and test Emacs Lisp
# packages. It aims to make linting and testing as simple as possible
# without requiring per-package configuration.
# It works similarly to a Makefile in that "rules" are called to
# perform actions such as byte-compiling, linting, testing, etc.
# Source and test files are discovered automatically from the
# project's Git repo, and package dependencies within them are parsed
# automatically.
# Output is simple: by default, there is no output unless errors
# occur. With increasing verbosity levels, more detail gives positive
# feedback. Output is colored by default to make reading easy.
# The script can run Emacs with the developer's local Emacs
# configuration, or with a clean, "sandbox" configuration that can be
# optionally removed afterward. This is especially helpful when
# upstream dependencies may have released new versions that differ
# from those installed in the developer's personal configuration.
# * License:
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 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
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# * Functions
function usage {
cat <<EOF
$0 [OPTIONS] RULES...
Linter- and test-specific rules will error when their linters or tests
are not found. With -vv, rules that run multiple rules will show a
message for unavailable linters or tests.
Rules:
all Run all lints and tests.
compile Byte-compile source files.
lint Run all linters, ignoring unavailable ones.
lint-checkdoc Run checkdoc.
lint-compile Byte-compile source files with warnings as errors.
lint-declare Run check-declare.
lint-elsa Run Elsa (not included in "lint" rule).
lint-indent Lint indentation.
lint-package Run package-lint.
lint-regexps Run relint.
test, tests Run all tests, ignoring missing test types.
test-buttercup Run Buttercup tests.
test-ert Run ERT tests.
test-ert-interactive Run ERT tests interactively.
batch Run Emacs in batch mode, loading project source and test files
automatically, with remaining args (after "--") passed to Emacs.
interactive Run Emacs interactively, loading project source and test files
automatically, with remaining args (after "--") passed to Emacs.
Options:
-d, --debug Print debug info.
-h, --help I need somebody!
-v, --verbose Increase verbosity, up to -vvv.
--no-color Disable color output.
--debug-load-path Print load-path from inside Emacs.
-E, --emacs PATH Run Emacs at PATH.
-e, --exclude FILE Exclude FILE from linting and testing.
-f, --file FILE Check FILE in addition to discovered files.
-c, --compile-batch Batch-compile files (instead of separately; quicker, but
may hide problems).
-C, --no-compile Don't compile files automatically.
Sandbox options:
-s[DIR], --sandbox[=DIR] Run Emacs with an empty config in a sandbox DIR.
If DIR does not exist, make it. If DIR is not
specified, use a temporary sandbox directory and
delete it afterward, implying --install-deps and
--install-linters.
--install-deps Automatically install package dependencies.
--install-linters Automatically install linters.
-i, --install PACKAGE Install PACKAGE before running rules.
An Emacs version-specific subdirectory is automatically made inside
the sandbox, allowing testing with multiple Emacs versions. When
specifying a sandbox directory, use options --install-deps and
--install-linters on first-run and omit them afterward to save time.
Source files are automatically discovered from git, or may be
specified with options. Package dependencies are discovered from
"Package-Requires" headers in source files, from -pkg.el files, and
from a Cask file.
EOF
}
# ** Elisp
# These functions return a path to an elisp file which can be loaded
# by Emacs on the command line with -l or --load.
function elisp-buttercup-file {
# The function buttercup-run, which is called by buttercup-run-discover,
# signals an error if it can't find any Buttercup test suites. We don't
# want that to be an error, so we define advice which ignores that error.
local file=$(mktemp)
cat >$file <<EOF
(defun makem-buttercup-run (oldfun &rest r)
"Call buttercup-run only if \`buttercup-suites' is non-nil."
(when buttercup-suites
(apply oldfun r)))
(advice-add #'buttercup-run :around #'makem-buttercup-run)
EOF
echo $file
}
function elisp-elint-file {
local file=$(mktemp)
cat >$file <<EOF
(require 'cl-lib)
(require 'elint)
(defun makem-elint-file (file)
(let ((errors 0))
(cl-letf (((symbol-function 'orig-message) (symbol-function 'message))
((symbol-function 'message) (symbol-function 'ignore))
((symbol-function 'elint-output)
(lambda (string)
(cl-incf errors)
(orig-message "%s" string))))
(elint-file file)
;; NOTE: \`errors' is not actually the number of errors, because
;; it's incremented for non-error header strings as well.
(kill-emacs errors))))
EOF
echo "$file"
}
function elisp-checkdoc-file {
# Since checkdoc doesn't have a batch function that exits non-zero
# when errors are found, we make one.
local file=$(mktemp)
cat >$file <<EOF
(defvar makem-checkdoc-errors-p nil)
(defun makem-checkdoc-files-and-exit ()
"Run checkdoc-file on files remaining on command line, exiting non-zero if there are warnings."
(let* ((files (mapcar #'expand-file-name command-line-args-left))
(checkdoc-create-error-function
(lambda (text start end &optional unfixable)
(let ((msg (concat (checkdoc-buffer-label) ":"
(int-to-string (count-lines (point-min) (or start (point-min))))
": " text)))
(message msg)
(setq makem-checkdoc-errors-p t)
;; Return nil because we *are* generating a buffered list of errors.
nil))))
(mapcar #'checkdoc-file files)
(when makem-checkdoc-errors-p
(kill-emacs 1))))
(setq checkdoc-spellcheck-documentation-flag t)
(makem-checkdoc-files-and-exit)
EOF
echo $file
}
function elisp-byte-compile-file {
# This seems to be the only way to make byte-compilation signal
# errors for warnings AND display all warnings rather than only
# the first one.
local file=$(mktemp)
# TODO: Add file to $paths_temp in other elisp- functions.
paths_temp+=("$file")
cat >"$file" <<EOF
(defun makem-batch-byte-compile (&rest args)
""
(let ((num-errors 0))
;; NOTE: Only accepts files as args, not directories.
(dolist (file command-line-args-left)
(unless (makem-byte-compile-file file)
(cl-incf num-errors)))
(zerop num-errors)))
(defun makem-byte-compile-file (filename &optional load)
"Call \`byte-compile-warn', returning nil if there are any warnings."
(let ((num-warnings 0))
(cl-letf (((symbol-function 'byte-compile-warn)
(lambda (format &rest args)
;; Copied from \`byte-compile-warn'.
(cl-incf num-warnings)
(setq format (apply #'format-message format args))
(byte-compile-log-warning format t :warning))))
(byte-compile-file filename load))
(zerop num-warnings)))
EOF
echo "$file"
}
function elisp-check-declare-file {
# Since check-declare doesn't have a batch function that exits
# non-zero when errors are found, we make one.
local file=$(mktemp)
cat >$file <<EOF
(require 'check-declare)
(defun makem-check-declare-files-and-exit ()
"Run check-declare-files on files remaining on command line, exiting non-zero if there are warnings."
(let* ((files (mapcar #'expand-file-name command-line-args-left))
(errors (apply #'check-declare-files files)))
(when errors
(with-current-buffer check-declare-warning-buffer
(print (buffer-string)))
(kill-emacs 1))))
EOF
echo $file
}
function elisp-lint-indent-file {
# This function prints warnings for indentation errors and exits
# non-zero when errors are found.
local file=$(mktemp)
cat >"$file" <<EOF
(require 'cl-lib)
(defun makem-lint-indent-batch-and-exit ()
"Print warnings for files which are not indented properly, then exit.
Exits non-zero if mis-indented lines are found. Checks files in
'command-line-args-left'."
(let ((errors-p))
(cl-labels ((lint-file (file)
(find-file file)
(let ((inhibit-message t))
(indent-region (point-min) (point-max)))
(when buffer-undo-list
;; Indentation changed: warn for each line.
(dolist (line (undo-lines buffer-undo-list))
(message "%s:%s: Indentation mismatch" (buffer-name) line))
(setf errors-p t)))
(undo-pos (entry)
(cl-typecase (car entry)
(number (car entry))
(string (abs (cdr entry)))))
(undo-lines (undo-list)
;; Return list of lines changed in UNDO-LIST.
(nreverse (cl-loop for elt in undo-list
for pos = (undo-pos elt)
when pos
collect (line-number-at-pos pos)))))
(mapc #'lint-file (mapcar #'expand-file-name command-line-args-left))
(when errors-p
(kill-emacs 1)))))
EOF
echo "$file"
}
function elisp-package-initialize-file {
local file=$(mktemp)
cat >$file <<EOF
(require 'package)
(setq package-archives (list (cons "gnu" "https://elpa.gnu.org/packages/")
(cons "melpa" "https://melpa.org/packages/")
(cons "melpa-stable" "https://stable.melpa.org/packages/")))
$elisp_org_package_archive
(package-initialize)
EOF
echo $file
}
# ** Emacs
function run_emacs {
# NOTE: The sandbox args need to come before the package
# initialization so Emacs will use the sandbox's packages.
local emacs_command=(
"${emacs_command[@]}"
-Q
--eval "(setq load-prefer-newer t)"
"${args_debug[@]}"
"${args_sandbox[@]}"
-l $package_initialize_file
$arg_batch
"${args_load_paths[@]}"
)
# Show debug message with load-path from inside Emacs.
[[ $debug_load_path ]] \
&& debug $("${emacs_command[@]}" \
--batch \
--eval "(message \"LOAD-PATH: %s\" load-path)" \
2>&1)
# Set output file.
output_file=$(mktemp) || die "Unable to make output file."
paths_temp+=("$output_file")
# Run Emacs.
debug "run_emacs: ${emacs_command[@]} $@ &>\"$output_file\""
"${emacs_command[@]}" "$@" &>"$output_file"
# Check exit code and output.
exit=$?
[[ $exit != 0 ]] \
&& debug "Emacs exited non-zero: $exit"
[[ $verbose -gt 1 || $exit != 0 ]] \
&& cat $output_file
return $exit
}
# ** Compilation
function batch-byte-compile {
debug "batch-byte-compile: ERROR-ON-WARN:$compile_error_on_warn"
[[ $compile_error_on_warn ]] && local error_on_warn=(--eval "(setq byte-compile-error-on-warn t)")
run_emacs \
--load "$(elisp-byte-compile-file)" \
"${error_on_warn[@]}" \
--eval "(unless (makem-batch-byte-compile) (kill-emacs 1))" \
"$@"
}
function byte-compile-file {
debug "byte-compile: ERROR-ON-WARN:$compile_error_on_warn"
local file="$1"
[[ $compile_error_on_warn ]] && local error_on_warn=(--eval "(setq byte-compile-error-on-warn t)")
# FIXME: Why is the line starting with "&& verbose 3" not indented properly? Emacs insists on indenting it back a level.
run_emacs \
--load "$(elisp-byte-compile-file)" \
"${error_on_warn[@]}" \
--eval "(unless (makem-byte-compile-file \"$file\") (kill-emacs 1))" \
&& verbose 3 "Compiling $file finished without errors." \
|| { verbose 3 "Compiling file failed: $file"; return 1; }
}
# ** Files
function dirs-project {
# Echo list of directories to be used in load path.
files-project-feature | dirnames
files-project-test | dirnames
}
function files-project-elisp {
# Echo list of Elisp files in project.
git ls-files 2>/dev/null \
| egrep "\.el$" \
| filter-files-exclude-default \
| filter-files-exclude-args
}
function files-project-feature {
# Echo list of Elisp files that are not tests and provide a feature.
files-project-elisp \
| egrep -v "$test_files_regexp" \
| filter-files-feature
}
function files-project-test {
# Echo list of Elisp test files.
files-project-elisp | egrep "$test_files_regexp"
}
function dirnames {
# Echo directory names for files on STDIN.
while read file
do
dirname "$file"
done
}
function filter-files-exclude-default {
# Filter out paths (STDIN) which should be excluded by default.
egrep -v "(/\.cask/|-autoloads.el|.dir-locals)"
}
function filter-files-exclude-args {
# Filter out paths (STDIN) which are excluded with --exclude.
if [[ ${files_exclude[@]} ]]
then
(
# We use a subshell to set IFS temporarily so we can send
# the list of files to grep -F. This is ugly but more
# correct than replacing spaces with line breaks. Note
# that, for some reason, using IFS="\n" or IFS='\n' doesn't
# work, and a literal line break seems to be required.
IFS="
"
grep -Fv "${files_exclude[*]}"
)
else
cat
fi
}
function filter-files-feature {
# Read paths on STDIN and echo ones that (provide 'a-feature).
while read path
do
egrep "^\\(provide '" "$path" &>/dev/null \
&& echo "$path"
done
}
function args-load-files {
# For file in $@, echo "--load $file".
for file in "$@"
do
sans_extension=${file%%.el}
printf -- '--load %q ' "$sans_extension"
done
}
function args-load-path {
# Echo load-path arguments.
for path in $(dirs-project | sort -u)
do
printf -- '-L %q ' "$path"
done
}
function test-files-p {
# Return 0 if $files_project_test is non-empty.
[[ "${files_project_test[@]}" ]]
}
function buttercup-tests-p {
# Return 0 if Buttercup tests are found.
test-files-p || die "No tests found."
debug "Checking for Buttercup tests..."
grep "(require 'buttercup)" "${files_project_test[@]}" &>/dev/null
}
function ert-tests-p {
# Return 0 if ERT tests are found.
test-files-p || die "No tests found."
debug "Checking for ERT tests..."
# We check for this rather than "(require 'ert)", because ERT may
# already be loaded in Emacs and might not be loaded with
# "require" in a test file.
grep "(ert-deftest" "${files_project_test[@]}" &>/dev/null
}
function package-main-file {
# Echo the package's main file.
file_pkg=$(git ls-files ./*-pkg.el 2>/dev/null)
if [[ $file_pkg ]]
then
# Use *-pkg.el file if it exists.
echo "$file_pkg"
else
# Use shortest filename (a sloppy heuristic that will do for now).
for file in "${files_project_feature[@]}"
do
echo ${#file} "$file"
done \
| sort -h \
| head -n1 \
| sed -r 's/^[[:digit:]]+ //'
fi
}
function dependencies {
# Echo list of package dependencies.
# Search package headers. Use -a so grep won't think that an Elisp file containing
# control characters (rare, but sometimes necessary) is binary and refuse to search it.
egrep -a -i '^;; Package-Requires: ' $(files-project-feature) $(files-project-test) \
| egrep -o '\([^([:space:]][^)]*\)' \
| egrep -o '^[^[:space:])]+' \
| sed -r 's/\(//g' \
| egrep -v '^emacs$' # Ignore Emacs version requirement.
# Search Cask file.
if [[ -r Cask ]]
then
egrep '\(depends-on "[^"]+"' Cask \
| sed -r -e 's/\(depends-on "([^"]+)".*/\1/g'
fi
# Search -pkg.el file.
if [[ $(git ls-files ./*-pkg.el 2>/dev/null) ]]
then
sed -nr 's/.*\(([-[:alnum:]]+)[[:blank:]]+"[.[:digit:]]+"\).*/\1/p' $(git ls-files ./*-pkg.el 2>/dev/null)
fi
}
# ** Sandbox
function sandbox {
verbose 2 "Initializing sandbox..."
# *** Sandbox arguments
# MAYBE: Optionally use branch-specific sandbox?
# Check or make user-emacs-directory.
if [[ $sandbox_dir ]]
then
# Directory given as argument: ensure it exists.
if ! [[ -d $sandbox_dir ]]
then
debug "Making sandbox directory: $sandbox_dir"
mkdir -p "$sandbox_dir" || die "Unable to make sandbox dir."
fi
# Add Emacs version-specific subdirectory, creating if necessary.
sandbox_dir="$sandbox_dir/$(emacs-version)"
if ! [[ -d $sandbox_dir ]]
then
mkdir "$sandbox_dir" || die "Unable to make sandbox subdir: $sandbox_dir"
fi
else
# Not given: make temp directory, and delete it on exit.
local sandbox_dir=$(mktemp -d) || die "Unable to make sandbox dir."
paths_temp+=("$sandbox_dir")
fi
# Make argument to load init file if it exists.
init_file="$sandbox_dir/init.el"
# Set sandbox args. This is a global variable used by the run_emacs function.
args_sandbox=(
--title "makem.sh: $(basename $(pwd)) (sandbox: $sandbox_dir)"
--eval "(setq user-emacs-directory (file-truename \"$sandbox_dir\"))"
--load package
--eval "(setq package-user-dir (expand-file-name \"elpa\" user-emacs-directory))"
--eval "(setq user-init-file (file-truename \"$init_file\"))"
)
# Add package-install arguments for dependencies.
if [[ $install_deps ]]
then
local deps=($(dependencies))
debug "Installing dependencies: ${deps[@]}"
for package in "${deps[@]}"
do
args_sandbox_package_install+=(--eval "(package-install '$package)")
done
fi
# Add package-install arguments for linters.
if [[ $install_linters ]]
then
debug "Installing linters: package-lint relint"
args_sandbox_package_install+=(
--eval "(package-install 'elsa)"
--eval "(package-install 'package-lint)"
--eval "(package-install 'relint)")
fi
# *** Install packages into sandbox
if [[ ${args_sandbox_package_install[@]} ]]
then
# Initialize the sandbox (installs packages once rather than for every rule).
verbose 1 "Installing packages into sandbox..."
run_emacs \
--eval "(package-refresh-contents)" \
"${args_sandbox_package_install[@]}" \
&& success "Packages installed." \
|| die "Unable to initialize sandbox."
fi
verbose 2 "Sandbox initialized."
}
# ** Utility
function cleanup {
# Remove temporary paths (${paths_temp[@]}).
for path in "${paths_temp[@]}"
do
if [[ $debug ]]
then
debug "Debugging enabled: not deleting temporary path: $path"
elif [[ -r $path ]]
then
rm -rf "$path"
else
debug "Temporary path doesn't exist, not deleting: $path"
fi
done
}
function echo-unset-p {
# Echo 0 if $1 is set, otherwise 1. IOW, this returns the exit
# code of [[ $1 ]] as STDOUT.
[[ $1 ]]
echo $?
}
function ensure-package-available {
# If package $1 is available, return 0. Otherwise, return 1, and
# if $2 is set, give error otherwise verbose. Outputting messages
# here avoids repetition in callers.
local package=$1
local direct_p=$2
if ! run_emacs --load $package &>/dev/null
then
if [[ $direct_p ]]
then
error "$package not available."
else
verbose 2 "$package not available."
fi
return 1
fi
}
function ensure-tests-available {
# If tests of type $1 (like "ERT") are available, return 0. Otherwise, if
# $2 is set, give an error and return 1; otherwise give verbose message. $1
# should have a corresponding predicate command, like ert-tests-p for ERT.
local test_name=$1
local test_command="${test_name,,}-tests-p" # Converts name to lowercase.
local direct_p=$2
if ! $test_command
then
if [[ $direct_p ]]
then
error "$test_name tests not found."
else
verbose 2 "$test_name tests not found."
fi
return 1
fi
}
function echo_color {
# This allows bold, italic, etc. without needing a function for
# each variation.
local color_code="COLOR_$1"
shift
if [[ $color ]]
then
echo -e "${!color_code}${@}${COLOR_off}"
else
echo "$@"
fi
}
function debug {
if [[ $debug ]]
then
function debug {
echo_color yellow "DEBUG ($(ts)): $@" >&2
}
debug "$@"
else
function debug {
true
}
fi
}
function error {
echo_color red "ERROR ($(ts)): $@" >&2
((errors++))
return 1
}
function die {
[[ $@ ]] && error "$@"
exit $errors
}
function log {
echo "LOG ($(ts)): $@" >&2
}
function log_color {
local color_name=$1
shift
echo_color $color_name "LOG ($(ts)): $@" >&2
}
function success {
if [[ $verbose -ge 2 ]]
then
log_color green "$@" >&2
fi
}
function verbose {
# $1 is the verbosity level, rest are echoed when appropriate.
if [[ $verbose -ge $1 ]]
then
[[ $1 -eq 1 ]] && local color_name=blue
[[ $1 -eq 2 ]] && local color_name=cyan
[[ $1 -ge 3 ]] && local color_name=white
shift
log_color $color_name "$@" >&2
fi
}
function ts {
date "+%Y-%m-%d %H:%M:%S"
}
function emacs-version {
# Echo Emacs version number.
# Don't use run_emacs function, which does more than we need.
"${emacs_command[@]}" -Q --batch --eval "(princ emacs-version)" \
|| die "Unable to get Emacs version."
}
function rule-p {
# Return 0 if $1 is a rule.
[[ $1 =~ ^(lint-?|tests?)$ ]] \
|| [[ $1 =~ ^(batch|interactive)$ ]] \
|| [[ $(type -t "$2" 2>/dev/null) =~ function ]]
}
# * Rules
# These functions are intended to be called as rules, like a Makefile.
# Some rules test $1 to determine whether the rule is being called
# directly or from a meta-rule; if directly, an error is given if the
# rule can't be run, otherwise it's skipped.
function all {
verbose 1 "Running all rules..."
lint
tests
}
function compile-batch {
[[ $compile ]] || return 0
unset compile # Only compile once.
verbose 1 "Compiling..."
verbose 2 "Batch-compiling files..."
debug "Byte-compile files: ${files_project_byte_compile[@]}"
batch-byte-compile "${files_project_byte_compile[@]}"
}
function compile-each {
[[ $compile ]] || return 0
unset compile # Only compile once.
verbose 1 "Compiling..."
debug "Byte-compile files: ${files_project_byte_compile[@]}"
local compile_errors
for file in "${files_project_byte_compile[@]}"
do
verbose 2 "Compiling file: $file..."
byte-compile-file "$file" \
|| compile_errors=t
done
[[ ! $compile_errors ]]
}
function compile {
if [[ $compile = batch ]]
then
compile-batch "$@"
else
compile-each "$@"
fi
local status=$?
if [[ $compile_error_on_warn ]]
then
# Linting: just return status code, because lint rule will print messages.
[[ $status = 0 ]]
else
# Not linting: print messages here.
[[ $status = 0 ]] \
&& success "Compiling finished without errors." \
|| error "Compiling failed."
fi
}
function batch {
# Run Emacs in batch mode with ${args_batch_interactive[@]} and
# with project source and test files loaded.
verbose 1 "Executing Emacs with arguments: ${args_batch_interactive[@]}"
run_emacs \
$(args-load-files "${files_project_feature[@]}" "${files_project_test[@]}") \
"${args_batch_interactive[@]}"
}
function interactive {
# Run Emacs interactively. Most useful with --sandbox and --install-deps.
local load_file_args=$(args-load-files "${files_project_feature[@]}" "${files_project_test[@]}")
verbose 1 "Running Emacs interactively..."
verbose 2 "Loading files: ${load_file_args//--load /}"
[[ $compile ]] && compile
unset arg_batch
run_emacs \
$load_file_args \
--eval "(load user-init-file)" \
"${args_batch_interactive[@]}"
arg_batch="--batch"
}
function lint {
verbose 1 "Linting..."
lint-checkdoc
lint-compile
lint-declare
# NOTE: Elint doesn't seem very useful at the moment. See comment
# in lint-elint function.
# lint-elint
lint-indent
lint-package
lint-regexps
}
function lint-checkdoc {
verbose 1 "Linting checkdoc..."
local checkdoc_file="$(elisp-checkdoc-file)"
paths_temp+=("$checkdoc_file")
run_emacs \
--load="$checkdoc_file" \
"${files_project_feature[@]}" \
&& success "Linting checkdoc finished without errors." \
|| error "Linting checkdoc failed."
}
function lint-compile {
verbose 1 "Linting compilation..."
compile_error_on_warn=true
compile "${files_project_byte_compile[@]}" \
&& success "Linting compilation finished without errors." \
|| error "Linting compilation failed."
unset compile_error_on_warn
}
function lint-declare {
verbose 1 "Linting declarations..."
local check_declare_file="$(elisp-check-declare-file)"
paths_temp+=("$check_declare_file")
run_emacs \
--load "$check_declare_file" \
-f makem-check-declare-files-and-exit \
"${files_project_feature[@]}" \
&& success "Linting declarations finished without errors." \
|| error "Linting declarations failed."
}
function lint-elsa {
verbose 1 "Linting with Elsa..."
# MAYBE: Install Elsa here rather than in sandbox init, to avoid installing
# it when not needed. However, we should be careful to be clear about when
# packages are installed, because installing them does execute code.
run_emacs \
--load elsa \
-f elsa-run-files-and-exit \
"${files_project_feature[@]}" \
&& success "Linting with Elsa finished without errors." \
|| error "Linting with Elsa failed."
}
function lint-elint {
# NOTE: Elint gives a lot of spurious warnings, apparently because it doesn't load files
# that are `require'd, so its output isn't very useful. But in case it's improved in
# the future, and since this wrapper code already works, we might as well leave it in.
verbose 1 "Linting with Elint..."
local errors=0
for file in "${files_project_feature[@]}"
do
verbose 2 "Linting with Elint: $file..."
run_emacs \
--load "$(elisp-elint-file)" \
--eval "(makem-elint-file \"$file\")" \
&& verbose 3 "Linting with Elint found no errors." \
|| { error "Linting with Elint failed: $file"; ((errors++)) ; }
done
[[ $errors = 0 ]] \
&& success "Linting with Elint finished without errors." \
|| error "Linting with Elint failed."
}
function lint-indent {
verbose 1 "Linting indentation..."
# We load project source files as well, because they may contain
# macros with (declare (indent)) rules which must be loaded to set
# indentation.
run_emacs \
--load "$(elisp-lint-indent-file)" \
$(args-load-files "${files_project_feature[@]}" "${files_project_test[@]}") \
--funcall makem-lint-indent-batch-and-exit \
"${files_project_feature[@]}" "${files_project_test[@]}" \
&& success "Linting indentation finished without errors." \
|| error "Linting indentation failed."
}
function lint-package {
ensure-package-available package-lint $1 || return $(echo-unset-p $1)
verbose 1 "Linting package..."
run_emacs \
--load package-lint \
--eval "(setq package-lint-main-file \"$(package-main-file)\")" \
--funcall package-lint-batch-and-exit \
"${files_project_feature[@]}" \
&& success "Linting package finished without errors." \
|| error "Linting package failed."
}
function lint-regexps {
ensure-package-available relint $1 || return $(echo-unset-p $1)
verbose 1 "Linting regexps..."
run_emacs \
--load relint \
--funcall relint-batch \
"${files_project_source[@]}" \
&& success "Linting regexps finished without errors." \
|| error "Linting regexps failed."
}
function tests {
verbose 1 "Running all tests..."
test-ert
test-buttercup
}
function test-ert-interactive {
verbose 1 "Running ERT tests interactively..."
unset arg_batch
run_emacs \
$(args-load-files "${files_project_test[@]}") \
--eval "(ert-run-tests-interactively t)"
arg_batch="--batch"
}
function test-buttercup {
ensure-tests-available Buttercup $1 || return $(echo-unset-p $1)
compile || die
verbose 1 "Running Buttercup tests..."
local buttercup_file="$(elisp-buttercup-file)"
paths_temp+=("$buttercup_file")
run_emacs \
$(args-load-files "${files_project_test[@]}") \
-f buttercup-run \
&& success "Buttercup tests finished without errors." \
|| error "Buttercup tests failed."
}
function test-ert {
ensure-tests-available ERT $1 || return $(echo-unset-p $1)
compile || die
verbose 1 "Running ERT tests..."
debug "Test files: ${files_project_test[@]}"
run_emacs \
$(args-load-files "${files_project_test[@]}") \
-f ert-run-tests-batch-and-exit \
&& success "ERT tests finished without errors." \
|| error "ERT tests failed."
}
# * Defaults
test_files_regexp='^((tests?|t)/)|-tests?.el$|^test-'
emacs_command=("emacs")
errors=0
verbose=0
compile=true
arg_batch="--batch"
compile=each
# MAYBE: Disable color if not outputting to a terminal. (OTOH, the
# colorized output is helpful in CI logs, and I don't know if,
# e.g. GitHub Actions logging pretends to be a terminal.)
color=true
# TODO: Using the current directory (i.e. a package's repo root directory) in
# load-path can cause weird errors in case of--you guessed it--stale .ELC files,
# the zombie problem that just won't die. It's incredible how many different ways
# this problem presents itself. In this latest example, an old .ELC file, for a
# .EL file that had since been renamed, was present on my local system, which meant
# that an example .EL file that hadn't been updated was able to "require" that .ELC
# file's feature without error. But on another system (in this case, trying to
# setup CI using GitHub Actions), the old .ELC was not present, so the example .EL
# file was not able to load the feature, which caused a byte-compilation error.
# In this case, I will prevent such example files from being compiled. But in
# general, this can cause weird problems that are tedious to debug. I guess
# the best way to fix it would be to actually install the repo's code as a
# package into the sandbox, but doing that would require additional tooling,
# pulling in something like Quelpa or package-build--and if the default recipe
# weren't being used, the actual recipe would have to be fetched off MELPA or
# something, which seems like getting too smart for our own good.
# TODO: Emit a warning if .ELC files that don't match any .EL files are detected.
# ** Colors
COLOR_off='\e[0m'
COLOR_black='\e[0;30m'
COLOR_red='\e[0;31m'
COLOR_green='\e[0;32m'
COLOR_yellow='\e[0;33m'
COLOR_blue='\e[0;34m'
COLOR_purple='\e[0;35m'
COLOR_cyan='\e[0;36m'
COLOR_white='\e[0;37m'
# ** Package system args
args_package_archives=(
--eval "(add-to-list 'package-archives '(\"gnu\" . \"https://elpa.gnu.org/packages/\") t)"
--eval "(add-to-list 'package-archives '(\"melpa\" . \"https://melpa.org/packages/\") t)"
)
args_org_package_archives=(
--eval "(add-to-list 'package-archives '(\"org\" . \"https://orgmode.org/elpa/\") t)"
)
args_package_init=(
--eval "(package-initialize)"
)
elisp_org_package_archive="(add-to-list 'package-archives '(\"org\" . \"https://orgmode.org/elpa/\") t)"
# * Args
args=$(getopt -n "$0" \
-o dhce:E:i:s::vf:CO \
-l compile-batch,exclude:,emacs:,install-deps,install-linters,debug,debug-load-path,help,install:,verbose,file:,no-color,no-compile,no-org-repo,sandbox:: \
-- "$@") \
|| { usage; exit 1; }
eval set -- "$args"
while true
do
case "$1" in
--install-deps)
install_deps=true
;;
--install-linters)
install_linters=true
;;
-d|--debug)
debug=true
verbose=2
args_debug=(--eval "(setq init-file-debug t)"
--eval "(setq debug-on-error t)")
;;
--debug-load-path)
debug_load_path=true
;;
-h|--help)
usage
exit
;;
-c|--compile-batch)
debug "Compiling files in batch mode"
compile=batch
;;
-E|--emacs)
shift
emacs_command=($1)
;;
-i|--install)
shift
args_sandbox_package_install+=(--eval "(package-install '$1)")
;;
-s|--sandbox)
sandbox=true
shift
sandbox_dir="$1"
if ! [[ $sandbox_dir ]]
then
debug "No sandbox dir: installing dependencies."
install_deps=true
else
debug "Sandbox dir: $1"
fi
;;
-v|--verbose)
((verbose++))
;;
-e|--exclude)
shift
debug "Excluding file: $1"
files_exclude+=("$1")
;;
-f|--file)
shift
args_files+=("$1")
;;
-O|--no-org-repo)
unset elisp_org_package_archive
;;
--no-color)
unset color
;;
-C|--no-compile)
unset compile
;;
--)
# Remaining args (required; do not remove)
shift
rest=("$@")
break
;;
esac
shift
done
debug "ARGS: $args"
debug "Remaining args: ${rest[@]}"
# Set package elisp (which depends on --no-org-repo arg).
package_initialize_file="$(elisp-package-initialize-file)"
paths_temp+=("$package_initialize_file")
# * Main
trap cleanup EXIT INT TERM
# Discover project files.
files_project_feature=($(files-project-feature))
files_project_test=($(files-project-test))
files_project_byte_compile=("${files_project_feature[@]}" "${files_project_test[@]}")
if [[ ${args_files[@]} ]]
then
# Add specified files.
files_project_feature+=("${args_files[@]}")
files_project_byte_compile+=("${args_files[@]}")
fi
debug "EXCLUDING FILES: ${files_exclude[@]}"
debug "FEATURE FILES: ${files_project_feature[@]}"
debug "TEST FILES: ${files_project_test[@]}"
debug "BYTE-COMPILE FILES: ${files_project_byte_compile[@]}"
debug "PACKAGE-MAIN-FILE: $(package-main-file)"
if ! [[ ${files_project_feature[@]} ]]
then
error "No files specified and not in a git repo."
exit 1
fi
# Set load path.
args_load_paths=($(args-load-path))
debug "LOAD PATH ARGS: ${args_load_paths[@]}"
# If rules include linters and sandbox-dir is unspecified, install
# linters automatically.
if [[ $sandbox && ! $sandbox_dir ]] && [[ "${rest[@]}" =~ lint ]]
then
debug "Installing linters automatically."
install_linters=true
fi
# Initialize sandbox.
[[ $sandbox ]] && sandbox
# Run rules.
for rule in "${rest[@]}"
do
if [[ $batch || $interactive ]]
then
debug "Adding batch/interactive argument: $rule"
args_batch_interactive+=("$rule")
elif [[ $rule = batch ]]
then
# Remaining arguments are passed to Emacs.
batch=true
elif [[ $rule = interactive ]]
then
# Remaining arguments are passed to Emacs.
interactive=true
elif type -t "$rule" 2>/dev/null | grep function &>/dev/null
then
# Pass called-directly as $1 to indicate that the rule is
# being called directly rather than from a meta-rule.
$rule called-directly
elif [[ $rule = test ]]
then
# Allow the "tests" rule to be called as "test". Since "test"
# is a shell builtin, this workaround is required.
tests
else
error "Invalid rule: $rule"
fi
done
# Batch/interactive rules.
[[ $batch ]] && batch
[[ $interactive ]] && interactive
if [[ $errors -gt 0 ]]
then
log_color red "Finished with $errors errors."
else
success "Finished without errors."
fi
exit $errors
================================================
FILE: plz.el
================================================
;;; plz.el --- HTTP library -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2023 Free Software Foundation, Inc.
;; Author: Adam Porter <adam@alphapapa.net>
;; Maintainer: Adam Porter <adam@alphapapa.net>
;; URL: https://github.com/alphapapa/plz.el
;; Version: 0.10-pre
;; Package-Requires: ((emacs "27.1"))
;; Keywords: comm, network, http
;; This file is part of GNU Emacs.
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; 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
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; An HTTP library that uses curl as a backend. Inspired by, and some
;; code copied from, Christopher Wellons's library, elfeed-curl.el.
;;
;; Why this package?
;;
;; 1. `url' works well for many things, but it has some issues.
;; 2. `request' works well for many things, but it has some issues.
;; 3. Chris Wellons doesn't have time to factor his excellent
;; elfeed-curl.el library out of Elfeed. This will have to do.
;;
;; Why is it called `plz'?
;;
;; 1. There's already a package called `http'.
;; 2. There's already a package called `request'.
;; 3. Naming things is hard.
;;;; Usage:
;; FIXME(v0.10): Remove the following note.
;; NOTE: In a future version of plz, only one error will be signaled:
;; `plz-error'. The existing errors, `plz-curl-error' and
;; `plz-http-error', inherit from `plz-error' to allow applications to
;; update their code while using earlier versions (i.e. any
;; `condition-case' forms should now handle only `plz-error', not the
;; other two).
;; Call function `plz' to make an HTTP request. Its docstring
;; explains its arguments. `plz' also supports other HTTP methods,
;; uploading and downloading binary files, sending URL parameters and
;; HTTP headers, configurable timeouts, error-handling "else" and
;; always-called "finally" functions, and more.
;; Basic usage is simple. For example, to make a synchronous request
;; and return the HTTP response body as a string:
;;
;; (plz 'get "https://httpbin.org/get")
;;
;; Which returns the JSON object as a string:
;;
;; "{
;; \"args\": {},
;; \"headers\": {
;; \"Accept\": \"*/*\",
;; \"Accept-Encoding\": \"deflate, gzip\",
;; \"Host\": \"httpbin.org\",
;; \"User-Agent\": \"curl/7.35.0\"
;; },
;; \"origin\": \"xxx.xxx.xxx.xxx\",
;; \"url\": \"https://httpbin.org/get\"
;; }"
;;
;; To make the same request asynchronously, decoding the JSON and
;; printing a message with a value from it:
;;
;; (plz 'get "https://httpbin.org/get" :as #'json-read
;; :then (lambda (alist) (message "URL: %s" (alist-get 'url alist))))
;;
;; Which, after the request returns, prints:
;;
;; URL: https://httpbin.org/get
;;;; Credits:
;; Thanks to Chris Wellons for inspiration, encouragement, and advice.
;;; Code:
;;;; Requirements
(require 'cl-lib)
(require 'map)
(require 'rx)
(require 'subr-x)
;;;; Errors
(define-error 'plz-error "plz error")
(define-error 'plz-curl-error "plz: Curl error" 'plz-error)
(define-error 'plz-http-error "plz: HTTP error" 'plz-error)
;;;; Structs
(cl-defstruct plz-response
version status headers body)
(cl-defstruct plz-error
curl-error response message)
;;;; Constants
(defconst plz-http-response-status-line-regexp
(rx "HTTP/" (group (or "1.0" "1.1" "2")) " "
;; Status code
(group (1+ digit)) " "
;; Reason phrase
(optional (group (1+ (not (any "\r\n")))))
(or
;; HTTP 1
"\r\n"
;; HTTP 2
"\n"))
"Regular expression matching HTTP response status line.")
(defconst plz-http-end-of-headers-regexp
(rx (or "\r\n\r\n" "\n\n"))
"Regular expression matching the end of HTTP headers.
This must work with both HTTP/1 (using CRLF) and HTTP/2 (using
only LF).")
(defconst plz-curl-errors
;; Copied from elfeed-curl.el.
'((1 . "Unsupported protocol.")
(2 . "Failed to initialize.")
(3 . "URL malformed. The syntax was not correct.")
(4 . "A feature or option that was needed to perform the desired request was not enabled or was explicitly disabled at build-time.")
(5 . "Couldn't resolve proxy. The given proxy host could not be resolved.")
(6 . "Couldn't resolve host. The given remote host was not resolved.")
(7 . "Failed to connect to host.")
(8 . "FTP weird server reply. The server sent data curl couldn't parse.")
(9 . "FTP access denied.")
(11 . "FTP weird PASS reply.")
(13 . "FTP weird PASV reply.")
(14 . "FTP weird 227 format.")
(15 . "FTP can't get host.")
(17 . "FTP couldn't set binary.")
(18 . "Partial file. Only a part of the file was transferred.")
(19 . "FTP couldn't download/access the given file, the RETR (or similar) command failed.")
(21 . "FTP quote error. A quote command returned error from the server.")
(22 . "HTTP page not retrieved.")
(23 . "Write error.")
(25 . "FTP couldn't STOR file.")
(26 . "Read error. Various reading problems.")
(27 . "Out of memory. A memory allocation request failed.")
(28 . "Operation timeout.")
(30 . "FTP PORT failed.")
(31 . "FTP couldn't use REST.")
(33 . "HTTP range error. The range \"command\" didn't work.")
(34 . "HTTP post error. Internal post-request generation error.")
(35 . "SSL connect error. The SSL handshaking failed.")
(36 . "FTP bad download resume.")
(37 . "FILE couldn't read file.")
(38 . "LDAP bind operation failed.")
(39 . "LDAP search failed.")
(41 . "Function not found. A required LDAP function was not found.")
(42 . "Aborted by callback.")
(43 . "Internal error. A function was called with a bad parameter.")
(45 . "Interface error. A specified outgoing interface could not be used.")
(47 . "Too many redirects.")
(48 . "Unknown option specified to libcurl.")
(49 . "Malformed telnet option.")
(51 . "The peer's SSL certificate or SSH MD5 fingerprint was not OK.")
(52 . "The server didn't reply anything, which here is considered an error.")
(53 . "SSL crypto engine not found.")
(54 . "Cannot set SSL crypto engine as default.")
(55 . "Failed sending network data.")
(56 . "Failure in receiving network data.")
(58 . "Problem with the local certificate.")
(59 . "Couldn't use specified SSL cipher.")
(60 . "Peer certificate cannot be authenticated with known CA certificates.")
(61 . "Unrecognized transfer encoding.")
(62 . "Invalid LDAP URL.")
(63 . "Maximum file size exceeded.")
(64 . "Requested FTP SSL level failed.")
(65 . "Sending the data requires a rewind that failed.")
(66 . "Failed to initialise SSL Engine.")
(67 . "The user name, password, or similar was not accepted and curl failed to log in.")
(68 . "File not found on TFTP server.")
(69 . "Permission problem on TFTP server.")
(70 . "Out of disk space on TFTP server.")
(71 . "Illegal TFTP operation.")
(72 . "Unknown TFTP transfer ID.")
(73 . "File already exists (TFTP).")
(74 . "No such user (TFTP).")
(75 . "Character conversion failed.")
(76 . "Character conversion functions required.")
(77 . "Problem with reading the SSL CA cert (path? access rights?).")
(78 . "The resource referenced in the URL does not exist.")
(79 . "An unspecified error occurred during the SSH session.")
(80 . "Failed to shut down the SSL connection.")
(82 . "Could not load CRL file, missing or wrong format (added in 7.19.0).")
(83 . "Issuer check failed (added in 7.19.0).")
(84 . "The FTP PRET command failed")
(85 . "RTSP: mismatch of CSeq numbers")
(86 . "RTSP: mismatch of Session Identifiers")
(87 . "unable to parse FTP file list")
(88 . "FTP chunk callback reported error")
(89 . "No connection available, the session will be queued")
(90 . "SSL public key does not matched pinned public key"))
"Alist mapping curl error code integers to helpful error messages.")
;;;; Customization
(defgroup plz nil
"Options for `plz'."
:group 'network
:link '(url-link "https://github.com/alphapapa/plz.el"))
(defcustom plz-curl-program "curl"
"Name of curl program to call."
:type 'string)
(defcustom plz-curl-default-args
'("--silent"
"--compressed"
"--location")
"Default arguments to curl.
Note that these arguments are passed on the command line, which
may be visible to other users on the local system."
:type '(repeat string))
(defcustom plz-connect-timeout 5
"Default connection timeout in seconds.
This limits how long the connection phase may last (the
\"--connect-timeout\" argument to curl)."
:type 'number)
;;;; Macros
(require 'warnings)
(cl-defmacro plz-debug (&rest args)
;; Copied from `ement-debug' in Ement.el, which see.
"Display a debug warning showing the run-time value of ARGS.
The warning automatically includes the name of the containing
function, and it is only displayed if `warning-minimum-log-level'
is `:debug' at expansion time (otherwise the macro expands to a
call to `ignore' with ARGS and is eliminated by the
byte-compiler). When debugging, the form also returns nil so,
e.g. it may be used in a conditional in place of nil.
Each of ARGS may be a string, which is displayed as-is, or a
symbol, the value of which is displayed prefixed by its name, or
a Lisp form, which is displayed prefixed by its first symbol.
Before the actual ARGS arguments, you can write keyword
arguments, i.e. alternating keywords and values. The following
keywords are supported:
:buffer BUFFER Name of buffer to pass to `display-warning'.
:level LEVEL Level passed to `display-warning', which see.
Default is :debug."
;; TODO: Can we use a compiler macro to handle this more elegantly?
(pcase-let* ((fn-name (when byte-compile-current-buffer
(with-current-buffer byte-compile-current-buffer
;; This is a hack, but a nifty one.
(save-excursion
(beginning-of-defun)
(cl-second (read (current-buffer)))))))
(plist-args (cl-loop while (keywordp (car args))
collect (pop args)
collect (pop args)))
((map (:buffer buffer) (:level level)) plist-args)
(level (or level :debug))
(string (cl-loop for arg in args
concat (pcase arg
((pred stringp) "%S ")
((pred symbolp)
(concat (upcase (symbol-name arg)) ":%S "))
((pred listp)
(concat "(" (upcase (symbol-name (car arg)))
(pcase (length arg)
(1 ")")
(_ "...)"))
":%S "))))))
(if (eq :debug warning-minimum-log-level)
`(let ((fn-name ,(if fn-name
`',fn-name
;; In an interpreted function: use `backtrace-frame' to get the
;; function name (we have to use a little hackery to figure out
;; how far up the frame to look, but this seems to work).
`(cl-loop for frame in (backtrace-frames)
for fn = (cl-second frame)
when (not (or (subrp fn)
(special-form-p fn)
(eq 'backtrace-frames fn)))
return (make-symbol (format "%s [interpreted]" fn))))))
(display-warning fn-name (format ,string ,@args) ,level ,buffer)
nil)
`(ignore ,@args))))
;;;; Compatibility
(defalias 'plz--generate-new-buffer
(if (version< emacs-version "28.1")
(lambda (name &optional _inhibit-buffer-hooks)
"Call `generate-new-buffer' with NAME.
Compatibility function for Emacs versions <28.1."
(generate-new-buffer name))
#'generate-new-buffer))
;;;; Functions
;;;;; Public
(cl-defun plz (method url &rest rest &key headers body else filter finally noquery timeout
(as 'string) (then 'sync)
(body-type 'text) (decode t decode-s)
(connect-timeout plz-connect-timeout))
"Request METHOD from URL with curl.
Return the curl process object or, for a synchronous request, the
selected result.
HEADERS may be an alist of extra headers to send with the
request.
BODY may be a string, a buffer, or a list like `(file FILENAME)'
to upload a file from disk.
BODY-TYPE may be `text' to send BODY as text, or `binary' to send
it as binary.
AS selects the kind of result to pass to the callback function
THEN, or the kind of result to return for synchronous requests.
It may be:
- `buffer' to pass the response buffer, which will be narrowed to
the response body and decoded according to DECODE.
- `binary' to pass the response body as an un-decoded string.
- `string' to pass the response body as a decoded string.
- `response' to pass a `plz-response' structure.
- `file' to pass a temporary filename to which the response body
has been saved without decoding.
- `(file FILENAME)' to pass FILENAME after having saved the
response body to it without decoding. FILENAME must be a
non-existent file; if it exists, it will not be overwritten,
and an error will be signaled. FILENAME is passed through
`expand-file-name', which see.
- A function, which is called in the response buffer with it
narrowed to the response body (suitable for, e.g. `json-read').
If DECODE is non-nil, the response body is decoded automatically.
For binary content, it should be nil. When AS is `binary',
DECODE is automatically set to nil.
THEN is a callback function, whose sole argument is selected
above with AS; if the request fails and no ELSE function is
given (see below), the argument will be a `plz-error' structure
describing the error. Or THEN may be `sync' to make a
synchronous request, in which case the result is returned
directly from this function.
ELSE is an optional callback function called when the request
fails (i.e. if curl fails, or if the HTTP response has a non-2xx
status code). It is called with one argument, a `plz-error'
structure. If ELSE is nil, a `plz-curl-error' or
`plz-http-error' is signaled when the request fails, with a
`plz-error' structure as the error data. For synchronous
requests, this argument is ignored.
NOTE: In a future version of `plz', only one error will be
signaled: `plz-error'. The existing errors, `plz-curl-error' and
`plz-http-error', inherit from `plz-error' to allow applications
to update their code while using earlier versions (i.e. any
`condition-case' forms should now handle only `plz-error', not
the other two).
FINALLY is an optional function called without argument after
THEN or ELSE, as appropriate. For synchronous requests, this
argument is ignored.
CONNECT-TIMEOUT and TIMEOUT are a number of seconds that limit
how long it takes to connect to a host and to receive a complete
response from a host, respectively.
NOQUERY is passed to `make-process', which see.
FILTER is an optional function to be used as the process filter
for the curl process. It can be used to handle HTTP responses in
a streaming way. The function must accept 2 arguments, the
process object running curl, and a string which is output
received from the process. The default process filter inserts
the output of the process into the process buffer. The provided
FILTER function should at least insert output up to the HTTP body
into the process buffer.
\(To silence checkdoc, we mention the internal argument REST.)"
;; FIXME(v0.10): Remove the note about error changes from the docstring.
;; FIXME(v0.10): Update error signals in docstring.
(declare (indent defun))
(setf decode (if (and decode-s (not decode))
nil decode))
;; NOTE: By default, for PUT requests and POST requests >1KB, curl sends an
;; "Expect:" header, which causes servers to send a "100 Continue" response, which
;; we don't want to have to deal with, so we disable it by setting the header to
;; the empty string. See <https://gms.tf/when-curl-sends-100-continue.html>.
;; TODO: Handle "100 Continue" responses and remove this workaround.
(push (cons "Expect" "") headers)
(let* (filename
(data-arg (pcase-exhaustive body-type
('binary "--data-binary")
('text "--data")))
(curl-command-line-args (append plz-curl-default-args
(list "--config" "-")))
(curl-config-header-args (cl-loop for (key . value) in headers
collect (cons "--header" (format "%s: %s" key value))))
(curl-config-args (append curl-config-header-args
(list (cons "--url" url)
(cons "--create-dirs" "")
(cons "--request" (upcase (symbol-name method)))
(cons "--dump-header" "-"))
(when connect-timeout
(list (cons "--connect-timeout"
(number-to-string connect-timeout))))
(when timeout
(list (cons "--max-time" (number-to-string timeout))))
(pcase as
('file
(setf filename (make-temp-file "plz-"))
(list (cons "--output" filename)))
(`(file ,(and (pred stringp) as-filename))
(when (file-exists-p as-filename)
(error "File exists, will not overwrite: %S" as-filename))
;; Use `expand-file-name' because curl doesn't
;; expand, e.g. "~" into "/home/...".
(setf filename (expand-file-name as-filename))
(list (cons "--output" filename)))
((guard (eq 'head method))
;; Don't duplicate headers for HEAD
;; requests which output to the terminal.
(list (cons "--dump-header" null-device))))
(pcase method
((or 'put 'post 'patch)
(list
;; It appears that this must be the last argument
;; in order to pass data on the rest of STDIN.
(pcase body
(`(file ,filename)
;; Use `expand-file-name' because curl doesn't
;; expand, e.g. "~" into "/home/...".
(cons "--upload-file" (expand-file-name filename)))
(_ (cons data-arg "@-")))))
('head
(list (cons "--head" ""))))))
(curl-config (cl-loop for (key . value) in curl-config-args
concat (format "%s \"%s\"\n" key value)))
(decode (pcase as
('binary nil)
(_ decode)))
(default-directory
;; Avoid making process in a nonexistent directory (in case the current
;; default-directory has since been removed). It's unclear what the best
;; directory is, but this seems to make sense, and it should still exist.
temporary-file-directory)
(process-buffer (plz--generate-new-buffer " *plz-request-curl*" t))
(stderr-process (make-pipe-process :name "plz-request-curl-stderr"
:buffer (plz--generate-new-buffer " *plz-request-curl-stderr*" t)
:noquery t
:sentinel #'plz--stderr-sentinel))
(process (make-process :name "plz-request-curl"
:buffer process-buffer
:coding 'binary
:command (append (list plz-curl-program) curl-command-line-args)
:connection-type 'pipe
:filter filter
:sentinel #'plz--sentinel
:stderr stderr-process
:noquery noquery))
sync-p)
(when (eq 'sync then)
(setf sync-p t
then (lambda (result)
(process-put process :plz-result result))
else nil))
(setf
;; Set the callbacks, etc. as process properties.
(process-get process :plz-then)
(pcase-exhaustive as
((or 'binary 'string)
(lambda ()
(let ((coding-system (or (plz--coding-system) 'utf-8)))
(pcase as
('binary (set-buffer-multibyte nil)))
(plz--narrow-to-body)
(when decode
(decode-coding-region (point) (point-max) coding-system))
(funcall then (or (buffer-string)
(make-plz-error :message (format "buffer-string is nil in buffer:%S" process-buffer)))))))
('buffer (progn
(setf (process-get process :plz-as) 'buffer)
(lambda ()
(let ((coding-system (or (plz--coding-system) 'utf-8)))
(pcase as
('binary (set-buffer-multibyte nil)))
(plz--narrow-to-body)
(when decode
(decode-coding-region (point) (point-max) coding-system)))
(funcall then (current-buffer)))))
('response (lambda ()
(funcall then (or (plz--response :decode-p decode)
(make-plz-error :message (format "response is nil for buffer:%S buffer-string:%S"
process-buffer (buffer-string)))))))
('file (lambda ()
(funcall then filename)))
(`(file ,(and (pred stringp) filename))
;; This requires a separate clause due to the FILENAME binding.
(lambda ()
(funcall then filename)))
((pred functionp) (lambda ()
(let ((coding-system (or (plz--coding-system) 'utf-8)))
(plz--narrow-to-body)
(when decode
(decode-coding-region (point) (point-max) coding-system))
(funcall then (funcall as))))))
(process-get process :plz-else) else
(process-get process :plz-finally) finally
(process-get process :plz-sync) sync-p
;; Record list of arguments for debugging purposes (e.g. when
;; using Edebug in a process buffer, this allows determining
;; which request the buffer is for).
(process-get process :plz-args) (apply #'list method url rest)
;; HACK: We set the result to a sentinel value so that any other
;; value, even nil, means that the response was processed, and
;; the sentinel does not need to be called again (see below).
(process-get process :plz-result) :plz-result)
;; Send --config arguments.
(process-send-string process curl-config)
(when body
(cl-typecase body
(string (process-send-string process body))
(buffer (with-current-buffer body
(process-send-region process (point-min) (point-max))))))
(process-send-eof process)
(if sync-p
(unwind-protect
(with-local-quit
;; See Info node `(elisp)Accepting Output'.
(unless (and process stderr-process)
(error "Process unexpectedly nil"))
(while (accept-process-output process))
(while (accept-process-output stderr-process))
(plz-debug (float-time) "BEFORE HACK" (process-buffer process))
(when (eq :plz-result (process-get process :plz-result))
(plz-debug (float-time) "INSIDE HACK" (process-buffer process))
;; HACK: Sentinel seems to not have been called: call it again. (Although
;; this is a hack, it seems to be a necessary one due to Emacs's process
;; handling.) See <https://github.com/alphapapa/plz.el/issues/3> and
;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=50166>.
(plz--sentinel process "workaround")
(plz-debug (float-time) "INSIDE HACK, AFTER CALLING SENTINEL" (process-buffer process))
(when (eq :plz-result (process-get process :plz-result))
(error "Plz: NO RESULT FROM PROCESS:%S ARGS:%S"
process rest)))
(plz-debug (float-time) "AFTER HACK" (process-buffer process))
;; Sentinel seems to have been called: check the result.
(pcase (process-get process :plz-result)
((and (pred plz-error-p) data)
;; The AS function signaled an error, which was collected
;; into a `plz-error' struct: re-signal the error here,
;; outside of the sentinel.
(if (plz-error-response data)
;; FIXME(v0.10): Signal only plz-error.
(signal 'plz-http-error (list "HTTP error" data))
(signal 'plz-curl-error (list "Curl error" data))))
(else
;; The AS function returned a value: return it.
else)))
(unless (eq as 'buffer)
(plz--kill-buffer process-buffer))
(plz--kill-buffer (process-buffer stderr-process)))
;; Async request: return the process object.
process)))
;;;;; Queue
;; A simple queue system.
(cl-defstruct plz-queued-request
"Structure representing a queued `plz' HTTP request.
For more details on these slots, see arguments to the function
`plz'."
method url headers body else finally noquery
as then body-type decode
connect-timeout timeout
next previous process)
(cl-defstruct plz-queue
"Structure forming a queue for `plz' requests.
The queue may be appended to (the default) and pre-pended to, and
items may be removed from the front of the queue (i.e. by
default, it's FIFO). Use functions `plz-queue', `plz-run', and
`plz-clear' to queue, run, and clear requests, respectively."
(limit 1
:documentation "Number of simultaneous requests.")
(active nil
:documentation "Active requests.")
(requests nil
:documentation "Queued requests.")
(canceled-p nil
:documentation "Non-nil when queue has been canceled.")
first-active last-active
first-request last-request
(finally nil
:documentation "Function called with no arguments after queue has been emptied or canceled."))
(defun plz-queue (queue &rest args)
"Queue request for ARGS on QUEUE and return QUEUE.
To pre-pend to QUEUE rather than append, it may be a list of the
form (`prepend' QUEUE). QUEUE is a `plz-request' queue. ARGS
are those passed to `plz', which see. Use `plz-run' to start
making QUEUE's requests."
(declare (indent defun))
(cl-assert (not (equal 'sync (plist-get (cddr args) :then))) nil
"Only async requests may be queued")
(pcase-let* ((`(,method ,url . ,rest) args)
(args `(:method ,method :url ,url ,@rest))
(request (apply #'make-plz-queued-request args)))
(pcase queue
(`(prepend ,queue) (plz--queue-prepend request queue))
(_ (plz--queue-append request queue))))
queue)
(defun plz--queue-append (request queue)
"Add REQUEST to end of QUEUE and return QUEUE."
(cl-check-type request plz-queued-request
"REQUEST must be a `plz-queued-request' structure.")
(cl-check-type queue plz-queue
"QUEUE must be a `plz-queue' structure.")
(when (plz-queue-last-request queue)
(setf (plz-queued-request-next (plz-queue-last-request queue)) request))
(setf (plz-queued-request-previous request) (plz-queue-last-request queue)
(plz-queue-last-request queue) request)
(unless (plz-queue-first-request queue)
(setf (plz-queue-first-request queue) request))
(unless (plz-queue-last-request queue)
(setf (plz-queue-last-request queue) request))
(push request (plz-queue-requests queue))
queue)
(defun plz--queue-prepend (request queue)
"Add REQUEST to front of QUEUE and return QUEUE."
(cl-check-type request plz-queued-request
"REQUEST must be a `plz-queued-request' structure.")
(cl-check-type queue plz-queue
"QUEUE must be a `plz-queue' structure.")
(when (plz-queue-requests queue)
(setf (plz-queued-request-next request) (car (plz-queue-requests queue))
(plz-queued-request-previous (plz-queued-request-next request)) request))
(setf (plz-queue-first-request queue) request)
(unless (plz-queue-first-request queue)
(setf (plz-queue-first-request queue) request))
(unless (plz-queue-last-request queue)
(setf (plz-queue-last-request queue) request))
(push request (plz-queue-requests queue))
queue)
(defun plz--queue-pop (queue)
"Return the first queued request on QUEUE and remove it from QUEUE."
(let* ((request (plz-queue-first-request queue))
(next (plz-queued-request-next request)))
(when next
(setf (plz-queued-request-previous next) nil))
(setf (plz-queue-first-request queue) next
(plz-queue-requests queue) (delq request (plz-queue-requests queue)))
(when (eq request (plz-queue-last-request queue))
(setf (plz-queue-last-request queue) nil))
request))
(defun plz-run (queue)
"Process requests in QUEUE and return QUEUE.
Return when QUEUE is at limit or has no more queued requests.
QUEUE should be a `plz-queue' structure."
(cl-labels ((readyp (queue)
(and (not (plz-queue-canceled-p queue))
(plz-queue-requests queue)
;; With apologies to skeeto...
(< (length (plz-queue-active queue)) (plz-queue-limit queue)))))
(while (readyp queue)
(pcase-let* ((request (plz--queue-pop queue))
((cl-struct plz-queued-request method url
headers body finally noquery as body-type decode connect-timeout timeout
(else orig-else) (then orig-then))
request)
(then (lambda (response)
(unwind-protect
;; Ensure any errors in the THEN function don't abort the queue.
(funcall orig-then response)
(setf (plz-queue-active queue) (delq request (plz-queue-active queue)))
(plz-run queue))))
(else (lambda (arg)
;; FIXME(v0.10): This should be done in `plz-queue' because
;; `plz-clear' will call the second queued-request's ELSE
;; before it can be set by `plz-run'.
(unwind-protect
;; Ensure any errors in the THEN function don't abort the queue.
(when orig-else
(funcall orig-else arg))
(setf (plz-queue-active queue) (delq request (plz-queue-active queue)))
(plz-run queue))))
(args (list method url
;; Omit arguments for which `plz' has defaults so as not to nil them.
:headers headers :body body :finally finally :noquery noquery
:connect-timeout connect-timeout :timeout timeout)))
;; Add arguments which override defaults.
(when as
(setf args (plist-put args :as as)))
(when else
(setf args (plist-put args :else else)))
(when then
(setf args (plist-put args :then then)))
(when decode
(setf args (plist-put args :decode decode)))
(when body-type
(setf args (plist-put args :body-type body-type)))
(when connect-timeout
(setf args (plist-put args :connect-timeout connect-timeout)))
(when timeout
(setf args (plist-put args :timeout timeout)))
(setf (plz-queued-request-process request) (apply #'plz args))
(push request (plz-queue-active queue))))
(when (and (plz-queue-finally queue)
(zerop (length (plz-queue-active queue)))
(zerop (length (plz-queue-requests queue))))
(funcall (plz-queue-finally queue)))
queue))
(defun plz-clear (queue)
"Clear QUEUE and return it.
Cancels any active or pending requests and calls the queue's
FINALLY function. For pending requests, their ELSE functions
will be called with a `plz-error' structure with the message,
\"`plz' queue cleared; request canceled.\"; active requests will
have their curl processes killed and their ELSE functions called
with the corresponding data."
(setf (plz-queue-canceled-p queue) t)
(dolist (request (plz-queue-active queue))
(when (process-live-p (plz-queued-request-process request))
(kill-process (plz-queued-request-process request)))
(setf (plz-queue-active queue) (delq request (plz-queue-active queue))))
(dolist (request (plz-queue-requests queue))
(funcall (plz-queued-request-else request)
(make-plz-error :message "`plz' queue cleared; request canceled."))
(setf (plz-queue-requests queue) (delq request (plz-queue-requests queue))))
(when (plz-queue-finally queue)
(funcall (plz-queue-finally queue)))
(setf (plz-queue-first-active queue) nil
(plz-queue-last-active queue) nil
(plz-queue-first-request queue) nil
(plz-queue-last-request queue) nil
(plz-queue-canceled-p queue) nil)
queue)
(defun plz-length (queue)
"Return number of of QUEUE's outstanding requests.
Includes active and queued requests."
(+ (length (plz-queue-active queue))
(length (plz-queue-requests queue))))
;;;;; Private
(defun plz--sentinel (process status)
"Sentinel for curl PROCESS.
STATUS should be the process's event string (see info
node `(elisp) Sentinels'). Calls `plz--respond' to process the
HTTP response (directly for synchronous requests, or from a timer
for asynchronous ones)."
(plz-debug (float-time) "BEFORE CONDITION"
process status (process-get process :plz-result))
(if (eq :plz-result (process-get process :plz-result))
;; Result not yet set: check process status (we call
;; `process-status' because the STATUS argument might not be
;; accurate--see "hack" in `plz').
(if (member (process-status process) '(run stop))
;; Process still alive: do nothing.
(plz-debug "Doing nothing because:" (process-status process))
;; Process appears to be dead: check STATUS argument.
(pcase status
((or "finished\n" "killed\n" "interrupt\n" "workaround"
(pred numberp)
(rx "exited abnormally with code " (group (1+ digit))))
;; STATUS seems okay: call `plz--respond'.
(let ((buffer (process-buffer process)))
(if (process-get process :plz-sync)
(plz--respond process buffer status)
(run-at-time 0 nil #'plz--respond process buffer status))))))
;; Result already set (likely indicating that Emacs did not call
;; the sentinel when `accept-process-output' was called, so we are
;; either being called from our "hack", or being called a second
;; time, after `plz' returned): do nothing.
(plz-debug (float-time) ":PLZ-RESULT ALREADY CHANGED"
process status (process-get process :plz-result))))
(defun plz--respond (process buffer status)
"Respond to HTTP response from PROCESS in BUFFER.
Parses the response and calls the THEN/ELSE callbacks
accordingly. To be called from `plz--sentinel'. STATUS is the
argument passed to `plz--sentinel', which see."
;; Is it silly to call this function "please respond"? Perhaps, but
;; naming things is hard. The term "process" has another meaning in
;; this context, and the old standby, "handle," is much overused.
;; "Respond" also means "to react to something," which is what this
;; does--react to receiving the HTTP response--and it's an internal
;; name, so why not.
(plz-debug (float-time) process status (process-status process) buffer)
(unwind-protect
(pcase-exhaustive (process-exit-status process)
(0
;; Curl exited normally: check HTTP status code.
(with-current-buffer buffer
;; NOTE: We only switch to the process's buffer if curl
;; exited successfully.
(goto-char (point-min))
(plz--skip-proxy-headers)
(while (plz--skip-redirect-headers))
(pcase (plz--http-status)
((and status (guard (<= 200 status 299)))
;; Any 2xx response is considered successful.
(ignore status) ; Byte-compiling in Emacs <28 complains without this.
(funcall (process-get process :plz-then)))
(_
;; TODO: If using ":as 'response", the HTTP response
;; should be passed to the THEN function, regardless
;; of the status code. Only for curl errors should
;; the ELSE function be called. (Maybe in v0.10.)
;; Any other status code is considered unsuccessful
;; (for now, anyway).
(let ((err (make-plz-error :response (plz--response))))
(pcase-exhaustive (process-get process :plz-else)
(`nil (process-put process :plz-result err))
((and (pred functionp) fn) (funcall fn err))))))))
((and code (guard (<= 1 code 90)))
;; Curl exited non-zero.
(let* ((curl-exit-code (cl-typecase code
(string (string-to-number code))
(number code)))
(curl-error-message (alist-get curl-exit-code plz-curl-errors))
(err (make-plz-error :curl-error (cons curl-exit-code curl-error-message))))
(pcase-exhaustive (process-get process :plz-else)
(`nil (process-put process :plz-result err))
((and (pred functionp) fn) (funcall fn err)))))
((and code (guard (not (<= 1 code 90))))
;; If we are here, it should mean that the curl process was
;; killed or interrupted, and the code should be something
;; not (<= 1 code 90).
(let* ((message (pcase status
("killed\n" "curl process killed")
("interrupt\n" "curl process interrupted")
(_ (format "Unexpected curl process status:%S code:%S. Please report this bug to the `plz' maintainer." status code))))
(err (make-plz-error :message message)))
(pcase-exhaustive (process-get process :plz-else)
(`nil (process-put process :plz-result err))
((and (pred functionp) fn) (funcall fn err)))))
(code
;; If we are here, something is really wrong.
(let* ((message (format "Unexpected curl process status:%S code:%S. Please report this bug to the `plz' maintainer." status code))
(err (make-plz-error :message message)))
(pcase-exhaustive (process-get process :plz-else)
(`nil (process-put process :plz-result err))
((and (pred functionp) fn) (funcall fn err))))))
(when-let ((finally (process-get process :plz-finally)))
(funcall finally))
(unless (or (process-get process :plz-sync)
(eq 'buffer (process-get process :plz-as)))
(plz--kill-buffer buffer))))
(defun plz--stderr-sentinel (process status)
"Sentinel for STDERR buffer.
Arguments are PROCESS and STATUS (ok, checkdoc?)."
(pcase status
((or "finished\n" "killed\n" "interrupt\n"
(pred numberp)
(rx "exited abnormally with code " (1+ digit)))
(plz--kill-buffer (process-buffer process)))))
(defun plz--kill-buffer (&optional buffer)
"Kill BUFFER unconditionally, without asking for confirmation.
Binds `kill-buffer-query-functions' to nil."
;; TODO(emacs-28): Remove this workaround when requiring Emacs 28+.
(let (kill-buffer-query-functions)
(kill-buffer buffer)))
;;;;;; HTTP Responses
;; Functions for parsing HTTP responses.
(defun plz--skip-proxy-headers ()
"Skip proxy headers in current buffer."
(when (looking-at plz-http-response-status-line-regexp)
(let* ((status-code (string-to-number (match-string 2)))
(reason-phrase (match-string 3)))
(when (and (equal 200 status-code)
(equal "Connection established" reason-phrase))
;; Skip proxy headers (curl apparently offers no way to omit
;; them).
(unless (re-search-forward "\r\n\r\n" nil t)
(signal 'plz-http-error '("plz--response: End of proxy headers not found")))))))
(defun plz--skip-redirect-headers ()
"Skip HTTP redirect headers in current buffer."
(when (and (looking-at plz-http-response-status-line-regexp)
(member (string-to-number (match-string 2)) '(301 302 303 307 308)))
;; Skip redirect headers ("--dump-header" forces redirect headers to be included
;; even when used with "--location").
(or (re-search-forward "\r\n\r\n" nil t)
(signal 'plz-http-error '("plz--response: End of redirect headers not found")))))
(cl-defun plz--response (&key (decode-p t))
"Return response structure for HTTP response in current buffer.
When DECODE-P is non-nil, decode the response body automatically
according to the apparent coding system.
Assumes that point is at beginning of HTTP response."
(save-excursion
;; Parse HTTP version and status code.
(unless (looking-at plz-http-response-status-line-regexp)
(signal 'plz-http-error
(list "plz--response: Unable to parse HTTP response status line"
(buffer-substring (point) (line-end-position)))))
(let* ((http-version (string-to-number (match-string 1)))
(status-code (string-to-number (match-string 2)))
(headers (plz--headers))
(coding-system (or (plz--coding-system headers) 'utf-8)))
(plz--narrow-to-body)
(when decode-p
(decode-coding-region (point) (point-max) coding-system))
(make-plz-response
:version http-version
:status status-code
:headers headers
:body (buffer-string)))))
(defun plz--coding-system (&optional headers)
"Return coding system for HTTP response in current buffer.
HEADERS may optionally be an alist of parsed HTTP headers to
refer to rather than the current buffer's un-parsed headers."
(let* ((headers (or headers (plz--headers)))
(content-type (alist-get 'content-type headers)))
(when content-type
(coding-system-from-name content-type))))
(defun plz--http-status ()
"Return HTTP status code for HTTP response in current buffer.
Assumes point is at start of HTTP response."
(when (looking-at plz-http-response-status-line-regexp)
(string-to-number (match-string 2))))
(defun plz--headers ()
"Return headers alist for HTTP response in current buffer.
Assumes point is at start of HTTP response."
(save-excursion
(forward-line 1)
(let ((limit (save-excursion
(re-search-forward plz-http-end-of-headers-regexp nil)
(point))))
(cl-loop while (re-search-forward (rx bol (group (1+ (not (in ":")))) ":" (1+ blank)
(group (1+ (not (in "\r\n")))))
limit t)
;; NOTE: Some HTTP servers send all-lowercase header keys, which means an alist
;; lookup with `equal' or `string=' fails when the case differs. We don't want
;; users to have to worry about this, so for consistency, we downcase the
;; header name. And while we're at it, we might as well intern it so we can
;; use `alist-get' without having to add "nil nil #'equal" every time.
collect (cons (intern (downcase (match-string 1))) (match-string 2))))))
(defun plz--narrow-to-body ()
"Narrow to body of HTTP response in current buffer.
Assumes point is at start of HTTP response."
(unless (re-search-forward plz-http-end-of-headers-regexp nil t)
(signal 'plz-http-error '("plz--narrow-to-body: Unable to find end of headers")))
(narrow-to-region (point) (point-max)))
;;;; Footer
(provide 'plz)
;;; plz.el ends here
================================================
FILE: tests/test-plz.el
================================================
;;; test-plz.el --- Tests for plz -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2023 Free Software Foundation, Inc.
;; Author: Adam Porter <adam@alphapapa.net>
;; Maintainer: Adam Porter <adam@alphapapa.net>
;; This file is part of GNU Emacs.
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; 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
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file implements tests for `plz'. By default, the requests are
;; made to "localhost", expecting an instance of httpbin
;; <https://github.com/postmanlabs/httpbin> running on port 80; it's
;; convenient to use the Docker image "kennethreitz/httpbin". By
;; changing the variable `plz-test-uri-prefix', the tests can be run
;; against other URLs, such as <https://httpbin.org> (but that server
;; is often overloaded, making for unreliable tests, so a local
;; instance is preferred).
;;; Code:
;;;; Requirements
(require 'ert)
(require 'json)
(require 'let-alist)
(require 'map)
(require 'plz)
;;;; Variables
(defvar plz-test-uri-prefix
;; "https://httpbin.org"
"http://localhost"
"URI prefix for HTTP requests, without trailing slash.
If running httpbin locally, set to \"http://localhost\".")
;;;; Customization
;;;; Commands
;;;; Macros
(cl-defun plz-test-wait (process &optional (seconds 0.1) (times 100))
"Wait for SECONDS seconds TIMES times for PROCESS to finish."
(when process
;; Sometimes it seems that the process is killed, the THEN
;; function called by its sentinel, and its buffer killed, all
;; before this function gets called with the process argument;
;; when that happens, tests that use this can fail. Testing
;; whether PROCESS is non-nil seems to fix it, but it's possible
;; that something funny is going on...
(cl-loop for i upto times ;; 10 seconds
while (equal 'run (process-status process))
do (sleep-for seconds))))
(cl-defmacro plz-deftest (name () &body docstring-keys-and-body)
"Like `ert-deftest', but defines tests for both HTTP/1.1 and HTTP/2.
Also defines local function `url' which returns its argument
appended to `plz-test-uri-prefix' (and any instance of
\"URI-PREFIX\" in URL-PART is replaced with `plz-test-uri-prefix'
in URL-encoded form)."
(declare (debug (&define [&name "test@" symbolp]
sexp [&optional stringp]
[&rest keywordp sexp] def-body))
(doc-string 3)
(indent 2))
`(progn
,@(cl-loop for http-version in '("1.1" "2")
collect (let ((name (intern (format "%s-http%s" name http-version))))
`(ert-deftest ,name ()
(let ((plz-curl-default-args
',(append plz-curl-default-args (list (format "--http%s" http-version)))))
(cl-labels ((url (part)
(setf part (replace-regexp-in-string
"URI-PREFIX" (url-hexify-string plz-test-uri-prefix)
part t t))
(concat plz-test-uri-prefix part)))
,@docstring-keys-and-body)))))))
;;;; Functions
(defmacro plz-test-get-response (response)
"Test parts of RESPONSE with `should'."
`(progn
(should (plz-response-p ,response))
(should (numberp (plz-response-version ,response)))
(should (eq 200 (plz-response-status ,response)))
(should (equal "application/json" (alist-get 'content-type (plz-response-headers ,response))))
(should (string-match "curl"
(map-nested-elt (json-read-from-string (plz-response-body ,response))
'(headers User-Agent))))))
;;;; Tests
;;;;; Async
(plz-deftest plz-get-string nil
(let* ((test-string)
(process (plz 'get (url "/get")
:as 'string
:then (lambda (string)
(setf test-string string)))))
(plz-test-wait process)
(should (string-match "curl" test-string))))
(plz-deftest plz-get-buffer nil
(let* ((result-buffer)
(process (plz 'get (url "/get")
:as 'buffer :then (lambda (buffer)
(setf result-buffer buffer)))))
(unwind-protect
(progn
(plz-test-wait process)
(should (buffer-live-p result-buffer))
(with-current-buffer result-buffer
(should-not (looking-at-p plz-http-response-status-line-regexp))
(should (string-match "curl" (buffer-string)))))
(kill-buffer result-buffer)
(should-not (buffer-live-p result-buffer)))))
(plz-deftest plz-get-response nil
(let* ((test-response)
(process (plz 'get (url "/get")
:as 'response
:then (lambda (response)
(setf test-response response)))))
(plz-test-wait process)
(plz-test-get-response test-response)))
(plz-deftest plz-get-json nil
(let* ((test-json)
(process (plz 'get (url "/get")
:as #'json-read
:then (lambda (json)
(setf test-json json)))))
(plz-test-wait process)
(let-alist test-json
(should (string-match "curl" .headers.User-Agent)))))
(plz-deftest plz-post-json-string nil
(let* ((json-string (json-encode (list (cons "key" "value"))))
(response-json)
(process (plz 'post (url "/post")
:headers '(("Content-Type" . "application/json"))
:body json-string
:as #'json-read
:then (lambda (json)
(setf response-json json)))))
(plz-test-wait process)
(let-alist response-json
(should (string-match "curl" .headers.User-Agent))
(should (string= "value" (alist-get 'key (json-read-from-string .data)))))))
(plz-deftest plz-patch-json-string nil
(let* ((json-string (json-encode (list (cons "key" "value"))))
(response-json)
(process (plz 'patch (url "/patch")
:headers '(("Content-Type" . "application/json"))
:body json-string
:as #'json-read
:then (lambda (json)
(setf response-json json)))))
(plz-test-wait process)
(let-alist response-json
(should (string-match "curl" .headers.User-Agent))
(should (string= "value" (alist-get 'key (json-read-from-string .data)))))))
(plz-deftest plz-post-jpeg-string nil
(let* ((jpeg-to-upload (plz 'get (url "/image/jpeg")
:as 'binary :then 'sync))
(_ (unless jpeg-to-upload
(error "jpeg-to-upload is nil")))
(response-json)
(response-jpeg)
(process (plz 'post (url "/post")
:headers '(("Content-Type" . "image/jpeg"))
:body jpeg-to-upload :body-type 'binary
:as #'json-read
:then (lambda (json)
(setf response-json json
response-jpeg
(base64-decode-string
(string-remove-prefix "data:application/octet-stream;base64,"
(alist-get 'data json))))))))
(should (equal 'jpeg (image-type-from-data jpeg-to-upload)))
(plz-test-wait process)
(should response-json)
(should (equal 'jpeg (image-type-from-data response-jpeg)))
(should (equal (length jpeg-to-upload) (length response-jpeg)))
(should (equal jpeg-to-upload response-jpeg))))
;; TODO: POST JSON buffer.
(plz-deftest plz-put-json-string nil
(let* ((json-string (json-encode (list (cons "key" "value"))))
(response-json)
(process (plz 'put (url "/put")
:headers '(("Content-Type" . "application/json"))
:body json-string
:as #'json-read
:then (lambda (json)
(setf response-json json)))))
(plz-test-wait process)
(let-alist response-json
(should (string-match "curl" .headers.User-Agent))
(should (string= "value" (alist-get 'key (json-read-from-string .data)))))))
;; TODO: Put JSON buffer.
;;;;; Sync
(plz-deftest plz-get-string-sync nil
(let-alist (json-read-from-string (plz 'get (url "/get")
:as 'string :then 'sync))
(should (equal (url "/get") .url))))
(plz-deftest plz-get-response-sync nil
(plz-test-get-response (plz 'get (url "/get")
:as 'response :then 'sync)))
(plz-deftest plz-get-json-sync nil
(let-alist (plz 'get (url "/get")
:as #'json-read :then 'sync)
(should (string-match "curl" .headers.User-Agent))))
(plz-deftest plz-get-buffer-sync nil
(let ((buffer (plz 'get (url "/get")
:as 'buffer :then 'sync)))
(unwind-protect
(should (buffer-live-p buffer))
(kill-buffer buffer))))
;;;;; Headers
;; These tests were added when plz--curl was changed to send headers
;; with "--config" rather than on the command line.
(plz-deftest plz-get-with-headers ()
(let* ((response-json)
(process (plz 'get (url "/get")
:headers '(("X-Plz-Test-Header" . "plz-test-header-value"))
:as #'json-read
:then (lambda (json)
(setf response-json json)))))
(plz-test-wait process)
(let-alist response-json
(should (equal "plz-test-header-value" .headers.X-Plz-Test-Header)))))
(plz-deftest plz-post-with-headers ()
(let* ((alist (list (cons "key" "value")))
(response-json)
(process (plz 'post (url "/post")
:headers '(("Content-Type" . "application/json")
("X-Plz-Test-Header" . "plz-test-header-value"))
:body (json-encode alist)
:as #'json-read
:then (lambda (json)
(setf response-json json)))))
(plz-test-wait process)
(let-alist response-json
(should (equal "plz-test-header-value" .headers.X-Plz-Test-Header))
(should (equal "value" (alist-get 'key (json-read-from-string .data)))))))
(plz-deftest plz-get-json-with-headers-sync ()
(let-alist (plz 'get (url "/get")
:headers '(("X-Plz-Test-Header" . "plz-test-header-value"))
:as #'json-read :then 'sync)
(should (string-match "curl" .headers.User-Agent))
(should (equal "plz-test-header-value" .headers.X-Plz-Test-Header))))
;;;;; HEAD requests
;; NOTE: httpbin.org doesn't appear to support a "/head" endpoint,
;; so we'll use "/get".
(plz-deftest plz-head-without-headers ()
;; I'm not sure how useful it may be to make a HEAD request without
;; caring about the headers, but perhaps it could be useful as a
;; lightweight way to test a server's presence, so we should
;; probably support it. This merely tests that no error is
;; signaled, which should mean that the HEAD request succeeded.
(should (plz 'head (url "/get"))))
(plz-deftest plz-head-as-response ()
(let ((response (plz 'head (url "/get")
:as 'response)))
(should (equal "application/json"
(alist-get 'content-type
(plz-response-headers response))))))
;;;;; POST requests
(plz-deftest plz-post-empty-body ()
(should (equal ""
(alist-get 'data
(json-read-from-string
(plz 'post (url "/post"))))))
(should (equal "application/json"
(alist-get 'content-type
(plz-response-headers
(plz 'post (url "/post") :as 'response))))))
;;;;; Status codes
(plz-deftest plz-201-succeeds ()
;; This merely tests that a 201 response does not signal an error.
(should (plz 'get (url "/status/201"))))
(plz-deftest plz-400-errors ()
(should-error (plz 'get (url "/status/400"))))
(plz-deftest plz-500-errors ()
(should-error (plz 'get (url "/status/500"))))
;;;;; Redirects
(plz-deftest plz-301-redirects ()
(plz-test-get-response
(plz 'get (url "/redirect-to?url=URI-PREFIX%2Fget&status_code=301")
:as 'response :then 'sync)))
(plz-deftest plz-302-redirects ()
(plz-test-get-response
(plz 'get (url "/redirect-to?url=URI-PREFIX%2Fget&status_code=302")
:as 'response :then 'sync)))
(plz-deftest plz-307-redirects ()
(plz-test-get-response
(plz 'get (url "/redirect-to?url=URI-PREFIX%2Fget&status_code=307")
:as 'response :then 'sync)))
(plz-deftest plz-308-redirects ()
(plz-test-get-response
(plz 'get (url "/redirect-to?url=URI-PREFIX%2Fget&status_code=308")
:as 'response :then 'sync)))
;;;;; Errors
;; TODO: Sync requests with ":as 'response" should return response for errors rather than signaling.
(plz-deftest plz-get-curl-error-async nil
;; Async.
(let* ((err)
(process (plz 'get "https://httpbinnnnnn.org/get/status/404"
:as 'string :then #'ignore
:else (lambda (e)
(setf err e)))))
(plz-test-wait process)
(should (plz-error-p err))
(should (equal '(6 . "Couldn't resolve host. The given remote host was not resolved.")
(plz-error-curl-error err)))))
;; FIXME: This test works interactively but not in batch mode: it
;; stalls the Emacs process indefinitely, using either sleep-for or
;; sit-for.
;; (plz-deftest plz-get-killed-error nil
;; ;; Async.
;; (let* ((err)
;; (process (plz 'get "https://httpbinnnnnn.org/get/status/404"
;; :as 'string
;; :else (lambda (e)
;; (setf err e)))))
;; (sit-for 0.01)
;; (delete-process process)
;; (should (not (process-live-p process)))
;; (should (plz-error-p err))
;; (should (equal "curl process killed"
;; (plz-error-message err)))))
(plz-deftest plz-get-curl-error-sync nil
;; Sync.
(pcase-let ((`(,_signal . (,_message ,data))
(should-error (plz 'get "https://httpbinnnnnn.org/get/status/404"
:as 'string :then 'sync)
:type 'plz-error)))
(should (plz-error-p data))
(should (equal '(6 . "Couldn't resolve host. The given remote host was not resolved.")
(plz-error-curl-error data)))))
(plz-deftest plz-get-404-error-sync nil
(pcase-let ((`(,_signal . (,_message ,data))
(should-error (plz 'get (url "/get/status/404")
:as 'string :then 'sync)
:type 'plz-error)))
(should (plz-error-p data))
(should (plz-response-p (plz-error-response data)))
(should (eq 404 (plz-response-status (plz-error-response data))))))
(plz-deftest plz-get-404-error-async nil
(let* ((err)
(process (plz 'get (url "/get/status/404")
:as 'string :then #'ignore
:else (lambda (e)
(setf err e)))))
(plz-test-wait process)
(should (plz-error-p err))
(should (plz-response-p (plz-error-response err)))
(should (eq 404 (plz-response-status (plz-error-response err))))))
(plz-deftest plz-get-timeout-error-sync nil
(pcase-let* ((start-time (current-time))
(`(,_signal . (,_message ,(cl-struct plz-error (curl-error `(,code . ,message)))))
(should-error (plz 'get (url "/delay/5")
:as 'string :then 'sync :timeout 1)
:type 'plz-error))
(end-time (current-time)))
(should (eq 28 code))
(should (equal "Operation timeout." message))
(should (< (time-to-seconds (time-subtract end-time start-time)) 1.1))))
(plz-deftest plz-get-timeout-error-async nil
(let* ((start-time (current-time))
(end-time)
(plz-error)
(process (plz 'get (url "/delay/5")
:as 'response :timeout 1 :then #'ignore
:else (lambda (e)
(setf end-time (current-time)
plz-error e)))))
(plz-test-wait process)
(should (eq 28 (car (plz-error-curl-error plz-error))))
(should (equal "Operation timeout." (cdr (plz-error-curl-error plz-error))))
(should (< (time-to-seconds (time-subtract end-time start-time)) 1.1))))
;;;;; Finally
(plz-deftest plz-get-finally nil
(let* ((finally-null t)
(process (plz 'get (url "/get")
:as 'string
:then #'ignore
:finally (lambda ()
(setf finally-null nil)))))
(plz-test-wait process)
(should-not finally-null)))
;;;;; Binary
(plz-deftest plz-get-jpeg ()
(let* ((test-jpeg)
(process (plz 'get (url "/image/jpeg")
:as 'binary
:then (lambda (string)
(setf test-jpeg string)))))
(plz-test-wait process)
(should (equal 'jpeg (image-type-from-data test-jpeg)))))
(plz-deftest plz-get-jpeg-sync ()
(let ((jpeg (plz 'get (url "/image/jpeg")
:as 'binary :then 'sync)))
(should (equal 'jpeg (image-type-from-data jpeg)))))
;;;;; Downloading to files
(plz-deftest plz-get-temp-file ()
(let ((filename (plz 'get (url "/image/jpeg")
:as 'file :then 'sync)))
(unwind-protect
(let ((jpeg-data (with-temp-buffer
(insert-file-contents filename)
(buffer-string))))
(should (equal 'jpeg (image-type-from-data jpeg-data))))
;; It's a temp file, so it should always be deleted.
(delete-file filename))))
(plz-deftest plz-head-temp-file ()
(let ((filename (plz 'head (url "/image/jpeg")
:as 'file :then 'sync)))
(unwind-protect
(with-temp-buffer
(insert-file-contents filename)
(should (re-search-forward "Content-Type: image/jpeg")))
;; It's a temp file, so it should always be deleted.
(delete-file filename))))
(plz-deftest plz-get-named-file ()
(let ((filename (make-temp-file "plz-")))
;; HACK: Delete the temp file and reuse its name, because
;; `make-temp-name' is less convenient to use.
(delete-file filename)
(unwind-protect
(progn
(plz 'get (url "/image/jpeg")
:as `(file ,filename) :then 'sync)
(let ((jpeg-data (with-temp-buffer
(insert-file-contents filename)
(buffer-string))))
(should (equal 'jpeg (image-type-from-data jpeg-data)))))
;; It's a temp file, so it should always be deleted.
(when (file-exists-p filename)
(delete-file filename)))))
(plz-deftest plz-upload-file-by-name ()
(let ((filename (make-temp-file "plz-"))
response-json process)
(unwind-protect
(progn
(with-temp-file filename
(insert "deadbeef"))
(setf process
(plz 'put (url "/put")
:body `(file ,filename)
:as #'json-read
:then (lambda (json)
(setf response-json json))))
(plz-test-wait process)
(should (equal "deadbeef" (alist-get 'data response-json)))
(should-not (alist-get 'files response-json)))
(delete-file filename))))
;;;;; Queue
;; TODO: Test that limit is enforced (though it seems to work fine).
(plz-deftest plz-queue-with-finally ()
"Ensure that a queue with a FINALLY function calls it correctly.
That is, that the function is called after the queue is emptied,
and only called once."
(let* ((finally-called-at nil)
(finally-called-times 0)
(queue (make-plz-queue :limit 2
:finally (lambda ()
(setf finally-called-at (current-time))
(cl-incf finally-called-times))))
(urls (list (url "/delay/2")))
completed-urls queue-started-at)
(dolist (url urls)
(plz-queue queue
'get url :then (lambda (_)
(push url completed-urls))))
(setf queue-started-at (current-time))
(plz-run queue)
(cl-loop with waits = 0
while (and (plz-queue-active queue) (< waits 60))
do (progn
(sleep-for 0.1)
(cl-incf waits)))
(should (seq-set-equal-p urls completed-urls))
(should (zerop (plz-length queue)))
(should (= 1 finally-called-times))
(should (>= (float-time (time-subtract finally-called-at queue-started-at))
2))))
(plz-deftest plz-queue-without-finally ()
"Ensure that a queue without a FINALLY function doesn't signal an error."
(let* ((queue (make-plz-queue :limit 2))
(urls (list (url "/get?foo=0")
(url "/get?foo=1")))
completed-urls)
(dolist (url urls)
(plz-queue queue
'get url :then (lambda (_)
(push url completed-urls))))
(plz-run queue)
(cl-loop with waits = 0
while (and (plz-queue-active queue) (< waits 20))
do (progn
(sleep-for 0.1)
(cl-incf waits)))
(should (seq-set-equal-p urls completed-urls))
(should (zerop (plz-length queue)))))
;; TODO: Add test for canceling queue.
;; Process filter
(defun test-plz-process-filter (process output)
"Write OUTPUT to the PROCESS buffer."
(when (buffer-live-p (process-buffer process))
(with-current-buffer (process-buffer process)
(let ((movingp (= (point) (process-mark process))))
(save-excursion
(goto-char (process-mark process))
(insert output)
(set-marker (process-mark process) (point)))
(when movingp
(goto-char (process-mark process)))))))
(plz-deftest plz-get-json-process-filter-async ()
(let* ((test-json) (outputs)
(process (plz 'get (url "/get")
:as #'json-read
:then (lambda (json)
(setf test-json json))
:filter (lambda (process output)
(test-plz-process-filter process output)
(push output outputs)))))
(plz-test-wait process)
(let-alist test-json
(should (string-match-p "curl" .headers.User-Agent)))
(let ((output (string-join (reverse outputs))))
(should (string-match-p "HTTP.*\s+200" output))
(should (string-match-p "Server: gunicorn" output))
(should (string-match-p "\"args\":\s*{}" output)))))
(plz-deftest plz-get-json-process-filter-sync ()
(let* ((outputs)
(response (plz 'get (url "/get")
:as 'response
:filter (lambda (process output)
(test-plz-process-filter process output)
(push output outputs)))))
(plz-test-get-response response)
(let ((output (string-join (reverse outputs))))
(should (string-match-p "HTTP.*\s+200" output))
(should (string-match-p "Server: gunicorn" output))
(should (string-match-p "\"args\":\s*{}" output)))))
;;;; Footer
(provide 'test-plz)
;;; test-plz.el ends here
gitextract_vkmmx439/
├── .elpaignore
├── .github/
│ └── workflows/
│ └── test.yml
├── .gitignore
├── LICENSE
├── Makefile
├── README.org
├── makem.sh
├── plz.el
└── tests/
└── test-plz.el
Condensed preview — 9 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (174K chars).
[
{
"path": ".elpaignore",
"chars": 76,
"preview": ".elpaignore\nMakefile\nmakem.sh\nNOTES.org\n.github/\nimages/\ntests/\nREADME.info\n"
},
{
"path": ".github/workflows/test.yml",
"chars": 2684,
"preview": "# * test.yml --- Test Emacs packages using makem.sh on GitHub Actions\n\n# URL: https://github.com/alphapapa/makem.sh\n# Ve"
},
{
"path": ".gitignore",
"chars": 55,
"preview": "*.elc\n/scratch.el\n.#*\n/sandbox/\n/.sandbox/\n/worktrees/\n"
},
{
"path": "LICENSE",
"chars": 35147,
"preview": " GNU GENERAL PUBLIC LICENSE\n Version 3, 29 June 2007\n\n Copyright (C) 2007 Free "
},
{
"path": "Makefile",
"chars": 1370,
"preview": "# * makem.sh/Makefile --- Script to aid building and testing Emacs Lisp packages\n\n# URL: https://github.com/alphapapa/ma"
},
{
"path": "README.org",
"chars": 19387,
"preview": "#+TITLE: plz.el\n\n#+PROPERTY: LOGGING nil\n\n# NOTE: It would be preferable to put these at the bottom of the file under th"
},
{
"path": "makem.sh",
"chars": 38550,
"preview": "#!/usr/bin/env bash\n\n# * makem.sh --- Script to aid building and testing Emacs Lisp packages\n\n# URL: https://github.com/"
},
{
"path": "plz.el",
"chars": 46497,
"preview": ";;; plz.el --- HTTP library -*- lexical-binding: t; -*-\n\n;; Copyright (C) 2019-2023 Free Softwa"
},
{
"path": "tests/test-plz.el",
"chars": 24208,
"preview": ";;; test-plz.el --- Tests for plz -*- lexical-binding: t; -*-\n\n;; Copyright (C) 2019-2023 Free Software Founda"
}
]
About this extraction
This page contains the full source code of the alphapapa/plz.el GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 9 files (164.0 KB), approximately 40.4k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.