GNU emacs: setting keybindings to highlight text with shift key - emacs

I'm trying to set some keybindings to use the Shift key to highlight text. I could use pc-selection-mode, but that doesn't offer all the key bindings I want. For example, I'd like to be able to shift-mark an entire paragraph by pressing Shift-Ctrl-down which I can do in most MS text editors, but pc-selection-mode doesn't allow you to do this.
I found this website which has a shift_mark.el file I can use to set all the key bindings I want. I've put in my .xemacs/init.el file to load shift_mark.el.
This is the error:
Warning (initialization): An error occurred while loading `/home/theory/phrkaj/\
.xemacs/init.el':
Wrong type argument: arrayp, (shift right)
So I've run Emacs with --debug-init to try and find the problem. This is what the debugger came up with:
Debugger entered--Lisp error: (wrong-type-argument arrayp (shift right))
signal(wrong-type-argument (arrayp (shift right)))
global-set-key((shift right) shift-mark-forward-char)
eval-buffer(#<buffer *load*<3>> nil "/home/theory/phrkaj/shift_mark.el" nil t) ; Reading at buffer position 1476
load-with-code-conversion("/home/theory/phrkaj/shift_mark.el" "/home/theory/phrkaj/shift_mark.el" nil nil)
load("~/shift_mark.el")
eval-buffer(#<buffer *load*<2>> nil "/home/theory/phrkaj/.xemacs/init.el" nil t) ; Reading at buffer position 25
load-with-code-conversion("/home/theory/phrkaj/.xemacs/init.el" "/home/theory/phrkaj/.xemacs/init.el" nil nil)
load("/home/theory/phrkaj/.xemacs/init.el" nil nil t)
load-file("/home/theory/phrkaj/.xemacs/init.el")
eval-buffer(#<buffer *load*> nil "/home/theory/phrkaj/.emacs" nil t) ; Reading at buffer position 253
load-with-code-conversion("/home/theory/phrkaj/.emacs" "/home/theory/phrkaj/.emacs" t t)
load("~/.emacs" t t)
#[nil "^H\205\264^# \306=\203^Q^#\307^H\310Q\2027^# \311=\2033^#\312\307\313\314#\203#^#\315\2027^#\312\307\313\316#\203/^#\317\2027^#\315\2027^#\307^H\320Q^Z\321^S\322\n\321\211#\210^K\321=\203_^#\323\324\325\307^H\326Q!\"^\\322\f\$
command-line()
normal-top-level()
Here's part of the shift_mark.el file which defines the highlighting of one char forward:
(defun shift-mark-forward-char ()
(interactive)
(shift-mark 'forward-char))
(global-set-key '(shift right) 'shift-mark-forward-char)
Any help is appreciated.

Under GNU Emacs, the key binding should look like
(global-set-key [(shift right)] 'shift-mark-forward-char)
([…] constructs a literal array). But I suspect you're going at this the wrong way. Are you running GNU Emacs, XEmacs, or both? What versions? Unless you're running extremely old versions, pc-selection-mode should do what you want under GNU Emacs, and no setup should be required under XEmacs. If you run both GNU Emacs and XEmacs, you can use the following code in your .emacs:
(defvar running-xemacs (string-match "XEmacs" emacs-version))
(if (not running-xemacs)
(pc-selection-mode 1))

C-hv shift-select-mode RET
shift-select-mode is a variable defined in `simple.el'.
Its value is nil
Documentation:
When non-nil, shifted motion keys activate the mark momentarily.
While the mark is activated in this way, any shift-translated point
motion key extends the region, and if Transient Mark mode was off, it
is temporarily turned on. Furthermore, the mark will be deactivated
by any subsequent point motion key that was not shift-translated, or
by any action that normally deactivates the mark in Transient Mark mode.
See `this-command-keys-shift-translated' for the meaning of
shift-translation.
You can customize this variable.
This variable was introduced in GNU Emacs 23.1:
** Temporarily active regions
* The new variable shift-select-mode, non-nil by default, controls
shift-selection. When Shift Select mode is on, shift-translated
motion keys (e.g. S-left and S-down) activate and extend a temporary
region, similar to mouse-selection.
* Temporarily active regions, created using shift-selection or
mouse-selection, are not necessarily deactivated in the next command.
They are only deactivated after point motion commands that are not
shift-translated, or after commands that would ordinarily deactivate
the mark in Transient Mark mode (e.g., any command that modifies the
buffer).

Related

function using font lock functions requires restarting font lock mode

I am confused about how font lock mode gets engaged. I do not have a statement that launches font lock mode in my init.el, but apparently it always runs as a minor mode. Furthermore I have the following function:
(defun testregexfunc ()
(interactive)
(make-variable-buffer-local 'font-lock-extra-managed-props)
(add-to-list 'font-lock-extra-managed-props 'invisible)
(font-lock-add-keywords nil
'(("\\(\\[\\)\\([a-zA-Z0-9_]+\\)\\(\\]\\)"
(1 '(face nil invisible t))
(3 '(face nil invisible t))))))
It uses font-lock specific things. But it only takes effect once I do M-x testregexfunc followed by M-x font-lock-mode twice. First time disables font lock mode second time starts it. But it is not running as a major mode now, as the buffer still displays whatever mode the buffer was in before. Okay, so I guess the function sets some values and only take effect once the mode restarts. I figured maybe I need to add a hook to font lock mode like this:
(add-hook
'font-lock-mode
'testregexfunc)
No... does not do anything. What do I need to do to not have to restart font lock mode for the function to work?
I got that function from here and modified it some. I don't understand most of its definition and the documentation on font lock does not really help me much:
https://emacs.stackexchange.com/questions/28154/using-font-lock-regexp-groups
I think the functions you are looking for are font-lock-flush and font-lock-ensure which together declare the buffer's font-locking out-of-date and then refontify it. So, you could alter your function as follows,
(defun testregexfunc (arg)
"Fontify buffer with new rules. With prefix arg restore default fontification."
(interactive "P")
(if arg
(font-lock-refresh-defaults) ;restore the defaults for the buffer
(make-variable-buffer-local 'font-lock-extra-managed-props)
(add-to-list 'font-lock-extra-managed-props 'invisible)
(font-lock-add-keywords nil ;make the "[" and "]" invisible
'(("\\(\\[\\)\\([a-zA-Z0-9_]+\\)\\(\\]\\)"
(1 '(face nil invisible t))
(3 '(face nil invisible t)))))
(font-lock-flush) ;declare the fontification out-of-date
(font-lock-ensure))) ;fontify the buffer using new rules

how to find syntax error in elisp

if this is a duplicate please delete.
I was just editing my .emacs file and after i had my changes done. I
restarted emacs. But whet it comes up again it claims that the config
file has some errors. And i should start with "--debug-init" thats what
i also have done. But it did not helped me to solve nor find the syntax error.
So is there a way to find the lines which have syntaxerror. Or even better if
there would be something like a (e)lisp lint like you have for e.g xml, json, js
and so on. Thanks in advance
Edit
Managed to get the dump:
Debugger entered--Lisp error: (void-variable whitespace-mode)
redisplay_internal\ \(C\ function\)()
delete-process(#<process server-client-test>)
Someunicodefoo" [("server") server-use-tcp server-auth-dir server-socket-dir generate-new-buffer " *temp*" funcall make-byte-code 0 "Someunicodefoo" vconcat vector [buffer-name kill-buffer] 2 "\n\n(fn)" insert-file-contents-literally expand-file-name looking-at "127\\.0\\.0\\.1:[0-9]+ \\([0-9]+\\)" comm process-attributes string-to-number match-string 1 t :other delete-process make-network-process :name "server-client-test" :family local :server nil :noquery :service] 14 "\n\n(fn)"])
server-running-p("server")
byte-code("Someunicodefoo" [load-file-name buffer-file-name recentf-menu-before user-emacs-directory recentf-save-file server-name add-to-list load-path file-name-directory "../packages" tool-bar-mode 0 load "ergoemacs-keybindings/ergoemacs-mode" ergoemacs-mode 1 "init_load_packages" "init_version" "init_functions" "init_settings" "init_aliases" "init_abbrevs-lisp-mode" "init_keybinding" "init_mouse" "init_clean_menus" require recentf "Close" ".recentf" recentf-mode server server-running-p t server-start] 4)
load("/home/dirk/.emacs.d/ergoemacs_1.9.3.1/site-lisp/../ergoemacs/init")
eval-buffer(#<buffer *load*-842275> nil "/home/dirk/.emacs.d/ergoemacs_1.9.3.1/site-lisp/site-start.el" nil t) ; Reading at buffer position 1432
load-with-code-conversion("/home/dirk/.emacs.d/ergoemacs_1.9.3.1/site-lisp/site-start.el" "/home/dirk/.emacs.d/ergoemacs_1.9.3.1/site-lisp/site-start.el" nil nil)
load("/home/dirk/.emacs.d/ergoemacs_1.9.3.1/site-lisp/site-start.el" nil nil t)
load-file("~/.emacs.d/ergoemacs_1.9.3.1/site-lisp/site-start.el")
eval-buffer(#<buffer *load*> nil "/home/dirk/.emacs" nil t) ; Reading at buffer position 2883
load-with-code-conversion("/home/dirk/.emacs" "/home/dirk/.emacs" t t)
load("~/.emacs" t t)
command-line()
normal-top-level()
One way to do it is to open the file and mark the first half of it and apply eval-region. If an error is reported, mark half of it and repeat. If an error isn't reported, apply it to the other half.
This way you could narrow down the problem and once you have found it you can hopefully figure out the problem.
Another technique I use to find incorrect parentheses is to add an extra parenthesis to the beginning and end of the file, respectively and run C-M-f and C-M-b from the beginning and end, respectively. If there are incorrect parentheses, they will match the newly added ones, and the point will move to the incorrect ones.
EDIT: You can use M-x check-parens to check for unmatched parentheses.
For "lint"-like checking, there's elint.el, but more importantly, there's the byte-compiler: M-x byte-compile-file RET lets you byte-compile a file and will give you all kinds of warnings about syntax and style.
But your backtrace indicates that the problem is probably not one of syntax. Most like there's a whitespace-mode that appears somewhere in a menu (i.e. in an :enable property of a menu entry), and that signals an error because the whitespace-mode variable happens not to be defined yet. So as Phils suggests, loading whitespace would probably work around the problem.

mmm-mode: Implementing C++ mode as a submode

We use an internal scripting language (let's call it pkc), which allows for embedding C++ code. The C++ code segments are delimited by {{{ and }}} markers.
I create an emacs mode for this language, using Generic Mode and mmm-mode.
Here is what I got (stripped down non-essential parts for posting here):
(require 'generic-x)
(setq pkc-imenu-generic-expression
'(("macros" "^[ \t]*macro[ \n\t]+\\([a-zA-Z0-9_]+\\)" 1)
("functions" "function[ \n\t]+\\([a-zA-Z0-9_]+\\)" 1)
))
(require 'cc-mode) ;; for c++-mode
(require 'mmm-auto)
(setq mmm-global-mode 'maybe)
(define-generic-mode
'pkc-mode ;; name of the mode to create
'("//" ("/*" . "*/")) ;; comments are same C++ comments
'( ... ) ;; some keywords
'("\\.pkc$") ;; files for which to activate this mode
;; other functions to call
'((lambda ()
(mmm-mode 1)
(setq mmm-submode-decoration-level 2)
(setq imenu-generic-expression pkc-imenu-generic-expression)
(which-function-mode 1)
(c-initialize-cc-mode t)
(c-init-language-vars-for 'c++-mode)
(c-common-init 'c++-mode)
(c-update-modeline)
(message "pkc-mode[mmm] is on")))
"A mode for pkc source files" ;; doc string for this mode
)
(mmm-add-classes
'((embedded-c++
:submode c++-mode
:face mmm-default-submode-face
:front "{{{"
:front-offset -1
:back "}}}"
:back-offset 1)))
(mmm-add-mode-ext-class 'pkc-mode nil 'embedded-c++)
When I load the source file with embedded C++ code segments, they are highlighted somewhat less than what would have been if C++ mode was the major mode (that's not my problem, however). When the cursor in C++ code, the modeline changes to pkc/l[C++/l] (as expected).
The problem is, whenever I press TAB to indent a line, I get the error Wrong type argument: stringp, nil. When I turned on debugger, this is the stack trace I see:
Debugger entered--Lisp error: (wrong-type-argument stringp nil)
c-syntactic-skip-backward(nil nil t)
c-looking-at-decl-block(nil t)
c-guess-basic-syntax()
c-indent-line()
#[nil \302>\203)\212\303 \210\304\305x\210\303 \210\306 )i\306 X\203\"\307 !\202'\212\307 !))\20 \207" [indent-line-function column (indent-relative indent-relative-maybe) beginning-of-line "\n " nil current-indentation indent-line-to] 2 1908700 nil]()
c-indent-command(nil)
c-indent-line-or-region(nil nil)
call-interactively(c-indent-line-or-region nil nil)
Looking at the definition of c-looking-at-decl-block and particularly the call to `c-syntactic-skip-backward, I find:
(c-syntactic-skip-backward c-block-prefix-charset limit t)
Examining the first argument c-block-prefix-charset reveals that its value is nil. Doing the same from a plain C++ buffer shows a non-nil value. So, I suspect that C++-mode requires some initialization that's not being properly done.
So, my questions are:
What am I missing in the C++-mode initialization section in my implementation?
Am I using the right approach (combining generic-x and mmm-mode)? Is there a better approach?
I also see another error (File mode specification error) when I load the file, but I suspect the problem is the same or something similar.
Thanks for your time.
(Rewritten after the discussion in the comments, for any later visitors).
The setup in the question is basically fine, but to work with current c++-mode you need to use an updated version of mmm-mode. It's available at the GitHub project page and also at Melpa.
The relevant patches (one, two, three) added new entries to the value of mmm-save-local-variables, which mmm-mode uses to decide which local variables to save or restore when leaving or entering a submode region.
From what I understand, a future update to cc-mode can add more such vars, so the list may need to be updated from time to time.
Extra tip: to have better indentation in the subregions, you may want to wrap the submode's indent-line-function with some code that will narrow the buffer before calling it (example here). Depending on the indentation function (and whether it calls widen), it may or may not help.

Configuring emacs for latex - void-variable LaTeX-mode-hook

Following this pdf document I added the following to my ~/.emacs file:
(load "auctex.el" nil t t)
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq TeX-PDF-mode t) ;; .pdf statt .dvi per default:
;;Zeilenumbruch
;;(add-hook ’LaTeX-mode-hook ’turn-on-auto-fill)
;;Syntax Higlight
(add-hook ’LaTeX-mode-hook ’turn-on-font-lock)
;; Mathe Modus
(add-hook ’LaTeX-mode-hook ’LaTeX-math-mode)
;; Reftex einflechten und laden
(setq reftex-plug-into-AUCTeX t)
(add-hook ’LaTeX-mode-hook ’turn-on-reftex)
;; Satzende ". " statt ". ". " fuer M-k: loeschen bis Satzende usw.
;;(setq sentence-end "[.?!][]\"’)}]*\\($\\| \\| \\)[
;;]*") ;; Da ist ein "Newline in der Zeile!"
;;(setq sentence-end-double-space nil)
;;direkte Rechtschreib Korrektur:
;;(add-hook ’LaTeX-mode-hook ’flyspell-mode)
;; Nur benutzen falls Auctex > 11.81 mit preview-latex:
(load "preview-latex.el" nil t t)
;; aspell ist besser als ispell.
;; Zeile kommentieren, falls nicht installiert:
(setq-default ispell-program-name "aspell")
;; Deutsche Rechtschreibung falls \usepackage{ngerman}
;; oder german benutzt wird
(add-hook ’TeX-language-de-hook
(function (lambda () (ispell-change-dictionary "german8"))))
Unfortunately emacs doesn't start now, instead it gives the error
Warning (initialization): An error occurred while loading `/home/../.emacs':
Symbol's value as variable is void: ’LaTeX-mode-hook
To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file. Start Emacs with
the `--debug-init' option to view a complete error backtrace.
When starting with --debug-init it gives the following information
Debugger entered--Lisp error: (void-variable ’LaTeX-mode-hook)
(add-hook ’LaTeX-mode-hook ’turn-on-font-lock)
eval-buffer(#<buffer *load*> nil "/home/../.emacs" nil t) ; Reading at buffer position 812
load-with-code-conversion("/home/../.emacs" "/home/../.emacs" t t)
load("~/.emacs" t t)
...
I tried using latex-mode-hook instead. I searched for a solution, but I seem to be the only one having exactly this problem.
I'm using Ubuntu 12.04 with the latest Emacs and Auctex. If required I'll post version information, but I rather think that something has to be added into the configuration or any package has to be installed instead.
How can I get emacs work with that configuration?
Are you using the wrong single quote character? it seems to be some kind of a backward quote instead of a plain single quote. Try
'LaTeX-mode-hook
instead of
’LaTeX-mode-hook
(and likewise for all other occurrences of that character).
As Thomas also said, the back quote is not the character you want to use there, it should be the single straight quote. But, in general, if you get "symbol's value as variable is void" error, it means the same as NPE (null pointer exception) in other languages. The way to check what went wrong is like so:
Move point to the variable that gives the problem and C-h v (or M-x describe-variable [name of the variable without quote]). You can use TAB to complete the variable name as you type to see if you by chance didn't mistype it.
Once you see the buffer that describes the variable - you know you've fixed the error.
Now, if you have LaTeX mode set through auctex.el, then latex-mode-hook must exist. However, you need to make sure that auctex.el actually loads and requires latex-mode. The way it does so isn't an idiomatic way for Emacs to do it, most of the time you add the source files for the mode to the load-path variable and then (require 'mode-name) or load the mode conditionally once Emacs opens the type of the file associated with it (makes startup time for Emacs shorter) through autoload as described here: Emacs: Best-practice for lazy loading modes in .emacs? .
However, whenever you see a variable called [something]-mode-hook it means that this is a list of functions you want to call when [something] mode loads up. If the [something] mode at all exists, there's a 99.99% chance that variable exists too (can't be void). So, if it is void - you need to make sure that the mode it belongs to at all loads.

Emacs Modes: "Command attempted to use minibuffer while in minibuffer"

Scenario:
I start to type M-x to type a command
I switch to another emacs window/buffer because I realise I'm executing the command in the wrong window
I start to type M-x again to execute the command in the correct window
Result: I get the dreaded "Command attempted to use minibuffer while in minibuffer"
This happens to me multiple times a day while using emacs, and not just in this scenario. This behaviour is highly user-hostile (ref. Modes and Pseudo-modes in The Humane Interface by Jef Raskin)
Is there a way to customize emacs behaviour so that instead of giving this error, it just cancels the first minibuffer and replaces it with a new one?
You can set the variable enable-recursive-minibuffers, which will prevent that error message from coming up. But it just enables multiple calls to the minibuffer - it doesn't redirect the current minibuffer's command to the new buffer. You can give this a try, but I think it'll be more confusing because the original action is still pending...
M-x is bound to 'execute-extended-command, and re-hosting (changing the original buffer) for that command is kind of like programming with continuation. i.e. you call a subroutine from location X, but instead of returning to X when done, you return to Y. I personally think it'd open up more confusion than it'd solve. But I understand the frustration (and know others who have the same frustration).
Indeed this emacs "feature" is aggressive and annoying.
I found this to be the right answer to the problem .Most likely you lost focus of the minibuffer because you switched windows with the mouse and NOT a minibuffer command. So whenever you lose focus using the mouse, the minibuffer will be cleared. Check this post. It works for me and it's way better than recursive minibuffers which will cause a headache
http://trey-jackson.blogspot.com/2010/04/emacs-tip-36-abort-minibuffer-when.html
I'm not sure if there is such a customization, but the way I avoid this is hitting ctrl-g to cancel the command I was in the middle of writing in the minibuffer.
Since my first answer doesn't directly give you what you want, I thought I'd come up with a real solution. This is what I have:
(defvar my-execute-extended-command-source-buffer nil
"var holding the buffer to which the extended-execute-command should apply")
(defvar in-my-execute-extended-command nil
"internal use - indicates whether we're in a 'recursive edit' of sorts")
(defun my-execute-extended-command (command)
"home-grown version of execute-extended-command that supports re-hosting the buffer"
(interactive (list (if in-my-execute-extended-command
nil
(let ((in-my-execute-extended-command t))
(setq my-execute-extended-command-source-buffer (current-buffer))
(completing-read "My-x " obarray 'commandp t nil 'extended-command-history nil nil)))))
(if in-my-execute-extended-command
(progn (setq my-execute-extended-command-source-buffer (current-buffer))
(select-window (minibuffer-window)))
(switch-to-buffer my-execute-extended-command-source-buffer)
(call-interactively (symbol-function (intern command)))))
I've tested it this way. I bound it to a key (F10 in my case b/c I didn't want to lose M-x). Then, with two windows open, each showing a different buffer (say A and B):
From window showing buffer A: F10 isearch-for
Switch from minibuffer to window showing A: C-x o
Switch from window showing A to that showing B: C-x o
"re-host" the command from buffer B: F10
Now back in the minibuffer, finish the command ward RET
When I started typing a search term, the search applied to buffer B.
This only replaces the M-x functionality, not the commands invoked from M-x. Also, this version does not support the prefix argument.
Hopefully this is what you want.
Here you go:
;; automatically cancel the minibuffer when you switch to it, to avoid
;; "attempted to use minibuffer" error.
;; cy was here
(provide 'cancel-minibuffer)
(defun cancel-minibuffer-first (sub-read &rest args)
(let ((active (active-minibuffer-window)))
(if active
(progn
;; we have to trampoline, since we're IN the minibuffer right now.
(apply 'run-at-time 0 nil sub-read args)
(abort-recursive-edit))
(apply sub-read args))))
(advice-add 'read-from-minibuffer :around #'cancel-minibuffer-first)
Can anyone improve on the following?
I've given up and just want to set \C-w to cancel any previous minibuffer before opening a new one (like doing \C-g\C-w)
So far thanks to Trey I've got:
(defun cancel-completing-read ()
(if (> (minibuffer-depth) 0) (exit-minibuffer))
(completing-read "My-x " obarray 'commandp t nil 'extended-command-history nil nil))
(defun cancel-and-execute-command (command)
(interactive (list (cancel-completing-read)))
(call-interactively (symbol-function (intern command))))
(global-set-key "\M-x" 'cancel-and-execute-command)
What command should I use in the place of exit-minibuffer above?
I've tried
keyboard-escape-quit
exit-minibuffer
keyboard-quit