icon.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. ;;; icon.el --- mode for editing Icon code
  2. ;; Copyright (C) 1989, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Chris Smith <csmith@convex.com>
  4. ;; Created: 15 Feb 89
  5. ;; Keywords: languages
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; A major mode for editing the Icon programming language.
  19. ;;; Code:
  20. (defvar icon-mode-abbrev-table nil
  21. "Abbrev table in use in Icon-mode buffers.")
  22. (define-abbrev-table 'icon-mode-abbrev-table ())
  23. (defvar icon-mode-map ()
  24. "Keymap used in Icon mode.")
  25. (if icon-mode-map
  26. ()
  27. (let ((map (make-sparse-keymap "Icon")))
  28. (setq icon-mode-map (make-sparse-keymap))
  29. (define-key icon-mode-map "{" 'electric-icon-brace)
  30. (define-key icon-mode-map "}" 'electric-icon-brace)
  31. (define-key icon-mode-map "\e\C-h" 'mark-icon-function)
  32. (define-key icon-mode-map "\e\C-a" 'beginning-of-icon-defun)
  33. (define-key icon-mode-map "\e\C-e" 'end-of-icon-defun)
  34. (define-key icon-mode-map "\e\C-q" 'indent-icon-exp)
  35. (define-key icon-mode-map "\177" 'backward-delete-char-untabify)
  36. (define-key icon-mode-map [menu-bar] (make-sparse-keymap "Icon"))
  37. (define-key icon-mode-map [menu-bar icon]
  38. (cons "Icon" map))
  39. (define-key map [beginning-of-icon-defun] '("Beginning of function" . beginning-of-icon-defun))
  40. (define-key map [end-of-icon-defun] '("End of function" . end-of-icon-defun))
  41. (define-key map [comment-region] '("Comment Out Region" . comment-region))
  42. (define-key map [indent-region] '("Indent Region" . indent-region))
  43. (define-key map [indent-line] '("Indent Line" . icon-indent-command))
  44. (put 'eval-region 'menu-enable 'mark-active)
  45. (put 'comment-region 'menu-enable 'mark-active)
  46. (put 'indent-region 'menu-enable 'mark-active)))
  47. (defvar icon-mode-syntax-table nil
  48. "Syntax table in use in Icon-mode buffers.")
  49. (if icon-mode-syntax-table
  50. ()
  51. (setq icon-mode-syntax-table (make-syntax-table))
  52. (modify-syntax-entry ?\\ "\\" icon-mode-syntax-table)
  53. (modify-syntax-entry ?# "<" icon-mode-syntax-table)
  54. (modify-syntax-entry ?\n ">" icon-mode-syntax-table)
  55. (modify-syntax-entry ?$ "." icon-mode-syntax-table)
  56. (modify-syntax-entry ?/ "." icon-mode-syntax-table)
  57. (modify-syntax-entry ?* "." icon-mode-syntax-table)
  58. (modify-syntax-entry ?+ "." icon-mode-syntax-table)
  59. (modify-syntax-entry ?- "." icon-mode-syntax-table)
  60. (modify-syntax-entry ?= "." icon-mode-syntax-table)
  61. (modify-syntax-entry ?% "." icon-mode-syntax-table)
  62. (modify-syntax-entry ?< "." icon-mode-syntax-table)
  63. (modify-syntax-entry ?> "." icon-mode-syntax-table)
  64. (modify-syntax-entry ?& "." icon-mode-syntax-table)
  65. (modify-syntax-entry ?| "." icon-mode-syntax-table)
  66. (modify-syntax-entry ?\' "\"" icon-mode-syntax-table))
  67. (defgroup icon nil
  68. "Mode for editing Icon code."
  69. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  70. :group 'languages)
  71. (defcustom icon-indent-level 4
  72. "*Indentation of Icon statements with respect to containing block."
  73. :type 'integer
  74. :group 'icon)
  75. (defcustom icon-brace-imaginary-offset 0
  76. "*Imagined indentation of a Icon open brace that actually follows a statement."
  77. :type 'integer
  78. :group 'icon)
  79. (defcustom icon-brace-offset 0
  80. "*Extra indentation for braces, compared with other text in same context."
  81. :type 'integer
  82. :group 'icon)
  83. (defcustom icon-continued-statement-offset 4
  84. "*Extra indent for Icon lines not starting new statements."
  85. :type 'integer
  86. :group 'icon)
  87. (defcustom icon-continued-brace-offset 0
  88. "*Extra indent for Icon substatements that start with open-braces.
  89. This is in addition to `icon-continued-statement-offset'."
  90. :type 'integer
  91. :group 'icon)
  92. (defcustom icon-auto-newline nil
  93. "*Non-nil means automatically newline before and after braces Icon code.
  94. This applies when braces are inserted."
  95. :type 'boolean
  96. :group 'icon)
  97. (defcustom icon-tab-always-indent t
  98. "*Non-nil means TAB in Icon mode should always reindent the current line.
  99. It will then reindent, regardless of where in the line point is
  100. when the TAB command is used."
  101. :type 'boolean
  102. :group 'icon)
  103. (defvar icon-imenu-generic-expression
  104. '((nil "^[ \t]*procedure[ \t]+\\(\\sw+\\)[ \t]*(" 1))
  105. "Imenu expression for Icon mode. See `imenu-generic-expression'.")
  106. ;;;###autoload
  107. (define-derived-mode icon-mode prog-mode "Icon"
  108. "Major mode for editing Icon code.
  109. Expression and list commands understand all Icon brackets.
  110. Tab indents for Icon code.
  111. Paragraphs are separated by blank lines only.
  112. Delete converts tabs to spaces as it moves back.
  113. \\{icon-mode-map}
  114. Variables controlling indentation style:
  115. icon-tab-always-indent
  116. Non-nil means TAB in Icon mode should always reindent the current line,
  117. regardless of where in the line point is when the TAB command is used.
  118. icon-auto-newline
  119. Non-nil means automatically newline before and after braces
  120. inserted in Icon code.
  121. icon-indent-level
  122. Indentation of Icon statements within surrounding block.
  123. The surrounding block's indentation is the indentation
  124. of the line on which the open-brace appears.
  125. icon-continued-statement-offset
  126. Extra indentation given to a substatement, such as the
  127. then-clause of an if or body of a while.
  128. icon-continued-brace-offset
  129. Extra indentation given to a brace that starts a substatement.
  130. This is in addition to `icon-continued-statement-offset'.
  131. icon-brace-offset
  132. Extra indentation for line if it starts with an open brace.
  133. icon-brace-imaginary-offset
  134. An open brace following other text is treated as if it were
  135. this far to the right of the start of its line.
  136. Turning on Icon mode calls the value of the variable `icon-mode-hook'
  137. with no args, if that value is non-nil."
  138. :abbrev-table icon-mode-abbrev-table
  139. (set (make-local-variable 'paragraph-start) (concat "$\\|" page-delimiter))
  140. (set (make-local-variable 'paragraph-separate) paragraph-start)
  141. (set (make-local-variable 'indent-line-function) #'icon-indent-line)
  142. (set (make-local-variable 'comment-start) "# ")
  143. (set (make-local-variable 'comment-end) "")
  144. (set (make-local-variable 'comment-start-skip) "# *")
  145. (set (make-local-variable 'comment-indent-function) 'icon-comment-indent)
  146. (set (make-local-variable 'indent-line-function) 'icon-indent-line)
  147. ;; font-lock support
  148. (set (make-local-variable 'font-lock-defaults)
  149. '((icon-font-lock-keywords
  150. icon-font-lock-keywords-1 icon-font-lock-keywords-2)
  151. nil nil ((?_ . "w")) beginning-of-defun
  152. ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
  153. ;;(font-lock-comment-start-regexp . "#")
  154. (font-lock-mark-block-function . mark-defun)))
  155. ;; imenu support
  156. (set (make-local-variable 'imenu-generic-expression)
  157. icon-imenu-generic-expression)
  158. ;; hideshow support
  159. ;; we start from the assertion that `hs-special-modes-alist' is autoloaded.
  160. (unless (assq 'icon-mode hs-special-modes-alist)
  161. (setq hs-special-modes-alist
  162. (cons '(icon-mode "\\<procedure\\>" "\\<end\\>" nil
  163. icon-forward-sexp-function)
  164. hs-special-modes-alist))))
  165. ;; This is used by indent-for-comment to decide how much to
  166. ;; indent a comment in Icon code based on its context.
  167. (defun icon-comment-indent ()
  168. (if (looking-at "^#") 0 comment-column))
  169. (defun electric-icon-brace (arg)
  170. "Insert character and correct line's indentation."
  171. (interactive "P")
  172. (let (insertpos)
  173. (if (and (not arg)
  174. (eolp)
  175. (or (save-excursion
  176. (skip-chars-backward " \t")
  177. (bolp))
  178. (if icon-auto-newline
  179. (progn (icon-indent-line) (newline) t)
  180. nil)))
  181. (progn
  182. (insert last-command-event)
  183. (icon-indent-line)
  184. (if icon-auto-newline
  185. (progn
  186. (newline)
  187. ;; (newline) may have done auto-fill
  188. (setq insertpos (- (point) 2))
  189. (icon-indent-line)))
  190. (save-excursion
  191. (if insertpos (goto-char (1+ insertpos)))
  192. (delete-char -1))))
  193. (if insertpos
  194. (save-excursion
  195. (goto-char insertpos)
  196. (self-insert-command (prefix-numeric-value arg)))
  197. (self-insert-command (prefix-numeric-value arg)))))
  198. (defun icon-indent-command (&optional whole-exp)
  199. "Indent current line as Icon code, or in some cases insert a tab character.
  200. If `icon-tab-always-indent' is non-nil (the default), always indent current
  201. line. Otherwise, indent the current line only if point is at the left margin
  202. or in the line's indentation; otherwise insert a tab.
  203. A numeric argument, regardless of its value, means indent rigidly all the
  204. lines of the expression starting after point so that this line becomes
  205. properly indented. The relative indentation among the lines of the
  206. expression are preserved."
  207. (interactive "P")
  208. (if whole-exp
  209. ;; If arg, always indent this line as Icon
  210. ;; and shift remaining lines of expression the same amount.
  211. (let ((shift-amt (icon-indent-line))
  212. beg end)
  213. (save-excursion
  214. (if icon-tab-always-indent
  215. (beginning-of-line))
  216. (setq beg (point))
  217. (forward-sexp 1)
  218. (setq end (point))
  219. (goto-char beg)
  220. (forward-line 1)
  221. (setq beg (point)))
  222. (if (> end beg)
  223. (indent-code-rigidly beg end shift-amt "#")))
  224. (if (and (not icon-tab-always-indent)
  225. (save-excursion
  226. (skip-chars-backward " \t")
  227. (not (bolp))))
  228. (insert-tab)
  229. (icon-indent-line))))
  230. (defun icon-indent-line ()
  231. "Indent current line as Icon code.
  232. Return the amount the indentation changed by."
  233. (let ((indent (calculate-icon-indent nil))
  234. beg shift-amt
  235. (case-fold-search nil)
  236. (pos (- (point-max) (point))))
  237. (beginning-of-line)
  238. (setq beg (point))
  239. (cond ((eq indent nil)
  240. (setq indent (current-indentation)))
  241. ((looking-at "^#")
  242. (setq indent 0))
  243. (t
  244. (skip-chars-forward " \t")
  245. (if (listp indent) (setq indent (car indent)))
  246. (cond ((and (looking-at "else\\b")
  247. (not (looking-at "else\\s_")))
  248. (setq indent (save-excursion
  249. (icon-backward-to-start-of-if)
  250. (current-indentation))))
  251. ((or (= (following-char) ?})
  252. (looking-at "end\\b"))
  253. (setq indent (- indent icon-indent-level)))
  254. ((= (following-char) ?{)
  255. (setq indent (+ indent icon-brace-offset))))))
  256. (skip-chars-forward " \t")
  257. (setq shift-amt (- indent (current-column)))
  258. (if (zerop shift-amt)
  259. (if (> (- (point-max) pos) (point))
  260. (goto-char (- (point-max) pos)))
  261. (delete-region beg (point))
  262. (indent-to indent)
  263. ;; If initial point was within line's indentation,
  264. ;; position after the indentation. Else stay at same point in text.
  265. (if (> (- (point-max) pos) (point))
  266. (goto-char (- (point-max) pos))))
  267. shift-amt))
  268. (defun calculate-icon-indent (&optional parse-start)
  269. "Return appropriate indentation for current line as Icon code.
  270. In usual case returns an integer: the column to indent to.
  271. Returns nil if line starts inside a string, t if in a comment."
  272. (save-excursion
  273. (beginning-of-line)
  274. (let ((indent-point (point))
  275. (case-fold-search nil)
  276. state
  277. containing-sexp
  278. toplevel)
  279. (if parse-start
  280. (goto-char parse-start)
  281. (setq toplevel (beginning-of-icon-defun)))
  282. (while (< (point) indent-point)
  283. (setq parse-start (point))
  284. (setq state (parse-partial-sexp (point) indent-point 0))
  285. (setq containing-sexp (car (cdr state))))
  286. (cond ((or (nth 3 state) (nth 4 state))
  287. ;; return nil or t if should not change this line
  288. (nth 4 state))
  289. ((and containing-sexp
  290. (/= (char-after containing-sexp) ?{))
  291. ;; line is expression, not statement:
  292. ;; indent to just after the surrounding open.
  293. (goto-char (1+ containing-sexp))
  294. (current-column))
  295. (t
  296. (if toplevel
  297. ;; Outside any procedures.
  298. (progn (icon-backward-to-noncomment (point-min))
  299. (if (icon-is-continuation-line)
  300. icon-continued-statement-offset 0))
  301. ;; Statement level.
  302. (if (null containing-sexp)
  303. (progn (beginning-of-icon-defun)
  304. (setq containing-sexp (point))))
  305. (goto-char indent-point)
  306. ;; Is it a continuation or a new statement?
  307. ;; Find previous non-comment character.
  308. (icon-backward-to-noncomment containing-sexp)
  309. ;; Now we get the answer.
  310. (if (icon-is-continuation-line)
  311. ;; This line is continuation of preceding line's statement;
  312. ;; indent icon-continued-statement-offset more than the
  313. ;; first line of the statement.
  314. (progn
  315. (icon-backward-to-start-of-continued-exp containing-sexp)
  316. (+ icon-continued-statement-offset (current-column)
  317. (if (save-excursion (goto-char indent-point)
  318. (skip-chars-forward " \t")
  319. (eq (following-char) ?{))
  320. icon-continued-brace-offset 0)))
  321. ;; This line starts a new statement.
  322. ;; Position following last unclosed open.
  323. (goto-char containing-sexp)
  324. ;; Is line first statement after an open-brace?
  325. (or
  326. ;; If no, find that first statement and indent like it.
  327. (save-excursion
  328. (if (looking-at "procedure\\s ")
  329. (forward-sexp 3)
  330. (forward-char 1))
  331. (while (progn (skip-chars-forward " \t\n")
  332. (looking-at "#"))
  333. ;; Skip over comments following openbrace.
  334. (forward-line 1))
  335. ;; The first following code counts
  336. ;; if it is before the line we want to indent.
  337. (and (< (point) indent-point)
  338. (current-column)))
  339. ;; If no previous statement,
  340. ;; indent it relative to line brace is on.
  341. ;; For open brace in column zero, don't let statement
  342. ;; start there too. If icon-indent-level is zero,
  343. ;; use icon-brace-offset + icon-continued-statement-offset
  344. ;; instead.
  345. ;; For open-braces not the first thing in a line,
  346. ;; add in icon-brace-imaginary-offset.
  347. (+ (if (and (bolp) (zerop icon-indent-level))
  348. (+ icon-brace-offset
  349. icon-continued-statement-offset)
  350. icon-indent-level)
  351. ;; Move back over whitespace before the openbrace.
  352. ;; If openbrace is not first nonwhite thing on the line,
  353. ;; add the icon-brace-imaginary-offset.
  354. (progn (skip-chars-backward " \t")
  355. (if (bolp) 0 icon-brace-imaginary-offset))
  356. ;; Get initial indentation of the line we are on.
  357. (current-indentation))))))))))
  358. ;; List of words to check for as the last thing on a line.
  359. ;; If cdr is t, next line is a continuation of the same statement,
  360. ;; if cdr is nil, next line starts a new (possibly indented) statement.
  361. (defconst icon-resword-alist
  362. '(("by" . t) ("case" . t) ("create") ("do") ("dynamic" . t) ("else")
  363. ("every" . t) ("if" . t) ("global" . t) ("initial" . t)
  364. ("link" . t) ("local" . t) ("of") ("record" . t) ("repeat" . t)
  365. ("static" . t) ("then") ("to" . t) ("until" . t) ("while" . t)))
  366. (defun icon-is-continuation-line ()
  367. (let* ((ch (preceding-char))
  368. (ch-syntax (char-syntax ch)))
  369. (if (eq ch-syntax ?w)
  370. (assoc (buffer-substring
  371. (progn (forward-word -1) (point))
  372. (progn (forward-word 1) (point)))
  373. icon-resword-alist)
  374. (not (memq ch '(0 ?\; ?\} ?\{ ?\) ?\] ?\" ?\' ?\# ?\, ?\. ?\n))))))
  375. (defun icon-backward-to-noncomment (lim)
  376. (let (opoint stop)
  377. (while (not stop)
  378. (skip-chars-backward " \t\n\f" lim)
  379. (setq opoint (point))
  380. (beginning-of-line)
  381. (if (and (nth 5 (parse-partial-sexp (point) opoint))
  382. (< lim (point)))
  383. (search-backward "#")
  384. (setq stop t)))))
  385. (defun icon-backward-to-start-of-continued-exp (lim)
  386. (if (memq (preceding-char) '(?\) ?\]))
  387. (forward-sexp -1))
  388. (beginning-of-line)
  389. (skip-chars-forward " \t")
  390. (cond
  391. ((<= (point) lim) (goto-char (1+ lim)))
  392. ((not (icon-is-continued-line)) 0)
  393. ((and (eq (char-syntax (following-char)) ?w)
  394. (cdr
  395. (assoc (buffer-substring (point)
  396. (save-excursion (forward-word 1) (point)))
  397. icon-resword-alist))) 0)
  398. (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim))))
  399. (defun icon-is-continued-line ()
  400. (save-excursion
  401. (end-of-line 0)
  402. (icon-is-continuation-line)))
  403. (defun icon-backward-to-start-of-if (&optional limit)
  404. "Move to the start of the last \"unbalanced\" if."
  405. (or limit (setq limit (save-excursion (beginning-of-icon-defun) (point))))
  406. (let ((if-level 1)
  407. (case-fold-search nil))
  408. (while (not (zerop if-level))
  409. (backward-sexp 1)
  410. (cond ((looking-at "else\\b")
  411. (setq if-level (1+ if-level)))
  412. ((looking-at "if\\b")
  413. (setq if-level (1- if-level)))
  414. ((< (point) limit)
  415. (setq if-level 0)
  416. (goto-char limit))))))
  417. (defun mark-icon-function ()
  418. "Put mark at end of Icon function, point at beginning."
  419. (interactive)
  420. (push-mark (point))
  421. (end-of-icon-defun)
  422. (push-mark (point))
  423. (beginning-of-line 0)
  424. (beginning-of-icon-defun))
  425. (defun beginning-of-icon-defun ()
  426. "Go to the start of the enclosing procedure; return t if at top level."
  427. (interactive)
  428. (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move)
  429. (looking-at "e")
  430. t))
  431. (defun end-of-icon-defun ()
  432. (interactive)
  433. (if (not (bobp)) (forward-char -1))
  434. (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move)
  435. (forward-word -1)
  436. (forward-line 1))
  437. (defun indent-icon-exp ()
  438. "Indent each line of the Icon grouping following point."
  439. (interactive)
  440. (let ((indent-stack (list nil))
  441. (contain-stack (list (point)))
  442. (case-fold-search nil)
  443. outer-loop-done inner-loop-done state ostate
  444. this-indent last-depth
  445. at-else at-brace
  446. (opoint (point))
  447. (next-depth 0))
  448. (save-excursion
  449. (forward-sexp 1))
  450. (save-excursion
  451. (setq outer-loop-done nil)
  452. (while (and (not (eobp)) (not outer-loop-done))
  453. (setq last-depth next-depth)
  454. ;; Compute how depth changes over this line
  455. ;; plus enough other lines to get to one that
  456. ;; does not end inside a comment or string.
  457. ;; Meanwhile, do appropriate indentation on comment lines.
  458. (setq inner-loop-done nil)
  459. (while (and (not inner-loop-done)
  460. (not (and (eobp) (setq outer-loop-done t))))
  461. (setq ostate state)
  462. (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  463. nil nil state))
  464. (setq next-depth (car state))
  465. (if (or (nth 4 ostate))
  466. (icon-indent-line))
  467. (if (or (nth 3 state))
  468. (forward-line 1)
  469. (setq inner-loop-done t)))
  470. (if (<= next-depth 0)
  471. (setq outer-loop-done t))
  472. (if outer-loop-done
  473. nil
  474. (while (> last-depth next-depth)
  475. (setq indent-stack (cdr indent-stack)
  476. contain-stack (cdr contain-stack)
  477. last-depth (1- last-depth)))
  478. (while (< last-depth next-depth)
  479. (setq indent-stack (cons nil indent-stack)
  480. contain-stack (cons nil contain-stack)
  481. last-depth (1+ last-depth)))
  482. (if (null (car contain-stack))
  483. (setcar contain-stack (or (car (cdr state))
  484. (save-excursion (forward-sexp -1)
  485. (point)))))
  486. (forward-line 1)
  487. (skip-chars-forward " \t")
  488. (if (eolp)
  489. nil
  490. (if (and (car indent-stack)
  491. (>= (car indent-stack) 0))
  492. ;; Line is on an existing nesting level.
  493. ;; Lines inside parens are handled specially.
  494. (if (/= (char-after (car contain-stack)) ?{)
  495. (setq this-indent (car indent-stack))
  496. ;; Line is at statement level.
  497. ;; Is it a new statement? Is it an else?
  498. ;; Find last non-comment character before this line
  499. (save-excursion
  500. (setq at-else (looking-at "else\\W"))
  501. (setq at-brace (= (following-char) ?{))
  502. (icon-backward-to-noncomment opoint)
  503. (if (icon-is-continuation-line)
  504. ;; Preceding line did not end in comma or semi;
  505. ;; indent this line icon-continued-statement-offset
  506. ;; more than previous.
  507. (progn
  508. (icon-backward-to-start-of-continued-exp (car contain-stack))
  509. (setq this-indent
  510. (+ icon-continued-statement-offset (current-column)
  511. (if at-brace icon-continued-brace-offset 0))))
  512. ;; Preceding line ended in comma or semi;
  513. ;; use the standard indent for this level.
  514. (if at-else
  515. (progn (icon-backward-to-start-of-if opoint)
  516. (setq this-indent (current-indentation)))
  517. (setq this-indent (car indent-stack))))))
  518. ;; Just started a new nesting level.
  519. ;; Compute the standard indent for this level.
  520. (let ((val (calculate-icon-indent
  521. (if (car indent-stack)
  522. (- (car indent-stack))))))
  523. (setcar indent-stack
  524. (setq this-indent val))))
  525. ;; Adjust line indentation according to its contents
  526. (if (or (= (following-char) ?})
  527. (looking-at "end\\b"))
  528. (setq this-indent (- this-indent icon-indent-level)))
  529. (if (= (following-char) ?{)
  530. (setq this-indent (+ this-indent icon-brace-offset)))
  531. ;; Put chosen indentation into effect.
  532. (or (= (current-column) this-indent)
  533. (progn
  534. (delete-region (point) (progn (beginning-of-line) (point)))
  535. (indent-to this-indent)))
  536. ;; Indent any comment following the text.
  537. (or (looking-at comment-start-skip)
  538. (if (re-search-forward comment-start-skip (line-end-position) t)
  539. (progn (indent-for-comment) (beginning-of-line))))))))))
  540. (defconst icon-font-lock-keywords-1
  541. (eval-when-compile
  542. (list
  543. ;; Fontify procedure name definitions.
  544. '("^[ \t]*\\(procedure\\)\\>[ \t]*\\(\\sw+\\)?"
  545. (1 font-lock-builtin-face) (2 font-lock-function-name-face nil t))))
  546. "Subdued level highlighting for Icon mode.")
  547. (defconst icon-font-lock-keywords-2
  548. (append
  549. icon-font-lock-keywords-1
  550. (eval-when-compile
  551. (list
  552. ;; Fontify all type specifiers.
  553. (cons
  554. (regexp-opt '("null" "string" "co-expression" "table" "integer"
  555. "cset" "set" "real" "file" "list") 'words)
  556. 'font-lock-type-face)
  557. ;; Fontify all keywords.
  558. ;;
  559. (cons
  560. (regexp-opt
  561. '("break" "do" "next" "repeat" "to" "by" "else" "if" "not" "return"
  562. "until" "case" "of" "while" "create" "every" "suspend" "default"
  563. "fail" "record" "then") 'words)
  564. 'font-lock-keyword-face)
  565. ;; "end" "initial"
  566. (cons (regexp-opt '("end" "initial") 'words)
  567. 'font-lock-builtin-face)
  568. ;; Fontify all system variables.
  569. (cons
  570. (regexp-opt
  571. '("&allocated" "&ascii" "&clock" "&col" "&collections" "&column"
  572. "&control" "&cset" "&current" "&date" "&dateline" "&digits" "&dump"
  573. "&e" "&error" "&errornumber" "&errortext" "&errorvalue" "&errout"
  574. "&eventcode" "&eventsource" "&eventvalue" "&fail" "&features"
  575. "&file" "&host" "&input" "&interval" "&lcase" "&ldrag" "&letters"
  576. "&level" "&line" "&lpress" "&lrelease" "&main" "&mdrag" "&meta"
  577. "&mpress" "&mrelease" "&null" "&output" "&phi" "&pi" "&pos"
  578. "&progname" "&random" "&rdrag" "&regions" "&resize" "&row"
  579. "&rpress" "&rrelease" "&shift" "&source" "&storage" "&subject"
  580. "&time" "&trace" "&ucase" "&version" "&window" "&x" "&y") t)
  581. 'font-lock-constant-face)
  582. (cons ;; global local static declarations and link files
  583. (concat
  584. "^[ \t]*"
  585. (regexp-opt '("global" "link" "local" "static") t)
  586. "\\(\\sw+\\>\\)*")
  587. '((1 font-lock-builtin-face)
  588. (font-lock-match-c-style-declaration-item-and-skip-to-next
  589. (goto-char (or (match-beginning 2) (match-end 1))) nil
  590. (1 (if (match-beginning 2)
  591. font-lock-function-name-face
  592. font-lock-variable-name-face)))))
  593. (cons ;; $define $elif $ifdef $ifndef $undef
  594. (concat "^"
  595. (regexp-opt'("$define" "$elif" "$ifdef" "$ifndef" "$undef") t)
  596. "\\>[ \t]*\\([^ \t\n]+\\)?")
  597. '((1 font-lock-builtin-face)
  598. (4 font-lock-variable-name-face nil t)))
  599. (cons ;; $dump $endif $else $include
  600. (concat
  601. "^" (regexp-opt'("$dump" "$endif" "$else" "$include") t) "\\>" )
  602. 'font-lock-builtin-face)
  603. (cons ;; $warning $error
  604. (concat "^" (regexp-opt '("$warning" "$error") t)
  605. "\\>[ \t]*\\(.+\\)?")
  606. '((1 font-lock-builtin-face) (3 font-lock-warning-face nil t))))))
  607. "Gaudy level highlighting for Icon mode.")
  608. (defvar icon-font-lock-keywords icon-font-lock-keywords-1
  609. "Default expressions to highlight in `icon-mode'.")
  610. ;;;used by hs-minor-mode
  611. (defun icon-forward-sexp-function (arg)
  612. (if (< arg 0)
  613. (beginning-of-icon-defun)
  614. (end-of-icon-defun)
  615. (forward-char -1)))
  616. (provide 'icon)
  617. ;;; icon.el ends here