Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 135 lines (112 sloc) 4.78 kb
3aa866a @klacke two-mode.el contributed by David Welton
authored
1 ;; two-mode-mode.el -- switches between tcl and sgml(html) modes
2 ;; $Id$
3
4 ;; Copyright 1999-2004 The Apache Software Foundation
5
6 ;; Licensed under the Apache License, Version 2.0 (the "License");
7 ;; you may not use this file except in compliance with the License.
8 ;; You may obtain a copy of the License at
9
0be3c7e @klacke untabified all of yaws
authored
10 ;; http://www.apache.org/licenses/LICENSE-2.0
3aa866a @klacke two-mode.el contributed by David Welton
authored
11
12 ;; Unless required by applicable law or agreed to in writing, software
13 ;; distributed under the License is distributed on an "AS IS" BASIS,
14 ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ;; See the License for the specific language governing permissions and
16 ;; limitations under the License.
17
18 ;; These same concepts could be used to do a number of neat 2-mode
19 ;; modes, for things like PHP, or anything else where you have a
20 ;; couple of modes you'd like to use.
21
22 ;; Use of 'psgml-mode' is highly recommended. It is, of course, a
23 ;; part of Debian GNU/Linux.
24
25 ;; Author: David N. Welton <davidw@dedasys.com>
26
27 ;; Modified by Marco Pantaleoni <panta@elasticworld.org>
28 ;; to allow execution of an hook on mode switching.
29 ;; Also added a standard mode hook and some documentation strings.
30
31 ;; Janko Heilgeist <janko@heilgeist.com> and Stefan Schimanski
32 ;; <1stein@gmx.de> submitted modifications that enable the use of
33 ;; multiple modes, so I suppose that 'two-mode-mode' isn't strictly
34 ;; accurate anymore.
35
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
36 ;; Modified May 2010 by Steve Vinoski to work with emacs 23.
37
3aa866a @klacke two-mode.el contributed by David Welton
authored
38 ;; configure these:
39 (defvar default-mode (list "SGML" 'sgml-mode))
40 (defvar second-modes (list
0be3c7e @klacke untabified all of yaws
authored
41 (list "Erlang" "<erl>" "</erl>" 'erlang-mode)
42 (list "Erlang" "<?erl" "?>" 'erlang-mode)
3aa866a @klacke two-mode.el contributed by David Welton
authored
43 (list "C++" "<?php" "?>" 'c++-mode)
44 (list "Python" "<?python" "?>" 'python-mode)
0be3c7e @klacke untabified all of yaws
authored
45 (list "Tcl" "<?" "?>" 'tcl-mode)
46 ))
3aa866a @klacke two-mode.el contributed by David Welton
authored
47 ;; ----------------
48
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
49 (defvar two-mode-update nil)
3aa866a @klacke two-mode.el contributed by David Welton
authored
50 (defvar two-mode-mode-idle-timer nil)
51 (defvar two-mode-bool nil)
52 (defvar two-mode-mode-delay (/ (float 1) (float 8)))
53
54 ;; Two mode hook
55 (defvar two-mode-hook nil
56 "*Hook called by `two-mode'.")
57 (setq two-mode-hook nil)
58
59 ;; Mode switching hook
60 (defvar two-mode-switch-hook nil
61 "*Hook called upon mode switching.")
62 (setq two-mode-switch-hook nil)
63
64 (defun two-mode-mode-setup ()
65 (add-hook 'post-command-hook 'two-mode-mode-need-update nil t)
66 (make-local-variable 'two-mode-bool)
67 (setq two-mode-bool t)
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
68 (make-local-variable 'two-mode-mode-idle-timer)
69 (if two-mode-mode-idle-timer
3aa866a @klacke two-mode.el contributed by David Welton
authored
70 (cancel-timer two-mode-mode-idle-timer))
71 (setq two-mode-mode-idle-timer
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
72 (run-with-idle-timer two-mode-mode-delay t 'two-mode-mode-update-mode))
73 (make-local-variable 'minor-mode-alist)
3aa866a @klacke two-mode.el contributed by David Welton
authored
74 (or (assq 'two-mode-bool minor-mode-alist)
75 (setq minor-mode-alist
0be3c7e @klacke untabified all of yaws
authored
76 (cons '(two-mode-bool " two-mode") minor-mode-alist))))
3aa866a @klacke two-mode.el contributed by David Welton
authored
77
78 (defun two-mode-mode-need-update ()
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
79 (setq two-mode-update t))
3aa866a @klacke two-mode.el contributed by David Welton
authored
80
81 (defun two-mode-change-mode (to-mode func)
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
82 (let ((mode (if (listp mode-name) (car (last mode-name)) mode-name)))
83 (if (string= to-mode mode)
84 t
3aa866a @klacke two-mode.el contributed by David Welton
authored
85 (funcall func)
86 ;; After the mode was set, we reread the "Local Variables" section.
87 ;; We do need this for example in SGML-mode if "sgml-parent-document"
88 ;; was set, or otherwise it will be reset to nil when sgml-mode is left.
89 (hack-local-variables)
90
91 (two-mode-mode-setup)
92 (if two-mode-switch-hook
0be3c7e @klacke untabified all of yaws
authored
93 (run-hooks 'two-mode-switch-hook))
3aa866a @klacke two-mode.el contributed by David Welton
authored
94 (if (eq font-lock-mode t)
0be3c7e @klacke untabified all of yaws
authored
95 (font-lock-fontify-buffer))
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
96 (if (fboundp 'turn-on-font-lock-if-enabled)
97 (turn-on-font-lock-if-enabled)
98 (turn-on-font-lock-if-desired)))))
3aa866a @klacke two-mode.el contributed by David Welton
authored
99
100 (defun two-mode-mode-update-mode ()
101 (when (and two-mode-bool two-mode-update)
102 (setq two-mode-update 0)
103 (let ((mode-list second-modes)
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
104 (to-mode (car default-mode))
105 (func (cadr default-mode)))
3aa866a @klacke two-mode.el contributed by David Welton
authored
106 (while mode-list
0be3c7e @klacke untabified all of yaws
authored
107 (let ((mode (car mode-list))
108 (lm -1)
109 (rm -1))
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
110 (save-excursion
0be3c7e @klacke untabified all of yaws
authored
111 (if (search-backward (cadr mode) nil t)
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
112 (setq lm (point))))
0be3c7e @klacke untabified all of yaws
authored
113 (save-excursion
114 (if (search-backward (car (cddr mode)) nil t)
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
115 (setq rm (point))))
0be3c7e @klacke untabified all of yaws
authored
116 (if (and (not (and (= lm -1) (= rm -1))) (>= lm rm))
117 (progn
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
118 (setq mode-list nil)
119 (setq to-mode (car mode))
120 (setq func (car (cdr (cddr mode)))))
121 (setq mode-list (cdr mode-list)))))
122 (two-mode-change-mode to-mode func))))
3aa866a @klacke two-mode.el contributed by David Welton
authored
123
124 (defun two-mode-mode ()
125 "Turn on two-mode-mode"
126 (interactive)
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
127 (setq two-mode-update t)
3aa866a @klacke two-mode.el contributed by David Welton
authored
128 (two-mode-mode-setup)
ebb4323 @vinoski updates to make two-mode-mode work with emacs 23
vinoski authored
129 (two-mode-mode-update-mode)
3aa866a @klacke two-mode.el contributed by David Welton
authored
130 (if two-mode-hook
131 (run-hooks 'two-mode-hook)))
132
133 (provide 'two-mode-mode)
134
Something went wrong with that request. Please try again.