From 7a0dd821daa614eeed976e601625bb524c1e9238 Mon Sep 17 00:00:00 2001 From: Chris Mann Date: Thu, 10 Jan 2008 15:12:42 +1030 Subject: [PATCH] * wesnoth-mode.el (wesnoth-insert-tag): Can now insert around region. (wesnoth-indent): New indentation behaviour is now very close (exactly?) as ESR uses. (wesnoth-check-structure): Fixed bug in region handling where start, end would be over-rided with interactive value. --- wesnoth-mode.el | 62 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/wesnoth-mode.el b/wesnoth-mode.el index 6447187..81cdafd 100644 --- a/wesnoth-mode.el +++ b/wesnoth-mode.el @@ -62,6 +62,7 @@ ;; * `wesnoth-indent-savefile' and `wesnoth-indent-default' now leave point at ;; the first non-whitespace character of the line. ;; * `wesnoth-check-tag-names' now reports on success. +;; * `wesnoth-insert-tag' is now able to insert tags around a region. ;; 1.2.1 ;; * Base indent now defaults to 4. ;; * Added support for #ifndef. @@ -219,20 +220,37 @@ indent the line." ;;; Insertion -(defun wesnoth-insert-tag (&optional tagname) +(defun wesnoth-insert-tag (tagname &optional start end) "Inserts the specified opening tag and it's matching closing tag. Both the opening and closing tags will be placed on their own lines with point positioned between them. Completion of tags at -the prompt uses `wesnoth-tags-list'." +the prompt uses `wesnoth-tags-list'. + +TAGNAME is the name of the tag to be inserted. If START and END +are given, the tags will be inserted around the specified region." (interactive (list (completing-read "Tag: " wesnoth-tags-list nil nil))) - (end-of-line) - (unless (looking-back "^[\t ]*$") - (newline)) - (wesnoth-insert-and-indent "[" tagname "]") - (wesnoth-insert-and-indent "\n") - (save-excursion - (wesnoth-insert-and-indent "\n[/" tagname "]"))) + (when (and (not (or start end)) transient-mark-mode mark-active) + (setq start (region-beginning) + end (copy-marker (region-end)))) + (if (and start end) + (progn + (goto-char start) + (or (looking-at "^[\t ]*$") + (open-line 1)) + (insert "[" tagname "]") + (goto-char end) + (or (looking-back "^[\t ]*$") + (newline)) + (insert "[/" tagname "]\n") + (setq end (point)) ;; New target for indent-region + (indent-region start end)) + (or (looking-back "^[\t ]*$") + (newline)) + (wesnoth-insert-and-indent "[" tagname "]") + (wesnoth-insert-and-indent "\n") + (save-excursion + (wesnoth-insert-and-indent "\n[/" tagname "]")))) (defun wesnoth-insert-missing-closing (&optional start end) "Insert the next exected closing element at point." @@ -392,7 +410,7 @@ Return the likely starting position of the WML if it is found. Otherwise return nil." (save-excursion (goto-char (point-min)) - (when (search-forward-regexp (wesnoth-element) + (when (search-forward-regexp (wesnoth-element nil) (buffer-size) t) (beginning-of-line) (point)))) @@ -408,7 +426,8 @@ surrounding tags." (if (or (not (wesnoth-wml-start-pos)) (<= (point) (wesnoth-wml-start-pos)) (nth 3 (syntax-ppss (point))) - (and preproc-bol (looking-at (concat "^[\t ]*" wesnoth-preprocessor-regexp)))) + (and preproc-bol (looking-at (concat "^[\t ]*" wesnoth-preprocessor-regexp))) + (not (wesnoth-check-structure (wesnoth-wml-start-pos) (point)))) (indent-line-to 0) (let ((cur-indent)) (save-excursion @@ -422,10 +441,12 @@ surrounding tags." (setq cur-indent (- (current-indentation) wesnoth-base-indent)))) ;; Opening regexp ((looking-at (wesnoth-element-opening preproc-bol)) + ;; Goto start-pos if unable to find target (search-backward-regexp - (wesnoth-element preproc-bol)) + (wesnoth-element preproc-bol) (wesnoth-wml-start-pos) 0) ;; If opening preceded by opening, increase indentation - (when (looking-at (wesnoth-element-opening preproc-bol)) + (when (or (looking-at (wesnoth-element-opening preproc-bol)) + (not (wesnoth-check-structure (wesnoth-wml-start-pos) (point)))) (setq cur-indent (+ (current-indentation) wesnoth-base-indent)))) ;; Not opening, not closing (t @@ -434,8 +455,8 @@ surrounding tags." (if savefile (and (looking-at (wesnoth-element-opening preproc-bol)) (setq cur-indent (+ (current-indentation) wesnoth-base-indent))) - (and (looking-at (wesnoth-element-closing preproc-bol)) - (setq cur-indent (- (current-indentation) wesnoth-base-indent)))))) + (and (looking-at (wesnoth-element-closing preproc-bol)) + (setq cur-indent (- (current-indentation) wesnoth-base-indent)))))) ;; Indentation change unnecessary (unless cur-indent (setq cur-indent (current-indentation)))) @@ -499,11 +520,12 @@ 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))) + (unless (or start end) + (if (and start end 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) (expected nil)) -- 2.11.4.GIT