Mercurial > emacs
comparison lisp/progmodes/pascal.el @ 10534:38b5efae433a
(pascal-*-completion, pascal-comp-defun)
(pascal-complete-word, pascal-completion-response, pascal-completion)
(pascal-get-completion-decl): Rename some internal variables
to start with 'pascal-'.
(pascal-str, pascal-all, pascal-pred, pascal-flag)
(pascal-buffer-to use): New dummy variables.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 24 Jan 1995 03:27:54 +0000 |
parents | 034609a036b1 |
children | ef10b4684bb5 |
comparison
equal
deleted
inserted
replaced
10533:52b954844444 | 10534:38b5efae433a |
---|---|
1025 | 1025 |
1026 | 1026 |
1027 ;;; | 1027 ;;; |
1028 ;;; Completion | 1028 ;;; Completion |
1029 ;;; | 1029 ;;; |
1030 (defvar pascal-str nil) | |
1031 (defvar pascal-all nil) | |
1032 (defvar pascal-pred nil) | |
1033 (defvar pascal-buffer-to-use nil) | |
1034 (defvar pascal-flag nil) | |
1035 | |
1030 (defun pascal-string-diff (str1 str2) | 1036 (defun pascal-string-diff (str1 str2) |
1031 "Return index of first letter where STR1 and STR2 differs." | 1037 "Return index of first letter where STR1 and STR2 differs." |
1032 (catch 'done | 1038 (catch 'done |
1033 (let ((diff 0)) | 1039 (let ((diff 0)) |
1034 (while t | 1040 (while t |
1043 ;; completions for procedures if argument is `procedure' or both functions and | 1049 ;; completions for procedures if argument is `procedure' or both functions and |
1044 ;; procedures otherwise. | 1050 ;; procedures otherwise. |
1045 | 1051 |
1046 (defun pascal-func-completion (type) | 1052 (defun pascal-func-completion (type) |
1047 ;; Build regular expression for function/procedure names | 1053 ;; Build regular expression for function/procedure names |
1048 (if (string= str "") | 1054 (if (string= pascal-str "") |
1049 (setq str "[a-zA-Z_]")) | 1055 (setq pascal-str "[a-zA-Z_]")) |
1050 (let ((str (concat (cond ((eq type 'procedure) "\\<\\(procedure\\)\\s +") | 1056 (let ((pascal-str (concat (cond |
1051 ((eq type 'function) "\\<\\(function\\)\\s +") | 1057 ((eq type 'procedure) "\\<\\(procedure\\)\\s +") |
1052 (t "\\<\\(function\\|procedure\\)\\s +")) | 1058 ((eq type 'function) "\\<\\(function\\)\\s +") |
1053 "\\<\\(" str "[a-zA-Z0-9_.]*\\)\\>")) | 1059 (t "\\<\\(function\\|procedure\\)\\s +")) |
1060 "\\<\\(" pascal-str "[a-zA-Z0-9_.]*\\)\\>")) | |
1054 match) | 1061 match) |
1055 | 1062 |
1056 (if (not (looking-at "\\<\\(function\\|procedure\\)\\>")) | 1063 (if (not (looking-at "\\<\\(function\\|procedure\\)\\>")) |
1057 (re-search-backward "\\<\\(function\\|procedure\\)\\>" nil t)) | 1064 (re-search-backward "\\<\\(function\\|procedure\\)\\>" nil t)) |
1058 (forward-char 1) | 1065 (forward-char 1) |
1059 | 1066 |
1060 ;; Search through all reachable functions | 1067 ;; Search through all reachable functions |
1061 (while (pascal-beg-of-defun) | 1068 (while (pascal-beg-of-defun) |
1062 (if (re-search-forward str (pascal-get-end-of-line) t) | 1069 (if (re-search-forward pascal-str (pascal-get-end-of-line) t) |
1063 (progn (setq match (buffer-substring (match-beginning 2) | 1070 (progn (setq match (buffer-substring (match-beginning 2) |
1064 (match-end 2))) | 1071 (match-end 2))) |
1065 (if (or (null predicate) | 1072 (if (or (null pascal-pred) |
1066 (funcall prdicate match)) | 1073 (funcall pascal-pred match)) |
1067 (setq all (cons match all))))) | 1074 (setq pascal-all (cons match pascal-all))))) |
1068 (goto-char (match-beginning 0))))) | 1075 (goto-char (match-beginning 0))))) |
1069 | 1076 |
1070 (defun pascal-get-completion-decl () | 1077 (defun pascal-get-completion-decl () |
1071 ;; Macro for searching through current declaration (var, type or const) | 1078 ;; Macro for searching through current declaration (var, type or const) |
1072 ;; for matches of `str' and adding the occurence tp `all' | 1079 ;; for matches of `str' and adding the occurence tp `all' |
1081 (concat "\\((\\|\\<\\(var\\|type\\|const\\)\\>\\)\\|" | 1088 (concat "\\((\\|\\<\\(var\\|type\\|const\\)\\>\\)\\|" |
1082 pascal-symbol-re) | 1089 pascal-symbol-re) |
1083 (pascal-get-beg-of-line) t) | 1090 (pascal-get-beg-of-line) t) |
1084 (not (match-end 1))) | 1091 (not (match-end 1))) |
1085 (setq match (buffer-substring (match-beginning 0) (match-end 0))) | 1092 (setq match (buffer-substring (match-beginning 0) (match-end 0))) |
1086 (if (string-match (concat "\\<" str) match) | 1093 (if (string-match (concat "\\<" pascal-str) match) |
1087 (if (or (null predicate) | 1094 (if (or (null pascal-pred) |
1088 (funcall predicate match)) | 1095 (funcall pascal-pred match)) |
1089 (setq all (cons match all)))))) | 1096 (setq pascal-all (cons match pascal-all)))))) |
1090 (if (re-search-forward "\\<record\\>" (pascal-get-end-of-line) t) | 1097 (if (re-search-forward "\\<record\\>" (pascal-get-end-of-line) t) |
1091 (pascal-declaration-end) | 1098 (pascal-declaration-end) |
1092 (forward-line 1))))) | 1099 (forward-line 1))))) |
1093 | 1100 |
1094 (defun pascal-type-completion () | 1101 (defun pascal-type-completion () |
1137 | 1144 |
1138 | 1145 |
1139 (defun pascal-keyword-completion (keyword-list) | 1146 (defun pascal-keyword-completion (keyword-list) |
1140 "Give list of all possible completions of keywords in KEYWORD-LIST." | 1147 "Give list of all possible completions of keywords in KEYWORD-LIST." |
1141 (mapcar '(lambda (s) | 1148 (mapcar '(lambda (s) |
1142 (if (string-match (concat "\\<" str) s) | 1149 (if (string-match (concat "\\<" pascal-str) s) |
1143 (if (or (null predicate) | 1150 (if (or (null pascal-pred) |
1144 (funcall predicate s)) | 1151 (funcall pascal-pred s)) |
1145 (setq all (cons s all))))) | 1152 (setq pascal-all (cons s pascal-all))))) |
1146 keyword-list)) | 1153 keyword-list)) |
1147 | 1154 |
1148 ;; Function passed to completing-read, try-completion or | 1155 ;; Function passed to completing-read, try-completion or |
1149 ;; all-completions to get completion on STR. If predicate is non-nil, | 1156 ;; all-completions to get completion on STR. If predicate is non-nil, |
1150 ;; it must be a function to be called for every match to check if this | 1157 ;; it must be a function to be called for every match to check if this |
1152 ;; of all possible completions. If it is nil it returns a string, the | 1159 ;; of all possible completions. If it is nil it returns a string, the |
1153 ;; longest possible completion, or t if STR is an exact match. If flag | 1160 ;; longest possible completion, or t if STR is an exact match. If flag |
1154 ;; is 'lambda, the function returns t if STR is an exact match, nil | 1161 ;; is 'lambda, the function returns t if STR is an exact match, nil |
1155 ;; otherwise. | 1162 ;; otherwise. |
1156 | 1163 |
1157 (defun pascal-completion (str predicate flag) | 1164 (defun pascal-completion (pascal-str pascal-pred pascal-flag) |
1158 (save-excursion | 1165 (save-excursion |
1159 (let ((all nil)) | 1166 (let ((pascal-all nil)) |
1160 ;; Set buffer to use for searching labels. This should be set | 1167 ;; Set buffer to use for searching labels. This should be set |
1161 ;; within functins which use pascal-completions | 1168 ;; within functins which use pascal-completions |
1162 (set-buffer buffer-to-use) | 1169 (set-buffer pascal-buffer-to-use) |
1163 | 1170 |
1164 ;; Determine what should be completed | 1171 ;; Determine what should be completed |
1165 (let ((state (car (pascal-calculate-indent)))) | 1172 (let ((state (car (pascal-calculate-indent)))) |
1166 (cond (;--Within a declaration or parameterlist | 1173 (cond (;--Within a declaration or parameterlist |
1167 (or (eq state 'declaration) (eq state 'paramlist) | 1174 (or (eq state 'declaration) (eq state 'paramlist) |
1192 | 1199 |
1193 ;; Now we have built a list of all matches. Give response to caller | 1200 ;; Now we have built a list of all matches. Give response to caller |
1194 (pascal-completion-response)))) | 1201 (pascal-completion-response)))) |
1195 | 1202 |
1196 (defun pascal-completion-response () | 1203 (defun pascal-completion-response () |
1197 (cond ((or (equal flag 'lambda) (null flag)) | 1204 (cond ((or (equal pascal-flag 'lambda) (null pascal-flag)) |
1198 ;; This was not called by all-completions | 1205 ;; This was not called by all-completions |
1199 (if (null all) | 1206 (if (null pascal-all) |
1200 ;; Return nil if there was no matching label | 1207 ;; Return nil if there was no matching label |
1201 nil | 1208 nil |
1202 ;; Get longest string common in the labels | 1209 ;; Get longest string common in the labels |
1203 (let* ((elm (cdr all)) | 1210 (let* ((elm (cdr pascal-all)) |
1204 (match (car all)) | 1211 (match (car pascal-all)) |
1205 (min (length match)) | 1212 (min (length match)) |
1206 exact tmp) | 1213 exact tmp) |
1207 (if (string= match str) | 1214 (if (string= match pascal-str) |
1208 ;; Return t if first match was an exact match | 1215 ;; Return t if first match was an exact match |
1209 (setq match t) | 1216 (setq match t) |
1210 (while (not (null elm)) | 1217 (while (not (null elm)) |
1211 ;; Find longest common string | 1218 ;; Find longest common string |
1212 (if (< (setq tmp (pascal-string-diff match (car elm))) min) | 1219 (if (< (setq tmp (pascal-string-diff match (car elm))) min) |
1213 (progn | 1220 (progn |
1214 (setq min tmp) | 1221 (setq min tmp) |
1215 (setq match (substring match 0 min)))) | 1222 (setq match (substring match 0 min)))) |
1216 ;; Terminate with match=t if this is an exact match | 1223 ;; Terminate with match=t if this is an exact match |
1217 (if (string= (car elm) str) | 1224 (if (string= (car elm) pascal-str) |
1218 (progn | 1225 (progn |
1219 (setq match t) | 1226 (setq match t) |
1220 (setq elm nil)) | 1227 (setq elm nil)) |
1221 (setq elm (cdr elm))))) | 1228 (setq elm (cdr elm))))) |
1222 ;; If this is a test just for exact match, return nil ot t | 1229 ;; If this is a test just for exact match, return nil ot t |
1223 (if (and (equal flag 'lambda) (not (equal match 't))) | 1230 (if (and (equal pascal-flag 'lambda) (not (equal match 't))) |
1224 nil | 1231 nil |
1225 match)))) | 1232 match)))) |
1226 ;; If flag is t, this was called by all-completions. Return | 1233 ;; If flag is t, this was called by all-completions. Return |
1227 ;; list of all possible completions | 1234 ;; list of all possible completions |
1228 (flag | 1235 (pascal-flag |
1229 all))) | 1236 pascal-all))) |
1230 | 1237 |
1231 (defvar pascal-last-word-numb 0) | 1238 (defvar pascal-last-word-numb 0) |
1232 (defvar pascal-last-word-shown nil) | 1239 (defvar pascal-last-word-shown nil) |
1233 (defvar pascal-last-completions nil) | 1240 (defvar pascal-last-completions nil) |
1234 | 1241 |
1237 \(See also `pascal-toggle-completions', `pascal-type-keywords', | 1244 \(See also `pascal-toggle-completions', `pascal-type-keywords', |
1238 `pascal-start-keywords' and `pascal-separator-keywords'.)" | 1245 `pascal-start-keywords' and `pascal-separator-keywords'.)" |
1239 (interactive) | 1246 (interactive) |
1240 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point))) | 1247 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point))) |
1241 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point))) | 1248 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point))) |
1242 (str (buffer-substring b e)) | 1249 (pascal-str (buffer-substring b e)) |
1243 ;; The following variable is used in pascal-completion | 1250 ;; The following variable is used in pascal-completion |
1244 (buffer-to-use (current-buffer)) | 1251 (pascal-buffer-to-use (current-buffer)) |
1245 (allcomp (if (and pascal-toggle-completions | 1252 (allcomp (if (and pascal-toggle-completions |
1246 (string= pascal-last-word-shown str)) | 1253 (string= pascal-last-word-shown pascal-str)) |
1247 pascal-last-completions | 1254 pascal-last-completions |
1248 (all-completions str 'pascal-completion))) | 1255 (all-completions pascal-str 'pascal-completion))) |
1249 (match (if pascal-toggle-completions | 1256 (match (if pascal-toggle-completions |
1250 "" (try-completion | 1257 "" (try-completion |
1251 str (mapcar '(lambda (elm) (cons elm 0)) allcomp))))) | 1258 pascal-str (mapcar '(lambda (elm) |
1259 (cons elm 0)) allcomp))))) | |
1252 ;; Delete old string | 1260 ;; Delete old string |
1253 (delete-region b e) | 1261 (delete-region b e) |
1254 | 1262 |
1255 ;; Toggle-completions inserts whole labels | 1263 ;; Toggle-completions inserts whole labels |
1256 (if pascal-toggle-completions | 1264 (if pascal-toggle-completions |
1263 (1+ pascal-last-word-numb))) | 1271 (1+ pascal-last-word-numb))) |
1264 (setq pascal-last-word-shown (elt allcomp pascal-last-word-numb)) | 1272 (setq pascal-last-word-shown (elt allcomp pascal-last-word-numb)) |
1265 ;; Display next match or same string if no match was found | 1273 ;; Display next match or same string if no match was found |
1266 (if (not (null allcomp)) | 1274 (if (not (null allcomp)) |
1267 (insert "" pascal-last-word-shown) | 1275 (insert "" pascal-last-word-shown) |
1268 (insert "" str) | 1276 (insert "" pascal-str) |
1269 (message "(No match)"))) | 1277 (message "(No match)"))) |
1270 ;; The other form of completion does not necessarly do that. | 1278 ;; The other form of completion does not necessarly do that. |
1271 | 1279 |
1272 ;; Insert match if found, or the original string if no match | 1280 ;; Insert match if found, or the original string if no match |
1273 (if (or (null match) (equal match 't)) | 1281 (if (or (null match) (equal match 't)) |
1274 (progn (insert "" str) | 1282 (progn (insert "" pascal-str) |
1275 (message "(No match)")) | 1283 (message "(No match)")) |
1276 (insert "" match)) | 1284 (insert "" match)) |
1277 ;; Give message about current status of completion | 1285 ;; Give message about current status of completion |
1278 (cond ((equal match 't) | 1286 (cond ((equal match 't) |
1279 (if (not (null (cdr allcomp))) | 1287 (if (not (null (cdr allcomp))) |
1280 (message "(Complete but not unique)") | 1288 (message "(Complete but not unique)") |
1281 (message "(Sole completion)"))) | 1289 (message "(Sole completion)"))) |
1282 ;; Display buffer if the current completion didn't help | 1290 ;; Display buffer if the current completion didn't help |
1283 ;; on completing the label. | 1291 ;; on completing the label. |
1284 ((and (not (null (cdr allcomp))) (= (length str) (length match))) | 1292 ((and (not (null (cdr allcomp))) (= (length pascal-str) |
1293 (length match))) | |
1285 (with-output-to-temp-buffer "*Completions*" | 1294 (with-output-to-temp-buffer "*Completions*" |
1286 (display-completion-list allcomp)) | 1295 (display-completion-list allcomp)) |
1287 ;; Wait for a keypress. Then delete *Completion* window | 1296 ;; Wait for a keypress. Then delete *Completion* window |
1288 (momentary-string-display "" (point)) | 1297 (momentary-string-display "" (point)) |
1289 (delete-window (get-buffer-window (get-buffer "*Completions*"))) | 1298 (delete-window (get-buffer-window (get-buffer "*Completions*"))) |
1292 (defun pascal-show-completions () | 1301 (defun pascal-show-completions () |
1293 "Show all possible completions at current point." | 1302 "Show all possible completions at current point." |
1294 (interactive) | 1303 (interactive) |
1295 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point))) | 1304 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point))) |
1296 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point))) | 1305 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point))) |
1297 (str (buffer-substring b e)) | 1306 (pascal-str (buffer-substring b e)) |
1298 ;; The following variable is used in pascal-completion | 1307 ;; The following variable is used in pascal-completion |
1299 (buffer-to-use (current-buffer)) | 1308 (pascal-buffer-to-use (current-buffer)) |
1300 (allcomp (if (and pascal-toggle-completions | 1309 (allcomp (if (and pascal-toggle-completions |
1301 (string= pascal-last-word-shown str)) | 1310 (string= pascal-last-word-shown pascal-str)) |
1302 pascal-last-completions | 1311 pascal-last-completions |
1303 (all-completions str 'pascal-completion)))) | 1312 (all-completions pascal-str 'pascal-completion)))) |
1304 ;; Show possible completions in a temporary buffer. | 1313 ;; Show possible completions in a temporary buffer. |
1305 (with-output-to-temp-buffer "*Completions*" | 1314 (with-output-to-temp-buffer "*Completions*" |
1306 (display-completion-list allcomp)) | 1315 (display-completion-list allcomp)) |
1307 ;; Wait for a keypress. Then delete *Completion* window | 1316 ;; Wait for a keypress. Then delete *Completion* window |
1308 (momentary-string-display "" (point)) | 1317 (momentary-string-display "" (point)) |
1334 ;; function returns a list of all possible completions. If it is nil | 1343 ;; function returns a list of all possible completions. If it is nil |
1335 ;; it returns a string, the longest possible completion, or t if STR | 1344 ;; it returns a string, the longest possible completion, or t if STR |
1336 ;; is an exact match. If flag is 'lambda, the function returns t if | 1345 ;; is an exact match. If flag is 'lambda, the function returns t if |
1337 ;; STR is an exact match, nil otherwise. | 1346 ;; STR is an exact match, nil otherwise. |
1338 | 1347 |
1339 (defun pascal-comp-defun (str predicate flag) | 1348 (defun pascal-comp-defun (pascal-str pascal-pred pascal-flag) |
1340 (save-excursion | 1349 (save-excursion |
1341 (let ((all nil) | 1350 (let ((pascal-all nil) |
1342 match) | 1351 match) |
1343 | 1352 |
1344 ;; Set buffer to use for searching labels. This should be set | 1353 ;; Set buffer to use for searching labels. This should be set |
1345 ;; within functins which use pascal-completions | 1354 ;; within functins which use pascal-completions |
1346 (set-buffer buffer-to-use) | 1355 (set-buffer pascal-buffer-to-use) |
1347 | 1356 |
1348 (let ((str str)) | 1357 (let ((pascal-str pascal-str)) |
1349 ;; Build regular expression for functions | 1358 ;; Build regular expression for functions |
1350 (if (string= str "") | 1359 (if (string= pascal-str "") |
1351 (setq str (pascal-build-defun-re "[a-zA-Z_]")) | 1360 (setq pascal-str (pascal-build-defun-re "[a-zA-Z_]")) |
1352 (setq str (pascal-build-defun-re str))) | 1361 (setq pascal-str (pascal-build-defun-re pascal-str))) |
1353 (goto-char (point-min)) | 1362 (goto-char (point-min)) |
1354 | 1363 |
1355 ;; Build a list of all possible completions | 1364 ;; Build a list of all possible completions |
1356 (while (re-search-forward str nil t) | 1365 (while (re-search-forward pascal-str nil t) |
1357 (setq match (buffer-substring (match-beginning 2) (match-end 2))) | 1366 (setq match (buffer-substring (match-beginning 2) (match-end 2))) |
1358 (if (or (null predicate) | 1367 (if (or (null pascal-pred) |
1359 (funcall predicate match)) | 1368 (funcall pascal-pred match)) |
1360 (setq all (cons match all))))) | 1369 (setq pascal-all (cons match pascal-all))))) |
1361 | 1370 |
1362 ;; Now we have built a list of all matches. Give response to caller | 1371 ;; Now we have built a list of all matches. Give response to caller |
1363 (pascal-completion-response)))) | 1372 (pascal-completion-response)))) |
1364 | 1373 |
1365 (defun pascal-goto-defun () | 1374 (defun pascal-goto-defun () |
1366 "Move to specified Pascal function/procedure. | 1375 "Move to specified Pascal function/procedure. |
1367 The default is a name found in the buffer around point." | 1376 The default is a name found in the buffer around point." |
1368 (interactive) | 1377 (interactive) |
1369 (let* ((default (pascal-get-default-symbol)) | 1378 (let* ((default (pascal-get-default-symbol)) |
1370 ;; The following variable is used in pascal-comp-function | 1379 ;; The following variable is used in pascal-comp-function |
1371 (buffer-to-use (current-buffer)) | 1380 (pascal-buffer-to-use (current-buffer)) |
1372 (default (if (pascal-comp-defun default nil 'lambda) | 1381 (default (if (pascal-comp-defun default nil 'lambda) |
1373 default "")) | 1382 default "")) |
1374 (label (if (not (string= default "")) | 1383 (label (if (not (string= default "")) |
1375 ;; Do completion with default | 1384 ;; Do completion with default |
1376 (completing-read (concat "Label: (default " default ") ") | 1385 (completing-read (concat "Label: (default " default ") ") |
1446 (progn | 1455 (progn |
1447 (setq selective-display nil) | 1456 (setq selective-display nil) |
1448 (pascal-show-all) | 1457 (pascal-show-all) |
1449 (use-local-map pascal-mode-map)))) | 1458 (use-local-map pascal-mode-map)))) |
1450 | 1459 |
1451 (defun pascal-outline-change (b e flag) | 1460 (defun pascal-outline-change (b e pascal-flag) |
1452 (let ((modp (buffer-modified-p))) | 1461 (let ((modp (buffer-modified-p))) |
1453 (unwind-protect | 1462 (unwind-protect |
1454 (subst-char-in-region b e (if (= flag ?\n) ?\^M ?\n) flag) | 1463 (subst-char-in-region b e (if (= pascal-flag ?\n) |
1464 ?\^M ?\n) pascal-flag) | |
1455 (set-buffer-modified-p modp)))) | 1465 (set-buffer-modified-p modp)))) |
1456 | 1466 |
1457 (defun pascal-show-all () | 1467 (defun pascal-show-all () |
1458 "Show all of the text in the buffer." | 1468 "Show all of the text in the buffer." |
1459 (interactive) | 1469 (interactive) |