; This plugin was tested with Gimp 2.7.5
; This plugin will not work for Gimp 2.6.x because of the unknown layer group
; 
; selectiontolayer.scm
; adds a layer from selection right above the selected
; layer. It can not be used on layer group
;
; This program 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 2 of the License, or
; (at your option) any later version.
; 
; This program 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 this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;
(define (script-fu-selection-to-layer-stable inImage inLayer)
	(let*
      (
        (layername (car (gimp-drawable-get-name inLayer)))
        (newlayer 0)
      )
      (gimp-image-undo-group-start inImage)
      (gimp-edit-copy inLayer)
      (set! newlayer (car (gimp-edit-paste inLayer 1)))
      (gimp-floating-sel-to-layer newlayer)        
      (let loop ((pos (pred (car (gimp-image-get-layer-position inImage inLayer)))))
      (unless (zero? pos)
        (gimp-image-lower-layer inImage newlayer)
        (loop (pred pos)) ))
      (gimp-drawable-set-name newlayer (string-append "select-" layername))
      (gimp-selection-none inImage)
  	  (gimp-image-undo-group-end inImage)
  )
)

(script-fu-register "script-fu-selection-to-layer-stable"
"Ebene aus Auswahl"
"selectiontolayer.scm
Neue Ebene aus Auswahl"
"H.Kuhse"
"H.Kuhse"
"2009-06-18"
"*"
SF-IMAGE "Image" 0
SF-DRAWABLE "Layer" 0
)

(script-fu-menu-register 
  "script-fu-selection-to-layer-stable" 
  "<Layers>"
)

(script-fu-menu-register 
  "script-fu-selection-to-layer-stable" 
  "<Image>/Layer"
)