From 1590660f2b113a4fa5df90acd802465e1d6f51fc Mon Sep 17 00:00:00 2001 From: Chris Mann Date: Wed, 9 Jan 2008 16:02:04 +1030 Subject: [PATCH] * (wesnoth-indent-line-default, wesnoth-indent-line-savefile): renamed. * (wesnoth-insert-missing-closing): New function. Bound to C-c /. * (wesnoth-newline): Behave more consistently. * (wesnoth-newline-and-indent): Obey wesnoth-auto-indent-flag. * (wesnoth-check-structure): Now able to work on a region. Fixed a bug where preprocessor statements, etc. were not returned when called non-interactively. Improved output. * (wesnoth-fix-structure): Removed. --- wesnoth-mode.el | 128 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 55 deletions(-) diff --git a/wesnoth-mode.el b/wesnoth-mode.el index a6e4df9..ac48f3d 100644 --- a/wesnoth-mode.el +++ b/wesnoth-mode.el @@ -32,16 +32,25 @@ ;;; Changes: ;; 1.2.2 -;; * Fixed #endif not highlighting correctly. -;; * Improvements to jumping to matching tag. -;; * Indentation style is now determined by `wesnoth-indentation-function'. -;; * Performing indentation now leaves point at the first non-whitespace -;; character on the line. -;; * Preprocessor statements are now checked for correct nesting when -;; performing `wesnoth-check-structure'. +;; * Fixed #endif not highlighting correctly. +;; * Improvements to jump-to-matching. +;; * Indentation style is now determined by `wesnoth-indentation-function'. +;; * Performing indentation now leaves point at the first non-whitespace +;; character on the line. +;; * Preprocessor statements are now checked for correct nesting when +;; performing `wesnoth-check-structure'. +;; * `wesnoth-check-structure' can now be applied over an active region. +;; * `wesnoth-insert-missing-closing' new function to insert the next +;; expected closing element. (Bound to C-c C-/.) +;; * Changed `wesnoth-newline' to behave more consistantly. +;; * Fixed automatic indentation ignoring the state of +;; `wesnoth-auto-indent-flag' in `wesnoth-newline-and-indent'. +;; * Renamed `wesnoth-indent-line-default' and `wesnoth-indent-line-savefile' +;; to `wesnoth-indent-default' and `wesnoth-indent-savefile', respectively. +;; Set default value for `wesnoth-indentation-function' accordingly. ;; 1.2.1 -;; * Base indent now defaults to 4. -;; * Added support for #ifndef. +;; * Base indent now defaults to 4. +;; * Added support for #ifndef. (defconst wesnoth-mode-version "1.2.2" "The current version of wesnoth-mode.") @@ -50,10 +59,10 @@ :group 'languages :prefix "wesnoth-") -(defcustom wesnoth-indentation-function 'wesnoth-indent-line-default +(defcustom wesnoth-indentation-function 'wesnoth-indent-default "Use the specified function when indenting WML. -You can specify either `wesnoth-indent-line-default' or -`wesnoth-indent-line-savefile' as the indentation style or a +You can specify either `wesnoth-indent-default' or +`wesnoth-indent-savefile' as the indentation style or a customised function for indentation." :type 'function :group 'wesnoth-mode) @@ -99,7 +108,9 @@ indent the line." (define-key wesnoth-mode-map "\C-c\C-n" 'wesnoth-check-tag-names) (define-key wesnoth-mode-map "\C-cn" 'wesnoth-check-tag-names) (define-key wesnoth-mode-map "\C-c\C-e" 'wesnoth-insert-tag) - (define-key wesnoth-mode-map "\C-ce" 'wesnoth-insert-tag)) + (define-key wesnoth-mode-map "\C-ce" 'wesnoth-insert-tag) + (define-key wesnoth-mode-map (kbd "C-c C-/") 'wesnoth-insert-missing-closing) + (define-key wesnoth-mode-map (kbd "C-c /") 'wesnoth-insert-missing-closing)) (defvar wesnoth-syntax-table (let ((wesnoth-syntax-table (make-syntax-table))) @@ -311,7 +322,7 @@ Otherwise return nil." (beginning-of-line) (point)))) -(defun wesnoth-indent-line-default () +(defun wesnoth-indent-default () "Indent the current line as WML using normal-style indentation." (beginning-of-line) (if (or (not (wesnoth-wml-start-pos)) @@ -369,7 +380,7 @@ Otherwise return nil." (beginning-of-line) (search-forward-regexp "[\t ]*")) -(defun wesnoth-indent-line-savefile () +(defun wesnoth-indent-savefile () "Indent the current line as WML code using savefile-style indentation." (beginning-of-line) (if (or (not (wesnoth-wml-start-pos)) @@ -409,12 +420,9 @@ Otherwise return nil." If `wesnoth-auto-indent-flag' is nil, indentation will not be performed." (interactive) - (when wesnoth-auto-indent-flag - (save-excursion - (beginning-of-line) - (if (looking-at "^[\t ]*$") - (indent-line-to 0) - (wesnoth-indent-line)))) + (save-excursion + (when wesnoth-auto-indent-flag + (wesnoth-indent-line))) (newline)) (defun wesnoth-newline-and-indent () @@ -423,7 +431,8 @@ If `wesnoth-auto-indent-flag' is nil, indentation will not be performed." (interactive) (wesnoth-newline) - (wesnoth-indent-line)) + (when wesnoth-auto-indent-flag + (wesnoth-indent-line))) (defun wesnoth-check-tag-names () "Check the names of all tags in the buffer for correctness. @@ -449,20 +458,26 @@ position." (message "'%s' is not known to exist" missing-tag-name)))) -(defun wesnoth-check-structure () +(defun wesnoth-check-structure (&optional start end) "Check the buffer for correct nesting of elements. If a problem is found in the structure, point will be placed at the location which an element was expected and the expected element will be displayed in the minibuffer." (interactive) + (if (and transient-mark-mode mark-active) + (setq start (region-beginning) + end (copy-marker (region-end))) + (setq start (point-min) + end (point-max))) (let ((unmatched-tag-list '()) - (error-position nil)) + (error-position nil) + (expected nil)) (save-excursion - (goto-char (point-min)) + (goto-char start) (while (and (search-forward-regexp (concat "^[\t ]*\\[\\|\\(" wesnoth-preprocessor-regexp "\\)") - (point-max) t) + end t) (not error-position)) (beginning-of-line) (if (looking-at "^[\t ]*#\\(\\w+\\)") @@ -499,38 +514,41 @@ element will be displayed in the minibuffer." (setq unmatched-tag-list (cdr unmatched-tag-list)) (setq error-position (point)))))) (end-of-line))) - (if (not (or unmatched-tag-list error-position)) - (message "%s" "Tag structure appears consistent.") - (if error-position - (goto-char error-position) - (goto-char (point-max))) - (let ((expected nil)) - (cond ((string= (car unmatched-tag-list) "define") - (setq expected "#enddef")) - ((string-match "ifn?def" (car unmatched-tag-list)) - (setq expected "#endif")) - ((not unmatched-tag-list) - (setq expected "Unexpected end of file"))) - (and (interactive-p) - (message "Expecting: '%s'" + (when unmatched-tag-list + (cond ((string= (car unmatched-tag-list) "define") + (setq expected "#enddef")) + ((string-match "ifn?def" (car unmatched-tag-list)) + (setq expected "#endif")) + ((not unmatched-tag-list) + (setq expected "Unexpected end of file")))) + (if (interactive-p) + (if (not (or unmatched-tag-list error-position)) + (message "%s" "Structure appears consistent.") + (and error-position + (goto-char error-position)) + (if (string= expected "Unexpected end of file") + (message "Error %s" expected) + (message "Error: Expecting %s" (or - expected (concat "[/" (car unmatched-tag-list) - "]")))) - (or expected (concat "[/" (car unmatched-tag-list) "]")))))) - -;; TODO This should allow interactive fixing. -(defun wesnoth-fix-structure () - "Attempt to fix faults in the structure of the current buffer." - (interactive) - (let ((element (wesnoth-check-structure))) - (while element + expected + (concat " [/" (car unmatched-tag-list) "]"))))) + (when (or expected unmatched-tag-list) + (or expected (concat "[/" (car unmatched-tag-list) "]")))))) + +(defun wesnoth-insert-missing-closing () + "Insert the next exected closing element at point." + (interactive "r") + (let ((element (wesnoth-check-structure start end))) + (if element (if (string= element "Unexpected end of file") - (delete-region (point) (point-max)) - (beginning-of-line) - (open-line 1) + (message "%s" "Error: Expected end of file") + (when (not (looking-at "^[\t ]*$")) + (end-of-line) + (wesnoth-newline)) (insert element) - (wesnoth-indent-line)) - (setq element (wesnoth-check-structure))))) + (wesnoth-indent-line) + (end-of-line)) + (error "%s" "Unable to find element to insert")))) (defun wesnoth-indent-line () "Determine and performs indentation on the current line. -- 2.11.4.GIT