* progmodes/ps-mode.el (ps-run-tmp-dir): Doc fix.
[bpt/emacs.git] / lisp / progmodes / ps-mode.el
CommitLineData
e8af40ee 1;;; ps-mode.el --- PostScript mode for GNU Emacs
99485bca 2
ae940284 3;; Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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
b1fc2b50 21;; GNU Emacs is free software: you can redistribute it and/or modify
99485bca 22;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
23;; the Free Software Foundation, either version 3 of the License, or
24;; (at your option) any later version.
99485bca
GM
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
b1fc2b50 32;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
99485bca
GM
33
34;;; Commentary:
35
36\f
37;;; Code:
38
e7c2398a
EZ
39(defconst ps-mode-version "1.1h, 16 Jun 2005")
40(defconst ps-mode-maintainer-address "Peter Kleiweg <p.c.j.kleiweg@rug.nl>")
c22d928f 41
99485bca
GM
42(require 'easymenu)
43
44;; Define core `PostScript' group.
45(defgroup PostScript nil
46 "PostScript mode for Emacs."
47 :group 'languages)
48
49(defgroup PostScript-edit nil
50 "PostScript editing."
8ec3bce0 51 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
99485bca
GM
52 :prefix "ps-mode-"
53 :group 'PostScript)
54
55(defgroup PostScript-interaction nil
56 "PostScript interaction."
57 :prefix "ps-run-"
58 :group 'PostScript)
59
60;; User variables.
61
62(defcustom ps-mode-auto-indent t
63 "*Should we use autoindent?"
64 :group 'PostScript-edit
65 :type 'boolean)
66
67(defcustom ps-mode-tab 4
68 "*Number of spaces to use when indenting."
69 :group 'PostScript-edit
70 :type 'integer)
71
72(defcustom ps-mode-paper-size '(595 842)
73 "*Default paper size.
74
75When inserting an EPSF template these values are used
76to set the boundingbox to include the whole page.
77When the figure is finished these values should be replaced."
78 :group 'PostScript-edit
79 :type '(choice
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))))
113
627a4e30
GM
114(defcustom ps-mode-print-function
115 (lambda ()
c22d928f 116 (let ((lpr-switches nil)
f3041af1 117 (lpr-command (if (memq system-type '(usg-unix-v hpux irix))
155fc930 118 "lp" "lpr")))
c22d928f 119 (lpr-buffer)))
99485bca
GM
120 "*Lisp function to print current buffer as PostScript."
121 :group 'PostScript-edit
122 :type 'function)
123
124(defcustom ps-run-prompt "\\(GS\\(<[0-9]+\\)?>\\)+"
125 "*Regexp to match prompt in interactive PostScript."
126 :group 'PostScript-interaction
127 :type 'regexp)
128
c22d928f
GM
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))
132 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))
627a4e30 139 ("^\\(Current file position is\\) \\([0-9]+\\)"
c22d928f
GM
140 (1 font-lock-comment-face nil nil)
141 (2 font-lock-warning-face nil nil))))
99485bca
GM
142 "*Medium level highlighting of messages from the PostScript interpreter.
143
144See 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"
150 :inline regexp
151 (list :tag "Highlighter"
152 (integer :tag "Subexp")
153 face
154 (boolean :tag "Override")
155 (boolean :tag "Laxmatch" :value t))))))
156
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))
161
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))
166
167(defcustom ps-run-init nil
168 "*String of commands to send to PostScript to start interactive.
169
c22d928f 170Example: \"executive\"
99485bca 171
627a4e30 172You won't need to set this option for Ghostscript."
99485bca
GM
173 :group 'PostScript-interaction
174 :type '(choice (const nil) string))
175
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)))
181
182(defcustom ps-run-tmp-dir nil
183 "*Name of directory to place temporary file.
184
185If nil, the following are tried in turn, until success:
186 1. \"$TEMP\"
187 2. \"$TMP\"
188 3. \"$HOME/tmp\"
627a4e30 189 4. \"/tmp\""
99485bca
GM
190 :group 'PostScript-interaction
191 :type '(choice (const nil) directory))
192
193\f
194;; Constants used for font-lock.
195
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"
201 "forall"
202 "dict" "begin" "end" "def"
203 "true" "false"
204 "exec" "if" "ifelse" "for" "repeat" "loop" "exit"
205 "stop" "stopped" "countexecstack" "execstack"
206 "quit" "start"
207 "save" "restore"
208 "bind" "null"
209 "gsave" "grestore" "grestoreall"
210 "showpage")))
211 (concat "\\<" (regexp-opt ops t) "\\>"))
627a4e30 212 "Regexp of PostScript operators that will be fontified.")
99485bca
GM
213
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)
627a4e30 222 ("^%%BoundingBox:[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]+-?[0-9]+[ \t]*$"
99485bca
GM
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.")
230
231;; Level 2 font-lock:
232;; - All from level 1
233;; - PostScript operators (keyword face)
234(defconst ps-mode-font-lock-keywords-2
235 (append
236 ps-mode-font-lock-keywords-1
237 (list
238 (cons
239 ;; exclude names prepended by `/'
240 (concat "\\(^\\|[^/\n]\\)" ps-mode-operators)
241 '(2 font-lock-keyword-face))))
242 "Medium level highlighting for PostScript mode.")
243
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:
248;; * a function
249;; * an array
250;; * a dictionary
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
262 (append
263 ps-mode-font-lock-keywords-1
264 (list
265 '("//\\w+" . font-lock-type-face)
6114f9e5
RS
266 `(,(concat
267 "^\\(/\\w+\\)\\>"
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.
99485bca
GM
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.")
276
277(defconst ps-mode-font-lock-keywords ps-mode-font-lock-keywords-1
278 "Default expressions to highlight in PostScript mode.")
279
280;; Level 1 font-lock for ps-run-mode
281;; - prompt (function name face)
282(defconst ps-run-font-lock-keywords-1
c22d928f
GM
283 (unless (string= "" ps-run-prompt)
284 (list (cons (if (= ?^ (string-to-char ps-run-prompt))
285 ps-run-prompt
286 (concat "^" ps-run-prompt))
287 'font-lock-function-name-face)))
99485bca
GM
288 "Subdued level highlighting for PostScript run mode.")
289
290(defconst ps-run-font-lock-keywords ps-run-font-lock-keywords-1
291 "Default expressions to highlight in PostScript run mode.")
292
293\f
294;; Variables.
295
296(defvar ps-mode-map nil
297 "Local keymap to use in PostScript mode.")
298
299(defvar ps-mode-syntax-table nil
300 "Syntax table used while in PostScript mode.")
301
302(defvar ps-run-mode-map nil
303 "Local keymap to use in PostScript run mode.")
304
305(defvar ps-mode-tmp-file nil
306 "Name of temporary file, set by `ps-run'.")
307
308(defvar ps-run-mark nil
309 "Mark to start of region that was sent to PostScript interpreter.")
310
311(defvar ps-run-parent nil
312 "Parent window of interactive PostScript.")
313
314\f
315;; Menu
316
317(defconst ps-mode-menu-main
318 '("PostScript"
319 ["EPSF Template, Sparse" ps-mode-epsf-sparse t]
320 ["EPSF Template, Rich" ps-mode-epsf-rich t]
321 "---"
322 ("Cookbook"
323 ["RE" ps-mode-RE 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])
328 ("Fonts (1)"
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 ])
342 ("Fonts (2)"
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])
365 "---"
366 ["Comment Out Region" ps-mode-comment-out-region (mark t)]
367 ["Uncomment Region" ps-mode-uncomment-region (mark t)]
368 "---"
369 ["8-bit to Octal Buffer" ps-mode-octal-buffer t]
370 ["8-bit to Octal Region" ps-mode-octal-region (mark t)]
371 "---"
627a4e30
GM
372 ["Auto Indent" (setq ps-mode-auto-indent (not ps-mode-auto-indent))
373 :style toggle :selected ps-mode-auto-indent]
99485bca
GM
374 "---"
375 ["Start PostScript"
376 ps-run-start
377 t]
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"
381 ps-run-buffer
382 (process-status "ps-run")]
383 ["Send Region to Interpreter"
384 ps-run-region
385 (and (mark t) (process-status "ps-run"))]
386 ["Send Newline to Interpreter"
387 ps-mode-other-newline
388 (process-status "ps-run")]
389 ["View BoundingBox"
390 ps-run-boundingbox
391 (process-status "ps-run")]
392 ["Clear/Reset PostScript Graphics"
393 ps-run-clear
394 (process-status "ps-run")]
395 "---"
396 ["Print Buffer as PostScript"
397 ps-mode-print-buffer
398 t]
399 ["Print Region as PostScript"
400 ps-mode-print-region
401 (mark t)]
402 "---"
403 ["Customize for PostScript"
404 (customize-group "PostScript")
aebf9ad1
GM
405 t]
406 "---"
407 ["Submit Bug Report"
408 ps-mode-submit-bug-report
99485bca
GM
409 t]))
410
411\f
412;; Mode maps for PostScript edit mode and PostScript interaction mode.
413
414(unless ps-mode-map
415 (setq ps-mode-map (make-sparse-keymap))
99485bca 416 (define-key ps-mode-map "\C-c\C-v" 'ps-run-boundingbox)
c22d928f
GM
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)
5ca7d652 426 (define-key ps-mode-map "\C-c\C-l" 'ps-run-clear)
c22d928f
GM
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)
99485bca
GM
435 (easy-menu-define ps-mode-main ps-mode-map "PostScript" ps-mode-menu-main))
436
437(unless ps-run-mode-map
438 (setq ps-run-mode-map (make-sparse-keymap))
99485bca
GM
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)
c22d928f
GM
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))
99485bca
GM
445
446\f
447;; Syntax table.
448
449(unless ps-mode-syntax-table
450 (setq ps-mode-syntax-table (make-syntax-table))
627a4e30 451
99485bca
GM
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)
627a4e30 458
99485bca
GM
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)
627a4e30 481
99485bca
GM
482 (let ((i 128))
483 (while (< i 256)
484 (modify-syntax-entry i "w " ps-mode-syntax-table)
485 (setq i (1+ i)))))
486
487\f
6d00e226 488
5cec3056 489(declare-function doc-view-minor-mode "doc-view")
6d00e226 490
99485bca
GM
491;; PostScript mode.
492
7c3d5ad9 493;;;###autoload
627a4e30 494(define-derived-mode ps-mode fundamental-mode "PostScript"
99485bca
GM
495 "Major mode for editing PostScript with GNU Emacs.
496
497Entry to this mode calls `ps-mode-hook'.
498
499The following variables hold user options, and can
500be set through the `customize' command:
501
627a4e30
GM
502 `ps-mode-auto-indent'
503 `ps-mode-tab'
504 `ps-mode-paper-size'
505 `ps-mode-print-function'
506 `ps-run-prompt'
507 `ps-run-font-lock-keywords-2'
508 `ps-run-x'
509 `ps-run-dumb'
510 `ps-run-init'
511 `ps-run-error-line-numbers'
512 `ps-run-tmp-dir'
99485bca
GM
513
514Type \\[describe-variable] for documentation on these options.
515
516
517\\{ps-mode-map}
518
519
520When starting an interactive PostScript process with \\[ps-run-start],
521a second window will be displayed, and `ps-run-mode-hook' will be called.
522The keymap for this second window is:
523
524\\{ps-run-mode-map}
525
526
527When Ghostscript encounters an error it displays an error message
528with a file position. Clicking mouse-2 on this number will bring
529point to the corresponding spot in the PostScript window, if input
530to the interpreter was sent from that window.
627a4e30
GM
531Typing \\<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)
6114f9e5
RS
537 t))
538 (set (make-local-variable 'comment-start) "%")
539 ;; NOTE: `\' has a special meaning in strings only
5b549c31
GM
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))
99485bca 544
c22d928f 545(defun ps-mode-show-version ()
627a4e30 546 "Show current version of PostScript mode."
c22d928f
GM
547 (interactive)
548 (message " *** PostScript Mode (ps-mode) Version %s *** " ps-mode-version))
549
aebf9ad1 550(defun ps-mode-submit-bug-report ()
627a4e30 551 "Submit via mail a bug report on PostScript mode."
aebf9ad1 552 (interactive)
627a4e30 553 (when (y-or-n-p "Submit bug report on PostScript mode? ")
8f011fdc
GM
554 (let ((reporter-prompt-for-summary-p nil)
555 (reporter-dont-compact-list '(ps-mode-print-function
556 ps-run-font-lock-keywords-2)))
aebf9ad1
GM
557 (reporter-submit-bug-report
558 ps-mode-maintainer-address
8f011fdc
GM
559 (format "ps-mode.el %s [%s]" ps-mode-version system-type)
560 '(ps-mode-auto-indent
561 ps-mode-tab
562 ps-mode-paper-size
563 ps-mode-print-function
564 ps-run-prompt
565 ps-run-font-lock-keywords-2
566 ps-run-x
567 ps-run-dumb
568 ps-run-init
569 ps-run-error-line-numbers
570 ps-run-tmp-dir)))))
aebf9ad1 571
99485bca
GM
572\f
573;; Helper functions for font-lock.
574
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
580;; return new point
581;; If false: return nil
582(defun ps-mode-looking-at-nested (limit)
583 (let ((first (point))
584 (level 1)
585 pos)
586 ;; Move past opening bracket.
587 (forward-char 1)
588 (setq pos (point))
589 (while (and (> level 0) (< pos limit))
590 ;; Search next bracket, stepping over escaped brackets.
591 (if (not (looking-at "\\([^()\\\n]\\|\\\\.\\)*\\([()]\\)"))
592 (setq level -1)
6114f9e5
RS
593 (setq level (+ level (if (string= "(" (match-string 2)) 1 -1)))
594 (goto-char (setq pos (match-end 0)))))
99485bca
GM
595 (if (not (= level 0))
596 nil
597 ;; Found string with nested brackets, now set match data nr 2.
6114f9e5
RS
598 (set-match-data (list first pos nil nil first pos))
599 pos)))
99485bca
GM
600
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.
608 nil
627a4e30 609 (let ((end (match-end 0)))
99485bca 610 (goto-char (match-beginning 0))
99485bca
GM
611 (cond ((looking-at "\\(%.*\\)\\|\\((\\([^()\\\n]\\|\\\\.\\)*)\\)")
612 ;; It's a comment or string without nested, unescaped brackets.
613 (goto-char (match-end 0))
614 (point))
615 ((ps-mode-looking-at-nested limit)
616 ;; It's a string with nested brackets.
617 (point))
618 (t
627a4e30 619 ;; Try next match.
99485bca
GM
620 (goto-char end)
621 (ps-mode-match-string-or-comment limit))))))
622
623\f
624;; Key-handlers.
625
626(defun ps-mode-target-column ()
627 "To what column should text on current line be indented?
628
629Identation is increased if the last token on the current line
630defines the beginning of a group. These tokens are: { [ <<"
631 (save-excursion
632 (beginning-of-line)
633 (if (looking-at "[ \t]*\\(}\\|\\]\\|>>\\)")
634 (condition-case err
635 (progn
636 (goto-char (match-end 0))
637 (backward-sexp 1)
638 (beginning-of-line)
639 (if (looking-at "[ \t]+")
640 (goto-char (match-end 0)))
641 (current-column))
642 (error
643 (ding)
29a4e67d 644 (message "%s" (error-message-string err))
99485bca
GM
645 0))
646 (let (target)
647 (if (not (re-search-backward "[^ \t\n\r\f][ \t\n\r\f]*\\=" nil t))
648 0
649 (goto-char (match-beginning 0))
650 (beginning-of-line)
651 (if (looking-at "[ \t]+")
652 (goto-char (match-end 0)))
653 (setq target (current-column))
654 (end-of-line)
655 (if (re-search-backward "\\({\\|\\[\\|<<\\)[ \t]*\\(%[^\n]*\\)?\\=" nil t)
656 (setq target (+ target ps-mode-tab)))
657 target)))))
658
659(defun ps-mode-newline ()
660 "Insert newline with proper indentation."
661 (interactive)
662 (delete-horizontal-space)
663 (insert "\n")
664 (if ps-mode-auto-indent
665 (indent-to (ps-mode-target-column))))
666
667(defun ps-mode-tabkey ()
627a4e30 668 "Indent/reindent current line, or insert tab."
99485bca
GM
669 (interactive)
670 (let ((column (current-column))
671 target)
672 (if (or (not ps-mode-auto-indent)
673 (< ps-mode-tab 1)
674 (not (re-search-backward "^[ \t]*\\=" nil t)))
675 (insert "\t")
676 (setq target (ps-mode-target-column))
677 (while (<= target column)
678 (setq target (+ target ps-mode-tab)))
627a4e30 679 (indent-line-to target))))
99485bca
GM
680
681(defun ps-mode-backward-delete-char ()
627a4e30 682 "Delete backward indentation, or delete backward character."
99485bca
GM
683 (interactive)
684 (let ((column (current-column))
685 target)
686 (if (or (not ps-mode-auto-indent)
687 (< ps-mode-tab 1)
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)))
695 (if (< target 0)
696 (setq target 0))
627a4e30 697 (indent-line-to target))))
99485bca
GM
698
699(defun ps-mode-r-brace ()
700 "Insert `}' and perform balance."
701 (interactive)
702 (insert "}")
703 (ps-mode-r-balance "}"))
704
705(defun ps-mode-r-angle ()
706 "Insert `]' and perform balance."
707 (interactive)
708 (insert "]")
709 (ps-mode-r-balance "]"))
710
711(defun ps-mode-r-gt ()
712 "Insert `>' and perform balance."
713 (interactive)
714 (insert ">")
715 (ps-mode-r-balance ">>"))
716
717(defun ps-mode-r-balance (right)
718 "Adjust indentification if point after RIGHT."
719 (if ps-mode-auto-indent
720 (save-excursion
721 (when (re-search-backward (concat "^[ \t]*" (regexp-quote right) "\\=") nil t)
627a4e30 722 (indent-line-to (ps-mode-target-column)))))
99485bca
GM
723 (blink-matching-open))
724
725(defun ps-mode-other-newline ()
627a4e30 726 "Perform newline in `*ps run*' buffer."
99485bca
GM
727 (interactive)
728 (let ((buf (current-buffer)))
729 (set-buffer "*ps run*")
730 (ps-run-newline)
731 (set-buffer buf)))
732
733\f
734;; Print PostScript.
735
736(defun ps-mode-print-buffer ()
627a4e30 737 "Print buffer as PostScript."
99485bca 738 (interactive)
627a4e30 739 (funcall ps-mode-print-function))
99485bca
GM
740
741(defun ps-mode-print-region (begin end)
742 "Print region as PostScript, adding minimal header and footer lines:
743
744%!PS
745<region>
627a4e30 746showpage"
99485bca 747 (interactive "r")
627a4e30
GM
748 (let ((buf (current-buffer)))
749 (with-temp-buffer
750 (insert "%!PS\n")
751 (insert-buffer-substring buf begin end)
752 (insert "\nshowpage\n")
753 (funcall ps-mode-print-function))))
99485bca
GM
754
755\f
756;; Comment Out / Uncomment.
757
758(defun ps-mode-comment-out-region (begin end)
759 "Comment out region."
760 (interactive "r")
761 (let ((endm (make-marker)))
762 (set-marker endm end)
763 (save-excursion
764 (goto-char begin)
765 (if (= (current-column) 0)
766 (insert "%"))
767 (while (and (= (forward-line) 0)
768 (< (point) (marker-position endm)))
769 (insert "%")))
770 (set-marker endm nil)))
771
772(defun ps-mode-uncomment-region (begin end)
773 "Uncomment region.
774
775Only one `%' is removed, and it has to be in the first column."
776 (interactive "r")
777 (let ((endm (make-marker)))
778 (set-marker endm end)
779 (save-excursion
780 (goto-char begin)
781 (if (looking-at "^%")
782 (delete-char 1))
783 (while (and (= (forward-line) 0)
784 (< (point) (marker-position endm)))
785 (if (looking-at "%")
786 (delete-char 1))))
787 (set-marker endm nil)))
788
789\f
790;; Convert 8-bit to octal codes.
791
792(defun ps-mode-octal-buffer ()
793 "Change 8-bit characters to octal codes in buffer."
794 (interactive)
795 (ps-mode-octal-region (point-min) (point-max)))
796
797(defun ps-mode-octal-region (begin end)
798 "Change 8-bit characters to octal codes in region."
799 (interactive "r")
800 (if buffer-read-only
801 (progn
802 (ding)
803 (message "Buffer is read only"))
804 (save-excursion
805 (let (endm i)
806 (setq endm (make-marker))
807 (set-marker endm end)
808 (goto-char begin)
809 (setq i 0)
810 (while (re-search-forward "[\200-\377]" (marker-position endm) t)
811 (setq i (1+ i))
812 (backward-char)
813 (insert (format "\\%03o" (string-to-char (buffer-substring (point) (1+ (point))))))
814 (delete-char 1))
c22d928f 815 (message "%d change%s made" i (if (= i 1) "" "s"))
99485bca
GM
816 (set-marker endm nil)))))
817
818\f
819;; Cookbook.
820
821(defun ps-mode-center ()
822 "Insert function /center."
823 (interactive)
824 (insert "
825/center {
826 dup stringwidth
827 exch 2 div neg
828 exch 2 div neg
829 rmoveto
830} bind def
831"))
832
833(defun ps-mode-right ()
834 "Insert function /right."
835 (interactive)
836 (insert "
837/right {
838 dup stringwidth
839 exch neg
840 exch neg
841 rmoveto
842} bind def
843"))
844
845(defun ps-mode-RE ()
846 "Insert function /RE."
847 (interactive)
848 (insert "
849% `new-font-name' `encoding-vector' `old-font-name' RE -
850/RE {
851 findfont
852 dup maxlength dict begin {
853 1 index /FID ne { def } { pop pop } ifelse
854 } forall
855 /Encoding exch def
856 dup /FontName exch def
857 currentdict end definefont pop
858} bind def
859"))
860
861(defun ps-mode-latin-extended ()
862 "Insert array /ISOLatin1Extended.
863
864This encoding vector contains all the entries from ISOLatin1Encoding
627a4e30 865plus the usually uncoded characters inserted on positions 1 through 28."
99485bca
GM
866 (interactive)
867 (insert "
868% ISOLatin1Encoding, extended with remaining uncoded glyphs
869/ISOLatin1Extended [
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
902] def
903"))
904
905(defun ps-mode-heapsort ()
906 "Insert function /Heapsort."
907 (interactive)
908 (insert "
909% `array-element' Heapsort-cvi-or-cvr-or-cvs `number-or-string'
910/Heapsort-cvi-or-cvr-or-cvs {
911 % 0 get
912} bind def
913% `array' Heapsort `sorted-array'
914/Heapsort {
915 dup length /hsR exch def
916 /hsL hsR 2 idiv 1 add def
917 {
918 hsR 2 lt { exit } if
919 hsL 1 gt {
920 /hsL hsL 1 sub def
921 } {
922 /hsR hsR 1 sub def
923 dup dup dup 0 get exch dup hsR get
924 0 exch put
925 hsR exch put
926 } ifelse
927 dup hsL 1 sub get /hsT exch def
928 /hsJ hsL def
929 {
930 /hsS hsJ def
931 /hsJ hsJ dup add def
932 hsJ hsR gt { exit } if
933 hsJ hsR lt {
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
937 } if
938 dup hsJ 1 sub get Heapsort-cvi-or-cvr-or-cvs
939 hsT Heapsort-cvi-or-cvr-or-cvs
940 le { exit } if
941 dup dup hsS 1 sub exch hsJ 1 sub get put
942 } loop
943 dup hsS 1 sub hsT put
944 } loop
945} bind def
946"))
947
948\f
949;; EPSF document lay-out.
950
951(defun ps-mode-epsf-sparse ()
952 "Insert sparse EPSF template."
953 (interactive)
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")
965 (insert "\nend\n"))
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"))
972
973(defun ps-mode-epsf-rich ()
974 "Insert rich EPSF template."
975 (interactive)
976 (ps-mode-epsf-sparse)
977 (forward-line -3)
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")
983 (forward-line 3))
984
985\f
986;; Interactive PostScript interpreter.
987
627a4e30 988(define-derived-mode ps-run-mode fundamental-mode "Interactive PS"
99485bca 989 "Major mode in interactive PostScript window.
627a4e30 990This mode is invoked from `ps-mode' and should not be called directly.
99485bca 991
627a4e30
GM
992\\{ps-run-mode-map}"
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)
997 t))
998 (setq mode-line-process '(":%s")))
99485bca
GM
999
1000(defun ps-run-running ()
627a4e30 1001 "Error if not in `ps-mode' or not running PostScript."
99485bca
GM
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")))
1006
1007(defun ps-run-start ()
1008 "Start interactive PostScript."
1009 (interactive)
627a4e30 1010 (let ((command (or (and window-system ps-run-x) ps-run-dumb))
99485bca
GM
1011 (init-file nil)
1012 (process-connection-type nil)
627a4e30 1013 (oldwin (selected-window)))
99485bca
GM
1014 (unless command
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)))
1018 (when ps-run-init
1019 (setq init-file (ps-run-make-tmp-filename))
c22d928f 1020 (write-region (concat ps-run-init "\n") 0 init-file)
99485bca
GM
1021 (setq init-file (list init-file)))
1022 (pop-to-buffer "*ps run*")
1023 (ps-run-mode)
1024 (when (process-status "ps-run")
1025 (delete-process "ps-run"))
1026 (erase-buffer)
627a4e30
GM
1027 (setq command (append command init-file))
1028 (insert (mapconcat 'identity command " ") "\n")
1029 (apply 'start-process "ps-run" "*ps run*" command)
99485bca
GM
1030 (select-window oldwin)))
1031
1032(defun ps-run-quit ()
1033 "Quit interactive PostScript."
1034 (interactive)
1035 (ps-run-send-string "quit" t)
1036 (ps-run-cleanup))
1037
1038(defun ps-run-kill ()
1039 "Kill interactive PostScript."
1040 (interactive)
1041 (delete-process "ps-run")
1042 (ps-run-cleanup))
1043
1044(defun ps-run-clear ()
1045 "Clear/reset PostScript graphics."
1046 (interactive)
1047 (ps-run-send-string "showpage" t)
1048 (sit-for 1)
1049 (ps-run-send-string "" t))
1050
1051(defun ps-run-buffer ()
1052 "Send buffer to PostScript interpreter."
1053 (interactive)
1054 (ps-run-region (point-min) (point-max)))
1055
1056(defun ps-run-region (begin end)
1057 "Send region to PostScript interpreter."
1058 (interactive "r")
1059 (ps-run-running)
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)))
1065
1066(defun ps-run-boundingbox ()
627a4e30 1067 "View BoundingBox."
99485bca
GM
1068 (interactive)
1069 (ps-run-running)
1070 (let (x1 y1 x2 y2 f
1071 (buf (current-buffer)))
1072 (save-excursion
1073 (goto-char 1)
1074 (re-search-forward
1075 "^%%BoundingBox:[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)[ \t]+\\(-?[0-9]+\\)")
1076 (setq x1 (match-string 1)
1077 y1 (match-string 2)
1078 x2 (match-string 3)
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))
1085 (write-region
1086 (format
1087 "gsave
1088 initgraphics
1089 2 setlinewidth
1090 %s %s moveto
1091 %s %s lineto
1092 %s %s lineto
1093 %s %s lineto
1094 closepath
1095 gsave
1096 [ 4 20 ] 0 setdash
1097 1 0 0 setrgbcolor
1098 stroke
1099 grestore
1100 gsave
1101 [ 4 20 ] 8 setdash
1102 0 1 0 setrgbcolor
1103 stroke
1104 grestore
1105 [ 4 20 ] 16 setdash
1106 0 0 1 setrgbcolor
1107 stroke
1108grestore
1109" x1 y1 x2 y1 x2 y2 x1 y2)
1110 0
1111 f)
1112 (ps-run-send-string (format "(%s) run" f) t)
1113 (set-buffer buf)))
1114
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))
1119 (when echo
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)))
1124
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"))
1131 (setq
1132 ps-run-tmp-dir
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
d498f475 1139 (make-temp-file
99485bca
GM
1140 (concat
1141 (if ps-run-tmp-dir
1142 (file-name-as-directory ps-run-tmp-dir)
1143 "")
1144 "ps-run-"))))
1145 ps-mode-tmp-file)
1146
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)
1154 (delete-file i)))))
1155
1156(defun ps-run-mouse-goto-error (event)
627a4e30 1157 "Set point at mouse click, then call `ps-run-goto-error'."
99485bca
GM
1158 (interactive "e")
1159 (mouse-set-point event)
1160 (ps-run-goto-error))
1161
1162(defun ps-run-newline ()
1163 "Process newline in PostScript interpreter window."
1164 (interactive)
1165 (end-of-line)
1166 (insert "\n")
1167 (forward-line -1)
c22d928f 1168 (when (looking-at ps-run-prompt)
99485bca
GM
1169 (goto-char (match-end 0)))
1170 (looking-at ".*")
1171 (goto-char (1+ (match-end 0)))
1172 (ps-run-send-string (buffer-substring (match-beginning 0) (match-end 0))))
1173
1174(defun ps-run-goto-error ()
1175 "Jump to buffer position read as integer at point.
627a4e30 1176Use line numbers if `ps-run-error-line-numbers' is not nil"
99485bca
GM
1177 (interactive)
1178 (let ((p (point)))
1179 (unless (looking-at "[0-9]")
1180 (goto-char (max 1 (1- (point)))))
1181 (when (looking-at "[0-9]")
1182 (forward-char 1)
1183 (forward-word -1)
1184 (when (looking-at "[0-9]+")
1185 (let (i)
1186 (setq
1187 i
027a4b6b 1188 (string-to-number
99485bca
GM
1189 (buffer-substring (match-beginning 0) (match-end 0))))
1190 (goto-char p)
1191 (pop-to-buffer ps-run-parent)
1192 (if ps-run-error-line-numbers
1193 (progn
1194 (goto-char (marker-position ps-run-mark))
1195 (forward-line (1- i))
1196 (end-of-line))
1197 (goto-char (+ i (marker-position ps-run-mark)))))))))
1198
1199\f
1200;;
1201(add-hook 'kill-emacs-hook 'ps-run-cleanup)
1202
1203(provide 'ps-mode)
1204
cbee283d 1205;; arch-tag: dce13d2d-69fb-4ec4-9d5d-6dd29c3f0e6e
99485bca 1206;;; ps-mode.el ends here