Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kpse file search #1440

Merged
merged 13 commits into from
Nov 9, 2019
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ As of v3.0.0 this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- Staff line thickness is now set in gsp-default.tex. This corrects a problem with the staff lines changing thickness when the default spacing configuration is loaded while the staff size is something other than the default (17). See [#1461](https://github.com/gregorio-project/gregorio/issues/1461).

### Added
- GregorioTeX will now look in additional places for scores using. Users can specify paths to look in using `\gresetgregpath`. If scores are not found in the current working directory or the list of provided paths, we fall back on kpse to try and find them. See [#1395](https://github.com/gregorio-project/gregorio/issues/1395).


## [5.2.1] - 2019-04-06
### Fixed
Expand Down
24 changes: 17 additions & 7 deletions doc/Command_Index_User.tex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ \subsection{Commands}
\subsubsection{Including scores}

\macroname{\textbackslash gregorioscore}{[\optional{\#1}]\{\#2\}}{gregoriotex-main.tex}
Macro for including scores. Works on both gabc and tex files.
Macro for including scores. Works on both gabc and gtex files.

\begin{argtable}
\#1 & \texttt{n} & Optional. \#2 will be included as is. \\
Expand Down Expand Up @@ -150,6 +150,16 @@ \subsubsection{Including scores}
compile the gabc file into a gtex file. This is similar to
\texttt{[a]} except the gabc is compiled every time.

\macroname{\textbackslash gregetgregpath}{\{\#1\}}{gregoriotex-main.tex}
Set a list of additional directories which should be searched for scores. Directories may be absolute or relative, but must end with a slash (\verb=/=) and enclosed in braces (\verb={}=), even if there is only one additional directory. For example, to look in a directory called “Scores” which is alongside the main project directory, one could use the following:

\begin{latexcode}
\gresetgregpath{{../Scores/}}
\end{latexcode}

Note that these directories are not searched recursively. If you want to include subdirectories, then each subdirectory must be included individually.


\macroname{\textbackslash gresetcompilegabc}{\{\#1\}}{gregoriotex-main.tex}
A macro to change the behavior of the way Gregorio\TeX\ includes scores. This is similar to using the package options \verb=[forcecompile]=, \verb=[autocompile]=, and \verb=[nevercompile]=, but does not necessarly apply to the entire document.

Expand All @@ -166,17 +176,17 @@ \subsubsection{Including scores}
\medskip This macro can be combined in the same document with different arguments to
switch between different behaviors: \par\medskip
\begin{latexcode}
\usepackage{gregoriotex} % [nevercompile] is the default.
\usepackage{gregoriotex} % [autocompile] is the default.
----
\gregorioscore{TecumPrincipium} % gabc never compiled.
\gregorioscore[f]{TecumPrincipium} % gabc always compiled.
\gregorioscore[a]{TecumPrincipium} % gabc auto compiled.

\gresetcompilegabc{auto}
\gregorioscore{TecumPrincipium} % gabc auto compiled.
\gregorioscore[n]{TecumPrincipium} % gabc never compiled.
\gregorioscore[f]{TecumPrincipium} % gabc always compiled.

\gresetcompilegabc{never}
\gregorioscore{TecumPrincipium} % gabc never compiled.
\gregorioscore[f]{TecumPrincipium} % gabc always compiled.
\gregorioscore[a]{TecumPrincipium} % gabc auto compiled.

\gresetcompilegabc{force}
\gregorioscore{TecumPrincipium} % gabc always compiled.
\gregorioscore[n]{TecumPrincipium} % gabc never compiled.
Expand Down
6 changes: 6 additions & 0 deletions doc/Command_Index_internal.tex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ \section{Gregorio\TeX{} Controls}
\macroname{\textbackslash gre@loadgregoriofont}{}{gregoriotex-main.tex}
Loads the chosen font for the neumes at the correct size.

\macroname{\textbackslash gre@maketable}{\#1}{gregoriotex-main.tex}
Helper to convert a \TeX\ list into a Lua table. Used to pass \verb=\gre@input@path= to Lua.

\macroname{\textbackslash gre@input@path}{}{gregoriotex-main.tex}
Macro holding the list of directories to be searched for scores. \verb=\input@path= gets aliased to this list when inside \verb=\gregorioscore=.

\macroname{\textbackslash gre@calculate@constantglyphraise}{}{gregoriotex-spaces.tex}
Macro to calculate \verb=\gre@constantglyphraise=

Expand Down
30 changes: 20 additions & 10 deletions tex/gregoriotex-main.tex
Original file line number Diff line number Diff line change
Expand Up @@ -630,16 +630,6 @@
}%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% macros for the score reference (unused)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\grescorereference#1{% OBSOLETE
\gre@error{\protect\grescorereference\space is obsolete.\MessageBreak It does nothing; stop using it.}%
\relax %
}%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% macros for the typesetting the things above the initial
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -1877,6 +1867,20 @@
%% score including
%%%%%%%%%%%%%%%%%%%


\def\gre@maketabl#1{\ifx\relax#1\else,"#1"\expandafter\gre@maketabl\fi}

\def\gresetgregpath#1{%
\def\gre@input@path{#1}%
\directlua{
gre_input_path = {""\expandafter\gre@maketabl\gre@input@path\relax}
}
}

\ifx\gre@input@path\@undefined
\let\gre@input@path\input@path
\fi

% Flag to track compilation behavior
% 0 = never compile (default)
% 1 = auto compile (compile outdated scores and those lacking a compiled version)
Expand Down Expand Up @@ -1910,6 +1914,8 @@
% This forces gregoriotex to recompile the gabc file.

\def\gre@gregorioscore#1{%
\begingroup%
\let\input@path\gre@input@path%
\ifcase\gre@compilegabc% case 0, never compile
\gre@debugmsg{compile}{Refusing to compile #1}%
\input #1%
Expand All @@ -1921,13 +1927,16 @@
\directlua{gregoriotex.include_score([[#1]], true, \gre@allowdeprecated@asboolean)}%
\fi%
\relax%
\endgroup%
}%

% The internal macro called when \gregorioscore is called with the optional argument. Behavior is determined by the value of the argument:
% n - do not attempt to compile the score. Simply include it as is.
% a - perform the checks to see if the score needs to be recompiled and do so only if necessary
% f - force the compilation of the score before including it
\def\gre@gregorioscore@option[#1]#2{%
\begingroup%
\let\input@path\gre@input@path%
\ifx #1n\relax%
\gre@debugmsg{compile}{Override not compiling #2}%
\input #2%
Expand All @@ -1945,6 +1954,7 @@
\fi%
\fi%
\relax%
\endgroup%
}%

% The main macro used by the user to input scores into the document.
Expand Down
56 changes: 46 additions & 10 deletions tex/gregoriotex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -769,11 +769,10 @@ local function clean_old_gtex_files(file_withdir)
string.find(os.getenv("PATH"),";",1,true)
if onwindows then
sep = "\\"
dirpath = string.match(file_withdir, "(.*)"..sep)
else
sep = "/"
dirpath = string.match(file_withdir, "(.*)"..sep)
end
dirpath = string.match(file_withdir, "(.*)"..sep)
if dirpath then -- dirpath is nil if current directory
filename = "^"..file_withdir:match(".*/".."(.*)").."%-%d+_%d+_%d+[-%a%d]*%.gtex$"
for a in lfs.dir(dirpath) do
Expand Down Expand Up @@ -854,6 +853,37 @@ local function compile_gabc(gabc_file, gtex_file, glog_file, allow_deprecated)
end
end

local function locate_file(filename)
local result
if not gre_input_path then
gre_input_path = {""}
end
for i,k in pairs(gre_input_path) do
log("Looking in %s", k)
if lfs.isfile(k .. filename) then
result = k..filename
if result == filename then
log("Found %s directly", filename)
else
log("Found %s in %s", filename, k)
end
break
end
end
if not result then
result = kpse.find_file(filename)
if result then
log("Found %s at\n%s using kpsewhich", filename, result)
if string.match(result," ") then
warn("%s contains a space in the path\nTeX will likely complain about this", filename)
end
else
log("Cannot find %s", filename)
end
end
return result
end

local function include_score(input_file, force_gabccompile, allow_deprecated)
if string.match(input_file, "[#%%]") then
err("GABC filename contains invalid character(s): # %%\n"
Expand All @@ -872,30 +902,36 @@ local function include_score(input_file, force_gabccompile, allow_deprecated)
end

local cleaned_filename = input_name:gsub("[%s%+%&%*%?$@:;!\"\'`]", "-")
local gabc_file = string.format("%s%s.gabc", file_dir, input_name)
local gtex_file = string.format("%s%s-%s.gtex", file_dir, cleaned_filename,
local gabc_filename = string.format("%s%s.gabc", file_dir, input_name)
local gabc_file = locate_file(gabc_filename)
local gtex_filename = string.format("%s%s-%s.gtex", file_dir, cleaned_filename,
internalversion:gsub("%.", "_"))
local gtex_file = locate_file(gtex_filename)
local glog_file = string.format("%s%s-%s.glog", file_dir, cleaned_filename,
internalversion:gsub("%.", "_"))
if not lfs.isfile(gtex_file) then
if not gtex_file then
clean_old_gtex_files(file_dir..cleaned_filename)
log("The file %s does not exist. Searching for a gabc file", gtex_file)
if lfs.isfile(gabc_file) then
log("The file %s does not exist. Will use gabc file", gtex_filename)
if gabc_file then
local gabc = io.open(gabc_file, 'r')
if gabc == nil then
err("\n Unable to open %s", gabc_file)
return
else
gabc:close()
end
compile_gabc(gabc_file, gtex_file, glog_file, allow_deprecated)
tex.print(string.format([[\input %s\relax]], gtex_file))
compile_gabc(gabc_file, gtex_filename, glog_file, allow_deprecated)
tex.print(string.format([[\input %s\relax]], gtex_filename))
return
else
err("The file %s does not exist.", gabc_file)
err("The file %s does not exist", gabc_filename)
return
end
end
if not gabc_file then
tex.print(string.format([[\input %s\relax]], gtex_file))
return
end
local gtex_timestamp = lfs.attributes(gtex_file).modification
local gabc_timestamp = lfs.attributes(gabc_file).modification
-- open the gabc file for reading so that LuaTeX records input from it
Expand Down