f90.el 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333
  1. ;;; f90.el --- Fortran-90 mode (free format) -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1995-1997, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Torbjörn Einarsson <Torbjorn.Einarsson@era.ericsson.se>
  4. ;; Maintainer: Glenn Morris <rgm@gnu.org>
  5. ;; Keywords: fortran, f90, 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. ;; Major mode for editing F90 programs in FREE FORMAT.
  19. ;; The minor language revision F95 is also supported (with font-locking).
  20. ;; Some/many (?) aspects of F2003 are supported.
  21. ;; Some aspects of F2008 are supported.
  22. ;; Knows about continuation lines, named structured statements, and other
  23. ;; features in F90 including HPF (High Performance Fortran) structures.
  24. ;; The basic feature provides accurate indentation of F90 programs.
  25. ;; In addition, there are many more features like automatic matching of all
  26. ;; end statements, an auto-fill function to break long lines, a join-lines
  27. ;; function which joins continued lines, etc.
  28. ;; To facilitate typing, a fairly complete list of abbreviations is provided.
  29. ;; All abbreviations begin with the backquote character "`"
  30. ;; For example, `i expands to integer (if abbrev-mode is on).
  31. ;; There are two separate features for altering the appearance of code:
  32. ;; 1) Upcasing or capitalizing of all keywords.
  33. ;; 2) Colors/fonts using font-lock-mode.
  34. ;; Automatic upcase or downcase of keywords is controlled by the variable
  35. ;; f90-auto-keyword-case.
  36. ;; The indentations of lines starting with ! is determined by the first of the
  37. ;; following matches (values in the left column are the defaults):
  38. ;; start-string/regexp indent variable holding start-string/regexp
  39. ;; !!! 0
  40. ;; !hpf\\$ (re) 0 f90-directive-comment-re
  41. ;; !!$ 0 f90-comment-region
  42. ;; ! (re) as code f90-indented-comment-re
  43. ;; default comment-column
  44. ;; Ex: Here is the result of 3 different settings of f90-indented-comment-re
  45. ;; f90-indented-comment-re !-indentation !!-indentation
  46. ;; ! as code as code
  47. ;; !! comment-column as code
  48. ;; ![^!] as code comment-column
  49. ;; Trailing comments are indented to comment-column with indent-for-comment.
  50. ;; The function f90-comment-region toggles insertion of
  51. ;; the variable f90-comment-region in every line of the region.
  52. ;; One common convention for free vs. fixed format is that free format files
  53. ;; have the ending .f90 or .f95 while fixed format files have the ending .f.
  54. ;; Emacs automatically loads Fortran files in the appropriate mode based
  55. ;; on extension. You can modify this by adjusting the variable auto-mode-alist.
  56. ;; For example:
  57. ;; (add-to-list 'auto-mode-alist '("\\.f\\'" . f90-mode))
  58. ;; Once you have entered f90-mode, you may get more info by using
  59. ;; the command describe-mode (C-h m). For online help use
  60. ;; C-h f <Name of function you want described>, or
  61. ;; C-h v <Name of variable you want described>.
  62. ;; To customize f90-mode for your taste, use, for example:
  63. ;; (you don't have to specify values for all the parameters below)
  64. ;;
  65. ;; (add-hook 'f90-mode-hook
  66. ;; ;; These are the default values.
  67. ;; (lambda () (setq f90-do-indent 3
  68. ;; f90-if-indent 3
  69. ;; f90-type-indent 3
  70. ;; f90-program-indent 2
  71. ;; f90-continuation-indent 5
  72. ;; f90-comment-region "!!$"
  73. ;; f90-directive-comment-re "!hpf\\$"
  74. ;; f90-indented-comment-re "!"
  75. ;; f90-break-delimiters "[-+\\*/><=,% \t]"
  76. ;; f90-break-before-delimiters t
  77. ;; f90-beginning-ampersand t
  78. ;; f90-smart-end 'blink
  79. ;; f90-auto-keyword-case nil
  80. ;; f90-leave-line-no nil
  81. ;; indent-tabs-mode nil
  82. ;; f90-font-lock-keywords f90-font-lock-keywords-2
  83. ;; )
  84. ;; ;; These are not default.
  85. ;; (abbrev-mode 1) ; turn on abbreviation mode
  86. ;; (f90-add-imenu-menu) ; extra menu with functions etc.
  87. ;; (if f90-auto-keyword-case ; change case of all keywords on startup
  88. ;; (f90-change-keywords f90-auto-keyword-case))
  89. ;; ))
  90. ;;
  91. ;; in your .emacs file. You can also customize the lists
  92. ;; f90-font-lock-keywords, etc.
  93. ;;
  94. ;; The auto-fill and abbreviation minor modes are accessible from the F90 menu,
  95. ;; or by using M-x auto-fill-mode and M-x abbrev-mode, respectively.
  96. ;; Remarks
  97. ;; 1) Line numbers are by default left-justified. If f90-leave-line-no is
  98. ;; non-nil, the line numbers are never touched.
  99. ;; 2) Multi-; statements like "do i=1,20 ; j=j+i ; end do" are not handled
  100. ;; correctly, but I imagine them to be rare.
  101. ;; 3) Regexps for hilit19 are no longer supported.
  102. ;; 4) For FIXED FORMAT code, use fortran mode.
  103. ;; 5) This mode does not work under emacs-18.x.
  104. ;; 6) Preprocessor directives, i.e., lines starting with # are left-justified
  105. ;; and are untouched by all case-changing commands. There is, at present, no
  106. ;; mechanism for treating multi-line directives (continued by \ ).
  107. ;; 7) f77 do-loops do 10 i=.. ; ; 10 continue are not correctly indented.
  108. ;; You are urged to use f90-do loops (with labels if you wish).
  109. ;; 8) The highlighting mode under XEmacs is not as complete as under Emacs.
  110. ;; List of user commands
  111. ;; f90-previous-statement f90-next-statement
  112. ;; f90-beginning-of-subprogram f90-end-of-subprogram f90-mark-subprogram
  113. ;; f90-comment-region
  114. ;; f90-indent-line f90-indent-new-line
  115. ;; f90-indent-region (can be called by calling indent-region)
  116. ;; f90-indent-subprogram
  117. ;; f90-break-line f90-join-lines
  118. ;; f90-fill-region
  119. ;; f90-insert-end
  120. ;; f90-upcase-keywords f90-upcase-region-keywords
  121. ;; f90-downcase-keywords f90-downcase-region-keywords
  122. ;; f90-capitalize-keywords f90-capitalize-region-keywords
  123. ;; f90-add-imenu-menu
  124. ;; f90-font-lock-1, f90-font-lock-2, f90-font-lock-3, f90-font-lock-4
  125. ;; Original author's thanks
  126. ;; Thanks to all the people who have tested the mode. Special thanks to Jens
  127. ;; Bloch Helmers for encouraging me to write this code, for creative
  128. ;; suggestions as well as for the lists of hpf-commands.
  129. ;; Also thanks to the authors of the fortran and pascal modes, on which some
  130. ;; of this code is built.
  131. ;;; Code:
  132. ;; TODO
  133. ;; 1. Any missing F2003 syntax?
  134. ;; 2. Have "f90-mode" just recognize F90 syntax, then derived modes
  135. ;; "f95-mode", "f2003-mode" for the language revisions.
  136. ;; 3. Support for align.
  137. ;; Font-locking:
  138. ;; 1. OpenMP, OpenMPI?, preprocessor highlighting.
  139. ;; 2. integer_name = 1
  140. ;; 3. Labels for "else" statements (F2003)?
  141. (defvar comment-auto-fill-only-comments)
  142. (defvar font-lock-keywords)
  143. ;; User options
  144. (defgroup f90 nil
  145. "Major mode for editing free format Fortran 90,95 code."
  146. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  147. :group 'languages)
  148. (defgroup f90-indent nil
  149. "Indentation in free format Fortran."
  150. :prefix "f90-"
  151. :group 'f90)
  152. (defcustom f90-do-indent 3
  153. "Extra indentation applied to DO blocks."
  154. :type 'integer
  155. :safe 'integerp
  156. :group 'f90-indent)
  157. (defcustom f90-if-indent 3
  158. "Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks."
  159. :type 'integer
  160. :safe 'integerp
  161. :group 'f90-indent)
  162. (defcustom f90-type-indent 3
  163. "Extra indentation applied to TYPE, ENUM, INTERFACE and BLOCK DATA blocks."
  164. :type 'integer
  165. :safe 'integerp
  166. :group 'f90-indent)
  167. (defcustom f90-program-indent 2
  168. "Extra indentation applied to PROGRAM, MODULE, SUBROUTINE, FUNCTION blocks."
  169. :type 'integer
  170. :safe 'integerp
  171. :group 'f90-indent)
  172. (defcustom f90-associate-indent 2
  173. "Extra indentation applied to ASSOCIATE blocks."
  174. :type 'integer
  175. :safe 'integerp
  176. :group 'f90-indent
  177. :version "23.1")
  178. (defcustom f90-critical-indent 2
  179. "Extra indentation applied to BLOCK, CRITICAL blocks."
  180. :type 'integer
  181. :safe 'integerp
  182. :group 'f90-indent
  183. :version "24.1")
  184. (defcustom f90-continuation-indent 5
  185. "Extra indentation applied to continuation lines."
  186. :type 'integer
  187. :safe 'integerp
  188. :group 'f90-indent)
  189. (defcustom f90-comment-region "!!$"
  190. "String inserted by \\[f90-comment-region] at start of each line in region."
  191. :type 'string
  192. :safe 'stringp
  193. :group 'f90-indent)
  194. (defcustom f90-indented-comment-re "!"
  195. "Regexp matching comments to indent as code."
  196. :type 'regexp
  197. :safe 'stringp
  198. :group 'f90-indent)
  199. ;; Should we add ^# to this? That's not really a comment.
  200. (defcustom f90-directive-comment-re "!hpf\\$"
  201. "Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented."
  202. :type 'regexp
  203. :safe 'stringp
  204. :group 'f90-indent)
  205. (defcustom f90-beginning-ampersand t
  206. "Non-nil gives automatic insertion of \& at start of continuation line."
  207. :type 'boolean
  208. :safe 'booleanp
  209. :group 'f90)
  210. (defcustom f90-smart-end 'blink
  211. "Qualification of END statements according to the matching block start.
  212. For example, the END that closes an IF block is changed to END
  213. IF. If the block has a label, this is added as well. Allowed
  214. values are 'blink, 'no-blink, and nil. If nil, nothing is done.
  215. The other two settings have the same effect, but 'blink
  216. additionally blinks the cursor to the start of the block."
  217. :type '(choice (const blink) (const no-blink) (const nil))
  218. :safe (lambda (value) (memq value '(blink no-blink nil)))
  219. :group 'f90)
  220. (defcustom f90-break-delimiters "[-+\\*/><=,% \t]"
  221. "Regexp matching delimiter characters at which lines may be broken.
  222. There are some common two-character tokens where one or more of
  223. the members matches this regexp. Although Fortran allows breaks
  224. within lexical tokens (provided the next line has a beginning ampersand),
  225. the constant `f90-no-break-re' ensures that such tokens are not split."
  226. :type 'regexp
  227. :safe 'stringp
  228. :group 'f90)
  229. (defcustom f90-break-before-delimiters t
  230. "Non-nil causes `f90-do-auto-fill' to break lines before delimiters."
  231. :type 'boolean
  232. :safe 'booleanp
  233. :group 'f90)
  234. (defcustom f90-auto-keyword-case nil
  235. "Automatic case conversion of keywords.
  236. The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil."
  237. :type '(choice (const downcase-word) (const upcase-word)
  238. (const capitalize-word) (const nil))
  239. :safe (lambda (value) (memq value '(downcase-word
  240. capitalize-word upcase-word nil)))
  241. :group 'f90)
  242. (defcustom f90-leave-line-no nil
  243. "If non-nil, line numbers are not left justified."
  244. :type 'boolean
  245. :safe 'booleanp
  246. :group 'f90)
  247. (defcustom f90-mode-hook nil
  248. "Hook run when entering F90 mode."
  249. :type 'hook
  250. ;; Not the only safe options, but some common ones.
  251. :safe (lambda (value) (member value '((f90-add-imenu-menu) nil)))
  252. :options '(f90-add-imenu-menu)
  253. :group 'f90)
  254. ;; User options end here.
  255. (defconst f90-keywords-re
  256. (regexp-opt '("allocatable" "allocate" "assign" "assignment" "backspace"
  257. "block" "call" "case" "character" "close" "common" "complex"
  258. "contains" "continue" "cycle" "data" "deallocate"
  259. "dimension" "do" "double" "else" "elseif" "elsewhere" "end"
  260. "enddo" "endfile" "endif" "entry" "equivalence" "exit"
  261. "external" "forall" "format" "function" "goto" "if"
  262. "implicit" "include" "inquire" "integer" "intent"
  263. "interface" "intrinsic" "logical" "module" "namelist" "none"
  264. "nullify" "only" "open" "operator" "optional" "parameter"
  265. "pause" "pointer" "precision" "print" "private" "procedure"
  266. "program" "public" "read" "real" "recursive" "result" "return"
  267. "rewind" "save" "select" "sequence" "stop" "subroutine"
  268. "target" "then" "type" "use" "where" "while" "write"
  269. ;; F95 keywords.
  270. "elemental" "pure"
  271. ;; F2003
  272. "abstract" "associate" "asynchronous" "bind" "class"
  273. "deferred" "enum" "enumerator" "extends" "extends_type_of"
  274. "final" "generic" "import" "non_intrinsic" "non_overridable"
  275. "nopass" "pass" "protected" "same_type_as" "value" "volatile"
  276. ;; F2008.
  277. "contiguous" "submodule" "concurrent" "codimension"
  278. "sync all" "sync memory" "critical" "image_index"
  279. ) 'words)
  280. "Regexp used by the function `f90-change-keywords'.")
  281. (defconst f90-keywords-level-3-re
  282. (regexp-opt
  283. '("allocatable" "allocate" "assign" "assignment" "backspace"
  284. "close" "deallocate" "dimension" "endfile" "entry" "equivalence"
  285. "external" "inquire" "intent" "intrinsic" "nullify" "only" "open"
  286. ;; FIXME operator and assignment should be F2003 procedures?
  287. "operator" "optional" "parameter" "pause" "pointer" "print" "private"
  288. "public" "read" "recursive" "result" "rewind" "save" "select"
  289. "sequence" "target" "write"
  290. ;; F95 keywords.
  291. "elemental" "pure"
  292. ;; F2003. asynchronous separate.
  293. "abstract" "deferred" "import" "final" "non_intrinsic" "non_overridable"
  294. "nopass" "pass" "protected" "value" "volatile"
  295. ;; F2008.
  296. ;; "concurrent" is only in the sense of "do [,] concurrent", but given
  297. ;; the [,] it's simpler to just do every instance (cf "do while").
  298. "contiguous" "concurrent" "codimension" "sync all" "sync memory"
  299. ) 'words)
  300. "Keyword-regexp for font-lock level >= 3.")
  301. (defconst f90-procedures-re
  302. (concat "\\<"
  303. (regexp-opt
  304. '("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint"
  305. "all" "allocated" "anint" "any" "asin" "associated"
  306. "atan" "atan2" "bit_size" "btest" "ceiling" "char" "cmplx"
  307. "conjg" "cos" "cosh" "count" "cshift" "date_and_time" "dble"
  308. "digits" "dim" "dot_product" "dprod" "eoshift" "epsilon"
  309. "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
  310. "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior"
  311. "ishft" "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt"
  312. "lle" "llt" "log" "log10" "logical" "matmul" "max"
  313. "maxexponent" "maxloc" "maxval" "merge" "min" "minexponent"
  314. "minloc" "minval" "mod" "modulo" "mvbits" "nearest" "nint"
  315. "not" "pack" "precision" "present" "product" "radix"
  316. ;; Real is taken out here to avoid highlighting declarations.
  317. "random_number" "random_seed" "range" ;; "real"
  318. "repeat" "reshape" "rrspacing" "scale" "scan"
  319. "selected_int_kind" "selected_real_kind" "set_exponent"
  320. "shape" "sign" "sin" "sinh" "size" "spacing" "spread" "sqrt"
  321. "sum" "system_clock" "tan" "tanh" "tiny" "transfer"
  322. "transpose" "trim" "ubound" "unpack" "verify"
  323. ;; F95 intrinsic functions.
  324. "null" "cpu_time"
  325. ;; F2003.
  326. "move_alloc" "command_argument_count" "get_command"
  327. "get_command_argument" "get_environment_variable"
  328. "selected_char_kind" "wait" "flush" "new_line"
  329. "extends" "extends_type_of" "same_type_as" "bind"
  330. ;; F2003 ieee_arithmetic intrinsic module.
  331. "ieee_support_underflow_control" "ieee_get_underflow_mode"
  332. "ieee_set_underflow_mode"
  333. ;; F2003 iso_c_binding intrinsic module.
  334. "c_loc" "c_funloc" "c_associated" "c_f_pointer"
  335. "c_f_procpointer"
  336. ;; F2008.
  337. "bge" "bgt" "ble" "blt" "dshiftl" "dshiftr" "leadz" "popcnt"
  338. "poppar" "trailz" "maskl" "maskr" "shifta" "shiftl" "shiftr"
  339. "merge_bits" "iall" "iany" "iparity" "storage_size"
  340. "bessel_j0" "bessel_j1" "bessel_jn"
  341. "bessel_y0" "bessel_y1" "bessel_yn"
  342. "erf" "erfc" "erfc_scaled" "gamma" "hypot" "log_gamma"
  343. "norm2" "parity" "findloc" "is_contiguous"
  344. "sync images" "lock" "unlock" "image_index"
  345. "lcobound" "ucobound" "num_images" "this_image"
  346. ;; F2008 iso_fortran_env module.
  347. "compiler_options" "compiler_version"
  348. ;; F2008 iso_c_binding module.
  349. "c_sizeof"
  350. ) t)
  351. ;; A left parenthesis to avoid highlighting non-procedures.
  352. "[ \t]*(")
  353. "Regexp whose first part matches F90 intrinsic procedures.")
  354. (defconst f90-operators-re
  355. (concat "\\."
  356. (regexp-opt '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
  357. "neqv" "not" "or" "true") t)
  358. "\\.")
  359. "Regexp matching intrinsic operators.")
  360. (defconst f90-hpf-keywords-re
  361. (regexp-opt
  362. ;; Intrinsic procedures.
  363. '("all_prefix" "all_scatter" "all_suffix" "any_prefix"
  364. "any_scatter" "any_suffix" "copy_prefix" "copy_scatter"
  365. "copy_suffix" "count_prefix" "count_scatter" "count_suffix"
  366. "grade_down" "grade_up"
  367. "hpf_alignment" "hpf_distribution" "hpf_template" "iall" "iall_prefix"
  368. "iall_scatter" "iall_suffix" "iany" "iany_prefix" "iany_scatter"
  369. "iany_suffix" "ilen" "iparity" "iparity_prefix"
  370. "iparity_scatter" "iparity_suffix" "leadz" "maxval_prefix"
  371. "maxval_scatter" "maxval_suffix" "minval_prefix" "minval_scatter"
  372. "minval_suffix" "number_of_processors" "parity"
  373. "parity_prefix" "parity_scatter" "parity_suffix" "popcnt" "poppar"
  374. "processors_shape" "product_prefix" "product_scatter"
  375. "product_suffix" "sum_prefix" "sum_scatter" "sum_suffix"
  376. ;; Directives.
  377. "align" "distribute" "dynamic" "independent" "inherit" "processors"
  378. "realign" "redistribute" "template"
  379. ;; Keywords.
  380. "block" "cyclic" "extrinsic" "new" "onto" "pure" "with") 'words)
  381. "Regexp for all HPF keywords, procedures and directives.")
  382. (defconst f90-constants-re
  383. (regexp-opt '( ;; F2003 iso_fortran_env constants.
  384. "iso_fortran_env"
  385. "input_unit" "output_unit" "error_unit"
  386. "iostat_end" "iostat_eor"
  387. "numeric_storage_size" "character_storage_size"
  388. "file_storage_size"
  389. ;; F2003 iso_c_binding constants.
  390. "iso_c_binding"
  391. "c_int" "c_short" "c_long" "c_long_long" "c_signed_char"
  392. "c_size_t"
  393. "c_int8_t" "c_int16_t" "c_int32_t" "c_int64_t"
  394. "c_int_least8_t" "c_int_least16_t" "c_int_least32_t"
  395. "c_int_least64_t"
  396. "c_int_fast8_t" "c_int_fast16_t" "c_int_fast32_t"
  397. "c_int_fast64_t"
  398. "c_intmax_t" "c_intptr_t"
  399. "c_float" "c_double" "c_long_double"
  400. "c_float_complex" "c_double_complex" "c_long_double_complex"
  401. "c_bool" "c_char"
  402. "c_null_char" "c_alert" "c_backspace" "c_form_feed"
  403. "c_new_line" "c_carriage_return" "c_horizontal_tab"
  404. "c_vertical_tab"
  405. "c_ptr" "c_funptr" "c_null_ptr" "c_null_funptr"
  406. "ieee_exceptions"
  407. "ieee_arithmetic"
  408. "ieee_features"
  409. ;; F2008 iso_fortran_env constants.
  410. "character_kinds" "int8" "int16" "int32" "int64"
  411. "integer_kinds" "iostat_inquire_internal_unit"
  412. "logical_kinds" "real_kinds" "real32" "real64" "real128"
  413. "lock_type" "atomic_int_kind" "atomic_logical_kind"
  414. ) 'words)
  415. "Regexp for Fortran intrinsic constants.")
  416. ;; cf f90-looking-at-type-like.
  417. (defun f90-typedef-matcher (limit)
  418. "Search for the start/end of the definition of a derived type, up to LIMIT.
  419. Set the match data so that subexpression 1,2 are the TYPE, and
  420. type-name parts, respectively."
  421. (let (found l)
  422. (while (and (re-search-forward "\\<\\(\\(?:end[ \t]*\\)?type\\)\\>[ \t]*"
  423. limit t)
  424. (not (setq found
  425. (progn
  426. (setq l (match-data))
  427. (unless (looking-at "\\(is\\>\\|(\\)")
  428. (when (if (looking-at "\\(\\sw+\\)")
  429. (goto-char (match-end 0))
  430. (re-search-forward
  431. "[ \t]*::[ \t]*\\(\\sw+\\)"
  432. (line-end-position) t))
  433. ;; 0 is wrong, but we don't use it.
  434. (set-match-data
  435. (append l (list (match-beginning 1)
  436. (match-end 1))))
  437. t)))))))
  438. found))
  439. (defvar f90-font-lock-keywords-1
  440. (list
  441. ;; Special highlighting of "module procedure".
  442. '("\\<\\(module[ \t]*procedure\\)\\>\\([^()\n]*::\\)?[ \t]*\\([^&!\n]*\\)"
  443. (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
  444. ;; Highlight definition of derived type.
  445. ;;; '("\\<\\(\\(?:end[ \t]*\\)?type\\)\\>\\([^()\n]*::\\)?[ \t]*\\(\\sw+\\)"
  446. ;;; (1 font-lock-keyword-face) (3 font-lock-function-name-face))
  447. '(f90-typedef-matcher
  448. (1 font-lock-keyword-face) (2 font-lock-function-name-face))
  449. ;; F2003. Prevent operators being highlighted as functions.
  450. '("\\<\\(\\(?:end[ \t]*\\)?interface[ \t]*\\(?:assignment\\|operator\\|\
  451. read\\|write\\)\\)[ \t]*(" (1 font-lock-keyword-face t))
  452. ;; Other functions and declarations. Named interfaces = F2003.
  453. ;; F2008: end submodule submodule_name.
  454. '("\\<\\(\\(?:end[ \t]*\\)?\\(program\\|\\(?:sub\\)?module\\|\
  455. function\\|associate\\|subroutine\\|interface\\)\\|use\\|call\\)\
  456. \\>[ \t]*\\(\\sw+\\)?"
  457. (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
  458. ;; F2008: submodule (parent_name) submodule_name.
  459. '("\\<\\(submodule\\)\\>[ \t]*([^)\n]+)[ \t]*\\(\\sw+\\)?"
  460. (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
  461. ;; F2003.
  462. '("\\<\\(use\\)[ \t]*,[ \t]*\\(\\(?:non_\\)?intrinsic\\)[ \t]*::[ \t]*\
  463. \\(\\sw+\\)"
  464. (1 font-lock-keyword-face) (2 font-lock-keyword-face)
  465. (3 font-lock-function-name-face))
  466. "\\<\\(\\(end[ \t]*\\)?block[ \t]*data\\|contains\\)\\>"
  467. ;; "abstract interface" is F2003.
  468. '("\\<abstract[ \t]*interface\\>" (0 font-lock-keyword-face t)))
  469. "This does fairly subdued highlighting of comments and function calls.")
  470. ;; NB not explicitly handling this, yet it seems to work.
  471. ;; type(...) function foo()
  472. (defun f90-typedec-matcher (limit)
  473. "Search for the declaration of variables of derived type, up to LIMIT.
  474. Set the match data so that subexpression 1,2 are the TYPE(...),
  475. and variable-name parts, respectively."
  476. ;; Matcher functions must return nil only when there are no more
  477. ;; matches within the search range.
  478. (let (found l)
  479. (while (and (re-search-forward "\\<\\(type\\|class\\)[ \t]*(" limit t)
  480. (not
  481. (setq found
  482. (condition-case nil
  483. (progn
  484. ;; Set l after this to just highlight
  485. ;; the "type" part.
  486. (backward-char 1)
  487. ;; Needed for: type( foo(...) ) :: bar
  488. (forward-sexp)
  489. (setq l (list (match-beginning 0) (point)))
  490. (skip-chars-forward " \t")
  491. (when
  492. (re-search-forward
  493. ;; type (foo) bar, qux
  494. (if (looking-at "\\sw+")
  495. "\\([^&!\n]+\\)"
  496. ;; type (foo), stuff :: bar, qux
  497. "::[ \t]*\\([^&!\n]+\\)")
  498. (line-end-position) t)
  499. (set-match-data
  500. (append (list (car l) (match-end 1))
  501. l (list (match-beginning 1)
  502. (match-end 1))))
  503. t))
  504. (error nil))))))
  505. found))
  506. (defvar f90-font-lock-keywords-2
  507. (append
  508. f90-font-lock-keywords-1
  509. (list
  510. ;; Variable declarations (avoid the real function call).
  511. ;; NB by accident (?), this correctly fontifies the "integer" in:
  512. ;; integer () function foo ()
  513. ;; because "() function foo ()" matches \\3.
  514. ;; The "pure" part does not really belong here, but was added to
  515. ;; exploit that hack.
  516. ;; The "function foo" bit is correctly fontified by keywords-1.
  517. ;; TODO ? actually check for balanced parens in that case.
  518. '("^[ \t0-9]*\\(?:pure\\|elemental\\)?[ \t]*\
  519. \\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
  520. enumerator\\|generic\\|procedure\\|logical\\|double[ \t]*precision\\)\
  521. \\(.*::\\|[ \t]*(.*)\\)?\\([^&!\n]*\\)"
  522. (1 font-lock-type-face t) (4 font-lock-variable-name-face t))
  523. ;; Derived type/class variables.
  524. ;; TODO ? If we just highlighted the "type" part, rather than
  525. ;; "type(...)", this could be in the previous expression. And this
  526. ;; would be consistent with integer( kind=8 ), etc.
  527. '(f90-typedec-matcher
  528. (1 font-lock-type-face) (2 font-lock-variable-name-face))
  529. ;; "real function foo (args)". Must override previous. Note hack
  530. ;; to get "args" unhighlighted again. Might not always be right,
  531. ;; but probably better than leaving them as variables.
  532. ;; NB not explicitly handling this case:
  533. ;; integer( kind=1 ) function foo()
  534. ;; thanks to the happy accident described above.
  535. ;; Not anchored, so don't need to worry about "pure" etc.
  536. '("\\<\\(\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
  537. logical\\|double[ \t]*precision\\|\
  538. \\(?:type\\|class\\)[ \t]*([ \t]*\\sw+[ \t]*)\\)[ \t]*\\)\
  539. \\(function\\)\\>[ \t]*\\(\\sw+\\)[ \t]*\\(([^&!\n]*)\\)"
  540. (1 font-lock-type-face t) (4 font-lock-keyword-face t)
  541. (5 font-lock-function-name-face t) (6 'default t))
  542. ;; enum (F2003; must be followed by ", bind(C)").
  543. '("\\<\\(enum\\)[ \t]*," (1 font-lock-keyword-face))
  544. ;; end do, enum (F2003), if, select, where, and forall constructs.
  545. ;; block, critical (F2008).
  546. ;; Note that "block data" may get somewhat mixed up with F2008 blocks,
  547. ;; but since the former is obsolete I'm not going to worry about it.
  548. '("\\<\\(end[ \t]*\\(do\\|if\\|enum\\|select\\|forall\\|where\\|\
  549. block\\|critical\\)\\)\\>\
  550. \\([ \t]+\\(\\sw+\\)\\)?"
  551. (1 font-lock-keyword-face) (3 font-lock-constant-face nil t))
  552. '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|\
  553. do\\([ \t]*while\\)?\\|select[ \t]*\\(?:case\\|type\\)\\|where\\|\
  554. forall\\|block\\|critical\\)\\)\\>"
  555. (2 font-lock-constant-face nil t) (3 font-lock-keyword-face))
  556. ;; Implicit declaration.
  557. '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\
  558. \\|enumerator\\|procedure\\|\
  559. logical\\|double[ \t]*precision\\|type[ \t]*(\\sw+)\\|none\\)[ \t]*"
  560. (1 font-lock-keyword-face) (2 font-lock-type-face))
  561. '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/"
  562. (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
  563. "\\<else\\([ \t]*if\\|where\\)?\\>"
  564. '("\\(&\\)[ \t]*\\(!\\|$\\)" (1 font-lock-keyword-face))
  565. "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
  566. '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)?\\>"
  567. (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
  568. '("\\<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
  569. ;; F2003 "class default".
  570. '("\\<\\(class\\)[ \t]*default" . 1)
  571. ;; F2003 "type is" in a "select type" block.
  572. '("\\<\\(\\(type\\|class\\)[ \t]*is\\)[ \t]*(" (1 font-lock-keyword-face t))
  573. '("\\<\\(do\\|go[ \t]*to\\)\\>[ \t]*\\([0-9]+\\)"
  574. (1 font-lock-keyword-face) (2 font-lock-constant-face))
  575. ;; Line numbers (lines whose first character after number is letter).
  576. '("^[ \t]*\\([0-9]+\\)[ \t]*[a-z]+" (1 font-lock-constant-face t))
  577. ;; Override eg for "#include".
  578. '("^#[ \t]*\\w+" (0 font-lock-preprocessor-face t)
  579. ("\\<defined\\>" nil nil (0 font-lock-preprocessor-face)))
  580. '("^#" ("\\(&&\\|||\\)" nil nil (0 font-lock-constant-face t)))
  581. '("^#[ \t]*define[ \t]+\\(\\w+\\)(" (1 font-lock-function-name-face))
  582. '("^#[ \t]*define[ \t]+\\(\\w+\\)" (1 font-lock-variable-name-face))
  583. '("^#[ \t]*include[ \t]+\\(<.+>\\)" (1 font-lock-string-face))))
  584. "Highlights declarations, do-loops and other constructs.")
  585. (defvar f90-font-lock-keywords-3
  586. (append f90-font-lock-keywords-2
  587. (list
  588. f90-keywords-level-3-re
  589. f90-operators-re
  590. ;; FIXME why isn't this font-lock-builtin-face, which
  591. ;; otherwise we hardly use, as in fortran.el?
  592. (list f90-procedures-re '(1 font-lock-keyword-face keep))
  593. "\\<real\\>" ; avoid overwriting real defs
  594. ;; As an attribute, but not as an optional argument.
  595. '("\\<\\(asynchronous\\)[ \t]*[^=]" . 1)))
  596. "Highlights all F90 keywords and intrinsic procedures.")
  597. (defvar f90-font-lock-keywords-4
  598. (append f90-font-lock-keywords-3
  599. (list (cons f90-constants-re 'font-lock-constant-face)
  600. f90-hpf-keywords-re))
  601. "Highlights all F90 and HPF keywords and constants.")
  602. (defvar f90-font-lock-keywords
  603. f90-font-lock-keywords-2
  604. "*Default expressions to highlight in F90 mode.
  605. Can be overridden by the value of `font-lock-maximum-decoration'.")
  606. (defvar f90-mode-syntax-table
  607. (let ((table (make-syntax-table)))
  608. (modify-syntax-entry ?\! "<" table) ; begin comment
  609. (modify-syntax-entry ?\n ">" table) ; end comment
  610. ;; FIXME: This goes against the convention: it should be "_".
  611. (modify-syntax-entry ?_ "w" table) ; underscore in names
  612. (modify-syntax-entry ?\' "\"" table) ; string quote
  613. (modify-syntax-entry ?\" "\"" table) ; string quote
  614. ;; FIXME: We used to set ` to word syntax for the benefit of abbrevs, but
  615. ;; we do not need it any more. Not sure if it should be "_" or "." now.
  616. (modify-syntax-entry ?\` "_" table)
  617. (modify-syntax-entry ?\r " " table) ; return is whitespace
  618. (modify-syntax-entry ?+ "." table) ; punctuation
  619. (modify-syntax-entry ?- "." table)
  620. (modify-syntax-entry ?= "." table)
  621. (modify-syntax-entry ?* "." table)
  622. (modify-syntax-entry ?/ "." table)
  623. (modify-syntax-entry ?% "." table) ; bug#8820
  624. ;; I think that the f95 standard leaves the behavior of \
  625. ;; unspecified, but that f2k will require it to be non-special.
  626. ;; Use `f90-backslash-not-special' to change.
  627. (modify-syntax-entry ?\\ "\\" table) ; escape chars
  628. table)
  629. "Syntax table used in F90 mode.")
  630. (defvar f90-mode-map
  631. (let ((map (make-sparse-keymap)))
  632. (define-key map "`" 'f90-abbrev-start)
  633. (define-key map "\C-c;" 'f90-comment-region)
  634. (define-key map "\C-\M-a" 'f90-beginning-of-subprogram)
  635. (define-key map "\C-\M-e" 'f90-end-of-subprogram)
  636. (define-key map "\C-\M-h" 'f90-mark-subprogram)
  637. (define-key map "\C-\M-n" 'f90-end-of-block)
  638. (define-key map "\C-\M-p" 'f90-beginning-of-block)
  639. (define-key map "\C-\M-q" 'f90-indent-subprogram)
  640. (define-key map "\C-j" 'f90-indent-new-line) ; LFD equals C-j
  641. ;;; (define-key map "\r" 'newline)
  642. (define-key map "\C-c\r" 'f90-break-line)
  643. ;;; (define-key map [M-return] 'f90-break-line)
  644. (define-key map "\C-c\C-a" 'f90-previous-block)
  645. (define-key map "\C-c\C-e" 'f90-next-block)
  646. (define-key map "\C-c\C-d" 'f90-join-lines)
  647. (define-key map "\C-c\C-f" 'f90-fill-region)
  648. (define-key map "\C-c\C-p" 'f90-previous-statement)
  649. (define-key map "\C-c\C-n" 'f90-next-statement)
  650. (define-key map "\C-c]" 'f90-insert-end)
  651. (define-key map "\C-c\C-w" 'f90-insert-end)
  652. ;; Standard tab binding will call this, and also handle regions.
  653. ;;; (define-key map "\t" 'f90-indent-line)
  654. (define-key map "," 'f90-electric-insert)
  655. (define-key map "+" 'f90-electric-insert)
  656. (define-key map "-" 'f90-electric-insert)
  657. (define-key map "*" 'f90-electric-insert)
  658. (define-key map "/" 'f90-electric-insert)
  659. (easy-menu-define f90-menu map "Menu for F90 mode."
  660. `("F90"
  661. ("Customization"
  662. ,(custom-menu-create 'f90)
  663. ;; FIXME useless?
  664. ["Set" Custom-set :active t
  665. :help "Set current value of all edited settings in the buffer"]
  666. ["Save" Custom-save :active t
  667. :help "Set and save all edited settings"]
  668. ["Reset to Current" Custom-reset-current :active t
  669. :help "Reset all edited settings to current"]
  670. ["Reset to Saved" Custom-reset-saved :active t
  671. :help "Reset all edited or set settings to saved"]
  672. ["Reset to Standard Settings" Custom-reset-standard :active t
  673. :help "Erase all customizations in buffer"]
  674. )
  675. "--"
  676. ["Indent Subprogram" f90-indent-subprogram t]
  677. ["Mark Subprogram" f90-mark-subprogram :active t :help
  678. "Mark the end of the current subprogram, move point to the start"]
  679. ["Beginning of Subprogram" f90-beginning-of-subprogram :active t
  680. :help "Move point to the start of the current subprogram"]
  681. ["End of Subprogram" f90-end-of-subprogram :active t
  682. :help "Move point to the end of the current subprogram"]
  683. "--"
  684. ["(Un)Comment Region" f90-comment-region :active mark-active
  685. :help "Comment or uncomment the region"]
  686. ["Indent Region" f90-indent-region :active mark-active]
  687. ["Fill Region" f90-fill-region :active mark-active
  688. :help "Fill long lines in the region"]
  689. "--"
  690. ["Break Line at Point" f90-break-line :active t
  691. :help "Break the current line at point"]
  692. ["Join with Previous Line" f90-join-lines :active t
  693. :help "Join the current line to the previous one"]
  694. ["Insert Block End" f90-insert-end :active t
  695. :help "Insert an end statement for the current code block"]
  696. "--"
  697. ("Highlighting"
  698. :help "Fontify this buffer to varying degrees"
  699. ["Toggle font-lock-mode" font-lock-mode :selected font-lock-mode
  700. :style toggle :help "Fontify text in this buffer"]
  701. "--"
  702. ["Light highlighting (level 1)" f90-font-lock-1 t]
  703. ["Moderate highlighting (level 2)" f90-font-lock-2 t]
  704. ["Heavy highlighting (level 3)" f90-font-lock-3 t]
  705. ["Maximum highlighting (level 4)" f90-font-lock-4 t]
  706. )
  707. ("Change Keyword Case"
  708. :help "Change the case of keywords in the buffer or region"
  709. ["Upcase Keywords (buffer)" f90-upcase-keywords t]
  710. ["Capitalize Keywords (buffer)" f90-capitalize-keywords t]
  711. ["Downcase Keywords (buffer)" f90-downcase-keywords t]
  712. "--"
  713. ["Upcase Keywords (region)" f90-upcase-region-keywords
  714. mark-active]
  715. ["Capitalize Keywords (region)" f90-capitalize-region-keywords
  716. mark-active]
  717. ["Downcase Keywords (region)" f90-downcase-region-keywords
  718. mark-active]
  719. )
  720. "--"
  721. ["Toggle Auto Fill" auto-fill-mode :selected auto-fill-function
  722. :style toggle
  723. :help "Automatically fill text while typing in this buffer"]
  724. ["Toggle Abbrev Mode" abbrev-mode :selected abbrev-mode
  725. :style toggle :help "Expand abbreviations while typing in this buffer"]
  726. ["Add Imenu Menu" f90-add-imenu-menu
  727. :active (not (lookup-key (current-local-map) [menu-bar index]))
  728. :included (fboundp 'imenu-add-to-menubar)
  729. :help "Add an index menu to the menu-bar"
  730. ]))
  731. map)
  732. "Keymap used in F90 mode.")
  733. (defun f90-font-lock-n (n)
  734. "Set `font-lock-keywords' to F90 level N keywords."
  735. (font-lock-mode 1)
  736. (setq font-lock-keywords
  737. (symbol-value (intern-soft (format "f90-font-lock-keywords-%d" n))))
  738. (font-lock-fontify-buffer))
  739. (defun f90-font-lock-1 ()
  740. "Set `font-lock-keywords' to `f90-font-lock-keywords-1'."
  741. (interactive)
  742. (f90-font-lock-n 1))
  743. (defun f90-font-lock-2 ()
  744. "Set `font-lock-keywords' to `f90-font-lock-keywords-2'."
  745. (interactive)
  746. (f90-font-lock-n 2))
  747. (defun f90-font-lock-3 ()
  748. "Set `font-lock-keywords' to `f90-font-lock-keywords-3'."
  749. (interactive)
  750. (f90-font-lock-n 3))
  751. (defun f90-font-lock-4 ()
  752. "Set `font-lock-keywords' to `f90-font-lock-keywords-4'."
  753. (interactive)
  754. (f90-font-lock-n 4))
  755. ;; Regexps for finding program structures.
  756. (defconst f90-blocks-re
  757. (concat "\\(block[ \t]*data\\|"
  758. (regexp-opt '("do" "if" "interface" "function" "module" "program"
  759. "select" "subroutine" "type" "where" "forall"
  760. ;; F2003.
  761. "enum" "associate"
  762. ;; F2008.
  763. "submodule" "block" "critical"))
  764. "\\)\\>")
  765. "Regexp potentially indicating a \"block\" of F90 code.")
  766. (defconst f90-program-block-re
  767. (regexp-opt '("program" "module" "subroutine" "function" "submodule") 'paren)
  768. "Regexp used to locate the start/end of a \"subprogram\".")
  769. ;; "class is" is F2003.
  770. (defconst f90-else-like-re
  771. "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\|\
  772. \\(class\\|type\\)[ \t]*is[ \t]*(\\|class[ \t]*default\\)"
  773. "Regexp matching an ELSE IF, ELSEWHERE, CASE, CLASS/TYPE IS statement.")
  774. (defconst f90-end-if-re
  775. (concat "end[ \t]*"
  776. (regexp-opt '("if" "select" "where" "forall") 'paren)
  777. "\\>")
  778. "Regexp matching the end of an IF, SELECT, WHERE, FORALL block.")
  779. (defconst f90-end-type-re
  780. "end[ \t]*\\(type\\|enum\\|interface\\|block[ \t]*data\\)\\>"
  781. "Regexp matching the end of a TYPE, ENUM, INTERFACE, BLOCK DATA section.")
  782. (defconst f90-end-associate-re
  783. "end[ \t]*associate\\>"
  784. "Regexp matching the end of an ASSOCIATE block.")
  785. ;; This is for a TYPE block, not a variable of derived TYPE.
  786. ;; Hence no need to add CLASS for F2003.
  787. (defconst f90-type-def-re
  788. ;; type word
  789. ;; type :: word
  790. ;; type, stuff :: word
  791. ;; type, bind(c) :: word
  792. ;; NOT "type ("
  793. "\\<\\(type\\)\\>\\(?:\\(?:[^()\n]*\\|\
  794. .*,[ \t]*bind[ \t]*([ \t]*c[ \t]*)[ \t]*\\)::\\)?[ \t]*\\(\\sw+\\)"
  795. "Regexp matching the definition of a derived type.")
  796. (defconst f90-typeis-re
  797. "\\<\\(class\\|type\\)[ \t]*is[ \t]*("
  798. "Regexp matching a CLASS/TYPE IS statement.")
  799. (defconst f90-no-break-re
  800. (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=" "(/" "/)") 'paren)
  801. "Regexp specifying two-character tokens not to split when breaking lines.
  802. Each token has one or more of the characters from `f90-break-delimiters'.
  803. Note that if only one of the characters is from that variable,
  804. then the presence of the token here allows a line-break before or
  805. after the other character, where a break would not normally be
  806. allowed. This minor issue currently only affects \"(/\" and \"/)\".")
  807. (defvar f90-cache-position nil
  808. "Temporary position used to speed up region operations.")
  809. (make-variable-buffer-local 'f90-cache-position)
  810. ;; Hideshow support.
  811. (defconst f90-end-block-re
  812. (concat "^[ \t0-9]*\\<end[ \t]*"
  813. (regexp-opt '("do" "if" "forall" "function" "interface"
  814. "module" "program" "select" "subroutine"
  815. "type" "where" "enum" "associate" "submodule"
  816. "block" "critical") t)
  817. "\\>")
  818. "Regexp matching the end of an F90 \"block\", from the line start.
  819. Used in the F90 entry in `hs-special-modes-alist'.")
  820. ;; Ignore the fact that FUNCTION, SUBROUTINE, WHERE, FORALL have a
  821. ;; following "(". DO, CASE, IF can have labels.
  822. (defconst f90-start-block-re
  823. (concat
  824. "^[ \t0-9]*" ; statement number
  825. "\\(\\("
  826. "\\(\\sw+[ \t]*:[ \t]*\\)?" ; structure label
  827. "\\(do\\|select[ \t]*\\(case\\|type\\)\\|"
  828. ;; See comments in fortran-start-block-re for the problems of IF.
  829. "if[ \t]*(\\(.*\\|"
  830. ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|"
  831. ;; Distinguish WHERE block from isolated WHERE.
  832. "\\(where\\|forall\\)[ \t]*(.*)[ \t]*\\(!\\|$\\)\\)\\)"
  833. "\\|"
  834. ;; Avoid F2003 "type is" in "select type",
  835. ;; and also variables of derived type "type (foo)".
  836. ;; "type, foo" must be a block (?).
  837. "type[ \t,]\\("
  838. "[^i(!\n\"\& \t]\\|" ; not-i(
  839. "i[^s!\n\"\& \t]\\|" ; i not-s
  840. "is\\sw\\)\\|"
  841. ;; "abstract interface" is F2003; "submodule" is F2008.
  842. "program\\|\\(?:abstract[ \t]*\\)?interface\\|\\(?:sub\\)?module\\|"
  843. ;; "enum", but not "enumerator".
  844. "function\\|subroutine\\|enum[^e]\\|associate\\|block\\|critical"
  845. "\\)"
  846. "[ \t]*")
  847. "Regexp matching the start of an F90 \"block\", from the line start.
  848. A simple regexp cannot do this in fully correct fashion, so this
  849. tries to strike a compromise between complexity and flexibility.
  850. Used in the F90 entry in `hs-special-modes-alist'.")
  851. ;; hs-special-modes-alist is autoloaded.
  852. (add-to-list 'hs-special-modes-alist
  853. `(f90-mode ,f90-start-block-re ,f90-end-block-re
  854. "!" f90-end-of-block nil))
  855. ;; Imenu support.
  856. ;; FIXME trivial to extend this to enum. Worth it?
  857. (defun f90-imenu-type-matcher ()
  858. "Search backward for the start of a derived type.
  859. Set subexpression 1 in the match-data to the name of the type."
  860. (let (found)
  861. (while (and (re-search-backward "^[ \t0-9]*type[ \t]*" nil t)
  862. (not (setq found
  863. (save-excursion
  864. (goto-char (match-end 0))
  865. (unless (looking-at "\\(is\\>\\|(\\)")
  866. (or (looking-at "\\(\\sw+\\)")
  867. (re-search-forward
  868. "[ \t]*::[ \t]*\\(\\sw+\\)"
  869. (line-end-position) t))))))))
  870. found))
  871. (defvar f90-imenu-generic-expression
  872. (let ((good-char "[^!\"\&\n \t]") (not-e "[^e!\n\"\& \t]")
  873. (not-n "[^n!\n\"\& \t]") (not-d "[^d!\n\"\& \t]")
  874. ;; (not-ib "[^i(!\n\"\& \t]") (not-s "[^s!\n\"\& \t]")
  875. )
  876. (list
  877. '(nil "^[ \t0-9]*program[ \t]+\\(\\sw+\\)" 1)
  878. '("Submodules" "^[ \t0-9]*submodule[ \t]*([^)\n]+)[ \t]*\
  879. \\(\\sw+\\)[ \t]*\\(!\\|$\\)" 1)
  880. '("Modules" "^[ \t0-9]*module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)" 1)
  881. (list "Types" 'f90-imenu-type-matcher 1)
  882. ;; Does not handle: "type[, stuff] :: foo".
  883. ;;; (format "^[ \t0-9]*type[ \t]+\\(\\(%s\\|i%s\\|is\\sw\\)\\sw*\\)"
  884. ;;; not-ib not-s)
  885. ;;; 1)
  886. ;; Can't get the subexpression numbers to match in the two branches.
  887. ;;; (format "^[ \t0-9]*type\\([ \t]*,.*\\(::\\)[ \t]*\\(\\sw+\\)\\|[ \t]+\\(\\(%s\\|i%s\\|is\\sw\\)\\sw*\\)\\)" not-ib not-s)
  888. ;;; 3)
  889. (list
  890. "Procedures"
  891. (concat
  892. "^[ \t0-9]*"
  893. "\\("
  894. ;; At least three non-space characters before function/subroutine.
  895. ;; Check that the last three non-space characters do not spell E N D.
  896. "[^!\"\&\n]*\\("
  897. not-e good-char good-char "\\|"
  898. good-char not-n good-char "\\|"
  899. good-char good-char not-d "\\)"
  900. "\\|"
  901. ;; Less than three non-space characters before function/subroutine.
  902. good-char "?" good-char "?"
  903. "\\)"
  904. "[ \t]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)")
  905. 4)))
  906. "Value for `imenu-generic-expression' in F90 mode.")
  907. (defun f90-add-imenu-menu ()
  908. "Add an imenu menu to the menubar."
  909. (interactive)
  910. (if (lookup-key (current-local-map) [menu-bar index])
  911. (message "%s" "F90-imenu already exists.")
  912. (imenu-add-to-menubar "F90-imenu")
  913. (redraw-frame (selected-frame))))
  914. ;; Abbrevs have generally two letters, except standard types `c, `i, `r, `t.
  915. (define-abbrev-table 'f90-mode-abbrev-table
  916. (mapcar (lambda (e) (list (car e) (cdr e) nil :system t))
  917. '(("`al" . "allocate" )
  918. ("`ab" . "allocatable" )
  919. ("`ai" . "abstract interface")
  920. ("`as" . "assignment" )
  921. ("`asy" . "asynchronous" )
  922. ("`ba" . "backspace" )
  923. ("`bd" . "block data" )
  924. ("`bl" . "block" )
  925. ("`c" . "character" )
  926. ("`cl" . "close" )
  927. ("`cm" . "common" )
  928. ("`cx" . "complex" )
  929. ("`cn" . "contains" )
  930. ("`cr" . "critical" )
  931. ("`cy" . "cycle" )
  932. ("`de" . "deallocate" )
  933. ("`df" . "define" )
  934. ("`di" . "dimension" )
  935. ("`dp" . "double precision")
  936. ("`dw" . "do while" )
  937. ("`el" . "else" )
  938. ("`eli" . "else if" )
  939. ("`elw" . "elsewhere" )
  940. ("`em" . "elemental" )
  941. ("`e" . "enumerator" )
  942. ("`eq" . "equivalence" )
  943. ("`ex" . "external" )
  944. ("`ey" . "entry" )
  945. ("`fl" . "forall" )
  946. ("`fo" . "format" )
  947. ("`fu" . "function" )
  948. ("`fa" . ".false." )
  949. ("`im" . "implicit none")
  950. ("`in" . "include" )
  951. ("`i" . "integer" )
  952. ("`it" . "intent" )
  953. ("`if" . "interface" )
  954. ("`lo" . "logical" )
  955. ("`mo" . "module" )
  956. ("`na" . "namelist" )
  957. ("`nu" . "nullify" )
  958. ("`op" . "optional" )
  959. ("`pa" . "parameter" )
  960. ("`po" . "pointer" )
  961. ("`pr" . "print" )
  962. ("`pi" . "private" )
  963. ("`pm" . "program" )
  964. ("`pr" . "protected" )
  965. ("`pu" . "public" )
  966. ("`r" . "real" )
  967. ("`rc" . "recursive" )
  968. ("`rt" . "return" )
  969. ("`rw" . "rewind" )
  970. ("`se" . "select" )
  971. ("`sq" . "sequence" )
  972. ("`su" . "subroutine" )
  973. ("`ta" . "target" )
  974. ("`tr" . ".true." )
  975. ("`t" . "type" )
  976. ("`vo" . "volatile" )
  977. ("`wh" . "where" )
  978. ("`wr" . "write" )))
  979. "Abbrev table for F90 mode."
  980. ;; Accept ` as the first char of an abbrev. Also allow _ in abbrevs.
  981. :regexp "\\(?:[^[:word:]_`]\\|^\\)\\(`?[[:word:]_]+\\)[^[:word:]_]*")
  982. ;;;###autoload
  983. (define-derived-mode f90-mode prog-mode "F90"
  984. "Major mode for editing Fortran 90,95 code in free format.
  985. For fixed format code, use `fortran-mode'.
  986. \\[f90-indent-line] indents the current line.
  987. \\[f90-indent-new-line] indents current line and creates a new\
  988. indented line.
  989. \\[f90-indent-subprogram] indents the current subprogram.
  990. Type `? or `\\[help-command] to display a list of built-in\
  991. abbrevs for F90 keywords.
  992. Key definitions:
  993. \\{f90-mode-map}
  994. Variables controlling indentation style and extra features:
  995. `f90-do-indent'
  996. Extra indentation within do blocks (default 3).
  997. `f90-if-indent'
  998. Extra indentation within if/select/where/forall blocks (default 3).
  999. `f90-type-indent'
  1000. Extra indentation within type/enum/interface/block-data blocks (default 3).
  1001. `f90-program-indent'
  1002. Extra indentation within program/module/subroutine/function blocks
  1003. (default 2).
  1004. `f90-associate-indent'
  1005. Extra indentation within associate blocks (default 2).
  1006. `f90-critical-indent'
  1007. Extra indentation within critical/block blocks (default 2).
  1008. `f90-continuation-indent'
  1009. Extra indentation applied to continuation lines (default 5).
  1010. `f90-comment-region'
  1011. String inserted by function \\[f90-comment-region] at start of each
  1012. line in region (default \"!!!$\").
  1013. `f90-indented-comment-re'
  1014. Regexp determining the type of comment to be intended like code
  1015. (default \"!\").
  1016. `f90-directive-comment-re'
  1017. Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented
  1018. (default \"!hpf\\\\$\").
  1019. `f90-break-delimiters'
  1020. Regexp holding list of delimiters at which lines may be broken
  1021. (default \"[-+*/><=,% \\t]\").
  1022. `f90-break-before-delimiters'
  1023. Non-nil causes `f90-do-auto-fill' to break lines before delimiters
  1024. (default t).
  1025. `f90-beginning-ampersand'
  1026. Automatic insertion of \& at beginning of continuation lines (default t).
  1027. `f90-smart-end'
  1028. From an END statement, check and fill the end using matching block start.
  1029. Allowed values are 'blink, 'no-blink, and nil, which determine
  1030. whether to blink the matching beginning (default 'blink).
  1031. `f90-auto-keyword-case'
  1032. Automatic change of case of keywords (default nil).
  1033. The possibilities are 'downcase-word, 'upcase-word, 'capitalize-word.
  1034. `f90-leave-line-no'
  1035. Do not left-justify line numbers (default nil).
  1036. Turning on F90 mode calls the value of the variable `f90-mode-hook'
  1037. with no args, if that value is non-nil."
  1038. :group 'f90
  1039. :abbrev-table f90-mode-abbrev-table
  1040. (set (make-local-variable 'indent-line-function) 'f90-indent-line)
  1041. (set (make-local-variable 'indent-region-function) 'f90-indent-region)
  1042. (set (make-local-variable 'comment-start) "!")
  1043. (set (make-local-variable 'comment-start-skip) "!+ *")
  1044. (set (make-local-variable 'comment-indent-function) 'f90-comment-indent)
  1045. (set (make-local-variable 'abbrev-all-caps) t)
  1046. (set (make-local-variable 'normal-auto-fill-function) 'f90-do-auto-fill)
  1047. (setq indent-tabs-mode nil) ; auto buffer local
  1048. (set (make-local-variable 'font-lock-defaults)
  1049. '((f90-font-lock-keywords f90-font-lock-keywords-1
  1050. f90-font-lock-keywords-2
  1051. f90-font-lock-keywords-3
  1052. f90-font-lock-keywords-4)
  1053. nil t))
  1054. (set (make-local-variable 'imenu-case-fold-search) t)
  1055. (set (make-local-variable 'imenu-generic-expression)
  1056. f90-imenu-generic-expression)
  1057. (set (make-local-variable 'beginning-of-defun-function)
  1058. 'f90-beginning-of-subprogram)
  1059. (set (make-local-variable 'end-of-defun-function) 'f90-end-of-subprogram)
  1060. (set (make-local-variable 'add-log-current-defun-function)
  1061. #'f90-current-defun))
  1062. ;; Inline-functions.
  1063. (defsubst f90-in-string ()
  1064. "Return non-nil if point is inside a string.
  1065. Checks from `point-min', or `f90-cache-position', if that is non-nil
  1066. and lies before point."
  1067. (let ((beg-pnt
  1068. (if (and f90-cache-position (> (point) f90-cache-position))
  1069. f90-cache-position
  1070. (point-min))))
  1071. (nth 3 (parse-partial-sexp beg-pnt (point)))))
  1072. (defsubst f90-in-comment ()
  1073. "Return non-nil if point is inside a comment.
  1074. Checks from `point-min', or `f90-cache-position', if that is non-nil
  1075. and lies before point."
  1076. (let ((beg-pnt
  1077. (if (and f90-cache-position (> (point) f90-cache-position))
  1078. f90-cache-position
  1079. (point-min))))
  1080. (nth 4 (parse-partial-sexp beg-pnt (point)))))
  1081. (defsubst f90-line-continued ()
  1082. "Return t if the current line is a continued one.
  1083. This includes comment lines embedded in continued lines, but
  1084. not the last line of a continued statement."
  1085. (save-excursion
  1086. (beginning-of-line)
  1087. (while (and (looking-at "[ \t]*\\(!\\|$\\)") (zerop (forward-line -1))))
  1088. (end-of-line)
  1089. (while (f90-in-comment)
  1090. (search-backward "!" (line-beginning-position))
  1091. (skip-chars-backward "!"))
  1092. (skip-chars-backward " \t")
  1093. (= (preceding-char) ?&)))
  1094. ;; GM this is not right, eg a continuation line starting with a number.
  1095. ;; Need f90-code-start-position function.
  1096. ;; And yet, things seems to work with this...
  1097. ;; cf f90-indent-line
  1098. ;; (beginning-of-line) ; digits after & \n are not line-nos
  1099. ;; (if (not (save-excursion (and (f90-previous-statement)
  1100. ;; (f90-line-continued))))
  1101. ;; (f90-indent-line-no)
  1102. (defsubst f90-current-indentation ()
  1103. "Return indentation of current line.
  1104. Line-numbers are considered whitespace characters."
  1105. (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")))
  1106. (defsubst f90-indent-to (col &optional no-line-number)
  1107. "Indent current line to column COL.
  1108. If optional argument NO-LINE-NUMBER is nil, jump over a possible
  1109. line-number before indenting."
  1110. (beginning-of-line)
  1111. (or no-line-number
  1112. (skip-chars-forward " \t0-9"))
  1113. (delete-horizontal-space)
  1114. ;; Leave >= 1 space after line number.
  1115. (indent-to col (if (zerop (current-column)) 0 1)))
  1116. (defsubst f90-get-present-comment-type ()
  1117. "If point lies within a comment, return the string starting the comment.
  1118. For example, \"!\" or \"!!\", followed by the appropriate amount of
  1119. whitespace, if any."
  1120. ;; Include the whitespace for consistent auto-filling of comment blocks.
  1121. (save-excursion
  1122. (when (f90-in-comment)
  1123. (beginning-of-line)
  1124. (re-search-forward "!+[ \t]*" (line-end-position))
  1125. (while (f90-in-string)
  1126. (re-search-forward "!+[ \t]*" (line-end-position)))
  1127. (match-string-no-properties 0))))
  1128. (defsubst f90-equal-symbols (a b)
  1129. "Compare strings A and B neglecting case and allowing for nil value."
  1130. (equal (if a (downcase a) nil)
  1131. (if b (downcase b) nil)))
  1132. (defsubst f90-looking-at-do ()
  1133. "Return (\"do\" NAME) if a do statement starts after point.
  1134. NAME is nil if the statement has no label."
  1135. (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\>")
  1136. (list (match-string 3) (match-string 2))))
  1137. (defsubst f90-looking-at-select-case ()
  1138. "Return (\"select\" NAME) if a select statement starts after point.
  1139. NAME is nil if the statement has no label."
  1140. (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
  1141. \\(select\\)[ \t]*\\(case\\|type\\)[ \t]*(")
  1142. (list (match-string 3) (match-string 2))))
  1143. (defsubst f90-looking-at-if-then ()
  1144. "Return (\"if\" NAME) if an if () then statement starts after point.
  1145. NAME is nil if the statement has no label."
  1146. (save-excursion
  1147. (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\>")
  1148. (let ((struct (match-string 3))
  1149. (label (match-string 2))
  1150. (pos (scan-lists (point) 1 0)))
  1151. (and pos (goto-char pos))
  1152. (skip-chars-forward " \t")
  1153. (if (or (looking-at "then\\>")
  1154. (when (f90-line-continued)
  1155. (f90-next-statement)
  1156. (skip-chars-forward " \t0-9&")
  1157. (looking-at "then\\>")))
  1158. (list struct label))))))
  1159. ;; FIXME label?
  1160. (defsubst f90-looking-at-associate ()
  1161. "Return (\"associate\") if an associate block starts after point."
  1162. (if (looking-at "\\<\\(associate\\)[ \t]*(")
  1163. (list (match-string 1))))
  1164. (defsubst f90-looking-at-critical ()
  1165. "Return (KIND NAME) if a critical or block block starts after point."
  1166. (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(critical\\|block\\)\\>")
  1167. (let ((struct (match-string 3))
  1168. (label (match-string 2)))
  1169. (if (or (not (string-equal "block" struct))
  1170. (save-excursion
  1171. (skip-chars-forward " \t")
  1172. (not (looking-at "data\\>"))))
  1173. (list struct label)))))
  1174. (defsubst f90-looking-at-end-critical ()
  1175. "Return non-nil if a critical or block block ends after point."
  1176. (if (looking-at "end[ \t]*\\(critical\\|block\\)\\>")
  1177. (or (not (string-equal "block" (match-string 1)))
  1178. (save-excursion
  1179. (skip-chars-forward " \t")
  1180. (not (looking-at "data\\>"))))))
  1181. (defsubst f90-looking-at-where-or-forall ()
  1182. "Return (KIND NAME) if a where or forall block starts after point.
  1183. NAME is nil if the statement has no label."
  1184. (save-excursion
  1185. (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
  1186. \\(where\\|forall\\)\\>")
  1187. (let ((struct (match-string 3))
  1188. (label (match-string 2))
  1189. (pos (scan-lists (point) 1 0)))
  1190. (and pos (goto-char pos))
  1191. (skip-chars-forward " \t")
  1192. (if (looking-at "\\(!\\|$\\)") (list struct label))))))
  1193. (defsubst f90-looking-at-type-like ()
  1194. "Return (KIND NAME) if a type/enum/interface/block-data starts after point.
  1195. NAME is non-nil only for type and certain interfaces."
  1196. (cond
  1197. ((save-excursion
  1198. (and (looking-at "\\<type\\>[ \t]*")
  1199. (goto-char (match-end 0))
  1200. (not (looking-at "\\(is\\>\\|(\\)"))
  1201. (or (looking-at "\\(\\sw+\\)")
  1202. (re-search-forward "[ \t]*::[ \t]*\\(\\sw+\\)"
  1203. (line-end-position) t))))
  1204. (list "type" (match-string 1)))
  1205. ;;; ((and (not (looking-at f90-typeis-re))
  1206. ;;; (looking-at f90-type-def-re))
  1207. ;;; (list (match-string 1) (match-string 2)))
  1208. ((looking-at "\\<\\(interface\\)\\>[ \t]*")
  1209. (list (match-string 1)
  1210. (save-excursion
  1211. (goto-char (match-end 0))
  1212. (if (or (looking-at "\\(operator\\|assignment\\|read\\|\
  1213. write\\)[ \t]*([^)\n]*)")
  1214. (looking-at "\\sw+"))
  1215. (match-string 0)))))
  1216. ((looking-at "\\(enum\\|block[ \t]*data\\)\\>")
  1217. (list (match-string 1) nil))
  1218. ((looking-at "abstract[ \t]*\\(interface\\)\\>")
  1219. (list (match-string 1) nil))))
  1220. (defsubst f90-looking-at-program-block-start ()
  1221. "Return (KIND NAME) if a program block with name NAME starts after point."
  1222. ;;;NAME is nil for an un-named main PROGRAM block."
  1223. (cond
  1224. ((looking-at "\\(program\\)[ \t]+\\(\\sw+\\)\\>")
  1225. (list (match-string 1) (match-string 2)))
  1226. ((and (not (looking-at "module[ \t]*procedure\\>"))
  1227. (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
  1228. (list (match-string 1) (match-string 2)))
  1229. ((looking-at "\\(submodule\\)[ \t]*([^)\n]+)[ \t]*\\(\\sw+\\)\\>")
  1230. (list (match-string 1) (match-string 2)))
  1231. ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
  1232. (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)[ \t]+\
  1233. \\(\\sw+\\)"))
  1234. (list (match-string 1) (match-string 2)))))
  1235. ;; Following will match an un-named main program block; however
  1236. ;; one needs to check if there is an actual PROGRAM statement after
  1237. ;; point (and before any END program). Adding this will require
  1238. ;; change to eg f90-calculate-indent.
  1239. ;;; ((save-excursion
  1240. ;;; (not (f90-previous-statement)))
  1241. ;;; '("program" nil))))
  1242. (defsubst f90-looking-at-program-block-end ()
  1243. "Return (KIND NAME) if a block with name NAME ends after point."
  1244. (cond ((looking-at "end[ \t]*\\(interface\\)[ \t]*\\(\
  1245. \\(?:assignment\\|operator\\|read\\|write\\)[ \t]*([^)\n]*)\\)")
  1246. (list (match-string 1) (match-string 2)))
  1247. ((looking-at (concat "end[ \t]*" f90-blocks-re
  1248. "?\\([ \t]+\\(\\sw+\\)\\)?\\>"))
  1249. (list (match-string 1) (match-string 3)))))
  1250. (defsubst f90-comment-indent ()
  1251. "Return the indentation to be used for a comment starting at point.
  1252. Used for `comment-indent-function' by F90 mode.
  1253. \"!!!\", `f90-directive-comment-re', variable `f90-comment-region' return 0.
  1254. `f90-indented-comment-re' (if not trailing code) calls `f90-calculate-indent'.
  1255. All others return `comment-column', leaving at least one space after code."
  1256. (cond ((looking-at "!!!") 0)
  1257. ((and f90-directive-comment-re
  1258. (looking-at f90-directive-comment-re)) 0)
  1259. ((looking-at (regexp-quote f90-comment-region)) 0)
  1260. ((and (looking-at f90-indented-comment-re)
  1261. ;; Don't attempt to indent trailing comment as code.
  1262. (save-excursion
  1263. (skip-chars-backward " \t")
  1264. (bolp)))
  1265. (f90-calculate-indent))
  1266. (t (save-excursion
  1267. (skip-chars-backward " \t")
  1268. (max (if (bolp) 0 (1+ (current-column))) comment-column)))))
  1269. (defsubst f90-present-statement-cont ()
  1270. "Return continuation properties of present statement.
  1271. Possible return values are:
  1272. single - statement is not continued.
  1273. begin - current line is the first in a continued statement.
  1274. end - current line is the last in a continued statement
  1275. middle - current line is neither first nor last in a continued statement.
  1276. Comment lines embedded amongst continued lines return 'middle."
  1277. (let (pcont cont)
  1278. (save-excursion
  1279. (setq pcont (if (f90-previous-statement) (f90-line-continued))))
  1280. (setq cont (f90-line-continued))
  1281. (cond ((and (not pcont) (not cont)) 'single)
  1282. ((and (not pcont) cont) 'begin)
  1283. ((and pcont (not cont)) 'end)
  1284. ((and pcont cont) 'middle)
  1285. (t (error "The impossible occurred")))))
  1286. (defsubst f90-indent-line-no ()
  1287. "If `f90-leave-line-no' is nil, left-justify a line number.
  1288. Leaves point at the first non-blank character after the line number.
  1289. Call from beginning of line."
  1290. (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]")
  1291. (delete-horizontal-space))
  1292. (skip-chars-forward " \t0-9"))
  1293. (defsubst f90-no-block-limit ()
  1294. "Return nil if point is at the edge of a code block.
  1295. Searches line forward for \"function\" or \"subroutine\",
  1296. if all else fails."
  1297. (save-excursion
  1298. (not (or (looking-at "end")
  1299. (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
  1300. \\|select[ \t]*\\(case\\|type\\)\\|case\\|where\\|forall\\|\
  1301. block\\|critical\\)\\>")
  1302. (looking-at "\\(program\\|\\(?:sub\\)?module\\|\
  1303. \\(?:abstract[ \t]*\\)?interface\\|block[ \t]*data\\)\\>")
  1304. (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
  1305. (looking-at f90-type-def-re)
  1306. (re-search-forward "\\(function\\|subroutine\\)"
  1307. (line-end-position) t)))))
  1308. (defsubst f90-update-line ()
  1309. "Change case of current line as per `f90-auto-keyword-case'."
  1310. (if f90-auto-keyword-case
  1311. (f90-change-keywords f90-auto-keyword-case
  1312. (line-beginning-position) (line-end-position))))
  1313. (defun f90-electric-insert (&optional arg)
  1314. "Change keyword case and auto-fill line as operators are inserted."
  1315. (interactive "*p")
  1316. (self-insert-command arg)
  1317. (if auto-fill-function (f90-do-auto-fill) ; also updates line
  1318. (f90-update-line)))
  1319. ;; Behave like self-insert-command for delete-selection-mode (bug#5593).
  1320. (put 'f90-electric-insert 'delete-selection t)
  1321. (defun f90-get-correct-indent ()
  1322. "Get correct indent for a line starting with line number.
  1323. Does not check type and subprogram indentation."
  1324. (let ((epnt (line-end-position)) icol)
  1325. (save-excursion
  1326. (while (and (f90-previous-statement)
  1327. (or (memq (f90-present-statement-cont) '(middle end))
  1328. (looking-at "[ \t]*[0-9]"))))
  1329. (setq icol (current-indentation))
  1330. (beginning-of-line)
  1331. (when (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
  1332. (line-end-position) t)
  1333. (beginning-of-line)
  1334. (skip-chars-forward " \t")
  1335. (cond ((f90-looking-at-do)
  1336. (setq icol (+ icol f90-do-indent)))
  1337. ((or (f90-looking-at-if-then)
  1338. (f90-looking-at-where-or-forall)
  1339. (f90-looking-at-select-case))
  1340. (setq icol (+ icol f90-if-indent)))
  1341. ;; FIXME this makes no sense, because this section/function is
  1342. ;; only for if/do/select/where/forall ?
  1343. ((f90-looking-at-associate)
  1344. (setq icol (+ icol f90-associate-indent))))
  1345. (end-of-line))
  1346. (while (re-search-forward
  1347. "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
  1348. (beginning-of-line)
  1349. (skip-chars-forward " \t0-9")
  1350. (cond ((f90-looking-at-do)
  1351. (setq icol (+ icol f90-do-indent)))
  1352. ((or (f90-looking-at-if-then)
  1353. (f90-looking-at-where-or-forall)
  1354. (f90-looking-at-select-case))
  1355. (setq icol (+ icol f90-if-indent)))
  1356. ;; FIXME this makes no sense, because this section/function is
  1357. ;; only for if/do/select/where/forall ?
  1358. ((f90-looking-at-associate)
  1359. (setq icol (+ icol f90-associate-indent)))
  1360. ((looking-at f90-end-if-re)
  1361. (setq icol (- icol f90-if-indent)))
  1362. ((looking-at f90-end-associate-re)
  1363. (setq icol (- icol f90-associate-indent)))
  1364. ((f90-looking-at-end-critical)
  1365. (setq icol (- icol f90-critical-indent)))
  1366. ((looking-at "end[ \t]*do\\>")
  1367. (setq icol (- icol f90-do-indent))))
  1368. (end-of-line))
  1369. icol)))
  1370. (defun f90-calculate-indent ()
  1371. "Calculate the indent column based on previous statements."
  1372. (interactive)
  1373. (let (icol cont (case-fold-search t) (pnt (point)))
  1374. (save-excursion
  1375. (if (not (f90-previous-statement))
  1376. ;; If f90-previous-statement returns nil, we must have been
  1377. ;; called from on or before the first line of the first statement.
  1378. (setq icol (if (or (save-excursion
  1379. (goto-char pnt)
  1380. (beginning-of-line)
  1381. ;; Preprocessor line before code statement.
  1382. (looking-at "[ \t]*#"))
  1383. (progn
  1384. ;; f90-previous-statement has moved us over
  1385. ;; comment/blank lines, so we need to get
  1386. ;; back to the first code statement.
  1387. (when (looking-at "[ \t]*\\([!#]\\|$\\)")
  1388. (f90-next-statement))
  1389. (skip-chars-forward " \t0-9")
  1390. (f90-looking-at-program-block-start)))
  1391. 0
  1392. ;; No explicit PROGRAM start statement.
  1393. f90-program-indent))
  1394. (setq cont (f90-present-statement-cont))
  1395. (if (eq cont 'end)
  1396. (while (not (eq 'begin (f90-present-statement-cont)))
  1397. (f90-previous-statement)))
  1398. (cond ((eq cont 'begin)
  1399. (setq icol (+ (f90-current-indentation)
  1400. f90-continuation-indent)))
  1401. ((eq cont 'middle) (setq icol (current-indentation)))
  1402. (t (setq icol (f90-current-indentation))
  1403. (skip-chars-forward " \t")
  1404. (if (looking-at "[0-9]")
  1405. (setq icol (f90-get-correct-indent))
  1406. (cond ((or (f90-looking-at-if-then)
  1407. (f90-looking-at-where-or-forall)
  1408. (f90-looking-at-select-case)
  1409. (looking-at f90-else-like-re))
  1410. (setq icol (+ icol f90-if-indent)))
  1411. ((f90-looking-at-do)
  1412. (setq icol (+ icol f90-do-indent)))
  1413. ((f90-looking-at-type-like)
  1414. (setq icol (+ icol f90-type-indent)))
  1415. ((f90-looking-at-associate)
  1416. (setq icol (+ icol f90-associate-indent)))
  1417. ((f90-looking-at-critical)
  1418. (setq icol (+ icol f90-critical-indent)))
  1419. ((or (f90-looking-at-program-block-start)
  1420. (looking-at "contains[ \t]*\\($\\|!\\)"))
  1421. (setq icol (+ icol f90-program-indent)))))
  1422. (goto-char pnt)
  1423. (beginning-of-line)
  1424. (cond ((looking-at "[ \t]*$"))
  1425. ((looking-at "[ \t]*#") ; check for cpp directive
  1426. (setq icol 0))
  1427. (t
  1428. (skip-chars-forward " \t0-9")
  1429. (cond ((or (looking-at f90-else-like-re)
  1430. (looking-at f90-end-if-re))
  1431. (setq icol (- icol f90-if-indent)))
  1432. ((looking-at "end[ \t]*do\\>")
  1433. (setq icol (- icol f90-do-indent)))
  1434. ((looking-at f90-end-type-re)
  1435. (setq icol (- icol f90-type-indent)))
  1436. ((looking-at f90-end-associate-re)
  1437. (setq icol (- icol f90-associate-indent)))
  1438. ((f90-looking-at-end-critical)
  1439. (setq icol (- icol f90-critical-indent)))
  1440. ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
  1441. (f90-looking-at-program-block-end))
  1442. (setq icol (- icol f90-program-indent))))))))))
  1443. icol))
  1444. (defun f90-previous-statement ()
  1445. "Move point to beginning of the previous F90 statement.
  1446. If no previous statement is found (i.e. if called from the first
  1447. statement in the buffer), move to the start of the buffer and
  1448. return nil. A statement is a line which is neither blank nor a
  1449. comment."
  1450. (interactive)
  1451. (let (not-first-statement)
  1452. (beginning-of-line)
  1453. (while (and (setq not-first-statement (zerop (forward-line -1)))
  1454. (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
  1455. not-first-statement))
  1456. (defun f90-next-statement ()
  1457. "Move point to beginning of the next F90 statement.
  1458. Return nil if no later statement is found."
  1459. (interactive)
  1460. (let (not-last-statement)
  1461. (beginning-of-line)
  1462. (while (and (setq not-last-statement
  1463. (and (zerop (forward-line 1))
  1464. (not (eobp))))
  1465. (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
  1466. not-last-statement))
  1467. (defun f90-beginning-of-subprogram ()
  1468. "Move point to the beginning of the current subprogram.
  1469. Return (TYPE NAME), or nil if not found."
  1470. (interactive)
  1471. (let ((count 1) (case-fold-search t) matching-beg)
  1472. (beginning-of-line)
  1473. (while (and (> count 0)
  1474. (re-search-backward f90-program-block-re nil 'move))
  1475. (beginning-of-line)
  1476. (skip-chars-forward " \t0-9")
  1477. (cond ((setq matching-beg (f90-looking-at-program-block-start))
  1478. (setq count (1- count)))
  1479. ((f90-looking-at-program-block-end)
  1480. (setq count (1+ count)))))
  1481. (beginning-of-line)
  1482. (if (zerop count)
  1483. matching-beg
  1484. ;; Note this includes the case of an un-named main program,
  1485. ;; in which case we go to (point-min).
  1486. (if (called-interactively-p 'interactive)
  1487. (message "No beginning found"))
  1488. nil)))
  1489. (defun f90-end-of-subprogram ()
  1490. "Move point to the end of the current subprogram.
  1491. Return (TYPE NAME), or nil if not found."
  1492. (interactive)
  1493. (let ((case-fold-search t)
  1494. (count 1)
  1495. matching-end)
  1496. (end-of-line)
  1497. (while (and (> count 0)
  1498. (re-search-forward f90-program-block-re nil 'move))
  1499. (beginning-of-line)
  1500. (skip-chars-forward " \t0-9")
  1501. (cond ((f90-looking-at-program-block-start)
  1502. (setq count (1+ count)))
  1503. ((setq matching-end (f90-looking-at-program-block-end))
  1504. (setq count (1- count))))
  1505. (end-of-line))
  1506. ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
  1507. ;; has a net non-zero effect, which seems odd.
  1508. ;;; (forward-line 1)
  1509. (if (zerop count)
  1510. matching-end
  1511. (if (called-interactively-p 'interactive)
  1512. (message "No end found"))
  1513. nil)))
  1514. (defun f90-end-of-block (&optional num)
  1515. "Move point forward to the end of the current code block.
  1516. With optional argument NUM, go forward that many balanced blocks.
  1517. If NUM is negative, go backward to the start of a block. Checks
  1518. for consistency of block types and labels (if present), and
  1519. completes outermost block if `f90-smart-end' is non-nil.
  1520. Interactively, pushes mark before moving point."
  1521. (interactive "p")
  1522. ;; Can move some distance.
  1523. (if (called-interactively-p 'any) (push-mark (point) t))
  1524. (and num (< num 0) (f90-beginning-of-block (- num)))
  1525. (let ((f90-smart-end (if f90-smart-end 'no-blink)) ; for final match-end
  1526. (case-fold-search t)
  1527. (count (or num 1))
  1528. start-list start-this start-type start-label end-type end-label)
  1529. (end-of-line) ; probably want this
  1530. (while (and (> count 0) (re-search-forward f90-blocks-re nil 'move))
  1531. (beginning-of-line)
  1532. (skip-chars-forward " \t0-9")
  1533. (cond ((or (f90-in-string) (f90-in-comment)))
  1534. ((setq start-this
  1535. (or
  1536. (f90-looking-at-do)
  1537. (f90-looking-at-select-case)
  1538. (f90-looking-at-type-like)
  1539. (f90-looking-at-associate)
  1540. (f90-looking-at-critical)
  1541. (f90-looking-at-program-block-start)
  1542. (f90-looking-at-if-then)
  1543. (f90-looking-at-where-or-forall)))
  1544. (setq start-list (cons start-this start-list) ; not add-to-list!
  1545. count (1+ count)))
  1546. ((looking-at (concat "end[ \t]*" f90-blocks-re
  1547. "[ \t]*\\(\\sw+\\)?"))
  1548. (setq end-type (match-string 1)
  1549. end-label (match-string 2)
  1550. count (1- count))
  1551. ;; Check any internal blocks.
  1552. (when start-list
  1553. (setq start-this (car start-list)
  1554. start-list (cdr start-list)
  1555. start-type (car start-this)
  1556. start-label (cadr start-this))
  1557. (or (f90-equal-symbols start-type end-type)
  1558. (error "End type `%s' does not match start type `%s'"
  1559. end-type start-type))
  1560. (or (f90-equal-symbols start-label end-label)
  1561. (error "End label `%s' does not match start label `%s'"
  1562. end-label start-label)))))
  1563. (end-of-line))
  1564. (if (> count 0) (error "Missing block end"))
  1565. ;; Check outermost block.
  1566. (when f90-smart-end
  1567. (save-excursion
  1568. (beginning-of-line)
  1569. (skip-chars-forward " \t0-9")
  1570. (f90-match-end)))))
  1571. (defun f90-beginning-of-block (&optional num)
  1572. "Move point backwards to the start of the current code block.
  1573. With optional argument NUM, go backward that many balanced blocks.
  1574. If NUM is negative, go forward to the end of a block.
  1575. Checks for consistency of block types and labels (if present).
  1576. Does not check the outermost block, because it may be incomplete.
  1577. Interactively, pushes mark before moving point."
  1578. (interactive "p")
  1579. (if (called-interactively-p 'any) (push-mark (point) t))
  1580. (and num (< num 0) (f90-end-of-block (- num)))
  1581. (let ((case-fold-search t)
  1582. (count (or num 1))
  1583. end-list end-this end-type end-label
  1584. start-this start-type start-label)
  1585. (beginning-of-line) ; probably want this
  1586. (while (and (> count 0) (re-search-backward f90-blocks-re nil 'move))
  1587. (beginning-of-line)
  1588. (skip-chars-forward " \t0-9")
  1589. (cond ((or (f90-in-string) (f90-in-comment)))
  1590. ((looking-at (concat "end[ \t]*" f90-blocks-re
  1591. "[ \t]*\\(\\sw+\\)?"))
  1592. (setq end-list (cons (list (match-string 1) (match-string 2))
  1593. end-list)
  1594. count (1+ count)))
  1595. ((setq start-this
  1596. (or
  1597. (f90-looking-at-do)
  1598. (f90-looking-at-select-case)
  1599. (f90-looking-at-type-like)
  1600. (f90-looking-at-associate)
  1601. (f90-looking-at-critical)
  1602. (f90-looking-at-program-block-start)
  1603. (f90-looking-at-if-then)
  1604. (f90-looking-at-where-or-forall)))
  1605. (setq start-type (car start-this)
  1606. start-label (cadr start-this)
  1607. count (1- count))
  1608. ;; Check any internal blocks.
  1609. (when end-list
  1610. (setq end-this (car end-list)
  1611. end-list (cdr end-list)
  1612. end-type (car end-this)
  1613. end-label (cadr end-this))
  1614. (or (f90-equal-symbols start-type end-type)
  1615. (error "Start type `%s' does not match end type `%s'"
  1616. start-type end-type))
  1617. (or (f90-equal-symbols start-label end-label)
  1618. (error "Start label `%s' does not match end label `%s'"
  1619. start-label end-label))))))
  1620. ;; Includes an un-named main program block.
  1621. (if (> count 0) (error "Missing block start"))))
  1622. (defun f90-next-block (&optional num)
  1623. "Move point forward to the next end or start of a code block.
  1624. With optional argument NUM, go forward that many blocks.
  1625. If NUM is negative, go backwards.
  1626. A block is a subroutine, if-endif, etc."
  1627. (interactive "p")
  1628. (let ((case-fold-search t)
  1629. (count (if num (abs num) 1)))
  1630. (while (and (> count 0)
  1631. (if (> num 0) (re-search-forward f90-blocks-re nil 'move)
  1632. (re-search-backward f90-blocks-re nil 'move)))
  1633. (beginning-of-line)
  1634. (skip-chars-forward " \t0-9")
  1635. (cond ((or (f90-in-string) (f90-in-comment)))
  1636. ((or
  1637. (looking-at "end[ \t]*")
  1638. (f90-looking-at-do)
  1639. (f90-looking-at-select-case)
  1640. (f90-looking-at-type-like)
  1641. (f90-looking-at-associate)
  1642. (f90-looking-at-critical)
  1643. (f90-looking-at-program-block-start)
  1644. (f90-looking-at-if-then)
  1645. (f90-looking-at-where-or-forall))
  1646. (setq count (1- count))))
  1647. (if (> num 0) (end-of-line)
  1648. (beginning-of-line)))))
  1649. (defun f90-previous-block (&optional num)
  1650. "Move point backward to the previous end or start of a code block.
  1651. With optional argument NUM, go backward that many blocks.
  1652. If NUM is negative, go forwards.
  1653. A block is a subroutine, if-endif, etc."
  1654. (interactive "p")
  1655. (f90-next-block (- (or num 1))))
  1656. (defun f90-mark-subprogram ()
  1657. "Put mark at end of F90 subprogram, point at beginning, push mark."
  1658. (interactive)
  1659. (let ((pos (point)) program)
  1660. (f90-end-of-subprogram)
  1661. (push-mark)
  1662. (goto-char pos)
  1663. (setq program (f90-beginning-of-subprogram))
  1664. (if (featurep 'xemacs)
  1665. (zmacs-activate-region)
  1666. (setq mark-active t
  1667. deactivate-mark nil))
  1668. program))
  1669. (defun f90-comment-region (beg-region end-region)
  1670. "Comment/uncomment every line in the region.
  1671. Insert the variable `f90-comment-region' at the start of every line
  1672. in the region, or, if already present, remove it."
  1673. (interactive "*r")
  1674. (let ((end (copy-marker end-region)))
  1675. (goto-char beg-region)
  1676. (beginning-of-line)
  1677. (if (looking-at (regexp-quote f90-comment-region))
  1678. (delete-region (point) (match-end 0))
  1679. (insert f90-comment-region))
  1680. (while (and (zerop (forward-line 1))
  1681. (< (point) end))
  1682. (if (looking-at (regexp-quote f90-comment-region))
  1683. (delete-region (point) (match-end 0))
  1684. (insert f90-comment-region)))
  1685. (set-marker end nil)))
  1686. (defun f90-indent-line (&optional no-update)
  1687. "Indent current line as F90 code.
  1688. Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
  1689. after indenting."
  1690. (interactive "*P")
  1691. (let ((case-fold-search t)
  1692. (pos (point-marker))
  1693. indent no-line-number)
  1694. (beginning-of-line) ; digits after & \n are not line-nos
  1695. (if (not (save-excursion (and (f90-previous-statement)
  1696. (f90-line-continued))))
  1697. (f90-indent-line-no)
  1698. (setq no-line-number t)
  1699. (skip-chars-forward " \t"))
  1700. (if (looking-at "!")
  1701. (setq indent (f90-comment-indent))
  1702. (and f90-smart-end (looking-at "end")
  1703. (f90-match-end))
  1704. (setq indent (f90-calculate-indent)))
  1705. (or (= indent (current-column))
  1706. (f90-indent-to indent no-line-number))
  1707. ;; If initial point was within line's indentation,
  1708. ;; position after the indentation. Else stay at same point in text.
  1709. (and (< (point) pos)
  1710. (goto-char pos))
  1711. (if auto-fill-function
  1712. ;; GM NO-UPDATE not honored, since this calls f90-update-line.
  1713. (f90-do-auto-fill)
  1714. (or no-update (f90-update-line)))
  1715. (set-marker pos nil)))
  1716. (defun f90-indent-new-line ()
  1717. "Re-indent current line, insert a newline and indent the newline.
  1718. An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
  1719. If run in the middle of a line, the line is not broken."
  1720. (interactive "*")
  1721. (if abbrev-mode (expand-abbrev))
  1722. (beginning-of-line) ; reindent where likely to be needed
  1723. (f90-indent-line) ; calls indent-line-no, update-line
  1724. (end-of-line)
  1725. (delete-horizontal-space) ; destroy trailing whitespace
  1726. (let ((string (f90-in-string))
  1727. (cont (f90-line-continued)))
  1728. (and string (not cont) (insert "&"))
  1729. (newline)
  1730. (if (or string (and cont f90-beginning-ampersand)) (insert "&")))
  1731. (f90-indent-line 'no-update)) ; nothing to update
  1732. ;; TODO not add spaces to empty lines at the start.
  1733. ;; Why is second line getting extra indent over first?
  1734. (defun f90-indent-region (beg-region end-region)
  1735. "Indent every line in region by forward parsing."
  1736. (interactive "*r")
  1737. (let ((end-region-mark (copy-marker end-region))
  1738. (save-point (point-marker))
  1739. (case-fold-search t)
  1740. block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
  1741. (goto-char beg-region)
  1742. ;; First find a line which is not a continuation line or comment.
  1743. (beginning-of-line)
  1744. (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
  1745. (progn (f90-indent-line 'no-update)
  1746. (zerop (forward-line 1)))
  1747. (< (point) end-region-mark)))
  1748. (setq cont (f90-present-statement-cont))
  1749. (while (and (memq cont '(middle end))
  1750. (f90-previous-statement))
  1751. (setq cont (f90-present-statement-cont)))
  1752. ;; Process present line for beginning of block.
  1753. (setq f90-cache-position (point))
  1754. (f90-indent-line 'no-update)
  1755. (setq ind-lev (f90-current-indentation)
  1756. ind-curr ind-lev)
  1757. (beginning-of-line)
  1758. (skip-chars-forward " \t0-9")
  1759. (setq struct nil
  1760. ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
  1761. ((or (setq struct (f90-looking-at-if-then))
  1762. (setq struct (f90-looking-at-select-case))
  1763. (setq struct (f90-looking-at-where-or-forall))
  1764. (looking-at f90-else-like-re))
  1765. f90-if-indent)
  1766. ((setq struct (f90-looking-at-type-like))
  1767. f90-type-indent)
  1768. ((setq struct (f90-looking-at-associate))
  1769. f90-associate-indent)
  1770. ((setq struct (f90-looking-at-critical))
  1771. f90-critical-indent)
  1772. ((or (setq struct (f90-looking-at-program-block-start))
  1773. (looking-at "contains[ \t]*\\($\\|!\\)"))
  1774. f90-program-indent)))
  1775. (if ind-b (setq ind-lev (+ ind-lev ind-b)))
  1776. (if struct (setq block-list (cons struct block-list)))
  1777. (while (and (f90-line-continued) (zerop (forward-line 1))
  1778. (< (point) end-region-mark))
  1779. (if (looking-at "[ \t]*!")
  1780. (f90-indent-to (f90-comment-indent))
  1781. (or (= (current-indentation)
  1782. (+ ind-curr f90-continuation-indent))
  1783. (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
  1784. ;; Process all following lines.
  1785. (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
  1786. (beginning-of-line)
  1787. (f90-indent-line-no)
  1788. (setq f90-cache-position (point))
  1789. (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
  1790. ((looking-at "[ \t]*#") (setq ind-curr 0))
  1791. ((looking-at "!") (setq ind-curr (f90-comment-indent)))
  1792. ((f90-no-block-limit) (setq ind-curr ind-lev))
  1793. ((looking-at f90-else-like-re) (setq ind-curr
  1794. (- ind-lev f90-if-indent)))
  1795. ((looking-at "contains[ \t]*\\($\\|!\\)")
  1796. (setq ind-curr (- ind-lev f90-program-indent)))
  1797. ((setq ind-b
  1798. (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
  1799. ((or (setq struct (f90-looking-at-if-then))
  1800. (setq struct (f90-looking-at-select-case))
  1801. (setq struct (f90-looking-at-where-or-forall)))
  1802. f90-if-indent)
  1803. ((setq struct (f90-looking-at-type-like))
  1804. f90-type-indent)
  1805. ((setq struct (f90-looking-at-associate))
  1806. f90-associate-indent)
  1807. ((setq struct (f90-looking-at-critical))
  1808. f90-critical-indent)
  1809. ((setq struct (f90-looking-at-program-block-start))
  1810. f90-program-indent)))
  1811. (setq ind-curr ind-lev)
  1812. (if ind-b (setq ind-lev (+ ind-lev ind-b)))
  1813. (setq block-list (cons struct block-list)))
  1814. ((setq end-struct (f90-looking-at-program-block-end))
  1815. (setq beg-struct (car block-list)
  1816. block-list (cdr block-list))
  1817. (if f90-smart-end
  1818. (save-excursion
  1819. (f90-block-match (car beg-struct) (cadr beg-struct)
  1820. (car end-struct) (cadr end-struct))))
  1821. (setq ind-b
  1822. (cond ((looking-at f90-end-if-re) f90-if-indent)
  1823. ((looking-at "end[ \t]*do\\>") f90-do-indent)
  1824. ((looking-at f90-end-type-re) f90-type-indent)
  1825. ((looking-at f90-end-associate-re)
  1826. f90-associate-indent)
  1827. ((f90-looking-at-end-critical) f90-critical-indent)
  1828. ((f90-looking-at-program-block-end)
  1829. f90-program-indent)))
  1830. (if ind-b (setq ind-lev (- ind-lev ind-b)))
  1831. (setq ind-curr ind-lev))
  1832. (t (setq ind-curr ind-lev)))
  1833. ;; Do the indentation if necessary.
  1834. (or (= ind-curr (current-column))
  1835. (f90-indent-to ind-curr))
  1836. (while (and (f90-line-continued) (zerop (forward-line 1))
  1837. (< (point) end-region-mark))
  1838. (if (looking-at "[ \t]*!")
  1839. (f90-indent-to (f90-comment-indent))
  1840. (or (= (current-indentation)
  1841. (+ ind-curr f90-continuation-indent))
  1842. (f90-indent-to
  1843. (+ ind-curr f90-continuation-indent) 'no-line-no)))))
  1844. ;; Restore point, etc.
  1845. (setq f90-cache-position nil)
  1846. (goto-char save-point)
  1847. (set-marker end-region-mark nil)
  1848. (set-marker save-point nil)
  1849. (if (featurep 'xemacs)
  1850. (zmacs-deactivate-region)
  1851. (deactivate-mark))))
  1852. (defun f90-indent-subprogram ()
  1853. "Properly indent the subprogram containing point."
  1854. (interactive "*")
  1855. (save-excursion
  1856. (let ((program (f90-mark-subprogram)))
  1857. (if program
  1858. (progn
  1859. (message "Indenting %s %s..."
  1860. (car program) (cadr program))
  1861. (indent-region (point) (mark) nil)
  1862. (message "Indenting %s %s...done"
  1863. (car program) (cadr program)))
  1864. (message "Indenting the whole file...")
  1865. (indent-region (point) (mark) nil)
  1866. (message "Indenting the whole file...done")))))
  1867. (defun f90-break-line (&optional no-update)
  1868. "Break line at point, insert continuation marker(s) and indent.
  1869. Unless in a string or comment, or if the optional argument NO-UPDATE
  1870. is non-nil, call `f90-update-line' after inserting the continuation marker."
  1871. (interactive "*P")
  1872. (cond ((f90-in-string)
  1873. (insert "&\n&"))
  1874. ((f90-in-comment)
  1875. (delete-horizontal-space) ; remove trailing whitespace
  1876. (insert "\n" (f90-get-present-comment-type)))
  1877. (t (insert "&")
  1878. (or no-update (f90-update-line))
  1879. (newline 1)
  1880. ;; FIXME also need leading ampersand if split lexical token (eg ==).
  1881. ;; Or respect f90-no-break-re.
  1882. (if f90-beginning-ampersand (insert "&"))))
  1883. (indent-according-to-mode))
  1884. (defun f90-find-breakpoint ()
  1885. "From `fill-column', search backward for break-delimiter."
  1886. ;; Commented text more likely than commented code.
  1887. (if (f90-in-comment)
  1888. (re-search-backward "\\s-" (line-beginning-position))
  1889. (re-search-backward f90-break-delimiters (line-beginning-position))
  1890. (if (not f90-break-before-delimiters)
  1891. (forward-char (if (looking-at f90-no-break-re) 2 1))
  1892. (backward-char)
  1893. (or (looking-at f90-no-break-re)
  1894. (forward-char)))))
  1895. (defun f90-do-auto-fill ()
  1896. "Break line if non-white characters beyond `fill-column'.
  1897. Update keyword case first."
  1898. (interactive "*")
  1899. ;; Break line before or after last delimiter (non-word char) if
  1900. ;; position is beyond fill-column.
  1901. ;; Will not break **, //, or => (as specified by f90-no-break-re).
  1902. (f90-update-line)
  1903. ;; Need this for `f90-electric-insert' and other f90- callers.
  1904. (unless (and (boundp 'comment-auto-fill-only-comments)
  1905. comment-auto-fill-only-comments
  1906. (not (f90-in-comment)))
  1907. (while (> (current-column) fill-column)
  1908. (let ((pos-mark (point-marker)))
  1909. (move-to-column fill-column)
  1910. (or (f90-in-string) (f90-find-breakpoint))
  1911. (f90-break-line)
  1912. (goto-char pos-mark)
  1913. (set-marker pos-mark nil)))))
  1914. (defun f90-join-lines (&optional arg)
  1915. "Join current line to previous, fix whitespace, continuation, comments.
  1916. With optional argument ARG, join current line to following line.
  1917. Like `join-line', but handles F90 syntax."
  1918. (interactive "*P")
  1919. (beginning-of-line)
  1920. (if arg (forward-line 1))
  1921. (when (eq (preceding-char) ?\n)
  1922. (skip-chars-forward " \t")
  1923. (if (looking-at "\&") (delete-char 1))
  1924. (beginning-of-line)
  1925. (delete-region (point) (1- (point)))
  1926. (skip-chars-backward " \t")
  1927. (and (eq (preceding-char) ?&) (delete-char -1))
  1928. (and (f90-in-comment)
  1929. (looking-at "[ \t]*!+")
  1930. (replace-match ""))
  1931. (or (f90-in-string)
  1932. (fixup-whitespace))))
  1933. (defun f90-fill-region (beg-region end-region)
  1934. "Fill every line in region by forward parsing. Join lines if possible."
  1935. (interactive "*r")
  1936. (let ((end-region-mark (copy-marker end-region))
  1937. (go-on t)
  1938. f90-smart-end f90-auto-keyword-case auto-fill-function)
  1939. (goto-char beg-region)
  1940. (while go-on
  1941. ;; Join as much as possible.
  1942. (while (progn
  1943. (end-of-line)
  1944. (skip-chars-backward " \t")
  1945. (eq (preceding-char) ?&))
  1946. (f90-join-lines 'forward))
  1947. ;; Chop the line if necessary.
  1948. (while (> (save-excursion (end-of-line) (current-column))
  1949. fill-column)
  1950. (move-to-column fill-column)
  1951. (f90-find-breakpoint)
  1952. (f90-break-line 'no-update))
  1953. (setq go-on (and (< (point) end-region-mark)
  1954. (zerop (forward-line 1)))
  1955. f90-cache-position (point)))
  1956. (setq f90-cache-position nil)
  1957. (set-marker end-region-mark nil)
  1958. (if (featurep 'xemacs)
  1959. (zmacs-deactivate-region)
  1960. (deactivate-mark))))
  1961. (defun f90-block-match (beg-block beg-name end-block end-name)
  1962. "Match end-struct with beg-struct and complete end-block if possible.
  1963. BEG-BLOCK is the type of block as indicated at the start (e.g., do).
  1964. BEG-NAME is the block start name (may be nil).
  1965. END-BLOCK is the type of block as indicated at the end (may be nil).
  1966. END-NAME is the block end name (may be nil).
  1967. Leave point at the end of line."
  1968. ;; Hack to deal with the case when this is called from
  1969. ;; f90-indent-region on a program block without an explicit PROGRAM
  1970. ;; statement at the start. Should really be an error (?).
  1971. (or beg-block (setq beg-block "program"))
  1972. (search-forward "end" (line-end-position))
  1973. (catch 'no-match
  1974. (if (and end-block (f90-equal-symbols beg-block end-block))
  1975. (search-forward end-block)
  1976. (if end-block
  1977. (progn
  1978. (message "END %s does not match %s." end-block beg-block)
  1979. (end-of-line)
  1980. (throw 'no-match nil))
  1981. (message "Inserting %s." beg-block)
  1982. (insert (concat " " beg-block))))
  1983. (if (f90-equal-symbols beg-name end-name)
  1984. (and end-name (search-forward end-name))
  1985. (cond ((and beg-name (not end-name))
  1986. (message "Inserting %s." beg-name)
  1987. (insert (concat " " beg-name)))
  1988. ((and beg-name end-name)
  1989. (message "Replacing %s with %s." end-name beg-name)
  1990. (search-forward end-name)
  1991. (replace-match beg-name))
  1992. ((and (not beg-name) end-name)
  1993. (message "Deleting %s." end-name)
  1994. (search-forward end-name)
  1995. (replace-match ""))))
  1996. (or (looking-at "[ \t]*!") (delete-horizontal-space))))
  1997. (defun f90-match-end ()
  1998. "From an end block statement, find the corresponding block and name."
  1999. (interactive)
  2000. (let ((count 1)
  2001. (top-of-window (window-start))
  2002. (end-point (point))
  2003. (case-fold-search t)
  2004. matching-beg beg-name end-name beg-block end-block end-struct)
  2005. (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
  2006. (setq end-struct (f90-looking-at-program-block-end)))
  2007. (setq end-block (car end-struct)
  2008. end-name (cadr end-struct))
  2009. (save-excursion
  2010. (beginning-of-line)
  2011. (while (and (> count 0)
  2012. (not (= (line-beginning-position) (point-min))))
  2013. (re-search-backward f90-blocks-re nil 'move)
  2014. (beginning-of-line)
  2015. ;; GM not a line number if continued line.
  2016. ;;; (skip-chars-forward " \t")
  2017. ;;; (skip-chars-forward "0-9")
  2018. (skip-chars-forward " \t0-9")
  2019. (cond ((or (f90-in-string) (f90-in-comment)))
  2020. ((setq matching-beg
  2021. (or
  2022. (f90-looking-at-do)
  2023. (f90-looking-at-if-then)
  2024. (f90-looking-at-where-or-forall)
  2025. (f90-looking-at-select-case)
  2026. (f90-looking-at-type-like)
  2027. (f90-looking-at-associate)
  2028. (f90-looking-at-critical)
  2029. (f90-looking-at-program-block-start)
  2030. ;; Interpret a single END without a block
  2031. ;; start to be the END of a program block
  2032. ;; without an initial PROGRAM line.
  2033. (if (= (line-beginning-position) (point-min))
  2034. '("program" nil))))
  2035. (setq count (1- count)))
  2036. ((looking-at (concat "end[ \t]*" f90-blocks-re))
  2037. (setq count (1+ count)))))
  2038. (if (> count 0)
  2039. (message "No matching beginning.")
  2040. (f90-update-line)
  2041. (if (eq f90-smart-end 'blink)
  2042. (if (< (point) top-of-window)
  2043. (message "Matches %s: %s"
  2044. (what-line)
  2045. (buffer-substring
  2046. (line-beginning-position)
  2047. (line-end-position)))
  2048. (sit-for blink-matching-delay)))
  2049. (setq beg-block (car matching-beg)
  2050. beg-name (cadr matching-beg))
  2051. (goto-char end-point)
  2052. (beginning-of-line)
  2053. (f90-block-match beg-block beg-name end-block end-name))))))
  2054. (defun f90-insert-end ()
  2055. "Insert a complete end statement matching beginning of present block."
  2056. (interactive "*")
  2057. (let ((f90-smart-end (or f90-smart-end 'blink)))
  2058. (insert "end")
  2059. (f90-indent-new-line)))
  2060. ;; Abbrevs and keywords.
  2061. (defun f90-abbrev-start ()
  2062. "Typing `\\[help-command] or `? lists all the F90 abbrevs.
  2063. Any other key combination is executed normally."
  2064. (interactive "*")
  2065. (insert last-command-event)
  2066. (let (char event)
  2067. (if (fboundp 'next-command-event) ; XEmacs
  2068. (setq event (next-command-event)
  2069. char (and (fboundp 'event-to-character)
  2070. (event-to-character event)))
  2071. (setq event (read-event)
  2072. char event))
  2073. ;; Insert char if not equal to `?', or if abbrev-mode is off.
  2074. (if (and abbrev-mode (memq char (list ?? help-char)))
  2075. (f90-abbrev-help)
  2076. (setq unread-command-events (list event)))))
  2077. (defun f90-abbrev-help ()
  2078. "List the currently defined abbrevs in F90 mode."
  2079. (interactive)
  2080. (message "Listing abbrev table...")
  2081. (display-buffer (f90-prepare-abbrev-list-buffer))
  2082. (message "Listing abbrev table...done"))
  2083. (defun f90-prepare-abbrev-list-buffer ()
  2084. "Create a buffer listing the F90 mode abbreviations."
  2085. (with-current-buffer (get-buffer-create "*Abbrevs*")
  2086. (erase-buffer)
  2087. (insert-abbrev-table-description 'f90-mode-abbrev-table t)
  2088. (goto-char (point-min))
  2089. (set-buffer-modified-p nil)
  2090. (edit-abbrevs-mode))
  2091. (get-buffer-create "*Abbrevs*"))
  2092. (defun f90-upcase-keywords ()
  2093. "Upcase all F90 keywords in the buffer."
  2094. (interactive "*")
  2095. (f90-change-keywords 'upcase-word))
  2096. (defun f90-capitalize-keywords ()
  2097. "Capitalize all F90 keywords in the buffer."
  2098. (interactive "*")
  2099. (f90-change-keywords 'capitalize-word))
  2100. (defun f90-downcase-keywords ()
  2101. "Downcase all F90 keywords in the buffer."
  2102. (interactive "*")
  2103. (f90-change-keywords 'downcase-word))
  2104. (defun f90-upcase-region-keywords (beg end)
  2105. "Upcase all F90 keywords in the region."
  2106. (interactive "*r")
  2107. (f90-change-keywords 'upcase-word beg end))
  2108. (defun f90-capitalize-region-keywords (beg end)
  2109. "Capitalize all F90 keywords in the region."
  2110. (interactive "*r")
  2111. (f90-change-keywords 'capitalize-word beg end))
  2112. (defun f90-downcase-region-keywords (beg end)
  2113. "Downcase all F90 keywords in the region."
  2114. (interactive "*r")
  2115. (f90-change-keywords 'downcase-word beg end))
  2116. ;; Change the keywords according to argument.
  2117. (defun f90-change-keywords (change-word &optional beg end)
  2118. "Change the case of F90 keywords in the region (if specified) or buffer.
  2119. CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
  2120. (save-excursion
  2121. (setq beg (or beg (point-min))
  2122. end (or end (point-max)))
  2123. (let ((keyword-re
  2124. (concat "\\("
  2125. f90-keywords-re "\\|" f90-procedures-re "\\|"
  2126. f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
  2127. (ref-point (point-min))
  2128. (modified (buffer-modified-p))
  2129. state saveword back-point)
  2130. (goto-char beg)
  2131. (unwind-protect
  2132. (while (re-search-forward keyword-re end t)
  2133. (unless (progn
  2134. (setq state (parse-partial-sexp ref-point (point)))
  2135. (or (nth 3 state) (nth 4 state)
  2136. ;; GM f90-directive-comment-re?
  2137. (save-excursion ; check for cpp directive
  2138. (beginning-of-line)
  2139. (skip-chars-forward " \t0-9")
  2140. (looking-at "#"))))
  2141. (setq ref-point (point)
  2142. back-point (save-excursion (backward-word 1) (point))
  2143. saveword (buffer-substring back-point ref-point))
  2144. (funcall change-word -1)
  2145. (or (string= saveword (buffer-substring back-point ref-point))
  2146. (setq modified t))))
  2147. (or modified (restore-buffer-modified-p nil))))))
  2148. (defun f90-current-defun ()
  2149. "Function to use for `add-log-current-defun-function' in F90 mode."
  2150. (save-excursion
  2151. (nth 1 (f90-beginning-of-subprogram))))
  2152. (defun f90-backslash-not-special (&optional all)
  2153. "Make the backslash character (\\) be non-special in the current buffer.
  2154. With optional argument ALL, change the default for all present
  2155. and future F90 buffers. F90 mode normally treats backslash as an
  2156. escape character."
  2157. (or (derived-mode-p 'f90-mode)
  2158. (error "This function should only be used in F90 buffers"))
  2159. (when (equal (char-syntax ?\\ ) ?\\ )
  2160. (or all (set-syntax-table (copy-syntax-table (syntax-table))))
  2161. (modify-syntax-entry ?\\ ".")))
  2162. (provide 'f90)
  2163. ;; Local Variables:
  2164. ;; coding: utf-8
  2165. ;; End:
  2166. ;;; f90.el ends here