home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / add-log.el.z / add-log.el
Encoding:
Text File  |  1998-05-21  |  24.1 KB  |  683 lines

  1. ;;; add-log.el --- change log maintenance commands for Emacs
  2.  
  3. ;; Copyright (C) 1985, 86, 88, 93, 94, 1997 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: maint
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  21. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23.  
  24. ;;; Synched up with: Emacs 20.0.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This facility is documented in the Emacs Manual.
  29.  
  30. ;;; Code:
  31.  
  32. ;; XEmacs: the byte-compiler warns on `beginning-of-fortran-subprogram'.
  33. (condition-case nil
  34.     (eval-when-compile
  35.       (require 'fortran))
  36.   (t nil))
  37.  
  38. (defgroup change-log nil
  39.   "Change log maintenance"
  40.   :group 'tools
  41.   :group 'maint
  42.   :prefix "change-log-"
  43.   :prefix "add-log-")
  44.  
  45.  
  46. (defcustom change-log-default-name nil
  47.   "*Name of a change log file for \\[add-change-log-entry]."
  48.   :type '(choice (const :tag "default" nil)
  49.          string)
  50.   :group 'change-log)
  51.  
  52. (defcustom add-log-current-defun-function nil
  53.   "\
  54. *If non-nil, function to guess name of current function from surrounding text.
  55. \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
  56. instead) with no arguments.  It returns a string or nil if it cannot guess."
  57.   :type 'boolean
  58.   :group 'change-log)
  59.  
  60. (defcustom add-log-full-name nil
  61.   "*Full name of user, for inclusion in ChangeLog daily headers.
  62. This defaults to the value returned by the `user-full-name' function."
  63.   :type '(choice (const :tag "Default" nil)
  64.          string)
  65.   :group 'change-log)
  66.  
  67. (defcustom add-log-mailing-address nil
  68.   "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
  69. This defaults to the value of `user-mail-address'."
  70.   :type '(choice (const :tag "Default" nil)
  71.          string)
  72.   :group 'change-log)
  73.  
  74. (defcustom add-log-time-format 'iso8601-time-string
  75.   "*Function that defines the time format.
  76. For example, `iso8601-time-string' (time in international ISO 8601 format)
  77. and `current-time-string' are valid values."
  78.   :type '(radio (const :tag "International ISO 8601 format" iso8601-time-string)
  79.         (const :tag "Old format, as returned by `current-time-string'"
  80.                current-time-string)
  81.         (function :tag "Other"))
  82.   :group 'change-log)
  83.  
  84.  
  85. (defvar change-log-font-lock-keywords
  86.   '(;;
  87.     ;; Date lines, new and old styles.
  88.     ("^\\sw.........[0-9: ]*"
  89.      (0 font-lock-string-face)
  90.      ("\\([^<]+\\)<\\([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\\)>" nil nil
  91.       (1 font-lock-reference-face)
  92.       (2 font-lock-variable-name-face)))
  93.     ;;
  94.     ;; File names.
  95.     ("^\t\\* \\([^ ,:([\n]+\\)"
  96.      (1 font-lock-function-name-face)
  97.      ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 font-lock-function-name-face)))
  98.     ;;
  99.     ;; Function or variable names.
  100.     ("(\\([^ ,:)\n]+\\)"
  101.      (1 font-lock-keyword-face)
  102.      ("\\=, \\([^ ,:\n]+\\)" nil nil (1 font-lock-keyword-face)))
  103.     ;;
  104.     ;; Conditionals.
  105.     ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face))
  106.     ;;
  107.     ;; Acknowledgments.
  108.     ("^\t\\(From\\|Reported by\\)" 1 font-lock-comment-face)
  109.     )
  110.   "Additional expressions to highlight in Change Log mode.")
  111. (put 'change-log-mode 'font-lock-defaults
  112.      '(change-log-font-lock-keywords t))
  113.  
  114. (defvar change-log-mode-map nil
  115.   "Keymap for Change Log major mode.")
  116. (if change-log-mode-map
  117.     nil
  118.   (setq change-log-mode-map (make-sparse-keymap))
  119.   (define-key change-log-mode-map "\C-c\C-c" 'change-log-exit)
  120.   (define-key change-log-mode-map "\C-c\C-k" 'change-log-cancel))
  121.  
  122. (defvar change-log-time-zone-rule nil
  123.   "Time zone used for calculating change log time stamps.
  124. It takes the same format as the TZ argument of `set-time-zone-rule'.
  125. If nil, use local time.")
  126.  
  127. (defun iso8601-time-zone (time)
  128.   (let* ((utc-offset (or (car (current-time-zone time)) 0))
  129.      (sign (if (< utc-offset 0) ?- ?+))
  130.      (sec (abs utc-offset))
  131.      (ss (% sec 60))
  132.      (min (/ sec 60))
  133.      (mm (% min 60))
  134.      (hh (/ min 60)))
  135.     (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
  136.           ((not (zerop mm)) "%c%02d:%02d")
  137.           (t "%c%02d"))
  138.         sign hh mm ss)))
  139.  
  140. (defun iso8601-time-string ()
  141.   (if change-log-time-zone-rule
  142.       (let ((tz (getenv "TZ"))
  143.         (now (current-time)))
  144.     (unwind-protect
  145.         (progn
  146.           (set-time-zone-rule
  147.            change-log-time-zone-rule)
  148.           (concat
  149.            (format-time-string "%Y-%m-%d " now)
  150.            (iso8601-time-zone now)))
  151.       (set-time-zone-rule tz)))
  152.     (format-time-string "%Y-%m-%d")))
  153.  
  154. (defun change-log-name ()
  155.   (or change-log-default-name
  156.       (if (eq system-type 'vax-vms) 
  157.       "$CHANGE_LOG$.TXT"
  158.     "ChangeLog")))
  159.  
  160. ;;;###autoload
  161. (defun prompt-for-change-log-name ()
  162.   "Prompt for a change log name."
  163.   (let* ((default (find-change-log))
  164.      (name (expand-file-name
  165.         (read-file-name "Log file: "
  166.                                 (file-name-directory default)
  167.                                 default nil
  168.                                 (file-name-nondirectory default)))))
  169.     ;; Handle something that is syntactically a directory name.
  170.     ;; Look for ChangeLog or whatever in that directory.
  171.     (if (string= (file-name-nondirectory name) "")
  172.     (expand-file-name (file-name-nondirectory default)
  173.               name)
  174.       ;; Handle specifying a file that is a directory.
  175.       (if (file-directory-p name)
  176.       (expand-file-name (file-name-nondirectory default)
  177.                 (file-name-as-directory name))
  178.     name))))
  179.  
  180. ;;;###autoload
  181. (defun find-change-log (&optional file-name)
  182.   "Find a change log file for \\[add-change-log-entry] and return the name.
  183.  
  184. Optional arg FILE-NAME specifies the file to use.
  185. If FILE-NAME is nil, use the value of `change-log-default-name'.
  186. If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
  187. \(or whatever we use on this operating system).
  188.  
  189. If 'change-log-default-name' contains a leading directory component, then
  190. simply find it in the current directory.  Otherwise, search in the current 
  191. directory and its successive parents for a file so named.
  192.  
  193. Once a file is found, `change-log-default-name' is set locally in the
  194. current buffer to the complete file name."
  195.   ;; If user specified a file name or if this buffer knows which one to use,
  196.   ;; just use that.
  197.   (or file-name
  198.       (setq file-name (and change-log-default-name
  199.                (file-name-directory change-log-default-name)
  200.                change-log-default-name))
  201.       (progn
  202.     ;; Chase links in the source file
  203.     ;; and use the change log in the dir where it points.
  204.     (setq file-name (or (and buffer-file-name
  205.                  (file-name-directory
  206.                   (file-chase-links buffer-file-name)))
  207.                 default-directory))
  208.     (if (file-directory-p file-name)
  209.         (setq file-name (expand-file-name (change-log-name) file-name)))
  210.     ;; Chase links before visiting the file.
  211.     ;; This makes it easier to use a single change log file
  212.     ;; for several related directories.
  213.     (setq file-name (file-chase-links file-name))
  214.     (setq file-name (expand-file-name file-name))
  215.     ;; Move up in the dir hierarchy till we find a change log file.
  216.     (let ((file1 file-name)
  217.           parent-dir)
  218.       (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
  219.               (progn (setq parent-dir
  220.                    (file-name-directory
  221.                     (directory-file-name
  222.                      (file-name-directory file1))))
  223.                  ;; Give up if we are already at the root dir.
  224.                  (not (string= (file-name-directory file1)
  225.                        parent-dir))))
  226.         ;; Move up to the parent dir and try again.
  227.         (setq file1 (expand-file-name 
  228.              (file-name-nondirectory (change-log-name))
  229.              parent-dir)))
  230.       ;; If we found a change log in a parent, use that.
  231.       (if (or (get-file-buffer file1) (file-exists-p file1))
  232.           (setq file-name file1)))))
  233.   ;; Make a local variable in this buffer so we needn't search again.
  234.   (set (make-local-variable 'change-log-default-name) file-name)
  235.   file-name)
  236.  
  237.  
  238. ;;;###autoload
  239. (defun add-change-log-entry (&optional whoami file-name other-window new-entry)
  240.   "Find change log file and add an entry for today.
  241. Optional arg (interactive prefix) non-nil means prompt for user name and site.
  242. Second arg is file name of change log.  If nil, uses `change-log-default-name'.
  243. Third arg OTHER-WINDOW non-nil means visit in other window.
  244. Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
  245. never append to an existing entry.  Today's date is calculated according to
  246. `change-log-time-zone-rule' if non-nil, otherwise in local time."
  247.   (interactive (list current-prefix-arg
  248.              (prompt-for-change-log-name)))
  249.   (or add-log-full-name
  250.       (setq add-log-full-name (user-full-name)))
  251.   (or add-log-mailing-address
  252.       (setq add-log-mailing-address (user-mail-address)))
  253.   (if whoami
  254.       (progn
  255.         (setq add-log-full-name (read-string "Full name: " add-log-full-name))
  256.     ;; Note that some sites have room and phone number fields in
  257.     ;; full name which look silly when inserted.  Rather than do
  258.     ;; anything about that here, let user give prefix argument so that
  259.     ;; s/he can edit the full name field in prompter if s/he wants.
  260.     (setq add-log-mailing-address
  261.           (read-string "Mailing address: " add-log-mailing-address))))
  262.   (let ((defun (funcall (or add-log-current-defun-function
  263.                 'add-log-current-defun)))
  264.     paragraph-end entry)
  265.  
  266.     (setq file-name (expand-file-name (find-change-log file-name)))
  267.  
  268.     ;; Set ENTRY to the file name to use in the new entry.
  269.     (and buffer-file-name
  270.      ;; Never want to add a change log entry for the ChangeLog file itself.
  271.      (not (string= buffer-file-name file-name))
  272.      (setq entry (if (string-match
  273.               (concat "^" (regexp-quote (file-name-directory
  274.                              file-name)))
  275.               buffer-file-name)
  276.              (substring buffer-file-name (match-end 0))
  277.                (file-name-nondirectory buffer-file-name))))
  278.  
  279.     (push-window-configuration)
  280.  
  281.     (if (and other-window (not (equal file-name buffer-file-name)))
  282.     (find-file-other-window file-name)
  283.       (find-file file-name))
  284.     (or (eq major-mode 'change-log-mode)
  285.     (change-log-mode))
  286.     (undo-boundary)
  287.     (goto-char (point-min))
  288.     (let ((new-entry (concat (funcall add-log-time-format)
  289.                  "  " add-log-full-name
  290.                  "  <" add-log-mailing-address ">")))
  291.       (if (looking-at (regexp-quote new-entry))
  292.       (forward-line 1)
  293.     (insert new-entry "\n\n")))
  294.  
  295.     ;; Search only within the first paragraph.
  296.     (if (looking-at "\n*[^\n* \t]")
  297.     (skip-chars-forward "\n")
  298.       (forward-paragraph 1))
  299.     (setq paragraph-end (point))
  300.     (goto-char (point-min))
  301.  
  302.     ;; Now insert the new line for this entry.
  303.     (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
  304.        ;; Put this file name into the existing empty entry.
  305.        (if entry
  306.            (insert entry)))
  307.       ((and (not new-entry)
  308.         (let (case-fold-search)
  309.           (re-search-forward
  310.            (concat (regexp-quote (concat "* " entry))
  311.                ;; Don't accept `foo.bar' when
  312.                ;; looking for `foo':
  313.                "\\(\\s \\|[(),:]\\)")
  314.            paragraph-end t)))
  315.        ;; Add to the existing entry for the same file.
  316.        (re-search-forward "^\\s *$\\|^\\s \\*")
  317.        (goto-char (match-beginning 0))
  318.        ;; Delete excess empty lines; make just 2.
  319.        (while (and (not (eobp)) (looking-at "^\\s *$"))
  320.          (delete-region (point) (save-excursion (forward-line 1) (point))))
  321.        (insert "\n\n")
  322.        (forward-line -2)
  323.        (indent-relative-maybe))
  324.       (t
  325.        ;; Make a new entry.
  326.        (forward-line 1)
  327.        (while (looking-at "\\sW")
  328.          (forward-line 1))
  329.        (while (and (not (eobp)) (looking-at "^\\s *$"))
  330.          (delete-region (point) (save-excursion (forward-line 1) (point))))
  331.        (insert "\n\n\n")
  332.        (forward-line -2)
  333.        (indent-to left-margin)
  334.        (insert "* " (or entry ""))))
  335.     ;; Now insert the function name, if we have one.
  336.     ;; Point is at the entry for this file,
  337.     ;; either at the end of the line or at the first blank line.
  338.     (if defun
  339.     (progn
  340.       ;; Make it easy to get rid of the function name.
  341.       (undo-boundary)
  342.       (insert (if (save-excursion
  343.             (beginning-of-line 1)
  344.             (looking-at "\\s *$")) 
  345.               ""
  346.             " ")
  347.           "(" defun "): "))
  348.       ;; No function name, so put in a colon unless we have just a star.
  349.       (if (not (save-excursion
  350.          (beginning-of-line 1)
  351.          (looking-at "\\s *\\(\\*\\s *\\)?$")))
  352.       (insert ": ")))))
  353.  
  354. ;;;###autoload
  355. (defun add-change-log-entry-other-window (&optional whoami file-name)
  356.   "Find change log file in other window and add an entry for today.
  357. Optional arg (interactive prefix) non-nil means prompt for user name and site.
  358. Second arg is file name of change log.  \
  359. If nil, uses `change-log-default-name'."
  360.   (interactive (if current-prefix-arg
  361.            (list current-prefix-arg
  362.              (prompt-for-change-log-name))))
  363.   (add-change-log-entry whoami file-name t))
  364. ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
  365.  
  366. ;;;###autoload
  367. (defun change-log-mode ()
  368.   "Major mode for editing change logs; like Indented Text Mode.
  369. Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
  370. New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
  371. Each entry behaves as a paragraph, and the entries for one day as a page.
  372. Runs `change-log-mode-hook'."
  373.   (interactive)
  374.   (kill-all-local-variables)
  375.   (indented-text-mode)
  376.   (setq major-mode 'change-log-mode
  377.     mode-name "Change Log"
  378.     left-margin 8
  379.     fill-column 74
  380.     indent-tabs-mode t
  381.     tab-width 8)
  382.   (use-local-map change-log-mode-map)
  383.   (set (make-local-variable 'fill-paragraph-function)
  384.        'change-log-fill-paragraph)
  385.   ;; Let each entry behave as one paragraph:
  386.   ;; We really do want "^" in paragraph-start below: it is only the lines that
  387.   ;; begin at column 0 (despite the left-margin of 8) that we are looking for.
  388.   (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
  389.   (set (make-local-variable 'paragraph-separate) "\\s *$\\|\f\\|^\\<")
  390.   ;; Let all entries for one day behave as one page.
  391.   ;; Match null string on the date-line so that the date-line
  392.   ;; is grouped with what follows.
  393.   (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
  394.   (set (make-local-variable 'version-control) 'never)
  395.   (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
  396.   ;;(set (make-local-variable 'font-lock-defaults)
  397.        ;;'(change-log-font-lock-keywords t))
  398.   (when (boundp 'filladapt-mode)
  399.     ;; Filladapt works badly with ChangeLogs.  Still, we disable it
  400.     ;; before change-log-mode-hook, so the users can override this
  401.     ;; choice.
  402.     (setq filladapt-mode nil))
  403.   (run-hooks 'change-log-mode-hook))
  404.  
  405. (defun change-log-exit ()
  406.   "Save the change-log buffer, and restores the old window configuration.
  407. Buries the buffer."
  408.   (interactive)
  409.   (save-buffer)
  410.   (let ((buf (current-buffer)))
  411.     (pop-window-configuration)
  412.     (bury-buffer buf)))
  413.  
  414. (defun change-log-cancel ()
  415.   "Cancel the changes to change-log buffer.
  416. This kills the buffer without saving, and restores the old window
  417.  configuration."
  418.   (interactive)
  419.   (kill-buffer (current-buffer))
  420.   (pop-window-configuration))
  421.  
  422. ;; It might be nice to have a general feature to replace this.  The idea I
  423. ;; have is a variable giving a regexp matching text which should not be
  424. ;; moved from bol by filling.  change-log-mode would set this to "^\\s *\\s(".
  425. ;; But I don't feel up to implementing that today.
  426. (defun change-log-fill-paragraph (&optional justify)
  427.   "Fill the paragraph, but preserve open parentheses at beginning of lines.
  428. Prefix arg means justify as well."
  429.   (interactive "P")
  430.   (let ((end (progn (forward-paragraph) (point)))
  431.     (beg (progn (backward-paragraph) (point)))
  432.     (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
  433.     (fill-region beg end justify)
  434.     t))
  435.  
  436. (defcustom add-log-current-defun-header-regexp
  437.   "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
  438.   "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
  439.   :type 'regexp
  440.   :group 'change-log)
  441.  
  442. ;;;###autoload
  443. (defvar add-log-lisp-like-modes
  444.     '(emacs-lisp-mode lisp-mode scheme-mode lisp-interaction-mode)
  445.   "*Modes that look like Lisp to `add-log-current-defun'.")
  446.  
  447. ;;;###autoload
  448. (defvar add-log-c-like-modes
  449.     '(c-mode c++-mode c++-c-mode objc-mode java-mode)
  450.   "*Modes that look like C to `add-log-current-defun'.")
  451.  
  452. ;;;###autoload
  453. (defvar add-log-tex-like-modes
  454.     '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode)
  455.   "*Modes that look like TeX to `add-log-current-defun'.")
  456.  
  457. ;;;###autoload
  458. (defun add-log-current-defun ()
  459.   "Return name of function definition point is in, or nil.
  460.  
  461. Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
  462. Texinfo (@node titles), Perl, and Fortran.
  463.  
  464. Other modes are handled by a heuristic that looks in the 10K before
  465. point for uppercase headings starting in the first column or
  466. identifiers followed by `:' or `=', see variable
  467. `add-log-current-defun-header-regexp'.
  468.  
  469. Has a preference of looking backwards."
  470.   (condition-case nil
  471.       (save-excursion
  472.     (let ((location (point)))
  473.       (cond ((memq major-mode add-log-lisp-like-modes)
  474.          ;; If we are now precisely at the beginning of a defun,
  475.          ;; make sure beginning-of-defun finds that one
  476.          ;; rather than the previous one.
  477.          (or (eobp) (forward-char 1))
  478.          (beginning-of-defun)
  479.          ;; Make sure we are really inside the defun found, not after it.
  480.          (if (and (looking-at "\\s(")
  481.               (progn (end-of-defun)
  482.                  (< location (point)))
  483.               (progn (forward-sexp -1)
  484.                  (>= location (point))))
  485.              (progn
  486.                (if (looking-at "\\s(")
  487.                (forward-char 1))
  488.                (forward-sexp 1)
  489.                (skip-chars-forward " '")
  490.                (buffer-substring (point)
  491.                      (progn (forward-sexp 1) (point))))))
  492.         ((and (memq major-mode add-log-c-like-modes)
  493.               (save-excursion
  494.             (beginning-of-line)
  495.             ;; Use eq instead of = here to avoid
  496.             ;; error when at bob and char-after
  497.             ;; returns nil.
  498.             (while (eq (char-after (- (point) 2)) ?\\)
  499.               (forward-line -1))
  500.             (looking-at "[ \t]*#[ \t]*define[ \t]")))
  501.          ;; Handle a C macro definition.
  502.          (beginning-of-line)
  503.          (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
  504.            (forward-line -1))
  505.          (search-forward "define")
  506.          (skip-chars-forward " \t")
  507.          (buffer-substring (point)
  508.                    (progn (forward-sexp 1) (point))))
  509.         ((memq major-mode add-log-c-like-modes)
  510.          (beginning-of-line)
  511.          ;; See if we are in the beginning part of a function,
  512.          ;; before the open brace.  If so, advance forward.
  513.          (while (not (looking-at "{\\|\\(\\s *$\\)"))
  514.            (forward-line 1))
  515.          (or (eobp)
  516.              (forward-char 1))
  517.          (beginning-of-defun)
  518.          (if (progn (end-of-defun)
  519.                 (< location (point)))
  520.              (progn
  521.                (backward-sexp 1)
  522.                (let (beg tem)
  523.  
  524.              (forward-line -1)
  525.              ;; Skip back over typedefs of arglist.
  526.              (while (and (not (bobp))
  527.                      (looking-at "[ \t\n]"))
  528.                (forward-line -1))
  529.              ;; See if this is using the DEFUN macro used in Emacs,
  530.              ;; or the DEFUN macro used by the C library.
  531.              (if (condition-case nil
  532.                  (and (save-excursion
  533.                     (end-of-line)
  534.                     (while (= (preceding-char) ?\\)
  535.                       (end-of-line 2))
  536.                     (backward-sexp 1)
  537.                     (beginning-of-line)
  538.                     (setq tem (point))
  539.                     (looking-at "DEFUN\\b"))
  540.                       (>= location tem))
  541.                    (error nil))
  542.                  (progn
  543.                    (goto-char tem)
  544.                    (down-list 1)
  545.                    (if (= (char-after (point)) ?\")
  546.                    (progn
  547.                      (forward-sexp 1)
  548.                      (skip-chars-forward " ,")))
  549.                    (buffer-substring (point)
  550.                          (progn (forward-sexp 1) (point))))
  551.                            (if (looking-at "^[+-]")
  552.                                (get-method-definition)
  553.                              ;; Ordinary C function syntax.
  554.                              (setq beg (point))
  555.                              (if (and (condition-case nil
  556.                       ;; Protect against "Unbalanced parens" error.
  557.                       (progn
  558.                         (down-list 1) ; into arglist
  559.                         (backward-up-list 1)
  560.                         (skip-chars-backward " \t")
  561.                         t)
  562.                     (error nil))
  563.                       ;; Verify initial pos was after
  564.                       ;; real start of function.
  565.                       (save-excursion
  566.                     (goto-char beg)
  567.                     ;; For this purpose, include the line
  568.                     ;; that has the decl keywords.  This
  569.                     ;; may also include some of the
  570.                     ;; comments before the function.
  571.                     (while (and (not (bobp))
  572.                             (save-excursion
  573.                               (forward-line -1)
  574.                               (looking-at "[^\n\f]")))
  575.                       (forward-line -1))
  576.                     (>= location (point)))
  577.                                           ;; Consistency check: going down and up
  578.                                           ;; shouldn't take us back before BEG.
  579.                                           (> (point) beg))
  580.                  (let (end middle)
  581.                    ;; Don't include any final newline
  582.                    ;; in the name we use.
  583.                    (if (= (preceding-char) ?\n)
  584.                        (forward-char -1))
  585.                    (setq end (point))
  586.                    (backward-sexp 1)
  587.                    ;; Now find the right beginning of the name.
  588.                    ;; Include certain keywords if they
  589.                    ;; precede the name.
  590.                    (setq middle (point))
  591.                    (forward-word -1)
  592.                    ;; Ignore these subparts of a class decl
  593.                    ;; and move back to the class name itself.
  594.                    (while (looking-at "public \\|private ")
  595.                      (skip-chars-backward " \t:")
  596.                      (setq end (point))
  597.                      (backward-sexp 1)
  598.                      (setq middle (point))
  599.                      (forward-word -1))
  600.                    (and (bolp)
  601.                     (looking-at "struct \\|union \\|class ")
  602.                     (setq middle (point)))
  603.                    (buffer-substring middle end)))))))))
  604.         ((memq major-mode add-log-tex-like-modes)
  605.          (if (re-search-backward
  606.               "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
  607.              (progn
  608.                (goto-char (match-beginning 0))
  609.                (buffer-substring (1+ (point));; without initial backslash
  610.                      (progn
  611.                        (end-of-line)
  612.                        (point))))))
  613.         ((eq major-mode 'texinfo-mode)
  614.          (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
  615.              (buffer-substring (match-beginning 1)
  616.                        (match-end 1))))
  617.         ((eq major-mode 'perl-mode)
  618.          (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t)
  619.              (buffer-substring (match-beginning 1)
  620.                        (match-end 1))))
  621.                 ((eq major-mode 'fortran-mode)
  622.                  ;; must be inside function body for this to work
  623.                  (beginning-of-fortran-subprogram)
  624.                  (let ((case-fold-search t)) ; case-insensitive
  625.                    ;; search for fortran subprogram start
  626.                    (if (re-search-forward
  627.              "^[ \t]*\\(program\\|subroutine\\|function\
  628. \\|[ \ta-z0-9*]*[ \t]+function\\)"
  629.              nil t)
  630.                        (progn
  631.                          ;; move to EOL or before first left paren
  632.                          (if (re-search-forward "[(\n]" nil t)
  633.                  (progn (forward-char -1)
  634.                     (skip-chars-backward " \t"))
  635.                (end-of-line))
  636.              ;; Use the name preceding that.
  637.                          (buffer-substring (point)
  638.                                            (progn (forward-sexp -1)
  639.                                                   (point)))))))
  640.         (t
  641.          ;; If all else fails, try heuristics
  642.          (let (case-fold-search)
  643.            (end-of-line)
  644.            (if (re-search-backward add-log-current-defun-header-regexp
  645.                        (- (point) 10000)
  646.                        t)
  647.                (buffer-substring (match-beginning 1)
  648.                      (match-end 1))))))))
  649.     (error nil)))
  650.  
  651. (defvar get-method-definition-md)
  652.  
  653. ;; Subroutine used within get-method-definition.
  654. ;; Add the last match in the buffer to the end of `md',
  655. ;; followed by the string END; move to the end of that match.
  656. (defun get-method-definition-1 (end)
  657.   (setq get-method-definition-md
  658.     (concat get-method-definition-md 
  659.         (buffer-substring (match-beginning 1) (match-end 1))
  660.         end))
  661.   (goto-char (match-end 0)))
  662.  
  663. ;; For objective C, return the method name if we are in a method.
  664. (defun get-method-definition ()
  665.   (let ((get-method-definition-md "["))
  666.     (save-excursion
  667.       (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
  668.       (get-method-definition-1 " ")))
  669.     (save-excursion
  670.       (cond
  671.        ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
  672.     (get-method-definition-1 "")
  673.     (while (not (looking-at "[{;]"))
  674.       (looking-at
  675.        "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
  676.       (get-method-definition-1 ""))
  677.     (concat get-method-definition-md "]"))))))
  678.  
  679.  
  680. (provide 'add-log)
  681.  
  682. ;;; add-log.el ends here
  683.