In ruby I frequently use File.expand_path(File.dirname(__FILE__)) for loading config files or files with test data. Right now I'm trying to load some html files for a test in my clojure app and I can't figure out how to do it without hard coding the full path to the file.
edit:
I'm using leinigen if that helps in any way
ref: __FILE__ is a special literal which returns the filename (including any path) given to the program when executed. see (rubydoc & perldata)
*file*
API Reference (add *file* to the url)
Here is one way to replicate that in Clojure:
(defn dirname [path]
(.getParent (java.io.File. path)))
(defn expand-path [path]
(.getCanonicalPath (java.io.File. path)))
Then your Ruby line File.expand_path(File.dirname(__FILE__)) in Clojure would be this:
(expand-path (dirname *file*))
See Java interop docs for .getParent & .getCanonicalPath.
NB. I think *file* always returns the absolute (though not canonical) pathname/filename in Clojure. Whereas __FILE__ returns the the pathname/filename provided at execution. However I don't think these difference should effect what your trying to do?
/I3az/
Neither of the 9 point solutions is correct. *file* gives you a file relative to the classpath. Using .getCanonicalPath or .getAbsolutePath on *file* will give you a nonexistant file. As pointed out in this old thread, you need to use ClassLoader to resolve *file* correctly. Here's what I use to get the parent directory of the current file:
(-> (ClassLoader/getSystemResource *file*) clojure.java.io/file .getParent)
Based on user83510's answer above, the full answer is:
(def path-to-this-file
(clojure.string/join "/" [(-> (ClassLoader/getSystemResource *file*) clojure.java.io/file .getParent) (last (clojure.string/split *file* #"/"))]))
It ain't pretty but it works :P
Related
I'm just getting started with clojure and core.typed, and keep running into the following error when I evaluate (check-ns 'stocks.db) with the following code:
(ns stocks.db
(:gen-class)
(:require [clojure.core.typed :refer [ann HMap]]))
(use '[datomic.api :only [q db] :as d])
(ann break-me [String -> (HMap)])
(defn break-me
"Do I break cored.typed?"
[]
{:db/id #db/id[:db.part/db]})
ExceptionInfo No reader function for tag id clojure.core/ex-info (core.clj:4327)
(break-me) evaluates to {:db/id #db/id[:db.part/db -1000000]} as you'd expect.
Similarly to this unanswered question, the offending term is definitely #db/id.
Thanks for any help.
I'm new to Clojure and Datomic but, I think you need to generate your temporary id with the tempid function that's part of the Datomic API (see http://docs.datomic.com/clojure/index.html#datomic.api/tempid).
So it'd look like this:
(ns stocks.db
(:gen-class)
(:require [clojure.core.typed :refer [ann HMap]]))
(use '[datomic.api :only [q db] :as d])
(ann break-me [String -> (HMap)])
(defn break-me
"Do I break cored.typed?"
[]
{:db/id (tempid :db.part/db)})
I'm having similar issues with clojure.edn/read-string, I haven't quite figured it out. Hope that works for you.
UPDATE:
In this code: https://github.com/Datomic/codeq/blob/master/src/datomic/codeq/core.clj the Datomic folks seem to be doing just what you are. So while in your case I think using tempid will work, I think there's a deeper issue, and it seems like it has to do with the literal #db/id.
UPDATE:
Here: https://github.com/Datomic/day-of-datomic/blob/master/samples/literals_vs_code.clj using tempid rather than literals is recommended.
UPDATE:
I've found a solution that also resolves my issue. I was trying to read in my schema from a file: schema.edn using clojure.edn/read-string after reading the contents as a string using clojure.core/slurp and got similar error complaining about the #db/id literal.
I implemented the read-all and transact-all functions found here: https://github.com/Datomic/day-of-datomic/blob/053b3bd983d165b8fa7c0c039712fb1cb75eddf3/src/datomic/samples/io.clj and it works fine. It seems like there are some sneaky issues with using the reader to parse #db/id literals, still haven't figured out exactly why.
UPDATE:
This is using a Clojure feature called "tagged literals" which allows you to define a map of "data readers" in a file named data_readers.clj at the root of your classpath that should contain a map of tag names and vars that will parse the arguments to the tag.
So with this data_readers.clj file:
{minutes my.project/to-minutes
seconds my.project/to-seconds}
You can do this:
#minutes 3
#seconds 4
You can read about tagged literals at the bottom of this page: http://clojure.org/reader.
However, I still have not figured out the reason for the issue with the #db/id tagged literal. I've posted a question on the Clojure group forum here: https://groups.google.com/forum/#!topic/datomic/Fi7iXps2npw. Update: Ben Kamphaus provides a nice explanation there.
I use Emacs+AUCTex for writing LaTeX documents. I have specific needs so my typical preamble is quite long. Today, I have a .tex file with only this preamble (a template so) and I use C-x C-w to write a new file from this template. It isn't the best solution because my template localization could be far away from the new file.
So is there a way to call LaTeX templates in Emacs in another (shorter) way?
EDIT : auto-insert-mode offers a way to achieve what I want but it doesn't put automatically my template (LaTeX preamble) when I create a .tex file. I have to launch M-x auto-insert. How can I automatize this based on the file extension?
At the end of VirTeX-common-initialization (essentially) TeX-master-file is added to find-file-hooks. This is the source for the %%% Local Variables: %%% mode: latex %%% TeX-master: t %%% End: stuff. (Note, that VirTeX-common-initialization is the first thing in LaTeX-common-initialization which is called in TeX-latex-mode being an alias for latex-mode.)
To get ride of the automagically added comments you can remove the hook:
(add-hook 'TeX-mode-hook '(lambda ()
(remove-hook 'find-file-hooks (car find-file-hooks) 'local)))
That looks like a hack. But adding TeX-master-file is quite hard-coded without user-options. So, it seems to me that you have no other chance.
After that correction the auto-insert stuff works automagically.
(At least for me.)
But, I have replaced the entries in auto-insert-alist. Meaning, instead of
(define-auto-insert "\\.tex$" "my-latex-template.tex")
I have something like that:
(let ((el (assoc 'latex-mode auto-insert-alist)))
(if el
(setcdr el "/c/temp/autoinsert.tex")
(define-auto-insert "\\.tex$" "/c/temp/autoinsert.tex")))
Maybe, that is important, maybe not. I've got to get home now and I cannot further investigate that.
You're probably looking for auto-insert-mode. This is orthogonal to AUCTeX - for instance, I use it to insert a class-template for .java files.
Put the following in your .emacs file:
(auto-insert-mode)
;; *NOTE* Trailing slash important
(setq auto-insert-directory "/path/to/template/directory/")
(setq auto-insert-query nil)
(define-auto-insert "\\.tex$" "my-latex-template.tex")
Of course, you could make the regular expression used as the first argument to define-auto-insert more complex e.g. to insert different preambles depending on the working directory.
I adapted this code from an example from the EmacsWiki where you can also find additional information.
This is the simplest solution I can think of:
(defun insert-latex-template()
(when (= (point-max) (point-min))
(insert-file "/path/to/your/template/file")))
(add-hook 'latex-mode-hook 'insert-latex-template)
My code is a mess many long lines in this language like the following
(defn check-if-installed[x] (:exit(sh "sh" "-c" (str "command -v " x " >/dev/null 2>&1 || { echo >&2 \"\"; exit 1; }"))))
or
(def Open-Action (action :handler (fn [e] (choose-file :type :open :selection-mode :files-only :dir ListDir :success-fn (fn [fc file](setup-list file)))) :name "Open" :key "menu O" :tip "Open spelling list"))
which is terrible. I would like to format it like so
(if (= a something)
(if (= b otherthing)
(foo)))
How can I beautify the source code in a better way?
The real answer hinges on whether you're willing to insert the newlines yourself. Many systems
can indent the lines for you in an idiomatic way, once you've broken it up into lines.
If you don't want to insert them manually, Racket provides a "pretty-print" that does some of what you want:
#lang racket
(require racket/pretty)
(parameterize ([pretty-print-columns 20])
(pretty-print '(b aosentuh onethunoteh (nte huna) oehnatoe unathoe)))
==>
'(b
aosentuh
onethunoteh
(nte huna)
oehnatoe
unathoe)
... but I'd be the first to admit that inserting newlines in the right places is hard, because
the choice of line breaks has a lot to do with how you want people to read your code.
I use Clojure.pprint often for making generated code more palatable to humans.
it works well for reporting thought it is targeted at producing text. The formatting built into the clojure-mode emacs package produces very nicely formatted Clojure if you put the newlines in your self.
Now you can do it with Srefactor package.
Some demos:
Formatting whole buffer demo in Emacs Lisp (applicable in Common Lisp as well).
Transform between one line <--> Multiline demo
Available Commands:
srefactor-lisp-format-buffer: format whole buffer
srefactor-lisp-format-defun: format current defun cursor is in
srefactor-lisp-format-sexp: format the current sexp cursor is in.
srefactor-lisp-one-line: turn the current sexp of the same level into one line; with prefix argument, recursively turn all inner sexps into one line.
Scheme variants are not as polished as Emacs Lisp and Common Lisp yet but work for simple and small sexp. If there is any problem, please submit an issue report and I will be happy to fix it.
I would like to use doxygen to generate code documentation (of functions) in .c or .cc files with Emacs. I found doxymacs, but it seems not to be maintained anymore (latest version 2007) and I also did not find a way to update the documentation of a function if I change the name of one of the parameters of the function ("unfortunately", I'm used to the great Roxygen for .R scripts which can do all nice things, even inserting a documentation right before a function when the point is somewhere in the function).
I found this, but it seems not very useful. However, there is an example here how to use yasnippets. Has anyone written a yasnippet for doxygen headers? Still, it would not update the parameters if the function name changes. Is there any "better" way to work with doxygen in Emacs? I would assume there are quite a lot of C/C++ programmers that work with Emacs and I would guess that there should be a good tool/approach for code documentation.
Update
I also found this. It's purely based on yasnippet (haven't tried it yet, though).
I use the following:
# -*- mode: snippet -*-
# name: cc-doxygen
# key: dox
# type: command
# contributor: Jonathan Kotta <jpkotta#gmail.com>
# --
(let* ((next-func-alist (doxymacs-find-next-func))
(func-name (cdr (assoc 'func next-func-alist)))
(params-list (cdr (assoc 'args next-func-alist)))
(return-name (cdr (assoc 'return next-func-alist)))
(snippet-text "")
(idx 1))
(setq snippet-text (format "/**\n * ${1:%s}\n * \n" func-name))
(setq idx 2)
(dolist (param params-list)
(unless (string= param "this")
(setq snippet-text (concat snippet-text
(format " * \\param %s ${%d:}\n" param idx)))
(setq idx (+ 1 idx))))
(when (and return-name (not (string= return-name "void")))
(setq snippet-text (concat snippet-text
(format " * \\return ${%d:%s}\n" idx return-name))))
(setq snippet-text (concat snippet-text " */"))
(yas/expand-snippet snippet-text))
1. Generate doxygen from Emacs
Which kind of tool are you using for compiling? CMake? Autotools? Scons?
Once I worked with CMake and we created a target to generate documentation with doxygen.
Something like this.
Then you will have to compile like this:
make doc
But this only covers the first part of your question, the documentation before each function it will be made manually. I will try to integrate this solution with yasnippet.
I have been struggling to find an answer or develop a solution. I am trying to figure out how to make code that makes code in Clojure. For my first feat, I want a function that will print to stdout the name of the symbol and its value, useful for debugging. Example:
(def mysymbol 5)
(debugging-function mysymbol)
mysymbol: 5
Does that make sense? Thanks for your help.
Post Discussion Update
Here is the answer from #amalloy:
(defmacro ?
"A useful debugging tool when you can't figure out what's going on:
wrap a form with ?, and the form will be printed alongside
its result. The result will still be passed along."
[val]
`(let [x# ~val]
(prn '~val '~'is x#)
x#))
So:
(? myvariable)
You can see a simple version of this that I wrote on github. The main point is that you can't do this with a function, but with a macro it's simple enough - you just have to get your quoting and unquoting right.