Moved on_close commit handler to its own function.
[mp-5.x.git] / mp_vcs.mpsl
blobf7bc22332842af930a65aed1f699fd02a727a352
1 /*
3     Minimum Profit 5.x
4     A Programmer's Text Editor
6     Version Control System support
8     Copyright (C) 1991-2012 Angel Ortega <angel@triptico.com>
10     This program is free software; you can redistribute it and/or
11     modify it under the terms of the GNU General Public License
12     as published by the Free Software Foundation; either version 2
13     of the License, or (at your option) any later version.
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
20     You should have received a copy of the GNU General Public License
21     along with this program; if not, write to the Free Software
22     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24     http://www.triptico.com
28 /** data **/
30 mp.vcs = [
31     {
32         id:     'git',
33         check:  [ '.git', '../.git', '../../.git' ],
34         opts:   {
35             'git diff'          => NULL,
36             'git status'        => NULL,
37             'git log'           => NULL,
38             "git diff '%s'"     => NULL,
39             "git log '%s'"      => NULL,
40             "git add '%s'"      => NULL,
41             "git blame '%s'"    => NULL,
42             "git checkout '%s'" => NULL,
43             "git stash"         => NULL,
44             "git stash apply"   => NULL,
45             "git push"          => NULL,
46             "git pull"          => NULL,
47             "git commit -a"     => {
48                 pre:    sub (doc) {
49                     mp.vcs_commit_on_close(doc, "git commit -a -F '%s'");
50                 }
51             }
52         }
53     },
54     {
55         id:     'SVN',
56         check:  [ '.svn' ],
57         opts:   {
58             'svn diff'          => NULL,
59             'svn status'        => NULL,
60             'svn log'           => NULL,
61             "svn diff '%s'"     => NULL,
62             "svn log '%s'"      => NULL,
63             "svn add '%s'"      => NULL,
64             "svn annotate '%s'" => NULL,
65             "svn update"        => NULL
66         }
67     }
70 /** editor actions **/
72 mp.actions['vcs'] = sub (d) {
73     local v = NULL;
75     foreach (n, mp.vcs) {
76         foreach (d, n.check) {
77             if (stat(d) != NULL) {
78                 v = n;
79                 break;
80             }
81         }
83         if (v != NULL)
84             break;
85     }
87     if (v == NULL)
88         mp.alert(L("This directory is not under a supported version control system"));
89     else {
90         local l = sort(keys(v.opts));
92         local t = mp.form(
93             [
94                 {
95                     label:  sprintf(L("Available %s commands"), v.id),
96                     type:   'list',
97                     list:   l,
98                     value:  mp.vcs_target
99                 }
100             ]
101         );
103         if (t != NULL) {
104             mp.vcs_target = t[0];
106             local cmd = sprintf(l[mp.vcs_target], d.name);
108             /* if a 'pre' hook exists, run it and get the
109                cmd to be executed (NULL if none) */
110             if (v.opts[cmd].pre) {
111                 cmd = v.opts[cmd].pre(d);
112             }
114             if (cmd != NULL) {
115                 local log = mp.open(sprintf(L("<%s output>"), cmd));
117                 log.txt.lines   = [ '' ];
118                 log.txt.y       = 0;
120                 if (v.opts[cmd] == NULL) {
121                     mp.actions.exec_command(log, cmd);
122                 }
124                 mp.detect_syntax(log);
125                 mp.move_bof(log);
126                 log.txt.mod     = 0;
127                 log.undo        = [];
128                 log.read_only   = 1;
129             }
130         }
131     }
134 /** default key bindings **/
136 mp.keycodes['ctrl-p'] = 'vcs';
138 /** action descriptions **/
140 mp.keycodes['vcs'] = LL("Version Control...");
142 /** code **/
144 sub mp.vcs_commit_on_close(doc, cmd)
145 /* on_close handler for commit message documents */
147     local msg_file = '.COMMIT_MESSAGE';
149     /* create a commit message */
150     local ci = mp.open(msg_file);
152     ci.commit_cmd = cmd;
154     /* and add an on_close handler */
155     push(ci.on_close,
156         sub (doc) {
157             local ret = 1;
159             if (stat(doc.name)) {
160                 doc.txt.lines   = [ '' ];
161                 doc.txt.y       = 0;
162                 mp.actions.exec_command(doc, sprintf(doc.commit_cmd, doc.name));
164                 mp.detect_syntax(doc);
165                 mp.move_bof(doc);
166                 doc.txt.mod     = 0;
167                 doc.undo        = [];
168                 doc.read_only   = 1;
170                 doc.name = sprintf(L("<%s output>"), doc.commit_cmd);
172                 /* reject closing (file has been modified) */
173                 ret = 0;
174             }
176             unlink(msg_file);
178             /* delete this on_close handler */
179             pop(doc.on_close);
181             return ret;
182         }
183     );
185     return NULL;