Insert regular double quotes in LaTeX mode with AUCTeX - emacs

How do I rebind the double quote key to simply insert a double quote in a LaTeX buffer with AUCTex enabled?
I tried redefining TeX open and close quote, but that didn't seem to work.
(add-hook 'LaTeX-mode-hook
'(progn
(setq-default TeX-close-quote "\"")
(setq-default tex-close-quote "\"")
(setq-default TeX-open-quote "\"")
(setq-default tex-open-quote "\"")
(setq-default TeX-quote-after-quote t)))
Update
The above code and the accepted answer would have worked, except that I have smartparens enabled. Smartparens helpfully redefines the quote key to insert LaTeX quotes. The code to use normal quotes is below:
(eval-after-load 'latex
'(progn
(require 'smartparens-latex)
;; removes the double quote trigger binding. Now smartparens will
;; insert a regular double quote
(sp-local-pair 'latex-mode "``" "''" :trigger "\"" :actions :rem)))

You can unset the auctex binding as:
(defun my-hook ()
(local-unset-key "\""))
(add-hook 'LaTeX-mode-hook 'my-hook)
Alternately, if you want to use the smart quotes most of the time but occasionally insert a literal double quote, just do C-q ".

Related

wierd behaivour of a global key

I've got in .emacs file:
(defun poc ()
(interactive)
(insert (char-from-name "DOUBLE LOW-9 QUOTATION MARK"))
(global-set-key [f12] 'poc))
(defun konc ()
(interactive)
(insert (char-from-name "LEFT DOUBLE QUOTATION MARK"))
(global-set-key [(shift f12)] 'konc))
Entering F12 key I get
<f12> is undefined
If I enter M-x poc, DOUBLE LOW-9 QUOTATION MARK is inserted. But from this moment on if I enter F12 key again, the quotation mark is correctly inserted.
And this repeats after restarting emacs.
Is something wrong with this code?
emacs-version is 26.1
You're binding the keys in the functions, so you need to run the functions first. This is not recommended. Bind the keys outside the functions, like so:
(defun poc ()
(interactive)
(insert (char-from-name "DOUBLE LOW-9 QUOTATION MARK")))
(global-set-key (kbd "<f12>") #'poc)
Since these are so simple (just inserting characters), you actually don't even need functions.
(global-set-key (kbd "<f12>") (string (char-from-name "DOUBLE LOW-9 QUOTATION MARK")))
(global-set-key (kbd "S-<f12>") (string (char-from-name "LEFT DOUBLE QUOTATION MARK")))
or if you use an encoding like utf-8 (which you should), you can put the characters directly in a string.
(global-set-key (kbd "<f12>") "„")
(global-set-key (kbd "S-<f12>") "“")

How do I disable electric indent on RET but still keep other electric characters (e.g., ‘{’)?

In Emacs 24.4, the default indentation behavior has been changed—new lines are now automatically indented. From the release notes:
*** `electric-indent-mode' is now enabled by default.
Typing RET reindents the current line and indents the new line.
`C-j' inserts a newline but does not indent. In some programming modes,
additional characters are electric (eg `{').
I prefer the old behavior, so I added
(electric-indent-mode 0)
to my .emacs file. However, this disables all electric characters, which is not what I intended.
Is there any way to disable the new behavior while still having characters like ‘{’ or ‘:’ trigger an indentation?
You want to remove ?\n from electric-indent-chars. You can do this globally with:
(setq electric-indent-chars (remq ?\n electric-indent-chars))
or only in a particular mode (e.g. C):
(add-hook 'c-mode-hook
(lambda ()
(setq-local electric-indent-chars (remq ?\n electric-indent-chars))))
By checking the documentation for c-electric-brace, I found that the behavior of electric characters is controlled by the buffer-local variable c-electric-flag. It worked after I added the following lines to my .emacs file:
(add-hook 'c-mode-hook
(lambda ()
(set 'c-electric-flag t)))
(add-hook 'c++-mode-hook
(lambda ()
(set 'c-electric-flag t)))

(ELisp) automatically nesting next line using brace return

I'm completely new to both Lisp and Emacs. In Emacs, when coding in Java for example, I want to be able to type "{" then hit "ENTER" and have the next line be ready for whatever is nested in the braces. For example, if I have the following line:
public void method()
and I type "{" then hit return I should get this:
public void method() {
// indentation applied, no additional tabbing necessary
}
I'm already able to insert by pairs, for example, typing "{" gives "{}" with my cursor between the braces. I did this by adding these lines to the emacs init file:
;; insert by pairs (parens, quotes, brackets, braces)
(defun insert-pair (leftChar rightChar)
(if (region-active-p)
(let (
(p1 (region-beginning))
(p2 (region-end))
)
(goto-char p2)
(insert rightChar)
(goto-char p1)
(insert leftChar)
(goto-char (+ p2 2))
)
(progn
(insert leftChar rightChar)
(backward-char 1) ) )
)
(defun insert-pair-brace () (interactive) (insert-pair "{" "}") )
(global-set-key (kbd "{") 'insert-pair-brace)
To get the auto-nesting I described above, I added these lines:
;; automatically nest next line
(defun auto-nest ()
(insert "\n\n")
(backward-char 1)
(insert "\t")
)
(defun auto-nest-brace () (interactive) (auto-nest) )
(global-set-key (kbd "{ RET") 'auto-nest-brace)
When I start up Emacs, however, I get this message:
error: Key sequence { RET starts with non-prefix key {
What am I doing wrong, and what can I do to fix it? I don't want to use a different key combination to do this. There are a lot of text editors in which this auto-nesting is standard, and it should be easy enough to code up in ELisp.
It's great that you are trying to add this functionality to Emacs yourself, but there's no need to reinvent the wheel here. Emacs already has a command for the purpose of auto-indenting; it's called newline-and-indent. It is bound to C-j by default, but you can rebind it to RET
globally:
(global-set-key (kbd "RET") 'newline-and-indent)
for a specific mode only:
(require 'cc-mode)
(define-key java-mode-map (kbd "RET") 'newline-and-indent)
java-mode-map is defined in cc-mode.el and not available by default, that's why you have to require cc-mode before you can modify java-mode-map.
Note that newline-and-indent indents according to major mode. That is, if you're e.g. in java-mode and press RET in some random location that's not meaningful w/r/t Java syntax, it won't insert additional whitespace at the beginning of the new line.
To read all there is to know about newline-and-indent do
C-h f newline-and-indent RET
I have something similar in my emacs config which I have been using for a while. It calls 'newline-and-indent twice then moves the point one line up before indenting correctly. Here is the snippet of code to do this from my config file:
;; auto indent on opening brace
(require 'cc-mode)
(defun av/auto-indent-method ()
"Automatically indent a method by adding two newlines.
Puts point in the middle line as well as indent it by correct amount."
(interactive)
(newline-and-indent)
(newline-and-indent)
(forward-line -1)
(c-indent-line-or-region))
(defun av/auto-indent-method-maybe ()
"Check if point is at a closing brace then auto indent."
(interactive)
(let ((char-at-point (char-after (point))))
(if (char-equal ?} char-at-point)
(av/auto-indent-method)
(newline-and-indent))))
(define-key java-mode-map (kbd "RET") 'av/auto-indent-method-maybe)
Pretty straightforward as you can see. Hopefully it will work for you. I have not used it in any other modes except java.
You want a combination of auto pairs (or alternatives) plus auto indentation. Check out the emacswiki on the former: http://www.emacswiki.org/emacs/AutoPairs
And on the latter:
http://www.emacswiki.org/emacs/AutoIndentation

Auto indent after inserting a semicolon or curly brace

GNU Emacs 24.3.1
Hello,
I am doing some coding in Java using emacs. Just to make my coding easier I want to auto indent each time I insert a semi-colon or curly brace {
;; Auto indent for java mode
(add-hook 'java-mode-hook '(lambda ()
(local-set-key (kbd "RET") 'newline-and-indent)))
(add-hook 'java-mode-hook '(lambda ()
(local-set-key (kbd "{") 'newline-and-indent)))
(add-hook 'java-mode-hook '(lambda ()
(local-set-key (kbd ";") 'newline-and-indent)))
The return works as expected. However, the curly brace and semi-colon just returns without entering the the ; or {.
Is this possible?
many thanks for any suggestions,
It's possible. Here is one way of doing it (replace the second and third call to add-hook with this):
(defun java-autoindent ()
(when (and (eq major-mode 'java-mode) (looking-back "[{;]"))
(newline-and-indent)))
(add-hook 'post-self-insert-hook 'java-autoindent)
The way this works is that every time you type a character in a java-mode buffer, Emacs will
check if that character is { or ;, and if that's the case
run newline-and-indent.

paredit curly brace matching in swank-clojure repl

I am using emacs 24 on Windows 7 and have installed technomancy's clojure-mode along with paredit 23 beta. I load the source file from my leiningen project and get a repl using clojure-jack-in. The problem is that while paredit is enabled in both Clojure mode and the repl, curly braces are not matched in the repl only in source files.
How can I get it to match braces in the repl as well?
I added the following to my .emacs file, that does the trick for me (I did not invent this myself, it's a snippet I found somewhere online - but I can't remember where):
(defun setup-slime-repl-paredit ()
(define-key slime-repl-mode-map
(kbd "DEL") 'paredit-backward-delete)
(define-key slime-repl-mode-map
(kbd "{") 'paredit-open-curly)
(define-key slime-repl-mode-map
(kbd "}") 'paredit-close-curly)
(modify-syntax-entry ?\{ "(}")
(modify-syntax-entry ?\} "){")
(modify-syntax-entry ?\[ "(]")
(modify-syntax-entry ?\] ")[")
(modify-syntax-entry ?~ "' ")
(modify-syntax-entry ?, " ")
(modify-syntax-entry ?^ "'")
(modify-syntax-entry ?= "'"))
(add-hook 'slime-repl-mode-hook 'setup-slime-repl-paredit)
(add-hook 'slime-repl-mode-hook 'enable-paredit-mode)
Grab Phil Hagelberg's durendal package, which
provide some clojure-specific enhancements to slime, then try this snippet:
(require 'durendal)
(durendal-enable t)
(defun slime-clojure-repl-setup ()
(when (string-equal (slime-lisp-implementation-name) "clojure")
(set-syntax-table clojure-mode-syntax-table)
(setq lisp-indent-function 'clojure-indent-function)))
(add-hook 'slime-repl-mode-hook 'slime-clojure-repl-setup)
In future, Phil may include the functionality of durendal in swank-clojure itself as an additional lisp payload, at which point the above would become unnecessary.