@@ -403,7 +403,7 @@ function wizard() {
403403}
404404
405405function run() ( set -euo pipefail
406- local makefile=' ' buffer=' ' task=' '
406+ local makefile=' ' buffer=' ' task=' ' baseindent= ' '
407407 local arg=' ' make_args=() named_args=() pos_args=() pos_arg_idx=0
408408
409409 local runfile_variables=' ' runfile_variable_re=' '
@@ -539,26 +539,48 @@ function run() ( set -euo pipefail
539539 runfile_variables=" ${runfile_variables} " $'\n\n'
540540 fi
541541
542+ baseindent=" $( grep -Eo ' ^[[:space:]]+' " $( smartcase-file runfile ) " | head -1 ) "
543+ if [[ -z " ${baseindent} " ]]
544+ then
545+ baseindent='\t'
546+ fi
547+
542548# ::::::::::::::::::::::::::::::::::::::::::
543549# Construct temporary Makefile from Runfile:
544550# Note: <<-EOF doesn't produce correct indentation for final lines; <<EOF required.
545551cat <<EOF> " ${makefile} "
552+ SHELL = bash
553+
546554${runfile_variables} .PHONY: _tasks
547555_tasks: .tasks
548556$(
549557 grep " ${runfile_grep_filter_args[@]} " " $( smartcase-file runfile ) " \
550558 | grep -Ev " ${runfile_variable_re} " \
551559 | sed -E \
552- -e ' s/[[:space:]]*$//' \
560+ -e " s/[[:space:]]*\ $ //" \
553561 ` # trim any trailing whitespace from lines` \
554- -e " s!^[[:space:]]*([^[:space:]])!\t\1!" \
555- ` # prefix every non-blank line with TAB` \
562+ -e " s/^${baseindent} //" \
563+ ` # trim leading base indent from lines` \
564+ -e " s/^[[:space:]]+/\t/" \
565+ ` # replace deeper indentation with TAB` \
566+ -e " s!^!\t!" \
567+ ` # prefix every line with TAB` \
568+ -e " s!^\t\$ !!" \
569+ ` # remove TAB from blank lines` \
556570 -e " s!^\t${task_re} (.*)\$ !\n.PHONY: \1\n\1:${subtask_re} \#\3!" \
557- ` # remove TAB prefix from lines that match task pattern` \
558- -e " s!^\t(if|elif|then|else|for|while)[[:space:]](.*;.*)\$ !\t\1 \2 \\\\ !" \
559- -e " s!^\t(if|elif|then|else|for|while)[[:space:]]([^;]*)\$ !\t\1 \2; \\\\ !" \
560- -e " s!^\t(then|do|else|elif)\$ !\t\1 \\\\ !" \
561- ` # automatically add backslashes to multiline statements (if, for, while)` \
571+ ` # remove TAB-prefix from lines that match task pattern` \
572+ ` # Improve Make's default handling of multiline statements (if, for, while):` \
573+ -e " s!^\t\t(.*;[[:space:]]*[^\\ ]?)\$ !\t\t\1 \\\\ !" \
574+ -e " s!^\t\t(.*[^\\ ])\$ !\t\t\1; \\\\ !" \
575+ ` # add backslash and/or semicolon to deep-indented lines where missing` \
576+ -e " s!^\t(if|elif|for|while)[[:space:]]([^;]*;[[:space:]]*(then|do))\$ !\t\1 \2 \\\\ !" \
577+ ` # add backslash after "if ...; then" and "for ...; do" lines` \
578+ -e " s!^\t(if|elif|then|else|for|while|do)[[:space:]](.*;)\$ !\t\1 \2 \\\\ !" \
579+ ` # add backslash after keyword lines that end with a semicolon` \
580+ -e " s!^\t(if|elif|then|else|for|while|do)[[:space:]](.*[^;\\ ])\$ !\t\1 \2; \\\\ !" \
581+ ` # add semicolon and backslash after keyword lines NOT ending with semicolon` \
582+ -e " s!^\t(then|else|do)\$ !\t\1 \\\\ !" \
583+ ` # add backslash after "then", "else", "do" when on their own line` \
562584 | cat -s
563585)
564586
0 commit comments