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)))
Related
I want to reuse current-prefix-arg when calling this-command. I can imagine ways of capturing its value and passing it to modified functions. But, this doesn't seem like a great solution. How can I reuse it (eg. push it back onto a sort of call stack, or whatever emacs uses)?
The example is a simple function to increment/decrement numbers in region I was just writing where I want to temporarily bind i to increment or decrement if it was called with prefix. Can I do this without adding another argument to the function, but just using the original prefix?
(defun my-toggle-increment-numbers (start end)
"Simple function to increment numbers in region. Decrement with prefix."
(interactive "r")
(let (deactivate-mark)
(goto-char start)
(while (re-search-forward "\\([[:digit:]]+\\)" end 'move)
(replace-match (number-to-string
(+ (if current-prefix-arg -1 1)
(string-to-number (match-string 1))))
nil nil nil 1))
;; what is good way to reused the current-prefix-argument value when
;; calling this-command?
(set-transient-map
(let ((km (make-sparse-keymap)))
(define-key km "i" this-command)
km)
t)))
Try this:
(defun my-toggle-increment-numbers (start end &optional decrement)
"Simple function to increment numbers in region. Decrement with prefix."
(interactive "r\nP")
(let (deactivate-mark)
(setq start (copy-marker start)
end (copy-marker end))
(goto-char start)
(while (re-search-forward "\\([-]?[[:digit:]]+\\)" end 'move)
(replace-match (number-to-string (+ (if decrement -1 1)
(string-to-number (match-string 1))))
nil nil nil 1))
(set-transient-map
(let ((km (make-sparse-keymap)))
(define-key km "i" `(lambda ()
(interactive)
(my-toggle-increment-numbers ',start ',end ',decrement)))
km)
t)))
You forgot to include the minus sign preceding your [[:digit:]].
You need to either use lexical binding or construct a command that binds the current values of start, end and current-prefix-arg (or decrement - see next).
It's a bit simpler if you use an argument instead of current-prefix-arg.
(You ask if you can just use current-prefix-arg instead of adding an arg for it. Yes. In that case, let-bind current-prefix-arg in the constructed command that gets invoked for subsequent calls.)
Since replacement can change the number of digits in a numeral (e.g. from 9 to 10, you need to use a marker instead of the number value of end in subsequent calls.
I think the cleanest way is to create a command which calls this-command passing it the prefix arg:
;; This relies on -*- lexical-binding:t -*-
(defun my-toggle-increment-numbers (start end)
[...]
(set-transient-map
(let ((km (make-sparse-keymap))
(tc this-command)
(cpa current-prefix-arg))
(define-key km "i"
(lambda () (interactive)
(let ((current-prefix-arg cpa))
(call-interactively tc))))
km)
t)))
It's probably more verbose than what you were imagining, but I think it's the closest there is to what you're asking. A simpler way to get almost the same result could be:
(defun my-toggle-increment-numbers (start end)
[...]
(setq prefix-arg current-prefix-arg)
(set-transient-map
(let ((km (make-sparse-keymap)))
(define-key km "i" this-command)
km)
t)))
Suppose I have some function which returns a struct:
(struct layer (points lines areas))
(define (build-new-layer height)
...
(layer list-a list-b list-c))
I want to keep track of the last returned result something like:
(define top-side (build-new-layer 0)) ; store the first result
...
(set! top-side (build-new-layer 0.5)) ; throw away the first result and store the new one
However, for that particular code I get the error:
set!: assignment disallowed;
cannot modify a constant
constant: top-side
Please, tell me what would be the right way to do what I want
What language are you using? it seems it's a matter of configuration, because in principle what you're doing should work. Go to the "choose language" window (Ctrl+L in Windows), click on "show details" and see if one of the options of the language currently in use disallows redefinition of variables. Alternatively, try using a different language.
Depending on where exactly you're going to use the stored result (I can't tell from the code in the question), you could pass it around as function parameters, in such a way that using a global variable is no longer necessary. This might be a better idea, relying on global state and mutation (the set! operation) is discouraged in Scheme.
If you always want to keep around the last layer, then you might prefer setting the last-layer every time one is built. Like this.
(define last-layer #f)
(define build-new-layer
(let ((save-layer #f))
(lambda (height)
(let ((new-layer (layer list-a ...)))
(set! last-layer save-layer)
(set! save-layer new-layer)
new-layer))))
Note: if the real problem is the 'constant-ness' of last-layer then build yourself a little abstraction as:
(define-values (last-layer-get last-layer-set!)
(begin
(define last-layer-access
(let ((last-layer #f))
(lambda (type . layer)
(case type
((get) last-layer)
((set) (set! last-layer (car layer)))))))
(values (lambda () (last-layer-access 'get))
(lambda (layer) (last-layer-access 'set layer))))
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.
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)))))
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-/.