Does anyone know how I can print λ instead of \ using haskell in emacs. I know that one can use haskell-font-lock-symbols, but I find the rest of them hard to read - the arrows are TOO small!
Is there a simple way of over-riding the rest of the keys?
You can also solve the problem with something like
(eval-after-load 'haskell-font-lock
'(setq haskell-font-lock-symbols-alist
(delq nil
(mapcar (lambda (rewrite)
(if (member (car rewrite) '("->" "<-"))
nil rewrite))
haskell-font-lock-symbols-alist))))
which should keep all mappings except for the one that changes "->" into "→" and "<-" into "←".
You can do this:
(defun pretty-lambdas-haskell ()
(font-lock-add-keywords
nil `((,(concat "\\(" (regexp-quote "\\") "\\)")
(0 (progn (compose-region (match-beginning 1) (match-end 1)
,(make-char 'greek-iso8859-7 107))
nil))))))
(add-hook 'haskell-mode-hook 'pretty-lambdas-haskell)
This adds the lambda as a keyword, meaning that it won't appear in escape sequences in strings for example (TODO: this is not the case after changing a thing). The ,(make-char 'greek-iso8859-7 107) is of course equivalent to ?λ, but you must make sure that your Emacs init file is encoded as unicode in that case.
You can also enable full symbol font locking and use a better (read: with wider arrows) font, like Pragmata Pro, Inconsolata or Ubuntu Monospace. I use the following code to select a good font:
(defun font-existsp (font)
"Check to see if the named FONT is available."
(if (null (x-list-fonts font))
nil t))
(require 'cl)
(defun font-avail (fonts)
"Finds the available fonts."
(remove-if-not 'font-existsp fonts))
(defvar font-preferences
'("PragmataPro"
"Inconsolata"
"DejaVu Sans Mono"
"Bitstream Vera Sans Mono"
"Anonymous Pro"
"Menlo"
"Consolas"))
(unless (eq window-system nil)
(let ((fonts (font-avail font-preferences)))
(unless (null fonts)
(set-face-attribute
'default nil :font
(car fonts)))))
Related
I'm creating a function in Emacs Lisp that will read that whole buffer aloud if there is no active region. Otherwise, it reads the active region.
(defun speak-my-text () "Speaks text in buffer."
(interactive)
(if (equal mark-active t)
(
(kill-ring-save (point-min) (point-max))
(start-process-shell-command "speakbuffvar" nil
"bash -c \"killall espeak;xsel --clipboard|espeak -s 290\"")
)
(
(kill-ring-save (region-beginning) (region-end))
(start-process-shell-command "speakbuffvar" nil
"bash -c \"killall espeak;xsel --clipboard|espeak -s 290\"")
)))
(global-set-key (kbd "C-z") 'speak-my-text)
I'm having trouble with the first line of the else clause:
(kill-ring-save (region-beginning) (region-end))
When I define the function, bind it, and run, I get "Invalid function" and it points to that line. I'm running Linux Mint.
Use C-h f progn to evaluate multiple expressions sequentially.
You're currently attempting to call a function named (kill-ring-save (region-beginning) (region-end)) and pass it the argument (start-process-shell-command ...)
Naturally, Emacs is telling you that (kill-ring-save (region-beginning) (region-end)) is not the name of a function.
In addition to phils' answer:
mark-active is not reliable in this context, as it might be
non-nil also if a region has no extend.
Function region-active-p would be usable, however depends on an
active transient-mark-mode, which might be unrelated - in most
cases it's useful and sufficient BTW.
Here is a example how to check for an valid region greater then
zero and surpassing transient-mark-mode issue:
(defun ar-region-active-p ()
"Check for and existing region of some extend.
Don't mix up that question with transient-mark-mode "
(interactive)
(and (ignore-errors (region-beginning))(region-end) (< (region-beginning)(region-end))))
Based on this, your command might be written slightly differently:
(defun my-command-on-region ()
"If a region of some extend exists, use on it.
Otherwise use whole buffer.
Doesn't require `transient-mark-mode' being active"
(interactive)
(let ((beg (or (and (ignore-errors (region-beginning))(region-end) (< (region-beginning)(region-end))(region-beginning))
(point-min)))
(end (or (and (ignore-errors (region-beginning))(region-end) (< (region-beginning)(region-end))(region-end))
(point-max))))
(DO-SOMETHING beg end)))
The code below "compiles", but doesn't function properly:
(defstruct (image-info
(:conc-name img-)
(:constructor %make-img-info (&key file))
(:print-function print-img-info))
(file nil :type string)
(gd-image nil :type (or cl-gd::image null))
(size '(0 . 0) :type cons)
(position '(0 . 0) :type cons))
(defun print-img-info (info stream level)
(declare (ignore level))
(let ((size (img-size info))
(pos (img-position info)))
(format stream "~s[width: ~d, height: ~d, x: ~d, y: ~d]"
(img-file info)
(car size) (cdr size) (car pos) (cdr pos))))
(defun make-img-info (path)
(let ((image (cl-gd:create-image-from-file path))
(info (%make-img-info :file path))) ; <--- problem here
(setf (img-gd-image info) image
(img-size info)
(cons (cl-gd:image-width image)
(cl-gd:image-height image))) info))
SBCL infers correctly the type of the argument to %make-img-info, as can be seen here:
(describe '%make-img-info)
SPRITESHEET::%MAKE-IMG-INFO
[symbol]
%MAKE-IMG-INFO names a compiled function:
Lambda-list: (&KEY (FILE NIL))
Declared type: (FUNCTION (&KEY (:FILE STRING))
(VALUES IMAGE-INFO &OPTIONAL))
But when I try to compile the make-img-info, I get this:
note: deleting unreachable code
warning:
Derived type of PATH is
(VALUES CL-GD::IMAGE &OPTIONAL),
conflicting with its asserted type
STRING.
I'm passing the correct argument (a string) to this function, but it still fails to call it because it "believes" that it has to be cl-gd:image. I suspect that the problem is that the layout is somehow alphabetical, and gd-image comes up before file in the list... but how do I then address this? I don't really want to rename the field?
Now I believe this was some sort of a glitch related to SLIME and SBCL not cooperating very well when compiling structs. I cannot consistently reproduce this behaviour, but it happens now and then with other structs too so that some times I need to kill SLIME and SWANK, restart SBCL and recompile because recompiling only the related parts of the struct will not work.
I'm not deleting the question because if anyone will come across similar behaviour, maybe it will help to restart the Lisp, so this experience can be useful.
Is there an easy way to use Emacs key-bindings when you are using a not-English (Russian) keyboard layout?
Whenever an international layout is on, all keystrokes are interpreted literally, M-ф instead of M-a. As a result I can't use commands.
It would also be nice if Linux could interpret non-prefixed and shift-prefixed keys according according to an international layout, while keeping the rest English.
You can set input method (kudos go to kindahero) by typing
M-x set-input-method RET cyrillic-yawerty RET
or
M-x set-input-method RET cyrillic-jcuken RET
To store it permanently, add
(setq default-input-method "cyrillic-yawerty")
to ~/.emacs config (and use C-\ to switch between keyboard layouts).
Here is an alternative solution that uses the OS language, based on syndikat's answer.
Some key translations are missing, but it should be easy to add them.
;; USAGE:
;; Put in your .emacs:
;;
;; (translate-keystrokes-ru->en)
;; (add-hook 'text-mode-hook
;; (lambda () (literal-insert-mode 1)))
;;
;; Only buffers with literal-insert-mode active will be sensitive to the
;; environment language. Prefixed keybindings will still be usable.
(defun translate-keystrokes-ru->en ()
"Make emacs output english characters, regardless whether
the OS keyboard is english or russian"
(flet ((make-key-stroke (prefix char)
(eval `(kbd ,(if (and (string-match "^C-" prefix)
(string-match "[A-Z]" (string char)))
(concat "S-" prefix (string (downcase char)))
(concat prefix (string char)))))))
(let ((case-fold-search nil)
(keys-pairs (mapcar* 'cons
"йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖ\ЭЯЧСМИТЬБЮ№"
"qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>#"))
(prefixes '("" "s-" "M-" "M-s-"
"C-" "C-s-" "C-M-" "C-M-s-")))
(mapc (lambda (prefix)
(mapc (lambda (pair)
(define-key key-translation-map
(make-key-stroke prefix (car pair))
(make-key-stroke prefix (cdr pair))))
keys-pairs))
prefixes))))
(defun literal-insert ()
(interactive)
(insert-char last-input-event 1))
(define-minor-mode literal-insert-mode
"Make emacs output characters corresponging to the OS keyboard,
ignoring the key-translation-map"
:keymap (let ((new-map (make-sparse-keymap))
(english-chars "qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>#"))
(mapc (lambda (char)
(define-key new-map (string char)
'literal-insert))
english-chars)
new-map))
Not sure, where did sabof got 150 billion. I ran this code (thanks to Yuri Khan, taken from EmacsWiki):
(loop
for from across "йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖ\ЭЯЧСМИТЬБЮ№"
for to across "qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>#"
do
(eval `(define-key key-translation-map (kbd ,(concat "C-" (string from))) (kbd ,(concat "C-" (string to)))))
(eval `(define-key key-translation-map (kbd ,(concat "M-" (string from))) (kbd ,(concat "M-" (string to))))))
It's only 128 combinations. Unfortunately, combinations with single letters like C-x b don't work. I'm still trying to find a better solution.
I use following snippet for Cyrillic keyboard and it works fine for me:
(defun reverse-input-method (input-method)
"Build the reverse mapping of single letters from INPUT-METHOD."
(interactive
(list (read-input-method-name "Use input method (default current): ")))
(if (and input-method (symbolp input-method))
(setq input-method (symbol-name input-method)))
(let ((current current-input-method)
(modifiers '(nil (control) (meta) (control meta))))
(when input-method
(activate-input-method input-method))
(when (and current-input-method quail-keyboard-layout)
(dolist (map (cdr (quail-map)))
(let* ((to (car map))
(from (quail-get-translation
(cadr map) (char-to-string to) 1)))
(when (and (characterp from) (characterp to))
(dolist (mod modifiers)
(define-key local-function-key-map
(vector (append mod (list from)))
(vector (append mod (list to)))))))))
(when input-method
(activate-input-method current))))
(reverse-input-method 'russian-computer)
Except:
The only issue I know is that recalculation of OrgTable formulas isn't
working in Russian layout because it is mapped to C-c-* and * change
its location.
source
If you want to keep using the Russian layout in Emacs (rather than use Emacs's own input methods), the only way I know of for now is to add bindings of the form:
(define-key function-key-map [?\M-ф] [?\M-a])
This will tell Emacs that in case M-ф is not bound, it should try to lookup M-a instead. Sadly, you'll need a lot of these bindings. If someone writes up a patch/package that can automatically provide all these bindings, I'd be happy to include it in Emacs.
Preamble
Using VTK library with C++, quite often I have to write something like this:
vtkInteractorStyleRubberBandZoom *isrbz = vtkInteractorStyleRubberBandZoom::New();
Furthermore, every time I need to use a new VTK class in my program, I have to go somewhere up the source file and add #include "vtkInteractorStyleRubberBandZoom.h"
How do I automate it, so I have to type each of the excruciatingly long class names one time instead of three?
I tried writing an Emacs minor mode for it. There are probably existing solutions out there already (YaSnippet?), but I thought that writing it myself would be a good excercise, too.
Code
;vtk-mode.el
;add to .emacs:
;(load "vtk-mode")
;(global-set-key [(control =)] 'expand-vtk)
(defun expand-vtk ()
(interactive)
(setq now (point))
(setq vtkstart (search-backward "vtk"))
(setq vtkend (- (search-forward " ") 1))
(setq vtkname (buffer-substring vtkstart vtkend))
;check for #include "vtkBlah.h"
(setq includename (format "#include \"%s.h\"\n" vtkname))
(search-backward includename nil (append-include-vtk includename))
(goto-char (+ now (length includename)))
(insert (format "= %s::New();" vtkname)))
(defun append-include-vtk (incname)
(goto-char 0)
(insert incname))
Problem
Basically, it works, except that searching for an include name always fails, e. g.:
vtkSomething *smth /*press C-= here, it looks backward for
#include "vtkSomething.h", can't find it and
calls append-include-vtk, adding it to the beginning
of the file, then comes back here and expands this line into: */
vtkSomething *smth = vtkSomething::New();
//and let's add another instance of vtkSomething...
vtkSomething *smth2 /*press C-= again, it looks backward for
#include "vtkSomething", and fails, despite the fact
that it was added by the previous command. So it adds it again."*/
What am I doing wrong here with search-backward?
(there's another (at least one) bug in the code, I shouldn't add the (length includename) if the search-backward for it was successful, but for now I am more interested in how to make it successful, in the first place)
OK, I got it. Somehow I got an idea that the third argument of search-backward (noerror) is a callback, which it isn't. Therefore it is evaluated every time, not just when the search fails. It should be something like this instead:
(defun expand-vtk ()
(interactive)
(setq now (point))
(setq vtkstart (search-backward "vtk"))
(setq vtkend (- (search-forward " ") 1))
(setq vtkname (buffer-substring vtkstart vtkend))
;check for #include "vtkBlah.h"
(setq includename (format "#include \"%s.h\"\n" vtkname))
(if (search-backward includename nil t)
(goto-char now)
(progn (append-include-vtk includename)
(goto-char (+ now (length includename)))))
(insert (format "= %s::New();" vtkname)))
(defun append-include-vtk (incname)
(goto-char 0)
(insert incname))
A command that's built into Emacs and will help you avoid typing excruciatingly long class names is dabbrev-expand (bound to M-/):
(dabbrev-expand ARG)
Expand previous word "dynamically".
Expands to the most recent, preceding word for which this is a prefix.
If no suitable preceding word is found, words following point are
considered. If still no suitable word is found, then look in the
buffers accepted by the function pointed out by variable
`dabbrev-friend-buffer-function'.
Having typed vtkInteractorStyleRubberBandZoom once, the next time you need it you just type vtkI M-/.
I have the following function that deletes the LaTeX command surrounding the current cursor position:
(defun remove-tex-cmd ()
(interactive)
(save-excursion
(let (cur-point beg-point end-point)
(setq cur-point (point))
(catch 'notexcmd
(if (not (re-search-backward "\\.*?{" nil t)) ; now the point is at the {
(throw 'notexcmd nil))
(search-backward "\\" nil t)
(setq beg-point (point))
(re-search-forward "}")
(setq end-point (point))
(if (> end-point cur-point)
(kill-region beg-point end-point))
(throw 'notexcmd nil)))
(if 'notexcmd
(message "no tex command at point"))))
It works well except for the following situation, because it simply matches the next closing }:
\test{a<cursor here>sdf ${bla}+1$}
results in
+1$}
I could, of course, count the opening and closing brackets. However, as this problem should occur frequently, I wonder whether there exists some more intelligent search function, or am I missing a totally different point?
Use list- or sexp- based operations:
(defun remove-tex-cmd ()
(interactive)
(backward-up-list 1)
(backward-sexp 1)
(kill-sexp 2))
To handle scan error when outside parentheses:
(defun remove-tex-cmd ()
(interactive)
(condition-case nil
(progn
(backward-up-list 1)
(backward-sexp 1)
(kill-sexp 2))
(scan-error (message "Outside parentheses."))))