| 1 |
;;; bg-build-mode.el --- bg-build-mode.el -*- lexical-binding: t; -*- |
| 2 |
;; Copyright (C) 2007-2008 Vesa Karvonen |
| 3 |
;; |
| 4 |
;; MLton is released under a HPND-style license. |
| 5 |
;; See the file MLton-LICENSE for details. |
| 6 |
|
| 7 |
(require 'compile) |
| 8 |
(require 'bg-build-util) |
| 9 |
(if (string-match "XEmacs" emacs-version) |
| 10 |
(require 'overlay)) |
| 11 |
|
| 12 |
;; This is a minor mode for ``handsfree'' background batch building. See |
| 13 |
;; http://mlton.org/EmacsBgBuildMode for further information. |
| 14 |
|
| 15 |
;; XXX: Cleanup. |
| 16 |
;; XXX: Combinators for making common project configurations: |
| 17 |
;; - E.g. grep for saved files from given file |
| 18 |
;; XXX: Locate project file(s) automatically |
| 19 |
;; XXX: Context menu to the mode line indicator |
| 20 |
;; XXX: `mode-line-format' (XEmacs 21.4) support |
| 21 |
|
| 22 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 23 |
;; Prelude |
| 24 |
|
| 25 |
(defvar bg-build-load-time t) |
| 26 |
|
| 27 |
(defun bg-build-set-custom-and-update (sym val) |
| 28 |
(custom-set-default sym val) |
| 29 |
(unless bg-build-load-time |
| 30 |
(bg-build-update))) |
| 31 |
|
| 32 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 33 |
;; Customization |
| 34 |
|
| 35 |
(defgroup bg-build nil |
| 36 |
"A minor mode for ``handsfree'' background batch builds." |
| 37 |
:group 'compilation) |
| 38 |
|
| 39 |
(defcustom bg-build-action-on-failure (function first-error) |
| 40 |
"Optional action to perform when build fails." |
| 41 |
:type `(choice |
| 42 |
(const :tag "None" ,(function (lambda () nil))) |
| 43 |
(function :tag "Action")) |
| 44 |
:group 'bg-build) |
| 45 |
|
| 46 |
(defcustom bg-build-action-on-messages (function first-error) |
| 47 |
"Optional action to perform when build does not fail, but produces |
| 48 |
messages (typically warnings)." |
| 49 |
:type `(choice |
| 50 |
(const :tag "None" ,(function (lambda () nil))) |
| 51 |
(function :tag "Action")) |
| 52 |
:group 'bg-build) |
| 53 |
|
| 54 |
(defcustom bg-build-delay 1.0 |
| 55 |
"Idle time in seconds to delay before automatically starting a build |
| 56 |
after a save or nil if you wish to disable automatic builds." |
| 57 |
:type '(choice |
| 58 |
(const :tag "disable" nil) |
| 59 |
(number :tag "seconds")) |
| 60 |
:group 'bg-build) |
| 61 |
|
| 62 |
(defcustom bg-build-key-bindings |
| 63 |
'() |
| 64 |
"Key bindings for the bg-build mode. The key specifications must be in |
| 65 |
a format accepted by the function `define-key'. Hint: You might want to |
| 66 |
type `M-x describe-function bg-build <TAB>' to see the available commands." |
| 67 |
:type '(repeat (cons :tag "Key Binding" |
| 68 |
(string :tag "Key") |
| 69 |
(function :tag "Command"))) |
| 70 |
:set (function bg-build-set-custom-and-update) |
| 71 |
:group 'bg-build) |
| 72 |
|
| 73 |
(defcustom bg-build-highlighting-overlay-priority 500 |
| 74 |
"Priority of highlighting overlays." |
| 75 |
:type 'integer |
| 76 |
:group 'bg-build) |
| 77 |
|
| 78 |
(defcustom bg-build-max-live-builds 1 |
| 79 |
"Maximum number of live build processes to run concurrently or nil for |
| 80 |
unlimited." |
| 81 |
:type '(choice |
| 82 |
(const :tag "Unlimited" nil) |
| 83 |
(number :tag "Number")) |
| 84 |
:group 'bg-build) |
| 85 |
|
| 86 |
(defface bg-build-message-sexp-face |
| 87 |
'((((class color)) (:background "orange")) |
| 88 |
(t (:background "gray"))) |
| 89 |
"Face for highlighting sexps that are referred to in messages." |
| 90 |
:group 'faces |
| 91 |
:group 'bg-build) |
| 92 |
|
| 93 |
(defcustom bg-build-message-highlighting '(sexp) |
| 94 |
"How to highlight source locations corresponding to messages. Unselect |
| 95 |
all to disable highlighting." |
| 96 |
:type '(set (const :tag "Sexp" sexp)) |
| 97 |
:group 'bg-build) |
| 98 |
|
| 99 |
(defcustom bg-build-notify '(messages failure) |
| 100 |
"When to notify about completed builds." |
| 101 |
:type '(set (const :tag "Success" success) |
| 102 |
(const :tag "Messages" messages) |
| 103 |
(const :tag "Failure" failure)) |
| 104 |
:group 'bg-build) |
| 105 |
|
| 106 |
(defcustom bg-build-projects-auto-load nil |
| 107 |
"Automatic loading of `bg-build-projects-recent' at startup." |
| 108 |
:type '(choice |
| 109 |
(const :tag "Disabled" nil) |
| 110 |
(const :tag "Enabled" t)) |
| 111 |
:group 'bg-build) |
| 112 |
|
| 113 |
(defcustom bg-build-projects-recent '() |
| 114 |
"Automatically updated list of BGB files currently or previously loaded. |
| 115 |
This customization variable is not usually manipulated directly by the |
| 116 |
user." |
| 117 |
:type '(repeat |
| 118 |
(file :tag "BGB file" :must-match t)) |
| 119 |
:group 'bg-build) |
| 120 |
|
| 121 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 122 |
;; Project Object |
| 123 |
|
| 124 |
(defun* bg-build-prj (file &key name build? shell) |
| 125 |
"Creates a project object for bg-build." |
| 126 |
(list |
| 127 |
(cons 'name |
| 128 |
(cond ((functionp name) |
| 129 |
name) |
| 130 |
((stringp name) |
| 131 |
(bg-build-const name)) |
| 132 |
(t |
| 133 |
(bg-build-const |
| 134 |
(file-name-nondirectory file))))) |
| 135 |
(cons 'build? |
| 136 |
(cond ((functionp build?) |
| 137 |
build?) |
| 138 |
(t |
| 139 |
(bg-build-const t)))) |
| 140 |
(cons 'shell |
| 141 |
(cond ((functionp shell) |
| 142 |
shell) |
| 143 |
((consp shell) |
| 144 |
(bg-build-const shell)) |
| 145 |
((stringp shell) |
| 146 |
(bg-build-const (split-string shell "[ \n\t]+"))) |
| 147 |
(t |
| 148 |
(error "Shell command required!")))) |
| 149 |
(cons 'attr |
| 150 |
(file-attributes file)))) |
| 151 |
|
| 152 |
(defun bg-build-call-prj (project fun &rest args) |
| 153 |
(let* ((file (car project)) |
| 154 |
(directory (file-name-directory file))) |
| 155 |
(with-temp-buffer |
| 156 |
(setq default-directory directory) |
| 157 |
(apply (bg-build-assoc-cdr fun project) args)))) |
| 158 |
|
| 159 |
(defun bg-build-prj-name (project) |
| 160 |
(bg-build-call-prj project 'name)) |
| 161 |
|
| 162 |
(defun bg-build-prj-build? (project saved-files) |
| 163 |
(bg-build-call-prj project 'build? saved-files)) |
| 164 |
|
| 165 |
(defun bg-build-prj-shell (project) |
| 166 |
(bg-build-call-prj project 'shell)) |
| 167 |
|
| 168 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 169 |
;; Active Projects |
| 170 |
|
| 171 |
(defvar bg-build-projects nil) |
| 172 |
|
| 173 |
(defun bg-build-set-projects (projects &optional dont-save) |
| 174 |
(setq bg-build-projects projects) |
| 175 |
(when (and (not dont-save) |
| 176 |
bg-build-projects-auto-load) |
| 177 |
(customize-save-variable |
| 178 |
'bg-build-projects-recent |
| 179 |
(mapcar (function car) projects)))) |
| 180 |
|
| 181 |
(defvar bg-build-add-project-history nil) |
| 182 |
|
| 183 |
(add-to-list 'auto-mode-alist '("\\.bgb$" . emacs-lisp-mode)) |
| 184 |
|
| 185 |
(defun bg-build-add-project (&optional file dont-save) |
| 186 |
"Adds a project file to bg-build minor mode. This basically |
| 187 |
reads and evaluates the first Emacs Lisp expression from specified file. |
| 188 |
The expression should evaluate to a bg-build project object." |
| 189 |
(interactive) |
| 190 |
(cond |
| 191 |
((not file) |
| 192 |
(bg-build-add-project |
| 193 |
(read-file-name |
| 194 |
"Specify bg-build -file: " nil nil t nil) |
| 195 |
dont-save)) |
| 196 |
((not (and (file-readable-p file) |
| 197 |
(file-regular-p file))) |
| 198 |
(error "Specified file is not a regular readable file")) |
| 199 |
(t |
| 200 |
(let* ((file (abbreviate-file-name (file-truename file))) |
| 201 |
(directory (file-name-directory file)) |
| 202 |
(data (with-temp-buffer |
| 203 |
(buffer-disable-undo) |
| 204 |
(insert-file-contents file) |
| 205 |
(setq default-directory directory) |
| 206 |
(goto-char (point-min)) |
| 207 |
(eval `(cl-labels |
| 208 |
((bg-build |
| 209 |
(&rest args) |
| 210 |
(apply (function bg-build-prj) ,file args))) |
| 211 |
,(read (current-buffer))))))) |
| 212 |
(bg-build-set-projects |
| 213 |
(bg-build-replace-in-assoc bg-build-projects file data) |
| 214 |
dont-save)) |
| 215 |
(bg-build-status-update)))) |
| 216 |
|
| 217 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 218 |
;; Running Builds |
| 219 |
|
| 220 |
(defvar bg-build-finished-builds nil) |
| 221 |
|
| 222 |
(defvar bg-build-live-builds nil) |
| 223 |
|
| 224 |
(defun bg-build-interrupt-build (project) |
| 225 |
(let* ((file (car project)) |
| 226 |
(proc (bg-build-assoc-cdr file bg-build-live-builds))) |
| 227 |
(cond |
| 228 |
((and proc (case (process-status proc) |
| 229 |
((run stop) t))) |
| 230 |
;; Ok. We interrupt the build. |
| 231 |
(interrupt-process proc)) |
| 232 |
(proc |
| 233 |
;; Hmm... Shouldn't normally happen. The sentinel is supposed |
| 234 |
;; to remove the build from the live list, so probably something |
| 235 |
;; unexpected occurred in the sentinel. |
| 236 |
(setq bg-build-live-builds |
| 237 |
(bg-build-remove-from-assoc |
| 238 |
bg-build-live-builds |
| 239 |
file)))) |
| 240 |
(bg-build-check-build-queue))) |
| 241 |
|
| 242 |
(defvar bg-build-messages nil) |
| 243 |
|
| 244 |
(defun bg-build-parse-messages () |
| 245 |
(let ((original-display-message |
| 246 |
(when (fboundp 'display-message) |
| 247 |
(symbol-function 'display-message)))) |
| 248 |
(when (fboundp 'display-message) |
| 249 |
(fset 'display-message |
| 250 |
(function |
| 251 |
(lambda (label &rest args) |
| 252 |
(unless (eq label 'progress) |
| 253 |
(apply original-display-message label args)))))) |
| 254 |
(unwind-protect |
| 255 |
(compilation-parse-errors (point-max)) |
| 256 |
(when (fboundp 'display-message) |
| 257 |
(fset 'display-message original-display-message))))) |
| 258 |
|
| 259 |
;; XXX: The following advice depends on the internals of the compilation mode. |
| 260 |
(defadvice next-error (after bg-build-next-error activate) |
| 261 |
(with-current-buffer compilation-last-buffer |
| 262 |
(bg-build-highlight-messages))) |
| 263 |
|
| 264 |
(defadvice compile-goto-error (after bg-build-compile-goto-error activate) |
| 265 |
(with-current-buffer compilation-last-buffer |
| 266 |
(bg-build-highlight-messages))) |
| 267 |
|
| 268 |
(defvar bg-build-highlighting-overlays nil) |
| 269 |
|
| 270 |
(defun bg-build-parse-message (message) |
| 271 |
(cond |
| 272 |
((consp message) |
| 273 |
(let ((message (cdr message))) |
| 274 |
(cond |
| 275 |
((markerp message) |
| 276 |
(let* ((buffer (marker-buffer message)) |
| 277 |
(file (buffer-file-name buffer)) |
| 278 |
(point (marker-position message)) |
| 279 |
(pos (bg-build-point-to-pos point))) |
| 280 |
(list (cons file pos)))) |
| 281 |
((consp message) |
| 282 |
(list |
| 283 |
(cons (caar message) |
| 284 |
(cons (cadr message) |
| 285 |
(1- (or (caddr message) 1))))))))) |
| 286 |
((vectorp message) |
| 287 |
(list (cons (aref message 0) |
| 288 |
(cons (aref message 1) (aref message 2))))))) |
| 289 |
|
| 290 |
(defun bg-build-delete-highlighting-overlays () |
| 291 |
(mapc (function |
| 292 |
(lambda (maybe-overlay) |
| 293 |
(when (overlayp maybe-overlay) |
| 294 |
(delete-overlay maybe-overlay)))) |
| 295 |
bg-build-highlighting-overlays) |
| 296 |
(setq bg-build-highlighting-overlays nil)) |
| 297 |
|
| 298 |
(defun bg-build-highlight-messages () |
| 299 |
(when (and bg-build-messages |
| 300 |
bg-build-message-highlighting) |
| 301 |
(let ((file-to-buffer (bg-build-make-hash-table))) |
| 302 |
(mapc (function |
| 303 |
(lambda (buffer) |
| 304 |
(puthash (buffer-file-name buffer) |
| 305 |
buffer |
| 306 |
file-to-buffer))) |
| 307 |
(buffer-list)) |
| 308 |
(setq bg-build-highlighting-overlays |
| 309 |
(mapcar (function |
| 310 |
(lambda (info-or-overlay) |
| 311 |
(if (overlayp info-or-overlay) |
| 312 |
info-or-overlay |
| 313 |
(let* ((info info-or-overlay) |
| 314 |
(file (car info)) |
| 315 |
(pos (cdr info)) |
| 316 |
(buffer (gethash file file-to-buffer))) |
| 317 |
(if (not buffer) |
| 318 |
info-or-overlay |
| 319 |
(with-current-buffer buffer |
| 320 |
(let* ((begin |
| 321 |
(bg-build-pos-to-point pos)) |
| 322 |
(beyond |
| 323 |
(save-excursion |
| 324 |
(goto-char begin) |
| 325 |
(condition-case () |
| 326 |
(sml-user-forward-sexp) ;; XXX |
| 327 |
(error |
| 328 |
(condition-case () |
| 329 |
(forward-sexp) |
| 330 |
(error |
| 331 |
(condition-case () |
| 332 |
(forward-word 1) |
| 333 |
(error |
| 334 |
)))))) |
| 335 |
(point))) |
| 336 |
(overlay |
| 337 |
(make-overlay begin beyond))) |
| 338 |
(overlay-put |
| 339 |
overlay 'priority |
| 340 |
bg-build-highlighting-overlay-priority) |
| 341 |
(overlay-put |
| 342 |
overlay 'face |
| 343 |
'bg-build-message-sexp-face) |
| 344 |
overlay))))))) |
| 345 |
bg-build-highlighting-overlays))))) |
| 346 |
|
| 347 |
(defun bg-build-process-sentinel (project) |
| 348 |
(let ((project project)) |
| 349 |
(lambda (process event) |
| 350 |
(let ((event (upcase event)) |
| 351 |
(file (car project)) |
| 352 |
(buffer (process-buffer process))) |
| 353 |
(when (buffer-live-p buffer) |
| 354 |
(with-current-buffer buffer |
| 355 |
(compilation-mode) |
| 356 |
(add-hook |
| 357 |
'kill-buffer-hook |
| 358 |
(bg-build-kill-buffer-hook project) |
| 359 |
nil t) |
| 360 |
(setq buffer-read-only nil) |
| 361 |
(let ((point (point))) |
| 362 |
(goto-char (point-max)) |
| 363 |
(insert "\n" event) |
| 364 |
(goto-char point)) |
| 365 |
(setq buffer-read-only t) |
| 366 |
(let ((previous (assoc file bg-build-finished-builds))) |
| 367 |
(when previous |
| 368 |
(kill-buffer (cdr previous)))) |
| 369 |
(push (cons file buffer) |
| 370 |
bg-build-finished-builds) |
| 371 |
(bg-build-parse-messages) |
| 372 |
(set (make-local-variable 'bg-build-messages) |
| 373 |
(or (and (boundp 'compilation-locs) |
| 374 |
(hash-table-p compilation-locs) |
| 375 |
(let ((entries nil)) |
| 376 |
(maphash |
| 377 |
(function |
| 378 |
(lambda (key value) |
| 379 |
(let* ((file (file-truename (caar value))) |
| 380 |
(lines (cddr value))) |
| 381 |
(mapc |
| 382 |
(function |
| 383 |
(lambda (line) |
| 384 |
(let ((locs (cdr line))) |
| 385 |
(mapc |
| 386 |
(function |
| 387 |
(lambda (loc) |
| 388 |
(push (vector |
| 389 |
file |
| 390 |
(or (cadr loc) 0) |
| 391 |
(or (car loc) 0)) |
| 392 |
entries))) |
| 393 |
locs)))) |
| 394 |
lines)))) |
| 395 |
compilation-locs) |
| 396 |
entries)) |
| 397 |
(and (consp compilation-error-list) |
| 398 |
compilation-error-list))) |
| 399 |
(set (make-local-variable 'bg-build-highlighting-overlays) |
| 400 |
(apply |
| 401 |
(function append) |
| 402 |
(mapcar (function bg-build-parse-message) |
| 403 |
bg-build-messages))))) |
| 404 |
(setq bg-build-live-builds |
| 405 |
(bg-build-remove-from-assoc bg-build-live-builds file)) |
| 406 |
(bg-build-check-build-queue) |
| 407 |
(when (buffer-live-p buffer) |
| 408 |
(with-current-buffer buffer |
| 409 |
(bg-build-highlight-messages))) |
| 410 |
(cond |
| 411 |
((string-match "EXITED ABNORMALLY WITH CODE \\([^\n]+\\)\n" event) |
| 412 |
(with-current-buffer buffer |
| 413 |
(condition-case () |
| 414 |
(funcall bg-build-action-on-failure) |
| 415 |
(error |
| 416 |
))) |
| 417 |
(when (memq 'failure bg-build-notify) |
| 418 |
(message "FAILED, %d MESSAGE(S): %s" |
| 419 |
(with-current-buffer buffer |
| 420 |
(length bg-build-messages)) |
| 421 |
(bg-build-prj-name project)))) |
| 422 |
((and (when (buffer-live-p buffer) |
| 423 |
(with-current-buffer buffer |
| 424 |
bg-build-messages)) |
| 425 |
(memq 'messages bg-build-notify) |
| 426 |
(string-match "FINISHED\n" event)) |
| 427 |
(with-current-buffer buffer |
| 428 |
(funcall bg-build-action-on-messages)) |
| 429 |
(message "%d MESSAGE(S): %s" |
| 430 |
(with-current-buffer buffer |
| 431 |
(length bg-build-messages)) |
| 432 |
(bg-build-prj-name project))) |
| 433 |
((and (memq 'success bg-build-notify) |
| 434 |
(string-match "FINISHED\n" event)) |
| 435 |
(message "SUCCEEDED: %s" (bg-build-prj-name project)))))))) |
| 436 |
|
| 437 |
(defun bg-build-kill-buffer-hook (project) |
| 438 |
(let ((project project)) |
| 439 |
(lambda () |
| 440 |
(let ((file (car project))) |
| 441 |
(setq bg-build-finished-builds |
| 442 |
(bg-build-remove-from-assoc bg-build-finished-builds file))) |
| 443 |
(bg-build-delete-highlighting-overlays) |
| 444 |
(bg-build-status-update)))) |
| 445 |
|
| 446 |
(defvar bg-build-counter 0) |
| 447 |
|
| 448 |
(defun bg-build-start-build (project) |
| 449 |
(setq bg-build-counter (1+ bg-build-counter)) |
| 450 |
(let* ((file (car project)) |
| 451 |
(directory (file-name-directory file)) |
| 452 |
(name (format "*%s (bg-build: %d)*" |
| 453 |
(bg-build-prj-name project) |
| 454 |
bg-build-counter)) |
| 455 |
(shell (bg-build-prj-shell project))) |
| 456 |
(when (and name shell) |
| 457 |
(let* ((buffer (generate-new-buffer name)) |
| 458 |
(process (with-current-buffer buffer |
| 459 |
(buffer-disable-undo) |
| 460 |
(add-hook |
| 461 |
'kill-buffer-hook |
| 462 |
(bg-build-kill-buffer-hook project) |
| 463 |
nil t) |
| 464 |
(insert "Compiling \"" file "\":\n\n") |
| 465 |
(setq buffer-read-only t) |
| 466 |
(setq default-directory directory) |
| 467 |
(apply |
| 468 |
(function start-process-shell-command) |
| 469 |
name |
| 470 |
buffer |
| 471 |
shell)))) |
| 472 |
(set-process-sentinel process (bg-build-process-sentinel project)) |
| 473 |
(push (cons file process) |
| 474 |
bg-build-live-builds))))) |
| 475 |
|
| 476 |
(defvar bg-build-build-queue nil) |
| 477 |
|
| 478 |
(defun bg-build-check-build-queue () |
| 479 |
(bg-build-status-update) |
| 480 |
(run-with-idle-timer |
| 481 |
0.01 nil |
| 482 |
(function |
| 483 |
(lambda () |
| 484 |
(when (and bg-build-build-queue |
| 485 |
(or (not bg-build-max-live-builds) |
| 486 |
(< (length bg-build-live-builds) |
| 487 |
bg-build-max-live-builds))) |
| 488 |
(bg-build-start-build (car (last bg-build-build-queue))) |
| 489 |
(setq bg-build-build-queue (butlast bg-build-build-queue)) |
| 490 |
(bg-build-check-build-queue)))))) |
| 491 |
|
| 492 |
(defun bg-build-build-project (project) |
| 493 |
(setq bg-build-build-queue |
| 494 |
(bg-build-cons-once project bg-build-build-queue)) |
| 495 |
(bg-build-interrupt-build project)) |
| 496 |
|
| 497 |
(defun bg-build-switch-to-messages (&optional other-window) |
| 498 |
"Switches to the latest finished build buffer with messages." |
| 499 |
(interactive "P") |
| 500 |
(let ((builds bg-build-finished-builds)) |
| 501 |
(while (and builds |
| 502 |
(not (with-current-buffer (cdar builds) |
| 503 |
bg-build-messages))) |
| 504 |
(pop builds)) |
| 505 |
(if builds |
| 506 |
(let ((buffer (cdar builds))) |
| 507 |
(if other-window |
| 508 |
(switch-to-buffer-other-window buffer) |
| 509 |
(switch-to-buffer buffer))) |
| 510 |
(message "No messages")))) |
| 511 |
|
| 512 |
(defun bg-build-switch-to-live (&optional other-window) |
| 513 |
"Switches to the latest live build buffer." |
| 514 |
(interactive "P") |
| 515 |
(if bg-build-live-builds |
| 516 |
(let ((buffer (process-buffer (cdar bg-build-live-builds)))) |
| 517 |
(if other-window |
| 518 |
(switch-to-buffer-other-window buffer) |
| 519 |
(switch-to-buffer buffer))) |
| 520 |
(message "No live builds"))) |
| 521 |
|
| 522 |
(defun bg-build-switch-to-finished (&optional other-window) |
| 523 |
"Switches to the latest finished build buffer." |
| 524 |
(interactive "P") |
| 525 |
(if bg-build-finished-builds |
| 526 |
(let ((buffer (cdar bg-build-finished-builds))) |
| 527 |
(if other-window |
| 528 |
(switch-to-buffer-other-window buffer) |
| 529 |
(switch-to-buffer buffer))) |
| 530 |
(message "No finished builds"))) |
| 531 |
|
| 532 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 533 |
;; Automatic Build Triggering |
| 534 |
|
| 535 |
(defvar bg-build-saved-files nil) |
| 536 |
|
| 537 |
(defun bg-build-files-saved-timeout () |
| 538 |
(mapc |
| 539 |
(function |
| 540 |
(lambda (project) |
| 541 |
(let ((file (car project)) |
| 542 |
(data (cdr project))) |
| 543 |
(when (bg-build-attr-newer? |
| 544 |
(file-attributes file) |
| 545 |
(bg-build-assoc-cdr 'attr data)) |
| 546 |
(bg-build-add-project file t))))) |
| 547 |
bg-build-projects) |
| 548 |
(let ((saved-files bg-build-saved-files)) |
| 549 |
(setq bg-build-saved-files nil) |
| 550 |
(mapc |
| 551 |
(function |
| 552 |
(lambda (project) |
| 553 |
(when (bg-build-prj-build? project saved-files) |
| 554 |
(bg-build-build-project project)))) |
| 555 |
bg-build-projects))) |
| 556 |
|
| 557 |
(defvar bg-build-timer nil) |
| 558 |
|
| 559 |
(defun bg-build-delete-timer () |
| 560 |
(when bg-build-timer |
| 561 |
(cancel-timer bg-build-timer) |
| 562 |
(setq bg-build-timer nil))) |
| 563 |
|
| 564 |
(defun bg-build-create-timer () |
| 565 |
(bg-build-delete-timer) |
| 566 |
(when bg-build-delay |
| 567 |
(setq bg-build-timer |
| 568 |
(run-with-idle-timer |
| 569 |
bg-build-delay nil (function bg-build-files-saved-timeout))))) |
| 570 |
|
| 571 |
(defun bg-build-after-save-hook () |
| 572 |
(setq bg-build-saved-files |
| 573 |
(bg-build-cons-once |
| 574 |
(abbreviate-file-name (file-truename (buffer-file-name))) |
| 575 |
bg-build-saved-files)) |
| 576 |
(bg-build-create-timer)) |
| 577 |
|
| 578 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 579 |
;; Status Mode |
| 580 |
|
| 581 |
(defconst bg-build-status-buffer-name "<:Bg-Build Status:>") |
| 582 |
|
| 583 |
(defconst bg-build-status-mode-map |
| 584 |
(let ((result (make-sparse-keymap))) |
| 585 |
(mapc (function |
| 586 |
(lambda (key-command) |
| 587 |
(define-key result |
| 588 |
(read (car key-command)) |
| 589 |
(cdr key-command)))) |
| 590 |
`(("[(b)]" . ,(function bury-buffer)) |
| 591 |
("[(q)]" . ,(function bg-build-kill-current-buffer)) |
| 592 |
("[(a)]" . ,(function bg-build-add-project)) |
| 593 |
("[(r)]" . ,(function bg-build-status-rem-project)) |
| 594 |
("[(p)]" . ,(function bg-build-status-visit-project-file)) |
| 595 |
("[(f)]" . ,(function bg-build-status-visit-finished-build)) |
| 596 |
("[(l)]" . ,(function bg-build-status-visit-live-build)) |
| 597 |
("[(return)]" . ,(function bg-build-status-start-build)))) |
| 598 |
result)) |
| 599 |
|
| 600 |
(define-derived-mode bg-build-status-mode fundamental-mode "Bg-Build-Status" |
| 601 |
"Major mode for browsing bg-build related data." |
| 602 |
:group 'bg-build-status) |
| 603 |
|
| 604 |
(defun bg-build-status () |
| 605 |
"Show a buffer with bg-build mode related data." |
| 606 |
(interactive) |
| 607 |
(let ((buffer (get-buffer-create bg-build-status-buffer-name))) |
| 608 |
(with-current-buffer buffer |
| 609 |
(buffer-disable-undo) |
| 610 |
(setq buffer-read-only t) |
| 611 |
(bg-build-status-mode)) |
| 612 |
(switch-to-buffer buffer)) |
| 613 |
(bg-build-status-update)) |
| 614 |
|
| 615 |
(defvar bg-build-status "" |
| 616 |
"Mode line status indicator for BGB mode") |
| 617 |
|
| 618 |
(defun bg-build-status-update () |
| 619 |
(let ((buffer (get-buffer bg-build-status-buffer-name))) |
| 620 |
(when buffer |
| 621 |
(with-current-buffer buffer |
| 622 |
(let ((point (point))) |
| 623 |
(setq buffer-read-only nil) |
| 624 |
(goto-char 1) |
| 625 |
(delete-char (buffer-size)) |
| 626 |
(insert "Status | Project |
| 627 |
-------+------------------------------------------------------------------\n") |
| 628 |
(mapc (function |
| 629 |
(lambda (project) |
| 630 |
(let ((file (car project))) |
| 631 |
(insert |
| 632 |
(let ((n (length (member project bg-build-build-queue)))) |
| 633 |
(if (zerop n) " " (format "%2d" n))) |
| 634 |
(if (assoc file bg-build-live-builds) "L" " ") |
| 635 |
(let ((buffer |
| 636 |
(bg-build-assoc-cdr |
| 637 |
file bg-build-finished-builds))) |
| 638 |
(cond ((and buffer |
| 639 |
(with-current-buffer buffer |
| 640 |
bg-build-messages)) |
| 641 |
"FM") |
| 642 |
(buffer |
| 643 |
"F ") |
| 644 |
(t |
| 645 |
" "))) |
| 646 |
" | " |
| 647 |
(bg-build-prj-name project) " (" file ")" |
| 648 |
"\n")))) |
| 649 |
bg-build-projects) |
| 650 |
(insert "\nTotal of " (number-to-string bg-build-counter) |
| 651 |
" builds started.\n") |
| 652 |
(setq buffer-read-only t) |
| 653 |
(goto-char point))))) |
| 654 |
(setq bg-build-status |
| 655 |
(cl-labels ((fmt (label n) |
| 656 |
(cond ((= n 0) "") |
| 657 |
((= n 1) label) |
| 658 |
(t (format "%s%d" label n))))) |
| 659 |
(let* ((queued (fmt "Q" (length bg-build-build-queue))) |
| 660 |
(live (fmt "L" (length bg-build-live-builds))) |
| 661 |
(messages |
| 662 |
(let ((n (reduce |
| 663 |
(function |
| 664 |
(lambda (n build) |
| 665 |
(with-current-buffer (cdr build) |
| 666 |
(+ n (length bg-build-messages))))) |
| 667 |
bg-build-finished-builds |
| 668 |
:initial-value 0))) |
| 669 |
(if (and (= 0 n) bg-build-finished-builds) |
| 670 |
"F" |
| 671 |
(fmt "M" n)))) |
| 672 |
(str (concat "[" queued live messages "] "))) |
| 673 |
(if (string= str "[] ") |
| 674 |
"" |
| 675 |
str))))) |
| 676 |
|
| 677 |
(defun bg-build-status-the-project () |
| 678 |
(let ((idx (- (bg-build-current-line) 3))) |
| 679 |
(when (and (<= 0 idx) |
| 680 |
(< idx (length bg-build-projects))) |
| 681 |
(nth idx bg-build-projects)))) |
| 682 |
|
| 683 |
(defun bg-build-status-rem-project () |
| 684 |
"Removes the project from bg-build." |
| 685 |
(interactive) |
| 686 |
(let ((project (bg-build-status-the-project))) |
| 687 |
(when project |
| 688 |
(bg-build-set-projects |
| 689 |
(bg-build-remove-from-assoc bg-build-projects (car project))) |
| 690 |
(bg-build-status-update)))) |
| 691 |
|
| 692 |
(defun bg-build-status-visit-project-file () |
| 693 |
"Visits the project file of the project." |
| 694 |
(interactive) |
| 695 |
(let ((project (bg-build-status-the-project))) |
| 696 |
(when project |
| 697 |
(find-file (car project))))) |
| 698 |
|
| 699 |
(defun bg-build-status-visit-finished-build () |
| 700 |
"Visits the buffer of the finished build of the project." |
| 701 |
(interactive) |
| 702 |
(let ((project (bg-build-status-the-project))) |
| 703 |
(when project |
| 704 |
(let ((build (assoc (car project) bg-build-finished-builds))) |
| 705 |
(if build |
| 706 |
(switch-to-buffer (cdr build)) |
| 707 |
(message "That project has no finished builds.")))))) |
| 708 |
|
| 709 |
(defun bg-build-status-visit-live-build () |
| 710 |
"Visits the buffer of the live build of the project." |
| 711 |
(interactive) |
| 712 |
(let ((project (bg-build-status-the-project))) |
| 713 |
(when project |
| 714 |
(let ((build (assoc (car project) bg-build-live-builds))) |
| 715 |
(if build |
| 716 |
(switch-to-buffer (process-buffer (cdr build))) |
| 717 |
(message "That project has no live builds.")))))) |
| 718 |
|
| 719 |
(defun bg-build-status-start-build () |
| 720 |
"Starts a new build of the project." |
| 721 |
(interactive) |
| 722 |
(let ((project (bg-build-status-the-project))) |
| 723 |
(when project |
| 724 |
(bg-build-build-project project)))) |
| 725 |
|
| 726 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 727 |
;; Mode |
| 728 |
|
| 729 |
(defun bg-build-mode-enabled-in-some-buffer () |
| 730 |
(loop for buffer in (buffer-list) do |
| 731 |
(if (with-current-buffer buffer |
| 732 |
bg-build-mode) |
| 733 |
(return t)))) |
| 734 |
|
| 735 |
(defvar bg-build-mode-map (make-sparse-keymap) |
| 736 |
"Keymap for Background-Build mode. This variable is updated by |
| 737 |
`bg-build-build-mode-map'.") |
| 738 |
|
| 739 |
(defun bg-build-build-mode-map () |
| 740 |
(let ((result (make-sparse-keymap))) |
| 741 |
(mapc (function |
| 742 |
(lambda (key-command) |
| 743 |
(define-key result (read (car key-command)) (cdr key-command)))) |
| 744 |
bg-build-key-bindings) |
| 745 |
(setq bg-build-mode-map result)) |
| 746 |
(let ((cons (assoc 'bg-build-mode minor-mode-map-alist))) |
| 747 |
(when cons |
| 748 |
(setcdr cons bg-build-mode-map)))) |
| 749 |
|
| 750 |
(define-minor-mode bg-build-mode |
| 751 |
"Minor mode for performing builds on the background. |
| 752 |
|
| 753 |
\\{bg-build-mode-map} |
| 754 |
" |
| 755 |
:group 'bg-build |
| 756 |
:global t |
| 757 |
(remove-hook |
| 758 |
'after-save-hook (function bg-build-after-save-hook)) |
| 759 |
(when (boundp 'mode-line-modes) |
| 760 |
(setq mode-line-modes |
| 761 |
(remove '(t bg-build-status) mode-line-modes))) |
| 762 |
(when (bg-build-mode-enabled-in-some-buffer) |
| 763 |
(add-hook |
| 764 |
'after-save-hook |
| 765 |
(function bg-build-after-save-hook)) |
| 766 |
(when (boundp 'mode-line-modes) |
| 767 |
(add-to-list 'mode-line-modes '(t bg-build-status))))) |
| 768 |
|
| 769 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 770 |
;; Finalization |
| 771 |
|
| 772 |
(setq bg-build-load-time nil) |
| 773 |
|
| 774 |
(defun bg-build-update () |
| 775 |
"Update data based on customization variables." |
| 776 |
(bg-build-build-mode-map)) |
| 777 |
|
| 778 |
(bg-build-update) |
| 779 |
|
| 780 |
(run-with-idle-timer |
| 781 |
1.0 nil |
| 782 |
(function |
| 783 |
(lambda () |
| 784 |
(when bg-build-projects-auto-load |
| 785 |
(mapc (function |
| 786 |
(lambda (file) |
| 787 |
(when (and (file-readable-p file) |
| 788 |
(file-regular-p file)) |
| 789 |
(bg-build-add-project file t)))) |
| 790 |
bg-build-projects-recent))))) |
| 791 |
|
| 792 |
(provide 'bg-build-mode) |