Saving the org-agenda - emacs

I'd like to save the output of org-agenda to a text file, every time that the org-agenda is calculated. This way, I can use an external program (like ATNotes on windows or conky on linux), to pick up this text file and display it on my desktop.
How can I do this?

I feel like I'm raining on your parade after you went to the trouble to write this code snipped (and used a piece of around advice, too!), but actually this feature is already baked into org-mode, and documented in the manual. The command you want is org-write-agenda (C-x C-w in an agenda buffer). See the section of the org-mode info entitled "Exporting Agenda Views."

If you want to do it while you have emacs open, you can just call save-buffer on the *Agenda* buffer via M-x save-buffer (since orgmode binds C-x C-s to org-save-all-org-buffer. You could bind save-buffer to something else in the org-mode-map if you wanted.
If you want to do it via a cron, you should be able to use the snippet in this thread on the org-mode mailing list to pipe the output to a file. I've used this in the past:
emacs -batch -eval '(org-batch-agenda "a" org-agenda-ndays 7 org-agenda-include-diary nil org-agenda-files (quote ("~/org/todo.org")))' > agenda.txt

So I finally decided to open the emacs lisp manual and figure this out myself. I wrote this bit of code, which seems to be working just fine! :)
;; Save the org-agenda for display with conky
(defadvice org-todo-list (after saveorgagenda activate)
"save this output to my todo file"
(get-buffer-create "todo")
(with-current-buffer "todo"
(set-buffer-modified-p nil))
(kill-buffer "todo")
(write-file "~/todo"))
EDIT REASONS:
1) Without kill-buffer, the defadvice creates a new todo buffer on every execution of org-todo-list. This gets pretty irritating.
2) Without the get-buffer-create function, kill-buffers fails the first time since there is no buffer named todo at that time.
3) Without set-buffer-modified-p, the function will keep telling you "todo buffer is modified. Really kill it? (y or n)" which would defeat the whole purpose really.
Whew! I'm so happy I actually took the time and effort to figure this out! :D

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)

Make eshell tab completion behave like Bash

How can I make eshell autocomplete behave like Bash and Emacs in general i.e. it offers a list of choices rather than arbitrary selects one for you?
For example, if I have two directories "Download" and "Downloads", when I type "Down" and hit TAB, I expect another buffer pops up and shows me the choices. But eshell just completes it for me, i.e. if I press TAB, it completes to "Download"; hit TAB again, it changes to "Downloads".
Use this:
(add-hook
'eshell-mode-hook
(lambda ()
(setq pcomplete-cycle-completions nil)))
(add-hook
'eshell-mode-hook
(lambda ()
(setq pcomplete-cycle-completions nil)))
and
(setq eshell-cmpl-cycle-completions nil)
Both do as you ask and show a buffer listing the completions when I run my emacs as 'emacs -q' to avoid my own customizations. This is with emacs 23.3, are you running a much older version?
Also see http://www.emacswiki.org/emacs/EshellCompletion which is where I first went to check this out.
Steps to try this out:
Start emacs using 'emacs -q' as the command -- no other arguments.
Change to the *scratch* buffer
Paste or type in one of the above code snippets
Put your cursor at the end of the snippet and press 'C-e' to execute the code.
Start eshell
test
if neither one works, report back here with your version info and any other relevant details
You only need to have the following line:
(setq eshell-cmpl-cycle-completions nil)
eshell-mode automatically set pcomplete-cycle-completions to the value of eshell-cmpl-cycle-completions locally.

Emacs/AUCTeX prefix arguments

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.

How can I show the Org-mode agenda on Emacs start-up?

I would like the Org-mode agenda to automatically show what I have to do today when I open Emacs. The org-agenda command is interactive, so it doesn't seem to work well for this purpose.
Is there a way to show the Org-mode agenda on Emacs start-up?
Thanks,
Conor
You can use after-init-hook to run a piece of code after initialization has finished. To run (org-agenda-list) after init, use:
(add-hook 'after-init-hook 'org-agenda-list)
This works for me (in .emacs):
(setq inhibit-splash-screen t)
(org-agenda-list)
(delete-other-windows)
Without the first line, the splash screen "covered" the agenda; without the third one, the scratch buffer remained visible.
One alternative to the hook is to set the initial-buffer-choice variable. This is particularly useful if there are multiple buffers or a number of functions on the hook. The function on this variable needs to return a buffer. Naively this might be:
(setq initial-buffer-choice (lambda ()
(org-agenda-list 1)
(get-buffer "*Org Agenda*")))
Try (org-agenda-list). If you just want today, (org-agenda-list 1).
And of course, apropos is your friend. C-h C-a org-agenda (or whatever command) will show you useful info on that command.
I have a bash alias to start emacs with the Agenda open:
alias org='/usr/bin/emacs --funcall org-agenda-list &'
Enjoy.
It is not exactly at startup, but I keep Emacs running so I need a different approach
(require 'midnight)
(midnight-delay-set 'midnight-delay "7:30am")
(add-hook 'midnight-hook 'org-agenda-list)
Credits to https://stackoverflow.com/a/14947354/217408

How can I apply a new Emacs C style to reformat all my source files?

I'd like to re-format all my source files using the Google formatting function for emacs: google-c-style.el (see here).
How can I apply this function to all my source files at once, so that they are all formatted and indented correctly according to the Google style?
There are several pieces to this:
you need to come up with EMACS functions to do all the reformatting you want. indent-region is a start, but you might also want to untabify or some other things.
you need to invoke them on each file, and since the indent functions work on ranges, you need a function that sets mark to cover the whole file: mark-whole-buffer.
you need to invoke EMACS on each file: this means invoking emacs with the --batch file.
There's a couple of nice blog posts on doing this here and here.
I have done this before by using a keyboard defined macro. I would load all of the files into emacs (something like find . -name "*.cpp" | xargs emacs) and then type the following keys. I've annotated each key combination with what it does.
C-x-( 'Begin recording macro
M-< 'Go to start of file
C-space 'Mark current location (now start of file)
M-> 'Go to end of file
M-x indent-region 'Indent entire file according to coding style
C-x C-s 'Save the current buffer
C-x C-k 'Close the current buffer
C-x-) 'End recording macro
Now you can run this on a buffer by typing C-x e. If you have loaded several files you can run something like C-u 100 C-x e to run this on 100 files. If this is more than the number of files, that is ok, you'll just get some "bell ring" or other error you can ignore once all the processing is complete.
I believe that this script does not do reformatting. Instead it's an example of how to build a custom "style" as described in: CC mode manual - Styles
CC-mode manual also says:
If you want to reformat old code, you're probably better off using some other tool instead, e.g. GNU indent, which has more powerful reformatting capabilities than CC Mode.
CC mode manual - Limitations-and-Known-Bugs
If you want to mark the source files in a dired buffer and then run a function to format each you can do something like this:
(defun clean-file(filename)
(your-function-goes-here))
(defun clean-each-dired-marked-file()
(interactive)
(for-each-dired-marked-file 'clean-file))
(defun for-each-dired-marked-file(fn)
"Do stuff for each marked file, only works in dired window"
(interactive)
(if (eq major-mode 'dired-mode)
(let ((filenames (dired-get-marked-files)))
(mapcar fn filenames))
(error (format "Not a Dired buffer \(%s\)" major-mode))))