From 44bd4455546c51dff805d8a2c56ccd733a8395e7 Mon Sep 17 00:00:00 2001 From: Chris Mann Date: Mon, 1 Dec 2008 21:13:56 +1030 Subject: [PATCH] * wesnoth-mode.el (wesnoth-font-lock-keywords): Updated tag regexp not to match arrays. (wesnoth-element-closing, wesnoth-element-opening, wesnoth-element): Added indentation support for FOREACH and NEXT macros. (wesnoth-element-completion): More reliably prompt. No longer insert valid substring when there's a longer valid alternative. (wesnoth-partial-macro-p): Added multi-line support. (wesnoth-indent-or-complete): Only attempt macro completion when an incomplete macro precedes point. Do not delete elements adjacent to the closing tag when completing. (wesnoth-complete-tag, wesnoth-complete-preprocessor, wesnoth-complete-attribute): No longer delete adjacent elements. (wesnoth-complete-macro): Ensure point does not go to to far forward when no arguments required for the macro. No longer delete adjacent elements. (wesnoth-indent): Added support for FOREACH..NEXT indentation. (wesnoth-determine-context): Do not base context when tag is opened and closed immediately on the same line; search for previous element. Added support for FOREACH..NEXT indentation. --- wesnoth-mode.el | 109 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 73 insertions(+), 36 deletions(-) diff --git a/wesnoth-mode.el b/wesnoth-mode.el index 3fb39bc..c861daf 100644 --- a/wesnoth-mode.el +++ b/wesnoth-mode.el @@ -272,7 +272,7 @@ level as their parent.") '("\\({[@~]?\\(\\w\\|\\.\\|/\\|-\\)+}\\)" (1 font-lock-function-name-face)) '("\\({\\(\\w\\|:\\|_\\)+\\|{[~@]?\\)" (1 font-lock-function-name-face)) '("}" . font-lock-function-name-face) - '("[\t ]*\\(\\[[^]]+\\]\\)" 1 font-lock-type-face) + '("[\t ]*\\(\\[/?[^$]\\(\\w\\|_\\)+\\]\\)" 1 font-lock-type-face) '("\\$\\(\\w\\|_\\)+" . font-lock-variable-name-face) '("\\(\\(\\w\\|_\\)+\\(\\,[\t ]*\\(\\w\\|_\\)+\\)*\\)=" 1 font-lock-variable-name-face)) @@ -284,7 +284,7 @@ If LIMITED is non-nil, return a regexp which matches only the #enddef preprocessor." (concat "^[\t ]*\\(\\[/\\(\\w\\|_\\)+\\|" (if limited - "#enddef" + "#enddef\\|{NEXT .+}" "#end\\(?:def\\|if\\)") "\\)")) @@ -294,7 +294,7 @@ If LIMITED is non-nil, return a regexp which matches only the #define preprocessor." (concat "^[\t ]*\\(\\[\\+?\\(\\w\\|_\\)+\\]\\|#define " (if limited - "" + "{FOREACH .+}" "\\|#ifn?def ") "\\)")) @@ -318,7 +318,7 @@ If LIMITED is non-nil, return a regexp which matches only the #define and #enddef preprocessors." (concat "^[\t ]*\\(\\[[/+]?\\(\\w\\|_\\)+\\]?\\|" (if limited - "#define \\|#enddef" + "#define \\|#enddef\\|{FOREACH .+}\\|{NEXT .+}" (substring wesnoth-preprocessor-regexp 5)) "\\)")) @@ -406,12 +406,12 @@ PARTIAL is the partial string on which to attempt completion." nil) ((and element (eq (try-completion element ,completions) t)) element) - ((not (if (listp (car ,completions)) - (assoc element ,completions) - (member element ,completions))) + ((> (length (all-completions (or element "") ,completions)) 1) (completing-read ,prompt ,completions nil nil element - 'wesnoth-history-list))))) + 'wesnoth-history-list)) + (t + element)))) (defun wesnoth-parent-tag () "Return the name of the parent tag. @@ -450,10 +450,10 @@ Otherwise, return a string containing the name of the parent tag." (defun wesnoth-partial-macro-p () "Return non-nil if point is in a partial macro." (save-excursion - (back-to-indentation) - (let ((end (save-excursion (end-of-line) (point))) - (opened 0)) - (while (search-forward-regexp "[{}]" end t) + (let ((opened 0) + (start (point))) + (search-backward-regexp "{" (point-min) t) + (while (search-forward-regexp "[{}]" (point-max) t) (if (string= (match-string 0) "{") (setq opened (1+ opened)) (setq opened (1- opened)))) @@ -466,15 +466,22 @@ matching tags." (interactive "P") (or elements (setq elements 0)) (cond - ((looking-back "\\[\\(\\(\\w\\|_\\)*\\)[\t ]*$") + ((looking-back "\\[\\(\\(\\w\\|_\\)*\\)") (wesnoth-complete-tag elements t)) - ((wesnoth-partial-macro-p) + ((and (looking-back "{\\(\\w\\|_\\)*") (wesnoth-partial-macro-p)) (wesnoth-complete-macro t)) ((looking-back "^#\\w+$") (wesnoth-complete-preprocessor elements t)) ((looking-back "\\[/\\(\\(\\w\\|_\\)*\\)[\t ]*$") - (delete-region (save-excursion (back-to-indentation) (point)) - (progn (end-of-line) (point))) + (delete-region + (save-excursion + (and (search-backward + "[/" + (save-excursion (back-to-indentation) + (point)) + t) + (point))) + (point)) (wesnoth-insert-missing-closing) (end-of-line)) ((looking-back "\\(\\(\\w\\|_\\)+\\)[\t ]*$") @@ -518,13 +525,26 @@ If COMPLETEP is non-nil, attempt to complete preprocessor at point." (when (string-match "#\\(define\\|ifn?def\\|undef\\)" preprocessor) (setq preprocessor (concat preprocessor " "))) (when partial - (delete-region (progn (back-to-indentation) (point)) - (progn (end-of-line) (point)))) + (delete-region + (save-excursion + (and (search-backward + "#" + (save-excursion (back-to-indentation) + (point)) + t) + (point))) + (point))) (wesnoth-preprocessor-closed-p preprocessor))))) (when preprocessor (when partial - (delete-region (progn (back-to-indentation) (point)) - (progn (end-of-line) (point)))) + (delete-region + (save-excursion + (and (search-backward + "#" (save-excursion (back-to-indentation) + (point)) + t) + (point))) + (point))) (if (and (string-match "#\\(define \\|ifn?def\\)" preprocessor) (not closedp)) (progn @@ -577,10 +597,10 @@ If COMPLETEP is non-nil, attempt to complete the macro at point." ;; Delete the region corresponding to the current macro. (delete-region (save-excursion - (search-backward "{" - (save-excursion (back-to-indentation) (point)) - t) - (point)) + (and (search-backward + "{" (save-excursion (back-to-indentation) (point)) + t) + (point))) (save-excursion (if (search-forward-regexp "[ }\t]" @@ -592,8 +612,7 @@ If COMPLETEP is non-nil, attempt to complete the macro at point." (wesnoth-insert-element-separately "{" macro (if args " }" "}"))) (save-excursion (wesnoth-indent)) - (when args - (forward-char -1)) + (forward-char -1) (when args (let ((input (read-string (concat (car args) ": ")))) (insert input (if (string= input "") "" " ")) @@ -615,10 +634,17 @@ If COMPLETEP is non-nil, attempt to complete the attribute at point." (attribute (wesnoth-element-completion completions "Attribute: " partial completep))) (when attribute - (if (and partial completep) + (if partial (progn - (delete-region (progn (back-to-indentation) (point)) - (progn (end-of-line) (point))) + (delete-region + (save-excursion + (or (and (search-backward-regexp + "[\t ]" (save-excursion + (back-to-indentation) + (point)) t) + (point)) + (back-to-indentation (point)))) + (point)) (insert attribute "=")) (wesnoth-insert-element-separately attribute (if (string-match "=" attribute) @@ -635,7 +661,7 @@ If COMPLETEP is non-nil, attempt to complete tag at point." (or elements (setq elements 0)) (let* ((completions (wesnoth-build-completion 0)) (partial (when (and completep - (looking-back "\\[\\(\\(\\w\\|_\\)+\\)")) + (looking-back "\\[\\(\\(\\w\\|_\\)+\\)[\t ]*$")) (match-string-no-properties 1))) (tag (wesnoth-element-completion completions "Tag: " partial completep)) @@ -649,9 +675,14 @@ If COMPLETEP is non-nil, attempt to complete tag at point." (progn (if completep (progn - (delete-region (progn (back-to-indentation) (point)) - (progn (end-of-line) (point))) - + (delete-region (save-excursion + (search-backward + "[" (save-excursion + (back-to-indentation) + (point)) + t) + (point)) + (point)) (if closedp (progn (wesnoth-insert-and-indent "[" tag "]") @@ -969,7 +1000,7 @@ CONTEXT represents the type of element which precedes the current element." (setq cur-indent (+ ref-indent wesnoth-base-indent)) (setq cur-indent ref-indent))) ((eq context 'closing) - (if (or (looking-at "^[\t ]*\\[/") + (if (or (looking-at "^[\t ]*\\(\\[/\\|{NEXT\\)") (and (not wesnoth-indent-savefile) (not (looking-at (wesnoth-element-opening t))))) (setq cur-indent (- ref-indent wesnoth-base-indent)) @@ -1043,6 +1074,12 @@ determine the context." (let ((match (or (and (search-backward-regexp (wesnoth-element t) (point-min) t) + (progn + (while (save-match-data + (looking-at "^[\t ]*\\[[^/].+\\]\\[/.+\\]")) + (search-backward-regexp (wesnoth-element t) + (point-min) t)) + t) (match-string 1)) "")) (depth (wesnoth-within-define position))) @@ -1059,9 +1096,9 @@ determine the context." ;; Found nothing of use; reset match and assume top-level tag. (setq match "")) (cond - ((string-match "\\[/\\|#enddef" match) + ((string-match "\\[/\\|#enddef\\|{NEXT " match) (cons 'closing (current-indentation))) - ((string-match "\\[[^/]?\\|#define" match) + ((string-match "\\[[^/]?\\|#define\\|{FOREACH " match) (cons 'opening (current-indentation))))))) (defun wesnoth-newline-and-indent (&optional indent) -- 2.11.4.GIT