From f8be670a1c09b23e11ef3d0e9612c54f6064c206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20F=C3=BCrmetz?= Date: Sun, 10 Jan 2016 20:10:56 +0100 Subject: [PATCH] Edit .dir-locals.el file of a project Add a command `projectile-edit-dir-locals' to quickly edit or create a .dir-locals.el file for the project. --- CHANGELOG.md | 1 + README.md | 4 ++++ projectile.el | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a67dcf07..e7acff50e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * Add relation between the `.h`, `.cxx`, `.ixx` and `.hxx` files in `projectile-other-file-alist`. * Add support to specify project name either via `.dir-locals.el` or by providing a customized `projectile-project-name-function'. * Add a command to switch between open projects (`projectile-switch-open-project`). +* Add a command to edit the .dir-locals.el file of the project (`projectile-edit-dir-locals`). ### Changes diff --git a/README.md b/README.md index 7df9a8bc8..ddab9734f 100644 --- a/README.md +++ b/README.md @@ -362,6 +362,7 @@ Keybinding | Description C-c p k | Kills all project buffers. C-c p D | Opens the root of the project in `dired`. C-c p e | Shows a list of recently visited project files. +C-c p E | Opens the `.dirs-local.el` file of the project. C-c p s s | Runs `ag` on the project. Requires the presence of `ag.el`. C-c p ! | Runs `shell-command` in the root directory of the project. C-c p & | Runs `async-shell-command` in the root directory of the project. @@ -498,6 +499,9 @@ entire project. A key with the name `eval` will evaluate its arguments. In the example above, this is used to create a function. It could also be used to e.g. add such a function to a key map. +You can also quickly visit the the `.dir-locals.el` file with C-c +p E (M-x projectile-edit-dir-locals RET). + Here are a few examples of how to use this feature with Projectile. #### Configuring Projectile's Behavior diff --git a/projectile.el b/projectile.el index fd0fedc38..76f687f56 100644 --- a/projectile.el +++ b/projectile.el @@ -2640,6 +2640,41 @@ is chosen." (projectile-commander-bindings) +(defun projectile-read-variable () + "Prompt for a variable and return its name." + (completing-read "Variable: " + obarray + '(lambda (v) + (and (boundp v) (not (keywordp v)))) + t)) + +(define-skeleton projectile-skel-variable-cons + "Insert a variable-name and a value in a cons-cell." + "Value: " + "(" + (projectile-read-variable) + " . " + str + ")") + +(define-skeleton projectile-skel-dir-locals + "Insert a .dir-locals.el template." + nil + "((nil . ((" + ("" '(projectile-skel-variable-cons) \n) + resume: + "))))") + +(defun projectile-edit-dir-locals () + "Edit or create a .dir-locals.el file of the project." + (interactive) + (let ((file (expand-file-name ".dir-locals.el" (projectile-project-root)))) + (find-file file) + (when (not (file-exists-p file)) + (unwind-protect + (projectile-skel-dir-locals) + (save-buffer))))) + ;;; Minor mode (defvar projectile-command-map (let ((map (make-sparse-keymap))) @@ -2658,6 +2693,7 @@ is chosen." (define-key map (kbd "d") #'projectile-find-dir) (define-key map (kbd "D") #'projectile-dired) (define-key map (kbd "e") #'projectile-recentf) + (define-key map (kbd "E") #'projectile-edit-dir-locals) (define-key map (kbd "f") #'projectile-find-file) (define-key map (kbd "g") #'projectile-find-file-dwim) (define-key map (kbd "F") #'projectile-find-file-in-known-projects) @@ -2704,6 +2740,7 @@ is chosen." ["Jump between implementation file and test file" projectile-toggle-between-implementation-and-test] ["Kill project buffers" projectile-kill-buffers] ["Recent files" projectile-recentf] + ["Edit .dir-locals.el" projectile-edit-dir-locals] "--" ["Open project in dired" projectile-dired] ["Switch to project" projectile-switch-project]