diff options
author | Alex Bennée <alex.bennee@linaro.org> | 2016-04-28 16:01:17 +0100 |
---|---|---|
committer | Alex Bennée <alex.bennee@linaro.org> | 2016-04-28 16:01:17 +0100 |
commit | 6db7c7e7715007fb7e7ce683763877474f39d5c4 (patch) | |
tree | 395f7abf372e648fecdea48dc7efea544792f191 | |
parent | 129a9083ed3fb0b1ef692cb96cdce9bd094c2cef (diff) |
lava-mode: move submit job logic into common code
Plus a little clean-up.
-rw-r--r-- | lava-json-mode.el | 55 | ||||
-rw-r--r-- | lava-mode.el | 98 | ||||
-rw-r--r-- | lava-yaml-mode.el | 2 |
3 files changed, 100 insertions, 55 deletions
diff --git a/lava-json-mode.el b/lava-json-mode.el index 82c099d..dc4156f 100644 --- a/lava-json-mode.el +++ b/lava-json-mode.el @@ -33,6 +33,7 @@ ;(setq edebug-all-defs t) (require 'json-mode) +(require 'lava-mode) (require 'lava-rpc) (require 'lava-job-list-mode) (require 'dash) @@ -43,10 +44,6 @@ "\"parameters\"" "\"image\"" "\"priority\"" "\"testdef_urls\"") "LAVA specific keywords.") -(defvar lava-mode-elisp-escape-regex - "`\\(.*\\)`" - "Regexp that describes the escape syntax for embedded elisp.") - (defvar lava-font-lock-defaults (list ;; LAVA Specific Keywords @@ -134,56 +131,6 @@ bits are correctly tagged.") (set (make-local-variable 'font-lock-fontify-region-function) #'lava-mode-set-syntax-then-fontify))) -(defun lava-mode-submit-job (arg) - "Submit a the current buffer JSON job to LAVA. - -When called with the prefix `arg' keep results of the escaped elisp. -If the prefix is specified twice don't send the json at all." - (interactive "P") - (let ((expanded-buffer (lava-mode-expand-buffer)) - (response)) - (unless (and (number-or-marker-p arg) (> arg 4)) - (with-current-buffer expanded-buffer - (setq response - (lava-rpc-submit-json-job - (buffer-substring-no-properties - (point-min) (point-max)))))) - (if arg - (switch-to-buffer expanded-buffer) - (kill-buffer expanded-buffer)) - response)) - -(defun lava-mode-expand-buffer () - "Expand the current buffer into a new one, expanding any embedded -lisp." - (let* ((new-name (concat "*Expanded " (buffer-name) "*")) - (new-buffer (get-buffer-create new-name))) - (save-excursion - (goto-char (point-min)) - (let ((last-pos (point))) - (while (re-search-forward lava-mode-elisp-escape-regex nil t) - (goto-char (match-beginning 1)) - ;; copy-to here verbatim - (let ((last-chunk - (buffer-substring - last-pos - (match-beginning 0))) - (new-str - (save-match-data - (let ((expr (read (current-buffer)))) - (set 'new-str (eval expr)))))) - (with-current-buffer new-buffer - (insert last-chunk) - (insert new-str))) - (set 'last-pos (match-end 0))) - (let ((last-chunk (buffer-substring last-pos (point-max)))) - (with-current-buffer new-buffer - (insert last-chunk))))) - (with-current-buffer new-buffer - (lava-mode) - (not-modified)) - new-buffer)) - (defun lava-mode--update-device-list (list) "Update the device list" (setq lava-mode-device-list diff --git a/lava-mode.el b/lava-mode.el new file mode 100644 index 0000000..c77f962 --- /dev/null +++ b/lava-mode.el @@ -0,0 +1,98 @@ +;;; lava-mode --- common lava mode helpers +;; +;; Copyright (C) 2014 Alex Bennée + +;; Author: Alex Bennée <alex.bennee@linaro.org> +;; Maintainer: Alex Bennée <alex.bennee@linaro.org> +;; Version: 0.1 +;; Homepage: + +;; This file is not part of GNU Emacs. + +;; This file 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, or (at your option) +;; any later version. + +;; This file 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/>. +;; +;;; Commentary: +;; +;; These are common helper functions shared between lava-json-mode and +;; lava-yaml-mode. +;; +;;; Code: + +;; Require prerequisites + +;; Variables + +(defvar lava-mode-elisp-escape-regex + "`\\(.*\\)`" + "Regexp that describes the escape syntax for embedded elisp.") + +(defun lava-mode-expand-buffer (&optional orig-buff) + "Expand `ORIG-BUFF' into a new one, evaluating embedded Lisp. + +Any Lisp functions are evaluated and replaced with the results of that +evaluation." + (unless orig-buff + (setq orig-buff (current-buffer))) + + (let* ((new-name (concat "*Expanded " (buffer-name orig-buff) "*")) + (new-buffer (get-buffer-create new-name)) + (new-mode (with-current-buffer orig-buff major-mode))) + + (with-current-buffer orig-buff + (goto-char (point-min)) + (let ((last-pos (point))) + (while (re-search-forward lava-mode-elisp-escape-regex nil t) + (goto-char (match-beginning 1)) + ;; copy-to here verbatim + (let ((last-chunk + (buffer-substring + last-pos + (match-beginning 0))) + (new-str + (save-match-data + (let ((expr (read (current-buffer)))) + (eval expr))))) + (with-current-buffer new-buffer + (insert last-chunk) + (insert new-str))) + (set 'last-pos (match-end 0))) + (let ((last-chunk (buffer-substring last-pos (point-max)))) + (with-current-buffer new-buffer + (insert last-chunk))))) + (with-current-buffer new-buffer + (funcall new-mode) + (not-modified)) + new-buffer)) + +(defun lava-mode-submit-job (&optional arg) + "Submit the current buffer to LAVA. + +When called with the prefix `arg' keep results of the escaped elisp. +If the prefix is specified twice don't submit the job at all." + (interactive "P") + (let ((expanded-buffer (lava-mode-expand-buffer)) + (response)) + (unless (and (number-or-marker-p arg) (> arg 4)) + (with-current-buffer expanded-buffer + (setq response + (lava-rpc-submit-job + (buffer-substring-no-properties + (point-min) (point-max)))))) + (if arg + (switch-to-buffer expanded-buffer) + (kill-buffer expanded-buffer)) + response)) + +(provide 'lava-mode) +;;; lava-mode.el ends here diff --git a/lava-yaml-mode.el b/lava-yaml-mode.el index b61d81d..e8a8707 100644 --- a/lava-yaml-mode.el +++ b/lava-yaml-mode.el @@ -38,7 +38,7 @@ ;;; Mode magic (defvar lava-yaml-mode-map (let ((map (make-sparse-keymap))) - (define-key map (kbd "C-c C-c") 'lava-yaml-mode-done) + (define-key map (kbd "C-c C-c") 'lava-mode-submit-job) map) "Keymap for major mode `lava-yaml-mode'.") |