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