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

adds option to run configuration to expand macros in environment variables #3872

Merged
merged 6 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Writerside/topics/Run-configuration-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ For example `TEXINPUTS=/path/to/directory//:`, where `//` means that LaTeX (and
Similarly, you can also set `TEXMFHOME` to some other path than the default `~/texmf`, so that sty and cls files will be searched in the `tex/latex` subdirectory or any child directory of it.
For more information about paths resolving, see [https://www.tug.org/texinfohtml/kpathsea.html#Path-searching](https://www.tug.org/texinfohtml/kpathsea.html#Path-searching)

### Expand macros in environment variables
_Since b0.10.1_

When ticked, macros such as `$ContentRoot$` (the path to the content root of the current run configuration's main file) are expanded.
An example use for this would be to add a directory containing the document class to be used to `TEXINPUTS`, e.g., `TEXINPUTS=$ContentRoot$/MyDir`.
Doing so might be especially helpful in the context of setting up a 'run configuration template,' which is a run configuration that is used by default for any time a LaTeX file is run (and thus compiled) for the first time. For more details on run configuration templates, see [https://www.jetbrains.com/help/idea/run-debug-configuration.html#templates](https://www.jetbrains.com/help/idea/run-debug-configuration.html#templates).

An overview of all built-in macros can be found at [https://www.jetbrains.com/help/idea/built-in-macros.html](https://www.jetbrains.com/help/idea/built-in-macros.html).
Whenever the documentation mentions 'the current file,' in the context of a TeXiFy run configuration, this always refers to the main `.tex` file being compiled.

## LaTeX code to run before compiling the main file
_Since b0.9.5_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ package nl.hannahsten.texifyidea.run.latex
import com.intellij.execution.ExecutionException
import com.intellij.execution.configurations.CommandLineState
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.impl.ExecutionManagerImpl
import com.intellij.execution.process.KillableProcessHandler
import com.intellij.execution.process.ProcessHandler
import com.intellij.execution.process.ProcessTerminatedListener
import com.intellij.execution.runners.ExecutionEnvironment
import com.intellij.execution.util.ProgramParametersConfigurator
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.impl.SimpleDataContext
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.util.SystemInfo
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.applyIf
import nl.hannahsten.texifyidea.editor.autocompile.AutoCompileDoneListener
import nl.hannahsten.texifyidea.lang.LatexPackage
import nl.hannahsten.texifyidea.lang.commands.LatexGenericRegularCommand
Expand All @@ -33,6 +38,7 @@ import nl.hannahsten.texifyidea.util.files.psiFile
import nl.hannahsten.texifyidea.util.includedPackages
import nl.hannahsten.texifyidea.util.magic.PackageMagic
import java.io.File
import java.util.*

/**
* Run the run configuration: start the compile process and initiate forward search (when applicable).
Expand All @@ -41,6 +47,8 @@ import java.io.File
*/
open class LatexCommandLineState(environment: ExecutionEnvironment, private val runConfig: LatexRunConfiguration) : CommandLineState(environment) {

private val programParamsConfigurator = ProgramParametersConfigurator()

@Throws(ExecutionException::class)
override fun startProcess(): ProcessHandler {
val compiler = runConfig.compiler ?: throw ExecutionException("No valid compiler specified.")
Expand Down Expand Up @@ -95,9 +103,17 @@ open class LatexCommandLineState(environment: ExecutionEnvironment, private val
?: throw ExecutionException("Compile command could not be created.")

val workingDirectory = if (compiler == LatexCompiler.TECTONIC && mainFile.hasTectonicTomlFile()) mainFile.findTectonicTomlFile()!!.parent.path else mainFile.parent.path

@Suppress("UnstableApiUsage")
val envVariables = runConfig.environmentVariables.envs.applyIf(runConfig.expandMacrosEnvVariables) {
ExecutionManagerImpl.withEnvironmentDataContext(SimpleDataContext.getSimpleContext(CommonDataKeys.VIRTUAL_FILE, mainFile, environment.dataContext)).use {
mapValues { programParamsConfigurator.expandPathAndMacros(it.value, null, runConfig.project) }
}
}

val commandLine = GeneralCommandLine(command).withWorkDirectory(workingDirectory)
.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE)
.withEnvironment(runConfig.environmentVariables.envs)
.withEnvironment(envVariables)
val handler = KillableProcessHandler(commandLine)

// Reports exit code to run output window when command is terminated
Expand Down
12 changes: 12 additions & 0 deletions src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class LatexRunConfiguration(
private const val HAS_BEEN_RUN = "has-been-run"
private const val BIB_RUN_CONFIG = "bib-run-config"
private const val MAKEINDEX_RUN_CONFIG = "makeindex-run-config"
private const val EXPAND_MACROS_IN_ENVIRONMENT_VARIABLES = "expand-macros-in-environment-variables"

// For backwards compatibility
private const val AUX_DIR = "aux-dir"
Expand All @@ -100,6 +101,7 @@ class LatexRunConfiguration(
}
}

var expandMacrosEnvVariables = false
var environmentVariables: EnvironmentVariablesData = EnvironmentVariablesData.DEFAULT
var beforeRunCommand: String? = null

Expand Down Expand Up @@ -280,6 +282,15 @@ class LatexRunConfiguration(
// Read environment variables
environmentVariables = EnvironmentVariablesData.readExternal(parent)

// Read whether to expand macros in run configuration
val expandMacrosRunConfigurationBoolean = parent.getChildText(EXPAND_MACROS_IN_ENVIRONMENT_VARIABLES)
if (expandMacrosRunConfigurationBoolean == null) {
this.expandMacrosEnvVariables = false
}
else {
this.expandMacrosEnvVariables = expandMacrosRunConfigurationBoolean.toBoolean()
}

val beforeRunCommandRead = parent.getChildText(BEFORE_RUN_COMMAND)
beforeRunCommand = if (beforeRunCommandRead.isNullOrEmpty()) null else beforeRunCommandRead

Expand Down Expand Up @@ -379,6 +390,7 @@ class LatexRunConfiguration(
parent.addContent(Element(VIEWER_COMMAND).also { it.text = viewerCommand ?: "" })
parent.addContent(Element(COMPILER_ARGUMENTS).also { it.text = this.compilerArguments ?: "" })
this.environmentVariables.writeExternal(parent)
parent.addContent(Element(EXPAND_MACROS_IN_ENVIRONMENT_VARIABLES).also { it.text = expandMacrosEnvVariables.toString() })
parent.addContent(Element(BEFORE_RUN_COMMAND).also { it.text = this.beforeRunCommand ?: "" })
parent.addContent(Element(MAIN_FILE).also { it.text = mainFile?.path ?: "" })
parent.addContent(Element(OUTPUT_PATH).also { it.text = outputPath.virtualFile?.path ?: outputPath.pathString })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nl.hannahsten.texifyidea.run.latex.ui

import com.intellij.execution.configuration.EnvironmentVariablesComponent
import com.intellij.ide.macro.MacrosDialog
import com.intellij.openapi.fileChooser.FileChooserDescriptor
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
import com.intellij.openapi.fileTypes.PlainTextFileType
Expand All @@ -16,6 +17,8 @@ import com.intellij.ui.SeparatorComponent
import com.intellij.ui.TitledSeparator
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.components.JBTextField
import com.intellij.ui.components.fields.ExtendableTextComponent
import com.intellij.ui.components.fields.ExtendableTextField
import nl.hannahsten.texifyidea.run.bibtex.BibtexRunConfigurationType
import nl.hannahsten.texifyidea.run.compiler.LatexCompiler
import nl.hannahsten.texifyidea.run.compiler.LatexCompiler.Format
Expand Down Expand Up @@ -57,6 +60,7 @@ class LatexSettingsEditor(private var project: Project?) : SettingsEditor<LatexR
// Not shown on non-MiKTeX systems
private var auxilPath: LabeledComponent<ComponentWithBrowseButton<*>>? = null

private var expandMacrosEnvVariables: JBCheckBox? = null
private var compileTwice: JBCheckBox? = null
private lateinit var outputFormat: LabeledComponent<ComboBox<Format>>
private lateinit var latexDistribution: LabeledComponent<ComboBox<LatexDistributionType>>
Expand Down Expand Up @@ -104,6 +108,9 @@ class LatexSettingsEditor(private var project: Project?) : SettingsEditor<LatexR

// Reset environment variables
environmentVariables.envData = runConfiguration.environmentVariables
if (expandMacrosEnvVariables != null) {
expandMacrosEnvVariables!!.isSelected = runConfiguration.expandMacrosEnvVariables
}

beforeRunCommand.component.text = runConfiguration.beforeRunCommand

Expand Down Expand Up @@ -209,6 +216,9 @@ class LatexSettingsEditor(private var project: Project?) : SettingsEditor<LatexR
// Apply environment variables
runConfiguration.environmentVariables = environmentVariables.envData

// Apply parse macros in environment variables
runConfiguration.expandMacrosEnvVariables = expandMacrosEnvVariables?.isSelected ?: false

runConfiguration.beforeRunCommand = beforeRunCommand.component.text

// Apply main file.
Expand Down Expand Up @@ -297,6 +307,30 @@ class LatexSettingsEditor(private var project: Project?) : SettingsEditor<LatexR
environmentVariables = EnvironmentVariablesComponent()
panel.add(environmentVariables)

expandMacrosEnvVariables = JBCheckBox("Expand macros in environment variables")
expandMacrosEnvVariables!!.isSelected = false

val environmentVariableTextField = environmentVariables.component.textField as ExtendableTextField
var envVariableTextFieldMacroSupportExtension: ExtendableTextComponent.Extension? = null

expandMacrosEnvVariables!!.addItemListener {
if (it.stateChange == 1) { // checkbox checked
if (envVariableTextFieldMacroSupportExtension != null) {
environmentVariableTextField.addExtension(envVariableTextFieldMacroSupportExtension!!)
}
else {
MacrosDialog.addTextFieldExtension(environmentVariableTextField)
envVariableTextFieldMacroSupportExtension = environmentVariableTextField.extensions.last()
}
}
else {
envVariableTextFieldMacroSupportExtension?.let { theExtension ->
environmentVariableTextField.removeExtension(theExtension)
}
}
}
panel.add(expandMacrosEnvVariables)

beforeRunCommand = LabeledComponent.create(RawCommandLineEditor(), "LaTeX code to run before compiling the main file")
panel.add(beforeRunCommand)

Expand Down
Loading