Skip to content

Commit

Permalink
Rework setting local vars to be more robust using global minor mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mkcms committed Jun 3, 2024
1 parent 6895599 commit cdea832
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions compiler-explorer.el
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ contents are replaced destructively and point is not preserved."
(unless (eq major-mode 'compilation-mode)
(compilation-mode)
(setq buffer-undo-list t))
(compiler-explorer-mode +1)

(when compiler-explorer--project-dir
(setq-local default-directory compiler-explorer--project-dir)
(setq-local compilation-parse-errors-filename-function
Expand All @@ -545,11 +545,9 @@ contents are replaced destructively and point is not preserved."
This will write the list of supported compilers in the execution
output buffer."
(with-current-buffer (get-buffer-create compiler-explorer--exe-output-buffer)
(compiler-explorer-mode +1)
(setq buffer-read-only t)
(setq buffer-undo-list t)
(setq header-line-format
`(:eval (compiler-explorer--header-line-format-executor)))

(let ((buffer-read-only nil)
(keymap (make-keymap))
(compiler (plist-get compiler-explorer--compiler-data :name))
Expand Down Expand Up @@ -581,11 +579,9 @@ output buffer."
(pcase-let (((map :stdout :stderr :code) response))
(with-current-buffer
(get-buffer-create compiler-explorer--exe-output-buffer)
(compiler-explorer-mode +1)
(setq buffer-read-only t)
(setq buffer-undo-list t)
(setq header-line-format
`(:eval (compiler-explorer--header-line-format-executor)))

(let ((buffer-read-only nil)
(buf (current-buffer)))
(with-temp-buffer
Expand Down Expand Up @@ -777,6 +773,7 @@ output buffer."
(restore-buffer-modified-p nil))

(defvar compiler-explorer--last-session)
(defvar compiler-explorer-mode)

(defun compiler-explorer--cleanup (&optional skip-save-session)
"Kill current session.
Expand Down Expand Up @@ -837,7 +834,10 @@ If SKIP-SAVE-SESSION is non-nil, don't attempt to save the last session."
(when compiler-explorer--project-dir
(with-demoted-errors "compiler-explorer--cleanup: delete-directory: %s"
(delete-directory compiler-explorer--project-dir t)))
(setq compiler-explorer--project-dir nil))
(setq compiler-explorer--project-dir nil)

(when compiler-explorer-mode
(compiler-explorer-mode -1)))


;; Source<->ASM overlays
Expand Down Expand Up @@ -1110,20 +1110,50 @@ It must have been created with `compiler-explorer--current-session'."
(defvar compiler-explorer-mode-map (make-sparse-keymap)
"Keymap used in `compiler-explorer-mode'.")

(define-minor-mode compiler-explorer-mode
(define-minor-mode compiler-explorer--local-mode
"Minor mode used in compiler explorer buffers."
:interactive nil
:lighter " CE"
:keymap compiler-explorer-mode-map
(cond
(compiler-explorer-mode
(compiler-explorer--local-mode
(add-hook 'kill-buffer-hook #'compiler-explorer--cleanup nil t)
(add-hook 'project-find-functions
#'compiler-explorer--project-find-function nil t))
#'compiler-explorer--project-find-function nil t)

(cond
((eq (current-buffer) (get-buffer compiler-explorer--buffer))
(setq header-line-format
`(:eval (compiler-explorer--header-line-format-source)))
(add-hook 'after-change-functions
#'compiler-explorer--after-change nil t))
((eq (current-buffer) (get-buffer compiler-explorer--compiler-buffer))
(setq header-line-format
`(:eval (compiler-explorer--header-line-format-compiler))))
((eq (current-buffer) (get-buffer compiler-explorer--exe-output-buffer))
(setq header-line-format
`(:eval (compiler-explorer--header-line-format-executor))))))
(t
(remove-hook 'kill-buffer-hook #'compiler-explorer--cleanup t)
(remove-hook 'project-find-functions
#'compiler-explorer--project-find-function t))))

(defun compiler-explorer--local-mode-maybe-enable ()
"Enable `compiler-explorer--local-mode' if required."
(when (member (current-buffer)
(list
(get-buffer compiler-explorer--buffer)
(get-buffer compiler-explorer--compiler-buffer)
(get-buffer compiler-explorer--output-buffer)
(get-buffer compiler-explorer--exe-output-buffer)))
(compiler-explorer--local-mode +1)))

(define-globalized-minor-mode compiler-explorer-mode
compiler-explorer--local-mode
compiler-explorer--local-mode-maybe-enable
(unless compiler-explorer-mode
(compiler-explorer--cleanup)))

(defun compiler-explorer--define-menu ()
"Define a menu in the menu bar for `compiler-explorer' commands."
(easy-menu-define compiler-explorer-menu
Expand Down Expand Up @@ -1416,12 +1446,8 @@ execution."
(with-current-buffer (get-buffer-create compiler-explorer--compiler-buffer)
(asm-mode)

(compiler-explorer-mode +1)

(setq buffer-read-only t)
(setq buffer-undo-list t)
(setq header-line-format
`(:eval (compiler-explorer--header-line-format-compiler)))

(compiler-explorer--request-async)

Expand Down Expand Up @@ -1602,7 +1628,7 @@ LAYOUT must be as described in `compiler-explorer-layouts'."
(setq layout (% layout (length compiler-explorer-layouts)))
(setq compiler-explorer--last-layout layout))
(when (window-dedicated-p)
(unless compiler-explorer-mode
(unless compiler-explorer--local-mode
(select-window (split-window-horizontally)))
(set-window-dedicated-p (selected-window) nil))
(delete-other-windows)
Expand Down Expand Up @@ -1686,6 +1712,7 @@ The source buffer is current when this hook runs.")
This is a subr of `compiler-explorer-new-session' that uses given
LANG, COMPILER, INTERACTIVE."
(compiler-explorer--cleanup)
(compiler-explorer-mode +1)
(when-let ((session compiler-explorer--last-session))
(ring-insert compiler-explorer--session-ring session)
(setq compiler-explorer--last-session nil))
Expand Down Expand Up @@ -1719,9 +1746,6 @@ LANG, COMPILER, INTERACTIVE."
(call-interactively #'compiler-explorer-set-compiler)
(signal (car err) (cdr err))))))

(setq header-line-format
`(:eval (compiler-explorer--header-line-format-source)))

(when compiler-explorer-make-temp-file
(setq compiler-explorer--project-dir
(make-temp-file "compiler-explorer" 'dir))
Expand All @@ -1733,9 +1757,6 @@ LANG, COMPILER, INTERACTIVE."

(compiler-explorer--define-menu)

(compiler-explorer-mode +1)
(add-hook 'after-change-functions
#'compiler-explorer--after-change nil t)
(add-hook 'kill-emacs-hook #'compiler-explorer--save-sessions)

(pop-to-buffer (current-buffer))
Expand Down

0 comments on commit cdea832

Please sign in to comment.