6

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.)

QuantumDot
  • 19,601
  • 7
  • 45
  • 121

1 Answers1

7

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.

Itai Seggev
  • 14,113
  • 60
  • 84
  • Dang. OK, I'll wait for others to provide other possible solutions. – QuantumDot Jul 31 '17 at 21:54
  • 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? – QuantumDot Aug 01 '17 at 15:11
  • It shouldn't be restricted to the FE, but I'm seeing the same behavior. Time to file a bug report... – Itai Seggev Aug 02 '17 at 03:05
  • Actually, $Pre is working fine, it's only $PreRead is broken. Are you use $Pre isn't working for you? Try $Pre=Echo. – Itai Seggev Aug 02 '17 at 03:34
  • 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 – Itai Seggev Aug 02 '17 at 03:51
  • Ah yes, I wasn't paying attention; $Pre actually does work on my system. I'll try the -rawterm option to see if it fixes $PreRead; EDIT yes it does work. Also, it allows me to enter special characters, too. (I was wondering why I couldn't do that before). – QuantumDot Aug 02 '17 at 04:12