about summary refs log tree commit diff stats
path: root/content/post/opening-projects-with-projectile.md
diff options
context:
space:
mode:
Diffstat (limited to 'content/post/opening-projects-with-projectile.md')
-rw-r--r--content/post/opening-projects-with-projectile.md20
1 files changed, 10 insertions, 10 deletions
diff --git a/content/post/opening-projects-with-projectile.md b/content/post/opening-projects-with-projectile.md index 04f8cb6..d88d309 100644 --- a/content/post/opening-projects-with-projectile.md +++ b/content/post/opening-projects-with-projectile.md
@@ -12,30 +12,30 @@ With this in mind, I decided to try to add support for opening projects under a
12 12
13I saw that projectile uses [Dash.el][] in some places, and after reading about [anaphoric macros], I decided that I'd try to use them to aid me. 13I saw that projectile uses [Dash.el][] in some places, and after reading about [anaphoric macros], I decided that I'd try to use them to aid me.
14 14
15{{% highlight cl %}} 15```elisp
16(defun ap/subfolder-projects (dir) 16(defun ap/subfolder-projects (dir)
17 (--map (file-relative-name it dir) 17 (--map (file-relative-name it dir)
18 (-filter (lambda (subdir) 18 (-filter (lambda (subdir)
19 (--reduce-from (or acc (funcall it subdir)) nil 19 (--reduce-from (or acc (funcall it subdir)) nil
20 projectile-project-root-files-functions)) 20 projectile-project-root-files-functions))
21 (-filter #'file-directory-p (directory-files dir t "\\<"))))) 21 (-filter #'file-directory-p (directory-files dir t "\\<")))))
22{{% /highlight %}} 22```
23 23
24First, this filters the non-special files under `dir`, filtering non-directories. Then it runs the list of `projectile-project-root-files-functions` on it to determine if it looks like a projectile project. To make the list more readable, it makes the filenames relative to the passed-in directory. It runs like this: 24First, this filters the non-special files under `dir`, filtering non-directories. Then it runs the list of `projectile-project-root-files-functions` on it to determine if it looks like a projectile project. To make the list more readable, it makes the filenames relative to the passed-in directory. It runs like this:
25 25
26{{% highlight cl %}} 26```elisp
27(ap/subfolder-projects "~/projects") => 27(ap/subfolder-projects "~/projects") =>
28("dotfiles" "ggtags" …) 28("dotfiles" "ggtags" …)
29{{% /highlight %}} 29```
30 30
31So, we've got ourselves a list, but now we need to be able to open the project that's there, even though the folders are relative. 31So, we've got ourselves a list, but now we need to be able to open the project that's there, even though the folders are relative.
32 32
33{{% highlight cl %}} 33```elisp
34(defun ap/open-subfolder-project (from-dir &optional arg) 34(defun ap/open-subfolder-project (from-dir &optional arg)
35 (let ((project-dir (projectile-completing-read "Open project: " 35 (let ((project-dir (projectile-completing-read "Open project: "
36 (ap/subfolder-projects from-dir)))) 36 (ap/subfolder-projects from-dir))))
37 (projectile-switch-project-by-name (expand-file-name project-dir from-dir) arg))) 37 (projectile-switch-project-by-name (expand-file-name project-dir from-dir) arg)))
38{{% /highlight %}} 38```
39 39
40By wrapping the call to `ap/subfolder-projects` in another function that takes the same directory argument, we can re-use the project parent directory and expand the selected project name into an absolute path before passing it to `projectile-switch-project-by-name`. 40By wrapping the call to `ap/subfolder-projects` in another function that takes the same directory argument, we can re-use the project parent directory and expand the selected project name into an absolute path before passing it to `projectile-switch-project-by-name`.
41 41
@@ -43,7 +43,7 @@ We get support for multiple completion systems for free, since projectile has a
43 43
44Then I defined some helper functions to make it easy to open work and home projects. 44Then I defined some helper functions to make it easy to open work and home projects.
45 45
46{{% highlight cl %}} 46```elisp
47(defvar work-project-directory "~/work") 47(defvar work-project-directory "~/work")
48(defvar home-project-directory "~/projects") 48(defvar home-project-directory "~/projects")
49 49
@@ -54,7 +54,7 @@ Then I defined some helper functions to make it easy to open work and home proje
54(defun ap/open-home-project (&optional arg) 54(defun ap/open-home-project (&optional arg)
55 (interactive "P") 55 (interactive "P")
56 (ap/open-subfolder-project home-project-directory arg)) 56 (ap/open-subfolder-project home-project-directory arg))
57{{% /highlight %}} 57```
58 58
59I could probably simplify this with a macro, but I'm not sure that there's much advantage in it. I only have two project types right now, after all. 59I could probably simplify this with a macro, but I'm not sure that there's much advantage in it. I only have two project types right now, after all.
60 60
@@ -62,14 +62,14 @@ With this all set up, whenever I want to start working on a project I just type
62 62
63I also considered trying to add all the projects under a directory to the projectile known project list. I didn't find it quite as easy to use, but it's available below if anyone would prefer that style. 63I also considered trying to add all the projects under a directory to the projectile known project list. I didn't find it quite as easy to use, but it's available below if anyone would prefer that style.
64 64
65{{% highlight cl %}} 65```elisp
66(defun ap/-add-known-subfolder-projects (dir) 66(defun ap/-add-known-subfolder-projects (dir)
67 (-map #'projectile-add-known-project (--map (concat (file-name-as-directory dir) it) (ap/subfolder-projects dir)))) 67 (-map #'projectile-add-known-project (--map (concat (file-name-as-directory dir) it) (ap/subfolder-projects dir))))
68 68
69(defun ap/add-known-subfolder-projects () 69(defun ap/add-known-subfolder-projects ()
70 (interactive) 70 (interactive)
71 (ap/-add-known-subfolder-projects (ido-read-directory-name "Add projects under: "))) 71 (ap/-add-known-subfolder-projects (ido-read-directory-name "Add projects under: ")))
72{{% /highlight %}} 72```
73 73
74[Projectile]: https://github.com/bbatsov/projectile 74[Projectile]: https://github.com/bbatsov/projectile
75[Dash.el]: https://github.com/magnars/dash.el 75[Dash.el]: https://github.com/magnars/dash.el