ps-mode.el 38 KB

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