From 143f23f3dfedd41476fdc46f9e568049726d10f2 Mon Sep 17 00:00:00 2001 From: Chris Mann Date: Sun, 12 Oct 2008 18:37:13 +1030 Subject: [PATCH] * wesnoth-mode.el (wesnoth-mode-version): Updated. (wesnoth-parent-tag): Improved performance. (wesnoth-build-completion): Use hash-table when possible. (wesnoth-check-element-type): Now takes numeric argument, instead of a function. Use hash-table when possible. (wesnoth-check-wml): Check hash-table for elements. Pass numeric arguments to `wesnoth-check-element-type'. (wesnoth-mode): Generate hash-table. --- wesnoth-mode.el | 65 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/wesnoth-mode.el b/wesnoth-mode.el index 8cd0e9e..c4a9a67 100644 --- a/wesnoth-mode.el +++ b/wesnoth-mode.el @@ -33,6 +33,9 @@ ;; to automatically load wesnoth-mode for all files ending in '.cfg'. ;;; History: +;; 1.3.1 +;; * Completion history available is now specific to wesnoth-mode. +;; * Significantly improved performance of completion and WML checking. ;; 1.3.0 ;; * Added support for Xemacs. ;; * WML checking is now context sensitive; checks attributes and macros. @@ -131,7 +134,7 @@ (require 'wesnoth-update) (require 'wesnoth-wml-data) -(defconst wesnoth-mode-version "1.3.0" +(defconst wesnoth-mode-version "1.3.0+git" "The current version of `wesnoth-mode'.") (defgroup wesnoth-mode nil "Wesnoth-mode access" @@ -270,16 +273,29 @@ PARTIAL is the partial string on which to attempt completion." element)) (defun wesnoth-parent-tag () - "Return the name of the parent tag, nil otherwise." + "Return the name of the parent tag. +If the parent is a preprocessor statement, return non-nil. +If the element has not parent, return nil. +Otherwise, return a string containing the name of the parent tag." (save-excursion - (let ((parent (when (and (wesnoth-wml-start-pos) - (> (point) (wesnoth-wml-start-pos))) - (wesnoth-check-structure (wesnoth-wml-start-pos) - (point))))) - (when parent - (if (string-match wesnoth-preprocessor-closing-regexp parent) - t - (substring parent 2 (1- (length parent)))))))) + (let ((start-point (point))) + (when (save-excursion (> (point) (progn (back-to-indentation) + (point)))) + (end-of-line)) + (when (wesnoth-search-for-matching-tag + 'search-backward-regexp + wesnoth-element-opening 'point-min) + (while (wesnoth-search-for-matching-tag + 'search-backward-regexp + wesnoth-element-opening 'point-min))) + (unless (= (point) start-point) + (beginning-of-line) + (when (looking-at + "^[\t ]*\\(\\[\\(\\(\\w\\|_\\)+\\)\\]\\|#define\\|#enddef\\)") + (let ((parent (match-string-no-properties 1))) + (if (string-match wesnoth-preprocessor-closing-regexp parent) + t + (substring parent 1 (1- (length parent)))))))))) (defun wesnoth-indent-or-complete (&optional elements) "Indent or complete the line at point, depending on context. @@ -398,9 +414,7 @@ than 22. POSITION is the argument passed to `nth' for (let* ((parent (wesnoth-parent-tag)) (candidates (if (or (stringp parent) (null parent)) - (dolist (tag wesnoth-tag-data) - (when (string= (car tag) (wesnoth-parent-tag)) - (return (nth position tag)))) + (nth (1- position) (gethash parent wesnoth-tag-hash-table)) (mapcar 'car wesnoth-tag-data)))) (wesnoth-emacs-completion-formats candidates))) @@ -729,18 +743,14 @@ be performed." (defun wesnoth-check-element-type (position last-tag) "Determine the context of the element. POSITION is the position of the element in the list. -LAST-TAG is the parent element." - (if (or (string= last-tag "#define") - (string= last-tag "#ifndef") - (string= last-tag "#ifdef")) +LAST-TAG is the parent element." + (let ((inhibit-changing-match-data t)) + (if (or (not last-tag) (string-match "#\\(?:define\\|ifn?def\\)" last-tag)) + (member (match-string-no-properties 1) + (mapcar 'car wesnoth-tag-data)) (member (match-string-no-properties 1) - (mapcar 'car wesnoth-tag-data)) - (let ((result '())) - (dolist (tag wesnoth-tag-data) - (when (member (match-string-no-properties 1) - (funcall position tag)) - (add-to-list 'result (car tag)))) - (member last-tag result)))) + (nth position (gethash last-tag + wesnoth-tag-hash-table)))))) ;; Provide `line-number-at-pos' implementation (not available in Emacs 21). (defun wesnoth-line-number-at-pos (&optional pos) @@ -771,7 +781,7 @@ ARGS is any additional data required by `format' to handle FORMAT-STRING." "Perform context-sensitive analysis of WML-code." (interactive) (wesnoth-update-project-information) - (unless wesnoth-tag-data + (when (= 0 (hash-table-count wesnoth-tag-hash-table)) (error "WML data not available; can not generate report")) (let ((unmatched-tag-list '()) (outbuf (get-buffer-create "*WML*"))) @@ -790,7 +800,7 @@ ARGS is any additional data required by `format' to handle FORMAT-STRING." (point-max) t) (beginning-of-line) (cond ((looking-at "^[\t ]*\\[\\+?\\(\\(\\w\\|_\\)+\\)\\]") - (unless (wesnoth-check-element-type 'second + (unless (wesnoth-check-element-type 0 (car unmatched-tag-list)) (wesnoth-check-output outbuf "Tag not available in this context: '%s'" @@ -812,7 +822,7 @@ ARGS is any additional data required by `format' to handle FORMAT-STRING." "Preprocessor statement does not nest correctly")) (setq unmatched-tag-list (cdr unmatched-tag-list))) ((looking-at "^[\t ]*\\(\\(\\w\\|_\\)+\\)=\\(.+\\)?") - (unless (wesnoth-check-element-type 'third + (unless (wesnoth-check-element-type 1 (car unmatched-tag-list)) (wesnoth-check-output outbuf "Attribute not available in this context: '%s'" @@ -956,6 +966,7 @@ positions of the buffer, respectively." (font-lock-syntactic-keywords . wesnoth-syntactic-keywords))) (setq indent-tabs-mode nil) (easy-menu-add wesnoth-menu wesnoth-mode-map) + (wesnoth-create-wml-hash-table) (run-hooks 'wesnoth-mode-hook)) (provide 'wesnoth-mode) -- 2.11.4.GIT