1 ;;; ps-mode.el --- PostScript mode for GNU Emacs
3 ;; Copyright (C) 1999, 2001-2016 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
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/>.
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")
46 ;; Define core `PostScript' group.
47 (defgroup PostScript nil
48 "PostScript mode for Emacs."
51 (defgroup PostScript-edit nil
53 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
57 (defgroup PostScript-interaction nil
58 "PostScript interaction."
64 (make-obsolete-variable 'ps-mode-auto-indent
'electric-indent-mode
"25.1")
66 (defcustom ps-mode-tab
4
67 "Number of spaces to use when indenting."
68 :group
'PostScript-edit
71 (defcustom ps-mode-paper-size
'(595 842)
74 When inserting an EPSF template these values are used
75 to set the boundingbox to include the whole page.
76 When the figure is finished these values should be replaced."
77 :group
'PostScript-edit
79 (const :tag
"letter" (612 792))
80 (const :tag
"legal" (612 1008))
81 (const :tag
"a0" (2380 3368))
82 (const :tag
"a1" (1684 2380))
83 (const :tag
"a2" (1190 1684))
84 (const :tag
"a3" (842 1190))
85 (const :tag
"a4" (595 842))
86 (const :tag
"a5" (421 595))
87 (const :tag
"a6" (297 421))
88 (const :tag
"a7" (210 297))
89 (const :tag
"a8" (148 210))
90 (const :tag
"a9" (105 148))
91 (const :tag
"a10" (74 105))
92 (const :tag
"b0" (2836 4008))
93 (const :tag
"b1" (2004 2836))
94 (const :tag
"b2" (1418 2004))
95 (const :tag
"b3" (1002 1418))
96 (const :tag
"b4" (709 1002))
97 (const :tag
"b5" (501 709))
98 (const :tag
"archE" (2592 3456))
99 (const :tag
"archD" (1728 2592))
100 (const :tag
"archC" (1296 1728))
101 (const :tag
"archB" (864 1296))
102 (const :tag
"archA" (648 864))
103 (const :tag
"flsa" (612 936))
104 (const :tag
"flse" (612 936))
105 (const :tag
"halfletter" (396 612))
106 (const :tag
"11x17" (792 1224))
107 (const :tag
"tabloid" (792 1224))
108 (const :tag
"ledger" (1224 792))
109 (const :tag
"csheet" (1224 1584))
110 (const :tag
"dsheet" (1584 2448))
111 (const :tag
"esheet" (2448 3168))))
113 (defcustom ps-mode-print-function
115 (let ((lpr-switches nil
)
116 (lpr-command (if (memq system-type
'(usg-unix-v hpux
))
119 "Lisp function to print current buffer as PostScript."
120 :group
'PostScript-edit
123 (defcustom ps-run-prompt
"\\(GS\\(<[0-9]+\\)?>\\)+"
124 "Regexp to match prompt in interactive PostScript."
125 :group
'PostScript-interaction
128 (defcustom ps-run-font-lock-keywords-2
129 (append (unless (string= ps-run-prompt
"")
130 (list (list (if (= ?^
(string-to-char ps-run-prompt
))
132 (concat "^" ps-run-prompt
))
133 '(0 font-lock-function-name-face nil nil
))))
134 '((">>showpage, press <return> to continue<<"
135 (0 font-lock-keyword-face nil nil
))
136 ("^\\(Error\\|Can't\\).*"
137 (0 font-lock-warning-face nil nil
))
138 ("^\\(Current file position is\\) \\([0-9]+\\)"
139 (1 font-lock-comment-face nil nil
)
140 (2 font-lock-warning-face nil nil
))))
141 "Medium level highlighting of messages from the PostScript interpreter.
143 See documentation on font-lock for details."
144 :group
'PostScript-interaction
145 :type
'(repeat (list :tag
"Expression with one or more highlighters"
146 :value
("" (0 default nil t
))
147 (regexp :tag
"Expression")
148 (repeat :tag
"Highlighters"
150 (list :tag
"Highlighter"
151 (integer :tag
"Subexp")
153 (boolean :tag
"Override")
154 (boolean :tag
"Laxmatch" :value t
))))))
156 (defcustom ps-run-x
'("gs" "-r72" "-sPAPERSIZE=a4")
157 "Command as list to run PostScript with graphic display."
158 :group
'PostScript-interaction
159 :type
'(repeat string
))
161 (defcustom ps-run-dumb
'("gs" "-dNODISPLAY")
162 "Command as list to run PostScript without graphic display."
163 :group
'PostScript-interaction
164 :type
'(repeat string
))
166 (defcustom ps-run-init nil
167 "String of commands to send to PostScript to start interactive.
169 Example: \"executive\"
171 You won't need to set this option for Ghostscript."
172 :group
'PostScript-interaction
173 :type
'(choice (const nil
) string
))
175 (defcustom ps-run-error-line-numbers nil
176 "What values are used by the PostScript interpreter in error messages?"
177 :group
'PostScript-interaction
178 :type
'(choice (const :tag
"line numbers" t
)
179 (const :tag
"byte counts" nil
)))
181 (defcustom ps-run-tmp-dir nil
182 "Name of directory to place temporary file.
183 If nil, use `temporary-file-directory'."
184 :group
'PostScript-interaction
185 :type
'(choice (const nil
) directory
))
188 ;; Constants used for font-lock.
190 ;; Only a small set of the PostScript operators is selected for fontification.
191 ;; Fontification is meant to clarify the document structure and process flow,
192 ;; fontifying all known PostScript operators would hinder that objective.
193 (defconst ps-mode-operators
194 (let ((ops '("clear" "mark" "cleartomark" "counttomark"
196 "dict" "begin" "end" "def"
198 "exec" "if" "ifelse" "for" "repeat" "loop" "exit"
199 "stop" "stopped" "countexecstack" "execstack"
203 "gsave" "grestore" "grestoreall"
205 (concat "\\_<" (regexp-opt ops t
) "\\_>"))
206 "Regexp of PostScript operators that will be fontified.")
208 ;; Level 1 font-lock:
209 ;; - Special comments (reference face)
210 ;; - Strings and other comments
211 ;; - Partial strings (warning face)
212 ;; - 8bit characters (warning face)
213 ;; Multiline strings are not supported. Strings with nested brackets are.
214 (defconst ps-mode-font-lock-keywords-1
215 '(("\\`%!PS.*" (0 font-lock-constant-face t
))
216 ("^%%BoundingBox:[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]*$"
217 (0 font-lock-constant-face t
))
218 ("[\200-\377]+" (0 font-lock-warning-face prepend nil
)))
219 "Subdued level highlighting for PostScript mode.")
221 ;; Level 2 font-lock:
222 ;; - All from level 1
223 ;; - PostScript operators (keyword face)
224 (defconst ps-mode-font-lock-keywords-2
226 ps-mode-font-lock-keywords-1
229 ;; exclude names prepended by `/'
230 (concat "\\(^\\|[^/\n]\\)" ps-mode-operators
)
231 '(2 font-lock-keyword-face
))))
232 "Medium level highlighting for PostScript mode.")
234 ;; Level 3 font-lock:
235 ;; - All from level 2
236 ;; - Immediately evaluated names: those starting with `//' (type face)
237 ;; - Names that look like they are used for the definition of:
241 ;; * a "global" variable
242 ;; (function name face)
243 ;; - Other names (variable name face)
244 ;; The rules used to determine what names fit in the first category are:
245 ;; - Only names that are at the left margin, and one of these on the same line:
246 ;; * Nothing after the name except possibly one or more `[' or a comment
247 ;; * A `{' or `<<' or `[0-9]+ dict' following the name
248 ;; * A `def' somewhere in the same line
249 ;; Names are fontified before PostScript operators, allowing the use of
250 ;; a more simple (efficient) regexp than the one used in level 2.
251 (defconst ps-mode-font-lock-keywords-3
252 `(,@ps-mode-font-lock-keywords-1
253 ("//\\(?:\\sw\\|\\s_\\)+" . font-lock-type-face
)
255 "^\\(/\\(?:\\sw\\|\\s_\\)+\\)\\_>"
256 "\\([[ \t]*\\(%.*\\)?\r?$" ; Nothing but `[' or comment after the name.
257 "\\|[ \t]*\\({\\|<<\\)" ; `{' or `<<' following the name.
258 "\\|[ \t]+[0-9]+[ \t]+dict\\_>" ; `[0-9]+ dict' following the name.
259 "\\|.*\\_<def\\_>\\)") ; `def' somewhere on the same line.
260 .
(1 font-lock-function-name-face
))
261 ("/\\(?:\\sw\\|\\s_\\)+" . font-lock-variable-name-face
)
262 (,ps-mode-operators . font-lock-keyword-face
))
263 "High level highlighting for PostScript mode.")
265 (defconst ps-mode-font-lock-keywords ps-mode-font-lock-keywords-1
266 "Default expressions to highlight in PostScript mode.")
268 ;; Level 1 font-lock for ps-run-mode
269 ;; - prompt (function name face)
270 (defconst ps-run-font-lock-keywords-1
271 (unless (string= "" ps-run-prompt
)
272 (list (cons (if (= ?^
(string-to-char ps-run-prompt
))
274 (concat "^" ps-run-prompt
))
275 'font-lock-function-name-face
)))
276 "Subdued level highlighting for PostScript run mode.")
278 (defconst ps-run-font-lock-keywords ps-run-font-lock-keywords-1
279 "Default expressions to highlight in PostScript run mode.")
285 (let ((map (make-sparse-keymap)))
286 (define-key map
"\C-c\C-v" 'ps-run-boundingbox
)
287 (define-key map
"\C-c\C-u" 'ps-mode-uncomment-region
)
288 (define-key map
"\C-c\C-t" 'ps-mode-epsf-rich
)
289 (define-key map
"\C-c\C-s" 'ps-run-start
)
290 (define-key map
"\C-c\C-r" 'ps-run-region
)
291 (define-key map
"\C-c\C-q" 'ps-run-quit
)
292 (define-key map
"\C-c\C-p" 'ps-mode-print-buffer
)
293 (define-key map
"\C-c\C-o" 'ps-mode-comment-out-region
)
294 (define-key map
"\C-c\C-k" 'ps-run-kill
)
295 (define-key map
"\C-c\C-j" 'ps-mode-other-newline
)
296 (define-key map
"\C-c\C-l" 'ps-run-clear
)
297 (define-key map
"\C-c\C-b" 'ps-run-buffer
)
298 ;; FIXME: Add `indent' to backward-delete-char-untabify-method instead?
299 (define-key map
"\177" 'ps-mode-backward-delete-char
)
301 "Local keymap to use in PostScript mode.")
303 (defvar ps-mode-syntax-table
304 (let ((st (make-syntax-table)))
306 (modify-syntax-entry ?\%
"< " st
)
307 (modify-syntax-entry ?
\n "> " st
)
308 (modify-syntax-entry ?
\r "> " st
)
309 (modify-syntax-entry ?
\f "> " st
)
310 (modify-syntax-entry ?\
< "(>" st
)
311 (modify-syntax-entry ?\
> ")<" st
)
313 (modify-syntax-entry ?\
! "_ " st
)
314 (modify-syntax-entry ?
\" "_ " st
)
315 (modify-syntax-entry ?\
# "_ " st
)
316 (modify-syntax-entry ?\$
"_ " st
)
317 (modify-syntax-entry ?\
& "_ " st
)
318 (modify-syntax-entry ?
\' "_ " st
)
319 (modify-syntax-entry ?\
* "_ " st
)
320 (modify-syntax-entry ?\
+ "_ " st
)
321 (modify-syntax-entry ?\
, "_ " st
)
322 (modify-syntax-entry ?\-
"_ " st
)
323 (modify-syntax-entry ?\.
"_ " st
)
324 (modify-syntax-entry ?\
: "_ " st
)
325 (modify-syntax-entry ?\
; "_ " st)
326 (modify-syntax-entry ?\
= "_ " st
)
327 (modify-syntax-entry ?
\? "_ " st
)
328 (modify-syntax-entry ?\
@ "_ " st
)
329 (modify-syntax-entry ?
\\ "\\" st
)
330 (modify-syntax-entry ?^
"_ " st
) ; NOT: ?\^
331 (modify-syntax-entry ?\_
"_ " st
)
332 (modify-syntax-entry ?\
` "_ " st
)
333 (modify-syntax-entry ?\|
"_ " st
)
334 (modify-syntax-entry ?\~
"_ " st
)
336 "Syntax table used while in PostScript mode.")
338 (defvar ps-run-mode-map
339 (let ((map (make-sparse-keymap)))
340 (set-keymap-parent map comint-mode-map
)
341 (define-key map
"\C-c\C-q" 'ps-run-quit
)
342 (define-key map
"\C-c\C-k" 'ps-run-kill
)
343 (define-key map
"\C-c\C-e" 'ps-run-goto-error
)
344 (define-key map
[mouse-2
] 'ps-run-mouse-goto-error
)
346 "Local keymap to use in PostScript run mode.")
348 (defvar ps-mode-tmp-file nil
349 "Name of temporary file, set by `ps-run'.")
351 (defvar ps-run-mark nil
352 "Mark to start of region that was sent to PostScript interpreter.")
354 (defvar ps-run-parent nil
355 "Parent window of interactive PostScript.")
360 (defconst ps-mode-menu-main
362 ["EPSF Template, Sparse" ps-mode-epsf-sparse t
]
363 ["EPSF Template, Rich" ps-mode-epsf-rich t
]
367 ["ISOLatin1Extended" ps-mode-latin-extended t
]
368 ["center" ps-mode-center t
]
369 ["right" ps-mode-right t
]
370 ["Heapsort" ps-mode-heapsort t
])
372 ["Times-Roman" (insert "/Times-Roman ") t
]
373 ["Times-Bold" (insert "/Times-Bold ") t
]
374 ["Times-Italic" (insert "/Times-Italic ") t
]
375 ["Times-BoldItalic" (insert "/Times-BoldItalic ") t
]
376 ["Helvetica" (insert "/Helvetica ") t
]
377 ["Helvetica-Bold" (insert "/Helvetica-Bold ") t
]
378 ["Helvetica-Oblique" (insert "/Helvetica-Oblique ") t
]
379 ["Helvetica-BoldOblique" (insert "/Helvetica-BoldOblique ") t
]
380 ["Courier" (insert "/Courier ") t
]
381 ["Courier-Bold" (insert "/Courier-Bold ") t
]
382 ["Courier-Oblique" (insert "/Courier-Oblique ") t
]
383 ["Courier-BoldOblique" (insert "/Courier-BoldOblique ") t
]
384 ["Symbol" (insert "/Symbol") t
])
386 ["AvantGarde-Book" (insert "/AvantGarde-Book ") t
]
387 ["AvantGarde-Demi" (insert "/AvantGarde-Demi ") t
]
388 ["AvantGarde-BookOblique" (insert "/AvantGarde-BookOblique ") t
]
389 ["AvantGarde-DemiOblique" (insert "/AvantGarde-DemiOblique ") t
]
390 ["Bookman-Light" (insert "/Bookman-Light ") t
]
391 ["Bookman-Demi" (insert "/Bookman-Demi ") t
]
392 ["Bookman-LightItalic" (insert "/Bookman-LightItalic ") t
]
393 ["Bookman-DemiItalic" (insert "/Bookman-DemiItalic ") t
]
394 ["Helvetica-Narrow" (insert "/Helvetica-Narrow ") t
]
395 ["Helvetica-Narrow-Bold" (insert "/Helvetica-Narrow-Bold ") t
]
396 ["Helvetica-Narrow-Oblique" (insert "/Helvetica-Narrow-Oblique ") t
]
397 ["Helvetica-Narrow-BoldOblique" (insert "/Helvetica-Narrow-BoldOblique ") t
]
398 ["NewCenturySchlbk-Roman" (insert "/NewCenturySchlbk-Roman ") t
]
399 ["NewCenturySchlbk-Bold" (insert "/NewCenturySchlbk-Bold ") t
]
400 ["NewCenturySchlbk-Italic" (insert "/NewCenturySchlbk-Italic ") t
]
401 ["NewCenturySchlbk-BoldItalic" (insert "/NewCenturySchlbk-BoldItalic ") t
]
402 ["Palatino-Roman" (insert "/Palatino-Roman ") t
]
403 ["Palatino-Bold" (insert "/Palatino-Bold ") t
]
404 ["Palatino-Italic" (insert "/Palatino-Italic ") t
]
405 ["Palatino-BoldItalic" (insert "/Palatino-BoldItalic ") t
]
406 ["ZapfChancery-MediumItalic" (insert "/ZapfChancery-MediumItalic ") t
]
407 ["ZapfDingbats" (insert "/ZapfDingbats ") t
])
409 ["Comment Out Region" ps-mode-comment-out-region
(mark t
)]
410 ["Uncomment Region" ps-mode-uncomment-region
(mark t
)]
412 ["8-bit to Octal Buffer" ps-mode-octal-buffer t
]
413 ["8-bit to Octal Region" ps-mode-octal-region
(mark t
)]
418 ["Quit PostScript" ps-run-quit
(process-status "ps-run")]
419 ["Kill PostScript" ps-run-kill
(process-status "ps-run")]
420 ["Send Buffer to Interpreter"
422 (process-status "ps-run")]
423 ["Send Region to Interpreter"
425 (and (mark t
) (process-status "ps-run"))]
426 ["Send Newline to Interpreter"
427 ps-mode-other-newline
428 (process-status "ps-run")]
431 (process-status "ps-run")]
432 ["Clear/Reset PostScript Graphics"
434 (process-status "ps-run")]
436 ["Print Buffer as PostScript"
439 ["Print Region as PostScript"
443 ["Customize for PostScript"
444 (customize-group "PostScript")
448 ps-mode-submit-bug-report
451 (easy-menu-define ps-mode-main ps-mode-map
"PostScript" ps-mode-menu-main
)
455 (declare-function doc-view-minor-mode
"doc-view")
459 (defun ps-mode-smie-rules (kind token
)
460 (pcase (cons kind token
)
461 (`(:after .
"<") (when (smie-rule-next-p "<") 0))
462 (`(:elem . basic
) ps-mode-tab
)
463 (`(:close-all .
">") t
)
464 (`(:list-intro .
,_
) t
)))
467 (define-derived-mode ps-mode prog-mode
"PostScript"
468 "Major mode for editing PostScript with GNU Emacs.
470 Entry to this mode calls `ps-mode-hook'.
472 The following variables hold user options, and can
473 be set through the `customize' command:
477 `ps-mode-print-function'
479 `ps-run-font-lock-keywords-2'
483 `ps-run-error-line-numbers'
486 Type \\[describe-variable] for documentation on these options.
492 When starting an interactive PostScript process with \\[ps-run-start],
493 a second window will be displayed, and `ps-run-mode-hook' will be called.
494 The keymap for this second window is:
499 When Ghostscript encounters an error it displays an error message
500 with a file position. Clicking mouse-2 on this number will bring
501 point to the corresponding spot in the PostScript window, if input
502 to the interpreter was sent from that window.
503 Typing \\<ps-run-mode-map>\\[ps-run-goto-error] when the cursor is at the number has the same effect."
504 (setq-local syntax-propertize-function
#'ps-mode-syntax-propertize
)
505 (set (make-local-variable 'font-lock-defaults
)
506 '((ps-mode-font-lock-keywords
507 ps-mode-font-lock-keywords-1
508 ps-mode-font-lock-keywords-2
509 ps-mode-font-lock-keywords-3
)
511 (smie-setup nil
#'ps-mode-smie-rules
)
512 (setq-local electric-indent-chars
513 (append '(?
> ?\
] ?\
}) electric-indent-chars
))
514 (set (make-local-variable 'comment-start
) "%")
515 ;; NOTE: `\' has a special meaning in strings only
516 (set (make-local-variable 'comment-start-skip
) "%+[ \t]*")
517 ;; enable doc-view-minor-mode => C-c C-c starts viewing the current ps file
518 ;; with doc-view-mode.
519 (doc-view-minor-mode 1))
521 (defun ps-mode-show-version ()
522 "Show current version of PostScript mode."
524 (message " *** PostScript Mode (ps-mode) Version %s *** " ps-mode-version
))
527 (defvar reporter-prompt-for-summary-p
)
528 (defvar reporter-dont-compact-list
)
530 (defun ps-mode-submit-bug-report ()
531 "Submit via mail a bug report on PostScript mode."
533 (when (y-or-n-p "Submit bug report on PostScript mode? ")
534 (let ((reporter-prompt-for-summary-p nil
)
535 (reporter-dont-compact-list '(ps-mode-print-function
536 ps-run-font-lock-keywords-2
)))
537 (reporter-submit-bug-report
538 ps-mode-maintainer-address
539 (format "ps-mode.el %s [%s]" ps-mode-version system-type
)
542 ps-mode-print-function
544 ps-run-font-lock-keywords-2
548 ps-run-error-line-numbers
552 ;; Helper functions for font-lock.
554 (defconst ps-mode--string-syntax-table
555 (let ((st (make-syntax-table ps-mode-syntax-table
)))
556 (modify-syntax-entry ?%
"." st
)
557 (modify-syntax-entry ?
< "." st
)
558 (modify-syntax-entry ?
> "." st
)
559 (modify-syntax-entry ?\
{ "." st
)
560 (modify-syntax-entry ?\
} "." st
)
561 (modify-syntax-entry ?\
[ "." st
)
562 (modify-syntax-entry ?\
] "." st
)
565 (defun ps-mode--syntax-propertize-special (end)
566 (let ((ppss (syntax-ppss))
569 ((not (nth 3 ppss
))) ;Not in (...), <~..base85..~>, or <..hex..>.
570 ((eq ?\
( (setq char
(char-after (nth 8 ppss
))))
572 (narrow-to-region (point-min) end
)
573 (goto-char (nth 8 ppss
))
575 (with-syntax-table ps-mode--string-syntax-table
576 (let ((parse-sexp-lookup-properties nil
))
578 (put-text-property (1- (point)) (point)
579 'syntax-table
(string-to-syntax "|")))
580 (scan-error (goto-char end
)))))
582 (when (re-search-forward (if (eq ?~
(char-after (1+ (nth 8 ppss
))))
585 (put-text-property (1- (point)) (point)
586 'syntax-table
(string-to-syntax "|")))))))
588 (defun ps-mode-syntax-propertize (start end
)
590 (ps-mode--syntax-propertize-special end
)
592 (syntax-propertize-rules
593 ("\\(<\\)\\(?:~\\|[ \n\t]*[[:xdigit:]]\\)\\|\\(?1:(\\)"
594 (1 (unless (or (eq (char-after (match-beginning 0))
595 (char-before (match-beginning 0))) ;Avoid "<<".
596 (nth 8 (save-excursion (syntax-ppss (match-beginning 1)))))
597 (put-text-property (match-beginning 1) (match-end 1)
598 'syntax-table
(string-to-syntax "|"))
599 (ps-mode--syntax-propertize-special end
)
606 (defun ps-mode-target-column ()
607 "To what column should text on current line be indented?
609 Indentation is increased if the last token on the current line
610 defines the beginning of a group. These tokens are: { [ <<"
613 (if (looking-at "[ \t]*\\(}\\|\\]\\|>>\\)")
616 (goto-char (match-end 0))
619 (if (looking-at "[ \t]+")
620 (goto-char (match-end 0)))
624 (message "%s" (error-message-string err
))
627 (if (not (re-search-backward "[^ \t\n\r\f][ \t\n\r\f]*\\=" nil t
))
629 (goto-char (match-beginning 0))
631 (if (looking-at "[ \t]+")
632 (goto-char (match-end 0)))
633 (setq target
(current-column))
635 (if (re-search-backward "\\({\\|\\[\\|<<\\)[ \t]*\\(%[^\n]*\\)?\\=" nil t
)
636 (setq target
(+ target ps-mode-tab
)))
639 (defun ps-mode-backward-delete-char ()
640 "Delete backward indentation, or delete backward character."
642 (let ((column (current-column))
644 (if (or (not electric-indent-mode
)
646 (not (re-search-backward "^[ \t]+\\=" nil t
)))
647 (call-interactively 'delete-backward-char
)
648 (setq target
(ps-mode-target-column))
649 (while (> column target
)
650 (setq target
(+ target ps-mode-tab
)))
651 (while (>= target column
)
652 (setq target
(- target ps-mode-tab
)))
655 (indent-line-to target
))))
657 (defun ps-mode-other-newline ()
658 "Perform newline in `*ps-run*' buffer."
660 (ps-run-send-string ""))
665 (defun ps-mode-print-buffer ()
666 "Print buffer as PostScript."
668 (funcall ps-mode-print-function
))
670 (defun ps-mode-print-region (begin end
)
671 "Print region as PostScript, adding minimal header and footer lines:
677 (let ((buf (current-buffer)))
680 (insert-buffer-substring buf begin end
)
681 (insert "\nshowpage\n")
682 (funcall ps-mode-print-function
))))
685 ;; Comment Out / Uncomment.
687 (defun ps-mode-comment-out-region (begin end
)
688 "Comment out region."
690 (let ((endm (make-marker)))
691 (set-marker endm end
)
694 (if (= (current-column) 0)
696 (while (and (= (forward-line) 0)
697 (< (point) (marker-position endm
)))
699 (set-marker endm nil
)))
701 (defun ps-mode-uncomment-region (begin end
)
704 Only one `%' is removed, and it has to be in the first column."
706 (let ((endm (make-marker)))
707 (set-marker endm end
)
710 (if (looking-at "^%")
712 (while (and (= (forward-line) 0)
713 (< (point) (marker-position endm
)))
716 (set-marker endm nil
)))
719 ;; Convert 8-bit to octal codes.
721 (defun ps-mode-octal-buffer ()
722 "Change 8-bit characters to octal codes in buffer."
724 (ps-mode-octal-region (point-min) (point-max)))
726 (defun ps-mode-octal-region (begin end
)
727 "Change 8-bit characters to octal codes in region."
732 (message "Buffer is read only"))
735 (setq endm
(make-marker))
736 (set-marker endm end
)
739 (while (re-search-forward "[\200-\377]" (marker-position endm
) t
)
742 (insert (format "\\%03o" (string-to-char (string-make-unibyte (buffer-substring (point) (1+ (point)))))))
744 (message "%d change%s made" i
(if (= i
1) "" "s"))
745 (set-marker endm nil
)))))
750 (defun ps-mode-center ()
751 "Insert function /center."
762 (defun ps-mode-right ()
763 "Insert function /right."
775 "Insert function /RE."
778 % `new-font-name' `encoding-vector' `old-font-name' RE -
781 dup maxlength dict begin {
782 1 index /FID ne { def } { pop pop } ifelse
785 dup /FontName exch def
786 currentdict end definefont pop
790 (defun ps-mode-latin-extended ()
791 "Insert array /ISOLatin1Extended.
793 This encoding vector contains all the entries from ISOLatin1Encoding
794 plus the usually uncoded characters inserted on positions 1 through 28."
797 % ISOLatin1Encoding, extended with remaining uncoded glyphs
799 /.notdef /Lslash /lslash /OE /oe /Scaron /scaron /Zcaron /zcaron
800 /Ydieresis /trademark /bullet /dagger /daggerdbl /ellipsis /emdash
801 /endash /fi /fl /florin /fraction /guilsinglleft /guilsinglright
802 /perthousand /quotedblbase /quotedblleft /quotedblright
803 /quotesinglbase /quotesingle /.notdef /.notdef /.notdef /space
804 /exclam /quotedbl /numbersign /dollar /percent /ampersand
805 /quoteright /parenleft /parenright /asterisk /plus /comma /minus
806 /period /slash /zero /one /two /three /four /five /six /seven /eight
807 /nine /colon /semicolon /less /equal /greater /question /at /A /B /C
808 /D /E /F /G /H /I /J /K /L /M /N /O /P /Q /R /S /T /U /V /W /X /Y /Z
809 /bracketleft /backslash /bracketright /asciicircum /underscore
810 /quoteleft /a /b /c /d /e /f /g /h /i /j /k /l /m /n /o /p /q /r /s
811 /t /u /v /w /x /y /z /braceleft /bar /braceright /asciitilde
812 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
813 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
814 /.notdef /.notdef /.notdef /dotlessi /grave /acute /circumflex
815 /tilde /macron /breve /dotaccent /dieresis /.notdef /ring /cedilla
816 /.notdef /hungarumlaut /ogonek /caron /space /exclamdown /cent
817 /sterling /currency /yen /brokenbar /section /dieresis /copyright
818 /ordfeminine /guillemotleft /logicalnot /hyphen /registered /macron
819 /degree /plusminus /twosuperior /threesuperior /acute /mu /paragraph
820 /periodcentered /cedilla /onesuperior /ordmasculine /guillemotright
821 /onequarter /onehalf /threequarters /questiondown /Agrave /Aacute
822 /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla /Egrave /Eacute
823 /Ecircumflex /Edieresis /Igrave /Iacute /Icircumflex /Idieresis /Eth
824 /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply
825 /Oslash /Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn
826 /germandbls /agrave /aacute /acircumflex /atilde /adieresis /aring
827 /ae /ccedilla /egrave /eacute /ecircumflex /edieresis /igrave
828 /iacute /icircumflex /idieresis /eth /ntilde /ograve /oacute
829 /ocircumflex /otilde /odieresis /divide /oslash /ugrave /uacute
830 /ucircumflex /udieresis /yacute /thorn /ydieresis
834 (defun ps-mode-heapsort ()
835 "Insert function /Heapsort."
838 % `array-element' Heapsort-cvi-or-cvr-or-cvs `number-or-string'
839 /Heapsort-cvi-or-cvr-or-cvs {
842 % `array' Heapsort `sorted-array'
844 dup length /hsR exch def
845 /hsL hsR 2 idiv 1 add def
852 dup dup dup 0 get exch dup hsR get
856 dup hsL 1 sub get /hsT exch def
861 hsJ hsR gt { exit } if
863 dup dup hsJ 1 sub get Heapsort-cvi-or-cvr-or-cvs
864 exch hsJ get Heapsort-cvi-or-cvr-or-cvs
865 lt { /hsJ hsJ 1 add def } if
867 dup hsJ 1 sub get Heapsort-cvi-or-cvr-or-cvs
868 hsT Heapsort-cvi-or-cvr-or-cvs
870 dup dup hsS 1 sub exch hsJ 1 sub get put
872 dup hsS 1 sub hsT put
878 ;; EPSF document lay-out.
880 (defun ps-mode-epsf-sparse ()
881 "Insert sparse EPSF template."
883 (goto-char (point-max))
884 (unless (re-search-backward "%%EOF[ \t\n]*\\'" nil t
)
885 (goto-char (point-max))
886 (insert "\n%%EOF\n"))
887 (goto-char (point-max))
888 (unless (re-search-backward "\\bshowpage[ \t\n]+%%EOF[ \t\n]*\\'" nil t
)
889 (re-search-backward "%%EOF")
890 (insert "showpage\n"))
891 (goto-char (point-max))
892 (unless (re-search-backward "\\bend[ \t\n]+\\bshowpage[ \t\n]+%%EOF[ \t\n]*\\'" nil t
)
893 (re-search-backward "showpage")
895 (goto-char (point-min))
896 (insert "%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 ")
897 (insert (format "%d %d\n\n"
898 (car ps-mode-paper-size
)
899 (car (cdr ps-mode-paper-size
))))
900 (insert "64 dict begin\n\n"))
902 (defun ps-mode-epsf-rich ()
903 "Insert rich EPSF template."
905 (ps-mode-epsf-sparse)
907 (when buffer-file-name
908 (insert "%%Title: " (file-name-nondirectory buffer-file-name
) "\n"))
909 (insert "%%Creator: " (user-full-name) "\n")
910 (insert "%%CreationDate: " (current-time-string) "\n")
911 (insert "%%EndComments\n")
915 ;; Interactive PostScript interpreter.
917 (define-derived-mode ps-run-mode comint-mode
"Interactive PS"
918 "Major mode in interactive PostScript window.
919 This mode is invoked from `ps-mode' and should not be called directly."
920 (set (make-local-variable 'font-lock-defaults
)
921 '((ps-run-font-lock-keywords
922 ps-run-font-lock-keywords-1
923 ps-run-font-lock-keywords-2
)
925 (setq mode-line-process
'(":%s")))
927 (defun ps-run-running ()
928 "Error if not in `ps-mode' or not running PostScript."
929 (unless (derived-mode-p 'ps-mode
)
930 (error "This function can only be called from PostScript mode"))
931 (unless (equal (process-status "ps-run") 'run
)
932 (error "No PostScript process running")))
934 (defun ps-run-start ()
935 "Start interactive PostScript."
937 (let ((command (or (and window-system ps-run-x
) ps-run-dumb
))
939 (process-connection-type nil
)
940 (oldwin (selected-window)))
942 (error "No command specified to run interactive PostScript"))
943 (unless (and ps-run-mark
(markerp ps-run-mark
))
944 (setq ps-run-mark
(make-marker)))
946 (setq init-file
(ps-run-make-tmp-filename))
947 (write-region (concat ps-run-init
"\n") 0 init-file
)
948 (setq init-file
(list init-file
)))
949 (pop-to-buffer "*ps-run*")
951 (when (process-status "ps-run")
952 (delete-process "ps-run"))
954 (setq command
(append command init-file
))
955 (insert (mapconcat 'identity command
" ") "\n")
956 (apply 'make-comint
"ps-run" (car command
) nil
(cdr command
))
957 (with-current-buffer "*ps-run*"
958 (use-local-map ps-run-mode-map
)
959 (setq comint-prompt-regexp ps-run-prompt
))
960 (select-window oldwin
)))
962 (defun ps-run-quit ()
963 "Quit interactive PostScript."
965 (ps-run-send-string "quit")
968 (defun ps-run-kill ()
969 "Kill interactive PostScript."
971 (delete-process "ps-run")
974 (defun ps-run-clear ()
975 "Clear/reset PostScript graphics."
977 (ps-run-send-string "showpage")
979 (ps-run-send-string ""))
981 (defun ps-run-buffer ()
982 "Send buffer to PostScript interpreter."
984 (ps-run-region (point-min) (point-max)))
986 (defun ps-run-region (begin end
)
987 "Send region to PostScript interpreter."
990 (setq ps-run-parent
(buffer-name))
991 (let ((f (ps-run-make-tmp-filename)))
992 (set-marker ps-run-mark begin
)
993 (write-region begin end f
)
994 (ps-run-send-string (format "(%s) run" f
))))
996 (defun ps-run-boundingbox ()
1001 (buf (current-buffer)))
1005 "^%%BoundingBox:[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)")
1006 (setq x1
(match-string 1)
1009 y2
(match-string 4)))
1010 (unless (< (string-to-number x1
) (string-to-number x2
))
1011 (error "x1 (%s) should be less than x2 (%s)" x1 x2
))
1012 (unless (< (string-to-number y1
) (string-to-number y2
))
1013 (error "y1 (%s) should be less than y2 (%s)" y1 y2
))
1014 (setq f
(ps-run-make-tmp-filename))
1039 " x1 y1 x2 y1 x2 y2 x1 y2
)
1042 (ps-run-send-string (format "(%s) run" f
))
1045 (defun ps-run-send-string (string)
1046 (let ((oldwin (selected-window)))
1047 (pop-to-buffer "*ps-run*")
1048 (comint-goto-process-mark)
1051 (select-window oldwin
)))
1053 (defun ps-run-make-tmp-filename ()
1054 (unless ps-mode-tmp-file
1055 (setq ps-mode-tmp-file
1056 (let ((temporary-file-directory (or ps-run-tmp-dir
1057 temporary-file-directory
)))
1058 (make-temp-file "ps-run-"))))
1061 ;; Remove temporary file
1062 ;; This shouldn't fail twice, because it is called at kill-emacs
1063 (defun ps-run-cleanup ()
1064 (when ps-mode-tmp-file
1065 (let ((i ps-mode-tmp-file
))
1066 (setq ps-mode-tmp-file nil
)
1067 (when (file-exists-p i
)
1070 (defun ps-run-mouse-goto-error (event)
1071 "Set point at mouse click, then call `ps-run-goto-error'."
1073 (mouse-set-point event
)
1074 (ps-run-goto-error))
1076 (defun ps-run-goto-error ()
1077 "Jump to buffer position read as integer at point.
1078 Use line numbers if `ps-run-error-line-numbers' is not nil"
1081 (unless (looking-at "[0-9]")
1082 (goto-char (max 1 (1- (point)))))
1083 (when (looking-at "[0-9]")
1085 (forward-word-strictly -
1)
1086 (when (looking-at "[0-9]+")
1091 (buffer-substring (match-beginning 0) (match-end 0))))
1093 (pop-to-buffer ps-run-parent
)
1094 (if ps-run-error-line-numbers
1096 (goto-char (marker-position ps-run-mark
))
1097 (forward-line (1- i
))
1099 (goto-char (+ i
(marker-position ps-run-mark
)))))))))
1103 (add-hook 'kill-emacs-hook
'ps-run-cleanup
)
1107 ;;; ps-mode.el ends here