Lines of code in my Emacs packages

John Haman

2020/07/11

Categories: Emacs R Tags: Emacs R

I was curious about how many lines of code comprised the individual emacs packages that I use, and decided to write a R script that will count the lines of code and make a little chart:

List all the packages in your ELPA directory:

library(utils)
p <- "~/.emacs.d/elpa/"
ps <- list.files(p, full.names = TRUE)
pshort <- list.files(p)

For simplicity, I just count the lines of code in the “.el” files. Some packages like Auctex have a lot of non-.el files that I’m just ignoring, but most of the code should be in the “.el” files.

The loop goes through each Emacs package and recursively counts the lines of code in the folder. If a folder has no “.el” files, we’ll count that as 0 lines of code.

(Btw, I’m not a programmer so this is just my naive solution, I’m sure there are much more elegant and accurate ways to do this…)

project_loc <- rep(NA, length(ps))
names(project_loc) <- pshort

for(i in 1:length(ps)) {
  els <- list.files(ps[i], recursive = TRUE, pattern = ".el$", full.names = TRUE)
  if (length(els) > 0)
    project_loc[i] <- sum(sapply(els, function(x) length(readLines(x))))
  else
    project_loc[i] <- 0
}

Here’s the number of lines of code for all my packages. The bar color is just proportional to the height of the bar, it doesn’t add any new information.

library(ggplot2)
library(forcats)

theme_set(theme_bw(base_size = 16))

pshort <- gsub("-\\d*\\.*\\d*\\.*\\d*$", "", pshort) #clean names

dat <- data.frame(pkg = pshort,
                  loc = project_loc)

dat$pkg<- fct_reorder(dat$pkg, dat$loc)

ggplot(dat, aes(x = pkg, y = loc)) +
  geom_bar(stat = "identity", aes(fill = loc)) +
  scale_y_continuous(n.breaks = 10) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position = "none") +
  ggtitle("Lines of Code of each installed package (.el files only)")

And the same graph, but on the \(\log_{10}\) scale.

options(scipen=10000)

ggplot(dat, aes(x = pkg, y = loc)) +
  geom_bar(stat = "identity", aes(fill = log10(loc))) +
  scale_y_log10(n.breaks = 10) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position = "none") +
  ggtitle("Lines of Code of each installed package (.el files only)")

Next, it could be interesting to regress the load time of each package on the total lines of code in the package to see if there is any relationship. But I defer some of my packages from loading on startup and don’t want to make any changes to my init file just yet.

Would be interesting if someone could apply my code to an installation of Doom or Spacemacs.