Re: How do I add define a key for a particular mode to say, insert stuff?

"Stephen J. Turnbull" <[email protected]>
Newsgroups gmane.emacs.xemacs.general
Organization The XEmacs Project
Message-ID <[email protected]>
>>>>> "Hareesh" == Hareesh Nagarajan <[email protected]> writes:

    Hareesh> I am using XEmacs 21.4 (patch 12). How do I define a key
    Hareesh> say F12: such that in LaTeX mode pressing that key will
    Hareesh> result in the insertion of the preamble of a general
    Hareesh> LaTeX document into the current buffer, and if I am in
    Hareesh> the CC mode pressing the same key will result in the GPL
    Hareesh> being inserted.

(defun hn-insert-preamble-by-mode ()        ; use initials to indicate
                                            ; not a standard function
                                            ; long, descriptive name
  ;; #### need to improve this docstring
  "insertion of the preamble into the current buffer by mode

a general preamble for LaTeX document, and if I am in
the CC mode pressing the same key will result in the GPL
being inserted."
  (interactive)                             ; required for commands

  (cond ((eq major-mode 'c-mode)
         ;; the backslash keeps the newline from becoming part of the string
         (insert "\
/* Copyright 2003 Hareesh Nagarajan

   This program is free software.  You may copy, modify, and
   redistribute such copies under the terms of the GNU General Public
   License, version 2 or any later version published by the FSF at
   your option.  The License text is in the file COPYING distributed
   with this program.
*/

"))
        ;; etc, etc for other modes
        ;; the mode symbol should be the name of the command which
        ;; invokes the mode; C-h v major-mode RET will tell you for sure.

        ;; give a useful message if the preamble is undefined
        (t (message "I don't have a preamble for mode %s" major-mode))
   ))

(define-key global-map [(f12)] #'hn-insert-preamble-by-mode)

There are other ways to express the conditional, for example as an
alist mapping mode symbols to preamble strings, then use assoc.  The
cond method is simpler if you are likely to do complex additional
processing which varies by mode.  The assoc method allows you to add
new preambles without editing the program (use defcustom to define the
alist variable for a reasonably convenient interface).

You might also want to set up the preambles as separate files and use
insert-file-contents to insert them.

-- 
Institute of Policy and Planning Sciences     http://turnbull.sk.tsukuba.ac.jp
University of Tsukuba                    Tennodai 1-1-1 Tsukuba 305-8573 JAPAN
               Ask not how you can "do" free software business;
              ask what your business can "do for" free software.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.