<?xml version="1.0"?>
<Denemo>
  <merge>
    <title>A Denemo Keymap</title>
    <author>AT, JRR, RTS</author>
    <map>
      <row>
        <after>SmallerStaff</after>
        <action>InstrumentName</action>
        <scheme>;;;InstrumentName
(let ((tag  "InstrumentName") (current "") (thematch "") (indent "0.0") (size 16.0) (nextmovement #f) (staff (number->string (d-GetStaff))))
  (if InstrumentName::params
		(set! current InstrumentName::params)
  	(begin
				(if (d-NextMovement)
					(begin
						(set! nextmovement (d-GetMovement))
						(d-PreviousMovement)))
			;so nextmovement is 2 if there is more than one movement and the name setting is on a staff in the first movement.		
  	
  	    (set! current (d-DirectiveGet-staff-display tag ))
  	    (if (not current)
  	    	(set! current "Violin"))
				(set! current (d-GetUserInput (_ "InstrumentName") (_ "Give name of instrument/voice/part\nfor current staff:") current))))
				
				
				
  (if current
 	 (let ((transparent-start "") (transparent-end "")(movement (number->string (d-GetMovement))))
  	(d-DirectivePut-staff-display tag current)
  	(d-DirectivePut-staff-override tag  DENEMO_OVERRIDE_GRAPHIC)
  	(if (equal?  current (_ "Unnamed"))
			(begin
				(set! transparent-start "\\transparent {")
				(set! transparent-end "}")))
  	(d-DirectivePut-staff-postfix tag  (string-append "\\set Staff.instrumentName = \\markup {  \\with-url #'\"scheme:(d-GoToPosition " 
			movement 
			" " staff " 1 1)(let ((choice (d-PopupMenu (list (cons (_ \\\"Change Name\\\") d-InstrumentName)   (cons (_ \\\"Change Indent\\\") d-ScoreIndent)))))
				(if choice (choice)))    \" " transparent-start "\"" current "\" " transparent-end"}"))

  	(if (&gt; (string-length current) 0)
  				(d-StaffProperties (string-append "denemo_name=" current))
  				(d-StaffProperties "denemo_name=unnamed"))
  	
  ;;if this is the first of several movements, ask to set the rest.		 	
	 (if (equal? nextmovement 2)
			(let ((do-all (d-GetUserInput (_ "Instrument Name") (_ "This sets the name in Movement 1\nSet this name for the same staff in other movements?") (_ "y"))))
				(if (equal? do-all (_ "y"))
					(let ((staffnum (d-GetStaff)))
						(d-PushPosition)
						(while (d-NextMovement)
							(if (d-GoToPosition #f staffnum 1 1)
								(d-InstrumentName current)))
						(d-PopPosition)))))
  				
 		(set! size (/ (string-&gt;number (d-ScoreProperties "query=fontsize")) 10.0))
 		(set! indent (d-DirectiveGet-score-prefix "ScoreIndent"))
  	(if indent
     			 (begin
				(set! thematch (string-match "\\layout \\{indent = ([-0-9.]+)" indent))
				;;(display thematch)
				(if (regexp-match? thematch)
	   			 (set! indent (match:substring thematch 1))))
	   		(set! indent "0.0"))

 		(set! indent (number-&gt;string (max (string-&gt;number indent) (* size (string-length current)))))
   	(d-DirectivePut-score-prefix "ScoreIndent" (string-append "\\layout {indent = " indent "}\n"))
  	(d-DirectivePut-score-override "ScoreIndent"	DENEMO_OVERRIDE_GRAPHIC)
 		(d-DirectivePut-score-display "ScoreIndent" (string-append (_ "indent=") indent))
	 	(d-SetSaved #f))

	 	
	 	))
</scheme>
        <label>Instrument Name</label>
        <tooltip>Print Instrument name before staff at start of score,</tooltip>
      </row>
    </map>
  </merge>
</Denemo>
