guix-build-log.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. ;;; guix-build-log.el --- Major and minor modes for build logs -*- lexical-binding: t -*-
  2. ;; Copyright © 2015 Alex Kost <alezost@gmail.com>
  3. ;; Copyright © 2020 Daniel Nagy
  4. ;; This file is part of Emacs-Guix.
  5. ;; Emacs-Guix is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;;
  10. ;; Emacs-Guix is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;;
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with Emacs-Guix. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This file provides a major mode (`guix-build-log-mode') and a minor mode
  19. ;; (`guix-build-log-minor-mode') for highlighting Guix build logs.
  20. ;;; Code:
  21. (require 'guix nil t)
  22. (require 'guix-utils)
  23. (defgroup guix-build-log nil
  24. "Settings for `guix-build-log-mode'."
  25. :group 'guix)
  26. (defgroup guix-build-log-faces nil
  27. "Faces for `guix-build-log-mode'."
  28. :group 'guix-build-log
  29. :group 'guix-faces)
  30. (defface guix-build-log-title-head
  31. '((t :inherit font-lock-keyword-face))
  32. "Face for '@' symbol of a log title."
  33. :group 'guix-build-log-faces)
  34. (defface guix-build-log-title-start
  35. '((t :inherit guix-build-log-title-head))
  36. "Face for a log title denoting a start of a process."
  37. :group 'guix-build-log-faces)
  38. (defface guix-build-log-title-success
  39. '((t :inherit guix-build-log-title-head))
  40. "Face for a log title denoting a successful end of a process."
  41. :group 'guix-build-log-faces)
  42. (defface guix-build-log-title-fail
  43. '((t :inherit error))
  44. "Face for a log title denoting a failed end of a process."
  45. :group 'guix-build-log-faces)
  46. (defface guix-build-log-title-end
  47. '((t :inherit guix-build-log-title-head))
  48. "Face for a log title denoting an undefined end of a process."
  49. :group 'guix-build-log-faces)
  50. (defface guix-build-log-phase-name
  51. '((t :inherit font-lock-function-name-face))
  52. "Face for a phase name."
  53. :group 'guix-build-log-faces)
  54. (defface guix-build-log-phase-start
  55. '((default :weight bold)
  56. (((class grayscale) (background light)) :foreground "Gray90")
  57. (((class grayscale) (background dark)) :foreground "DimGray")
  58. (((class color) (min-colors 16) (background light))
  59. :foreground "DarkGreen")
  60. (((class color) (min-colors 16) (background dark))
  61. :foreground "LimeGreen")
  62. (((class color) (min-colors 8)) :foreground "green"))
  63. "Face for the start line of a phase."
  64. :group 'guix-build-log-faces)
  65. (defface guix-build-log-phase-end
  66. '((((class grayscale) (background light)) :foreground "Gray90")
  67. (((class grayscale) (background dark)) :foreground "DimGray")
  68. (((class color) (min-colors 16) (background light))
  69. :foreground "ForestGreen")
  70. (((class color) (min-colors 16) (background dark))
  71. :foreground "LightGreen")
  72. (((class color) (min-colors 8)) :foreground "green")
  73. (t :weight bold))
  74. "Face for the end line of a phase."
  75. :group 'guix-build-log-faces)
  76. (defface guix-build-log-phase-success
  77. '((t))
  78. "Face for the 'succeeded' word of a phase line."
  79. :group 'guix-build-log-faces)
  80. (defface guix-build-log-phase-fail
  81. '((t :inherit error))
  82. "Face for the 'failed' word of a phase line."
  83. :group 'guix-build-log-faces)
  84. (defface guix-build-log-phase-seconds
  85. '((t :inherit font-lock-constant-face))
  86. "Face for the number of seconds for a phase."
  87. :group 'guix-build-log-faces)
  88. (defcustom guix-build-log-mode-hook '()
  89. "Hook run after `guix-build-log-mode' is entered."
  90. :type 'hook
  91. :group 'guix-build-log)
  92. (defvar guix-build-log-phase-name-regexp "`\\([^']+\\)'"
  93. "Regexp for a phase name.")
  94. (defvar guix-build-log-phase-start-regexp
  95. (concat "^starting phase " guix-build-log-phase-name-regexp)
  96. "Regexp for the start line of a 'build' phase.")
  97. (defvar guix-build-log-imenu-generic-expression
  98. `((nil ,guix-build-log-phase-start-regexp 1))
  99. "Imenu generic expression for `guix-build-log-mode'.")
  100. (defun guix-build-log-title-regexp (&optional state)
  101. "Return regexp for the log title.
  102. STATE is a symbol denoting a state of the title. It should be
  103. `start', `fail', `success' or `nil' (for a regexp matching any
  104. state)."
  105. (let* ((word-rx (rx (1+ (any word "-"))))
  106. (state-rx (cond ((eq state 'start) (concat word-rx "started"))
  107. ((eq state 'success) (concat word-rx "succeeded"))
  108. ((eq state 'fail) (concat word-rx "failed"))
  109. (t word-rx))))
  110. (rx-to-string
  111. `(and bol (group "@") " " (group (regexp ,state-rx)))
  112. t)))
  113. (defun guix-build-log-phase-end-regexp (&optional state)
  114. "Return regexp for the end line of a 'build' phase.
  115. STATE is a symbol denoting how a build phase was ended. It should be
  116. `fail', `success' or `nil' (for a regexp matching any state)."
  117. (let ((state-rx (cond ((eq state 'success) "succeeded")
  118. ((eq state 'fail) "failed")
  119. (t (regexp-opt '("succeeded" "failed"))))))
  120. (rx-to-string
  121. `(and bol "phase " (regexp ,guix-build-log-phase-name-regexp)
  122. " " (group (regexp ,state-rx)) " after "
  123. (group (1+ (or digit "."))) " seconds")
  124. t)))
  125. (defvar guix-build-log-phase-end-regexp
  126. ;; For efficiency, it is better to have a regexp for the general line
  127. ;; of the phase end, then to call the function all the time.
  128. (guix-build-log-phase-end-regexp)
  129. "Regexp for the end line of a 'build' phase.")
  130. (defvar guix-build-log-font-lock-keywords
  131. `((,(guix-build-log-title-regexp 'start)
  132. (1 'guix-build-log-title-head)
  133. (2 'guix-build-log-title-start))
  134. (,(guix-build-log-title-regexp 'success)
  135. (1 'guix-build-log-title-head)
  136. (2 'guix-build-log-title-success))
  137. (,(guix-build-log-title-regexp 'fail)
  138. (1 'guix-build-log-title-head)
  139. (2 'guix-build-log-title-fail))
  140. (,(guix-build-log-title-regexp)
  141. (1 'guix-build-log-title-head)
  142. (2 'guix-build-log-title-end))
  143. (,guix-build-log-phase-start-regexp
  144. (0 'guix-build-log-phase-start)
  145. (1 'guix-build-log-phase-name prepend))
  146. (,(guix-build-log-phase-end-regexp 'success)
  147. (0 'guix-build-log-phase-end)
  148. (1 'guix-build-log-phase-name prepend)
  149. (2 'guix-build-log-phase-success prepend)
  150. (3 'guix-build-log-phase-seconds prepend))
  151. (,(guix-build-log-phase-end-regexp 'fail)
  152. (0 'guix-build-log-phase-end)
  153. (1 'guix-build-log-phase-name prepend)
  154. (2 'guix-build-log-phase-fail prepend)
  155. (3 'guix-build-log-phase-seconds prepend)))
  156. "A list of `font-lock-keywords' for `guix-build-log-mode'.")
  157. (defvar guix-build-log-common-map
  158. (let ((map (make-sparse-keymap)))
  159. (define-key map (kbd "M-n") 'guix-build-log-next-phase)
  160. (define-key map (kbd "M-p") 'guix-build-log-previous-phase)
  161. (define-key map (kbd "TAB") 'guix-build-log-phase-toggle)
  162. (define-key map (kbd "<tab>") 'guix-build-log-phase-toggle)
  163. (define-key map (kbd "<backtab>") 'guix-build-log-phase-toggle-all)
  164. (define-key map [(shift tab)] 'guix-build-log-phase-toggle-all)
  165. map)
  166. "Parent keymap for 'build-log' buffers.
  167. For `guix-build-log-mode' this map is used as is.
  168. For `guix-build-log-minor-mode' this map is prefixed with 'C-c'.")
  169. (defvar guix-build-log-mode-map
  170. (let ((map (make-sparse-keymap)))
  171. (set-keymap-parent
  172. map (make-composed-keymap (list guix-build-log-common-map)
  173. special-mode-map))
  174. (define-key map (kbd "c") 'compilation-shell-minor-mode)
  175. (define-key map (kbd "v") 'view-mode)
  176. map)
  177. "Keymap for `guix-build-log-mode' buffers.")
  178. (defvar guix-build-log-minor-mode-map
  179. (let ((map (make-sparse-keymap)))
  180. (define-key map (kbd "C-c") guix-build-log-common-map)
  181. map)
  182. "Keymap for `guix-build-log-minor-mode' buffers.")
  183. (defun guix-build-log-phase-start (&optional with-header?)
  184. "Return the start point of the current build phase.
  185. If WITH-HEADER? is non-nil, do not skip 'starting phase ...' header.
  186. Return nil, if there is no phase start before the current point."
  187. (save-excursion
  188. (end-of-line)
  189. (when (re-search-backward guix-build-log-phase-start-regexp nil t)
  190. (unless with-header? (end-of-line))
  191. (point))))
  192. (defun guix-build-log-phase-end ()
  193. "Return the end point of the current build phase."
  194. (save-excursion
  195. (beginning-of-line)
  196. (when (re-search-forward guix-build-log-phase-end-regexp nil t)
  197. (point))))
  198. (defun guix-build-log-phase-hide ()
  199. "Hide the body of the current build phase."
  200. (interactive)
  201. (let ((beg (guix-build-log-phase-start))
  202. (end (guix-build-log-phase-end)))
  203. (when (and beg end)
  204. ;; If not on the header line, move to it.
  205. (when (and (> (point) beg)
  206. (< (point) end))
  207. (goto-char (guix-build-log-phase-start t)))
  208. (remove-overlays beg end 'invisible t)
  209. (let ((o (make-overlay beg end)))
  210. (overlay-put o 'evaporate t)
  211. (overlay-put o 'invisible t)))))
  212. (defun guix-build-log-phase-show ()
  213. "Show the body of the current build phase."
  214. (interactive)
  215. (let ((beg (guix-build-log-phase-start))
  216. (end (guix-build-log-phase-end)))
  217. (when (and beg end)
  218. (remove-overlays beg end 'invisible t))))
  219. (defun guix-build-log-phase-hidden-p ()
  220. "Return non-nil, if the body of the current build phase is hidden."
  221. (let ((beg (guix-build-log-phase-start)))
  222. (and beg
  223. (cl-some (lambda (o)
  224. (overlay-get o 'invisible))
  225. (overlays-at beg)))))
  226. (defun guix-build-log-phase-toggle-function ()
  227. "Return a function to toggle the body of the current build phase."
  228. (if (guix-build-log-phase-hidden-p)
  229. #'guix-build-log-phase-show
  230. #'guix-build-log-phase-hide))
  231. (defun guix-build-log-phase-toggle ()
  232. "Show/hide the body of the current build phase."
  233. (interactive)
  234. (funcall (guix-build-log-phase-toggle-function)))
  235. (defun guix-build-log-phase-toggle-all ()
  236. "Show/hide the bodies of all build phases."
  237. (interactive)
  238. (save-excursion
  239. ;; Some phases may be hidden, and some shown. Whether to hide or to
  240. ;; show them, it is determined by the state of the first phase here.
  241. (goto-char (point-min))
  242. (let ((fun (save-excursion
  243. (re-search-forward guix-build-log-phase-start-regexp nil t)
  244. (guix-build-log-phase-toggle-function))))
  245. (while (re-search-forward guix-build-log-phase-start-regexp nil t)
  246. (funcall fun)))))
  247. (defun guix-build-log-next-phase (&optional arg)
  248. "Move to the next build phase.
  249. With ARG, do it that many times. Negative ARG means move
  250. backward."
  251. (interactive "^p")
  252. (if arg
  253. (when (= 0 arg) (user-error "Try again"))
  254. (setq arg 1))
  255. (let ((search-fun (if (> arg 0)
  256. #'re-search-forward
  257. #'re-search-backward))
  258. (n (abs arg))
  259. found last-found)
  260. (save-excursion
  261. (end-of-line (if (> arg 0) 1 0)) ; skip the current line
  262. (while (and (/= 0 n)
  263. (setq found
  264. (funcall search-fun
  265. guix-build-log-phase-start-regexp
  266. nil t)))
  267. (setq n (1- n)
  268. last-found found)))
  269. (when last-found
  270. (goto-char last-found)
  271. (forward-line 0))
  272. (or found
  273. (user-error (if (> arg 0)
  274. "No next build phase"
  275. "No previous build phase")))))
  276. (defun guix-build-log-previous-phase (&optional arg)
  277. "Move to the previous build phase.
  278. With ARG, do it that many times. Negative ARG means move
  279. forward."
  280. (interactive "^p")
  281. (guix-build-log-next-phase (- (or arg 1))))
  282. ;;;###autoload
  283. (define-derived-mode guix-build-log-mode special-mode
  284. "Guix-Build-Log"
  285. "Major mode for viewing Guix build logs.
  286. \\{guix-build-log-mode-map}"
  287. (setq
  288. font-lock-defaults '(guix-build-log-font-lock-keywords t)
  289. imenu-generic-expression guix-build-log-imenu-generic-expression))
  290. ;;;###autoload
  291. (define-minor-mode guix-build-log-minor-mode
  292. "Toggle Guix Build Log minor mode.
  293. With a prefix argument ARG, enable Guix Build Log minor mode if
  294. ARG is positive, and disable it otherwise. If called from Lisp,
  295. enable the mode if ARG is omitted or nil.
  296. When Guix Build Log minor mode is enabled, it highlights build
  297. log in the current buffer. This mode can be enabled
  298. programmatically using hooks, like this:
  299. (add-hook 'shell-mode-hook 'guix-build-log-minor-mode)
  300. \\{guix-build-log-minor-mode-map}"
  301. :init-value nil
  302. :lighter " Guix-Build-Log"
  303. :keymap guix-build-log-minor-mode-map
  304. :group 'guix-build-log
  305. (if guix-build-log-minor-mode
  306. (font-lock-add-keywords nil guix-build-log-font-lock-keywords)
  307. (font-lock-remove-keywords nil guix-build-log-font-lock-keywords))
  308. (guix-font-lock-flush))
  309. (defun guix-build-log-find-file (file-or-url)
  310. "Open FILE-OR-URL in `guix-build-log-mode'."
  311. (guix-find-file-or-url file-or-url)
  312. (guix-build-log-mode))
  313. (provide 'guix-build-log)
  314. ;;; guix-build-log.el ends here