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 ()
  (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)