Refiling to new node in org-mode - emacs

These are the setting in my .emacs for refiling in org-mode:
(setq org-refile-targets (quote ((nil :maxlevel . 10)
(org-agenda-files :maxlevel . 10))))
(setq org-refile-use-outline-path t)
(setq org-outline-path-complete-in-steps nil)
(setq org-refile-allow-creating-parent-nodes (quote confirm))
Refiling to an existing path works as it should but I can't create a new node and refile to that as the last line should allow me to do. When I refile to a path like:
Existing node"/New node"
I get the error:
Please save the buffer to a file before refiling
Supposedly this means that emacs can figure out the target file but the target is set with the org-refile-targets line so what's wrong?

Related

Emacs AucTeX; How to set C-c C-c default command?

I have set this in my .emacs file:
(add-hook 'TeX-mode-hook
(lambda ()
(setq TeX-command-default "LaTeX"))
(add-hook 'LaTeX-mode-hook
(lambda ()
(setq TeX-command-default "LaTeX"))
I see that C-c C-c is bound to TeX-command-master, which calls TeX-command-query. But since my (TeX-master-file) is "<none>", I expect the default command to be called, but keeps wanting to invoke "View" instead of "LaTeX".
If you check the source for TeX-command-query you'll find that it checks the modification date of the tex (lines 4-9) and bbl (lines 10-19) files involved in your document. Unless those files are more recent than the output file and there is no known next command to be performed (lines 20-22) it will use the "View" command as default (line 23).
This behaviour is of course sensible because normally you don't want to recompile unless there are changes (modified tex files). Apart from "patching" the command [posted below, would not really recommend to use because it will not receive automatic updates ;-) ] there isn't really anything you can do.
If you decide to use the patched command, just put is somewhere in your init file after the original command has been loaded. You could for example wrap it into (replace ;; BODY by code)
(eval-after-load "tex-buf"
'(progn
;; BODY
))
Here comes the patched command:
(defun TeX-command-query (name)
"Query the user for what TeX command to use."
(let* ((default
(cond ((if (string-equal name TeX-region)
(TeX-check-files (concat name "." (TeX-output-extension))
(list name)
TeX-file-extensions)
(TeX-save-document (TeX-master-file)))
TeX-command-default)
((and (memq major-mode '(doctex-mode latex-mode))
;; Want to know if bib file is newer than .bbl
;; We don't care whether the bib files are open in emacs
(TeX-check-files (concat name ".bbl")
(mapcar 'car
(LaTeX-bibliography-list))
(append BibTeX-file-extensions
TeX-Biber-file-extensions)))
;; We should check for bst files here as well.
(if LaTeX-using-Biber TeX-command-Biber TeX-command-BibTeX))
((TeX-process-get-variable name
'TeX-command-next
;; HERE COMES THE PATCH
;; was TeX-command-View
TeX-command-default))
;; END OF PATCH
(TeX-command-Show)))
(completion-ignore-case t)
(answer (or TeX-command-force
(completing-read
(concat "Command: (default " default ") ")
(TeX-mode-specific-command-list major-mode) nil t
nil 'TeX-command-history))))
;; If the answer is "latex" it will not be expanded to "LaTeX"
(setq answer (car-safe (TeX-assoc answer TeX-command-list)))
(if (and answer
(not (string-equal answer "")))
answer
default)))

How to turn filenames with line numbers into hyperlinks?

I am novice emacs user, and currently i am trying to set up work environment for python. I am using rope, but has come across the following: although rope's "Find occurences" command works fine, its result are put in a modeless buffer, and to access them i must copy file names.
Buffer contents are here
As far as i can tell, closes functionality to what i want (that is, opening a file on a given line after clicking on it or pressing RET) is provided by compilation-mode. However, as things are, enabling compilation-mode only causes highlighting of the filenames.
If i undestand correctly, to process lines i need to provide items into compilation-error-regexp-alist, like it is done in following snippet (from emacs wiki
(require 'compile)
(let ((symbol 'compilation-ledger)
(pattern '("^Error: \"\\([^\"\n]+?\\)\", line \\([0-9]+\\):" 1 2)))
(cond ((eval-when-compile (boundp 'compilation-error-regexp-systems-list))
;; xemacs21
(add-to-list 'compilation-error-regexp-alist-alist
(list symbol pattern))
(compilation-build-compilation-error-regexp-alist))
((eval-when-compile (boundp 'compilation-error-regexp-alist-alist))
;; emacs22 up
(add-to-list 'compilation-error-regexp-alist symbol)
(add-to-list 'compilation-error-regexp-alist-alist
(cons symbol pattern)))
(t
;; emacs21
(add-to-list 'compilation-error-regexp-alist pattern))))
How should i modify it to make it work with my buffer?
Are there better/quicker alternatives?
Generally, the quickest way to open a file when its name is displayed in a buffer is
M-x ffap
(short for M-x find-file-at-point)
If you want to open the file automatically, you could define your own function:
(defun open-file-at-point ()
(interactive)
(let ((file (ffap-file-at-point)))
(if file
(find-file file)
(error "No file at point"))))
and maybe bind it to a key with
(global-set-key (kbd "C-<return>") 'open-file-at-point)
If you want to use compilation-mode, you will have to add a matching regexp to compilation-error-regexp-alist(-alist). For your example, the following seems to work:
(add-to-list
'compilation-error-regexp-alist
'python-file-name)
(add-to-list
'compilation-error-regexp-alist-alist
(list
'python-file-name
(concat "\\(?1:.*?\\)" ;; file name
" : " ;; seperator
"\\(?2:[[:digit:]]+\\)") ;; line number
1 2)) ;; subexpr 1 is the file name, subexp 2 is the line number

In Emacs org-mode, how to refile highlighted text under an org heading?

In Emacs org-mode, is there a way to automatically refile highlighted text under an org heading? i.e. to cut the highlighted text and automatically paste it under the org-heading of my choice?
You could call it org-refile-region. Similar to org-refile, but to refile not the entire subtree, but only the highlighted region under any heading in the current document.
UPDATE:
Ideally this functionality would be independent of the org-agenda files used by org-refile, so as to avoid displaying irrelevant headings as possible targets.
Currently this is doable by doing:
1. select text
2. cut
3. other-window
4. navigate to desired target heading
5. paste text
6. other window
The proposed new function would make this much more efficient:
1. select text
2. org-refile-region
3. choose target
The most useful form of this would allow you to choose a target from among any currently open documents. My use case involves selecting text from one buffer and refiling it from among org-headings in another buffer, i.e. moving text from a source document displayed in one window and refiling to targets within the hierarchy of a target document displayed in another window, like so:
If you are using emacs 24.1 or later, you can try
(setq org-refile-active-region-within-subtree t)
which will almost do what you want, but turn the line in which you have highlighted text (the emacs term is "active region") into a headline.
If you want to move the text you have highlighted to another heading, you have to extend org-mode. Fortunately, org provides the tools you need. Here is an example:
(defvar org-refile-region-format "\n%s\n")
(defvar org-refile-region-position 'top
"Where to refile a region. Use 'bottom to refile at the
end of the subtree. ")
(defun org-refile-region (beg end copy)
"Refile the active region.
If no region is active, refile the current paragraph.
With prefix arg C-u, copy region instad of killing it."
(interactive "r\nP")
;; mark paragraph if no region is set
(unless (use-region-p)
(setq beg (save-excursion
(backward-paragraph)
(skip-chars-forward "\n\t ")
(point))
end (save-excursion
(forward-paragraph)
(skip-chars-backward "\n\t ")
(point))))
(let* ((target (save-excursion (org-refile-get-location)))
(file (nth 1 target))
(pos (nth 3 target))
(text (buffer-substring-no-properties beg end)))
(unless copy (kill-region beg end))
(deactivate-mark)
(with-current-buffer (find-file-noselect file)
(save-excursion
(goto-char pos)
(if (eql org-refile-region-position 'bottom)
(org-end-of-subtree)
(org-end-of-meta-data-and-drawers))
(insert (format org-refile-region-format text))))))
We use org-refile-get-location to apply the org refiling mechanism and extract the file and the location. Then we go to that location and insert the copied text. Two variables added for convenience.
org-refile-targets lets you control which files to consider, e.g.:
nil ;; only the current file
'((org-agenda-files :maxlevel . 2)) ;; all agenda files, 1st/2nd level
'((org-files-list :maxlevel . 4)) ;; all agenda and all open files
'((my-org-files-list :maxlevel . 4)) ;; all files returned by `my-org-files-list'
To restrict refiling to the currently open org buffers, define a function
(defun my-org-files-list ()
(mapcar (lambda (buffer)
(buffer-file-name buffer))
(org-buffer-list 'files t)))
And then either
(setq org-refile-targets '((my-org-files-list :maxlevel . 4)))
or use
M-x customize-option <ret> org-refile-targets
select "Function" from the "value menu", and type my-org-files-list

Refile from one file to other

I'm trying to implement GTD in emacs based on http://members.optusnet.com.au/~charles57/GTD/gtd_workflow.html and I have a problem with refiling.
In the file .emacs I have such a configuration
(setq org-refile-use-outline-path 'file)
'(org-refile-targets (quote (("gtd.org" :maxlevel . 1) ("done.org" :level . 1))))
The sequence Cc Cw can select only the place of the current file:
gtd.org/
gtd.org/tasks
gtd.org/projects
Please help in determining why do not I move to done.org
Regards
Krzysiek
Here's a fix of your code:
(setq org-refile-targets
'(("gtd.org" :maxlevel . 1)
("done.org" :maxlevel . 1)))
Here's a setup similar to what I use now:
(setq org-agenda-files
'("gtd.org" "done.org"))
(setq org-refile-targets
'((nil :maxlevel . 3)
(org-agenda-files :maxlevel . 3)))
This first element of org-refile-targets decides the heading levels to consider
within current file, the second element - within other agenda files.

If desktop previously saved, then switch from *scratch* to last viewed file

Could someone please give me a hand switching to the last viewed file from the desktop save when opening Emacs, taking into consideration that my *scratch* buffer is also an existing file?
The *scratch* buffer always trumps the desktop save in terms of the initial buffer choice.
(setq initial-scratch-message nil)
;; (setq initial-buffer-choice "~/.0.data/.0.emacs/*scratch*")
(defun kill-default-scratch ()
"Avoid having a buffer named `*scratch*<2>` when Emacs loads
my preferred *scratch* file from a specified location -- however,
yield to the last viewed file if it exists from desktop-save."
(kill-buffer "*scratch*")
(find-file "~/.0.data/.0.emacs/*scratch*") )
(add-hook 'after-init-hook 'kill-default-scratch)
(require 'saveplace)
(setq-default save-place t)
(setq save-place-file "~/.0.data/.0.emacs/.saved-places")
(desktop-save-mode 1)
(setq desktop-dirname "~/.0.data/.0.emacs/"
desktop-base-file-name ".emacs.desktop"
desktop-base-lock-name ".lock"
desktop-path (list desktop-dirname)
desktop-save t
desktop-files-not-to-save "[*]bbdb[*]\\|[*]BBDB[*]\\|[*]TODO[*]" ;; "^$" reload tramp paths
desktop-load-locked-desktop nil )
(setq desktop-buffers-not-to-save
(concat "\\("
"^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS"
"\\|\\.emacs.*\\|\\.diary\\|\\.newsrc-dribble"
"\\)$"))
(add-to-list 'desktop-modes-not-to-save 'dired-mode)
(add-to-list 'desktop-modes-not-to-save 'Info-mode)
(add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
(add-to-list 'desktop-modes-not-to-save 'fundamental-mode)
EDIT: Based upon the answer provided by #juanleon, I decided to just let nature run its course and deal with it using the emacs-startup-hook (which loads subsequent to the after-init-hook). The scratch buffer wants to trump desktop.el, so let it and then bury it. To deal with the situation when there is no last viewed file saved by desktop.el (i.e., because they were all closed before exiting), I bury the *Messages* buffer (if it has focus) so the result is that the custom *scratch* buffer has focus again. To remove the custom *scratch* file from the desktop.el save feature, I added [*]scratch[*] to the list of desktop-files-not-to-save. I am using auto-save-buffers-enhanced to automatically save my custom *scratch* file (set to occur 1 second after every modification) and that utility lets me exclude user-defined file types with (setq auto-save-buffers-enhanced-exclude-regexps . . .): https://github.com/kentaro/auto-save-buffers-enhanced/blob/master/auto-save-buffers-enhanced.el [NOTE: The reference to flet therein would need to be changed to cl-flet if using a recent version of Emacs.]
(setq initial-scratch-message nil)
(setq initial-buffer-choice t)
(require 'auto-save-buffers-enhanced)
(auto-save-buffers-enhanced t)
(setq auto-save-buffers-enhanced-save-scratch-buffer-to-file-p 1)
(setq auto-save-buffers-enhanced-exclude-regexps '("^not-save-file" "\\.ignore$" "\\.txt" "[*]TODO[*]" "\\.yasnippet" "\\.tex" "\\user_pref" "\\.org_archive" "\\.org" "\\.ppet"))
(require 'saveplace)
(setq-default save-place t)
(setq save-place-file "~/.0.data/.0.emacs/.saved-places")
(desktop-save-mode 1) ;; uses the after-init-hook
(setq desktop-dirname "~/.0.data/.0.emacs/"
desktop-base-file-name ".emacs.desktop"
desktop-base-lock-name ".lock"
desktop-path (list desktop-dirname)
desktop-save t
desktop-files-not-to-save "[*]scratch[*]\\|[*]bbdb[*]\\|[*]BBDB[*]\\|[*]TODO[*]" ;; "^$" reload tramp paths
desktop-load-locked-desktop nil )
(setq desktop-buffers-not-to-save
(concat "\\("
"^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS"
"\\|\\.emacs.*\\|\\.diary\\|\\.newsrc-dribble"
"\\)$"))
(add-to-list 'desktop-modes-not-to-save 'dired-mode)
(add-to-list 'desktop-modes-not-to-save 'Info-mode)
(add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
(add-to-list 'desktop-modes-not-to-save 'fundamental-mode)
(defun custom-scratch-setup ()
"Avoid having a buffer named `*scratch*<2>` when Emacs loads
my preferred *scratch* file from a specified location -- however,
yield to the last viewed file if it exists from desktop-save."
(kill-buffer "*scratch*")
(message "The default `*scratch*` buffer has been killed.")
(find-file "~/.0.data/.0.emacs/*scratch*")
(message "Finished loading the custom `*scratch*` file.")
(bury-buffer)
(message "The buffer *scratch* has been buried.")
(if (eq (current-buffer)
(get-buffer "*Messages*") )
(progn (bury-buffer)
(message "The buffer *Messages* has been buried."))) )
(add-hook 'emacs-startup-hook 'custom-scratch-setup)
My guess would be that after-init-hook runs after desktop has finished loading its stuff (desktop uses that hook)
So, the find-file will run at the end of everything, and find-file happens to "unbury" the buffer of a file, if already loaded. You may add a (bury-buffer) at the end of your kill-default-scratch if you don't want it to taking over initial buffer choice, nor being the initial buffer choice.
Another option would be to swap the order the hooks in after-init-hook are run. You can do that by playing with the moment desktop.el is "required" and/or using the arg APPEND of add-hook. If your function is run before desktop's function, (desktop-read) will take precedence over your find-file