]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/cc-cmds.el
(c-defun-name): Make it work for "struct foo bar [] = { ...".
[gnu-emacs] / lisp / progmodes / cc-cmds.el
index 1a2ee3f0ce57670185a7d0d9e850cc9b26145c26..8e7df4c95a69db4ee683abfa6323efdff3c16a5c 100644 (file)
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -27,9 +27,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with this program; see the file COPYING.  If not, write to
-;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -1679,6 +1677,71 @@ the open-parenthesis that starts a defun; see `beginning-of-defun'."
     (c-keep-region-active)
     (= arg 0)))
 
+(defun c-defun-name ()
+  "Return the name of the current defun, or NIL if there isn't one.
+\"Defun\" here means a function, or other top level construct
+with a brace block."
+  (interactive)
+  (c-save-buffer-state
+      (beginning-of-defun-function end-of-defun-function
+       where pos name-end)
+    (save-excursion
+      ;; Move back out of any macro/comment/string we happen to be in.
+      (c-beginning-of-macro)
+      (setq pos (c-literal-limits))
+      (if pos (goto-char (car pos)))
+
+      (setq where (c-where-wrt-brace-construct))
+
+      ;; Move to the beginning of the current defun, if any, if we're not
+      ;; already there.
+      (if (eq where 'outwith-function)
+         nil
+       (unless (eq where 'at-header)
+         (c-backward-to-nth-BOF-{ 1 where)
+         (c-beginning-of-decl-1))
+
+       ;; Pick out the defun name, according to the type of defun.
+       (cond
+         ;; struct, union, enum, or similar:
+        ((and (looking-at c-type-prefix-key)
+              (progn (c-forward-token-2 2) ; over "struct foo "
+                     (or (eq (char-after) ?\{)
+                         (looking-at c-symbol-key)))) ; "struct foo bar ..."
+         (save-match-data (c-forward-token-2))
+         (when (eq (char-after) ?\{)
+           (c-backward-token-2)
+           (looking-at c-symbol-key))
+         (match-string-no-properties 0))
+
+        ((looking-at "DEFUN\\_>")
+         ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory
+         ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK        
+         (down-list 1)
+         (c-forward-syntactic-ws)
+         (when (eq (char-after) ?\")
+           (forward-sexp 1)
+           (c-forward-token-2))        ; over the comma and following WS.
+         (buffer-substring-no-properties
+          (point)
+          (progn
+            (c-forward-token-2)
+            (c-backward-syntactic-ws)
+            (point))))
+               
+        (t
+        ;; Normal function or initializer.
+         (when (c-syntactic-re-search-forward "[{(]" nil t)
+           (backward-char)
+           (c-backward-syntactic-ws)
+           (when (eq (char-before) ?\=) ; struct foo bar = {0, 0} ;
+             (c-backward-token-2)
+             (c-backward-syntactic-ws))
+           (setq name-end (point))
+           (c-backward-token-2)
+           (buffer-substring-no-properties (point) name-end))))))))
+
 (defun c-declaration-limits (near)
   ;; Return a cons of the beginning and end positions of the current
   ;; top level declaration or macro.  If point is not inside any then
@@ -1810,6 +1873,15 @@ function does not require the declaration to contain a brace block."
       (goto-char (car decl-limits))
       (push-mark (cdr decl-limits) nil t))))
 
+(defun c-cpp-define-name ()
+  "Return the name of the current CPP macro, or NIL if we're not in one."
+  (interactive)
+  (save-excursion
+    (and c-opt-cpp-macro-define-start
+        (c-beginning-of-macro)
+        (looking-at c-opt-cpp-macro-define-start)
+        (match-string-no-properties 1))))
+
 \f
 ;; Movement by statements.
 (defun c-in-comment-line-prefix-p ()
@@ -3023,46 +3095,46 @@ non-nil."
                    (c-parsing-error nil)
                    ;; shut up any echo msgs on indiv lines
                    (c-echo-syntactic-information-p nil)
-                   (in-macro (and c-auto-align-backslashes
-                                  (c-save-buffer-state ()
-                                    (save-excursion (c-beginning-of-macro)))
-                                  start))
+                   (ml-macro-start     ; Start pos of multi-line macro.
+                    (and (c-save-buffer-state ()
+                           (save-excursion (c-beginning-of-macro)))
+                         (eq (char-before (c-point 'eol)) ?\\)
+                         start))
                    (c-fix-backslashes nil)
                    syntax)
                (unwind-protect
                    (progn
                      (c-progress-init start end 'c-indent-region)
-                     (while (and (bolp)
+
+                     (while (and (bolp) ;; One line each time round the loop.
                                  (not (eobp))
                                  (< (point) endmark))
                        ;; update progress
                        (c-progress-update)
                        ;; skip empty lines
-                       (skip-chars-forward " \t\n")
-                       (beginning-of-line)
-                       ;; Get syntax and indent.
-                       (c-save-buffer-state nil
-                         (setq syntax (c-guess-basic-syntax)))
-                       (if (and c-auto-align-backslashes
-                                (assq 'cpp-macro syntax))
-                           ;; Record macro start.
-                           (setq in-macro (point)))
-                       (if in-macro
-                           (if (looking-at "\\s *\\\\$")
-                               (forward-line)
-                             (c-indent-line syntax t t)
-                             (if (progn (end-of-line)
-                                        (not (eq (char-before) ?\\)))
-                                 (progn
-                                   ;; Fixup macro backslashes.
-                                   (forward-line)
-                                   (c-backslash-region in-macro (point) nil)
-                                   (setq in-macro nil))
-                               (forward-line)))
-                         (c-indent-line syntax t t)
-                         (forward-line)))
-                     (if in-macro
-                         (c-backslash-region in-macro (c-point 'bopl) nil t)))
+                       (unless (or (looking-at "\\s *$")
+                                   (and ml-macro-start (looking-at "\\s *\\\\$")))
+                         ;; Get syntax and indent.
+                         (c-save-buffer-state nil
+                           (setq syntax (c-guess-basic-syntax)))
+                         (c-indent-line syntax t t))
+
+                       (if ml-macro-start
+                           ;; End of current multi-line macro?
+                           (when (and c-auto-align-backslashes
+                                      (not (eq (char-before (c-point 'eol)) ?\\)))
+                             ;; Fixup macro backslashes.
+                             (c-backslash-region ml-macro-start (c-point 'bonl) nil)
+                             (setq ml-macro-start nil))
+                         ;; New multi-line macro?
+                         (if (and (assq 'cpp-macro syntax)
+                                  (eq (char-before (c-point 'eol)) ?\\))
+                           (setq ml-macro-start (point))))
+
+                       (forward-line))
+
+                     (if (and ml-macro-start c-auto-align-backslashes)
+                         (c-backslash-region ml-macro-start (c-point 'bopl) nil t)))
                  (set-marker endmark nil)
                  (c-progress-fini 'c-indent-region))
                (c-echo-parsing-error quiet))
@@ -4456,5 +4528,5 @@ normally bound to C-o.  See `c-context-line-break' for the details."
 \f
 (cc-provide 'cc-cmds)
 
-;;; arch-tag: bf0611dc-d1f4-449e-9e45-4ec7c6936677
+;; arch-tag: bf0611dc-d1f4-449e-9e45-4ec7c6936677
 ;;; cc-cmds.el ends here