| 1 |
;;; psgml-fs.el --- Format a SGML-file according to a style file |
| 2 |
;; Copyright (C) 1995, 2000 Lennart Staflin |
| 3 |
|
| 4 |
;; Author: Lennart Staflin <[email protected]> |
| 5 |
;; Version: $Id: psgml-fs.el,v 1.13 2002/07/14 10:03:26 lenst Exp $ |
| 6 |
;; Keywords: |
| 7 |
|
| 8 |
;;; This program is free software; you can redistribute it and/or modify |
| 9 |
;;; it under the terms of the GNU General Public License as published by |
| 10 |
;;; the Free Software Foundation; either version 2, or (at your option) |
| 11 |
;;; any later version. |
| 12 |
;;; |
| 13 |
;;; This program is distributed in the hope that it will be useful, |
| 14 |
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
;;; GNU General Public License for more details. |
| 17 |
;;; |
| 18 |
;;; A copy of the GNU General Public License can be obtained from this |
| 19 |
;;; program's author (send electronic mail to [email protected]) or from |
| 20 |
;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA |
| 21 |
;;; 02139, USA. |
| 22 |
;;; |
| 23 |
;;; Commentary: |
| 24 |
|
| 25 |
;; The function `style-format' formats the SGML-file in the current buffer |
| 26 |
;; according to the style defined in the file `style.fs' (or the file given |
| 27 |
;; by the variable `fs-style'). |
| 28 |
|
| 29 |
;; To try it load this file and open the test file example.sgml. Then |
| 30 |
;; run the emacs command `M-x style-format'. |
| 31 |
|
| 32 |
;; The style file should contain a single Lisp list. The elements of |
| 33 |
;; this list, themsleves lists, describe the style for an element type. |
| 34 |
;; The sublists begin with the generic identifier for the element types and |
| 35 |
;; the rest of the lists are characteristic/value pairs. |
| 36 |
|
| 37 |
;; E.g. ("p" block t left 4 top 2) |
| 38 |
|
| 39 |
;; Defines the style for p-elements to be blocks with left margin 4 and |
| 40 |
;; at least two blank lines before the block. |
| 41 |
|
| 42 |
|
| 43 |
;;; Code: |
| 44 |
(require 'psgml-api) |
| 45 |
(eval-when-compile (require 'cl) |
| 46 |
(require 'ps-print)) |
| 47 |
|
| 48 |
;;;; Formatting parameters |
| 49 |
|
| 50 |
(defvar fs-char |
| 51 |
'((left . 0) |
| 52 |
(first . nil) |
| 53 |
(default-top . 0) |
| 54 |
(default-bottom . 0) |
| 55 |
(ignore-empty-para . nil) |
| 56 |
(literal . nil))) |
| 57 |
|
| 58 |
(defvar fs-special-styles |
| 59 |
'(top bottom before after hang-from text sub-style title) |
| 60 |
"Style attributes that should not be entered in the characteristics table.") |
| 61 |
|
| 62 |
|
| 63 |
;;; Dynamic variables |
| 64 |
|
| 65 |
(defvar fs-current-element nil) |
| 66 |
(defvar fs-buffer) |
| 67 |
|
| 68 |
|
| 69 |
;;;; Formatting engine |
| 70 |
|
| 71 |
(defun fs-char (p) |
| 72 |
(cdr (assq p fs-char))) |
| 73 |
|
| 74 |
(defun fs-set-char (p val) |
| 75 |
(setcdr (assq p fs-char) val)) |
| 76 |
|
| 77 |
(defsetf fs-char fs-set-char) |
| 78 |
|
| 79 |
(defvar fs-para-acc "" |
| 80 |
"Accumulate text of paragraph") |
| 81 |
|
| 82 |
(defvar fs-hang-from nil |
| 83 |
"Hanging indent of current paragraph") |
| 84 |
|
| 85 |
(defvar fs-first-indent nil) |
| 86 |
(defvar fs-left-indent nil) |
| 87 |
|
| 88 |
(defvar fs-vspace 0 |
| 89 |
"Vertical space after last paragraph") |
| 90 |
|
| 91 |
(defvar fs-filename) |
| 92 |
(defvar fs-title) |
| 93 |
|
| 94 |
(defun fs-add-output (str &optional just) |
| 95 |
(save-excursion |
| 96 |
(set-buffer fs-buffer) |
| 97 |
(goto-char (point-max)) |
| 98 |
(let ((start (point))) |
| 99 |
(insert str) |
| 100 |
(when just |
| 101 |
(set-justification start (point) just))))) |
| 102 |
|
| 103 |
|
| 104 |
(defun fs-addvspace (n) |
| 105 |
(when (> n fs-vspace) |
| 106 |
(fs-add-output (make-string (- n fs-vspace) ?\n)) |
| 107 |
(setq fs-vspace n))) |
| 108 |
|
| 109 |
|
| 110 |
(defun fs-para () |
| 111 |
(when (if (fs-char 'ignore-empty-para) |
| 112 |
(string-match "[^\t\n ]" fs-para-acc) |
| 113 |
fs-left-indent) |
| 114 |
(assert fs-left-indent) |
| 115 |
(fs-output-para fs-para-acc fs-first-indent fs-left-indent |
| 116 |
fs-hang-from |
| 117 |
(fs-char 'literal)) |
| 118 |
(setq fs-vspace 0 |
| 119 |
fs-hang-from nil)) |
| 120 |
(setq fs-para-acc "" |
| 121 |
fs-first-indent nil |
| 122 |
fs-left-indent nil)) |
| 123 |
|
| 124 |
(defun fs-paraform-data (data) |
| 125 |
(unless fs-left-indent |
| 126 |
(setq fs-left-indent (fs-char 'left) |
| 127 |
fs-first-indent (fs-char 'first))) |
| 128 |
(let ((face (fs-char 'face))) |
| 129 |
(when face |
| 130 |
(setq data (copy-sequence data)) |
| 131 |
(put-text-property 0 (length data) |
| 132 |
'face face data)) |
| 133 |
(setq fs-para-acc (concat fs-para-acc data)))) |
| 134 |
|
| 135 |
|
| 136 |
(defun fs-output-para (text first-indent indent hang-from literal) |
| 137 |
(sgml-push-to-string text) |
| 138 |
(let ((indent-tabs-mode nil) |
| 139 |
(fill-prefix (make-string indent ? ))) |
| 140 |
(cond |
| 141 |
(literal |
| 142 |
(goto-char (point-max)) |
| 143 |
(unless (bolp) |
| 144 |
(insert ?\n)) |
| 145 |
(goto-char (point-min)) |
| 146 |
(while (not (eobp)) |
| 147 |
(insert fill-prefix) |
| 148 |
(beginning-of-line 2))) |
| 149 |
(t |
| 150 |
(while (re-search-forward "[ \t\n\r]+" nil t) |
| 151 |
(replace-match " ")) |
| 152 |
(goto-char (point-min)) |
| 153 |
(delete-horizontal-space) |
| 154 |
(insert |
| 155 |
(if hang-from |
| 156 |
hang-from |
| 157 |
(make-string (or first-indent indent) ? ))) |
| 158 |
(fill-region-as-paragraph (point-min) (point-max)) |
| 159 |
(goto-char (point-max)) |
| 160 |
(unless (bolp) |
| 161 |
(insert ?\n)))) |
| 162 |
(fs-add-output (buffer-string) (fs-char 'justification))) |
| 163 |
(sgml-pop-entity) |
| 164 |
(sit-for 0)) |
| 165 |
|
| 166 |
(defun fs-paraform-phrase (e) |
| 167 |
(sgml-map-content e |
| 168 |
(function fs-paraform-phrase) |
| 169 |
(function fs-paraform-data) |
| 170 |
nil |
| 171 |
(function fs-paraform-entity))) |
| 172 |
|
| 173 |
(defun fs-paraform-entity (entity) |
| 174 |
(let ((entity-map (fs-char 'entity-map)) |
| 175 |
(text nil)) |
| 176 |
(when entity-map |
| 177 |
(setq text |
| 178 |
(loop for (name val) on entity-map by 'cddr |
| 179 |
thereis (if (equal name (sgml-entity-name entity)) |
| 180 |
val)))) |
| 181 |
(unless text |
| 182 |
(setq text (sgml-entity-text entity))) |
| 183 |
(fs-paraform-data text))) |
| 184 |
|
| 185 |
;;;; Style driven engine |
| 186 |
|
| 187 |
(defvar fs-style "style.fs" |
| 188 |
"*Style sheet to use for `style-format'. |
| 189 |
The value can be the style-sheet list, or it can be a file name |
| 190 |
\(string) of a file containing the style sheet or it can be the name |
| 191 |
\(symbol) of a variable containing the style sheet.") |
| 192 |
|
| 193 |
(put 'fs-style 'variable-interactive |
| 194 |
"fStyle file: ") |
| 195 |
|
| 196 |
(defvar fs-cached-styles nil) |
| 197 |
|
| 198 |
(defun fs-get-style (style) |
| 199 |
(cond ((stringp style) |
| 200 |
(sgml-cache-catalog style |
| 201 |
'fs-cached-styles |
| 202 |
(function (lambda () |
| 203 |
(read (current-buffer)))))) |
| 204 |
((symbolp style) |
| 205 |
(fs-get-style (symbol-value style))) |
| 206 |
((listp style) |
| 207 |
style) |
| 208 |
(t |
| 209 |
(error "Illegal style value: %s" style)))) |
| 210 |
|
| 211 |
(defun fs-engine (e) |
| 212 |
(fs-do-style e |
| 213 |
(cdr (or (assoc (sgml-element-gi e) fs-style) |
| 214 |
(assq t fs-style))))) |
| 215 |
|
| 216 |
(defun fs-do-style (fs-current-element style) |
| 217 |
(let ((hang-from (eval (plist-get style 'hang-from)))) |
| 218 |
(when hang-from |
| 219 |
(setq fs-hang-from |
| 220 |
(format "%s%s " |
| 221 |
(make-string |
| 222 |
(or (fs-char 'hang-left) (fs-char 'left)) |
| 223 |
? ) |
| 224 |
hang-from)))) |
| 225 |
(let ((fs-char (nconc |
| 226 |
(loop for st on style by 'cddr |
| 227 |
unless (memq (car st) fs-special-styles) |
| 228 |
collect (cons (car st) |
| 229 |
(eval (cadr st)))) |
| 230 |
fs-char))) |
| 231 |
(when (plist-get style 'block) |
| 232 |
(fs-para) |
| 233 |
(fs-addvspace (or (plist-get style 'top) |
| 234 |
(fs-char 'default-top)))) |
| 235 |
(let ((before (plist-get style 'before))) |
| 236 |
(when before |
| 237 |
(fs-do-style e before))) |
| 238 |
(let ((fs-style |
| 239 |
(append (plist-get style 'sub-style) |
| 240 |
fs-style))) |
| 241 |
(cond ((plist-get style 'text) |
| 242 |
(let ((text (eval (plist-get style 'text)))) |
| 243 |
(when (stringp text) |
| 244 |
(fs-paraform-data text)))) |
| 245 |
(t |
| 246 |
(sgml-map-content e |
| 247 |
(function fs-engine) |
| 248 |
(function fs-paraform-data) |
| 249 |
nil |
| 250 |
(function fs-paraform-entity))))) |
| 251 |
(let ((title (plist-get style 'title))) |
| 252 |
(when title |
| 253 |
(setq title (eval title)) |
| 254 |
(save-excursion |
| 255 |
(set-buffer fs-buffer) |
| 256 |
(setq fs-title title)))) |
| 257 |
(let ((after (plist-get style 'after))) |
| 258 |
(when after |
| 259 |
(fs-do-style e after))) |
| 260 |
(when (plist-get style 'block) |
| 261 |
(fs-para) |
| 262 |
(fs-addvspace (or (plist-get style 'bottom) |
| 263 |
(fs-char 'default-bottom)))))) |
| 264 |
|
| 265 |
|
| 266 |
(defun fs-clear () |
| 267 |
(setq fs-para-acc "" |
| 268 |
fs-hang-from nil |
| 269 |
fs-first-indent nil |
| 270 |
fs-left-indent nil |
| 271 |
fs-vspace 0) ) |
| 272 |
|
| 273 |
|
| 274 |
(defun fs-setup-buffer () |
| 275 |
(save-excursion |
| 276 |
(let ((orig-filename (buffer-file-name (current-buffer)))) |
| 277 |
(set-buffer fs-buffer) |
| 278 |
(erase-buffer) |
| 279 |
(setq ps-left-header |
| 280 |
'(fs-title fs-filename)) |
| 281 |
(make-local-variable 'fs-filename) |
| 282 |
(setq fs-filename (file-name-nondirectory orig-filename)) |
| 283 |
(make-local-variable 'fs-title) |
| 284 |
(setq fs-title "")))) |
| 285 |
|
| 286 |
(defun fs-wrapper (buffer-name thunk) |
| 287 |
(fs-clear) |
| 288 |
(let ((fs-style (fs-get-style fs-style)) |
| 289 |
(fs-buffer (get-buffer-create buffer-name))) |
| 290 |
(fs-setup-buffer) |
| 291 |
(funcall thunk) |
| 292 |
(fs-para) |
| 293 |
(save-excursion |
| 294 |
(set-buffer fs-buffer) |
| 295 |
(goto-char (point-min))) |
| 296 |
fs-buffer)) |
| 297 |
|
| 298 |
|
| 299 |
;;;###autoload |
| 300 |
(defun style-format () |
| 301 |
(interactive) |
| 302 |
(fs-wrapper "*Formatted*" |
| 303 |
(lambda () |
| 304 |
(display-buffer fs-buffer) |
| 305 |
(fs-engine (sgml-top-element))))) |
| 306 |
|
| 307 |
|
| 308 |
;;;; Helper functions for use in style sheet |
| 309 |
|
| 310 |
(defun fs-element (&rest moves) |
| 311 |
"Find current or related element." |
| 312 |
(let ((element fs-current-element)) |
| 313 |
(while moves |
| 314 |
(case (pop moves) |
| 315 |
(parent (setq element (sgml-element-parent element))) |
| 316 |
(next (setq element (sgml-element-next element))) |
| 317 |
(child (setq element (sgml-element-content element))))) |
| 318 |
element)) |
| 319 |
|
| 320 |
(defun fs-element-content (&optional e) |
| 321 |
(unless e (setq e (fs-element))) |
| 322 |
(let ((fs-para-acc "") fs-first-indent fs-left-indent) |
| 323 |
(sgml-map-content e |
| 324 |
(function fs-paraform-phrase) |
| 325 |
(function fs-paraform-data) |
| 326 |
nil |
| 327 |
(function fs-paraform-entity)) |
| 328 |
fs-para-acc)) |
| 329 |
|
| 330 |
(defun fs-attval (name &optional element) |
| 331 |
(sgml-element-attval (if element element (fs-element)) |
| 332 |
name)) |
| 333 |
|
| 334 |
(defun fs-child-number (&optional element) |
| 335 |
(let* ((element (or element (fs-element))) |
| 336 |
(parent (sgml-element-parent element)) |
| 337 |
(child (sgml-element-content parent)) |
| 338 |
(number 0)) |
| 339 |
(while (and child (not (eq child element))) |
| 340 |
(incf number) |
| 341 |
(setq child (sgml-element-next child))) |
| 342 |
number)) |
| 343 |
|
| 344 |
|
| 345 |
(defun fs-element-with-id (id) |
| 346 |
(block func |
| 347 |
(let ((element (sgml-top-element))) |
| 348 |
(while (not (sgml-off-top-p element)) |
| 349 |
(let ((attlist (sgml-element-attlist element))) |
| 350 |
(loop for attdecl in attlist |
| 351 |
if (eq 'ID (sgml-attdecl-declared-value attdecl)) |
| 352 |
do (if (compare-strings id nil nil |
| 353 |
(sgml-element-attval |
| 354 |
element (sgml-attdecl-name attdecl)) |
| 355 |
nil nil) |
| 356 |
(return-from func element)))) |
| 357 |
;; Next element |
| 358 |
(if (sgml-element-content element) |
| 359 |
(setq element (sgml-element-content element)) |
| 360 |
(while (null (sgml-element-next element)) |
| 361 |
(setq element (sgml-element-parent element)) |
| 362 |
(if (sgml-off-top-p element) |
| 363 |
(return-from func nil))) |
| 364 |
(setq element (sgml-element-next element))))) |
| 365 |
nil)) |
| 366 |
|
| 367 |
|
| 368 |
(defun fs-split-tokens (s) |
| 369 |
"Split a string S into a list of tokens." |
| 370 |
(let ((result nil)) |
| 371 |
(sgml-push-to-string s) |
| 372 |
(while (not (eobp)) |
| 373 |
(skip-syntax-forward "-") |
| 374 |
(let ((start (point))) |
| 375 |
(skip-syntax-forward "^-") |
| 376 |
(when (/= start (point)) |
| 377 |
(push (buffer-substring-no-properties start (point)) |
| 378 |
result)))) |
| 379 |
(sgml-pop-entity) |
| 380 |
(nreverse result))) |
| 381 |
|
| 382 |
|
| 383 |
;;; fs.el ends here |