M-x Compile: A Deep Dive into Compiling Code with Emacs
Summary
TLDRIn this video, the presenter explores the powerful built-in compile package in Emacs, which allows for compiling programs, running tests, and executing interpreters without the need for additional plugins. They demonstrate how to use the package with Python, navigate errors, and customize commands for different file types. The video also highlights the package's versatility, including its ability to handle ANSI color codes and set special environments for compilation. The presenter concludes by showcasing the package's integration with shell and terminal modes, enabling error navigation directly from logs or error messages.
Takeaways
- 💻 Emac is a powerful editor with built-in capabilities for compiling programs, running tests, and executing interpreters without needing additional plugins.
- 🔧 The 'compile' package in Emac is versatile and can be used for various tasks beyond just compiling code.
- 🐍 The video focuses on using Python with Emac, demonstrating how to compile and run Python scripts directly within the editor.
- 💡 Emac's compile command can be customized for different file types and projects, allowing users to run specific commands tailored to their workflow.
- 🛠️ Errors in code can be navigated and viewed directly within Emac, which uses special filters to parse and display error messages.
- 🎨 The video discusses handling colored output from compile commands, showing how to integrate with the 'ansi-color' package for better visibility.
- 📝 Custom regular expressions can be created and applied to the compile command to better parse and navigate errors from different compilers or interpreters.
- 🌐 The compile functionality is not limited to Emac's editor window; it also works in shell and terminal emulators within Emac, such as 'shell-mode' and 'vterm'.
- 🔗 The video mentions 'Brilliant.org' as a learning resource for Python, math, data science, and computer science, offering interactive courses.
- 👨🏫 The presenter, Gavin, shares personal experiences and tips on using Emac for programming, emphasizing the efficiency and convenience of its built-in tools.
Q & A
What is the main topic of the video script?
-The main topic of the video script is exploring the compile package in Emacs, which allows users to compile programs, run tests, and run interpreters directly within the editor.
Why does the speaker think many people are not aware of the compile package in Emacs?
-The speaker believes many people are not aware of the compile package in Emacs because they often resort to creating packages or using plugins, not realizing that Emacs has built-in functionality for these tasks.
What is the purpose of the 'compile' command in Emacs as discussed in the script?
-The 'compile' command in Emacs is used to execute external commands, such as compiling code, running tests, or executing interpreters, and it can be customized for different programming languages and tasks.
How can the 'compile' command be customized for different file types in Emacs?
-The 'compile' command can be customized for different file types by setting the 'compile-command' local variable in the file or by using a hook in the mode to automatically set the command when a file of that type is opened.
What is the benefit of using the 'compile' command for error navigation in Emacs?
-Using the 'compile' command for error navigation in Emacs allows users to quickly jump between errors in their code by using the 'n' and 'p' keys, and pressing 'enter' to go directly to the error location.
How can the 'compile' package handle colored output from compilation commands?
-The 'compile' package can handle colored output by using the 'ansi-color' package and a filter that processes the ANSI color codes into a format Emacs can display.
What is the significance of the 'project.el' functionality mentioned in the script?
-The 'project.el' functionality is significant because it allows the 'compile' command to recognize the root of a project and execute compilation commands from there, which is helpful for managing compilation in larger projects.
How can the 'compile' package be used to update X resources files in Emacs?
-The 'compile' package can be used to update X resources files by setting the 'compile-command' to run 'xrdb' with the file name, allowing users to update their X resources by simply invoking the 'compile' command.
What is the advantage of using the 'compile' package over external plugins for compilation in Emacs?
-Using the 'compile' package over external plugins has the advantage of leveraging Emacs' built-in functionality, which is powerful and does not require additional package management or configuration.
How can the 'compile' package be extended to support new compilers or interpreters in Emacs?
-The 'compile' package can be extended to support new compilers or interpreters by adding new entries to the 'compilation-error-regexp-alist' and 'compilation-error-regexp-alist-alist' with the appropriate regular expressions to match error messages.
What is the role of 'brilliant.org' in the video script?
-In the video script, 'brilliant.org' is mentioned as a sponsor that offers interactive courses in Python, math, data science, and computer science, which can be beneficial for learning at one's own pace.
Outlines
💻 Exploring Emacs' Compile Package
The speaker begins by expressing their fondness for Emacs, an advanced text editor with built-in capabilities for compiling programs, running tests, and interpreters. They note that while many users create custom packages for these tasks, Emacs already has a powerful built-in compile package that is often underutilized. The video aims to explore this package, particularly for compiling Python programs. The speaker also mentions their recent haircut and introduces the sponsor for the video, Brilliant, which offers interactive courses in Python, math, data science, and computer science.
🐍 Utilizing Emacs for Python Development
The focus shifts to using Emacs for Python development. The speaker demonstrates how to install and use the compile package within Emacs to run Python scripts. They explain the use of the 'compile' command, which by default uses 'make' but can be customized for Python. The video shows how to run Python scripts directly from Emacs and navigate through compilation errors using the 'n' and 'p' keys. The speaker also discusses setting up a default compile command for Python files and handling error messages with special filters.
🎨 Customizing Emacs for Colorful Compilation Outputs
The video continues with customization tips for Emacs to handle colorful compilation outputs. The speaker shows how to enable error scrolling for long lists of errors and how to handle colored output from compilation commands. They introduce the 'ansi-color' package and demonstrate how to filter and display ANSI colors within the Emacs compilation buffer. Additionally, the speaker explains how to set up special environments for compilation within Emacs, which can be useful for setting specific variables or paths for the compilation process.
🔍 Advanced Error Handling and Regex in Emacs
The speaker delves into advanced error handling in Emacs, explaining how to create and use regular expressions to parse and navigate compilation errors. They guide viewers through the process of building a regex for Python's pyite tool and adding it to Emacs' compilation error parsing system. The video also covers how to use the compilation functionality within Emacs' shell and terminal modes, allowing for seamless error navigation even when working in those environments. The speaker concludes by expressing excitement about these features and encourages viewers to explore and utilize Emacs' compile package.
🙌 Appreciation and Closing Remarks
In the final paragraph, the speaker expresses gratitude to their supporters on Patreon and GitHub Sponsors, acknowledging their importance and the impact of their support. They reflect on the value of the compile package in Emacs and encourage viewers to experiment with it, especially for those who work with shells or terminals frequently. The speaker wraps up the video with a warm thank you and a wish for the viewers to have a great day.
Mindmap
Keywords
💡Emacs
💡Compile Package
💡IDE (Integrated Development Environment)
💡Python
💡Error Navigation
💡Filters
💡X Resources File
💡Shell Minor Mode
💡V Term
💡Regular Expressions
Highlights
Emacs is a powerful editor with built-in capabilities for compiling programs, running tests, and interpreters without needing additional plugins.
The compile package in Emacs is versatile and underutilized by many users.
Emacs can be used for programming in Python, with the compile package facilitating tasks like running Python scripts.
Brilliant.org is highlighted as a sponsor, offering interactive courses in Python, math, data science, and computer science.
The compile command in Emacs can be customized for different tasks, such as running 'xrdb' for X resources files.
Errors in code can be navigated using 'n' and 'p' keys in Emacs, with the compile package automatically detecting and highlighting them.
The 'compile' command can be set up on a per-file basis or as a default command for specific programming languages like Python.
Emacs can scroll through errors automatically when compiling, enhancing the debugging process.
The ansi-color package and a custom filter can be used to handle colored output from compile commands in Emacs.
Special environments can be set up for compilation within Emacs, useful for setting variables like 'HOME' for root.
Regular expressions can be used to create custom filters for different compilers and error messages in Emacs.
The 'compile' functionality can be used in shell minor mode in Emacs, allowing navigation between errors in shell commands.
The same compile functionality works in V term within Emacs, showing the versatility of the compile package.
The video concludes with a call to action for viewers to try out the compile package and explore its features.
The video creator expresses gratitude to supporters on Patreon and GitHub sponsors for their ongoing support.
Transcripts
how's it going everybody I love emac
emac is a perfectly great editor and it
has a lot of amazing things built into
it and one thing that I think I as well
as probably the majority of people that
have used emac have wanted to do is
compile programs run tests and run
interpreters you know the usual stuff
that you would expect to be able to do
in an IDE now I've seen a lot of people
create packages and come up with their
own personal ways to do this in emac
however I don't think as many people
realize that this is built into emac you
don't need to use some special Plugin or
package to do it and it's actually
really powerful and I see a lot of
useful features that I don't see enough
people taking advantage of so in this
video I wanted to dig into the compile
package which is built into emac that
can be used for doing all of these
different things and more hey everybody
future Gavin here yes I did get a
haircut thank you very much for noticing
now in this video I'll be referring to
the programming language python quite
heavily if you aren't familiar with
python and would like to learn it for
free be sure to to check out today's
sponsor brilliant brilliant offers an
amazing interactive and multi-level
python course and if that's not your
thing then maybe try and dip your toes
into any of its multile levels of math
data science and computer science
courses with new courses every month
you'll never run out of content and
you'll never run out of learning
opportunities in addition to brilliant
letting you learn at your own pace it
can make the chaotic life that a lot of
us live much easier and much more of an
opportunity to learn at any given moment
whether you be sitting on the bus
waiting for a call anything like that
you can always pick up brilliant and get
a little bit of learning done each day
for me this has been a huge impact in my
life since I don't have a lot of time
day-to-day to research and learn new
things now I only have a few minutes to
myself every few days because of that
brilliant's been a huge help in this
area since I can pick it up for a few
minutes do a bit of learning and since
it's so interactive I tend to absorb it
a lot better than a big binge moment
where I try to learn a bunch on a topic
in a few hours to try everything
brilliant has to offer now for free for
a full 30 days check out brilliant.org
Gavin Freeborn the first 200 of view
will get 20% off Brilliance premium
annual subscription now let's get into
the video now for this video we will be
using the compile package so we can just
evaluate that eisp right here just to
get the compile package installed and
then what we can do is we can use MX
compile and this will by default it will
give you make- K and so this is
basically a simple one if you're using
make files for your project um but
that's not very helpful in our case cuz
we're not going to be using make we're
going to be using python and a python
file so just to open up a python file
right here this is just a temp file and
we can go ahead and try this out so we
can do alt X compile this will give us a
prompt where we can put in any command
we could do pilent python we could even
run any program on this really and by
default you could also do it without the
file name obviously so hitting enter on
that will simply run python tmpy as
you'd expect and this will give us the
result right here hello world and and if
we do compile again we get the same
result we can also do uh recompile and
this will just run whatever the last
command we had was alternatively in the
compile window you can hit G and this
will rerun as you can see down here
recompile which is pretty powerful now
if you're in a project and you're using
project. you can use control
xpc and this will do the same thing but
it will compile from wherever project.
thinks the root of your project is which
is pretty helpful and we'll use that
later on in this video so you can use
the compile command for a bunch of
different things the most important one
is finding errors so by default if you
are in our python file and we made an
error so let's put l at the start of
this and let's maybe hit a couple extra
lines just for the sake of uh showing
this and then if we did compile and we
compile it you see that we will get an
error now we can actually navigate
between these errors by hitting n and p
by default and enter will actually take
us to the error itself so how does it
know about these errors well it's using
special filters that we'll talk about
later in the video but this is super
helpful and Powerful for General
debugging and testing and stuff like
that now compile has a few other useful
features this is uh one that I got from
the emac wiki actually is if you go to a
file like this this right here is an X
resources file and whenever you modify
an X resources file and you want to
update it you have to run a command
called
xrdb and then the file name which right
here I've added this little comment
which will actually set the compile
command so if I run alt X compile it
actually sets xrdb and the file name as
the compile command so you can actually
use it for a bunch of different stuff um
other than just compiling code and this
is a really easy way to set it on a
perile basis for example when I was
using dwm actually I have a little uh
shortcut right here so that way when I
do compile it will do pseudo make clean
install which is uh super useful and so
you can use this for basically any file
alternatively you can set up a default
command so right here this is basically
a really quick little command it will
set the local variable called compile
command this is what compile by default
uses and I'm basically con catting
python with the current buffer name so
let's go ahead and add that and then
we'll add this to the hook to python
mode so whenever I open a file in Python
mode this will be a set up for us I
opened it brand new this time and so
when I do compile instead of giving the
US the previous thing that we had before
it's able to actually determine the path
for us so let's just go ahead and test
this out with our error here and when we
do compile yes it will give us the error
message as we would expect so what if we
want our errors to scroll so for example
if we get a ton of Errors let's go to
D.C and we do compile make clean and
make so this will just basically rebuild
our project we get a bunch of errors but
by default it's not going to know to
auto scroll to kind of just bring us to
the bottom and so what we can do to do
that is just evaluate this emac code
right here and then if we go ahead and
compile
again it will scroll our errors with us
which could be very helpful now what if
our compile command produces colors well
this is pretty easy to handle so let's
go ahead and give you an example of what
this would look like so in let's go to
this project and we will compile but
instead of using our previous command
what we're going to do is we're going to
do gp-- color equals always and this
will be searching actually let's search
for
base so this will be searching all
throughout this project and as you can
see right here we are getting a bunch of
garbage because of the Escape sequences
so that's not going to really work for
us so to solve this we can use the an
color package as well as this filter
right here um which I will link both
these down in the description this
basically adds an extra filter to our
compilation buffer to basically filter
our ansy colors into something we can
can actually use so let's go ahead and
go back to here compile run the same
compile command and now as you can see
we get pretty little colors no longer
all these ugly Escape sequences now this
setting right here for the special
environments isn't useful for everyone
but say for example you're compiling
something and you need to set some
special variable so here I'm setting the
home directory as root for when I'm
compiling things and if I go ahead and
do compile and we Echo home you'll see
that we get root now if I was to get the
environment of home we would get Gavin
okay because this is only available in
the compilation buffer uh super useful
if you guys are ever um needing a
special environment that's not going to
be available when you're programming
this is kind of useful if you don't want
to mess up your LSP but you need to do
some special compilation settings um at
certain points this can be useful for
that now I alluded to a way to kind of
set up different filters I talked about
the sort of filters that were able to
determine what kind of Errors we're
getting and so these are actually
determined using a regular expression
now there's two important variables we
have here so if we did uh let's go to a
python project and if I did actually
let's do a smaller python project uh if
I did alt X compile and I Chang this to
pyite Dot and hit enter just give it a
second and we will get a bunch of errors
and here we go so as you can see we have
a bunch of errors but it's not really
able to kind of determine where the
errors are coming from and so this is
because there isn't a regular expression
available for it to use if we go back
here and we look at these variables so
we do hvv and we take a look at this
variable this variable is basically a
list of different filters it can use or
different regular Expressions it can use
and there's a big old breakdown down
here and basically all these values are
looked up in this compilation error rega
XP aist aist so the A-list A-list is an
a list of all these different values
that we get rather than the original
which doesn't really have anything other
than just these symbols which are
basically references to the actual
regular expression in this A-list down
here so what we need to do is we need to
basically add a symbol which is going to
represent our different compiler and
we're going to add that to just the
regular A-list and then we also need to
actually add something to the um the
A-list A-list I know kind of annoying
that basically references that symbol
and the regular expression and then how
we want to match that regular expression
to each of the different uh values so
here I've set up one for pyite but we
can actually kind of derive something
like this on our own so let's go ahead
and go to the errors and so what we can
use to derive this is we can do alt X
and we can do regex Builder now what we
can do here is we can kind of come up
with how we want to do this so what we
want to do is we want to capture things
so as we can see our error messages are
basically two spaces so we want to do
back slash back slash space why do we
need two backslashes well a backslash is
an escape and since this is a string we
need two backslashes and if we did that
again in space then we've matched those
two spaces leading up to it now a back
slash backs slash and a parenthesis this
will basically capture something and so
now what we want to capture is we want
to capture uh any combination of a slash
A to Z A to Z basically saying you want
to capture against a slash any letters
we also need to handle a dash or an
underscore and then we want to repeat
that so any repeated thing of any of
these varieties so this will capture our
file oh and I forgot the period there we
go now we can capture the pi at the end
and so that will capture our file name
now we need to do a colon and then we
need to capture our line number so doing
the same thing we will do 0
to9 Plus so basically any number from
zero to nine and then that's what we are
going to capture and then colon and then
we need to do the exact same thing
because we're basically instead of
capturing a line number we're capturing
a
column and that should be enough for now
so as you can see pretty easy to capture
all of this stuff and then you could
just say do a DAT star dollar sign uh
basically just as a way to capture
everything else all right cool now we
have created our regex now if we put put
that here it's basically I think it
actually is identical yeah okay that is
identical to what I ended up writing
originally now we want to add this to
our a list aist but the big thing that
we still need to do is determine how
we're going to map from this file which
is here the line number that is here and
the column number that is here now the
nice thing is that basically the way
that the actual matching works is in our
A-list we give it our um obviously the
symbol to match to it we give give it a
regular expression and then each of
these represents one of the things that
we've captured so the first one is our
file the second one is our line number
and the third one is the column now if
we were to transpose this that basically
means that the first thing we captured
was our number our line number the
second thing was our file and the third
thing was our column uh so basically
luckily it's pretty self-explanatory out
of the box when you go into this so if
you actually look at this variable it
explains it decently well um but you'd
probably want to look at it so basically
what's our reex and then the first thing
that it captures is your file and so we
just say that the file is the first
element and then optionally you can give
a line number you can give a column
number and a type that's kind of how you
can add your own filters if you have
questions about that feel free to ask me
because I feel like this was actually
kind of a questionable example to use um
but yeah now we can add it by doing CR X
CR e on each of these guys and then if
we go down into here and we hit G to
rerun our compile compilation we will
actually get something
useful and there we go so now we can
like navigate by hitting NP and we can
see all of our
errors very powerful very simple okay
now this one is kind of just something
that I never expected to see in the
editor but I found really cool and so
that is if we go to a Shell let's go to
the same project we were looking at
before and we did pyite dot so there we
go we get our errors now obviously in
the Shell we're not going to have the
same available to us as we did in
compile mode but you can do shell minor
mode hit enter and this will actually
allow you to use those very same regular
Expressions as you had previously and
then if you do contrl alt P this will
navigate between them and if you hit
enter it will jump to the file and you
can use all those same rxes it works
exactly as you'd expect it works with
your mouse too now this is pretty sweet
but some people don't even use the shell
uh functionality well let me tell you
this it works in other places there's
even more uses for it so let's go ahead
and kill this shell and instead we will
open V term now in V term if we CD to
the same thing CD Aries uh endorser and
we do pyite dot we get the same errors
just a second and there we go we've
gotten the same errors now if we enter
the same mode yep that's right it works
just as you'd expect most support and
everything even in V term which is super
awesome not really something that I
expect Ed to see and I actually was just
only recently told about this the other
day so pretty cool a really awesome
feature if you guys make a lot of use of
the shells or terminals you can make use
of the compilation functionality right
from there anyways guys I appreciate all
of you for coming and watching this
video I hope that really last part got
you as excited as it got me excited when
I found out about it because even though
it's not something I use every single
day it's something that every once in a
while I'll find like a back trace or an
error message in some logs when I had a
shell open and I can just run this and
boom right away I can jump to the errors
and I don't have to like reverse
engineer where it would be which is
really awesome uh highly recommend
giving some of this stuff a try
especially just the general compile
functionality um yeah anyways that's
about it for me today uh I would like to
give a big shout out to my supporters on
patreon and GitHub sponsors you guys
have done so much for me um yeah it
really means a lot that you guys have
supported me especially when you know
sometimes I'm not able to upload as much
as I would like and yeah I I really
appreciate it great seeing you guys
great talking to you and I hope you have
a wonderful
[Music]
day
Weitere ähnliche Videos ansehen
5.0 / 5 (0 votes)