Save colored emacs text from shell - bash

I am attempting to save a few hundred short files (~100 lines ea.) as a Post Script (or pdf) file using the colors as emacs renders them. So I am writing a bash script that opens them in emacs and saves them as a .ps file.
My best attempt so far is to iteratively use the following command:
emacs --file $(filename).m --no-desktop --eval '(ps-print-buffer-with-faces)' --eval '(kill-emacs)'
However, this directly sends it to the default printer. I'd like to save it as $(filename)_PS.ps. Within the emacs gui, I can do
C-u M-x ps-print-buffer-with-faces
and it brings up the file save prompt. However doing this by hand is cumbersome. How might this be done with a shell script?

The easy answer is to just provide the code, but then I'm the one learning and
your still having to ask each time you need help with Emacs! Much more fun for
me to try and point you in the right direction - probably less fun for you
initially, but if I get it right, you won't need to ask as many questions in the
future and then have to wait for another opinionated answer like this one :)
The most important skill to learn with Emacs is how to use the built-in help
facility. The second most important is to learn some basic Emacs lisp by reading
the Emacs Lisp Reference (bundled with Emacs). The third is learning to scratch
your own itch.
You have the basic function, so we can start there. C-h f
ps-print-buffer-with-faces tells us....
ps-print-buffer-with-faces is an interactive autoloaded Lisp function
in ‘ps-print.el’.
It is bound to .
(ps-print-buffer-with-faces &optional FILENAME)
Generate and print a PostScript image of the buffer. Like
‘ps-print-buffer’, but includes font, color, and underline information
in the generated image. This command works only if you are using a
window system, so it has a way to determine color values.
OK, but at first glance, there doesn't seem to be much useful info
there. However, there is a link to ps-print-buffer which is clickable. Clicking
on it we get...
ps-print-buffer is an interactive autoloaded Lisp function in
‘ps-print.el’.
It is bound to .
(ps-print-buffer &optional FILENAME)
Generate and print a PostScript image of the buffer.
Interactively, when you use a prefix argument (C-u), the command
prompts the user for a file name, and saves the PostScript image in
that file instead of sending it to the printer.
Noninteractively, the argument FILENAME is treated as follows: if it
is nil, send the image to the printer. If FILENAME is a string, save
the PostScript image in a file with that name.
This gives us a bit more, including important details on how calling it
non-interactively, the argument is the name of the file to write the output
to. This seems promising as we don't want to be forced to call this
interactively using C-u for every one of the many files we want to print.
If we could define a new command which would take care of this, we could just
use M-x - even better, we could bind it to a key and then we only need to use
that shortcut.
Looking in the Emacs Lisp Reference (also bundled with Emacs), we see the menu
option Command Loop. Looking in that node, we see a menu option for Defining
Commands. This looks promising. After reading that node and following some links
in that node about defining functions etc, we get a basic starting point of ...
(defun buffer-to-ps ()
(interactive)
;; do something
)
We put that in the scratch buffer and evaluate it. Not terribly exciting. Lets
add some documentation so we can remember what this is for ...
(defun buffer-to-ps ()
"Write the current buffer to a PS file"
(interactive)
;; do something
)
Now we can do C-h f buffer-to-ps and we will see our documentation. Still, not
doing a lot. We need to call our function and provide it with a file name
argument. We try ...
(defun buffer-to-ps ()
"Write the current buffer to a PS file"
(interactive)
(ps-print-buffer-with-faces "test.ps"))
With the cursor at the end of the definition, while in the scratch buffer, we do
C-x C-e to evaluate our definition. We try running our new command while in the
scratch buffer, we do M-x buffer-to-ps and we see a message in the echo area
which says a file call test.ps has been written. Opening that file with a viewer
and we see the file has been created and it has the colours etc. However, we
don't want all the files to be called test.ps, so now we need to find a way to
generate a unique name to use as the file name argument. There are a few ways we
could do this, but the first obvious one is to somehow try and use the name of
the buffer. We use C-h a and enter "buffer name", which gives a few hits. Going
through the list, we see buffer-name. Doing C-h f buffer-name gives us
buffer-name is a built-in function in ‘C source code’.
(buffer-name &optional BUFFER)
Return the name of BUFFER, as a string. BUFFER defaults to the current
buffer. Return nil if BUFFER has been killed.
Looks promising, so let's give it a go. ...
(defun buffer-to-ps ()
"Write the current buffer to a PS file"
(interactive)
(ps-print-buffer-with-faces (buffer-name)))
Evaluating the definition and running it with M-x results in a new file being
created called scratch. Looking at it with a ps viewer and we see it is our
buffer contents with colour etc. So we have done it!
But wait, we have a problem Huston! If we run this with a buffer which contains
contents from a file, the buffer name will be the same as the file name and when
we run our command, it will overwrite our file - probably not what we want.
We probably want the output file to be similar to the corresponding script, but
we also probably want it to reflect that it is a postscript rendering of that
script. There are a few things we can try, but being lazy, we will go for the
easiest first. What about if we just generate a file name which has ".ps"
appended to it. This will also mean we have a file name with an extension which
is a better fit with file name conventions. How do we concatenate a ".ps" to the
file name string returned by buffer-name? C-h a concat and we get
concat is a built-in function in ‘C source code’.
(concat &rest SEQUENCES)
Concatenate all the arguments and make the result a string. The result
is a string whose elements are the elements of all the arguments. Each
argument may be a string or a list or vector of characters (integers).
So we update our function to be ...
(defun buffer-to-ps ()
"Write the current buffer to a PS file"
(interactive)
(ps-print-buffer-with-faces (concat (buffer-name) ".ps")))
We use M-x to try it out and end up with the file scratch.ps.
So we now have the basic functionality. We could bind this command to a key
sequence so that when we are in a buffer, we just hit the key sequence and emacs
will generate a file with the extension .ps based on the current buffer name and
which contains a postscript rendering of the buffer contents with the
corresponding face colours.
What next? Well, using this function requires that we first open the file in a
buffer and then call the function (possibly just by hitting a key). We could do
it on the command line as your earlier version shows, but starting emacs each
time just to do this is woefully inefficient. What would be a better approach?
One thing to try would be to define a function which lists all the files in a
directory with a particular file name pattern i.e. all script files and for each
of them, load them into a buffer and then call the function you have just
defined. This is left as an exercise for the reader! Post an update to this
question or create a new one and perhaps someone will provide some more help :)

Related

Is it possible to add elements to a list, and have the list save these values even after closing an Emacs session?

I have a variable called my-projects that points to a list of strings of my current projects (e.g., "chores", "lectures", etc.). Whenever I call a function, say (completing-read ...), and I happen to create a new project on the fly, I would like to have a function that adds this new project to the list of projects forever. What I can't figure out is how to "save" the updated list my-projects so that it keeps the newly added values even after the end of the current Emacs session. I can only come up with convoluted solutions involving replacing strings in the file where the variable my-projects is defined, but I surmise that there must be a more efficient way to do this.
You could start by developing a function that saves your data structure to a file.
(defvar my-projects
'("chores"
"lectures"))
(defun my-save-my-projects ()
"Saves my projects in my home folder."
(interactive)
(with-temp-buffer
(insert (prin1-to-string my-projects))
(write-region (point-min)
(point-max)
"~/.my-projects.eld")))
Next you may want to find a suitable event that you can hook your saving function onto. For example, you could save your project list whenever Emacs closes down:
(when (not (memq 'my-save-my-projects kill-emacs-hook))
(add-hook 'kill-emacs-hook
'my-save-my-projects)))
There may be better hooks for your use-case.
Ultimately, you will want to repopulate your variable by reading in that file when Emacs starts. I'll leave that out here (or as an "exercise to the reader" ;-). There's plenty of examples out there.
You could use defcustom to create a user-customizable variable. Emacs already has built-in ways to persist the value of customized variables. Use customize-var to interactively update its value when you start a new project.

How to debug script-fu scripts for gimp in scheme?

I try to make some script for gimp, using script-fu, scheme.
Naturally, as a beginner, there are lots of errors and misunderstandings.
Now I'm looking for a way to debug those scripts.
I found (gimp-message), but the result does not show up. I'm not aware if there is a possibility to print debug messages to anywhere I could check them.
Generating new images filled with text would probably work ;-) but looks a bit like an overkill.
What ways to debug a script in gimp are there?
There may be a better way, but, the way I do it is to:
open the script fu console (Filters -> Script Fu -> Console)
edit a file (say for example: /Users/jamesanderson/code/scheme/gimp/learn1.scm
type: (load "/Users/jamesanderson/code/scheme/gimp/learn1.scm") into the console
hit enter
edit file for changes
keyboard arrow up (to get get the load function call again without typing)
hit enter
Note this is an extension of my answer based on the comment. When going beyond the simple proof of concept scripts like the above, its important to know that that console retains state between calls. So for instance, if we run this scheme script:
(define jea-test-img-id nil)
(define (jea-find-test-img)
(car (gimp-image-list)))
and then go to the console and type:
jea-test-img-id
we get as a result:
> jea-test-img-id
()
which is good, the script initiated the variable as needed and we see the result. So var set in script, console retains state change from script. Now lets change the state in the console:
(set! jea-test-img-id (jea-find-test-img))
we call a convenience function that grabs the first image ID in the active image list and store it in the variable we previously declared (which was then nil). Now lets examine the question again:
jea-test-img-id
then the result:
> jea-test-img-id
1
so high level: when you are working on a script, create working variables that hold the things you want to work on, like image, pxiels, widths etc then piece by piece get the functions working. In the end you may collapse it into one clean function once you have the short snippet pieces.
The output of gimp-message goes to the "Error console" (if you have this dockable dialog setup), otherwise in a warning dialog.
If you are in Linux or OSX, you can also start Gimp from a terminal and use (print ...) calls, they will show in the terminal.
You can make your life easier and write your scripts in Python (easier to learn, and more powerful...). See here for the doc, here for some examples, and here for some debug tricks for Windows.
If you run gimp from the command line with the -c flag, messages are printed in the terminal:
$ gimp -c myfile.xcf
# run my plugin from the GUI menu
My Plugin-Warning: this is a message
Tested with GIMP v2.10.18 see gimp --help for more command line options.

Unclear behavior DOS application's function

I'm trying to reverse some DOS application. It's uses *.VAR file like database (perhaps encoding this file). That's application looks like system for testing students. The application shows random questions from the DB-file and get your answer.
In this place programm read VAR-file length, then open the file and assign it to "newFilePtr_256_byte" var (yes, it's lenght 256 byte).
Next, going call of SUBJ FUNCTION (first screenshot).
After there is FileCounter check and do this call again (until all file would be read).
The function, i can't explain for, on second screenshot.
It's read a symbol from newFilePtr_256_byte and write it to console (nothing on console in real life working)?
Or it's do something else?
I can't recognize function behavior, coz i don't know, where i can get fully description Pascal functions in assembler listing.
I don't put in my question listing of caller function, coz it's really big.
As i can recognize, this function encode file content, by xoring with 0CDh constant, and then write it to buffer in memory.

Rstudio difference between run and source

I am using Rstudio and not sure how options "run" and "source" are different.
I tried googling these terms but 'source' is a very common word and wasn't able to get good search results :(
Run and source have subtly different meanings. According to the RStudio documentation,
The difference between running lines from a selection and invoking
Source is that when running a selection all lines are inserted
directly into the console whereas for Source the file is saved to a
temporary location and then sourced into the console from there
(thereby creating less clutter in the console).
Something to be aware of, is that sourcing functions in files makes them available for scripts to use. What does this mean? Imagine you are trying to troubleshoot a function that is called from a script. You need to source the file containing the function, to make the changes available in the function be used when that line in the script is then run.
A further aspect of this is that you can source functions from your scripts. I use this code to automatically source all of the functions in a directory, which makes it easy to run a long script with a single run:
# source our functions
code.dir <- "c:\temp"
code.files = dir(code.dir, pattern = "[.r]")
for (file in code.files){
source(file = file.path(code.dir,file))
}
Sometimes, for reasons I don't understand, you will get different behavior depending on whether you select all the lines of code and press the run the button or go to code menu and chose 'source.' For example, in one specific case, writing a gplot to a png file worked when I selected all my lines of code but the write failed to when I went to the code menu and chose 'source.' However, if I choose 'Source with Echo,' I'm able to print to a png file again.
I'm simply reporting a difference here that I've seen between the selecting and running all your lines and code and going to code menu and choosing 'source,' at least in the case when trying to print a gplot to a png file.
An important implication of #AndyClifton's answer is:
Rstudio breakpoints work in source (Ctrl-Shift-S) but not in run (Ctrl-Enter)
Presumably the reason is that with run, the code is getting passed straight into the console with no support for a partial submission.
You can still use browser() though with run though.
print() to console is supported in debugSource (Ctrl-Shift-S) as well as run.
The "run" button simply executes the selected line or lines. The "source" button will execute the entire active document. But why not just try them and see the difference?
I also just discovered that the encoding used to read the function sourced can also be different if you source the file or if you add the function of the source file to your environment with Ctrl+Enter!
In my case there was a regex with a special character (µ) in my function. When I imported the function directly (Ctrl+Enter) everything would work, while I had an error when sourcing the file containing this function.
To solve this issue I specified the encoding of the sourced file in the source function (source("utils.R", encoding = "UTF-8")).
Run will run each line of code, which means that it hits enter at the beginning of each line, which prints the output to the console. Source won't print anything unless you source with echo, which means that ggplot won't print to pngs, as another posted mentioned.
A big practical difference between run and source is that if you get an unaccounted for error in source it'll break you out of the code without finishing, whereas run will just pass the next line to the console and keep going. This has been the main practical difference I've seen working on cleaning up other people's scripts.
When using RSTudio u can press the run button in the script section - it will run the selected line.
Next to it you have the re - run button, to run the line again. and the source button next to it will run entire chuncks of code.
I found a video about this topic:
http://www.youtube.com/watch?v=5YmcEYTSN7k
Source/Source with echo is used to execute the whole file whereas Run as far as my personal experience goes executes the line in which your cursor is present.
Thus, Run helps you to debug your code. Watch out for the environment. It will display what's happening in the stack.
To those saying plots do not show. They won't show in Plots console. But you can definitely save the plot to disc using Source in RStudio. Using this snippet:
png(filename)
print(p)
dev.off()
I can confirm plots are written to disc. Furthermore print statements are also outputted to the console

copy file into another file in prolog

Good morning/evening
how can I write something in a file and then copy its content into the current file?
for example I consult file1.pro then I have rule write something in file2.pro , after this rule finish its job I want append the content of the file2.pro int file1.pro .
when I tried to append into file1.pro directly , the data appear like undefined symbols ,I don't know why
please hellp me
thank you.
Specifics of the solution might depend on the Prolog dialect. Here I am using SWI-Prolog. SWI-Prolog allows you to open a file with open(SrcDest, Mode, Stream), where SrcDest will be your file name, Mode is read/write/append/update, and Stream is the "file descriptor" the system will return. The manual clarifies difference between appending and updating as follows: "Mode append opens the file for writing, positioning the file-pointer at the end. Mode update opens the file for writing, positioning the file-pointer at the beginning of the file without truncating the file."
To copy from one stream to another you should use copy_stream_data(Stream1,Stream2).
Finally, you should close the streams, otherwise the output file will be empty.
Putting everything together gives
copy(File1,File2) :- open(File1,read,Stream1), open(File2,write,Stream2),copy_stream_data(File1,File2),close(File1),close(File2).
If you need to rewrite the second file, just use update/append mode.

Resources