>>>> "Stephen" == Stephen J Turnbull
<stephen(a)xemacs.org> writes:
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))))))))
Thanks that is much more elegant then the solution I found meanwhile:
(defun footnote-addons-local-var ()
;; Heavy borrowings from hack-local-variables here.
"Set up the local variables for writing, or not.
Returns (prefix . suffix) for write."
(interactive)
(let ((lv-string (concat "Local " "Variables:")) (prefix
"") (suffix "")
(case-fold-search t) (xtra "") preflen ends haspref)
;; hack to avoid this looking like a real local variables entry
(goto-char (point-max))
(search-backward "\n\^L"
(max (- (point-max) 3000) (point-min)) 'move)
(if (search-forward lv-string nil 1)
(my-insert-counter-value) )))
I don't know why but putting it in the
kill-buffer-hook caused problems so I think the write-file-hooks is
better.
Uwe
_______________________________________________
XEmacs-Beta mailing list
XEmacs-Beta(a)xemacs.org
http://calypso.tux.org/cgi-bin/mailman/listinfo/xemacs-beta