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