comparison lisp/progmodes/ada-mode.el @ 82453:d04e217671f9

(ada-create-syntax-table): Move set-syntax-table from here to ... (ada-mode): ... here. Do not change global value of comment-multi-line. Call new function ada-initialize-syntax-table-properties and add new function ada-handle-syntax-table-properties to font-lock-mode-hook. (ada-deactivate-properties, ada-initialize-properties): Replace by new functions ... (ada-handle-syntax-table-properties) (ada-initialize-syntax-table-properties) (ada-set-syntax-table-properties): ... to set up syntax-table properties uniformly, independently from whether font-lock-mode is enabled or not. Handle read-only buffers and do not change undo-list when setting syntax-table properties. (ada-after-change-function): Use ada-set-syntax-table-properties.
author Martin Rudalics <rudalics@gmx.at>
date Sat, 18 Aug 2007 08:37:41 +0000
parents c1ec1c8a8d2e
children 4f60c94dc647 aaccdab0ee26
comparison
equal deleted inserted replaced
82452:58eaf01ab6e0 82453:d04e217671f9
827 ;; - " starts a string, but not if inside a constant character. 827 ;; - " starts a string, but not if inside a constant character.
828 ;; - ( and ) should be ignored if inside a constant character. 828 ;; - ( and ) should be ignored if inside a constant character.
829 ;; Thus their syntax property is changed automatically, and we can still use 829 ;; Thus their syntax property is changed automatically, and we can still use
830 ;; the standard Emacs functions for sexp (see `ada-in-string-p') 830 ;; the standard Emacs functions for sexp (see `ada-in-string-p')
831 ;; 831 ;;
832 ;; On Emacs, this is done through the `syntax-table' text property. The 832 ;; On Emacs, this is done through the `syntax-table' text property. The
833 ;; modification is done automatically each time the user as typed a new 833 ;; corresponding action is applied automatically each time the buffer
834 ;; character. This is already done in `font-lock-mode' (in 834 ;; changes. If `font-lock-mode' is enabled (the default) the action is
835 ;; `font-lock-syntactic-keywords', so we take advantage of the existing 835 ;; set up by `font-lock-syntactic-keywords'. Otherwise, we do it
836 ;; mechanism. If font-lock-mode is not activated, we do it by hand in 836 ;; manually in `ada-after-change-function'. The proper method is
837 ;; `ada-after-change-function', thanks to `ada-deactivate-properties' and 837 ;; installed by `ada-handle-syntax-table-properties'.
838 ;; `ada-initialize-properties'.
839 ;; 838 ;;
840 ;; on XEmacs, the `syntax-table' property does not exist and we have to use a 839 ;; on XEmacs, the `syntax-table' property does not exist and we have to use a
841 ;; slow advice to `parse-partial-sexp' to do the same thing. 840 ;; slow advice to `parse-partial-sexp' to do the same thing.
842 ;; When executing parse-partial-sexp, we simply modify the strings before and 841 ;; When executing parse-partial-sexp, we simply modify the strings before and
843 ;; after, so that the special constants '"', '(' and ')' do not interact 842 ;; after, so that the special constants '"', '(' and ')' do not interact
850 "Create the two syntax tables use in the Ada mode. 849 "Create the two syntax tables use in the Ada mode.
851 The standard table declares `_' as a symbol constituent, the second one 850 The standard table declares `_' as a symbol constituent, the second one
852 declares it as a word constituent." 851 declares it as a word constituent."
853 (interactive) 852 (interactive)
854 (setq ada-mode-syntax-table (make-syntax-table)) 853 (setq ada-mode-syntax-table (make-syntax-table))
855 (set-syntax-table ada-mode-syntax-table)
856 854
857 ;; define string brackets (`%' is alternative string bracket, but 855 ;; define string brackets (`%' is alternative string bracket, but
858 ;; almost never used as such and throws font-lock and indentation 856 ;; almost never used as such and throws font-lock and indentation
859 ;; off the track.) 857 ;; off the track.)
860 (modify-syntax-entry ?% "$" ada-mode-syntax-table) 858 (modify-syntax-entry ?% "$" ada-mode-syntax-table)
934 (goto-char (caar change)) 932 (goto-char (caar change))
935 (delete-char (cadar change)) 933 (delete-char (cadar change))
936 (insert (caddar change)) 934 (insert (caddar change))
937 (setq change (cdr change))))))) 935 (setq change (cdr change)))))))
938 936
939 (defun ada-deactivate-properties () 937 (defun ada-set-syntax-table-properties ()
940 "Deactivate Ada mode's properties handling. 938 "Assign `syntax-table' properties in accessible part of buffer.
941 This would be a duplicate of font-lock if both are used at the same time." 939 In particular, character constants are said to be strings, #...#
942 (remove-hook 'after-change-functions 'ada-after-change-function t)) 940 are treated as numbers instead of gnatprep comments."
943 941 (let ((modified (buffer-modified-p))
944 (defun ada-initialize-properties () 942 (buffer-undo-list t)
945 "Initialize some special text properties in the whole buffer. 943 (inhibit-read-only t)
946 In particular, character constants are said to be strings, #...# are treated 944 (inhibit-point-motion-hooks t)
947 as numbers instead of gnatprep comments." 945 (inhibit-modification-hooks t))
948 (save-excursion 946 (remove-text-properties (point-min) (point-max) '(syntax-table nil))
949 (save-restriction 947 (goto-char (point-min))
950 (widen) 948 (while (re-search-forward
951 (goto-char (point-min)) 949 ;; The following regexp was adapted from
952 (while (re-search-forward "'.'" nil t) 950 ;; `ada-font-lock-syntactic-keywords'.
953 (add-text-properties (match-beginning 0) (match-end 0) 951 "^[ \t]*\\(#\\(?:if\\|else\\|elsif\\|end\\)\\)\\|[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)"
954 '(syntax-table ("'" . ?\")))) 952 nil t)
955 (goto-char (point-min)) 953 (if (match-beginning 1)
956 (while (re-search-forward "^[ \t]*#" nil t) 954 (put-text-property
957 (add-text-properties (match-beginning 0) (match-end 0) 955 (match-beginning 1) (match-end 1) 'syntax-table '(11 . ?\n))
958 '(syntax-table (11 . 10)))) 956 (put-text-property
959 (set-buffer-modified-p nil) 957 (match-beginning 2) (match-end 2) 'syntax-table '(7 . ?'))
960 958 (put-text-property
961 ;; Setting this only if font-lock is not set won't work 959 (match-beginning 3) (match-end 3) 'syntax-table '(7 . ?'))))
962 ;; if the user activates or deactivates font-lock-mode, 960 (unless modified
963 ;; but will make things faster most of the time 961 (restore-buffer-modified-p nil))))
964 (add-hook 'after-change-functions 'ada-after-change-function nil t)
965 )))
966 962
967 (defun ada-after-change-function (beg end old-len) 963 (defun ada-after-change-function (beg end old-len)
968 "Called when the region between BEG and END was changed in the buffer. 964 "Called when the region between BEG and END was changed in the buffer.
969 OLD-LEN indicates what the length of the replaced text was." 965 OLD-LEN indicates what the length of the replaced text was."
970 (let ((inhibit-point-motion-hooks t) 966 (save-excursion
971 (eol (point))) 967 (save-restriction
968 (let ((from (progn (goto-char beg) (line-beginning-position)))
969 (to (progn (goto-char end) (line-end-position))))
970 (narrow-to-region from to)
971 (save-match-data
972 (ada-set-syntax-table-properties))))))
973
974 (defun ada-initialize-syntax-table-properties ()
975 "Assign `syntax-table' properties in current buffer."
972 (save-excursion 976 (save-excursion
973 (save-match-data 977 (save-restriction
974 (beginning-of-line) 978 (widen)
975 (remove-text-properties (point) eol '(syntax-table nil)) 979 (save-match-data
976 (while (re-search-forward "'.'" eol t) 980 (ada-set-syntax-table-properties))))
977 (add-text-properties (match-beginning 0) (match-end 0) 981 (add-hook 'after-change-functions 'ada-after-change-function nil t))
978 '(syntax-table ("'" . ?\")))) 982
979 (beginning-of-line) 983 (defun ada-handle-syntax-table-properties ()
980 (if (looking-at "^[ \t]*#") 984 "Handle `syntax-table' properties."
981 (add-text-properties (match-beginning 0) (match-end 0) 985 (if font-lock-mode
982 '(syntax-table (11 . 10)))))))) 986 ;; `font-lock-mode' will take care of `syntax-table' properties.
987 (remove-hook 'after-change-functions 'ada-after-change-function t)
988 ;; Take care of `syntax-table' properties manually.
989 (ada-initialize-syntax-table-properties)))
983 990
984 ;;------------------------------------------------------------------ 991 ;;------------------------------------------------------------------
985 ;; Testing the grammatical context 992 ;; Testing the grammatical context
986 ;;------------------------------------------------------------------ 993 ;;------------------------------------------------------------------
987 994
1148 or '\\[ada-goto-declaration]' with point on the identifier 1155 or '\\[ada-goto-declaration]' with point on the identifier
1149 Complete identifier: '\\[ada-complete-identifier]'." 1156 Complete identifier: '\\[ada-complete-identifier]'."
1150 1157
1151 (interactive) 1158 (interactive)
1152 (kill-all-local-variables) 1159 (kill-all-local-variables)
1160
1161 (set-syntax-table ada-mode-syntax-table)
1153 1162
1154 (set (make-local-variable 'require-final-newline) mode-require-final-newline) 1163 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
1155 1164
1156 ;; Set the paragraph delimiters so that one can select a whole block 1165 ;; Set the paragraph delimiters so that one can select a whole block
1157 ;; simply with M-h 1166 ;; simply with M-h
1338 ;; Support for which-function mode 1347 ;; Support for which-function mode
1339 (make-local-variable 'which-func-functions) 1348 (make-local-variable 'which-func-functions)
1340 (setq which-func-functions '(ada-which-function)) 1349 (setq which-func-functions '(ada-which-function))
1341 1350
1342 ;; Support for indent-new-comment-line (Especially for XEmacs) 1351 ;; Support for indent-new-comment-line (Especially for XEmacs)
1343 (setq comment-multi-line nil) 1352 (set (make-local-variable 'comment-multi-line) nil)
1344 1353
1345 (setq major-mode 'ada-mode 1354 (setq major-mode 'ada-mode
1346 mode-name "Ada") 1355 mode-name "Ada")
1347 1356
1348 (use-local-map ada-mode-map) 1357 (use-local-map ada-mode-map)
1375 1384
1376 ;; Run this after the hook to give the users a chance to activate 1385 ;; Run this after the hook to give the users a chance to activate
1377 ;; font-lock-mode 1386 ;; font-lock-mode
1378 1387
1379 (unless (featurep 'xemacs) 1388 (unless (featurep 'xemacs)
1380 (progn 1389 (ada-initialize-syntax-table-properties)
1381 (ada-initialize-properties) 1390 (add-hook 'font-lock-mode-hook 'ada-handle-syntax-table-properties nil t))
1382 (add-hook 'font-lock-mode-hook 'ada-deactivate-properties nil t)))
1383 1391
1384 ;; the following has to be done after running the ada-mode-hook 1392 ;; the following has to be done after running the ada-mode-hook
1385 ;; because users might want to set the values of these variable 1393 ;; because users might want to set the values of these variable
1386 ;; inside the hook 1394 ;; inside the hook
1387 1395
5198 ;; set this special case, then the rest of the buffer is highlighted as 5206 ;; set this special case, then the rest of the buffer is highlighted as
5199 ;; a string 5207 ;; a string
5200 ;; This sets the properties of the characters, so that ada-in-string-p 5208 ;; This sets the properties of the characters, so that ada-in-string-p
5201 ;; correctly handles '"' too... 5209 ;; correctly handles '"' too...
5202 '(("[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?'))) 5210 '(("[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?')))
5203 ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n))) 5211 ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n)))))
5204 ))
5205 5212
5206 (defvar ada-font-lock-keywords 5213 (defvar ada-font-lock-keywords
5207 (eval-when-compile 5214 (eval-when-compile
5208 (list 5215 (list
5209 ;; 5216 ;;