5
$\begingroup$

This the second question of (152743), the preamble of which is reproduced here:

Those who don't have/prefer the front end of Mathematica will either use a plain ASCII editor to write a script in an .m file or just use Mathematica directly from a command-line terminal. In either case, everything must be supplied in InputForm.

When working with a Mathematica package, commonly used symbols/functions might be excessively verbose in InputForm, and certain shortcuts/aliases would be greatly desired. Examples of built-in InputForm shortcuts are the infix operators + (Plus), - (Minus), ... and . (Dot).

Two questions that arise as a package developer:

  1. (152743)

  2. Is there something analogous to #define of C and C++ that can be placed at the top of an .m file that instructs the kernel to make replacements of literal occurrences before the lines are read? (this issue may be easily bypassed if question 1 is answered.)

$\endgroup$
7
$\begingroup$

No. In fact, even $PreRead is ignored when reading .m files.

What you can do is define a myGet as

myGet[file_] := Module[{str},
    str = Import[file,"Text"];
    str = myTextReplacemeansts[str];
    ToExpression[str];
]

to make your own substitutions.

$\endgroup$
  • $\begingroup$ Dang. OK, I'll wait for others to provide other possible solutions. $\endgroup$ – QuantumDot Jul 31 '17 at 21:54
  • $\begingroup$ Hmm.. I'm finding that $Pre and $PreRead are not observed when using Mathematica from a command-line terminal. Are $Pre and $PreRead strictly front-end functions? $\endgroup$ – QuantumDot Aug 1 '17 at 15:11
  • $\begingroup$ It shouldn't be restricted to the FE, but I'm seeing the same behavior. Time to file a bug report... $\endgroup$ – Itai Seggev Aug 2 '17 at 3:05
  • $\begingroup$ Actually, $Pre is working fine, it's only $PreRead is broken. Are you use $Pre isn't working for you? Try $Pre=Echo. $\endgroup$ – Itai Seggev Aug 2 '17 at 3:34
  • $\begingroup$ Here is a work around. If you launch the kernel with the -rawterm option, it will fix $PreRead, though that will disable the arrow keys. On Linux (and probably on OSX using homebrew or similar), you can use the rlwrap program as wrapper around the kernel to restore the arrow OKs $\endgroup$ – Itai Seggev Aug 2 '17 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.