Emacs determining keyboard layout - emacs

Is there a way for Emacs to detect the current keyboard layout?
I often write texts in English and German, switching the (Win OS) keyboard layout. However, some functions (C-Y e.g.) should always be at the same physical key, no matter what language Im currently typing in.
Thanks

Consider using M-x set-input-method and M-x toggle-input-method. Toggle is bound to C-\, set is bound to C-x RET C-\. I recommend this binding, if you have hyper key:
(global-set-key [?\H-\\] 'set-input-method).
If you asking not how to type in different language, but how to make several commands work when you using different languages in your OS, try just to bind them. It worked nice on russian symbols. One black-black night i even wrote
(setq russian-symbols '(
(?й . ?q)
(?ц . ?w)
(?у . ?e)
(?к . ?r)
(?е . ?t)
(?н . ?y)
(?г . ?u)
(?ш . ?i)
(?щ . ?o)
(?з . ?p)
(?х . ?\[)
(?ъ . ?\])
(?ф . ?a)
(?ы . ?s)
(?в . ?d)
(?а . ?f)
(?п . ?g)
(?р . ?h)
(?о . ?j)
(?л . ?k)
(?д . ?l)
(?ж . ?\;)
(?э . ?')
(?я . ?z)
(?ч . ?x)
(?с . ?c)
(?м . ?v)
(?и . ?b)
(?т . ?n)
(?ь . ?m)
(?б . ?,)
(?ю . ?.)
(?Й . ?Q)
(?Ц . ?W)
(?У . ?E)
(?К . ?R)
(?Е . ?T)
(?Н . ?Y)
(?Г . ?U)
(?Ш . ?I)
(?Щ . ?O)
(?З . ?P)
(?Х . ?{)
(?Ъ . ?})
(?Ф . ?A)
(?Ы . ?S)
(?В . ?D)
(?А . ?F)
(?П . ?G)
(?Р . ?H)
(?О . ?J)
(?Л . ?K)
(?Д . ?L)
(?Ж . ?:)
(?Э . ?\")
(?Я . ?Z)
(?Ч . ?X)
(?С . ?C)
(?М . ?V)
(?И . ?B)
(?Т . ?N)
(?Ь . ?M)
(?Б . ?<)
(?Ю . ?>)
(?Ё . ?~)
(?ё . ?`)
))
(setq russian-symbols-full (append russian-symbols
'((?. . ?/)
(?, . ??)
(?\" . ?#)
(?№ . ?#)
(?\; . ?$)
(?: . ?^)
(?\? . ?&))))
(defun cm-ru-to-en-string(string)
(apply 'concat (mapcar (lambda (arg) (setq arg (format "%c" (or (cdr (assoc arg russian-symbols-full)) arg)))) string)))
(defun cm-en-to-ru-string(string)
(apply 'concat (mapcar (lambda (arg) (setq arg (format "%c" (or (car (rassoc arg russian-symbols-full)) arg)))) string)))
(defun cm-ru-to-en-region()
(interactive)
(let ((text (buffer-substring-no-properties (mark) (point))))
(delete-region (mark) (point))
(insert (cm-ru-to-en-string text))))
(defun cm-en-to-tu-region()
(interactive)
(let ((text (buffer-substring-no-properties (mark) (point))))
(delete-region (mark) (point))
(insert (cm-en-to-ru-string text))))
;; DO NOT USE vvv SINCE YOU WILL NOT BE ABLE TO INPUT THROUGH C-\
;; (let ((symbols russian-symbols))
;; (while symbols
;; (global-set-key (vector (car (car symbols))) (vector (cdr (car symbols))))
;; (setq symbols (cdr symbols))))
;; DO NOT USE ^^^ SINCE YOU WILL NOT BE ABLE TO INPUT THROUGH C-\
;; (- ?\C-ы ?ы) ;;russian C-
;; (- ?\C-s ?s) ;;english C-
;; (- ?\M-ы ?ы) ;;russian M-
;; (- ?\M-s ?s) ;;english M-
(setq russian-symbols-map1
(append
(mapcar (lambda (arg) (setq arg (cons (+ (- ?\C-ы ?ы) (car arg)) (+ (- ?\C-s ?s) (cdr arg))))) russian-symbols)
(mapcar (lambda (arg) (setq arg (cons (+ (- ?\M-ы ?ы) (car arg)) (+ (- ?\M-s ?s) (cdr arg))))) russian-symbols)
(mapcar (lambda (arg) (setq arg (cons (+ (- ?\C-\M-ы ?ы) (car arg)) (+ (- ?\C-\M-s ?s) (cdr arg))))) russian-symbols)
(mapcar (lambda (arg) (setq arg (cons (+ (- ?\H-ы ?ы) (car arg)) (+ (- ?\H-s ?s) (cdr arg))))) russian-symbols)
(mapcar (lambda (arg) (setq arg (cons (+ (- ?\H-\C-ы ?ы) (car arg)) (+ (- ?\H-\C-s ?s) (cdr arg))))) russian-symbols)
(mapcar (lambda (arg) (setq arg (cons (+ (- ?\H-\M-ы ?ы) (car arg)) (+ (- ?\H-\M-s ?s) (cdr arg))))) russian-symbols)
(mapcar (lambda (arg) (setq arg (cons (+ (- ?\H-\C-\M-ы ?ы) (car arg)) (+ (- ?\H-\C-\M-s ?s) (cdr arg))))) russian-symbols)))
(setq russian-symbols-map2
(append
russian-symbols-map1
russian-symbols)) ; We must not start with russian letters, but if it is element in a sequence - in should be fine.
(setq symbols2 russian-symbols-map1) ; One-key sequence command.
(let ((symbols2 russian-symbols-map1))
(while symbols2
(if
(and (symbolp (lookup-key global-map
(vector
(cdr (car symbols2))
)))
(lookup-key global-map
(vector
(cdr (car symbols2))
)))
(global-set-key
(vector
(car (car symbols2))
)
(lookup-key global-map
(vector
(cdr (car symbols2))
))))
(setq symbols2 (cdr symbols2))))
(let ((symbols1 russian-symbols-map2) (symbols2 russian-symbols-map1)) ; Two keys sequence
(while symbols1
(while symbols2
(if
(and (symbolp (lookup-key global-map
(vector
(cdr (car symbols2))
(cdr (car symbols1))
)))
(lookup-key global-map
(vector
(cdr (car symbols2))
(cdr (car symbols1))
)))
(global-set-key
(vector
(car (car symbols2))
(car (car symbols1))
)
(lookup-key global-map
(vector
(cdr (car symbols2))
(cdr (car symbols1))
))))
(setq symbols2 (cdr symbols2)))
(setq symbols2 russian-symbols-map1)
(setq symbols1 (cdr symbols1))))
(provide 'shamanizm) ;russian emacs-users should lol reading this
It works. I've binded all global hotkeys up to 2 level with russian symbols.
I am not using it now, it eat startup time, and it ruins readability of my *Help* with crazy things like It is bound to C-x b, C-x и, C-ч b, C-ч и. Use it wisely.

Related

Can't start emacs (spacemacs) anymore: Invalid read syntax: Invalid byte-code object

Can someone tell me how I can recover from this error? I runs emacs 28.0.50 with spacemacs 0.300.0#28.0.50 on Ubuntu 19.10.
Here is the backtrace from emacs --debug-init
Debugger entered--Lisp error: (invalid-read-syntax "Invalid byte-code object")
read(get-file-char)
require(use-package-core)
byte-code("\300\301!\210\300\302!\210\300\303!\210\300\304!\210\300\305!\210\306\307\310\311\312$\210\313\314!\207" [require use-package-core use-package-bind-key use-package-diminish use-package-delight use-package-ensure autoload use-package-jump-to-package-form "use-package-jump" nil t provide use-package] 5)
require(use-package)
spacemacs-bootstrap/init-use-package()
funcall(spacemacs-bootstrap/init-use-package)
(let* ((pkg-name (eieio-oref pkg ':name)) (owner (car (eieio-oref pkg ':owners)))) (spacemacs-buffer/message (format "%S -> init (%S)..." pkg-name owner)) (funcall (intern (format "%S/init-%S" owner pkg-name))))
configuration-layer//configure-package(#<cfgl-package cfgl-package-15636ced90d0>)
mapc(configuration-layer//configure-package (#<cfgl-package cfgl-package-15636ced6330> #<cfgl-package cfgl-package-15636ced7678> #<cfgl-package cfgl-package-15636ceccf10> #<cfgl-package cfgl-package-15636ced78e4> #<cfgl-package cfgl-package-15636ced7b48> #<cfgl-package cfgl-package-15636ced7db0> #<cfgl-package cfgl-package-15636ced90d0> #<cfgl-package cfgl-package-15636ced9340>))
(let (packages-to-configure) (let ((--dolist-tail-- packages) pkg-name) (while --dolist-tail-- (setq pkg-name (car --dolist-tail--)) (let ((pkg (configuration-layer/get-package pkg-name))) (cond ((eieio-oref pkg ':lazy-install) (spacemacs-buffer/message (format "%S ignored since it can be lazily installed." pkg-name))) ((and (eieio-oref pkg ...) (not ...)) (spacemacs-buffer/message (format "%S ignored since it has been excluded." pkg-name))) ((null (eieio-oref pkg ...)) (spacemacs-buffer/message (format "%S ignored since it has no owner layer." pkg-name))) ((not (configuration-layer//package-reqs-used-p pkg)) (spacemacs-buffer/message (format ... pkg-name))) ((not (cfgl-package-enabled-p pkg)) (spacemacs-buffer/message (format "%S is disabled." pkg-name))) (t (let (...) (if dir ...)) (if (memq ... ...) nil (configuration-layer//activate-package pkg-name)) (cond (... ...) (t ... ...))))) (setq --dolist-tail-- (cdr --dolist-tail--)))) (setq packages-to-configure (reverse packages-to-configure)) (mapc 'configuration-layer//configure-package packages-to-configure) (mapc 'configuration-layer//post-configure-package packages-to-configure))
configuration-layer//configure-packages-2((async bind-key bind-map diminish evil hydra use-package which-key))
configuration-layer//configure-packages((abbrev ac-ispell academic-phrases ace-jump-helm-line ace-link ace-window add-node-modules-path aggressive-indent all-the-icons all-the-icons-dired amx anaconda-mode anki-editor ansi-colors archive-mode async attrap auctex auctex-latexmk auto-compile auto-complete auto-correct auto-dim-other-buffers auto-highlight-symbol auto-yasnippet avy beacon biblio biblio-core bibtex bind-key bind-map blacken bnf-mode bookmark bracketed-paste bug-hunter calendar calibre-mode camcorder centered-cursor-mode cheat-sh chronometer clean-aindent-mode cloc cmm-mode color-identifiers-mode column-enforce-mode comint command-log-mode ...))
configuration-layer//load()
(cond (changed-since-last-dump-p (configuration-layer//load) (if (spacemacs/emacs-with-pdumper-set-p) (progn (configuration-layer/message "Layer list has changed since last dump.") (configuration-layer//dump-emacs)))) (spacemacs-force-dump (configuration-layer//load) (if (spacemacs/emacs-with-pdumper-set-p) (progn (configuration-layer/message (concat "--force-dump passed on the command line, " "forcing a redump.")) (configuration-layer//dump-emacs)))) ((spacemacs-is-dumping-p) (configuration-layer//load)) ((and (spacemacs/emacs-with-pdumper-set-p) (spacemacs-run-from-dump-p)) (configuration-layer/message "Running from a dumped file. Skipping the loading p...")) (t (configuration-layer//load) (if (spacemacs/emacs-with-pdumper-set-p) (progn (configuration-layer/message (concat "Layer list has not changed since last time. " "Skipping dumping process!"))))))
configuration-layer/load()
(let ((file-name-handler-alist nil)) (require 'core-spacemacs) (spacemacs/dump-restore-load-path) (configuration-layer/load-lock-file) (spacemacs/init) (configuration-layer/stable-elpa-init) (configuration-layer/load) (spacemacs-buffer/display-startup-note) (spacemacs/setup-startup-hook) (spacemacs/dump-eval-delayed-functions) (if (and dotspacemacs-enable-server (not (spacemacs-is-dumping-p))) (progn (require 'server) (if dotspacemacs-server-socket-dir (progn (setq server-socket-dir dotspacemacs-server-socket-dir))) (if (server-running-p) nil (message "Starting a server...") (server-start)))))
(if (not (version<= spacemacs-emacs-min-version emacs-version)) (error (concat "Your version of Emacs (%s) is too old. " "Spacemacs requires Emacs version %s or above.") emacs-version spacemacs-emacs-min-version) (let ((file-name-handler-alist nil)) (require 'core-spacemacs) (spacemacs/dump-restore-load-path) (configuration-layer/load-lock-file) (spacemacs/init) (configuration-layer/stable-elpa-init) (configuration-layer/load) (spacemacs-buffer/display-startup-note) (spacemacs/setup-startup-hook) (spacemacs/dump-eval-delayed-functions) (if (and dotspacemacs-enable-server (not (spacemacs-is-dumping-p))) (progn (require 'server) (if dotspacemacs-server-socket-dir (progn (setq server-socket-dir dotspacemacs-server-socket-dir))) (if (server-running-p) nil (message "Starting a server...") (server-start))))))
eval-buffer(#<buffer *load*-45090> nil "/home/chriad/.dotfiles/emacs.d/init.el" nil t) ; Reading at buffer position 1880
load-with-code-conversion("/home/chriad/.dotfiles/emacs.d/init.el" "/home/chriad/.dotfiles/emacs.d/init.el" nil nil)
load("/home/chriad/.dotfiles/emacs.d/init.el")
(let* ((emacs-directory (file-name-as-directory (chemacs-emacs-profile-key 'user-emacs-directory))) (init-file (expand-file-name "init.el" emacs-directory)) (custom-file- (chemacs-emacs-profile-key 'custom-file init-file)) (server-name- (chemacs-emacs-profile-key 'server-name))) (setq user-emacs-directory emacs-directory) (if server-name- (progn (setq server-name server-name-))) (mapcar #'(lambda (env) (setenv (car env) (cdr env))) (chemacs-emacs-profile-key 'env)) (if (chemacs-emacs-profile-key 'straight-p) (progn (chemacs-load-straight))) (load init-file) (if (not custom-file) (progn (setq custom-file custom-file-) (if (equal custom-file init-file) nil (load custom-file)))))
chemacs-load-profile("default")
(if args (let ((s (split-string (car args) "="))) (cond ((equal (car args) "--with-profile") (add-to-list 'command-switch-alist '("--with-profile" lambda (_) (pop command-line-args-left))) (chemacs-load-profile (car (cdr args)))) ((equal (car s) "--with-profile") (add-to-list 'command-switch-alist (cons (car args) '(lambda ...))) (chemacs-load-profile (mapconcat 'identity (cdr s) "="))) (t (chemacs-check-command-line-args (cdr args))))) (chemacs-load-profile (chemacs-detect-default-profile)))
chemacs-check-command-line-args(nil)
(cond ((equal (car args) "--with-profile") (add-to-list 'command-switch-alist '("--with-profile" lambda (_) (pop command-line-args-left))) (chemacs-load-profile (car (cdr args)))) ((equal (car s) "--with-profile") (add-to-list 'command-switch-alist (cons (car args) '(lambda (_)))) (chemacs-load-profile (mapconcat 'identity (cdr s) "="))) (t (chemacs-check-command-line-args (cdr args))))
(let ((s (split-string (car args) "="))) (cond ((equal (car args) "--with-profile") (add-to-list 'command-switch-alist '("--with-profile" lambda (_) (pop command-line-args-left))) (chemacs-load-profile (car (cdr args)))) ((equal (car s) "--with-profile") (add-to-list 'command-switch-alist (cons (car args) '(lambda (_)))) (chemacs-load-profile (mapconcat 'identity (cdr s) "="))) (t (chemacs-check-command-line-args (cdr args)))))
(if args (let ((s (split-string (car args) "="))) (cond ((equal (car args) "--with-profile") (add-to-list 'command-switch-alist '("--with-profile" lambda (_) (pop command-line-args-left))) (chemacs-load-profile (car (cdr args)))) ((equal (car s) "--with-profile") (add-to-list 'command-switch-alist (cons (car args) '(lambda ...))) (chemacs-load-profile (mapconcat 'identity (cdr s) "="))) (t (chemacs-check-command-line-args (cdr args))))) (chemacs-load-profile (chemacs-detect-default-profile)))
chemacs-check-command-line-args(("emacs"))
eval-buffer(#<buffer *load*> nil "/home/chriad/.emacs" nil t) ; Reading at buffer position 7021
load-with-code-conversion("/home/chriad/.emacs" "/home/chriad/.emacs" t t)
load("~/.emacs" noerror nomessage)
startup--load-user-init-file(#f(compiled-function () #<bytecode 0x2bc902d994a710f>) #f(compiled-function () #<bytecode 0x81a386fa1b19353>) t)
command-line()
normal-top-level()
I had the same issue this morning.
I'm using emacs 28.0.50 on Kubuntu 19.10.
My issue was gone by deleting use-package:
rm -rf ~/.emacs.d/elpa/use-package-*
# if using the `develop` branch:
# rm -rf ~/.emacs.d/elpa/<your emacs version>/develop/use-package-*
and re-installing it:
emacs -q
eval:
(progn
(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
(package-initialize)
(package-refresh-contents)
(package-install 'use-package)
(require 'use-package)
)
For me it was a matter of doing "M-x reinstall-package RET use-package RET" and restarting Emacs.
Reinstalling use-package via the list-packages UI worked for me.
(Delete use-package, install it again)
I did not see a re-install option there.
Thank you for the hints here.
I received almost exactly the same error (emacs 28.0.50 with spacemacs 0.200.13#28.0.50 on Ubuntu 18.04). I updated the emacs packages and the problem disappeared. If I had any better ideas (or explanations) I would include them but that's all I got. Good luck!

How to use a cons cell to define and later remove overlays with `dolist`

I am looking for some guidance, please, to reduce the time needed to perform my custom overlay removal function. The delay of up to 0.1 seconds is caused because a plethora of variables all have values, however, not every variable is necessarily used.
My goal is to attach a second variable that can be set to non-nil whenever the first variable is used, but I am unsure how to set this up and how to incorporate that into the overlay removal function.
Perhaps something that looks like this would be useful:
if (variable-one . t), then (remove-overlays (point-min) (point-max) 'display character)
In the following example, M-x sub-char-mode will place an overlay over the characters 1, 2 or 3 whenever the cursor is visiting any of those characters:
1 will become |1
2 will become |2
3 will become |3
(defvar variable-one (concat
(propertize (char-to-string ?\u007C)
'face 'font-lock-warning-face
'cursor t)
(propertize "1" 'face 'highlight 'cursor t) ))
(defvar variable-one-p (cons variable-one nil))
(defvar variable-two (concat
(propertize (char-to-string ?\u007C)
'face 'font-lock-warning-face
'cursor t)
(propertize "2" 'face 'highlight 'cursor t) ))
(defvar variable-two-p (cons variable-two nil))
(defvar variable-three (concat
(propertize (char-to-string ?\u007C)
'face 'font-lock-warning-face
'cursor t)
(propertize "3" 'face 'highlight 'cursor t) ))
(defvar variable-three-p (cons variable-three nil))
(defun substitute-character ()
(cond
((eq (char-after (point)) 49)
(setq variable-one-p (cons variable-one t))
(overlay-put (make-overlay (point) (1+ (point))) 'display variable-one))
((eq (char-after (point)) 50)
(setq variable-two-p (cons variable-two t))
(overlay-put (make-overlay (point) (1+ (point))) 'display variable-two))
((eq (char-after (point)) 51)
(setq variable-three-p (cons variable-three t))
(overlay-put (make-overlay (point) (1+ (point))) 'display variable-three))))
(defun remove-sub-char ()
(dolist (character `(
,variable-one
,variable-two
,variable-three))
(remove-overlays (point-min) (point-max) 'display character))
(dolist (my-variable `(
,variable-one-p
,variable-two-p
,variable-three-p))
(setq my-variable nil)) )
(defun sub-char-post-command-hook ()
(remove-sub-char)
(substitute-character))
(define-minor-mode sub-char-mode
"A minor-mode for testing overlay-removal with cons cells."
:init-value nil
:lighter " OV-REMOVE"
:keymap nil
:global nil
:group 'lawlist
(cond
(sub-char-mode
(add-hook 'post-command-hook 'sub-char-post-command-hook nil t)
(message "Turned ON `sub-char-mode`."))
(t
(remove-hook 'post-command-hook 'sub-char-post-command-hook t)
(remove-sub-char)
(message "Turned OFF `sub-char-mode`."))))
Apologies for pasting this image here - feel free to remove it. But I couldn't paste it into a comment, to reply to your comment asking for the appearance. This is vline-style = compose and col-highlight-vline-face-flag = nil:
First Draft (August 24, 2014):  The first draft answer defines the variables as a cons cell -- the car is a predetermined overlay string, and the cdr is nil. When the cursor visits the characters 1, 2 or 3, an overlay is placed on top of those characters and the cdr of the applicable cons cell is set to t by using setcdr. The overlay removal function contains a list of variable names and the corresponding cons cells -- when the cdr of the cons cell is non-nil (i.e., t), the overlay is removed, and the cdr of the applicable cons cell is set back to nil using setcdr. The advantage of this type of setup is that the overlay removal function will quickly look at and then skip over variables whose cons cell cdr is nil -- thus saving a substantial amount of time when dealing with large quantities of variables with predetermined overlay strings.
EDIT (August 26, 2014):  Modified code to permit using the same variable names in different buffers and set buffer-local values. Related threads are: How to use `setcdr` with buffer-local variables and Incorporate variable name into `dolist` cycle and change its value .
(defvar variable-one
(cons
(concat
(propertize (char-to-string ?\u007C)
'face 'font-lock-warning-face
'cursor t)
(propertize "1" 'face 'highlight 'cursor t) )
nil))
(make-variable-buffer-local 'variable-one)
(defvar variable-two
(cons
(concat
(propertize (char-to-string ?\u007C)
'face 'font-lock-warning-face
'cursor t)
(propertize "2" 'face 'highlight 'cursor t) )
nil))
(make-variable-buffer-local 'variable-two)
(defvar variable-three
(cons
(concat
(propertize (char-to-string ?\u007C)
'face 'font-lock-warning-face
'cursor t)
(propertize "3" 'face 'highlight 'cursor t) )
nil))
(make-variable-buffer-local 'variable-three)
(defun sub-char ()
(cond
((eq (char-after (point)) 49)
(let ((newlist (copy-list variable-one)))
(setcdr newlist t)
(setq-local variable-one newlist)
(overlay-put (make-overlay (point) (1+ (point))) 'display (car variable-one))))
((eq (char-after (point)) 50)
(let ((newlist (copy-list variable-two)))
(setcdr newlist t)
(setq-local variable-two newlist)
(overlay-put (make-overlay (point) (1+ (point))) 'display (car variable-two))))
((eq (char-after (point)) 51)
(let ((newlist (copy-list variable-three)))
(setcdr newlist t)
(setq-local variable-three newlist)
(overlay-put (make-overlay (point) (1+ (point))) 'display (car variable-three))))))
(defun remove-sub-char ()
(dolist (character `(
(variable-one ,variable-one)
(variable-two ,variable-two)
(variable-three ,variable-three)))
(when (cdr (car (cdr character)))
(let* (
(var (car character))
(newlist (copy-list (car (cdr character)))) )
(remove-overlays (point-min) (point-max) 'display (car (car (cdr character))))
(setcdr newlist nil)
(set (car character) newlist)
(message "var1: %s | var2: %s | var3: %s" variable-one variable-two variable-three) ))))
(defun sub-char-post-command-hook ()
(remove-sub-char)
(sub-char))
(define-minor-mode sub-char-mode
"A minor-mode for testing overlay-removal with cons cells."
:init-value nil
:lighter " OV-REMOVE"
:keymap nil
:global nil
:group 'lawlist
(cond
(sub-char-mode
(add-hook 'post-command-hook 'sub-char-post-command-hook nil t)
(message "Turned ON `sub-char-mode`."))
(t
(remove-hook 'post-command-hook 'sub-char-post-command-hook t)
(remove-sub-char)
(message "Turned OFF `sub-char-mode`."))))
I suggest you start by getting rid of your variable-FOO-p vars: not only their names are wrong (the "-p" suffix is meant for use with predicates which are necessarily functions, and not variables) but they're unneeded. Rather than tell remove-overlays to remove all overlays with a particular display property, just add a property of your own (e.g. (overlay-put <youroverlay> 'vline t)) so you can then do a single (remove-overlays (point-min) (point-max) 'vline t). Of course, another approach which will work at least as well is to keep a single buffer-local variable (better yet, window-local) which holds a list of all the overlays you currently have placed. This way, you can remove those overlays with a single (mapc #'delete-overlay vline--overlays), which is more efficient. It can be made even more efficient by moving/reusing those overlays rather than deleting them and then creating new ones instead.

Emacs -- How to extract all elements of a list

I am looking for some assistance, please, to extract all elements of a list of files and/or directories that have been marked in dired-mode. Essentially, if there were some way to just remove the parentheses from around the result of (mapcar (lambda (filename) (file-name-nondirectory filename)) (dired-get-marked-files)), then that would do the trick.
(start-process
"name-of-process"
"*output-buffer*"
"/usr/bin/zip"
"zip-file-name.zip"
(mapcar
(lambda (filename) (file-name-nondirectory filename))
(dired-get-marked-files)) )
The result I am seeking will look like this:
(start-process
"name-of-process"
"*output-buffer*"
"/usr/bin/zip"
"zip-file-name.zip"
"filename-number-one"
"filename-number-two"
"filename-number-three" )
EDIT:
The start-process function does not generally accept a single concatenated string of arguments. Instead, each argument must be separately spelled out (with quotation marks around each argument), or the argument can be a variable.
Here is the debugger message from the first example above -- the error occurs because there is a parentheses around the file names -- i.e., it cannot be a list.
Debugger entered--Lisp error: (wrong-type-argument stringp ("file-name-number-one" "file-name-number-two" "file-name-number-three"))
start-process("name-of-process" "*output-buffer*" "/usr/bin/zip" "zip-file-name.zip" ("file-name-number-one" "file-name-number-two" "file-name-number-three"))
eval((start-process "name-of-process" "*output-buffer*" "/usr/bin/zip" "zip-file-name.zip" (mapcar (lambda (filename) (file-name-nondirectory filename)) (dired-get-marked-files))) nil)
(cons (eval exp lexical-binding) values)
(setq values (cons (eval exp lexical-binding) values))
(let ((debug-on-error old-value)) (setq values (cons (eval exp lexical-binding) values)) (setq new-value debug-on-error))
(let ((old-value (make-symbol "t")) new-value) (let ((debug-on-error old-value)) (setq values (cons (eval exp lexical-binding) values)) (setq new-value debug-on-error)) (if (eq old-value new-value) nil (setq debug-on-error new-value)))
(if (null eval-expression-debug-on-error) (setq values (cons (eval exp lexical-binding) values)) (let ((old-value (make-symbol "t")) new-value) (let ((debug-on-error old-value)) (setq values (cons (eval exp lexical-binding) values)) (setq new-value debug-on-error)) (if (eq old-value new-value) nil (setq debug-on-error new-value))))
(let ((exp (if exp exp (read--expression "Eval: ")))) (if (null eval-expression-debug-on-error) (setq values (cons (eval exp lexical-binding) values)) (let ((old-value (make-symbol "t")) new-value) (let ((debug-on-error old-value)) (setq values (cons (eval exp lexical-binding) values)) (setq new-value debug-on-error)) (if (eq old-value new-value) nil (setq debug-on-error new-value)))) (let ((print-length (and (not (= 0 (prefix-numeric-value insert-value))) eval-expression-print-length)) (print-level (and (not (= 0 (prefix-numeric-value insert-value))) eval-expression-print-level)) (deactivate-mark)) (if insert-value (with-no-warnings (let ((standard-output (current-buffer))) (prog1 (prin1 (car values)) (if (= 0 ...) (progn ...))))) (prog1 (prin1 (car values) t) (let ((str (eval-expression-print-format ...))) (if str (princ str t)))))))
(if (active-minibuffer-window) nil (let ((exp (if exp exp (read--expression "Eval: ")))) (if (null eval-expression-debug-on-error) (setq values (cons (eval exp lexical-binding) values)) (let ((old-value (make-symbol "t")) new-value) (let ((debug-on-error old-value)) (setq values (cons (eval exp lexical-binding) values)) (setq new-value debug-on-error)) (if (eq old-value new-value) nil (setq debug-on-error new-value)))) (let ((print-length (and (not (= 0 ...)) eval-expression-print-length)) (print-level (and (not (= 0 ...)) eval-expression-print-level)) (deactivate-mark)) (if insert-value (with-no-warnings (let ((standard-output ...)) (prog1 (prin1 ...) (if ... ...)))) (prog1 (prin1 (car values) t) (let ((str ...)) (if str (princ str t))))))))
lawlist-eval-expression()
funcall-interactively(lawlist-eval-expression)
call-interactively(lawlist-eval-expression nil nil)
command-execute(lawlist-eval-expression)
What you want is to use apply with (mapcar ...) as its last argument:
(apply 'start-process
"name-of-process"
"*output-buffer*"
"/usr/bin/zip"
"zip-file-name.zip"
(mapcar #'file-name-nondirectory (dired-get-marked-files)))
Note that (mapcar #'function list) is a shorter spelling of (mapcar (lambda (arg) (function arg)) list).
combine-and-quote-strings is what you want:
(combine-and-quote-strings (mapcar (lambda (x)
(file-name-nondirectory x))
(dired-get-marked-files)))
EDIT: the following will give you a single, quoted string with internal quotes. Not sure if it'll play nicely with start-process:
(mapconcat
(lambda (x)
(concat "\"" (file-name-nondirectory x) "\""))
(dired-get-marked-files) " ")
EDIT: Righty-o, let's try this. Splice the backquoted list with ,#, then eval the whole thing:
(eval `(start-process
"name-of-process"
"*output-buffer*"
"/usr/bin/zip"
"zip-file-name.zip"
,#(mapcar
(lambda (x)
(file-name-nondirectory x))
(dired-get-marked-files))))

Toggle case of next letter in elisp

I'd like to be able to toggle the case of the letter under the point. To that end, I wrote this:
(defun toggle-case-next-letter ()
"Toggles the case of the next letter, then moves the point forward one character"
(interactive)
(let* ((p (point))
(upcased (upcasep (char-after)))
(f (if upcased 'downcase-region 'upcase-region)))
(progn
(f p (+ 1 p))
(forward-char))))
However, when I run it (I've bound it to M-#), I get progn: Symbol's function definition is void: f. I assume this means f isn't bound, but I'm not sure.
Upcasep is defined as:
(defun upcasep (c) (eq c (upcase c)))
Is the problem in the let binding, or something else? (Also, if there's a better way to do this, that'd be nice as well).
Note that originally I had (upcased (upcasep (buffer-substring-no-properties p (+ 1 p)))), which I've corrected to (upcased (upcasep (char-after)), because using upcasep as defined above is always nil for strings (so I couldn't downcase again).
You've got a typical case of lisp-1 / lisp-2 confusion. Here's a fix (just a funcall):
(defun toggle-case-next-letter ()
"Toggles the case of the next letter, then moves the point forward one character"
(interactive)
(let* ((p (point))
(upcased (char-upcasep (buffer-substring-no-properties p (+ 1 p))))
(f (if upcased 'downcase-region 'upcase-region)))
(progn
(funcall f p (+ 1 p))
(forward-char))))
And here's what I have:
(global-set-key (kbd "C->") 'upcase-word-toggle)
(global-set-key (kbd "C-z") 'capitalize-word-toggle)
(defun char-upcasep (letter)
(eq letter (upcase letter)))
(defun capitalize-word-toggle ()
(interactive)
(let ((start (car
(save-excursion
(backward-word)
(bounds-of-thing-at-point 'symbol)))))
(if start
(save-excursion
(goto-char start)
(funcall
(if (char-upcasep (char-after))
'downcase-region
'upcase-region)
start (1+ start)))
(capitalize-word -1))))
(defun upcase-word-toggle ()
(interactive)
(let ((bounds (bounds-of-thing-at-point 'symbol))
beg end
regionp)
(if (eq this-command last-command)
(setq regionp (get this-command 'regionp))
(put this-command 'regionp nil))
(cond
((or (region-active-p) regionp)
(setq beg (region-beginning)
end (region-end))
(put this-command 'regionp t))
(bounds
(setq beg (car bounds)
end (cdr bounds)))
(t
(setq beg (point)
end (1+ beg))))
(save-excursion
(goto-char (1- beg))
(and (re-search-forward "[A-Za-z]" end t)
(funcall (if (char-upcasep (char-before))
'downcase-region
'upcase-region)
beg end)))))
I couldn't get #abo-abo's answer working for me but using his comments I was able to google better and found the following at http://chneukirchen.org/dotfiles/.emacs
(defun chris2-toggle-case ()
(interactive)
(let ((char (following-char)))
(if (eq char (upcase char))
(insert-char (downcase char) 1 t)
(insert-char (upcase char) 1 t)))
(delete-char 1 nil)
(backward-char))
(global-set-key (kbd "M-#") 'chris2-toggle-case)
This answers the original question if you remove (backward-char).
I realize this is a very old question, but having stumbled upon the same problem recently, I'd like to suggest a simpler solution.
I start with a pure function for toggling character case, based on char code property inspection:
(cl-defun toggle-char-case (c)
(cl-case (get-char-code-property c 'general-category)
(Lu (downcase c))
(Ll (upcase c))
(t c)))
I then use it from within an interactive function operating at point:
(cl-defun toggle-char-case-at-point ()
(interactive)
(let ((new (toggle-char-case (char-after))))
(delete-char 1)
(insert new)))
I then bound this interactive function to a keybinding of my choice:
(global-set-key (kbd "C-M-c") 'toggle-char-case-at-point)
The way this function operates is, after toggling the case it advances the point by one. So calling it repeatedly will toggle the cases of a sequence of chars. One could make it keep the point unchanged - that would require adding (backward-char) to the body.

How to toggle letter cases in a region in emacs

How can I toggle the case of letters (switch uppercase letters to lowercase and lowercase letters to uppercase) of a region's text in Emacs?
There are listed commands for conversion but nothing for toggling.
Example:
PLease toggLE MY LETTER case
should become:
plEASE TOGGle my letter CASE
You can do it with a regexp substitution:
M-x replace-regexp RET
\([[:upper:]]+\)?\([[:lower:]]+\)? RET
\,(concat (downcase (or \1 "")) (upcase (or \2 ""))) RET
It's up to you to bind a key to this.
I wrote it for you; it did not have thorough testing, but it appears to do what you seek.
The logic behind it is to loop over every single character in the text. If the character is equal to the character in downcase, append it to the return string in upcase. If not, append it in downcase. At the end, delete region and insert the return string.
It works immediate on a page of text, though I'd be wary to use it on huge texts (should be fine still).
(defun toggle-case ()
(interactive)
(when (region-active-p)
(let ((i 0)
(return-string "")
(input (buffer-substring-no-properties (region-beginning) (region-end))))
(while (< i (- (region-end) (region-beginning)))
(let ((current-char (substring input i (+ i 1))))
(if (string= (substring input i (+ i 1)) (downcase (substring input i (+ i 1))))
(setq return-string
(concat return-string (upcase (substring input i (+ i 1)))))
(setq return-string
(concat return-string (downcase (substring input i (+ i 1)))))))
(setq i (+ i 1)))
(delete-region (region-beginning) (region-end))
(insert return-string))))
Commands upcase-region, downcase-region, andcapitalize-region are not toggles, and are perhaps the "conversion" commands you referred to. Here is a command that cycles among them.
(defvar cycle-region-capitalization-last 'upper)
(defun cycle-region-capitalization (&optional msgp)
"Cycle the region text among uppercase, lowercase and capitalized (title case)."
(interactive "p")
(setq cycle-region-capitalization-last
(case cycle-region-capitalization-last
(upper (call-interactively #'downcase-region) 'lower)
(lower (call-interactively #'capitalize-region) 'title)
(title (call-interactively #'upcase-region) 'upper)))
(when msgp (message "Region is now %scase" cycle-region-capitalization-last)))
If you mean letter case, then this function works nicely: http://ergoemacs.org/emacs/modernization_upcase-word.html
(defun toggle-letter-case ()
"Toggle the letter case of current word or text selection.
Toggles between: “all lower”, “Init Caps”, “ALL CAPS”."
(interactive)
(let (p1 p2 (deactivate-mark nil) (case-fold-search nil))
(if (region-active-p)
(setq p1 (region-beginning) p2 (region-end))
(let ((bds (bounds-of-thing-at-point 'word) ) )
(setq p1 (car bds) p2 (cdr bds)) ) )
(when (not (eq last-command this-command))
(save-excursion
(goto-char p1)
(cond
((looking-at "[[:lower:]][[:lower:]]") (put this-command 'state "all lower"))
((looking-at "[[:upper:]][[:upper:]]") (put this-command 'state "all caps") )
((looking-at "[[:upper:]][[:lower:]]") (put this-command 'state "init caps") )
((looking-at "[[:lower:]]") (put this-command 'state "all lower"))
((looking-at "[[:upper:]]") (put this-command 'state "all caps") )
(t (put this-command 'state "all lower") ) ) ) )
(cond
((string= "all lower" (get this-command 'state))
(upcase-initials-region p1 p2) (put this-command 'state "init caps"))
((string= "init caps" (get this-command 'state))
(upcase-region p1 p2) (put this-command 'state "all caps"))
((string= "all caps" (get this-command 'state))
(downcase-region p1 p2) (put this-command 'state "all lower")) )
) )
I liked the other answer's technique of comparing this-command and last-command,
so I've incorporated it into my old function. Here's the result:
(defun upcase-word-toggle ()
(interactive)
(let ((bounds (bounds-of-thing-at-point 'symbol))
beg end
regionp)
(if (eq this-command last-command)
(setq regionp (get this-command 'regionp))
(put this-command 'regionp nil))
(cond
((or (region-active-p) regionp)
(setq beg (region-beginning)
end (region-end))
(put this-command 'regionp t))
(bounds
(setq beg (car bounds)
end (cdr bounds)))
(t
(setq beg (point)
end (1+ beg))))
(save-excursion
(goto-char (1- beg))
(and (re-search-forward "[A-Za-z]" end t)
(funcall (if (char-upcasep (char-after))
'downcase-region
'upcase-region)
beg end)))))
(defun char-upcasep (letter)
(eq letter (upcase letter)))
(global-set-key (kbd "C->") 'upcase-word-toggle)