Subject: [patch] add-log-current-defun-function for sh-mode

From: Masatake YAMATO <[email-masked]>
Date: Mon, 01 Apr 2013 17:25:58 +0900 (JST)

This is a feature request with a patch. Please, review the 
patch and install it to the official source tree if appreciated.

The patch comes from two parts:
    1. definition of add-log-current-defun-function for sh-mode
    2. Test case

Mode own add-log-current-defun-function is not defined in sh-mode.
So wc cannot pick up a function name with C-x 4 a.
This patch implement it.

------------------------------------------------------------
revno: 112201
committer: Masatake YAMATO <[email-masked]>
branch nick: emacs-sh-add-log
timestamp: Mon 2013-04-01 17:22:03 +0900
message:
  * lisp/progmodes/sh-script.el (sh-current-defun-name): New function.
  * automated/add-log-tests.el: New file.
diff:
=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog	2013-04-01 08:13:33 +0000
+++ lisp/ChangeLog	2013-04-01 08:22:03 +0000
@@ -1,5 +1,9 @@
 2013-04-01  Masatake YAMATO  <[email-masked]>
 
+	* progmodes/sh-script.el (sh-current-defun-name): New function.
+
+2013-04-01  Masatake YAMATO  <[email-masked]>
+
 	* progmodes/sh-script.el (sh-imenu-generic-expression): Handle
 	function names with a single character.

=== modified file 'lisp/progmodes/sh-script.el'
--- lisp/progmodes/sh-script.el	2013-04-01 08:13:33 +0000
+++ lisp/progmodes/sh-script.el	2013-04-01 08:22:03 +0000
@@ -353,6 +353,29 @@
   :group 'sh-script
   :version "20.4")
 
+
+(defun sh-current-defun-name ()
+  "Find the name of function or variable at point.
+For use in `add-log-current-defun-function'."
+  (save-excursion
+    (end-of-line)
+    (when (re-search-backward
+	   (concat "\\(?:"
+		   ;; function FOO
+		   ;; function FOO()
+		   "^\\s-*function\\s-+\\\([[:alpha:]_][[:alnum:]_]*\\)\\s-*\\(?:()\\)?"
+		   "\\)\\|\\(?:"
+		   ;; FOO()
+		   "^\\s-*\\([[:alpha:]_][[:alnum:]_]*\\)\\s-*()"
+		   "\\)\\|\\(?:"
+		   ;; FOO=
+		   "^\\([[:alpha:]_][[:alnum:]_]*\\)="
+		   "\\)")
+	   nil t)
+      (or (match-string-no-properties 1)
+	  (match-string-no-properties 2)
+	  (match-string-no-properties 3)))))
+
 (defvar sh-shell-variables nil
   "Alist of shell variable names that should be included in completion.
 These are used for completion in addition to all the variables named
@@ -1533,6 +1556,7 @@
   (setq-local skeleton-newline-indent-rigidly t)
   (setq-local defun-prompt-regexp
 	      (concat "^\\(function[ \t]\\|[[:alnum:]]+[ \t]+()[ \t]+\\)"))
+  (setq-local add-log-current-defun-function #'sh-current-defun-name)
   ;; Parse or insert magic number for exec, and set all variables depending
   ;; on the shell thus determined.
   (sh-set-shell

=== modified file 'test/ChangeLog'
--- test/ChangeLog	2013-04-01 08:13:33 +0000
+++ test/ChangeLog	2013-04-01 08:22:03 +0000
@@ -1,5 +1,9 @@
 2013-04-01  Masatake YAMATO  <[email-masked]>
 
+	* automated/add-log-tests.el: New file.
+
+2013-04-01  Masatake YAMATO  <[email-masked]>
+
 	* automated/imenu-tests.el: New file.
 
 2013-03-30  Fabián Ezequiel Gallina  <[email-masked]>

=== added file 'test/automated/add-log-tests.el'
--- test/automated/add-log-tests.el	1970-01-01 00:00:00 +0000
+++ test/automated/add-log-tests.el	2013-04-01 08:22:03 +0000
@@ -0,0 +1,84 @@
+;;; add-log-tests.el --- Test suite for add-log.
+
+;; Copyright (C) 2013 Free Software Foundation, Inc.
+
+;; Author: Masatake YAMATO <[email-masked]>
+;; Keywords: vc tools
+
+;; This file is part of GNU Emacs.
+
+;; 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 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
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'add-log)
+
+(defmacro add-log-current-defun-deftest (name doc major-mode
+					      content marker expected-defun)
+  "Generate an ert test for mode-own `add-log-current-defun-function'.
+Run `add-log-current-defun' at the point where MARKER specifies in a
+buffer which content is CONTENT under MAJOR-MODE. Then it compares the
+result with EXPECTED-DEFUN."
+  (let ((xname (intern (concat "add-log-current-defun-test-"
+			       (symbol-name name)
+			       ))))
+    `(ert-deftest ,xname ()
+	 ,doc
+       (with-temp-buffer
+	 (insert ,content)
+	 (goto-char (point-min))
+	 (funcall ',major-mode)
+	 (should (equal (when (search-forward ,marker nil t)
+			  (replace-match "" nil t)
+			  (add-log-current-defun))
+			,expected-defun))))))
+
+(add-log-current-defun-deftest
+ sh-func1
+ "Test sh-current-defun-name can find function."
+ sh-mode "
+function foo
+{
+	><
+}" "><" "foo")
+
+(add-log-current-defun-deftest
+ sh-func2
+ "Test sh-current-defun-name can find function."
+ sh-mode "
+foo()
+{
+	><
+}" "><" "foo")
+
+(add-log-current-defun-deftest
+ sh-func3
+ "Test sh-current-defun-name can find function."
+ sh-mode "
+function foo()
+{
+	><
+}" "><" "foo")
+
+(add-log-current-defun-deftest
+ sh-var
+ "Test sh-current-defun-name can find variabe definision."
+ sh-mode "
+PATH=a:/ab:/usr/abc
+DIR=/pr><oc"
+"><" "DIR")
+
+(provide 'add-log-tests)
+
+;;; add-log-tests.el ends here