forked from christophermaier/sqitch-for-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqitch-plan-mode.el
151 lines (122 loc) · 5.37 KB
/
sqitch-plan-mode.el
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
140
141
142
143
144
145
146
147
148
149
150
151
;;; sqitch-plan-mode.el --- Major mode for interacting with Sqitch plan files
;; Copyright (C) 2014 Christopher Maier
;; Author: Christopher Maier <[email protected]>
;; Maintainer: Christopher Maier <[email protected]>
;; Version: 0.0.1
;; Keywords: sql, sqitch
;; URL: https://github.com/christophermaier/sqitch-for-emacs
;; This file is NOT part of GNU Emacs.
;;; License:
;; 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, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Provides basic navigation across files of a Sqitch project from a
;; 'sqitch.plan' file. See http://www.sqitch.org for details.
;;
;; To use this file, make sure that the following code (or its
;; equivalent) is in your Emacs configuration:
;;
;; (add-to-list 'load-path "/path/to/sqitch-for-emacs")
;; (require 'sqitch-plan-mode)
;;
;; Files named 'sqitch.plan' are automatically opened in this mode.
;;; Code:
(require 'derived)
(require 'thingatpt)
(defun sqitch-plan-maybe-changeset-at-point ()
"Crude way of (maybe) getting the changeset at point. Not at
all guaranteed to actually *be* a changeset, absent a more robust
implementation."
(thing-at-point 'symbol))
(defun sqitch-plan-find-script (script-type)
"Opens the SCRIPT-TYPE Sqitch script for the changeset
described by the current line of a Sqitch plan file.
SCRIPT-TYPE should be one of \"deploy\", \"verify\",
\"revert\" or \"test\"."
(let ((changeset (sqitch-plan-maybe-changeset-at-point)))
(if changeset
(let* ((script-dir (concat (file-name-directory (buffer-file-name)) script-type))
(script (concat changeset ".sql"))
(script-file (concat (file-name-as-directory script-dir)
script)))
(if (file-exists-p script-file)
(find-file script-file)
(message "'%s' is not a sqitch changeset" changeset))))))
(defun sqitch-plan-find-deploy-script ()
"Open the deploy script for the changeset described by the
current line of a Sqitch plan file."
(interactive)
(sqitch-plan-find-script "deploy"))
(defun sqitch-plan-find-verify-script ()
"Open the verify script for the changeset described by the
current line of a Sqitch plan file."
(interactive)
(sqitch-plan-find-script "verify"))
(defun sqitch-plan-find-revert-script ()
"Open the revert script for the changeset described by the
current line of a Sqitch plan file."
(interactive)
(sqitch-plan-find-script "revert"))
(defun sqitch-plan-find-test-script ()
"Open the test script for the changeset described by the
current line of a Sqitch plan file."
(interactive)
(sqitch-plan-find-script "test"))
(defvar sqitch-plan-mode-map nil "sqitch-plan-mode keymap")
(if sqitch-plan-mode-map
nil
(setq sqitch-plan-mode-map (make-sparse-keymap))
(define-key sqitch-plan-mode-map (kbd "C-c C-d") 'sqitch-plan-find-deploy-script)
(define-key sqitch-plan-mode-map (kbd "C-c C-v") 'sqitch-plan-find-verify-script)
(define-key sqitch-plan-mode-map (kbd "C-c C-r") 'sqitch-plan-find-revert-script)
(define-key sqitch-plan-mode-map (kbd "C-c C-t") 'sqitch-plan-find-test-script))
(defvar sqitch-plan-highligts nil "first element for `font-lock-defaults'")
(setq sqitch-plan-highligts
'(
;; pragmas
("^[[:space:]]*\\(%\\)[[:space:]]*\\([^=]+\\)[[:space:]]*\\(=\\)[[:space:]]*\\([^\n]+\\)[[:space:]]*$" .
((1 font-lock-type-face)
(2 font-lock-variable-name-face)
(3 font-lock-type-face)
(4 font-lock-string-face)
))
;; timestamp
("[[:space:]]+\\([[:digit:]]\\{4\\}-[[:digit:]]\\{2\\}-[[:digit:]]\\{2\\}T[[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}Z\\)[[:space:]]+" .
(1 font-lock-builtin-face))
;; requirements
("[[:space:]]+\\(\\[[^]]+\\][[:space:]]+\\)" . (1 font-lock-keyword-face))
;; tags
("^[[:space:]]*\\(@[^ ]+\\)" . (1 font-lock-warning-face))
;; changes
("^[[:space:]]*\\([^%@][^ ]+\\)" . (1 font-lock-constant-face))
;; author
("Z[[:space:]]+\\([^>]+>\\)" . (1 font-lock-string-face))
))
(defvar sqitch-plan-mode-syntax-table nil "Syntax table for `sqitch-plan-mode'")
(setq sqitch-plan-mode-syntax-table
(let ((synTable (make-syntax-table)))
;; perl style comment: "# …"
(modify-syntax-entry ?# "<" synTable)
(modify-syntax-entry ?\n ">" synTable)
synTable))
;;;###autoload
(define-derived-mode sqitch-plan-mode text-mode "sqitch-plan"
"Major mode for interacting with Sqitch plan files.
\\{sqitch-plan-mode-map}"
(setq font-lock-defaults '(sqitch-plan-highligts)))
;;;###autoload
(add-to-list 'auto-mode-alist '("sqitch\.plan$" . sqitch-plan-mode))
(provide 'sqitch-plan-mode)
;;; sqitch-plan-mode.el ends here