Klimi's new dotfiles with stow.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

989 rader
42 KiB

5 år sedan
  1. ;;; graphviz-dot-mode.el --- Mode for the dot-language used by graphviz (att).
  2. ;; Copyright (C) 2002 - 2012 Pieter Pareit <pieter.pareit@gmail.com>
  3. ;; This program is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU General Public License as
  5. ;; published by the Free Software Foundation; either version 2 of
  6. ;; the License, or (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be
  8. ;; useful, but WITHOUT ANY WARRANTY; without even the implied
  9. ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  10. ;; PURPOSE. See the GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public
  12. ;; License along with this program; if not, write to the Free
  13. ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  14. ;; MA 02111-1307 USA
  15. ;; Authors: Pieter Pareit <pieter.pareit@gmail.com>
  16. ;; Rubens Ramos <rubensr AT users.sourceforge.net>
  17. ;; Eric Anderson http://www.ece.cmu.edu/~andersoe/
  18. ;; Maintainer: Pieter Pareit <pieter.pareit@gmail.com>
  19. ;; Homepage: http://ppareit.github.com/graphviz-dot-mode/
  20. ;; Created: 28 Oct 2002
  21. ;; Last modified: 25 May 2015
  22. ;; Version: 0.3.10
  23. ;; Package-Version: 20181118.551
  24. ;; Keywords: mode dot dot-language dotlanguage graphviz graphs att
  25. ;;; Commentary:
  26. ;; Use this mode for editing files in the dot-language (www.graphviz.org and
  27. ;; http://www.research.att.com/sw/tools/graphviz/).
  28. ;;
  29. ;; To use graphviz-dot-mode, add
  30. ;; (load-file "PATH_TO_FILE/graphviz-dot-mode.el")
  31. ;; to your ~/.emacs(.el) or ~/.xemacs/init.el
  32. ;;
  33. ;; The graphviz-dot-mode will do font locking, indentation, preview of graphs
  34. ;; and eases compilation/error location. There is support for both GNU Emacs
  35. ;; and XEmacs.
  36. ;;
  37. ;; Font locking is automatic, indentation uses the same commands as
  38. ;; other modes, tab, M-j and C-M-q. Insertion of comments uses the
  39. ;; same commands as other modes, M-; . You can compile a file using
  40. ;; M-x compile or C-c c, after that M-x next-error will also work.
  41. ;; There is support for viewing an generated image with C-c p.
  42. ;;; Todo:
  43. ;; * cleanup the mess of graphviz-dot-compilation-parse-errors.
  44. ;; * electric indentation is fundamentally broken, because
  45. ;; {...} are also used for record nodes. You could argue, I suppose, that
  46. ;; many diagrams don't need those, but it would be worth having a note (and
  47. ;; it makes sense that the default is now for electric indentation to be
  48. ;; off).
  49. ;;; History:
  50. ;; Version 0.3.11 Olli Piepponen
  51. ;; 29/01/2016: * use define-derived-mode for the mode-definition
  52. ;; * add support for a auto-loading live preview work flow
  53. ;; Version 0.3.10 Kevin Ryde
  54. ;; 25/05/2015: * shell-quote-argument for safety
  55. ;; * use read-shell-command whenever available, don't set novaproc
  56. ;; Version 0.3.9 Titus Barik <titus AT barik.net>
  57. ;; 28/08/2012: * compile-command uses -ofile instead of >
  58. ;; Version 0.3.8 new home
  59. ;; 27/06/2012: * put graphviz-dot-mode into git, updated links
  60. ;; Version 0.3.7 Tim Allen
  61. ;; 09/03/2011: * fix spaces in file names when compiling
  62. ;; Version 0.3.6 maintenance
  63. ;; 19/02/2011: * .gv is the new extension (Pander)
  64. ;; * comments can start with # (Pander)
  65. ;; * highlight of new keywords (Pander)
  66. ;; Version 0.3.5 bug (or at least feature I dislike) fix
  67. ;; 11/11/2010: Eric Anderson http://www.ece.cmu.edu/~andersoe/
  68. ;; * Preserve indentation across blank (whitespace-only) lines
  69. ;; Version 0.3.4 bug fixes
  70. ;; 24/02/2005: * fixed a bug in graphviz-dot-preview
  71. ;; Version 0.3.3 bug fixes
  72. ;; 13/02/2005: Reuben Thomas <rrt AT sc3d.org>
  73. ;; * add graphviz-dot-indent-width
  74. ;; Version 0.3.2 bug fixes
  75. ;; 25/03/2004: Rubens Ramos <rubensr AT users.sourceforge.net>
  76. ;; * semi-colons and brackets are added when electric
  77. ;; behaviour is disabled.
  78. ;; * electric characters do not behave electrically inside
  79. ;; comments or strings.
  80. ;; * default for electric-braces is disabled now (makes more
  81. ;; sense I guess).
  82. ;; * using read-from-minibuffer instead of read-shell-command
  83. ;; for emacs.
  84. ;; * Fixed test for easymenu, so that it works on older
  85. ;; versions of XEmacs.
  86. ;; * Fixed indentation error when trying to indent last brace
  87. ;; of an empty graph.
  88. ;; * region-active-p does not exist in emacs (21.2 at least),
  89. ;; so removed from code
  90. ;; * Added uncomment menu option
  91. ;; Version 0.3.1 bug fixes
  92. ;; 03/03/2004: * backward-word needs argument for older emacs
  93. ;; Version 0.3 added features and fixed bugs
  94. ;; 10/01/2004: fixed a bug in graphviz-dot-indent-graph
  95. ;; 08/01/2004: Rubens Ramos <rubensr AT users.sourceforge.net>
  96. ;; * added customization support
  97. ;; * Now it works on XEmacs and Emacs
  98. ;; * Added support to use an external Viewer
  99. ;; * Now things do not break when dot mode is entered
  100. ;; when there is no buffer name, but the side effect is
  101. ;; that in this case, the compilation command is not
  102. ;; correct.
  103. ;; * Preview works on XEmacs and emacs.
  104. ;; * Electric indentation on newline
  105. ;; * Minor changes to indentation
  106. ;; * Added keyword completion (but could be A LOT better)
  107. ;; * There are still a couple of ugly hacks. Look for 'RR'.
  108. ;; Version 0.2 added features
  109. ;; 11/11/2002: added preview support.
  110. ;; 10/11/2002: indent a graph or subgraph at once with C-M-q.
  111. ;; 08/11/2002: relaxed rules for indentation, the may now be extra chars
  112. ;; after beginning of graph (comment's for example).
  113. ;; Version 0.1.2 bug fixes and naming issues
  114. ;; 06/11/2002: renamed dot-font-lock-defaults to dot-font-lock-keywords.
  115. ;; added some documentation to dot-colors.
  116. ;; provided a much better way to handle my max-specpdl-size
  117. ;; problem.
  118. ;; added an extra autoload cookie (hope this helps, as I don't
  119. ;; yet use autoload myself)
  120. ;; Version 0.1.1 bug fixes
  121. ;; 06/11/2002: added an missing attribute, for font-locking to work.
  122. ;; fixed the regex generating, so that it only recognizes
  123. ;; whole words
  124. ;; 05/11/2002: there can now be extra white space chars after an '{'.
  125. ;; 04/11/2002: Why I use max-specpdl-size is now documented, and old value
  126. ;; gets restored.
  127. ;; Version 0.1 initial release
  128. ;; 02/11/2002: implemented parser for *compilation* of a .dot file.
  129. ;; 01/11/2002: implemented compilation of an .dot file.
  130. ;; 31/10/2002: added syntax-table to the mode.
  131. ;; 30/10/2002: implemented indentation code.
  132. ;; 29/10/2002: implemented all of font-lock.
  133. ;; 28/10/2002: derived graphviz-dot-mode from fundamental-mode, started
  134. ;; implementing font-lock.
  135. ;;; Code:
  136. (require 'compile)
  137. (require 'subr-x)
  138. (defconst graphviz-dot-mode-version "0.3.10"
  139. "Version of `graphviz-dot-mode.el'.")
  140. (defgroup graphviz nil
  141. "Major mode for editing Graphviz Dot files"
  142. :group 'tools)
  143. (defun graphviz-dot-customize ()
  144. "Run \\[customize-group] for the `graphviz' group."
  145. (interactive)
  146. (customize-group 'graphviz))
  147. (defvar graphviz-dot-mode-abbrev-table nil
  148. "Abbrev table in use in Graphviz Dot mode buffers.")
  149. (define-abbrev-table 'graphviz-dot-mode-abbrev-table ())
  150. (defcustom graphviz-dot-dot-program "dot"
  151. "*Location of the dot program. This is used by `compile'."
  152. :type 'string
  153. :group 'graphviz)
  154. (defcustom graphviz-dot-layout-programs
  155. '("dot" "neato" "fdp" "sfdp" "twopi" "twopi" "circo")
  156. "*List of layout programs for the user to choose from."
  157. :type 'list
  158. :group 'graphviz)
  159. (defcustom graphviz-dot-view-command "doted %s"
  160. "*External program to run on the buffer. You can use `%s' in this string,
  161. and it will be substituted by the buffer name."
  162. :type 'string
  163. :group 'graphviz)
  164. (defcustom graphviz-dot-view-edit-command nil
  165. "*Whether to allow the user to edit the command to run an external
  166. viewer."
  167. :type 'boolean
  168. :group 'graphviz)
  169. (defcustom graphviz-dot-save-before-view t
  170. "*If not nil, M-x graphviz-dot-view saves the current buffer before running
  171. the command."
  172. :type 'boolean
  173. :group 'graphviz)
  174. (defcustom graphviz-dot-auto-indent-on-newline t
  175. "*If not nil, `electric-graphviz-dot-terminate-line' is executed in a line is terminated."
  176. :type 'boolean
  177. :group 'graphviz)
  178. (defcustom graphviz-dot-indent-width tab-width
  179. "*Indentation width in Graphviz Dot mode buffers."
  180. :type 'integer
  181. :group 'graphviz)
  182. (defcustom graphviz-dot-auto-indent-on-braces nil
  183. "*If not nil, `electric-graphviz-dot-open-brace' and `electric-graphviz-dot-close-brace' are executed when { or } are typed"
  184. :type 'boolean
  185. :group 'graphviz)
  186. (defcustom graphviz-dot-auto-indent-on-semi t
  187. "*If not nil, `electric-graphviz-dot-semi' is executed when semicolon is typed"
  188. :type 'boolean
  189. :group 'graphviz)
  190. (defcustom graphviz-dot-preview-extension "png"
  191. "*The extension to use for the compilation and preview commands. The format
  192. for the compilation command is
  193. `dot -T<extension> file.dot > file.<extension>'."
  194. :type 'string
  195. :group 'graphviz)
  196. (defcustom graphviz-dot-toggle-completions nil
  197. "*Non-nil means that repeated use of \
  198. \\<graphviz-dot-mode-map>\\[graphviz-dot-complete-word] will toggle the possible
  199. completions in the minibuffer. Normally, when there is more than one possible
  200. completion, a buffer will display all completions."
  201. :type 'boolean
  202. :group 'graphviz)
  203. (defcustom graphviz-dot-delete-completions nil
  204. "*Non-nil means that the completion buffer is automatically deleted when a
  205. key is pressed."
  206. :type 'boolean
  207. :group 'graphviz)
  208. (defcustom graphviz-dot-auto-preview-on-save nil
  209. "*Determines if saving the buffer should automatically trigger preview."
  210. :type 'boolean
  211. :group 'graphviz)
  212. (defcustom graphviz-dot-revert-delay 300
  213. "*Amount of time to sleep for before attempting to display the
  214. rendered image."
  215. :type 'number
  216. :group 'graphviz)
  217. (defcustom graphviz-dot-attr-keywords
  218. '("graph" "digraph" "subgraph" "node" "edge" "strict" "rankdir"
  219. "size" "page" "Damping" "Epsilon" "URL" "arrowhead" "arrowsize"
  220. "arrowtail" "bb" "bgcolor" "bottomlabel" "center" "clusterrank"
  221. "color" "colorscheme" "comment" "compound"
  222. "concentrate" "constraint" "decorate"
  223. "dim" "dir" "distortion" "fillcolor" "fixedsize" "fontcolor"
  224. "fontname" "fontpath" "fontsize" "group" "headURL" "headlabel"
  225. "headport" "height" "label" "labelangle" "labeldistance" "labelfloat"
  226. "labelfontcolor" "labelfontname" "labelfontsize" "labeljust"
  227. "labelloc" "layer" "layers" "len" "lhead" "lp" "ltail" "margin"
  228. "maxiter" "mclimit" "minlen" "model" "nodesep" "normalize" "nslimit"
  229. "nslimit1" "ordering" "orientation" "overlap" "pack" "pagedir"
  230. "pencolor" "peripheries" "pin" "pos" "quantum" "rank" "ranksep"
  231. "ratio" "rects" "regular" "remincross" "rotate" "samehead" "sametail"
  232. "samplepoint" "searchsize" "sep" "shape" "shapefile" "showboxes"
  233. "sides" "skew" "splines" "start" "style" "stylesheet" "tailURL"
  234. "taillabel" "tailport" "toplabel" "vertices" "voro_margin" "weight"
  235. "z" "width" "penwidth" "mindist" "scale" "patch" "root")
  236. "*Keywords for attribute names in a graph. This is used by the auto
  237. completion code. The actual completion tables are built when the mode
  238. is loaded, so changes to this are not immediately visible.
  239. Check http://www.graphviz.org/doc/schema/attributes.xml on new releases."
  240. :type '(repeat (string :tag "Keyword"))
  241. :group 'graphviz)
  242. (defcustom graphviz-dot-value-keywords
  243. '("true" "false" "normal" "inv" "dot" "invdot" "odot" "invodot"
  244. "none" "tee" "empty" "invempty" "diamond" "odiamond" "box" "obox"
  245. "open" "crow" "halfopen" "local" "global" "none" "forward" "back"
  246. "both" "none" "BL" "BR" "TL" "TR" "RB" "RT" "LB" "LT" ":n" ":ne" ":e"
  247. ":se" ":s" ":sw" ":w" ":nw" "same" "min" "source" "max" "sink" "LR"
  248. "box" "polygon" "ellipse" "circle" "point" "egg" "triangle"
  249. "plaintext" "diamond" "trapezium" "parallelogram" "house" "hexagon"
  250. "octagon" "doublecircle" "doubleoctagon" "tripleoctagon" "invtriangle"
  251. "invtrapezium" "invhouse" "Mdiamond" "Msquare" "Mcircle" "record"
  252. "Mrecord" "dashed" "dotted" "solid" "invis" "bold" "filled"
  253. "diagonals" "rounded" )
  254. "*Keywords for attribute values. This is used by the auto completion
  255. code. The actual completion tables are built when the mode is loaded,
  256. so changes to this are not immediately visible."
  257. :type '(repeat (string :tag "Keyword"))
  258. :group 'graphviz)
  259. ;;; Font-locking:
  260. (defvar graphviz-dot-colors-list
  261. '(aliceblue antiquewhite antiquewhite1 antiquewhite2
  262. antiquewhite3 antiquewhite4 aquamarine aquamarine1
  263. aquamarine2 aquamarine3 aquamarine4 azure azure1
  264. azure2 azure3 azure4 beige bisque bisque1 bisque2
  265. bisque3 bisque4 black blanchedalmond blue blue1
  266. blue2 blue3 blue4 blueviolet brown brown1 brown2
  267. brown3 brown4 burlywood burlywood1 burlywood2
  268. burlywood3 burlywood4 cadetblue cadetblue1
  269. cadetblue2 cadetblue3 cadetblue4 chartreuse
  270. chartreuse1 chartreuse2 chartreuse3 chartreuse4
  271. chocolate chocolate1 chocolate2 chocolate3 chocolate4
  272. coral coral1 coral2 coral3 coral4 cornflowerblue
  273. cornsilk cornsilk1 cornsilk2 cornsilk3 cornsilk4
  274. crimson cyan cyan1 cyan2 cyan3 cyan4 darkgoldenrod
  275. darkgoldenrod1 darkgoldenrod2 darkgoldenrod3
  276. darkgoldenrod4 darkgreen darkkhaki darkolivegreen
  277. darkolivegreen1 darkolivegreen2 darkolivegreen3
  278. darkolivegreen4 darkorange darkorange1 darkorange2
  279. darkorange3 darkorange4 darkorchid darkorchid1
  280. darkorchid2 darkorchid3 darkorchid4 darksalmon
  281. darkseagreen darkseagreen1 darkseagreen2
  282. darkseagreen3 darkseagreen4 darkslateblue
  283. darkslategray darkslategray1 darkslategray2
  284. darkslategray3 darkslategray4 darkslategrey
  285. darkturquoise darkviolet deeppink deeppink1
  286. deeppink2 deeppink3 deeppink4 deepskyblue
  287. deepskyblue1 deepskyblue2 deepskyblue3 deepskyblue4
  288. dimgray dimgrey dodgerblue dodgerblue1 dodgerblue2
  289. dodgerblue3 dodgerblue4 firebrick firebrick1
  290. firebrick2 firebrick3 firebrick4 floralwhite
  291. forestgreen gainsboro ghostwhite gold gold1 gold2
  292. gold3 gold4 goldenrod goldenrod1 goldenrod2
  293. goldenrod3 goldenrod4 gray gray0 gray1 gray10 gray100
  294. gray11 gray12 gray13 gray14 gray15 gray16 gray17
  295. gray18 gray19 gray2 gray20 gray21 gray22 gray23
  296. gray24 gray25 gray26 gray27 gray28 gray29 gray3
  297. gray30 gray31 gray32 gray33 gray34 gray35 gray36
  298. gray37 gray38 gray39 gray4 gray40 gray41 gray42
  299. gray43 gray44 gray45 gray46 gray47 gray48 gray49
  300. gray5 gray50 gray51 gray52 gray53 gray54 gray55
  301. gray56 gray57 gray58 gray59 gray6 gray60 gray61
  302. gray62 gray63 gray64 gray65 gray66 gray67 gray68
  303. gray69 gray7 gray70 gray71 gray72 gray73 gray74
  304. gray75 gray76 gray77 gray78 gray79 gray8 gray80
  305. gray81 gray82 gray83 gray84 gray85 gray86 gray87
  306. gray88 gray89 gray9 gray90 gray91 gray92 gray93
  307. gray94 gray95 gray96 gray97 gray98 gray99 green
  308. green1 green2 green3 green4 greenyellow grey grey0
  309. grey1 grey10 grey100 grey11 grey12 grey13 grey14
  310. grey15 grey16 grey17 grey18 grey19 grey2 grey20
  311. grey21 grey22 grey23 grey24 grey25 grey26 grey27
  312. grey28 grey29 grey3 grey30 grey31 grey32 grey33
  313. grey34 grey35 grey36 grey37 grey38 grey39 grey4
  314. grey40 grey41 grey42 grey43 grey44 grey45 grey46
  315. grey47 grey48 grey49 grey5 grey50 grey51 grey52
  316. grey53 grey54 grey55 grey56 grey57 grey58 grey59
  317. grey6 grey60 grey61 grey62 grey63 grey64 grey65
  318. grey66 grey67 grey68 grey69 grey7 grey70 grey71
  319. grey72 grey73 grey74 grey75 grey76 grey77 grey78
  320. grey79 grey8 grey80 grey81 grey82 grey83 grey84
  321. grey85 grey86 grey87 grey88 grey89 grey9 grey90
  322. grey91 grey92 grey93 grey94 grey95 grey96 grey97
  323. grey98 grey99 honeydew honeydew1 honeydew2 honeydew3
  324. honeydew4 hotpink hotpink1 hotpink2 hotpink3 hotpink4
  325. indianred indianred1 indianred2 indianred3 indianred4
  326. indigo ivory ivory1 ivory2 ivory3 ivory4 khaki khaki1
  327. khaki2 khaki3 khaki4 lavender lavenderblush
  328. lavenderblush1 lavenderblush2 lavenderblush3
  329. lavenderblush4 lawngreen lemonchiffon lemonchiffon1
  330. lemonchiffon2 lemonchiffon3 lemonchiffon4 lightblue
  331. lightblue1 lightblue2 lightblue3 lightblue4
  332. lightcoral lightcyan lightcyan1 lightcyan2 lightcyan3
  333. lightcyan4 lightgoldenrod lightgoldenrod1
  334. lightgoldenrod2 lightgoldenrod3 lightgoldenrod4
  335. lightgoldenrodyellow lightgray lightgrey lightpink
  336. lightpink1 lightpink2 lightpink3 lightpink4
  337. lightsalmon lightsalmon1 lightsalmon2 lightsalmon3
  338. lightsalmon4 lightseagreen lightskyblue lightskyblue1
  339. lightskyblue2 lightskyblue3 lightskyblue4
  340. lightslateblue lightslategray lightslategrey
  341. lightsteelblue lightsteelblue1 lightsteelblue2
  342. lightsteelblue3 lightsteelblue4 lightyellow
  343. lightyellow1 lightyellow2 lightyellow3 lightyellow4
  344. limegreen linen magenta magenta1 magenta2 magenta3
  345. magenta4 maroon maroon1 maroon2 maroon3 maroon4
  346. mediumaquamarine mediumblue mediumorchid
  347. mediumorchid1 mediumorchid2 mediumorchid3
  348. mediumorchid4 mediumpurple mediumpurple1
  349. mediumpurple2 mediumpurple3 mediumpurple4
  350. mediumseagreen mediumslateblue mediumspringgreen
  351. mediumturquoise mediumvioletred midnightblue
  352. mintcream mistyrose mistyrose1 mistyrose2 mistyrose3
  353. mistyrose4 moccasin navajowhite navajowhite1
  354. navajowhite2 navajowhite3 navajowhite4 navy navyblue
  355. oldlace olivedrab olivedrap olivedrab1 olivedrab2
  356. olivedrap3 oragne palegoldenrod palegreen palegreen1
  357. palegreen2 palegreen3 palegreen4 paleturquoise
  358. paleturquoise1 paleturquoise2 paleturquoise3
  359. paleturquoise4 palevioletred palevioletred1
  360. palevioletred2 palevioletred3 palevioletred4
  361. papayawhip peachpuff peachpuff1 peachpuff2
  362. peachpuff3 peachpuff4 peru pink pink1 pink2 pink3
  363. pink4 plum plum1 plum2 plum3 plum4 powderblue
  364. purple purple1 purple2 purple3 purple4 red red1 red2
  365. red3 red4 rosybrown rosybrown1 rosybrown2 rosybrown3
  366. rosybrown4 royalblue royalblue1 royalblue2 royalblue3
  367. royalblue4 saddlebrown salmon salmon1 salmon2 salmon3
  368. salmon4 sandybrown seagreen seagreen1 seagreen2
  369. seagreen3 seagreen4 seashell seashell1 seashell2
  370. seashell3 seashell4 sienna sienna1 sienna2 sienna3
  371. sienna4 skyblue skyblue1 skyblue2 skyblue3 skyblue4
  372. slateblue slateblue1 slateblue2 slateblue3 slateblue4
  373. slategray slategray1 slategray2 slategray3 slategray4
  374. slategrey snow snow1 snow2 snow3 snow4 springgreen
  375. springgreen1 springgreen2 springgreen3 springgreen4
  376. steelblue steelblue1 steelblue2 steelblue3 steelblue4
  377. tan tan1 tan2 tan3 tan4 thistle thistle1 thistle2
  378. thistle3 thistle4 tomato tomato1 tomato2 tomato3
  379. tomato4 transparent turquoise turquoise1 turquoise2
  380. turquoise3 turquoise4 violet violetred violetred1
  381. violetred2 violetred3 violetred4 wheat wheat1 wheat2
  382. wheat3 wheat4 white whitesmoke yellow yellow1 yellow2
  383. yellow3 yellow4 yellowgreen)
  384. "Possible color constants in the dot language.
  385. The list of constant is available at http://www.research.att.com/~erg/graphviz\
  386. /info/colors.html")
  387. (defvar graphviz-dot-color-keywords
  388. (mapcar 'symbol-name graphviz-dot-colors-list))
  389. (defvar graphviz-attr-keywords
  390. (mapcar #'(lambda (elm) (cons elm 0)) graphviz-dot-attr-keywords))
  391. (defvar graphviz-value-keywords
  392. (mapcar #'(lambda (elm) (cons elm 0)) graphviz-dot-value-keywords))
  393. (defvar graphviz-color-keywords
  394. (mapcar #'(lambda (elm) (cons elm 0)) graphviz-dot-color-keywords))
  395. ;;; Key map
  396. (defvar graphviz-dot-mode-map
  397. (let ((map (make-sparse-keymap)))
  398. (define-key map "\r" 'electric-graphviz-dot-terminate-line)
  399. (define-key map "{" 'electric-graphviz-dot-open-brace)
  400. (define-key map "}" 'electric-graphviz-dot-close-brace)
  401. (define-key map ";" 'electric-graphviz-dot-semi)
  402. (define-key map "\C-c\M-t" 'graphviz-dot-complete-word)
  403. (define-key map "\C-\M-q" 'graphviz-dot-indent-graph)
  404. (define-key map "\C-c\C-p" 'graphviz-dot-preview)
  405. (define-key map "\C-c\C-c" 'compile)
  406. (define-key map "\C-c\C-v" 'graphviz-dot-view)
  407. map)
  408. "Keymap used in Graphviz Dot mode.")
  409. ;;; Syntax table
  410. (defvar graphviz-dot-mode-syntax-table
  411. (let ((st (make-syntax-table)))
  412. (modify-syntax-entry ?/ ". 124b" st)
  413. (modify-syntax-entry ?* ". 23" st)
  414. (modify-syntax-entry ?\n "> b" st)
  415. (modify-syntax-entry ?= "." st)
  416. (modify-syntax-entry ?_ "_" st)
  417. (modify-syntax-entry ?- "_" st)
  418. (modify-syntax-entry ?> "." st)
  419. (modify-syntax-entry ?\[ "(]" st)
  420. (modify-syntax-entry ?\] ")[" st)
  421. (modify-syntax-entry ?\" "\"" st)
  422. (setq graphviz-dot-mode-syntax-table st))
  423. "Syntax table for `graphviz-dot-mode'.")
  424. (defvar graphviz-dot-syntax-propertize-function
  425. (syntax-propertize-rules
  426. ("^#" (0 "< b"))))
  427. (defvar graphviz-dot-font-lock-keywords
  428. `(("\\(:?di\\|sub\\)?graph \\(\\sw+\\)"
  429. (2 font-lock-function-name-face))
  430. (,(regexp-opt graphviz-dot-value-keywords 'words)
  431. . font-lock-reference-face)
  432. ;; to build the font-locking for the colors,
  433. ;; we need more room for max-specpdl-size,
  434. ;; after that we take the list of symbols,
  435. ;; convert them to a list of strings, and make
  436. ;; an optimized regexp from them
  437. (,(let ((max-specpdl-size (max max-specpdl-size 1200)))
  438. (regexp-opt graphviz-dot-color-keywords 'words))
  439. . font-lock-string-face)
  440. (,(concat
  441. (regexp-opt graphviz-dot-attr-keywords 'words)
  442. "[ \\t\\n]*=")
  443. ;; RR - ugly, really, but I dont know why xemacs does not work
  444. ;; if I change the next car to "1"...
  445. (0 font-lock-variable-name-face))
  446. ;; See the 'graph' nonterminal in
  447. ;; https://graphviz.gitlab.io/_pages/doc/info/lang.html.
  448. ("\\(?:\\_<\\(strict\\)[[:space:]]+\\)?\\(\\(?:di\\)?graph\\)\\_>"
  449. (1 'font-lock-keyword-face) (2 'font-lock-keyword-face))
  450. ;; See the 'attr_stmt' nonterminal in
  451. ;; https://graphviz.gitlab.io/_pages/doc/info/lang.html.
  452. ("\\_<\\(edge\\|graph\\|node\\)\\_>[[:space:]]*\\["
  453. 1 'font-lock-keyword-face)
  454. ;; See the 'subgraph' nonterminal in
  455. ;; https://graphviz.gitlab.io/_pages/doc/info/lang.html.
  456. ("\\_<subgraph\\_>" . 'font-lock-keyword-face))
  457. "Keyword highlighting specification for `graphviz-dot-mode'.")
  458. (defun graphviz-output-file-name (f-name)
  459. (concat (file-name-sans-extension f-name)
  460. "." graphviz-dot-preview-extension))
  461. (defun graphviz-compile-command (f-name)
  462. (when f-name
  463. (setq compile-command
  464. (concat graphviz-dot-dot-program
  465. " -T" graphviz-dot-preview-extension " "
  466. (shell-quote-argument f-name)
  467. " -o "
  468. (shell-quote-argument
  469. (graphviz-output-file-name f-name))))))
  470. (defvar dot-menu nil
  471. "Menu for Graphviz Dot Mode.
  472. This menu will get created automatically if you have the `easymenu'
  473. package. Note that the latest X/Emacs releases contain this package.")
  474. ;;;###autoload
  475. (define-derived-mode graphviz-dot-mode prog-mode "dot"
  476. "Major mode for the dot language. \\<graphviz-dot-mode-map>
  477. TAB indents for graph lines.
  478. \\[graphviz-dot-indent-graph]\t- Indentation function.
  479. \\[graphviz-dot-preview]\t- Previews graph in a buffer.
  480. \\[graphviz-dot-view]\t- Views graph in an external viewer.
  481. \\[graphviz-dot-indent-line]\t- Indents current line of code.
  482. \\[graphviz-dot-complete-word]\t- Completes the current word.
  483. \\[electric-graphviz-dot-terminate-line]\t- Electric newline.
  484. \\[electric-graphviz-dot-open-brace]\t- Electric open braces.
  485. \\[electric-graphviz-dot-close-brace]\t- Electric close braces.
  486. \\[electric-graphviz-dot-semi]\t- Electric semi colons.
  487. Variables specific to this mode:
  488. graphviz-dot-dot-program (default `dot')
  489. Location of the dot program.
  490. graphviz-dot-view-command (default `doted %s')
  491. Command to run when `graphviz-dot-view' is executed.
  492. graphviz-dot-view-edit-command (default nil)
  493. If the user should be asked to edit the view command.
  494. graphviz-dot-save-before-view (default t)
  495. Automatically save current buffer berore `graphviz-dot-view'.
  496. graphviz-dot-preview-extension (default `png')
  497. File type to use for `graphviz-dot-preview'.
  498. graphviz-dot-auto-indent-on-newline (default t)
  499. Whether to run `electric-graphviz-dot-terminate-line' when
  500. newline is entered.
  501. graphviz-dot-auto-indent-on-braces (default t)
  502. Whether to run `electric-graphviz-dot-open-brace' and
  503. `electric-graphviz-dot-close-brace' when braces are
  504. entered.
  505. graphviz-dot-auto-indent-on-semi (default t)
  506. Whether to run `electric-graphviz-dot-semi' when semi colon
  507. is typed.
  508. graphviz-dot-toggle-completions (default nil)
  509. If completions should be displayed in the buffer instead of a
  510. completion buffer when \\[graphviz-dot-complete-word] is
  511. pressed repeatedly.
  512. This mode can be customized by running \\[graphviz-dot-customize].
  513. Turning on Graphviz Dot mode calls the value of the variable
  514. `graphviz-dot-mode-hook' with no args, if that value is non-nil."
  515. (setq-local font-lock-defaults '(graphviz-dot-font-lock-keywords))
  516. (setq-local comment-start "//")
  517. (setq-local comment-start-skip "/\\*+ *\\|//+ *")
  518. (setq-local indent-line-function 'graphviz-dot-indent-line)
  519. (setq-local syntax-propertize-function
  520. graphviz-dot-syntax-propertize-function)
  521. (when (buffer-file-name)
  522. (setq-local compile-command
  523. (graphviz-compile-command (buffer-file-name))))
  524. (setq-local compilation-parse-errors-function 'graphviz-dot-compilation-parse-errors)
  525. (when dot-menu (easy-menu-add dot-menu))
  526. (add-hook 'after-save-hook 'graphviz-live-reload-hook)
  527. (run-hooks 'graphviz-dot-mode-hook))
  528. ;;;; Menu definitions
  529. (and (condition-case nil
  530. (require 'easymenu)
  531. (error nil))
  532. (easy-menu-define
  533. dot-menu graphviz-dot-mode-map "Graphviz Mode menu"
  534. '("Graphviz"
  535. ["Indent Graph" graphviz-dot-indent-graph t]
  536. ["Comment Out Region" comment-region (mark)]
  537. ["Uncomment Region" uncomment-region (mark)]
  538. "-"
  539. ["Compile" compile t]
  540. ["Preview" graphviz-dot-preview
  541. (and (buffer-file-name)
  542. (not (buffer-modified-p)))]
  543. ["External Viewer" graphviz-dot-view (buffer-file-name)]
  544. "-"
  545. ["Customize..." graphviz-dot-customize t]
  546. )))
  547. ;;;; Compilation
  548. ;; note on graphviz-dot-compilation-parse-errors:
  549. ;; It would nicer if we could just use compilation-error-regexp-alist
  550. ;; to do that, 3 options:
  551. ;; - still write dot-compilation-parse-errors, don't build
  552. ;; a return list, but modify the *compilation* buffer
  553. ;; in a way compilation-error-regexp-alist recognizes the
  554. ;; format.
  555. ;; to do that, I should globally change compilation-parse-function
  556. ;; to this function, and call the old value of comp..-parse-fun..
  557. ;; to provide the return value.
  558. ;; two drawbacks are that, every compilation would be run through
  559. ;; this function (performance) and that in autoload there would
  560. ;; be a chance that this function would not yet be known.
  561. ;; - let the compilation run through a filter that would
  562. ;; modify the output of dot or neato:
  563. ;; dot -Tpng input.dot | filter
  564. ;; drawback: ugly, extra work for user, extra decency ...
  565. ;; no-option
  566. ;; - modify dot and neato !!! (PP:15/02/2005 seems to have happend,
  567. ;; so version 0.4.0 should clean this mess up!)
  568. (defun graphviz-dot-compilation-parse-errors (limit-search find-at-least)
  569. "Parse the current buffer for dot errors.
  570. See variable `compilation-parse-errors-functions' for interface."
  571. (interactive)
  572. (with-current-buffer "*compilation*"
  573. (goto-char (point-min))
  574. (setq compilation-error-list nil)
  575. (let (buffer-of-error)
  576. (while (not (eobp))
  577. (cond
  578. ((looking-at "^dot\\( -[^ ]+\\)* \\(.*\\)")
  579. (setq buffer-of-error (find-file-noselect
  580. (buffer-substring-no-properties
  581. (nth 4 (match-data t))
  582. (nth 5 (match-data t))))))
  583. ((looking-at ".*:.*line \\([0-9]+\\)")
  584. (let ((line-of-error
  585. (string-to-number (buffer-substring-no-properties
  586. (nth 2 (match-data t))
  587. (nth 3 (match-data t))))))
  588. (setq compilation-error-list
  589. (cons
  590. (cons
  591. (point-marker)
  592. (with-current-buffer buffer-of-error
  593. (goto-char (point-min))
  594. (forward-line (1- line-of-error))
  595. (point-marker)))
  596. compilation-error-list))))
  597. (t t))
  598. (forward-line 1)) )))
  599. ;;;;
  600. ;;;; Indentation
  601. ;;;;
  602. (defun graphviz-dot-indent-line ()
  603. "Indent current line of dot code."
  604. (interactive)
  605. (if (bolp)
  606. (graphviz-dot-real-indent-line)
  607. (save-excursion
  608. (graphviz-dot-real-indent-line))))
  609. (defun graphviz-dot-real-indent-line ()
  610. "Indent current line of dot code."
  611. (beginning-of-line)
  612. (cond
  613. ((bobp)
  614. ;; simple case, indent to 0
  615. (indent-line-to 0))
  616. ((looking-at "^[ \t]*}[ \t]*$")
  617. ;; block closing, deindent relative to previous line
  618. (indent-line-to (save-excursion
  619. (forward-line -1)
  620. (if (looking-at "\\(^.*{[^}]*$\\)")
  621. ;; previous line opened a block
  622. ;; use same indentation
  623. (current-indentation)
  624. (max 0 (- (current-indentation) graphviz-dot-indent-width))))))
  625. ;; other cases need to look at previous lines
  626. (t
  627. (indent-line-to (save-excursion
  628. (forward-line -1)
  629. (cond
  630. ((looking-at "\\(^.*{[^}]*$\\)")
  631. ;; previous line opened a block
  632. ;; indent to that line
  633. (+ (current-indentation) graphviz-dot-indent-width))
  634. ((and (not (looking-at ".*\\[.*\\].*"))
  635. (looking-at ".*\\[.*")) ; TODO:PP : can be 1 regex
  636. ;; previous line started filling
  637. ;; attributes, intend to that start
  638. (search-forward "[")
  639. (current-column))
  640. ((and (not (looking-at ".*\\[.*\\].*"))
  641. (looking-at ".*\\].*")) ; TODO:PP : "
  642. ;; previous line stopped filling
  643. ;; attributes, find the line that started
  644. ;; filling them and indent to that line
  645. (while (or (looking-at ".*\\[.*\\].*")
  646. (not (looking-at ".*\\[.*"))) ; TODO:PP : "
  647. (forward-line -1))
  648. (current-indentation))
  649. (t
  650. ;; default case, indent the
  651. ;; same as previous NON-BLANK line
  652. ;; (or the first line, if there are no previous non-blank lines)
  653. (while (and (< (point-min) (point))
  654. (looking-at "^\[ \t\]*$"))
  655. (forward-line -1))
  656. ;; if we find a closing square bracket, don't indent
  657. ;; to the level of its attributes, but instead
  658. ;; find the opening bracket and indent to that
  659. (if (looking-at ".*\\].*")
  660. (while (not (looking-at ".*\\[.*"))
  661. (forward-line -1)))
  662. (current-indentation)) ))) )))
  663. (defun graphviz-dot-indent-graph ()
  664. "Indent the graph/digraph/subgraph where point is at.
  665. This will first teach the beginning of the graph were point is at, and
  666. then indent this and each subgraph in it."
  667. (interactive)
  668. (save-excursion
  669. ;; position point at start of graph
  670. (while (not (or (looking-at "\\(^.*{[^}]*$\\)") (bobp)))
  671. (forward-line -1))
  672. ;; bracket { one +; bracket } one -
  673. (let ((bracket-count 0))
  674. (while
  675. (progn
  676. (cond
  677. ;; update bracket-count
  678. ((looking-at "\\(^.*{[^}]*$\\)")
  679. (setq bracket-count (+ bracket-count 1)))
  680. ;; update bracket-count
  681. ((looking-at "^[ \t]*}[ \t]*$")
  682. (setq bracket-count (- bracket-count 1))))
  683. ;; indent this line and move on
  684. (graphviz-dot-indent-line)
  685. (forward-line 1)
  686. ;; as long as we are not completed or at end of buffer
  687. (and (> bracket-count 0) (not (eobp))))))))
  688. ;;;;
  689. ;;;; Electric indentation
  690. ;;;;
  691. (defun graphviz-dot-comment-or-string-p ()
  692. (let ((state (parse-partial-sexp (point-min) (point))))
  693. (or (nth 4 state) (nth 3 state))))
  694. (defun graphviz-dot-newline-and-indent ()
  695. (save-excursion
  696. (beginning-of-line)
  697. (skip-chars-forward " \t")
  698. (graphviz-dot-indent-line))
  699. (delete-horizontal-space)
  700. (newline)
  701. (graphviz-dot-indent-line))
  702. (defun electric-graphviz-dot-terminate-line ()
  703. "Terminate line and indent next line."
  704. (interactive)
  705. (if graphviz-dot-auto-indent-on-newline
  706. (graphviz-dot-newline-and-indent)
  707. (newline)))
  708. (defun electric-graphviz-dot-open-brace ()
  709. "Terminate line and indent next line."
  710. (interactive)
  711. (insert "{")
  712. (if (and graphviz-dot-auto-indent-on-braces
  713. (not (graphviz-dot-comment-or-string-p)))
  714. (graphviz-dot-newline-and-indent)))
  715. (defun electric-graphviz-dot-close-brace ()
  716. "Terminate line and indent next line."
  717. (interactive)
  718. (insert "}")
  719. (if (and graphviz-dot-auto-indent-on-braces
  720. (not (graphviz-dot-comment-or-string-p)))
  721. (progn
  722. (save-excursion
  723. (beginning-of-line)
  724. (skip-chars-forward " \t")
  725. (graphviz-dot-indent-line))
  726. (newline)
  727. (graphviz-dot-indent-line))))
  728. (defun electric-graphviz-dot-semi ()
  729. "Terminate line and indent next line."
  730. (interactive)
  731. (insert ";")
  732. (if (and graphviz-dot-auto-indent-on-semi
  733. (not (graphviz-dot-comment-or-string-p)))
  734. (graphviz-dot-newline-and-indent)))
  735. (defun graphviz-compile-warning-msg ()
  736. (set-buffer "*compilation*")
  737. (goto-char (point-min))
  738. (save-match-data
  739. (when (re-search-forward "^\\(Warning: .+\\)$" nil t)
  740. (match-string-no-properties 1))))
  741. ;;;###autoload
  742. (defun graphviz-dot-preview ()
  743. (interactive)
  744. (save-buffer)
  745. (let ((windows (window-list))
  746. (f-name (graphviz-output-file-name (buffer-file-name)))
  747. (warn-msg (string-trim (shell-command-to-string compile-command))))
  748. (if (string-match-p "^Warning: .+ line \\([0-9]+\\)" warn-msg)
  749. (message warn-msg)
  750. (progn
  751. (sleep-for 0 graphviz-dot-revert-delay)
  752. (when (= (length windows) 1)
  753. (split-window-sensibly))
  754. (with-selected-window (selected-window)
  755. (switch-to-buffer-other-window (find-file-noselect f-name t))
  756. ;; I get "changed on disk; really edit the buffer?" prompt w/o this
  757. (sleep-for 0 50)
  758. (revert-buffer t t))))))
  759. ;;;###autoload
  760. (defun graphviz-turn-on-live-preview ()
  761. "Turns on live preview on save."
  762. (interactive)
  763. (setq graphviz-dot-auto-preview-on-save t)
  764. (add-hook 'after-save-hook 'graphviz-live-reload-hook))
  765. ;;;###autoload
  766. (defun graphviz-turn-off-live-preview ()
  767. "Turns off live preview on save."
  768. (interactive)
  769. (setq graphviz-dot-auto-preview-on-save nil)
  770. (remove-hook 'after-save-hook 'graphviz-live-reload-hook))
  771. (defun graphviz-live-reload-hook ()
  772. (when (and (eq major-mode 'graphviz-dot-mode) graphviz-dot-auto-preview-on-save)
  773. (graphviz-dot-preview)))
  774. ;;;;
  775. ;;;; View
  776. ;;;;
  777. (defun graphviz-dot-view ()
  778. "Runs an external viewer. This creates an external process every time it
  779. is executed. If `graphviz-dot-save-before-view' is set, the current
  780. buffer is saved before the command is executed."
  781. (interactive)
  782. (let ((cmd (if graphviz-dot-view-edit-command
  783. (if (fboundp 'read-shell-command)
  784. (read-shell-command "View command: "
  785. (format graphviz-dot-view-command
  786. (shell-quote-argument (buffer-file-name))))
  787. ;; read-shell-command not available in GNU Emacs 21
  788. (read-from-minibuffer "View command: "
  789. (format graphviz-dot-view-command
  790. (shell-quote-argument (buffer-file-name)))))
  791. (format graphviz-dot-view-command
  792. (shell-quote-argument (buffer-file-name))))))
  793. (if graphviz-dot-save-before-view
  794. (save-buffer))
  795. (start-process-shell-command (downcase mode-name) nil cmd)
  796. (message (format "Executing `%s'..." cmd))))
  797. ;;;;
  798. ;;;; Completion
  799. ;;;;
  800. (defvar graphviz-dot-str nil)
  801. (defvar graphviz-dot-all nil)
  802. (defvar graphviz-dot-pred nil)
  803. (defvar graphviz-dot-buffer-to-use nil)
  804. (defvar graphviz-dot-flag nil)
  805. (defun graphviz-dot-get-state ()
  806. "Returns the syntax state of the current point."
  807. (let ((state (parse-partial-sexp (point-min) (point))))
  808. (cond
  809. ((nth 4 state) 'comment)
  810. ((nth 3 state) 'string)
  811. ((not (nth 1 state)) 'out)
  812. (t (save-excursion
  813. (skip-chars-backward "^[,=\\[]{};")
  814. (backward-char)
  815. (cond
  816. ((looking-at "[\\[,]{};") 'attribute)
  817. ((looking-at "=") (progn
  818. (backward-word 1)
  819. (if (looking-at "[a-zA-Z]*color")
  820. 'color
  821. 'value)))
  822. (t 'other)))))))
  823. (defun graphviz-dot-get-keywords ()
  824. "Return possible completions for a word"
  825. (let ((state (graphviz-dot-get-state)))
  826. (cond
  827. ((equal state 'comment) ())
  828. ((equal state 'string) ())
  829. ((equal state 'out) graphviz-attr-keywords)
  830. ((equal state 'value) graphviz-value-keywords)
  831. ((equal state 'color) graphviz-color-keywords)
  832. ((equal state 'attribute) graphviz-attr-keywords)
  833. (t graphviz-attr-keywords))))
  834. (defvar graphviz-dot-last-word-numb 0)
  835. (defvar graphviz-dot-last-word-shown nil)
  836. (defvar graphviz-dot-last-completions nil)
  837. (defun graphviz-dot-complete-word ()
  838. "Complete word at current point."
  839. (interactive)
  840. (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
  841. (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
  842. (graphviz-dot-str (buffer-substring b e))
  843. (allcomp (if (and graphviz-dot-toggle-completions
  844. (string= graphviz-dot-last-word-shown
  845. graphviz-dot-str))
  846. graphviz-dot-last-completions
  847. (all-completions graphviz-dot-str
  848. (graphviz-dot-get-keywords))))
  849. (match (if graphviz-dot-toggle-completions
  850. "" (try-completion
  851. graphviz-dot-str (mapcar #'(lambda (elm)
  852. (cons elm 0)) allcomp)))))
  853. ;; Delete old string
  854. (delete-region b e)
  855. ;; Toggle-completions inserts whole labels
  856. (if graphviz-dot-toggle-completions
  857. (progn
  858. ;; Update entry number in list
  859. (setq graphviz-dot-last-completions allcomp
  860. graphviz-dot-last-word-numb
  861. (if (>= graphviz-dot-last-word-numb (1- (length allcomp)))
  862. 0
  863. (1+ graphviz-dot-last-word-numb)))
  864. (setq graphviz-dot-last-word-shown
  865. (elt allcomp graphviz-dot-last-word-numb))
  866. ;; Display next match or same string if no match was found
  867. (if (not (null allcomp))
  868. (insert "" graphviz-dot-last-word-shown)
  869. (insert "" graphviz-dot-str)
  870. (message "(No match)")))
  871. ;; The other form of completion does not necessarily do that.
  872. ;; Insert match if found, or the original string if no match
  873. (if (or (null match) (equal match 't))
  874. (progn (insert "" graphviz-dot-str)
  875. (message "(No match)"))
  876. (insert "" match))
  877. ;; Give message about current status of completion
  878. (cond ((equal match 't)
  879. (if (not (null (cdr allcomp)))
  880. (message "(Complete but not unique)")
  881. (message "(Sole completion)")))
  882. ;; Display buffer if the current completion didn't help
  883. ;; on completing the label.
  884. ((and (not (null (cdr allcomp))) (= (length graphviz-dot-str)
  885. (length match)))
  886. (with-output-to-temp-buffer "*Completions*"
  887. (display-completion-list allcomp))
  888. ;; Wait for a keypress. Then delete *Completion* window
  889. (momentary-string-display "" (point))
  890. (if graphviz-dot-delete-completions
  891. (delete-window
  892. (get-buffer-window (get-buffer "*Completions*"))))
  893. )))))
  894. (defun graphviz-dot-set-layout ()
  895. "Changes the value of `graphviz-dot-dot-program'."
  896. (interactive)
  897. (setq graphviz-dot-dot-program
  898. (completing-read "Layout: " graphviz-dot-layout-programs)))
  899. ;;;###autoload
  900. (add-to-list 'auto-mode-alist '("\\.dot\\'" . graphviz-dot-mode))
  901. ;;;###autoload
  902. (add-to-list 'auto-mode-alist '("\\.gv\\'" . graphviz-dot-mode))
  903. (defvar org-src-lang-modes) ; defined in org-src.el
  904. (eval-after-load 'org-mode
  905. '(add-to-list 'org-src-lang-modes '("dot" . graphviz-dot)))
  906. (provide 'graphviz-dot-mode)
  907. ;;; graphviz-dot-mode.el ends here