Using Java library in Clojure - clojure

I am writing an image-upload system using Clojure. I use a Java library javadoc to make a thumbnail image.
(defn upload-file [file]
(let [file-name (file :filename)
actual-file (file :tempfile)
image (Thumbnails/fromFiles (java.util.ArrayList. [actual-file]))
image-jpg (.outputFormat image "jpg")
thumb (.forceSize image-jpg 250 150)]
...
My code changes both thumb var and image-jpg var, but I want to have two separate images (one with normal size and thumb). How do I make a copy of image-jpg to change its size?

You could just try doing the same operation again on the same actual-file. And create a function that you call twice:
(defn make-thumb [width height file]
(let [image (Thumbnails/fromFiles (java.util.ArrayList. [file]))
image-jpg (.outputFormat image "jpg")]
(.forceSize image-jpg width height)))
Another way would be to deep copy image. See How do you make a deep copy of an object in Java? and do the same thing in Clojure.

Related

How to define a column format for entire org-mode file

PROBLEM:
For some reason when I copy and paste a column property format header in a org-mode file, such as
#+COLUMNS: %25ITEM(Task) %5TODO(To-do) %1PRIORITY %10TAGS
to change the global properties of the trees of the file, it doesn't changes the default format. i.e. first column remains named as ITEM and not task (for this example). I've tried other changes and they are never applied.
TESTS:
I have reloaded the column mode without success.
Tried to add/delete columns with M-S-right/left and it updates the header/columns. But obviously I don't want to have to do it manually.
Here is my .emacs setup for org mode
;; Open ODT files with libre office
(defun my-org-export-to-odt-and-open ()
(interactive)
(org-open-file (org-odt-export-to-odt)))
(define-key global-map "\C-cl" 'org-store-link)
(define-key global-map "\C-ca" 'org-agenda)
(setq org-log-done t)
(setq org-support-shift-select 't)
(setq org-agenda-include-diary t)
(setq org-src-fontify-natively t)
(setq org-src-tab-acts-natively t)
Just for completeness: when in-buffer settings like #+COLUMNS: ... are changed, the change does not take effect until you press C-c C-c on any in-buffer setting (although doing it on the just-changed setting is probably the most natural).
Alternatively, if you save and kill the buffer and visit the file anew, or
if you save the buffer and revert the buffer from the file, the change should take effect (the OP's lack of success with this notwithstanding - I'm not sure why it did not work).
The relevant documentation is In-buffer settings.

How to add to lists defined with c-lang-defconst?

I'm trying to add a C++ mode hook to add C++11 keywords to the appropriate keyword lists (which are defined in lisp/progmodes/cc-langs.el in the Emacs source code). As a minimal example, I started with c-modifier-kwds and tried adding the following to my .emacs file:
(add-hook 'c++-mode-hook
(lambda ()
(c-lang-defconst c-modifier-kwds
c++
(append '("thread_local" "noexcept")
(c-lang-const c-modifier-kwds))))
t)
(I copied the c-lang-defconst statement from this C++11 mode implementation. However, I don't want to create a whole new mode, I just want to add to the regular C++ mode.)
That didn't work (I didn't really expect it to, either). I also tried the following:
(add-hook 'c++-mode-hook
(lambda ()
(setq (c-lang-defconst c-modifier-kwds)
(append '("thread_local" "noexcept")
(c-lang-const c-modifier-kwds))))
t)
That didn't work either.
What's the right way to do this?
If there is no right way, what's a good hackish way to do this?

Org-mode library of babel: can't #'CALL what I define

I want to use the library of babel of org-mode to define a new Clojure function that would be accessible to any org-mode document.
What I did is to define that new function in a named codeblock like this:
#+NAME: foo
#+BEGIN_SRC clojure
(defn foofn
[]
(println "foo test"))
#+END_SRC
Then I saved that into my library of bable using C-c C-v i, then I selected the org file to save in the library and everything looked fine.
Then in another org file I wanted to call that block such that it becomes defined in that other context. So I used the following syntax:
#+CALL: foo
However when I execute that org file I am getting the following error:
Reference `nil' not found in this buffer
Which tell me that it can't find that named block.
Any idea what I am doing wrong? Also once it works, is there a way to add new parameters to that code block when called using #+CALL:?
Finally, where is supposed to be located my library of babel? (how to know if it got properly added or not?)
I am obviously missing some core information that I can't find in the worg documentation.
Try:
#+CALL: foo()
Also, check the value of the variable org-babel-library-of-babel to make sure that the C-c C-v i worked properly.

Rendering a label doesn't work unless the screen function evaluates to it

I'm trying to render! a simple label on the on-render screen function, but it doesn't work. All I get is a blank screen.
It took me some time but I finally found that this happens when I return something else but the label. For example, this works:
(defscreen main-screen
:on-show
(fn [screen entities]
(update! screen :renderer (stage))
[])
:on-render
(fn [screen _]
(clear!)
(render! screen [(label "Hello, world!" (color :white))])))
This doesn't (all that's changed is the last line on on-render):
(defscreen main-screen
:on-show
(fn [screen entities]
(update! screen :renderer (stage))
[])
:on-render
(fn [screen _]
(clear!)
(render! screen [(label "Hello, world!" (color :white))])
["I just wanna pass my own data here, ugh.."]))
For testing, I replaced the label with texture and it did work.
What's causing this? it seems so arbitrary (the label I evaluate to in the first example gets completely ignored anyway! so why do I have to pass it? I don't get what's going on).
P.S. creating the label on every frame is intentional in this case, and I know I could just set it from one I make in on-show, but I tried to be a bit more dynamic. I also tried to put a Thread/sleep after render! to make sure I have a visible time-window between rendering & clearing.
I'm a complete beginner I'd like to know why the above doesn't work in addition to how can I fix it?
checkout the play-clj's defscreen-doc , in particular:
All functions take a screen map and entities vector as arguments, and
return the entities list at the end with any desired changes. If a
function returns nil, the entities list is not changed.
You neither return nil nor the (optionally changed) entities, but a vector with a string.
I expect that is the reason for the non-functioning code - haven't tested it though.
What is your intention with your "own data"?

Can I clean the repl?

I have played with a lot of code in a repl console, how can I clear it? I would like a fresh one without restarting it. Can that be done?
If you want to clear the current namespace of all temporary variables and functions you declared you can use this one liner (or make a function of it) :
(map #(ns-unmap *ns* %) (keys (ns-interns *ns*)))
or
(ns myutil)
(defn ns-clean
"Remove all internal mappings from a given name space or the current one if no parameter given."
([] (ns-clean *ns*))
([ns] (map #(ns-unmap ns %) (keys (ns-interns ns)))))
(ns mytest)
... make loads of junk ...
(myutil/ns-clean)
... great!!! I can now make all new junk ...
It does not claim to give you a squeaky clean namespace, just one with less of the junk which usually accumulates in a typical repl session.
Use with caution : do not pull the rug from under your feet!
If you are running the repl through a terminal window (eg: Terminal.app on MacOS or xterm/aterm/urxvt etc on linux) then you can type Control-L and it should clear the terminal window and give you a new repl prompt. However all macros/atoms you previously defined are still going to be in memory, so this is just a "Cosmetic" clear.
In EMACS/slime REPLs
C-c C-o clears the last output (in case you've typed something that gave a very long answer)
C-c M-o clears the whole thing
In GNOME terminals, you've got a menu option Terminal/Reset and Clear
The shorcut to clean the whole buffer : C-u C-c C-o
The shortcut to clean the last output : C-c C-o
Note, the old way was : C-c M-o
Also, cider-repl-clear-buffer (which is bound to C-ENTER s-c on my machine)
If you are using Emacs + nREPL, you can either:
Run Mx nrepl-clear-buffer or
Run Cc Mo
If the key binding is not enabled, run Mxnrepl-interaction-mode to enable it. You can find other useful shortcuts in nrepl.el and/or customize the key bindings to fit your needs.
Note: you can find all nREPL's key bindings in your system by running M-x v nrepl-mode-map and following the nrepl.el link.
I use the Emacs command cider-repl-clear-buffer via M-x. One might also use cider-repl-clear-output with a prefix argument: C-u C-c C-o.
It depends what you mean by 'clean'. To remove all namespaces within a 'package' you can use:
(mapv remove-ns
(map symbol
(filter #(.startsWith % "org.mycompany")
(map str (all-ns)))))
For users of the Cursive IDE plugin for IntelliJ IDEA who end up here, like me:
You can "cosmetically" clear the REPL (keeping your def'd symbols etc.) by clicking on this icon at the top of the REPL frame:
There's no default key binding for this, but you can easily add one by opening your preferences, navigating to Keymap > Plugins > Cursive, and adding a binding for "Clear output for current REPL".
Alternatively, you can right-click in the editor and access "Clear output for current REPL" via the REPL commands.