pax_global_header 0000666 0000000 0000000 00000000064 15174165411 0014517 g ustar 00root root 0000000 0000000 52 comment=b8012d73ea566edd514a179e61ee98cf55db8ae7
emacs-keymap-popup/ 0000775 0000000 0000000 00000000000 15174165411 0014600 5 ustar 00root root 0000000 0000000 emacs-keymap-popup/.elpaignore 0000664 0000000 0000000 00000000106 15174165411 0016723 0 ustar 00root root 0000000 0000000 CONTRIBUTING.org
LICENSE
Makefile
manifest.scm
guix.scm
tests
decks
emacs-keymap-popup/.gitignore 0000664 0000000 0000000 00000000143 15174165411 0016566 0 ustar 00root root 0000000 0000000 research
*.elc
*-autoloads.el
*-pkg.el
.*
!.gitignore
!.elpaignore
\#*
*.md
*.info
*.texi
refs/*
emacs-keymap-popup/LICENSE 0000664 0000000 0000000 00000104516 15174165411 0015614 0 ustar 00root root 0000000 0000000 GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
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.
Copyright (C)
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 .
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:
Copyright (C)
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
.
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
.
emacs-keymap-popup/Makefile 0000664 0000000 0000000 00000002354 15174165411 0016244 0 ustar 00root root 0000000 0000000 .POSIX:
ifndef EMACS_CMD
GUIX := $(shell command -v guix 2>/dev/null)
ifdef GUIX
GUIX_SHELL := guix shell --pure -D -f guix.scm emacs-next --
EMACS_CMD := $(GUIX_SHELL) emacs
else
GUIX_SHELL :=
EMACS_CMD := emacs
endif
endif
GUIX_WRAP = $(if $(GUIX_SHELL),$(GUIX_SHELL) $(MAKE) --no-print-directory EMACS_CMD=emacs,$(MAKE) --no-print-directory)
SRCS = keymap-popup.el
TESTS = tests/keymap-popup-tests.el
BATCH = $(EMACS_CMD) -Q --batch
.PHONY: all compile do-compile test do-test lint do-lint clean dev load
all: compile
compile:
@$(GUIX_WRAP) do-compile
do-compile:
@echo "Compiling $(SRCS)..."
@$(BATCH) -f batch-byte-compile $(SRCS)
test:
@$(GUIX_WRAP) do-test
do-test:
@echo "Testing $(TESTS)..."
@$(BATCH) -l ert -l $(SRCS) -l $(TESTS) -f ert-run-tests-batch-and-exit
lint:
@$(GUIX_WRAP) do-lint
do-lint:
@echo "Running checkdoc..."
@$(BATCH) --eval '(checkdoc-file "$(SRCS)")'
dev: compile lint test
load:
@emacsclient --eval "(progn \
(add-to-list (quote load-path) \"$(CURDIR)\") \
(when (boundp (quote keymap-popup-display-action)) \
(makunbound (quote keymap-popup-display-action))) \
(load-file \"$(CURDIR)/$(SRCS)\"))" > /dev/null
@printf "\033[32mLoaded keymap-popup into Emacs\033[0m\n"
clean:
rm -f *.elc
emacs-keymap-popup/README.org 0000664 0000000 0000000 00000012007 15174165411 0016246 0 ustar 00root root 0000000 0000000 #+title: keymap-popup.el
A macro that defines a keymap with embedded descriptions and a popup
to display them.
=One definition, two uses: direct key dispatch and interactive menu.=
Requires Emacs 29.1+.
** Quick start
#+begin_src emacs-lisp
(keymap-popup-define my-commands-map
"My commands."
:group "Edit"
"c" ("Comment" comment-dwim)
"r" ("Rename" rename-file)
:group "View"
"g" ("Refresh" revert-buffer)
"q" ("Quit" quit-window))
;; Use as a normal keymap:
(keymap-set some-mode-map "C-c m" my-commands-map)
;; Or show the popup directly:
(keymap-popup my-commands-map)
#+end_src
Press =h= in the keymap to open the popup. Press =q= to dismiss.
** Features
- =:switch= -- buffer-local toggle with =[on]/[off]= display
- =:keymap= -- sub-menu with stack navigation (=q= / =C-g= pops back)
- =:stay-open= -- command executes without dismissing the popup
- =:inapt-if= -- grays out and blocks entries based on a predicate
- =:c-u= -- prefix argument mode (=C-u= highlights eligible entries)
- =:if= -- conditionally hide entries
- =:group= / =:row= -- column layout
- Dynamic descriptions via lambdas
- =keymap-popup-annotate= -- add popup descriptions to existing keymaps
** Full example
Eval this block, then =M-x kp-test=. It creates a buffer with
state, a popup with switches, sub-menus, inapt entries, dynamic
descriptions, and prefix argument support.
#+begin_src emacs-lisp
(require 'keymap-popup)
;; Force fresh keymaps on re-eval (defvar won't re-set bound variables)
(mapc #'makunbound
(cl-remove-if-not #'boundp '(kp-test--map kp-test--sub-map)))
;;; Buffer rendering
(defvar-local kp-test--name nil)
(defun kp-test--render ()
"Redraw the *kp-test* buffer from buffer-local state."
(let ((inhibit-read-only t))
(erase-buffer)
(insert (propertize "keymap-popup live test\n" 'face 'bold)
(make-string 40 ?-) "\n\n"
(format " Name: %s\n" (or kp-test--name "(not set)"))
"\n"
(propertize "Press h for popup, H for child-frame, q to quit.\n" 'face 'shadow))))
(defun kp-test--refresh ()
"Refresh the display (stay-open)."
(interactive)
(kp-test--render)
(message "Refreshed"))
;;; Commands
(defun kp-test--greet ()
"Greet using buffer-local state."
(interactive)
(let ((name (or kp-test--name "world"))
(loud current-prefix-arg))
(message (if loud
(format "%s!!!" (upcase name))
(format "Hello, %s." name)))
(kp-test--render)))
(defun kp-test--sub-action ()
(interactive)
(message "Sub-menu action! prefix=%s" current-prefix-arg))
;;; Sub-menu keymap
(keymap-popup-define kp-test--sub-map
:group "Sub-menu"
"s" ("Sub action" kp-test--sub-action)
"x" ("Greet from sub" kp-test--greet))
;;; Root keymap
(keymap-popup-define kp-test--map
"Test popup"
:description "keymap-popup live test"
:group "Actions"
"a" ("Greet" kp-test--greet :c-u "SHOUT (C-u)")
"g" ("Refresh" kp-test--refresh :stay-open t)
:group "Infixes"
"v" ("Verbose" :switch kp-test--verbose)
"n" ((lambda () (concat "Name ="
(if (and kp-test--name (not (string-empty-p kp-test--name)))
(propertize kp-test--name 'face 'success)
(propertize "?" 'face 'warning))))
(lambda () (interactive)
(setq-local kp-test--name (read-string "Your name: ")))
:stay-open t)
:group "Navigate"
"s" ("Sub-menu" :keymap kp-test--sub-map)
"q" ("Quit" quit-window)
"H" ("Popup (child-frame)" (lambda () (interactive)
(let ((keymap-popup-backend #'keymap-popup-backend-child-frame))
(keymap-popup kp-test--map))))
:row
:group "Inapt (entry-level)"
"m" ("Merge (always blocked)" kp-test--greet :inapt-if (lambda () t))
"d" ("Dynamic inapt" kp-test--greet
:inapt-if (lambda () (not kp-test--verbose)))
:group ("Group inapt (when verbose off)" :inapt-if (lambda () (not kp-test--verbose)))
"x" ("Group-blocked cmd" kp-test--greet)
:group ("Toggle (visible when verbose)" :if (lambda () kp-test--verbose))
"t" ("Verbose-only action" kp-test--greet))
;;; Entry point
(defun kp-test ()
"Open the *kp-test* buffer and activate the popup.
h opens side-window popup, H opens child-frame popup."
(interactive)
(let ((buf (get-buffer-create "*kp-test*")))
(with-current-buffer buf
(setq-local buffer-read-only t)
(kp-test--render)
(use-local-map kp-test--map))
(pop-to-buffer-same-window buf)
(keymap-popup kp-test--map)))
#+end_src
** Annotating existing keymaps
#+begin_src emacs-lisp
(keymap-popup-annotate dired-mode-map
:group "Navigate"
dired-next-line "Next"
dired-previous-line "Previous"
:group "Mark"
dired-mark "Mark"
dired-unmark "Unmark")
#+end_src
Keys are resolved dynamically via =where-is-internal=, so the popup
always reflects the user's current bindings.
emacs-keymap-popup/guix.scm 0000664 0000000 0000000 00000004111 15174165411 0016255 0 ustar 00root root 0000000 0000000 ;;; guix.scm --- Build keymap-popup from the current working tree.
;;
;; Usage:
;;
;; One-shot install into the user profile:
;; guix package -f guix.scm
;;
;; Development shell with all dependencies:
;; guix shell -D -f guix.scm
;;
;; Run tests inside Guix:
;; guix shell -D -f guix.scm -- make test
(use-modules (gnu packages emacs)
(guix build-system emacs)
((guix licenses) #:prefix license:)
(guix packages)
(guix gexp)
(ice-9 popen)
(ice-9 rdelim))
(define %source-dir (dirname (current-filename)))
(define (git-output . args)
"Run `git -C %source-dir ARGS...' and return trimmed stdout, or #f
if the command fails."
(let* ((port (apply open-pipe* OPEN_READ "git" "-C" %source-dir args))
(line (read-line port)))
(close-pipe port)
(if (eof-object? line) #f line)))
(define %version
(or (git-output "describe" "--tags" "--always" "--dirty")
(and=> (git-output "rev-parse" "--short" "HEAD")
(lambda (hash) (string-append "0.1.0-" hash)))
"0.1.0-git"))
(define (source-file? file stat)
"Include everything except VCS metadata and build artifacts."
(let ((name (basename file)))
(not (or (string-prefix? "." name)
(string-contains file "/refs/")
(string-suffix? ".elc" file)
(string-suffix? "~" file)))))
(define-public emacs-keymap-popup-git
(package
(name "emacs-keymap-popup-git")
(version %version)
(source (local-file %source-dir
"keymap-popup-checkout"
#:recursive? #t
#:select? source-file?))
(build-system emacs-build-system)
(home-page "https://codeberg.org/thanosapollo/emacs-forgejo")
(synopsis "Described keymaps with popup help")
(description
"A single macro that produces both a real Emacs keymap (for direct
key dispatch) and stored descriptions (for a popup help window).
One definition, two uses. No EIEIO, no OOP, no infix arguments.")
(license license:gpl3+)))
emacs-keymap-popup-git
emacs-keymap-popup/keymap-popup.el 0000664 0000000 0000000 00000130005 15174165411 0017550 0 ustar 00root root 0000000 0000000 ;;; keymap-popup.el --- Described keymaps with popup help -*- lexical-binding: t; -*-
;; Copyright (C) 2026 Free Software Foundation, Inc.
;; Author: Thanos Apollo
;; Version: 0.2.1
;; Package-Requires: ((emacs "29.1"))
;; Keywords: convenience
;; URL: https://codeberg.org/thanosapollo/emacs-keymap-popup
;; 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 .
;;; Commentary:
;; Two macros: `keymap-popup-define' produces a real `defvar-keymap'
;; with embedded descriptions; `keymap-popup-annotate' adds popup
;; descriptions to an existing keymap. `keymap-popup' displays
;; either as an interactive menu. One definition, two uses:
;; direct key dispatch and popup help.
;;; Code:
(require 'cl-lib)
(defgroup keymap-popup nil
"Described keymaps with popup help."
:group 'convenience)
(defcustom keymap-popup-display-action
'(display-buffer-in-side-window (side . bottom))
"Display action for the popup buffer.
Only used by `keymap-popup-backend-side-window'.
Common values:
(display-buffer-in-side-window (side . bottom)) - frame-wide
(display-buffer-below-selected) - current window only"
:type 'sexp
:group 'keymap-popup)
(defcustom keymap-popup-backend #'keymap-popup-backend-side-window
"Function returning a display backend plist (:show :fit :hide).
Each backend function receives a single buffer argument.
:show displays the buffer, :fit refits after content changes,
:hide removes the display and cleans up."
:type '(choice (const :tag "Side window" keymap-popup-backend-side-window)
(const :tag "Child frame" keymap-popup-backend-child-frame)
function)
:group 'keymap-popup)
(defcustom keymap-popup-child-frame-parameters
'((no-accept-focus . t)
(no-focus-on-map . t)
(min-width . t)
(min-height . t)
(border-width . 0)
(child-frame-border-width . 1)
(left-fringe . 0)
(right-fringe . 0)
(vertical-scroll-bars . nil)
(horizontal-scroll-bars . nil)
(menu-bar-lines . 0)
(tool-bar-lines . 0)
(tab-bar-lines . 0)
(no-other-frame . t)
(no-other-window . t)
(no-delete-other-windows . t)
(unsplittable . t)
(undecorated . t)
(cursor-type . nil)
(no-special-glyphs . t)
(desktop-dont-save . t))
"Frame parameters for the child-frame backend.
These are merged with runtime parameters (parent-frame, minibuffer,
visibility) when creating the child frame."
:type '(alist :key-type symbol :value-type sexp)
:group 'keymap-popup)
;;; Faces
(defface keymap-popup-key
'((t :inherit help-key-binding))
"Face for key bindings in the popup.")
(defface keymap-popup-group-header
'((t :weight bold))
"Face for group headers in the popup.")
(defface keymap-popup-value
'((t :inherit font-lock-string-face :weight bold))
"Face for switch values in the popup.")
(defface keymap-popup-submenu
'((t :inherit font-lock-type-face))
"Face for sub-menu entries in the popup.")
(defface keymap-popup-inapt
'((t :inherit shadow))
"Face for inapt (disabled) entries in the popup.")
;;; Keymap metadata
(defun keymap-popup--meta (keymap prop)
"Get popup metadata PROP from KEYMAP via pseudo-key lookup."
(lookup-key keymap (vector prop)))
(gv-define-setter keymap-popup--meta (val keymap prop)
`(define-key ,keymap (vector ,prop) ,val))
;;; Parsers
(defun keymap-popup--extract-props (plist)
"Extract known properties from PLIST.
Recognized keys: :if, :inapt-if, :stay-open, :c-u."
(cl-loop for (k v) on plist by #'cddr
when (memq k '(:if :inapt-if :stay-open :c-u))
append (list k v)))
(defun keymap-popup--parse-entry (key spec)
"Parse binding SPEC for KEY into a plist.
KEY is a key string for normal entries, or a command symbol for
annotated entries. SPEC is (DESCRIPTION COMMAND-OR-TYPE &rest PROPS)
for key-based entries, or (DESCRIPTION &rest PROPS) for annotated ones."
(if (symbolp key)
;; Annotated entry: key is a command symbol, spec is (DESC . PROPS)
;; or a bare string
(let* ((spec (if (stringp spec) (list spec) spec))
(description (car spec))
(props (cdr spec)))
`(:key nil :description ,description :type suffix
:command ,key
,@(keymap-popup--extract-props props)))
;; Normal entry: key is a string
(let* ((description (car spec))
(second (cadr spec))
(rest (cddr spec)))
(pcase second
(:switch
`(:key ,key :description ,description :type switch
:variable ,(car rest)
,@(keymap-popup--extract-props (cdr rest))))
(:keymap
`(:key ,key :description ,description :type keymap
:target ,(car rest)
,@(keymap-popup--extract-props (cdr rest))))
(_
`(:key ,key :description ,description :type suffix
:command ,second
,@(keymap-popup--extract-props rest)))))))
(defun keymap-popup--split-groups (bindings)
"Split BINDINGS at :group and :row keywords.
Returns a list of rows, each row a list of (NAME . FLAT-ENTRIES) chunks.
`:group' starts a new group within the current row.
`:row' starts a new row."
(keymap-popup--split-groups-1 bindings nil nil nil nil))
(defun keymap-popup--split-groups-1 (rest name entries groups rows)
"Recursive helper for `keymap-popup--split-groups'.
REST is remaining bindings, NAME is current group name, ENTRIES
is accumulated entries (reversed), GROUPS is current row's groups
\(reversed), ROWS is accumulated rows (reversed)."
(let ((flush-group (if entries
(cons (cons name (reverse entries)) groups)
groups)))
(cond
((null rest)
(reverse (if flush-group
(cons (reverse flush-group) rows)
rows)))
((eq (car rest) :row)
(keymap-popup--split-groups-1
(cdr rest) nil nil nil
(if flush-group (cons (reverse flush-group) rows) rows)))
((eq (car rest) :group)
(keymap-popup--split-groups-1
(cddr rest) (cadr rest) nil flush-group rows))
(t
(keymap-popup--split-groups-1
(cddr rest) name
(cons (cons (car rest) (cadr rest)) entries)
groups rows)))))
(defun keymap-popup--parse-group-name (raw)
"Parse RAW group name into (NAME . PROPS).
RAW is a string, a lambda, or a list (NAME :if PRED :inapt-if PRED).
A list whose car is not `lambda' is treated as a name with properties."
(if (and (consp raw) (not (eq (car raw) 'lambda)))
(cons (car raw) (keymap-popup--extract-props (cdr raw)))
(cons raw nil)))
(defun keymap-popup--parse-chunk (chunk)
"Parse CHUNK of (NAME . ((KEY . SPEC) ...)) into a group plist."
(let* ((name-props (keymap-popup--parse-group-name (car chunk)))
(name (car name-props))
(group-props (cdr name-props))
(entries (mapcar (lambda (pair)
(keymap-popup--parse-entry (car pair) (cdr pair)))
(cdr chunk))))
`(:name ,name :entries ,entries ,@group-props)))
(defun keymap-popup--parse-bindings (bindings)
"Parse BINDINGS into a list of rows.
Each row is a list of group plists with :name and :entries."
(mapcar (lambda (row) (mapcar #'keymap-popup--parse-chunk row))
(keymap-popup--split-groups bindings)))
;;; Infix generators
(defun keymap-popup--switch-forms (map-name entry)
"Return (defvar-local defun) forms for switch ENTRY in MAP-NAME."
(let* ((variable (plist-get entry :variable))
(description (plist-get entry :description))
(fn-name (intern (format "%s--toggle-%s" map-name variable))))
(list
`(defvar-local ,variable nil)
`(defun ,fn-name ()
,(format "Toggle %s." description)
(interactive)
(setq-local ,variable (not ,variable))
(message "%s: %s" ,description (if ,variable "on" "off"))))))
(defun keymap-popup--entry-command (map-name entry)
"Return the command to bind in MAP-NAME's keymap for ENTRY."
(pcase (plist-get entry :type)
('suffix (plist-get entry :command))
('switch (intern (format "%s--toggle-%s" map-name (plist-get entry :variable))))
('keymap (let ((target (plist-get entry :target)))
`(lambda () (interactive) (keymap-popup ,target))))))
;;; Macro helpers
(defun keymap-popup--build-keymap-pairs (map-name entries)
"Build flat key/command list for `defvar-keymap' from ENTRIES.
MAP-NAME is used to derive generated command names."
(cl-loop for entry in entries
for cmd = (keymap-popup--entry-command map-name entry)
append (list (plist-get entry :key)
(if (symbolp cmd) `#',cmd cmd))))
(defun keymap-popup--quote-if-needed (form)
"Quote FORM unless it is a lambda, in which case return as-is."
(if (and (consp form) (eq (car form) 'lambda))
form
`',form))
(defun keymap-popup--build-entry-form (entry)
"Build a `list' form for a single ENTRY that evaluates lambdas properly."
(let* ((type (plist-get entry :type))
(key (plist-get entry :key))
(desc-form (keymap-popup--quote-if-needed
(plist-get entry :description)))
(type-props (pcase type
('suffix `(:command ,(keymap-popup--quote-if-needed
(plist-get entry :command))
,@(when (plist-get entry :stay-open)
'(:stay-open t))))
('keymap `(:target ,(plist-get entry :target)))
('switch `(:variable ',(plist-get entry :variable)))))
(if-pred (plist-get entry :if))
(inapt-if (plist-get entry :inapt-if)))
`(list :key ,key
:description ,desc-form
:type ',type
,@type-props
,@(and if-pred (list :if if-pred))
,@(and inapt-if (list :inapt-if inapt-if))
,@(and-let* ((c-u (plist-get entry :c-u)))
(list :c-u c-u)))))
(defun keymap-popup--build-descriptions-form (rows)
"Build a `list' form that constructs descriptions at load time.
ROWS is a list of rows, each row a list of groups.
Uses list calls so lambdas get compiled."
`(list ,@(mapcar
(lambda (row)
`(list ,@(mapcar
(lambda (group)
(let ((if-pred (plist-get group :if))
(inapt-if (plist-get group :inapt-if)))
`(list :name ,(plist-get group :name)
:entries (list ,@(mapcar #'keymap-popup--build-entry-form
(plist-get group :entries)))
,@(and if-pred (list :if if-pred))
,@(and inapt-if (list :inapt-if inapt-if)))))
row)))
rows)))
;;; Macro
(defun keymap-popup--consume-keyword (rest keyword)
"If REST starts with KEYWORD, return (VALUE . REMAINING), else nil."
(and (eq (car rest) keyword)
(cons (cadr rest) (cddr rest))))
(defun keymap-popup--extract-macro-opts (body)
"Extract macro options from BODY.
Returns (DOCSTRING POPUP-KEY EXIT-KEY PARENT DESCRIPTION BINDINGS).
A string followed by a list is a key binding, not a docstring."
(let* ((docstring (and (stringp (car body))
(or (null (cadr body))
(not (listp (cadr body))))
(car body)))
(rest (if docstring (cdr body) body))
(popup-pair (keymap-popup--consume-keyword rest :popup-key))
(popup-key (if popup-pair (car popup-pair) "h"))
(rest (if popup-pair (cdr popup-pair) rest))
(exit-pair (keymap-popup--consume-keyword rest :exit-key))
(exit-key (if exit-pair (car exit-pair) ?q))
(rest (if exit-pair (cdr exit-pair) rest))
(parent-pair (keymap-popup--consume-keyword rest :parent))
(parent (when parent-pair (car parent-pair)))
(rest (if parent-pair (cdr parent-pair) rest))
(desc-pair (keymap-popup--consume-keyword rest :description))
(description (when desc-pair (car desc-pair)))
(bindings (if desc-pair (cdr desc-pair) rest)))
(list docstring popup-key exit-key parent description bindings)))
;;;###autoload
(defmacro keymap-popup-define (name &rest body)
"Define NAME as a keymap with embedded descriptions.
BODY is an optional docstring, optional :popup-key KEY (default
\"h\"), optional :exit-key CHAR (default ?q), optional :parent
KEYMAP, optional :description STRING-OR-FUNCTION, followed by
:group keywords and KEY (DESC ...) pairs."
(declare (indent 1))
(pcase-let* ((`(,docstring ,popup-key ,exit-key ,parent ,description ,bindings)
(keymap-popup--extract-macro-opts body))
(rows (keymap-popup--parse-bindings bindings))
(all-entries (cl-loop for row in rows
append (cl-loop for group in row
append (plist-get group :entries))))
(infix-forms (cl-loop for entry in all-entries
append (pcase (plist-get entry :type)
('switch (keymap-popup--switch-forms name entry))
(_ nil))))
(keymap-pairs (keymap-popup--build-keymap-pairs name all-entries)))
`(progn
,@infix-forms
(defvar-keymap ,name
,@(when docstring (list :doc docstring))
,@(when parent (list :parent parent))
,@keymap-pairs
,popup-key (lambda () (interactive) (keymap-popup ,name)))
(setf (keymap-popup--meta ,name 'keymap-popup--descriptions)
,(keymap-popup--build-descriptions-form rows))
(setf (keymap-popup--meta ,name 'keymap-popup--exit-key) ,exit-key)
,@(when description
`((setf (keymap-popup--meta ,name 'keymap-popup--description) ,description))))))
;;;###autoload
(defmacro keymap-popup-annotate (keymap &rest body)
"Annotate existing KEYMAP with popup descriptions.
BODY is :group keywords and COMMAND-SYMBOL DESCRIPTION pairs.
COMMAND-SYMBOL is a function symbol already bound in the keymap.
DESCRIPTION is a string or (STRING &rest PROPS).
Keys are resolved dynamically via `where-is-internal' at display
time, so the popup always reflects the user's current bindings."
(declare (indent 1))
(let ((rows (keymap-popup--parse-bindings body)))
`(progn
(setf (keymap-popup--meta ,keymap 'keymap-popup--descriptions)
,(keymap-popup--build-descriptions-form rows))
;; t is the default binding in keymaps, lookup-key ignores it.
(setf (keymap-popup--meta ,keymap 'keymap-popup--annotated) 'yes))))
;;; Public API
(defun keymap-popup--map-groups (rows fn)
"Apply FN to each group in ROWS, returning the transformed rows.
FN receives a group plist and returns a new group plist."
(mapcar (lambda (row) (mapcar fn row)) rows))
(defun keymap-popup--add-entry-to-rows (rows entry group-name)
"Return ROWS with ENTRY appended to the group named GROUP-NAME.
Falls back to the first group if GROUP-NAME is not found."
(let ((target (or (cl-loop for row in rows
thereis (cl-loop for g in row
when (equal (plist-get g :name) group-name)
return group-name))
(plist-get (caar rows) :name))))
(keymap-popup--map-groups
rows
(lambda (group)
(if (equal (plist-get group :name) target)
(list :name (plist-get group :name)
:entries (append (plist-get group :entries) (list entry)))
group)))))
(defun keymap-popup--remove-key-from-rows (rows key)
"Return ROWS with entries matching KEY filtered out."
(keymap-popup--map-groups
rows
(lambda (group)
(list :name (plist-get group :name)
:entries (cl-remove-if
(lambda (e) (equal (plist-get e :key) key))
(plist-get group :entries))))))
;;;###autoload
(defun keymap-popup-add-entry (keymap key description command &optional group)
"Add KEY binding with DESCRIPTION and COMMAND to KEYMAP.
GROUP is the group name to add to (nil for the first group).
Updates both the keymap and the popup descriptions."
(let ((descs (keymap-popup--meta keymap 'keymap-popup--descriptions)))
(or descs (user-error "No descriptions in keymap"))
(keymap-set keymap key command)
(let ((entry (list :key key :description description
:type 'suffix :command command)))
(setf (keymap-popup--meta keymap 'keymap-popup--descriptions)
(keymap-popup--add-entry-to-rows descs entry group)))))
;;;###autoload
(defun keymap-popup-remove-entry (keymap key)
"Remove KEY binding from KEYMAP.
Updates both the keymap and the popup descriptions."
(keymap-set keymap key nil)
(setf (keymap-popup--meta keymap 'keymap-popup--descriptions)
(keymap-popup--remove-key-from-rows
(keymap-popup--meta keymap 'keymap-popup--descriptions) key)))
;;; Renderer
(defun keymap-popup--resolve-description (desc)
"If DESC is a function, call it; otherwise return as-is."
(if (functionp desc) (funcall desc) desc))
(defun keymap-popup--render-entry (entry &optional prefix-mode key-width)
"Render ENTRY into a formatted line, or nil if :if hides it.
When PREFIX-MODE is non-nil, entries with :c-u are highlighted and
their :c-u description is shown; other entries are dimmed.
KEY-WIDTH pads the key column for alignment."
(when (or (null (plist-get entry :if))
(funcall (plist-get entry :if)))
(let* ((inapt (and-let* ((pred (plist-get entry :inapt-if)))
(funcall pred)))
(raw-desc (keymap-popup--resolve-description
(plist-get entry :description)))
(type (plist-get entry :type))
(desc (if (eq type 'keymap)
(propertize raw-desc 'face 'keymap-popup-submenu)
raw-desc))
(c-u-desc (plist-get entry :c-u))
(raw-key (plist-get entry :key))
(padded-key (if key-width
(concat raw-key
(make-string (max 0 (- key-width (length raw-key)))
?\s))
raw-key))
(key-str (propertize padded-key 'face 'keymap-popup-key))
(value-str (if (eq type 'switch)
(propertize
(if (symbol-value (plist-get entry :variable))
" [on]" " [off]")
'face 'keymap-popup-value)
""))
(c-u-str (and c-u-desc
(if prefix-mode
(propertize (format " (%s)" c-u-desc)
'face 'warning)
(propertize (format " (%s)" c-u-desc)
'face 'shadow))))
(line (format " %s %s%s%s" key-str desc value-str
(or c-u-str ""))))
(cond
(inapt (propertize line 'face 'keymap-popup-inapt))
((and prefix-mode (not c-u-desc))
(propertize line 'face 'shadow))
(t line)))))
(defun keymap-popup--render-group-lines (group &optional prefix-mode)
"Render GROUP into a list of lines (strings).
When PREFIX-MODE is non-nil, pass it to entry rendering.
Returns nil if the group is hidden by :if or has no visible entries.
When the group has :inapt-if that returns non-nil, all entries are
rendered with the inapt face."
(when (or (null (plist-get group :if))
(funcall (plist-get group :if)))
(let* ((group-inapt (and-let* ((pred (plist-get group :inapt-if)))
(funcall pred)))
(entries (plist-get group :entries))
(key-width (cl-loop for entry in entries
maximize (length (plist-get entry :key))))
(header (and-let* ((raw-name (plist-get group :name))
(name (keymap-popup--resolve-description raw-name)))
(propertize name 'face (if group-inapt
'keymap-popup-inapt
'keymap-popup-group-header))))
(lines (cl-loop for entry in entries
for line = (keymap-popup--render-entry
entry prefix-mode key-width)
when line collect line)))
(when lines
(let ((result (if header (cons header lines) lines)))
(if group-inapt
(mapcar (lambda (line) (propertize line 'face 'keymap-popup-inapt))
result)
result))))))
(defun keymap-popup--string-width-visible (str)
"Return the visible width of STR, ignoring text properties."
(string-width (substring-no-properties str)))
(defun keymap-popup--column-width (col)
"Return the max visible width of lines in COL."
(cl-loop for line in col
maximize (keymap-popup--string-width-visible line)))
(defun keymap-popup--join-columns (columns separator col-widths)
"Join COLUMNS side by side with SEPARATOR between them.
COL-WIDTHS is a list of minimum widths per column position.
Shorter columns are padded with blank lines."
(let* ((max-height (cl-loop for col in columns maximize (length col)))
(padded-cols (cl-mapcar
(lambda (col width)
(let ((padded (mapcar (lambda (line)
(string-pad line width))
col))
(blanks (make-list (- max-height (length col))
(make-string width ?\s))))
(append padded blanks)))
columns col-widths)))
(cl-loop for row from 0 below max-height
collect (string-trim-right
(mapconcat (lambda (col) (nth row col))
padded-cols
separator)))))
(defun keymap-popup--rows-to-columns (rows &optional prefix-mode)
"Render each row of ROWS into its list of column line-lists.
When PREFIX-MODE is non-nil, pass it to group rendering.
Returns a list of ((col-lines ...) ...) per row, filtering empty groups."
(mapcar (lambda (row)
(cl-loop for group in row
when (keymap-popup--render-group-lines group prefix-mode)
collect it))
rows))
(defun keymap-popup--global-col-widths (rendered-rows)
"Compute max column width per position across all RENDERED-ROWS."
(let ((max-cols (cl-loop for cols in rendered-rows
maximize (length cols))))
(cl-loop for i from 0 below max-cols
collect (cl-loop for cols in rendered-rows
when (nth i cols)
maximize (keymap-popup--column-width (nth i cols))))))
(defun keymap-popup--render (docstring rows &optional prefix-mode)
"Render DOCSTRING and ROWS into a complete popup string.
ROWS is a list of rows, each row a list of groups.
When PREFIX-MODE is non-nil, highlight :c-u entries and dim others.
Column widths are aligned across all rows."
(let* ((resolved (when docstring
(keymap-popup--resolve-description docstring)))
(doc (when resolved
(concat (if (text-properties-at 0 resolved)
resolved
(propertize resolved 'face 'font-lock-doc-face))
"\n")))
(rendered-rows (keymap-popup--rows-to-columns rows prefix-mode))
(col-widths (keymap-popup--global-col-widths rendered-rows))
(sections (cl-loop for cols in rendered-rows
when cols
collect (mapconcat #'identity
(keymap-popup--join-columns
cols " " col-widths)
"\n"))))
(concat doc (mapconcat #'identity sections "\n") "\n")))
;;; Popup state
(defvar-local keymap-popup--source-buffer nil
"The buffer from which the popup was invoked.
Switch variables are buffer-local there, so rendering must read
`symbol-value' in that buffer's context.")
(defvar-local keymap-popup--active-keymap nil
"The currently displayed keymap in the popup.")
(defvar-local keymap-popup--active-descriptions nil
"Descriptions for the currently active keymap.")
(defvar-local keymap-popup--active-docstring nil
"Docstring for the currently active keymap.")
(defvar-local keymap-popup--stack nil
"Stack of parent state plists for sub-menu navigation.")
(defvar-local keymap-popup--prefix-mode nil
"Non-nil when C-u prefix mode is active.")
(defvar-local keymap-popup--reentering nil
"Non-nil when a sub-menu just popped, preventing cascading exit.")
(defvar-local keymap-popup--hook-fn nil
"The post-command-hook function for this popup session.")
(defvar-local keymap-popup--display-backend nil
"The active display backend plist (:show :fit :hide).")
;;; Popup display
(defun keymap-popup--collect-descriptions (keymap)
"Collect descriptions from KEYMAP and all its parent keymaps.
Walks the native parent chain via `keymap-parent'."
(cl-loop for map = keymap then (keymap-parent map)
while map
when (keymap-popup--meta map 'keymap-popup--descriptions)
append it))
(defun keymap-popup--find-entry-by-key (descriptions key-str)
"Find the entry matching KEY-STR in DESCRIPTIONS.
DESCRIPTIONS is a list of rows, each row a list of groups.
Returns the entry plist, or nil."
(cl-loop for row in descriptions
thereis (cl-loop for group in row
thereis (cl-loop for entry in (plist-get group :entries)
when (equal (plist-get entry :key) key-str)
return entry))))
(defun keymap-popup--infix-p (descriptions key-str)
"Return non-nil if KEY-STR maps to a switch entry in DESCRIPTIONS."
(and-let* ((entry (keymap-popup--find-entry-by-key descriptions key-str)))
(eq (plist-get entry :type) 'switch)))
(defun keymap-popup--keymap-target (descriptions key-str)
"Return the target map symbol if KEY-STR is a :keymap entry in DESCRIPTIONS."
(and-let* ((entry (keymap-popup--find-entry-by-key descriptions key-str))
(_ (eq (plist-get entry :type) 'keymap)))
(plist-get entry :target)))
(defun keymap-popup--find-group-for-key (descriptions key-str)
"Find the group containing KEY-STR in DESCRIPTIONS."
(cl-loop for row in descriptions
thereis (cl-loop for group in row
when (cl-loop for entry in (plist-get group :entries)
thereis (equal (plist-get entry :key) key-str))
return group)))
(defun keymap-popup--inapt-p (descriptions key-str)
"Return non-nil if KEY-STR is inapt in DESCRIPTIONS.
Checks both group-level and entry-level :inapt-if predicates."
(or (and-let* ((group (keymap-popup--find-group-for-key descriptions key-str))
(pred (plist-get group :inapt-if)))
(funcall pred))
(and-let* ((entry (keymap-popup--find-entry-by-key descriptions key-str))
(pred (plist-get entry :inapt-if)))
(funcall pred))))
(defun keymap-popup--stay-open-p (descriptions key-str)
"Return non-nil if KEY-STR should keep the popup open in DESCRIPTIONS.
True for switches and suffixes with :stay-open."
(and-let* ((entry (keymap-popup--find-entry-by-key descriptions key-str)))
(or (eq (plist-get entry :type) 'switch)
(plist-get entry :stay-open))))
(defun keymap-popup--keep-popup-p (descriptions key-str)
"Return non-nil if KEY-STR should keep the popup open.
True for switches, inapt keys, :keymap entries, and C-u.
Suffixes with :stay-open dismiss and reopen instead."
(or (keymap-popup--infix-p descriptions key-str)
(keymap-popup--inapt-p descriptions key-str)
(keymap-popup--keymap-target descriptions key-str)
(equal key-str "C-u")))
(defun keymap-popup--refresh-buffer (buf descriptions &optional docstring prefix-mode)
"Re-render popup BUF with DESCRIPTIONS, refit via backend.
DOCSTRING is shown at the top if non-nil. PREFIX-MODE toggles
prefix argument highlighting."
(let ((content (keymap-popup--render docstring descriptions prefix-mode)))
(with-current-buffer buf
(let ((inhibit-read-only t))
(erase-buffer)
(insert content)
(goto-char (point-min))))
(when-let* ((fit (plist-get (buffer-local-value 'keymap-popup--display-backend buf)
:fit)))
(funcall fit buf))))
(defun keymap-popup--refresh (buf)
"Re-render popup BUF from its buffer-local state.
Renders in the source buffer's context so `symbol-value' for
switch variables sees the user's buffer-local values."
(when (buffer-live-p buf)
(let ((source (buffer-local-value 'keymap-popup--source-buffer buf))
(descs (buffer-local-value 'keymap-popup--active-descriptions buf))
(doc (buffer-local-value 'keymap-popup--active-docstring buf))
(prefix (buffer-local-value 'keymap-popup--prefix-mode buf)))
(with-current-buffer (if (buffer-live-p source) source buf)
(keymap-popup--refresh-buffer buf descs doc prefix)))))
(defun keymap-popup--resolve-key (entry keymap)
"Resolve ENTRY's :command to a key in KEYMAP.
Returns entry with :key filled in, or nil if unbound."
(if (plist-get entry :key) entry
(and-let* ((cmd (plist-get entry :command))
(keys (where-is-internal cmd keymap t)))
(plist-put (copy-sequence entry) :key (key-description keys)))))
(defun keymap-popup--resolve-descriptions (rows keymap)
"Resolve command symbols to keys in ROWS using KEYMAP.
Drops entries whose command has no binding."
(keymap-popup--map-groups
rows
(lambda (group)
(plist-put (copy-sequence group) :entries
(cl-loop for entry in (plist-get group :entries)
when (keymap-popup--resolve-key entry keymap)
collect it)))))
;;; Display backends
(defun keymap-popup--show-side-window (buf)
"Display BUF in a side window."
(display-buffer buf (append keymap-popup-display-action
'((window-height . fit-window-to-buffer))))
(when-let* ((win (get-buffer-window buf)))
(fit-window-to-buffer win)))
(defun keymap-popup--fit-side-window (buf)
"Refit the side window displaying BUF."
(when-let* ((win (get-buffer-window buf)))
(when (window-live-p win)
(fit-window-to-buffer win))))
(defun keymap-popup--hide-side-window (buf)
"Delete the side window displaying BUF."
(when-let* ((win (get-buffer-window buf)))
(delete-window win)))
(defun keymap-popup--show-child-frame (buf)
"Display BUF in a child frame centered on the parent.
Frame parameters are taken from `keymap-popup-child-frame-parameters'."
(let* ((parent (selected-frame))
(after-make-frame-functions nil)
(frame (make-frame
`((parent-frame . ,parent)
(minibuffer . ,(minibuffer-window))
(visibility . nil)
,@keymap-popup-child-frame-parameters)))
(win (frame-root-window frame)))
(set-window-buffer win buf)
(set-window-dedicated-p win t)
(fit-frame-to-buffer frame)
(let ((x (/ (- (frame-pixel-width parent) (frame-pixel-width frame)) 2))
(y (/ (- (frame-pixel-height parent) (frame-pixel-height frame)) 2)))
(set-frame-position frame (max 0 x) (max 0 y)))
(make-frame-visible frame)
(redirect-frame-focus frame parent)))
(defun keymap-popup--fit-child-frame (buf)
"Refit the child frame displaying BUF."
(when-let* ((win (get-buffer-window buf t))
(frame (window-frame win)))
(when (frame-parent frame)
(fit-frame-to-buffer frame))))
(defun keymap-popup--hide-child-frame (buf)
"Delete the child frame displaying BUF."
(when-let* ((win (get-buffer-window buf t))
(frame (window-frame win)))
(when (frame-parent frame)
(delete-frame frame))))
(defun keymap-popup-backend-side-window ()
"Return a side-window display backend."
(list :show #'keymap-popup--show-side-window
:fit #'keymap-popup--fit-side-window
:hide #'keymap-popup--hide-side-window))
(defun keymap-popup-backend-child-frame ()
"Return a child-frame display backend."
(list :show #'keymap-popup--show-child-frame
:fit #'keymap-popup--fit-child-frame
:hide #'keymap-popup--hide-child-frame))
(defun keymap-popup--prepare-buffer ()
"Create and configure the popup buffer."
(let ((buf (get-buffer-create "*keymap-popup*")))
(with-current-buffer buf
(setq-local buffer-read-only t
cursor-type nil
mode-line-format nil
header-line-format nil
tab-line-format nil
left-margin-width 1
right-margin-width 1))
buf))
(defun keymap-popup--teardown (buf)
"Remove the popup display for BUF, clean up hooks, and kill it."
(when (buffer-live-p buf)
(when-let* ((fn (buffer-local-value 'keymap-popup--hook-fn buf)))
(remove-hook 'post-command-hook fn))
(when-let* ((hide (plist-get (buffer-local-value 'keymap-popup--display-backend buf)
:hide)))
(funcall hide buf))
(kill-buffer buf)))
(defun keymap-popup--make-keep-pred (buf)
"Return a keep-pred for `set-transient-map'.
Reads state from BUF. Consumes the reentering flag on read."
(lambda ()
(and (buffer-live-p buf)
(or (when (buffer-local-value 'keymap-popup--reentering buf)
(with-current-buffer buf
(setq-local keymap-popup--reentering nil))
t)
(and-let* ((keys (this-command-keys-vector))
(key-str (key-description keys))
(descs (buffer-local-value 'keymap-popup--active-descriptions buf)))
(keymap-popup--keep-popup-p descs key-str))))))
(defun keymap-popup--make-post-command-fn (buf)
"Return a post-command-hook function that refreshes BUF.
Removes itself when BUF is killed externally."
(let ((fn (make-symbol "keymap-popup--post-command")))
(fset fn
(lambda ()
(if (not (buffer-live-p buf))
(remove-hook 'post-command-hook fn)
(keymap-popup--refresh buf))))
fn))
(defun keymap-popup--make-on-exit (buf)
"Return an on-exit callback for `set-transient-map'.
Pops the sub-menu stack if non-empty, otherwise tears down."
(lambda ()
(when (buffer-live-p buf)
(with-current-buffer buf
(if keymap-popup--stack
(pcase-let ((`(:keymap ,km :descriptions ,descs :docstring ,doc)
(pop keymap-popup--stack)))
(setq-local keymap-popup--active-keymap km
keymap-popup--active-descriptions descs
keymap-popup--active-docstring doc
keymap-popup--reentering t
keymap-popup--prefix-mode nil)
(keymap-popup--refresh buf))
(keymap-popup--teardown buf))))))
(defun keymap-popup--collect-entries (descriptions fn)
"Collect non-nil results of (FN ENTRY GROUP) across DESCRIPTIONS.
Walks rows, groups, and entries. FN receives an entry plist and
its parent group plist; non-nil return values are collected."
(mapcan (lambda (row)
(mapcan (lambda (group)
(mapcan (lambda (entry)
(and-let* ((result (funcall fn entry group)))
(list result)))
(plist-get group :entries)))
row))
descriptions))
(defun keymap-popup--inapt-keys (descriptions)
"Return key-strings that may be inapt in DESCRIPTIONS.
Includes keys with entry-level or group-level :inapt-if."
(keymap-popup--collect-entries
descriptions
(lambda (entry group)
(when (and (plist-get entry :key)
(or (plist-get entry :inapt-if)
(plist-get group :inapt-if)))
(plist-get entry :key)))))
(defun keymap-popup--stay-open-suffix-keys (descriptions)
"Return key-strings for :stay-open suffix entries in DESCRIPTIONS."
(keymap-popup--collect-entries
descriptions
(lambda (entry _group)
(when (and (plist-get entry :key)
(eq (plist-get entry :type) 'suffix)
(plist-get entry :stay-open))
(plist-get entry :key)))))
(defun keymap-popup--switch-keys (descriptions)
"Return key-strings for switch entries in DESCRIPTIONS."
(keymap-popup--collect-entries
descriptions
(lambda (entry _group)
(when (eq (plist-get entry :type) 'switch)
(plist-get entry :key)))))
(defun keymap-popup--submenu-keys (descriptions)
"Return alist of (KEY-STRING . TARGET-KEYMAP) for :keymap entries."
(keymap-popup--collect-entries
descriptions
(lambda (entry _group)
(when (eq (plist-get entry :type) 'keymap)
(cons (plist-get entry :key)
(plist-get entry :target))))))
(defun keymap-popup--push-submenu (buf child-keymap)
"Push current popup state and activate CHILD-KEYMAP's transient map."
(with-current-buffer buf
(push (list :keymap keymap-popup--active-keymap
:descriptions keymap-popup--active-descriptions
:docstring keymap-popup--active-docstring)
keymap-popup--stack)
(let* ((raw (keymap-popup--collect-descriptions child-keymap))
(descs (if (keymap-popup--meta child-keymap 'keymap-popup--annotated)
(keymap-popup--resolve-descriptions raw child-keymap)
raw))
(doc (keymap-popup--meta child-keymap 'keymap-popup--description))
(exit-key (or (keymap-popup--meta child-keymap 'keymap-popup--exit-key) ?q)))
(setq-local keymap-popup--active-keymap child-keymap
keymap-popup--active-descriptions descs
keymap-popup--active-docstring doc
keymap-popup--prefix-mode nil)
(keymap-popup--refresh buf)
(set-transient-map
(keymap-popup--build-wrapper-map child-keymap descs buf exit-key)
(keymap-popup--make-keep-pred buf)
(keymap-popup--make-on-exit buf)))))
(defun keymap-popup--core-overrides (buf exit-key)
"Return alist of core overrides: exit key and C-u prefix toggle."
(list (cons (key-description (vector exit-key))
(lambda () (interactive)))
(cons "C-u"
(lambda () (interactive)
(with-current-buffer buf
(setq-local keymap-popup--prefix-mode
(not keymap-popup--prefix-mode))
(setq prefix-arg
(when keymap-popup--prefix-mode '(4))))))))
(defun keymap-popup--with-inapt-guard (buf key-str cmd)
"Wrap CMD with a dynamic inapt check for KEY-STR.
When inapt, blocks execution and preserves prefix-arg.
When not inapt, calls CMD."
(lambda () (interactive)
(let ((descs (buffer-local-value 'keymap-popup--active-descriptions buf)))
(if (keymap-popup--inapt-p descs key-str)
(progn
(message "Command unavailable")
(when (buffer-local-value 'keymap-popup--prefix-mode buf)
(setq prefix-arg '(4))))
(funcall cmd)))))
(defun keymap-popup--submenu-overrides (descriptions buf)
"Return alist of submenu key overrides."
(mapcar (lambda (pair)
(cons (car pair)
(let ((target (cdr pair)))
(lambda () (interactive)
(keymap-popup--push-submenu buf target)))))
(keymap-popup--submenu-keys descriptions)))
(defun keymap-popup--switch-overrides (keymap descriptions buf)
"Return alist of switch key overrides.
Wraps the toggle command with prefix-mode consumption."
(mapcar (lambda (key-str)
(cons key-str
(lambda () (interactive)
(call-interactively (keymap-lookup keymap key-str))
(when (buffer-local-value 'keymap-popup--prefix-mode buf)
(with-current-buffer buf
(setq-local keymap-popup--prefix-mode nil))
(setq prefix-arg nil)))))
(keymap-popup--switch-keys descriptions)))
(defun keymap-popup--stay-open-overrides (keymap descriptions)
"Return alist of stay-open suffix overrides.
Each command dismisses the popup, executes, and reopens."
(mapcar (lambda (key-str)
(cons key-str
(lambda () (interactive)
(call-interactively (keymap-lookup keymap key-str))
(keymap-popup keymap))))
(keymap-popup--stay-open-suffix-keys descriptions)))
(defun keymap-popup--build-wrapper-map (keymap descriptions buf exit-key)
"Build wrapper keymap over KEYMAP with all popup overrides.
Inapt guards are applied as a layer over specialized handlers."
(let* ((map (make-sparse-keymap))
(inapt (keymap-popup--inapt-keys descriptions))
(overrides (append (keymap-popup--core-overrides buf exit-key)
(keymap-popup--switch-overrides keymap descriptions buf)
(keymap-popup--submenu-overrides descriptions buf)
(keymap-popup--stay-open-overrides keymap descriptions))))
(set-keymap-parent map keymap)
(pcase-dolist (`(,key . ,cmd) overrides)
(keymap-set map key
(if (member key inapt)
(keymap-popup--with-inapt-guard buf key cmd)
cmd)))
;; Inapt keys with no specialized handler get a guard over the base command
(dolist (key inapt)
(unless (assoc key overrides)
(keymap-set map key
(keymap-popup--with-inapt-guard buf key
(lambda () (interactive)
(call-interactively (keymap-lookup keymap key)))))))
map))
;;;###autoload
(defun keymap-popup (keymap)
"Show popup help for described KEYMAP.
Activates KEYMAP as a transient map. Switch keys execute and
re-render without closing. Inapt keys are blocked.
Sub-menu keys push a navigation stack. C-u toggles prefix mode."
(or (keymap-popup--meta keymap 'keymap-popup--descriptions)
(user-error "No descriptions in keymap"))
(let* ((source (current-buffer))
(buf (keymap-popup--prepare-buffer))
(backend (funcall keymap-popup-backend))
(raw (keymap-popup--collect-descriptions keymap))
(descriptions (if (keymap-popup--meta keymap 'keymap-popup--annotated)
(keymap-popup--resolve-descriptions raw keymap)
raw))
(docstring (keymap-popup--meta keymap 'keymap-popup--description))
(exit-key (or (keymap-popup--meta keymap 'keymap-popup--exit-key) ?q))
(hook-fn (keymap-popup--make-post-command-fn buf)))
(with-current-buffer buf
(setq-local keymap-popup--source-buffer source
keymap-popup--active-keymap keymap
keymap-popup--active-descriptions descriptions
keymap-popup--active-docstring docstring
keymap-popup--display-backend backend
keymap-popup--stack nil
keymap-popup--prefix-mode nil
keymap-popup--reentering nil
keymap-popup--hook-fn hook-fn))
(keymap-popup--refresh buf)
(funcall (plist-get backend :show) buf)
(add-hook 'post-command-hook hook-fn)
(set-transient-map
(keymap-popup--build-wrapper-map keymap descriptions buf exit-key)
(keymap-popup--make-keep-pred buf)
(keymap-popup--make-on-exit buf))))
(provide 'keymap-popup)
;;; keymap-popup.el ends here
emacs-keymap-popup/tests/ 0000775 0000000 0000000 00000000000 15174165411 0015742 5 ustar 00root root 0000000 0000000 emacs-keymap-popup/tests/keymap-popup-tests.el 0000664 0000000 0000000 00000106710 15174165411 0022060 0 ustar 00root root 0000000 0000000 ;;; keymap-popup-tests.el --- Tests -*- lexical-binding: t; -*-
(require 'ert)
(load (expand-file-name
"../keymap-popup.el"
(file-name-directory (or load-file-name buffer-file-name))))
;;; Parser tests
(ert-deftest keymap-popup-test-parse-suffix-entry ()
(let ((result (keymap-popup--parse-entry "c" '("Comment" forgejo-view-comment))))
(should (equal (plist-get result :key) "c"))
(should (equal (plist-get result :description) "Comment"))
(should (equal (plist-get result :command) 'forgejo-view-comment))
(should (equal (plist-get result :type) 'suffix))))
(ert-deftest keymap-popup-test-parse-suffix-with-if ()
(let* ((pred (lambda () t))
(result (keymap-popup--parse-entry "b" `("Browse" forgejo-browse :if ,pred))))
(should (equal (plist-get result :type) 'suffix))
(should (eq (plist-get result :if) pred))))
(ert-deftest keymap-popup-test-parse-switch-entry ()
(let ((result (keymap-popup--parse-entry "v" '("Verbose" :switch my-verbose-var))))
(should (equal (plist-get result :type) 'switch))
(should (equal (plist-get result :variable) 'my-verbose-var))))
(ert-deftest keymap-popup-test-parse-dynamic-description ()
(let* ((desc-fn (lambda () "dynamic"))
(result (keymap-popup--parse-entry "d" `(,desc-fn some-command))))
(should (functionp (plist-get result :description)))))
(ert-deftest keymap-popup-test-parse-bindings-groups ()
(let* ((rows (keymap-popup--parse-bindings
'(:group "Actions"
"c" ("Comment" forgejo-view-comment)
"r" ("Reply" forgejo-issue-reply)
:group "Navigate"
"g" ("Refresh" forgejo-view-refresh)
"q" ("Quit" quit-window))))
(row (car rows)))
(should (= (length rows) 1))
(should (= (length row) 2))
(should (equal (plist-get (car row) :name) "Actions"))
(should (= (length (plist-get (car row) :entries)) 2))
(should (equal (plist-get (cadr row) :name) "Navigate"))
(should (= (length (plist-get (cadr row) :entries)) 2))))
(ert-deftest keymap-popup-test-parse-bindings-no-group ()
(let* ((rows (keymap-popup--parse-bindings
'("c" ("Comment" forgejo-view-comment)
"r" ("Reply" forgejo-issue-reply))))
(row (car rows)))
(should (= (length rows) 1))
(should (= (length row) 1))
(should (null (plist-get (car row) :name)))
(should (= (length (plist-get (car row) :entries)) 2))))
(ert-deftest keymap-popup-test-parse-bindings-rows ()
(let ((rows (keymap-popup--parse-bindings
'(:group "A"
"a" ("Aaa" ignore)
:group "B"
"b" ("Bbb" ignore)
:row
:group "C"
"c" ("Ccc" ignore)))))
(should (= (length rows) 2))
(should (= (length (car rows)) 2))
(should (= (length (cadr rows)) 1))
(should (equal (plist-get (caar (cdr rows)) :name) "C"))))
(ert-deftest keymap-popup-test-parse-keymap-entry ()
(let ((result (keymap-popup--parse-entry "a" '("Metadata" :keymap my-sub-map))))
(should (equal (plist-get result :type) 'keymap))
(should (equal (plist-get result :target) 'my-sub-map))))
;;; Infix generator tests
(ert-deftest keymap-popup-test-switch-forms ()
(let* ((entry '(:key "v" :description "Verbose" :type switch
:variable my-verbose-var))
(forms (keymap-popup--switch-forms 'test-map entry)))
(should (= (length forms) 2))
(should (eq (car (nth 0 forms)) 'defvar-local))
(should (eq (cadr (nth 0 forms)) 'my-verbose-var))
(should (eq (car (nth 1 forms)) 'defun))
(should (eq (cadr (nth 1 forms)) 'test-map--toggle-my-verbose-var))))
(ert-deftest keymap-popup-test-entry-command ()
(should (eq (keymap-popup--entry-command 'map '(:type suffix :command my-cmd))
'my-cmd))
(should (eq (keymap-popup--entry-command 'map '(:type switch :variable my-var))
'map--toggle-my-var)))
;;; Macro tests
(ert-deftest keymap-popup-test-macro-creates-keymap ()
(eval '(keymap-popup-define keymap-popup--test-map-1
"Test keymap."
:group "Actions"
"c" ("Comment" ignore)
"q" ("Quit" quit-window))
t)
(should (keymapp keymap-popup--test-map-1))
(should (eq (keymap-lookup keymap-popup--test-map-1 "c") #'ignore))
(should (eq (keymap-lookup keymap-popup--test-map-1 "q") #'quit-window)))
(ert-deftest keymap-popup-test-macro-stores-descriptions ()
(eval '(keymap-popup-define keymap-popup--test-map-2
"Test."
:group "A"
"c" ("Comment" ignore)
:group "B"
"g" ("Go" ignore))
t)
(let* ((descs (keymap-popup--meta keymap-popup--test-map-2
'keymap-popup--descriptions))
(row (car descs)))
(should (= (length descs) 1))
(should (= (length row) 2))
(should (equal (plist-get (car row) :name) "A"))
(should (equal (plist-get (cadr row) :name) "B"))))
(ert-deftest keymap-popup-test-macro-switch-infix ()
(eval '(keymap-popup-define keymap-popup--test-map-3
"Test."
"v" ("Verbose" :switch keymap-popup--test-sw))
t)
(should (boundp 'keymap-popup--test-sw))
(should (fboundp 'keymap-popup--test-map-3--toggle-keymap-popup--test-sw)))
(ert-deftest keymap-popup-test-macro-lambda-command ()
(eval '(keymap-popup-define keymap-popup--test-map-5
"Test."
"x" ("Run" (lambda () (interactive) (message "running"))))
t)
(should (functionp (keymap-lookup keymap-popup--test-map-5 "x"))))
(ert-deftest keymap-popup-test-macro-no-docstring ()
(eval '(keymap-popup-define keymap-popup--test-map-nodoc
:group "Actions"
"c" ("Comment" ignore))
t)
(should (keymapp keymap-popup--test-map-nodoc))
(should (eq (keymap-lookup keymap-popup--test-map-nodoc "c") #'ignore))
(let* ((descs (keymap-popup--meta keymap-popup--test-map-nodoc
'keymap-popup--descriptions))
(row (car descs)))
(should (= (length descs) 1))
(should (= (length row) 1))
(should (equal (plist-get (car row) :name) "Actions"))))
(ert-deftest keymap-popup-test-macro-default-popup-key ()
(eval '(keymap-popup-define keymap-popup--test-map-defkey
"c" ("Comment" ignore))
t)
(should (functionp (keymap-lookup keymap-popup--test-map-defkey "h"))))
(ert-deftest keymap-popup-test-macro-custom-popup-key ()
(eval '(keymap-popup-define keymap-popup--test-map-custkey
:popup-key "?"
"c" ("Comment" ignore))
t)
(should (functionp (keymap-lookup keymap-popup--test-map-custkey "?")))
(should (null (keymap-lookup keymap-popup--test-map-custkey "h"))))
(ert-deftest keymap-popup-test-macro-exit-key ()
(eval '(keymap-popup-define keymap-popup--test-exit-key
:exit-key ?x
"c" ("Comment" ignore))
t)
(should (eq (keymap-popup--meta keymap-popup--test-exit-key
'keymap-popup--exit-key)
?x)))
(ert-deftest keymap-popup-test-popup-key-with-docstring ()
(eval '(keymap-popup-define keymap-popup--test-pkdoc
"My commands."
:popup-key "?"
:group "Actions"
"c" ("Comment" ignore))
t)
(should (functionp (keymap-lookup keymap-popup--test-pkdoc "?")))
(should (null (keymap-lookup keymap-popup--test-pkdoc "h")))
(should (string-match-p "My commands"
(documentation-property 'keymap-popup--test-pkdoc
'variable-documentation))))
;;; Renderer tests
(ert-deftest keymap-popup-test-render-suffix ()
(let* ((rows (list (list (list :name "Actions"
:entries (list (list :key "c" :description "Comment"
:type 'suffix :command 'ignore))))))
(output (keymap-popup--render "Test." rows)))
(should (string-match-p "Test\\." output))
(should (string-match-p "Actions" output))
(should (string-match-p "Comment" output))))
(ert-deftest keymap-popup-test-render-switch-value ()
(defvar keymap-popup--test-render-sw nil)
(let* ((rows (list (list (list :name nil
:entries (list (list :key "v" :description "Verbose"
:type 'switch
:variable 'keymap-popup--test-render-sw))))))
(output-off (keymap-popup--render nil rows)))
(should (string-match-p "\\[off\\]" output-off))
(setq keymap-popup--test-render-sw t)
(let ((output-on (keymap-popup--render nil rows)))
(should (string-match-p "\\[on\\]" output-on)))))
(ert-deftest keymap-popup-test-render-if-hidden ()
(let* ((rows (list (list (list :name nil
:entries (list (list :key "b" :description "Browse"
:type 'suffix :command 'ignore
:if (lambda () nil)))))))
(output (keymap-popup--render nil rows)))
(should-not (string-match-p "Browse" output))))
(ert-deftest keymap-popup-test-render-if-shown ()
(let* ((rows (list (list (list :name nil
:entries (list (list :key "b" :description "Browse"
:type 'suffix :command 'ignore
:if (lambda () t)))))))
(output (keymap-popup--render nil rows)))
(should (string-match-p "Browse" output))))
(ert-deftest keymap-popup-test-render-dynamic-description ()
(let* ((rows (list (list (list :name nil
:entries (list (list :key "d"
:description (lambda () "Dynamic!")
:type 'suffix :command 'ignore))))))
(output (keymap-popup--render nil rows)))
(should (string-match-p "Dynamic!" output))))
;;; Column layout tests
(ert-deftest keymap-popup-test-render-columns-side-by-side ()
(let* ((rows (list (list (list :name "Alpha"
:entries (list (list :key "a" :description "Aaa"
:type 'suffix :command 'ignore)))
(list :name "Beta"
:entries (list (list :key "b" :description "Bbb"
:type 'suffix :command 'ignore))))))
(output (keymap-popup--render nil rows))
(lines (split-string output "\n" t)))
(should (string-match-p "Alpha" (car lines)))
(should (string-match-p "Beta" (car lines)))))
(ert-deftest keymap-popup-test-render-rows-separated ()
(let* ((rows (list (list (list :name "Row1"
:entries (list (list :key "a" :description "Aaa"
:type 'suffix :command 'ignore))))
(list (list :name "Row2"
:entries (list (list :key "b" :description "Bbb"
:type 'suffix :command 'ignore))))))
(output (keymap-popup--render nil rows)))
(should (string-match-p "Row1" output))
(should (string-match-p "Row2" output))
(let ((lines (split-string output "\n" t)))
(should-not (and (string-match-p "Row1" (car lines))
(string-match-p "Row2" (car lines)))))))
(ert-deftest keymap-popup-test-columns-aligned-across-rows ()
(let* ((rows (list
(list (list :name "A"
:entries (list (list :key "a" :description "X"
:type 'suffix :command 'ignore)))
(list :name "B"
:entries (list (list :key "b" :description "Y"
:type 'suffix :command 'ignore))))
(list (list :name "Longer Name"
:entries (list (list :key "c" :description "Something longer"
:type 'suffix :command 'ignore)))
(list :name "D"
:entries (list (list :key "d" :description "Z"
:type 'suffix :command 'ignore))))))
(output (keymap-popup--render nil rows))
(plain (substring-no-properties output))
(lines (split-string plain "\n" t)))
(let ((b-pos (string-match "B" (cl-find-if (lambda (l) (string-match-p "\\bB\\b" l)) lines)))
(d-pos (string-match "D" (cl-find-if (lambda (l) (string-match-p "\\bD\\b" l)) lines))))
(should b-pos)
(should d-pos)
(should (= b-pos d-pos)))))
(ert-deftest keymap-popup-test-join-columns ()
(let* ((col-a '("Header A" " a Aaa" " b Bbb"))
(col-b '("Header B" " c Ccc"))
(widths (list (keymap-popup--column-width col-a)
(keymap-popup--column-width col-b)))
(result (keymap-popup--join-columns (list col-a col-b) " " widths)))
(should (= (length result) 3))
(should (string-match-p "Header A" (nth 0 result)))
(should (string-match-p "Header B" (nth 0 result)))
(should (string-match-p "Bbb" (nth 2 result)))))
;;; Lookup helper tests
(ert-deftest keymap-popup-test-find-entry-by-key ()
(let ((descs (list (list (list :name "G"
:entries (list (list :key "c" :description "Comment"
:type 'suffix :command 'ignore)
(list :key "v" :description "Verbose"
:type 'switch :variable 'some-var)))))))
(should (equal (plist-get (keymap-popup--find-entry-by-key descs "c") :type) 'suffix))
(should (equal (plist-get (keymap-popup--find-entry-by-key descs "v") :type) 'switch))
(should (null (keymap-popup--find-entry-by-key descs "z")))))
(ert-deftest keymap-popup-test-infix-p ()
(let ((descs (list (list (list :name nil
:entries (list (list :key "c" :type 'suffix :command 'ignore)
(list :key "v" :type 'switch :variable 'x)))))))
(should-not (keymap-popup--infix-p descs "c"))
(should (keymap-popup--infix-p descs "v"))
(should-not (keymap-popup--infix-p descs "z"))))
(ert-deftest keymap-popup-test-keymap-target ()
(let ((descs (list (list (list :name nil
:entries (list (list :key "c" :type 'suffix :command 'ignore)
(list :key "a" :type 'keymap
:target 'my-sub)))))))
(should (eq (keymap-popup--keymap-target descs "a") 'my-sub))
(should (null (keymap-popup--keymap-target descs "c")))))
(ert-deftest keymap-popup-test-inapt-p ()
(let ((descs (list (list (list :name nil
:entries (list (list :key "m" :type 'suffix :command 'ignore
:inapt-if (lambda () t))
(list :key "c" :type 'suffix
:command 'ignore)))))))
(should (keymap-popup--inapt-p descs "m"))
(should-not (keymap-popup--inapt-p descs "c"))))
(ert-deftest keymap-popup-test-stay-open-p ()
(let ((descs (list (list (list :name nil
:entries (list (list :key "c" :type 'suffix :command 'ignore)
(list :key "g" :type 'suffix :command 'ignore
:stay-open t)
(list :key "v" :type 'switch :variable 'x)))))))
(should-not (keymap-popup--stay-open-p descs "c"))
(should (keymap-popup--stay-open-p descs "g"))
(should (keymap-popup--stay-open-p descs "v"))))
(ert-deftest keymap-popup-test-keep-popup-p ()
(let ((descs (list (list (list :name nil
:entries (list (list :key "c" :type 'suffix :command 'ignore)
(list :key "v" :type 'switch :variable 'x)
(list :key "m" :type 'suffix :command 'ignore
:inapt-if (lambda () t))
(list :key "a" :type 'keymap :target 'sub)
(list :key "g" :type 'suffix :command 'ignore
:stay-open t)))))))
(should-not (keymap-popup--keep-popup-p descs "c"))
(should (keymap-popup--keep-popup-p descs "v"))
(should (keymap-popup--keep-popup-p descs "m"))
(should (keymap-popup--keep-popup-p descs "a"))
;; stay-open suffixes dismiss and reopen, not kept
(should-not (keymap-popup--keep-popup-p descs "g"))
(should (keymap-popup--keep-popup-p descs "C-u"))))
;;; C-u rendering tests
(ert-deftest keymap-popup-test-c-u-desc-in-normal-mode ()
(let* ((rows (list (list (list :name nil
:entries (list (list :key "s" :description "Submit"
:type 'suffix :command 'ignore
:c-u "force push"))))))
(output (keymap-popup--render nil rows)))
(should (string-match-p "(force push)" output))
(let ((pos (string-match "(force push)" output)))
(should (eq (get-text-property pos 'face output) 'shadow)))))
(ert-deftest keymap-popup-test-c-u-desc-in-prefix-mode ()
(let* ((rows (list (list (list :name nil
:entries (list (list :key "s" :description "Submit"
:type 'suffix :command 'ignore
:c-u "force")
(list :key "g" :description "Refresh"
:type 'suffix :command 'ignore))))))
(output (keymap-popup--render nil rows t)))
(let ((pos (string-match "(force)" output)))
(should pos)
(should-not (eq (get-text-property pos 'face output) 'shadow)))
(let ((pos (string-match "Refresh" output)))
(should (eq (get-text-property pos 'face output) 'shadow)))))
;;; Inapt rendering tests
(ert-deftest keymap-popup-test-inapt-rendered-with-face ()
(let* ((rows (list (list (list :name nil
:entries (list (list :key "m" :description "Merge"
:type 'suffix :command 'ignore
:inapt-if (lambda () t)))))))
(output (keymap-popup--render nil rows)))
(should (string-match-p "Merge" output))
(let ((pos (string-match "Merge" output)))
(should (eq (get-text-property pos 'face output) 'keymap-popup-inapt)))))
(ert-deftest keymap-popup-test-inapt-not-when-predicate-nil ()
(let* ((rows (list (list (list :name nil
:entries (list (list :key "m" :description "Merge"
:type 'suffix :command 'ignore
:inapt-if (lambda () nil)))))))
(output (keymap-popup--render nil rows)))
(let ((pos (string-match "Merge" output)))
(should-not (eq (get-text-property pos 'face output) 'keymap-popup-inapt)))))
(ert-deftest keymap-popup-test-keymap-entry-gets-submenu-face ()
(let* ((rows (list (list (list :name nil
:entries (list (list :key "a" :description "Sub"
:type 'keymap :target 'x))))))
(output (keymap-popup--render nil rows)))
(let ((pos (string-match "Sub" output)))
(should (eq (get-text-property pos 'face output) 'keymap-popup-submenu)))))
;;; Group-level predicate tests
(ert-deftest keymap-popup-test-group-if-hidden ()
(let* ((rows (list (list (list :name "Visible"
:entries (list (list :key "a" :description "Alpha"
:type 'suffix :command 'ignore)))
(list :name "Hidden" :if (lambda () nil)
:entries (list (list :key "b" :description "Beta"
:type 'suffix :command 'ignore))))))
(output (keymap-popup--render nil rows)))
(should (string-match-p "Alpha" output))
(should-not (string-match-p "Beta" output))))
(ert-deftest keymap-popup-test-group-if-shown ()
(let* ((rows (list (list (list :name "Shown" :if (lambda () t)
:entries (list (list :key "a" :description "Alpha"
:type 'suffix :command 'ignore))))))
(output (keymap-popup--render nil rows)))
(should (string-match-p "Alpha" output))
(should (string-match-p "Shown" output))))
(ert-deftest keymap-popup-test-group-inapt-grays-all ()
(let* ((rows (list (list (list :name "Disabled"
:inapt-if (lambda () t)
:entries (list (list :key "a" :description "Alpha"
:type 'suffix :command 'ignore)
(list :key "b" :description "Beta"
:type 'suffix :command 'ignore))))))
(output (keymap-popup--render nil rows)))
(let ((pos-a (string-match "Alpha" output))
(pos-b (string-match "Beta" output)))
(should (eq (get-text-property pos-a 'face output) 'keymap-popup-inapt))
(should (eq (get-text-property pos-b 'face output) 'keymap-popup-inapt)))))
(ert-deftest keymap-popup-test-group-inapt-blocks-dispatch ()
(let ((descs (list (list (list :name "OK"
:entries (list (list :key "a" :type 'suffix :command 'ignore)))
(list :name "Nope" :inapt-if (lambda () t)
:entries (list (list :key "b" :type 'suffix :command 'ignore)))))))
(should-not (keymap-popup--inapt-p descs "a"))
(should (keymap-popup--inapt-p descs "b"))))
;;; Integration tests
(ert-deftest keymap-popup-test-full-definition ()
(eval '(keymap-popup-define keymap-popup--test-full
"Full test."
:group "Actions"
"c" ("Comment" ignore)
:group "Switches"
"v" ("Verbose" :switch keymap-popup--test-full-verbose)
:row
:group "Navigate"
"b" ("Browse" ignore :if (lambda () t))
"q" ("Quit" quit-window))
t)
(should (keymapp keymap-popup--test-full))
(should (eq (keymap-lookup keymap-popup--test-full "c") #'ignore))
(let ((descs (keymap-popup--meta keymap-popup--test-full
'keymap-popup--descriptions)))
(should (= (length descs) 2))))
(ert-deftest keymap-popup-test-switch-toggle-roundtrip ()
(eval '(keymap-popup-define keymap-popup--test-rt
"Test." "v" ("Verbose" :switch keymap-popup--test-rt-sw))
t)
(with-temp-buffer
(should (null keymap-popup--test-rt-sw))
(funcall-interactively #'keymap-popup--test-rt--toggle-keymap-popup--test-rt-sw)
(should (eq keymap-popup--test-rt-sw t))
(funcall-interactively #'keymap-popup--test-rt--toggle-keymap-popup--test-rt-sw)
(should (null keymap-popup--test-rt-sw))))
(ert-deftest keymap-popup-test-stay-open-in-descriptions ()
(eval '(keymap-popup-define keymap-popup--test-stay
"g" ("Refresh" ignore :stay-open t))
t)
(let* ((descs (keymap-popup--meta keymap-popup--test-stay
'keymap-popup--descriptions))
(entry (keymap-popup--find-entry-by-key descs "g")))
(should (plist-get entry :stay-open))))
(ert-deftest keymap-popup-test-dynamic-group-name ()
(eval '(keymap-popup-define keymap-popup--test-dyngrp
:group (lambda () "Dynamic Group")
"c" ("Comment" ignore))
t)
(let* ((descs (keymap-popup--meta keymap-popup--test-dyngrp
'keymap-popup--descriptions))
(output (keymap-popup--render nil descs)))
(should (string-match-p "Dynamic Group" output))))
;;; Parent inheritance tests
(ert-deftest keymap-popup-test-parent-keymap-bindings ()
(eval '(keymap-popup-define keymap-popup--test-parent
:group "Common"
"g" ("Refresh" ignore)
"q" ("Quit" quit-window))
t)
(eval '(keymap-popup-define keymap-popup--test-child
:parent keymap-popup--test-parent
:group "Child"
"c" ("Comment" ignore))
t)
(should (eq (keymap-lookup keymap-popup--test-child "c") #'ignore))
(should (eq (keymap-lookup keymap-popup--test-child "g") #'ignore)))
(ert-deftest keymap-popup-test-parent-descriptions-merged ()
(eval '(keymap-popup-define keymap-popup--test-parent2
:group "Common"
"g" ("Refresh" ignore))
t)
(eval '(keymap-popup-define keymap-popup--test-child2
:parent keymap-popup--test-parent2
:group "Child"
"c" ("Comment" ignore))
t)
(let ((all (keymap-popup--collect-descriptions keymap-popup--test-child2)))
(should (>= (length all) 2))))
(ert-deftest keymap-popup-test-collect-descriptions-chain ()
(eval '(keymap-popup-define keymap-popup--test-grandparent
:group "GP"
"g" ("Go" ignore))
t)
(eval '(keymap-popup-define keymap-popup--test-mid
:parent keymap-popup--test-grandparent
:group "Mid"
"m" ("Mid cmd" ignore))
t)
(eval '(keymap-popup-define keymap-popup--test-leaf
:parent keymap-popup--test-mid
:group "Leaf"
"l" ("Leaf cmd" ignore))
t)
(let ((all (keymap-popup--collect-descriptions keymap-popup--test-leaf)))
(should (>= (length all) 3))))
;;; Macro via-macro integration tests
(ert-deftest keymap-popup-test-inapt-via-macro ()
(eval '(keymap-popup-define keymap-popup--test-inapt-map
"m" ("Merge" ignore :inapt-if (lambda () t))
"c" ("Comment" ignore))
t)
(let* ((descs (keymap-popup--meta keymap-popup--test-inapt-map
'keymap-popup--descriptions))
(output (keymap-popup--render nil descs)))
(let ((pos (string-match "Merge" output)))
(should pos)
(should (eq (get-text-property pos 'face output) 'keymap-popup-inapt)))
(should (string-match-p "Comment" output))))
(ert-deftest keymap-popup-test-group-inapt-via-macro ()
(eval '(keymap-popup-define keymap-popup--test-group-inapt-map
:group ("Disabled" :inapt-if (lambda () t))
"a" ("Alpha" ignore)
"b" ("Beta" ignore))
t)
(let* ((descs (keymap-popup--meta keymap-popup--test-group-inapt-map
'keymap-popup--descriptions))
(output (keymap-popup--render nil descs))
(pos (string-match "Alpha" output)))
(should pos)
(should (eq (get-text-property pos 'face output) 'keymap-popup-inapt))))
(ert-deftest keymap-popup-test-group-if-via-macro ()
(eval '(keymap-popup-define keymap-popup--test-group-if-map
:group ("Hidden" :if (lambda () nil))
"a" ("Alpha" ignore)
:group "Shown"
"b" ("Beta" ignore))
t)
(let* ((descs (keymap-popup--meta keymap-popup--test-group-if-map
'keymap-popup--descriptions))
(output (keymap-popup--render nil descs)))
(should-not (string-match-p "Alpha" output))
(should (string-match-p "Beta" output))))
(ert-deftest keymap-popup-test-if-on-switch ()
(eval '(keymap-popup-define keymap-popup--test-if-sw
"v" ("Verbose" :switch keymap-popup--test-if-sw-var
:if (lambda () nil)))
t)
(should (keymap-lookup keymap-popup--test-if-sw "v"))
(let* ((descs (keymap-popup--meta keymap-popup--test-if-sw
'keymap-popup--descriptions))
(output (keymap-popup--render nil descs)))
(should-not (string-match-p "Verbose" output))))
;;; Wrapper map tests
(ert-deftest keymap-popup-test-wrapper-map-has-exit-key ()
(eval '(keymap-popup-define keymap-popup--test-wrap
"c" ("Comment" ignore))
t)
(let* ((descs (keymap-popup--meta keymap-popup--test-wrap
'keymap-popup--descriptions))
(buf (get-buffer-create "*keymap-popup-test*"))
(map (keymap-popup--build-wrapper-map
keymap-popup--test-wrap descs buf ?q)))
(unwind-protect
(progn
(should (functionp (keymap-lookup map "q")))
(should (eq (keymap-lookup map "c") #'ignore)))
(kill-buffer buf))))
(ert-deftest keymap-popup-test-wrapper-map-has-c-u ()
(eval '(keymap-popup-define keymap-popup--test-wrap-cu
"c" ("Comment" ignore))
t)
(let* ((descs (keymap-popup--meta keymap-popup--test-wrap-cu
'keymap-popup--descriptions))
(buf (get-buffer-create "*keymap-popup-test*"))
(map (keymap-popup--build-wrapper-map
keymap-popup--test-wrap-cu descs buf ?q)))
(unwind-protect
(should (functionp (keymap-lookup map "C-u")))
(kill-buffer buf))))
(ert-deftest keymap-popup-test-inapt-keys-collected ()
(let ((descs (list (list (list :name nil
:entries (list (list :key "m" :type 'suffix
:inapt-if (lambda () t))
(list :key "c" :type 'suffix)))))))
(should (equal (keymap-popup--inapt-keys descs) '("m")))))
(ert-deftest keymap-popup-test-inapt-keys-from-group ()
(let ((descs (list (list (list :name "G" :inapt-if (lambda () t)
:entries (list (list :key "a" :type 'suffix)
(list :key "b" :type 'suffix)))))))
(should (equal (keymap-popup--inapt-keys descs) '("a" "b")))))
(ert-deftest keymap-popup-test-submenu-keys-collected ()
(let ((descs (list (list (list :name nil
:entries (list (list :key "a" :type 'keymap :target 'sub)
(list :key "c" :type 'suffix)))))))
(should (equal (keymap-popup--submenu-keys descs) '(("a" . sub))))))
(ert-deftest keymap-popup-test-stay-open-suffix-keys ()
(let ((descs (list (list (list :name nil
:entries (list (list :key "g" :type 'suffix :stay-open t)
(list :key "v" :type 'switch :variable 'x)
(list :key "c" :type 'suffix)))))))
(should (equal (keymap-popup--stay-open-suffix-keys descs) '("g")))))
;;; Add/remove entry tests
(ert-deftest keymap-popup-test-add-entry ()
(eval '(keymap-popup-define keymap-popup--test-add
:group "Actions"
"c" ("Comment" ignore))
t)
(keymap-popup-add-entry keymap-popup--test-add "z" "New" #'forward-char "Actions")
(should (eq (keymap-lookup keymap-popup--test-add "z") #'forward-char))
(let* ((descs (keymap-popup--meta keymap-popup--test-add
'keymap-popup--descriptions))
(output (keymap-popup--render nil descs)))
(should (string-match-p "New" output))))
(ert-deftest keymap-popup-test-remove-entry ()
(eval '(keymap-popup-define keymap-popup--test-rm
:group "Actions"
"c" ("Comment" ignore)
"r" ("Reply" ignore))
t)
(keymap-popup-remove-entry keymap-popup--test-rm "r")
(should (null (keymap-lookup keymap-popup--test-rm "r")))
(let* ((descs (keymap-popup--meta keymap-popup--test-rm
'keymap-popup--descriptions))
(output (keymap-popup--render nil descs)))
(should (string-match-p "Comment" output))
(should-not (string-match-p "Reply" output))))
;;; Annotate tests
(defvar keymap-popup--test-annotate-map
(let ((map (make-sparse-keymap)))
(keymap-set map "a" #'forward-char)
(keymap-set map "b" #'backward-char)
(keymap-set map "c" #'kill-line)
map))
(ert-deftest keymap-popup-test-annotate-parse ()
(let ((entry (keymap-popup--parse-entry 'forward-char '("Forward"))))
(should (eq (plist-get entry :command) 'forward-char))
(should-not (plist-get entry :key))
(should (equal (plist-get entry :description) "Forward"))))
(ert-deftest keymap-popup-test-annotate-parse-with-props ()
(let ((entry (keymap-popup--parse-entry 'forward-char '("Forward" :stay-open t))))
(should (eq (plist-get entry :command) 'forward-char))
(should (plist-get entry :stay-open))))
(ert-deftest keymap-popup-test-annotate-parse-bare-string ()
(let ((entry (keymap-popup--parse-entry 'forward-char "Forward")))
(should (eq (plist-get entry :command) 'forward-char))
(should (equal (plist-get entry :description) "Forward"))))
(ert-deftest keymap-popup-test-resolve-key ()
(let* ((entry (list :key nil :description "Forward" :type 'suffix
:command 'forward-char))
(resolved (keymap-popup--resolve-key entry keymap-popup--test-annotate-map)))
(should resolved)
(should (equal (plist-get resolved :key) "a"))))
(ert-deftest keymap-popup-test-resolve-key-unbound ()
(let ((entry (list :key nil :description "Nope" :type 'suffix
:command 'some-nonexistent-command-xyz)))
(should-not (keymap-popup--resolve-key entry keymap-popup--test-annotate-map))))
(ert-deftest keymap-popup-test-resolve-descriptions ()
(let* ((rows (list (list (list :name "Test"
:entries (list (list :key nil :description "Forward"
:type 'suffix :command 'forward-char)
(list :key nil :description "Nope"
:type 'suffix :command 'nonexistent-xyz))))))
(resolved (keymap-popup--resolve-descriptions rows keymap-popup--test-annotate-map))
(entries (plist-get (car (car resolved)) :entries)))
(should (= (length entries) 1))
(should (equal (plist-get (car entries) :key) "a"))))
(ert-deftest keymap-popup-test-annotate-macro ()
(eval '(keymap-popup-annotate keymap-popup--test-annotate-map
:group "Move"
forward-char "Forward"
backward-char "Backward")
t)
(should (eq (keymap-popup--meta keymap-popup--test-annotate-map
'keymap-popup--annotated)
'yes))
(let* ((descs (keymap-popup--meta keymap-popup--test-annotate-map
'keymap-popup--descriptions))
(entries (plist-get (car (car descs)) :entries)))
(should (= (length entries) 2))
(should (eq (plist-get (car entries) :command) 'forward-char))))
;;; Metadata tests
(ert-deftest keymap-popup-test-meta-read-write ()
(let ((map (make-sparse-keymap)))
(setf (keymap-popup--meta map 'keymap-popup--descriptions) '(test-data))
(should (equal (keymap-popup--meta map 'keymap-popup--descriptions) '(test-data)))))
(ert-deftest keymap-popup-test-meta-nil-for-missing ()
(let ((map (make-sparse-keymap)))
(should (null (keymap-popup--meta map 'keymap-popup--descriptions)))))
(ert-deftest keymap-popup-test-no-descriptions-error ()
(let ((map (make-sparse-keymap)))
(should-error (keymap-popup map) :type 'user-error)))
(provide 'keymap-popup-tests)
;;; keymap-popup-tests.el ends here