summaryrefslogtreecommitdiff
path: root/lisp/km-projectile.el
blob: e22eeae48550bd88ff67f62e0811458dc9e1bd8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
;;; km-projectile.el --- Projectile configuration

;; Copyright (C) 2012-2020 Kyle Meyer <kyle@kyleam.com>

;; Author: Kyle Meyer <kyle@kyleam.com>
;; URL: https://git.kyleam.com/emacs.d

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(require 'dired)
(require 'projectile)
(require 'org-element)

;;;###autoload
(defun km/projectile-switch-project (&optional arg)
  "Switch to a project.

Like `projectile-switch-project', but instead of calling
`projectile-commander' when a prefix argument ARG is given, save
something for the current project before switching.

`projectile-switch-project-action' is set to
`km/projectile-maybe-restore-thing'.  If the thing saved for the
destination project is the the window configuration, this may not
end up in the project if the buffers are now dead."
  (interactive "P")
  (when arg (call-interactively #'km/projectile-save-thing))
  (let ((projectile-switch-project-action 'km/projectile-maybe-restore-thing))
    (projectile-switch-project)))

(declare-function km/open-external-terminal "km-shell")
;;;###autoload
(defun km/projectile-open-external-terminal-in-root ()
  "Run `km/open-external-terminal' in project root."
  (interactive)
  (let ((default-directory (projectile-project-root)))
    (km/open-external-terminal)))

;;;###autoload
(defun km/projectile-view-file ()
  "View project file.
Interactive arguments are processed according to
`projectile-find-file'."
  (interactive)
  (call-interactively #'projectile-find-file)
  (view-mode 1))

;;;###autoload
(defun km/projectile-view-file-other-window ()
  "View project file in other window.
Interactive arguments are processed according to
`projectile-find-file-other-window'."
  (interactive)
  (call-interactively 'projectile-find-file-other-window)
  (view-mode 1))

(defun km/project-filename-at-point ()
  "Return file name relative to `projectile-project-root'.
In the case of multiple files marked in Dired, return the file
names separated by a space."
  (let* ((el (and (derived-mode-p 'org-mode)
                  (org-element-lineage (org-element-context) '(link) t)))
         (fname (or (and (eq (org-element-type el) 'link)
                         (org-element-property :path el))
                    (and (derived-mode-p 'dired-mode)
                         (dired-get-marked-files 'nodir nil))
                    (when-let ((fname (thing-at-point 'filename)))
                      (and (file-exists-p fname) fname)))))
    (when fname
      (mapconcat
       `(lambda (f) (file-relative-name f ,(projectile-project-root)))
       (if (listp fname) fname (list fname))
       " "))))

(defun km/projectile-project-filename ()
  (or (km/project-filename-at-point)
      (and buffer-file-name
           (file-relative-name buffer-file-name
                               (projectile-project-root)))))

;;;###autoload
(defun km/projectile-copy-project-filename-as-kill ()
  "Copy name of project file.
If point is on a file, copy this as the file name.  Otherwise,
use the name of the current file."
  (interactive)
  (when-let ((fname (km/projectile-project-filename)))
    (if (eq last-command 'kill-region)
        (kill-append fname nil)
      (kill-new fname))
    (message "%s" fname)))

;;;###autoload
(defun km/projectile-copy-project-module-as-kill ()
  "Copy name of project file, transformed into a project module.
Currently, only Python mode is supported."
  (interactive)
  (when (derived-mode-p 'python-mode)
    (when-let ((fname (km/projectile-project-filename))
               (module (replace-regexp-in-string
                        "\\.py" ""
                        (replace-regexp-in-string "/" "." fname nil t)
                        t t)))
      (if (eq last-command 'kill-region)
          (kill-append module nil)
        (kill-new module))
      (message "%s" module))))

(defvar km/projectile-project-saved-thing nil
  "Property list of saved thing for projects.
The keys are project roots (strings), so use `lax-plist-put' and
`lax-plist-get'.")

;;;###autoload
(defun km/projectile-save-thing (thing)
  "Save thing for current project.

Thing is a character representing
-  . point marker
- (b)uffer
- (f)ile
- (w)indow configuration

- (d)elete saved thing"
  (interactive (list
                (let ((letters '(?. ?b ?f ?w ?d)))
                  (read-char-choice (concat "Save [" letters "]: ")
                                    letters))))
  (let ((value (cl-case thing
                 (?.
                  (point-marker))
                 (?b
                  (current-buffer))
                 (?f
                  (buffer-file-name))
                 (?w
                  (current-window-configuration))
                 (?d nil))))
    (setq km/projectile-project-saved-thing
          (lax-plist-put km/projectile-project-saved-thing
                         (projectile-project-root)
                         (cons thing value)))))

(defun km/projectile-restore-thing ()
  "Restore saved thing for current project.
Return nil if there is no thing saved for the current project."
  (interactive)
  (when-let ((thing-value (lax-plist-get km/projectile-project-saved-thing
                                         (projectile-project-root)))
             (thing (car thing-value))
             (value (cdr thing-value)))
    (cl-case thing
      (?.
       (switch-to-buffer
        (or (marker-buffer value)
            (user-error "Buffer no longer exists")))
       (goto-char value))
      (?b
       (if (buffer-live-p value)
           (switch-to-buffer value)
         (user-error "Buffer no longer exists")))
      (?f
       (find-file value))
      (?w
       (set-window-configuration value)))
    t))

(defvar km/projectile-switch-fallback 'projectile-commander)

(defun km/projectile-maybe-restore-thing ()
  "Try to restore thing for current project.
If there is nothing ot restore, call
`km/projectile-switch-fallback'."
  (or (km/projectile-restore-thing)
      (funcall km/projectile-switch-fallback)))

;;;###autoload
(defun km/projectile-kill-buffers ()
  "Kill all project buffers.
Like `projectile-kill-buffers', but
- Before killing buffers, delete any saved thing for the project.
- Don't ask for confirmation to kill project buffers (but
  `kill-buffer' will still ask when killing a modified buffer)."
  (interactive)
  (km/projectile-save-thing ?d)
  (cl-letf (((symbol-function 'yes-or-no-p) (lambda (&rest args) t)))
    (projectile-kill-buffers)))

;;;###autoload
(defun km/projectile-kill-nondisplayed-buffers ()
  "Kill project buffers that aren't displayed in current frame"
  (interactive)
  (mapc #'kill-buffer
        (cl-remove-if
         (lambda (b) (or (buffer-base-buffer b)
                         (get-buffer-window b)))
         (projectile-project-buffers))))

(defun km/projectile-maybe-activate-xref-etags-mode ()
  (let ((root (projectile-project-root)))
    (when (and root
               (file-exists-p (expand-file-name "TAGS" root)))
      (xref-etags-mode 1))))

(provide 'km-projectile)
;;; km-projectile.el ends here