| 1 |
;;; psgml-edit.el --- Editing commands for SGML-mode with parsing support |
| 2 |
;; |
| 3 |
;; $Id: psgml-edit.el,v 2.73 2005/03/02 19:46:31 lenst Exp $ |
| 4 |
|
| 5 |
;; Copyright (C) 1994, 1995, 1996 Lennart Staflin |
| 6 |
|
| 7 |
;; Author: Lennart Staflin <[email protected]> |
| 8 |
|
| 9 |
;; This program is free software; you can redistribute it and/or |
| 10 |
;; modify it under the terms of the GNU General Public License |
| 11 |
;; as published by the Free Software Foundation; either version 2 |
| 12 |
;; of the License, or (at your option) any later version. |
| 13 |
;; |
| 14 |
;; This program is distributed in the hope that it will be useful, |
| 15 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
;; GNU General Public License for more details. |
| 18 |
;; |
| 19 |
;; You should have received a copy of the GNU General Public License |
| 20 |
;; along with this program; if not, write to the Free Software |
| 21 |
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 |
|
| 23 |
|
| 24 |
;;;; Commentary: |
| 25 |
|
| 26 |
;; Part of major mode for editing the SGML document-markup language. |
| 27 |
|
| 28 |
|
| 29 |
;;;; Code: |
| 30 |
|
| 31 |
(provide 'psgml-edit) |
| 32 |
(require 'psgml) |
| 33 |
(require 'psgml-parse) |
| 34 |
(require 'psgml-ids) |
| 35 |
(eval-when-compile (require 'cl)) |
| 36 |
|
| 37 |
;; (eval-when-compile |
| 38 |
;; (setq byte-compile-warnings '(free-vars unresolved callargs redefine))) |
| 39 |
|
| 40 |
|
| 41 |
;;;; Variables |
| 42 |
|
| 43 |
(defvar sgml-split-level nil |
| 44 |
"Used by sgml-split-element") |
| 45 |
|
| 46 |
|
| 47 |
;;;; SGML mode: structure editing |
| 48 |
|
| 49 |
(defun sgml-last-element () |
| 50 |
"Return the element where last command left point. |
| 51 |
This either uses the save value in `sgml-last-element' or parses the buffer |
| 52 |
to find current open element." |
| 53 |
(setq sgml-markup-type nil) |
| 54 |
(if (and (not sgml-xml-p) |
| 55 |
(memq last-command sgml-users-of-last-element) |
| 56 |
sgml-last-element) ; Don't return nil |
| 57 |
sgml-last-element |
| 58 |
(setq sgml-last-element (sgml-find-context-of (point)))) ) |
| 59 |
|
| 60 |
(defun sgml-set-last-element (&optional el) |
| 61 |
(if el (setq sgml-last-element el)) |
| 62 |
(sgml-show-context sgml-last-element)) |
| 63 |
|
| 64 |
(defun sgml-beginning-of-element () |
| 65 |
"Move to after the start-tag of the current element. |
| 66 |
If the start-tag is implied, move to the start of the element." |
| 67 |
(interactive) |
| 68 |
(goto-char (sgml-element-stag-end (sgml-last-element))) |
| 69 |
(sgml-set-last-element (if (sgml-element-empty sgml-last-element) |
| 70 |
(sgml-element-parent sgml-last-element)))) |
| 71 |
|
| 72 |
(defun sgml-end-of-element () |
| 73 |
"Move to before the end-tag of the current element." |
| 74 |
(interactive) |
| 75 |
(goto-char (sgml-element-etag-start (sgml-last-element))) |
| 76 |
(sgml-set-last-element (if (sgml-element-empty sgml-last-element) |
| 77 |
(sgml-element-parent sgml-last-element)))) |
| 78 |
|
| 79 |
(defun sgml-backward-up-element () |
| 80 |
"Move backward out of this element level. |
| 81 |
That is move to before the start-tag or where a start-tag is implied." |
| 82 |
(interactive) |
| 83 |
(goto-char (sgml-element-start (sgml-last-element))) |
| 84 |
(sgml-set-last-element (sgml-element-parent sgml-last-element))) |
| 85 |
|
| 86 |
(defun sgml-up-element () |
| 87 |
"Move forward out of this element level. |
| 88 |
That is move to after the end-tag or where an end-tag is implied." |
| 89 |
(interactive) |
| 90 |
(goto-char (sgml-element-end (sgml-last-element))) |
| 91 |
(sgml-set-last-element (sgml-element-parent sgml-last-element))) |
| 92 |
|
| 93 |
(defun sgml-forward-element () |
| 94 |
"Move forward over next element." |
| 95 |
(interactive) |
| 96 |
(let ((next |
| 97 |
(sgml-find-element-after (point) (sgml-last-element)))) |
| 98 |
(goto-char (sgml-element-end next)) |
| 99 |
(sgml-set-last-element (sgml-element-parent next)))) |
| 100 |
|
| 101 |
(defun sgml-backward-element () |
| 102 |
"Move backward over previous element at this level. |
| 103 |
With implied tags this is ambiguous." |
| 104 |
(interactive) |
| 105 |
(let ((prev ; previous element |
| 106 |
(sgml-find-previous-element (point) (sgml-last-element)))) |
| 107 |
(goto-char (sgml-element-start prev)) |
| 108 |
(sgml-set-last-element (sgml-element-parent prev)))) |
| 109 |
|
| 110 |
(defun sgml-down-element () |
| 111 |
"Move forward and down one level in the element structure." |
| 112 |
(interactive) |
| 113 |
(let ((to |
| 114 |
(sgml-find-element-after (point) (sgml-last-element)))) |
| 115 |
(when (sgml-strict-epos-p (sgml-element-stag-epos to)) |
| 116 |
(error "Sub-element in other entity")) |
| 117 |
(goto-char (sgml-element-stag-end to)) |
| 118 |
(sgml-set-last-element (if (sgml-element-empty to) |
| 119 |
(sgml-element-parent to) |
| 120 |
to)))) |
| 121 |
|
| 122 |
(defun sgml-kill-element () |
| 123 |
"Kill the element following the cursor." |
| 124 |
(interactive "*") |
| 125 |
(sgml-parse-to-here) |
| 126 |
(when sgml-markup-type |
| 127 |
(error "Point is inside markup")) |
| 128 |
(kill-region (point) |
| 129 |
(sgml-element-end (sgml-find-element-after (point))))) |
| 130 |
|
| 131 |
(defun sgml-transpose-element () |
| 132 |
"Interchange element before point with element after point, leave point after." |
| 133 |
(interactive "*") |
| 134 |
(let ((pre (sgml-find-previous-element (point))) |
| 135 |
(next (sgml-find-element-after (point))) |
| 136 |
s1 s2 m2) |
| 137 |
(goto-char (sgml-element-start next)) |
| 138 |
(setq m2 (point-marker)) |
| 139 |
(setq s2 (buffer-substring (point) |
| 140 |
(sgml-element-end next))) |
| 141 |
(delete-region (point) (sgml-element-end next)) |
| 142 |
(goto-char (sgml-element-start pre)) |
| 143 |
(setq s1 (buffer-substring (point) (sgml-element-end pre))) |
| 144 |
(delete-region (point) (sgml-element-end pre)) |
| 145 |
(insert-before-markers s2) |
| 146 |
(goto-char m2) |
| 147 |
(insert s1) |
| 148 |
(sgml-message ""))) |
| 149 |
|
| 150 |
(defun sgml-mark-element () |
| 151 |
"Set mark after next element." |
| 152 |
(interactive) |
| 153 |
(push-mark (sgml-element-end (sgml-find-element-after (point))) nil t)) |
| 154 |
|
| 155 |
(defun sgml-mark-current-element () |
| 156 |
"Set mark at end of current element, and leave point before current element." |
| 157 |
(interactive) |
| 158 |
(let ((el (sgml-find-element-of (point)))) |
| 159 |
(goto-char (sgml-element-start el)) |
| 160 |
(push-mark (sgml-element-end el) nil t))) |
| 161 |
|
| 162 |
|
| 163 |
(defun sgml-change-element-name (gi) |
| 164 |
"Replace the name of the current element with a new name. |
| 165 |
Eventual attributes of the current element will be translated if |
| 166 |
possible." |
| 167 |
(interactive |
| 168 |
(list (let ((el (sgml-find-element-of (point)))) |
| 169 |
(goto-char (sgml-element-start el)) |
| 170 |
(sgml-read-element-name |
| 171 |
(format "Change %s to: " (sgml-element-name el)))))) |
| 172 |
(when (or (null gi) (equal gi "")) |
| 173 |
(error "Illegal name")) |
| 174 |
(let* ((element (sgml-find-element-of (point))) |
| 175 |
(attspec (sgml-element-attribute-specification-list element)) |
| 176 |
(oldattlist (sgml-element-attlist element)) |
| 177 |
(tagc (if (and sgml-xml-p (sgml-element-empty element)) |
| 178 |
(sgml-delim "XML-TAGCE") |
| 179 |
(sgml-delim "TAGC"))) |
| 180 |
(tagc-len (length tagc))) |
| 181 |
(goto-char (sgml-element-end element)) |
| 182 |
(unless (sgml-element-empty element) |
| 183 |
(delete-char (- (sgml-element-etag-len element)))) |
| 184 |
(insert (sgml-end-tag-of gi)) |
| 185 |
(goto-char (sgml-element-start element)) |
| 186 |
(delete-char (sgml-element-stag-len element)) |
| 187 |
(insert (sgml-delim "STAGO") |
| 188 |
(sgml-general-insert-case gi) |
| 189 |
tagc) |
| 190 |
(let* ((newel (sgml-find-context-of (point))) |
| 191 |
(newattlist (sgml-element-attlist newel)) |
| 192 |
(newasl (sgml-translate-attribute-specification-list |
| 193 |
attspec oldattlist newattlist))) |
| 194 |
(backward-char tagc-len) |
| 195 |
(sgml-insert-attributes newasl newattlist) |
| 196 |
(forward-char tagc-len)))) |
| 197 |
|
| 198 |
|
| 199 |
(defun sgml-translate-attribute-specification-list (values from to) |
| 200 |
"Translate attribute specification from one element type to another. |
| 201 |
Input attribute values in VALUES using attlist FROM is translated into |
| 202 |
a list using attlist TO." |
| 203 |
(let ((new-values nil) |
| 204 |
(sgml-show-warnings t) |
| 205 |
tem) |
| 206 |
(loop for attspec in values |
| 207 |
as from-decl = (sgml-lookup-attdecl (sgml-attspec-name attspec) from) |
| 208 |
as to-decl = (sgml-lookup-attdecl (sgml-attspec-name attspec) to) |
| 209 |
do |
| 210 |
(cond |
| 211 |
;; Special case ID attribute |
| 212 |
((and (eq 'ID (sgml-attdecl-declared-value from-decl)) |
| 213 |
(setq tem (sgml-attribute-with-declared-value to 'ID))) |
| 214 |
(push |
| 215 |
(sgml-make-attspec (sgml-attdecl-name tem) |
| 216 |
(sgml-attspec-attval attspec)) |
| 217 |
new-values)) |
| 218 |
;; Use attribute with same name if compatible type |
| 219 |
((equal (sgml-attdecl-declared-value from-decl) |
| 220 |
(sgml-attdecl-declared-value to-decl)) |
| 221 |
(push attspec new-values)) |
| 222 |
(to-decl |
| 223 |
(sgml-log-warning |
| 224 |
"Attribute %s has new declared-value" |
| 225 |
(sgml-attspec-name attspec)) |
| 226 |
(push attspec new-values)) |
| 227 |
(t |
| 228 |
(sgml-log-warning "Can't translate attribute %s = %s" |
| 229 |
(sgml-attspec-name attspec) |
| 230 |
(sgml-attspec-attval attspec))))) |
| 231 |
new-values)) |
| 232 |
|
| 233 |
(defun sgml-untag-element () |
| 234 |
"Remove tags from current element." |
| 235 |
(interactive "*") |
| 236 |
(let ((el (sgml-find-element-of (point)))) |
| 237 |
(when (or (sgml-strict-epos-p (sgml-element-stag-epos el)) |
| 238 |
(sgml-strict-epos-p (sgml-element-etag-epos el))) |
| 239 |
(error "Current element has some tag inside an entity reference")) |
| 240 |
(goto-char (sgml-element-etag-start el)) |
| 241 |
(delete-char (sgml-element-etag-len el)) |
| 242 |
(goto-char (sgml-element-start el)) |
| 243 |
(delete-char (sgml-element-stag-len el)))) |
| 244 |
|
| 245 |
(defun sgml-kill-markup () |
| 246 |
"Kill next tag, markup declaration or process instruction." |
| 247 |
(interactive "*") |
| 248 |
(let ((start (point))) |
| 249 |
(sgml-with-parser-syntax |
| 250 |
(sgml-parse-s) |
| 251 |
(setq sgml-markup-start (point)) |
| 252 |
(cond ((sgml-parse-markup-declaration 'ignore)) |
| 253 |
((sgml-parse-processing-instruction)) |
| 254 |
((sgml-skip-tag))) |
| 255 |
(kill-region start (point))))) |
| 256 |
|
| 257 |
|
| 258 |
;;;; SGML mode: folding |
| 259 |
|
| 260 |
(defun sgml-fold-region (beg end &optional unhide) |
| 261 |
"Hide (or if prefixarg unhide) region. |
| 262 |
If called from a program first two arguments are start and end of |
| 263 |
region. And optional third argument true unhides." |
| 264 |
(interactive "r\nP") |
| 265 |
(setq selective-display t) |
| 266 |
(let ((mp (buffer-modified-p)) |
| 267 |
(inhibit-read-only t) |
| 268 |
(before-change-functions nil) |
| 269 |
(after-change-functions nil)) |
| 270 |
(unwind-protect |
| 271 |
(subst-char-in-region beg end |
| 272 |
(if unhide ?\r ?\n) |
| 273 |
(if unhide ?\n ?\r) |
| 274 |
'noundo) |
| 275 |
(when sgml-buggy-subst-char-in-region |
| 276 |
(set-buffer-modified-p mp))))) |
| 277 |
|
| 278 |
(defun sgml-fold-element () |
| 279 |
"Fold the lines comprising the current element, leaving the first line visible. |
| 280 |
This uses the selective display feature." |
| 281 |
(interactive) |
| 282 |
(sgml-parse-to-here) |
| 283 |
(cond ((and (eq sgml-current-tree sgml-top-tree) ; outside document element |
| 284 |
sgml-markup-type) |
| 285 |
(sgml-fold-region sgml-markup-start |
| 286 |
(save-excursion |
| 287 |
(sgml-parse-to (point)) |
| 288 |
(point)))) |
| 289 |
((and (eq sgml-current-tree sgml-top-tree) ; outside document element |
| 290 |
(looking-at " *<!")) |
| 291 |
(sgml-fold-region (point) |
| 292 |
(save-excursion |
| 293 |
(skip-chars-forward " \t") |
| 294 |
(sgml-parse-to (1+ (point))) |
| 295 |
(point)))) |
| 296 |
|
| 297 |
(t |
| 298 |
(let ((el (sgml-find-element-of (point)))) |
| 299 |
(when (eq el sgml-top-tree) |
| 300 |
(error "No element here")) |
| 301 |
(save-excursion |
| 302 |
(goto-char (sgml-element-end el)) |
| 303 |
(when (zerop (sgml-element-etag-len el)) |
| 304 |
(skip-chars-backward " \t\n")) |
| 305 |
(sgml-fold-region (sgml-element-start el) |
| 306 |
(point))))))) |
| 307 |
|
| 308 |
(defun sgml-fold-subelement () |
| 309 |
"Fold all elements current elements content, leaving the first lines visible. |
| 310 |
This uses the selective display feature." |
| 311 |
(interactive) |
| 312 |
(let* ((el (sgml-find-element-of (point))) |
| 313 |
(c (sgml-element-content el))) |
| 314 |
(while c |
| 315 |
(sgml-fold-region (sgml-element-start c) |
| 316 |
(sgml-element-end c)) |
| 317 |
(setq c (sgml-element-next c))))) |
| 318 |
|
| 319 |
(defun sgml-unfold-line () |
| 320 |
"Show hidden lines in current line." |
| 321 |
(interactive) |
| 322 |
(let ((op (point))) |
| 323 |
(beginning-of-line) |
| 324 |
(push-mark) |
| 325 |
(end-of-line) |
| 326 |
(exchange-point-and-mark) |
| 327 |
(sgml-fold-region (point) (mark) 'unhide) |
| 328 |
(goto-char op))) |
| 329 |
|
| 330 |
(defun sgml-unfold-element () |
| 331 |
"Show all hidden lines in current element." |
| 332 |
(interactive) |
| 333 |
(let* ((element (sgml-find-element-of (point)))) |
| 334 |
(sgml-fold-region (sgml-element-start element) |
| 335 |
(sgml-element-end element) |
| 336 |
'unfold))) |
| 337 |
|
| 338 |
(defun sgml-expand-element () |
| 339 |
"As sgml-fold-subelement, but unfold first." |
| 340 |
(interactive) |
| 341 |
(sgml-unfold-element) |
| 342 |
(sgml-fold-subelement)) |
| 343 |
|
| 344 |
(defun sgml-unfold-all () |
| 345 |
"Show all hidden lines in buffer." |
| 346 |
(interactive) |
| 347 |
(sgml-fold-region (point-min) |
| 348 |
(point-max) |
| 349 |
'unfold)) |
| 350 |
|
| 351 |
;;;; SGML mode: indentation and movement |
| 352 |
|
| 353 |
|
| 354 |
(defun sgml-indent-according-to-level (element) |
| 355 |
(* sgml-indent-step |
| 356 |
(sgml-element-level element))) |
| 357 |
|
| 358 |
(defun sgml-indent-according-to-stag (element) |
| 359 |
(save-excursion |
| 360 |
(goto-char (sgml-element-start element)) |
| 361 |
(+ (current-column) sgml-indent-step))) |
| 362 |
|
| 363 |
(defun sgml-indent-according-to-stag-end (element) |
| 364 |
(save-excursion |
| 365 |
(goto-char (sgml-element-start element)) |
| 366 |
(+ |
| 367 |
(current-column) |
| 368 |
(length (sgml-element-gi element)) |
| 369 |
2))) |
| 370 |
|
| 371 |
|
| 372 |
;;(setq sgml-content-indent-function 'sgml-indent-according-to-stag) |
| 373 |
|
| 374 |
(defun sgml-indent-line (&optional col element) |
| 375 |
"Indent line, calling parser to determine level unless COL or ELEMENT |
| 376 |
is given. If COL is given it should be the column to indent to. If |
| 377 |
ELEMENT is given it should be a parse tree node, from which the level |
| 378 |
is determined. |
| 379 |
Deprecated: ELEMENT" |
| 380 |
(sgml-debug "-> sgml-indent-line %s %s" |
| 381 |
col (if element (sgml-element-gi element))) |
| 382 |
(when sgml-indent-step |
| 383 |
(let ((here (point-marker)) |
| 384 |
;; Where the indentation goes, i.e., will this be data |
| 385 |
element-insert |
| 386 |
;; Where we compute indentation, where the thing we indent is. |
| 387 |
;; Can be different from above if end-tag is omitted. |
| 388 |
element-level) |
| 389 |
(back-to-indentation) |
| 390 |
(unless col |
| 391 |
;; Determine element |
| 392 |
(setq element-insert |
| 393 |
(let ((sgml-throw-on-error 'parse-error)) |
| 394 |
(catch sgml-throw-on-error |
| 395 |
;; This used to be (sgml-find-element-of (point)) |
| 396 |
;; Why? Possibly to handle omitted end-tags |
| 397 |
(sgml-debug "-- sgml-indent-line find context") |
| 398 |
(sgml-find-context-of (point))))) |
| 399 |
(setq element-level element-insert) |
| 400 |
(when (and (not (eobp)) element-level) |
| 401 |
(setq element-level (sgml-find-element-of (point))) |
| 402 |
;; It would be good if sgml-find-element-of would also tell |
| 403 |
;; us if the character is in the start-tag/end-tag or |
| 404 |
;; content |
| 405 |
(when (or (= (point) (sgml-element-start element-level)) |
| 406 |
(sgml-with-parser-syntax (sgml-is-end-tag))) |
| 407 |
(setq element-level (sgml-element-parent element-level))))) |
| 408 |
(when (eq element-level sgml-top-tree) ; not in a element at all |
| 409 |
(setq element-level nil) ; forget element |
| 410 |
(goto-char here)) ; insert normal tab insted) |
| 411 |
(when element-level |
| 412 |
(cond ((and (> (point) (sgml-element-start element-insert)) |
| 413 |
(< (point) (sgml-element-stag-end element-insert)) |
| 414 |
(not (sgml-element-data-p |
| 415 |
(sgml-element-parent element-insert)))) |
| 416 |
(setq col |
| 417 |
(funcall sgml-attribute-indent-function element-insert))) |
| 418 |
((or sgml-indent-data |
| 419 |
(not (sgml-element-data-p element-insert))) |
| 420 |
(setq col |
| 421 |
(funcall sgml-content-indent-function element-level))))) |
| 422 |
(when (and col (/= col (current-column))) |
| 423 |
(beginning-of-line 1) |
| 424 |
(delete-horizontal-space) |
| 425 |
(indent-to col)) |
| 426 |
(when (< (point) here) |
| 427 |
(goto-char here)) |
| 428 |
col))) |
| 429 |
|
| 430 |
|
| 431 |
(defun sgml-next-data-field () |
| 432 |
"Move forward to next point where data is allowed." |
| 433 |
(interactive) |
| 434 |
(when (eobp) |
| 435 |
(error "End of buffer")) |
| 436 |
(let ((sgml-throw-on-warning 'next-data) |
| 437 |
(avoid-el (sgml-last-element))) |
| 438 |
;; Avoid stopping in current element, unless point is in the start |
| 439 |
;; tag of the element |
| 440 |
(when (< (point) (sgml-element-stag-end avoid-el)) |
| 441 |
(setq avoid-el nil)) |
| 442 |
(catch sgml-throw-on-warning |
| 443 |
(while (progn |
| 444 |
(sgml-parse-to (1+ (point))) |
| 445 |
(setq sgml-last-element |
| 446 |
(if (not (eq ?< (following-char))) |
| 447 |
(sgml-find-element-of (point)) |
| 448 |
sgml-current-tree)) |
| 449 |
(or (eq sgml-last-element avoid-el) |
| 450 |
(not (sgml-element-data-p sgml-last-element))))) |
| 451 |
(sgml-set-last-element)))) |
| 452 |
|
| 453 |
|
| 454 |
(defun sgml-next-trouble-spot () |
| 455 |
"Move forward to next point where something is amiss with the structure." |
| 456 |
(interactive) |
| 457 |
(push-mark) |
| 458 |
(sgml-note-change-at (point)) ; Prune the parse tree |
| 459 |
(sgml-parse-to (point)) |
| 460 |
(let ((sgml-throw-on-warning 'trouble)) |
| 461 |
(or (catch sgml-throw-on-warning |
| 462 |
(sgml-parse-until-end-of nil t)) |
| 463 |
(message "Ok")))) |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
;;;; SGML mode: information display |
| 468 |
|
| 469 |
(defun sgml-list-valid-tags () |
| 470 |
"Display a list of the contextually valid tags." |
| 471 |
(interactive) |
| 472 |
(sgml-parse-to-here) |
| 473 |
(let ((model (sgml-element-model sgml-current-tree)) |
| 474 |
(smap-name (sgml-lookup-shortref-name |
| 475 |
(sgml-dtd-shortmaps sgml-dtd-info) |
| 476 |
sgml-current-shortmap))) |
| 477 |
(with-output-to-temp-buffer "*Tags*" |
| 478 |
(princ (format "Current element: %s %s\n" |
| 479 |
(sgml-element-name sgml-current-tree) |
| 480 |
(if (sgml-eltype-defined |
| 481 |
(sgml-element-eltype sgml-current-tree)) |
| 482 |
"" |
| 483 |
"[UNDEFINED]"))) |
| 484 |
(princ (format "Element content: %s %s\n" |
| 485 |
(cond ((or (sgml-current-mixed-p) (eq model sgml-any)) |
| 486 |
"mixed") |
| 487 |
((sgml-model-group-p model) |
| 488 |
"element") |
| 489 |
(t |
| 490 |
model)) |
| 491 |
(if (eq model sgml-any) |
| 492 |
"[ANY]" ""))) |
| 493 |
|
| 494 |
(when smap-name |
| 495 |
(princ (format "Current short reference map: %s\n" smap-name))) |
| 496 |
|
| 497 |
(cond ((sgml-final-p sgml-current-state) |
| 498 |
(princ "Valid end-tags : ") |
| 499 |
(loop for e in (sgml-current-list-of-endable-eltypes) |
| 500 |
do (princ (sgml-end-tag-of e)) (princ " ")) |
| 501 |
(terpri)) |
| 502 |
(t |
| 503 |
(princ "Current element can not end here\n"))) |
| 504 |
;;; (let ((s (sgml-tree-shortmap sgml-current-tree))) |
| 505 |
;;; (when s |
| 506 |
;;; (princ (format "Current shortref map: %s\n" s)))) |
| 507 |
(princ "Valid start-tags\n") |
| 508 |
(sgml-print-valid-tags "In current element:" |
| 509 |
sgml-current-tree sgml-current-state)))) |
| 510 |
|
| 511 |
(defun sgml-print-valid-tags (prompt tree state &optional exclude omitted-stag) |
| 512 |
(if (not (sgml-model-group-p state)) |
| 513 |
(princ (format "%s (in %s)\n" prompt state)) |
| 514 |
(let* ((req (sgml-required-tokens state)) |
| 515 |
(elems (nconc req |
| 516 |
(delq sgml-pcdata-token |
| 517 |
(sgml-optional-tokens state)))) |
| 518 |
(in (sgml-tree-includes tree)) |
| 519 |
(ex (append exclude (sgml-tree-excludes tree)))) |
| 520 |
;; Modify for exceptions |
| 521 |
(while in |
| 522 |
(unless (memq (car in) elems) |
| 523 |
(setq elems (nconc elems (list (car in))))) |
| 524 |
(setq in (cdr in))) |
| 525 |
(while ex |
| 526 |
(setq elems (delq (car ex) elems)) |
| 527 |
(setq ex (cdr ex))) |
| 528 |
;; |
| 529 |
(setq elems (sort elems (function string-lessp))) |
| 530 |
(sgml-print-list-of-tags prompt elems) |
| 531 |
;; Check for omissable start-tags |
| 532 |
(when (and req (null (cdr req))) |
| 533 |
;; *** Assumes tokens are eltypes |
| 534 |
(let ((el (sgml-fake-open-element tree (car req)))) |
| 535 |
(when (sgml-element-stag-optional el) |
| 536 |
(sgml-print-valid-tags |
| 537 |
(format "If omitting %s:" (sgml-start-tag-of el)) |
| 538 |
el |
| 539 |
(sgml-element-model el) |
| 540 |
(append exclude elems) |
| 541 |
'omitted-stag)))) |
| 542 |
;; Check for omissable end-tag |
| 543 |
(when (and (not omitted-stag) |
| 544 |
(sgml-final-p state) |
| 545 |
(sgml-element-etag-optional tree)) |
| 546 |
(sgml-print-valid-tags |
| 547 |
(format "If omitting %s:" (sgml-end-tag-of tree)) |
| 548 |
(sgml-element-parent tree) |
| 549 |
(sgml-element-pstate tree) |
| 550 |
(append exclude elems)))))) |
| 551 |
|
| 552 |
(defun sgml-print-list-of-tags (prompt list) |
| 553 |
(when list |
| 554 |
(princ prompt) |
| 555 |
(let ((col (length prompt)) |
| 556 |
(w (1- (frame-width)))) |
| 557 |
(loop for e in list |
| 558 |
as str = (sgml-start-tag-of e) |
| 559 |
do |
| 560 |
(setq col (+ col (length str) 2)) |
| 561 |
(cond ((>= col w) |
| 562 |
(setq col (+ (length str) 2)) |
| 563 |
(terpri))) |
| 564 |
(princ " ") |
| 565 |
(princ str)) |
| 566 |
(terpri)))) |
| 567 |
|
| 568 |
|
| 569 |
(defun sgml-show-context-standard (el &optional markup-type) |
| 570 |
(let* ((model (sgml-element-model el))) |
| 571 |
(format "%s %s" |
| 572 |
(cond (markup-type (format "%s" markup-type)) |
| 573 |
((sgml-element-mixed el) |
| 574 |
"#PCDATA") |
| 575 |
((not (sgml-model-group-p model)) |
| 576 |
model) |
| 577 |
(t "")) |
| 578 |
(if (eq el sgml-top-tree) |
| 579 |
"in empty context" |
| 580 |
(sgml-element-context-string el))))) |
| 581 |
|
| 582 |
|
| 583 |
(defun sgml-show-context-backslash (el &optional markup-type) |
| 584 |
(let ((gis nil)) |
| 585 |
(while (not (sgml-off-top-p el)) |
| 586 |
(push (sgml-element-gi el) gis) |
| 587 |
(setq el (sgml-element-parent el))) |
| 588 |
(mapconcat #'sgml-general-insert-case gis "\\"))) |
| 589 |
|
| 590 |
|
| 591 |
(defun sgml-show-context (&optional element) |
| 592 |
"Display where the cursor is in the element hierarchy." |
| 593 |
(interactive) |
| 594 |
(message "%s" (funcall sgml-show-context-function |
| 595 |
(or element (sgml-last-element)) |
| 596 |
(if element nil sgml-markup-type)))) |
| 597 |
|
| 598 |
|
| 599 |
(defun sgml-what-element () |
| 600 |
"Display what element is under the cursor." |
| 601 |
(interactive) |
| 602 |
(let* ((pos (point)) |
| 603 |
(nobol (eq (point) sgml-rs-ignore-pos)) |
| 604 |
(sref (and sgml-current-shortmap |
| 605 |
(sgml-deref-shortmap sgml-current-shortmap nobol))) |
| 606 |
(el nil)) |
| 607 |
(goto-char pos) |
| 608 |
(setq el (sgml-find-element-of pos)) |
| 609 |
(assert (not (null el))) |
| 610 |
(message "%s %s" |
| 611 |
(cond ((eq el sgml-top-tree) |
| 612 |
"outside document element") |
| 613 |
((< (point) (sgml-element-stag-end el)) |
| 614 |
"start-tag") |
| 615 |
((>= (point) (sgml-element-etag-start el)) |
| 616 |
"end-tag") |
| 617 |
(sref |
| 618 |
"shortref") |
| 619 |
(t |
| 620 |
"content")) |
| 621 |
(sgml-element-context-string el)))) |
| 622 |
|
| 623 |
;;;; SGML mode: keyboard inserting |
| 624 |
|
| 625 |
(defun sgml-coerce-element-type (obj) |
| 626 |
(when (stringp obj) |
| 627 |
(setq obj (sgml-lookup-eltype (sgml-general-case obj)))) |
| 628 |
(when nil ;FIXME: need predicate |
| 629 |
(setq obj (sgml-tree-eltype obj))) |
| 630 |
obj) |
| 631 |
|
| 632 |
(defun sgml-break-brefore-stag-p (element) |
| 633 |
(sgml-eltype-appdata (sgml-coerce-element-type element) |
| 634 |
'break-brefore-stag)) |
| 635 |
|
| 636 |
(defun sgml-break-after-stag-p (element) |
| 637 |
(sgml-eltype-appdata (sgml-coerce-element-type element) |
| 638 |
'break-after-stag)) |
| 639 |
|
| 640 |
(defun sgml-insert-break () |
| 641 |
(skip-chars-backward " \t") |
| 642 |
(cond ((bolp) |
| 643 |
(if (looking-at "^\\s-*$") |
| 644 |
(fixup-whitespace))) |
| 645 |
(t |
| 646 |
;; FIXME: fixup-whitespace ?? |
| 647 |
(insert "\n")))) |
| 648 |
|
| 649 |
|
| 650 |
(defun sgml-insert-tag (tag &optional silent no-nl-after) |
| 651 |
"Insert a tag, reading tag name in minibuffer with completion. |
| 652 |
If sgml-leave-point-after-insert is t, the point is left after the |
| 653 |
inserted tag(s), unless the element has some required content. If |
| 654 |
sgml-leave-point-after-insert is nil the point is left after the first |
| 655 |
tag inserted." |
| 656 |
(interactive |
| 657 |
(list |
| 658 |
(let ((completion-ignore-case sgml-namecase-general)) |
| 659 |
(completing-read "Tag: " (sgml-completion-table) nil t "<" )))) |
| 660 |
(sgml-find-context-of (point)) |
| 661 |
(assert (null sgml-markup-type)) |
| 662 |
;; Fix white-space before tag |
| 663 |
(unless (sgml-element-data-p (sgml-parse-to-here)) |
| 664 |
(skip-chars-backward " \t") |
| 665 |
(cond ((bolp) |
| 666 |
(if (looking-at "^\\s-*$") |
| 667 |
(fixup-whitespace))) |
| 668 |
(t |
| 669 |
(insert "\n")))) |
| 670 |
(insert tag) |
| 671 |
(sgml-indent-line) |
| 672 |
(unless no-nl-after |
| 673 |
(save-excursion |
| 674 |
(unless (sgml-element-data-p (sgml-parse-to-here)) |
| 675 |
(unless (eolp) |
| 676 |
(save-excursion (insert "\n")))))) |
| 677 |
(or silent (sgml-show-context))) |
| 678 |
|
| 679 |
(defvar sgml-new-attribute-list-function |
| 680 |
(function sgml-default-asl)) |
| 681 |
|
| 682 |
(defun sgml-insert-element (name &optional after silent) |
| 683 |
"Reads element name from minibuffer and inserts start and end tags. |
| 684 |
If sgml-leave-point-after-insert is t, the point |
| 685 |
is left after the inserted tag(s), unless the element has some required |
| 686 |
content. If sgml-leave-point-after-insert is nil the point is left |
| 687 |
after the first tag inserted." |
| 688 |
(interactive (list (sgml-read-element-name "Element: ") |
| 689 |
sgml-leave-point-after-insert)) |
| 690 |
(let (newpos ; position to leave cursor at |
| 691 |
element ; inserted element |
| 692 |
(sgml-show-warnings nil)) |
| 693 |
(when (and name (not (equal name ""))) |
| 694 |
(when (sgml-break-brefore-stag-p name) |
| 695 |
(sgml-insert-break)) |
| 696 |
(sgml-insert-tag (sgml-start-tag-of name) 'silent) |
| 697 |
(if (and sgml-xml-p (sgml-check-empty name)) |
| 698 |
(forward-char -2) |
| 699 |
(forward-char -1)) |
| 700 |
(setq element (sgml-find-element-of (point))) |
| 701 |
(sgml-insert-attributes (funcall sgml-new-attribute-list-function |
| 702 |
element) |
| 703 |
(sgml-element-attlist element)) |
| 704 |
;; Get element with new attributes |
| 705 |
(setq element (sgml-find-context-of (point))) |
| 706 |
(if (and sgml-xml-p (sgml-check-empty name)) |
| 707 |
(forward-char 2) |
| 708 |
(forward-char 1)) |
| 709 |
(when (sgml-break-after-stag-p name) |
| 710 |
(sgml-insert-break)) |
| 711 |
(when (not (sgml-element-empty element)) |
| 712 |
(when (and sgml-auto-insert-required-elements |
| 713 |
(sgml-model-group-p sgml-current-state)) |
| 714 |
(let (tem) |
| 715 |
(while (and (setq tem (sgml-required-tokens sgml-current-state)) |
| 716 |
(null (cdr tem))) |
| 717 |
(setq tem (sgml-insert-element (car tem) t t)) |
| 718 |
(setq newpos (or newpos tem)) |
| 719 |
(sgml-parse-to-here)) |
| 720 |
(when tem ; more than one req elem |
| 721 |
(insert "\n") |
| 722 |
(when sgml-insert-missing-element-comment |
| 723 |
(insert (format "<!-- one of %s -->" tem)) |
| 724 |
(sgml-indent-line))))) |
| 725 |
(setq newpos (or newpos (point))) |
| 726 |
(when sgml-insert-end-tag-on-new-line |
| 727 |
(insert "\n")) |
| 728 |
(sgml-insert-tag (sgml-end-tag-of name) 'silent) |
| 729 |
(unless after |
| 730 |
(goto-char newpos)) |
| 731 |
(unless silent (sgml-show-context))) |
| 732 |
newpos))) |
| 733 |
|
| 734 |
(defun sgml-default-asl (element) |
| 735 |
(loop for attdecl in (sgml-element-attlist element) |
| 736 |
when (sgml-default-value-type-p (sgml-attdecl-default-value attdecl) |
| 737 |
'REQUIRED) |
| 738 |
collect |
| 739 |
(sgml-make-attspec |
| 740 |
(sgml-attdecl-name attdecl) |
| 741 |
(sgml-read-attribute-value attdecl (sgml-element-name element) nil)))) |
| 742 |
|
| 743 |
(defun sgml-tag-region (element start end) |
| 744 |
"Reads element name from minibuffer and inserts start and end tags." |
| 745 |
(interactive |
| 746 |
(list |
| 747 |
(save-excursion (goto-char (region-beginning)) |
| 748 |
(sgml-read-element-name "Tag region with element: ")) |
| 749 |
(region-beginning) |
| 750 |
(region-end))) |
| 751 |
(save-excursion |
| 752 |
(when (and element (not (equal element ""))) |
| 753 |
(goto-char end) |
| 754 |
(insert (sgml-end-tag-of element)) |
| 755 |
(goto-char start) |
| 756 |
(sgml-insert-tag (sgml-start-tag-of element))))) |
| 757 |
|
| 758 |
(defun sgml-insert-attributes (avl attlist) |
| 759 |
"Insert the attributes with values AVL and declarations ATTLIST. |
| 760 |
AVL should be a assoc list mapping symbols to strings." |
| 761 |
(let (name val dcl def) |
| 762 |
(loop for attspec in attlist do |
| 763 |
(setq name (sgml-attspec-name attspec) |
| 764 |
val (cdr-safe (sgml-lookup-attspec name avl)) |
| 765 |
dcl (sgml-attdecl-declared-value attspec) |
| 766 |
def (sgml-attdecl-default-value attspec)) |
| 767 |
(setq name (sgml-general-insert-case name)) |
| 768 |
(unless val ; no value given |
| 769 |
;; Supply the default value if a value is needed |
| 770 |
(cond ((sgml-default-value-type-p 'REQUIRED def) |
| 771 |
(setq val "")) |
| 772 |
((and (or (not (or sgml-xml-p sgml-omittag sgml-shorttag)) |
| 773 |
sgml-insert-defaulted-attributes) |
| 774 |
(consp def)) |
| 775 |
(setq val (sgml-default-value-attval def))))) |
| 776 |
(when val |
| 777 |
(cond ((eq dcl 'CDATA)) |
| 778 |
((eq dcl 'ENTITY) (setq val (sgml-entity-insert-case val))) |
| 779 |
(t (setq val (sgml-general-insert-case val))))) |
| 780 |
(cond |
| 781 |
((null val)) ; Ignore |
| 782 |
;; Ignore attributes with default value |
| 783 |
((and (consp def) |
| 784 |
(eq sgml-minimize-attributes 'max) |
| 785 |
(or sgml-omittag sgml-shorttag) |
| 786 |
(equal val (sgml-default-value-attval def)))) |
| 787 |
;; No attribute name for token groups |
| 788 |
((and sgml-minimize-attributes sgml-shorttag |
| 789 |
(member (sgml-general-case val) |
| 790 |
(sgml-declared-value-token-group dcl))) |
| 791 |
(insert " " val)) |
| 792 |
(t |
| 793 |
(insert " " name "=" (sgml-quote-attribute-value val))))) |
| 794 |
(when auto-fill-function |
| 795 |
(funcall auto-fill-function)))) |
| 796 |
|
| 797 |
|
| 798 |
(defun sgml-quote-attribute-value (value) |
| 799 |
"Add quotes to the string VALUE unless minimization is on." |
| 800 |
(let ((quote "")) |
| 801 |
(cond ((and (not sgml-always-quote-attributes) |
| 802 |
sgml-shorttag |
| 803 |
(string-match "\\`[-.A-Za-z0-9]+\\'" value)) |
| 804 |
) ; no need to quote |
| 805 |
((not (string-match "\"" value)) ; can use "" quotes |
| 806 |
(setq quote "\"")) |
| 807 |
(t ; use '' quotes |
| 808 |
(setq quote "'"))) |
| 809 |
(concat quote value quote))) |
| 810 |
|
| 811 |
(defun sgml-completion-table (&optional avoid-tags-in-cdata) |
| 812 |
(sgml-parse-to-here) |
| 813 |
(when sgml-markup-type |
| 814 |
(error "No tags allowed")) |
| 815 |
(cond ((or (sgml-model-group-p sgml-current-state) |
| 816 |
(eq sgml-current-state sgml-any)) |
| 817 |
(append |
| 818 |
(mapcar (function (lambda (x) (cons (sgml-end-tag-of x) x))) |
| 819 |
(sgml-current-list-of-endable-eltypes)) |
| 820 |
(mapcar (function (lambda (x) (cons (sgml-start-tag-of x) x))) |
| 821 |
(sgml-current-list-of-valid-eltypes)))) |
| 822 |
(t |
| 823 |
(sgml-message "%s" sgml-current-state) |
| 824 |
nil))) |
| 825 |
|
| 826 |
(defun sgml-element-endable-p () |
| 827 |
(sgml-parse-to-here) |
| 828 |
(and (not (eq sgml-current-tree sgml-top-tree)) |
| 829 |
(sgml-final-p sgml-current-state))) |
| 830 |
|
| 831 |
(defun sgml-insert-end-tag () |
| 832 |
"Insert end-tag for the current open element." |
| 833 |
(interactive "*") |
| 834 |
(sgml-parse-to-here) |
| 835 |
(cond |
| 836 |
((eq sgml-current-tree sgml-top-tree) |
| 837 |
(sgml-error "No open element")) |
| 838 |
((not (sgml-final-p sgml-current-state)) |
| 839 |
(sgml-error "Can`t end element here")) |
| 840 |
(t |
| 841 |
(when (and sgml-indent-step |
| 842 |
(not (sgml-element-data-p sgml-current-tree))) |
| 843 |
(delete-horizontal-space) |
| 844 |
(unless (bolp) |
| 845 |
(insert "\n"))) |
| 846 |
(when (prog1 (bolp) |
| 847 |
(insert (if (eq t (sgml-element-net-enabled sgml-current-tree)) |
| 848 |
"/" |
| 849 |
(sgml-end-tag-of sgml-current-tree)))) |
| 850 |
(sgml-indent-line))))) |
| 851 |
|
| 852 |
(defun sgml-insert-start-tag (name asl attlist &optional net) |
| 853 |
;; Insert a start-tag with attributes |
| 854 |
;; if NET is true end with NESTC unless XML then end with NESTC NET |
| 855 |
;; (aka XML-TAGCE). |
| 856 |
(insert (sgml-delim "STAGO") (sgml-general-insert-case name)) |
| 857 |
(sgml-insert-attributes asl attlist) |
| 858 |
;; In XML, force net if element is always empty |
| 859 |
(when (and sgml-xml-p (sgml-check-empty name)) |
| 860 |
(setq net t)) |
| 861 |
(insert (if net (if sgml-xml-p |
| 862 |
(sgml-delim "XML-TAGCE") |
| 863 |
(sgml-delim "NESTC")) |
| 864 |
(sgml-delim "TAGC")))) |
| 865 |
|
| 866 |
(defun sgml-change-start-tag (element asl) |
| 867 |
(let ((name (sgml-element-gi element)) |
| 868 |
(attlist (sgml-element-attlist element))) |
| 869 |
;; Concoct an attribute specification list using the names of the |
| 870 |
;; existing attributes and those ot be changed. |
| 871 |
(when (and (not attlist) sgml-dtd-less) |
| 872 |
(dolist (elt (mapcar 'car asl)) |
| 873 |
(unless (assoc elt attlist) ; avoid duplicates |
| 874 |
(push (sgml-make-attdecl elt 'CDATA 'REQUIRED) attlist))) |
| 875 |
(setq attlist (nreverse attlist))) |
| 876 |
(assert (sgml-bpos-p (sgml-element-stag-epos element))) |
| 877 |
(goto-char (sgml-element-start element)) |
| 878 |
(delete-char (sgml-element-stag-len element)) |
| 879 |
(sgml-insert-start-tag name asl attlist |
| 880 |
(if sgml-xml-p |
| 881 |
(sgml-element-empty element) |
| 882 |
(eq t (sgml-element-net-enabled element)))))) |
| 883 |
|
| 884 |
(defun sgml-read-attribute-value (attdecl element curvalue) |
| 885 |
"Return the attribute value read from user. |
| 886 |
ATTDECL is the attribute declaration for the attribute to read. |
| 887 |
CURVALUE is nil or a string that will be used as default value." |
| 888 |
(assert attdecl) |
| 889 |
(let* ((name (sgml-attdecl-name attdecl)) |
| 890 |
(dv (sgml-attdecl-declared-value attdecl)) |
| 891 |
(tokens (sgml-declared-value-token-group dv)) |
| 892 |
(notations (sgml-declared-value-notation dv)) |
| 893 |
; JDF's addition |
| 894 |
(ids (and (memq dv '(IDREF IDREFS)) (sgml-id-list))) |
| 895 |
(type (cond (tokens "token") |
| 896 |
(notations "NOTATION") |
| 897 |
(t (symbol-name dv)))) |
| 898 |
(prompt |
| 899 |
(format "Value for %s in %s (%s%s): " |
| 900 |
name element type |
| 901 |
(if (and curvalue (not (eq dv 'IDREFS))) |
| 902 |
(format " Default: %s" curvalue) |
| 903 |
""))) |
| 904 |
value) |
| 905 |
(setq value |
| 906 |
(cond ((or tokens notations) |
| 907 |
(let ((completion-ignore-case sgml-namecase-general)) |
| 908 |
(completing-read prompt |
| 909 |
(mapcar 'list (or tokens notations)) |
| 910 |
nil t))) |
| 911 |
(ids |
| 912 |
(let ((completion-ignore-case sgml-namecase-general) |
| 913 |
(minibuffer-local-completion-map sgml-edit-idrefs-map)) |
| 914 |
(completing-read prompt |
| 915 |
'sgml-idrefs-completer |
| 916 |
nil nil |
| 917 |
(and curvalue |
| 918 |
(cons curvalue (length curvalue)))))) |
| 919 |
(t |
| 920 |
(read-string prompt)))) |
| 921 |
(if (and curvalue (equal value "")) |
| 922 |
curvalue value))) |
| 923 |
|
| 924 |
(defun sgml-idrefs-completer (fullstring pred action) |
| 925 |
(let* ((start (string-match "\\(\\(:?-\\|\\w\\)*\\)$" fullstring)) |
| 926 |
(string (match-string 0 fullstring)) |
| 927 |
(prefix (substring fullstring 0 start))) |
| 928 |
;(message "prefix: %s string: %s" prefix string) |
| 929 |
(cond ((null action) |
| 930 |
(let ((completion (try-completion string (sgml-id-alist) pred))) |
| 931 |
(if (eq completion t) |
| 932 |
t |
| 933 |
(concat prefix completion)))) |
| 934 |
((eq action t) |
| 935 |
(all-completions string (sgml-id-alist) pred)) |
| 936 |
((eq action 'lambda) |
| 937 |
(member string (sgml-id-alist)))))) |
| 938 |
|
| 939 |
(defun sgml-non-fixed-attributes (attlist) |
| 940 |
(loop for attdecl in attlist |
| 941 |
unless (sgml-default-value-type-p 'FIXED |
| 942 |
(sgml-attdecl-default-value attdecl)) |
| 943 |
collect attdecl)) |
| 944 |
|
| 945 |
(defun sgml-insert-attribute (name value) |
| 946 |
"Read attribute name and value from minibuffer and insert attribute spec." |
| 947 |
(interactive |
| 948 |
(let* ((el (sgml-find-attribute-element)) |
| 949 |
(name |
| 950 |
(sgml-general-case |
| 951 |
(let ((completion-ignore-case sgml-namecase-general)) |
| 952 |
(completing-read |
| 953 |
"Attribute name: " |
| 954 |
(mapcar |
| 955 |
(function (lambda (a) (list (sgml-attdecl-name a)))) |
| 956 |
(if sgml-dtd-less |
| 957 |
(sgml-tree-asl el) |
| 958 |
(sgml-non-fixed-attributes (sgml-element-attlist el)))) |
| 959 |
nil (not sgml-dtd-less)))))) |
| 960 |
(list name |
| 961 |
(sgml-read-attribute-value |
| 962 |
(if sgml-dtd-less |
| 963 |
(list name) |
| 964 |
(sgml-lookup-attdecl name (sgml-element-attlist el))) |
| 965 |
(sgml-element-name el) |
| 966 |
(sgml-element-attval el name))))) |
| 967 |
;; Body |
| 968 |
(assert (stringp name)) |
| 969 |
(assert (or (null value) (stringp value))) |
| 970 |
(let* ((el (sgml-find-attribute-element)) |
| 971 |
(asl (cons (sgml-make-attspec name value) |
| 972 |
(sgml-element-attribute-specification-list el))) |
| 973 |
(in-tag (< (point) (sgml-element-stag-end el)))) |
| 974 |
(sgml-change-start-tag el asl) |
| 975 |
(when in-tag (forward-char -1)))) |
| 976 |
|
| 977 |
(defun sgml-split-element () |
| 978 |
"Split the current element at point. |
| 979 |
If repeated, the containing element will be split before the beginning |
| 980 |
of then current element." |
| 981 |
(interactive "*") |
| 982 |
(setq sgml-split-level |
| 983 |
(if (eq this-command last-command) |
| 984 |
(1+ sgml-split-level) |
| 985 |
0)) |
| 986 |
(let ((u (sgml-find-context-of (point))) |
| 987 |
(start (point-marker))) |
| 988 |
(loop repeat sgml-split-level do |
| 989 |
(goto-char (sgml-element-start u)) |
| 990 |
(setq u (sgml-element-parent u))) |
| 991 |
;; Verify that a new element can be started |
| 992 |
(unless (and (sgml-element-pstate u) ; in case of top element |
| 993 |
(sgml-get-move (sgml-element-pstate u) |
| 994 |
(sgml-element-name u))) |
| 995 |
|
| 996 |
(sgml-error "The %s element can't be split" |
| 997 |
(sgml-element-name u))) |
| 998 |
;; Do the split |
| 999 |
(sgml-insert-end-tag) |
| 1000 |
(insert ?\n) |
| 1001 |
(sgml-insert-tag (sgml-start-tag-of u) 'silent) |
| 1002 |
(skip-chars-forward " \t\n") |
| 1003 |
(sgml-indent-line) |
| 1004 |
(when (> sgml-split-level 0) |
| 1005 |
(goto-char start)) |
| 1006 |
(or (eq sgml-top-tree |
| 1007 |
(setq u (sgml-element-parent u))) |
| 1008 |
(sgml-message |
| 1009 |
"Repeat the command to split the containing %s element" |
| 1010 |
(sgml-element-name u))))) |
| 1011 |
|
| 1012 |
;;; David Megginson's custom menus for keys |
| 1013 |
|
| 1014 |
(defun sgml-custom-dtd (doctype) |
| 1015 |
"Insert a DTD declaration from the sgml-custom-dtd alist." |
| 1016 |
(interactive |
| 1017 |
(list (completing-read "Insert DTD: " sgml-custom-dtd nil t))) |
| 1018 |
(let ((entry (assoc doctype sgml-custom-dtd))) |
| 1019 |
(sgml-doctype-insert (second entry) (cddr entry)))) |
| 1020 |
|
| 1021 |
(defun sgml-custom-markup (markup) |
| 1022 |
"Insert markup from the sgml-custom-markup alist." |
| 1023 |
(interactive |
| 1024 |
(let ((completion-ignore-case sgml-namecase-general)) |
| 1025 |
(list (completing-read "Insert Markup: " sgml-custom-markup nil t)))) |
| 1026 |
(sgml-insert-markup (cadr (assoc markup sgml-custom-markup)))) |
| 1027 |
|
| 1028 |
|
| 1029 |
;;;; SGML mode: Menu inserting |
| 1030 |
|
| 1031 |
(defun sgml-tags-menu (event) |
| 1032 |
"Pop up a menu with valid tags and insert the chosen tag. |
| 1033 |
If the variable sgml-balanced-tag-edit is t, also inserts the |
| 1034 |
corresponding end tag. If sgml-leave-point-after-insert is t, the point |
| 1035 |
is left after the inserted tag(s), unless the element has some required |
| 1036 |
content. If sgml-leave-point-after-insert is nil the point is left |
| 1037 |
after the first tag inserted." |
| 1038 |
(interactive "*e") |
| 1039 |
(let ((end (sgml-mouse-region))) |
| 1040 |
(sgml-parse-to-here) |
| 1041 |
(cond |
| 1042 |
((eq sgml-markup-type 'start-tag) |
| 1043 |
(sgml-attrib-menu event)) |
| 1044 |
(t |
| 1045 |
(let ((what |
| 1046 |
(sgml-menu-ask event (if (or end sgml-balanced-tag-edit) |
| 1047 |
'element 'tags)))) |
| 1048 |
(cond |
| 1049 |
((null what)) |
| 1050 |
(end |
| 1051 |
(sgml-tag-region what (point) end)) |
| 1052 |
(sgml-balanced-tag-edit |
| 1053 |
(sgml-insert-element what)) |
| 1054 |
(t |
| 1055 |
(sgml-insert-tag what)))))))) |
| 1056 |
|
| 1057 |
(defun sgml-element-menu (event) |
| 1058 |
"Pop up a menu with valid elements and insert choice. |
| 1059 |
If sgml-leave-point-after-insert is nil the point is left after the first |
| 1060 |
tag inserted." |
| 1061 |
(interactive "*e") |
| 1062 |
(let ((what (sgml-menu-ask event 'element))) |
| 1063 |
(and what (sgml-insert-element what)))) |
| 1064 |
|
| 1065 |
(defun sgml-add-element-menu (event) |
| 1066 |
(interactive "*e") |
| 1067 |
(let ((what (sgml-menu-ask event 'add-element))) |
| 1068 |
(and what (sgml-add-element-to-element what nil)))) |
| 1069 |
|
| 1070 |
(defun sgml-start-tag-menu (event) |
| 1071 |
"Pop up a menu with valid start-tags and insert choice." |
| 1072 |
(interactive "*e") |
| 1073 |
(let ((what (sgml-menu-ask event 'start-tag))) |
| 1074 |
(and what (sgml-insert-tag what)))) |
| 1075 |
|
| 1076 |
(defun sgml-end-tag-menu (event) |
| 1077 |
"Pop up a menu with valid end-tags and insert choice." |
| 1078 |
(interactive "*e") |
| 1079 |
(let ((what (sgml-menu-ask event 'end-tag))) |
| 1080 |
(and what (sgml-insert-tag what)))) |
| 1081 |
|
| 1082 |
(defun sgml-tag-region-menu (event) |
| 1083 |
"Pop up a menu with valid elements and tag current region with the choice." |
| 1084 |
(interactive "*e") |
| 1085 |
(let ((what (sgml-menu-ask event 'element))) |
| 1086 |
(and what (sgml-tag-region what |
| 1087 |
(region-beginning) |
| 1088 |
(region-end))))) |
| 1089 |
|
| 1090 |
(defun sgml-menu-ask (event type) |
| 1091 |
(sgml-parse-to-here) |
| 1092 |
(let (tab |
| 1093 |
(title (capitalize (symbol-name type)))) |
| 1094 |
(cond |
| 1095 |
((eq type 'add-element) |
| 1096 |
(setq tab |
| 1097 |
(mapcar #'sgml-eltype-name |
| 1098 |
(sgml--all-possible-elements |
| 1099 |
(sgml-find-context-of (point)))))) |
| 1100 |
(sgml-markup-type) |
| 1101 |
((eq type 'element) |
| 1102 |
(setq tab |
| 1103 |
(mapcar (function symbol-name) |
| 1104 |
(sgml-current-list-of-valid-eltypes)))) |
| 1105 |
(t |
| 1106 |
(unless (eq type 'start-tag) |
| 1107 |
(setq tab |
| 1108 |
(mapcar (function sgml-end-tag-of) |
| 1109 |
(sgml-current-list-of-endable-eltypes)))) |
| 1110 |
(unless (eq type 'end-tag) |
| 1111 |
(setq tab |
| 1112 |
(nconc tab |
| 1113 |
(mapcar (function sgml-start-tag-of) |
| 1114 |
(sgml-current-list-of-valid-eltypes))))))) |
| 1115 |
(if sgml-dtd-less |
| 1116 |
;; The best we can do is assemble a list of elements we've |
| 1117 |
;; seen so far. |
| 1118 |
(dolist (n (append (sgml-dtd-eltypes sgml-dtd-info) '()) |
| 1119 |
;; Space avoids possible clash with valid element. |
| 1120 |
(setq tab (cons "Any " (cons "--" tab)))) |
| 1121 |
(when (and (symbolp n) (not (memq n tab))) |
| 1122 |
(push (symbol-name n) tab)))) |
| 1123 |
(or tab |
| 1124 |
(error "No valid %s at this point" type)) |
| 1125 |
(let ((elt (sgml-popup-menu event |
| 1126 |
title |
| 1127 |
(mapcar (function (lambda (x) (cons x x))) |
| 1128 |
tab)))) |
| 1129 |
(if (equal elt "Any ") |
| 1130 |
(setq elt (sgml-read-element-name "Element: "))) |
| 1131 |
(or elt (message nil))))) |
| 1132 |
|
| 1133 |
(defun sgml-entities-menu (event) |
| 1134 |
(interactive "*e") |
| 1135 |
(sgml-need-dtd) |
| 1136 |
(let ((menu |
| 1137 |
(mapcar (function (lambda (x) (cons x x))) |
| 1138 |
(sort (sgml-map-entities (function sgml-entity-name) |
| 1139 |
(sgml-dtd-entities sgml-dtd-info) |
| 1140 |
t) |
| 1141 |
(function string-lessp)))) |
| 1142 |
choice) |
| 1143 |
(unless menu |
| 1144 |
(error "No entities defined")) |
| 1145 |
(setq choice (sgml-popup-menu event "Entities" menu)) |
| 1146 |
(when choice |
| 1147 |
(insert "&" choice ";")))) |
| 1148 |
|
| 1149 |
(defun sgml-doctype-insert (doctype vars) |
| 1150 |
"Insert string DOCTYPE (ignored if nil) and set variables in &rest VARS. |
| 1151 |
VARS should be a list of variables and values. |
| 1152 |
For backward compatibility a single string instead of a variable is |
| 1153 |
assigned to sgml-default-dtd-file. |
| 1154 |
All variables are made buffer local and are also added to the |
| 1155 |
buffers local variables list." |
| 1156 |
(when doctype |
| 1157 |
(unless (bolp) |
| 1158 |
(insert "\n")) |
| 1159 |
(unless (eolp) |
| 1160 |
(insert "\n") |
| 1161 |
(forward-char -1)) |
| 1162 |
(sgml-insert-markup doctype)) |
| 1163 |
(while vars |
| 1164 |
(cond ((stringp (car vars)) |
| 1165 |
(sgml-set-local-variable 'sgml-default-dtd-file (car vars)) |
| 1166 |
(setq vars (cdr vars))) |
| 1167 |
((car vars) ; Avoid nil |
| 1168 |
(sgml-set-local-variable (car vars) (cadr vars)) |
| 1169 |
(setq vars (cddr vars))) |
| 1170 |
(t |
| 1171 |
(setq vars (cddr vars))))) |
| 1172 |
(setq sgml-top-tree nil)) |
| 1173 |
|
| 1174 |
(defun sgml-attrib-menu (event) |
| 1175 |
"Pop up a menu of the attributes of the current element |
| 1176 |
\(or the element with start-tag before point)." |
| 1177 |
(interactive "e") |
| 1178 |
(let ((menu (sgml-make-attrib-menu (sgml-find-attribute-element)))) |
| 1179 |
(sgml-popup-multi-menu event "Attributes" menu))) |
| 1180 |
|
| 1181 |
(defun sgml-make-attrib-menu (el) |
| 1182 |
(let ((attlist (sgml-non-fixed-attributes (sgml-element-attlist el)))) |
| 1183 |
(if (and (not attlist) sgml-dtd-less) |
| 1184 |
(let ((name |
| 1185 |
(sgml-general-case |
| 1186 |
(let ((completion-ignore-case sgml-namecase-general)) |
| 1187 |
(completing-read |
| 1188 |
"Attribute name: " |
| 1189 |
(mapcar |
| 1190 |
(lambda (a) (list (sgml-attdecl-name a))) |
| 1191 |
(if sgml-dtd-less |
| 1192 |
(sgml-tree-asl el) |
| 1193 |
(sgml-non-fixed-attributes (sgml-element-attlist el)))) |
| 1194 |
nil (not sgml-dtd-less)))))) |
| 1195 |
(if name |
| 1196 |
(setq attlist (list (sgml-make-attdecl name 'CDATA nil)))))) |
| 1197 |
(or attlist |
| 1198 |
(error "No non-fixed attributes for element")) |
| 1199 |
(loop for attdecl in attlist |
| 1200 |
for name = (sgml-attdecl-name attdecl) |
| 1201 |
for defval = (sgml-attdecl-default-value attdecl) |
| 1202 |
for tokens = (or (sgml-declared-value-token-group |
| 1203 |
(sgml-attdecl-declared-value attdecl)) |
| 1204 |
(sgml-declared-value-notation |
| 1205 |
(sgml-attdecl-declared-value attdecl))) |
| 1206 |
collect |
| 1207 |
(cons |
| 1208 |
(sgml-attdecl-name attdecl) |
| 1209 |
(nconc |
| 1210 |
(if tokens |
| 1211 |
(loop for val in tokens collect |
| 1212 |
(list val |
| 1213 |
(list 'sgml-insert-attribute name val))) |
| 1214 |
(list |
| 1215 |
(list "Set attribute value" |
| 1216 |
(list 'sgml-insert-attribute |
| 1217 |
(sgml-attdecl-name attdecl) |
| 1218 |
(list 'sgml-read-attribute-value |
| 1219 |
(list 'quote attdecl) |
| 1220 |
(list 'quote (sgml-element-name el)) |
| 1221 |
(sgml-element-attval el name)))))) |
| 1222 |
(if (sgml-default-value-type-p 'REQUIRED defval) |
| 1223 |
nil |
| 1224 |
(list "--" |
| 1225 |
(list (if (sgml-default-value-type-p nil defval) |
| 1226 |
(format "Default: %s" |
| 1227 |
(sgml-default-value-attval defval)) |
| 1228 |
"#IMPLIED") |
| 1229 |
(list 'sgml-insert-attribute name nil))))))))) |
| 1230 |
|
| 1231 |
|
| 1232 |
;;;; New Right Button Menu |
| 1233 |
|
| 1234 |
(defun sgml-right-menu (event) |
| 1235 |
"Pop up a menu with valid tags and insert the choosen tag. |
| 1236 |
If the variable sgml-balanced-tag-edit is t, also inserts the |
| 1237 |
corresponding end tag. If sgml-leave-point-after-insert is t, the point |
| 1238 |
is left after the inserted tag(s), unless the element has som required |
| 1239 |
content. If sgml-leave-point-after-insert is nil the point is left |
| 1240 |
after the first tag inserted." |
| 1241 |
(interactive "*e") |
| 1242 |
(let ((end (sgml-mouse-region))) |
| 1243 |
(sgml-parse-to-here) |
| 1244 |
(cond |
| 1245 |
((eq sgml-markup-type 'start-tag) |
| 1246 |
(sgml-right-stag-menu event)) |
| 1247 |
(t |
| 1248 |
(let ((what |
| 1249 |
(sgml-menu-ask event (if (or end sgml-balanced-tag-edit) |
| 1250 |
'element 'tags)))) |
| 1251 |
(cond |
| 1252 |
((null what)) |
| 1253 |
(end |
| 1254 |
(sgml-tag-region what (point) end)) |
| 1255 |
(sgml-balanced-tag-edit |
| 1256 |
(sgml-insert-element what)) |
| 1257 |
(t |
| 1258 |
(sgml-insert-tag what)))))))) |
| 1259 |
|
| 1260 |
|
| 1261 |
(defun sgml-right-stag-menu (event) |
| 1262 |
(let* ((el (sgml-find-attribute-element)) |
| 1263 |
(attrib-menu (ignore-errors (sgml-make-attrib-menu el)))) |
| 1264 |
|
| 1265 |
(let* ((alt-gi (mapcar (function sgml-eltype-name) |
| 1266 |
(progn |
| 1267 |
(sgml-find-context-of (sgml-element-start el)) |
| 1268 |
(sgml-current-list-of-valid-eltypes)))) |
| 1269 |
(change-menu |
| 1270 |
(cons "Change To" |
| 1271 |
(loop for gi in alt-gi |
| 1272 |
collect `(,gi (sgml-change-element-name ,gi)))))) |
| 1273 |
(sgml-popup-multi-menu |
| 1274 |
event "Start Tag" |
| 1275 |
(list* `("Misc" |
| 1276 |
("Edit attributes" (sgml-edit-attributes)) |
| 1277 |
("Normalize" (sgml-normalize-element)) |
| 1278 |
("Fill" (sgml-fill-element |
| 1279 |
(sgml-find-context-of (point)))) |
| 1280 |
("Splice" (sgml-untag-element)) |
| 1281 |
("Fold" (sgml-fold-element))) |
| 1282 |
change-menu |
| 1283 |
;;`("--" "--") |
| 1284 |
attrib-menu))))) |
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
;;;; SGML mode: Fill |
| 1289 |
|
| 1290 |
(defun sgml-element-fillable (element) |
| 1291 |
(and (sgml-element-mixed element) |
| 1292 |
(not (sgml-element-appdata element 'nofill)))) |
| 1293 |
|
| 1294 |
(defun sgml-fill-element (element) |
| 1295 |
"Fill biggest enclosing element with mixed content. |
| 1296 |
If current element has pure element content, recursively fill the |
| 1297 |
subelements." |
| 1298 |
(interactive (list (sgml-find-element-of (point)))) |
| 1299 |
;; |
| 1300 |
(message "Filling...") |
| 1301 |
(when (sgml-element-fillable element) |
| 1302 |
;; Find biggest enclosing fillable element |
| 1303 |
(while (sgml-element-fillable (sgml-element-parent element)) |
| 1304 |
(setq element (sgml-element-parent element)))) |
| 1305 |
;; |
| 1306 |
(sgml-do-fill element) |
| 1307 |
(sgml-message "Done")) |
| 1308 |
|
| 1309 |
(defun sgml-do-fill (element) |
| 1310 |
(when sgml-debug |
| 1311 |
(goto-char (sgml-element-start element)) |
| 1312 |
(sit-for 0)) |
| 1313 |
(save-excursion |
| 1314 |
(cond |
| 1315 |
((sgml-element-fillable element) |
| 1316 |
(let (last-pos |
| 1317 |
(c (sgml-element-content element)) |
| 1318 |
(agenda nil)) ; regions to fill later |
| 1319 |
(goto-char (sgml-element-stag-end element)) |
| 1320 |
(when (eolp) (forward-char 1)) |
| 1321 |
(setq last-pos (point)) |
| 1322 |
(while c |
| 1323 |
(cond |
| 1324 |
((sgml-element-fillable c)) |
| 1325 |
(t |
| 1326 |
;; Put region before element on agenda. Can't fill it now |
| 1327 |
;; that would mangle the parse tree that is being traversed. |
| 1328 |
(push (cons last-pos (sgml-element-start c)) |
| 1329 |
agenda) |
| 1330 |
(goto-char (sgml-element-start c)) |
| 1331 |
(sgml-do-fill c) |
| 1332 |
;; Fill may change parse tree, get a fresh |
| 1333 |
(setq c (sgml-find-element-of (point))) |
| 1334 |
(setq last-pos (sgml-element-end c)))) |
| 1335 |
(setq c (sgml-element-next c))) |
| 1336 |
;; Fill the last region in content of element, |
| 1337 |
;; but get a fresh parse tree, if it has change due to other fills. |
| 1338 |
(goto-char last-pos) |
| 1339 |
(when (bolp) (sgml-indent-line)) |
| 1340 |
(sgml-fill-region last-pos |
| 1341 |
(sgml-element-etag-start |
| 1342 |
(sgml-find-element-of |
| 1343 |
(sgml-element-start element)))) |
| 1344 |
(while agenda |
| 1345 |
(sgml-fill-region (caar agenda) (cdar agenda)) |
| 1346 |
(setq agenda (cdr agenda))))) |
| 1347 |
(t |
| 1348 |
;; If element is not mixed, fill subelements recursively |
| 1349 |
(let ((c (sgml-element-content element))) |
| 1350 |
(while c |
| 1351 |
(goto-char (sgml-element-etag-start c)) |
| 1352 |
(sgml-indent-line) |
| 1353 |
(goto-char (sgml-element-start c)) |
| 1354 |
(sgml-indent-line) |
| 1355 |
(setq c (sgml-find-element-of (point))) |
| 1356 |
(sgml-do-fill c) |
| 1357 |
(setq c (sgml-element-next (sgml-find-element-of (point)))))))))) |
| 1358 |
|
| 1359 |
(defun sgml-fill-region (start end) |
| 1360 |
(sgml-message "Filling...") |
| 1361 |
(save-excursion |
| 1362 |
(goto-char end) |
| 1363 |
(skip-chars-backward " \t\n") |
| 1364 |
(while (progn (beginning-of-line 1) |
| 1365 |
(< start (point))) |
| 1366 |
(delete-char -1) |
| 1367 |
(delete-horizontal-space) |
| 1368 |
(insert " ")) |
| 1369 |
(end-of-line 1) |
| 1370 |
(let (give-up prev-column opoint oopoint) |
| 1371 |
(while (and (not give-up) (> (current-column) fill-column)) |
| 1372 |
(setq prev-column (current-column)) |
| 1373 |
(setq oopoint (point)) |
| 1374 |
(move-to-column (1+ fill-column)) |
| 1375 |
(skip-chars-backward "^ \t\n") |
| 1376 |
(setq opoint (point)) |
| 1377 |
(skip-chars-backward " \t") |
| 1378 |
(if (bolp) |
| 1379 |
(progn |
| 1380 |
(goto-char opoint) |
| 1381 |
(if (re-search-forward "[ \t]" oopoint t) |
| 1382 |
(save-excursion |
| 1383 |
(skip-chars-forward " \t") |
| 1384 |
(setq opoint (point))) |
| 1385 |
(setq give-up t)))) |
| 1386 |
(if (not give-up) |
| 1387 |
(progn |
| 1388 |
(delete-region (point) opoint) |
| 1389 |
(newline) |
| 1390 |
(sgml-indent-line) |
| 1391 |
(end-of-line 1) |
| 1392 |
(setq give-up (>= (current-column) prev-column)))))))) |
| 1393 |
|
| 1394 |
;;;; SGML mode: Attribute editing |
| 1395 |
|
| 1396 |
(defvar sgml-start-attributes nil) |
| 1397 |
(defvar sgml-main-buffer nil) |
| 1398 |
(defvar sgml-attlist nil) |
| 1399 |
|
| 1400 |
(defun sgml-edit-attributes () |
| 1401 |
"Edit attributes of current element. |
| 1402 |
Editing is done in a separate window." |
| 1403 |
(interactive) |
| 1404 |
(let ((element (sgml-find-attribute-element))) |
| 1405 |
(unless (sgml-bpos-p (sgml-element-stag-epos element)) |
| 1406 |
(error "Element's start-tag is not in the buffer")) |
| 1407 |
(push-mark) |
| 1408 |
(goto-char (sgml-element-start element)) |
| 1409 |
(let* ((start (point-marker)) |
| 1410 |
(asl (sgml-element-attribute-specification-list element)) |
| 1411 |
(cb (current-buffer)) |
| 1412 |
(quote sgml-always-quote-attributes) |
| 1413 |
(xml-p sgml-xml-p)) |
| 1414 |
(switch-to-buffer-other-window |
| 1415 |
(sgml-attribute-buffer element asl)) |
| 1416 |
(make-local-variable 'sgml-start-attributes) |
| 1417 |
(setq sgml-start-attributes start) |
| 1418 |
(make-local-variable 'sgml-always-quote-attributes) |
| 1419 |
(setq sgml-always-quote-attributes quote) |
| 1420 |
(make-local-variable 'sgml-main-buffer) |
| 1421 |
(setq sgml-main-buffer cb) |
| 1422 |
(make-local-variable 'sgml-xml-p) |
| 1423 |
(setq sgml-xml-p xml-p)))) |
| 1424 |
|
| 1425 |
|
| 1426 |
(defun sgml-effective-attlist (eltype) |
| 1427 |
(let ((effective-attlist nil) |
| 1428 |
(attlist (sgml-eltype-attlist eltype)) |
| 1429 |
(attnames (or (sgml-eltype-appdata eltype 'attnames) |
| 1430 |
'(*)))) |
| 1431 |
(while (and attnames (not (eq '* (car attnames)))) |
| 1432 |
(let ((attdecl (sgml-lookup-attdecl (car attnames) attlist))) |
| 1433 |
(if attdecl |
| 1434 |
(push attdecl effective-attlist) |
| 1435 |
(message "Attnames specefication error: no %s attribute in %s" |
| 1436 |
(car attnames) eltype))) |
| 1437 |
(setq attnames (cdr attnames))) |
| 1438 |
(when (eq '* (car attnames)) |
| 1439 |
(while attlist |
| 1440 |
(let ((attdecl (sgml-lookup-attdecl (sgml-attdecl-name (car attlist)) |
| 1441 |
effective-attlist))) |
| 1442 |
(unless attdecl |
| 1443 |
(push (car attlist) effective-attlist))) |
| 1444 |
(setq attlist (cdr attlist)))) |
| 1445 |
(nreverse effective-attlist))) |
| 1446 |
|
| 1447 |
|
| 1448 |
(defun sgml-attribute-buffer (element asl) |
| 1449 |
(let ((bname "*Edit attributes*") |
| 1450 |
(buf nil) |
| 1451 |
(inhibit-read-only t)) |
| 1452 |
(save-excursion |
| 1453 |
(when (setq buf (get-buffer bname)) |
| 1454 |
(kill-buffer buf)) |
| 1455 |
(setq buf (get-buffer-create bname)) |
| 1456 |
(set-buffer buf) |
| 1457 |
(erase-buffer) |
| 1458 |
(sgml-edit-attrib-mode) |
| 1459 |
(make-local-variable 'sgml-attlist) |
| 1460 |
(setq sgml-attlist (sgml-effective-attlist |
| 1461 |
(sgml-element-eltype element))) |
| 1462 |
(sgml-insert '(read-only t) |
| 1463 |
(substitute-command-keys |
| 1464 |
"<%s -- Edit values and finish with \ |
| 1465 |
\\[sgml-edit-attrib-finish], abort with \\[sgml-edit-attrib-abort] --\n") |
| 1466 |
(sgml-element-name element)) |
| 1467 |
(loop |
| 1468 |
for attr in sgml-attlist do |
| 1469 |
;; Produce text like |
| 1470 |
;; name = value |
| 1471 |
;; -- declaration : default -- |
| 1472 |
(let* ((aname (sgml-attdecl-name attr)) |
| 1473 |
(dcl-value (sgml-attdecl-declared-value attr)) |
| 1474 |
(def-value (sgml-attdecl-default-value attr)) |
| 1475 |
(cur-value (sgml-lookup-attspec aname asl))) |
| 1476 |
(sgml-insert ; atribute name |
| 1477 |
'(read-only t category sgml-form) " %s =" aname) |
| 1478 |
(cond ; attribute value |
| 1479 |
((sgml-default-value-type-p 'FIXED def-value) |
| 1480 |
(sgml-insert '(read-only t category sgml-fixed) |
| 1481 |
" #FIXED %s" |
| 1482 |
(sgml-default-value-attval def-value))) |
| 1483 |
((and (null cur-value) |
| 1484 |
(or (memq def-value '(IMPLIED CONREF CURRENT)) |
| 1485 |
(sgml-default-value-attval def-value))) |
| 1486 |
(sgml-insert '(read-only t category sgml-form |
| 1487 |
rear-nonsticky (read-only category)) |
| 1488 |
" ") |
| 1489 |
(sgml-insert '(category sgml-default rear-nonsticky (category)) |
| 1490 |
"#DEFAULT")) |
| 1491 |
(t |
| 1492 |
(sgml-insert '(read-only t category sgml-form |
| 1493 |
rear-nonsticky (read-only category)) |
| 1494 |
" ") |
| 1495 |
(when (not (null cur-value)) |
| 1496 |
(sgml-insert nil "%s" (sgml-attspec-attval cur-value))))) |
| 1497 |
(sgml-insert |
| 1498 |
'(read-only 1) |
| 1499 |
"\n\t-- %s: %s --\n" |
| 1500 |
(cond ((sgml-declared-value-token-group dcl-value)) |
| 1501 |
((sgml-declared-value-notation dcl-value) |
| 1502 |
(format "NOTATION %s" |
| 1503 |
(sgml-declared-value-notation dcl-value))) |
| 1504 |
(t |
| 1505 |
dcl-value)) |
| 1506 |
(cond ((sgml-default-value-attval def-value)) |
| 1507 |
(t |
| 1508 |
(concat "#" (upcase (symbol-name def-value)))))))) |
| 1509 |
(sgml-insert '(read-only t) ">") |
| 1510 |
(goto-char (point-min)) |
| 1511 |
(sgml-edit-attrib-next)) |
| 1512 |
buf)) |
| 1513 |
|
| 1514 |
|
| 1515 |
(defvar sgml-edit-attrib-mode-map (make-sparse-keymap)) |
| 1516 |
|
| 1517 |
;; used as only for #DEFAULT in attribute editing. Binds all normally inserting |
| 1518 |
;; keys to a command that will clear the #DEFAULT before doing self-insert. |
| 1519 |
(defvar sgml-attr-default-keymap |
| 1520 |
(let ((map (make-sparse-keymap))) |
| 1521 |
(set-keymap-parent map sgml-edit-attrib-mode-map) |
| 1522 |
(substitute-key-definition 'self-insert-command |
| 1523 |
'sgml-attr-clean-and-insert |
| 1524 |
map |
| 1525 |
global-map) |
| 1526 |
(put 'sgml-default 'local-map map))) |
| 1527 |
|
| 1528 |
(define-key sgml-edit-attrib-mode-map "\C-c\C-c" 'sgml-edit-attrib-finish) |
| 1529 |
(define-key sgml-edit-attrib-mode-map "\C-c\C-d" 'sgml-edit-attrib-default) |
| 1530 |
(define-key sgml-edit-attrib-mode-map "\C-c\C-k" 'sgml-edit-attrib-abort) |
| 1531 |
|
| 1532 |
(define-key sgml-edit-attrib-mode-map "\C-a" 'sgml-edit-attrib-field-start) |
| 1533 |
(define-key sgml-edit-attrib-mode-map "\C-e" 'sgml-edit-attrib-field-end) |
| 1534 |
(define-key sgml-edit-attrib-mode-map "\t" 'sgml-edit-attrib-next) |
| 1535 |
|
| 1536 |
(defun sgml-edit-attrib-mode () |
| 1537 |
"Major mode to edit attribute specification list.\\<sgml-edit-attrib-mode-map> |
| 1538 |
Use \\[sgml-edit-attrib-next] to move between input fields. Use |
| 1539 |
\\[sgml-edit-attrib-default] to make an attribute have its default |
| 1540 |
value. To abort edit kill buffer (\\[kill-buffer]) and remove window |
| 1541 |
\(\\[delete-window]). To finish edit use \\[sgml-edit-attrib-finish]. |
| 1542 |
|
| 1543 |
\\{sgml-edit-attrib-mode-map}" |
| 1544 |
(setq mode-name "SGML edit attributes" |
| 1545 |
major-mode 'sgml-edit-attrib-mode) |
| 1546 |
(use-local-map sgml-edit-attrib-mode-map) |
| 1547 |
(run-hooks 'text-mode-hook 'sgml-edit-attrib-mode-hook)) |
| 1548 |
|
| 1549 |
(defun sgml-edit-attrib-abort () |
| 1550 |
"Abort the attribute editor, removing the window." |
| 1551 |
(interactive) |
| 1552 |
(let ((cb (current-buffer)) |
| 1553 |
(start sgml-start-attributes)) |
| 1554 |
(delete-windows-on cb) |
| 1555 |
(kill-buffer cb) |
| 1556 |
(when (markerp start) |
| 1557 |
(switch-to-buffer (marker-buffer start)) |
| 1558 |
(goto-char start)))) |
| 1559 |
|
| 1560 |
(defun sgml-edit-attrib-finish () |
| 1561 |
"Finish editing and insert attribute values in original buffer." |
| 1562 |
(interactive) |
| 1563 |
(let ((cb (current-buffer)) |
| 1564 |
(asl (sgml-edit-attrib-specification-list)) |
| 1565 |
;; save buffer local variables |
| 1566 |
(start sgml-start-attributes)) |
| 1567 |
(when (markerp start) |
| 1568 |
(delete-windows-on cb) |
| 1569 |
(switch-to-buffer (marker-buffer start)) |
| 1570 |
(kill-buffer cb) |
| 1571 |
(goto-char start) |
| 1572 |
(let ((element (sgml-find-element-of start))) |
| 1573 |
;; *** Should the it be verified that this element |
| 1574 |
;; is the one edited? |
| 1575 |
(sgml-change-start-tag element asl))))) |
| 1576 |
|
| 1577 |
|
| 1578 |
(defun sgml-edit-attrib-specification-list () |
| 1579 |
(goto-char (point-min)) |
| 1580 |
(forward-line 1) |
| 1581 |
(sgml-with-parser-syntax |
| 1582 |
(let ((asl nil) |
| 1583 |
(al sgml-attlist)) |
| 1584 |
(while (not (eq ?> (following-char))) |
| 1585 |
(sgml-parse-s) |
| 1586 |
(sgml-check-nametoken) ; attribute name, should match head of al |
| 1587 |
(forward-char 3) |
| 1588 |
(unless (memq (get-text-property (point) 'category) |
| 1589 |
'(sgml-default sgml-fixed)) |
| 1590 |
(push |
| 1591 |
(sgml-make-attspec (sgml-attdecl-name (car al)) |
| 1592 |
(sgml-extract-attribute-value |
| 1593 |
(sgml-attdecl-declared-value (car al)))) |
| 1594 |
asl)) |
| 1595 |
(while (progn (beginning-of-line 2) |
| 1596 |
(or (eolp) |
| 1597 |
(not (get-text-property (point) 'read-only))))) |
| 1598 |
|
| 1599 |
(forward-line 1) |
| 1600 |
(setq al (cdr al))) |
| 1601 |
asl))) |
| 1602 |
|
| 1603 |
|
| 1604 |
(defun sgml-extract-attribute-value (type) |
| 1605 |
(save-excursion |
| 1606 |
(save-restriction |
| 1607 |
(narrow-to-region (point) |
| 1608 |
(progn (sgml-edit-attrib-field-end) |
| 1609 |
(point))) |
| 1610 |
(goto-char (point-min)) |
| 1611 |
(while (not (eobp)) |
| 1612 |
(if (eq 'sgml-default (get-text-property (point) 'category)) |
| 1613 |
(delete-char 1) |
| 1614 |
(forward-char 1))) |
| 1615 |
(unless (eq type 'CDATA) |
| 1616 |
(subst-char-in-region (point-min) (point-max) ?\n ? ) |
| 1617 |
(goto-char (point-min)) |
| 1618 |
(delete-horizontal-space)) |
| 1619 |
(goto-char (point-min)) |
| 1620 |
(when (search-forward "\"" nil t) ; don't allow both " and ' |
| 1621 |
(goto-char (point-min)) |
| 1622 |
(while (search-forward "'" nil t) ; replace ' with char ref |
| 1623 |
(replace-match "'"))) |
| 1624 |
(buffer-string)))) |
| 1625 |
|
| 1626 |
(defun sgml-edit-attrib-default () |
| 1627 |
"Set current attribute value to default." |
| 1628 |
(interactive) |
| 1629 |
(sgml-edit-attrib-clear) |
| 1630 |
(save-excursion |
| 1631 |
(sgml-insert '(category sgml-default rear-nonsticky (category)) |
| 1632 |
"#DEFAULT"))) |
| 1633 |
|
| 1634 |
(defun sgml-edit-attrib-clear () |
| 1635 |
"Kill the value of current attribute." |
| 1636 |
(interactive) |
| 1637 |
(let ((inhibit-read-only '(sgml-default))) |
| 1638 |
(sgml-edit-attrib-field-start) |
| 1639 |
(let ((end (save-excursion (sgml-edit-attrib-field-end) (point)))) |
| 1640 |
(put-text-property (point) end 'read-only nil) |
| 1641 |
(let ((inhibit-read-only t)) |
| 1642 |
(put-text-property (1- (point)) (point) |
| 1643 |
'rear-nonsticky '(read-only category))) |
| 1644 |
(kill-region (point) end)))) |
| 1645 |
|
| 1646 |
|
| 1647 |
(defun sgml-attr-clean-and-insert (n) |
| 1648 |
"Insert the character you type, after clearing the current attribute." |
| 1649 |
(interactive "p") |
| 1650 |
(sgml-edit-attrib-clear) |
| 1651 |
(self-insert-command n)) |
| 1652 |
|
| 1653 |
|
| 1654 |
(defun sgml-edit-attrib-field-start () |
| 1655 |
"Go to the start of the attribute value field." |
| 1656 |
(interactive) |
| 1657 |
(let (start) |
| 1658 |
(beginning-of-line 1) |
| 1659 |
(while (not (eq t (get-text-property (point) 'read-only))) |
| 1660 |
(beginning-of-line 0)) |
| 1661 |
(while (eq 'sgml-form (get-text-property (point) 'category)) |
| 1662 |
(setq start (next-single-property-change (point) 'category)) |
| 1663 |
(unless start (error "No attribute value here")) |
| 1664 |
(assert (number-or-marker-p start)) |
| 1665 |
(goto-char start)))) |
| 1666 |
|
| 1667 |
(defun sgml-edit-attrib-field-end () |
| 1668 |
"Go to the end of the attribute value field." |
| 1669 |
(interactive) |
| 1670 |
(sgml-edit-attrib-field-start) |
| 1671 |
(let ((end (if (and (eolp) |
| 1672 |
(get-text-property (1+ (point)) 'read-only)) |
| 1673 |
(point) |
| 1674 |
(next-single-property-change (point) 'read-only)))) |
| 1675 |
(assert (number-or-marker-p end)) |
| 1676 |
(goto-char end))) |
| 1677 |
|
| 1678 |
(defun sgml-edit-attrib-next () |
| 1679 |
"Move to next attribute value." |
| 1680 |
(interactive) |
| 1681 |
(if (eq t (get-text-property (point) 'read-only)) |
| 1682 |
(beginning-of-line 1)) |
| 1683 |
(or (search-forward-regexp (if sgml-have-re-char-clases |
| 1684 |
"^ *[-_.:[:alnum:]]+ *= ?" |
| 1685 |
"^ *[-_.:A-Za-z0-9]+ *= ?") nil t) |
| 1686 |
(goto-char (point-min)))) |
| 1687 |
|
| 1688 |
|
| 1689 |
;;;; SGML mode: Hiding tags/attributes |
| 1690 |
|
| 1691 |
(defconst sgml-tag-regexp |
| 1692 |
(if sgml-have-re-char-clases |
| 1693 |
"\\(</?>\\|</?[_[:alpha:]][-_:[:alnum:].]*\\(\\([^'\"></]\\|'[^']*'\\|\"[^\"]*\"\\)*\\)/?>?\\)" |
| 1694 |
"\\(</?>\\|</?[_A-Za-z][-_:A-Za-z0-9.]*\\(\\([^'\"></]\\|'[^']*'\\|\"[^\"]*\"\\)*\\)/?>?\\)")) |
| 1695 |
|
| 1696 |
(defun sgml-operate-on-tags (action &optional attr-p) |
| 1697 |
(let ((buffer-modified-p (buffer-modified-p)) |
| 1698 |
(inhibit-read-only t) |
| 1699 |
(buffer-read-only nil) |
| 1700 |
(before-change-functions nil) |
| 1701 |
(markup-index ; match-data index in tag regexp |
| 1702 |
(if attr-p 2 1)) |
| 1703 |
(tagcount ; number tags to give them uniq |
| 1704 |
; invisible properties |
| 1705 |
1)) |
| 1706 |
(unwind-protect |
| 1707 |
(save-excursion |
| 1708 |
(goto-char (point-min)) |
| 1709 |
(while (re-search-forward sgml-tag-regexp nil t) |
| 1710 |
(cond |
| 1711 |
((eq action 'hide) |
| 1712 |
(let ((tag (downcase |
| 1713 |
(buffer-substring-no-properties |
| 1714 |
(1+ (match-beginning 0)) |
| 1715 |
(match-beginning 2))))) |
| 1716 |
(if (or attr-p (not (member tag sgml-exposed-tags))) |
| 1717 |
(add-text-properties |
| 1718 |
(match-beginning markup-index) (match-end markup-index) |
| 1719 |
(list 'invisible tagcount |
| 1720 |
'rear-nonsticky '(invisible face)))))) |
| 1721 |
((eq action 'show) ; ignore markup-index |
| 1722 |
(remove-text-properties (match-beginning 0) (match-end 0) |
| 1723 |
'(invisible nil))) |
| 1724 |
(t (error "Invalid action: %s" action))) |
| 1725 |
(incf tagcount))) |
| 1726 |
(sgml-restore-buffer-modified-p buffer-modified-p)))) |
| 1727 |
|
| 1728 |
(defun sgml-hide-tags () |
| 1729 |
"Hide all tags in buffer." |
| 1730 |
(interactive) |
| 1731 |
(sgml-operate-on-tags 'hide)) |
| 1732 |
|
| 1733 |
(defun sgml-show-tags () |
| 1734 |
"Show hidden tags in buffer." |
| 1735 |
(interactive) |
| 1736 |
(sgml-operate-on-tags 'show)) |
| 1737 |
|
| 1738 |
(defun sgml-hide-attributes () |
| 1739 |
"Hide all attribute specifications in the buffer." |
| 1740 |
(interactive) |
| 1741 |
(sgml-operate-on-tags 'hide 'attributes)) |
| 1742 |
|
| 1743 |
(defun sgml-show-attributes () |
| 1744 |
"Show all attribute specifications in the buffer." |
| 1745 |
(interactive) |
| 1746 |
(sgml-operate-on-tags 'show 'attributes)) |
| 1747 |
|
| 1748 |
|
| 1749 |
;;;; SGML mode: Normalize (and misc manipulations) |
| 1750 |
|
| 1751 |
(defun sgml-expand-shortref-to-text (name) |
| 1752 |
(let (before-change-functions |
| 1753 |
(entity (sgml-lookup-entity name (sgml-dtd-entities sgml-dtd-info)))) |
| 1754 |
(cond |
| 1755 |
((null entity) (sgml-error "Undefined entity %s" name)) |
| 1756 |
((sgml-entity-data-p entity) |
| 1757 |
(sgml-expand-shortref-to-entity name)) |
| 1758 |
(t |
| 1759 |
(delete-region sgml-markup-start (point)) |
| 1760 |
(sgml-entity-insert-text entity) |
| 1761 |
(setq sgml-goal (point-max)) ; May have changed size of buffer |
| 1762 |
;; now parse the entity text |
| 1763 |
(setq sgml-rs-ignore-pos sgml-markup-start) |
| 1764 |
(goto-char sgml-markup-start))))) |
| 1765 |
|
| 1766 |
(defun sgml-expand-shortref-to-entity (name) |
| 1767 |
(let ((end (point)) |
| 1768 |
(re-found nil) |
| 1769 |
before-change-functions) |
| 1770 |
(goto-char sgml-markup-start) |
| 1771 |
(setq re-found (search-forward "\n" end t)) |
| 1772 |
(delete-region sgml-markup-start end) |
| 1773 |
(insert "&" name (if re-found "\n" ";")) |
| 1774 |
(setq sgml-goal (point-max)) ; May have changed size of buffer |
| 1775 |
(goto-char (setq sgml-rs-ignore-pos sgml-markup-start)))) |
| 1776 |
|
| 1777 |
(defun sgml-expand-all-shortrefs (to-entity) |
| 1778 |
"Expand all short references in the buffer. |
| 1779 |
Short references to text entities are expanded to the replacement text |
| 1780 |
of the entity; other short references are expanded into general entity |
| 1781 |
references. If argument TO-ENTITY is non-nil, or if called |
| 1782 |
interactively with a numeric prefix argument, all short references are |
| 1783 |
replaced by general entity references." |
| 1784 |
(interactive "*P") |
| 1785 |
(sgml-reparse-buffer |
| 1786 |
(if to-entity |
| 1787 |
(function sgml-expand-shortref-to-entity) |
| 1788 |
(function sgml-expand-shortref-to-text)))) |
| 1789 |
|
| 1790 |
(defun sgml-normalize (to-entity &optional element) |
| 1791 |
"Normalize buffer by filling in omitted tags and expanding empty tags. |
| 1792 |
Argument TO-ENTITY controls how short references are expanded as with |
| 1793 |
`sgml-expand-all-shortrefs'. An optional argument ELEMENT can be the |
| 1794 |
element to normalize instead of the whole buffer, if used no short |
| 1795 |
references will be expanded." |
| 1796 |
(interactive "*P") |
| 1797 |
(unless element |
| 1798 |
(sgml-expand-all-shortrefs to-entity)) |
| 1799 |
(let ((only-one (not (null element)))) |
| 1800 |
(setq element (or element (sgml-top-element))) |
| 1801 |
(goto-char (sgml-element-end element)) |
| 1802 |
;; FIXME: actually the sgml-note-change-at called by the |
| 1803 |
;; before-change-functions need to be delayed to after the normalize |
| 1804 |
;; to avoid destroying the tree wile traversing it. |
| 1805 |
(let ((before-change-functions nil)) |
| 1806 |
(sgml-normalize-content element only-one))) |
| 1807 |
(sgml-note-change-at (sgml-element-start element)) |
| 1808 |
(sgml-message "Done")) |
| 1809 |
|
| 1810 |
(defun sgml-normalize-element () |
| 1811 |
(interactive "*") |
| 1812 |
(sgml-normalize nil (sgml-find-element-of (point)))) |
| 1813 |
|
| 1814 |
(defun sgml-normalize-content (element only-first) |
| 1815 |
"Normalize all elements in a content where ELEMENT is first element. |
| 1816 |
If sgml-normalize-trims is non-nil, trim off white space from ends of |
| 1817 |
elements with omitted end-tags." |
| 1818 |
(let ((content nil)) |
| 1819 |
(while element ; Build list of content elements |
| 1820 |
(push element content) |
| 1821 |
(setq element (if only-first |
| 1822 |
nil |
| 1823 |
(sgml-element-next element)))) |
| 1824 |
(while content |
| 1825 |
(setq element (car content)) |
| 1826 |
;; Progress report |
| 1827 |
(sgml-lazy-message "Normalizing %d%% left" |
| 1828 |
(/ (point) (/ (+ (point-max) 100) 100))) |
| 1829 |
;; Fix the end-tag |
| 1830 |
(sgml-normalize-end-tag element) |
| 1831 |
;; Fix tags of content |
| 1832 |
(sgml-normalize-content (sgml-tree-content element) nil) |
| 1833 |
;; Fix the start-tag |
| 1834 |
(sgml-normalize-start-tag element) |
| 1835 |
;; Next content element |
| 1836 |
(setq content (cdr content))))) |
| 1837 |
|
| 1838 |
(defun sgml-normalize-start-tag (element) |
| 1839 |
(when (sgml-bpos-p (sgml-element-stag-epos element)) |
| 1840 |
(goto-char (min (point) (sgml-element-start element))) |
| 1841 |
(let ((name (sgml-element-gi element)) |
| 1842 |
(attlist (sgml-element-attlist element)) |
| 1843 |
(asl (sgml-element-attribute-specification-list element))) |
| 1844 |
(save-excursion |
| 1845 |
(assert (or (zerop (sgml-element-stag-len element)) |
| 1846 |
(= (point) (sgml-element-start element)))) |
| 1847 |
(delete-char (sgml-element-stag-len element)) |
| 1848 |
(sgml-insert-start-tag name asl attlist nil))))) |
| 1849 |
|
| 1850 |
(defun sgml-normalize-end-tag (element) |
| 1851 |
(unless (sgml-element-empty element) |
| 1852 |
(when (sgml-bpos-p (sgml-element-etag-epos element)) |
| 1853 |
(goto-char (min (point) (sgml-element-etag-start element))) |
| 1854 |
(if (and (zerop (sgml-element-etag-len element)) |
| 1855 |
sgml-normalize-trims) |
| 1856 |
(skip-chars-backward " \t\n\r")) |
| 1857 |
(delete-char (sgml-tree-etag-len element)) |
| 1858 |
(save-excursion (insert (sgml-end-tag-of element)))))) |
| 1859 |
|
| 1860 |
|
| 1861 |
(defun sgml-make-character-reference (&optional invert) |
| 1862 |
"Convert character after point into a character reference. |
| 1863 |
If called with a numeric argument, convert a character reference back |
| 1864 |
to a normal character. If called from a program, set optional |
| 1865 |
argument INVERT to non-nil. If the function `decode-char' is defined, |
| 1866 |
convert to and from Unicodes. Otherwise will only work for ASCII or 8-bit |
| 1867 |
characters in the current coding system." |
| 1868 |
(interactive "*P") |
| 1869 |
(cond |
| 1870 |
(invert |
| 1871 |
(or (looking-at "&#\\([0-9]+\\)[;\n]?") |
| 1872 |
(error "No character reference after point")) |
| 1873 |
(let ((c (string-to-int (buffer-substring (match-beginning 1) |
| 1874 |
(match-end 1))))) |
| 1875 |
(delete-region (match-beginning 0) |
| 1876 |
(match-end 0)) |
| 1877 |
(if (fboundp 'decode-char) ; Emacs 21, Mule-UCS |
| 1878 |
(setq c (decode-char 'ucs c)) |
| 1879 |
;; Else have to assume 8-bit character. |
| 1880 |
(if (fboundp 'unibyte-char-to-multibyte) ; Emacs 20 |
| 1881 |
(setq c (unibyte-char-to-multibyte c)))) |
| 1882 |
(insert c))) |
| 1883 |
;; Convert character to &#nn; |
| 1884 |
(t |
| 1885 |
(let ((c (following-char))) |
| 1886 |
(delete-char 1) |
| 1887 |
(if (fboundp 'encode-char) |
| 1888 |
(setq c (encode-char c 'ucs)) |
| 1889 |
(if (fboundp 'multibyte-char-to-unibyte) |
| 1890 |
(setq c (multibyte-char-to-unibyte c)))) |
| 1891 |
(insert (format "&#%d;" c)))))) |
| 1892 |
|
| 1893 |
(defun sgml-expand-entity-reference () |
| 1894 |
"Insert the text of the entity referenced at point." |
| 1895 |
(interactive) |
| 1896 |
(save-excursion |
| 1897 |
(sgml-with-parser-syntax |
| 1898 |
(setq sgml-markup-start (point)) |
| 1899 |
(or (sgml-parse-delim "ERO") |
| 1900 |
(progn |
| 1901 |
(skip-syntax-backward "w_") |
| 1902 |
(forward-char -1) ; @@ Really length of ERO |
| 1903 |
(setq sgml-markup-start (point)) |
| 1904 |
(sgml-check-delim "ERO"))) |
| 1905 |
(let* ((ename (sgml-check-name t)) |
| 1906 |
(entity (sgml-lookup-entity ename |
| 1907 |
(sgml-dtd-entities |
| 1908 |
(sgml-pstate-dtd |
| 1909 |
sgml-buffer-parse-state))))) |
| 1910 |
(unless entity |
| 1911 |
(error "Undefined entity %s" ename)) |
| 1912 |
(or (sgml-parse-delim "REFC") |
| 1913 |
(sgml-parse-RE)) |
| 1914 |
(delete-region sgml-markup-start (point)) |
| 1915 |
(sgml-entity-insert-text entity))))) |
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
(defun sgml-trim-and-leave-element () |
| 1920 |
"Remove blanks at end of current element and move point to after element." |
| 1921 |
(interactive) |
| 1922 |
(goto-char (sgml-element-etag-start (sgml-last-element))) |
| 1923 |
(while (progn (forward-char -1) |
| 1924 |
(looking-at "\\s-")) |
| 1925 |
(delete-char 1)) |
| 1926 |
(sgml-up-element)) |
| 1927 |
|
| 1928 |
|
| 1929 |
(defvar sgml-notation-handlers |
| 1930 |
'((gif . "xv") |
| 1931 |
(jpeg . "xv")) |
| 1932 |
"*An alist mapping notations to programs handling them") |
| 1933 |
|
| 1934 |
;; Function contributed by Matthias Clasen <[email protected]> |
| 1935 |
(defun sgml-edit-external-entity () |
| 1936 |
"Open a new window and display the external entity at the point." |
| 1937 |
(interactive) |
| 1938 |
(sgml-need-dtd) |
| 1939 |
(save-excursion |
| 1940 |
(sgml-with-parser-syntax |
| 1941 |
(setq sgml-markup-start (point)) |
| 1942 |
(unless (sgml-parse-delim "ERO") |
| 1943 |
(search-backward-regexp "[&>;]") |
| 1944 |
(setq sgml-markup-start (point)) |
| 1945 |
(sgml-check-delim "ERO")) |
| 1946 |
(sgml-parse-to-here) ; get an up-to-date parse tree |
| 1947 |
(let* ( (parent (buffer-file-name)) ; used to be (sgml-file) |
| 1948 |
(ename (sgml-check-name t)) |
| 1949 |
(entity (sgml-lookup-entity ename |
| 1950 |
(sgml-dtd-entities |
| 1951 |
(sgml-pstate-dtd |
| 1952 |
sgml-buffer-parse-state)))) |
| 1953 |
(buffer nil) |
| 1954 |
(ppos nil)) |
| 1955 |
(unless entity |
| 1956 |
(error "Undefined entity %s" ename)) |
| 1957 |
|
| 1958 |
(let* ((type (sgml-entity-type entity)) |
| 1959 |
(notation (sgml-entity-notation entity)) |
| 1960 |
(handler (cdr (assoc notation sgml-notation-handlers)))) |
| 1961 |
(case type |
| 1962 |
(ndata |
| 1963 |
(if handler |
| 1964 |
(progn |
| 1965 |
(message (format "Using '%s' to handle notation '%s'." |
| 1966 |
handler notation)) |
| 1967 |
(save-excursion |
| 1968 |
(set-buffer (get-buffer-create "*SGML background*")) |
| 1969 |
(erase-buffer) |
| 1970 |
(let* ((file (sgml-external-file |
| 1971 |
(sgml-entity-text entity) |
| 1972 |
type |
| 1973 |
(sgml-entity-name entity))) |
| 1974 |
(process (start-process |
| 1975 |
(format "%s background" handler) |
| 1976 |
nil handler file))) |
| 1977 |
(process-kill-without-query process)))) |
| 1978 |
(error "Don't know how to handle notation '%s'." notation))) |
| 1979 |
(text (progn |
| 1980 |
|
| 1981 |
;; here I try to construct a useful value for |
| 1982 |
;; `sgml-parent-element'. |
| 1983 |
|
| 1984 |
;; find sensible values for the HAS-SEEN-ELEMENT part |
| 1985 |
(let ((seen nil) |
| 1986 |
(child (sgml-tree-content sgml-current-tree))) |
| 1987 |
(while (and child |
| 1988 |
(sgml-tree-etag-epos child) |
| 1989 |
(<= (sgml-tree-end child) (point))) |
| 1990 |
(push (sgml-element-gi child) seen) |
| 1991 |
(setq child (sgml-tree-next child))) |
| 1992 |
(push (nreverse seen) ppos)) |
| 1993 |
|
| 1994 |
;; find ancestors |
| 1995 |
(let ((rover sgml-current-tree)) |
| 1996 |
(while (not (eq rover sgml-top-tree)) |
| 1997 |
(push (sgml-element-gi rover) ppos) |
| 1998 |
(setq rover (sgml-tree-parent rover)))) |
| 1999 |
|
| 2000 |
(find-file-other-window |
| 2001 |
(sgml-external-file (sgml-entity-text entity) |
| 2002 |
(sgml-entity-type entity) |
| 2003 |
(sgml-entity-name entity))) |
| 2004 |
(goto-char (point-min)) |
| 2005 |
(sgml-mode) |
| 2006 |
(setq sgml-parent-document (cons parent ppos)) |
| 2007 |
;; update the live element indicator of the new window |
| 2008 |
(sgml-parse-to-here))) |
| 2009 |
(t (error "Can't edit entities of type '%s'." type)))))))) |
| 2010 |
|
| 2011 |
;;;; SGML mode: TAB completion |
| 2012 |
|
| 2013 |
(defun sgml-complete () |
| 2014 |
"Complete the word/tag/entity before point. |
| 2015 |
If it is a tag (starts with < or </) complete with valid tags. |
| 2016 |
If it is an entity (starts with &) complete with declared entities. |
| 2017 |
If it is a markup declaration (starts with <!) complete with markup |
| 2018 |
declaration names. If it is a reserved word starting with # complete |
| 2019 |
reserved words. |
| 2020 |
If it is something else complete with ispell-complete-word." |
| 2021 |
(interactive "*") |
| 2022 |
(let ((tab ; The completion table |
| 2023 |
nil) |
| 2024 |
(ignore-case ; If ignore case in matching completion |
| 2025 |
sgml-namecase-general) |
| 2026 |
(insert-case |
| 2027 |
'sgml-general-insert-case) |
| 2028 |
(pattern nil) |
| 2029 |
(c nil) |
| 2030 |
(here (point))) |
| 2031 |
(skip-chars-backward "^ \n\t</!&%#") |
| 2032 |
(setq pattern (buffer-substring (point) here)) |
| 2033 |
(setq c (char-after (1- (point)))) |
| 2034 |
(cond |
| 2035 |
;; entitiy |
| 2036 |
((eq c ?&) |
| 2037 |
(sgml-need-dtd) |
| 2038 |
(setq insert-case 'sgml-entity-insert-case) |
| 2039 |
(setq tab |
| 2040 |
(sgml-entity-completion-table |
| 2041 |
(sgml-dtd-entities (sgml-pstate-dtd sgml-buffer-parse-state))))) |
| 2042 |
;; start-tag |
| 2043 |
((eq c ?<) |
| 2044 |
(save-excursion |
| 2045 |
(backward-char 1) |
| 2046 |
(sgml-parse-to-here) |
| 2047 |
(setq tab (sgml-eltype-completion-table |
| 2048 |
(sgml-current-list-of-valid-eltypes))))) |
| 2049 |
;; end-tag |
| 2050 |
((eq c ?/) |
| 2051 |
(save-excursion |
| 2052 |
(backward-char 2) |
| 2053 |
(sgml-parse-to-here) |
| 2054 |
(setq tab (sgml-eltype-completion-table |
| 2055 |
(sgml-current-list-of-endable-eltypes))))) |
| 2056 |
;; markup declaration |
| 2057 |
((eq c ?!) |
| 2058 |
(setq tab sgml-markup-declaration-table |
| 2059 |
ignore-case t)) |
| 2060 |
;; Reserved words with '#' prefix |
| 2061 |
((eq c ?#) |
| 2062 |
(setq tab '(("PCDATA") ("NOTATION") ("IMPLIED") ("REQUIRED") |
| 2063 |
("FIXED") ("EMPTY")) |
| 2064 |
ignore-case t)) |
| 2065 |
(t |
| 2066 |
(goto-char here) |
| 2067 |
(ispell-complete-word))) |
| 2068 |
(when tab |
| 2069 |
(let* ((completion-ignore-case ignore-case) |
| 2070 |
(completion (try-completion pattern tab))) |
| 2071 |
(cond ((null completion) |
| 2072 |
(goto-char here) |
| 2073 |
(message "Can't find completion for \"%s\"" pattern) |
| 2074 |
(ding)) |
| 2075 |
((eq completion t) |
| 2076 |
(goto-char here) |
| 2077 |
(message "[Complete]")) |
| 2078 |
((not (string= pattern completion)) |
| 2079 |
(delete-char (length pattern)) |
| 2080 |
(insert (funcall insert-case completion))) |
| 2081 |
(t |
| 2082 |
(goto-char here) |
| 2083 |
(message "Making completion list...") |
| 2084 |
(let ((list (all-completions pattern tab))) |
| 2085 |
(with-output-to-temp-buffer " *Completions*" |
| 2086 |
(display-completion-list list))) |
| 2087 |
(message "Making completion list...%s" "done"))))))) |
| 2088 |
|
| 2089 |
|
| 2090 |
;;;; SGML mode: Options menu |
| 2091 |
|
| 2092 |
(defun sgml-file-options-menu (&optional event) |
| 2093 |
(interactive "e") |
| 2094 |
(sgml-options-menu event sgml-file-options)) |
| 2095 |
|
| 2096 |
(defun sgml-user-options-menu (&optional event) |
| 2097 |
(interactive "e") |
| 2098 |
(sgml-options-menu event sgml-user-options)) |
| 2099 |
|
| 2100 |
(defun sgml-options-menu (event vars) |
| 2101 |
(let ((var |
| 2102 |
(let ((maxlen |
| 2103 |
(loop for var in vars |
| 2104 |
maximize (length (sgml-variable-description var))))) |
| 2105 |
(sgml-popup-menu |
| 2106 |
event "Options" |
| 2107 |
(loop for var in vars |
| 2108 |
for desc = (sgml-variable-description var) |
| 2109 |
collect |
| 2110 |
(cons |
| 2111 |
(format "%s%s [%s]" |
| 2112 |
desc |
| 2113 |
(make-string (- maxlen (length desc)) ? ) |
| 2114 |
(sgml-option-value-indicator var)) |
| 2115 |
var)))))) |
| 2116 |
(when var |
| 2117 |
(sgml-do-set-option var event)))) |
| 2118 |
|
| 2119 |
;; Fixme: Use Customize for this. |
| 2120 |
(defun sgml-do-set-option (var &optional event) |
| 2121 |
(let ((type (sgml-variable-type var)) |
| 2122 |
(val (symbol-value var))) |
| 2123 |
(cond |
| 2124 |
((eq 'toggle type) |
| 2125 |
(message "%s set to %s" var (not val)) |
| 2126 |
(set var (not val))) |
| 2127 |
((eq 'string type) |
| 2128 |
(describe-variable var) |
| 2129 |
(setq val (read-string (concat (sgml-variable-description var) ": "))) |
| 2130 |
(when (stringp val) |
| 2131 |
(set var val))) |
| 2132 |
((eq 'file-list type) |
| 2133 |
(describe-variable var) |
| 2134 |
(sgml-append-to-help-buffer "\ |
| 2135 |
Enter as many filenames as you want. Entering a directory |
| 2136 |
or non-existing filename will exit the loop.") |
| 2137 |
(setq val nil) |
| 2138 |
(while (let ((next |
| 2139 |
(expand-file-name |
| 2140 |
(read-file-name |
| 2141 |
(concat (sgml-variable-description var) ": ") |
| 2142 |
nil "" nil nil)))) |
| 2143 |
(if (and (file-exists-p next) (not (file-directory-p next))) |
| 2144 |
(setq val (cons next val))))) |
| 2145 |
(set var val)) |
| 2146 |
((eq 'file-or-nil type) |
| 2147 |
(describe-variable var) |
| 2148 |
(sgml-append-to-help-buffer "\ |
| 2149 |
Entering a directory or non-existing filename here |
| 2150 |
will reset the variable.") |
| 2151 |
(setq val (expand-file-name |
| 2152 |
(read-file-name |
| 2153 |
(concat (sgml-variable-description var) ": ") |
| 2154 |
nil (if (stringp val) (file-name-nondirectory val)) |
| 2155 |
nil (if (stringp val) (file-name-nondirectory val)) ))) |
| 2156 |
(if (and (file-exists-p val) (not (file-directory-p val))) |
| 2157 |
(set var val) |
| 2158 |
(set var nil))) |
| 2159 |
((consp type) |
| 2160 |
(let ((val |
| 2161 |
(sgml-popup-menu event |
| 2162 |
(sgml-variable-description var) |
| 2163 |
(loop for c in type collect |
| 2164 |
(cons |
| 2165 |
(if (consp c) (car c) (format "%s" c)) |
| 2166 |
(if (consp c) (cdr c) c)))))) |
| 2167 |
(set var val) |
| 2168 |
(message "%s set to %s" var val))) |
| 2169 |
(t |
| 2170 |
(describe-variable var) |
| 2171 |
(setq val (read-string (concat (sgml-variable-description var) |
| 2172 |
" (sexp): "))) |
| 2173 |
(when (stringp val) |
| 2174 |
(set var (car (read-from-string val))))))) |
| 2175 |
(force-mode-line-update)) |
| 2176 |
|
| 2177 |
(defun sgml-append-to-help-buffer (string) |
| 2178 |
(save-excursion |
| 2179 |
(set-buffer "*Help*") |
| 2180 |
(let ((inhibit-read-only t)) |
| 2181 |
(goto-char (point-max)) |
| 2182 |
(insert "\n" string)))) |
| 2183 |
|
| 2184 |
;;;; SGML mode: insert element where valid |
| 2185 |
|
| 2186 |
(defun sgml--add-before-p (tok state child) |
| 2187 |
;; Can TOK be added in STATE followed by CHILD |
| 2188 |
(let ((snext (sgml-get-move state tok)) |
| 2189 |
(c child)) |
| 2190 |
(when snext |
| 2191 |
(while c |
| 2192 |
(setq snext (sgml-get-move snext |
| 2193 |
(sgml-eltype-token |
| 2194 |
(sgml-element-eltype c)))) |
| 2195 |
(setq c (and snext (sgml-element-next c))))) |
| 2196 |
;; If snext is still non nill it can be inserted |
| 2197 |
snext)) |
| 2198 |
|
| 2199 |
(defun sgml--all-possible-elements (el) |
| 2200 |
(let ((c (sgml-element-content el)) |
| 2201 |
(s (sgml-element-model el)) |
| 2202 |
(found nil)) |
| 2203 |
(loop do |
| 2204 |
;; Fixme: this test avoids an error when DTD-less, but it's |
| 2205 |
;; probably an inappropriate kludge. -- fx |
| 2206 |
(when (not (eq s 'ANY)) |
| 2207 |
(dolist (tok (nconc (sgml-optional-tokens s) |
| 2208 |
(sgml-required-tokens s))) |
| 2209 |
(unless (memq tok found) |
| 2210 |
;; tok is optional here and not already found -- check that |
| 2211 |
;; it would not make the content invalid |
| 2212 |
(when (sgml--add-before-p tok s c) |
| 2213 |
(push tok found))))) |
| 2214 |
while c do |
| 2215 |
(setq s (sgml-element-pstate c)) |
| 2216 |
(setq c (sgml-element-next c))) |
| 2217 |
(mapcar #'sgml-token-eltype found))) |
| 2218 |
|
| 2219 |
|
| 2220 |
(defun sgml-add-element-to-element (gi first) |
| 2221 |
"Add an element of type GI to the current element. |
| 2222 |
The element will be added at the last legal position if FIRST is `nil', |
| 2223 |
otherwise it will be added at the first legal position." |
| 2224 |
(interactive |
| 2225 |
(let ((tab |
| 2226 |
(mapcar (lambda (et) (cons (sgml-eltype-name et) nil)) |
| 2227 |
(sgml--all-possible-elements |
| 2228 |
(sgml-find-context-of (point)))))) |
| 2229 |
(cond ((null tab) |
| 2230 |
(error "No element possible")) |
| 2231 |
(t |
| 2232 |
(let ((completion-ignore-case sgml-namecase-general)) |
| 2233 |
(list (completing-read "Element: " tab nil t |
| 2234 |
(and (null (cdr tab)) (caar tab))) |
| 2235 |
current-prefix-arg)))))) |
| 2236 |
(let ((el (sgml-find-context-of (point))) |
| 2237 |
(et (sgml-lookup-eltype (sgml-general-case gi)))) |
| 2238 |
;; First expand empty tag |
| 2239 |
(when (and sgml-xml-p (sgml-element-empty el)) |
| 2240 |
(save-excursion |
| 2241 |
(goto-char (sgml-element-stag-end el)) |
| 2242 |
(delete-char -2) |
| 2243 |
(insert ">\n" (sgml-end-tag-of sgml-current-tree)) |
| 2244 |
(sgml-indent-line)) |
| 2245 |
(setq el (sgml-find-context-of (point)))) |
| 2246 |
(let ((c (sgml-element-content el)) |
| 2247 |
(s (sgml-element-model el)) |
| 2248 |
(tok (sgml-eltype-token et)) |
| 2249 |
(last nil)) |
| 2250 |
;; Find legal position for new element |
| 2251 |
(while (and (not (cond |
| 2252 |
((sgml--add-before-p tok s c) |
| 2253 |
(setq last (if c (sgml-element-start c) |
| 2254 |
(sgml-element-etag-start el))) |
| 2255 |
first))) |
| 2256 |
(cond |
| 2257 |
(c (setq s (sgml-element-pstate c)) |
| 2258 |
(setq c (sgml-element-next c)) |
| 2259 |
t)))) |
| 2260 |
(cond (last |
| 2261 |
(goto-char last) |
| 2262 |
(sgml-insert-element gi)) |
| 2263 |
(t |
| 2264 |
(error "A %s element is not valid in current element" gi)))))) |
| 2265 |
|
| 2266 |
;;;; Show current element type |
| 2267 |
;; Candidate for C-c C-t |
| 2268 |
|
| 2269 |
(autoload 'sgml-princ-names "psgml-info") |
| 2270 |
(autoload 'sgml-eltype-refrenced-elements "psgml-info") |
| 2271 |
|
| 2272 |
(defun sgml-show-current-element-type () |
| 2273 |
"Show information about the current element and its type." |
| 2274 |
(interactive) |
| 2275 |
(let* ((el (sgml-find-context-of (point))) |
| 2276 |
(et (sgml-element-eltype el))) |
| 2277 |
(with-output-to-temp-buffer "*Current Element Type*" |
| 2278 |
(princ (format "ELEMENT: %s%s\n" (sgml-eltype-name et) |
| 2279 |
(let ((help-text (sgml-eltype-appdata et 'help-text))) |
| 2280 |
(if help-text |
| 2281 |
(format " -- %s" help-text) |
| 2282 |
"")))) |
| 2283 |
(when sgml-omittag |
| 2284 |
(princ (format "\n Start-tag is %s.\n End-tag is %s.\n" |
| 2285 |
(if (sgml-eltype-stag-optional et) |
| 2286 |
"optional" "required") |
| 2287 |
(if (sgml-eltype-etag-optional et) |
| 2288 |
"optional" "required")))) |
| 2289 |
;; ---- |
| 2290 |
(princ "\nCONTENT: ") |
| 2291 |
(cond ((symbolp (sgml-eltype-model et)) (princ (sgml-eltype-model et))) |
| 2292 |
(t |
| 2293 |
(princ (if (sgml-eltype-mixed et) |
| 2294 |
"mixed\n" |
| 2295 |
"element\n")) |
| 2296 |
(sgml-print-position-in-model el et (point) sgml-current-state) |
| 2297 |
(princ "\n\n") |
| 2298 |
(sgml-princ-names |
| 2299 |
(mapcar #'symbol-name (sgml-eltype-refrenced-elements et)) |
| 2300 |
"All: "))) |
| 2301 |
(let ((incl (sgml-eltype-includes et)) |
| 2302 |
(excl (sgml-eltype-excludes et))) |
| 2303 |
(when (or incl excl) |
| 2304 |
(princ "\n\nEXCEPTIONS:")) |
| 2305 |
(when incl |
| 2306 |
(princ "\n + ") |
| 2307 |
(sgml-princ-names (mapcar #'symbol-name incl))) |
| 2308 |
(when excl |
| 2309 |
(princ "\n - ") |
| 2310 |
(sgml-princ-names (mapcar #'symbol-name excl)))) |
| 2311 |
;; ---- |
| 2312 |
(princ "\n\nATTRIBUTES:\n") |
| 2313 |
(sgml-print-attlist et) |
| 2314 |
;; ---- |
| 2315 |
(let ((s (sgml-eltype-shortmap et))) |
| 2316 |
(when s |
| 2317 |
(princ (format "\nUSEMAP: %s\n" s)))) |
| 2318 |
;; ---- |
| 2319 |
(princ "\nOCCURS IN:\n") |
| 2320 |
(let ((occurs-in ())) |
| 2321 |
(sgml-map-eltypes |
| 2322 |
(function (lambda (cand) |
| 2323 |
(when (memq et (sgml-eltype-refrenced-elements cand)) |
| 2324 |
(push cand occurs-in)))) |
| 2325 |
(sgml-pstate-dtd sgml-buffer-parse-state)) |
| 2326 |
(sgml-princ-names (mapcar 'sgml-eltype-name |
| 2327 |
(sort occurs-in (function string-lessp)))))))) |
| 2328 |
|
| 2329 |
(defun sgml-print-attlist (et) |
| 2330 |
(let ((ob (current-buffer))) |
| 2331 |
(set-buffer standard-output) |
| 2332 |
(unwind-protect |
| 2333 |
(loop |
| 2334 |
for attdecl in (sgml-eltype-attlist et) do |
| 2335 |
(princ " ") |
| 2336 |
(princ (sgml-attdecl-name attdecl)) |
| 2337 |
(let ((dval (sgml-attdecl-declared-value attdecl)) |
| 2338 |
(defl (sgml-attdecl-default-value attdecl))) |
| 2339 |
(when (listp dval) |
| 2340 |
(setq dval (concat (if (eq (first dval) |
| 2341 |
'NOTATION) |
| 2342 |
"#NOTATION (" "(") |
| 2343 |
(mapconcat (function identity) |
| 2344 |
(second dval) |
| 2345 |
"|") |
| 2346 |
")"))) |
| 2347 |
(indent-to 15 1) |
| 2348 |
(princ dval) |
| 2349 |
(cond ((sgml-default-value-type-p 'FIXED defl) |
| 2350 |
(setq defl (format "#FIXED '%s'" |
| 2351 |
(sgml-default-value-attval defl)))) |
| 2352 |
((symbolp defl) |
| 2353 |
(setq defl (upcase (format "#%s" defl)))) |
| 2354 |
(t |
| 2355 |
(setq defl (format "'%s'" |
| 2356 |
(sgml-default-value-attval defl))))) |
| 2357 |
|
| 2358 |
(indent-to 48 1) |
| 2359 |
(princ defl) |
| 2360 |
(terpri))) |
| 2361 |
(set-buffer ob)))) |
| 2362 |
|
| 2363 |
|
| 2364 |
(defun sgml-print-position-in-model (element element-type buffer-pos parse-state) |
| 2365 |
(let ((u (sgml-element-content element)) |
| 2366 |
(names nil)) |
| 2367 |
(while (and u (>= buffer-pos (sgml-element-end u))) |
| 2368 |
(push (sgml-element-gi u) names) |
| 2369 |
(setq u (sgml-element-next u))) |
| 2370 |
(when names |
| 2371 |
(sgml-princ-names (nreverse names) " " ", ") |
| 2372 |
(princ "\n"))) |
| 2373 |
(princ " ->") |
| 2374 |
(let* ((state parse-state) |
| 2375 |
(required-seq ; the seq of req el following point |
| 2376 |
(loop for required = (sgml-required-tokens state) |
| 2377 |
while (and required (null (cdr required))) |
| 2378 |
collect (sgml-eltype-name (car required)) |
| 2379 |
do (setq state (sgml-get-move state (car required))))) |
| 2380 |
(last-alt |
| 2381 |
(mapcar 'sgml-eltype-name |
| 2382 |
(append (sgml-optional-tokens state) |
| 2383 |
(sgml-required-tokens state))))) |
| 2384 |
(cond |
| 2385 |
(required-seq |
| 2386 |
(when last-alt |
| 2387 |
(nconc required-seq |
| 2388 |
(list (concat "(" |
| 2389 |
(mapconcat (lambda (x) x) |
| 2390 |
last-alt " | ") |
| 2391 |
(if (sgml-final state) |
| 2392 |
")?" ")"))))) |
| 2393 |
(sgml-princ-names required-seq " " ", ")) |
| 2394 |
|
| 2395 |
(last-alt |
| 2396 |
(sgml-princ-names last-alt " (" " | ") |
| 2397 |
(princ ")") |
| 2398 |
(when (sgml-final state) |
| 2399 |
(princ "?")))))) |
| 2400 |
|
| 2401 |
|
| 2402 |
;;;; Structure Viewing and Navigating |
| 2403 |
|
| 2404 |
|
| 2405 |
(defun sgml-show-structure () |
| 2406 |
"Show the document structure in a separate buffer." |
| 2407 |
(interactive) |
| 2408 |
(let ((source (current-buffer)) |
| 2409 |
(result (get-buffer-create "*Document structure*"))) |
| 2410 |
(set-buffer result) |
| 2411 |
(occur-mode) |
| 2412 |
(erase-buffer) |
| 2413 |
(let ((structure |
| 2414 |
(save-excursion |
| 2415 |
(set-buffer source) |
| 2416 |
(sgml-structure-elements (sgml-top-element))))) |
| 2417 |
(sgml-show-structure-insert structure)) |
| 2418 |
(goto-char (point-min)) |
| 2419 |
(display-buffer result))) |
| 2420 |
|
| 2421 |
|
| 2422 |
(defun sgml-show-structure-insert (structure) |
| 2423 |
(loop for (gi level marker title) in structure do |
| 2424 |
(let ((start (point))) |
| 2425 |
(insert (make-string (* 2 level) ? )) |
| 2426 |
(sgml-insert `(face match mouse-face highlight) gi) |
| 2427 |
(sgml-insert `(mouse-face highlight) " %s" title) |
| 2428 |
(insert "\n") |
| 2429 |
(add-text-properties |
| 2430 |
start (point) |
| 2431 |
`(occur-target ,marker help-echo "mouse-2: go to this occurrence"))))) |
| 2432 |
|
| 2433 |
|
| 2434 |
(defun sgml-show-struct-element-p (element) |
| 2435 |
(let ((configured (sgml-element-appdata element 'structure))) |
| 2436 |
(unless (eql configured 'ignore) |
| 2437 |
(or configured |
| 2438 |
(and (not (sgml-element-data-p element)) |
| 2439 |
(not (sgml-element-empty element))))))) |
| 2440 |
|
| 2441 |
|
| 2442 |
(defun sgml-structure-elements (element) |
| 2443 |
(when (sgml-show-struct-element-p element) |
| 2444 |
(let ((gi (sgml-element-gi element)) |
| 2445 |
(level (sgml-element-level element)) |
| 2446 |
(child1 (sgml-element-content element)) |
| 2447 |
(marker nil) |
| 2448 |
(title "")) |
| 2449 |
(goto-char (sgml-element-start element)) |
| 2450 |
(setq marker (copy-marker (point-marker))) |
| 2451 |
(when (and child1 |
| 2452 |
(not (sgml-show-struct-element-p child1)) |
| 2453 |
(sgml-element-data-p child1)) |
| 2454 |
(let ((start-epos (sgml-element-stag-epos child1)) |
| 2455 |
(end-epos (sgml-element-etag-epos child1))) |
| 2456 |
(when (and (sgml-bpos-p start-epos) |
| 2457 |
(sgml-bpos-p end-epos)) |
| 2458 |
(goto-char start-epos) |
| 2459 |
(forward-char (sgml-element-stag-len child1)) |
| 2460 |
(when (looking-at "\\s-*$") |
| 2461 |
(forward-line 1)) |
| 2462 |
(when (< (point) end-epos) |
| 2463 |
(setq title |
| 2464 |
(buffer-substring (point) |
| 2465 |
(min (line-end-position) |
| 2466 |
end-epos))))))) |
| 2467 |
(cons (list (sgml-general-insert-case gi) |
| 2468 |
level marker title) |
| 2469 |
(loop for child = child1 then (sgml-element-next child) |
| 2470 |
while child |
| 2471 |
nconc (sgml-structure-elements child)))))) |
| 2472 |
|
| 2473 |
|
| 2474 |
;;; psgml-edit.el ends here |