Emacs, changing the default reftex citation - emacs

I'd like to modify emacs' behaviour when using reftex, so that after pressing 'C-c [' and choosing a citation format the default regex that comes up is one that will give me the citation I used last (the normal behaviour is to default to the word before the cursor, which is rarely of any use). I often cite the same source many times in a row, particularly when making notes on a single paper, so this would be a nice way to save a few keystrokes, and that's what we're all using emacs for, right :)
I know a little lisp, so I expect I'll end up working out a way to do this myself eventually, but I thought it'd be worth asking around to see if anyone else has done it first, no point re-inventing the wheel. (If you do want this feature, but also don't know how to achieve it, let me know and I'll drop you an email when I've done it.)
Thanks

redifining reftex-get-bibkey-default after you have loaded reftex (e.g. in your AUCTeX mode hook) should do it. The simplest would thus be:
(defun reftex-get-bibkey-default () (car reftex-cite-regexp-hist) )
However, this will destroy the default behaviour and wouldn't return anything if your history is empty, to keep the "previous word at point" behaviour from reftex in that case and then use the history, you can redefine it this way:
(defun reftex-get-bibkey-default () (if reftex-cite-regexp-hist
(car reftex-cite-regexp-hist)
(let* ((macro (reftex-what-macro 1)))
(save-excursion
(if (and macro (string-match "cite" (car macro)))
(goto-char (cdr macro)))
(skip-chars-backward "^a-zA-Z0-9")
(reftex-this-word)))
)
)

(WARNING: This is the first elisp code I've ever written which is more than 3 lines long, it could be terrible code. But it seems to work. But any comments on style or best practices would be most appreciated.)
I've worked it out! Just add the following code to .emacs it behaves exactly as I'd hoped. If you've not cited anything before then it behaves as normal, otherwise the default citation is the last used.
(defvar reftex-last-citation nil)
(defadvice reftex-citation (after reftex-citation-and-remember-citation activate)
"Save last citation to 'reftex-last-citation after running 'reftex-citation"
(setq reftex-last-citation ad-return-value))
(defadvice reftex-get-bibkey-default (around reftex-just-return-last-citation activate)
"If there is a 'reftex-last-citation then just return that instead of running 'reftex-get-bibkey-default"
(if reftex-last-citation
(setq ad-return-value reftex-last-citation)
ad-do-it))
Thanks for the help Mortimer, without your starting point I'd never had got here!
(Just wondering, is there any reason why your solution didn't use defadvice? As I implied above, elisp is all very new to me, so it'd be useful to know the best way of doing things.)

Related

How can I automatically close brackets in Emacs? [duplicate]

I've just started using emacs, and there's one feature I'd really like, and searching around a bit was fruitless. I hope someone else has done this because I don't want to learn elisp just yet.
void foo()<cursor>
I would like typing an "{" to cause this to happen
void foo(){
<cursor>
}
I would like this to only happen in cc-mode, and only at the end of a line when not in a string/comment/etc
The first thing that came to mind was rebinding "{" to do this always(I could figure out how to do this myself), but it would be hard to make it only happen at the right time.
any hints would be appreciated.
on latest emacs you can use :
electric-pair-mode is an interactive compiled Lisp function.
(electric-pair-mode &optional ARG)
Automatically pair-up parens when inserting an open paren.
this is integrated in Emacs 24.1 (actually CVS)
This will do it:
(defun my-c-mode-insert-lcurly ()
(interactive)
(insert "{")
(let ((pps (syntax-ppss)))
(when (and (eolp) (not (or (nth 3 pps) (nth 4 pps)))) ;; EOL and not in string or comment
(c-indent-line)
(insert "\n\n}")
(c-indent-line)
(forward-line -1)
(c-indent-line))))
(define-key c-mode-base-map "{" 'my-c-mode-insert-lcurly)
turn on electric-pair-mode in emacs 24 or newer version.
(electric-pair-mode 1)
I heartily recommend you to try out the excellent autopair minor mode - it does a lot more than simply inserting braces and makes Emacs a lot more IDE like in that area. I guess combining it with the electric braces setting in cc-mode will give you more or less the behavior you seek.
Try yasnippet (or on the Emacs Wiki page yasnippet). There are many packages for Emacs which support doing this kind of thing, but yasnippet seems to have momentum currently and is very extensible. Check out the videos.
You will need to delve into emacs-lisp to do this exactly as you wish, since YASnippet will do something nice for you but not exactly what you're asking for.
I think the simplest way to do this would be to bind a function to the RET key, in the cc-mode key-map.
The function should check to that the previous character is an { and if so, perform the required RET, RET, TAB, }, Up, TAB to get the cursor where you want and the closing } inserted.
You can make the feature more robust by having it check for a balanced closing } but this would be more complicated, and I'd recommend seeing how it feels without this additional polishing feature.
If you like I can write the function and the key-map binding for you, but since you asked for an idea of how it's done, I'll leave it up to you to ask for more assistance if you need it.
Alternatively, I find that autopair.el does this nicely enough for me, and I do the newlines myself ;)
You might want to keep the option of an empty function body, in which case you would want the closing brace to stay on the same line. If that is the case, then you can try this alternative solution:
Rely on the packages mentioned in the previous replies to automatically add the closing brace.
When you want to add statements to the function body, you press the Return key (while the automatically added closing brace is still under the cursor). The 'Return' key is bound as follows:
;; automatic first line in function
(defun my-c-mode-insert-funline ()
(interactive)
(newline-and-indent)
(when (looking-at "}")
(newline-and-indent)
(forward-line -1)
(c-indent-line)))
(global-set-key (kbd "RET") 'my-c-mode-insert-funline)

How to make a function that makes each sentence in a paragraph occupy one line?

I use emacs for my creative writing. To better analyze the structure of my sentences I would like to see my paragraphs displayed as consisting of one sentence per line. So I need a function that can take a normal auto-filled paragraph and do the following: 1) stretches all sentences into one line, and 2) put only one sentence per line.
Imagine I had written the following paragraph (lyrics from Suzanne Vega)
My name is Luka. I live on the second floor. I live upstairs from you. Yes I think you've seen me before. If you hear something late at night. Some kind of trouble. Some kind of fight.
With the function I want the paragraph would appear like this:
My name is Luka.
I live on the second floor.
I live upstairs from you.
Yes I think you've seen me before.
If you hear something late at night.
Some kind of trouble.
Some kind of fight.
Since I would like to do some of the writing when the sentences are displayed like this, the function should in addition to stretching out the sentences also turn off the autofill mode.
Ideally I would like a function that can toggle the display between auto-fill mode with all sentences wrapped, and this new mode with auto-fill turned off and all sentences stretched out.
Thanks in advance to all sort of suggestions or help to make such a function!
#Drew: Here is a text I am not able to split up with your code:
There are two ways to enable it: the first is with M-x visual-line-mode (for those with real menus, apparently Options->Line Wrapping in this Buffer->Word Wrap), which will give you a minor mode “wrap” in the mode line. As explained in C-h f visual-line-mode, one of the effects of this command is to subtly change the effect of commands that deal with “lines”: C-a, C-e no longer go to the end of the line (as in \n), but go to the end of the line (as in display line). M-a, M-e still work as they should. In addition, vertical split windows are guaranteed to not be truncated, and resize properly on changing width. Works wonderfully, especially if you have free form text that you’re keeping in version control (like a thesis in Latex) where hard-wrapping just doesn’t work out very well. It also makes vertical splitting much more useful, especially with huge windows. In my experience, it slows down redraw a little bit, but it’s worth it.
I guess something like this is what you're asking for.
(defun split-para-at-sentence-ends ()
"Split current paragraph into lines with one sentence each.
Then turn off `auto-fill-mode'."
(interactive)
(let ((mode major-mode))
(unwind-protect
(progn (text-mode)
(save-excursion
(let ((emacs-lisp-docstring-fill-column t)
(fill-column (point-max)))
(fill-paragraph))
(let ((bop (copy-marker (progn (backward-paragraph) (point))))
(eop (copy-marker (progn (forward-paragraph) (point)))))
(goto-char bop)
(while (< (point) eop)
(forward-sentence)
(forward-whitespace 1)
(unless (>= (point) eop)
(delete-horizontal-space)
(insert "\n"))))))
(funcall mode)))
(auto-fill-mode -1))
(define-minor-mode split-para-mode
"Toggle between a filled paragraph and one split into sentences."
nil nil nil
(if (not split-para-mode)
(split-para-at-sentence-ends)
(auto-fill-mode 1)
(fill-paragraph)))
(global-set-key "\C-o" 'split-para-mode) ; Or some other key.

Emacs preview-latex

I use preview-latex for displaying LaTeX results in an Emacs window. I use preview-at-point to toggle back and forth between code and output. However if I am not on Latex code (by mistake, maybe I missed my intended line by one, or two) then preview-at-point tries to compile everything, brings up the "other" window, and fails. All this process slows things down.
My question is how can I disable this compilation (attempt)? If no toggling is possible, then preview should not do anything. Is there a setting for preview-latex for that? Or perhaps a function I can override?
error in process sentinel: LaTeX found no preview images
Thanks,
The real work is done by preview-region so we can advise that to be a noop in certain cases. The following is not perfect since I don't think there is a way to know ahead of time what is going to previewed—the user can specify any environment or macro to be previewed. If, for example, you only care about math previews then you can remove the previewable-environments pieces.
(defvar previewable-environments
"List of environments that should be previewed."
'("tabular" "tabular*" "tikzpicture" "..."))
(defadvice preview-region (around preview-at-point-no-long-pauses activate)
"Make `preview-at-point' a no-op if mark is inactive and point is not on a preview."
(when (or (not (eq this-command 'preview-at-point))
(TeX-active-mark)
(texmathp)
(member (LaTeX-current-environment) previewable-environments))
ad-do-it))
A variation on the accepted answer: The code will trigger preview toggle if it is on an equation, but I would also like the entire document to be previewed when I am not on any math snippet. The code for that is
(defvar previewable-environments
"List of environments that should be previewed."
'("tabular" "tabular*" "tikzpicture" "..."))
(defadvice preview-region (around preview-at-point-no-long-pauses activate)
"Make `preview-at-point' a no-op if mark is inactive and point is not on a preview."
(message "preview-region")
(if (or (not (eq this-command 'preview-at-point))
(TeX-active-mark)
(texmathp)
(member (LaTeX-current-environment) previewable-environments))
ad-do-it
(preview-section)
)
)

How can can I get emacs to insert closing braces automatically

I've just started using emacs, and there's one feature I'd really like, and searching around a bit was fruitless. I hope someone else has done this because I don't want to learn elisp just yet.
void foo()<cursor>
I would like typing an "{" to cause this to happen
void foo(){
<cursor>
}
I would like this to only happen in cc-mode, and only at the end of a line when not in a string/comment/etc
The first thing that came to mind was rebinding "{" to do this always(I could figure out how to do this myself), but it would be hard to make it only happen at the right time.
any hints would be appreciated.
on latest emacs you can use :
electric-pair-mode is an interactive compiled Lisp function.
(electric-pair-mode &optional ARG)
Automatically pair-up parens when inserting an open paren.
this is integrated in Emacs 24.1 (actually CVS)
This will do it:
(defun my-c-mode-insert-lcurly ()
(interactive)
(insert "{")
(let ((pps (syntax-ppss)))
(when (and (eolp) (not (or (nth 3 pps) (nth 4 pps)))) ;; EOL and not in string or comment
(c-indent-line)
(insert "\n\n}")
(c-indent-line)
(forward-line -1)
(c-indent-line))))
(define-key c-mode-base-map "{" 'my-c-mode-insert-lcurly)
turn on electric-pair-mode in emacs 24 or newer version.
(electric-pair-mode 1)
I heartily recommend you to try out the excellent autopair minor mode - it does a lot more than simply inserting braces and makes Emacs a lot more IDE like in that area. I guess combining it with the electric braces setting in cc-mode will give you more or less the behavior you seek.
Try yasnippet (or on the Emacs Wiki page yasnippet). There are many packages for Emacs which support doing this kind of thing, but yasnippet seems to have momentum currently and is very extensible. Check out the videos.
You will need to delve into emacs-lisp to do this exactly as you wish, since YASnippet will do something nice for you but not exactly what you're asking for.
I think the simplest way to do this would be to bind a function to the RET key, in the cc-mode key-map.
The function should check to that the previous character is an { and if so, perform the required RET, RET, TAB, }, Up, TAB to get the cursor where you want and the closing } inserted.
You can make the feature more robust by having it check for a balanced closing } but this would be more complicated, and I'd recommend seeing how it feels without this additional polishing feature.
If you like I can write the function and the key-map binding for you, but since you asked for an idea of how it's done, I'll leave it up to you to ask for more assistance if you need it.
Alternatively, I find that autopair.el does this nicely enough for me, and I do the newlines myself ;)
You might want to keep the option of an empty function body, in which case you would want the closing brace to stay on the same line. If that is the case, then you can try this alternative solution:
Rely on the packages mentioned in the previous replies to automatically add the closing brace.
When you want to add statements to the function body, you press the Return key (while the automatically added closing brace is still under the cursor). The 'Return' key is bound as follows:
;; automatic first line in function
(defun my-c-mode-insert-funline ()
(interactive)
(newline-and-indent)
(when (looking-at "}")
(newline-and-indent)
(forward-line -1)
(c-indent-line)))
(global-set-key (kbd "RET") 'my-c-mode-insert-funline)

Emacs: highlighting TODO *only* in comments

This question is related to another one, Emacs :TODO indicator at left side. I recently came across a minor mode I like a lot called FixmeMode. It supports auto highlighting of TODO marks, and navigating between them. However, I think it makes more sense to recognize the "TODO" strings only in comments, rather than polluting the whole file. Is it possible?
Check out the library fic-mode.el, it has been verified in C++ and Emacs-Lisp.
It was written specifically to answer this question.
The installation is like any standard package:
(require 'fic-mode)
(add-hook 'c++-mode-hook 'turn-on-fic-mode)
Though Wei Hu did ask for an easy way to add it to multiple modes, so here goes:
(defun add-something-to-mode-hooks (mode-list something)
"helper function to add a callback to multiple hooks"
(dolist (mode mode-list)
(add-hook (intern (concat (symbol-name mode) "-mode-hook")) something)))
(add-something-to-mode-hooks '(c++ tcl emacs-lisp) 'turn-on-fic-mode)
It's possible but quite a bit trickier. Fixme mode uses font-lock to do its highlighting, so it works on an as-you-type basis to highlight the keywords. Font-lock hooks in at a very low level, basically running after every change is made to the buffer's contents. It is highly optimized, though, which allows it to appear instantaneous on modern computers.
The TODO indicator in the left fringe is static. Execute the function and all current TODO's are highlighted; change the buffer (adding or removing TODO's) does not change the fringe indicator; that's only changed when the function runs again.
Your approach would have to get into syntax tables, determining first when you're in a comment and then looking for the keywords. The tricky part comes in doing this interactively (i.e. as you type). You should be able to hook into the font-lock constructs to do this, but the function you provide to search for the comment syntax table and then for the keywords better be very efficient, as it will be run each and every time a buffer changes (though it will only run on the changed region, I think). You would want to stuff all of this in font-lock-syntactic-keywords rather than font-lock-keywords because the syntactic-keyword pass happens before the syntactic pass (which happens before the keyword pass), and you need to set TODO inside comments before comments themselves are set.
Sorry it's not a full working-code answer.....
Maybe this will help: there's a fn c-in-literal in
cc-mode, and a similar csharp-in-literal in csharp mode. The
return value is c if in a C-style comment, c++ if in a C++
style comment. You could add that to the code at
Emacs :TODO indicator at left side
to get what you want.
(defun annotate-todo ()
"put fringe marker on TODO: lines in the curent buffer"
(interactive)
(let (lit)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "TODO:" nil t)
(progn
(setq lit (c-in-literal)) ;; or csharp-in-literal
(if (or (eq lit 'c) (eq lit 'c++))
(let ((overlay (make-overlay (- (point) 5) (point))))
(overlay-put overlay 'before-string
(propertize "A"
'display
'(left-fringe ;; right
horizontal-bar
better-fringes-important-bitmap))))))))))
https://github.com/tarsius/hl-todo seems to do exactly what you want. I just tried it and love it.