Uwe Brauer writes:
My first intent was something like this.
(defun my-insert-counter-value ()
(interactive)
(save-excursion
(goto-char (point-max))
(search-backward ";;; counter-value:")
(beginning-of-line 1)
(search-forward ";;; counter-value:")
(kill-line)
(insert (concat " " (format "%d" counter-value) " "))))
I would do it this way:
(defun my-insert-counter-value ()
(interactive)
(save-excursion
(goto-char (point-max))
(save-match-data
;; Is there a Local variables section?
(when (re-search-backward
"^\\(.*\\S-\\)?\\s-*Local variables:\\s-*\\(\\S-.*\\)?$"
;; This "t" is what allows you to use a search as a
;; Boolean expression.
nil t)
(let* ((prefix (match-string 1))
(suffix (match-string 2))
(end-re (format "^%s\\s-*End:\\s-*%s$" suffix prefix))
(my-re (format "^%s\\s-*counter-value:\\s-*%s$" suffix prefix))
;; Search limit "coming back" from End:
(here (point)))
;; Check for syntactic validity of variables section
(if (not (re-search-forward end-re nil t))
;; You do want this to be an error; if it occurs, the
;; Local variables section will be ignored, and you'll
;; probably not know why!
(error 'invalid-state "Ill-formed Local variables section!")
;; Now go "back" to your variable.
;; #### This may be off-by-one if the counter-value line
;; #### isn't there yet. (If so, do `(forward-line 1)' here.)
(when (re-search-backward my-re here t)
;; #### `kill-entire-line' may be 21.5-only.
(kill-entire-line))
(insert (format "%s counter-value: %d %s\n"
prefix counter-value suffix))))))))
_______________________________________________
XEmacs-Beta mailing list
XEmacs-Beta(a)xemacs.org
http://calypso.tux.org/cgi-bin/mailman/listinfo/xemacs-beta