Emacs does not reference C's head files directly - elisp

When invoking C's API to write dynamic modules,
emacs does not reference C' head files directly but use
emacs_value Fmake_hash_table = env->intern(env, "make-hash-table")
emacs_value sth = env->funcall(env, Fmake_hash_table, 2, arg)
Why get it done in such a cumbersome way?

Related

How to create a shortcut of a required function (`cp = FileUtils.cp`)?

I just want to call cp instead of FileUtils.cp. This is turning to be surprisingly hard to do in Ruby!
In Javascript this would simply be: cp = FileUtils.cp. This doesn't work in Ruby because its paren-less calling doesn't allow assigning this way (Ruby thinks I'm calling FileUtils.cp but I'm trying to assign it, I get the error: wrong number of arguments (given 0, expected 1) (ArgumentError)).
I tried alias but alias cp FileUtils.cp doesn't work because of the dot (I get the error: syntax error, unexpected '.', expecting end-of-input). alias_method would be for creating an alias within FileUtils, like FileUtils.cp2, but that's not what I want to do.
cp = FileUtils.method :cp works BUT my new shortcut is a "second-class" function, whenever I use it I have to call it as cp.call instead of just cp, thus reducing the brevity of my shortcut and forcing me to remember this new way of calling some functions.
Is there a way to simply get cp be FileUtils.cp? Thanks!
You can create a function in global space
def cp(src, dest, options = {})
FileUtils.cp(src, dest, options = {})
end

Can I make an alias for `set -l` in the fish shell?

I make liberal use of set -l/set --local in my fish scripts, so I considered making an alias for it. The name local seems apt. Unfortunately, the obvious approach doesn't work:
function local
set -l $argv
end
This fails because, of course, the variable this creates is local to the local function itself. Using alias doesn't work, either, since it's just a (perhaps legacy) shorthand for the above function.
Is there any way I can create a local variable in the calling scope without doing anything more complicated than local foo 'bar'? Obviously I could make something like eval (local foo 'bar') work, but that would sort of defeat the purpose. I'm open to any sort of trickery or hacks that would make this work, if any exist.
I'm afraid not, local variables are always local to the topmost scope, and there's no way to wrap set without creating a new scope. The best you can do is this:
function def --no-scope-shadowing
set $argv
end
This creates a variable in the calling function scope. This is subtly different than local: local is scoped to a block, while the above is scoped to the function. Still it may be sufficient for your use case.

Julia: Having a function f() containing the macro #printf, how can I access the output outside f()?

In the Julia NMF package a verbose option provides information on convergence using the #printf macro.
How can I access this output without rewriting the NMF package io?
To rephrase, having a function f() containing the macro #printf, how can I access the output outside f()?
This does seem like useful functionality to have: I would suggest that you file an issue with the package.
However, as a quick hack, something like the following should work:
oldout = STDOUT
(rd,wr) = redirect_stdout()
start_reading(rd)
# call your function here
flush_cstdio()
redirect_stdout(oldout)
close(wr)
s = readall(rd)
close(rd)
s

Python: Call a shell script which calls a bin. With arguments

The context: There is a map somewhere on the system with bin files which I'd like to call. They are not callable directly though, but through shell scripts which do all kinds of magic and then call the corresponding bin with: "$ENV_VAR/path/to/the/bin" "$#" (the software is non-free, that's probably why this construction is used)
The problem: Calling this from within Python. I tried to use:
from subprocess import call
call(["nameOfBin", "-input somefile"])
But this gave the error ERROR: nameOfBin - Illegal option: input somefile. This means the '-' sign in front of 'input' has disapeared along the way (putting more '-' signs in front doesn't help).
Possible solutions:
1: In some way preserving the '-' sign so the bin at the end actually takes '-input' as an option instead of 'input'.
2: Fix the magic in a dirty way (I will probably manage), and have a way to call a bin at a location defined by a $ENV_VAR (environment variable).
I searched for both methods, but appearantly nobody before me had such a problem (or I didn't see it: Sorry if that's the case).
Each item in the list should be a single argument. Replace "-input somefile" with "-input", "somefile":
from subprocess import call
rc = call(["nameOfBin", "-input", "somefile"])

How to get a filename from an IO object in ruby

In ruby...
I have an IO object created by an external process, which I need to get the file name from.
However I only seem to be able to get the File descriptor (3), which is not very useful to me.
Is there a way to get the filename from this object or even to get a File Object?
I am getting the IO object from notifier. So this may be a way of getting the file path as well?
There is a similar question on how to get a the filename in C, I will present here the answer to this question in a ruby way.
Getting the filename in Linux
Suppose io is your IO Object. The following code gives you the filename.
File.readlink("/proc/self/fd/#{io.fileno}")
This does not work for example if the file was removed after the io object was created for it. With this solution you have the filename, but not an File object.
Getting a File object which does not know the filename
The method IO#for_fd can create an IO and it's subclasses for any given integer filedescriptor. Your get your File object for your fd by doing:
File.for_fd(io.fileno)
Unfortunely this File object does not know the filename.
File.for_fd(io.fileno).path # => nil
I scanned through the ruby-1.9.2 sources. There seems to be no way in pure ruby to manipulate the path after the file object was created.
Getting a File object which does know the filename
An extension to ruby can be created in C which first calls File#for_fd and afterwards manipulates the Files internal data structures. This sourcecode does work for ruby-1.9.2, for other versions of ruby it may has to be adjustet.
#include "ruby.h"
#include "ruby/io.h"
VALUE file_fd_filename(VALUE self, VALUE fd, VALUE filename) {
VALUE file= rb_funcall3(self, rb_intern("for_fd"), 1, &fd);
rb_io_t *fptr= RFILE(rb_io_taint_check(file))->fptr;
fptr->pathv= rb_str_dup(filename);
return file;
}
void Init_filename() {
rb_define_singleton_method(rb_cFile, "for_fd_with_filename", file_fd_filename, 2);
}
Now you can do after compiling:
require "./filename"
f= File.for_fd_with_filename(io.fileno, File.readlink("/proc/self/fd/#{io.fileno}"))
f.path # => the filename
The readlink could also be put into the File#for_fd_with_filename definiton. This examples is just to show how it works.
If you are sure that the IO object represents a File you could try something like this
path = io.path if io.respond_to?(:path)
See documentation for File#path

Resources