9.3 Setup Emacs

  9.3.1 Setup aldor-mode for Emacs
  9.3.2 Setup mmm-mode for Emacs
  9.3.3 Error Search After a Failed Compilation

This section gives examples of initialization code that should be put into ~/.emacs or ~/.xemacs/init.el. Since much of this code is spread over several code chunks it is probably useful to execute

notangle -Remacscode allprose.tex.nw > emacs.el

and then cut and paste the appropriate lines from that file.

91aemacscode 91a
emacs: emacsclient and gnuclient 91b
emacs: general mode selection from file extension 91c
emacs: useful packages 92
emacs: aldor-mode 93
emacs: mmm-mode 94
emacs: error search 102

For the use of the inverse search facility of xdvi and kdvi, it is convenient not start a new Emacs every time you click in the xdvi window. See also Section24.3.9.

91bemacs: emacsclient and gnuclient 91b  (91a)
;-- let ’emacsclient’ connect to a running emacs.
(server-start)

;-- let ’gnuclient’ connect to a running xemacs
;(gnuserv-start)
;(setq gnuserv-frame (selected-frame))  ;; Don’t open new frame.

Through the following code file patterns are associated with modes.

91cemacs: general mode selection from file extension 91c  (91a)
(setq auto-mode-alist (append ’(
  ("\\.as$" . aldor-mode)
  ("Makefile.*\\.\\(allprose\\|def\\|inc\\)$" . makefile-mode)
  ("\\.\\(nw\\|pamphlet\\)$" . latex-mode)
  ("\\.\\(mpl\\|maple\\)$" . maple-mode)
  ("\\.\\(mma\\|mathematica\\)$" . mathematica-mode)
) auto-mode-alist))

Defines:
auto-mode-alist, used in chunk 95a.

It is quite useful to load AUCTEX and syntax highlighting.

92emacs: useful packages 92  (91a)
;-- syntax highlighting on
(require ’font-lock)
(setq font-lock-auto-fontify t)

;-- start AUCTEX (I use version 11.55.)
(require ’tex-site)

;-- Parentheses matching
(require ’paren)

9.3.1 Setup aldor-mode for Emacs

My aldor-mode supports syntax highlighting and indenting Aldor code, but consult its documentation for more details.

aldor-mode is itself a literate program, so tangle and weave it.

notangle aldor.el.nw > aldor.el  
noweave aldor.el.nw > aldor.tex

Suppose you put the file aldor.el into the directory ˜/elisp. Then you should put the following code into your ~/.emacs or ~/.xemacs/init.el and restart Emacs. See also the definition of auto-mode-alist in Section 9.3.

93emacs: aldor-mode 93  (91a)
(setq load-path (append ’("~/elisp") load-path)) ;-- path to aldor.el
(setq shell-prompt-pattern "^[^>]*[>]+ *")
(autoload ’aldor-mode "aldor" "Aldor Mode" t)

The aldor-mode works fine with mmm-mode.

9.3.2 Setup mmm-mode for Emacs

The multiple major mode (mmm-mode) is useful for editing code and documentation of .nw files in Emacs.

Setup can be done as follows.

  1. Install mmm-mode into the ˜/elisp/mmm. Go to the directory where the extracted mmm archive is. Then type the following.
    EMACS=/usr/bin/emacs ./configure --prefix ~/elisp/mmm  
    make  
    make install

    Be careful to compile for the flavour (Emacs or XEmacs) you use, see below.

  2. Add the following lines to your ~/.emacs file. Or, better, select the appropriate lines.

    First, the setup for mmm-mode in general and its association to certain file types.

    94emacs: mmm-mode 94  (91a)  95a
    (setq load-path (append ’("~/elisp/mmm") load-path)) ;-- path to mmm-mode
    (require ’mmm-mode)
    (setq mmm-global-mode ’maybe)
    (mmm-add-mode-ext-class ’latex-mode "\\.nw$" ’noweb)
    (mmm-add-mode-ext-class ’latex-mode "\\.pamphlet$" ’noweb)

    The following code is partly taken from Emacs (subr.el assoc-default), because the function assoc-default is not available in XEmacs.

    It takes a string (key) and tries to string-match the car (first of a list) of an element of auto-mode-alist against key. If found, it returns the cdr (rest of the list) of the matching pair. If not found then fundamental-mode is returned.

    Note that an element of auto-mode-alist can either be a dotted pair or an atom. In the latter case, this atom is returned if it matches key. (Maybe that makes not much sense here, but we did not want to deviate too much from assoc-default.

    95aemacs: mmm-mode 94+   (91a)  94  95b
    (defun get-auto-mode (key)
      (let (found (tail auto-mode-alist) value)
        (while (and tail (not found))
          (let ((elt (car tail)))
            (when (string-match (if (consp elt) (car elt) elt) key)
              (setq found t value (if (consp elt) (cdr elt) ’fundamental-mode))))
          (setq tail (cdr tail)))
        value))

    Defines:
    get-auto-mode, used in chunk 95b.

    Uses auto-mode-alist 91c.

    We need get-auto-mode to set the mmm-noweb-code-mode depending on the file type. Whenever mmm-mode is switched on, the hook below tests whether the filename ends in .pamphlet or .nw. This extension is removed and the filename is matched against the auto-mode-alist. From the latter we get via the function get-auto-mode the corresponding mode that will then be assigned to mmm-noweb-code-mode.

    95bemacs: mmm-mode 94+   (91a)  95a
    (add-hook ’mmm-major-mode-hook
      ’(lambda () (setq mmm-noweb-code-mode
          (get-auto-mode (substring buffer-file-name 0
             (string-match "\\.\\(pamphlet\\|nw\\)$" buffer-file-name))))))

    Uses code 432, get-auto-mode 95a, and noweb 200.
  3. This step is actually unnecessary if get-auto-mode is used. It is only left in order to document how mmm-noweb-code-mode would have to be set if the function get-auto-mode would not be hooked into mmm-major-mode-hook.

    You could put

    % -*- mode: latex; mmm-noweb-code-mode: makefile-mode; -*-

    at the beginning of your file.

    Or, at the end of a .nw file say something like

    %EMACS Local Variables:  
    %EMACS mmm-noweb-code-mode: makefile-mode  
    %EMACS End:

    where, of course, makefile-mode has to be replaced by the mode that is appropriate for the code inside the code chunks, e. g., aldor-mode.

    Of course, it is undesirable to put extra Emacs specific code into a source file, since one cannot expect that everyone is using Emacs.

Unfortunately, for XEmacs the setup is not identical. First of all, you will have to say

EMACS=/usr/bin/xemacs ./configure --prefix ~/elisp/mmm

and the above code goes into the file ~/.xemacs/init.el.

I have not investigated the reason for the following. Additionally, I had to add the preview-latex package (version 0.7.8) into the directory ˜/.xemacs/xemacs-packages in order to make mmm-mode work.

9.3.3 Error Search After a Failed Compilation

There is not much help in case the LATEX compilation of the dvi file fails. There are, however two files that can be investigated in order to find the reason for the error. The file myalps.trace is generated via

latex projectname.tex >> projectname.trace

The file myalps.log is the by default generated by LATEX itself.

One suggestion is to look into these files and search backwards for the extension .nw.tex in order to figure out in which file the error occurred. Note that you should edit the corresponding .nw file, since the .nw.tex file is generated on the fly.

Here we describe the case if the compilation of the library is started from within Emacs via the command

M-x compile RET cd $RootToProject; make RET

and fails. Emacs provides the command next-error. Calling it via

M-x next-error RET

lets Emacs load the corresponding file and jump directly to the error position.

You can bind the above command to a key. For example, put the following code into the ~/.emacs or the ~/.xemacs/init.el file.

102emacs: error search 102  (91a)
(global-set-key [f12] ’next-error)
(global-set-key [(shift f12)] ’previous-error)