1 #+OPTIONS: H:3 num:nil toc:t \n:nil @:t ::t |:t ^:t -:t f:t *:t TeX:t LaTeX:t skip:nil d:(HIDE) tags:not-in-toc
2 #+STARTUP: align fold nodlcheck hidestars oddeven lognotestate
3 #+SEQ_TODO: TODO(t) INPROGRESS(i) WAITING(w@) | DONE(d) CANCELED(c@)
4 #+TAGS: Write(w) Update(u) Fix(f) Check(c)
5 #+TITLE: Org ad hoc code, quick hacks and workarounds
7 #+EMAIL: mdl AT imapmail DOT org
12 # This file is the default header for new Org files in Worg. Feel free
13 # to tailor it to your needs.
15 [[file:index.org][{Back to Worg's index}]]
17 This page is for ad hoc bits of code. Feel free to add quick hacks and
20 * Hacking Org: Working within Org-mode.
21 ** Building and Managing Org
22 *** Compiling Org without make
24 :CUSTOM_ID: compiling-org-without-make
27 This file is the result of [[http://article.gmane.org/gmane.emacs.orgmode/15264][one of our discussions]] on the mailing list.
30 To use this function, adjust the variables =my/org-lisp-directory= and
31 =my/org-compile-sources= to suite your needs.
33 #+BEGIN_SRC emacs-lisp
34 (defvar my/org-lisp-directory "~/.emacs.d/org/lisp"
35 "Directory where your org-mode files live.")
37 (defvar my/org-compile-sources t
38 "If `nil', never compile org-sources. `my/compile-org' will only create
39 the autoloads file `org-install.el' then. If `t', compile the sources, too.")
42 (setq my/org-lisp-directory "~/.emacs.d/org/lisp")
45 (setq my/org-compile-sources t)
47 (defun my/compile-org(&optional directory)
48 "Compile all *.el files that come with org-mode."
50 (setq directory (concat
52 (or directory my/org-lisp-directory)) "/"))
54 (add-to-list 'load-path directory)
56 (let ((list-of-org-files (file-expand-wildcards (concat directory "*.el"))))
58 ;; create the org-install file
60 (setq esf/org-install-file (concat directory "org-install.el"))
61 (find-file esf/org-install-file)
64 (generate-file-autoloads x))
66 (insert "\n(provide (quote org-install))\n")
69 (byte-compile-file esf/org-install-file t)
71 (dolist (f list-of-org-files)
72 (if (file-exists-p (concat f "c")) ; delete compiled files
73 (delete-file (concat f "c")))
74 (if my/org-compile-sources ; Compile, if `my/org-compile-sources' is t
75 (byte-compile-file f)))))
79 As of Org version 6.23b (released Sunday Feb 22, 2009) there is a new
80 function to reload org files.
82 Normally you want to use the compiled files since they are faster.
83 If you update your org files you can easily reload them with
87 If you run into a bug and want to generate a useful backtrace you can
88 reload the source files instead of the compiled files with
92 and turn on the "Enter Debugger On Error" option. Redo the action
93 that generates the error and cut and paste the resulting backtrace.
94 To switch back to the compiled version just reload again with
98 ** Enhancing the Org experience.
100 Speed commands are described [[http://orgmode.org/manual/Speed-keys.html#Speed-keys][here]] in the manual. Add your own speed
102 *** Show next/prev heading tidily
104 These close the current heading and open the next/previous heading.
106 #+begin_src emacs-lisp
107 (defun ded/org-show-next-heading-tidily ()
108 "Show next entry, keeping other entries closed."
109 (if (save-excursion (end-of-line) (outline-invisible-p))
110 (progn (org-show-entry) (show-children))
111 (outline-next-heading)
112 (unless (and (bolp) (org-on-heading-p))
113 (org-up-heading-safe)
115 (error "Boundary reached"))
121 (defun ded/org-show-previous-heading-tidily ()
122 "Show previous entry, keeping other entries closed."
124 (outline-previous-heading)
125 (unless (and (< (point) pos) (bolp) (org-on-heading-p))
128 (error "Boundary reached"))
134 (setq org-use-speed-commands t)
135 (add-to-list 'org-speed-commands-user
136 '("n" ded/org-show-next-heading-tidily))
137 (add-to-list 'org-speed-commands-user
138 '("p" ded/org-show-previous-heading-tidily))
141 *** Changelog support for org headers
144 Put the following in your =.emacs=, and =C-x 4 a= and other functions which
145 use =add-log-current-defun= like =magit-add-log= will pick up the nearest org
146 headline as the "current function" if you add a changelog entry from an org
149 #+BEGIN_SRC emacs-lisp
150 (defun org-log-current-defun ()
152 (org-back-to-heading)
153 (if (looking-at org-complex-heading-regexp)
156 (add-hook 'org-mode-hook
158 (make-variable-buffer-local 'add-log-current-defun-function)
159 (setq add-log-current-defun-function 'org-log-current-defun)))
162 *** Different org-cycle-level behavior
165 In recent org versions, when your point (cursor) is at the end of an
166 empty header line (like after you first created the header), the TAB
167 key (=org-cycle=) has a special behavior: it cycles the headline through
168 all possible levels. However, I did not like the way it determined
169 "all possible levels," so I rewrote the whole function, along with a
170 couple of supporting functions.
172 The original function's definition of "all possible levels" was "every
173 level from 1 to one more than the initial level of the current
174 headline before you started cycling." My new definition is "every
175 level from 1 to one more than the previous headline's level." So, if
176 you have a headline at level 4 and you use ALT+RET to make a new
177 headline below it, it will cycle between levels 1 and 5, inclusive.
179 The main advantage of my custom =org-cycle-level= function is that it
180 is stateless: the next level in the cycle is determined entirely by
181 the contents of the buffer, and not what command you executed last.
182 This makes it more predictable, I hope.
184 #+BEGIN_SRC emacs-lisp
187 (defun org-point-at-end-of-empty-headline ()
188 "If point is at the end of an empty headline, return t, else nil."
189 (and (looking-at "[ \t]*$")
191 (beginning-of-line 1)
192 (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp "\\)?[ \t]*")))))
194 (defun org-level-increment ()
195 "Return the number of stars that will be added or removed at a
196 time to headlines when structure editing, based on the value of
197 `org-odd-levels-only'."
198 (if org-odd-levels-only 2 1))
200 (defvar org-previous-line-level-cached nil)
202 (defun org-recalculate-previous-line-level ()
203 "Same as `org-get-previous-line-level', but does not use cached
204 value. It does *set* the cached value, though."
205 (set 'org-previous-line-level-cached
206 (let ((current-level (org-current-level))
207 (prev-level (when (> (line-number-at-pos) 1)
210 (org-current-level)))))
211 (cond ((null current-level) nil) ; Before first headline
212 ((null prev-level) 0) ; At first headline
215 (defun org-get-previous-line-level ()
216 "Return the outline depth of the last headline before the
217 current line. Returns 0 for the first headline in the buffer, and
218 nil if before the first headline."
219 ;; This calculation is quite expensive, with all the regex searching
220 ;; and stuff. Since org-cycle-level won't change lines, we can reuse
221 ;; the last value of this command.
222 (or (and (eq last-command 'org-cycle-level)
223 org-previous-line-level-cached)
224 (org-recalculate-previous-line-level)))
226 (defun org-cycle-level ()
228 (let ((org-adapt-indentation nil))
229 (when (org-point-at-end-of-empty-headline)
230 (setq this-command 'org-cycle-level) ;Only needed for caching
231 (let ((cur-level (org-current-level))
232 (prev-level (org-get-previous-line-level)))
234 ;; If first headline in file, promote to top-level.
236 (loop repeat (/ (- cur-level 1) (org-level-increment))
237 do (org-do-promote)))
238 ;; If same level as prev, demote one.
239 ((= prev-level cur-level)
241 ;; If parent is top-level, promote to top level if not already.
243 (loop repeat (/ (- cur-level 1) (org-level-increment))
244 do (org-do-promote)))
245 ;; If top-level, return to prev-level.
247 (loop repeat (/ (- prev-level 1) (org-level-increment))
249 ;; If less than prev-level, promote one.
250 ((< cur-level prev-level)
252 ;; If deeper than prev-level, promote until higher than
254 ((> cur-level prev-level)
255 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
256 do (org-do-promote))))
261 **** Transpose tables (Juan Pechiar)
263 This function by Juan Pechiar will transpose a table:
265 #+begin_src emacs-lisp
266 (defun org-transpose-table-at-point ()
267 "Transpose orgmode table at point, eliminate hlines"
270 (apply #'mapcar* #'list
271 ;; remove 'hline from list
272 (remove-if-not 'listp
273 ;; signals error if not table
274 (org-table-to-lisp)))))
275 (delete-region (org-table-begin) (org-table-end))
276 (insert (mapconcat (lambda(x) (concat "| " (mapconcat 'identity x " | " ) " |\n" ))
288 will be transposed as
295 (Note that horizontal lines disappeared.)
297 *** Dates computation
299 **** Question ([[http://article.gmane.org/gmane.emacs.orgmode/15692][Xin Shi]])
301 I have a table in org which stores the date, I'm wondering if there is
302 any function to calculate the duration? For example:
304 | Start Date | End Date | Duration |
305 |------------+------------+----------|
306 | 2004.08.07 | 2005.07.08 | |
308 I tried to use B&-C&, but failed ...
310 **** Answer ([[http://article.gmane.org/gmane.emacs.orgmode/15694][Nick Dokos]])
314 | Start Date | End Date | Duration |
315 |------------+------------+----------|
316 | 2004.08.07 | 2005.07.08 | 335 |
317 :#+TBLFM: $3=(date(<$2>)-date(<$1>))
321 http://thread.gmane.org/gmane.emacs.orgmode/7741
323 as well as this post (which is really a followup on the
326 http://article.gmane.org/gmane.emacs.orgmode/7753
328 The problem that this last article pointed out was solved
331 http://article.gmane.org/gmane.emacs.orgmode/8001
333 and Chris Randle's original musings are at
335 http://article.gmane.org/gmane.emacs.orgmode/6536/
337 *** Field coordinates in formulas (=@#= and =$#=)
341 Following are some use cases that can be implemented with the
342 _field coordinates in formulas_ described in the corresponding
343 chapter in the [[http://orgmode.org/manual/References.html#References][Org manual]], available since =org-version= 6.35.
345 **** Copy a column from a remote table into a column
347 current column =$3= = remote column =$2=:
348 : #+TBLFM: $3 = remote(FOO, @@#$2)
350 **** Copy a row from a remote table transposed into a column
352 current column =$1= = transposed remote row =@1=:
353 : #+TBLFM: $1 = remote(FOO, @$#$@#)
355 **** Transpose a table
359 This is more like a demonstration of using _field coordinates in formulas_
360 to [[http://en.wikipedia.org/wiki/Transpose][transpose]] a table or to do it without using org-babel. The efficient
361 and simple solution for this with the help of org-babel and Emacs Lisp has
362 been provided by Thomas S. Dye on the [[http://thread.gmane.org/gmane.emacs.orgmode/23809/focus=23815][mailing list]].
364 To transpose this 4x7 table
367 : | year | 2004 | 2005 | 2006 | 2007 | 2008 | 2009 |
368 : |------+------+------+------+------+------+------|
369 : | min | 401 | 501 | 601 | 701 | 801 | 901 |
370 : | avg | 402 | 502 | 602 | 702 | 802 | 902 |
371 : | max | 403 | 503 | 603 | 703 | 803 | 903 |
373 start with a 7x4 table without any horizontal line (to have filled
374 also the column header) and yet empty:
384 Then add the =TBLFM= below with the same formula repeated for each column.
385 After recalculation this will end up with the transposed copy:
387 : | year | min | avg | max |
388 : | 2004 | 401 | 402 | 403 |
389 : | 2005 | 501 | 502 | 503 |
390 : | 2006 | 601 | 602 | 603 |
391 : | 2007 | 701 | 702 | 703 |
392 : | 2008 | 801 | 802 | 803 |
393 : | 2009 | 901 | 902 | 903 |
394 : #+TBLFM: $1 = remote(FOO, @$#$@#) :: $2 = remote(FOO, @$#$@#) :: $3 = remote(FOO, @$#$@#) :: $4 = remote(FOO, @$#$@#)
396 The formulas simply exchange row and column numbers by taking
397 - the absolute remote row number =@$#= from the current column number =$#=
398 - the absolute remote column number =$@#= from the current row number =@#=
400 Possible field formulas from the remote table will have to be transferred
401 manually. Since there are no row formulas yet there is no need to transfer
402 column formulas to row formulas or vice versa.
404 **** Dynamic variation of ranges
408 In this example all columns next to =quote= are calculated from the column
409 =quote= and show the average change of the time series =quote[year]=
410 during the period of the preceding =1=, =2=, =3= or =4= years:
412 : | year | quote | 1 a | 2 a | 3 a | 4 a |
413 : |------+-------+-------+-------+-------+-------|
414 : | 2005 | 10 | | | | |
415 : | 2006 | 12 | 0.200 | | | |
416 : | 2007 | 14 | 0.167 | 0.183 | | |
417 : | 2008 | 16 | 0.143 | 0.155 | 0.170 | |
418 : | 2009 | 18 | 0.125 | 0.134 | 0.145 | 0.158 |
419 : #+TBLFM: $3=if(@# >= $#, ($2 / subscr(@-I$2..@+I$2, @# + 1 - $#)) ^ (1 / ($# - 2)) - 1, string("")); f3::$4=if(@# >= $#, ($2 / subscr(@-I$2..@+I$2, @# + 1 - $#)) ^ (1 / ($# - 2)) - 1, string("")); f3::$5=if(@# >= $#, ($2 / subscr(@-I$2..@+I$2, @# + 1 - $#)) ^ (1 / ($# - 2)) - 1, string("")); f3::$6=if(@# >= $#, ($2 / subscr(@-I$2..@+I$2, @# + 1 - $#)) ^ (1 / ($# - 2)) - 1, string("")); f3
421 The formula is the same for each column =$3= through =$6=. This can easily
422 be seen with the great formula editor invoked by C-c ' on the
423 table. The important part of the formula without the field blanking is:
425 : ($2 / subscr(@-I$2..@+I$2, @# + 1 - $#)) ^ (1 / ($# - 2)) - 1
427 which is the Emacs Calc implementation of the equation
429 /AvgChange(i, a) = (quote[i] / quote[i - a]) ^ 1 / n - 1/
431 where /i/ is the current time and /a/ is the length of the preceding period.
433 *** Customize the size of the frame for remember
434 (Note: this hack is likely out of date due to the development of
438 On emacs-orgmode, Ryan C. Thompson suggested this:
441 I am using org-remember set to open a new frame when used,
442 and the default frame size is much too large. To fix this, I have
443 designed some advice and a custom variable to implement custom
444 parameters for the remember frame:
447 #+begin_src emacs-lisp
448 (defcustom remember-frame-alist nil
449 "Additional frame parameters for dedicated remember frame."
453 (defadvice remember (around remember-frame-parameters activate)
454 "Set some frame parameters for the remember frame."
455 (let ((default-frame-alist (append remember-frame-alist
456 default-frame-alist)))
460 Setting remember-frame-alist to =((width . 80) (height . 15)))= give a
461 reasonable size for the frame.
462 *** Promote all items in subtree
465 This function will promote all items in a subtree. Since I use
466 subtrees primarily to organize projects, the function is somewhat
467 unimaginatively called my-org-un-project:
469 #+begin_src emacs-lisp
470 (defun my-org-un-project ()
472 (org-map-entries 'org-do-promote "LEVEL>1" 'tree)
476 *** Turn a heading into an Org link
480 #+begin_src emacs-lisp
481 (defun dmj:turn-headline-into-org-mode-link ()
482 "Replace word at point by an Org mode link."
484 (when (org-at-heading-p)
485 (let ((hl-text (nth 4 (org-heading-components))))
486 (unless (or (null hl-text)
487 (org-string-match-p "^[ \t]*:[^:]+:$" hl-text))
489 (search-forward hl-text (point-at-eol))
492 (format "[[file:%s.org][%s]]"
493 (org-link-escape hl-text)
494 (org-link-escape hl-text '((?\] . "%5D") (?\[ . "%5B"))))
495 nil (- (point) (length hl-text)) (point))))))
498 ** Archiving Content in Org-Mode
499 *** Preserve top level headings when archiving to a file
502 To preserve (somewhat) the integrity of your archive structure while
503 archiving lower level items to a file, you can use the following
506 #+begin_src emacs-lisp
507 (defadvice org-archive-subtree (around my-org-archive-subtree activate)
508 (let ((org-archive-location
509 (if (save-excursion (org-back-to-heading)
510 (> (org-outline-level) 1))
511 (concat (car (split-string org-archive-location "::"))
513 (car (org-get-outline-path)))
514 org-archive-location)))
518 Thus, if you have an outline structure such as...
526 ...archiving "Subsubheading" to a new file will set the location in
527 the new file to the top level heading:
534 While this hack obviously destroys the outline hierarchy somewhat, it
535 at least preserves the logic of level one groupings.
537 *** Archive in a date tree
539 Posted to Org-mode mailing list by Osamu Okano
542 #+begin_src emacs-lisp
543 ;; (setq org-archive-location "%s_archive::date-tree")
544 (defadvice org-archive-subtree
545 (around org-archive-subtree-to-data-tree activate)
546 "org-archive-subtree to date-tree"
549 (org-extract-archive-heading
550 (org-get-local-archive-location)))
551 (let* ((dct (decode-time (org-current-time)))
555 (this-buffer (current-buffer))
556 (location (org-get-local-archive-location))
557 (afile (org-extract-archive-file location))
558 (org-archive-location
559 (format "%s::*** %04d-%02d-%02d %s" afile y m d
560 (format-time-string "%A" (encode-time 0 0 0 d m y)))))
561 (message "afile=%s" afile)
563 (error "Invalid `org-archive-location'"))
565 (switch-to-buffer (find-file-noselect afile))
566 (org-datetree-find-year-create y)
567 (org-datetree-find-month-create y m)
568 (org-datetree-find-day-create y m d)
570 (switch-to-buffer this-buffer))
575 *** Add inherited tags to archived entries
577 To make =org-archive-subtree= keep inherited tags, Osamu OKANO suggests to
578 advise the function like this:
581 (defadvice org-archive-subtree
582 (before add-inherited-tags-before-org-archive-subtree activate)
583 "add inherited tags before org-archive-subtree"
584 (org-set-tags-to (org-get-tags-at)))
587 ** Using and Managing Org-Metadata
588 *** Remove redundant tags of headlines
591 A small function that processes all headlines in current buffer and
592 removes tags that are local to a headline and inherited by a parent
593 headline or the #+FILETAGS: statement.
595 #+BEGIN_SRC emacs-lisp
596 (defun dmj/org-remove-redundant-tags ()
597 "Remove redundant tags of headlines in current buffer.
599 A tag is considered redundant if it is local to a headline and
600 inherited by a parent headline."
602 (when (eq major-mode 'org-mode)
606 (let ((alltags (split-string (or (org-entry-get (point) "ALLTAGS") "") ":"))
608 (dolist (tag alltags)
609 (if (get-text-property 0 'inherited tag)
610 (push tag inherited) (push tag local)))
612 (if (member tag inherited) (org-toggle-tag tag 'off)))))
616 *** Remove empty property drawers
618 David Maus proposed this:
620 #+begin_src emacs-lisp
621 (defun dmj:org:remove-empty-propert-drawers ()
622 "*Remove all empty property drawers in current file."
624 (unless (eq major-mode 'org-mode)
625 (error "You need to turn on Org mode for this function."))
627 (goto-char (point-min))
628 (while (re-search-forward ":PROPERTIES:" nil t)
630 (org-remove-empty-drawer-at "PROPERTIES" (match-beginning 0))))))
633 *** Group task list by a property
635 This advice allows you to group a task list in Org-Mode. To use it,
636 set the variable =org-agenda-group-by-property= to the name of a
637 property in the option list for a TODO or TAGS search. The resulting
638 agenda view will group tasks by that property prior to searching.
640 #+begin_src emacs-lisp
641 (defvar org-agenda-group-by-property nil
642 "Set this in org-mode agenda views to group tasks by property")
644 (defun org-group-bucket-items (prop items)
647 (let* ((marker (get-text-property 0 'org-marker item))
648 (pvalue (org-entry-get marker prop t))
649 (cell (assoc pvalue buckets)))
651 (setcdr cell (cons item (cdr cell)))
652 (setq buckets (cons (cons pvalue (list item))
654 (setq buckets (mapcar (lambda (bucket)
656 (reverse (cdr bucket))))
658 (sort buckets (lambda (i1 i2)
659 (string< (car i1) (car i2))))))
661 (defadvice org-finalize-agenda-entries (around org-group-agenda-finalize
662 (list &optional nosort))
663 "Prepare bucketed agenda entry lists"
664 (if org-agenda-group-by-property
665 ;; bucketed, handle appropriately
667 (dolist (bucket (org-group-bucket-items
668 org-agenda-group-by-property
670 (let ((header (concat "Property "
671 org-agenda-group-by-property
673 (or (car bucket) "<nil>") ":\n")))
674 (add-text-properties 0 (1- (length header))
675 (list 'face 'org-agenda-structure)
679 ;; recursively process
680 (let ((org-agenda-group-by-property nil))
681 (org-finalize-agenda-entries
682 (cdr bucket) nosort))
684 (setq ad-return-value text))
686 (ad-activate 'org-finalize-agenda-entries)
688 *** Dynamically adjust tag position
689 Here is a bit of code that allows you to have the tags always
690 right-adjusted in the buffer.
692 This is useful when you have bigger window than default window-size
693 and you dislike the aesthetics of having the tag in the middle of the
696 This hack solves the problem of adjusting it whenever you change the
698 Before saving it will revert the file to having the tag position be
699 left-adjusted so that if you track your files with version control,
700 you won't run into artificial diffs just because the window-size
703 *IMPORTANT*: This is probably slow on very big files.
705 #+begin_src emacs-lisp
706 (setq ba/org-adjust-tags-column t)
708 (defun ba/org-adjust-tags-column-reset-tags ()
709 "In org-mode buffers it will reset tag position according to
712 (not (string= (buffer-name) "*Remember*"))
713 (eql major-mode 'org-mode))
714 (let ((b-m-p (buffer-modified-p)))
717 (goto-char (point-min))
718 (command-execute 'outline-next-visible-heading)
719 ;; disable (message) that org-set-tags generates
720 (flet ((message (&rest ignored) nil))
722 (set-buffer-modified-p b-m-p))
725 (defun ba/org-adjust-tags-column-now ()
726 "Right-adjust `org-tags-column' value, then reset tag position."
727 (set (make-local-variable 'org-tags-column)
728 (- (- (window-width) (length org-ellipsis))))
729 (ba/org-adjust-tags-column-reset-tags))
731 (defun ba/org-adjust-tags-column-maybe ()
732 "If `ba/org-adjust-tags-column' is set to non-nil, adjust tags."
733 (when ba/org-adjust-tags-column
734 (ba/org-adjust-tags-column-now)))
736 (defun ba/org-adjust-tags-column-before-save ()
737 "Tags need to be left-adjusted when saving."
738 (when ba/org-adjust-tags-column
739 (setq org-tags-column 1)
740 (ba/org-adjust-tags-column-reset-tags)))
742 (defun ba/org-adjust-tags-column-after-save ()
743 "Revert left-adjusted tag position done by before-save hook."
744 (ba/org-adjust-tags-column-maybe)
745 (set-buffer-modified-p nil))
747 ; automatically align tags on right-hand side
748 (add-hook 'window-configuration-change-hook
749 'ba/org-adjust-tags-column-maybe)
750 (add-hook 'before-save-hook 'ba/org-adjust-tags-column-before-save)
751 (add-hook 'after-save-hook 'ba/org-adjust-tags-column-after-save)
752 (add-hook 'org-agenda-mode-hook '(lambda ()
753 (setq org-agenda-tags-column (- (window-width)))))
755 ; between invoking org-refile and displaying the prompt (which
756 ; triggers window-configuration-change-hook) tags might adjust,
757 ; which invalidates the org-refile cache
758 (defadvice org-refile (around org-refile-disable-adjust-tags)
759 "Disable dynamically adjusting tags"
760 (let ((ba/org-adjust-tags-column nil))
762 (ad-activate 'org-refile)
764 ** Org Agenda and Task Management
765 *** Make it easier to set org-agenda-files from multiple directories
768 #+begin_src emacs-lisp
769 (defun my-org-list-files (dirs ext)
770 "Function to create list of org files in multiple subdirectories.
771 This can be called to generate a list of files for
772 org-agenda-files or org-refile-targets.
774 DIRS is a list of directories.
776 EXT is a list of the extensions of files to be included."
777 (let ((dirs (if (listp dirs)
790 (file-expand-wildcards
791 (concat (file-name-as-directory x) "*" y)))))
796 (when (or (string-match "/.#" x)
797 (string-match "#$" x))
798 (setq files (delete x files))))
802 (defvar my-org-agenda-directories '("~/org/")
803 "List of directories containing org files.")
804 (defvar my-org-agenda-extensions '(".org")
805 "List of extensions of agenda files")
807 (setq my-org-agenda-directories '("~/org/" "~/work/"))
808 (setq my-org-agenda-extensions '(".org" ".ref"))
810 (defun my-org-set-agenda-files ()
812 (setq org-agenda-files (my-org-list-files
813 my-org-agenda-directories
814 my-org-agenda-extensions)))
816 (my-org-set-agenda-files)
819 The code above will set your "default" agenda files to all files
820 ending in ".org" and ".ref" in the directories "~/org/" and "~/work/".
821 You can change these values by setting the variables
822 my-org-agenda-extensions and my-org-agenda-directories. The function
823 my-org-agenda-files-by-filetag uses these two variables to determine
824 which files to search for filetags (i.e., the larger set from which
825 the subset will be drawn).
827 You can also easily use my-org-list-files to "mix and match"
828 directories and extensions to generate different lists of agenda
831 *** Restrict org-agenda-files by filetag
833 :CUSTOM_ID: set-agenda-files-by-filetag
837 It is often helpful to limit yourself to a subset of your agenda
838 files. For instance, at work, you might want to see only files related
839 to work (e.g., bugs, clientA, projectxyz, etc.). The FAQ has helpful
840 information on filtering tasks using [[file:org-faq.org::#limit-agenda-with-tag-filtering][filetags]] and [[file:org-faq.org::#limit-agenda-with-category-match][custom agenda
841 commands]]. These solutions, however, require reapplying a filter each
842 time you call the agenda or writing several new custom agenda commands
843 for each context. Another solution is to use directories for different
844 types of tasks and to change your agenda files with a function that
845 sets org-agenda-files to the appropriate directory. But this relies on
846 hard and static boundaries between files.
848 The following functions allow for a more dynamic approach to selecting
849 a subset of files based on filetags:
851 #+begin_src emacs-lisp
852 (defun my-org-agenda-restrict-files-by-filetag (&optional tag)
853 "Restrict org agenda files only to those containing filetag."
855 (let* ((tagslist (my-org-get-all-filetags))
857 (completing-read "Tag: "
858 (mapcar 'car tagslist)))))
859 (org-agenda-remove-restriction-lock 'noupdate)
860 (put 'org-agenda-files 'org-restrict (cdr (assoc ftag tagslist)))
861 (setq org-agenda-overriding-restriction 'files)))
863 (defun my-org-get-all-filetags ()
864 "Get list of filetags from all default org-files."
865 (let ((files org-agenda-files)
867 (save-window-excursion
868 (while (setq x (pop files))
869 (set-buffer (find-file-noselect x))
872 (let ((tagfiles (assoc y tagslist)))
874 (setcdr tagfiles (cons x (cdr tagfiles)))
875 (add-to-list 'tagslist (list y x)))))
876 (my-org-get-filetags)))
879 (defun my-org-get-filetags ()
880 "Get list of filetags for current buffer"
881 (let ((ftags org-file-tags)
885 (org-substring-no-properties x))
889 Calling my-org-agenda-restrict-files-by-filetag results in a prompt
890 with all filetags in your "normal" agenda files. When you select a
891 tag, org-agenda-files will be restricted to only those files
892 containing the filetag. To release the restriction, type C-c C-x >
893 (org-agenda-remove-restriction-lock).
895 *** Highlight the agenda line under cursor
897 This is useful to make sure what task you are operating on.
899 #+BEGIN_SRC emacs-lisp
900 (add-hook 'org-agenda-mode-hook '(lambda () (hl-line-mode 1)))
905 #+BEGIN_SRC emacs-lisp
906 ;; hl-line seems to be only for emacs
908 (add-hook 'org-agenda-mode-hook '(lambda () (highline-mode 1)))
910 ;; highline-mode does not work straightaway in tty mode.
911 ;; I use a black background
913 '(highline-face ((((type tty) (class color))
914 (:background "white" :foreground "black")))))
917 *** Split horizontally for agenda
919 If you would like to split the frame into two side-by-side windows when
920 displaying the agenda, try this hack from Jan Rehders, which uses the
921 `toggle-window-split' from
923 http://www.emacswiki.org/cgi-bin/wiki/ToggleWindowSplit
925 #+BEGIN_SRC emacs-lisp
926 ;; Patch org-mode to use vertical splitting
927 (defadvice org-prepare-agenda (after org-fix-split)
928 (toggle-window-split))
929 (ad-activate 'org-prepare-agenda)
932 *** Automatically add an appointment when clocking in a task
934 #+BEGIN_SRC emacs-lisp
935 ;; Make sure you have a sensible value for `appt-message-warning-time'
936 (defvar bzg-org-clock-in-appt-delay 100
937 "Number of minutes for setting an appointment by clocking-in")
940 This function let's you add an appointment for the current entry.
941 This can be useful when you need a reminder.
943 #+BEGIN_SRC emacs-lisp
944 (defun bzg-org-clock-in-add-appt (&optional n)
945 "Add an appointment for the Org entry at point in N minutes."
948 (org-back-to-heading t)
949 (looking-at org-complex-heading-regexp)
950 (let* ((msg (match-string-no-properties 4))
951 (ct-time (decode-time))
952 (appt-min (+ (cadr ct-time)
953 (or n bzg-org-clock-in-appt-delay)))
954 (appt-time ; define the time for the appointment
955 (progn (setf (cadr ct-time) appt-min) ct-time)))
956 (appt-add (format-time-string
957 "%H:%M" (apply 'encode-time appt-time)) msg)
958 (if (interactive-p) (message "New appointment for %s" msg)))))
961 You can advise =org-clock-in= so that =C-c C-x C-i= will automatically
964 #+BEGIN_SRC emacs-lisp
965 (defadvice org-clock-in (after org-clock-in-add-appt activate)
966 "Add an appointment when clocking a task in."
967 (bzg-org-clock-in-add-appt))
970 You may also want to delete the associated appointment when clocking
971 out. This function does this:
973 #+BEGIN_SRC emacs-lisp
974 (defun bzg-org-clock-out-delete-appt nil
975 "When clocking out, delete any associated appointment."
978 (org-back-to-heading t)
979 (looking-at org-complex-heading-regexp)
980 (let* ((msg (match-string-no-properties 4)))
981 (setq appt-time-msg-list
985 (if (not (string-match (regexp-quote msg)
987 appt-time-msg-list)))
991 And here is the advice for =org-clock-out= (=C-c C-x C-o=)
993 #+BEGIN_SRC emacs-lisp
994 (defadvice org-clock-out (before org-clock-out-delete-appt activate)
995 "Delete an appointment when clocking a task out."
996 (bzg-org-clock-out-delete-appt))
999 *IMPORTANT*: You can add appointment by clocking in in both an
1000 =org-mode= and an =org-agenda-mode= buffer. But clocking out from
1001 agenda buffer with the advice above will bring an error.
1003 *** Remove time grid lines that are in an appointment
1005 The agenda shows lines for the time grid. Some people think that
1006 these lines are a distraction when there are appointments at those
1007 times. You can get rid of the lines which coincide exactly with the
1008 beginning of an appointment. Michael Ekstrand has written a piece of
1009 advice that also removes lines that are somewhere inside an
1012 #+begin_src emacs-lisp
1013 (defun org-time-to-minutes (time)
1014 "Convert an HHMM time to minutes"
1015 (+ (* (/ time 100) 60) (% time 100)))
1017 (defun org-time-from-minutes (minutes)
1018 "Convert a number of minutes to an HHMM time"
1019 (+ (* (/ minutes 60) 100) (% minutes 60)))
1021 (defadvice org-agenda-add-time-grid-maybe (around mde-org-agenda-grid-tweakify
1022 (list ndays todayp))
1023 (if (member 'remove-match (car org-agenda-time-grid))
1024 (flet ((extract-window
1026 (let ((start (get-text-property 1 'time-of-day line))
1027 (dur (get-text-property 1 'duration line)))
1031 (org-time-from-minutes
1032 (+ dur (org-time-to-minutes start)))))
1035 (let* ((windows (delq nil (mapcar 'extract-window list)))
1036 (org-agenda-time-grid
1037 (list (car org-agenda-time-grid)
1038 (cadr org-agenda-time-grid)
1041 (find-if (lambda (w)
1044 (and (>= time (car w))
1047 (caddr org-agenda-time-grid)))))
1050 (ad-activate 'org-agenda-add-time-grid-maybe)
1052 *** Disable vc for Org mode agenda files
1055 Even if you use Git to track your agenda files you might not need
1056 vc-mode to be enabled for these files.
1058 #+begin_src emacs-lisp
1059 (add-hook 'find-file-hook 'dmj/disable-vc-for-agenda-files-hook)
1060 (defun dmj/disable-vc-for-agenda-files-hook ()
1061 "Disable vc-mode for Org agenda files."
1062 (if (and (fboundp 'org-agenda-file-p)
1063 (org-agenda-file-p (buffer-file-name)))
1064 (remove-hook 'find-file-hook 'vc-find-file-hook)
1065 (add-hook 'find-file-hook 'vc-find-file-hook)))
1068 *** Easy customization of TODO colors
1071 Here is some code I came up with some code to make it easier to
1072 customize the colors of various TODO keywords. As long as you just
1073 want a different color and nothing else, you can customize the
1074 variable org-todo-keyword-faces and use just a string color (i.e. a
1075 string of the color name) as the face, and then org-get-todo-face
1076 will convert the color to a face, inheriting everything else from
1077 the standard org-todo face.
1079 To demonstrate, I currently have org-todo-keyword-faces set to
1081 #+BEGIN_SRC emacs-lisp
1082 (("IN PROGRESS" . "dark orange")
1083 ("WAITING" . "red4")
1084 ("CANCELED" . "saddle brown"))
1085 #+END_SRC emacs-lisp
1087 Here's the code, in a form you can put in your =.emacs=
1089 #+BEGIN_SRC emacs-lisp
1090 (eval-after-load 'org-faces
1092 (defcustom org-todo-keyword-faces nil
1093 "Faces for specific TODO keywords.
1094 This is a list of cons cells, with TODO keywords in the car and
1095 faces in the cdr. The face can be a symbol, a color, or a
1096 property list of attributes, like (:foreground \"blue\" :weight
1097 bold :underline t)."
1102 (string :tag "Keyword")
1103 (choice color (sexp :tag "Face")))))))
1105 (eval-after-load 'org
1107 (defun org-get-todo-face-from-color (color)
1108 "Returns a specification for a face that inherits from org-todo
1109 face and has the given color as foreground. Returns nil if
1112 `(:inherit org-warning :foreground ,color)))
1114 (defun org-get-todo-face (kwd)
1115 "Get the right face for a TODO keyword KWD.
1116 If KWD is a number, get the corresponding match group."
1117 (if (numberp kwd) (setq kwd (match-string kwd)))
1118 (or (let ((face (cdr (assoc kwd org-todo-keyword-faces))))
1120 (org-get-todo-face-from-color face)
1122 (and (member kwd org-done-keywords) 'org-done)
1124 #+END_SRC emacs-lisp
1126 *** Add an effort estimate on the fly when clocking in
1128 You can use =org-clock-in-prepare-hook= to add an effort estimate.
1129 This way you can easily have a "tea-timer" for your tasks when they
1130 don't already have an effort estimate.
1132 #+begin_src emacs-lisp
1133 (add-hook 'org-clock-in-prepare-hook
1134 'my-org-mode-ask-effort)
1136 (defun my-org-mode-ask-effort ()
1137 "Ask for an effort estimate when clocking in."
1138 (unless (org-entry-get (point) "Effort")
1142 (org-entry-get-multivalued-property (point) "Effort"))))
1143 (unless (equal effort "")
1144 (org-set-property "Effort" effort)))))
1147 Or you can use a default effort for such a timer:
1149 #+begin_src emacs-lisp
1150 (add-hook 'org-clock-in-prepare-hook
1151 'my-org-mode-add-default-effort)
1153 (defvar org-clock-default-effort "1:00")
1155 (defun my-org-mode-add-default-effort ()
1156 "Add a default effort estimation."
1157 (unless (org-entry-get (point) "Effort")
1158 (org-set-property "Effort" org-clock-default-effort)))
1161 *** Refresh the agenda view regurally
1163 Hack sent by Kiwon Um:
1165 #+begin_src emacs-lisp
1166 (defun kiwon/org-agenda-redo-in-other-window ()
1167 "Call org-agenda-redo function even in the non-agenda buffer."
1169 (let ((agenda-window (get-buffer-window org-agenda-buffer-name t)))
1171 (with-selected-window agenda-window (org-agenda-redo)))))
1172 (run-at-time nil 300 'kiwon/org-agenda-redo-in-other-window)
1175 *** Reschedule agenda items to today with a single command
1177 This was suggested by Carsten in reply to David Abrahams:
1179 #+begin_example emacs-lisp
1180 (defun org-agenda-reschedule-to-today ()
1182 (flet ((org-read-date (&rest rest) (current-time)))
1183 (call-interactively 'org-agenda-schedule)))
1186 * Hacking Org: Working with Org-mode and other Emacs Packages.
1187 ** org-remember-anything
1189 [[http://www.emacswiki.org/cgi-bin/wiki/Anything][Anything]] users may find the snippet below interesting:
1191 #+BEGIN_SRC emacs-lisp
1192 (defvar org-remember-anything
1193 '((name . "Org Remember")
1194 (candidates . (lambda () (mapcar 'car org-remember-templates)))
1195 (action . (lambda (name)
1196 (let* ((orig-template org-remember-templates)
1197 (org-remember-templates
1198 (list (assoc name orig-template))))
1199 (call-interactively 'org-remember))))))
1202 You can add it to your 'anything-sources' variable and open remember directly
1203 from anything. I imagine this would be more interesting for people with many
1204 remember templatesm, so that you are out of keys to assign those to. You should
1205 get something like this:
1207 [[file:images/thumbs/org-remember-anything.png]]
1209 ** Org-mode and saveplace.el
1211 Fix a problem with saveplace.el putting you back in a folded position:
1213 #+begin_src emacs-lisp
1214 (add-hook 'org-mode-hook
1216 (when (outline-invisible-p)
1218 (outline-previous-visible-heading 1)
1219 (org-show-subtree)))))
1222 ** Using ido-completing-read to find attachments
1225 Org-attach is great for quickly linking files to a project. But if you
1226 use org-attach extensively you might find yourself wanting to browse
1227 all the files you've attached to org headlines. This is not easy to do
1228 manually, since the directories containing the files are not human
1229 readable (i.e., they are based on automatically generated ids). Here's
1230 some code to browse those files using ido (obviously, you need to be
1233 #+begin_src emacs-lisp
1234 (load-library "find-lisp")
1236 ;; Adapted from http://www.emacswiki.org/emacs/RecentFiles
1238 (defun my-ido-find-org-attach ()
1239 "Find files in org-attachment directory"
1241 (let* ((enable-recursive-minibuffers t)
1242 (files (find-lisp-find-files org-attach-directory "."))
1245 (cons (file-name-nondirectory x)
1249 (remove-duplicates (mapcar #'car file-assoc-list)
1251 (filename (ido-completing-read "Org attachments: " filename-list nil t))
1252 (longname (cdr (assoc filename file-assoc-list))))
1253 (ido-set-current-directory
1254 (if (file-directory-p longname)
1256 (file-name-directory longname)))
1257 (setq ido-exit 'refresh
1258 ido-text-init ido-text
1262 (add-hook 'ido-setup-hook 'ido-my-keys)
1264 (defun ido-my-keys ()
1265 "Add my keybindings for ido."
1266 (define-key ido-completion-map (kbd "C-;") 'my-ido-find-org-attach))
1269 To browse your org attachments using ido fuzzy matching and/or the
1270 completion buffer, invoke ido-find-file as usual (=C-x C-f=) and then
1273 ** Use idle timer for automatic agenda views
1275 From John Wiegley's mailing list post (March 18, 2010):
1278 I have the following snippet in my .emacs file, which I find very
1279 useful. Basically what it does is that if I don't touch my Emacs for 5
1280 minutes, it displays the current agenda. This keeps my tasks "always
1281 in mind" whenever I come back to Emacs after doing something else,
1282 whereas before I had a tendency to forget that it was there.
1285 - [[http://mid.gmane.org/55590EA7-C744-44E5-909F-755F0BBE452D@gmail.com][John Wiegley: Displaying your Org agenda after idle time]]
1287 #+begin_src emacs-lisp
1288 (defun jump-to-org-agenda ()
1290 (let ((buf (get-buffer "*Org Agenda*"))
1293 (if (setq wind (get-buffer-window buf))
1294 (select-window wind)
1295 (if (called-interactively-p)
1297 (select-window (display-buffer buf t t))
1298 (org-fit-window-to-buffer)
1299 ;; (org-agenda-redo)
1301 (with-selected-window (display-buffer buf)
1302 (org-fit-window-to-buffer)
1303 ;; (org-agenda-redo)
1305 (call-interactively 'org-agenda-list)))
1306 ;;(let ((buf (get-buffer "*Calendar*")))
1307 ;; (unless (get-buffer-window buf)
1308 ;; (org-agenda-goto-calendar)))
1311 (run-with-idle-timer 300 t 'jump-to-org-agenda)
1315 : [nil 0 300 0 t jump-to-org-agenda nil idle]
1317 ** Link to Gnus messages by Message-Id
1319 In a [[http://thread.gmane.org/gmane.emacs.orgmode/8860][recent thread]] on the Org-Mode mailing list, there was some
1320 discussion about linking to Gnus messages without encoding the folder
1321 name in the link. The following code hooks in to the store-link
1322 function in Gnus to capture links by Message-Id when in nnml folders,
1323 and then provides a link type "mid" which can open this link. The
1324 =mde-org-gnus-open-message-link= function uses the
1325 =mde-mid-resolve-methods= variable to determine what Gnus backends to
1326 scan. It will go through them, in order, asking each to locate the
1327 message and opening it from the first one that reports success.
1329 It has only been tested with a single nnml backend, so there may be
1330 bugs lurking here and there.
1332 The logic for finding the message was adapted from [[http://www.emacswiki.org/cgi-bin/wiki/FindMailByMessageId][an Emacs Wiki
1335 #+begin_src emacs-lisp
1336 ;; Support for saving Gnus messages by Message-ID
1337 (defun mde-org-gnus-save-by-mid ()
1338 (when (memq major-mode '(gnus-summary-mode gnus-article-mode))
1339 (when (eq major-mode 'gnus-article-mode)
1340 (gnus-article-show-summary))
1341 (let* ((group gnus-newsgroup-name)
1342 (method (gnus-find-method-for-group group)))
1343 (when (eq 'nnml (car method))
1344 (let* ((article (gnus-summary-article-number))
1345 (header (gnus-summary-article-header article))
1346 (from (mail-header-from header))
1349 (let ((mid (mail-header-id header)))
1350 (if (string-match "<\\(.*\\)>" mid)
1351 (match-string 1 mid)
1352 (error "Malformed message ID header %s" mid)))))
1353 (date (mail-header-date header))
1354 (subject (gnus-summary-subject-string)))
1355 (org-store-link-props :type "mid" :from from :subject subject
1356 :message-id message-id :group group
1357 :link (org-make-link "mid:" message-id))
1358 (apply 'org-store-link-props
1359 :description (org-email-link-description)
1360 org-store-link-plist)
1363 (defvar mde-mid-resolve-methods '()
1364 "List of methods to try when resolving message ID's. For Gnus,
1365 it is a cons of 'gnus and the select (type and name).")
1366 (setq mde-mid-resolve-methods
1369 (defvar mde-org-gnus-open-level 1
1370 "Level at which Gnus is started when opening a link")
1371 (defun mde-org-gnus-open-message-link (msgid)
1372 "Open a message link with Gnus"
1374 (require 'org-table)
1375 (catch 'method-found
1376 (message "[MID linker] Resolving %s" msgid)
1377 (dolist (method mde-mid-resolve-methods)
1379 ((and (eq (car method) 'gnus)
1380 (eq (cadr method) 'nnml))
1381 (funcall (cdr (assq 'gnus org-link-frame-setup))
1382 mde-org-gnus-open-level)
1383 (when gnus-other-frame-object
1384 (select-frame gnus-other-frame-object))
1385 (let* ((msg-info (nnml-find-group-number
1386 (concat "<" msgid ">")
1388 (group (and msg-info (car msg-info)))
1389 (message (and msg-info (cdr msg-info)))
1391 (if (gnus-methods-equal-p
1395 (gnus-group-full-name group (cdr method))))))
1397 (gnus-summary-read-group qname nil t)
1398 (gnus-summary-goto-article message nil t))
1399 (throw 'method-found t)))
1400 (t (error "Unknown link type"))))))
1402 (eval-after-load 'org-gnus
1404 (add-to-list 'org-store-link-functions 'mde-org-gnus-save-by-mid)
1405 (org-add-link-type "mid" 'mde-org-gnus-open-message-link)))
1408 ** Store link upon sending a message in Gnus
1410 Ulf Stegemann came up with this solution (see his [[http://www.mail-archive.com/emacs-orgmode@gnu.org/msg33278.html][original message]]):
1412 #+begin_src emacs-lisp
1413 (defun ulf-message-send-and-org-gnus-store-link (&optional arg)
1414 "Send message with `message-send-and-exit' and store org link to message copy.
1415 If multiple groups appear in the Gcc header, the link refers to
1416 the copy in the last group."
1420 (message-narrow-to-headers)
1421 (let ((gcc (car (last
1422 (message-unquote-tokens
1423 (message-tokenize-header
1424 (mail-fetch-field "gcc" nil t) " ,")))))
1425 (buf (current-buffer))
1426 (message-kill-buffer-on-exit nil)
1427 id to from subject desc link newsgroup xarchive)
1428 (message-send-and-exit arg)
1430 ;; gcc group found ...
1432 (save-current-buffer
1433 (progn (set-buffer buf)
1434 (setq id (org-remove-angle-brackets
1435 (mail-fetch-field "Message-ID")))
1436 (setq to (mail-fetch-field "To"))
1437 (setq from (mail-fetch-field "From"))
1438 (setq subject (mail-fetch-field "Subject"))))
1439 (org-store-link-props :type "gnus" :from from :subject subject
1440 :message-id id :group gcc :to to)
1441 (setq desc (org-email-link-description))
1442 (setq link (org-gnus-article-link
1443 gcc newsgroup id xarchive))
1444 (setq org-stored-links
1445 (cons (list link desc) org-stored-links)))
1446 ;; no gcc group found ...
1447 (message "Can not create Org link: No Gcc header found."))))))
1449 (define-key message-mode-map [(control c) (control meta c)]
1450 'ulf-message-send-and-org-gnus-store-link)
1453 ** Send html messages and attachments with Wanderlust
1456 /Note/: The module [[file:org-contrib/org-mime.org][Org-mime]] in Org's contrib directory provides
1457 similar functionality for both Wanderlust and Gnus. The hack below is
1458 still somewhat different: It allows you to toggle sending of html
1459 messages within Wanderlust transparently. I.e. html markup of the
1460 message body is created right before sending starts.
1462 *** Send HTML message
1464 Putting the code below in your .emacs adds following four functions:
1466 - dmj/wl-send-html-message
1468 Function that does the job: Convert everything between "--text
1469 follows this line--" and first mime entity (read: attachment) or
1470 end of buffer into html markup using `org-export-region-as-html'
1471 and replaces original body with a multipart MIME entity with the
1472 plain text version of body and the html markup version. Thus a
1473 recipient that prefers html messages can see the html markup,
1474 recipients that prefer or depend on plain text can see the plain
1477 Cannot be called interactively: It is hooked into SEMI's
1478 `mime-edit-translate-hook' if message should be HTML message.
1480 - dmj/wl-send-html-message-draft-init
1482 Cannot be called interactively: It is hooked into WL's
1483 `wl-mail-setup-hook' and provides a buffer local variable to
1486 - dmj/wl-send-html-message-draft-maybe
1488 Cannot be called interactively: It is hooked into WL's
1489 `wl-draft-send-hook' and hooks `dmj/wl-send-html-message' into
1490 `mime-edit-translate-hook' depending on whether HTML message is
1493 - dmj/wl-send-html-message-toggle
1495 Toggles sending of HTML message. If toggled on, the letters
1496 "HTML" appear in the mode line.
1498 Call it interactively! Or bind it to a key in `wl-draft-mode'.
1500 If you have to send HTML messages regularly you can set a global
1501 variable `dmj/wl-send-html-message-toggled-p' to the string "HTML" to
1502 toggle on sending HTML message by default.
1504 The image [[http://s11.directupload.net/file/u/15851/48ru5wl3.png][here]] shows an example of how the HTML message looks like in
1505 Google's web front end. As you can see you have the whole markup of
1506 Org at your service: *bold*, /italics/, tables, lists...
1508 So even if you feel uncomfortable with sending HTML messages at least
1509 you send HTML that looks quite good.
1511 #+begin_src emacs-lisp
1512 (defun dmj/wl-send-html-message ()
1513 "Send message as html message.
1514 Convert body of message to html using
1515 `org-export-region-as-html'."
1518 (let (beg end html text)
1519 (goto-char (point-min))
1520 (re-search-forward "^--text follows this line--$")
1521 ;; move to beginning of next line
1522 (beginning-of-line 2)
1524 (if (not (re-search-forward "^--\\[\\[" nil t))
1525 (setq end (point-max))
1530 (setq text (buffer-substring-no-properties beg end))
1536 (when (re-search-backward "^-- \n" nil t)
1537 ;; preserve link breaks in signature
1538 (insert "\n#+BEGIN_VERSE\n")
1539 (goto-char (point-max))
1540 (insert "\n#+END_VERSE\n")
1542 (setq html (org-export-region-as-html
1543 (point-min) (point-max) t 'string))))
1544 (delete-region beg end)
1547 "--" "<<alternative>>-{\n"
1548 "--" "[[text/plain]]\n" text
1549 "--" "[[text/html]]\n" html
1550 "--" "}-<<alternative>>\n")))))
1552 (defun dmj/wl-send-html-message-toggle ()
1553 "Toggle sending of html message."
1555 (setq dmj/wl-send-html-message-toggled-p
1556 (if dmj/wl-send-html-message-toggled-p
1558 (message "Sending html message toggled %s"
1559 (if dmj/wl-send-html-message-toggled-p
1562 (defun dmj/wl-send-html-message-draft-init ()
1563 "Create buffer local settings for maybe sending html message."
1564 (unless (boundp 'dmj/wl-send-html-message-toggled-p)
1565 (setq dmj/wl-send-html-message-toggled-p nil))
1566 (make-variable-buffer-local 'dmj/wl-send-html-message-toggled-p)
1567 (add-to-list 'global-mode-string
1568 '(:eval (if (eq major-mode 'wl-draft-mode)
1569 dmj/wl-send-html-message-toggled-p))))
1571 (defun dmj/wl-send-html-message-maybe ()
1572 "Maybe send this message as html message.
1574 If buffer local variable `dmj/wl-send-html-message-toggled-p' is
1575 non-nil, add `dmj/wl-send-html-message' to
1576 `mime-edit-translate-hook'."
1577 (if dmj/wl-send-html-message-toggled-p
1578 (add-hook 'mime-edit-translate-hook 'dmj/wl-send-html-message)
1579 (remove-hook 'mime-edit-translate-hook 'dmj/wl-send-html-message)))
1581 (add-hook 'wl-draft-reedit-hook 'dmj/wl-send-html-message-draft-init)
1582 (add-hook 'wl-mail-setup-hook 'dmj/wl-send-html-message-draft-init)
1583 (add-hook 'wl-draft-send-hook 'dmj/wl-send-html-message-maybe)
1586 *** Attach HTML of region or subtree
1588 Instead of sending a complete HTML message you might only send parts
1589 of an Org file as HTML for the poor souls who are plagued with
1590 non-proportional fonts in their mail program that messes up pretty
1593 This short function does the trick: It exports region or subtree to
1594 HTML, prefixes it with a MIME entity delimiter and pushes to killring
1595 and clipboard. If a region is active, it uses the region, the
1596 complete subtree otherwise.
1598 #+begin_src emacs-lisp
1599 (defun dmj/org-export-region-as-html-attachment (beg end arg)
1600 "Export region between BEG and END as html attachment.
1601 If BEG and END are not set, use current subtree. Region or
1602 subtree is exported to html without header and footer, prefixed
1603 with a mime entity string and pushed to clipboard and killring.
1604 When called with prefix, mime entity is not marked as
1606 (interactive "r\nP")
1608 (let* ((beg (if (region-active-p) (region-beginning)
1610 (org-back-to-heading)
1612 (end (if (region-active-p) (region-end)
1614 (org-end-of-subtree)
1616 (html (concat "--[[text/html"
1617 (if arg "" "\nContent-Disposition: attachment")
1619 (org-export-region-as-html beg end t 'string))))
1620 (when (fboundp 'x-set-selection)
1621 (ignore-errors (x-set-selection 'PRIMARY html))
1622 (ignore-errors (x-set-selection 'CLIPBOARD html)))
1623 (message "html export done, pushed to kill ring and clipboard"))))
1626 *** Adopting for Gnus
1628 The whole magic lies in the special strings that mark a HTML
1629 attachment. So you might just have to find out what these special
1630 strings are in message-mode and modify the functions accordingly.
1631 * Hacking Org: Working with Org-mode and External Programs.
1632 ** Use Org-mode with Screen [Andrew Hyatt]
1634 "The general idea is that you start a task in which all the work will
1635 take place in a shell. This usually is not a leaf-task for me, but
1636 usually the parent of a leaf task. From a task in your org-file, M-x
1637 ash-org-screen will prompt for the name of a session. Give it a name,
1638 and it will insert a link. Open the link at any time to go the screen
1639 session containing your work!"
1641 http://article.gmane.org/gmane.emacs.orgmode/5276
1643 #+BEGIN_SRC emacs-lisp
1646 (defun ash-org-goto-screen (name)
1647 "Open the screen with the specified name in the window"
1648 (interactive "MScreen name: ")
1649 (let ((screen-buffer-name (ash-org-screen-buffer-name name)))
1650 (if (member screen-buffer-name
1651 (mapcar 'buffer-name (buffer-list)))
1652 (switch-to-buffer screen-buffer-name)
1653 (switch-to-buffer (ash-org-screen-helper name "-dr")))))
1655 (defun ash-org-screen-buffer-name (name)
1656 "Returns the buffer name corresponding to the screen name given."
1657 (concat "*screen " name "*"))
1659 (defun ash-org-screen-helper (name arg)
1660 ;; Pick the name of the new buffer.
1661 (let ((term-ansi-buffer-name
1662 (generate-new-buffer-name
1663 (ash-org-screen-buffer-name name))))
1664 (setq term-ansi-buffer-name
1665 (term-ansi-make-term
1666 term-ansi-buffer-name "/usr/bin/screen" nil arg name))
1667 (set-buffer term-ansi-buffer-name)
1670 (term-set-escape-char ?\C-x)
1671 term-ansi-buffer-name))
1673 (defun ash-org-screen (name)
1674 "Start a screen session with name"
1675 (interactive "MScreen name: ")
1677 (ash-org-screen-helper name "-S"))
1678 (insert-string (concat "[[screen:" name "]]")))
1680 ;; And don't forget to add ("screen" . "elisp:(ash-org-goto-screen
1681 ;; \"%s\")") to org-link-abbrev-alist.
1684 ** Org Agenda + Appt + Zenity
1686 Russell Adams posted this setup [[http://article.gmane.org/gmane.emacs.orgmode/5806][on the list]]. It make sure your agenda
1687 appointments are known by Emacs, and it displays warnings in a [[http://live.gnome.org/Zenity][zenity]]
1690 #+BEGIN_SRC emacs-lisp
1691 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1692 ; For org appointment reminders
1694 ;; Get appointments for today
1695 (defun my-org-agenda-to-appt ()
1697 (setq appt-time-msg-list nil)
1698 (let ((org-deadline-warning-days 0)) ;; will be automatic in org 5.23
1699 (org-agenda-to-appt)))
1701 ;; Run once, activate and schedule refresh
1702 (my-org-agenda-to-appt)
1704 (run-at-time "24:01" nil 'my-org-agenda-to-appt)
1707 (setq appt-message-warning-time 15)
1708 (setq appt-display-interval 5)
1710 ; Update appt each time agenda opened.
1711 (add-hook 'org-finalize-agenda-hook 'my-org-agenda-to-appt)
1713 ; Setup zenify, we tell appt to use window, and replace default function
1714 (setq appt-display-format 'window)
1715 (setq appt-disp-window-function (function my-appt-disp-window))
1717 (defun my-appt-disp-window (min-to-app new-time msg)
1718 (save-window-excursion (shell-command (concat
1719 "/usr/bin/zenity --info --title='Appointment' --text='"
1720 msg "' &") nil nil)))
1723 ** Org-Mode + gnome-osd
1725 Richard Riley uses gnome-osd in interaction with Org-Mode to display
1726 appointments. You can look at the code on the [[http://www.emacswiki.org/emacs-en/OrgMode-OSD][emacswiki]].
1732 http://article.gmane.org/gmane.emacs.orgmode/5073
1735 Remind (http://www.roaringpenguin.com/products/remind) is a very powerful
1736 command line calendaring program. Its features superseed the possibilities
1737 of orgmode in the area of date specifying, so that I want to use it
1738 combined with orgmode.
1740 Using the script below I'm able use remind and incorporate its output in my
1741 agenda views. The default of using 13 months look ahead is easily
1742 changed. It just happens I sometimes like to look a year into the
1746 ** Useful webjumps for conkeror
1748 If you are using the [[http://conkeror.org][conkeror browser]], maybe you want to put this into
1749 your =~/.conkerorrc= file:
1752 define_webjump("orglist", "http://search.gmane.org/?query=%s&group=gmane.emacs.orgmode");
1753 define_webjump("worg", "http://www.google.com/cse?cx=002987994228320350715%3Az4glpcrritm&ie=UTF-8&q=%s&sa=Search&siteurl=orgmode.org%2Fworg%2F");
1756 It creates two [[http://conkeror.org/Webjumps][webjumps]] for easily searching the Worg website and the
1757 Org-mode mailing list.
1759 ** Use MathJax for HTML export without requiring JavaScript
1760 As of 2010-08-14, MathJax is the default method used to export math to HTML.
1762 If you like the results but do not want JavaScript in the exported pages,
1763 check out [[http://www.jboecker.de/2010/08/15/staticmathjax.html][Static MathJax]], a XULRunner application which generates a static
1764 HTML file from the exported version. It can also embed all referenced fonts
1765 within the HTML file itself, so there are no dependencies to external files.
1767 The download archive contains an elisp file which integrates it into the Org
1768 export process (configurable per file with a "#+StaticMathJax:" line).
1770 Read README.org and the comments in org-static-mathjax.el for usage instructions.
1771 ** Search Org files using lgrep
1773 Matt Lundi suggests this:
1775 #+begin_src emacs-lisp
1776 (defun my-org-grep (search &optional context)
1777 "Search for word in org files.
1779 Prefix argument determines number of lines."
1780 (interactive "sSearch for: \nP")
1781 (let ((grep-find-ignored-files '("#*" ".#*"))
1782 (grep-template (concat "grep <X> -i -nH "
1784 (concat "-C" (number-to-string context)))
1786 (lgrep search "*org*" "/home/matt/org/")))
1788 (global-set-key (kbd "<f8>") 'my-org-grep)
1791 ** Automatic screenshot insertion
1793 Suggested by Jonathan Bisson:
1795 #+begin_src emacs-lisp
1796 (defun my-screenshot ()
1797 "Take a screenshot into a unique-named file in the current buffer file
1798 directory and insert a link to this file."
1803 (file-name-directory (buffer-file-name))) ".jpg"))
1804 (call-process "import" nil nil nil filename)
1805 (insert (concat "[[" filename "]]"))
1806 (org-display-inline-images))