fortran.el 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209
  1. ;;; fortran.el --- Fortran mode for GNU Emacs
  2. ;; Copyright (C) 1986, 1993-1995, 1997-2012 Free Software Foundation, Inc.
  3. ;; Author: Michael D. Prange <prange@erl.mit.edu>
  4. ;; Maintainer: Glenn Morris <rgm@gnu.org>
  5. ;; Keywords: fortran, 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. ;; This mode is documented in the Emacs manual.
  19. ;;
  20. ;; Note that it is for editing Fortran77 or Fortran90 fixed source
  21. ;; form. For editing Fortran 90 free format source, use `f90-mode'
  22. ;; (f90.el). It is meant to support the GNU Fortran language
  23. ;; implemented by g77 (its extensions to Fortran77 and
  24. ;; interpretations, e.g. of backslash in strings).
  25. ;;; History:
  26. ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
  27. ;; We acknowledge many contributions and valuable suggestions by
  28. ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
  29. ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon,
  30. ;; Gary Sabot and Richard Stallman.
  31. ;;; Code:
  32. ;; Todo:
  33. ;; * Tidy it all up (more)!
  34. ;; * Implement insertion and removal of statement continuations in
  35. ;; mixed f77/f90 style, with the first `&' past column 72 and the
  36. ;; second in column 6.
  37. ;; * Support any other extensions to f77 grokked by GNU Fortran I've missed.
  38. ;; silence compiler
  39. (defvar dabbrev-case-fold-search)
  40. (defvar gud-find-expr-function)
  41. (defvar imenu-case-fold-search)
  42. (defvar imenu-syntax-alist)
  43. (defvar comment-region-function)
  44. (defvar uncomment-region-function)
  45. (defgroup fortran nil
  46. "Major mode for editing fixed format Fortran code."
  47. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  48. :link '(custom-manual "(emacs)Fortran")
  49. :group 'languages)
  50. (defgroup fortran-indent nil
  51. "Indentation variables in Fortran mode."
  52. :prefix "fortran-"
  53. :group 'fortran)
  54. (defgroup fortran-comment nil
  55. "Comment-handling variables in Fortran mode."
  56. :prefix "fortran-"
  57. :group 'fortran)
  58. (defcustom fortran-tab-mode-default nil
  59. "Default tabbing/carriage control style for empty files in Fortran mode.
  60. A non-nil value specifies tab-digit style of continuation control.
  61. A value of nil specifies that continuation lines are marked
  62. with a character in column 6."
  63. :type 'boolean
  64. :safe 'booleanp
  65. :group 'fortran-indent)
  66. ;; TODO add more detail of what tab mode is to doc string.
  67. (defcustom fortran-tab-mode-string
  68. (propertize "/t" 'help-echo "This buffer is in Fortran TAB mode"
  69. 'mouse-face 'mode-line-highlight
  70. 'local-map
  71. (make-mode-line-mouse-map 'mouse-1
  72. (lambda ()
  73. (interactive)
  74. (describe-variable
  75. 'fortran-tab-mode-string))))
  76. "String to appear in mode line in TAB format buffers.
  77. See Info node `(emacs)ForIndent Cont'."
  78. :type 'string
  79. :risky t
  80. :group 'fortran-indent)
  81. (defcustom fortran-do-indent 3
  82. "Extra indentation applied to DO blocks."
  83. :type 'integer
  84. :safe 'integerp
  85. :group 'fortran-indent)
  86. (defcustom fortran-if-indent 3
  87. "Extra indentation applied to IF, SELECT CASE and WHERE blocks."
  88. :type 'integer
  89. :safe 'integerp
  90. :group 'fortran-indent)
  91. (defcustom fortran-structure-indent 3
  92. "Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
  93. :type 'integer
  94. :safe 'integerp
  95. :group 'fortran-indent)
  96. (defcustom fortran-continuation-indent 5
  97. "Extra indentation applied to continuation lines."
  98. :type 'integer
  99. :safe 'integerp
  100. :group 'fortran-indent)
  101. (defcustom fortran-comment-indent-style 'fixed
  102. "How to indent comments.
  103. nil forces comment lines not to be touched;
  104. `fixed' indents to `fortran-comment-line-extra-indent' columns beyond
  105. `fortran-minimum-statement-indent-fixed' (if `indent-tabs-mode' nil), or
  106. `fortran-minimum-statement-indent-tab' (if `indent-tabs-mode' non-nil);
  107. `relative' indents to current Fortran indentation plus
  108. `fortran-comment-line-extra-indent'."
  109. :type '(radio (const :tag "Untouched" nil) (const fixed) (const relative))
  110. :safe (lambda (value) (memq value '(nil fixed relative)))
  111. :group 'fortran-indent)
  112. (defcustom fortran-comment-line-extra-indent 0
  113. "Amount of extra indentation for text within full-line comments."
  114. :type 'integer
  115. :safe 'integerp
  116. :group 'fortran-indent
  117. :group 'fortran-comment)
  118. (defcustom fortran-comment-line-start "C"
  119. "Delimiter inserted to start new full-line comment.
  120. You might want to change this to \"*\", for instance; or \"!\" to
  121. allow trailing comments on a line."
  122. :version "21.1"
  123. :type 'string
  124. :safe 'stringp
  125. :group 'fortran-comment)
  126. ;; This used to match preprocessor lines too, but that messes up
  127. ;; filling and doesn't seem to be necessary.
  128. (defcustom fortran-comment-line-start-skip
  129. "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*"
  130. "Regexp to match the start of a full-line comment."
  131. :version "21.1"
  132. :type 'regexp
  133. :safe 'stringp
  134. :group 'fortran-comment)
  135. (defcustom fortran-directive-re
  136. "^[ \t]*#.*"
  137. "Regexp to match a directive line.
  138. The matching text will be fontified with `font-lock-keyword-face'.
  139. The matching line will be given zero indentation."
  140. :version "22.1"
  141. :type 'regexp
  142. :safe 'stringp
  143. :group 'fortran-indent)
  144. (defcustom fortran-minimum-statement-indent-fixed 6
  145. "Minimum statement indentation for fixed format continuation style."
  146. :type 'integer
  147. :safe 'integerp
  148. :group 'fortran-indent)
  149. (defcustom fortran-minimum-statement-indent-tab (max tab-width 6)
  150. "Minimum statement indentation for TAB format continuation style."
  151. :type 'integer
  152. :safe 'integerp
  153. :group 'fortran-indent)
  154. ;; Note that this is documented in the v18 manuals as being a string
  155. ;; of length one rather than a single character.
  156. ;; The code in this file accepts either format for compatibility.
  157. (defcustom fortran-comment-indent-char " "
  158. "Single-character string inserted for Fortran comment indentation.
  159. Normally a space."
  160. :type 'string
  161. :safe (lambda (value) (or (characterp value)
  162. (and (stringp value) (= (length value) 1))))
  163. :group 'fortran-comment)
  164. (defcustom fortran-line-number-indent 1
  165. "Maximum indentation for Fortran line numbers.
  166. 5 means right-justify them within their five-column field."
  167. :type 'integer
  168. :safe 'integerp
  169. :group 'fortran-indent)
  170. (defcustom fortran-check-all-num-for-matching-do nil
  171. "Non-nil causes all numbered lines to be treated as possible DO loop ends."
  172. :type 'boolean
  173. :safe 'booleanp
  174. :group 'fortran)
  175. (defcustom fortran-blink-matching-if nil
  176. "Non-nil causes \\[fortran-indent-line] on ENDIF to blink on matching IF.
  177. Also, from an ENDDO statement blink on matching DO [WHILE] statement."
  178. :type 'boolean
  179. :safe 'booleanp
  180. :group 'fortran)
  181. (defcustom fortran-continuation-string "$"
  182. "Single-character string used for Fortran continuation lines.
  183. In fixed format continuation style, this character is inserted in
  184. column 6 by \\[fortran-split-line] to begin a continuation line.
  185. Also, if \\[fortran-indent-line] finds this at the beginning of a
  186. line, it will convert the line into a continuation line of the
  187. appropriate style. Normally \"$\"."
  188. :type 'string
  189. :safe (lambda (value) (and (stringp value) (= (length value) 1)))
  190. :group 'fortran)
  191. (defcustom fortran-comment-region "c$$$"
  192. "String inserted by \\[fortran-comment-region] at start of each \
  193. line in region."
  194. :type 'string
  195. :safe 'stringp
  196. :group 'fortran-comment)
  197. (defcustom fortran-electric-line-number t
  198. "Non-nil causes line numbers to be moved to the correct column as typed."
  199. :type 'boolean
  200. :safe 'booleanp
  201. :group 'fortran)
  202. ;; TODO use fortran-line-length, somehow.
  203. (defcustom fortran-column-ruler-fixed
  204. "0 4 6 10 20 30 40 5\
  205. 0 60 70\n\
  206. \[ ]|{ | | | | | | | | \
  207. \| | | | |}\n"
  208. "String displayed above current line by \\[fortran-column-ruler].
  209. This variable is used in fixed format mode.
  210. See the variable `fortran-column-ruler-tab' for TAB format mode."
  211. :type 'string
  212. :safe 'stringp
  213. :group 'fortran)
  214. ;; TODO use fortran-line-length, somehow.
  215. (defcustom fortran-column-ruler-tab
  216. "0 810 20 30 40 5\
  217. 0 60 70\n\
  218. \[ ]| { | | | | | | | | \
  219. \| | | | |}\n"
  220. "String displayed above current line by \\[fortran-column-ruler].
  221. This variable is used in TAB format mode.
  222. See the variable `fortran-column-ruler-fixed' for fixed format mode."
  223. :type 'string
  224. :safe 'stringp
  225. :group 'fortran)
  226. (defcustom fortran-analyze-depth 100
  227. "Number of lines to scan to identify fixed or TAB format style."
  228. :type 'integer
  229. :safe 'integerp
  230. :group 'fortran)
  231. (defcustom fortran-break-before-delimiters t
  232. "Non-nil causes filling to break lines before delimiters.
  233. Delimiters are characters matching the regexp `fortran-break-delimiters-re'."
  234. :type 'boolean
  235. :safe 'booleanp
  236. :group 'fortran)
  237. ;; TODO 0 as no-limit, as per g77.
  238. (defcustom fortran-line-length 72
  239. "Maximum number of characters in a line of fixed-form Fortran code.
  240. Characters beyond this point are treated as comments. Setting
  241. this variable directly (after fortran mode is loaded) does not
  242. take effect. Use either \\[customize] (which affects all Fortran
  243. buffers and the default) or the function
  244. `fortran-line-length' (which can also operate on just the current
  245. buffer). This corresponds to the g77 compiler option
  246. `-ffixed-line-length-N'."
  247. :type 'integer
  248. :safe 'integerp
  249. :initialize 'custom-initialize-default
  250. :set (lambda (_symbol value)
  251. ;; Do all fortran buffers, and the default.
  252. (fortran-line-length value t))
  253. :version "23.1"
  254. :group 'fortran)
  255. (make-variable-buffer-local 'fortran-line-length)
  256. (defcustom fortran-mode-hook nil
  257. "Hook run when entering Fortran mode."
  258. :type 'hook
  259. :group 'fortran)
  260. (defconst fortran-break-delimiters-re "[-+*/><=, \t]"
  261. "Regexp matching delimiter characters at which lines may be broken.
  262. There are certain tokens comprised entirely of characters
  263. matching this regexp that should not be split, and these are
  264. specified by the constant `fortran-no-break-re'.")
  265. ;; The ">=", etc F77 extensions are supported by g77.
  266. (defconst fortran-no-break-re
  267. (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=") 'paren)
  268. "Regexp specifying where not to break lines when filling.
  269. This regexp matches certain tokens comprised entirely of
  270. characters matching the regexp `fortran-break-delimiters-re' that should
  271. not be split by filling. Each element is assumed to be two
  272. characters long.")
  273. (defconst fortran-if-start-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*("
  274. "Regexp matching the start of an IF statement.")
  275. ;; Note fortran-current-defun uses the subgroups.
  276. (defconst fortran-start-prog-re
  277. "^[ \t]*\\(program\\|subroutine\\|function\
  278. \\|[ \ta-z0-9*()]*[ \t]+function\\|\
  279. \\(block[ \t]*data\\)\\)"
  280. "Regexp matching the start of a subprogram, from the line start.")
  281. (defconst fortran-end-prog-re1
  282. "end\
  283. \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
  284. \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?"
  285. "Regexp possibly matching the end of a subprogram.")
  286. (defconst fortran-end-prog-re
  287. (concat "^[ \t0-9]*" fortran-end-prog-re1)
  288. "Regexp possibly matching the end of a subprogram, from the line start.
  289. See also `fortran-end-prog-re1'.")
  290. (defconst fortran-type-types
  291. (concat "\\<"
  292. (mapconcat 'identity ; " " -> "[ \t]*"
  293. (split-string
  294. (regexp-opt
  295. (let ((simple-types
  296. '("character" "byte" "integer" "logical"
  297. "none" "real" "complex"
  298. "double precision" "double complex"))
  299. (structured-types '("structure" "union" "map"))
  300. (other-types '("record" "dimension"
  301. "parameter" "common" "save"
  302. "external" "intrinsic" "data"
  303. "equivalence")))
  304. (append
  305. (mapcar (lambda (x) (concat "implicit " x))
  306. simple-types)
  307. simple-types
  308. (mapcar (lambda (x) (concat "end " x))
  309. structured-types)
  310. structured-types
  311. other-types)) 'paren))
  312. "[ \t]*") "\\>")
  313. "Regexp matching Fortran types.")
  314. (defvar fortran-font-lock-keywords-1
  315. ;; Program, subroutine and function declarations, plus calls.
  316. '(("\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|\
  317. program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?"
  318. (1 font-lock-keyword-face)
  319. (2 font-lock-function-name-face nil t)))
  320. "Subdued level highlighting for Fortran mode.")
  321. (defvar fortran-font-lock-keywords-2
  322. (append fortran-font-lock-keywords-1
  323. (list
  324. ;; Fontify all type specifiers (must be first - see below).
  325. (cons fortran-type-types 'font-lock-type-face)
  326. ;; Builtin keywords (except logical, do and goto - see below).
  327. (concat "\\<" (regexp-opt
  328. '("continue" "format" "end" "enddo"
  329. "if" "then" "else" "endif" "elseif"
  330. "while" "inquire" "stop" "return"
  331. "include" "open" "close" "read"
  332. "write" "format" "print" "select" "case"
  333. "cycle" "exit" "rewind" "backspace"
  334. "where" "elsewhere")
  335. 'paren) "\\>")
  336. ;; Builtin operators.
  337. (concat "\\." (regexp-opt
  338. '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
  339. "neqv" "not" "or" "true")
  340. 'paren) "\\.")
  341. ;; do/goto keywords and targets, and goto tags.
  342. '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
  343. (1 font-lock-keyword-face)
  344. (2 font-lock-constant-face nil t))
  345. '("^ *\\([0-9]+\\)" . font-lock-constant-face)))
  346. "Medium level highlighting for Fortran mode.")
  347. ;; See bug#1385. Never really looked into _why_ this matters...
  348. (defun fortran-match-and-skip-declaration (limit)
  349. "Like `font-lock-match-c-style-declaration-item-and-skip-to-next'.
  350. The only difference is, it returns t in a case when the default returns nil."
  351. (when (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?")
  352. (when (and (match-end 2) (> (- (match-end 2) (match-beginning 2)) 1))
  353. (let ((pos (point)))
  354. (skip-chars-backward " \t\n")
  355. (skip-syntax-backward "w")
  356. (unless (looking-at "\\(\\sw+\\)[ \t\n]*\\sw+[ \t\n]*\\(((?\\)?")
  357. (goto-char pos)
  358. (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?"))))
  359. (save-match-data
  360. (condition-case nil
  361. (save-restriction
  362. (narrow-to-region (point-min) limit)
  363. (goto-char (match-end 1))
  364. (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)"))
  365. (goto-char (or (scan-sexps (point) 1) (point-max))))
  366. (goto-char (match-end 2)))
  367. (error t)))))
  368. (defvar fortran-font-lock-keywords-3
  369. (append
  370. fortran-font-lock-keywords-1
  371. ;; All type specifiers plus their declared items.
  372. (list
  373. (list (concat fortran-type-types "[ \t(/]*\\(*\\)?")
  374. ;; Type specifier.
  375. '(1 font-lock-type-face)
  376. ;; Declaration item (or just /.../ block name).
  377. `(fortran-match-and-skip-declaration
  378. ;; Start after any *(...) expression.
  379. (condition-case nil
  380. (and (match-beginning ,(1+ (regexp-opt-depth
  381. fortran-type-types)))
  382. (forward-sexp)
  383. (forward-sexp))
  384. (error nil))
  385. ;; No need to clean up.
  386. nil
  387. ;; Fontify as a variable name, functions fontified elsewhere.
  388. (1 font-lock-variable-name-face nil t))))
  389. ;; Things extra to `fortran-font-lock-keywords-3' (must be done first).
  390. (list
  391. ;; Goto-like `err=label'/`end=label' in read/write statements.
  392. '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?"
  393. (1 font-lock-keyword-face) (4 font-lock-constant-face nil t))
  394. ;; Standard continuation character and in a TAB-formatted line.
  395. '("^ \\{5\\}\\([^ 0\n]\\)" 1 font-lock-string-face)
  396. '("^\t\\([1-9]\\)" 1 font-lock-string-face))
  397. `((,fortran-directive-re (0 font-lock-keyword-face t)))
  398. ;; `fortran-font-lock-keywords-2' without types (see above).
  399. (cdr (nthcdr (length fortran-font-lock-keywords-1)
  400. fortran-font-lock-keywords-2)))
  401. "Gaudy level highlighting for Fortran mode.")
  402. (defvar fortran-font-lock-keywords-4
  403. (append fortran-font-lock-keywords-3
  404. (list (list
  405. (concat "\\<"
  406. (regexp-opt
  407. '("int" "ifix" "idint" "real" "float" "sngl"
  408. "dble" "cmplx" "ichar" "char" "aint" "dint"
  409. "anint" "dnint" "nint" "idnint" "iabs" "abs"
  410. "dabs" "cabs" "mod" "amod" "dmod" "isign"
  411. "sign" "dsign" "idim" "dim" "ddim" "dprod"
  412. "max" "max0" "amax1" "dmax1" "amax0" "max1"
  413. "min" "min0" "amin1" "dmin1" "amin0" "min1"
  414. "len" "index" "lge" "lgt" "lle" "llt" "aimag"
  415. "conjg" "sqrt" "dsqrt" "csqrt" "exp" "dexp"
  416. "cexp" "log" "alog" "dlog" "clog" "log10"
  417. "alog10" "dlog10" "sin" "dsin" "csin" "cos"
  418. "dcos" "ccos" "tan" "dtan" "asin" "dasin"
  419. "acos" "dacos" "atan" "datan" "atan2" "datan2"
  420. "sinh" "dsinh" "cosh" "dcosh" "tanh" "dtanh")
  421. 'paren) "[ \t]*(") '(1 font-lock-builtin-face))))
  422. "Maximum highlighting for Fortran mode.
  423. Consists of level 3 plus all other intrinsics not already highlighted.")
  424. ;; Comments are real pain in Fortran because there is no way to
  425. ;; represent the standard comment syntax in an Emacs syntax table.
  426. ;; (We can do so for F90-style). Therefore an unmatched quote in a
  427. ;; standard comment will throw fontification off on the wrong track.
  428. ;; So we do syntactic fontification with regexps.
  429. (defun fortran-make-syntax-propertize-function (line-length)
  430. "Return a value for `syntax-propertize-function' in Fortran mode.
  431. This varies according to the value of LINE-LENGTH.
  432. This is used to fontify fixed-format Fortran comments."
  433. ;; This results in a non-byte-compiled function. We could pass it through
  434. ;; `byte-compile', but simple benchmarks indicate that it's probably not
  435. ;; worth the trouble (about 0.5% of slow down).
  436. (eval ;I hate `eval', but it's hard to avoid it here.
  437. `(syntax-propertize-rules
  438. ("^[cd\\*]" (0 "<"))
  439. ;; We mark all chars after line-length as "comment-start", rather than
  440. ;; just the first one. This is so that a closing ' that's past the
  441. ;; line-length will indeed be ignored (and will result in a string that
  442. ;; leaks into subsequent lines).
  443. ((format "^[^cd\\*\t\n].\\{%d\\}\\(.+\\)" (1- line-length))
  444. (1 "<")))))
  445. (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
  446. "Default expressions to highlight in Fortran mode.")
  447. (defvar fortran-imenu-generic-expression
  448. ;; These patterns could be confused by sequence nos. in cols 72+ and
  449. ;; don't allow continuations everywhere.
  450. (list
  451. (list
  452. nil
  453. ;; [This will be fooled by `end function' allowed by G77. Also,
  454. ;; it assumes sensible whitespace is employed.]
  455. (concat
  456. ;; leading whitespace:
  457. "^\\s-+\\("
  458. ;; function declaration with optional type, e.g. `real',
  459. ;; `real*4', character(*), `double precision':
  460. "\\(\\sw\\|\\s-\\|[*()+]\\)*"
  461. "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
  462. ;; Possible statement continuation:
  463. "[ \t" fortran-continuation-string "]+"
  464. ;; Variable to index:
  465. "\\(\\sw+\\)")
  466. 3)
  467. ;; Un-named block data.
  468. '(nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
  469. "Value for `imenu-generic-expression' in Fortran mode.")
  470. ;; Hideshow support.
  471. (defconst fortran-blocks-re
  472. (concat "block[ \t]*data\\|select[ \t]*case\\|"
  473. (regexp-opt '("do" "if" "interface" "function" "map" "program"
  474. "structure" "subroutine" "union" "where")))
  475. "Regexp potentially indicating the start or end of a Fortran \"block\".
  476. Omits naked END statements, and DO-loops closed by anything other
  477. than ENDDO.")
  478. (defconst fortran-end-block-re
  479. ;; Do-loops terminated by things other than ENDDO cannot be handled
  480. ;; with a regexp. This omission does not seem to matter to hideshow...
  481. (concat "^[ \t0-9]*\\<end[ \t]*\\("
  482. fortran-blocks-re
  483. ;; Naked END statement.
  484. "\\|!\\|$\\)")
  485. "Regexp matching the end of a Fortran \"block\", from the line start.
  486. Note that only ENDDO is handled for the end of a DO-loop. Used
  487. in the Fortran entry in `hs-special-modes-alist'.")
  488. (defconst fortran-start-block-re
  489. (concat
  490. "^[ \t0-9]*\\(" ; statement number
  491. ;; Structure label for DO, IF, SELECT, WHERE.
  492. "\\(\\(\\sw+[ \t]*:[ \t]*\\)?"
  493. ;; IF blocks are a nuisance:
  494. ;; IF ( ... ) foo is not a block, but a single statement.
  495. ;; IF ( ... ) THEN can be split over multiple lines.
  496. ;; [So can, eg, a DO WHILE (... ), but that is less common, I hope.]
  497. ;; The regexp below allows for it to be split over at most 2 lines.
  498. ;; That leads to the problem of not matching two consecutive IF
  499. ;; statements as one, eg:
  500. ;; IF ( ... ) foo
  501. ;; IF ( ... ) THEN
  502. ;; It simply is not possible to do this in a 100% correct fashion
  503. ;; using a regexp - see the functions fortran-end-if,
  504. ;; fortran-beginning-if for the hoops we have to go through.
  505. ;; An alternative is to match on THEN at a line end, eg:
  506. ;; ".*)[ \t]*then[ \t]*\\($\\|!\\)"
  507. ;; This would also match ELSE branches, though. This does not seem
  508. ;; right to me, because then one has neighboring blocks that are
  509. ;; not nested in each other.
  510. "\\(if[ \t]*(\\(.*\\|"
  511. ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|"
  512. "do\\|select[ \t]*case\\|where\\)\\)\\|"
  513. (regexp-opt '("interface" "function" "map" "program"
  514. "structure" "subroutine" "union"))
  515. "\\|block[ \t]*data\\)[ \t]*")
  516. "Regexp matching the start of a Fortran \"block\", from the line start.
  517. A simple regexp cannot do this in fully correct fashion, so this
  518. tries to strike a compromise between complexity and flexibility.
  519. Used in the Fortran entry in `hs-special-modes-alist'.")
  520. (add-to-list 'hs-special-modes-alist
  521. `(fortran-mode ,fortran-start-block-re ,fortran-end-block-re
  522. "^[cC*!]" fortran-end-of-block nil))
  523. (defvar fortran-mode-syntax-table
  524. (let ((table (make-syntax-table)))
  525. ;; Was a word-constituent (for abbrevs), now punctuation (g77
  526. ;; multi-statement lines).
  527. (modify-syntax-entry ?\; "." table)
  528. (modify-syntax-entry ?\r " " table)
  529. (modify-syntax-entry ?+ "." table)
  530. (modify-syntax-entry ?- "." table)
  531. (modify-syntax-entry ?= "." table)
  532. (modify-syntax-entry ?* "." table)
  533. (modify-syntax-entry ?/ "." table)
  534. (modify-syntax-entry ?% "." table) ; bug#8820
  535. (modify-syntax-entry ?\' "\"" table)
  536. (modify-syntax-entry ?\" "\"" table)
  537. ;; Consistent with GNU Fortran's default -- see the manual.
  538. ;; The F77 standard imposes no rule on this issue.
  539. (modify-syntax-entry ?\\ "\\" table)
  540. ;; This might be better as punctuation, as for C, but this way you
  541. ;; can treat floating-point numbers as symbols.
  542. (modify-syntax-entry ?. "_" table) ; e.g. `a.ne.b'
  543. (modify-syntax-entry ?_ "_" table)
  544. (modify-syntax-entry ?$ "_" table) ; esp. VMSisms
  545. (modify-syntax-entry ?\! "<" table)
  546. (modify-syntax-entry ?\n ">" table)
  547. table)
  548. "Syntax table used in Fortran mode.")
  549. (defvar fortran-gud-syntax-table
  550. (let ((st (make-syntax-table fortran-mode-syntax-table)))
  551. (modify-syntax-entry ?\n "." st)
  552. st)
  553. "Syntax table used to parse Fortran expressions for printing in GUD.")
  554. (defvar fortran-mode-map
  555. (let ((map (make-sparse-keymap)))
  556. (define-key map ";" 'fortran-abbrev-start)
  557. (define-key map "\C-c;" 'fortran-comment-region)
  558. ;; The default comment-dwim does at least as much as this.
  559. ;;; (define-key map "\M-;" 'fortran-indent-comment)
  560. (define-key map "\M-\n" 'fortran-split-line)
  561. (define-key map "\M-\C-n" 'fortran-end-of-block)
  562. (define-key map "\M-\C-p" 'fortran-beginning-of-block)
  563. (define-key map "\M-\C-q" 'fortran-indent-subprogram)
  564. (define-key map "\C-c\C-w" 'fortran-window-create-momentarily)
  565. (define-key map "\C-c\C-r" 'fortran-column-ruler)
  566. (define-key map "\C-c\C-p" 'fortran-previous-statement)
  567. (define-key map "\C-c\C-n" 'fortran-next-statement)
  568. (define-key map "\C-c\C-d" 'fortran-join-line) ; like f90
  569. (define-key map "\M-^" 'fortran-join-line) ; subvert delete-indentation
  570. (define-key map "0" 'fortran-electric-line-number)
  571. (define-key map "1" 'fortran-electric-line-number)
  572. (define-key map "2" 'fortran-electric-line-number)
  573. (define-key map "3" 'fortran-electric-line-number)
  574. (define-key map "4" 'fortran-electric-line-number)
  575. (define-key map "5" 'fortran-electric-line-number)
  576. (define-key map "6" 'fortran-electric-line-number)
  577. (define-key map "7" 'fortran-electric-line-number)
  578. (define-key map "8" 'fortran-electric-line-number)
  579. (define-key map "9" 'fortran-electric-line-number)
  580. (easy-menu-define fortran-menu map "Menu for Fortran mode."
  581. `("Fortran"
  582. ["Manual" (info "(emacs)Fortran") :active t
  583. :help "Read the Emacs manual chapter on Fortran mode"]
  584. ("Customization"
  585. ,(custom-menu-create 'fortran)
  586. ;; FIXME useless?
  587. ["Set" Custom-set :active t
  588. :help "Set current value of all edited settings in the buffer"]
  589. ["Save" Custom-save :active t
  590. :help "Set and save all edited settings"]
  591. ["Reset to Current" Custom-reset-current :active t
  592. :help "Reset all edited settings to current"]
  593. ["Reset to Saved" Custom-reset-saved :active t
  594. :help "Reset all edited or set settings to saved"]
  595. ["Reset to Standard Settings" Custom-reset-standard :active t
  596. :help "Erase all customizations in buffer"]
  597. )
  598. "--"
  599. ["Comment Region" fortran-comment-region mark-active]
  600. ["Uncomment Region"
  601. (fortran-comment-region (region-beginning) (region-end) 1)
  602. mark-active]
  603. ["Indent Region" indent-region mark-active]
  604. ["Indent Subprogram" fortran-indent-subprogram t]
  605. "--"
  606. ["Beginning of Subprogram" fortran-beginning-of-subprogram :active t
  607. :help "Move point to the start of the current subprogram"]
  608. ["End of Subprogram" fortran-end-of-subprogram :active t
  609. :help "Move point to the end of the current subprogram"]
  610. ("Mark"
  611. :help "Mark a region of code"
  612. ["Subprogram" mark-defun t]
  613. ["IF Block" fortran-mark-if t]
  614. ["DO Block" fortran-mark-do t]
  615. )
  616. ["Narrow to Subprogram" narrow-to-defun t]
  617. ["Widen" widen t]
  618. "--"
  619. ["Temporary Column Ruler" fortran-column-ruler :active t
  620. :help "Briefly display Fortran column numbers"]
  621. ;; May not be '72', depending on fortran-line-length, but this
  622. ;; seems ok for a menu item.
  623. ["72-column Window" fortran-window-create :active t
  624. :help "Set window width to Fortran line length"]
  625. ["Full Width Window"
  626. (enlarge-window-horizontally (- (frame-width) (window-width)))
  627. :active (not (window-full-width-p))
  628. :help "Make window full width"]
  629. ["Momentary 72-Column Window" fortran-window-create-momentarily
  630. :active t :help "Briefly set window width to Fortran line length"]
  631. "--"
  632. ["Break Line at Point" fortran-split-line :active t
  633. :help "Break the current line at point"]
  634. ["Join Line" fortran-join-line :active t
  635. :help "Join the current line to the previous one"]
  636. ["Fill Statement/Comment" fill-paragraph t]
  637. "--"
  638. ["Toggle Auto Fill" auto-fill-mode :selected auto-fill-function
  639. :style toggle
  640. :help "Automatically fill text while typing in this buffer"]
  641. ["Toggle Abbrev Mode" abbrev-mode :selected abbrev-mode
  642. :style toggle :help "Expand abbreviations while typing in this buffer"]
  643. ["Add Imenu Menu" imenu-add-menubar-index
  644. :active (not (lookup-key (current-local-map) [menu-bar index]))
  645. :included (fboundp 'imenu-add-to-menubar)
  646. :help "Add an index menu to the menu-bar"]))
  647. map)
  648. "Keymap used in Fortran mode.")
  649. (define-abbrev-table 'fortran-mode-abbrev-table
  650. (mapcar (lambda (e) (list (car e) (cdr e) nil :system t))
  651. '((";au" . "automatic" )
  652. (";b" . "byte" )
  653. (";bd" . "block data" )
  654. (";ch" . "character" )
  655. (";cl" . "close" )
  656. (";c" . "continue" )
  657. (";cm" . "common" )
  658. (";cx" . "complex" )
  659. (";df" . "define" )
  660. (";di" . "dimension" )
  661. (";do" . "double" )
  662. (";dc" . "double complex" )
  663. (";dp" . "double precision" )
  664. (";dw" . "do while" )
  665. (";e" . "else" )
  666. (";ed" . "enddo" )
  667. (";el" . "elseif" )
  668. (";en" . "endif" )
  669. (";eq" . "equivalence" )
  670. (";ew" . "endwhere" )
  671. (";ex" . "external" )
  672. (";ey" . "entry" )
  673. (";f" . "format" )
  674. (";fa" . ".false." )
  675. (";fu" . "function" )
  676. (";g" . "goto" )
  677. (";im" . "implicit" )
  678. (";ib" . "implicit byte" )
  679. (";ic" . "implicit complex" )
  680. (";ich" . "implicit character")
  681. (";ii" . "implicit integer" )
  682. (";il" . "implicit logical" )
  683. (";ir" . "implicit real" )
  684. (";inc" . "include" )
  685. (";in" . "integer" )
  686. (";intr" . "intrinsic" )
  687. (";l" . "logical" )
  688. (";n" . "namelist" )
  689. (";o" . "open" ) ; was ;op
  690. (";pa" . "parameter" )
  691. (";pr" . "program" )
  692. (";ps" . "pause" )
  693. (";p" . "print" )
  694. (";rc" . "record" )
  695. (";re" . "real" )
  696. (";r" . "read" )
  697. (";rt" . "return" )
  698. (";rw" . "rewind" )
  699. (";s" . "stop" )
  700. (";sa" . "save" )
  701. (";st" . "structure" )
  702. (";sc" . "static" )
  703. (";su" . "subroutine" )
  704. (";tr" . ".true." )
  705. (";ty" . "type" )
  706. (";vo" . "volatile" )
  707. (";w" . "write" )
  708. (";wh" . "where" )))
  709. "Abbrev table for Fortran mode."
  710. ;; Accept ; as the first char of an abbrev. Also allow _ in abbrevs.
  711. :regexp "\\(?:[^[:word:]_;]\\|^\\)\\(;?[[:word:]_]+\\)[^[:word:]_]*")
  712. ;;;###autoload
  713. (define-derived-mode fortran-mode prog-mode "Fortran"
  714. "Major mode for editing Fortran code in fixed format.
  715. For free format code, use `f90-mode'.
  716. \\[fortran-indent-line] indents the current Fortran line correctly.
  717. Note that DO statements must not share a common CONTINUE.
  718. Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\
  719. Fortran keywords.
  720. Key definitions:
  721. \\{fortran-mode-map}
  722. Variables controlling indentation style and extra features:
  723. `fortran-comment-line-start'
  724. To use comments starting with `!', set this to the string \"!\".
  725. `fortran-do-indent'
  726. Extra indentation within DO blocks (default 3).
  727. `fortran-if-indent'
  728. Extra indentation within IF blocks (default 3).
  729. `fortran-structure-indent'
  730. Extra indentation within STRUCTURE, UNION, MAP and INTERFACE blocks.
  731. (default 3)
  732. `fortran-continuation-indent'
  733. Extra indentation applied to continuation statements (default 5).
  734. `fortran-comment-line-extra-indent'
  735. Amount of extra indentation for text in full-line comments (default 0).
  736. `fortran-comment-indent-style'
  737. How to indent the text in full-line comments. Allowed values are:
  738. nil don't change the indentation
  739. fixed indent to `fortran-comment-line-extra-indent' beyond the
  740. value of either
  741. `fortran-minimum-statement-indent-fixed' (fixed format) or
  742. `fortran-minimum-statement-indent-tab' (TAB format),
  743. depending on the continuation format in use.
  744. relative indent to `fortran-comment-line-extra-indent' beyond the
  745. indentation for a line of code.
  746. (default 'fixed)
  747. `fortran-comment-indent-char'
  748. Single-character string to be inserted instead of space for
  749. full-line comment indentation (default \" \").
  750. `fortran-minimum-statement-indent-fixed'
  751. Minimum indentation for statements in fixed format mode (default 6).
  752. `fortran-minimum-statement-indent-tab'
  753. Minimum indentation for statements in TAB format mode (default 9).
  754. `fortran-line-number-indent'
  755. Maximum indentation for line numbers (default 1). A line number will
  756. get less than this much indentation if necessary to avoid reaching
  757. column 5.
  758. `fortran-check-all-num-for-matching-do'
  759. Non-nil causes all numbered lines to be treated as possible \"continue\"
  760. statements (default nil).
  761. `fortran-blink-matching-if'
  762. Non-nil causes \\[fortran-indent-line] on an ENDIF (or ENDDO) statement
  763. to blink on the matching IF (or DO [WHILE]). (default nil)
  764. `fortran-continuation-string'
  765. Single-character string to be inserted in column 5 of a continuation
  766. line (default \"$\").
  767. `fortran-comment-region'
  768. String inserted by \\[fortran-comment-region] at start of each line in
  769. the region (default \"c$$$\").
  770. `fortran-electric-line-number'
  771. Non-nil causes line number digits to be moved to the correct column
  772. as typed (default t).
  773. `fortran-break-before-delimiters'
  774. Non-nil causes lines to be broken before delimiters (default t).
  775. Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
  776. with no args, if that value is non-nil."
  777. :group 'fortran
  778. :syntax-table fortran-mode-syntax-table
  779. :abbrev-table fortran-mode-abbrev-table
  780. (set (make-local-variable 'indent-line-function) 'fortran-indent-line)
  781. (set (make-local-variable 'indent-region-function)
  782. (lambda (start end)
  783. (let (fortran-blink-matching-if ; avoid blinking delay
  784. indent-region-function)
  785. (indent-region start end nil))))
  786. (set (make-local-variable 'require-final-newline) mode-require-final-newline)
  787. ;; The syntax tables don't understand the column-0 comment-markers.
  788. (set (make-local-variable 'comment-use-syntax) nil)
  789. (set (make-local-variable 'comment-padding) "$$$")
  790. (set (make-local-variable 'comment-start) fortran-comment-line-start)
  791. (set (make-local-variable 'comment-start-skip)
  792. ;; We can't reuse `fortran-comment-line-start-skip' directly because
  793. ;; it contains backrefs whereas we need submatch-1 to end at the
  794. ;; beginning of the comment delimiter.
  795. ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
  796. "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
  797. (set (make-local-variable 'comment-indent-function) 'fortran-comment-indent)
  798. (set (make-local-variable 'comment-region-function) 'fortran-comment-region)
  799. (set (make-local-variable 'uncomment-region-function)
  800. 'fortran-uncomment-region)
  801. (set (make-local-variable 'comment-insert-comment-function)
  802. 'fortran-indent-comment)
  803. (set (make-local-variable 'abbrev-all-caps) t)
  804. (set (make-local-variable 'normal-auto-fill-function) 'fortran-auto-fill)
  805. (set (make-local-variable 'indent-tabs-mode) (fortran-analyze-file-format))
  806. (setq mode-line-process '(indent-tabs-mode fortran-tab-mode-string))
  807. (set (make-local-variable 'fill-column) fortran-line-length)
  808. (set (make-local-variable 'fill-paragraph-function) 'fortran-fill-paragraph)
  809. (set (make-local-variable 'font-lock-defaults)
  810. '((fortran-font-lock-keywords
  811. fortran-font-lock-keywords-1
  812. fortran-font-lock-keywords-2
  813. fortran-font-lock-keywords-3
  814. fortran-font-lock-keywords-4)
  815. nil t ((?/ . "$/") ("_$" . "w"))
  816. fortran-beginning-of-subprogram))
  817. (set (make-local-variable 'syntax-propertize-function)
  818. (fortran-make-syntax-propertize-function fortran-line-length))
  819. (set (make-local-variable 'imenu-case-fold-search) t)
  820. (set (make-local-variable 'imenu-generic-expression)
  821. fortran-imenu-generic-expression)
  822. (set (make-local-variable 'imenu-syntax-alist) '(("_$" . "w")))
  823. (set (make-local-variable 'beginning-of-defun-function)
  824. #'fortran-beginning-of-subprogram)
  825. (set (make-local-variable 'end-of-defun-function)
  826. #'fortran-end-of-subprogram)
  827. (set (make-local-variable 'add-log-current-defun-function)
  828. #'fortran-current-defun)
  829. (set (make-local-variable 'dabbrev-case-fold-search) 'case-fold-search)
  830. (set (make-local-variable 'gud-find-expr-function) 'fortran-gud-find-expr)
  831. (add-hook 'hack-local-variables-hook 'fortran-hack-local-variables nil t))
  832. (defun fortran-line-length (nchars &optional global)
  833. "Set the length of fixed-form Fortran lines to NCHARS.
  834. This normally only affects the current buffer, which must be in
  835. Fortran mode. If the optional argument GLOBAL is non-nil, it
  836. affects all Fortran buffers, and also the default.
  837. If a numeric prefix argument is specified, it will be used as NCHARS,
  838. otherwise is a non-numeric prefix arg is specified, the length will be
  839. provided via the minibuffer, and otherwise the current column is used."
  840. (interactive
  841. (list (cond
  842. ((numberp current-prefix-arg) current-prefix-arg)
  843. (current-prefix-arg
  844. (read-number "Line length: " (default-value 'fortran-line-length)))
  845. (t (current-column)))))
  846. (dolist (buff (if global
  847. (buffer-list)
  848. (list (current-buffer))))
  849. (with-current-buffer buff
  850. (when (derived-mode-p 'fortran-mode)
  851. (unless (eq fortran-line-length nchars)
  852. (setq fortran-line-length nchars
  853. fill-column fortran-line-length
  854. syntax-propertize-function
  855. (fortran-make-syntax-propertize-function nchars))
  856. (syntax-ppss-flush-cache (point-min))
  857. (if font-lock-mode (font-lock-mode 1))))))
  858. (if global
  859. (setq-default fortran-line-length nchars)))
  860. (defun fortran-hack-local-variables ()
  861. "Fortran mode adds this to `hack-local-variables-hook'."
  862. (fortran-line-length fortran-line-length))
  863. (declare-function gud-find-c-expr "gud.el" nil)
  864. (defun fortran-gud-find-expr ()
  865. ;; Consider \n as punctuation (end of expression).
  866. (with-syntax-table fortran-gud-syntax-table
  867. (gud-find-c-expr)))
  868. (defsubst fortran-comment-indent ()
  869. "Return the indentation appropriate for the current comment line.
  870. This is 0 for a line matching `fortran-comment-line-start-skip', else
  871. the value of `comment-column' (leaving at least one space after code)."
  872. (if (looking-at fortran-comment-line-start-skip) 0
  873. (save-excursion
  874. (skip-chars-backward " \t")
  875. (max (1+ (current-column)) comment-column))))
  876. (defun fortran-indent-comment ()
  877. "Align or create comment on current line.
  878. Existing comments of all types are recognized and aligned.
  879. If the line has no comment, a side-by-side comment is inserted and aligned,
  880. if the value of `comment-start' is not nil and allows such comments.
  881. Otherwise, a separate-line comment is inserted, on this line
  882. or on a new line inserted before this line if this line is not blank."
  883. (interactive "*")
  884. (beginning-of-line)
  885. ;; Recognize existing comments of either kind.
  886. (cond ((fortran-find-comment-start-skip 'all)
  887. (goto-char (match-beginning 0))
  888. (if (bolp)
  889. (fortran-indent-line)
  890. (unless (= (current-column) (fortran-comment-indent))
  891. (delete-horizontal-space)
  892. (indent-to (fortran-comment-indent)))))
  893. ;; No existing comment.
  894. ;; If side-by-side comments are defined, insert one,
  895. ;; unless line is now blank.
  896. ((and comment-start (not (looking-at "[ \t]*$"))
  897. (string-match comment-start-skip (concat " " comment-start)))
  898. (end-of-line)
  899. (delete-horizontal-space)
  900. (indent-to (fortran-comment-indent))
  901. (insert comment-start))
  902. ;; Else insert separate-line comment, making a new line if nec.
  903. (t
  904. (if (looking-at "^[ \t]*$")
  905. (delete-horizontal-space)
  906. (beginning-of-line)
  907. (insert ?\n)
  908. (forward-char -1))
  909. (insert fortran-comment-line-start)
  910. (insert-char (if (stringp fortran-comment-indent-char)
  911. (aref fortran-comment-indent-char 0)
  912. fortran-comment-indent-char)
  913. (- (fortran-calculate-indent) (current-column))))))
  914. (defun fortran-comment-region (beg-region end-region arg)
  915. "Comment every line in the region.
  916. Inserts the string variable `fortran-comment-region' at the beginning of
  917. every line in the region.
  918. BEG-REGION and END-REGION specify the region boundaries.
  919. With non-nil ARG, uncomments the region."
  920. (interactive "*r\nP")
  921. (let ((end-region-mark (copy-marker end-region))
  922. (save-point (point-marker)))
  923. (goto-char beg-region)
  924. (beginning-of-line)
  925. (if arg
  926. (let ((com (regexp-quote fortran-comment-region))) ; uncomment
  927. (if (looking-at com)
  928. (delete-region (point) (match-end 0)))
  929. (while (and (zerop (forward-line 1))
  930. (< (point) end-region-mark))
  931. (if (looking-at com)
  932. (delete-region (point) (match-end 0)))))
  933. (insert fortran-comment-region) ; comment
  934. (while (and (zerop (forward-line 1))
  935. (< (point) end-region-mark))
  936. (insert fortran-comment-region)))
  937. (goto-char save-point)
  938. (set-marker end-region-mark nil)
  939. (set-marker save-point nil)))
  940. ;; uncomment-region calls this with 3 args.
  941. (defun fortran-uncomment-region (start end &optional ignored)
  942. "Uncomment every line in the region."
  943. (fortran-comment-region start end t))
  944. (defun fortran-abbrev-start ()
  945. "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
  946. Any other key combination is executed normally."
  947. (interactive "*")
  948. (insert last-command-event)
  949. (let* ((event (if (fboundp 'next-command-event) ; XEmacs
  950. (next-command-event)
  951. (read-event)))
  952. (char (if (fboundp 'event-to-character)
  953. (event-to-character event) event)))
  954. ;; Insert char if not equal to `?', or if abbrev-mode is off.
  955. (if (and abbrev-mode (or (eq char ??) (eq char help-char)
  956. (memq event help-event-list)))
  957. (fortran-abbrev-help)
  958. (push event unread-command-events))))
  959. (defun fortran-abbrev-help ()
  960. "List the currently defined abbrevs in Fortran mode."
  961. (interactive)
  962. (message "Listing abbrev table...")
  963. (display-buffer (fortran-prepare-abbrev-list-buffer))
  964. (message "Listing abbrev table...done"))
  965. (defun fortran-prepare-abbrev-list-buffer ()
  966. "Create a buffer listing the Fortran mode abbreviations."
  967. (with-current-buffer (get-buffer-create "*Abbrevs*")
  968. (erase-buffer)
  969. (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
  970. (goto-char (point-min))
  971. (set-buffer-modified-p nil)
  972. (edit-abbrevs-mode))
  973. (get-buffer-create "*Abbrevs*"))
  974. (defun fortran-column-ruler ()
  975. "Insert a column ruler momentarily above current line, till next keystroke.
  976. The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
  977. format mode, and `fortran-column-ruler-tab' in TAB format mode.
  978. The next key typed is executed unless it is SPC."
  979. (interactive)
  980. (momentary-string-display
  981. (if indent-tabs-mode
  982. fortran-column-ruler-tab
  983. fortran-column-ruler-fixed)
  984. (save-excursion
  985. (beginning-of-line)
  986. (if (eq (window-start (selected-window))
  987. (window-point (selected-window)))
  988. (line-beginning-position 2)
  989. (point)))
  990. nil "Type SPC or any command to erase ruler."))
  991. (defun fortran-window-create ()
  992. "Make the window `fortran-line-length' (default 72) columns wide.
  993. See also `fortran-window-create-momentarily'."
  994. (interactive)
  995. (let ((window-min-width 2))
  996. (unless (window-full-width-p)
  997. (enlarge-window-horizontally (- (frame-width)
  998. (window-width) 1)))
  999. (let* ((window-edges (window-edges))
  1000. (scroll-bar-width (- (nth 2 window-edges)
  1001. (car window-edges)
  1002. (window-width))))
  1003. (split-window-right (+ fortran-line-length scroll-bar-width)))
  1004. (other-window 1)
  1005. (switch-to-buffer " fortran-window-extra" t)
  1006. (select-window (previous-window))))
  1007. (defun fortran-window-create-momentarily (&optional arg)
  1008. "Momentarily make the window `fortran-line-length' (default 72) columns wide.
  1009. Optional ARG non-nil and non-unity disables the momentary feature.
  1010. See also `fortran-window-create'."
  1011. (interactive "p")
  1012. (if (or (not arg)
  1013. (= arg 1))
  1014. (save-window-excursion
  1015. (progn
  1016. (condition-case nil
  1017. (fortran-window-create)
  1018. (error (error "No room for Fortran window")))
  1019. (message "Type SPC to continue editing.")
  1020. (let ((char (read-event)))
  1021. (or (equal char ?\s)
  1022. (setq unread-command-events (list char))))))
  1023. (fortran-window-create)))
  1024. (defun fortran-split-line ()
  1025. "Break line at point and insert continuation marker and alignment."
  1026. (interactive "*")
  1027. (delete-horizontal-space)
  1028. (if (save-excursion
  1029. (let ((pos (point)))
  1030. (beginning-of-line)
  1031. (and (fortran-find-comment-start-skip 'all)
  1032. (< (match-beginning 0) pos))))
  1033. (insert ?\n (match-string 0))
  1034. (if indent-tabs-mode
  1035. (insert ?\n ?\t (fortran-numerical-continuation-char))
  1036. (insert "\n " fortran-continuation-string))) ; space after \n important
  1037. (fortran-indent-line)) ; when cont string is C, c or *
  1038. (defun fortran-remove-continuation ()
  1039. "Delete any Fortran continuation characters at point.
  1040. Returns t if anything actually deleted."
  1041. (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
  1042. (replace-match "")
  1043. (delete-indentation)
  1044. t))
  1045. (defun fortran-join-line (arg)
  1046. "Join current line to the previous one and re-indent.
  1047. With a prefix argument, repeat this operation that many times.
  1048. If the prefix argument ARG is negative, join the next -ARG lines.
  1049. Continuation lines are correctly handled."
  1050. (interactive "*p")
  1051. (save-excursion
  1052. (when (> 0 arg)
  1053. (setq arg (- arg))
  1054. (forward-line arg))
  1055. (while (not (zerop arg))
  1056. (beginning-of-line)
  1057. (or (fortran-remove-continuation)
  1058. (delete-indentation))
  1059. (setq arg (1- arg)))
  1060. (fortran-indent-line)))
  1061. (defun fortran-numerical-continuation-char ()
  1062. "Return a digit for tab-digit style of continuation lines.
  1063. If previous line is a tab-digit continuation line, return that digit
  1064. plus one, otherwise return 1. Zero not allowed."
  1065. (save-excursion
  1066. (forward-line -1)
  1067. (if (looking-at "\t[1-9]")
  1068. (+ ?1 (% (- (char-after (1+ (point))) ?0) 9))
  1069. ?1)))
  1070. (put 'fortran-electric-line-number 'delete-selection t)
  1071. (defun fortran-electric-line-number (arg)
  1072. "Self insert, but if part of a Fortran line number indent it automatically.
  1073. Auto-indent does not happen if a numeric ARG is used."
  1074. (interactive "*P")
  1075. (if (or arg (not fortran-electric-line-number))
  1076. (if arg
  1077. (self-insert-command (prefix-numeric-value arg))
  1078. (self-insert-command 1))
  1079. (if (or (and (= 5 (current-column))
  1080. (save-excursion
  1081. (beginning-of-line)
  1082. ;; In col 5 with only spaces to the left.
  1083. (looking-at " \\{5\\}")))
  1084. (and (= (if indent-tabs-mode
  1085. fortran-minimum-statement-indent-tab
  1086. fortran-minimum-statement-indent-fixed) (current-column))
  1087. ;; In col 8 with a single tab to the left.
  1088. (eq ?\t (char-after (line-beginning-position)))
  1089. (not (or (eq last-command 'fortran-indent-line)
  1090. (eq last-command
  1091. 'fortran-indent-new-line))))
  1092. (save-excursion
  1093. (re-search-backward "[^ \t0-9]"
  1094. (line-beginning-position)
  1095. t)) ; not a line number
  1096. (looking-at "[0-9]")) ; within a line number
  1097. (self-insert-command (prefix-numeric-value arg))
  1098. (skip-chars-backward " \t")
  1099. (insert last-command-event)
  1100. (fortran-indent-line))))
  1101. (defun fortran-check-end-prog-re ()
  1102. "Check a preliminary match against `fortran-end-prog-re'."
  1103. ;; Having got a possible match for the subprogram end, we need a
  1104. ;; match of whitespace, avoiding possible column 73+ stuff.
  1105. (save-match-data
  1106. (string-match "^\\s-*\\(\\'\\|\\s<\\)"
  1107. (buffer-substring (match-end 0)
  1108. (min (line-end-position)
  1109. (+ fortran-line-length
  1110. (line-beginning-position)))))))
  1111. ;; This is more complex than first expected because the beginning of a
  1112. ;; main program may be implicit (ie not marked by a PROGRAM statement).
  1113. ;; This would be fine (we could just go to bob in the absence of a match),
  1114. ;; except it need not even be the first subprogram in the file (eg it
  1115. ;; could follow a subroutine). Hence we have to search for END
  1116. ;; statements instead.
  1117. ;; cf fortran-beginning-of-block, f90-beginning-of-subprogram
  1118. ;; Note that unlike the latter, we don't have to worry about nested
  1119. ;; subprograms (?).
  1120. ;; FIXME push-mark?
  1121. (defun fortran-beginning-of-subprogram ()
  1122. "Move point to the beginning of the current Fortran subprogram."
  1123. (interactive)
  1124. (let ((case-fold-search t))
  1125. ;; If called already at the start of subprogram, go to the previous.
  1126. (beginning-of-line (if (bolp) 0 1))
  1127. (save-match-data
  1128. (or (looking-at fortran-start-prog-re)
  1129. ;; This leaves us at bob if before the first subprogram.
  1130. (eq (fortran-previous-statement) 'first-statement)
  1131. (if (or (catch 'ok
  1132. (while (re-search-backward fortran-end-prog-re nil 'move)
  1133. (if (fortran-check-end-prog-re) (throw 'ok t))))
  1134. ;; If the search failed, must be at bob.
  1135. ;; First code line is the start of the subprogram.
  1136. ;; FIXME use a more rigorous test, cf fortran-next-statement?
  1137. ;; Though that needs to handle continuations too.
  1138. (not (looking-at "^\\([ \t]*[0-9]\\|[ \t]+[^!#]\\)")))
  1139. (fortran-next-statement))))))
  1140. ;; This is simpler than f-beginning-of-s because the end of a
  1141. ;; subprogram is never implicit.
  1142. (defun fortran-end-of-subprogram ()
  1143. "Move point to the end of the current Fortran subprogram."
  1144. (interactive)
  1145. (let ((case-fold-search t))
  1146. (beginning-of-line)
  1147. (save-match-data
  1148. (while (and (re-search-forward fortran-end-prog-re nil 'move)
  1149. (not (fortran-check-end-prog-re))))
  1150. (forward-line))))
  1151. (defun fortran-previous-statement ()
  1152. "Move point to beginning of the previous Fortran statement.
  1153. Returns 'first-statement if that statement is the first
  1154. non-comment Fortran statement in the file, and nil otherwise.
  1155. Directive lines are treated as comments."
  1156. (interactive)
  1157. (let (not-first-statement continue-test)
  1158. (beginning-of-line)
  1159. (setq continue-test
  1160. (and
  1161. (not (looking-at fortran-comment-line-start-skip))
  1162. (not (looking-at fortran-directive-re))
  1163. (or (looking-at
  1164. (concat "[ \t]*"
  1165. (regexp-quote fortran-continuation-string)))
  1166. (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
  1167. (while (and (setq not-first-statement (zerop (forward-line -1)))
  1168. (or (looking-at fortran-comment-line-start-skip)
  1169. (looking-at fortran-directive-re)
  1170. (looking-at
  1171. (concat "[ \t]*"
  1172. (regexp-quote fortran-continuation-string)))
  1173. (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
  1174. (looking-at (concat "[ \t]*" comment-start-skip)))))
  1175. (cond ((and continue-test
  1176. (not not-first-statement))
  1177. (message "Incomplete continuation statement."))
  1178. (continue-test
  1179. (fortran-previous-statement))
  1180. ((not not-first-statement)
  1181. 'first-statement))))
  1182. (defun fortran-next-statement ()
  1183. "Move point to beginning of the next Fortran statement.
  1184. Returns 'last-statement if that statement is the last
  1185. non-comment Fortran statement in the file, and nil otherwise.
  1186. Directive lines are treated as comments."
  1187. (interactive)
  1188. (let (not-last-statement)
  1189. (beginning-of-line)
  1190. (while (and (setq not-last-statement
  1191. (and (zerop (forward-line 1))
  1192. (not (eobp))))
  1193. (or (looking-at fortran-comment-line-start-skip)
  1194. (looking-at fortran-directive-re)
  1195. (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
  1196. (looking-at (concat "[ \t]*" comment-start-skip)))))
  1197. (if (not not-last-statement)
  1198. 'last-statement)))
  1199. (defun fortran-looking-at-if-then ()
  1200. "Return non-nil if at the start of a line with an IF ... THEN statement."
  1201. ;; cf f90-looking-at-if-then.
  1202. (let ((p (point))
  1203. (i (fortran-beginning-if)))
  1204. (if i
  1205. (save-excursion
  1206. (goto-char i)
  1207. (= (line-beginning-position) p)))))
  1208. ;; Used in hs-special-modes-alist.
  1209. (defun fortran-end-of-block (&optional num)
  1210. "Move point forward to the end of the current code block.
  1211. With optional argument NUM, go forward that many balanced blocks.
  1212. If NUM is negative, go backward to the start of a block. Does
  1213. not check for consistency of block types. Interactively, pushes
  1214. mark before moving point."
  1215. (interactive "p")
  1216. (if (called-interactively-p 'any) (push-mark (point) t))
  1217. (and num (< num 0) (fortran-beginning-of-block (- num)))
  1218. (let ((case-fold-search t)
  1219. (count (or num 1)))
  1220. (end-of-line)
  1221. (while (and (> count 0)
  1222. (re-search-forward
  1223. (concat "\\(" fortran-blocks-re
  1224. (if fortran-check-all-num-for-matching-do
  1225. "\\|^[ \t]*[0-9]+" "")
  1226. "\\|continue\\|end\\)\\>")
  1227. nil 'move))
  1228. (beginning-of-line)
  1229. (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
  1230. (fortran-looking-at-if-then)
  1231. (looking-at fortran-start-block-re))
  1232. (setq count (1+ count))
  1233. (if (or (looking-at fortran-end-block-re)
  1234. (and (or (looking-at "^[0-9 \t]*continue")
  1235. (and fortran-check-all-num-for-matching-do
  1236. (looking-at "[ \t]*[0-9]+")))
  1237. (fortran-check-for-matching-do)))
  1238. (setq count (1- count))))
  1239. (end-of-line))
  1240. (if (> count 0) (error "Missing block end"))))
  1241. (defun fortran-beginning-of-block (&optional num)
  1242. "Move point backwards to the start of the current code block.
  1243. With optional argument NUM, go backward that many balanced
  1244. blocks. If NUM is negative, go forward to the end of a block.
  1245. Does not check for consistency of block types. Interactively,
  1246. pushes mark before moving point."
  1247. (interactive "p")
  1248. (if (called-interactively-p 'any) (push-mark (point) t))
  1249. (and num (< num 0) (fortran-end-of-block (- num)))
  1250. (let ((case-fold-search t)
  1251. (count (or num 1)))
  1252. (beginning-of-line)
  1253. (while (and (> count 0)
  1254. (re-search-backward
  1255. (concat "\\(" fortran-blocks-re
  1256. (if fortran-check-all-num-for-matching-do
  1257. "\\|^[ \t]*[0-9]+" "")
  1258. "\\|continue\\|end\\)\\>")
  1259. nil 'move))
  1260. (beginning-of-line)
  1261. (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
  1262. (fortran-looking-at-if-then)
  1263. (looking-at fortran-start-block-re))
  1264. (setq count (1- count))
  1265. (if (or (looking-at fortran-end-block-re)
  1266. (and (or (looking-at "^[0-9 \t]*continue")
  1267. (and fortran-check-all-num-for-matching-do
  1268. (looking-at "[ \t]*[0-9]+")))
  1269. (fortran-check-for-matching-do)))
  1270. (setq count (1+ count)))))
  1271. ;; Includes an un-named main program block.
  1272. (if (> count 0) (error "Missing block start"))))
  1273. (defun fortran-blink-match (regex keyword find-begin)
  1274. "From a line matching REGEX, blink matching KEYWORD statement line.
  1275. Use function FIND-BEGIN to match it."
  1276. (let ((top-of-window (window-start))
  1277. (end-point (point))
  1278. (case-fold-search t)
  1279. matching
  1280. message)
  1281. (when (save-excursion
  1282. (beginning-of-line)
  1283. (skip-chars-forward " \t0-9")
  1284. (looking-at regex))
  1285. (if (not (setq matching (funcall find-begin)))
  1286. (setq message (concat "No matching " keyword "."))
  1287. (if (< matching top-of-window)
  1288. (save-excursion
  1289. (goto-char matching)
  1290. (beginning-of-line)
  1291. (setq message
  1292. (concat "Matches "
  1293. (buffer-substring (point)
  1294. (line-end-position)))))))
  1295. (if message
  1296. (message "%s" message)
  1297. (goto-char matching)
  1298. (sit-for blink-matching-delay)
  1299. (goto-char end-point)))))
  1300. (defun fortran-blink-matching-if ()
  1301. "From an ENDIF or ELSE statement, blink the matching IF statement."
  1302. (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
  1303. "if" #'fortran-beginning-if))
  1304. (defun fortran-blink-matching-do ()
  1305. "From an ENDDO statement, blink the matching DO or DO WHILE statement."
  1306. (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do))
  1307. (defun fortran-mark-do ()
  1308. "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
  1309. The marks are pushed."
  1310. (interactive)
  1311. (let (enddo-point do-point)
  1312. (if (setq enddo-point (fortran-end-do))
  1313. (if (not (setq do-point (fortran-beginning-do)))
  1314. (message "No matching do.")
  1315. (goto-char enddo-point)
  1316. (push-mark)
  1317. (goto-char do-point)))))
  1318. (defun fortran-end-do ()
  1319. "Search forward for first unmatched ENDDO.
  1320. Return point or nil."
  1321. (let ((case-fold-search t))
  1322. (if (save-excursion (beginning-of-line)
  1323. (skip-chars-forward " \t0-9")
  1324. (looking-at "end[ \t]*do\\b"))
  1325. ;; Sitting on one.
  1326. (match-beginning 0)
  1327. ;; Search for one.
  1328. (save-excursion
  1329. (let ((count 1))
  1330. (while (and (not (zerop count))
  1331. (not (eq (fortran-next-statement) 'last-statement))
  1332. ;; Keep local to subprogram.
  1333. (not (and (looking-at fortran-end-prog-re)
  1334. (fortran-check-end-prog-re))))
  1335. (skip-chars-forward " \t0-9")
  1336. (cond ((looking-at "end[ \t]*do\\b")
  1337. (setq count (1- count)))
  1338. ((looking-at
  1339. "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
  1340. (setq count (1+ count)))))
  1341. (and (zerop count)
  1342. ;; All pairs accounted for.
  1343. (point)))))))
  1344. (defun fortran-beginning-do ()
  1345. "Search backwards for first unmatched DO [WHILE].
  1346. Return point or nil. Ignores labeled DO loops (ie DO 10 ... 10 CONTINUE)."
  1347. (let ((case-fold-search t)
  1348. (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
  1349. (if (save-excursion
  1350. (beginning-of-line)
  1351. (skip-chars-forward " \t0-9")
  1352. (looking-at dostart-re))
  1353. ;; Sitting on one.
  1354. (match-beginning 0)
  1355. ;; Search for one.
  1356. (save-excursion
  1357. (let ((count 1))
  1358. (while (and (not (zerop count))
  1359. (not (eq (fortran-previous-statement) 'first-statement))
  1360. ;; Keep local to subprogram.
  1361. (not (and (looking-at fortran-end-prog-re)
  1362. (fortran-check-end-prog-re))))
  1363. (skip-chars-forward " \t0-9")
  1364. (cond ((looking-at dostart-re)
  1365. (setq count (1- count)))
  1366. ;; Note labeled loop ends not considered.
  1367. ((looking-at "end[ \t]*do\\b")
  1368. (setq count (1+ count)))))
  1369. (and (zerop count)
  1370. ;; All pairs accounted for.
  1371. (point)))))))
  1372. (defun fortran-mark-if ()
  1373. "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
  1374. The marks are pushed."
  1375. (interactive)
  1376. (let (endif-point if-point)
  1377. (if (setq endif-point (fortran-end-if))
  1378. (if (not (setq if-point (fortran-beginning-if)))
  1379. (message "No matching if.")
  1380. ;; Set mark, move point.
  1381. (goto-char endif-point)
  1382. (push-mark)
  1383. (goto-char if-point)))))
  1384. (defun fortran-end-if ()
  1385. "Search forwards for first unmatched ENDIF.
  1386. Return point or nil."
  1387. (let ((case-fold-search t))
  1388. (if (save-excursion (beginning-of-line)
  1389. (skip-chars-forward " \t0-9")
  1390. (looking-at "end[ \t]*if\\b"))
  1391. ;; Sitting on one.
  1392. (match-beginning 0)
  1393. ;; Search for one. The point has been already been moved to first
  1394. ;; letter on line but this should not cause troubles.
  1395. (save-excursion
  1396. (let ((count 1))
  1397. (while (and (not (zerop count))
  1398. (not (eq (fortran-next-statement) 'last-statement))
  1399. ;; Keep local to subprogram.
  1400. (not (and (looking-at fortran-end-prog-re)
  1401. (fortran-check-end-prog-re))))
  1402. (skip-chars-forward " \t0-9")
  1403. (cond ((looking-at "end[ \t]*if\\b")
  1404. (setq count (1- count)))
  1405. ((looking-at fortran-if-start-re)
  1406. (save-excursion
  1407. (if (or
  1408. (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
  1409. (let (then-test) ; multi-line if-then
  1410. (while
  1411. (and
  1412. (zerop (forward-line 1))
  1413. ;; Search forward for then.
  1414. (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
  1415. (not
  1416. (setq then-test
  1417. (looking-at
  1418. ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
  1419. then-test))
  1420. (setq count (1+ count)))))))
  1421. (and (zerop count)
  1422. ;; All pairs accounted for.
  1423. (point)))))))
  1424. (defun fortran-beginning-if ()
  1425. "Search backwards for first unmatched IF-THEN.
  1426. Return point or nil."
  1427. (let ((case-fold-search t))
  1428. (if (save-excursion
  1429. ;; May be sitting on multi-line if-then statement, first
  1430. ;; move to beginning of current statement. Note:
  1431. ;; `fortran-previous-statement' moves to previous statement
  1432. ;; *unless* current statement is first one. Only move
  1433. ;; forward if not first-statement.
  1434. (if (not (eq (fortran-previous-statement) 'first-statement))
  1435. (fortran-next-statement))
  1436. (skip-chars-forward " \t0-9")
  1437. (and
  1438. (looking-at fortran-if-start-re)
  1439. (save-match-data
  1440. (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
  1441. ;; Multi-line if-then.
  1442. (let (then-test)
  1443. (while
  1444. (and (zerop (forward-line 1))
  1445. ;; Search forward for then.
  1446. (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
  1447. (not
  1448. (setq then-test
  1449. (looking-at
  1450. ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
  1451. then-test)))))
  1452. ;; Sitting on one.
  1453. (match-beginning 0)
  1454. ;; Search for one.
  1455. (save-excursion
  1456. (let ((count 1))
  1457. (while (and (not (zerop count))
  1458. (not (eq (fortran-previous-statement) 'first-statement))
  1459. ;; Keep local to subprogram.
  1460. (not (and (looking-at fortran-end-prog-re)
  1461. (fortran-check-end-prog-re))))
  1462. (skip-chars-forward " \t0-9")
  1463. (cond ((looking-at fortran-if-start-re)
  1464. (save-excursion
  1465. (if (or
  1466. (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
  1467. (let (then-test) ; multi-line if-then
  1468. (while
  1469. (and
  1470. (zerop (forward-line 1))
  1471. ;; Search forward for then.
  1472. (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
  1473. (not
  1474. (setq then-test
  1475. (looking-at
  1476. (concat ".*then\\b[ \t]*"
  1477. "[^ \t(=a-z0-9]"))))))
  1478. then-test))
  1479. (setq count (1- count)))))
  1480. ((looking-at "end[ \t]*if\\b")
  1481. (setq count (1+ count)))))
  1482. (and (zerop count)
  1483. ;; All pairs accounted for.
  1484. (point)))))))
  1485. (defun fortran-indent-line ()
  1486. "Indent current Fortran line based on its contents and on previous lines."
  1487. (interactive "*")
  1488. (let ((cfi (fortran-calculate-indent)))
  1489. (save-excursion
  1490. (beginning-of-line)
  1491. (if (or (not (= cfi (fortran-current-line-indentation)))
  1492. (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
  1493. (not (fortran-line-number-indented-correctly-p))))
  1494. (fortran-indent-to-column cfi)
  1495. (beginning-of-line)
  1496. (if (fortran-find-comment-start-skip)
  1497. (fortran-indent-comment))))
  1498. ;; Never leave point in left margin.
  1499. (if (< (current-column) cfi)
  1500. (move-to-column cfi))
  1501. (and auto-fill-function
  1502. (> (save-excursion (end-of-line) (current-column))
  1503. fill-column)
  1504. (save-excursion
  1505. (end-of-line)
  1506. (fortran-fill)))
  1507. (when fortran-blink-matching-if
  1508. (fortran-blink-matching-if)
  1509. (fortran-blink-matching-do))))
  1510. (defun fortran-auto-fill ()
  1511. "Function to use for `normal-auto-fill-function' in Fortran mode."
  1512. (if (> (current-column) (current-fill-column))
  1513. (let ((cfi (fortran-calculate-indent)))
  1514. (save-excursion
  1515. (beginning-of-line)
  1516. (if (or (not (= cfi (fortran-current-line-indentation)))
  1517. (and (re-search-forward "^[ \t]*[0-9]+"
  1518. (+ (point) 4) t)
  1519. (not (fortran-line-number-indented-correctly-p))))
  1520. (fortran-indent-to-column cfi)
  1521. (beginning-of-line)
  1522. (if (fortran-find-comment-start-skip)
  1523. (fortran-indent-comment))))
  1524. (fortran-fill)
  1525. ;; Never leave point in left margin.
  1526. (if (< (current-column) cfi)
  1527. (move-to-column cfi)))))
  1528. ;; Historically this was a separate function which advertised itself
  1529. ;; as reindenting but only did so where `most likely to be necessary'.
  1530. (defalias 'fortran-indent-new-line 'reindent-then-newline-and-indent)
  1531. (defun fortran-indent-subprogram ()
  1532. "Properly indent the Fortran subprogram containing point."
  1533. (interactive "*")
  1534. (save-excursion
  1535. (mark-defun)
  1536. (message "Indenting subprogram...")
  1537. (indent-region (point) (mark) nil))
  1538. (message "Indenting subprogram...done."))
  1539. (defun fortran-calculate-indent ()
  1540. "Calculates the Fortran indent column based on previous lines."
  1541. (let (icol first-statement (case-fold-search t)
  1542. (fortran-minimum-statement-indent
  1543. (if indent-tabs-mode
  1544. fortran-minimum-statement-indent-tab
  1545. fortran-minimum-statement-indent-fixed)))
  1546. (save-excursion
  1547. (setq first-statement (fortran-previous-statement))
  1548. (if first-statement
  1549. (setq icol fortran-minimum-statement-indent)
  1550. (if (= (point) (point-min))
  1551. (setq icol fortran-minimum-statement-indent)
  1552. (setq icol (fortran-current-line-indentation)))
  1553. (skip-chars-forward " \t0-9")
  1554. (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
  1555. (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
  1556. (let (then-test) ; multi-line if-then
  1557. (while (and (zerop (forward-line 1))
  1558. ;; Search forward for then.
  1559. (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
  1560. (not (setq then-test
  1561. (looking-at
  1562. ".*then\\b[ \t]\
  1563. *[^ \t_$(=a-z0-9]")))))
  1564. then-test))
  1565. (setq icol (+ icol fortran-if-indent))))
  1566. ((looking-at "else\\(if\\)?\\b")
  1567. (setq icol (+ icol fortran-if-indent)))
  1568. ((looking-at "select[ \t]*case[ \t](.*)")
  1569. (setq icol (+ icol fortran-if-indent)))
  1570. ((looking-at "case[ \t]*(.*)")
  1571. (setq icol (+ icol fortran-if-indent)))
  1572. ((looking-at "case[ \t]*default\\b")
  1573. (setq icol (+ icol fortran-if-indent)))
  1574. ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
  1575. (setq icol (+ icol fortran-if-indent)))
  1576. ((looking-at "where[ \t]*(.*)[ \t]*\n")
  1577. (setq icol (+ icol fortran-if-indent)))
  1578. ((looking-at "do\\b")
  1579. (setq icol (+ icol fortran-do-indent)))
  1580. ((looking-at
  1581. "\\(structure\\|union\\|map\\|interface\\)\
  1582. \\b[ \t]*[^ \t=(a-z]")
  1583. (setq icol (+ icol fortran-structure-indent)))
  1584. ((and (looking-at fortran-end-prog-re1)
  1585. (fortran-check-end-prog-re))
  1586. ;; Previous END resets indent to minimum.
  1587. (setq icol fortran-minimum-statement-indent))
  1588. ;; Previous statement was a numbered DO loop without a
  1589. ;; closing CONTINUE or END DO, so we indented the
  1590. ;; terminator like the loop body.
  1591. ((and fortran-check-all-num-for-matching-do
  1592. (not (looking-at "\\(continue\\|end[ \t]*do\\)\\>"))
  1593. (progn
  1594. (beginning-of-line)
  1595. (and (looking-at "[ \t]*[0-9]+")
  1596. (fortran-check-for-matching-do))))
  1597. (setq icol (- icol fortran-do-indent))))))
  1598. (save-excursion
  1599. (beginning-of-line)
  1600. (cond ((looking-at "[ \t]*$"))
  1601. ;; Check for directive before comment, so as not to indent.
  1602. ((looking-at fortran-directive-re)
  1603. (setq fortran-minimum-statement-indent 0 icol 0))
  1604. ((looking-at fortran-comment-line-start-skip)
  1605. (cond ((eq fortran-comment-indent-style 'relative)
  1606. (setq icol (+ icol fortran-comment-line-extra-indent)))
  1607. ((eq fortran-comment-indent-style 'fixed)
  1608. (setq icol (+ fortran-minimum-statement-indent
  1609. fortran-comment-line-extra-indent))))
  1610. (setq fortran-minimum-statement-indent 0))
  1611. ((or (looking-at (concat "[ \t]*"
  1612. (regexp-quote
  1613. fortran-continuation-string)))
  1614. (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
  1615. (skip-chars-forward " \t")
  1616. ;; Do not introduce extra whitespace into a broken string.
  1617. (setq icol
  1618. (if (fortran-is-in-string-p (point))
  1619. 6
  1620. (+ icol fortran-continuation-indent))))
  1621. (first-statement)
  1622. ;; The terminating statement is actually part of the
  1623. ;; loop body, so unless it is a CONTINUE or END DO, we
  1624. ;; indent it like the loop body (see above).
  1625. ((and fortran-check-all-num-for-matching-do
  1626. (looking-at "[ \t]*[0-9]+[ \t]*\
  1627. \\(continue\\|end[ \t]*do\\)\\>")
  1628. (fortran-check-for-matching-do))
  1629. (setq icol (- icol fortran-do-indent)))
  1630. (t
  1631. (skip-chars-forward " \t0-9")
  1632. (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
  1633. (setq icol (- icol fortran-if-indent)))
  1634. ((looking-at "else\\(if\\)?\\b")
  1635. (setq icol (- icol fortran-if-indent)))
  1636. ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
  1637. (setq icol (- icol fortran-if-indent)))
  1638. ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
  1639. (setq icol (- icol fortran-if-indent)))
  1640. ((and (looking-at "continue\\b")
  1641. (fortran-check-for-matching-do))
  1642. (setq icol (- icol fortran-do-indent)))
  1643. ((looking-at "end[ \t]*do\\b")
  1644. (setq icol (- icol fortran-do-indent)))
  1645. ((looking-at "end[ \t]*\
  1646. \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
  1647. (setq icol (- icol fortran-structure-indent)))
  1648. ((and (looking-at fortran-end-prog-re1)
  1649. (fortran-check-end-prog-re)
  1650. (not (= icol fortran-minimum-statement-indent)))
  1651. (message "Warning: `end' not in column %d. Probably\
  1652. an unclosed block." fortran-minimum-statement-indent))))))
  1653. (max fortran-minimum-statement-indent icol)))
  1654. (defun fortran-current-line-indentation ()
  1655. "Indentation of current line, ignoring Fortran line number or continuation.
  1656. This is the column position of the first non-whitespace character
  1657. aside from the line number and/or column 5/8 line-continuation character.
  1658. For comment lines, returns indentation of the first
  1659. non-indentation text within the comment."
  1660. (save-excursion
  1661. (beginning-of-line)
  1662. (cond ((looking-at fortran-comment-line-start-skip)
  1663. (goto-char (match-end 0))
  1664. (skip-chars-forward
  1665. (if (stringp fortran-comment-indent-char)
  1666. fortran-comment-indent-char
  1667. (char-to-string fortran-comment-indent-char))))
  1668. ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
  1669. (goto-char (match-end 0)))
  1670. (t
  1671. ;; Move past line number.
  1672. (skip-chars-forward "[ \t0-9]")))
  1673. ;; Move past whitespace.
  1674. (skip-chars-forward " \t")
  1675. (current-column)))
  1676. (defun fortran-indent-to-column (col)
  1677. "Indent current line to column COL.
  1678. notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
  1679. line, and this continuation character is retained on indentation;
  1680. 2) If `fortran-continuation-string' is the first non-whitespace
  1681. character, this is a continuation line;
  1682. 3) A non-continuation line which has a number as the first
  1683. non-whitespace character is a numbered line.
  1684. 4) A TAB followed by a digit indicates a continuation line."
  1685. (save-excursion
  1686. (beginning-of-line)
  1687. (if (looking-at fortran-comment-line-start-skip)
  1688. (if fortran-comment-indent-style
  1689. (let* ((char (if (stringp fortran-comment-indent-char)
  1690. (aref fortran-comment-indent-char 0)
  1691. fortran-comment-indent-char))
  1692. (chars (string ?\s ?\t char)))
  1693. (goto-char (match-end 0))
  1694. (skip-chars-backward chars)
  1695. (delete-region (point) (progn (skip-chars-forward chars)
  1696. (point)))
  1697. (insert-char char (- col (current-column)))))
  1698. (if (looking-at "\t[1-9]")
  1699. (if indent-tabs-mode
  1700. (goto-char (match-end 0))
  1701. (delete-char 2)
  1702. (insert-char ?\s 5)
  1703. (insert fortran-continuation-string))
  1704. (if (looking-at " \\{5\\}[^ 0\n]")
  1705. (if indent-tabs-mode
  1706. (progn (delete-char 6)
  1707. (insert ?\t (fortran-numerical-continuation-char) 1))
  1708. (forward-char 6))
  1709. (delete-horizontal-space)
  1710. ;; Put line number in columns 0-4, or
  1711. ;; continuation character in column 5.
  1712. (cond ((eobp))
  1713. ((looking-at (regexp-quote fortran-continuation-string))
  1714. (if indent-tabs-mode
  1715. (progn
  1716. (indent-to
  1717. (if indent-tabs-mode
  1718. fortran-minimum-statement-indent-tab
  1719. fortran-minimum-statement-indent-fixed))
  1720. (delete-char 1)
  1721. (insert-char (fortran-numerical-continuation-char) 1))
  1722. (indent-to 5)
  1723. (forward-char 1)))
  1724. ((looking-at "[0-9]+")
  1725. (let ((extra-space (- 5 (- (match-end 0) (point)))))
  1726. (if (< extra-space 0)
  1727. (message "Warning: line number exceeds 5-digit limit.")
  1728. (indent-to (min fortran-line-number-indent extra-space))))
  1729. (skip-chars-forward "0-9")))))
  1730. ;; Point is now after any continuation character or line number.
  1731. ;; Put body of statement where specified.
  1732. (delete-horizontal-space)
  1733. (indent-to col)
  1734. ;; Indent any comment following code on the same line.
  1735. (when (fortran-find-comment-start-skip)
  1736. (goto-char (match-beginning 0))
  1737. (unless (= (current-column) (fortran-comment-indent))
  1738. (delete-horizontal-space)
  1739. (indent-to (fortran-comment-indent)))))))
  1740. (defun fortran-line-number-indented-correctly-p ()
  1741. "Return t if current line's line number is correctly indented.
  1742. Do not call if there is no line number."
  1743. (save-excursion
  1744. (beginning-of-line)
  1745. (skip-chars-forward " \t")
  1746. (and (<= (current-column) fortran-line-number-indent)
  1747. (or (= (current-column) fortran-line-number-indent)
  1748. (progn (skip-chars-forward "0-9")
  1749. (= (current-column) 5))))))
  1750. (defun fortran-check-for-matching-do ()
  1751. "When called from a numbered statement, return t if matching DO is found.
  1752. Otherwise return nil."
  1753. (let ((case-fold-search t)
  1754. charnum)
  1755. (save-excursion
  1756. (beginning-of-line)
  1757. (when (looking-at "[ \t]*[0-9]+")
  1758. (skip-chars-forward " \t")
  1759. (skip-chars-forward "0") ; skip past leading zeros
  1760. (setq charnum
  1761. (buffer-substring (point) (progn
  1762. (skip-chars-forward "0-9")
  1763. (point))))
  1764. (beginning-of-line)
  1765. (save-restriction
  1766. (save-excursion
  1767. (narrow-to-defun)
  1768. (and (re-search-backward
  1769. (concat
  1770. "\\(^[ \t0-9]*do[ \t]*0*"
  1771. charnum "\\b\\)\\|" "\\(^[ \t]*0*"
  1772. charnum "\\b\\)")
  1773. nil t)
  1774. (looking-at
  1775. (concat "^[ \t0-9]*do[ \t]*0*"
  1776. charnum)))))))))
  1777. (defun fortran-find-comment-start-skip (&optional all)
  1778. "Move to past `comment-start-skip' found on current line.
  1779. Return non-nil if `comment-start-skip' found, nil if not.
  1780. If ALL is nil, only match comments that start in column > 0."
  1781. ;; Hopefully at some point we can just use the line below! -stef
  1782. ;; (comment-search-forward (line-end-position) t))
  1783. (when (or all comment-start-skip)
  1784. (let ((pos (point))
  1785. (css (if comment-start-skip
  1786. (concat fortran-comment-line-start-skip
  1787. "\\|" comment-start-skip)
  1788. fortran-comment-line-start-skip)))
  1789. (when (re-search-forward css (line-end-position) t)
  1790. (if (and (or all (> (match-beginning 0) (line-beginning-position)))
  1791. (or (save-match-data
  1792. (not (fortran-is-in-string-p (match-beginning 0))))
  1793. ;; Recurse for rest of line.
  1794. (fortran-find-comment-start-skip all)))
  1795. (point)
  1796. (goto-char pos)
  1797. nil)))))
  1798. ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
  1799. ;; Test if TAB format continuation lines work.
  1800. (defun fortran-is-in-string-p (where)
  1801. "Return non-nil if WHERE (a buffer position) is inside a Fortran string."
  1802. (save-excursion
  1803. (goto-char where)
  1804. (cond
  1805. ((bolp) nil) ; bol is never inside a string
  1806. ((save-excursion ; comment lines too
  1807. (beginning-of-line)
  1808. (looking-at fortran-comment-line-start-skip)) nil)
  1809. (t (let ((parse-state '(0 nil nil nil nil nil 0))
  1810. (quoted-comment-start (if comment-start
  1811. (regexp-quote comment-start)))
  1812. (not-done t)
  1813. parse-limit end-of-line)
  1814. ;; Move to start of current statement.
  1815. (fortran-next-statement)
  1816. (fortran-previous-statement)
  1817. ;; Now parse up to WHERE.
  1818. (while not-done
  1819. (if (or ;; Skip to next line if:
  1820. ;; - comment line?
  1821. (looking-at fortran-comment-line-start-skip)
  1822. ;; - at end of line?
  1823. (eolp)
  1824. ;; - not in a string and after comment-start?
  1825. (and (not (nth 3 parse-state))
  1826. comment-start
  1827. (equal comment-start
  1828. (char-to-string (preceding-char)))))
  1829. (if (> (forward-line) 0)
  1830. (setq not-done nil))
  1831. ;; else:
  1832. ;; If we are at beginning of code line, skip any
  1833. ;; whitespace, labels and tab continuation markers.
  1834. (if (bolp) (skip-chars-forward " \t0-9"))
  1835. ;; If we are in column <= 5 now, check for continuation char.
  1836. (cond ((= 5 (current-column)) (forward-char 1))
  1837. ((and (< (current-column) 5)
  1838. (equal fortran-continuation-string
  1839. (char-to-string (following-char)))
  1840. (forward-char 1))))
  1841. ;; Find out parse-limit from here.
  1842. (setq end-of-line (line-end-position))
  1843. (setq parse-limit (min where end-of-line))
  1844. ;; Parse max up to comment-start, if non-nil and in current line.
  1845. (if comment-start
  1846. (save-excursion
  1847. (if (re-search-forward quoted-comment-start end-of-line t)
  1848. (setq parse-limit (min (point) parse-limit)))))
  1849. ;; Now parse if still in limits.
  1850. (if (< (point) where)
  1851. (setq parse-state (parse-partial-sexp
  1852. (point) parse-limit nil nil parse-state))
  1853. (setq not-done nil))))
  1854. ;; Result.
  1855. (nth 3 parse-state))))))
  1856. ;; From old version.
  1857. (defalias 'fortran-auto-fill-mode 'auto-fill-mode)
  1858. (defun fortran-fill ()
  1859. "Fill the current line at an appropriate point(s)."
  1860. (let* ((auto-fill-function #'fortran-auto-fill)
  1861. (opoint (point))
  1862. (bol (line-beginning-position))
  1863. (eol (line-end-position))
  1864. (bos (min eol (+ bol (fortran-current-line-indentation))))
  1865. ;; If in a string at fill-column, break it either before the
  1866. ;; initial quote, or at fill-col (if string is too long).
  1867. (quote
  1868. (save-excursion
  1869. (goto-char bol)
  1870. ;; OK to break quotes on comment lines.
  1871. (unless (looking-at fortran-comment-line-start-skip)
  1872. (let (fcpoint start)
  1873. (move-to-column fill-column)
  1874. (when (fortran-is-in-string-p (setq fcpoint (point)))
  1875. (save-excursion
  1876. (re-search-backward "\\S\"\\s\"\\S\"?" bol t)
  1877. (setq start
  1878. (if fortran-break-before-delimiters
  1879. (point)
  1880. (1+ (point)))))
  1881. (if (re-search-forward "\\S\"\\s\"\\S\"" eol t)
  1882. (backward-char 2))
  1883. ;; If the current string is longer than (fill-column
  1884. ;; - 6) chars, break it at the fill column (else
  1885. ;; infinite loop).
  1886. (if (> (- (point) start)
  1887. (- fill-column 6 fortran-continuation-indent))
  1888. fcpoint
  1889. start))))))
  1890. ;; Decide where to split the line. If a position for a quoted
  1891. ;; string was found above then use that, else break the line
  1892. ;; before/after the last delimiter.
  1893. (fill-point
  1894. (or quote
  1895. (save-excursion
  1896. ;; If f-b-b-d is t, have an extra column to play with,
  1897. ;; since delimiter gets shifted to new line.
  1898. (move-to-column (if fortran-break-before-delimiters
  1899. (1+ fill-column)
  1900. fill-column))
  1901. (let ((repeat t))
  1902. (while repeat
  1903. (setq repeat nil)
  1904. ;; Adapted from f90-find-breakpoint.
  1905. (re-search-backward fortran-break-delimiters-re bol)
  1906. (if (not fortran-break-before-delimiters)
  1907. (if (looking-at fortran-no-break-re)
  1908. ;; Deal with cases such as "**" split over
  1909. ;; fill-col. Simpler alternative would be
  1910. ;; to start from (1- fill-column) above.
  1911. (if (> (+ 2 (current-column)) fill-column)
  1912. (setq repeat t)
  1913. (forward-char 2))
  1914. (forward-char 1))
  1915. (backward-char)
  1916. (or (looking-at fortran-no-break-re)
  1917. (forward-char)))))
  1918. ;; Line indented beyond fill-column?
  1919. (when (<= (point) bos)
  1920. (move-to-column (1+ fill-column))
  1921. ;; What is this doing???
  1922. (or (re-search-forward "[\t\n,'+-/*)=]" eol t)
  1923. (goto-char bol)))
  1924. (if (bolp)
  1925. (re-search-forward "[ \t]" opoint t))
  1926. (point)))))
  1927. ;; If we are in an in-line comment, don't break unless the
  1928. ;; line of code is longer than it should be. Otherwise
  1929. ;; break the line at the column computed above.
  1930. ;;
  1931. ;; Need to use fortran-find-comment-start-skip to make sure that
  1932. ;; quoted !'s don't prevent a break.
  1933. (when (and (save-excursion
  1934. (beginning-of-line)
  1935. (if (not (fortran-find-comment-start-skip))
  1936. t
  1937. (goto-char (match-beginning 0))
  1938. (>= (point) fill-point)))
  1939. (save-excursion
  1940. (goto-char fill-point)
  1941. (not (bolp)))
  1942. (> (save-excursion
  1943. (goto-char opoint)
  1944. (current-column))
  1945. (min (1+ fill-column)
  1946. (+ (fortran-calculate-indent)
  1947. fortran-continuation-indent))))
  1948. (goto-char fill-point)
  1949. (fortran-break-line)
  1950. (end-of-line))))
  1951. (defun fortran-break-line ()
  1952. "Call `fortran-split-line'. Joins continuation lines first, then refills."
  1953. (let ((bol (line-beginning-position))
  1954. (comment-string
  1955. (save-excursion
  1956. (if (fortran-find-comment-start-skip)
  1957. (delete-and-extract-region
  1958. (match-beginning 0) (line-end-position))))))
  1959. ;; Forward line 1 really needs to go to next non white line.
  1960. (if (save-excursion (forward-line)
  1961. (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
  1962. (progn
  1963. (end-of-line)
  1964. (delete-region (point) (match-end 0))
  1965. (delete-horizontal-space)
  1966. (fortran-fill))
  1967. (fortran-split-line))
  1968. (if comment-string
  1969. (save-excursion
  1970. (goto-char bol)
  1971. (end-of-line)
  1972. (delete-horizontal-space)
  1973. (indent-to (fortran-comment-indent))
  1974. (insert comment-string)))))
  1975. (defun fortran-analyze-file-format ()
  1976. "Return nil if fixed format is used, t if TAB formatting is used.
  1977. Use `fortran-tab-mode-default' if no non-comment statements are found
  1978. before the end or in the first `fortran-analyze-depth' lines."
  1979. (let ((i 0))
  1980. (save-excursion
  1981. (goto-char (point-min))
  1982. (while (not (or
  1983. (eobp)
  1984. (eq (char-after) ?\t)
  1985. (looking-at " \\{6\\}")
  1986. (> i fortran-analyze-depth)))
  1987. (forward-line)
  1988. (setq i (1+ i)))
  1989. (cond
  1990. ((eq (char-after) ?\t) t)
  1991. ((looking-at " \\{6\\}") nil)
  1992. (t fortran-tab-mode-default)))))
  1993. (defun fortran-fill-paragraph (&optional justify)
  1994. "Fill surrounding comment block as paragraphs, else fill statement.
  1995. Intended as the value of `fill-paragraph-function'.
  1996. A comment block is filled by calling `fill-comment-paragraph' with
  1997. argument JUSTIFY, otherwise `fortran-fill-statement' is called.
  1998. Always returns non-nil (to prevent `fill-paragraph' being called)."
  1999. (interactive "*P")
  2000. (or (fill-comment-paragraph justify)
  2001. (fortran-fill-statement)
  2002. t))
  2003. (defun fortran-fill-statement ()
  2004. "Fill a Fortran statement up to `fill-column'."
  2005. (interactive "*")
  2006. (let ((auto-fill-function #'fortran-auto-fill))
  2007. (unless (save-excursion
  2008. (beginning-of-line)
  2009. (or (looking-at "[ \t]*$")
  2010. (looking-at fortran-comment-line-start-skip)
  2011. (and comment-start-skip
  2012. (looking-at (concat "[ \t]*" comment-start-skip)))))
  2013. (save-excursion
  2014. ;; Find beginning of statement.
  2015. (fortran-next-statement)
  2016. (fortran-previous-statement)
  2017. ;; Re-indent initially.
  2018. (fortran-indent-line)
  2019. ;; Replace newline plus continuation field plus indentation with
  2020. ;; single space.
  2021. (while (progn
  2022. (forward-line)
  2023. (fortran-remove-continuation)))
  2024. (fortran-previous-statement)))
  2025. (fortran-indent-line)))
  2026. (defun fortran-strip-sequence-nos (&optional do-space)
  2027. "Delete all text in column `fortran-line-length' (default 72) and up.
  2028. This is assumed to be sequence numbers. Normally also deletes
  2029. trailing whitespace after stripping such text. Supplying prefix
  2030. arg DO-SPACE prevents stripping the whitespace."
  2031. (interactive "*p")
  2032. (save-excursion
  2033. (goto-char (point-min))
  2034. (while (re-search-forward (format "^.\\{%d\\}\\(.*\\)" fortran-line-length)
  2035. nil t)
  2036. (replace-match "" nil nil nil 1)
  2037. (unless do-space (delete-horizontal-space)))))
  2038. ;; This code used to live in add-log.el, but this is a better place for it.
  2039. (defun fortran-current-defun ()
  2040. "Function to use for `add-log-current-defun-function' in Fortran mode."
  2041. (save-excursion
  2042. ;; We must be inside function body for this to work.
  2043. (fortran-beginning-of-subprogram)
  2044. (let ((case-fold-search t))
  2045. ;; Search for fortran subprogram start.
  2046. (if (re-search-forward
  2047. fortran-start-prog-re
  2048. (save-excursion (fortran-end-of-subprogram)
  2049. (point))
  2050. t)
  2051. (or (match-string-no-properties 2)
  2052. (progn
  2053. ;; Move to EOL or before first left paren.
  2054. (if (re-search-forward "[(\n]" nil t)
  2055. (progn (backward-char)
  2056. (skip-chars-backward " \t"))
  2057. (end-of-line))
  2058. ;; Use the name preceding that.
  2059. (buffer-substring-no-properties (point) (progn (backward-sexp)
  2060. (point)))))
  2061. "main"))))
  2062. (provide 'fortran)
  2063. ;;; fortran.el ends here