How to locally unset (org-clock-into-drawer t) - emacs

Is there a possibility to locally unset the clocking location variable org-clock-into-drawer?
I'd like to have clocking tucked away most of the time in some drawers. But for my general tracking file I do not want to clock into a drawer
Enabling clocking behavior globally
(setq org-drawers (quote ("PROPERTIES" "LOGBOOK"))) ;; Separate drawers for clocking and logs
(setq org-clock-into-drawer t) ;; Save clock data and state changes and notes in the LOGBOOK drawer
will result in something like this:
* Maintenance
:LOGBOOK:
CLOCK: [2014-10-16 Thu 08:48]--[2014-10-16 Thu 09:08] => 0:20
CLOCK: [2014-10-15 Wed 08:51]--[2014-10-15 Wed 09:01] => 0:10
CLOCK: [2014-10-14 Tue 08:40]--[2014-10-14 Tue 08:45] => 0:05
CLOCK: [2014-10-13 Mon 08:41]--[2014-10-13 Mon 08:59] => 0:18
:END:
I want to unset the behavior for particular files. Based on Clocking-commands I expect something like #+PROPERTY CLOCK_INTO_DRAWER: nil to result in the following output
* Maintenance
CLOCK: [2014-10-16 Thu 08:48]--[2014-10-16 Thu 09:08] => 0:20
CLOCK: [2014-10-15 Wed 08:51]--[2014-10-15 Wed 09:01] => 0:10
CLOCK: [2014-10-14 Tue 08:40]--[2014-10-14 Tue 08:45] => 0:05
CLOCK: [2014-10-13 Mon 08:41]--[2014-10-13 Mon 08:59] => 0:18
But it seems not to work as intended.

That seems like a bug to me. You should be able to override
org-clock-into-drawer with
#+PROPERTY: CLOCK_INTO_DRAWER nil
Note that the above line is slightly different than what you used, but
it still won't work. The issue is with the function
org-clock-into-drawer. I'll send these changes to the Org mode list,
but if you want to get this working on your end immediately, the
following patch should fix the issue.
diff --git c/lisp/org-clock.el w/lisp/org-clock.el
index 2ffcbfa..092a6aa 100644
--- c/lisp/org-clock.el
+++ w/lisp/org-clock.el
## -74,13 +74,15 ## (defun org-clock-into-drawer ()
it will be used instead of the default value.
The default is the value of the customizable variable `org-clock-into-drawer',
which see."
- (let ((p (org-entry-get nil "CLOCK_INTO_DRAWER" 'inherit))
- (q (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
- (cond
- ((or (not (or p q)) (equal p "nil") (equal q "nil")) org-clock-into-drawer)
- ((or (equal p "t") (equal q "t")) "LOGBOOK")
- ((not p) q)
- (t p))))
+ (let ((p (org-entry-get nil "CLOCK_INTO_DRAWER" 'inherit t))
+ (q (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
+ (cond ((equal p "nil") nil)
+ ((equal p "t") t)
+ (p)
+ ((equal q "nil") nil)
+ ((equal q "t") t)
+ (q)
+ (t org-clock-into-drawer))))
(defcustom org-clock-out-when-done t
"When non-nil, clock will be stopped when the clocked entry is marked DONE.
Edit: I've updated the patch based on this discussion. The fix is in commit 70e0b08e.

Related

How to convert (day month year) to ( month day year)

My e-mails in Wanderlust have a header that looks like this:
Date: Wed, 23 Oct 2013 12:18:15 -0700
I would like to modify the beginning of my print-to-pdf function so that it searches the current buffer for the first date it finds (usually the first line of the buffer) and converts it into a proposed pdf-file-name that looks like this:
10_23_2013.pdf
The beginning of my print-to-pdf function looks like this:
(defun print-to-pdf (pdf-file-name)
"Print the current buffer to the given file."
(interactive (list
(ns-read-file-name "Write PDF file: " "/Users/HOME/.0.data/" nil ".pdf")))
(cond (
(not (equal pdf-file-name nil))
***
Can anyone think of a way to search for the date and turn it into a proposed pdf-file-name?
EDIT:  Here are some of the date string functions I found by grepping the Wanderlust code:
(defun wl-make-date-string ()
(let ((system-time-locale "C"))
(format-time-string "%a, %d %b %Y %T %z")))
(defsubst wl-get-date-iso8601 (date)
(or (get-text-property 0 'wl-date date)
(let* ((d1 (timezone-fix-time date nil nil))
(time (format "%04d%02d%02dT%02d%02d%02d"
(aref d1 0) (aref d1 1) (aref d1 2)
(aref d1 3) (aref d1 4) (aref d1 5))))
(put-text-property 0 1 'wl-date time date)
time)))
(defun wl-make-date-string ()
(let ((s (current-time-string)))
(string-match "\\`\\([A-Z][a-z][a-z]\\) +[A-Z][a-z][a-z] +[0-9][0-9]? *[0-9][0-9]?:[0-9][0-9]:[0-9][0-9] *[0-9]?[0-9]?[0-9][0-9]"
s)
(concat (wl-match-string 1 s) ", "
(timezone-make-date-arpa-standard s (current-time-zone)))))
(defun wl-date-iso8601 (date)
"Convert the DATE to YYMMDDTHHMMSS."
(condition-case ()
(wl-get-date-iso8601 date)
(error "")))
Here's the function.
If you can find a way to extract Wed, 23 Oct 2013 12:18:15 -0700, it will produce
10_23_2013.pdf.
(defun transform-date (s &optional shift)
(let ((time (apply 'encode-time
(org-read-date-analyze
s nil
(decode-time (current-time))))))
(when shift
(setq time (time-add time (days-to-time shift))))
(format-time-string "%m_%d_%Y.pdf" time)))
Here's a simple finder for the date:
(defun find-and-transform ()
(goto-char (point-min))
(when (re-search-forward "Date: \\([^:]*?\\)[0-9]+:")
(transform-date
(match-string-no-properties 1))))

Custom function for next / previous month in agenda view and the calendar

I'm looking for a way to select next or previous month in both agenda view and the calendar. I've written a concept/prototype function (below), but it doesn't calculate the next or previous months and I would also need to rewrite the function every month.
The date format for the org-agenda-month-view is different than the date format for calendar-other-month. Further down below are some functions that are related to what I'm trying to accomplish -- e.g., calendar already has the ability to move forward or backward by month.
I think what may be needed is a function that identifies the month being viewed and then adds plus-or-minus one month (in the proper format) when hitting the next or previous button.
(defun lawlist-org-agenda-view-mode-dispatch ()
"Select the month in agenda view."
(interactive)
(message "View: [7] JUL | [8] AUG | [9] SEP | [o]CT | [n]OV | [d]EC ")
(let ((a (read-char-exclusive)))
(case a
(?7
(org-agenda nil "a")
(org-agenda-month-view 201307)
(calendar)
(calendar-other-month 7 2013)
(lawlist-org-agenda-view-mode-dispatch))
(?8
(org-agenda nil "a")
(org-agenda-month-view 201308)
(calendar)
(calendar-other-month 8 2013)
(lawlist-org-agenda-view-mode-dispatch))
(?9
(org-agenda nil "a")
(org-agenda-month-view 201309)
(calendar)
(calendar-other-month 9 2013)
(lawlist-org-agenda-view-mode-dispatch))
(?o
(org-agenda nil "a")
(org-agenda-month-view 201310)
(calendar)
(calendar-other-month 10 2013)
(lawlist-org-agenda-view-mode-dispatch))
(?n
(org-agenda nil "a")
        (org-agenda-month-view 201311)
(calendar)
(calendar-other-month 11 2013)
(lawlist-org-agenda-view-mode-dispatch))
(?d
(org-agenda nil "a")
(org-agenda-month-view 201312)
(calendar)
(calendar-other-month 12 2013)
(lawlist-org-agenda-view-mode-dispatch))
(?q (message "Abort"))
(otherwise (error "Either press \"q\" to quit, or select another option." )))))
Here are some related functions I've extracted from cal-move.el and calendar.el:
(defun calendar-other-month (month year &optional event)
"Display a three-month calendar centered around MONTH and YEAR.
EVENT is an event like `last-nonmenu-event'."
(interactive (let ((event (list last-nonmenu-event)))
(append (calendar-read-date 'noday) event)))
(save-selected-window
(and event
(setq event (event-start event))
(select-window (posn-window event)))
(unless (and (= month displayed-month)
(= year displayed-year))
(let ((old-date (calendar-cursor-to-date))
(today (calendar-current-date)))
(calendar-generate-window month year)
(calendar-cursor-to-visible-date
(cond
((calendar-date-is-visible-p old-date) old-date)
((calendar-date-is-visible-p today) today)
(t (list month 1 year))))))))
;;;###cal-autoload
(defun calendar-forward-month (arg)
"Move the cursor forward ARG months.
Movement is backward if ARG is negative."
(interactive "p")
(calendar-cursor-to-nearest-date)
(let* ((cursor-date (calendar-cursor-to-date t))
(month (calendar-extract-month cursor-date))
(day (calendar-extract-day cursor-date))
(year (calendar-extract-year cursor-date))
(last (progn
(calendar-increment-month month year arg)
(calendar-last-day-of-month month year)))
(day (min last day))
;; Put the new month on the screen, if needed, and go to the new date.
(new-cursor-date (list month day year)))
(if (not (calendar-date-is-visible-p new-cursor-date))
(calendar-other-month month year))
(calendar-cursor-to-visible-date new-cursor-date))
(run-hooks 'calendar-move-hook))
;;;###cal-autoload
(defun calendar-backward-month (arg)
"Move the cursor backward by ARG months.
Movement is forward if ARG is negative."
(interactive "p")
(calendar-forward-month (- arg)))
;;;###cal-autoload
(defun calendar-forward-year (arg)
"Move the cursor forward by ARG years.
Movement is backward if ARG is negative."
(interactive "p")
(calendar-forward-month (* 12 arg)))
;;;###cal-autoload
(defun calendar-backward-year (arg)
"Move the cursor backward ARG years.
Movement is forward is ARG is negative."
(interactive "p")
(calendar-forward-month (* -12 arg)))
;;;###cal-autoload
(defun calendar-scroll-left (&optional arg event)
"Scroll the displayed calendar left by ARG months.
If ARG is negative the calendar is scrolled right. Maintains the relative
position of the cursor with respect to the calendar as well as possible.
EVENT is an event like `last-nonmenu-event'."
(interactive (list (prefix-numeric-value current-prefix-arg)
last-nonmenu-event))
(unless arg (setq arg 1))
(save-selected-window
;; Nil if called from menu-bar.
(if (setq event (event-start event)) (select-window (posn-window event)))
(calendar-cursor-to-nearest-date)
(unless (zerop arg)
(let ((old-date (calendar-cursor-to-date))
(today (calendar-current-date))
(month displayed-month)
(year displayed-year))
(calendar-increment-month month year arg)
(calendar-generate-window month year)
(calendar-cursor-to-visible-date
(cond
((calendar-date-is-visible-p old-date) old-date)
((calendar-date-is-visible-p today) today)
(t (list month 1 year))))))
(run-hooks 'calendar-move-hook)))
(define-obsolete-function-alias
'scroll-calendar-left 'calendar-scroll-left "23.1")
;;;###cal-autoload
(defun calendar-scroll-right (&optional arg event)
"Scroll the displayed calendar window right by ARG months.
If ARG is negative the calendar is scrolled left. Maintains the relative
position of the cursor with respect to the calendar as well as possible.
EVENT is an event like `last-nonmenu-event'."
(interactive (list (prefix-numeric-value current-prefix-arg)
last-nonmenu-event))
(calendar-scroll-left (- (or arg 1)) event))
(define-obsolete-function-alias
'scroll-calendar-right 'calendar-scroll-right "23.1")
Here's my take:
(defvar lawlist-month)
(defun lawlist-org-agenda-view-mode-dispatch ()
"Select the month in agenda view."
(interactive)
(message "View: [1-9] [o]CT [n]OV [d]EC, j(next), k(prev).")
(let* ((a (read-char-exclusive))
(month (case a
(?o 10)
(?n 11)
(?d 12)
(?j (or (and lawlist-month (mod (1+ lawlist-month) 12)) 1))
(?k (or (and lawlist-month (mod (1- lawlist-month) 12)) 1))
(t (and (> a ?0) (<= a ?9) (- a ?0))))))
(if (setq lawlist-month month)
(let ((year (nth 5 (decode-time (current-time)))))
(org-agenda nil "a")
(org-agenda-month-view
(read (format "%d%02d" year month)))
(calendar)
(calendar-other-month month year)
(lawlist-org-agenda-view-mode-dispatch))
(message "Aborted"))))
It still misses some functionality like saving the window configuration and
recovering on abort.
UPD
The updated code can be found in this gist.
I've added other years besides current, with support for j/k, as well as h/l for years.

How to automatically trigger a change in TODO state in Emacs org-mode

Using Emacs org-mode, I often have a list of TODO items that I want to have active one at a time, in sequence. For example when the Item A in a list is marked "DONE", then I want Item B to be automatically be marked "TODO". How do I make this happen? This should not be any kind of general behavior, it should only happen with specific pairs or lists of items that I chose.
;; init-org-lawlist.el
;;
;; QUESTION #Brian Z -- stackoverflow.com: "How to automatically trigger a change in TODO state in Emacs org-mode."
;;
;; "I often have a list of TODO items that I want to have active one at a time, in sequence. For example when the
;; Item A in a list is marked "DONE", then I want Item B to be automatically be marked "TODO". How do I make this
;; happen? This should not be any kind of general behavior, it should only happen with specific pairs or lists of
;; items that I chose."
;;
;;
;; ANSWER #lawlist -- stackoverflow.com -- This script provides two (2) possible options:
;;
;; 1. Navigate to a ** TODO heading (must have at least one todo subtree underneath) and enter: M-x lawlist-done
;; OR
;; 2. Within a ** TODO heading (must have at least one todo subtree underneath), Shift-Right or Shift-Left and cycle
;; to the option ** DONE and then confirm yes or no.
;;
;; NOTE # A: This configuration script assumes the `org-archive-location` will use the SAME lawlist.org file for everything.
;; In this example, the script uses: (setq org-archive-location "/Users/HOME/.0.data/lawlist.org::* ARCHIVES")
;;
;; NOTE # B: Whenever using setq . . ., there is a likelihood that other settings with the same variable will be trumped.
;; So, after testing this script, it would be best to mix and match portions you like with your own configuration for org-mode.
;; You may need to temporarily disable your other configurations that use the same setq variables so as not to conflict while
;; testing this script.
;;
;; NOTE # C: Author to consider adding a check for the existence of at least one subsequent todo subtree as a condition precedent.
;;
;; NOTE # D: Author to consider adding a check that the user is on a second level tier (i.e., a todo) as a condition precedent.
;;
;;
;;
;;
;; SAMPLE lawlist.org CONFIGURATION FILE
;;
;;
;; * TASKS
;;
;; ** TODO [#A] First, figure out this new feature request. :lawlist:
;; DEADLINE: <2013-07-09 Tue>
;; :PROPERTIES:
;; :lawlist-drawer: ACTIVE
;; :END:
;;
;; ** NEXT [#B] Second, If at first you don't succeed, then try again. :lawlist:
;; :PROPERTIES:
;; :lawlist-drawer: PENDING
;; :END:
;;
;; ** WAITING [#C] Third, try again to figure out this new feature request. :lawlist: :WAITING:
;; :PROPERTIES:
;; :lawlist-drawer: DORMANT
;; :END:
;;
;; ** HOLD [#D] Fourth, try, try again to figure out this new feature request. :lawlist: :WAITING:HOLD:
;; :PROPERTIES:
;; :lawlist-drawer: DORMANT
;; :END:
;;
;;
;; * ARCHIVES
;;
;; ** DONE [#E] This task is a done deal. :lawlist:
;; :PROPERTIES:
;; :lawlist-drawer: COMPLETED
;; :END:
(require 'org)
(setq org-startup-folded 'showeverything) ;; overview | content | all | showeverything
(setq org-tags-column 0)
(setq org-startup-indented nil)
(setq org-indent-mode nil)
(setq org-support-shift-select t) ;; 'always to disable; or, t to use shift-select only when cursor is within a special context.
(setq org-cycle-separator-lines 1)
(setq org-insert-heading-respect-content t)
(setq org-blank-before-new-entry '((heading . t) (plain-list-item . nil)))
(setq org-enable-priority-commands t)
(setq org-highest-priority ?A)
(setq org-lowest-priority ?E)
(setq org-default-priority ?A)
(setq org-ellipsis " \u25bc" )
;; '!' (for a timestamp) or '#' (for a note with timestamp)
(setq org-todo-keywords
(quote ((sequence "TODO(t)" "NEXT(n)" "WAITING(w)" "HOLD(h)" "|" "DONE(d)" "CANCELED(c)") )))
(setq org-global-properties '(("lawlist-drawer_ALL". "ACTIVE PENDING DORMANT COMPLETED")))
(setq org-default-properties (cons "lawlist-drawer" org-default-properties))
(setq org-todo-state-tags-triggers
(quote (("CANCELED" ("CANCELED" . t))
("WAITING" ("WAITING" . t))
("HOLD" ("WAITING" . t) ("HOLD" . t))
(done ("WAITING") ("HOLD"))
("TODO" ("WAITING") ("CANCELED") ("HOLD"))
("NEXT" ("WAITING") ("CANCELED") ("HOLD"))
("DONE" ("WAITING") ("CANCELED") ("HOLD")))))
(setq org-tag-alist (quote ((:startgroup)
("john-doe" . ?j)
("jane-doe" . ?J)
(:endgroup)
("lawlist" . ?0))))
(add-hook 'org-after-todo-state-change-hook 'lawlist-hook)
(defun lawlist-hook (&optional default-heading)
(let ((lawlist-item default-heading)
result)
(unless lawlist-item
(condition-case nil
(progn
(org-back-to-heading t)
(setq lawlist-item (elt (org-heading-components) 4)))
)
)
(when (string-equal org-state "DONE")
(if (string-equal org-state "DONE")
(or (yes-or-no-p (format "%s -- process?" org-state))
(error "You changed your mind.")))
(org-forward-heading-same-level 1)
(org-todo "TODO")
(org-priority ?A)
(org-deadline nil "<%<%Y-%m-%d %a>>")
(org-set-property "lawlist-drawer" "ACTIVE")
(org-backward-heading-same-level 1)
(org-priority ?E)
(org-deadline 'remove)
(org-set-property "lawlist-drawer" "COMPLETED")
;; (setq org-archive-save-context-info nil) ;; Set to nil if user doesn't want archive info.
;; NOTE: User must set the correct path to his / her lawlist.org file.
(setq org-archive-location "/Users/HOME/.0.data/lawlist.org::* ARCHIVES")
(org-archive-subtree)
(goto-char (point-min))
(re-search-forward "^\* ARCHIVES" nil t)
(org-sort-entries t ?a)
;; (org-sort-entries t ?d) additional sorting criteria if deadline is not removed.
(lawlist-org-cleanup)
(goto-char (point-min))
(re-search-forward lawlist-item nil t)
(beginning-of-line)
(message "Please take a moment to visually verify your completed tasks has been refiled correctly, then press any key.")
(read-event) )))
(defun delete-trailing-blank-lines-at-end-of-file ()
"Deletes all blank lines at the end of the file, even the last one"
(interactive)
(save-excursion
(save-restriction
(widen)
(goto-char (point-max))
(delete-blank-lines)
(let ((trailnewlines (abs (skip-chars-backward "\n\t"))))
(if (> trailnewlines 0)
(progn
(delete-char trailnewlines)))))))
(defun lawlist-org-cleanup ()
(interactive)
(save-excursion
(replace-regexp "\n+\\*\\* " "\n\n** " nil (point-min) (point-max))
(replace-regexp "\n+\\* " "\n\n\n* " nil (point-min) (point-max))
(replace-regexp "\n\t\s*" "\n " nil (point-min) (point-max)) )
(delete-trailing-blank-lines-at-end-of-file) )
(defun lawlist-done (&optional default-heading)
(interactive)
(remove-hook 'org-after-todo-state-change-hook 'lawlist-hook)
(let ((lawlist-item default-heading)
result)
(unless lawlist-item
(condition-case nil
(progn
(org-back-to-heading t)
(setq lawlist-item (elt (org-heading-components) 4)))
)
)
(org-forward-heading-same-level 1)
(org-todo "TODO")
(org-priority ?A)
(org-deadline nil "<%<%Y-%m-%d %a>>")
(org-set-property "lawlist-drawer" "ACTIVE")
(org-backward-heading-same-level 1)
(org-todo "DONE")
(org-priority ?E)
(org-deadline 'remove)
(org-set-property "lawlist-drawer" "COMPLETED")
;; (setq org-archive-save-context-info nil) ;; Set to nil if user doesn't want archive info.
;; NOTE: User must set the correct path to his / her lawlist.org file.
(setq org-archive-location "/Users/HOME/.0.data/lawlist.org::* ARCHIVES")
(org-archive-subtree)
(goto-char (point-min))
(re-search-forward "^\* ARCHIVES" nil t)
(org-sort-entries t ?a)
;; (org-sort-entries t ?d) additional sorting criteria if deadline is not removed.
(lawlist-org-cleanup)
(goto-char (point-min))
(re-search-forward lawlist-item nil t)
(beginning-of-line))
(add-hook 'org-after-todo-state-change-hook 'lawlist-hook) )
(provide 'init-org-lawlist)
The lawlist.org sample file used for testing the script should look exactly like this:
* TASKS
** TODO [#A] First, figure out this new feature request. :lawlist:
DEADLINE: <2013-07-09 Tue>
:PROPERTIES:
:lawlist-drawer: ACTIVE
:END:
** NEXT [#B] Second, If at first you don't succeed, then try again. :lawlist:
:PROPERTIES:
:lawlist-drawer: PENDING
:END:
** WAITING [#C] Third, try again to figure out this new feature request. :lawlist: :WAITING:
:PROPERTIES:
:lawlist-drawer: DORMANT
:END:
** HOLD [#D] Fourth, try, try again to figure out this new feature request. :lawlist: :WAITING:HOLD:
:PROPERTIES:
:lawlist-drawer: DORMANT
:END:
* ARCHIVES
** DONE [#E] This task is a done deal. :lawlist:
:PROPERTIES:
:lawlist-drawer: COMPLETED
:END:
You're not giving a lot of context here, is this what you want?
(defun org-todo-plus ()
(interactive)
(org-todo)
(org-forward-element)
(org-todo))
It works for this list:
* TODO spam
* eggs
* foo
* bar
With point on spam, just keep M-x org-todo-plus until everything is done.

Org Mode. Sorting in column view

How do we sort in a column view. In specific, I have a list of estimated taks, along with their priority, I want to be able to sort them based on any field, dynamically or atleast by specifying a field as a property.
AFAIK, you can't sort while you are in column view. But you exit column view (by typing "q"). They you can sort the entries by property with M-x org-sort (C-c ^). Type r for property and then enter the property you want to sort by.
E.g., let's say we begin with this:
* Test
** First task
:PROPERTIES:
:Effort: 1:30
:END:
** Second task
:PROPERTIES:
:Effort: 0:30
:END:
Navigate to "* Test" and type C-c ^ (or M-x org-sort). In this case, type "r" and then enter "Effort" to get the resulting order:
* Test
** Second task
:PROPERTIES:
:Effort: 0:30
:END:
** First task
:PROPERTIES:
:Effort: 1:30
:END:
Then enter column view again. (Note, you can also sort by priority --- you'll see the option when you invoke org-sort.)
Additionally, you can export your column view to a table and then sort the table by column:
http://orgmode.org/manual/Capturing-column-view.html
Wanting to sort in column view quite often, I wrote a command which does it by exiting column view, sorting, and then reentering column view:
(defun org-columns-sort (&optional arg)
"Sort entries from column view by value in current column.
This only sorts the children of the column view top level,
which is the entry with the current column view specification.
When ARG is non-nil (interactively with prefix), sort in
reverse order."
(interactive "P")
(let ((colname (get-char-property (point) 'org-columns-key))
(colnum (org-current-text-column)))
(org-columns-goto-top-level)
(org-columns-quit)
(org-sort-entries nil (if arg ?R ?r) nil nil colname)
(org-columns)
(outline-hide-sublevels
(1+ (org-current-level)))
(forward-char colnum)))
(org-defkey org-columns-map "^" #'org-columns-sort)
Or, if you want an alternative keybinding:
(defun org-columns-sort-reverse ()
"Calls `org-columns-sort' with non-nil argument."
(interactive)
(org-columns-sort t))
(org-defkey org-columns-map [(meta down)] #'org-columns-sort)
(org-defkey org-columns-map [(meta up)] #'org-columns-sort-reverse)
Suggestions for improvement are welcome. It doesn't quite restore the original folding and position. I tried storing (point-marker) and then use goto-char but somehow the marker gets lost.
Example: custom sort order
To get a nicer sort order for columns containing numbers or a mix of non-number strings and numbers, replace (org-sort-entries nil (if arg ?R ?r) nil nil colname) above with
(org-sort-entries nil (if arg ?F ?f)
(lambda () (org-entry-get nil colname))
#'my-org-columns-sort-function)
with
(defconst my-string-number-regex
(concat "^[+-]?\\(?:[0-9]+\\(?:[.,][0-9]*\\)?\\(?:e[+-]?[0-9]+\\)?"
"\\|[.,][0-9]+\\(?:e[+-]?[0-9]+\\)?\\)$")
"Matches integers and floats with exponent.
This allows for leading and trailing decimal point, leading zeros in base,
leading zeros in exponent, + signs, and , as alternative decimal separator.")
(defun my-org-columns-sort-function (x y)
"Returns non-nil if X should sort before Y (is less than Y).
This function gives a nice sort order when used with `org-columns-sort'.
Both arguments can be either a cons, a non-number string, or a
number-string. Across types the sort order is as mentioned.
Within types, `string-lessp` is used for non-number strings,
and `<` is used for number-strings."
(cond
((and (consp x)(consp y))
nil)
((and (consp x)(not (consp y)))
t)
((and (not (consp x))(consp y))
nil)
(t
(let ((sxp (not (string-match-p my-string-number-regex x)))
(syp (not (string-match-p my-string-number-regex y))))
(cond
((and sxp syp)
(string-lessp x y))
((and sxp (not syp))
t)
((and (not sxp) syp)
nil)
(t
(< (string-to-number x) (string-to-number y))))))))
See here if you want a less general pattern for matching numbers.

Mixing org-mode and c-mode in emacs

I'd like to mix org-mode and c-mode in Emacs. Everything inside a
comment should be org-mode, the rests should be default major-mode
c-mode:
/*
Org-mode here
** Section 1
text
** Section 2
text
Org-mode should end here
*/
func1()
{
}
I tried using nXhtml multi-major-mode, I guess there are other modes
that support multimodes too. My problem now is that if I type TAB
on "section 2" then all below "Section 2" will be folded and
made invisible. But I would like to contain the region that org-mode
folds/unfolds to the comment section. The TAB should only fold/unfold
till the "*/".
I wonder how I can achieve this?
You can try M-x orgstruct-mode RET.
I found a solution:
http://lists.gnu.org/archive/html/emacs-orgmode/2011-01/msg00036.html
lists a patch for org-mode:
--- emacs-23.2/lisp/org/org.el 2010-04-04 00:26:08.000000000 +0200
+++ src/c51/mk/org.el 2011-01-02 20:26:10.266860827 +0100
## -5245,6 +5245,8 ##
(defun org-cycle-internal-local ()
"Do the local cycling action."
(org-back-to-heading)
+ (cond
+ ((not (looking-at (concat outline-regexp "\s*#" )))
(let ((goal-column 0) eoh eol eos level has-children children-skipped)
;; First, some boundaries
(save-excursion
## -5318,7 +5320,7 ##
(hide-subtree)
(message "FOLDED")
(setq org-cycle-subtree-status 'folded)
- (run-hook-with-args 'org-cycle-hook 'folded)))))
+ (run-hook-with-args 'org-cycle-hook 'folded)))))))
;;;###autoload
(defun org-global-cycle (&optional arg)
--- emacs-23.2/lisp/outline.el 2010-04-04 00:26:04.000000000 +0200
+++ src/c51/mk/outline.el 2011-01-02 20:35:17.303609833 +0100
## -913,8 +913,15 ##
;; Then unhide the top level headers.
(outline-map-region
(lambda ()
- (if (<= (funcall outline-level) levels)
- (outline-show-heading)))
+ (if (<= (funcall outline-level) level)
+ (if (looking-at (concat outline-regexp "\s*#" ))
+ (progn
+ (outline-show-heading )
+ (show-entry ))
+ (outline-show-heading))))
+;; (lambda ()
+;; (if (<= (funcall outline-level) levels)
+;; (outline-show-heading)))
beg end)))
(run-hooks 'outline-view-change-hook))
## -994,7 +1001,11 ##
(outline-map-region
(lambda ()
(if (<= (funcall outline-level) level)
- (outline-show-heading)))
+ (if (looking-at (concat outline-regexp "\s*#" ))
+ (progn
+ (outline-show-heading )
+ (show-entry ))
+ (outline-show-heading))))
(point)
(progn (outline-end-of-subtree)
(if (eobp) (point-max) (1+ (point)))))))
This patch has to be applied by hand, its not that difficult.
It adds the marker *# that will break the indention. #bzg pointed out
the M-x orgstruct-mode RET mode, credits to him. Now I can write in c.mode with orgstruct-mode in the background (no multi-major-mode needed any more):
/*
Org-mode here
** Section 1
text
** Section 2
text
*#
Org-mode should end here
*/
And I will have org-mode in comments, The Section 1 and Section 2 will fold until the *# marker.