home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / help-lucid-emacs / text0417.txt < prev    next >
Encoding:
Text File  |  1993-07-14  |  2.8 KB  |  80 lines

  1.  
  2. Help!
  3.  
  4. Okay.  I give up!  I am trying to set up a submenu which lists the files
  5. in a given directory which can be inserted at point when the file name is
  6. selected from the menu.  The following code kind-of works, but the
  7. templates menu is updated only _after_ I leave and reenter the buffer.
  8. I have tried 'set-menubar-dirty-flag; that doesn't help.
  9.  
  10. PS: I am definitely not an elisp hacker; constructive comments on how to
  11. improve the following code are welcome!
  12.  
  13. PPS: *Please* email replies to chan@jpmorgan.com as well as posting
  14.      because our netnews server is currently hosed.  Thanks!
  15.  
  16. --------------------------------------------------
  17. ;; template-files-menu.el - Include files menu.
  18. ;; 
  19. ;; Mostly snarfed from menubar.el
  20. ;; Last modified: 5/28/1993 M A Chan.
  21.  
  22. (defvar template-files-dir (concat (getenv "TEMPLATES") "/C++/")
  23.   "*Directory in which C++ template files are located.
  24. Must end with a slash!")
  25.  
  26. (defvar template-files-menu
  27.   '(["Reread template dir" build-template-menu t]))
  28.  
  29. (defvar template-menu-function 'insert-file
  30.   "*Function to insert TEMPLATE file into current buffer.
  31. 'insert-file is a good choice.")
  32.  
  33. (defvar template-menu-path (list "Language" "Templates")
  34.   "*Path list to templates submenu.")
  35.  
  36. (defsubst build-template-menu-internal (files)
  37.   (mapcar
  38.    (function (lambda (file)
  39.            (vector file
  40.                (list template-menu-function
  41.                  (concat template-files-dir file))
  42.                t)))
  43.    files))
  44.  
  45. (defun build-template-menu ()
  46.   "This function changes the contents of the \"Templates\" menu to correspond
  47. to the files in template-files-dir."
  48.   (interactive)
  49.   (let ((template-menu (car (find-menu-item current-menubar template-menu-path)))
  50.     files)
  51.     (if (not template-menu)
  52.     (error "template menu \"%s\" not found" template-menu-path)
  53.       (setq files (directory-files template-files-dir nil nil t t))
  54.       (setq files (build-template-menu-internal files))
  55.       (setq files (nconc (delq nil files)))
  56.       (setq files (nconc (delq nil files)
  57.              '("----" ["Reread template dir" build-template-menu t])))
  58.       ;; Update menu iff changed.
  59.       (if (equal files (cdr template-menu))
  60.       (message "no need to update template menu")
  61.     (setq template-files-menu files)
  62.     (setcdr template-menu files)
  63.     (set-menubar current-menubar)))))
  64.  
  65. (defun set-template-menu-hook ()
  66.   (let ((template-menu (car (find-menu-item current-menubar template-menu-path))))
  67.     (if template-menu
  68.     (setcdr template-menu template-files-menu)
  69.       (error "template menu \"%s\" not found" template-menu-path))))
  70.  
  71. (add-hook 'c++-mode-hook 'set-template-menu-hook t)
  72. --------------------------------------------------
  73.  
  74. -Milo
  75. --
  76. Milo Chan, Vice President                    J.P. Morgan Securities, Inc.
  77. email:   chan@jpmorgan.com         ...or...   chan@fractl.tn.cornell.edu
  78. phone:   212.648.4483              ...or...          212.648.4486 (sec'y)
  79.  
  80.