org-colview-xemacs.el 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  1. ;;; org-colview-xemacs.el --- Column View in Org-mode, XEmacs-specific version
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.06b
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 3, or (at your option)
  13. ;; any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file contains the face definitons for Org.
  26. ;;; Code:
  27. (eval-when-compile (require 'cl))
  28. (require 'org)
  29. (declare-function org-agenda-redo "org-agenda" ())
  30. ;;; Definie additional faces for column view
  31. (when (featurep 'xemacs)
  32. (defface org-columns-level-1;; font-lock-function-name-face
  33. (org-compatible-face
  34. 'outline-1
  35. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1" :background "grey90"))
  36. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue" :background "grey30"))
  37. (((class color) (min-colors 16) (background light)) (:foreground "Blue" :background "grey90"))
  38. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue" :background "grey30"))
  39. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  40. (t (:bold t))))
  41. "Face used for columns-level 1 headlines."
  42. :group 'org-faces)
  43. (defface org-columns-level-2;; font-lock-variable-name-face
  44. (org-compatible-face
  45. 'outline-2
  46. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod" :background "grey90"))
  47. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod" :background "grey30"))
  48. (((class color) (min-colors 8) (background light)) (:foreground "yellow" :background "grey90"))
  49. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  50. (t (:bold t))))
  51. "Face used for columns-level 2 headlines."
  52. :group 'org-faces)
  53. (defface org-columns-level-3;; font-lock-keyword-face
  54. (org-compatible-face
  55. 'outline-3
  56. '((((class color) (min-colors 88) (background light)) (:foreground "Purple" :background "grey90"))
  57. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1" :background "grey30"))
  58. (((class color) (min-colors 16) (background light)) (:foreground "Purple" :background "grey90"))
  59. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan" :background "grey30"))
  60. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  61. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  62. (t (:bold t))))
  63. "Face used for columns-level 3 headlines."
  64. :group 'org-faces)
  65. (defface org-columns-level-4;; font-lock-comment-face
  66. (org-compatible-face
  67. 'outline-4
  68. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick" :background "grey90"))
  69. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1" :background "grey30"))
  70. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  71. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  72. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  73. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  74. (t (:bold t))))
  75. "Face used for columns-level 4 headlines."
  76. :group 'org-faces)
  77. (defface org-columns-level-5;; font-lock-type-face
  78. (org-compatible-face
  79. 'outline-5
  80. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :background "grey90"))
  81. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :background "grey30"))
  82. (((class color) (min-colors 8)) (:foreground "green"))))
  83. "Face used for columns-level 5 headlines."
  84. :group 'org-faces)
  85. (defface org-columns-level-6;; font-lock-constant-face
  86. (org-compatible-face
  87. 'outline-6
  88. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue" :background "grey90"))
  89. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine" :background "grey30"))
  90. (((class color) (min-colors 8)) (:foreground "magenta"))))
  91. "Face used for columns-level 6 headlines."
  92. :group 'org-faces)
  93. (defface org-columns-level-7;; font-lock-builtin-face
  94. (org-compatible-face
  95. 'outline-7
  96. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid" :background "grey90"))
  97. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue" :background "grey30"))
  98. (((class color) (min-colors 8)) (:foreground "blue"))))
  99. "Face used for columns-level 7 headlines."
  100. :group 'org-faces)
  101. (defface org-columns-level-8;; font-lock-string-face
  102. (org-compatible-face
  103. 'outline-8
  104. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown" :background "grey90"))
  105. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon" :background "grey30"))
  106. (((class color) (min-colors 8)) (:foreground "green"))))
  107. "Face used for columns-level 8 headlines."
  108. :group 'org-faces)
  109. (defface org-columns-space;; font-lock-function-name-face
  110. (org-compatible-face
  111. 'outline-1
  112. '((((class color) (min-colors 88) (background light)) (:background "grey90"))
  113. (((class color) (min-colors 88) (background dark)) (:background "grey30"))
  114. (((class color) (min-colors 16) (background light)) (:background "grey90"))
  115. (((class color) (min-colors 16) (background dark)) (:background "grey30"))
  116. (((class color) (min-colors 8)) (:bold t :underline t))))
  117. "Face used for columns space headlines."
  118. :group 'org-faces)
  119. (defface org-columns-space1;; font-lock-function-name-face
  120. (org-compatible-face
  121. 'outline-1
  122. '((((class color) (min-colors 88) (background light)) (:background "grey90"))
  123. (((class color) (min-colors 88) (background dark)) (:background "grey30"))
  124. (((class color) (min-colors 16) (background light)) (:background "grey90"))
  125. (((class color) (min-colors 16) (background dark)) (:background "grey30"))
  126. (((class color) (min-colors 8)) (:bold t :underline t))))
  127. "Face used for columns space headlines."
  128. :group 'org-faces)
  129. )
  130. (when (featurep 'xemacs)
  131. (defconst org-columns-level-faces
  132. '(org-columns-level-1
  133. org-columns-level-2 org-columns-level-3
  134. org-columns-level-4 org-columns-level-5 org-columns-level-6
  135. org-columns-level-7 org-columns-level-8
  136. ))
  137. (defun org-get-columns-level-face (n)
  138. "Get the right face for match N in font-lock matching of healdines."
  139. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  140. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  141. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-columns-level-faces))
  142. (cond
  143. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  144. ((eq n 2) org-f)
  145. (t (if org-level-color-stars-only nil org-f))))
  146. )
  147. ;;; Column View
  148. (defvar org-columns-overlays nil
  149. "Holds the list of current column overlays.")
  150. (defvar org-columns-current-fmt nil
  151. "Local variable, holds the currently active column format.")
  152. (make-variable-buffer-local 'org-columns-current-fmt)
  153. (defvar org-columns-current-fmt-compiled nil
  154. "Local variable, holds the currently active column format.
  155. This is the compiled version of the format.")
  156. (make-variable-buffer-local 'org-columns-current-fmt-compiled)
  157. (defvar org-columns-current-widths nil
  158. "Loval variable, holds the currently widths of fields.")
  159. (make-variable-buffer-local 'org-columns-current-widths)
  160. (defvar org-columns-current-maxwidths nil
  161. "Loval variable, holds the currently active maximum column widths.")
  162. (make-variable-buffer-local 'org-columns-current-maxwidths)
  163. (defvar org-columns-begin-marker (make-marker)
  164. "Points to the position where last a column creation command was called.")
  165. (defvar org-columns-top-level-marker (make-marker)
  166. "Points to the position where current columns region starts.")
  167. (defvar org-columns-map (make-sparse-keymap)
  168. "The keymap valid in column display.")
  169. (defun org-columns-content ()
  170. "Switch to contents view while in columns view."
  171. (interactive)
  172. (org-overview)
  173. (org-content))
  174. (org-defkey org-columns-map "c" 'org-columns-content)
  175. (org-defkey org-columns-map "o" 'org-overview)
  176. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  177. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  178. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  179. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  180. (org-defkey org-columns-map "v" 'org-columns-show-value)
  181. (org-defkey org-columns-map "q" 'org-columns-quit)
  182. (org-defkey org-columns-map "r" 'org-columns-redo)
  183. (org-defkey org-columns-map "g" 'org-columns-redo)
  184. (org-defkey org-columns-map [left] 'org-columns-backward-char)
  185. (org-defkey org-columns-map "\M-b" 'org-columns-backward-char)
  186. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  187. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  188. (org-defkey org-columns-map "\M-f" 'org-columns-forward-char)
  189. (org-defkey org-columns-map [right] 'org-columns-forward-char)
  190. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  191. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  192. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  193. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  194. (org-defkey org-columns-map "<" 'org-columns-narrow)
  195. (org-defkey org-columns-map ">" 'org-columns-widen)
  196. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  197. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  198. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  199. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  200. (dotimes (i 10)
  201. (org-defkey org-columns-map (number-to-string i)
  202. `(lambda () (interactive)
  203. (org-columns-next-allowed-value nil ,i))))
  204. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  205. '("Column"
  206. ["Edit property" org-columns-edit-value t]
  207. ["Next allowed value" org-columns-next-allowed-value t]
  208. ["Previous allowed value" org-columns-previous-allowed-value t]
  209. ["Show full value" org-columns-show-value t]
  210. ["Edit allowed values" org-columns-edit-allowed t]
  211. "--"
  212. ["Edit column attributes" org-columns-edit-attributes t]
  213. ["Increase column width" org-columns-widen t]
  214. ["Decrease column width" org-columns-narrow t]
  215. "--"
  216. ["Move column right" org-columns-move-right t]
  217. ["Move column left" org-columns-move-left t]
  218. ["Add column" org-columns-new t]
  219. ["Delete column" org-columns-delete t]
  220. "--"
  221. ["CONTENTS" org-columns-content t]
  222. ["OVERVIEW" org-overview t]
  223. ["Refresh columns display" org-columns-redo t]
  224. "--"
  225. ["Open link" org-columns-open-link t]
  226. "--"
  227. ["Quit" org-columns-quit t]))
  228. (defun org-columns-current-column ()
  229. (if (featurep 'xemacs)
  230. (/ (current-column) 2)
  231. (current-column)))
  232. (defun org-columns-forward-char ()
  233. (interactive)
  234. (forward-char)
  235. (if (featurep 'xemacs)
  236. (while (not (or (eolp)
  237. (member (extent-at
  238. (point) (current-buffer)
  239. 'org-columns-key) org-columns-overlays)))
  240. (forward-char))))
  241. (defun org-columns-backward-char ()
  242. (interactive)
  243. (backward-char)
  244. (if (featurep 'xemacs)
  245. (while (not (or (bolp)
  246. (member (extent-at (point) (current-buffer) 'org-columns-key) org-columns-overlays)))
  247. (backward-char))))
  248. (defun org-columns-new-overlay (beg end &optional string face)
  249. "Create a new column overlay and add it to the list."
  250. (let ((ov (org-make-overlay beg end)))
  251. (if (featurep 'xemacs)
  252. (progn
  253. (org-overlay-put ov 'face (or face 'org-columns-space1))
  254. (org-overlay-put ov 'start-open t)
  255. (if string
  256. (org-overlay-display ov string (or face 'org-columns-space1))))
  257. (org-overlay-put ov 'face (or face 'secondary-selection))
  258. (org-overlay-display ov string face))
  259. (push ov org-columns-overlays)
  260. ov))
  261. (defun org-columns-display-here (&optional props)
  262. "Overlay the current line with column display."
  263. (interactive)
  264. (let* ((fmt org-columns-current-fmt-compiled)
  265. (beg (point-at-bol))
  266. (level-face (save-excursion
  267. (beginning-of-line 1)
  268. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  269. (org-get-level-face 2))))
  270. (item (save-match-data
  271. (org-no-properties
  272. (org-remove-tabs
  273. (buffer-substring-no-properties
  274. (point-at-bol) (point-at-eol))))))
  275. (color (if (featurep 'xemacs)
  276. (save-excursion
  277. (beginning-of-line 1)
  278. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  279. (org-get-columns-level-face 2)))
  280. (list :foreground
  281. (face-attribute
  282. (or level-face
  283. (and (eq major-mode 'org-agenda-mode)
  284. (get-text-property (point-at-bol) 'face))
  285. 'default) :foreground))))
  286. (face (if (featurep 'xemacs) color (list color 'org-column)))
  287. (pl (or (get-text-property (point-at-bol) 'prefix-length) 0))
  288. (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
  289. pom property ass width f string ov column val modval s1 s2)
  290. ;; Check if the entry is in another buffer.
  291. (unless props
  292. (if (eq major-mode 'org-agenda-mode)
  293. (setq pom (or (get-text-property (point) 'org-hd-marker)
  294. (get-text-property (point) 'org-marker))
  295. props (if pom (org-entry-properties pom) nil))
  296. (setq props (org-entry-properties nil))))
  297. ;; Walk the format
  298. (while (setq column (pop fmt))
  299. (setq property (car column)
  300. ass (if (equal property "ITEM")
  301. (cons "ITEM" item)
  302. (assoc property props))
  303. width (or (cdr (assoc property org-columns-current-maxwidths))
  304. (nth 2 column)
  305. (length property))
  306. f (format (if (featurep 'xemacs) "%%-%d.%ds |" "%%-%d.%ds | ")
  307. width width)
  308. val (or (cdr ass) "")
  309. modval (if (equal property "ITEM")
  310. (if (org-mode-p)
  311. (org-columns-cleanup-item
  312. val org-columns-current-fmt-compiled)
  313. (org-agenda-columns-cleanup-item
  314. val pl cphr org-columns-current-fmt-compiled))))
  315. (setq s2 (org-columns-add-ellipses (or modval val) width))
  316. (setq string (format f s2))
  317. ;; Create the overlay
  318. (org-unmodified
  319. (setq ov (org-columns-new-overlay
  320. beg (setq beg (1+ beg)) string face))
  321. (org-overlay-put ov 'keymap org-columns-map)
  322. (org-overlay-put ov 'org-columns-key property)
  323. (org-overlay-put ov 'org-columns-value (cdr ass))
  324. (org-overlay-put ov 'org-columns-value-modified modval)
  325. (org-overlay-put ov 'org-columns-pom pom)
  326. (org-overlay-put ov 'org-columns-format f)
  327. (when (featurep 'xemacs)
  328. (if (or (not (char-after beg))
  329. (equal (char-after beg) ?\n))
  330. (let ((inhibit-read-only t))
  331. (save-excursion
  332. (goto-char beg)
  333. (org-unmodified (insert " "))
  334. ;; FIXME: add props and remove later?
  335. )))
  336. (goto-char beg)
  337. (org-columns-new-overlay
  338. beg (1+ beg) nil 'org-columns-space)
  339. (setq beg (1+ beg))))
  340. (if (or (not (char-after beg))
  341. (equal (char-after beg) ?\n))
  342. (let ((inhibit-read-only t))
  343. (save-excursion
  344. (goto-char beg)
  345. ;; FIXME: add props and remove later?
  346. (org-unmodified (insert " "))))))
  347. ;; Make the rest of the line disappear.
  348. (org-unmodified
  349. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  350. (org-overlay-put ov 'invisible t)
  351. (org-overlay-put ov 'keymap org-columns-map)
  352. (org-overlay-put ov 'intangible t)
  353. (push ov org-columns-overlays)
  354. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  355. (org-overlay-put ov 'keymap org-columns-map)
  356. (push ov org-columns-overlays)
  357. (let ((inhibit-read-only t))
  358. (put-text-property (max (point-min) (1- (point-at-bol)))
  359. (min (point-max) (1+ (point-at-eol)))
  360. 'read-only "Type `e' to edit property")))))
  361. (defun org-columns-add-ellipses (string width)
  362. "Truncate STRING with WIDTH characters, with ellipses."
  363. (cond
  364. ((<= (length string) width) string)
  365. ((<= width (length org-columns-ellipses))
  366. (substring org-columns-ellipses 0 width))
  367. (t (concat (substring string 0 (- width (length org-columns-ellipses)))
  368. org-columns-ellipses))))
  369. (defvar org-columns-full-header-line-format nil
  370. "Fthe full header line format, will be shifted by horizontal scrolling." )
  371. (defvar org-previous-header-line-format nil
  372. "The header line format before column view was turned on.")
  373. (defvar org-columns-inhibit-recalculation nil
  374. "Inhibit recomputing of columns on column view startup.")
  375. (defvar header-line-format)
  376. (defvar org-columns-previous-hscroll 0)
  377. (defun org-columns-display-here-title ()
  378. "Overlay the newline before the current line with the table title."
  379. (interactive)
  380. (let ((fmt org-columns-current-fmt-compiled)
  381. string (title "")
  382. property width f column str widths)
  383. (while (setq column (pop fmt))
  384. (setq property (car column)
  385. str (or (nth 1 column) property)
  386. width (or (cdr (assoc property org-columns-current-maxwidths))
  387. (nth 2 column)
  388. (length str))
  389. widths (push width widths)
  390. f (format "%%-%d.%ds | " width width)
  391. string (format f str)
  392. title (concat title string)))
  393. (if (featurep 'xemacs)
  394. (let ((ext (make-extent nil nil)))
  395. (set-extent-endpoints ext 0 (length title) title)
  396. (set-extent-face ext (list 'bold 'underline 'org-columns-space1))
  397. (org-set-local 'org-previous-header-line-format
  398. (specifier-specs top-gutter))
  399. (org-set-local 'org-columns-current-widths (nreverse widths))
  400. (set-specifier top-gutter (make-gutter-specifier
  401. (cons (current-buffer) title))))
  402. (setq title (concat
  403. (org-add-props " " nil 'display '(space :align-to 0))
  404. (org-add-props title nil 'face '(:weight bold :underline t))))
  405. (org-set-local 'org-previous-header-line-format header-line-format)
  406. (org-set-local 'org-columns-current-widths (nreverse widths))
  407. (setq org-columns-full-header-line-format title)
  408. (setq org-columns-previous-hscroll -1)
  409. (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local))))
  410. (defun org-columns-hscoll-title ()
  411. "Set the header-line-format so that it scrolls along with the table."
  412. (sit-for .0001) ; need to force a redisplay to update window-hscroll
  413. (when (not (= (window-hscroll) org-columns-previous-hscroll))
  414. (setq header-line-format
  415. (concat (substring org-columns-full-header-line-format 0 1)
  416. (substring org-columns-full-header-line-format
  417. (1+ (window-hscroll))))
  418. org-columns-previous-hscroll (window-hscroll))
  419. (force-mode-line-update)))
  420. (defun org-columns-remove-overlays ()
  421. "Remove all currently active column overlays."
  422. (interactive)
  423. (when (marker-buffer org-columns-begin-marker)
  424. (with-current-buffer (marker-buffer org-columns-begin-marker)
  425. (when (local-variable-p 'org-previous-header-line-format (current-buffer))
  426. (if (featurep 'xemacs)
  427. (set-specifier top-gutter
  428. (make-gutter-specifier
  429. (cons (current-buffer)
  430. (cdar org-previous-header-line-format))))
  431. (setq header-line-format org-previous-header-line-format)
  432. (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
  433. (kill-local-variable 'org-previous-header-line-format))
  434. (move-marker org-columns-begin-marker nil)
  435. (move-marker org-columns-top-level-marker nil)
  436. (org-unmodified
  437. (mapc 'org-delete-overlay org-columns-overlays)
  438. (setq org-columns-overlays nil)
  439. (let ((inhibit-read-only t))
  440. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  441. (defun org-columns-cleanup-item (item fmt)
  442. "Remove from ITEM what is a column in the format FMT."
  443. (if (not org-complex-heading-regexp)
  444. item
  445. (when (string-match org-complex-heading-regexp item)
  446. (setq item
  447. (concat
  448. (org-add-props (match-string 1 item) nil
  449. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  450. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  451. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  452. " " (save-match-data (org-columns-compact-links (match-string 4 item)))
  453. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
  454. (add-text-properties
  455. 0 (1+ (match-end 1))
  456. (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  457. item)
  458. item)))
  459. (defun org-columns-compact-links (s)
  460. "Replace [[link][desc]] with [desc] or [link]."
  461. (while (string-match org-bracket-link-regexp s)
  462. (setq s (replace-match
  463. (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
  464. t t s)))
  465. s)
  466. (defvar org-agenda-columns-remove-prefix-from-item)
  467. (defun org-agenda-columns-cleanup-item (item pl cphr fmt)
  468. "Cleanup the tiem property for agenda column view.
  469. See also the variable `org-agenda-columns-remove-prefix-from-item'."
  470. (let* ((org-complex-heading-regexp cphr)
  471. (prefix (substring item 0 pl))
  472. (rest (substring item pl))
  473. (fake (concat "* " rest))
  474. (cleaned (org-trim (substring (org-columns-cleanup-item fake fmt) 1))))
  475. (if org-agenda-columns-remove-prefix-from-item
  476. cleaned
  477. (concat prefix cleaned))))
  478. (defun org-columns-show-value ()
  479. "Show the full value of the property."
  480. (interactive)
  481. (let ((value (get-char-property (point) 'org-columns-value)))
  482. (message "Value is: %s" (or value ""))))
  483. (defvar org-agenda-columns-active) ;; defined in org-agenda.el
  484. (defun org-columns-quit ()
  485. "Remove the column overlays and in this way exit column editing."
  486. (interactive)
  487. (org-unmodified
  488. (org-columns-remove-overlays)
  489. (let ((inhibit-read-only t))
  490. (remove-text-properties (point-min) (point-max) '(read-only t))))
  491. (when (eq major-mode 'org-agenda-mode)
  492. (setq org-agenda-columns-active nil)
  493. (message
  494. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  495. (defun org-columns-check-computed ()
  496. "Check if this column value is computed.
  497. If yes, throw an error indicating that changing it does not make sense."
  498. (let ((val (get-char-property (point) 'org-columns-value)))
  499. (when (and (stringp val)
  500. (get-char-property 0 'org-computed val))
  501. (error "This value is computed from the entry's children"))))
  502. (defun org-columns-todo (&optional arg)
  503. "Change the TODO state during column view."
  504. (interactive "P")
  505. (org-columns-edit-value "TODO"))
  506. (defun org-columns-set-tags-or-toggle (&optional arg)
  507. "Toggle checkbox at point, or set tags for current headline."
  508. (interactive "P")
  509. (if (string-match "\\`\\[[ xX-]\\]\\'"
  510. (get-char-property (point) 'org-columns-value))
  511. (org-columns-next-allowed-value)
  512. (org-columns-edit-value "TAGS")))
  513. (defun org-columns-edit-value (&optional key)
  514. "Edit the value of the property at point in column view.
  515. Where possible, use the standard interface for changing this line."
  516. (interactive)
  517. (org-columns-check-computed)
  518. (let* ((external-key key)
  519. (col (current-column))
  520. (key (or key (get-char-property (point) 'org-columns-key)))
  521. (value (get-char-property (point) 'org-columns-value))
  522. (bol (point-at-bol)) (eol (point-at-eol))
  523. (pom (or (get-text-property bol 'org-hd-marker)
  524. (point))) ; keep despite of compiler warning
  525. (line-overlays
  526. (delq nil (mapcar (lambda (x)
  527. (and (eq (org-overlay-buffer x) (current-buffer))
  528. (>= (org-overlay-start x) bol)
  529. (<= (org-overlay-start x) eol)
  530. x))
  531. org-columns-overlays)))
  532. nval eval allowed)
  533. (cond
  534. ((equal key "CLOCKSUM")
  535. (error "This special column cannot be edited"))
  536. ((equal key "ITEM")
  537. (setq eval '(org-with-point-at pom (org-edit-headline))))
  538. ((equal key "TODO")
  539. (setq eval '(org-with-point-at
  540. pom
  541. (let ((current-prefix-arg
  542. (if external-key current-prefix-arg '(4))))
  543. (call-interactively 'org-todo)))))
  544. ((equal key "PRIORITY")
  545. (setq eval '(org-with-point-at pom
  546. (call-interactively 'org-priority))))
  547. ((equal key "TAGS")
  548. (setq eval '(org-with-point-at
  549. pom
  550. (let ((org-fast-tag-selection-single-key
  551. (if (eq org-fast-tag-selection-single-key 'expert)
  552. t org-fast-tag-selection-single-key)))
  553. (call-interactively 'org-set-tags)))))
  554. ((equal key "DEADLINE")
  555. (setq eval '(org-with-point-at
  556. pom
  557. (call-interactively 'org-deadline))))
  558. ((equal key "SCHEDULED")
  559. (setq eval '(org-with-point-at
  560. pom
  561. (call-interactively 'org-schedule))))
  562. (t
  563. (setq allowed (org-property-get-allowed-values pom key 'table))
  564. (if allowed
  565. (setq nval (completing-read "Value: " allowed nil t))
  566. (setq nval (read-string "Edit: " value)))
  567. (setq nval (org-trim nval))
  568. (when (not (equal nval value))
  569. (setq eval '(org-entry-put pom key nval)))))
  570. (when eval
  571. (cond
  572. ((equal major-mode 'org-agenda-mode)
  573. (org-columns-eval eval)
  574. ;; The following let preserves the current format, and makes sure
  575. ;; that in only a single file things need to be upated.
  576. (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
  577. (buffer (marker-buffer pom))
  578. (org-agenda-contributing-files
  579. (list (with-current-buffer buffer
  580. (buffer-file-name (buffer-base-buffer))))))
  581. (org-agenda-columns)))
  582. (t
  583. (let ((inhibit-read-only t))
  584. (org-unmodified
  585. (remove-text-properties
  586. (max (point-min) (1- bol)) eol '(read-only t)))
  587. (unwind-protect
  588. (progn
  589. (setq org-columns-overlays
  590. (org-delete-all line-overlays org-columns-overlays))
  591. (mapc 'org-delete-overlay line-overlays)
  592. (org-columns-eval eval))
  593. (org-columns-display-here)))
  594. (org-move-to-column col)
  595. (if (and (org-mode-p)
  596. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  597. (org-columns-update key)))))))
  598. (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
  599. "Edit the current headline, the part without TODO keyword, TAGS."
  600. (org-back-to-heading)
  601. (when (looking-at org-todo-line-regexp)
  602. (let ((pos (point))
  603. (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  604. (txt (match-string 3))
  605. (post "")
  606. txt2)
  607. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  608. (setq post (match-string 0 txt)
  609. txt (substring txt 0 (match-beginning 0))))
  610. (setq txt2 (read-string "Edit: " txt))
  611. (when (not (equal txt txt2))
  612. (goto-char pos)
  613. (insert pre txt2 post)
  614. (delete-region (point) (point-at-eol))
  615. (org-set-tags nil t)))))
  616. (defun org-columns-edit-allowed ()
  617. "Edit the list of allowed values for the current property."
  618. (interactive)
  619. (let* ((pom (or (get-text-property (point-at-bol) 'org-marker)
  620. (get-text-property (point-at-bol) 'org-hd-marker)
  621. (point)))
  622. (key (get-char-property (point) 'org-columns-key))
  623. (key1 (concat key "_ALL"))
  624. (allowed (org-entry-get pom key1 t))
  625. nval)
  626. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  627. ;; FIXME: Write back to #+PROPERTY setting if that is needed.
  628. (setq nval (read-string "Allowed: " allowed))
  629. (org-entry-put
  630. (cond ((marker-position org-entry-property-inherited-from)
  631. org-entry-property-inherited-from)
  632. ((marker-position org-columns-top-level-marker)
  633. org-columns-top-level-marker)
  634. (t pom))
  635. key1 nval)))
  636. (defun org-columns-eval (form)
  637. (let (hidep)
  638. (save-excursion
  639. (beginning-of-line 1)
  640. ;; `next-line' is needed here, because it skips invisible line.
  641. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  642. (setq hidep (org-on-heading-p 1)))
  643. (eval form)
  644. (and hidep (hide-entry))))
  645. (defun org-columns-previous-allowed-value ()
  646. "Switch to the previous allowed value for this column."
  647. (interactive)
  648. (org-columns-next-allowed-value t))
  649. (defun org-columns-next-allowed-value (&optional previous nth)
  650. "Switch to the next allowed value for this column.
  651. When PREVIOUS is set, go to the previous value. When NTH is
  652. an integer, select that value."
  653. (interactive)
  654. (org-columns-check-computed)
  655. (let* ((col (current-column))
  656. (key (get-char-property (point) 'org-columns-key))
  657. (value (get-char-property (point) 'org-columns-value))
  658. (bol (point-at-bol)) (eol (point-at-eol))
  659. (pom (or (get-text-property bol 'org-hd-marker)
  660. (point))) ; keep despite of compiler waring
  661. (line-overlays
  662. (delq nil (mapcar (lambda (x)
  663. (and (eq (org-overlay-buffer x) (current-buffer))
  664. (>= (org-overlay-start x) bol)
  665. (<= (org-overlay-start x) eol)
  666. x))
  667. org-columns-overlays)))
  668. (allowed (or (org-property-get-allowed-values pom key)
  669. (and (memq
  670. (nth 4 (assoc key org-columns-current-fmt-compiled))
  671. '(checkbox checkbox-n-of-m checkbox-percent))
  672. '("[ ]" "[X]"))))
  673. nval)
  674. (when (integerp nth)
  675. (setq nth (1- nth))
  676. (if (= nth -1) (setq nth 9)))
  677. (when (equal key "ITEM")
  678. (error "Cannot edit item headline from here"))
  679. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  680. (error "Allowed values for this property have not been defined"))
  681. (if (member key '("SCHEDULED" "DEADLINE"))
  682. (setq nval (if previous 'earlier 'later))
  683. (if previous (setq allowed (reverse allowed)))
  684. (cond
  685. (nth
  686. (setq nval (nth nth allowed))
  687. (if (not nval)
  688. (error "There are only %d allowed values for property `%s'"
  689. (length allowed) key)))
  690. ((member value allowed)
  691. (setq nval (or (car (cdr (member value allowed)))
  692. (car allowed)))
  693. (if (equal nval value)
  694. (error "Only one allowed value for this property")))
  695. (t (setq nval (car allowed)))))
  696. (cond
  697. ((equal major-mode 'org-agenda-mode)
  698. (org-columns-eval '(org-entry-put pom key nval))
  699. ;; The following let preserves the current format, and makes sure
  700. ;; that in only a single file things need to be upated.
  701. (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
  702. (buffer (marker-buffer pom))
  703. (org-agenda-contributing-files
  704. (list (with-current-buffer buffer
  705. (buffer-file-name (buffer-base-buffer))))))
  706. (org-agenda-columns)))
  707. (t
  708. (let ((inhibit-read-only t))
  709. (remove-text-properties (1- bol) eol '(read-only t))
  710. (unwind-protect
  711. (progn
  712. (setq org-columns-overlays
  713. (org-delete-all line-overlays org-columns-overlays))
  714. (mapc 'org-delete-overlay line-overlays)
  715. (org-columns-eval '(org-entry-put pom key nval)))
  716. (org-columns-display-here)))
  717. (org-move-to-column col)
  718. (and (nth 3 (assoc key org-columns-current-fmt-compiled))
  719. (org-columns-update key))))))
  720. (defun org-verify-version (task)
  721. (cond
  722. ((eq task 'columns)
  723. (if (or (and (featurep 'xemacs) (not (featurep 'org-colview-xemacs)))
  724. (and (not (featurep 'xemacs)) (< emacs-major-version 22)))
  725. (error "This version of Emacs cannot run Column View")))))
  726. (defun org-columns-open-link (&optional arg)
  727. (interactive "P")
  728. (let ((value (get-char-property (point) 'org-columns-value)))
  729. (org-open-link-from-string value arg)))
  730. (defun org-columns-get-format-and-top-level ()
  731. (let (fmt)
  732. (when (condition-case nil (org-back-to-heading) (error nil))
  733. (move-marker org-entry-property-inherited-from nil)
  734. (setq fmt (org-entry-get nil "COLUMNS" t)))
  735. (setq fmt (or fmt org-columns-default-format))
  736. (org-set-local 'org-columns-current-fmt fmt)
  737. (org-columns-compile-format fmt)
  738. (if (marker-position org-entry-property-inherited-from)
  739. (move-marker org-columns-top-level-marker
  740. org-entry-property-inherited-from)
  741. (move-marker org-columns-top-level-marker (point)))
  742. fmt))
  743. (defun org-columns ()
  744. "Turn on column view on an org-mode file."
  745. (interactive)
  746. (org-verify-version 'columns)
  747. (when (featurep 'xemacs)
  748. (set-face-foreground 'org-columns-space
  749. (face-background 'org-columns-space)))
  750. (org-columns-remove-overlays)
  751. (move-marker org-columns-begin-marker (point))
  752. (let (beg end fmt cache maxwidths)
  753. (setq fmt (org-columns-get-format-and-top-level))
  754. (save-excursion
  755. (goto-char org-columns-top-level-marker)
  756. (setq beg (point))
  757. (unless org-columns-inhibit-recalculation
  758. (org-columns-compute-all))
  759. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  760. (point-max)))
  761. ;; Get and cache the properties
  762. (goto-char beg)
  763. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  764. (save-excursion
  765. (save-restriction
  766. (narrow-to-region beg end)
  767. (org-clock-sum))))
  768. (while (re-search-forward (concat "^" outline-regexp) end t)
  769. (push (cons (org-current-line) (org-entry-properties)) cache))
  770. (when cache
  771. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  772. (org-set-local 'org-columns-current-maxwidths maxwidths)
  773. (org-columns-display-here-title)
  774. (mapc (lambda (x)
  775. (goto-line (car x))
  776. (org-columns-display-here (cdr x)))
  777. cache)))))
  778. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  779. "Insert a new column, to the left of the current column."
  780. (interactive)
  781. (let ((n (org-columns-current-column))
  782. (editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  783. cell)
  784. (setq prop (completing-read
  785. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  786. nil nil prop))
  787. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  788. (setq width (read-string "Column width: " (if width (number-to-string width))))
  789. (if (string-match "\\S-" width)
  790. (setq width (string-to-number width))
  791. (setq width nil))
  792. (setq fmt (completing-read "Summary [none]: "
  793. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  794. nil t))
  795. (if (string-match "\\S-" fmt)
  796. (setq fmt (intern fmt))
  797. (setq fmt nil))
  798. (if (eq fmt 'none) (setq fmt nil))
  799. (if editp
  800. (progn
  801. (setcar editp prop)
  802. (setcdr editp (list title width nil fmt)))
  803. (setq cell (nthcdr (1- n) org-columns-current-fmt-compiled))
  804. (setcdr cell (cons (list prop title width nil fmt)
  805. (cdr cell))))
  806. (org-columns-store-format)
  807. (org-columns-redo)))
  808. (defun org-columns-delete ()
  809. "Delete the column at point from columns view."
  810. (interactive)
  811. (let* ((n (org-columns-current-column))
  812. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  813. (when (y-or-n-p
  814. (format "Are you sure you want to remove column \"%s\"? " title))
  815. (setq org-columns-current-fmt-compiled
  816. (delq (nth n org-columns-current-fmt-compiled)
  817. org-columns-current-fmt-compiled))
  818. (org-columns-store-format)
  819. (org-columns-redo)
  820. (if (>= (org-columns-current-column)
  821. (length org-columns-current-fmt-compiled))
  822. (org-columns-backward-char)))))
  823. (defun org-columns-edit-attributes ()
  824. "Edit the attributes of the current column."
  825. (interactive)
  826. (let* ((n (org-columns-current-column))
  827. (info (nth n org-columns-current-fmt-compiled)))
  828. (apply 'org-columns-new info)))
  829. (defun org-columns-widen (arg)
  830. "Make the column wider by ARG characters."
  831. (interactive "p")
  832. (let* ((n (org-columns-current-column))
  833. (entry (nth n org-columns-current-fmt-compiled))
  834. (width (or (nth 2 entry)
  835. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  836. (setq width (max 1 (+ width arg)))
  837. (setcar (nthcdr 2 entry) width)
  838. (org-columns-store-format)
  839. (org-columns-redo)))
  840. (defun org-columns-narrow (arg)
  841. "Make the column nrrower by ARG characters."
  842. (interactive "p")
  843. (org-columns-widen (- arg)))
  844. (defun org-columns-move-right ()
  845. "Swap this column with the one to the right."
  846. (interactive)
  847. (let* ((n (org-columns-current-column))
  848. (cell (nthcdr n org-columns-current-fmt-compiled))
  849. e)
  850. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  851. (error "Cannot shift this column further to the right"))
  852. (setq e (car cell))
  853. (setcar cell (car (cdr cell)))
  854. (setcdr cell (cons e (cdr (cdr cell))))
  855. (org-columns-store-format)
  856. (org-columns-redo)
  857. (org-columns-forward-char)))
  858. (defun org-columns-move-left ()
  859. "Swap this column with the one to the left."
  860. (interactive)
  861. (let* ((n (org-columns-current-column)))
  862. (when (= n 0)
  863. (error "Cannot shift this column further to the left"))
  864. (org-columns-backward-char)
  865. (org-columns-move-right)
  866. (org-columns-backward-char)))
  867. (defun org-columns-store-format ()
  868. "Store the text version of the current columns format in appropriate place.
  869. This is either in the COLUMNS property of the node starting the current column
  870. display, or in the #+COLUMNS line of the current buffer."
  871. (let (fmt (cnt 0))
  872. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  873. (org-set-local 'org-columns-current-fmt fmt)
  874. (if (marker-position org-columns-top-level-marker)
  875. (save-excursion
  876. (goto-char org-columns-top-level-marker)
  877. (if (and (org-at-heading-p)
  878. (org-entry-get nil "COLUMNS"))
  879. (org-entry-put nil "COLUMNS" fmt)
  880. (goto-char (point-min))
  881. ;; Overwrite all #+COLUMNS lines....
  882. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  883. (setq cnt (1+ cnt))
  884. (replace-match (concat "#+COLUMNS: " fmt) t t))
  885. (unless (> cnt 0)
  886. (goto-char (point-min))
  887. (or (org-on-heading-p t) (outline-next-heading))
  888. (let ((inhibit-read-only t))
  889. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  890. (org-set-local 'org-columns-default-format fmt))))))
  891. (defvar org-agenda-overriding-columns-format nil
  892. "When set, overrides any other format definition for the agenda.
  893. Don't set this, this is meant for dynamic scoping.")
  894. (defun org-columns-get-autowidth-alist (s cache)
  895. "Derive the maximum column widths from the format and the cache."
  896. (let ((start 0) rtn)
  897. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  898. (push (cons (match-string 1 s) 1) rtn)
  899. (setq start (match-end 0)))
  900. (mapc (lambda (x)
  901. (setcdr x (apply 'max
  902. (mapcar
  903. (lambda (y)
  904. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  905. cache))))
  906. rtn)
  907. rtn))
  908. (defun org-columns-compute-all ()
  909. "Compute all columns that have operators defined."
  910. (org-unmodified
  911. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  912. (let ((columns org-columns-current-fmt-compiled) col)
  913. (while (setq col (pop columns))
  914. (when (nth 3 col)
  915. (save-excursion
  916. (org-columns-compute (car col)))))))
  917. (defun org-columns-update (property)
  918. "Recompute PROPERTY, and update the columns display for it."
  919. (org-columns-compute property)
  920. (let (fmt val pos face)
  921. (save-excursion
  922. (mapc (lambda (ov)
  923. (when (equal (org-overlay-get ov 'org-columns-key) property)
  924. (setq pos (org-overlay-start ov))
  925. (goto-char pos)
  926. (when (setq val (cdr (assoc property
  927. (get-text-property
  928. (point-at-bol) 'org-summaries))))
  929. (setq fmt (org-overlay-get ov 'org-columns-format))
  930. (org-overlay-put ov 'org-columns-value val)
  931. (if (featurep 'xemacs)
  932. (progn
  933. (setq face (glyph-face (extent-end-glyph ov)))
  934. (org-overlay-display ov (format fmt val) face))
  935. (org-overlay-display ov (format fmt val))))))
  936. org-columns-overlays))))
  937. (defun org-columns-compute (property)
  938. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  939. (interactive)
  940. (let* ((re (concat "^" outline-regexp))
  941. (lmax 30) ; Does anyone use deeper levels???
  942. (lsum (make-vector lmax 0))
  943. (lflag (make-vector lmax nil))
  944. (level 0)
  945. (ass (assoc property org-columns-current-fmt-compiled))
  946. (format (nth 4 ass))
  947. (printf (nth 5 ass))
  948. (beg org-columns-top-level-marker)
  949. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  950. (save-excursion
  951. ;; Find the region to compute
  952. (goto-char beg)
  953. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  954. (goto-char end)
  955. ;; Walk the tree from the back and do the computations
  956. (while (re-search-backward re beg t)
  957. (setq sumpos (match-beginning 0)
  958. last-level level
  959. level (org-outline-level)
  960. val (org-entry-get nil property)
  961. valflag (and val (string-match "\\S-" val)))
  962. (cond
  963. ((< level last-level)
  964. ;; put the sum of lower levels here as a property
  965. (setq sum (aref lsum last-level) ; current sum
  966. flag (aref lflag last-level) ; any valid entries from children?
  967. str (org-columns-number-to-string sum format printf)
  968. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  969. useval (if flag str1 (if valflag val ""))
  970. sum-alist (get-text-property sumpos 'org-summaries))
  971. (if (assoc property sum-alist)
  972. (setcdr (assoc property sum-alist) useval)
  973. (push (cons property useval) sum-alist)
  974. (org-unmodified
  975. (add-text-properties sumpos (1+ sumpos)
  976. (list 'org-summaries sum-alist))))
  977. (when (and val (not (equal val (if flag str val))))
  978. (org-entry-put nil property (if flag str val)))
  979. ;; add current to current level accumulator
  980. (when (or flag valflag)
  981. (aset lsum level (+ (aref lsum level)
  982. (if flag sum (org-column-string-to-number
  983. (if flag str val) format))))
  984. (aset lflag level t))
  985. ;; clear accumulators for deeper levels
  986. (loop for l from (1+ level) to (1- lmax) do
  987. (aset lsum l 0)
  988. (aset lflag l nil)))
  989. ((>= level last-level)
  990. ;; add what we have here to the accumulator for this level
  991. (aset lsum level (+ (aref lsum level)
  992. (org-column-string-to-number (or val "0") format)))
  993. (and valflag (aset lflag level t)))
  994. (t (error "This should not happen")))))))
  995. (defun org-columns-redo ()
  996. "Construct the column display again."
  997. (interactive)
  998. (message "Recomputing columns...")
  999. (save-excursion
  1000. (if (marker-position org-columns-begin-marker)
  1001. (goto-char org-columns-begin-marker))
  1002. (org-columns-remove-overlays)
  1003. (if (org-mode-p)
  1004. (call-interactively 'org-columns)
  1005. (org-agenda-redo)
  1006. (call-interactively 'org-agenda-columns)))
  1007. (when (featurep 'xemacs)
  1008. (while (not (or (eolp)
  1009. (member (extent-at (point)) org-columns-overlays)))
  1010. (forward-char)))
  1011. (message "Recomputing columns...done"))
  1012. (defun org-columns-not-in-agenda ()
  1013. (if (eq major-mode 'org-agenda-mode)
  1014. (error "This command is only allowed in Org-mode buffers")))
  1015. (defun org-string-to-number (s)
  1016. "Convert string to number, and interpret hh:mm:ss."
  1017. (if (not (string-match ":" s))
  1018. (string-to-number s)
  1019. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  1020. (while l
  1021. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  1022. sum)))
  1023. (defun org-columns-number-to-string (n fmt &optional printf)
  1024. "Convert a computed column number to a string value, according to FMT."
  1025. (cond
  1026. ((eq fmt 'add_times)
  1027. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  1028. (format org-time-clocksum-format h m)))
  1029. ((eq fmt 'checkbox)
  1030. (cond ((= n (floor n)) "[X]")
  1031. ((> n 1.) "[-]")
  1032. (t "[ ]")))
  1033. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  1034. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  1035. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  1036. (printf (format printf n))
  1037. ((eq fmt 'currency)
  1038. (format "%.2f" n))
  1039. (t (number-to-string n))))
  1040. (defun org-nofm-to-completion (n m &optional percent)
  1041. (if (not percent)
  1042. (format "[%d/%d]" n m)
  1043. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  1044. (defun org-column-string-to-number (s fmt)
  1045. "Convert a column value to a number that can be used for column computing."
  1046. (cond
  1047. ((string-match ":" s)
  1048. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  1049. (while l
  1050. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  1051. sum))
  1052. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  1053. (if (equal s "[X]") 1. 0.000001))
  1054. (t (string-to-number s))))
  1055. (defun org-columns-uncompile-format (cfmt)
  1056. "Turn the compiled columns format back into a string representation."
  1057. (let ((rtn "") e s prop title op width fmt printf)
  1058. (while (setq e (pop cfmt))
  1059. (setq prop (car e)
  1060. title (nth 1 e)
  1061. width (nth 2 e)
  1062. op (nth 3 e)
  1063. fmt (nth 4 e)
  1064. printf (nth 5 e))
  1065. (cond
  1066. ((eq fmt 'add_times) (setq op ":"))
  1067. ((eq fmt 'checkbox) (setq op "X"))
  1068. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  1069. ((eq fmt 'checkbox-percent) (setq op "X%"))
  1070. ((eq fmt 'add_numbers) (setq op "+"))
  1071. ((eq fmt 'currency) (setq op "$")))
  1072. (if (and op printf) (setq op (concat op ";" printf)))
  1073. (if (equal title prop) (setq title nil))
  1074. (setq s (concat "%" (if width (number-to-string width))
  1075. prop
  1076. (if title (concat "(" title ")"))
  1077. (if op (concat "{" op "}"))))
  1078. (setq rtn (concat rtn " " s)))
  1079. (org-trim rtn)))
  1080. (defun org-columns-compile-format (fmt)
  1081. "Turn a column format string into an alist of specifications.
  1082. The alist has one entry for each column in the format. The elements of
  1083. that list are:
  1084. property the property
  1085. title the title field for the columns
  1086. width the column width in characters, can be nil for automatic
  1087. operator the operator if any
  1088. format the output format for computed results, derived from operator
  1089. printf a printf format for computed values"
  1090. (let ((start 0) width prop title op f printf)
  1091. (setq org-columns-current-fmt-compiled nil)
  1092. (while (string-match
  1093. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  1094. fmt start)
  1095. (setq start (match-end 0)
  1096. width (match-string 1 fmt)
  1097. prop (match-string 2 fmt)
  1098. title (or (match-string 3 fmt) prop)
  1099. op (match-string 4 fmt)
  1100. f nil
  1101. printf nil)
  1102. (if width (setq width (string-to-number width)))
  1103. (when (and op (string-match ";" op))
  1104. (setq printf (substring op (match-end 0))
  1105. op (substring op 0 (match-beginning 0))))
  1106. (cond
  1107. ((equal op "+") (setq f 'add_numbers))
  1108. ((equal op "$") (setq f 'currency))
  1109. ((equal op ":") (setq f 'add_times))
  1110. ((equal op "X") (setq f 'checkbox))
  1111. ((equal op "X/") (setq f 'checkbox-n-of-m))
  1112. ((equal op "X%") (setq f 'checkbox-percent))
  1113. )
  1114. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  1115. (setq org-columns-current-fmt-compiled
  1116. (nreverse org-columns-current-fmt-compiled))))
  1117. ;;; Dynamic block for Column view
  1118. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  1119. "Get the column view of the current buffer or subtree.
  1120. The first optional argument MAXLEVEL sets the level limit. A
  1121. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  1122. empty rows, an empty row being one where all the column view
  1123. specifiers except ITEM are empty. This function returns a list
  1124. containing the title row and all other rows. Each row is a list
  1125. of fields."
  1126. (if (featurep 'xemacs)
  1127. (save-excursion
  1128. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  1129. (n (length title)) row tbl)
  1130. (goto-char (point-min))
  1131. (while (re-search-forward "^\\(\\*+\\) " nil t)
  1132. (when (and (or (null maxlevel)
  1133. (>= maxlevel
  1134. (if org-odd-levels-only
  1135. (/ (1+ (length (match-string 1))) 2)
  1136. (length (match-string 1)))))
  1137. (get-char-property (match-beginning 0) 'org-columns-key))
  1138. (goto-char (match-beginning 0))
  1139. (setq row nil)
  1140. (loop for i from 0 to (1- n) do
  1141. (push (or (get-char-property (point)
  1142. 'org-columns-value-modified)
  1143. (get-char-property (point) 'org-columns-value)
  1144. "")
  1145. row)
  1146. (org-columns-forward-char))
  1147. (setq row (nreverse row))
  1148. (unless (and skip-empty-rows
  1149. (eq 1 (length (delete "" (delete-dups row)))))
  1150. (push row tbl))))
  1151. (append (list title 'hline) (nreverse tbl))))
  1152. (save-excursion
  1153. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  1154. (n (length title)) row tbl)
  1155. (goto-char (point-min))
  1156. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  1157. (or (null maxlevel)
  1158. (>= maxlevel
  1159. (if org-odd-levels-only
  1160. (/ (1+ (length (match-string 1))) 2)
  1161. (length (match-string 1))))))
  1162. (when (get-char-property (match-beginning 0) 'org-columns-key)
  1163. (setq row nil)
  1164. (loop for i from 0 to (1- n) do
  1165. (push (or (get-char-property (+ (match-beginning 0) i)
  1166. 'org-columns-value-modified)
  1167. (get-char-property (+ (match-beginning 0) i)
  1168. 'org-columns-value)
  1169. "")
  1170. row))
  1171. (setq row (nreverse row))
  1172. (unless (and skip-empty-rows
  1173. (eq 1 (length (delete "" (delete-dups row)))))
  1174. (push row tbl))))
  1175. (append (list title 'hline) (nreverse tbl))))))
  1176. (defun org-dblock-write:columnview (params)
  1177. "Write the column view table.
  1178. PARAMS is a property list of parameters:
  1179. :width enforce same column widths with <N> specifiers.
  1180. :id the :ID: property of the entry where the columns view
  1181. should be built, as a string. When `local', call locally.
  1182. When `global' call column view with the cursor at the beginning
  1183. of the buffer (usually this means that the whole buffer switches
  1184. to column view).
  1185. :hlines When t, insert a hline before each item. When a number, insert
  1186. a hline before each level <= that number.
  1187. :vlines When t, make each column a colgroup to enforce vertical lines.
  1188. :maxlevel When set to a number, don't capture headlines below this level.
  1189. :skip-empty-rows
  1190. When t, skip rows where all specifiers other than ITEM are empty."
  1191. (let ((pos (move-marker (make-marker) (point)))
  1192. (hlines (plist-get params :hlines))
  1193. (vlines (plist-get params :vlines))
  1194. (maxlevel (plist-get params :maxlevel))
  1195. (skip-empty-rows (plist-get params :skip-empty-rows))
  1196. tbl id idpos nfields tmp)
  1197. (save-excursion
  1198. (save-restriction
  1199. (when (setq id (plist-get params :id))
  1200. (cond ((not id) nil)
  1201. ((eq id 'global) (goto-char (point-min)))
  1202. ((eq id 'local) nil)
  1203. ((setq idpos (org-find-entry-with-id id))
  1204. (goto-char idpos))
  1205. (t (error "Cannot find entry with :ID: %s" id))))
  1206. (org-columns)
  1207. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  1208. (setq nfields (length (car tbl)))
  1209. (org-columns-quit)))
  1210. (goto-char pos)
  1211. (move-marker pos nil)
  1212. (when tbl
  1213. (when (plist-get params :hlines)
  1214. (setq tmp nil)
  1215. (while tbl
  1216. (if (eq (car tbl) 'hline)
  1217. (push (pop tbl) tmp)
  1218. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  1219. (if (and (not (eq (car tmp) 'hline))
  1220. (or (eq hlines t)
  1221. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  1222. (push 'hline tmp)))
  1223. (push (pop tbl) tmp)))
  1224. (setq tbl (nreverse tmp)))
  1225. (when vlines
  1226. (setq tbl (mapcar (lambda (x)
  1227. (if (eq 'hline x) x (cons "" x)))
  1228. tbl))
  1229. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  1230. (setq pos (point))
  1231. (insert (org-listtable-to-string tbl))
  1232. (when (plist-get params :width)
  1233. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  1234. org-columns-current-widths "|")))
  1235. (goto-char pos)
  1236. (org-table-align))))
  1237. (defun org-listtable-to-string (tbl)
  1238. "Convert a listtable TBL to a string that contains the Org-mode table.
  1239. The table still need to be alligned. The resulting string has no leading
  1240. and tailing newline characters."
  1241. (mapconcat
  1242. (lambda (x)
  1243. (cond
  1244. ((listp x)
  1245. (concat "|" (mapconcat 'identity x "|") "|"))
  1246. ((eq x 'hline) "|-|")
  1247. (t (error "Garbage in listtable: %s" x))))
  1248. tbl "\n"))
  1249. (defun org-insert-columns-dblock ()
  1250. "Create a dynamic block capturing a column view table."
  1251. (interactive)
  1252. (when (featurep 'xemacs) (org-columns-quit))
  1253. (let ((defaults '(:name "columnview" :hlines 1))
  1254. (id (completing-read
  1255. "Capture columns (local, global, entry with :ID: property) [local]: "
  1256. (append '(("global") ("local"))
  1257. (mapcar 'list (org-property-values "ID"))))))
  1258. (if (equal id "") (setq id 'local))
  1259. (if (equal id "global") (setq id 'global))
  1260. (setq defaults (append defaults (list :id id)))
  1261. (org-create-dblock defaults)
  1262. (org-update-dblock)))
  1263. ;;; Column view in the agenda
  1264. (defvar org-agenda-view-columns-initially nil
  1265. "When set, switch to columns view immediately after creating the agenda.")
  1266. (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
  1267. (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
  1268. (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
  1269. (defun org-agenda-columns ()
  1270. "Turn on or update column view in the agenda."
  1271. (interactive)
  1272. (org-verify-version 'columns)
  1273. (org-columns-remove-overlays)
  1274. (move-marker org-columns-begin-marker (point))
  1275. (let (fmt cache maxwidths m p a d)
  1276. (cond
  1277. ((and (boundp 'org-agenda-overriding-columns-format)
  1278. org-agenda-overriding-columns-format)
  1279. (setq fmt org-agenda-overriding-columns-format)
  1280. (org-set-local 'org-agenda-overriding-columns-format fmt))
  1281. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  1282. (setq fmt (or (org-entry-get m "COLUMNS" t)
  1283. (with-current-buffer (marker-buffer m)
  1284. org-columns-default-format))))
  1285. ((and (boundp 'org-columns-current-fmt)
  1286. (local-variable-p 'org-columns-current-fmt (current-buffer))
  1287. org-columns-current-fmt)
  1288. (setq fmt org-columns-current-fmt))
  1289. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  1290. (setq m (get-text-property m 'org-hd-marker))
  1291. (setq fmt (or (org-entry-get m "COLUMNS" t)
  1292. (with-current-buffer (marker-buffer m)
  1293. org-columns-default-format)))))
  1294. (setq fmt (or fmt org-columns-default-format))
  1295. (org-set-local 'org-columns-current-fmt fmt)
  1296. (org-columns-compile-format fmt)
  1297. (when org-agenda-columns-compute-summary-properties
  1298. (org-agenda-colview-compute org-columns-current-fmt-compiled))
  1299. (save-excursion
  1300. ;; Get and cache the properties
  1301. (goto-char (point-min))
  1302. (while (not (eobp))
  1303. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  1304. (get-text-property (point) 'org-marker)))
  1305. (setq p (org-entry-properties m))
  1306. (when (or (not (setq a (assoc org-effort-property p)))
  1307. (not (string-match "\\S-" (or (cdr a) ""))))
  1308. ;; OK, the property is not defined. Use appointment duration?
  1309. (when (and org-agenda-columns-add-appointments-to-effort-sum
  1310. (setq d (get-text-property (point) 'duration)))
  1311. (setq d (org-minutes-to-hh:mm-string d))
  1312. (put-text-property 0 (length d) 'face 'org-warning d)
  1313. (push (cons org-effort-property d) p)))
  1314. (push (cons (org-current-line) p) cache))
  1315. (beginning-of-line 2))
  1316. (when cache
  1317. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  1318. (org-set-local 'org-columns-current-maxwidths maxwidths)
  1319. (org-columns-display-here-title)
  1320. (mapc (lambda (x)
  1321. (goto-line (car x))
  1322. (org-columns-display-here (cdr x)))
  1323. cache)
  1324. (when org-agenda-columns-show-summaries
  1325. (org-agenda-colview-summarize cache))))))
  1326. (defun org-agenda-colview-summarize (cache)
  1327. "Summarize the summarizable columns in column view in the agenda.
  1328. This will add overlays to the date lines, to show the summary for each day."
  1329. (let* ((fmt (mapcar (lambda (x)
  1330. (list (car x) (if (equal (car x) "CLOCKSUM")
  1331. 'add_times (nth 4 x))))
  1332. org-columns-current-fmt-compiled))
  1333. line c c1 stype props lsum entries prop v)
  1334. (catch 'exit
  1335. (when (delq nil (mapcar 'cadr fmt))
  1336. ;; OK, at least one summation column, it makes sense to try this
  1337. (goto-char (point-max))
  1338. (while t
  1339. (when (or (get-text-property (point) 'org-date-line)
  1340. (eq (get-text-property (point) 'face)
  1341. 'org-agenda-structure))
  1342. ;; OK, this is a date line that should be used
  1343. (setq line (org-current-line))
  1344. (setq entries nil c cache cache nil)
  1345. (while (setq c1 (pop c))
  1346. (if (> (car c1) line)
  1347. (push c1 entries)
  1348. (push c1 cache)))
  1349. ;; now ENTRIES are the ones we want to use, CACHE is the rest
  1350. ;; Compute the summaries for the properties we want,
  1351. ;; set nil properties for the rest.
  1352. (when (setq entries (mapcar 'cdr entries))
  1353. (setq props
  1354. (mapcar
  1355. (lambda (f)
  1356. (setq prop (car f) stype (nth 1 f))
  1357. (cond
  1358. ((equal prop "ITEM")
  1359. (cons prop (buffer-substring (point-at-bol)
  1360. (point-at-eol))))
  1361. ((not stype) (cons prop ""))
  1362. (t
  1363. ;; do the summary
  1364. (setq lsum 0)
  1365. (mapc (lambda (x)
  1366. (setq v (cdr (assoc prop x)))
  1367. (if v (setq lsum (+ lsum
  1368. (org-column-string-to-number
  1369. v stype)))))
  1370. entries)
  1371. (setq lsum (org-columns-number-to-string lsum stype))
  1372. (put-text-property
  1373. 0 (length lsum) 'face 'bold lsum)
  1374. (cons prop lsum))))
  1375. fmt))
  1376. (org-columns-display-here props)
  1377. (org-set-local 'org-agenda-columns-active t)))
  1378. (if (bobp) (throw 'exit t))
  1379. (beginning-of-line 0))))))
  1380. (defun org-agenda-colview-compute (fmt)
  1381. "Compute the relevant columns in the contributing source buffers."
  1382. (let ((files org-agenda-contributing-files)
  1383. (org-columns-begin-marker (make-marker))
  1384. (org-columns-top-level-marker (make-marker))
  1385. f fm a b)
  1386. (while (setq f (pop files))
  1387. (setq b (find-buffer-visiting f))
  1388. (with-current-buffer (or (buffer-base-buffer b) b)
  1389. (save-excursion
  1390. (save-restriction
  1391. (widen)
  1392. (org-unmodified
  1393. (remove-text-properties (point-min) (point-max)
  1394. '(org-summaries t)))
  1395. (goto-char (point-min))
  1396. (org-columns-get-format-and-top-level)
  1397. (while (setq fm (pop fmt))
  1398. (if (equal (car fm) "CLOCKSUM")
  1399. (org-clock-sum)
  1400. (when (and (nth 4 fm)
  1401. (setq a (assoc (car fm)
  1402. org-columns-current-fmt-compiled))
  1403. (equal (nth 4 a) (nth 4 fm)))
  1404. (org-columns-compute (car fm)))))))))))
  1405. (provide 'org-colview)
  1406. (provide 'org-colview-xemacs)
  1407. ;;; org-colview-xemacs.el ends here