Emacs Lisp

Table of Contents

1. Per-directory minor modes

The following example enables company-mode only for JavaScript (and related) files for some given project path:

(defun per-directory-company-mode ()
  (let ((fname (buffer-file-name)))
    (when fname
      (let ((fname (expand-file-name fname)))
        (when (string-prefix-p "/home/b0h/development/project/" fname)
          (when (or
                 (string-suffix-p ".ts" fname)
                 (string-suffix-p ".tsx" fname)
                 (string-suffix-p ".js" fname)
                 (string-suffix-p ".jsx" fname))
            (company-mode 1)))))))
(add-hook 'find-file-hook 'per-directory-company-mode)

2. Per-directory build commands

The following example makes pressing F5 run different build commands depending on where the file you're editing is located:

(defun build ()
  (interactive)
  (let ((fname (buffer-file-name))
        (compilation-scroll-output t))
    (when fname
      (let ((fname (expand-file-name fname)))
        (cond ((string-prefix-p "/home/b0h/development/project1/" fname)
               (let ((default-directory "/home/b0h/development/project1/"))
                 (compile "npm run build ")))
              ((string-prefix-p "/home/b0h/development/project2/" fname)
               (let ((default-directory "/home/b0h/development/project2/"))
                 (compile "make all "))))))))
(global-set-key (kbd "<f5>") 'build)

3. Load all files in a directory matching some regexp

Useful if you always want some set of files loaded at all times (projects, etc)

(defun load-files ()
  (interactive)
  (let ((projects '(("/home/b0h/Development/project1" "\\.qc$" "\\.src$")
                    ("/home/b0h/Development/project2" "\\.tsx?$"))))
    (dolist (project projects)
      (let ((dir (car project))
            (fileregexes (cdr project)))
        (dolist (fileregex fileregexes)
          (let ((files (directory-files-recursively dir fileregex)))
            (dolist (file files)
              (find-file-noselect file))))))))