fortran.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. ;;; Fortran mode for GNU Emacs (beta test version 1.21, Oct. 1, 1985)
  2. ;;; Copyright (c) 1986 Free Software Foundation, Inc.
  3. ;;; Written by Michael D. Prange (mit-eddie!mit-erl!prange).
  4. ;;; This file is not part of the GNU Emacs distribution (yet).
  5. ;; This file is distributed in the hope that it will be useful,
  6. ;; but WITHOUT ANY WARRANTY. No author or distributor
  7. ;; accepts responsibility to anyone for the consequences of using it
  8. ;; or for whether it serves any particular purpose or works at all,
  9. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  10. ;; License for full details.
  11. ;; Everyone is granted permission to copy, modify and redistribute
  12. ;; this file, but only under the conditions described in the
  13. ;; GNU Emacs General Public License. A copy of this license is
  14. ;; supposed to have been given to you along with GNU Emacs so you
  15. ;; can know your rights and responsibilities. It should be in a
  16. ;; file named COPYING. Among other things, the copyright notice
  17. ;; and this notice must be preserved on all copies.
  18. ;;; Author acknowledges help from Stephen Gildea <mit-erl!gildea>
  19. ;;; Bugs to mit-erl!bug-fortran-mode.
  20. (defvar fortran-do-indent 3
  21. "*Extra indentation applied to `do' blocks.")
  22. (defvar fortran-if-indent 3
  23. "*Extra indentation applied to `if' blocks.")
  24. (defvar fortran-continuation-indent 5
  25. "*Extra indentation applied to `continuation' lines.")
  26. (defvar fortran-comment-indent-style 'fixed
  27. "*nil forces comment lines not to be touched,
  28. 'fixed produces fixed comment indentation to comment-column,
  29. and 'relative indents to current fortran indentation plus comment-column.")
  30. (defvar fortran-comment-line-column 6
  31. "*Indentation for text in comment lines.")
  32. (defvar comment-line-start nil
  33. "*Delimiter inserted to start new full-line comment.")
  34. (defvar comment-line-start-skip nil
  35. "*Regexp to match the start of a full-line comment.")
  36. (defvar fortran-minimum-statement-indent 6
  37. "*Minimum indentation for fortran statements.")
  38. ;; Note that this is documented in the v18 manuals as being a string
  39. ;; of length one rather than a single character.
  40. ;; The code in this file accepts either format for compatibility.
  41. (defvar fortran-comment-indent-char ?
  42. "*Character to be inserted for Fortran comment indentation.
  43. Normally a space.")
  44. (defvar fortran-line-number-indent 1
  45. "*Maximum indentation for Fortran line numbers.
  46. 5 means right-justify them within their five-column field.")
  47. (defvar fortran-check-all-num-for-matching-do nil
  48. "*Non-nil causes all numbered lines to be treated as possible do-loop ends.")
  49. (defvar fortran-continuation-char ?$
  50. "*Character which is inserted in column 5 by \\[fortran-split-line]
  51. to begin a continuation line. Normally $.")
  52. (defvar fortran-comment-region "c$$$"
  53. "*String inserted by \\[fortran-comment-region] at start of each line in region.")
  54. (defvar fortran-electric-line-number t
  55. "*Non-nil causes line number digits to be moved to the correct column as typed.")
  56. (defvar fortran-startup-message t
  57. "*Non-nil displays a startup message when fortran-mode is first called.")
  58. (defvar fortran-column-ruler
  59. (concat "0 4 6 10 20 30 40 50 60 70\n"
  60. "[ ]|{ | | | | | | | | | | | | |}\n")
  61. "*String displayed above current line by \\[fortran-column-ruler].")
  62. (defconst fortran-mode-version "1.21")
  63. (defvar fortran-mode-syntax-table nil
  64. "Syntax table in use in fortran-mode buffers.")
  65. (if fortran-mode-syntax-table
  66. ()
  67. (setq fortran-mode-syntax-table (make-syntax-table))
  68. (modify-syntax-entry ?\; "w" fortran-mode-syntax-table)
  69. (modify-syntax-entry ?+ "." fortran-mode-syntax-table)
  70. (modify-syntax-entry ?- "." fortran-mode-syntax-table)
  71. (modify-syntax-entry ?* "." fortran-mode-syntax-table)
  72. (modify-syntax-entry ?/ "." fortran-mode-syntax-table)
  73. (modify-syntax-entry ?\' "\"" fortran-mode-syntax-table)
  74. (modify-syntax-entry ?\" "\"" fortran-mode-syntax-table)
  75. (modify-syntax-entry ?\\ "/" fortran-mode-syntax-table)
  76. (modify-syntax-entry ?. "w" fortran-mode-syntax-table)
  77. (modify-syntax-entry ?\n ">" fortran-mode-syntax-table))
  78. (defvar fortran-mode-map ()
  79. "Keymap used in fortran mode.")
  80. (if fortran-mode-map
  81. ()
  82. (setq fortran-mode-map (make-sparse-keymap))
  83. (define-key fortran-mode-map ";" 'fortran-abbrev-start)
  84. (define-key fortran-mode-map "\C-c;" 'fortran-comment-region)
  85. (define-key fortran-mode-map "\e\C-a" 'beginning-of-fortran-subprogram)
  86. (define-key fortran-mode-map "\e\C-e" 'end-of-fortran-subprogram)
  87. (define-key fortran-mode-map "\e;" 'fortran-indent-comment)
  88. (define-key fortran-mode-map "\e\C-h" 'mark-fortran-subprogram)
  89. (define-key fortran-mode-map "\e\n" 'fortran-split-line)
  90. (define-key fortran-mode-map "\e\C-q" 'fortran-indent-subprogram)
  91. (define-key fortran-mode-map "\C-c\C-w" 'fortran-window-create)
  92. (define-key fortran-mode-map "\C-c\C-r" 'fortran-column-ruler)
  93. (define-key fortran-mode-map "\C-c\C-p" 'fortran-previous-statement)
  94. (define-key fortran-mode-map "\C-c\C-n" 'fortran-next-statement)
  95. (define-key fortran-mode-map "\t" 'fortran-indent-line)
  96. (define-key fortran-mode-map "0" 'fortran-electric-line-number)
  97. (define-key fortran-mode-map "1" 'fortran-electric-line-number)
  98. (define-key fortran-mode-map "2" 'fortran-electric-line-number)
  99. (define-key fortran-mode-map "3" 'fortran-electric-line-number)
  100. (define-key fortran-mode-map "4" 'fortran-electric-line-number)
  101. (define-key fortran-mode-map "5" 'fortran-electric-line-number)
  102. (define-key fortran-mode-map "6" 'fortran-electric-line-number)
  103. (define-key fortran-mode-map "7" 'fortran-electric-line-number)
  104. (define-key fortran-mode-map "8" 'fortran-electric-line-number)
  105. (define-key fortran-mode-map "9" 'fortran-electric-line-number))
  106. (defvar fortran-mode-abbrev-table nil)
  107. (if fortran-mode-abbrev-table
  108. ()
  109. (define-abbrev-table 'fortran-mode-abbrev-table ())
  110. (let ((abbrevs-changed nil))
  111. (define-abbrev fortran-mode-abbrev-table ";b" "byte" nil)
  112. (define-abbrev fortran-mode-abbrev-table ";ch" "character" nil)
  113. (define-abbrev fortran-mode-abbrev-table ";cl" "close" nil)
  114. (define-abbrev fortran-mode-abbrev-table ";c" "continue" nil)
  115. (define-abbrev fortran-mode-abbrev-table ";cm" "common" nil)
  116. (define-abbrev fortran-mode-abbrev-table ";cx" "complex" nil)
  117. (define-abbrev fortran-mode-abbrev-table ";di" "dimension" nil)
  118. (define-abbrev fortran-mode-abbrev-table ";do" "double" nil)
  119. (define-abbrev fortran-mode-abbrev-table ";dc" "double complex" nil)
  120. (define-abbrev fortran-mode-abbrev-table ";dp" "double precision" nil)
  121. (define-abbrev fortran-mode-abbrev-table ";dw" "do while" nil)
  122. (define-abbrev fortran-mode-abbrev-table ";e" "else" nil)
  123. (define-abbrev fortran-mode-abbrev-table ";ed" "enddo" nil)
  124. (define-abbrev fortran-mode-abbrev-table ";el" "elseif" nil)
  125. (define-abbrev fortran-mode-abbrev-table ";en" "endif" nil)
  126. (define-abbrev fortran-mode-abbrev-table ";eq" "equivalence" nil)
  127. (define-abbrev fortran-mode-abbrev-table ";ex" "external" nil)
  128. (define-abbrev fortran-mode-abbrev-table ";ey" "entry" nil)
  129. (define-abbrev fortran-mode-abbrev-table ";f" "format" nil)
  130. (define-abbrev fortran-mode-abbrev-table ";fu" "function" nil)
  131. (define-abbrev fortran-mode-abbrev-table ";g" "goto" nil)
  132. (define-abbrev fortran-mode-abbrev-table ";im" "implicit" nil)
  133. (define-abbrev fortran-mode-abbrev-table ";ib" "implicit byte" nil)
  134. (define-abbrev fortran-mode-abbrev-table ";ic" "implicit complex" nil)
  135. (define-abbrev fortran-mode-abbrev-table ";ich" "implicit character" nil)
  136. (define-abbrev fortran-mode-abbrev-table ";ii" "implicit integer" nil)
  137. (define-abbrev fortran-mode-abbrev-table ";il" "implicit logical" nil)
  138. (define-abbrev fortran-mode-abbrev-table ";ir" "implicit real" nil)
  139. (define-abbrev fortran-mode-abbrev-table ";inc" "include" nil)
  140. (define-abbrev fortran-mode-abbrev-table ";in" "integer" nil)
  141. (define-abbrev fortran-mode-abbrev-table ";intr" "intrinsic" nil)
  142. (define-abbrev fortran-mode-abbrev-table ";l" "logical" nil)
  143. (define-abbrev fortran-mode-abbrev-table ";op" "open" nil)
  144. (define-abbrev fortran-mode-abbrev-table ";pa" "parameter" nil)
  145. (define-abbrev fortran-mode-abbrev-table ";pr" "program" nil)
  146. (define-abbrev fortran-mode-abbrev-table ";p" "print" nil)
  147. (define-abbrev fortran-mode-abbrev-table ";re" "real" nil)
  148. (define-abbrev fortran-mode-abbrev-table ";r" "read" nil)
  149. (define-abbrev fortran-mode-abbrev-table ";rt" "return" nil)
  150. (define-abbrev fortran-mode-abbrev-table ";rw" "rewind" nil)
  151. (define-abbrev fortran-mode-abbrev-table ";s" "stop" nil)
  152. (define-abbrev fortran-mode-abbrev-table ";su" "subroutine" nil)
  153. (define-abbrev fortran-mode-abbrev-table ";ty" "type" nil)
  154. (define-abbrev fortran-mode-abbrev-table ";w" "write" nil)))
  155. (defun fortran-mode ()
  156. "Major mode for editing fortran code.
  157. Tab indents the current fortran line correctly.
  158. `do' statements must not share a common `continue'.
  159. Type `;?' or `;\\[help-command]' to display a list of built-in abbrevs for Fortran keywords.
  160. Variables controlling indentation style and extra features:
  161. comment-start
  162. Normally nil in Fortran mode. If you want to use comments
  163. starting with `!', set this to the string \"!\".
  164. fortran-do-indent
  165. Extra indentation within do blocks. (default 3)
  166. fortran-if-indent
  167. Extra indentation within if blocks. (default 3)
  168. fortran-continuation-indent
  169. Extra indentation appled to continuation statements. (default 5)
  170. fortran-comment-line-column
  171. Amount of indentation for text within full-line comments. (default 6)
  172. fortran-comment-indent-style
  173. nil means don't change indentation of text in full-line comments,
  174. fixed means indent that text at column fortran-comment-line-column
  175. relative means indent at fortran-comment-line-column beyond the
  176. indentation for a line of code.
  177. Default value is fixed.
  178. fortran-comment-indent-char
  179. Character to be inserted instead of space for full-line comment
  180. indentation. (default SPC)
  181. fortran-minimum-statement-indent
  182. Minimum indentation for fortran statements. (default 6)
  183. fortran-line-number-indent
  184. Maximum indentation for line numbers. A line number will get
  185. less than this much indentation if necessary to avoid reaching
  186. column 5. (default 1)
  187. fortran-check-all-num-for-matching-do
  188. Non-nil causes all numbered lines to be treated as possible 'continue'
  189. statements. (default nil)
  190. fortran-continuation-char
  191. character to be inserted in column 5 of a continuation line.
  192. (default $)
  193. fortran-comment-region
  194. String inserted by \\[fortran-comment-region] at start of each line in
  195. region. (default \"c$$$\")
  196. fortran-electric-line-number
  197. Non-nil causes line number digits to be moved to the correct column
  198. as typed. (default t)
  199. fortran-startup-message
  200. Set to nil to inhibit message first time fortran-mode is used.
  201. Turning on Fortran mode calls the value of the variable fortran-mode-hook
  202. with no args, if that value is non-nil.
  203. \\{fortran-mode-map}"
  204. (interactive)
  205. (kill-all-local-variables)
  206. (if fortran-startup-message
  207. (message "Emacs Fortran mode version %s. Bugs to mit-erl!bug-fortran-mode" fortran-mode-version))
  208. (setq fortran-startup-message nil)
  209. (setq local-abbrev-table fortran-mode-abbrev-table)
  210. (set-syntax-table fortran-mode-syntax-table)
  211. (make-local-variable 'indent-line-function)
  212. (setq indent-line-function 'fortran-indent-line)
  213. (make-local-variable 'comment-indent-hook)
  214. (setq comment-indent-hook 'fortran-comment-hook)
  215. (make-local-variable 'comment-line-start-skip)
  216. (setq comment-line-start-skip "^[Cc*][^ \t\n]*[ \t]*") ;[^ \t\n]* handles comment strings such as c$$$
  217. (make-local-variable 'comment-line-start)
  218. (setq comment-line-start "c")
  219. (make-local-variable 'comment-start-skip)
  220. (setq comment-start-skip "![ \t]*")
  221. (make-local-variable 'comment-start)
  222. (setq comment-start nil)
  223. (make-local-variable 'require-final-newline)
  224. (setq require-final-newline t)
  225. (make-local-variable 'abbrev-all-caps)
  226. (setq abbrev-all-caps t)
  227. (make-local-variable 'indent-tabs-mode)
  228. (setq indent-tabs-mode nil)
  229. (use-local-map fortran-mode-map)
  230. (setq mode-name "Fortran")
  231. (setq major-mode 'fortran-mode)
  232. (run-hooks 'fortran-mode-hook))
  233. (defun fortran-comment-hook ()
  234. (save-excursion
  235. (skip-chars-backward " \t")
  236. (max (+ 1 (current-column))
  237. comment-column)))
  238. (defun fortran-indent-comment ()
  239. "Align or create comment on current line.
  240. Existing comments of all types are recognized and aligned.
  241. If the line has no comment, a side-by-side comment is inserted and aligned
  242. if the value of comment-start is not nil.
  243. Otherwise, a separate-line comment is inserted, on this line
  244. or on a new line inserted before this line if this line is not blank."
  245. (interactive)
  246. (beginning-of-line)
  247. ;; Recognize existing comments of either kind.
  248. (cond ((looking-at comment-line-start-skip)
  249. (fortran-indent-line))
  250. ((re-search-forward comment-start-skip
  251. (save-excursion (end-of-line) (point)) t)
  252. (indent-for-comment))
  253. ;; No existing comment.
  254. ;; If side-by-side comments are defined, insert one,
  255. ;; unless line is now blank.
  256. ((and comment-start (not (looking-at "^[ \t]*$")))
  257. (end-of-line)
  258. (delete-horizontal-space)
  259. (indent-to (fortran-comment-hook))
  260. (insert comment-start))
  261. ;; Else insert separate-line comment, making a new line if nec.
  262. (t
  263. (if (looking-at "^[ \t]*$")
  264. (delete-horizontal-space)
  265. (beginning-of-line)
  266. (insert "\n")
  267. (forward-char -1))
  268. (insert comment-line-start)
  269. (insert-char (if (stringp fortran-comment-indent-char)
  270. (aref fortran-comment-indent-char 0)
  271. fortran-comment-indent-char)
  272. (- (calculate-fortran-indent) (current-column))))))
  273. (defun fortran-comment-region (beg-region end-region arg)
  274. "Comments every line in the region.
  275. Puts fortran-comment-region at the beginning of every line in the region.
  276. BEG-REGION and END-REGION are args which specify the region boundaries.
  277. With non-nil ARG, uncomments the region."
  278. (interactive "*r\nP")
  279. (let ((end-region-mark (make-marker)) (save-point (point-marker)))
  280. (set-marker end-region-mark end-region)
  281. (goto-char beg-region)
  282. (beginning-of-line)
  283. (if (not arg) ;comment the region
  284. (progn (insert fortran-comment-region)
  285. (while (and (= (forward-line 1) 0)
  286. (< (point) end-region-mark))
  287. (insert fortran-comment-region)))
  288. (let ((com (regexp-quote fortran-comment-region))) ;uncomment the region
  289. (if (looking-at com)
  290. (delete-region (point) (match-end 0)))
  291. (while (and (= (forward-line 1) 0)
  292. (< (point) end-region-mark))
  293. (if (looking-at com)
  294. (delete-region (point) (match-end 0))))))
  295. (goto-char save-point)
  296. (set-marker end-region-mark nil)
  297. (set-marker save-point nil)))
  298. (defun fortran-abbrev-start ()
  299. "Typing \";\\[help-command]\" or \";?\" lists all the fortran abbrevs.
  300. Any other key combination is executed normally." ;\\[help-command] is just a way to print the value of the variable help-char.
  301. (interactive)
  302. (let (c)
  303. (insert last-command-char)
  304. (if (or (= (setq c (read-char)) ??) ;insert char if not equal to `?'
  305. (= c help-char))
  306. (fortran-abbrev-help)
  307. (setq unread-command-char c))))
  308. (defun fortran-abbrev-help ()
  309. "List the currently defined abbrevs in Fortran mode."
  310. (interactive)
  311. (message "Listing abbrev table...")
  312. (require 'abbrevlist)
  313. (list-one-abbrev-table fortran-mode-abbrev-table "*Help*")
  314. (message "Listing abbrev table...done"))
  315. (defun fortran-column-ruler ()
  316. "Inserts a column ruler momentarily above current line, till next keystroke.
  317. The ruler is defined by the value of fortran-column-ruler.
  318. The key typed is executed unless it is SPC."
  319. (interactive)
  320. (momentary-string-display
  321. fortran-column-ruler (save-excursion (beginning-of-line) (point))
  322. nil "Type SPC or any command to erase ruler."))
  323. (defun fortran-window-create ()
  324. "Makes the window 72 columns wide."
  325. (interactive)
  326. (let ((window-min-width 2))
  327. (split-window-horizontally 73))
  328. (other-window 1)
  329. (switch-to-buffer " fortran-window-extra" t)
  330. (select-window (previous-window)))
  331. (defun fortran-split-line ()
  332. "Break line at point and insert continuation marker and alignment."
  333. (interactive)
  334. (delete-horizontal-space)
  335. (if (save-excursion (beginning-of-line) (looking-at comment-line-start-skip))
  336. (insert ?\n comment-line-start ?\ )
  337. (insert ?\n fortran-continuation-char))
  338. (fortran-indent-line))
  339. (defun delete-horizontal-regexp (chars)
  340. "Delete all characters in CHARS around point.
  341. CHARS is like the inside of a [...] in a regular expression
  342. except that ] is never special and \ quotes ^, - or \."
  343. (interactive "*s")
  344. (skip-chars-backward chars)
  345. (delete-region (point) (progn (skip-chars-forward chars) (point))))
  346. (defun fortran-electric-line-number (arg)
  347. "Self insert, but if part of a Fortran line number indent it automatically.
  348. Auto-indent does not happen if a numeric arg is used."
  349. (interactive "P")
  350. (if (or arg (not fortran-electric-line-number))
  351. (self-insert-command arg)
  352. (if (or (save-excursion (re-search-backward "[^ \t0-9]"
  353. (save-excursion
  354. (beginning-of-line)
  355. (point))
  356. t)) ;not a line number
  357. (looking-at "[0-9]")) ;within a line number
  358. (insert last-command-char)
  359. (skip-chars-backward " \t")
  360. (insert last-command-char)
  361. (fortran-indent-line))))
  362. (defun beginning-of-fortran-subprogram ()
  363. "Moves point to the beginning of the current fortran subprogram."
  364. (interactive)
  365. (let ((case-fold-search t))
  366. (beginning-of-line -1)
  367. (re-search-backward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil 'move)
  368. (if (looking-at "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")
  369. (forward-line 1))))
  370. (defun end-of-fortran-subprogram ()
  371. "Moves point to the end of the current fortran subprogram."
  372. (interactive)
  373. (let ((case-fold-search t))
  374. (beginning-of-line 2)
  375. (re-search-forward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil 'move)
  376. (goto-char (match-beginning 0))
  377. (forward-line 1)))
  378. (defun mark-fortran-subprogram ()
  379. "Put mark at end of fortran subprogram, point at beginning.
  380. The marks are pushed."
  381. (interactive)
  382. (end-of-fortran-subprogram)
  383. (push-mark (point))
  384. (beginning-of-fortran-subprogram))
  385. (defun fortran-previous-statement ()
  386. "Moves point to beginning of the previous fortran statement.
  387. Returns 'first-statement if that statement is the first
  388. non-comment Fortran statement in the file, and nil otherwise."
  389. (interactive)
  390. (let (not-first-statement continue-test)
  391. (beginning-of-line)
  392. (setq continue-test
  393. (or (looking-at
  394. (concat "[ \t]*" (regexp-quote (char-to-string
  395. fortran-continuation-char))))
  396. (looking-at " [^ 0\n]")))
  397. (while (and (setq not-first-statement (= (forward-line -1) 0))
  398. (or (looking-at comment-line-start-skip)
  399. (looking-at "[ \t]*$")
  400. (looking-at " [^ 0\n]")
  401. (looking-at (concat "[ \t]*" comment-start-skip)))))
  402. (cond ((and continue-test
  403. (not not-first-statement))
  404. (message "Incomplete continuation statement."))
  405. (continue-test
  406. (fortran-previous-statement))
  407. ((not not-first-statement)
  408. 'first-statement))))
  409. (defun fortran-next-statement ()
  410. "Moves point to beginning of the next fortran statement.
  411. Returns 'last-statement if that statement is the last
  412. non-comment Fortran statement in the file, and nil otherwise."
  413. (interactive)
  414. (let (not-last-statement)
  415. (beginning-of-line)
  416. (while (and (setq not-last-statement (= (forward-line 1) 0))
  417. (or (looking-at comment-line-start-skip)
  418. (looking-at "[ \t]*$")
  419. (looking-at " [^ 0\n]")
  420. (looking-at (concat "[ \t]*" comment-start-skip)))))
  421. (if (not not-last-statement)
  422. 'last-statement)))
  423. (defun fortran-indent-line ()
  424. "Indents current fortran line based on its contents and on previous lines."
  425. (interactive)
  426. (let ((cfi (calculate-fortran-indent)))
  427. (save-excursion
  428. (beginning-of-line)
  429. (if (or (not (= cfi (fortran-current-line-indentation)))
  430. (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
  431. (not (fortran-line-number-indented-correctly-p))))
  432. (fortran-indent-to-column cfi)
  433. (beginning-of-line)
  434. (if (re-search-forward comment-start-skip
  435. (save-excursion (end-of-line) (point)) 'move)
  436. (fortran-indent-comment))))
  437. ;; Never leave point in left margin.
  438. (if (< (current-column) cfi)
  439. (move-to-column cfi))))
  440. (defun fortran-indent-subprogram ()
  441. "Properly indents the Fortran subprogram which contains point."
  442. (interactive)
  443. (save-excursion
  444. (mark-fortran-subprogram)
  445. (message "Indenting subprogram...")
  446. (indent-region (point) (mark) nil))
  447. (message "Indenting subprogram...done."))
  448. (defun calculate-fortran-indent ()
  449. "Calculates the fortran indent column based on previous lines."
  450. (let (icol first-statement (case-fold-search t))
  451. (save-excursion
  452. (setq first-statement (fortran-previous-statement))
  453. (if first-statement
  454. (setq icol fortran-minimum-statement-indent)
  455. (progn
  456. (if (= (point) (point-min))
  457. (setq icol fortran-minimum-statement-indent)
  458. (setq icol (fortran-current-line-indentation)))
  459. (skip-chars-forward " \t0-9")
  460. (cond ((looking-at "if[ \t]*(")
  461. (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
  462. (let (then-test) ;multi-line if-then
  463. (while (and (= (forward-line 1) 0) ;search forward for then
  464. (looking-at " [^ 0]")
  465. (not (setq then-test (looking-at ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
  466. then-test))
  467. (setq icol (+ icol fortran-if-indent))))
  468. ((looking-at "\\(else\\|elseif\\)\\b")
  469. (setq icol (+ icol fortran-if-indent)))
  470. ((looking-at "do\\b")
  471. (setq icol (+ icol fortran-do-indent)))))))
  472. (save-excursion
  473. (beginning-of-line)
  474. (cond ((looking-at "[ \t]*$"))
  475. ((looking-at comment-line-start-skip)
  476. (cond ((eq fortran-comment-indent-style 'relative)
  477. (setq icol (+ icol fortran-comment-line-column)))
  478. ((eq fortran-comment-indent-style 'fixed)
  479. (setq icol fortran-comment-line-column))))
  480. ((or (looking-at (concat "[ \t]*"
  481. (regexp-quote (char-to-string fortran-continuation-char))))
  482. (looking-at " [^ 0\n]"))
  483. (setq icol (+ icol fortran-continuation-indent)))
  484. (first-statement)
  485. ((and fortran-check-all-num-for-matching-do
  486. (looking-at "[ \t]*[0-9]+")
  487. (fortran-check-for-matching-do))
  488. (setq icol (- icol fortran-do-indent)))
  489. (t
  490. (skip-chars-forward " \t0-9")
  491. (cond ((looking-at "end[ \t]*if\\b")
  492. (setq icol (- icol fortran-if-indent)))
  493. ((looking-at "\\(else\\|elseif\\)\\b")
  494. (setq icol (- icol fortran-if-indent)))
  495. ((and (looking-at "continue\\b")
  496. (fortran-check-for-matching-do))
  497. (setq icol (- icol fortran-do-indent)))
  498. ((looking-at "end[ \t]*do\\b")
  499. (setq icol (- icol fortran-do-indent)))
  500. ((and (looking-at "end\\b[ \t]*[^ \t=(a-z]")
  501. (not (= icol fortran-minimum-statement-indent)))
  502. (message "Warning: `end' not in column %d. Probably an unclosed block." fortran-minimum-statement-indent))))))
  503. (max fortran-minimum-statement-indent icol)))
  504. (defun fortran-current-line-indentation ()
  505. "Indentation of current line, ignoring Fortran line number or continuation.
  506. This is the column position of the first non-whitespace character
  507. aside from the line number and/or column 5 line-continuation character.
  508. For comment lines, returns indentation of the first
  509. non-indentation text within the comment."
  510. (save-excursion
  511. (beginning-of-line)
  512. (cond ((looking-at comment-line-start-skip)
  513. (goto-char (match-end 0))
  514. (skip-chars-forward
  515. (if (stringp fortran-comment-indent-char)
  516. fortran-comment-indent-char
  517. (char-to-string fortran-comment-indent-char))))
  518. ((looking-at " [^ 0\n]")
  519. (goto-char (match-end 0)))
  520. (t
  521. ;; Move past line number.
  522. (move-to-column 5)))
  523. ;; Move past whitespace.
  524. (skip-chars-forward " \t")
  525. (current-column)))
  526. (defun fortran-indent-to-column (col)
  527. "Indents current line with spaces to column COL.
  528. notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
  529. line, and this continuation character is retained on indentation;
  530. 2) If fortran-continuation-char is the first non-whitespace character,
  531. this is a continuation line;
  532. 3) A non-continuation line which has a number as the first
  533. non-whitespace character is a numbered line."
  534. (save-excursion
  535. (beginning-of-line)
  536. (if (looking-at comment-line-start-skip)
  537. (if fortran-comment-indent-style
  538. (let ((char (if (stringp fortran-comment-indent-char)
  539. (aref fortran-comment-indent-char 0)
  540. fortran-comment-indent-char)))
  541. (goto-char (match-end 0))
  542. (delete-horizontal-regexp (concat " \t" (char-to-string char)))
  543. (insert-char char (- col (current-column)))))
  544. (if (looking-at " [^ 0\n]")
  545. (forward-char 6)
  546. (delete-horizontal-space)
  547. ;; Put line number in columns 0-4
  548. ;; or put continuation character in column 5.
  549. (cond ((eobp))
  550. ((= (following-char) fortran-continuation-char)
  551. (indent-to 5)
  552. (forward-char 1))
  553. ((looking-at "[0-9]+")
  554. (let ((extra-space (- 5 (- (match-end 0) (point)))))
  555. (if (< extra-space 0)
  556. (message "Warning: line number exceeds 5-digit limit.")
  557. (indent-to (min fortran-line-number-indent extra-space))))
  558. (skip-chars-forward "0-9"))))
  559. ;; Point is now after any continuation character or line number.
  560. ;; Put body of statement where specified.
  561. (delete-horizontal-space)
  562. (indent-to col)
  563. ;; Indent any comment following code on the same line.
  564. (if (re-search-forward comment-start-skip
  565. (save-excursion (end-of-line) (point)) t)
  566. (progn (goto-char (match-beginning 0))
  567. (if (not (= (current-column) (fortran-comment-hook)))
  568. (progn (delete-horizontal-space)
  569. (indent-to (fortran-comment-hook)))))))))
  570. (defun fortran-line-number-indented-correctly-p ()
  571. "Return t if current line's line number is correctly indente.
  572. Do not call if there is no line number."
  573. (save-excursion
  574. (beginning-of-line)
  575. (skip-chars-forward " \t")
  576. (and (<= (current-column) fortran-line-number-indent)
  577. (or (= (current-column) fortran-line-number-indent)
  578. (progn (skip-chars-forward "0-9")
  579. (= (current-column) 5))))))
  580. (defun fortran-check-for-matching-do ()
  581. "When called from a numbered statement, returns t
  582. if matching 'do' is found, and nil otherwise."
  583. (let (charnum
  584. (case-fold-search t))
  585. (save-excursion
  586. (beginning-of-line)
  587. (if (looking-at "[ \t]*[0-9]+")
  588. (progn
  589. (skip-chars-forward " \t")
  590. (skip-chars-forward "0") ;skip past leading zeros
  591. (setq charnum (buffer-substring (point)
  592. (progn (skip-chars-forward "0-9")
  593. (point))))
  594. (beginning-of-line)
  595. (and (re-search-backward
  596. (concat "\\(^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]\\)\\|\\(^[ \t0-9]*do[ \t]*0*"
  597. charnum "\\b\\)\\|\\(^[ \t]*0*" charnum "\\b\\)")
  598. nil t)
  599. (looking-at (concat "^[ \t0-9]*do[ \t]*0*" charnum))))))))