1 ;;; ps-mode.el --- PostScript mode for GNU Emacs
3 ;; Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Peter Kleiweg <p.c.j.kleiweg@rug.nl>
7 ;; Maintainer: Peter Kleiweg <p.c.j.kleiweg@rug.nl>
8 ;; Created: 20 Aug 1997
9 ;; Version: 1.1h, 16 Jun 2005
10 ;; Keywords: PostScript, languages
12 ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
13 ;; file on 18/3/2008, and the maintainer agreed that when a bug is
14 ;; filed in the Emacs bug reporting system against this file, a copy
15 ;; of the bug report be sent to the maintainer's email address, but
16 ;; only if: "... those e-mails have a link to the bug report system,
17 ;; where I can cancel these e-mails if I want to.".
19 ;; This file is part of GNU Emacs.
21 ;; GNU Emacs is free software: you can redistribute it and/or modify
22 ;; it under the terms of the GNU General Public License as published by
23 ;; the Free Software Foundation, either version 3 of the License, or
24 ;; (at your option) any later version.
26 ;; GNU Emacs is distributed in the hope that it will be useful,
27 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
28 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 ;; GNU General Public License for more details.
31 ;; You should have received a copy of the GNU General Public License
32 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
39 (defconst ps-mode-version
"1.1h, 16 Jun 2005")
40 (defconst ps-mode-maintainer-address
"Peter Kleiweg <p.c.j.kleiweg@rug.nl>")
44 ;; Define core `PostScript' group.
45 (defgroup PostScript nil
46 "PostScript mode for Emacs."
49 (defgroup PostScript-edit nil
51 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
55 (defgroup PostScript-interaction nil
56 "PostScript interaction."
62 (defcustom ps-mode-auto-indent t
63 "*Should we use autoindent?"
64 :group
'PostScript-edit
67 (defcustom ps-mode-tab
4
68 "*Number of spaces to use when indenting."
69 :group
'PostScript-edit
72 (defcustom ps-mode-paper-size
'(595 842)
75 When inserting an EPSF template these values are used
76 to set the boundingbox to include the whole page.
77 When the figure is finished these values should be replaced."
78 :group
'PostScript-edit
80 (const :tag
"letter" (612 792))
81 (const :tag
"legal" (612 1008))
82 (const :tag
"a0" (2380 3368))
83 (const :tag
"a1" (1684 2380))
84 (const :tag
"a2" (1190 1684))
85 (const :tag
"a3" (842 1190))
86 (const :tag
"a4" (595 842))
87 (const :tag
"a5" (421 595))
88 (const :tag
"a6" (297 421))
89 (const :tag
"a7" (210 297))
90 (const :tag
"a8" (148 210))
91 (const :tag
"a9" (105 148))
92 (const :tag
"a10" (74 105))
93 (const :tag
"b0" (2836 4008))
94 (const :tag
"b1" (2004 2836))
95 (const :tag
"b2" (1418 2004))
96 (const :tag
"b3" (1002 1418))
97 (const :tag
"b4" (709 1002))
98 (const :tag
"b5" (501 709))
99 (const :tag
"archE" (2592 3456))
100 (const :tag
"archD" (1728 2592))
101 (const :tag
"archC" (1296 1728))
102 (const :tag
"archB" (864 1296))
103 (const :tag
"archA" (648 864))
104 (const :tag
"flsa" (612 936))
105 (const :tag
"flse" (612 936))
106 (const :tag
"halfletter" (396 612))
107 (const :tag
"11x17" (792 1224))
108 (const :tag
"tabloid" (792 1224))
109 (const :tag
"ledger" (1224 792))
110 (const :tag
"csheet" (1224 1584))
111 (const :tag
"dsheet" (1584 2448))
112 (const :tag
"esheet" (2448 3168))))
114 (defcustom ps-mode-print-function
116 (let ((lpr-switches nil
)
117 (lpr-command (if (memq system-type
'(usg-unix-v hpux irix
))
120 "*Lisp function to print current buffer as PostScript."
121 :group
'PostScript-edit
124 (defcustom ps-run-prompt
"\\(GS\\(<[0-9]+\\)?>\\)+"
125 "*Regexp to match prompt in interactive PostScript."
126 :group
'PostScript-interaction
129 (defcustom ps-run-font-lock-keywords-2
130 (append (unless (string= ps-run-prompt
"")
131 (list (list (if (= ?^
(string-to-char ps-run-prompt
))
133 (concat "^" ps-run-prompt
))
134 '(0 font-lock-function-name-face nil nil
))))
135 '((">>showpage, press <return> to continue<<"
136 (0 font-lock-keyword-face nil nil
))
137 ("^\\(Error\\|Can't\\).*"
138 (0 font-lock-warning-face nil nil
))
139 ("^\\(Current file position is\\) \\([0-9]+\\)"
140 (1 font-lock-comment-face nil nil
)
141 (2 font-lock-warning-face nil nil
))))
142 "*Medium level highlighting of messages from the PostScript interpreter.
144 See documentation on font-lock for details."
145 :group
'PostScript-interaction
146 :type
'(repeat (list :tag
"Expression with one or more highlighters"
147 :value
("" (0 default nil t
))
148 (regexp :tag
"Expression")
149 (repeat :tag
"Highlighters"
151 (list :tag
"Highlighter"
152 (integer :tag
"Subexp")
154 (boolean :tag
"Override")
155 (boolean :tag
"Laxmatch" :value t
))))))
157 (defcustom ps-run-x
'("gs" "-r72" "-sPAPERSIZE=a4")
158 "*Command as list to run PostScript with graphic display."
159 :group
'PostScript-interaction
160 :type
'(repeat string
))
162 (defcustom ps-run-dumb
'("gs" "-dNODISPLAY")
163 "*Command as list to run PostScript without graphic display."
164 :group
'PostScript-interaction
165 :type
'(repeat string
))
167 (defcustom ps-run-init nil
168 "*String of commands to send to PostScript to start interactive.
170 Example: \"executive\"
172 You won't need to set this option for Ghostscript."
173 :group
'PostScript-interaction
174 :type
'(choice (const nil
) string
))
176 (defcustom ps-run-error-line-numbers nil
177 "*What values are used by the PostScript interpreter in error messages?"
178 :group
'PostScript-interaction
179 :type
'(choice (const :tag
"line numbers" t
)
180 (const :tag
"byte counts" nil
)))
182 (defcustom ps-run-tmp-dir nil
183 "*Name of directory to place temporary file.
185 If nil, the following are tried in turn, until success:
190 :group
'PostScript-interaction
191 :type
'(choice (const nil
) directory
))
194 ;; Constants used for font-lock.
196 ;; Only a small set of the PostScript operators is selected for fontification.
197 ;; Fontification is meant to clarify the document structure and process flow,
198 ;; fontifying all known PostScript operators would hinder that objective.
199 (defconst ps-mode-operators
200 (let ((ops '("clear" "mark" "cleartomark" "counttomark"
202 "dict" "begin" "end" "def"
204 "exec" "if" "ifelse" "for" "repeat" "loop" "exit"
205 "stop" "stopped" "countexecstack" "execstack"
209 "gsave" "grestore" "grestoreall"
211 (concat "\\<" (regexp-opt ops t
) "\\>"))
212 "Regexp of PostScript operators that will be fontified.")
214 ;; Level 1 font-lock:
215 ;; - Special comments (reference face)
216 ;; - Strings and other comments
217 ;; - Partial strings (warning face)
218 ;; - 8bit characters (warning face)
219 ;; Multiline strings are not supported. Strings with nested brackets are.
220 (defconst ps-mode-font-lock-keywords-1
221 '(("\\`%!PS.*" . font-lock-reference-face
)
222 ("^%%BoundingBox:[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]*$"
223 . font-lock-reference-face
)
224 (ps-mode-match-string-or-comment
225 (1 font-lock-comment-face nil t
)
226 (2 font-lock-string-face nil t
))
227 ("([^()\n%]*\\|[^()\n]*)" . font-lock-warning-face
)
228 ("[\200-\377]+" (0 font-lock-warning-face prepend nil
)))
229 "Subdued level highlighting for PostScript mode.")
231 ;; Level 2 font-lock:
232 ;; - All from level 1
233 ;; - PostScript operators (keyword face)
234 (defconst ps-mode-font-lock-keywords-2
236 ps-mode-font-lock-keywords-1
239 ;; exclude names prepended by `/'
240 (concat "\\(^\\|[^/\n]\\)" ps-mode-operators
)
241 '(2 font-lock-keyword-face
))))
242 "Medium level highlighting for PostScript mode.")
244 ;; Level 3 font-lock:
245 ;; - All from level 2
246 ;; - Immediately evaluated names: those starting with `//' (type face)
247 ;; - Names that look like they are used for the definition of:
251 ;; * a "global" variable
252 ;; (function name face)
253 ;; - Other names (variable name face)
254 ;; The rules used to determine what names fit in the first category are:
255 ;; - Only names that are at the left margin, and one of these on the same line:
256 ;; * Nothing after the name except possibly one or more `[' or a comment
257 ;; * A `{' or `<<' or `[0-9]+ dict' following the name
258 ;; * A `def' somewhere in the same line
259 ;; Names are fontified before PostScript operators, allowing the use of
260 ;; a more simple (efficient) regexp than the one used in level 2.
261 (defconst ps-mode-font-lock-keywords-3
263 ps-mode-font-lock-keywords-1
265 '("//\\w+" . font-lock-type-face
)
268 "\\([[ \t]*\\(%.*\\)?\r?$" ; Nothing but `[' or comment after the name.
269 "\\|[ \t]*\\({\\|<<\\)" ; `{' or `<<' following the name.
270 "\\|[ \t]+[0-9]+[ \t]+dict\\>" ; `[0-9]+ dict' following the name.
271 "\\|.*\\<def\\>\\)") ; `def' somewhere on the same line.
272 .
(1 font-lock-function-name-face
))
273 '("/\\w+" . font-lock-variable-name-face
)
274 (cons ps-mode-operators
'font-lock-keyword-face
)))
275 "High level highliting for PostScript mode.")
277 (defconst ps-mode-font-lock-keywords ps-mode-font-lock-keywords-1
278 "Default expressions to highlight in PostScript mode.")
280 ;; Level 1 font-lock for ps-run-mode
281 ;; - prompt (function name face)
282 (defconst ps-run-font-lock-keywords-1
283 (unless (string= "" ps-run-prompt
)
284 (list (cons (if (= ?^
(string-to-char ps-run-prompt
))
286 (concat "^" ps-run-prompt
))
287 'font-lock-function-name-face
)))
288 "Subdued level highlighting for PostScript run mode.")
290 (defconst ps-run-font-lock-keywords ps-run-font-lock-keywords-1
291 "Default expressions to highlight in PostScript run mode.")
296 (defvar ps-mode-map nil
297 "Local keymap to use in PostScript mode.")
299 (defvar ps-mode-syntax-table nil
300 "Syntax table used while in PostScript mode.")
302 (defvar ps-run-mode-map nil
303 "Local keymap to use in PostScript run mode.")
305 (defvar ps-mode-tmp-file nil
306 "Name of temporary file, set by `ps-run'.")
308 (defvar ps-run-mark nil
309 "Mark to start of region that was sent to PostScript interpreter.")
311 (defvar ps-run-parent nil
312 "Parent window of interactive PostScript.")
317 (defconst ps-mode-menu-main
319 ["EPSF Template, Sparse" ps-mode-epsf-sparse t
]
320 ["EPSF Template, Rich" ps-mode-epsf-rich t
]
324 ["ISOLatin1Extended" ps-mode-latin-extended t
]
325 ["center" ps-mode-center t
]
326 ["right" ps-mode-right t
]
327 ["Heapsort" ps-mode-heapsort t
])
329 ["Times-Roman" (insert "/Times-Roman ") t
]
330 ["Times-Bold" (insert "/Times-Bold ") t
]
331 ["Times-Italic" (insert "/Times-Italic ") t
]
332 ["Times-BoldItalic" (insert "/Times-BoldItalic ") t
]
333 ["Helvetica" (insert "/Helvetica ") t
]
334 ["Helvetica-Bold" (insert "/Helvetica-Bold ") t
]
335 ["Helvetica-Oblique" (insert "/Helvetica-Oblique ") t
]
336 ["Helvetica-BoldOblique" (insert "/Helvetica-BoldOblique ") t
]
337 ["Courier" (insert "/Courier ") t
]
338 ["Courier-Bold" (insert "/Courier-Bold ") t
]
339 ["Courier-Oblique" (insert "/Courier-Oblique ") t
]
340 ["Courier-BoldOblique" (insert "/Courier-BoldOblique ") t
]
341 ["Symbol" (insert "/Symbol") t
])
343 ["AvantGarde-Book" (insert "/AvantGarde-Book ") t
]
344 ["AvantGarde-Demi" (insert "/AvantGarde-Demi ") t
]
345 ["AvantGarde-BookOblique" (insert "/AvantGarde-BookOblique ") t
]
346 ["AvantGarde-DemiOblique" (insert "/AvantGarde-DemiOblique ") t
]
347 ["Bookman-Light" (insert "/Bookman-Light ") t
]
348 ["Bookman-Demi" (insert "/Bookman-Demi ") t
]
349 ["Bookman-LightItalic" (insert "/Bookman-LightItalic ") t
]
350 ["Bookman-DemiItalic" (insert "/Bookman-DemiItalic ") t
]
351 ["Helvetica-Narrow" (insert "/Helvetica-Narrow ") t
]
352 ["Helvetica-Narrow-Bold" (insert "/Helvetica-Narrow-Bold ") t
]
353 ["Helvetica-Narrow-Oblique" (insert "/Helvetica-Narrow-Oblique ") t
]
354 ["Helvetica-Narrow-BoldOblique" (insert "/Helvetica-Narrow-BoldOblique ") t
]
355 ["NewCenturySchlbk-Roman" (insert "/NewCenturySchlbk-Roman ") t
]
356 ["NewCenturySchlbk-Bold" (insert "/NewCenturySchlbk-Bold ") t
]
357 ["NewCenturySchlbk-Italic" (insert "/NewCenturySchlbk-Italic ") t
]
358 ["NewCenturySchlbk-BoldItalic" (insert "/NewCenturySchlbk-BoldItalic ") t
]
359 ["Palatino-Roman" (insert "/Palatino-Roman ") t
]
360 ["Palatino-Bold" (insert "/Palatino-Bold ") t
]
361 ["Palatino-Italic" (insert "/Palatino-Italic ") t
]
362 ["Palatino-BoldItalic" (insert "/Palatino-BoldItalic ") t
]
363 ["ZapfChancery-MediumItalic" (insert "/ZapfChancery-MediumItalic ") t
]
364 ["ZapfDingbats" (insert "/ZapfDingbats ") t
])
366 ["Comment Out Region" ps-mode-comment-out-region
(mark t
)]
367 ["Uncomment Region" ps-mode-uncomment-region
(mark t
)]
369 ["8-bit to Octal Buffer" ps-mode-octal-buffer t
]
370 ["8-bit to Octal Region" ps-mode-octal-region
(mark t
)]
372 ["Auto Indent" (setq ps-mode-auto-indent
(not ps-mode-auto-indent
))
373 :style toggle
:selected ps-mode-auto-indent
]
378 ["Quit PostScript" ps-run-quit
(process-status "ps-run")]
379 ["Kill PostScript" ps-run-kill
(process-status "ps-run")]
380 ["Send Buffer to Interpreter"
382 (process-status "ps-run")]
383 ["Send Region to Interpreter"
385 (and (mark t
) (process-status "ps-run"))]
386 ["Send Newline to Interpreter"
387 ps-mode-other-newline
388 (process-status "ps-run")]
391 (process-status "ps-run")]
392 ["Clear/Reset PostScript Graphics"
394 (process-status "ps-run")]
396 ["Print Buffer as PostScript"
399 ["Print Region as PostScript"
403 ["Customize for PostScript"
404 (customize-group "PostScript")
408 ps-mode-submit-bug-report
412 ;; Mode maps for PostScript edit mode and PostScript interaction mode.
415 (setq ps-mode-map
(make-sparse-keymap))
416 (define-key ps-mode-map
"\C-c\C-v" 'ps-run-boundingbox
)
417 (define-key ps-mode-map
"\C-c\C-u" 'ps-mode-uncomment-region
)
418 (define-key ps-mode-map
"\C-c\C-t" 'ps-mode-epsf-rich
)
419 (define-key ps-mode-map
"\C-c\C-s" 'ps-run-start
)
420 (define-key ps-mode-map
"\C-c\C-r" 'ps-run-region
)
421 (define-key ps-mode-map
"\C-c\C-q" 'ps-run-quit
)
422 (define-key ps-mode-map
"\C-c\C-p" 'ps-mode-print-buffer
)
423 (define-key ps-mode-map
"\C-c\C-o" 'ps-mode-comment-out-region
)
424 (define-key ps-mode-map
"\C-c\C-k" 'ps-run-kill
)
425 (define-key ps-mode-map
"\C-c\C-j" 'ps-mode-other-newline
)
426 (define-key ps-mode-map
"\C-c\C-l" 'ps-run-clear
)
427 (define-key ps-mode-map
"\C-c\C-b" 'ps-run-buffer
)
428 (define-key ps-mode-map
">" 'ps-mode-r-gt
)
429 (define-key ps-mode-map
"]" 'ps-mode-r-angle
)
430 (define-key ps-mode-map
"}" 'ps-mode-r-brace
)
431 (define-key ps-mode-map
"\177" 'ps-mode-backward-delete-char
)
432 (define-key ps-mode-map
"\t" 'ps-mode-tabkey
)
433 (define-key ps-mode-map
"\r" 'ps-mode-newline
)
434 (define-key ps-mode-map
[return] 'ps-mode-newline)
435 (easy-menu-define ps-mode-main ps-mode-map "PostScript" ps-mode-menu-main))
437 (unless ps-run-mode-map
438 (setq ps-run-mode-map (make-sparse-keymap))
439 (define-key ps-run-mode-map "\C-c\C-q" 'ps-run-quit)
440 (define-key ps-run-mode-map "\C-c\C-k" 'ps-run-kill)
441 (define-key ps-run-mode-map "\C-c\C-e" 'ps-run-goto-error)
442 (define-key ps-run-mode-map [mouse-2] 'ps-run-mouse-goto-error)
443 (define-key ps-run-mode-map "\r" 'ps-run-newline)
444 (define-key ps-run-mode-map [return] 'ps-run-newline
))
449 (unless ps-mode-syntax-table
450 (setq ps-mode-syntax-table
(make-syntax-table))
452 (modify-syntax-entry ?\%
"< " ps-mode-syntax-table
)
453 (modify-syntax-entry ?
\n "> " ps-mode-syntax-table
)
454 (modify-syntax-entry ?
\r "> " ps-mode-syntax-table
)
455 (modify-syntax-entry ?
\f "> " ps-mode-syntax-table
)
456 (modify-syntax-entry ?\
< "(>" ps-mode-syntax-table
)
457 (modify-syntax-entry ?\
> ")<" 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)
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
)
476 (modify-syntax-entry ?^
"w " ps-mode-syntax-table
) ; NOT: ?\^
477 (modify-syntax-entry ?\_
"w " ps-mode-syntax-table
)
478 (modify-syntax-entry ?\
` "w " ps-mode-syntax-table
)
479 (modify-syntax-entry ?\|
"w " ps-mode-syntax-table
)
480 (modify-syntax-entry ?\~
"w " ps-mode-syntax-table
)
484 (modify-syntax-entry i
"w " ps-mode-syntax-table
)
489 (declare-function doc-view-minor-mode
"doc-view")
494 (define-derived-mode ps-mode fundamental-mode
"PostScript"
495 "Major mode for editing PostScript with GNU Emacs.
497 Entry to this mode calls `ps-mode-hook'.
499 The following variables hold user options, and can
500 be set through the `customize' command:
502 `ps-mode-auto-indent'
505 `ps-mode-print-function'
507 `ps-run-font-lock-keywords-2'
511 `ps-run-error-line-numbers'
514 Type \\[describe-variable] for documentation on these options.
520 When starting an interactive PostScript process with \\[ps-run-start],
521 a second window will be displayed, and `ps-run-mode-hook' will be called.
522 The keymap for this second window is:
527 When Ghostscript encounters an error it displays an error message
528 with a file position. Clicking mouse-2 on this number will bring
529 point to the corresponding spot in the PostScript window, if input
530 to the interpreter was sent from that window.
531 Typing \\<ps-run-mode-map>\\[ps-run-goto-error] when the cursor is at the number has the same effect."
532 (set (make-local-variable 'font-lock-defaults
)
533 '((ps-mode-font-lock-keywords
534 ps-mode-font-lock-keywords-1
535 ps-mode-font-lock-keywords-2
536 ps-mode-font-lock-keywords-3
)
538 (set (make-local-variable 'comment-start
) "%")
539 ;; NOTE: `\' has a special meaning in strings only
540 (set (make-local-variable 'comment-start-skip
) "%+[ \t]*")
541 ;; enable doc-view-minor-mode => C-c C-c starts viewing the current ps file
542 ;; with doc-view-mode.
543 (doc-view-minor-mode 1))
545 (defun ps-mode-show-version ()
546 "Show current version of PostScript mode."
548 (message " *** PostScript Mode (ps-mode) Version %s *** " ps-mode-version
))
550 (defun ps-mode-submit-bug-report ()
551 "Submit via mail a bug report on PostScript mode."
553 (when (y-or-n-p "Submit bug report on PostScript mode? ")
554 (let ((reporter-prompt-for-summary-p nil
)
555 (reporter-dont-compact-list '(ps-mode-print-function
556 ps-run-font-lock-keywords-2
)))
557 (reporter-submit-bug-report
558 ps-mode-maintainer-address
559 (format "ps-mode.el %s [%s]" ps-mode-version system-type
)
560 '(ps-mode-auto-indent
563 ps-mode-print-function
565 ps-run-font-lock-keywords-2
569 ps-run-error-line-numbers
573 ;; Helper functions for font-lock.
575 ;; When this function is called, point is at an opening bracket.
576 ;; This function should test if point is at the start of a string
577 ;; with nested brackets.
578 ;; If true: move point to end of string
579 ;; set string to match data nr 2
581 ;; If false: return nil
582 (defun ps-mode-looking-at-nested (limit)
583 (let ((first (point))
586 ;; Move past opening bracket.
589 (while (and (> level
0) (< pos limit
))
590 ;; Search next bracket, stepping over escaped brackets.
591 (if (not (looking-at "\\([^()\\\n]\\|\\\\.\\)*\\([()]\\)"))
593 (setq level
(+ level
(if (string= "(" (match-string 2)) 1 -
1)))
594 (goto-char (setq pos
(match-end 0)))))
595 (if (not (= level
0))
597 ;; Found string with nested brackets, now set match data nr 2.
598 (set-match-data (list first pos nil nil first pos
))
601 ;; This function should search for a string or comment
602 ;; If comment, return as match data nr 1
603 ;; If string, return as match data nr 2
604 (defun ps-mode-match-string-or-comment (limit)
605 ;; Find the first potential match.
606 (if (not (re-search-forward "[%(]" limit t
))
607 ;; Nothing found: return failure.
609 (let ((end (match-end 0)))
610 (goto-char (match-beginning 0))
611 (cond ((looking-at "\\(%.*\\)\\|\\((\\([^()\\\n]\\|\\\\.\\)*)\\)")
612 ;; It's a comment or string without nested, unescaped brackets.
613 (goto-char (match-end 0))
615 ((ps-mode-looking-at-nested limit
)
616 ;; It's a string with nested brackets.
621 (ps-mode-match-string-or-comment limit
))))))
626 (defun ps-mode-target-column ()
627 "To what column should text on current line be indented?
629 Identation is increased if the last token on the current line
630 defines the beginning of a group. These tokens are: { [ <<"
633 (if (looking-at "[ \t]*\\(}\\|\\]\\|>>\\)")
636 (goto-char (match-end 0))
639 (if (looking-at "[ \t]+")
640 (goto-char (match-end 0)))
644 (message "%s" (error-message-string err
))
647 (if (not (re-search-backward "[^ \t\n\r\f][ \t\n\r\f]*\\=" nil t
))
649 (goto-char (match-beginning 0))
651 (if (looking-at "[ \t]+")
652 (goto-char (match-end 0)))
653 (setq target
(current-column))
655 (if (re-search-backward "\\({\\|\\[\\|<<\\)[ \t]*\\(%[^\n]*\\)?\\=" nil t
)
656 (setq target
(+ target ps-mode-tab
)))
659 (defun ps-mode-newline ()
660 "Insert newline with proper indentation."
662 (delete-horizontal-space)
664 (if ps-mode-auto-indent
665 (indent-to (ps-mode-target-column))))
667 (defun ps-mode-tabkey ()
668 "Indent/reindent current line, or insert tab."
670 (let ((column (current-column))
672 (if (or (not ps-mode-auto-indent
)
674 (not (re-search-backward "^[ \t]*\\=" nil t
)))
676 (setq target
(ps-mode-target-column))
677 (while (<= target column
)
678 (setq target
(+ target ps-mode-tab
)))
679 (indent-line-to target
))))
681 (defun ps-mode-backward-delete-char ()
682 "Delete backward indentation, or delete backward character."
684 (let ((column (current-column))
686 (if (or (not ps-mode-auto-indent
)
688 (not (re-search-backward "^[ \t]+\\=" nil t
)))
689 (delete-backward-char 1)
690 (setq target
(ps-mode-target-column))
691 (while (> column target
)
692 (setq target
(+ target ps-mode-tab
)))
693 (while (>= target column
)
694 (setq target
(- target ps-mode-tab
)))
697 (indent-line-to target
))))
699 (defun ps-mode-r-brace ()
700 "Insert `}' and perform balance."
703 (ps-mode-r-balance "}"))
705 (defun ps-mode-r-angle ()
706 "Insert `]' and perform balance."
709 (ps-mode-r-balance "]"))
711 (defun ps-mode-r-gt ()
712 "Insert `>' and perform balance."
715 (ps-mode-r-balance ">>"))
717 (defun ps-mode-r-balance (right)
718 "Adjust indentification if point after RIGHT."
719 (if ps-mode-auto-indent
721 (when (re-search-backward (concat "^[ \t]*" (regexp-quote right
) "\\=") nil t
)
722 (indent-line-to (ps-mode-target-column)))))
723 (blink-matching-open))
725 (defun ps-mode-other-newline ()
726 "Perform newline in `*ps run*' buffer."
728 (let ((buf (current-buffer)))
729 (set-buffer "*ps run*")
736 (defun ps-mode-print-buffer ()
737 "Print buffer as PostScript."
739 (funcall ps-mode-print-function
))
741 (defun ps-mode-print-region (begin end
)
742 "Print region as PostScript, adding minimal header and footer lines:
748 (let ((buf (current-buffer)))
751 (insert-buffer-substring buf begin end
)
752 (insert "\nshowpage\n")
753 (funcall ps-mode-print-function
))))
756 ;; Comment Out / Uncomment.
758 (defun ps-mode-comment-out-region (begin end
)
759 "Comment out region."
761 (let ((endm (make-marker)))
762 (set-marker endm end
)
765 (if (= (current-column) 0)
767 (while (and (= (forward-line) 0)
768 (< (point) (marker-position endm
)))
770 (set-marker endm nil
)))
772 (defun ps-mode-uncomment-region (begin end
)
775 Only one `%' is removed, and it has to be in the first column."
777 (let ((endm (make-marker)))
778 (set-marker endm end
)
781 (if (looking-at "^%")
783 (while (and (= (forward-line) 0)
784 (< (point) (marker-position endm
)))
787 (set-marker endm nil
)))
790 ;; Convert 8-bit to octal codes.
792 (defun ps-mode-octal-buffer ()
793 "Change 8-bit characters to octal codes in buffer."
795 (ps-mode-octal-region (point-min) (point-max)))
797 (defun ps-mode-octal-region (begin end
)
798 "Change 8-bit characters to octal codes in region."
803 (message "Buffer is read only"))
806 (setq endm
(make-marker))
807 (set-marker endm end
)
810 (while (re-search-forward "[\200-\377]" (marker-position endm
) t
)
813 (insert (format "\\%03o" (string-to-char (buffer-substring (point) (1+ (point))))))
815 (message "%d change%s made" i
(if (= i
1) "" "s"))
816 (set-marker endm nil
)))))
821 (defun ps-mode-center ()
822 "Insert function /center."
833 (defun ps-mode-right ()
834 "Insert function /right."
846 "Insert function /RE."
849 % `new-font-name' `encoding-vector' `old-font-name' RE -
852 dup maxlength dict begin {
853 1 index /FID ne { def } { pop pop } ifelse
856 dup /FontName exch def
857 currentdict end definefont pop
861 (defun ps-mode-latin-extended ()
862 "Insert array /ISOLatin1Extended.
864 This encoding vector contains all the entries from ISOLatin1Encoding
865 plus the usually uncoded characters inserted on positions 1 through 28."
868 % ISOLatin1Encoding, extended with remaining uncoded glyphs
870 /.notdef /Lslash /lslash /OE /oe /Scaron /scaron /Zcaron /zcaron
871 /Ydieresis /trademark /bullet /dagger /daggerdbl /ellipsis /emdash
872 /endash /fi /fl /florin /fraction /guilsinglleft /guilsinglright
873 /perthousand /quotedblbase /quotedblleft /quotedblright
874 /quotesinglbase /quotesingle /.notdef /.notdef /.notdef /space
875 /exclam /quotedbl /numbersign /dollar /percent /ampersand
876 /quoteright /parenleft /parenright /asterisk /plus /comma /minus
877 /period /slash /zero /one /two /three /four /five /six /seven /eight
878 /nine /colon /semicolon /less /equal /greater /question /at /A /B /C
879 /D /E /F /G /H /I /J /K /L /M /N /O /P /Q /R /S /T /U /V /W /X /Y /Z
880 /bracketleft /backslash /bracketright /asciicircum /underscore
881 /quoteleft /a /b /c /d /e /f /g /h /i /j /k /l /m /n /o /p /q /r /s
882 /t /u /v /w /x /y /z /braceleft /bar /braceright /asciitilde
883 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
884 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
885 /.notdef /.notdef /.notdef /dotlessi /grave /acute /circumflex
886 /tilde /macron /breve /dotaccent /dieresis /.notdef /ring /cedilla
887 /.notdef /hungarumlaut /ogonek /caron /space /exclamdown /cent
888 /sterling /currency /yen /brokenbar /section /dieresis /copyright
889 /ordfeminine /guillemotleft /logicalnot /hyphen /registered /macron
890 /degree /plusminus /twosuperior /threesuperior /acute /mu /paragraph
891 /periodcentered /cedilla /onesuperior /ordmasculine /guillemotright
892 /onequarter /onehalf /threequarters /questiondown /Agrave /Aacute
893 /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla /Egrave /Eacute
894 /Ecircumflex /Edieresis /Igrave /Iacute /Icircumflex /Idieresis /Eth
895 /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply
896 /Oslash /Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn
897 /germandbls /agrave /aacute /acircumflex /atilde /adieresis /aring
898 /ae /ccedilla /egrave /eacute /ecircumflex /edieresis /igrave
899 /iacute /icircumflex /idieresis /eth /ntilde /ograve /oacute
900 /ocircumflex /otilde /odieresis /divide /oslash /ugrave /uacute
901 /ucircumflex /udieresis /yacute /thorn /ydieresis
905 (defun ps-mode-heapsort ()
906 "Insert function /Heapsort."
909 % `array-element' Heapsort-cvi-or-cvr-or-cvs `number-or-string'
910 /Heapsort-cvi-or-cvr-or-cvs {
913 % `array' Heapsort `sorted-array'
915 dup length /hsR exch def
916 /hsL hsR 2 idiv 1 add def
923 dup dup dup 0 get exch dup hsR get
927 dup hsL 1 sub get /hsT exch def
932 hsJ hsR gt { exit } if
934 dup dup hsJ 1 sub get Heapsort-cvi-or-cvr-or-cvs
935 exch hsJ get Heapsort-cvi-or-cvr-or-cvs
936 lt { /hsJ hsJ 1 add def } if
938 dup hsJ 1 sub get Heapsort-cvi-or-cvr-or-cvs
939 hsT Heapsort-cvi-or-cvr-or-cvs
941 dup dup hsS 1 sub exch hsJ 1 sub get put
943 dup hsS 1 sub hsT put
949 ;; EPSF document lay-out.
951 (defun ps-mode-epsf-sparse ()
952 "Insert sparse EPSF template."
954 (goto-char (point-max))
955 (unless (re-search-backward "%%EOF[ \t\n]*\\'" nil t
)
956 (goto-char (point-max))
957 (insert "\n%%EOF\n"))
958 (goto-char (point-max))
959 (unless (re-search-backward "\\bshowpage[ \t\n]+%%EOF[ \t\n]*\\'" nil t
)
960 (re-search-backward "%%EOF")
961 (insert "showpage\n"))
962 (goto-char (point-max))
963 (unless (re-search-backward "\\bend[ \t\n]+\\bshowpage[ \t\n]+%%EOF[ \t\n]*\\'" nil t
)
964 (re-search-backward "showpage")
966 (goto-char (point-min))
967 (insert "%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 ")
968 (insert (format "%d %d\n\n"
969 (car ps-mode-paper-size
)
970 (car (cdr ps-mode-paper-size
))))
971 (insert "64 dict begin\n\n"))
973 (defun ps-mode-epsf-rich ()
974 "Insert rich EPSF template."
976 (ps-mode-epsf-sparse)
978 (when buffer-file-name
979 (insert "%%Title: " (file-name-nondirectory buffer-file-name
) "\n"))
980 (insert "%%Creator: " (user-full-name) "\n")
981 (insert "%%CreationDate: " (current-time-string) "\n")
982 (insert "%%EndComments\n")
986 ;; Interactive PostScript interpreter.
988 (define-derived-mode ps-run-mode fundamental-mode
"Interactive PS"
989 "Major mode in interactive PostScript window.
990 This mode is invoked from `ps-mode' and should not be called directly.
993 (set (make-local-variable 'font-lock-defaults
)
994 '((ps-run-font-lock-keywords
995 ps-run-font-lock-keywords-1
996 ps-run-font-lock-keywords-2
)
998 (setq mode-line-process
'(":%s")))
1000 (defun ps-run-running ()
1001 "Error if not in `ps-mode' or not running PostScript."
1002 (unless (equal major-mode
'ps-mode
)
1003 (error "This function can only be called from PostScript mode"))
1004 (unless (equal (process-status "ps-run") 'run
)
1005 (error "No PostScript process running")))
1007 (defun ps-run-start ()
1008 "Start interactive PostScript."
1010 (let ((command (or (and window-system ps-run-x
) ps-run-dumb
))
1012 (process-connection-type nil
)
1013 (oldwin (selected-window)))
1015 (error "No command specified to run interactive PostScript"))
1016 (unless (and ps-run-mark
(markerp ps-run-mark
))
1017 (setq ps-run-mark
(make-marker)))
1019 (setq init-file
(ps-run-make-tmp-filename))
1020 (write-region (concat ps-run-init
"\n") 0 init-file
)
1021 (setq init-file
(list init-file
)))
1022 (pop-to-buffer "*ps run*")
1024 (when (process-status "ps-run")
1025 (delete-process "ps-run"))
1027 (setq command
(append command init-file
))
1028 (insert (mapconcat 'identity command
" ") "\n")
1029 (apply 'start-process
"ps-run" "*ps run*" command
)
1030 (select-window oldwin
)))
1032 (defun ps-run-quit ()
1033 "Quit interactive PostScript."
1035 (ps-run-send-string "quit" t
)
1038 (defun ps-run-kill ()
1039 "Kill interactive PostScript."
1041 (delete-process "ps-run")
1044 (defun ps-run-clear ()
1045 "Clear/reset PostScript graphics."
1047 (ps-run-send-string "showpage" t
)
1049 (ps-run-send-string "" t
))
1051 (defun ps-run-buffer ()
1052 "Send buffer to PostScript interpreter."
1054 (ps-run-region (point-min) (point-max)))
1056 (defun ps-run-region (begin end
)
1057 "Send region to PostScript interpreter."
1060 (setq ps-run-parent
(buffer-name))
1061 (let ((f (ps-run-make-tmp-filename)))
1062 (set-marker ps-run-mark begin
)
1063 (write-region begin end f
)
1064 (ps-run-send-string (format "(%s) run" f
) t
)))
1066 (defun ps-run-boundingbox ()
1071 (buf (current-buffer)))
1075 "^%%BoundingBox:[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)")
1076 (setq x1
(match-string 1)
1079 y2
(match-string 4)))
1080 (unless (< (string-to-number x1
) (string-to-number x2
))
1081 (error "x1 (%s) should be less than x2 (%s)" x1 x2
))
1082 (unless (< (string-to-number y1
) (string-to-number y2
))
1083 (error "y1 (%s) should be less than y2 (%s)" y1 y2
))
1084 (setq f
(ps-run-make-tmp-filename))
1109 " x1 y1 x2 y1 x2 y2 x1 y2
)
1112 (ps-run-send-string (format "(%s) run" f
) t
)
1115 (defun ps-run-send-string (string &optional echo
)
1116 (let ((oldwin (selected-window)))
1117 (pop-to-buffer "*ps run*")
1118 (goto-char (point-max))
1120 (insert string
"\n"))
1121 (set-marker (process-mark (get-process "ps-run")) (point))
1122 (process-send-string "ps-run" (concat string
"\n"))
1123 (select-window oldwin
)))
1125 (defun ps-run-make-tmp-filename ()
1126 (unless ps-mode-tmp-file
1127 (cond (ps-run-tmp-dir)
1128 ((setq ps-run-tmp-dir
(getenv "TEMP")))
1129 ((setq ps-run-tmp-dir
(getenv "TMP")))
1130 ((setq ps-run-tmp-dir
(getenv "HOME"))
1133 (concat (file-name-as-directory ps-run-tmp-dir
) "tmp"))
1134 (unless (file-directory-p ps-run-tmp-dir
)
1135 (setq ps-run-tmp-dir nil
))))
1136 (unless ps-run-tmp-dir
1137 (setq ps-run-tmp-dir
"/tmp"))
1138 (setq ps-mode-tmp-file
1142 (file-name-as-directory ps-run-tmp-dir
)
1147 ;; Remove temporary file
1148 ;; This shouldn't fail twice, because it is called at kill-emacs
1149 (defun ps-run-cleanup ()
1150 (when ps-mode-tmp-file
1151 (let ((i ps-mode-tmp-file
))
1152 (setq ps-mode-tmp-file nil
)
1153 (when (file-exists-p i
)
1156 (defun ps-run-mouse-goto-error (event)
1157 "Set point at mouse click, then call `ps-run-goto-error'."
1159 (mouse-set-point event
)
1160 (ps-run-goto-error))
1162 (defun ps-run-newline ()
1163 "Process newline in PostScript interpreter window."
1168 (when (looking-at ps-run-prompt
)
1169 (goto-char (match-end 0)))
1171 (goto-char (1+ (match-end 0)))
1172 (ps-run-send-string (buffer-substring (match-beginning 0) (match-end 0))))
1174 (defun ps-run-goto-error ()
1175 "Jump to buffer position read as integer at point.
1176 Use line numbers if `ps-run-error-line-numbers' is not nil"
1179 (unless (looking-at "[0-9]")
1180 (goto-char (max 1 (1- (point)))))
1181 (when (looking-at "[0-9]")
1184 (when (looking-at "[0-9]+")
1189 (buffer-substring (match-beginning 0) (match-end 0))))
1191 (pop-to-buffer ps-run-parent
)
1192 (if ps-run-error-line-numbers
1194 (goto-char (marker-position ps-run-mark
))
1195 (forward-line (1- i
))
1197 (goto-char (+ i
(marker-position ps-run-mark
)))))))))
1201 (add-hook 'kill-emacs-hook
'ps-run-cleanup
)
1205 ;; arch-tag: dce13d2d-69fb-4ec4-9d5d-6dd29c3f0e6e
1206 ;;; ps-mode.el ends here