| 1 |
;;; mgp.el --- Magic Point tool |
| 2 |
;;; Copyright (C) 1999 Electrotechnical Lab., JAPAN |
| 3 |
|
| 4 |
;; Author: K.Handa <[email protected]> |
| 5 |
;; Created: 1999/04/28 |
| 6 |
;; Keywords: mgp, Magic Point |
| 7 |
|
| 8 |
;; This file is not part of GNU Emacs, but the same permissions apply. |
| 9 |
;; |
| 10 |
;; GNU Emacs is free software; you can redistribute it and/or modify |
| 11 |
;; it under the terms of the GNU General Public License as published by |
| 12 |
;; the Free Software Foundation; either version 2, or (at your option) |
| 13 |
;; any later version. |
| 14 |
;; |
| 15 |
;; GNU Emacs is distributed in the hope that it will be useful, |
| 16 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
;; GNU General Public License for more details. |
| 19 |
;; |
| 20 |
;; You should have received a copy of the GNU General Public License |
| 21 |
;; along with GNU Emacs; see the file COPYING. If not, write to the |
| 22 |
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 |
;; Boston, MA 02111-1307, USA. |
| 24 |
|
| 25 |
;;; Code: |
| 26 |
|
| 27 |
(eval-and-compile |
| 28 |
|
| 29 |
(or mule-version |
| 30 |
(error "This program requires Mule feature")) |
| 31 |
|
| 32 |
;; For compatibility between Mule 2.3 and Emacs 20. |
| 33 |
|
| 34 |
(defun mgp-buffer-file-coding-system () |
| 35 |
(if (boundp 'buffer-file-coding-system) |
| 36 |
(symbol-value 'buffer-file-coding-system) |
| 37 |
(symbol-value 'file-coding-system))) |
| 38 |
|
| 39 |
(or (fboundp 'set-buffer-file-coding-system) |
| 40 |
(defalias 'set-buffer-file-coding-system 'set-file-coding-system)) |
| 41 |
|
| 42 |
(or (fboundp 'coding-system-base) |
| 43 |
(defun coding-system-base (coding) |
| 44 |
(cond ((memq coding '(*ctext* *iso-8859-1*)) 'latin-1) |
| 45 |
((eq coding '*iso-8859-2*) 'latin-2) |
| 46 |
((eq coding '*iso-8859-3*) 'latin-3) |
| 47 |
((eq coding '*iso-8859-4*) 'latin-4) |
| 48 |
((eq coding '*iso-8859-5*) 'latin-5)))) |
| 49 |
|
| 50 |
(or (fboundp 'coding-system-equal) |
| 51 |
(defalias 'coding-system-equal 'eq)) |
| 52 |
|
| 53 |
(or (fboundp 'line-beginning-position) |
| 54 |
(defun line-beginning-position () |
| 55 |
(save-excursion |
| 56 |
(beginning-of-line) |
| 57 |
(point)))) |
| 58 |
|
| 59 |
(or (fboundp 'line-end-position) |
| 60 |
(defun line-end-position () |
| 61 |
(save-excursion |
| 62 |
(end-of-line) |
| 63 |
(point)))) |
| 64 |
|
| 65 |
) |
| 66 |
|
| 67 |
(defvar mgp-version "0.90") |
| 68 |
|
| 69 |
(defvar mgp-paper-size "a4") |
| 70 |
(defvar mgp-image-scale-factor 0.95) |
| 71 |
(defvar mgp-landscape t) |
| 72 |
(defvar mgp-lib-directory "/usr/X11R6/lib/X11/mgp") |
| 73 |
(defvar mgp-disable-color t) |
| 74 |
|
| 75 |
(defconst mgp-paper-size-list '("a5" "a4" "b5" "letter" "legal" "executive")) |
| 76 |
(defconst mgp-predefined-color-list |
| 77 |
'("black" "white" "red" "green" "blue" "yellow" "cyan" "magenta" "gray")) |
| 78 |
(defvar mgp-color-list nil) |
| 79 |
|
| 80 |
(defvar mgp-entex-inbuf nil) |
| 81 |
(defvar mgp-entex-outbuf nil) |
| 82 |
|
| 83 |
(defvar mgp-entex-source nil) |
| 84 |
|
| 85 |
(defun mgp-entex-file (file) |
| 86 |
(interactive "fMGP source file: ") |
| 87 |
(setq mgp-entex-source file) |
| 88 |
(mgp-entex-setup-color-data) |
| 89 |
(mgp-entex-setup-buffer) |
| 90 |
(mgp-entex-process-buffer) |
| 91 |
(switch-to-buffer mgp-entex-outbuf) |
| 92 |
(goto-char (point-min)) |
| 93 |
(save-buffer)) |
| 94 |
|
| 95 |
(defun mgp-entex-buffer () |
| 96 |
(interactive) |
| 97 |
(setq mgp-entex-source (current-buffer)) |
| 98 |
(mgp-entex-setup-color-data) |
| 99 |
(mgp-entex-setup-buffer) |
| 100 |
(mgp-entex-process-buffer) |
| 101 |
(switch-to-buffer mgp-entex-outbuf) |
| 102 |
(goto-char (point-min)) |
| 103 |
(save-buffer)) |
| 104 |
|
| 105 |
(defvar mgp-max-color-value nil) |
| 106 |
|
| 107 |
(defun mgp-entex-setup-color-data () |
| 108 |
(if (eq window-system 'x) |
| 109 |
(setq mgp-max-color-value (car (x-color-values "white"))))) |
| 110 |
|
| 111 |
(defmacro mgp-with-output-buffer (&rest body) |
| 112 |
`(let ((buf (current-buffer))) |
| 113 |
(or (eq buf mgp-entex-outbuf) |
| 114 |
(set-buffer mgp-entex-outbuf)) |
| 115 |
(unwind-protect |
| 116 |
(progn ,@body) |
| 117 |
(or (eq buf mgp-entex-outbuf) |
| 118 |
(set-buffer buf))))) |
| 119 |
|
| 120 |
(put 'mgp-with-output-buffer 'lisp-indent-function 0) |
| 121 |
|
| 122 |
(defun mgp-entex-setup-buffer () |
| 123 |
(let (filename dir coding) |
| 124 |
(save-excursion |
| 125 |
(setq mgp-entex-inbuf (get-buffer-create "*mgp*")) |
| 126 |
(set-buffer mgp-entex-inbuf) |
| 127 |
(erase-buffer) |
| 128 |
(insert "%%\n") |
| 129 |
(if (bufferp mgp-entex-source) |
| 130 |
(progn |
| 131 |
(save-excursion |
| 132 |
(set-buffer mgp-entex-source) |
| 133 |
(setq filename (concat (file-name-sans-extension |
| 134 |
(or buffer-file-name (buffer-name))) |
| 135 |
".tex") |
| 136 |
dir default-directory |
| 137 |
coding (mgp-buffer-file-coding-system))) |
| 138 |
(set-buffer-file-coding-system coding) |
| 139 |
(setq default-directory dir) |
| 140 |
(insert-buffer mgp-entex-source)) |
| 141 |
(setq filename |
| 142 |
(concat (file-name-sans-extension mgp-entex-source) ".tex") |
| 143 |
dir |
| 144 |
(file-name-directory (expand-file-name mgp-entex-source))) |
| 145 |
(insert-file-contents (expand-file-name mgp-entex-source)) |
| 146 |
(setq coding (mgp-buffer-file-coding-system)) |
| 147 |
(setq default-directory dir)) |
| 148 |
|
| 149 |
(goto-char (point-min)) |
| 150 |
(while (and (search-forward "\n%include" nil t) |
| 151 |
(looking-at " +\\([^ ]+\\)")) |
| 152 |
(let ((incfile (mgp-entex-get-string-arg)) |
| 153 |
(pos (line-beginning-position)) |
| 154 |
len) |
| 155 |
(forward-line 1) |
| 156 |
(delete-region pos (point)) |
| 157 |
(if (or (file-readable-p incfile) |
| 158 |
(progn |
| 159 |
(setq incfile (expand-file-name incfile mgp-lib-directory)) |
| 160 |
(file-readable-p incfile))) |
| 161 |
(progn |
| 162 |
(message "Inserting file %s..." incfile) |
| 163 |
(insert-file-contents incfile))) |
| 164 |
(goto-char (point-min)))) |
| 165 |
(goto-char (point-min)) |
| 166 |
(while (search-forward "\\\n" nil t) |
| 167 |
(delete-char -2)) |
| 168 |
(goto-char (point-min)) |
| 169 |
(setq mgp-entex-outbuf |
| 170 |
(get-buffer-create (file-name-nondirectory filename))) |
| 171 |
(set-buffer mgp-entex-outbuf) |
| 172 |
(erase-buffer) |
| 173 |
(setq default-directory dir |
| 174 |
buffer-file-name filename) |
| 175 |
(set-buffer-file-coding-system coding)))) |
| 176 |
|
| 177 |
(defvar mgp-entex-continue nil) |
| 178 |
(defvar mgp-entex-linehead nil) |
| 179 |
(defvar mgp-entex-prefix "") |
| 180 |
|
| 181 |
(defun mgp-entex-process-buffer () |
| 182 |
(message "Converting MGP to LaTeX...") |
| 183 |
(mgp-entex-init-global-setting) |
| 184 |
(save-excursion |
| 185 |
(set-buffer mgp-entex-inbuf) |
| 186 |
(while (not (or (eobp) (looking-at "^%page"))) |
| 187 |
(mgp-entex-process-preamble) |
| 188 |
(forward-line 1)) |
| 189 |
(mgp-entex-start-body) |
| 190 |
(forward-line 1) |
| 191 |
(let ((page 0) |
| 192 |
line) |
| 193 |
(while (not (eobp)) |
| 194 |
(mgp-entex-init-local-setting) |
| 195 |
(setq page (1+ page)) |
| 196 |
(message "Page %d" page) |
| 197 |
(setq line 1) |
| 198 |
(mgp-entex-start-page) |
| 199 |
(setq mgp-entex-linehead t |
| 200 |
mgp-entex-prefix "") |
| 201 |
(while (not (or (eobp) (looking-at "^%page"))) |
| 202 |
(setq line (mgp-entex-process-page line)) |
| 203 |
(forward-line 1)) |
| 204 |
(mgp-entex-finish-page line) |
| 205 |
(forward-line 1)) |
| 206 |
(mgp-entex-finish-body)))) |
| 207 |
|
| 208 |
(defconst mgp-setting-num 128) |
| 209 |
(defvar mgp-global-setting nil) |
| 210 |
(defvar mgp-local-setting nil) |
| 211 |
|
| 212 |
(defun mgp-entex-init-global-setting () |
| 213 |
(let ((i 2) |
| 214 |
(len (* mgp-setting-num 2))) |
| 215 |
(setq mgp-global-setting (make-vector len nil)) |
| 216 |
(aset mgp-global-setting 1 (list 'font nil)) |
| 217 |
(while (< i len) |
| 218 |
(aset mgp-global-setting i (list 'font nil)) |
| 219 |
(setq i (1+ i))))) |
| 220 |
|
| 221 |
(defun mgp-entex-init-local-setting () |
| 222 |
(let ((len (length mgp-global-setting)) |
| 223 |
(i 0)) |
| 224 |
(if (< (length mgp-local-setting) len) |
| 225 |
(setq mgp-local-setting (make-vector len nil))) |
| 226 |
(while (< i len) |
| 227 |
(aset mgp-local-setting i (copy-sequence (aref mgp-global-setting i))) |
| 228 |
(setq i (1+ i))))) |
| 229 |
|
| 230 |
(defun mgp-entex-parse-cmd (globalp defaultp index) |
| 231 |
(save-restriction |
| 232 |
(narrow-to-region (point) (line-end-position)) |
| 233 |
(while (re-search-forward "[a-z]+" nil t) |
| 234 |
(let ((func (intern (concat "mgp-entex-cmd-" |
| 235 |
(downcase (match-string 0)))))) |
| 236 |
(if (fboundp func) |
| 237 |
(funcall func globalp defaultp index))) |
| 238 |
(skip-chars-forward ",") |
| 239 |
(or (eobp) |
| 240 |
(forward-char 1))))) |
| 241 |
|
| 242 |
(defun mgp-entex-process-preamble () |
| 243 |
(if (looking-at "^%\\(default\\|tab\\) *") |
| 244 |
(progn |
| 245 |
(forward-char 1) |
| 246 |
(or (eolp) |
| 247 |
(mgp-entex-parse-cmd 'global nil nil))))) |
| 248 |
|
| 249 |
(defun mgp-entex-start-body () |
| 250 |
(mgp-with-output-buffer |
| 251 |
(insert "%%% This file was generated by mgp.el ") |
| 252 |
(insert (format "(Ver.%s).\n" mgp-version)) |
| 253 |
(insert "%%% Document source: ") |
| 254 |
(if (bufferp mgp-entex-source) |
| 255 |
(insert (format "buffer \"%s\"\n" (buffer-name mgp-entex-source))) |
| 256 |
(insert (format "file \"%s\"\n" mgp-entex-source))) |
| 257 |
(insert (format "\\documentclass[%spaper%s]{article}\n\\input{mgp.sty}\n" |
| 258 |
(if (member mgp-paper-size mgp-paper-size-list) |
| 259 |
mgp-paper-size |
| 260 |
"a4") |
| 261 |
(if mgp-landscape ",landscape" ""))) |
| 262 |
(or mgp-disable-color |
| 263 |
(progn |
| 264 |
(insert "\\usepackage{color}\n\\definecolor{gray}{gray}{0.001}") |
| 265 |
(setq mgp-color-list (cons "gray" mgp-predefined-color-list)))) |
| 266 |
(let ((coding (mgp-buffer-file-coding-system))) |
| 267 |
(if coding |
| 268 |
(let ((base (coding-system-base (mgp-buffer-file-coding-system)))) |
| 269 |
(cond ((coding-system-equal base 'latin-1) |
| 270 |
(insert "\\usepackage[latin1]{inputenc}\n")) |
| 271 |
((coding-system-equal base 'latin-2) |
| 272 |
(insert "\\usepackage[latin2]{inputenc}\n")) |
| 273 |
((coding-system-equal base 'latin-3) |
| 274 |
(insert "\\usepackage[latin3]{inputenc}\n")) |
| 275 |
((coding-system-equal base 'latin-5) |
| 276 |
(insert "\\usepackage[latin5]{inputenc}\n")))))) |
| 277 |
(insert "\\begin{document}\n"))) |
| 278 |
|
| 279 |
(defun mgp-entex-finish-body () |
| 280 |
(mgp-with-output-buffer |
| 281 |
(insert "\n\\end{document}\n"))) |
| 282 |
|
| 283 |
(defun mgp-entex-set-setting (globalp defaultp index prop val) |
| 284 |
(let ((table (if globalp mgp-global-setting mgp-local-setting))) |
| 285 |
(or defaultp |
| 286 |
(setq index (+ mgp-setting-num index))) |
| 287 |
(plist-put (aref table index) prop val))) |
| 288 |
|
| 289 |
(defun mgp-entex-get-setting (globalp defaultp index prop this-line-only) |
| 290 |
(let ((table (if globalp mgp-global-setting mgp-local-setting)) |
| 291 |
(limit 0) |
| 292 |
val) |
| 293 |
(or defaultp |
| 294 |
(setq index (+ mgp-setting-num index) |
| 295 |
limit mgp-setting-num)) |
| 296 |
(if this-line-only |
| 297 |
(setq limit index)) |
| 298 |
(while (and (>= index limit) (not val)) |
| 299 |
(setq val (plist-get (aref table index) prop)) |
| 300 |
(setq index (1- index))) |
| 301 |
val)) |
| 302 |
|
| 303 |
(defun mgp-entex-get-string-arg () |
| 304 |
(skip-chars-forward " \t") |
| 305 |
(if (= (following-char) ?\") |
| 306 |
(read (current-buffer)) |
| 307 |
(if (looking-at "[^, \n]+") |
| 308 |
(progn |
| 309 |
(goto-char (match-end 0)) |
| 310 |
(match-string 0))))) |
| 311 |
|
| 312 |
(defun mgp-entex-get-number-arg () |
| 313 |
(if (looking-at " *[0-9.]+") |
| 314 |
(let ((num (string-to-int (match-string 0)))) |
| 315 |
(goto-char (match-end 0)) |
| 316 |
num))) |
| 317 |
|
| 318 |
(defun mgp-entex-cmd-default (globalp &rest ignore) |
| 319 |
(let ((index (mgp-entex-get-number-arg))) |
| 320 |
(goto-char (match-end 0)) |
| 321 |
(mgp-entex-parse-cmd globalp 'default index))) |
| 322 |
|
| 323 |
(put 'mgp-entex-cmd-default 'mgp-entex-special t) |
| 324 |
|
| 325 |
(defun mgp-entex-cmd-tab (globalp &rest ignore) |
| 326 |
(let ((index (mgp-entex-get-number-arg))) |
| 327 |
(goto-char (match-end 0)) |
| 328 |
(mgp-entex-parse-cmd globalp nil index))) |
| 329 |
|
| 330 |
(put 'mgp-entex-cmd-tab 'mgp-entex-special t) |
| 331 |
|
| 332 |
(defvar mgp-mark-position nil) |
| 333 |
(defvar mgp-current-line nil) |
| 334 |
|
| 335 |
(defun mgp-entex-cmd-mark (globalp defaultp index) |
| 336 |
(setq mgp-mark-position |
| 337 |
(cons mgp-current-line |
| 338 |
(mgp-with-output-buffer (line-beginning-position))))) |
| 339 |
|
| 340 |
(defun mgp-entex-cmd-again (globalp defaultp index) |
| 341 |
(mgp-entex-set-setting globalp defaultp index 'again t)) |
| 342 |
|
| 343 |
(defun mgp-entex-cmd-fore (globalp defaultp index) |
| 344 |
(let ((arg (mgp-entex-get-string-arg))) |
| 345 |
(if arg |
| 346 |
(mgp-entex-set-setting globalp defaultp index 'fore arg)))) |
| 347 |
|
| 348 |
(defun mgp-entex-cmd-back (globalp defaultp index) |
| 349 |
(let ((arg (mgp-entex-get-string-arg))) |
| 350 |
(if arg |
| 351 |
(mgp-entex-set-setting globalp defaultp index 'back arg)))) |
| 352 |
|
| 353 |
(defun mgp-entex-cmd-size (globalp defaultp index) |
| 354 |
(let ((arg (mgp-entex-get-number-arg))) |
| 355 |
(if arg |
| 356 |
(mgp-entex-set-setting globalp defaultp index 'size arg)))) |
| 357 |
|
| 358 |
(defun mgp-entex-cmd-center (globalp defaultp index) |
| 359 |
(mgp-entex-set-setting globalp defaultp index 'align 'center)) |
| 360 |
|
| 361 |
(defun mgp-entex-cmd-left (globalp defaultp index) |
| 362 |
(mgp-entex-set-setting globalp defaultp index 'align 'left)) |
| 363 |
|
| 364 |
(defun mgp-entex-cmd-leftfill (globalp defaultp index) |
| 365 |
(mgp-entex-set-setting globalp defaultp index 'align 'left)) |
| 366 |
|
| 367 |
(defun mgp-entex-cmd-right (globalp defaultp index) |
| 368 |
(mgp-entex-set-setting globalp defaultp index 'align 'right)) |
| 369 |
|
| 370 |
(defun mgp-entex-cmd-vgap (globalp defaultp index) |
| 371 |
(let ((arg (mgp-entex-get-number-arg))) |
| 372 |
(if arg |
| 373 |
(mgp-entex-set-setting globalp defaultp index 'vgap arg)))) |
| 374 |
|
| 375 |
(defun mgp-entex-cmd-font (globalp defaultp index) |
| 376 |
(let ((arg (mgp-entex-get-string-arg))) |
| 377 |
(if arg |
| 378 |
(mgp-entex-set-setting globalp defaultp index 'font arg)))) |
| 379 |
|
| 380 |
(defun mgp-entex-cmd-xfont (globalp defaultp index) |
| 381 |
(let ((arg (mgp-entex-get-string-arg))) |
| 382 |
(if arg |
| 383 |
(mgp-entex-set-setting globalp defaultp index 'font arg)))) |
| 384 |
|
| 385 |
(defun mgp-entex-cmd-cont (globalp defaultp index) |
| 386 |
(mgp-entex-set-setting globalp defaultp index 'cont t)) |
| 387 |
|
| 388 |
(defun mgp-entex-cmd-nodefault (globalp defaultp index) |
| 389 |
(if globalp |
| 390 |
(error "%nodefault can't be used in preamble")) |
| 391 |
(let ((len (length mgp-local-setting)) |
| 392 |
(i 2)) |
| 393 |
(aset mgp-local-setting 1 (list 'font nil)) |
| 394 |
(while (< i len) |
| 395 |
(aset mgp-local-setting i (list 'font nil)) |
| 396 |
(setq i (1+ i))))) |
| 397 |
|
| 398 |
(defun mgp-entex-cmd-bar (globalp defaultp index) |
| 399 |
(let ((color (mgp-entex-get-string-arg)) |
| 400 |
(width (or (mgp-entex-get-number-arg) 10)) |
| 401 |
(start (or (mgp-entex-get-number-arg) 0)) |
| 402 |
(length (or (mgp-entex-get-number-arg) 100))) |
| 403 |
(mgp-entex-set-setting globalp defaultp index 'bar |
| 404 |
(list color width start length)))) |
| 405 |
|
| 406 |
(defun mgp-entex-cmd-image (globalp defaultp index) |
| 407 |
(let ((arg1 (mgp-entex-get-string-arg))) |
| 408 |
(if arg1 |
| 409 |
(let ((arg2 (mgp-entex-get-string-arg)) |
| 410 |
(arg3 (mgp-entex-get-string-arg)) |
| 411 |
(arg4 (mgp-entex-get-string-arg)) |
| 412 |
(arg5 (mgp-entex-get-string-arg))) |
| 413 |
;;; SYNTAX for %image is documented as below: |
| 414 |
;;; %image "imagefile" <numcolor> <xzoomrate> <yzoomrate> <zoomflag> |
| 415 |
;;; %image "imagefile" [<numcolor>] <screensize> |
| 416 |
;;; but it seems the second form should actually be: |
| 417 |
;;; %image "imagefile" [ [<numcolor>] <screensize> ] |
| 418 |
(if arg4 |
| 419 |
(setq arg2 (and arg2 (string-to-int arg2)) |
| 420 |
arg3 (and arg3 (string-to-int arg3)) |
| 421 |
arg4 (and arg4 (string-to-int arg4)) |
| 422 |
arg5 (and arg5 (string-to-int arg5))) |
| 423 |
(if arg3 |
| 424 |
(setq arg2 (string-to-int arg2)) |
| 425 |
(setq arg3 arg2 arg2 nil))) |
| 426 |
(mgp-entex-set-setting globalp defaultp index 'image |
| 427 |
(list arg1 arg2 arg3 arg4 arg5)))))) |
| 428 |
|
| 429 |
(defun mgp-entex-cmd-prefix (globalp defaultp index) |
| 430 |
(let ((arg (mgp-entex-get-string-arg))) |
| 431 |
(if arg |
| 432 |
(mgp-entex-set-setting globalp defaultp index 'prefix arg)))) |
| 433 |
|
| 434 |
(defun mgp-entex-cmd-icon (globalp defaultp index) |
| 435 |
(let* ((type (mgp-entex-get-string-arg)) |
| 436 |
;; Currently color and size are just ignored. |
| 437 |
(color (mgp-entex-get-string-arg)) |
| 438 |
(size (mgp-entex-get-number-arg)) |
| 439 |
(str (cond ((string= type "arc") "$\\bullet$") |
| 440 |
((string= type "box") "$\\Box$") |
| 441 |
((string= type "dia") "$\\Diamond$") |
| 442 |
((string= type "delta1") "$\\bigtriangleup$") |
| 443 |
((string= type "delta2") "$\\bigtriangledown$") |
| 444 |
((string= type "delta3") "$\\triangleright$") |
| 445 |
((string= type "delta4") "$\\triangleleft$") |
| 446 |
(t "$\\diamond$")))) |
| 447 |
(mgp-entex-set-setting globalp defaultp index 'icon str))) |
| 448 |
|
| 449 |
(defconst mgp-entex-special-characters |
| 450 |
'((?# . "\\#") |
| 451 |
(?$ . "\\$") |
| 452 |
(?% . "\\%") |
| 453 |
(?& . "\\&") |
| 454 |
(?_ . "\\_") |
| 455 |
(?@ . "{\\makeatletter@\\makeatother}") |
| 456 |
(?< . "$<$") |
| 457 |
(?> . "$>$") |
| 458 |
(?~ . "\\~{ }"))) |
| 459 |
|
| 460 |
(defconst mgp-entex-space-width 4) |
| 461 |
|
| 462 |
(defun mgp-entex-handle-string (str prefix &optional size force-indent) |
| 463 |
(mgp-with-output-buffer |
| 464 |
(let ((len (length str)) |
| 465 |
(indent 0) |
| 466 |
i j) |
| 467 |
(if prefix |
| 468 |
(progn |
| 469 |
(if (string-match "^ +" prefix) |
| 470 |
(setq indent (match-end 0) |
| 471 |
prefix (substring prefix indent))))) |
| 472 |
(if (and (not force-indent) |
| 473 |
(= indent 0) (= (length prefix) 0) (= len 0)) |
| 474 |
(if (eq mgp-entex-linehead t) |
| 475 |
(setq mgp-entex-linehead 'empty)) |
| 476 |
(if (or prefix force-indent) |
| 477 |
(insert "{")) |
| 478 |
(if size |
| 479 |
(mgp-entex-handle-size size)) |
| 480 |
(if (or (> indent 0) (> (length prefix) 0)) |
| 481 |
(insert (format "\\mgpi{%d}{%s}" indent prefix))) |
| 482 |
(if (string-match "^ +" str) |
| 483 |
(if (< (match-end 0) len) |
| 484 |
(progn |
| 485 |
(setq i (match-end 0) j i) |
| 486 |
(insert (format "\\mgps{%d}" i))) |
| 487 |
(setq i len j i)) |
| 488 |
(setq i 0 j 0)) |
| 489 |
(while (< i len) |
| 490 |
(let ((slot (assq (aref str i) mgp-entex-special-characters))) |
| 491 |
(if slot |
| 492 |
(progn |
| 493 |
(if (< j i) |
| 494 |
(insert (substring str j i))) |
| 495 |
(insert (cdr slot)) |
| 496 |
(setq j (1+ i))))) |
| 497 |
(setq i (1+ i))) |
| 498 |
(if (< j len) |
| 499 |
(insert (substring str j))) |
| 500 |
(setq mgp-entex-linehead nil))))) |
| 501 |
|
| 502 |
(defun mgp-entex-process-page (line) |
| 503 |
(setq mgp-current-line line) |
| 504 |
(if (memq (following-char) '(?% ?#)) |
| 505 |
(progn |
| 506 |
(forward-char 1) |
| 507 |
(if (or (eolp) (= (preceding-char) ?#) (= (following-char) ?%)) |
| 508 |
nil |
| 509 |
(mgp-entex-parse-cmd nil 'default line))) |
| 510 |
(let* ((str (buffer-substring (point) (line-end-position))) |
| 511 |
(plist (aref mgp-local-setting line)) |
| 512 |
(continued (plist-get plist 'cont))) |
| 513 |
(if continued |
| 514 |
(plist-put plist 'cont nil) |
| 515 |
(mgp-with-output-buffer |
| 516 |
(cond ((eq mgp-entex-linehead 'empty) |
| 517 |
(insert "\\nl\n") |
| 518 |
(setq mgp-entex-linehead t)) |
| 519 |
((not mgp-entex-linehead) |
| 520 |
(insert "\\nl}\n") |
| 521 |
(setq mgp-entex-linehead t))) |
| 522 |
(setq line (1+ line)))) |
| 523 |
(let (func) |
| 524 |
(while plist |
| 525 |
(setq func (intern (concat "mgp-entex-handle-" |
| 526 |
(symbol-name (car plist))))) |
| 527 |
(if (fboundp func) |
| 528 |
(funcall func (nth 1 plist))) |
| 529 |
(setq plist (nthcdr 2 plist)))) |
| 530 |
(let ((indent 0) |
| 531 |
(len (length str))) |
| 532 |
(or continued |
| 533 |
(while (and (< indent len) (= (aref str indent) ?\t)) |
| 534 |
(setq indent (1+ indent)))) |
| 535 |
(if (> indent 0) |
| 536 |
(mgp-entex-handle-indent indent str) |
| 537 |
(mgp-entex-handle-string str |
| 538 |
(and (not continued) mgp-entex-prefix)))))) |
| 539 |
line) |
| 540 |
|
| 541 |
(defun mgp-entex-start-page () |
| 542 |
(mgp-with-output-buffer |
| 543 |
(insert "\\parbox{\\hsize}{\n") |
| 544 |
)) |
| 545 |
|
| 546 |
(defun mgp-entex-finish-page (line) |
| 547 |
(mgp-with-output-buffer |
| 548 |
(let ((plist (aref mgp-local-setting line)) |
| 549 |
func) |
| 550 |
(while plist |
| 551 |
(setq func (intern (concat "mgp-entex-handle-" |
| 552 |
(symbol-name (car plist))))) |
| 553 |
(if (fboundp func) |
| 554 |
(funcall func (nth 1 plist))) |
| 555 |
(setq plist (nthcdr 2 plist)))) |
| 556 |
(cond ((eq mgp-entex-linehead 'empty) |
| 557 |
(insert "\\nl\n") |
| 558 |
(setq mgp-entex-linehead t)) |
| 559 |
((not mgp-entex-linehead) |
| 560 |
(insert "\\nl}\n") |
| 561 |
(setq mgp-entex-linehead t))) |
| 562 |
(insert "}\n\\newpage\n") |
| 563 |
(setq mgp-entex-linehead t))) |
| 564 |
|
| 565 |
(defun mgp-entex-handle-again (&rest arg) |
| 566 |
(mgp-with-output-buffer |
| 567 |
(if mgp-mark-position |
| 568 |
(progn |
| 569 |
;;(setq line (car mgp-mark-position)) |
| 570 |
(goto-char (cdr mgp-mark-position)) |
| 571 |
(delete-region (point) (point-max)))))) |
| 572 |
|
| 573 |
(defun mgp-entex-handle-font (arg) |
| 574 |
(if arg |
| 575 |
(mgp-with-output-buffer |
| 576 |
(insert "\\normalfont") |
| 577 |
(cond ((string-match "bold-r" arg) (insert "\\bfseries")) |
| 578 |
((string-match "bold-[oi]" arg) (insert "\\bfseries\\itshape")) |
| 579 |
((string-match "medium-[oi]" arg) (insert "\\itshape"))) |
| 580 |
(insert " ")))) |
| 581 |
|
| 582 |
(defun mgp-entex-define-color (arg) |
| 583 |
(or (member arg mgp-color-list) |
| 584 |
(and (eq window-system 'x) |
| 585 |
(let ((color-values (x-color-values arg))) |
| 586 |
(insert (format "\\definecolor{%s}{rgb}{" arg)) |
| 587 |
(while color-values |
| 588 |
(insert (format "%.3f" (/ (float (car color-values)) |
| 589 |
mgp-max-color-value))) |
| 590 |
(setq color-values (cdr color-values)) |
| 591 |
(if color-values |
| 592 |
(insert ","))) |
| 593 |
(insert "}") |
| 594 |
t)))) |
| 595 |
|
| 596 |
(defun mgp-entex-handle-back (arg) |
| 597 |
(or mgp-disable-color |
| 598 |
(mgp-with-output-buffer |
| 599 |
(if (string= arg "black") |
| 600 |
(setq arg "gray")) |
| 601 |
(if (mgp-entex-define-color arg) |
| 602 |
(insert (format "\\pagecolor{%s}" arg)))))) |
| 603 |
|
| 604 |
(defun mgp-entex-handle-fore (arg) |
| 605 |
(or mgp-disable-color |
| 606 |
(mgp-with-output-buffer |
| 607 |
(if (mgp-entex-define-color arg) |
| 608 |
(insert (format "\\color{%s}" arg)))))) |
| 609 |
|
| 610 |
(defun mgp-entex-handle-size (arg) |
| 611 |
(mgp-with-output-buffer |
| 612 |
(cond ((>= arg 10) (insert "\\Huge ")) |
| 613 |
((>= arg 7) (insert "\\huge ")) |
| 614 |
((>= arg 6) (insert "\\LARGE ")) |
| 615 |
((>= arg 5) (insert "\\Large ")) |
| 616 |
((>= arg 4) (insert "\\large ")) |
| 617 |
((>= arg 3) (insert "\\normalsize ")) |
| 618 |
(t (insert "\\tiny "))))) |
| 619 |
|
| 620 |
(defun mgp-entex-handle-icon (arg) |
| 621 |
(mgp-with-output-buffer |
| 622 |
(insert arg))) |
| 623 |
|
| 624 |
(defun mgp-entex-handle-indent (indent str) |
| 625 |
(mgp-with-output-buffer |
| 626 |
(let ((prefix (or (mgp-entex-get-setting nil nil indent 'prefix t) " ")) |
| 627 |
(size (mgp-entex-get-setting nil nil indent 'size t)) |
| 628 |
(icon (mgp-entex-get-setting nil nil indent 'icon t))) |
| 629 |
(if icon |
| 630 |
(setq prefix (concat prefix icon))) |
| 631 |
(mgp-entex-handle-string (substring str indent) prefix size t)))) |
| 632 |
|
| 633 |
(defun mgp-entex-handle-prefix (arg) |
| 634 |
(setq mgp-entex-prefix arg)) |
| 635 |
|
| 636 |
(defun mgp-entex-handle-align (arg) |
| 637 |
(mgp-with-output-buffer |
| 638 |
(insert |
| 639 |
(cond ((eq arg 'center) "\\centering ") |
| 640 |
((eq arg 'right) "\\raggedleft ") |
| 641 |
(t "\\raggedright "))))) |
| 642 |
|
| 643 |
(defun mgp-entex-handle-vgap (arg) |
| 644 |
;; Currently we don't support it. |
| 645 |
) |
| 646 |
|
| 647 |
(defun mgp-entex-handle-bar (arg) |
| 648 |
(mgp-with-output-buffer |
| 649 |
(cond ((eq mgp-entex-linehead 'empty) |
| 650 |
(insert "\\nl\n") |
| 651 |
(setq mgp-entex-linehead t)) |
| 652 |
((not mgp-entex-linehead) |
| 653 |
(insert "\\nl}\n") |
| 654 |
(setq mgp-entex-linehead t))) |
| 655 |
(insert (format "\\mgpb{%d}{%d}{%d}\n" |
| 656 |
(floor (nth 1 arg)) |
| 657 |
(floor (nth 2 arg)) |
| 658 |
(floor (nth 3 arg)))))) |
| 659 |
|
| 660 |
(defun mgp-entex-handle-image (arg) |
| 661 |
(let ((file (car arg)) |
| 662 |
;;(numcolor (nth 1 arg)) ; not supported |
| 663 |
(xzoom (nth 2 arg)) |
| 664 |
(yzoom (nth 3 arg)) |
| 665 |
(zoomflag (nth 4 arg)) |
| 666 |
(screensize)) |
| 667 |
(setq file (concat (file-name-sans-extension file) ".ps")) |
| 668 |
(or (file-readable-p file) |
| 669 |
(setq file (concat (file-name-sans-extension file) ".eps"))) |
| 670 |
(if (file-readable-p file) |
| 671 |
(mgp-with-output-buffer |
| 672 |
(if zoomflag |
| 673 |
(if (and (= yzoom 0) (= xzoom 0)) |
| 674 |
(insert (format "\\includegraphics{%s}" file)) |
| 675 |
(insert |
| 676 |
(format "\\scalebox{%f}[%f]{\\includegraphics{%s}}" |
| 677 |
(* (/ (float xzoom) 100) mgp-image-scale-factor) |
| 678 |
(* (/ (float yzoom) 100) mgp-image-scale-factor) |
| 679 |
file))) |
| 680 |
(insert (format "\\resizebox{\\hsize}{!}{\\includegraphics{%s}}" |
| 681 |
file))))))) |
| 682 |
|
| 683 |
|
| 684 |
(provide 'mgp) |
| 685 |
|
| 686 |
;; mgp.el ends here |