| 1 |
|
| 2 |
@! fwmode.fw - FunnelWeb major mode for Emacs (Elisp). |
| 3 |
|
| 4 |
@p typesetter = tex |
| 5 |
|
| 6 |
\documentstyle{report} |
| 7 |
|
| 8 |
\title{ |
| 9 |
An Emacs/Elisp Major Mode for FunnelWeb |
| 10 |
} |
| 11 |
|
| 12 |
\author{ |
| 13 |
A.B.Coates \\ |
| 14 |
Department of Physics \\ |
| 15 |
The University of Queensland \\ |
| 16 |
QLD\ \ 4072 \\ |
| 17 |
Australia \\ |
| 18 |
Email: {\tt coates@@physics.uq.oz.au} |
| 19 |
} |
| 20 |
|
| 21 |
\begin{document} |
| 22 |
|
| 23 |
\maketitle |
| 24 |
|
| 25 |
\tableofcontents |
| 26 |
|
| 27 |
\chapter{Major and Minor Modes} |
| 28 |
|
| 29 |
\section{The FunnelWeb Major Mode} |
| 30 |
This is the definition of the function which defines the FunnelWeb |
| 31 |
mode. A brief (and not exhaustive) description of the functions |
| 32 |
available is given in its description string. |
| 33 |
|
| 34 |
@$@<fw-mode Function Definitions@>+=@{ |
| 35 |
(defun fw-mode () |
| 36 |
"Major mode for editing FunnelWeb files. Built on top of |
| 37 |
tex-mode. Makes @@} , @@) , and @@> display their matching |
| 38 |
opening braces @@{ , @@( , or @@< . |
| 39 |
|
| 40 |
Use \\[fw-buffer] to run FunnelWeb on the current buffer |
| 41 |
*without* saving it. |
| 42 |
Use \\[fw-file] to be prompted to save the buffer to a file |
| 43 |
first, before running FunnelWeb on the file. |
| 44 |
Use \\[fw-tex] to run the (La)TeX output file through TeX or |
| 45 |
LaTeX as appropriate. |
| 46 |
Use \\[fw-print] to print a .dvi file. |
| 47 |
Use \\[fw-show-print-queue] to show the print queue that |
| 48 |
\\[fw-print] put your job on. |
| 49 |
Commands from tex-mode (or a similar selected mode) are |
| 50 |
mapped to operate on the (La)TeX file produced by FunnelWeb. |
| 51 |
|
| 52 |
Key sequences: |
| 53 |
|
| 54 |
see documentation for `switch-mode' for key sequences |
| 55 |
relating to mode switching. |
| 56 |
|
| 57 |
Mode variables: |
| 58 |
fw-autofill-mode |
| 59 |
Whether autofill-mode is automatically invoked for |
| 60 |
FunnelWeb files. Set to 0 for no, to a positive |
| 61 |
number for yes. |
| 62 |
fw-TeX-mode |
| 63 |
The major (La)TeX mode on which fw-mode is based. |
| 64 |
Typically either 'tex-mode or 'latex-mode |
| 65 |
(or perhaps 'tex-init for Auc-TeX |
| 66 |
or 'html-mode for hypertext ...). |
| 67 |
fw-directory |
| 68 |
Directory in which to create temporary files for |
| 69 |
TeX jobs run by \\[fw-buffer] or \\[fw-file]. |
| 70 |
fw-use-TeX-quote-style |
| 71 |
Set this to nil so the the \" key produces the |
| 72 |
normal \" character. If set to true, the \" key |
| 73 |
works as for (La)TeX mode, producing either |
| 74 |
`` or ''. |
| 75 |
fw-dvi-print-command |
| 76 |
Command string used by \\[fw-print] to print a |
| 77 |
.dvi file. |
| 78 |
fw-show-queue-command |
| 79 |
Command string used by \\[fw-show-print-queue] to |
| 80 |
show the print queue that \\[fw-print] put your |
| 81 |
job on. |
| 82 |
|
| 83 |
Entering FunnelWeb-mode calls the value of fw-mode-hook." |
| 84 |
(interactive) |
| 85 |
(funcall fw-TeX-mode) |
| 86 |
(hack-local-variables) |
| 87 |
(setq mode-name "FunnelWeb") |
| 88 |
(setq major-mode 'fw-mode) |
| 89 |
(auto-fill-mode fw-auto-fill-mode) |
| 90 |
(if fw-mode-map (use-local-map fw-mode-map)) |
| 91 |
(setq fw-buffer-p t) |
| 92 |
(switch-mode fw-switch-minor-mode-init) |
| 93 |
(run-hooks 'fw-mode-hook)) |
| 94 |
@} |
| 95 |
|
| 96 |
\section{The Switch Minor Mode} |
| 97 |
|
| 98 |
This is the definition of the function which defines the Switch |
| 99 |
minor mode. A brief (and not exhaustive) description of the functions |
| 100 |
available is given in its description string. |
| 101 |
|
| 102 |
@$@<switch-mode Function Definitions 2@>+=@{ |
| 103 |
(defun switch-mode |
| 104 |
( &optional set-mode-p |
| 105 |
no-check-p |
| 106 |
region-open-string |
| 107 |
region-close-string |
| 108 |
region-boundary-pattern ) |
| 109 |
"With no arguments, toggle whether the region mode |
| 110 |
checking is activated or not. Optional first argument |
| 111 |
SET-MODE-P can be used to definitely switch the region |
| 112 |
mode checking on or off: a positive number for on, off |
| 113 |
otherwise. |
| 114 |
Optional second argument NO-CHECK-P is nil for a |
| 115 |
check of whether the point is in a code region or not |
| 116 |
to be taken immediately. With any other value, no check |
| 117 |
is done. |
| 118 |
Optional third and fourth arguments REGION-OPEN-STRING |
| 119 |
and REGION-CLOSE-STRING define the boundaries by which a |
| 120 |
code region is recognised. |
| 121 |
Optional fourth argument REGION-BOUNDARY_PATTERN should |
| 122 |
be a pattern equivalent to |
| 123 |
\\(REGION-OPEN-STRING\\|REGION-CLOSE-STRING\\) |
| 124 |
i.e. a pattern which recognises either an opening or closing |
| 125 |
pattern. |
| 126 |
|
| 127 |
Key sequences: |
| 128 |
|
| 129 |
\\C-x\\C-a key sequences are used for the \"Switch\" minor |
| 130 |
mode, which allows modes to be swapped while editing a file. |
| 131 |
|
| 132 |
\\C-x \\C-a t : toggle Switch minor-mode on and off |
| 133 |
\\C-x \\C-a c : check whether in macro region or not |
| 134 |
\\C-x \\C-a p : toggles whether the default macro region |
| 135 |
mode is used with or without prompting |
| 136 |
\\C-x \\C-a s : set the current macro region mode" |
| 137 |
(interactive) |
| 138 |
@} |
| 139 |
|
| 140 |
A variable, local to each buffer, is needed to note whether |
| 141 |
switch-mode is active or not. |
| 142 |
|
| 143 |
@$@<switch-mode Variable Definitions 1@>+=@{ |
| 144 |
(defvar switch-mode nil |
| 145 |
"Whether the region checking minor mode is active or not.") |
| 146 |
(make-variable-buffer-local 'switch-mode) |
| 147 |
@} |
| 148 |
|
| 149 |
The boolean value {\tt switch-on-p} is given the value {\tt t} if the |
| 150 |
switch minor mode should be turned on, {\tt nil} otherwise. The value |
| 151 |
depends on when the function call toggles the state or sets it |
| 152 |
directly. |
| 153 |
|
| 154 |
@$@<switch-mode Function Definitions 2@>+=@{@- |
| 155 |
(let ((switch-on-p |
| 156 |
(or (and (numberp set-mode-p) |
| 157 |
(> set-mode-p 0)) |
| 158 |
(not switch-mode)))) |
| 159 |
(if switch-on-p |
| 160 |
(let ((old-switch-mode-value |
| 161 |
switch-mode)) |
| 162 |
(setq switch-mode t |
| 163 |
switch-force-region-check-p t) |
| 164 |
(if (not (or no-check-p old-switch-mode-value)) |
| 165 |
(switch-check-if-in-region))) |
| 166 |
(setq switch-mode nil)) |
| 167 |
@} |
| 168 |
|
| 169 |
Three variables, with local values in each buffer, are required to |
| 170 |
hold the values of the region opening and closing strings and a |
| 171 |
combined pattern. |
| 172 |
|
| 173 |
@$@<switch-mode Variable Definitions 1@>+=@{ |
| 174 |
(defvar switch-open-region-string "@@{" |
| 175 |
"String defining the beginning of a code region.") |
| 176 |
(make-variable-buffer-local 'switch-open-region-string) |
| 177 |
|
| 178 |
(defvar switch-close-region-string "@@}" |
| 179 |
"String defining the end of a code region.") |
| 180 |
(make-variable-buffer-local 'switch-close-region-string) |
| 181 |
|
| 182 |
(defvar switch-boundary-region-pattern "@@\\({\\|}\\)" |
| 183 |
"Regular expression defining the beginning or end of a code |
| 184 |
region.") |
| 185 |
(make-variable-buffer-local 'switch-boundary-region-pattern) |
| 186 |
@} |
| 187 |
|
| 188 |
@$@<switch-mode Function Definitions 2@>+=@{@- |
| 189 |
(if region-open-string |
| 190 |
(setq switch-open-region-string |
| 191 |
region-open-string)) |
| 192 |
(if region-close-string |
| 193 |
(setq switch-close-region-string |
| 194 |
region-close-string)) |
| 195 |
(if region-boundary-pattern |
| 196 |
(setq switch-boundary-region-pattern |
| 197 |
region-boundary-pattern)) |
| 198 |
(force-mode-line-update t))) |
| 199 |
@} |
| 200 |
|
| 201 |
\section{Invocation of the FunnelWeb mode} |
| 202 |
To cause FunnelWeb to be invoked for {\tt .fw} and {\tt .fwi} files, |
| 203 |
the following lines should be added to the user's ``{\tt .emacs}'' |
| 204 |
file. |
| 205 |
|
| 206 |
@$@<Emacs initialisation file additions@>@Z==@{ |
| 207 |
(autoload 'fw-mode "fw-mode") |
| 208 |
(setq |
| 209 |
auto-mode-alist |
| 210 |
(reverse |
| 211 |
(append |
| 212 |
(reverse auto-mode-alist) |
| 213 |
(list |
| 214 |
(append |
| 215 |
(list "\\.fw$") |
| 216 |
'fw-mode |
| 217 |
) |
| 218 |
(append |
| 219 |
(list "\\.fwi$") |
| 220 |
'fw-mode |
| 221 |
) |
| 222 |
) |
| 223 |
) |
| 224 |
) |
| 225 |
) |
| 226 |
@} |
| 227 |
|
| 228 |
\chapter{Function and Variable Definitions for the FunnelWeb Mode} |
| 229 |
An alternative command for saving buffers converts all tabs to spaces |
| 230 |
before saving, since FunnelWeb does not accept tab characters in .fw |
| 231 |
files by default. Because mode-switching is expected to be used when |
| 232 |
editing FunnelWeb files, a local variable in each buffer is set to |
| 233 |
define whether a buffer is a FunnelWeb buffer or not. A global |
| 234 |
variable is also provided to switch off the replacement of tabs by |
| 235 |
spaces. The string used to replace tabs with spaces can also be |
| 236 |
changed from the default of 8 spaces. Another global variable also |
| 237 |
controls the removal of trailing spaces, since these are also not |
| 238 |
accepted by FunnelWeb by default. |
| 239 |
|
| 240 |
@$@<fw-mode Variable Definitions2@>+=@{ |
| 241 |
(defvar fw-untabify-before-saving-p t |
| 242 |
"*Whether to convert tabs to spaces before saving FunnelWeb |
| 243 |
buffers.") |
| 244 |
|
| 245 |
(defvar fw-tab-string "@^D(009)" |
| 246 |
"Tab character as string.") |
| 247 |
|
| 248 |
(defvar fw-tab-space-replacement " " |
| 249 |
"*String of spaces for replacing tabs.") |
| 250 |
(make-variable-buffer-local 'fw-tab-space-replacement) |
| 251 |
|
| 252 |
(defvar fw-remove-trailing-spaces-before-saving-p t |
| 253 |
"*Whether to remove trailing spaces on lines before saving |
| 254 |
FunnelWeb buffers.") |
| 255 |
|
| 256 |
(defvar fw-space (string-to-char " ") |
| 257 |
"Space character, used in removing trailing spaces.") |
| 258 |
|
| 259 |
(defvar fw-trailing-space-pattern " +$" |
| 260 |
"*Pattern used to identify trailing spaces on lines.") |
| 261 |
|
| 262 |
(defvar fw-null-string "" |
| 263 |
"Empty string used for replacing trailing spaces.") |
| 264 |
@} |
| 265 |
|
| 266 |
@$@<fw-mode Function Definitions@>+=@{ |
| 267 |
(defun fw-untabify-save-buffer () |
| 268 |
"Save the buffer, optionally removing tabs and trailing spaces |
| 269 |
first if the buffer is a FunnelWeb buffer." |
| 270 |
(interactive) |
| 271 |
(if fw-buffer-p |
| 272 |
(progn |
| 273 |
(if fw-untabify-before-saving-p |
| 274 |
(progn |
| 275 |
(message "Removing tabs ...") |
| 276 |
(save-excursion |
| 277 |
(goto-char (point-min)) |
| 278 |
(while (search-forward fw-tab-string nil t) |
| 279 |
(replace-match fw-tab-space-replacement nil t))))) |
| 280 |
(if fw-remove-trailing-spaces-before-saving-p |
| 281 |
(progn |
| 282 |
(message "Removing trailing spaces ...") |
| 283 |
(save-excursion |
| 284 |
(goto-char (point-min)) |
| 285 |
(while (< (point) (point-max)) |
| 286 |
(end-of-line) |
| 287 |
(while (eq (preceding-char) fw-space) |
| 288 |
(backward-delete-char 1)) |
| 289 |
(forward-line 1))))))) |
| 290 |
(save-buffer)) |
| 291 |
@} |
| 292 |
|
| 293 |
Other variable definitions follows. Those for which the decription |
| 294 |
text begins with a \verb$*$ can be changed on the command line by the |
| 295 |
user, and all can be set in the users {\tt .emacs} file. |
| 296 |
|
| 297 |
@$@<fw-mode Variable Definitions1@>+=@{ |
| 298 |
(defvar fw-TeX-mode 'tex-mode |
| 299 |
"*The default TeX-based foundation for FunnelWeb mode.") |
| 300 |
@} |
| 301 |
|
| 302 |
@$@<fw-mode Variable Definitions2@>+=@{ |
| 303 |
(defvar fw-switch-minor-mode-init 1 |
| 304 |
"*Whether to activate the Switch minor mode automatically |
| 305 |
on entering FunnelWeb mode. A positive integer for yes.") |
| 306 |
|
| 307 |
(defvar fw-buffer-p nil |
| 308 |
"Whether a buffer is a FunnelWeb buffer or not.") |
| 309 |
(make-variable-buffer-local 'fw-buffer-p) |
| 310 |
(switch-add-to-preservation-list 'fw-buffer-p) |
| 311 |
|
| 312 |
(defvar fw-quote-style t |
| 313 |
"*Whether to map \" to a single character or use (La)TeX |
| 314 |
mapping to `` or '' as appropriate. Can be set to nil to |
| 315 |
remove the (La)TeX mapping, or anything else to enable it.") |
| 316 |
|
| 317 |
(defvar fw-auto-fill-mode 1 |
| 318 |
"*Whether autofill-mode is automatically invoked for |
| 319 |
FunnelWeb files. Set to 0 for no, to a positive number |
| 320 |
for yes.") |
| 321 |
|
| 322 |
(defvar fw-command "fw" |
| 323 |
"*The command to run FunnelWeb on a file. |
| 324 |
Any pre-options (fw-command-pre-options) will be appended |
| 325 |
to this string, separated by a space, followed by the |
| 326 |
filename, also separated by a space, and finally any |
| 327 |
post-options (fw-command-post-options), again separated |
| 328 |
by a space.") |
| 329 |
|
| 330 |
(defvar fw-command-pre-options nil |
| 331 |
"*Options which go before the filename when |
| 332 |
calling FunnelWeb.") |
| 333 |
(make-variable-buffer-local 'fw-command-pre-options) |
| 334 |
|
| 335 |
(defvar fw-command-post-options "+t +D" |
| 336 |
"*Options which go after the filename when |
| 337 |
calling FunnelWeb.") |
| 338 |
(make-variable-buffer-local 'fw-command-post-options) |
| 339 |
|
| 340 |
(defvar fw-shell-cd-command "cd" |
| 341 |
"*Command to give to shell running FunnelWeb to |
| 342 |
change directory. The value of fw-directory will be |
| 343 |
appended to this, separated by a space.") |
| 344 |
|
| 345 |
(defvar fw-mode-syntax-table nil |
| 346 |
"Syntax table used while in FunnelWeb mode.") |
| 347 |
|
| 348 |
(defvar fw-mode-map nil |
| 349 |
"Keymap for FunnelWeb mode.") |
| 350 |
|
| 351 |
(defvar fw-close-definition-block-additive t |
| 352 |
"*Whether definitions should be closed using \"+=\" (t) or |
| 353 |
\"==\" (nil).") |
| 354 |
(make-variable-buffer-local |
| 355 |
'fw-close-definition-block-additive) |
| 356 |
|
| 357 |
(defvar fw-close-definition-block-newline-suppress t |
| 358 |
"*Whether definitions should be closed using \"@@-\".") |
| 359 |
(make-variable-buffer-local |
| 360 |
'fw-close-definition-block-newline-suppress) |
| 361 |
|
| 362 |
(defvar fw-close-definition-block-blank-line t |
| 363 |
"Whether definitions should be closed leaving |
| 364 |
a blank line.") |
| 365 |
(make-variable-buffer-local |
| 366 |
'fw-close-definition-block-blank-line) |
| 367 |
@} |
| 368 |
|
| 369 |
\chapter{Key Definitions for the FunnelWeb Mode} |
| 370 |
These are the special key definitions for FunnelWeb commands. |
| 371 |
|
| 372 |
@$@<fw-mode Key Definitions@>==@{ |
| 373 |
@!(define-key fw-mode-map "\C-c\C-f" 'fw-close-definition-block) |
| 374 |
(global-set-key "\C-x\C-s" 'fw-untabify-save-buffer) |
| 375 |
@} |
| 376 |
|
| 377 |
\chapter{Function and Variable Definitions for the Switch Minor Mode} |
| 378 |
The Switch minor mode works by adding a function, run after each emacs |
| 379 |
command, which checks the position of point. If point is outside of |
| 380 |
the current region, each code or text, a function is run to determine |
| 381 |
whether point lies in a code or text region. Note that for speed, the |
| 382 |
code does not update the boundaries of the current region after each |
| 383 |
command, and so the checking may be fooled when and if sections are |
| 384 |
deleted from a code or text region. |
| 385 |
|
| 386 |
Variables are needed to hold the bounds of the current region and the |
| 387 |
whether flag marking whether a region check is in progress or not (to |
| 388 |
avoid an infinite recursion). |
| 389 |
|
| 390 |
@$@<switch-mode Variable Definitions 1@>+=@{ |
| 391 |
(defvar switch-region-min nil |
| 392 |
"Start of current macro or text region, or nil.") |
| 393 |
(make-variable-buffer-local 'switch-region-min) |
| 394 |
|
| 395 |
(defvar switch-region-max nil |
| 396 |
"End of current macro or text region, or nil.") |
| 397 |
(make-variable-buffer-local 'switch-region-max) |
| 398 |
|
| 399 |
(defvar switch-not-in-region-check-routine-p t |
| 400 |
"Whether a region check is not in progress.") |
| 401 |
(make-variable-buffer-local |
| 402 |
'switch-not-in-region-check-routine-p) |
| 403 |
@} |
| 404 |
|
| 405 |
@$@<switch-mode Function Definitions 2@>+=@{ |
| 406 |
(defun switch-post-command-hook-function () |
| 407 |
"Function to check if the user is in a code definition |
| 408 |
region or not." |
| 409 |
(if (and (not isearch-mode) |
| 410 |
switch-mode |
| 411 |
switch-not-in-region-check-routine-p |
| 412 |
(or (not switch-region-min) |
| 413 |
(not switch-region-max) |
| 414 |
(let ((the-point (point))) |
| 415 |
(or |
| 416 |
(< the-point switch-region-min) |
| 417 |
(> the-point switch-region-max))))) |
| 418 |
(let ((switch-not-in-region-check-routine-p nil)) |
| 419 |
(switch-check-if-in-region)))) |
| 420 |
@} |
| 421 |
|
| 422 |
The function for checking whether the point is in a code or text |
| 423 |
region or not is as follows. |
| 424 |
|
| 425 |
@$@<switch-mode Function Definitions 2@>+=@{ |
| 426 |
(defun switch-check-if-in-region () |
| 427 |
"Check if point is inside or outside a FunnelWeb macro |
| 428 |
definition. If outside, switch to (Auc/La)TeX mode. |
| 429 |
If inside, either ask the user for a mode, defaulting |
| 430 |
to the last mode used in a macro definition, or directly |
| 431 |
use the last mode chosen (depends on value of variable |
| 432 |
switch-mode-prompt-p)." |
| 433 |
(interactive) |
| 434 |
@} |
| 435 |
|
| 436 |
First, searches are carried out forwards and backwards from point to |
| 437 |
determine what sort of region point is in. |
| 438 |
|
| 439 |
@$@<switch-mode Function Definitions 2@>+=@{@- |
| 440 |
(let ((backward-search-result |
| 441 |
(switch-search-backward-check |
| 442 |
switch-boundary-region-pattern |
| 443 |
switch-open-region-string)) |
| 444 |
(forward-search-result |
| 445 |
(switch-search-forward-check |
| 446 |
switch-boundary-region-pattern |
| 447 |
switch-close-region-string)) |
| 448 |
(backward-search-value) |
| 449 |
(forward-search-value)) |
| 450 |
(setq backward-search-value |
| 451 |
(car backward-search-result)) |
| 452 |
(setq forward-search-value |
| 453 |
(car forward-search-result)) |
| 454 |
(setq switch-region-min |
| 455 |
(car (cdr backward-search-result))) |
| 456 |
(setq switch-region-max |
| 457 |
(car (cdr forward-search-result))) |
| 458 |
(if (not (or (< backward-search-value 0) |
| 459 |
(< forward-search-value 0) |
| 460 |
(and (eq backward-search-value 0) |
| 461 |
(eq forward-search-value 0)))) |
| 462 |
@} |
| 463 |
|
| 464 |
The following code determines what is done if point is inside a code |
| 465 |
region. Variables are required to define the behaviour, and to store |
| 466 |
the current code region mode, as well as to store whether point was |
| 467 |
last in a code or text region. |
| 468 |
|
| 469 |
@$@<switch-mode Variable Definitions 1@>+=@{ |
| 470 |
(defvar switch-force-region-check-p nil |
| 471 |
"Whether a region check should be forced or not, |
| 472 |
regardless of whether it seems to be required or not.") |
| 473 |
(make-variable-buffer-local 'switch-force-region-check-p) |
| 474 |
|
| 475 |
(defvar switch-mode-prompt-p t |
| 476 |
"*Whether the user should be prompted for a new |
| 477 |
major mode type each time point enters a code region.") |
| 478 |
(make-variable-buffer-local 'switch-mode-prompt-p) |
| 479 |
|
| 480 |
(defvar switch-current-mode nil |
| 481 |
"The current mode for a code region.") |
| 482 |
(make-variable-buffer-local 'switch-current-mode) |
| 483 |
|
| 484 |
(defvar switch-currently-in-region-p nil |
| 485 |
"Whether point is in a code region or not.") |
| 486 |
(make-variable-buffer-local 'switch-currently-in-region-p) |
| 487 |
|
| 488 |
(defvar switch-highlight-p nil |
| 489 |
"Whether to highlight regions as they are entered. |
| 490 |
(This requires the `hilit19' library to be loaded.)") |
| 491 |
(make-variable-buffer-local 'switch-highlight-p) |
| 492 |
@} |
| 493 |
|
| 494 |
@$@<switch-mode Function Definitions 2@>+=@{@- |
| 495 |
(if (or (not switch-currently-in-region-p) |
| 496 |
switch-force-region-check-p) |
| 497 |
(progn |
| 498 |
(switch-select-mode (not switch-mode-prompt-p)) |
| 499 |
(if switch-current-mode |
| 500 |
(progn |
| 501 |
(message (concat "Changed to " |
| 502 |
(prin1-to-string |
| 503 |
switch-current-mode))) |
| 504 |
(setq switch-currently-in-region-p t))))) |
| 505 |
@} |
| 506 |
|
| 507 |
Now the code for the case where point is {\em not} in a code region, |
| 508 |
but in a text region. |
| 509 |
|
| 510 |
@$@<switch-mode Function Definitions 2@>+=@{@- |
| 511 |
(if switch-currently-in-region-p |
| 512 |
(let ((preservation-list |
| 513 |
(switch-get-preservation-values))) |
| 514 |
(fw-mode) |
| 515 |
(switch-set-preservation-values |
| 516 |
preservation-list) |
| 517 |
(message "Changed to fw-mode") |
| 518 |
(setq switch-currently-in-region-p nil)))) |
| 519 |
(setq switch-force-region-check-p nil))) |
| 520 |
@} |
| 521 |
|
| 522 |
A function is provided to allow the user to toggle whether the mode is |
| 523 |
prompted for each time a code region is entered or not. |
| 524 |
|
| 525 |
@$@<switch-mode Function Definitions 2@>+=@{ |
| 526 |
(defun switch-mode-prompt-toggle () |
| 527 |
"Toggle whether the user is prompted for the major mode |
| 528 |
each time a code region is entered." |
| 529 |
(interactive) |
| 530 |
(setq switch-mode-prompt-p (not switch-mode-prompt-p)) |
| 531 |
(if switch-mode-prompt-p |
| 532 |
(message "Mode prompting switched on.") |
| 533 |
(message "Mode prompting switched off."))) |
| 534 |
@} |
| 535 |
|
| 536 |
It is also sometimes useful to be able to force a major mode to be |
| 537 |
asked for. This is necessary when editing changes cause the program |
| 538 |
to mistake the boundaries of the current region. |
| 539 |
|
| 540 |
@$@<switch-mode Function Definitions 2@>+=@{ |
| 541 |
(defun switch-mode-force-prompt () |
| 542 |
"Cause the user to be prompted for a mode type for |
| 543 |
the current region, if a code region." |
| 544 |
(interactive) |
| 545 |
(let ((switch-force-region-check-p t)) |
| 546 |
(switch-check-if-in-region))) |
| 547 |
@} |
| 548 |
|
| 549 |
The mode selection function allows the user to choose from any command |
| 550 |
which ends with string \"-mode\". It optionally beeps on activation, |
| 551 |
to note to the user that a mode selection is necessary. |
| 552 |
|
| 553 |
@$@<switch-mode Variable Definitions 2@>+=@{ |
| 554 |
(defvar switch-mode-list (make-switch-mode-list) |
| 555 |
"Return a list of strings of all possible major modes |
| 556 |
from which the user can choose.") |
| 557 |
|
| 558 |
(defvar switch-beep-on-mode-selection-p t |
| 559 |
"*Whether to beep when a mode selection is necessary.") |
| 560 |
@} |
| 561 |
|
| 562 |
@$@<switch-mode Function Definitions 2@>+=@{ |
| 563 |
(defun switch-select-mode ( &optional no-prompt-p ) |
| 564 |
"Interactively get the user to select a macro mode, |
| 565 |
giving the last-used macro mode as a default, and allowing |
| 566 |
the user to select from all possible major mode commands |
| 567 |
with name completion. |
| 568 |
Optional parameter NO-PROMPT-P, if non-nil, stops |
| 569 |
prompting from taking place unless the current mode |
| 570 |
(switch-current-mode) is nil." |
| 571 |
(let ((old-switch-mode-value |
| 572 |
(if switch-mode 1 0))) |
| 573 |
(if (not (and no-prompt-p switch-current-mode)) |
| 574 |
(progn |
| 575 |
(if switch-beep-on-mode-selection-p (beep)) |
| 576 |
(let ((new-mode (completing-read |
| 577 |
"Mode: " |
| 578 |
switch-mode-list |
| 579 |
nil |
| 580 |
nil |
| 581 |
(switch-convert-to-default-string |
| 582 |
switch-current-mode) |
| 583 |
nil))) |
| 584 |
(setq switch-current-mode |
| 585 |
(car (read-from-string new-mode)))))) |
| 586 |
(let ((preservation-list |
| 587 |
(switch-get-preservation-values)) |
| 588 |
(region-min switch-region-min) |
| 589 |
(region-max switch-region-max)) |
| 590 |
(message "Changing to %s" |
| 591 |
(prin1-to-string switch-current-mode)) |
| 592 |
(if switch-current-mode (funcall switch-current-mode)) |
| 593 |
(if (and switch-highlight-p region-min region-max) |
| 594 |
(progn |
| 595 |
(hilit-unhighlight-region region-min region-max) |
| 596 |
(hilit-highlight-region region-min region-max))) |
| 597 |
(switch-set-preservation-values preservation-list)) |
| 598 |
(switch-mode old-switch-mode-value t) |
| 599 |
(force-mode-line-update))) |
| 600 |
@} |
| 601 |
|
| 602 |
A short function to convert an object to a string if non-nil. |
| 603 |
|
| 604 |
@$@<switch-mode Function Definitions 1@>+=@{ |
| 605 |
(defun switch-convert-to-default-string ( object ) |
| 606 |
"Convert an object into a default string for |
| 607 |
name completion." |
| 608 |
(if object (prin1-to-string object))) |
| 609 |
@} |
| 610 |
|
| 611 |
The following function returns a list of possible modes, in the form |
| 612 |
of an Emacs ``alist''. A second function allows the user to update |
| 613 |
the list. |
| 614 |
|
| 615 |
@$@<switch-mode Function Definitions 1@>+=@{ |
| 616 |
(defun make-switch-mode-list () |
| 617 |
"Return a list of all commands ending in \"-mode\", |
| 618 |
in the form of an alist." |
| 619 |
(let ((mode-list (apropos-internal "-mode$"))) |
| 620 |
(mapcar 'switch-convert-to-alist-string mode-list))) |
| 621 |
@} |
| 622 |
|
| 623 |
@$@<switch-mode Function Definitions 2@>+=@{ |
| 624 |
(defun update-switch-mode-list () |
| 625 |
"Update the mode list used for the switch minor-mode." |
| 626 |
(interactive) |
| 627 |
(setq switch-mode-list (make-switch-mode-list))) |
| 628 |
@} |
| 629 |
|
| 630 |
This simple function converts objects to a form suitable for an Elisp |
| 631 |
``alist''. |
| 632 |
|
| 633 |
@$@<switch-mode Function Definitions 1@>+=@{ |
| 634 |
(defun switch-convert-to-alist-string ( object ) |
| 635 |
"Convert an object to a string in its own list, |
| 636 |
the format appropriate for an alist." |
| 637 |
(list (prin1-to-string object))) |
| 638 |
@} |
| 639 |
|
| 640 |
Two functions are used to search for code region boundaries, one for |
| 641 |
searching forwards from point, the other for searching backwards. |
| 642 |
Each searches for the given regexp, and if found, compares it to the |
| 643 |
given string. A list is returned containing an integer value |
| 644 |
describing the search result and the position of the string which was |
| 645 |
matched. |
| 646 |
|
| 647 |
@$@<switch-mode Function Definitions 1@>+=@{ |
| 648 |
(defun switch-search-forward-check ( regexp string ) |
| 649 |
"Search forward for the given regexp, and if the matching |
| 650 |
pattern is found and it matches the given string, return `1' |
| 651 |
and the beginning of the pattern, else return `-1' and the |
| 652 |
beginning of the pattern. If the pattern is not found when |
| 653 |
the end of the buffer is reached, return `0' and the end of |
| 654 |
the buffer." |
| 655 |
(save-excursion |
| 656 |
(if (search-forward-regexp regexp (point-max) t) |
| 657 |
(if (string-equal |
| 658 |
string |
| 659 |
(buffer-substring |
| 660 |
(match-beginning 0) |
| 661 |
(match-end 0))) |
| 662 |
(list 1 (match-beginning 0)) |
| 663 |
(list -1 (match-beginning 0))) |
| 664 |
(list 0 (point-max))))) |
| 665 |
|
| 666 |
(defun switch-search-backward-check ( regexp string ) |
| 667 |
"Search backward for the given regexp, and if the matching |
| 668 |
pattern is found and it matches the given string, return `1' |
| 669 |
and the end of the pattern, else return `-1' and the end of |
| 670 |
the pattern. If the pattern is not found when the beginning |
| 671 |
of the buffer is reached, return `0' and the beginning of the |
| 672 |
buffer." |
| 673 |
(save-excursion |
| 674 |
(if (search-backward-regexp regexp (point-min) t) |
| 675 |
(if (string-equal |
| 676 |
string |
| 677 |
(buffer-substring |
| 678 |
(match-beginning 0) |
| 679 |
(match-end 0))) |
| 680 |
(list 1 (match-end 0)) |
| 681 |
(list -1 (match-end 0))) |
| 682 |
(list 0 (point-min))))) |
| 683 |
@} |
| 684 |
|
| 685 |
It is useful to allow functions to maintain a list of variables which |
| 686 |
should be preserved by switch-mode operations. This list can be |
| 687 |
changed by the user. |
| 688 |
|
| 689 |
@$@<switch-mode Variable Definitions 1@>+=@{ |
| 690 |
(defvar switch-preservation-list |
| 691 |
(list 'switch-current-mode 'switch-mode-prompt-p) |
| 692 |
"List of names of variables that need to be specially |
| 693 |
preserved by switch-mode operations.") |
| 694 |
@} |
| 695 |
|
| 696 |
@$@<switch-mode Function Definitions 2@>+=@{ |
| 697 |
(defun switch-add-to-preservation-list ( var ) |
| 698 |
"Add a variable to the switch-mode preservation list." |
| 699 |
(if (not (memq var switch-preservation-list)) |
| 700 |
(setq switch-preservation-list |
| 701 |
(append switch-preservation-list (list var))))) |
| 702 |
|
| 703 |
(defun switch-get-preservation-values () |
| 704 |
"Returns a list of current values which can be used by |
| 705 |
switch-set-preservation-values to restore the values of |
| 706 |
the variables in the preservation list |
| 707 |
(switch-preservation-list)." |
| 708 |
(if switch-preservation-list |
| 709 |
(mapcar |
| 710 |
'switch-preserve-form |
| 711 |
switch-preservation-list))) |
| 712 |
|
| 713 |
(defun switch-preserve-form ( var ) |
| 714 |
"Return a list of the variable name and its value." |
| 715 |
(list var (eval var))) |
| 716 |
|
| 717 |
(defun switch-set-preservation-values ( vallist ) |
| 718 |
"Reset the variables in the preservation value list |
| 719 |
using the value list previously created by |
| 720 |
switch-get-preservation-values." |
| 721 |
(mapcar 'switch-apply-set vallist)) |
| 722 |
|
| 723 |
(defun switch-apply-set ( varpair ) |
| 724 |
"Given a list of the form (NAME . VALUE), assign the value |
| 725 |
to the name." |
| 726 |
(apply 'set varpair)) |
| 727 |
@} |
| 728 |
|
| 729 |
\chapter{Key Definitions for the Switch Minor Mode} |
| 730 |
These are the special key definitions for Switch minor-mode commands. |
| 731 |
|
| 732 |
@$@<switch-mode Key Definitions@>==@{ |
| 733 |
(defvar switch-prefix nil |
| 734 |
"Switch minor-mode region check \\C-x\\C-a keymap.") |
| 735 |
|
| 736 |
(define-prefix-command 'switch-prefix) |
| 737 |
|
| 738 |
(global-set-key "\C-x\C-a" 'switch-prefix) |
| 739 |
|
| 740 |
(define-key switch-prefix "c" 'switch-check-if-in-region) |
| 741 |
|
| 742 |
(define-key switch-prefix "t" 'switch-mode) |
| 743 |
|
| 744 |
(define-key switch-prefix "p" 'switch-mode-prompt-toggle) |
| 745 |
|
| 746 |
(define-key switch-prefix "s" 'switch-mode-force-prompt) |
| 747 |
@} |
| 748 |
|
| 749 |
\chapter{Construction of the elisp files} |
| 750 |
The file ``{\tt switch-mode.el}'' requires the standard Emacs |
| 751 |
distribution provides the new minor mode function `{\tt switch-mode}'. |
| 752 |
|
| 753 |
The variable and function definitions are each divided up into two |
| 754 |
lots, in the order |
| 755 |
\begin{itemize} |
| 756 |
\item variables which don't rely on functions for their default |
| 757 |
values |
| 758 |
\item functions which don't rely on variables in their definitions |
| 759 |
\item variables which rely on functions for their default |
| 760 |
values |
| 761 |
\item functions which rely on variables in their definitions. |
| 762 |
\end{itemize} |
| 763 |
|
| 764 |
@$@<switch-mode Constructed Code@>+=@{@- |
| 765 |
(provide 'switch-mode) |
| 766 |
|
| 767 |
@<switch-mode Variable Definitions 1@> |
| 768 |
|
| 769 |
@<switch-mode Function Definitions 1@> |
| 770 |
|
| 771 |
@<switch-mode Variable Definitions 2@> |
| 772 |
|
| 773 |
@<switch-mode Function Definitions 2@> |
| 774 |
|
| 775 |
@<switch-mode Key Definitions@> |
| 776 |
@} |
| 777 |
|
| 778 |
The following code installs the minor mode and runs any user-provided |
| 779 |
hook function. |
| 780 |
|
| 781 |
@$@<switch-mode Constructed Code@>+=@{@- |
| 782 |
(setq minor-mode-alist |
| 783 |
(reverse |
| 784 |
(append (reverse minor-mode-alist) |
| 785 |
(list (list |
| 786 |
'switch-mode |
| 787 |
" Switch"))))) |
| 788 |
(force-mode-line-update t) |
| 789 |
|
| 790 |
(add-hook 'post-command-hook |
| 791 |
'switch-post-command-hook-function) |
| 792 |
|
| 793 |
(run-hooks 'switch-mode-hook)@} |
| 794 |
|
| 795 |
@$@<Standard Header@>@M==@{@- |
| 796 |
; A.B.Coates |
| 797 |
; Department of Physics |
| 798 |
; The University of Queensland QLD 4072 |
| 799 |
; Australia |
| 800 |
; Standard GNU copyleft provisions apply to this file.@} |
| 801 |
|
| 802 |
@O@<switch-mode.el@>==@{ |
| 803 |
@<Standard Header@> |
| 804 |
|
| 805 |
@<switch-mode Constructed Code@> |
| 806 |
@} |
| 807 |
|
| 808 |
The file ``{\tt fw-mode.el}'' requires the switch minor mode file |
| 809 |
``{\tt switch-mode.el}'', and provides the new mode function |
| 810 |
`{\tt fw-mode}'. Calling the selected \TeX-mode is a way of making |
| 811 |
sure that the required definitions are loaded, since the |
| 812 |
mode-selection function may be defined as an {\em autoload} function. |
| 813 |
|
| 814 |
@$@<fw-mode Constructed Code@>+=@{@- |
| 815 |
(require 'switch-mode) |
| 816 |
|
| 817 |
(provide 'fw-mode) |
| 818 |
|
| 819 |
@<fw-mode Variable Definitions1@> |
| 820 |
|
| 821 |
(funcall fw-TeX-mode) |
| 822 |
|
| 823 |
@<fw-mode Variable Definitions2@> |
| 824 |
@} |
| 825 |
|
| 826 |
\noindent In case the user forgets the precise name, some synonyms for |
| 827 |
`{\tt fw-mode}' are provided. |
| 828 |
|
| 829 |
@$@<fw-mode Constructed Code@>+=@{@- |
| 830 |
(fset 'FW-mode 'fw-mode) |
| 831 |
(fset 'funnelweb-mode 'fw-mode) |
| 832 |
(fset 'FunnelWeb-mode 'fw-mode) |
| 833 |
|
| 834 |
@<fw-mode Function Definitions@>@} |
| 835 |
|
| 836 |
\noindent If the keymap has not already been set in the user's |
| 837 |
``{\tt .emacs}'' file, then a TeX-mode keymap is copied and |
| 838 |
used if possible. |
| 839 |
|
| 840 |
@$@<fw-mode Constructed Code@>+=@{@- |
| 841 |
(if fw-mode-map |
| 842 |
(progn |
| 843 |
(defvar tex-mode-map nil) |
| 844 |
(if tex-mode-map |
| 845 |
(setq fw-mode-map tex-mode-map) |
| 846 |
(progn |
| 847 |
(defvar latex-mode-map nil) |
| 848 |
(if latex-mode-map |
| 849 |
(setq fw-mode-map latex-mode-map) |
| 850 |
(progn |
| 851 |
(defvar LaTeX-mode-map nil) |
| 852 |
(if LaTeX-mode-map |
| 853 |
(setq fw-mode-map LaTeX-mode-map) |
| 854 |
(progn |
| 855 |
(defvar TeX-mode-map nil) |
| 856 |
(if TeX-mode-map |
| 857 |
(setq fw-mode-map TeX-mode-map)))))))))) |
| 858 |
|
| 859 |
@<fw-mode Key Definitions@>@} |
| 860 |
|
| 861 |
@O@<fw-mode.el@>==@{ |
| 862 |
@<Standard Header@> |
| 863 |
|
| 864 |
@<fw-mode Constructed Code@> |
| 865 |
@} |
| 866 |
|
| 867 |
\end{document} |
| 868 |
|
| 869 |
% Local Variables: |
| 870 |
% TeX-master: fwmode |
| 871 |
% End: |
| 872 |
|
| 873 |
@! end of fwmode.fw |
| 874 |
|