]> git.donarmstrong.com Git - lilypond.git/blobdiff - lilypond-font-lock.el
* scm/font.scm: remove old markup legacy
[lilypond.git] / lilypond-font-lock.el
index 3212867a9a60dd15f21e55abaca1cf4239b211a8..cb07b331d75f0db39381d356091610c49e768491 100644 (file)
@@ -6,12 +6,13 @@
 ;;  * Emacs-mode: new keywords, reserved words, identifiers, notenames, 
 ;;    some dynamics and brackets are font-lock-keywords
 ;;  * File lilypond.words gives keywords, identifiers and reserved words
+;;  * context-dependent syntax-tables
 ;; Author: 1997: Han-Wen Nienhuys
 ;; Author: 1995-1996 Barry A. Warsaw
 ;;         1992-1994 Tim Peters
 ;; Created:       Feb 1992
-;; Version:       1.7.18
-;; Last Modified: 6MAY2003
+;; Version:       1.7.25
+;; Last Modified: 20JUL2003
 ;; Keywords: lilypond languages music notation
 
 ;; This software is provided as-is, without express or implied
 ;; "on top", ... (multiline-)scheme: try find slurs up to 7th
       '("[_^-]?#\\(#[ft]\\|-?[0-9.]+\\|\"[^\"]*\"\\|['`]?[a-zA-Z:-]+\\|['`]?([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^)]*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*[^)]*)\\)" 0 font-lock-string-face t)
 
-;; "on top", ... strings
-      '("\\([_^-]?\"\\([^\"\\\\]\\|\\\\.\\|\\\\\n\\)*\"\\)" 0 font-lock-string-face t)
+;; "on top", ... strings, match also unending strings at eof:
+;;               if '\n' was not found, it must be '$' which is eof (?).
+      '("\\([_^-]?\"\\([^\"\\\\]\\|\\\\.\\|\\\\\n\\)*\\(\"\\|$\\)\\)" 0 font-lock-string-face t)
 
 ;; "on top", ... (multiline-)comments
       '("\\(%\\({[^%]*%\\(}\\|\\([^}][^%]*%\\)+}\\)\\|.*\\)\\)" 0 font-lock-comment-face t)
 (defvar LilyPond-mode-syntax-table nil
   "Syntax table used in `LilyPond-mode' buffers.")
 
-(if LilyPond-mode-syntax-table
-    ()
+(defun LilyPond-mode-set-syntax-table (&optional not-punct)
+  "Change syntax table according to the argument `not-punct' which contains characters which are given a context dependent non-punctuation syntax: parentheses may be set to parenthesis syntax and characters `-', `^' and `_' may be set to escape syntax."
+  (if (not not-punct) (setq not-punct '()))
   (setq LilyPond-mode-syntax-table (make-syntax-table))
-  ;; NOTE: Emacs knows only "13"-style (used), XEmacs knows also "1b3b", etc.
-  (mapcar (function
-          (lambda (x) (modify-syntax-entry
-                       (car x) (cdr x) LilyPond-mode-syntax-table)))
+  (let ((defaults        
          '(
-           ;; also '['- and ']'-slurs are handled by lilypond-indent.el
-           ;( ?\[ . "(]" ) ( ?\] . ")[" )
-           ;; all the paren characters are now handled by   
-           ;; lily-specific indenting/matching code in lilypond-indent.el 
-           ( ?\[ . "." ) ( ?\] . "." )
-           ( ?\( . "." ) ( ?\) . "." ) 
-           ( ?\< . "." ) ( ?\> . ".") 
-           ( ?\{  .  ". 2" )  ; also 2nd char in begin of block-comment
-           ( ?\}  .  ". 4" )  ; also 2nd char in end of block-comment
-           ( ?\%  .  "< 13" ) ; comment starter, 1st char in block-comments
+           ;; NOTE: Emacs knows only "13"-style (used), XEmacs knows also "1b3b", etc.
+           ( ?\% . "< 13" )   ; comment starter, 1st char in block-comments
            ( ?\n . ">")       ; newline: comment ender
            ( ?\r . ">")       ; formfeed: comment ender
            ( ?\\ . "\\" )     ; escape characters (as '\n' in strings)
            ( ?\$ . "." ) ( ?\& . "." )
            ( ?\* . "." ) ( ?\+ . "." ) ( ?\/ . "." )  ( ?\= . "." )
            ( ?\| . "." )      ; bar line
-           ( ?\- . "." ) ( ?\_ . "." ) ( ?\^ . "." ) ; accent positioners
-           ))
-  )
-
+           )))
+    ;; all the paren characters are now handled by lily-specific indenting/matching code in lilypond-indent.el
+    (if (or (memq ?\{ not-punct) (memq ?\} not-punct))
+       (setq defaults (cons '( ?\{ . "(} 2" ) (cons '( ?\} . "){ 4" ) defaults))) ; begin and end of a block-comment
+      (setq defaults (cons '( ?\{ . ". 2" ) (cons '( ?\} . ". 4" ) defaults))))    ; begin and end of a block-comment
+    (if (or (memq ?\[ not-punct) (memq ?\] not-punct))
+       (setq defaults (cons '( ?\[ . "(]" ) (cons '( ?\] . ")[" ) defaults)))
+      (setq defaults (cons '( ?\[ . "." ) (cons '( ?\] . "." ) defaults))))
+    (if (or (memq ?\< not-punct) (memq ?\> not-punct))
+       (setq defaults (cons '( ?\< . "(>" ) (cons '( ?\> . ")<" ) defaults)))
+      (setq defaults (cons '( ?\< . "." ) (cons '( ?\> . "." ) defaults))))
+    (if (or (memq ?\( not-punct) (memq ?\) not-punct))
+       (setq defaults (cons '( ?\( . "()" ) (cons '( ?\) . ")(" ) defaults)))
+      (setq defaults (cons '( ?\( . "." ) (cons '( ?\) . "." ) defaults))))
+    ;; In LilyPond the following chars serve as escape chars, e.g., c^> d-) e_( , 
+    ;; but they may be set to punctuation chars, since inside strings they should not act as escape chars
+    (setq defaults (cons (if (memq ?\- not-punct) '( ?\- . "\\" ) '( ?\- . "." ) ) defaults))
+    (setq defaults (cons (if (memq ?\^ not-punct) '( ?\^ . "\\" ) '( ?\^ . "." ) ) defaults))
+    (setq defaults (cons (if (memq ?\_ not-punct) '( ?\_ . "\\" ) '( ?\_ . "." ) ) defaults))
+    (mapcar (function
+            (lambda (x) (modify-syntax-entry
+                         (car x) (cdr x) LilyPond-mode-syntax-table)))
+           defaults)
+    (set-syntax-table LilyPond-mode-syntax-table)))
+
+(defun LilyPond-mode-context-set-syntax-table ()
+  "Change syntax table according to current context."
+  (interactive)
+  ;; default syntax table sets parentheses to punctuation characters
+  (LilyPond-mode-set-syntax-table) 
+  ;; find current context
+  (setq context (parse-partial-sexp (point-min) (point)))
+  (cond ((nth 3 context)) ; inside string
+       ((nth 4 context)) ; inside a comment
+       ((eq (char-syntax (char-before (point))) ?\\)) ; found escape-char
+       ((and (eq (char-syntax (char-before (- (point) 1))) ?\\)
+             (memq (char-before (point)) '( ?\) ?\] )))) ; found escape-char
+       ((memq (char-before (point)) '( ?\) ))
+        (LilyPond-mode-set-syntax-table '( ?\( ?\) )))
+       ((memq (char-before (point)) '( ?\] ))
+        (LilyPond-mode-set-syntax-table '( ?\[ ?\] )))
+       ((memq (char-before (point)) '( ?\> ?\} ))
+        (LilyPond-mode-set-syntax-table '( ?\< ?\> ?\{ ?\} ?\^ ?\- ?\_ )))
+       ((memq (char-after (point)) '( ?\( ))
+        (LilyPond-mode-set-syntax-table '( ?\( ?\) )))
+       ((memq (char-after (point)) '( ?\[ ))
+        (LilyPond-mode-set-syntax-table '( ?\[ ?\] )))
+       ((memq (char-after (point)) '( ?\< ?\{ ))
+        (LilyPond-mode-set-syntax-table '( ?\< ?\> ?\{ ?\} ?\^ ?\- ?\_ )))
+       ))