readline history not working - command-line

Okay, so Chicken-scheme has this great readline egg that can be used to add history, tab completion, and other sane features to the csi command-line environment. However, I can't seem to get it to work correctly, that is it only works the first time I start csi up after creating a new history file.
I've checked the documentation for the egg and other similar eggs such as linenoise and parley, but neither of them offer tab-completion or any advice on how to address this shortcoming of readline.
Here's the code I'm working with:
(current-input-port (make-gnu-readline-port))
(gnu-history-install-file-manager
(string-append (or (get-environment-variable "HOME") ".") "/.csi_history"))
NOTE
While writing this I think I may have figured out the problem or at least part of the problem. The problem, I think, lies in the fact that I'm installing a history file; as in it only works for the first installation?
However, my attempt to cook my own readline file-manager setup makes it so that gnu readline never writes to the history file, however, it will read from it.
(current-input-port (make-gnu-readline-port))
(let ((histfile (string-append (or (get-environment-variable "HOME") ".")
"/.csi_history")))
(and (file-exists? histfile) (gnu-readline-read-history histfile))
(gnu-readline-append-history histfile))
Has anyone else encountered this problem?

Turns out that in my ~/.zshrc file I had an alias for csi that called csi with rlwrap:
alias csi='rlwrap csi'
Calling rlwrap on csi was causing csi to use rlwrap's history instead of the built-in history provided by the readline egg.

readline is deprecated in chicken scheme solution is to install linenoise
and put this in your .csirc:
(import linenoise)
(current-input-port (make-linenoise-port))
(set-history-length! 300)
(load-history-from-file ".linenoise-history")
(let loop ((expr (linenoise "> ")))
(cond ((equal? expr "bye")
(save-history-to-file ".linenoise-history")
"Bye!")
(else
(display (eval (read (open-input-string expr))))
(newline)
(history-add expr)
(loop (linenoise "> ")))))

Related

Racket flymake-mode for emacs

Is it possible to make flymake-mode be aware of syntax (or other) errors in racket files like it done for example for erlang or python? I'm using geiser-mode for racket, if it is matters.
It's probably very easy, since Racket spits out warning messages in a standard line:column format.
You don't even need to invoke the compiler -- it's enough to just run the code via racket the-file. But as a semi-side-note, an even better command-line to use is racket -qf the-file. The thing about running the code as above is that it will actually ... run it. More specifically, it loads the module definition and then invokes it. Using -f it will just load the definition but not invoke it and therefore the runtime is not executed. Note that this assumes that the file is one that has just a module, which is what you get for all files that start with a #lang.
Update: I tried it, and indeed it was easy to set things up. I've posted this code on the mailing list:
(defun flymake-racket-init ()
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-inplace))
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name))))
(list "racket" (list "-qf" local-file))))
(push '("\\.rkt\\'" flymake-racket-init)
flymake-allowed-file-name-masks)

Emacs ediff error "no newline at end of file"

On Debian Wheezy, Emacs 23.3.1, running ediff-files with a file that is missing a newline at the end results in the error \ No newline at end of file (I hope that's the correct translation; it's German \ Kein Zeilenumbruch am Dateiende. on my computer.)
Is it possible to have just a warning instead, so that I can see the diff and work on it (and fix the missing newline)? It's just a bit tedious to first have ediff fail, then open the file, add the newline, ediff again.
Try changing the value of the variable ediff-diff-ok-lines-regexp to include the German text ("Kein Zeilenumbruch am Dateiende"):
(setq ediff-diff-ok-lines-regexp
(concat
"^\\("
"[0-9,]+[acd][0-9,]+\C-m?$"
"\\|[] "
"\\|---"
"\\|.*Warning *:"
"\\|.*No +newline"
"\\|.*missing +newline"
"\\|.*Kein +Zeilenumbruch +am +Dateiende"
"\\|^\C-m?$"
"\\)"))
Update: Looking at the source code, it does seem that Ediff doesn't make any attempt to deal with the issue of localization of messages from diff. It should also be possible to work around this by wrapping diff in a shell script, e.g:
#!/bin/bash
LANG=C diff $*
..then customising the ediff-diff-program to call the wrapper instead:
(setq ediff-diff-program "~/bin/my-diff.sh")
Other code in the Emacs source directory lisp/vc does seem to handle this, for example vc-hg-state:
(defun vc-hg-state (file)
"Hg-specific version of `vc-state'."
...
(with-output-to-string
(with-current-buffer
standard-output
(setq status
(condition-case nil
;; Ignore all errors.
(let ((process-environment
;; Avoid localization of messages so we
;; can parse the output.
(append (list "TERM=dumb" "LANGUAGE=C")
process-environment)))
...
It seems a bit strange that Ediff doesn't also do this, but perhaps I'm missing something.
Ok, I found out what's wrong, and sadly, it's quite obvious: my environment has LANG=de, therefore when Emacs invokes diff, the warning message is returned in German as well, and Emacs, not recognising this “unkown” message, fails.
Starting emacs with LANG=C emacs works around this problem. However, I consider it a (quite silly) bug of emacs to make assumption on the user's language being English.

How to determine whether a package is installed in elisp?

I want to customize environment while the specific package is installed properly. How to check whether some package is installed in elisp?
Something like this?:
(if (require 'ecb)
(progn (setq ....))
(message "ECB not installed!"))
tripleee's answer is a handy example of error handling, but unnecessary in this instance.
(when (require 'some-library nil 'noerror)
do-things)
That 'noerror can be any non-nil value, but of course it's more descriptive this way. I often see :noerror used as well, but I've no idea if there's any particular advantage to using a keyword argument over a symbol (comments, anyone? I'm quite interested to know).
require is a built-in function in C source code.
(require FEATURE &optional FILENAME NOERROR)
If feature FEATURE is not loaded, load it from FILENAME.
If FEATURE is not a member of the list features, then the feature
is not loaded; so load the file FILENAME.
If FILENAME is omitted, the printname of FEATURE is used as the file name,
and load will try to load this name appended with the suffix .elc or
.el, in that order. The name without appended suffix will not be used.
See get-load-suffixes for the complete list of suffixes.
If the optional third argument NOERROR is non-nil,
then return nil if the file is not found instead of signaling an error.
Normally the return value is FEATURE.
The normal messages at start and end of loading FILENAME are suppressed.
The (require) will throw an error if it fails. That should really be all you need.
If you want to configure ECB's behavior only when it is available, look primarily into adding stuff to ecb-hook -- this is the normal way to configure an Emacs package conditionally.
If no hook is available, or you want to roll it by hand for some reason, try something like
(eval-after-load 'ecb '(setq ecb-be-more-like-better-yes-p t))
If you really really want to roll this by hand, you can trap the error from a failed require like this:
(condition-case nil
(progn
(require 'ecb)
(setq ecb-be-more-like-better-yes-p t) )
(file-error (message "ECB not available; not configuring") ))
Note that the condition-case will catch any file-error from inside the progn so you want to make sure you don't do any other file operations inside. Ultimately you may want to put just the require inside a condition-case and use that as the condition for your original if form, but this is already getting out of hand ...
(if (condition-case nil (require 'ecb) (error nil))
(setq ecb-be-more-like-better-yes-p t)
(message "ECB not available; not configuring") )
Why not using "featurep"?
(featurep FEATURE &optional SUBFEATURE)
Return t if FEATURE is present in this Emacs.
(if (featurep 'ecb)
(message "ECB is there!"))
For people wondering how to check if a package.el package is installed, use package-installed-p.
Four years late on this question, but... here's a simple macro that will do this for you.
(defmacro when-feature-loaded (feature &rest body)
"Executes BODY if and only if FEATURE is loaded."
(declare (indent defun))
`(when (featurep ,module)
#,body))
For example:
(when-feature-loaded 'foo
(message "foo is loaded!"))
Here's another version with an "else" case, in case you need to handle that as well.
(defmacro if-feature-loaded (module then-form else-form)
"Executes THEN-FORM if and only if MODULE is already loaded, otherwise executes ELSE-FORM."
(declare (indent 2))
`(if (featurep ,module)
,then-form
,else-form))
The simplest answer is to use require and eval-after-load as stated in other answers.
However that is not always convenient, such as in a function called by a mode hook that wants to activate another minor mode but only if the package for it is installed. In this case featurep is relevant.
Emacs packages increasingly use autoload to improve startup time. If you test for the presence of a package by using require then you are wearing the cost of loading the file(s). If using ELPA/MELPA/Marmalade packages (available by default since version 24) then many packages may be available in an as-yet unloaded state, but a package foo with autoloads will provide a feature foo-autoloads. I wrote a function that is useful for testing whether a package is available in terms of already being loaded or set to autoload.
(defun autofeaturep (feature)
"For a feature symbol 'foo, return a result equivalent to:
(or (featurep 'foo-autoloads) (featurep 'foo))
Does not support subfeatures."
(catch 'result
(let ((feature-name (symbol-name feature)))
(unless (string-match "-autoloads$" feature-name)
(let ((feature-autoloads (intern-soft (concat feature-name "-autoloads"))))
(when (and feature-autoloads (featurep feature-autoloads))
(throw 'result t))))
(featurep feature))))
N.B.: Using (require ..) will load the package, which defeats the benefit of autoloads (deferred loading).
I use this in my code to configure functions which are autoloaded:
(when (fboundp 'some-function)
...)
Example
I have this in my init.el:
(when (fboundp 'ace-select-window)
(keymap-global-set "M-s-u" 'ace-select-window))
If you have ace-window installed, you can test this yourself by copying it into your own init file, restarting Emacs, and running:
(autoloadp (symbol-function 'ace-select-window))
It should return T. Then run the command, and execute that snippet again; it should return nil.
(newbie tip: Hit altshift:, then enter that, and press enter)
This avoids loading your package just to configure it (which is the point of autoloading).
(newbie tip nr 2: using fbound is for functions; use boundp for values)

Emacs Clojure mode without paredit

I'm using the Clojure mode package from ELPA. Otherwise everything is fine, but I just can't stand paredit mode. I can't seem to turn it off easily, now I just disable it for every buffer I open. I tried setting this variable to nil:
(setq clojure-enable-paredit nil)
But paredit still appears. Any ideas?
Not an answer to your actual question, but give paredit mode a chance. I, too, was really annoyed with it automatically closing my parens, and refusing to delete just a single paren for me.
But doing this enables it to be certain at all times that the buffer is a well-balanced sexp, so it can perform many useful sexp-oriented tasks for you instead of just text-oriented tasks. For example, I use the following all the time:
M-( to wrap a sexp with a new one, eg turn (map f some-list) into (doto (map f some-list) println)
C-) to "slurp" another sexp into the current one, eg turn (let [x 10]) (println x) into (let [x 10] (println x))
M-<UP> and/or M-r to pull the sexp at point a level "higher" in the source tree, destroying the thing that was wrapping it, eg to turn (first (map f some-list)) into (map f some-list) or (first some-list)
There are zillions of useful features like this, that let you start editing code instead of text. And while there are plenty of excellent Lisp hackers who don't like paredit mode, I advise you not to decide against it before you realize the awesome stuff it can do for you.
Found one trick that works. Before the elpa packages are loaded in init.el, add this hook to clojure mode:
(add-hook 'clojure-mode-hook (lambda () (paredit-mode nil)))
For what it's worth, I use clojure-mode through ELPA too, and it doesn't imply paredit. Maybe just uninstall it? I find clojure-mode, slime and slime-repl are the only packages I need to install on a clean EMACS to get clojure/swank/slime working.
I only tested this:
http://www.learningclojure.com/2010/08/clojure-emacs-swank-slime-maven-maven.html
a few weeks ago, and it still works fine.

A gentle tutorial to Emacs/Swank/Paredit for Clojure

I am moving to Emacs to work on Clojure/Lisp.
What is all the information I need to setup on Emacs to be able to do the following?
automatic matching/generation of corresponding closing brackets
autoindent Lisp/Clojure style, not C++/Java style
Syntax highlighting
Invoking REPL
To be able to load a part of code from file into the REPL and evaluate it.
It would be great if I could also get the list of commands to get these things after setting things up on Emacs.
[Edit from non-author: this is from 2010, and the process has been significantly simplified since May 2011. I'll add a post to this answer with my setup notes as of Feb 2012.]
You'll need to put together a few pieces: Emacs, SLIME (which works perfectly well with Clojure -- see swank-clojure), swank-clojure (the Clojure implementation of SLIME's server counterpart), clojure-mode, Paredit and, of course, the Clojure jar for a start, then perhaps some extras among which Leiningen would perhaps be the most notable. Once you do set it all up, you'll have -- within Emacs -- all the workflow / editing features you mention in the question.
Basic setup:
The following are to great tutorials which describe how to set all of this up; there's more on the Web, but some of the others are quite outdated, whereas these two seem to be ok for now:
in which are found tricks of the trade concerning clojure authorship post on Phil Hagelberg's blog; Phil maintains swank-clojure and clojure-mode, as well as a package called the Emacs Starter Kit which is something any newcomer to the Emacs world would be well-advised to have a look at. These instructions seem to have been brought up to date with recent changes to the infrastructure; in case of doubt, look for additional information on Clojure's Google group.
Setting up Clojure, Incanter, Emacs, Slime, Swank, and Paredit post on the blog of the Incanter project. Incanter is a fascinating package providing an R-like DSL for statistical computations embedded right into Clojure. This post will be useful even if you don't plan on using -- or even installing -- Incanter.
Putting it all to work:
Once you set up all of this stuff, you could try and start using it right away, but I would strongly advise you to do the following:
Have a look at SLIME's manual -- it's included in the sources and is actually very readable. Also, there's absolutely no reason why you should read the whole 50-page monster manual; just have a look around to see what features are available.
Note: the autodoc feature of SLIME as found in the latest upstream sources is incompatible with swank-clojure -- this problem won't come up if you follow Phil Hagelberg's recommendation to use the ELPA version (see his aforementioned blog post for an explanation) or simply leave autodoc off (which is the default state of things). The latter option has some added appeal in that you can still use the latest SLIME with Common Lisp, in case you use that as well.
Have a look at the docs for paredit. There are two ways to go about this: (1) look at the source -- there's a huge amount of comments at the top of the file which contain all the information you're likely to need; (2) type C-h m in Emacs while paredit-mode is active -- a buffer will pop up with information on the current major mode followed by information on all active minor modes (paredit is one of those).
Update: I've just found this cool set of notes on Paredit by Phil Hagelberg... That's a link to a text file, I remember seeing a nice set of slides with this information somewhere, but can't seem to find it now. Anyway, it is a nice summary of how it works. Definitely take a look at it, I can't live without Paredit now and this file should make it very easy to start using it, I believe. :-)
In fact, the C-h m combination will tell you about all keybindings active at the SLIME REPL, in clojure-mode (you'll want to remember C-c C-k for sending the current buffer off for compilation) and indeed in any Emacs buffer.
As for loading the code from a file and then experimenting with it at the REPL: use the aforementioned C-c C-k combination to compile the current buffer, then use or require its namespace at the REPL. Next, experiment away.
Final notes:
Be prepared to have to tweak things for a while before it all clicks. There's a lot of tools involved and their interactions are mostly fairly smooth, but not to the point where it would be safe to assume you won't have to make some adjustments initially.
Finally, here's a bit of code I keep in .emacs which you won't find elsewhere (although it's based on a cool function by Phil Hagelberg). I alternate between starting my swank instances with lein swank (one of the cooler features of Leiningen) and using the clojure-project function as found below to start the whole thing from within Emacs. I've done my best to make the latter produce an environment closely matching that provided by lein swank. Oh, and if you just want a REPL in Emacs for a quick and dirty experiment, then with the correct setup you should be able to use M-x slime directly.
(setq clojure-project-extra-classpaths
'(
; "deps/"
"src/"
"classes/"
"test/"
))
(setq clojure-project-jar-classpaths
'(
; "deps/"
"lib/"
))
(defun find-clojure-project-jars (path)
(apply #'append
(mapcar (lambda (d)
(loop for jar in (remove-if (lambda (f) (member f '("." "..")))
(directory-files d t))
collect jar into jars
finally return jars))
(remove-if-not #'file-exists-p
clojure-project-jar-classpaths))))
(defun find-clojure-jar (jars)
(let ((candidates
(remove-if-not
(lambda (jar)
(string-match-p "clojure\\([0-9.-]+\\(SNAPSHOT|MASTER\\)?\\)?\\.jar$" jar))
jars)))
(if candidates
(car candidates)
(expand-file-name "~/.clojure/clojure.jar"))))
(defun find-clojure-contrib-jar (jars)
(let ((candidates
(remove-if-not
(lambda (jar)
(string-match-p "clojure-contrib\\([0-9.-]+\\(SNAPSHOT|MASTER\\)?\\)?\\.jar$" jar))
jars)))
(if candidates
(car candidates)
(expand-file-name "~/.clojure/clojure-contrib.jar"))))
;;; original due to Phil Hagelberg
;;; (see `Best practices for Slime with Clojure' thread on Clojure Google Group)
(defun clojure-project (path)
"Sets up classpaths for a clojure project and starts a new SLIME session.
Kills existing SLIME session, if any."
(interactive (list (ido-read-directory-name
"Project root:"
(locate-dominating-file default-directory "pom.xml"))))
(when (get-buffer "*inferior-lisp*")
(kill-buffer "*inferior-lisp*"))
(cd path)
;; I'm not sure if I want to mkdir; doing that would be a problem
;; if I wanted to open e.g. clojure or clojure-contrib as a project
;; (both lack "deps/")
; (mapcar (lambda (d) (mkdir d t)) '("deps" "src" "classes" "test"))
(let* ((jars (find-clojure-project-jars path))
(clojure-jar (find-clojure-jar jars))
(clojure-contrib-jar (find-clojure-contrib-jar jars)))
(setq swank-clojure-binary nil
;; swank-clojure-jar-path (expand-file-name "~/.clojure/clojure.jar")
swank-clojure-jar-path clojure-jar
swank-clojure-extra-classpaths
(cons clojure-contrib-jar
(append (mapcar (lambda (d) (expand-file-name d path))
clojure-project-extra-classpaths)
(find-clojure-project-jars path)))
swank-clojure-extra-vm-args
(list (format "-Dclojure.compile.path=%s"
(expand-file-name "classes/" path)))
slime-lisp-implementations
(cons `(clojure ,(swank-clojure-cmd) :init swank-clojure-init)
(remove-if #'(lambda (x) (eq (car x) 'clojure))
slime-lisp-implementations))))
(slime))
There is one more excelent tutorial:
http://www.braveclojure.com/basic-emacs/ (1st part)
http://www.braveclojure.com/using-emacs-with-clojure/ (2nd part)
In 30 to 45 minutes one can have everything setup from scratch.
The tutorial does not assumes any prior knowladge of Emacs (and Clojure too - in earlier posts there is a nice intro to Clojure).
The Emacs Starter kit has gotten great reviews for getting started with Clojure:
To answer only the swank part of your question:
Leiningen is a really easy way of setting up swank with the correct classpath and get it connected to Emacs.
A great video is here: http://vimeo.com/channels/fulldisclojure#8934942
Here is an example of a project.clj file that
(defproject project "0.1"
:dependencies [[org.clojure/clojure
"1.1.0-master-SNAPSHOT"]
[org.clojure/clojure-contrib
"1.0-SNAPSHOT"]]
:dev-dependencies [[leiningen/lein-swank "1.1.0"]]
:main my.project.main)
then run:
lein swank
and from Emacs:
alt-x slime-connect
Clojure with Emacs on Clojure Documentation can be useful too.
CIDER (Clojure Interactive
Development Environment) must be mentioned here.
It’ll cover most of what you’re looking for. It includes:
interactive REPL
debugging
test running
code navigation
documentation lookup
lots more
In addition to CIDER, there are some other essential and nice-to-have
add-ons for clojure development, which I’ll try to group respectively
(and subjectively):
Essentials
smartparens – parentheses
pairing, manipulation, navigation (or
parinfer if you prefer)
clj-refactor –-
has a couple amazing features, like auto-adding/compiling namepaces
(it may be incorporated into CIDER soon)
clojure-mode –
font-lock, indentation, navigation
company – text completion
framework (or choose another auto-completer)
rainbow delimeters –
highlights/colorizes delimiters such as parentheses, brackets or
braces according to their depth
flycheck – on-the-fly syntax
checking extension
flycheck-clj-kondo –
integration for clj-kondo
Niceties
clojure-snippets –
tab-expandable shortcuts to longer code chunks
dumb-jump – jump to
definitions
which-key – displays
available keybindings in popup
highlight parentheses –
highlight surrounding parentheses
crux – a Collection of
Ridiculously Useful eXtensions for Emacs
comment-dwim-2 –
replacement for Emacs’ built-in comment-dwim
General Essentials (for any language)
magit – git porcelain inside Emacs
projectile – project mgmt
for finding files, searching, etc
helm – incremental completion
and selection narrowing framework (or
swiper)
Other Resources
If you’re looking for a setup that already has done most/all of this
work for you, a couple options are:
prelude
spacemacs