How to load file from dir using Racket? - scheme

I'm trying to set a dir to load files from using racket. I want to set the dir and then use command (load "extract.rktl") to load the file.
I'm on windows environment.
Command I'm trying is :
(add-to-list 'load-path ("c:/Users/racket/")
I receive error :
add-to-list: undefined;
cannot reference undefined identifier
context...:
the dir c:\Users\racket exists. Are commands correct ?
Update : this helped : How do I include files in DrScheme?

In Racket, path is a type and strings are not paths. So convert the name of a path with string->path.
(define default-dir
(string->path "c:\\user\\racket"))
Notes:
The Windows separator character '\' must be escaped as '\'.
Many Racket functions that act on paths will implicitly convert strings to paths without an explicit call to string->path.
However string operations cannot be permed on path objects.
>(string-split default-dir "\\")
string-split: contract violation
expected: string?
given: #<path:c:\user\racket>
Alternatively, a GUI can be used:
> (require racket/gui)
> (define my-file (get-file))
> my-file
#<path:/home/ben/Documents/racket/my-module.rkt>

Related

LUA: how to correctly read UFT8 file names and path with accented letters and umlaut?

Inside a more complex script in LUA, I created the following function, that is supposed to
retrieve the list of all audio files located into a directory, which is specified when the function is called (first parameter).
The function returns a formatted HTML line or a CSV one, based on the value of the 2nd parameter.
If 1, it returns HTML
If 2, it returns CSV
This function works fine unless path and/or file names don't have accented letters or umlauts.
Then fails.
here an exaggerated example, with which I've made tests:
J:\PRODUCTION\tüv-ààéérï-utf8\Rendering\tüv-ààéérï-utf8_.wav
I did try to implement solution like the one presented here:
converting UTF-8 string to ASCII in pure LUA
and here:
Handle special characters in lua file path (umlauts)
but unsuccessfully.
Is there a way, in LUA, to get file names and path as they have accented/umlauted characters?
IMPORTANT:
the LUA interpreter is not standalone. It's the one built-in the audio application Reaper.
Hence, I can't install some particular LUA extension apart.
So, I need to write some code but I'm totally lost.
[EDIT]
operating system Windows, but i should work also on Mac and Linux as well
----------------------------------------------
-- SCAN RENDERED AUDIO
----------------------------------------------
function scandir(directory,format)
local i, t, popen = 0, {}, io.popen
t = ''
local f=io.popen('dir '..directory)
for filename in popen('dir "'..directory..'" /b'):lines() do
local extension = filename:match("^.+(%..+)$")
if extension == ".wav" or
extension == ".mp3" or
extension == ".flac" or
extension == ".mov" or
extension == ".ogg" or
extension == ".mp4" then
uriFormat = filename:gsub(" ", "%%20")
if format == 1 then
t = t..'<tr class="Rendered"><td>'..directory..'</td><td>'..tostring(filename)..'</td><td><audio controls src="'..directory..'/'..tostring(uriFormat)..'"/></td></tr>'
elseif format == 2 then
t = t..directory..','..tostring(filename)..','..directory..tostring(filename)..LF
end
i = i + 1
end
end
return t
end
The first problem is converting output of dir command to UTF8 string.
Create file cp.bat somewhere on your disk:
#chcp %1 >nul
Replace
popen('dir "'..
with
popen('C:\\path\\to\\cp.bat 65001 <nul & dir "'..
The second problem is converting input parameter directory (a UTF-8 string passed to the function scandir as argument) to the correct encoding.
io.popen expects its argument to be in 1252 codepage.
So, use function utf8_to_win from this script.
Do not forget to set your codepage (1252) at line #11

On MacOS, hunspell can't find existing .aff and .dic files in Library/Spelling/

I have downloaded hunspell with a brew command, and want to use it in Emacs, but hunspell doesn't seem to locate my *.aff and *.dic files in ~/Library/Spelling, even though they are there.
With the command line command hunspell -D, the result is:
SEARCH PATH:
.::/usr/share/hunspell:/usr/share/myspell:/usr/share/myspell/dicts:/Library/Spelling:/Users/macbook/.openoffice.org/3/user/wordbook:/Users/macbook/.openoffice.org2/user/wordbook:/Users/macbook/.openoffice.org2.0/user/wordbook:/Users/macbook/Library/Spelling:/opt/openoffice.org/basis3.0/share/dict/ooo:/usr/lib/openoffice.org/basis3.0/share/dict/ooo:/opt/openoffice.org2.4/share/dict/ooo:/usr/lib/openoffice.org2.4/share/dict/ooo:/opt/openoffice.org2.3/share/dict/ooo:/usr/lib/openoffice.org2.3/share/dict/ooo:/opt/openoffice.org2.2/share/dict/ooo:/usr/lib/openoffice.org2.2/share/dict/ooo:/opt/openoffice.org2.1/share/dict/ooo:/usr/lib/openoffice.org2.1/share/dict/ooo:/opt/openoffice.org2.0/share/dict/ooo:/usr/lib/openoffice.org2.0/share/dict/ooo
AVAILABLE DICTIONARIES (path is not mandatory for -d option):
/Users/macbook/Library/Spelling/cs_CZ
Here, cs_CZ is the name of a file where my personal spelling is stored. There are other files in the folder, including cs_CZ.aff and cs_CZ.dic, as well as en_GB, but hunspell simply ignores those.
In Emacs, I tried:
(setenv "DICPATH"
(concat (getenv "HOME") "/Library/Spelling"))
(when (executable-find "hunspell")
(setq-default ispell-program-name "hunspell")
(setq ispell-really-hunspell t))
After running ispell-change-dictinary with a recommended input "czech", ispell-word gives me:
Starting new Ispell process hunspell with czech dictionary...
ispell-init-process: Can't open affix or dictionary files for
dictionary named "czech".
...and flyspell-mode:
Error enabling Flyspell mode: (Can't open affix or dictionary files
for dictionary named "czech".)
Thank you.
Spell-checkers in Emacs look up spelling in two dictionaries: the standard dictionary and your personal dictionary. The standard dictionary is specified by the variable ispell-local-dictionary or, if that is nil, by the variable ispell-dictionary. And as I can see in your output, Hunspell have only cs_CZ, but not czech dictionary:
AVAILABLE DICTIONARIES (path is not mandatory for -d option):
/Users/macbook/Library/Spelling/cs_CZ
By the way, Hunspell 1.7.0 was broken. For more see: https://github.com/hunspell/hunspell/issues/608. You can compare the output between
hunspell -D
and
hunspell -D /dev/null
to see the difference.
First, set the desired dictionary name you have installed:
;; Default dictionary to use
(setq ispell-local-dictionary "czech")
To see a list of found and parsed dictionaries use C-h v ispell-hunspell-dict-paths-alist.
In addition, I would like to suggest add your preferred dictionaries to ispell-local-dictionary-alist. This variable used to specify an alist of dictionaries and their associated parameters. Note, you have to use the same dictionary name a in ispell-dictionary. Something like this should work:
;; Here ("-d" "cs_CZ") is dictionary file name.
;; You could use '("-d" "file1,file2")' form to use multiple dictionaries.
(add-to-list
'ispell-local-dictionary-alist
'(("czech" "[[:alpha:]]" "[^[:alpha]]" "[0-9']" t
("-d" "cs_CZ") nil utf-8)))
Note, Homebrew itself provides no dictionaries for Hunspell, but you can download
compatible dictionaries from other sources, such as https://extensions.libreoffice.org/en/extensions/show/czech-dictionaries. For instructions see for example: https://passingcuriosity.com/2017/emacs-hunspell-and-dictionaries.
Finally, I highly recommend to set DICTIONARY environment variable on macOS. Without this variable you'll see on macOS something like this:
Can't open affix or dictionary files for dictionary named "XXX"
I managed to reproduce this issue on macOS. But on Linux, with the same configuration and Emacs version everything works fine. To set dictionary file name use setenv defun as follows:
(when (string-equal system-type "darwin") ; Only for macOs
;; Dictionary file name
(setenv "DICTIONARY" "cs_CZ"))
This environment variable is used by Hunspell. For more see man 1 hunspell.

How can I read file loaded in a variable in guile?

I am new to to guile and scheme and what I am trying to do right now is take a scheme file (file.scm) and load it up into a variable so I will be able to parse it, and I am having trouble finding how to do this anywhere.
What I have right now is
(define var (load "file.scm")) ; loads file scheme
but I am unsure how to start reading the lines.
load parses and runs the scheme code in a file. If you just want to read a file, use open-input-file.
(define file (open-input-file "file address here"))
(display (read-line file))
If you just want to read an entire file as a string, there's a function for that in the textual-ports module. You'd use it something like:
(define contents (call-with-input-file "file.txt" get-string-all))
(You can use call-with-input-file and with-input-from-file to avoid having to manually open and close a file port, which is handy)

Validate for File naming scheme

I have a file that drops into a folder location. I'm looking for a file named like this.
ID _Data_Tape_CCYYMMDD_hhmmss.txt
How do i accomplish this?
How about using a regular expression? in Racket, this will match the given file name:
(define name "42_Data_Tape_11220331_234532.txt")
(regexp-match #px"\\d+_Data_Tape_\\d{8}_\\d{6}.txt" name)
=> '("42_Data_Tape_11220331_234532.txt")
(regexp-match #px"\\d+_Data_Tape_\\d{8}_\\d{6}.txt" "something-else")
=> #f
To list all the files in a directory use directory-list or an equivalent procedure in your interpreter, then match their names against the above regular expression - and there, you found the files with the expected name.

Racket Scheme: Include externel rkt file with dynamic file name

I want to load external rkt file in racket scheme from a parameter of a function.
E.G.,
(define (test fileName)
(include fileName)
)
However, I am getting error indicating that fileName is not a pathname string, file' form, orlib' form.
Is there a way to fix this or is there another better way to include a file from dynamic filename?
The best way to do this is to make the external file a module, and use dynamic-require.

Resources