]> git.donarmstrong.com Git - lilypond.git/blob - scm/editor.scm
* scm/editor.scm (slashify): New function.
[lilypond.git] / scm / editor.scm
1 ;;;; editor.scm --
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2005 Jan Nieuwenhuizen <janneke@gnu.org>
6
7 (define-module (scm editor))
8
9 (use-modules
10  (ice-9 regex))
11
12 (define editor-command-template-alist
13   '(("emacs" .  "emacsclient --no-wait +%(line)s:%(column)s %(file)s")
14     ("gvim" . "gvim --remote +:%(line)s:norm%(column)s %(file)s")
15     ("nedit" . "nc -noask +%(line)s %(file)s")
16     ("gedit" . "gedit +%(line)s %(file)s")
17     ("jedit" . "jedit %(file)s +line:%(line)s")))
18
19 (define (get-editor)
20   (or (getenv "LYEDITOR")
21       (getenv "XEDITOR")
22       (getenv "EDITOR")
23       "emacs"))
24
25 (define (get-command-template alist editor)
26   (define (get-command-template-helper)
27     (if (null? alist)
28         (if (string-match "%\\(file\\)s" editor)
29             editor
30             (string-append editor " %(file)s"))
31         (if (string-match (caar alist) editor)
32             (cdar alist)
33             (get-command-template (cdr alist) editor))))
34   (if (string-match "%\\(file\\)s" editor)
35       editor
36       (get-command-template-helper)))
37
38 (define (re-sub re sub string)
39   (regexp-substitute/global #f re string 'pre sub 'post))
40
41 (define (slashify x)
42  (if (string-index x #\/)
43      x
44      (re-sub "\\\\" "/" x)))
45
46 (define-public (get-editor-command file-name line column)
47   (let* ((editor (get-editor))
48          (template (get-command-template editor-command-template-alist editor))
49          (command
50           (re-sub "%\\(file\\)s" (format #f "~S" file-name)
51                   (re-sub "%\\(line\\)s" (format #f "~a" line)
52                           (re-sub "%\\(column\\)s" (format #f "~a" column)
53                                   (slashify template))))))
54     command))