summaryrefslogtreecommitdiff
path: root/lisp/km-compile.el
blob: 97bd44e05db81cdf029f568865518635b4c443b1 (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
;;; km-compile.el --- Compilation extensions

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

;; Author: Kyle Meyer <kyle@kyleam.com>
;; URL: https://git.sr.ht/~kyleam/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 'compile)
(require 'dash)

(defvar km/compilation-buffer-name-prefix "compilation: ")

(defun km/compilation-name-by-directory (&optional mode)
  (let ((name (if (and mode (not (equal mode "compilation")))
                  (downcase mode)
                (concat km/compilation-buffer-name-prefix
                        (abbreviate-file-name default-directory)))))
    (concat "*" name "*")))

(defun km/compilation-buffer-p (buffer)
  (with-current-buffer buffer
    (and (derived-mode-p 'compilation-mode)
         (string-prefix-p (concat "*" km/compilation-buffer-name-prefix)
                          (buffer-name)))))

(defvar km/compilation-last-buffer nil
  "Like `compilation-last-buffer' but don't set in derived modes.")

(defun km/store-compilation-last-buffer (_proc)
  (when (eq major-mode 'compilation-mode)
    (setq km/compilation-last-buffer (current-buffer))))

(add-hook 'compilation-start-hook #'km/store-compilation-last-buffer)

;;;###autoload
(defun km/compile-in-home-dir ()
  (interactive)
  (let ((default-directory "~/"))
    (call-interactively #'compile)))

;;;###autoload
(defun km/compilation-recompile (&optional arg)
  "Recompile buffer.
By default, use `km/compilation-last-buffer'.  If ARG is 0, get
buffer with name given by `km/compilation-name-by-directory'.
Otherwise, if ARG is non-nil, prompt with buffers from
`km/compilation-buffer-list'."
  (interactive (list (and current-prefix-arg
                          (prefix-numeric-value current-prefix-arg))))
  (with-current-buffer (km/compilation--get-buffer arg)
    (recompile)))

(defun km/compilation-display-buffer (&optional arg)
  "Display compilation buffer.
By default, use `compilation-last-buffer'.  If ARG is 0, get
buffer with name given by `km/compilation-name-by-directory'.
Otherwise, if ARG is non-nil, prompt with buffers from
`km/compilation-buffer-list'."
  (interactive (list (and current-prefix-arg
                          (prefix-numeric-value current-prefix-arg))))
  (display-buffer (km/compilation--get-buffer arg)))

(defun km/compilation--get-buffer (&optional arg)
  (cond
   ((and (not arg)
         (buffer-live-p km/compilation-last-buffer)
         km/compilation-last-buffer))
   ((and (numberp arg)
         (= arg 0))
    (get-buffer (km/compilation-name-by-directory)))
   (t
    (let ((cbufs (-map #'buffer-name (km/compilation-buffer-list)))
          buf)
      (cl-case (length cbufs)
        (0 (user-error "No compilation buffers found"))
        (1 (setq buf (car cbufs)))
        (t (setq buf (completing-read "Compilation buffer: " cbufs
                                      nil nil nil nil (car cbufs)))))
      buf))))

(defun km/compilation-buffer-list ()
  (-filter #'km/compilation-buffer-p (buffer-list)))

(defun km/compile-make-pdf-target (command)
  (and (string-match (rx string-start
                         (zero-or-one "snake") "make"
                         (zero-or-more not-newline) space
                         (group (one-or-more (not space)) ".pdf")
                         (zero-or-more space)
                         string-end)
                     command)
       (match-string 1 command)))

(defun km/compile-latexmk-pdf-target (command)
  (and (string-match (rx string-start
                         "latexmk"
                         (zero-or-more not-newline) space
                         (group (one-or-more (not space)))
                         (zero-or-one ".tex")
                         (zero-or-more space)
                         string-end)
                     command)
       (concat (match-string 1 command) ".pdf")))

(defvar km/compile-pdf-functions '(km/compile-make-pdf-target
                                   km/compile-latexmk-pdf-target))

(defun km/compile-check-pdf (buf exit)
  (when (equal exit "finished\n")
    (with-current-buffer buf
      (let* ((cmd (car compilation-arguments))
             (pdf-file (run-hook-with-args-until-success
                        'km/compile-pdf-functions cmd)))
        (when pdf-file
          (call-process "xdotool" nil nil nil
                        "search" "--all" "--class" "--name"
                        (concat "^mupdf|" (file-name-nondirectory pdf-file))
                        "key" "--window" "%@" "r"))))))

(add-to-list 'compilation-finish-functions #'km/compile-check-pdf)

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