]> git.donarmstrong.com Git - lilypond.git/commitdiff
add functions to set syntax table according to context
authorHeikki Junes <heikki.junes@hut.fi>
Thu, 17 Jul 2003 23:01:13 +0000 (23:01 +0000)
committerHeikki Junes <heikki.junes@hut.fi>
Thu, 17 Jul 2003 23:01:13 +0000 (23:01 +0000)
ChangeLog
lilypond-font-lock.el
lilypond-mode.el

index d9157147094598dedd1007a18e3b923b51cc10bb..4516704c7fc1daa88659c1580efb2f0d7a290dda 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2003-07-18  Heikki Junes  <hjunes@cc.hut.fi>
+
+       * lilypond-font-lock.el (LilyPond-mode-set-syntax-table, 
+       LilyPond-mode-context-set-syntax-table): new functions to define
+       the syntax table according to context.
+
+       * lilypond-mode.el: use LilyPond-mode-set-syntax-table.
+
 2003-07-17  Han-Wen Nienhuys  <hanwen@cs.uu.nl>
 
        * GNUmakefile.in: remove no kpathsea hack.
index b4ab0cf84fb582d112b27f366d855fdc36559cf4..8dc3eb79edd23d96a2a541ec0a2affd290ccf829 100644 (file)
 (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 which can be customized according to a context."
+  (interactive)
+  (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        
          '(
-           ;; all the paren characters are now handled by   
-           ;; lily-specific indenting/matching code in lilypond-indent.el
-           ;; Emacs' show-paren-function and XEmacs' paren-highlight use
-           ;; these slur-definitions through Lilypond specific scan-sexps.
-           ( ?\[ . "(]" ) ( ?\] . ")[" )
-           ( ?\( . "()" ) ( ?\) . ")(" ) 
-           ( ?\< . "(>" ) ( ?\> . ")<") 
-           ( ?\{  .  "(} 2" )  ; also 2nd char in begin of block-comment
-           ( ?\}  .  "){ 4" )  ; also 2nd char in end of block-comment
+           ;; 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
            ( ?\$ . "." ) ( ?\& . "." )
            ( ?\* . "." ) ( ?\+ . "." ) ( ?\/ . "." )  ( ?\= . "." )
            ( ?\| . "." )      ; bar line
-           ;; In LilyPond the following chars serve as escape chars, 
-           ;; e.g., c^> d-) e_( , but they are set to punctuation chars, 
-           ;; since inside strings they should not act as escape chars
-           ( ?\- . "\\" ) ( ?\_ . "." ) ( ?\^ . "." )
-           ))
-  )
-
+           )))
+    ;; 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 which can be customized according to a context."
+  ;; make a syntax table without parentheses
+  (interactive)
+  ;; default map sets parentheses to punctuation characters
+  (LilyPond-mode-set-syntax-table) 
+  ;; find the context
+  (setq context (parse-partial-sexp (point-min) (point)))
+  (cond ((nth 3 context)) ; inside string
+       ((nth 4 context)) ; inside a comment
+       ((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 '( ?\< ?\> ?\{ ?\} ?\^ ?\- ?\_ )))
+       ))
index fa3fc55e4b79cc9151d9d9fae2826f5bf40cc1cd..e06325f8b730dee7d38e4bc5f4252d04435552d9 100644 (file)
@@ -689,6 +689,7 @@ command."
   (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren)
   (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren)
   (define-key LilyPond-mode-map "]" 'LilyPond-electric-close-paren)
+  (define-key LilyPond-mode-map "\C-c\C-x" 'LilyPond-mode-context-set-syntax-table) ; try it
   (if (string-match "XEmacs\\|Lucid" emacs-version)
       (define-key LilyPond-mode-map [iso-left-tab] 'LilyPond-autocompletion)
     (define-key LilyPond-mode-map [iso-lefttab] 'LilyPond-autocompletion))
@@ -1112,7 +1113,7 @@ LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
   (make-local-variable 'indent-line-function)
   (setq indent-line-function 'LilyPond-indent-line)
 
-  (set-syntax-table LilyPond-mode-syntax-table)
+  (LilyPond-mode-set-syntax-table '(?\< ?\> ?\{ ?\}))
   (setq major-mode 'LilyPond-mode)
   (setq mode-name "LilyPond")
   (setq local-abbrev-table LilyPond-mode-abbrev-table)