Narrow to subtree with org-velocity - emacs

I would like to use org-velocity as my primary means of navigating large .org files but with the following changes:
After running a search in org-velocity, I would like the buffer to automatically narrow to that subtree, once I make my selection.
Org-velocity should run its search against the entire file, even if the buffer is narrowed.
For part (1) I think something like this should work:
(add-hook 'org-follow-link-hook (lambda () (org-narrow-to-subtree)))
But this is not the right hook. Not sure how to approach (2). Any ideas? Thanks!

I am writing a partial answer for part (2) of the question since the following does not fit nicely into a comment. Note, that I do not use org-velocity. So, the following is not really tested.
save-restriction saves the current narrowing to the org-subtree and widen removes this narrowing temporarily during the search. To see exactly how it works read the help for the functions save-restriction and widen with C-h f and consult the info C-h i for elisp and there the Section "Advising functions".
(defadvice org-velocity (around search-all activate)
"Widen for search with org-velocity"
(save-restriction
(widen)
ad-do-it
))

Okay, I think I have a complete solution!
Make sure you have this fork of org-velocity installed:
https://github.com/Fuco1/org-velocity
Open your org-velocity.el file and replace lines 763-765 with this:
(progn
(with-current-buffer (org-velocity-match-buffer)
(kill-buffer-and-window))
(org-narrow-to-subtree)
(show-all))))))
The additional code tells org-velocity to first narrow the buffer to the selected subtree and secondly to expand that node.
Put this code somewhere in your search path (init.el, .emacs, etc.)
(defadvice org-velocity (around search-all activate)
"Widen for search with org-velocity"
(widen)
ad-do-it)
And that's it!
Thank you Tobias, Paul and Matúš for walking me through this!!
Take care,
-Adam

Related

emacs > org mode > agenda - always use current buffer

I am using many different org mode files for various projects, and I rev them by adding the date to the filename eg filename-2020-09-17.org. I realize I could use version control but in this case that is not possible, due to needing to share the file with others who are not using VC.
I would like the Agenda to always show just the items for the current file/buffer.
When I save eg the file with filename-2020-09-16.org to filename-2020-09-17.org, then the agenda still shows the old file name unless I remove it from the agenda file list and add the new file.
I realize that I can use C-c a < a but I am lazy and would rather not have to type S-, each time to get the <.
I looked at
Agenda view of the current buffer
And the OP says the solution was simple but he/she/they did not provide the solution - at least I don't see it and I tried the posted code but it no works.
I also found https://www.reddit.com/r/orgmode/comments/bxwovd/agenda_for_current_buffer/ but that did not seem to meet my need.
Specifically I would like to put something in .emacs so that this would apply to all files all the time.
I also looked into a keystroke macro programs but this does not seem ideal.
Any help is appreciated.
Thanks ahead of time.
Here's a simple function to do what you want, but there is no error checking to make sure e.g. that you are invoking it from a buffer that is visiting an Org mode file. The idea is that you set the org-agenda-files list to contain just the file which the buffer is visiting and then you call the regular org-agenda function. Binding the modified function to the C-c a key may or may not be what you want to do, but you can try and decide for yourself:
(defun org-agenda-current-buffer ()
(interactive)
(let ((org-agenda-files (list (buffer-file-name (current-buffer)))))
(org-agenda)))
(define-key global-map (kbd "C-c a") #'org-agenda-current-buffer)

how to prevent helm-swoop from returning symbol at point?

I would like to invoke M-x helm-swoop such that it does NOT return the symbol at point. I find that it always picks up undesired symbols, e.g. when I invoke M-x helm-swoop in org-mode, I get Swoop: \*, and I then have to delete the \* before I can enter my desired search term. How can I do this?
This has been bugging me as well, for exactly the same reason (swoop in an Org-mode buffer picking up heading characters), so this question motivated me to go and look.
Looking at the source, Helm-swoop calls helm-swoop-pre-input-function to populate the prompt, and by default this is set to a function that returns (thing-at-point 'symbol), which is what causes the problem in headings.
A quick test with the following in my init file seems to work:
(setq helm-swoop-pre-input-function
(lambda () nil))
This could certainly be improved by, for example, keeping the default behaviour in non-Org buffers but as I'm only really using swoop in Org buffers this is good enough for me.

Create "M-x" command shortcuts in emacs?

Some of the commands in emacs are so long to type.
Still, I'd like them as M-x commands (not keyboard shortcuts) as I have so many, I'd start stumble on them.
And, the old ones should not be removed. It'll take a while getting used to the new ones.
Only way I could think of, doing it one by one like this. Better way?
(defun icd (dict) "Alias for ispell-change-dictionary"
(interactive "sDictionary: ")
(ispell-change-dictionary (downcase dict)))
Use defalias:
(defalias 'bc 'emacs-lisp-byte-compile)
Before defining aliases for every command, you should use ido-mode
See here: EmacsWiki
If you put
(setq completion-styles (append completion-styles '(initials)))
(define-key minibuffer-local-completion-map
[?\M-\t] 'minibuffer-force-complete)
in your .emacs, then M-x icd M-TAB M-TAB will get you ispell-change-directory. And next time around, a single M-TAB will be sufficient (because the cycling prefers entries that are found in the history).

Emacs - Toggling between Visual Markers?

I'm trying to figure out if this functionality exists, or if not how difficult it would be to program.
Essentially what I'm looking for is the ability to mark multiple lines in a file with some form of highlighting to point out that they're important lines. From there on it would be great to be able to toggle on/off the marker, and be able to toggle through them - for larger files it would be great to be able to do this for important lines.
The markers don't have to persist, just for that session would be great.
I took a look into the "Overview of Markers" page - but I'm not sure this is exactly what I want, and if it's worth the time to try and implement it if it's not.
Thanks for any/all help!
It looks like bm.el does exactly what you want.
You want quick, perhaps temporary bookmarks that highlight the location (e.g. line): Bookmark+.
Autonamed bookmarks: hit the same key to create/delete.
Temporary bookmarks: any bookmarks can be temporary; quick to toggle temp/permanent.
Highlighting bookmark locations: any bookmarks can be highlighted, in various ways.
FWIW, Bookmark+ does everything bm.el does, and more.
This is somewhat similar to what highlight-regexp does, except arbitrary text instead of a regexp. Based on that, I think something like this should work:
(defun highlight-text ()
"Highlight the current region."
(interactive)
(let ((overlay (make-overlay (region-beginning) (region-end))))
(overlay-put overlay 'face 'hi-yellow)))

Emacs, changing the default reftex citation

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.)