I am typing a math document in Russian using Emacs 24.3.1 and AUCTeX 11.87. I am using the russian-computer input method to type Russian. It would be very convenient to disable this input method inside math delimiters such as \( \) so that inside formulas I automatically switch to typing English without pressing Ctrl-\. For example, I could type "Рассмотрим формулу \(\)" ("Consider the formula \(\)"), put the point between the parentheses and start typing \forall. Currently, I would be typing \ащкфдд, but I would like Emacs to recognize that I am inside a formula and switch off the Russian input method.
I suspect that this can be done using post-self-insert-hook and texmathp from AUCTeX, but I am not sure if this is the most elegant method.
I found solution to the problem at http://lists.gnu.org/archive/html/help-gnu-emacs/2009-08/msg00200.html The code works almost as expected. But drawback is that one can't switch to English from e.g. Russian outside of math delimiters.
This function does the job: switch between english and your current input method.
(defun LaTeX-dynamic-input-method-toggle-maybe ()
(when (or (and current-input-method
(texmathp))
(and (not current-input-method)
(not (texmathp))))
(toggle-input-method)))
"Define a minor-mode, so this behaviour can be conviniently
enabled/disabled. The minor-mode puts the above function on the
`post-command-hook', so that the above function gets called
everytime you do something (e.g. move point)."
(define-minor-mode LaTeX-dynamic-input-method
"Dynamically disable input-method in math-mode."
nil nil nil
(if LaTeX-dynamic-input-method
(add-hook 'post-command-hook 'LaTeX-dynamic-input-method-toggle-maybe nil t)
(remove-hook 'post-command-hook 'LaTeX-dynamic-input-method-toggle-maybe t)))
Enable the mode in LaTeX-mode.
(add-hook 'LaTeX-mode-hook 'LaTeX-dynamic-input-method)
Related
I can't seem to get abbrevs working in ess-mode when editing R files.
I have ess-mode hook to activate abbrev-mode, but I get no expansion for my global abbrevs. When I do add-mode-abbrev I can add an abbrev, but it is not written to my abbrev file and of course doesn't get ever expanded (after read-abbrev-file or restart).
If I by hand add something like this into my abbrev file and reload:
(define-abbrev-table 'ess-mode-abbrev-table
'(
("-=" "<-" nil 0)
))
it doesn't work either.
How can I make ess-mode aware of my abbrevs?
I'm using a workaround setting the ess-mode-abbrev-table locally. First I define the abbrev-table, just in case that I load my abbrev-tables later and don't want an error when starting ESS:
(define-abbrev-table 'ess-mode-abbrev-table nil)
(add-hook 'ess-mode-hook
(lambda ()
(setq local-abbrev-table ess-mode-abbrev-table)))
I prefer to use the same abbrevs in the interpreter, so this comes in handy:
(dolist (hook '(ess-mode-hook inferior-ess-mode-hook))
(add-hook hook
(lambda ()
(setq local-abbrev-table ess-mode-abbrev-table))))
Abbreviations are expected to have word-syntax - forward-word is used internally.
Writing/re-defining abbrevs is a costly operation, takes some seconds. Thus this is not done after any edit/definition - except when C-x C-s is asked for explicitly. Abbrev-file commonly is written when session is closed.
I wrote the following code to highlight dollar signs in AUCTeX buffers in different colors, but then I found that it's even highlighting dollar signs in comments, which was unintended, but I am starting to like it. But now just for curiosity, I wonder if that can be avoided.
(defun my-LaTeX-mode-dollars ()
(font-lock-add-keywords
nil
`((,(rx "$") (0 'success t)))
t))
(add-hook 'LaTeX-mode-hook 'my-LaTeX-mode-dollars)
From the documentation of font-lock-keywords:
MATCH-HIGHLIGHT should be of the form:
(SUBEXP FACENAME [OVERRIDE [LAXMATCH]])
OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing
fontification can be overwritten. If keep', only parts not already
fontified are highlighted. Ifprepend' or `append', existing
fontification is merged with the new, in which the new or existing
fontification, respectively, takes precedence.
In other words, if you drop the t after 'success, it will no longer fontify dollar signs in comments and strings.
EDIT:
Apparently, the above solution is not sufficient in this situation, probably because dollar signs have been colored using another face earlier.
One way that might work is to not pass the HOW parameter (currently t) to font-lock-add-keywords. This means that they should be added to the end of the list. However, this might cause other things to stop working.
If we need a bigger hammer, you can write a bit more advanced rule that inspects the current fontification, and decides what to do upon this. For example, the following is used by Emacs to add a warning face to parentheses placed at column 0 in strings:
"^\\s("
(0
(if
(memq
(get-text-property
(match-beginning 0)
'face)
'(font-lock-string-face font-lock-doc-face font-lock-comment-face))
(list 'face font-lock-warning-face 'help-echo "Looks like a toplevel defun: escape the parenthesis"))
prepend)
A third way to do this is to replace the regexp (rx "$") with the name of function that could search for $ and check that it appears in the correct context. One example of such font-lock rules can be found in the standard Emacs package cwarn.
I would really like to disable flyspell checking of camel-cased words - most of the times they aren't valid English. Is there any way to do that?
You could set the variable flyspell-generic-check-word-predicate to a function that returns nil if point is at a camel-cased word.
Note that this variable automatically becomes buffer-local when set, so setting it with a plain setq in your .emacs file wouldn't work. Set it from a mode hook instead, e.g.:
(add-hook 'java-mode-hook
(lambda ()
(setq flyspell-generic-check-word-predicate 'my-new-function)))
(replacing my-new-function with the name of your camel-case detecting function)
In LaTeX mode C-c C-c is bound to:
(TeX-command-master &optional OVERRIDE-CONFIRM)
Normally this interactive function runs a command, perhaps a LaTeX compilation, asking for confirmation.
In tex-buf.el it reads:
If a prefix argument OVERRIDE-CONFIRM is given, confirmation will
depend on it being positive instead of the entry in `TeX-command-list'.
This is a bit cryptic for me and reading C-h v TeX-command-list didn't help.
How can I pass the prefix argument to "TeX-command-master" so that I avoid all the confirmation requests?
Take a look at Emacs' documentation to find out about prefix arguments. In general, you can pass a command a prefix argument with C-u followed by a number. For one-digit numbers, you can also just type Meta followed by the digit. Thus to pass a positive prefix argument to TeX-command-master you could type:
M-1 C-c C-c
However, this will actually add another minibuffer confirmation, namely about the shell command to be used to compile the LaTeX source. Without the prefix argument, a command-dependent default is used for that.
If you want to avoid the question about the command to use, you can bind the undocumented variable TeX-command-force to "LaTeX" via:
(setq TeX-command-force "LaTeX")
However, this will have the downside that you're basically binding C-c C-c to the "latex" command, you cannot use any of the other commands such as "bibtex" or "view".
Other than that, LaTeX-mode does not allow for any customization of C-c C-c. Your best options are to either advise the function TeX-command-query or to bind C-c C-c to a wrapper function to set TeX-command-force dynamically. The latter would probably be the preferred option if you also want to auto-save the buffer.
It seems that the mystery of the OVERRIDE-CONFIRM continues. In the meantime a fellow suggests that, if we are unable to manage TeX-command-master, we can simply rewrite it.
In my version, based on his, if the buffer is not modified, the external viewer is launched; if the buffer is modified the compiler is run.
Everything with no confirmation for saving or running the given command.
(defun my-run-latex ()
(interactive)
(if (buffer-modified-p)
(progn
(setq TeX-save-query nil)
(TeX-save-document (TeX-master-file))
(TeX-command "LaTeX" 'TeX-master-file -1))
(TeX-view)))
Of course one can bind my-run-latex to whatever keybinding.
On the user's point of view this is a solution to my own question.
Do I click the close tag? Well, on the curious guy point of view I am still interested in understanding the mysterious TeX-command-master technicalities.
If someone should happen to know...
P.S.
Yes, TeX-save-query overrides the save-file request, also with TeX-command-master, that is C-c C-c. But you will still be asked to confirm the command action.
Build & view
Again, this solution, instead of modifying the behaviour of the TeX-command-master, rewrites it. The rewritten version of the command, named build-view, follows a rather straightforward logic.
If the LaTeX file buffer is not-modified, it runs the default viewer;
If the buffer is dirty, it runs the default LaTeX compiler and, after the build, opens the output in the default viewer.
Here's the code:
(defun build-view ()
(interactive)
(if (buffer-modified-p)
(progn
(let ((TeX-save-query nil))
(TeX-save-document (TeX-master-file)))
(setq build-proc (TeX-command "LaTeX" 'TeX-master-file -1))
(set-process-sentinel build-proc 'build-sentinel))
(TeX-view)))
(defun build-sentinel (process event)
(if (string= event "finished\n")
(TeX-view)
(message "Errors! Check with C-`")))
You can now type M-x build-view and start the told build-view process or associate it with a new keybinding such as “F2”:
(add-hook 'LaTeX-mode-hook '(lambda () (local-set-key (kbd "<f2>") 'build-view)))
Note: As suggested by Tyler, TeX-save-query variable is changed locally, therefore the old C-c C-c/ TeX-command-master is unaffected and will keep asking confirmations.
Do edit this code to make it better or easier to read!
I puzzled over the OVERRIDE-CONFIRM bit for a while, and couldn't figure out how it was supposed to work. If you want to automatically run Latex on your file, without being bothered about saving it first, or confirming that you want latex (rather than view, bibtex etc), you could use a function like this:
(defun my-run-latex ()
(interactive)
(TeX-save-document (TeX-master-file))
(TeX-command "LaTeX" 'TeX-master-file -1))
Bind this to something handy, and you'll still have C-c C-c for when you want to use the default processing commands. You may want to modify the TeX-command line if "Latex" isn't the processor you want to call.
If you are just looking to compile the latex source without a confirmation dialog, just add the following to your .emacs:
(setq TeX-command-force "")
You can then compile the source with C-c C-c and it won't ask to confirm. The only problem with this solution is that you can no longer change the command, but with most documents you won't want to. I might suggest that at the same time you can add this to your .emacs for even more flexibility, giving you a C-c C-c equivalent to the former behavior:
(define-key LaTeX-mode-map "\C-c\C-a"
;;; 'a' for ask, change to anything you want
(lambda (arg) (interactive "P")
(let ((TeX-command-force nil))
(TeX-command-master arg))))
You can then just work away at your document, do a C-x C-s, C-c C-c and then C-c C-v to see it. Like others have suggested you can also do the same for the save command and have it compile automatically on save, but some of my documents are in CVS and so I avoid putting hooks on that.
Credit to Ivan for some help on this one - don't know if he is on StackOverflow
I think the gist of this question is "how do I quickly compile my TeX document from AUCTeX without all the key presses and confirmations?"
My answer to that is to use the latexmk command rather than trying to coerce AUCTeX to do it.
latexmk -pdf -pvc myfile.tex
latexmk will monitor the file in question and rebuilt it as soon as you save it. If you use a good pdf viewer, it will notice the change in PDF and re-display it immediately. On OS X, skim works well for this.
Two questions about Emacs Tuareg for OCaml:
I have seen some configurations where it displays an alpha symbol instead of a'. How can I turn this on?
What is the keyboard shortcut for "evaluate phrase" or "evaluate buffer"?
I can only answer part (2):
To start an Ocaml top-level: C-c C-s
To evaluate a phrase: C-x C-e
To evaluate a buffer: C-c C-b
To evaluate a region: C-c C-r
Launch the tuareg mode (e.g. by M-x tuareg-mode), and look at its documentation pressing C-h m.
The symbols are displayed by the sym-lock mode only works for Xemacs and its variants I'm afraid, but you'll find how to configure it in your .emacs in the help mentioned above. The shortcut to execute a statement is C-x C-e (see section 'Special keys' of the help).
I'm not sure if this is exactly what you mean for part 1 of your question, but I have a font-lock-mode keyword to display the lambda keyword as the Greek lambda symbol, which could be adapted to do what you ask. It only requires that font-lock-mode be enabled. (I didn't write it, just found it floating around somewhere).
;; real lisp hackers use the lambda character
;; courtesy of stefan monnier on c.l.l
(defun sm-lambda-mode-hook ()
(font-lock-add-keywords
nil `(("\\<lambda\\>"
(0 (progn (compose-region (match-beginning 0) (match-end 0)
,(make-char 'greek-iso8859-7 107))
nil))))))
(add-hook 'emacs-lisp-mode-hook 'sm-lambda-mode-hook)
(add-hook 'lisp-interactive-mode-hook 'sm-lamba-mode-hook)
(add-hook 'scheme-mode-hook 'sm-lambda-mode-hook)
You can look to my existing configs, based on the code from EmacsWiki with some extensions - function to handle conversion from text to chars, and example of it use for erlang mode - you can change it for ocaml mode also
P.S. but this code has one disadvantage - it also displays these characters inside strings and comments