Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / progmodes / ps-mode.el
blobcd2d0fb74dd80f793e16469880147ae9d43f4715
1 ;;; ps-mode.el --- PostScript mode for GNU Emacs
3 ;; Copyright (C) 1999, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Peter Kleiweg <p.c.j.kleiweg@rug.nl>
6 ;; Maintainer: Peter Kleiweg <p.c.j.kleiweg@rug.nl>
7 ;; Created: 20 Aug 1997
8 ;; Version: 1.1i
9 ;; Keywords: PostScript, languages
11 ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
12 ;; file on 18/3/2008, and the maintainer agreed that when a bug is
13 ;; filed in the Emacs bug reporting system against this file, a copy
14 ;; of the bug report be sent to the maintainer's email address, but
15 ;; only if: "... those e-mails have a link to the bug report system,
16 ;; where I can cancel these e-mails if I want to.".
18 ;; This file is part of GNU Emacs.
20 ;; GNU Emacs is free software: you can redistribute it and/or modify
21 ;; it under the terms of the GNU General Public License as published by
22 ;; the Free Software Foundation, either version 3 of the License, or
23 ;; (at your option) any later version.
25 ;; GNU Emacs is distributed in the hope that it will be useful,
26 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;; GNU General Public License for more details.
30 ;; You should have received a copy of the GNU General Public License
31 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
33 ;;; Commentary:
36 ;;; Code:
38 (defconst ps-mode-version "1.1i, 17 May 2008")
39 (defconst ps-mode-maintainer-address
40 "Peter Kleiweg <p.c.j.kleiweg@rug.nl>, bug-gnu-emacs@gnu.org")
42 (require 'comint)
43 (require 'easymenu)
45 ;; Define core `PostScript' group.
46 (defgroup PostScript nil
47 "PostScript mode for Emacs."
48 :group 'languages)
50 (defgroup PostScript-edit nil
51 "PostScript editing."
52 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
53 :prefix "ps-mode-"
54 :group 'PostScript)
56 (defgroup PostScript-interaction nil
57 "PostScript interaction."
58 :prefix "ps-run-"
59 :group 'PostScript)
61 ;; User variables.
63 (defcustom ps-mode-auto-indent t
64 "Should we use autoindent?"
65 :group 'PostScript-edit
66 :type 'boolean)
68 (defcustom ps-mode-tab 4
69 "Number of spaces to use when indenting."
70 :group 'PostScript-edit
71 :type 'integer)
73 (defcustom ps-mode-paper-size '(595 842)
74 "Default paper size.
76 When inserting an EPSF template these values are used
77 to set the boundingbox to include the whole page.
78 When the figure is finished these values should be replaced."
79 :group 'PostScript-edit
80 :type '(choice
81 (const :tag "letter" (612 792))
82 (const :tag "legal" (612 1008))
83 (const :tag "a0" (2380 3368))
84 (const :tag "a1" (1684 2380))
85 (const :tag "a2" (1190 1684))
86 (const :tag "a3" (842 1190))
87 (const :tag "a4" (595 842))
88 (const :tag "a5" (421 595))
89 (const :tag "a6" (297 421))
90 (const :tag "a7" (210 297))
91 (const :tag "a8" (148 210))
92 (const :tag "a9" (105 148))
93 (const :tag "a10" (74 105))
94 (const :tag "b0" (2836 4008))
95 (const :tag "b1" (2004 2836))
96 (const :tag "b2" (1418 2004))
97 (const :tag "b3" (1002 1418))
98 (const :tag "b4" (709 1002))
99 (const :tag "b5" (501 709))
100 (const :tag "archE" (2592 3456))
101 (const :tag "archD" (1728 2592))
102 (const :tag "archC" (1296 1728))
103 (const :tag "archB" (864 1296))
104 (const :tag "archA" (648 864))
105 (const :tag "flsa" (612 936))
106 (const :tag "flse" (612 936))
107 (const :tag "halfletter" (396 612))
108 (const :tag "11x17" (792 1224))
109 (const :tag "tabloid" (792 1224))
110 (const :tag "ledger" (1224 792))
111 (const :tag "csheet" (1224 1584))
112 (const :tag "dsheet" (1584 2448))
113 (const :tag "esheet" (2448 3168))))
115 (defcustom ps-mode-print-function
116 (lambda ()
117 (let ((lpr-switches nil)
118 (lpr-command (if (memq system-type '(usg-unix-v hpux irix))
119 "lp" "lpr")))
120 (lpr-buffer)))
121 "Lisp function to print current buffer as PostScript."
122 :group 'PostScript-edit
123 :type 'function)
125 (defcustom ps-run-prompt "\\(GS\\(<[0-9]+\\)?>\\)+"
126 "Regexp to match prompt in interactive PostScript."
127 :group 'PostScript-interaction
128 :type 'regexp)
130 (defcustom ps-run-font-lock-keywords-2
131 (append (unless (string= ps-run-prompt "")
132 (list (list (if (= ?^ (string-to-char ps-run-prompt))
133 ps-run-prompt
134 (concat "^" ps-run-prompt))
135 '(0 font-lock-function-name-face nil nil))))
136 '((">>showpage, press <return> to continue<<"
137 (0 font-lock-keyword-face nil nil))
138 ("^\\(Error\\|Can't\\).*"
139 (0 font-lock-warning-face nil nil))
140 ("^\\(Current file position is\\) \\([0-9]+\\)"
141 (1 font-lock-comment-face nil nil)
142 (2 font-lock-warning-face nil nil))))
143 "Medium level highlighting of messages from the PostScript interpreter.
145 See documentation on font-lock for details."
146 :group 'PostScript-interaction
147 :type '(repeat (list :tag "Expression with one or more highlighters"
148 :value ("" (0 default nil t))
149 (regexp :tag "Expression")
150 (repeat :tag "Highlighters"
151 :inline regexp
152 (list :tag "Highlighter"
153 (integer :tag "Subexp")
154 face
155 (boolean :tag "Override")
156 (boolean :tag "Laxmatch" :value t))))))
158 (defcustom ps-run-x '("gs" "-r72" "-sPAPERSIZE=a4")
159 "Command as list to run PostScript with graphic display."
160 :group 'PostScript-interaction
161 :type '(repeat string))
163 (defcustom ps-run-dumb '("gs" "-dNODISPLAY")
164 "Command as list to run PostScript without graphic display."
165 :group 'PostScript-interaction
166 :type '(repeat string))
168 (defcustom ps-run-init nil
169 "String of commands to send to PostScript to start interactive.
171 Example: \"executive\"
173 You won't need to set this option for Ghostscript."
174 :group 'PostScript-interaction
175 :type '(choice (const nil) string))
177 (defcustom ps-run-error-line-numbers nil
178 "What values are used by the PostScript interpreter in error messages?"
179 :group 'PostScript-interaction
180 :type '(choice (const :tag "line numbers" t)
181 (const :tag "byte counts" nil)))
183 (defcustom ps-run-tmp-dir nil
184 "Name of directory to place temporary file.
185 If nil, use `temporary-file-directory'."
186 :group 'PostScript-interaction
187 :type '(choice (const nil) directory))
190 ;; Constants used for font-lock.
192 ;; Only a small set of the PostScript operators is selected for fontification.
193 ;; Fontification is meant to clarify the document structure and process flow,
194 ;; fontifying all known PostScript operators would hinder that objective.
195 (defconst ps-mode-operators
196 (let ((ops '("clear" "mark" "cleartomark" "counttomark"
197 "forall"
198 "dict" "begin" "end" "def"
199 "true" "false"
200 "exec" "if" "ifelse" "for" "repeat" "loop" "exit"
201 "stop" "stopped" "countexecstack" "execstack"
202 "quit" "start"
203 "save" "restore"
204 "bind" "null"
205 "gsave" "grestore" "grestoreall"
206 "showpage")))
207 (concat "\\<" (regexp-opt ops t) "\\>"))
208 "Regexp of PostScript operators that will be fontified.")
210 ;; Level 1 font-lock:
211 ;; - Special comments (reference face)
212 ;; - Strings and other comments
213 ;; - Partial strings (warning face)
214 ;; - 8bit characters (warning face)
215 ;; Multiline strings are not supported. Strings with nested brackets are.
216 (defconst ps-mode-font-lock-keywords-1
217 '(("\\`%!PS.*" . font-lock-constant-face)
218 ("^%%BoundingBox:[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]*$"
219 . font-lock-constant-face)
220 (ps-mode-match-string-or-comment
221 (1 font-lock-comment-face nil t)
222 (2 font-lock-string-face nil t))
223 ("([^()\n%]*\\|[^()\n]*)" . font-lock-warning-face)
224 ("[\200-\377]+" (0 font-lock-warning-face prepend nil)))
225 "Subdued level highlighting for PostScript mode.")
227 ;; Level 2 font-lock:
228 ;; - All from level 1
229 ;; - PostScript operators (keyword face)
230 (defconst ps-mode-font-lock-keywords-2
231 (append
232 ps-mode-font-lock-keywords-1
233 (list
234 (cons
235 ;; exclude names prepended by `/'
236 (concat "\\(^\\|[^/\n]\\)" ps-mode-operators)
237 '(2 font-lock-keyword-face))))
238 "Medium level highlighting for PostScript mode.")
240 ;; Level 3 font-lock:
241 ;; - All from level 2
242 ;; - Immediately evaluated names: those starting with `//' (type face)
243 ;; - Names that look like they are used for the definition of:
244 ;; * a function
245 ;; * an array
246 ;; * a dictionary
247 ;; * a "global" variable
248 ;; (function name face)
249 ;; - Other names (variable name face)
250 ;; The rules used to determine what names fit in the first category are:
251 ;; - Only names that are at the left margin, and one of these on the same line:
252 ;; * Nothing after the name except possibly one or more `[' or a comment
253 ;; * A `{' or `<<' or `[0-9]+ dict' following the name
254 ;; * A `def' somewhere in the same line
255 ;; Names are fontified before PostScript operators, allowing the use of
256 ;; a more simple (efficient) regexp than the one used in level 2.
257 (defconst ps-mode-font-lock-keywords-3
258 (append
259 ps-mode-font-lock-keywords-1
260 (list
261 '("//\\w+" . font-lock-type-face)
262 `(,(concat
263 "^\\(/\\w+\\)\\>"
264 "\\([[ \t]*\\(%.*\\)?\r?$" ; Nothing but `[' or comment after the name.
265 "\\|[ \t]*\\({\\|<<\\)" ; `{' or `<<' following the name.
266 "\\|[ \t]+[0-9]+[ \t]+dict\\>" ; `[0-9]+ dict' following the name.
267 "\\|.*\\<def\\>\\)") ; `def' somewhere on the same line.
268 . (1 font-lock-function-name-face))
269 '("/\\w+" . font-lock-variable-name-face)
270 (cons ps-mode-operators 'font-lock-keyword-face)))
271 "High level highlighting for PostScript mode.")
273 (defconst ps-mode-font-lock-keywords ps-mode-font-lock-keywords-1
274 "Default expressions to highlight in PostScript mode.")
276 ;; Level 1 font-lock for ps-run-mode
277 ;; - prompt (function name face)
278 (defconst ps-run-font-lock-keywords-1
279 (unless (string= "" ps-run-prompt)
280 (list (cons (if (= ?^ (string-to-char ps-run-prompt))
281 ps-run-prompt
282 (concat "^" ps-run-prompt))
283 'font-lock-function-name-face)))
284 "Subdued level highlighting for PostScript run mode.")
286 (defconst ps-run-font-lock-keywords ps-run-font-lock-keywords-1
287 "Default expressions to highlight in PostScript run mode.")
290 ;; Variables.
292 (defvar ps-mode-map nil
293 "Local keymap to use in PostScript mode.")
295 (defvar ps-mode-syntax-table nil
296 "Syntax table used while in PostScript mode.")
298 (defvar ps-run-mode-map nil
299 "Local keymap to use in PostScript run mode.")
301 (defvar ps-mode-tmp-file nil
302 "Name of temporary file, set by `ps-run'.")
304 (defvar ps-run-mark nil
305 "Mark to start of region that was sent to PostScript interpreter.")
307 (defvar ps-run-parent nil
308 "Parent window of interactive PostScript.")
311 ;; Menu
313 (defconst ps-mode-menu-main
314 '("PostScript"
315 ["EPSF Template, Sparse" ps-mode-epsf-sparse t]
316 ["EPSF Template, Rich" ps-mode-epsf-rich t]
317 "---"
318 ("Cookbook"
319 ["RE" ps-mode-RE t]
320 ["ISOLatin1Extended" ps-mode-latin-extended t]
321 ["center" ps-mode-center t]
322 ["right" ps-mode-right t]
323 ["Heapsort" ps-mode-heapsort t])
324 ("Fonts (1)"
325 ["Times-Roman" (insert "/Times-Roman ") t]
326 ["Times-Bold" (insert "/Times-Bold ") t]
327 ["Times-Italic" (insert "/Times-Italic ") t]
328 ["Times-BoldItalic" (insert "/Times-BoldItalic ") t]
329 ["Helvetica" (insert "/Helvetica ") t]
330 ["Helvetica-Bold" (insert "/Helvetica-Bold ") t]
331 ["Helvetica-Oblique" (insert "/Helvetica-Oblique ") t]
332 ["Helvetica-BoldOblique" (insert "/Helvetica-BoldOblique ") t]
333 ["Courier" (insert "/Courier ") t]
334 ["Courier-Bold" (insert "/Courier-Bold ") t]
335 ["Courier-Oblique" (insert "/Courier-Oblique ") t]
336 ["Courier-BoldOblique" (insert "/Courier-BoldOblique ") t]
337 ["Symbol" (insert "/Symbol") t ])
338 ("Fonts (2)"
339 ["AvantGarde-Book" (insert "/AvantGarde-Book ") t]
340 ["AvantGarde-Demi" (insert "/AvantGarde-Demi ") t]
341 ["AvantGarde-BookOblique" (insert "/AvantGarde-BookOblique ") t]
342 ["AvantGarde-DemiOblique" (insert "/AvantGarde-DemiOblique ") t]
343 ["Bookman-Light" (insert "/Bookman-Light ") t]
344 ["Bookman-Demi" (insert "/Bookman-Demi ") t]
345 ["Bookman-LightItalic" (insert "/Bookman-LightItalic ") t]
346 ["Bookman-DemiItalic" (insert "/Bookman-DemiItalic ") t]
347 ["Helvetica-Narrow" (insert "/Helvetica-Narrow ") t]
348 ["Helvetica-Narrow-Bold" (insert "/Helvetica-Narrow-Bold ") t]
349 ["Helvetica-Narrow-Oblique" (insert "/Helvetica-Narrow-Oblique ") t]
350 ["Helvetica-Narrow-BoldOblique" (insert "/Helvetica-Narrow-BoldOblique ") t]
351 ["NewCenturySchlbk-Roman" (insert "/NewCenturySchlbk-Roman ") t]
352 ["NewCenturySchlbk-Bold" (insert "/NewCenturySchlbk-Bold ") t]
353 ["NewCenturySchlbk-Italic" (insert "/NewCenturySchlbk-Italic ") t]
354 ["NewCenturySchlbk-BoldItalic" (insert "/NewCenturySchlbk-BoldItalic ") t]
355 ["Palatino-Roman" (insert "/Palatino-Roman ") t]
356 ["Palatino-Bold" (insert "/Palatino-Bold ") t]
357 ["Palatino-Italic" (insert "/Palatino-Italic ") t]
358 ["Palatino-BoldItalic" (insert "/Palatino-BoldItalic ") t]
359 ["ZapfChancery-MediumItalic" (insert "/ZapfChancery-MediumItalic ") t]
360 ["ZapfDingbats" (insert "/ZapfDingbats ") t])
361 "---"
362 ["Comment Out Region" ps-mode-comment-out-region (mark t)]
363 ["Uncomment Region" ps-mode-uncomment-region (mark t)]
364 "---"
365 ["8-bit to Octal Buffer" ps-mode-octal-buffer t]
366 ["8-bit to Octal Region" ps-mode-octal-region (mark t)]
367 "---"
368 ["Auto Indent" (setq ps-mode-auto-indent (not ps-mode-auto-indent))
369 :style toggle :selected ps-mode-auto-indent]
370 "---"
371 ["Start PostScript"
372 ps-run-start
374 ["Quit PostScript" ps-run-quit (process-status "ps-run")]
375 ["Kill PostScript" ps-run-kill (process-status "ps-run")]
376 ["Send Buffer to Interpreter"
377 ps-run-buffer
378 (process-status "ps-run")]
379 ["Send Region to Interpreter"
380 ps-run-region
381 (and (mark t) (process-status "ps-run"))]
382 ["Send Newline to Interpreter"
383 ps-mode-other-newline
384 (process-status "ps-run")]
385 ["View BoundingBox"
386 ps-run-boundingbox
387 (process-status "ps-run")]
388 ["Clear/Reset PostScript Graphics"
389 ps-run-clear
390 (process-status "ps-run")]
391 "---"
392 ["Print Buffer as PostScript"
393 ps-mode-print-buffer
395 ["Print Region as PostScript"
396 ps-mode-print-region
397 (mark t)]
398 "---"
399 ["Customize for PostScript"
400 (customize-group "PostScript")
402 "---"
403 ["Submit Bug Report"
404 ps-mode-submit-bug-report
405 t]))
408 ;; Mode maps for PostScript edit mode and PostScript interaction mode.
410 (unless ps-mode-map
411 (setq ps-mode-map (make-sparse-keymap))
412 (define-key ps-mode-map "\C-c\C-v" 'ps-run-boundingbox)
413 (define-key ps-mode-map "\C-c\C-u" 'ps-mode-uncomment-region)
414 (define-key ps-mode-map "\C-c\C-t" 'ps-mode-epsf-rich)
415 (define-key ps-mode-map "\C-c\C-s" 'ps-run-start)
416 (define-key ps-mode-map "\C-c\C-r" 'ps-run-region)
417 (define-key ps-mode-map "\C-c\C-q" 'ps-run-quit)
418 (define-key ps-mode-map "\C-c\C-p" 'ps-mode-print-buffer)
419 (define-key ps-mode-map "\C-c\C-o" 'ps-mode-comment-out-region)
420 (define-key ps-mode-map "\C-c\C-k" 'ps-run-kill)
421 (define-key ps-mode-map "\C-c\C-j" 'ps-mode-other-newline)
422 (define-key ps-mode-map "\C-c\C-l" 'ps-run-clear)
423 (define-key ps-mode-map "\C-c\C-b" 'ps-run-buffer)
424 (define-key ps-mode-map ">" 'ps-mode-r-gt)
425 (define-key ps-mode-map "]" 'ps-mode-r-angle)
426 (define-key ps-mode-map "}" 'ps-mode-r-brace)
427 (define-key ps-mode-map "\177" 'ps-mode-backward-delete-char)
428 (define-key ps-mode-map "\t" 'ps-mode-tabkey)
429 (define-key ps-mode-map "\r" 'ps-mode-newline)
430 (define-key ps-mode-map [return] 'ps-mode-newline)
431 (easy-menu-define ps-mode-main ps-mode-map "PostScript" ps-mode-menu-main))
433 (unless ps-run-mode-map
434 (setq ps-run-mode-map (make-sparse-keymap))
435 (set-keymap-parent ps-run-mode-map comint-mode-map)
436 (define-key ps-run-mode-map "\C-c\C-q" 'ps-run-quit)
437 (define-key ps-run-mode-map "\C-c\C-k" 'ps-run-kill)
438 (define-key ps-run-mode-map "\C-c\C-e" 'ps-run-goto-error)
439 (define-key ps-run-mode-map [mouse-2] 'ps-run-mouse-goto-error))
442 ;; Syntax table.
444 (unless ps-mode-syntax-table
445 (setq ps-mode-syntax-table (make-syntax-table))
447 (modify-syntax-entry ?\% "< " ps-mode-syntax-table)
448 (modify-syntax-entry ?\n "> " ps-mode-syntax-table)
449 (modify-syntax-entry ?\r "> " ps-mode-syntax-table)
450 (modify-syntax-entry ?\f "> " ps-mode-syntax-table)
451 (modify-syntax-entry ?\< "(>" ps-mode-syntax-table)
452 (modify-syntax-entry ?\> ")<" ps-mode-syntax-table)
454 (modify-syntax-entry ?\! "w " ps-mode-syntax-table)
455 (modify-syntax-entry ?\" "w " ps-mode-syntax-table)
456 (modify-syntax-entry ?\# "w " ps-mode-syntax-table)
457 (modify-syntax-entry ?\$ "w " ps-mode-syntax-table)
458 (modify-syntax-entry ?\& "w " ps-mode-syntax-table)
459 (modify-syntax-entry ?\' "w " ps-mode-syntax-table)
460 (modify-syntax-entry ?\* "w " ps-mode-syntax-table)
461 (modify-syntax-entry ?\+ "w " ps-mode-syntax-table)
462 (modify-syntax-entry ?\, "w " ps-mode-syntax-table)
463 (modify-syntax-entry ?\- "w " ps-mode-syntax-table)
464 (modify-syntax-entry ?\. "w " ps-mode-syntax-table)
465 (modify-syntax-entry ?\: "w " ps-mode-syntax-table)
466 (modify-syntax-entry ?\; "w " ps-mode-syntax-table)
467 (modify-syntax-entry ?\= "w " ps-mode-syntax-table)
468 (modify-syntax-entry ?\? "w " ps-mode-syntax-table)
469 (modify-syntax-entry ?\@ "w " ps-mode-syntax-table)
470 (modify-syntax-entry ?\\ "w " ps-mode-syntax-table)
471 (modify-syntax-entry ?^ "w " ps-mode-syntax-table) ; NOT: ?\^
472 (modify-syntax-entry ?\_ "w " ps-mode-syntax-table)
473 (modify-syntax-entry ?\` "w " ps-mode-syntax-table)
474 (modify-syntax-entry ?\| "w " ps-mode-syntax-table)
475 (modify-syntax-entry ?\~ "w " ps-mode-syntax-table)
477 (let ((i 128))
478 (while (< i 256)
479 (modify-syntax-entry i "w " ps-mode-syntax-table)
480 (setq i (1+ i)))))
484 (declare-function doc-view-minor-mode "doc-view")
486 ;; PostScript mode.
488 ;;;###autoload
489 (define-derived-mode ps-mode prog-mode "PostScript"
490 "Major mode for editing PostScript with GNU Emacs.
492 Entry to this mode calls `ps-mode-hook'.
494 The following variables hold user options, and can
495 be set through the `customize' command:
497 `ps-mode-auto-indent'
498 `ps-mode-tab'
499 `ps-mode-paper-size'
500 `ps-mode-print-function'
501 `ps-run-prompt'
502 `ps-run-font-lock-keywords-2'
503 `ps-run-x'
504 `ps-run-dumb'
505 `ps-run-init'
506 `ps-run-error-line-numbers'
507 `ps-run-tmp-dir'
509 Type \\[describe-variable] for documentation on these options.
512 \\{ps-mode-map}
515 When starting an interactive PostScript process with \\[ps-run-start],
516 a second window will be displayed, and `ps-run-mode-hook' will be called.
517 The keymap for this second window is:
519 \\{ps-run-mode-map}
522 When Ghostscript encounters an error it displays an error message
523 with a file position. Clicking mouse-2 on this number will bring
524 point to the corresponding spot in the PostScript window, if input
525 to the interpreter was sent from that window.
526 Typing \\<ps-run-mode-map>\\[ps-run-goto-error] when the cursor is at the number has the same effect."
527 (set (make-local-variable 'font-lock-defaults)
528 '((ps-mode-font-lock-keywords
529 ps-mode-font-lock-keywords-1
530 ps-mode-font-lock-keywords-2
531 ps-mode-font-lock-keywords-3)
533 (set (make-local-variable 'comment-start) "%")
534 ;; NOTE: `\' has a special meaning in strings only
535 (set (make-local-variable 'comment-start-skip) "%+[ \t]*")
536 ;; enable doc-view-minor-mode => C-c C-c starts viewing the current ps file
537 ;; with doc-view-mode.
538 (doc-view-minor-mode 1))
540 (defun ps-mode-show-version ()
541 "Show current version of PostScript mode."
542 (interactive)
543 (message " *** PostScript Mode (ps-mode) Version %s *** " ps-mode-version))
545 ;; From reporter.el
546 (defvar reporter-prompt-for-summary-p)
547 (defvar reporter-dont-compact-list)
549 (defun ps-mode-submit-bug-report ()
550 "Submit via mail a bug report on PostScript mode."
551 (interactive)
552 (when (y-or-n-p "Submit bug report on PostScript mode? ")
553 (let ((reporter-prompt-for-summary-p nil)
554 (reporter-dont-compact-list '(ps-mode-print-function
555 ps-run-font-lock-keywords-2)))
556 (reporter-submit-bug-report
557 ps-mode-maintainer-address
558 (format "ps-mode.el %s [%s]" ps-mode-version system-type)
559 '(ps-mode-auto-indent
560 ps-mode-tab
561 ps-mode-paper-size
562 ps-mode-print-function
563 ps-run-prompt
564 ps-run-font-lock-keywords-2
565 ps-run-x
566 ps-run-dumb
567 ps-run-init
568 ps-run-error-line-numbers
569 ps-run-tmp-dir)))))
572 ;; Helper functions for font-lock.
574 ;; When this function is called, point is at an opening bracket.
575 ;; This function should test if point is at the start of a string
576 ;; with nested brackets.
577 ;; If true: move point to end of string
578 ;; set string to match data nr 2
579 ;; return new point
580 ;; If false: return nil
581 (defun ps-mode-looking-at-nested (limit)
582 (let ((first (point))
583 (level 1)
584 pos)
585 ;; Move past opening bracket.
586 (forward-char 1)
587 (setq pos (point))
588 (while (and (> level 0) (< pos limit))
589 ;; Search next bracket, stepping over escaped brackets.
590 (if (not (looking-at "\\([^()\\\n]\\|\\\\.\\)*\\([()]\\)"))
591 (setq level -1)
592 (setq level (+ level (if (string= "(" (match-string 2)) 1 -1)))
593 (goto-char (setq pos (match-end 0)))))
594 (if (not (= level 0))
596 ;; Found string with nested brackets, now set match data nr 2.
597 (set-match-data (list first pos nil nil first pos))
598 pos)))
600 ;; This function should search for a string or comment
601 ;; If comment, return as match data nr 1
602 ;; If string, return as match data nr 2
603 (defun ps-mode-match-string-or-comment (limit)
604 ;; Find the first potential match.
605 (if (not (re-search-forward "[%(]" limit t))
606 ;; Nothing found: return failure.
608 (let ((end (match-end 0)))
609 (goto-char (match-beginning 0))
610 (cond ((looking-at "\\(%.*\\)\\|\\((\\([^()\\\n]\\|\\\\.\\)*)\\)")
611 ;; It's a comment or string without nested, unescaped brackets.
612 (goto-char (match-end 0))
613 (point))
614 ((ps-mode-looking-at-nested limit)
615 ;; It's a string with nested brackets.
616 (point))
618 ;; Try next match.
619 (goto-char end)
620 (ps-mode-match-string-or-comment limit))))))
623 ;; Key-handlers.
625 (defun ps-mode-target-column ()
626 "To what column should text on current line be indented?
628 Indentation is increased if the last token on the current line
629 defines the beginning of a group. These tokens are: { [ <<"
630 (save-excursion
631 (beginning-of-line)
632 (if (looking-at "[ \t]*\\(}\\|\\]\\|>>\\)")
633 (condition-case err
634 (progn
635 (goto-char (match-end 0))
636 (backward-sexp 1)
637 (beginning-of-line)
638 (if (looking-at "[ \t]+")
639 (goto-char (match-end 0)))
640 (current-column))
641 (error
642 (ding)
643 (message "%s" (error-message-string err))
645 (let (target)
646 (if (not (re-search-backward "[^ \t\n\r\f][ \t\n\r\f]*\\=" nil t))
648 (goto-char (match-beginning 0))
649 (beginning-of-line)
650 (if (looking-at "[ \t]+")
651 (goto-char (match-end 0)))
652 (setq target (current-column))
653 (end-of-line)
654 (if (re-search-backward "\\({\\|\\[\\|<<\\)[ \t]*\\(%[^\n]*\\)?\\=" nil t)
655 (setq target (+ target ps-mode-tab)))
656 target)))))
658 (defun ps-mode-newline ()
659 "Insert newline with proper indentation."
660 (interactive)
661 (delete-horizontal-space)
662 (insert "\n")
663 (if ps-mode-auto-indent
664 (indent-to (ps-mode-target-column))))
666 (defun ps-mode-tabkey ()
667 "Indent/reindent current line, or insert tab."
668 (interactive)
669 (let ((column (current-column))
670 target)
671 (if (or (not ps-mode-auto-indent)
672 (< ps-mode-tab 1)
673 (not (re-search-backward "^[ \t]*\\=" nil t)))
674 (insert "\t")
675 (setq target (ps-mode-target-column))
676 (while (<= target column)
677 (setq target (+ target ps-mode-tab)))
678 (indent-line-to target))))
680 (defun ps-mode-backward-delete-char ()
681 "Delete backward indentation, or delete backward character."
682 (interactive)
683 (let ((column (current-column))
684 target)
685 (if (or (not ps-mode-auto-indent)
686 (< ps-mode-tab 1)
687 (not (re-search-backward "^[ \t]+\\=" nil t)))
688 (call-interactively 'delete-backward-char)
689 (setq target (ps-mode-target-column))
690 (while (> column target)
691 (setq target (+ target ps-mode-tab)))
692 (while (>= target column)
693 (setq target (- target ps-mode-tab)))
694 (if (< target 0)
695 (setq target 0))
696 (indent-line-to target))))
698 (defun ps-mode-r-brace ()
699 "Insert `}' and perform balance."
700 (interactive)
701 (insert "}")
702 (ps-mode-r-balance "}"))
704 (defun ps-mode-r-angle ()
705 "Insert `]' and perform balance."
706 (interactive)
707 (insert "]")
708 (ps-mode-r-balance "]"))
710 (defun ps-mode-r-gt ()
711 "Insert `>' and perform balance."
712 (interactive)
713 (insert ">")
714 (ps-mode-r-balance ">>"))
716 (defun ps-mode-r-balance (right)
717 "Adjust indenting if point after RIGHT."
718 (if ps-mode-auto-indent
719 (save-excursion
720 (when (re-search-backward (concat "^[ \t]*" (regexp-quote right) "\\=") nil t)
721 (indent-line-to (ps-mode-target-column)))))
722 (blink-matching-open))
724 (defun ps-mode-other-newline ()
725 "Perform newline in `*ps-run*' buffer."
726 (interactive)
727 (ps-run-send-string ""))
730 ;; Print PostScript.
732 (defun ps-mode-print-buffer ()
733 "Print buffer as PostScript."
734 (interactive)
735 (funcall ps-mode-print-function))
737 (defun ps-mode-print-region (begin end)
738 "Print region as PostScript, adding minimal header and footer lines:
740 %!PS
741 <region>
742 showpage"
743 (interactive "r")
744 (let ((buf (current-buffer)))
745 (with-temp-buffer
746 (insert "%!PS\n")
747 (insert-buffer-substring buf begin end)
748 (insert "\nshowpage\n")
749 (funcall ps-mode-print-function))))
752 ;; Comment Out / Uncomment.
754 (defun ps-mode-comment-out-region (begin end)
755 "Comment out region."
756 (interactive "r")
757 (let ((endm (make-marker)))
758 (set-marker endm end)
759 (save-excursion
760 (goto-char begin)
761 (if (= (current-column) 0)
762 (insert "%"))
763 (while (and (= (forward-line) 0)
764 (< (point) (marker-position endm)))
765 (insert "%")))
766 (set-marker endm nil)))
768 (defun ps-mode-uncomment-region (begin end)
769 "Uncomment region.
771 Only one `%' is removed, and it has to be in the first column."
772 (interactive "r")
773 (let ((endm (make-marker)))
774 (set-marker endm end)
775 (save-excursion
776 (goto-char begin)
777 (if (looking-at "^%")
778 (delete-char 1))
779 (while (and (= (forward-line) 0)
780 (< (point) (marker-position endm)))
781 (if (looking-at "%")
782 (delete-char 1))))
783 (set-marker endm nil)))
786 ;; Convert 8-bit to octal codes.
788 (defun ps-mode-octal-buffer ()
789 "Change 8-bit characters to octal codes in buffer."
790 (interactive)
791 (ps-mode-octal-region (point-min) (point-max)))
793 (defun ps-mode-octal-region (begin end)
794 "Change 8-bit characters to octal codes in region."
795 (interactive "r")
796 (if buffer-read-only
797 (progn
798 (ding)
799 (message "Buffer is read only"))
800 (save-excursion
801 (let (endm i)
802 (setq endm (make-marker))
803 (set-marker endm end)
804 (goto-char begin)
805 (setq i 0)
806 (while (re-search-forward "[\200-\377]" (marker-position endm) t)
807 (setq i (1+ i))
808 (backward-char)
809 (insert (format "\\%03o" (string-to-char (string-make-unibyte (buffer-substring (point) (1+ (point)))))))
810 (delete-char 1))
811 (message "%d change%s made" i (if (= i 1) "" "s"))
812 (set-marker endm nil)))))
815 ;; Cookbook.
817 (defun ps-mode-center ()
818 "Insert function /center."
819 (interactive)
820 (insert "
821 /center {
822 dup stringwidth
823 exch 2 div neg
824 exch 2 div neg
825 rmoveto
826 } bind def
829 (defun ps-mode-right ()
830 "Insert function /right."
831 (interactive)
832 (insert "
833 /right {
834 dup stringwidth
835 exch neg
836 exch neg
837 rmoveto
838 } bind def
841 (defun ps-mode-RE ()
842 "Insert function /RE."
843 (interactive)
844 (insert "
845 % `new-font-name' `encoding-vector' `old-font-name' RE -
846 /RE {
847 findfont
848 dup maxlength dict begin {
849 1 index /FID ne { def } { pop pop } ifelse
850 } forall
851 /Encoding exch def
852 dup /FontName exch def
853 currentdict end definefont pop
854 } bind def
857 (defun ps-mode-latin-extended ()
858 "Insert array /ISOLatin1Extended.
860 This encoding vector contains all the entries from ISOLatin1Encoding
861 plus the usually uncoded characters inserted on positions 1 through 28."
862 (interactive)
863 (insert "
864 % ISOLatin1Encoding, extended with remaining uncoded glyphs
865 /ISOLatin1Extended [
866 /.notdef /Lslash /lslash /OE /oe /Scaron /scaron /Zcaron /zcaron
867 /Ydieresis /trademark /bullet /dagger /daggerdbl /ellipsis /emdash
868 /endash /fi /fl /florin /fraction /guilsinglleft /guilsinglright
869 /perthousand /quotedblbase /quotedblleft /quotedblright
870 /quotesinglbase /quotesingle /.notdef /.notdef /.notdef /space
871 /exclam /quotedbl /numbersign /dollar /percent /ampersand
872 /quoteright /parenleft /parenright /asterisk /plus /comma /minus
873 /period /slash /zero /one /two /three /four /five /six /seven /eight
874 /nine /colon /semicolon /less /equal /greater /question /at /A /B /C
875 /D /E /F /G /H /I /J /K /L /M /N /O /P /Q /R /S /T /U /V /W /X /Y /Z
876 /bracketleft /backslash /bracketright /asciicircum /underscore
877 /quoteleft /a /b /c /d /e /f /g /h /i /j /k /l /m /n /o /p /q /r /s
878 /t /u /v /w /x /y /z /braceleft /bar /braceright /asciitilde
879 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
880 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
881 /.notdef /.notdef /.notdef /dotlessi /grave /acute /circumflex
882 /tilde /macron /breve /dotaccent /dieresis /.notdef /ring /cedilla
883 /.notdef /hungarumlaut /ogonek /caron /space /exclamdown /cent
884 /sterling /currency /yen /brokenbar /section /dieresis /copyright
885 /ordfeminine /guillemotleft /logicalnot /hyphen /registered /macron
886 /degree /plusminus /twosuperior /threesuperior /acute /mu /paragraph
887 /periodcentered /cedilla /onesuperior /ordmasculine /guillemotright
888 /onequarter /onehalf /threequarters /questiondown /Agrave /Aacute
889 /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla /Egrave /Eacute
890 /Ecircumflex /Edieresis /Igrave /Iacute /Icircumflex /Idieresis /Eth
891 /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply
892 /Oslash /Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn
893 /germandbls /agrave /aacute /acircumflex /atilde /adieresis /aring
894 /ae /ccedilla /egrave /eacute /ecircumflex /edieresis /igrave
895 /iacute /icircumflex /idieresis /eth /ntilde /ograve /oacute
896 /ocircumflex /otilde /odieresis /divide /oslash /ugrave /uacute
897 /ucircumflex /udieresis /yacute /thorn /ydieresis
898 ] def
901 (defun ps-mode-heapsort ()
902 "Insert function /Heapsort."
903 (interactive)
904 (insert "
905 % `array-element' Heapsort-cvi-or-cvr-or-cvs `number-or-string'
906 /Heapsort-cvi-or-cvr-or-cvs {
907 % 0 get
908 } bind def
909 % `array' Heapsort `sorted-array'
910 /Heapsort {
911 dup length /hsR exch def
912 /hsL hsR 2 idiv 1 add def
914 hsR 2 lt { exit } if
915 hsL 1 gt {
916 /hsL hsL 1 sub def
918 /hsR hsR 1 sub def
919 dup dup dup 0 get exch dup hsR get
920 0 exch put
921 hsR exch put
922 } ifelse
923 dup hsL 1 sub get /hsT exch def
924 /hsJ hsL def
926 /hsS hsJ def
927 /hsJ hsJ dup add def
928 hsJ hsR gt { exit } if
929 hsJ hsR lt {
930 dup dup hsJ 1 sub get Heapsort-cvi-or-cvr-or-cvs
931 exch hsJ get Heapsort-cvi-or-cvr-or-cvs
932 lt { /hsJ hsJ 1 add def } if
933 } if
934 dup hsJ 1 sub get Heapsort-cvi-or-cvr-or-cvs
935 hsT Heapsort-cvi-or-cvr-or-cvs
936 le { exit } if
937 dup dup hsS 1 sub exch hsJ 1 sub get put
938 } loop
939 dup hsS 1 sub hsT put
940 } loop
941 } bind def
945 ;; EPSF document lay-out.
947 (defun ps-mode-epsf-sparse ()
948 "Insert sparse EPSF template."
949 (interactive)
950 (goto-char (point-max))
951 (unless (re-search-backward "%%EOF[ \t\n]*\\'" nil t)
952 (goto-char (point-max))
953 (insert "\n%%EOF\n"))
954 (goto-char (point-max))
955 (unless (re-search-backward "\\bshowpage[ \t\n]+%%EOF[ \t\n]*\\'" nil t)
956 (re-search-backward "%%EOF")
957 (insert "showpage\n"))
958 (goto-char (point-max))
959 (unless (re-search-backward "\\bend[ \t\n]+\\bshowpage[ \t\n]+%%EOF[ \t\n]*\\'" nil t)
960 (re-search-backward "showpage")
961 (insert "\nend\n"))
962 (goto-char (point-min))
963 (insert "%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 ")
964 (insert (format "%d %d\n\n"
965 (car ps-mode-paper-size)
966 (car (cdr ps-mode-paper-size))))
967 (insert "64 dict begin\n\n"))
969 (defun ps-mode-epsf-rich ()
970 "Insert rich EPSF template."
971 (interactive)
972 (ps-mode-epsf-sparse)
973 (forward-line -3)
974 (when buffer-file-name
975 (insert "%%Title: " (file-name-nondirectory buffer-file-name) "\n"))
976 (insert "%%Creator: " (user-full-name) "\n")
977 (insert "%%CreationDate: " (current-time-string) "\n")
978 (insert "%%EndComments\n")
979 (forward-line 3))
982 ;; Interactive PostScript interpreter.
984 (define-derived-mode ps-run-mode comint-mode "Interactive PS"
985 "Major mode in interactive PostScript window.
986 This mode is invoked from `ps-mode' and should not be called directly."
987 (set (make-local-variable 'font-lock-defaults)
988 '((ps-run-font-lock-keywords
989 ps-run-font-lock-keywords-1
990 ps-run-font-lock-keywords-2)
992 (setq mode-line-process '(":%s")))
994 (defun ps-run-running ()
995 "Error if not in `ps-mode' or not running PostScript."
996 (unless (derived-mode-p 'ps-mode)
997 (error "This function can only be called from PostScript mode"))
998 (unless (equal (process-status "ps-run") 'run)
999 (error "No PostScript process running")))
1001 (defun ps-run-start ()
1002 "Start interactive PostScript."
1003 (interactive)
1004 (let ((command (or (and window-system ps-run-x) ps-run-dumb))
1005 (init-file nil)
1006 (process-connection-type nil)
1007 (oldwin (selected-window)))
1008 (unless command
1009 (error "No command specified to run interactive PostScript"))
1010 (unless (and ps-run-mark (markerp ps-run-mark))
1011 (setq ps-run-mark (make-marker)))
1012 (when ps-run-init
1013 (setq init-file (ps-run-make-tmp-filename))
1014 (write-region (concat ps-run-init "\n") 0 init-file)
1015 (setq init-file (list init-file)))
1016 (pop-to-buffer "*ps-run*")
1017 (ps-run-mode)
1018 (when (process-status "ps-run")
1019 (delete-process "ps-run"))
1020 (erase-buffer)
1021 (setq command (append command init-file))
1022 (insert (mapconcat 'identity command " ") "\n")
1023 (apply 'make-comint "ps-run" (car command) nil (cdr command))
1024 (with-current-buffer "*ps-run*"
1025 (use-local-map ps-run-mode-map)
1026 (setq comint-prompt-regexp ps-run-prompt))
1027 (select-window oldwin)))
1029 (defun ps-run-quit ()
1030 "Quit interactive PostScript."
1031 (interactive)
1032 (ps-run-send-string "quit")
1033 (ps-run-cleanup))
1035 (defun ps-run-kill ()
1036 "Kill interactive PostScript."
1037 (interactive)
1038 (delete-process "ps-run")
1039 (ps-run-cleanup))
1041 (defun ps-run-clear ()
1042 "Clear/reset PostScript graphics."
1043 (interactive)
1044 (ps-run-send-string "showpage")
1045 (sit-for 1)
1046 (ps-run-send-string ""))
1048 (defun ps-run-buffer ()
1049 "Send buffer to PostScript interpreter."
1050 (interactive)
1051 (ps-run-region (point-min) (point-max)))
1053 (defun ps-run-region (begin end)
1054 "Send region to PostScript interpreter."
1055 (interactive "r")
1056 (ps-run-running)
1057 (setq ps-run-parent (buffer-name))
1058 (let ((f (ps-run-make-tmp-filename)))
1059 (set-marker ps-run-mark begin)
1060 (write-region begin end f)
1061 (ps-run-send-string (format "(%s) run" f))))
1063 (defun ps-run-boundingbox ()
1064 "View BoundingBox."
1065 (interactive)
1066 (ps-run-running)
1067 (let (x1 y1 x2 y2 f
1068 (buf (current-buffer)))
1069 (save-excursion
1070 (goto-char 1)
1071 (re-search-forward
1072 "^%%BoundingBox:[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)")
1073 (setq x1 (match-string 1)
1074 y1 (match-string 2)
1075 x2 (match-string 3)
1076 y2 (match-string 4)))
1077 (unless (< (string-to-number x1) (string-to-number x2))
1078 (error "x1 (%s) should be less than x2 (%s)" x1 x2))
1079 (unless (< (string-to-number y1) (string-to-number y2))
1080 (error "y1 (%s) should be less than y2 (%s)" y1 y2))
1081 (setq f (ps-run-make-tmp-filename))
1082 (write-region
1083 (format
1084 "gsave
1085 initgraphics
1086 2 setlinewidth
1087 %s %s moveto
1088 %s %s lineto
1089 %s %s lineto
1090 %s %s lineto
1091 closepath
1092 gsave
1093 [ 4 20 ] 0 setdash
1094 1 0 0 setrgbcolor
1095 stroke
1096 grestore
1097 gsave
1098 [ 4 20 ] 8 setdash
1099 0 1 0 setrgbcolor
1100 stroke
1101 grestore
1102 [ 4 20 ] 16 setdash
1103 0 0 1 setrgbcolor
1104 stroke
1105 grestore
1106 " x1 y1 x2 y1 x2 y2 x1 y2)
1109 (ps-run-send-string (format "(%s) run" f))
1110 (set-buffer buf)))
1112 (defun ps-run-send-string (string)
1113 (let ((oldwin (selected-window)))
1114 (pop-to-buffer "*ps-run*")
1115 (comint-goto-process-mark)
1116 (insert string)
1117 (comint-send-input)
1118 (select-window oldwin)))
1120 (defun ps-run-make-tmp-filename ()
1121 (unless ps-mode-tmp-file
1122 (setq ps-mode-tmp-file
1123 (let ((temporary-file-directory (or ps-run-tmp-dir
1124 temporary-file-directory)))
1125 (make-temp-file "ps-run-"))))
1126 ps-mode-tmp-file)
1128 ;; Remove temporary file
1129 ;; This shouldn't fail twice, because it is called at kill-emacs
1130 (defun ps-run-cleanup ()
1131 (when ps-mode-tmp-file
1132 (let ((i ps-mode-tmp-file))
1133 (setq ps-mode-tmp-file nil)
1134 (when (file-exists-p i)
1135 (delete-file i)))))
1137 (defun ps-run-mouse-goto-error (event)
1138 "Set point at mouse click, then call `ps-run-goto-error'."
1139 (interactive "e")
1140 (mouse-set-point event)
1141 (ps-run-goto-error))
1143 (defun ps-run-goto-error ()
1144 "Jump to buffer position read as integer at point.
1145 Use line numbers if `ps-run-error-line-numbers' is not nil"
1146 (interactive)
1147 (let ((p (point)))
1148 (unless (looking-at "[0-9]")
1149 (goto-char (max 1 (1- (point)))))
1150 (when (looking-at "[0-9]")
1151 (forward-char 1)
1152 (forward-word -1)
1153 (when (looking-at "[0-9]+")
1154 (let (i)
1155 (setq
1157 (string-to-number
1158 (buffer-substring (match-beginning 0) (match-end 0))))
1159 (goto-char p)
1160 (pop-to-buffer ps-run-parent)
1161 (if ps-run-error-line-numbers
1162 (progn
1163 (goto-char (marker-position ps-run-mark))
1164 (forward-line (1- i))
1165 (end-of-line))
1166 (goto-char (+ i (marker-position ps-run-mark)))))))))
1170 (add-hook 'kill-emacs-hook 'ps-run-cleanup)
1172 (provide 'ps-mode)
1174 ;;; ps-mode.el ends here