1 ;;; fortran-misc.el --- Routines than can be used with fortran mode.
3 ;;; Copyright (c) 1992 Free Software Foundation, Inc.
5 ;; Author: Various authors.
10 ;; This file is not part of GNU Emacs.
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)
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.
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
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;;; This file contains various routines that may be useful with GNU emacs
29 ;;; fortran mode, but just don't seem to fit in.
31 (defun fortran-fill-statement ()
32 "Fill a fortran statement up to `fill-column'."
36 (or (looking-at "[ \t]*$")
37 (looking-at comment-line-start-skip)
38 (and comment-start-skip
39 (looking-at (concat "[ \t]*" comment-start-skip)))))
41 (let ((opos (point)) (beg) (cfi))
43 (fortran-next-statement)
44 (fortran-previous-statement)
45 (setq cfi (calculate-fortran-indent))
51 (if (or (not (= cfi (fortran-current-line-indentation)))
52 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
53 (not (fortran-line-number-indented-correctly-p))))
54 (fortran-indent-to-column cfi)))
57 (or (looking-at " [^ 0\n]")
58 (looking-at "\t[1-9]")))
61 (delete-horizontal-space))
62 (fortran-previous-statement)
63 (if (> (save-excursion (end-of-line) (current-column)) fill-column)
64 (fortran-do-auto-fill)))
65 (if (< (point) opos) (goto-char opos))
66 (let ((cfi (calculate-fortran-indent)))
67 (if (< (current-column) cfi)
68 (move-to-column cfi))))))
70 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
71 ;;; The upcase/downcase and beautifier code is originally from Ralph Finch
72 ;;; (rfinch@water.ca.gov).
74 (defun fortran-downcase-subprogram ()
75 "Properly downcases the Fortran subprogram which contains point."
78 (mark-fortran-subprogram)
79 (message "Downcasing subprogram...")
80 (fortran-downcase-region (point) (mark)))
81 (message "Downcasing subprogram...done."))
83 (defun fortran-downcase-region (start end)
84 "Downcase region, excluding comment lines and anything
87 (fortran-case-region start end nil))
89 (defun fortran-upcase-region (start end)
90 "Upcase region, excluding comment lines and anything
93 (fortran-case-region start end t))
95 (defun fortran-upcase-subprogram ()
96 "Properly upcases the Fortran subprogram which contains point."
99 (mark-fortran-subprogram)
100 (message "Upcasing subprogram...")
101 (fortran-upcase-region (point) (mark)))
102 (message "Upcasing subprogram...done."))
104 (defun fortran-case-region (start end up)
105 "Upcase region if UP is t, downcase, if UP downcase region,
106 excluding comment lines and anything between quote marks."
107 (let* ((start-re-comment "^[cC*#]")
110 (end-re-quote "\\('\\|$\\)")
111 (start-re-dquote (char-to-string ?\"))
112 (end-re-dquote (concat "\\(" start-re-dquote "\\|$\\)"))
116 (narrow-to-region start end)
117 (goto-char (point-min))
118 (if (inside-re start-re-comment end-re-comment)
119 (re-search-forward end-re-comment end 0))
120 (if (inside-re start-re-quote end-re-quote)
121 (re-search-forward end-re-quote end 0))
122 (if (inside-re start-re-dquote end-re-dquote)
123 (re-search-forward end-re-dquote end 0))
125 (while (< (point) (point-max))
127 (concat "\\(" start-re-comment "\\|"
128 start-re-quote "\\|" start-re-dquote "\\)") end 0)
131 (upcase-region strt fin)
132 (downcase-region strt fin))
133 (if (inside-re start-re-comment end-re-comment)
134 (re-search-forward end-re-comment end 0))
135 (if (inside-re start-re-quote end-re-quote)
136 (re-search-forward end-re-quote end 0))
137 (if (inside-re start-re-dquote end-re-dquote)
138 (re-search-forward end-re-dquote end 0))
139 (setq strt (point)))))))
141 (defun inside-re (start-re end-re)
142 "Returns t if inside a starting regexp and an ending regexp
145 (let ((start-line) (end-line))
147 (setq start-line (progn (beginning-of-line) (point)))
148 (setq end-line (progn (end-of-line) (point))))
149 (if (and (save-excursion
150 (re-search-backward start-re start-line t))
152 (re-search-forward end-re end-line t)))
156 ;;; Note: Just as with some other routines, fortran-beautify-line
157 ;;; assumes trailing blanks are not significant. Code may need
158 ;;; to be adjusted to comply with this.
161 (defun fortran-beautify-subprogram (&optional downit)
162 "Beautify Fortran subprogram:
163 1) Remove trailing blanks.
164 2) Replace all continuation characters with fortran-continuation-char.
165 3) Replace all empty comment lines with blank lines.
166 4) Replace all multiple blank lines with one blank line.
168 6) With prefix arg, downcase the subprogram, avoiding comments and
172 (mark-fortran-subprogram)
173 (message "Beautifying subprogram...")
174 (fortran-beautify-region (point) (mark) downit))
175 (message "Beautify subprogram...done."))
177 (defun fortran-beautify-region (start end &optional downit)
178 "Beautify region in a Fortran program:
179 1) Remove trailing blanks.
180 2) Replace all continuation characters with fortran-continuation-char.
181 3) Replace all empty comment lines with blank lines.
182 4) Replace all multiple blank lines with one blank line.
184 6) With prefix arg, downcase the region, avoiding comments and
189 (let ((m1 (make-marker))
191 (set-marker m1 start)
193 (indent-region start end nil)
194 (narrow-to-region m1 m2)
195 (goto-char (point-min)) ; trailing blanks
196 (perform-replace "[ \t]+$" "" nil t nil)
197 (goto-char (point-min)) ; continuation characters
198 (perform-replace (concat "^ [^ " fortran-continuation-string
200 (concat " " fortran-continuation-string)
202 (goto-char (point-min)) ; empty comments
203 (perform-replace "^[cC][ \t]*$" "" nil t nil)
204 (goto-char (point-min)) ; multiple blank lines
205 (perform-replace "\n\n\n+" "\n\n" nil t nil)
207 (fortran-downcase-region (point-min) (point-max)))