org-list.el 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. ;;; org-list.el --- Plain lists for Org-mode
  2. ;;
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Bastien Guerry <bzg AT altern DOT org>
  8. ;; Keywords: outlines, hypermedia, calendar, wp
  9. ;; Homepage: http://orgmode.org
  10. ;; Version: 6.28trans
  11. ;;
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;; This file contains the code dealing with plain lists in Org-mode.
  28. ;;; Code:
  29. (require 'org-macs)
  30. (require 'org-compat)
  31. (defvar org-blank-before-new-entry)
  32. (defvar org-M-RET-may-split-line)
  33. (declare-function org-invisible-p "org" ())
  34. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  35. (declare-function outline-next-heading "outline" ())
  36. (declare-function outline-back-to-heading "outline" (&optional invisible-ok))
  37. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  38. (declare-function org-back-over-empty-lines "org" ())
  39. (declare-function org-skip-whitespace "org" ())
  40. (declare-function org-trim "org" (s))
  41. (declare-function org-get-indentation "org" (&optional line))
  42. (declare-function org-timer-item "org-timer" (&optional arg))
  43. (declare-function org-combine-plists "org" (&rest plists))
  44. (declare-function org-entry-get "org" (pom property &optional inherit))
  45. (declare-function org-narrow-to-subtree "org" ())
  46. (declare-function org-show-subtree "org" ())
  47. (defgroup org-plain-lists nil
  48. "Options concerning plain lists in Org-mode."
  49. :tag "Org Plain lists"
  50. :group 'org-structure)
  51. (defcustom org-cycle-include-plain-lists t
  52. "When t, make TAB cycle visibility on plain list items.
  53. Cycling plain lists works only when the cursor is on a plain list
  54. item. When the cursor is on an outline heading, plain lists are
  55. treated as text. This is the most stable way of handling this,
  56. which is why it is the default.
  57. When this is the symbol `integrate', then during cycling, plain
  58. list items will *temporarily* be interpreted as outline headlines
  59. with a level given by 1000+i where i is the indentation of the
  60. bullet. This setting can lead to strange effects when switching
  61. visibility to `children', because the first \"child\" in a
  62. subtree decides what children should be listed. If that first
  63. \"child\" is a plain list item with an implied large level
  64. number, all true children and grand children of the outline
  65. heading will be exposed in a children' view."
  66. :group 'org-plain-lists
  67. :type '(choice
  68. (const :tag "Never" nil)
  69. (const :tag "With cursor in plain list (recommended)" t)
  70. (const :tag "As children of outline headings" integrate)))
  71. (defcustom org-list-demote-modify-bullet nil
  72. "Default bullet type installed when demoting an item.
  73. This is an association list, for each bullet type, this alist will point
  74. to the bulled that should be used when this item is demoted."
  75. :group 'org-plain-lists
  76. :type '(repeat
  77. (cons
  78. (choice :tag "If the current bullet is "
  79. (const "-")
  80. (const "+")
  81. (const "*")
  82. (const "1.")
  83. (const "1)"))
  84. (choice :tag "demotion will change it to"
  85. (const "-")
  86. (const "+")
  87. (const "*")
  88. (const "1.")
  89. (const "1)")))))
  90. (defcustom org-plain-list-ordered-item-terminator t
  91. "The character that makes a line with leading number an ordered list item.
  92. Valid values are ?. and ?\). To get both terminators, use t. While
  93. ?. may look nicer, it creates the danger that a line with leading
  94. number may be incorrectly interpreted as an item. ?\) therefore is
  95. the safe choice."
  96. :group 'org-plain-lists
  97. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  98. (const :tag "paren like in \"2)\"" ?\))
  99. (const :tab "both" t)))
  100. (defcustom org-list-two-spaces-after-bullet-regexp nil
  101. "A regular expression matching bullets that should have 2 spaces after them.
  102. When nil, no bullet will have two spaces after them.
  103. When a string, it will be used as a regular expression. When the bullet
  104. type of a list is changed, the new bullet type will be matched against this
  105. regexp. If it matches, there will be two spaces instead of one after
  106. the bullet in each item of he list."
  107. :group 'org-plain-list
  108. :type '(choice
  109. (const :tag "never" nil)
  110. (regexp)))
  111. (defcustom org-empty-line-terminates-plain-lists nil
  112. "Non-nil means, an empty line ends all plain list levels.
  113. When nil, empty lines are part of the preceding item."
  114. :group 'org-plain-lists
  115. :type 'boolean)
  116. (defcustom org-auto-renumber-ordered-lists t
  117. "Non-nil means, automatically renumber ordered plain lists.
  118. Renumbering happens when the sequence have been changed with
  119. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  120. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  121. :group 'org-plain-lists
  122. :type 'boolean)
  123. (defcustom org-provide-checkbox-statistics t
  124. "Non-nil means, update checkbox statistics after insert and toggle.
  125. When this is set, checkbox statistics is updated each time you
  126. either insert a new checkbox with \\[org-insert-todo-heading] or
  127. toggle a checkbox with \\[org-ctrl-c-ctrl-c]."
  128. :group 'org-plain-lists
  129. :type 'boolean)
  130. (defcustom org-hierarchical-checkbox-statistics t
  131. "Non-nil means, checkbox statistics counts only the state of direct children.
  132. When nil, all boxes below the cookie are counted."
  133. :group 'org-plain-lists
  134. :type 'boolean)
  135. (defcustom org-description-max-indent 20
  136. "Maximum indentation for the second line of a description list.
  137. When the indentation would be larger than this, it will become
  138. 5 characters instead."
  139. :group 'org-plain-lists
  140. :type 'integer)
  141. (defvar org-list-beginning-re
  142. "^\\([ \t]*\\)\\([-+]\\|[0-9]+[.)]\\) +\\(.*\\)$")
  143. (defcustom org-list-radio-list-templates
  144. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  145. % END RECEIVE ORGLST %n
  146. \\begin{comment}
  147. #+ORGLST: SEND %n org-list-to-latex
  148. | | |
  149. \\end{comment}\n")
  150. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  151. @c END RECEIVE ORGLST %n
  152. @ignore
  153. #+ORGLST: SEND %n org-list-to-texinfo
  154. | | |
  155. @end ignore\n")
  156. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  157. <!-- END RECEIVE ORGLST %n -->
  158. <!--
  159. #+ORGLST: SEND %n org-list-to-html
  160. | | |
  161. -->\n"))
  162. "Templates for radio lists in different major modes.
  163. All occurrences of %n in a template will be replaced with the name of the
  164. list, obtained by prompting the user."
  165. :group 'org-plain-lists
  166. :type '(repeat
  167. (list (symbol :tag "Major mode")
  168. (string :tag "Format"))))
  169. ;;;; Plain list items, including checkboxes
  170. ;;; Plain list items
  171. (defun org-at-item-p ()
  172. "Is point in a line starting a hand-formatted item?"
  173. (let ((llt org-plain-list-ordered-item-terminator))
  174. (save-excursion
  175. (goto-char (point-at-bol))
  176. (looking-at
  177. (cond
  178. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  179. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  180. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  181. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  182. (defun org-at-item-bullet-p ()
  183. "Is point at the bullet of a plain list item?"
  184. (and (org-at-item-p)
  185. (not (member (char-after) '(?\ ?\t)))
  186. (< (point) (match-end 0))))
  187. (defun org-in-item-p ()
  188. "It the cursor inside a plain list item.
  189. Does not have to be the first line."
  190. (save-excursion
  191. (condition-case nil
  192. (progn
  193. (org-beginning-of-item)
  194. (org-at-item-p)
  195. t)
  196. (error nil))))
  197. (defun org-insert-item (&optional checkbox)
  198. "Insert a new item at the current level.
  199. Return t when things worked, nil when we are not in an item."
  200. (when (save-excursion
  201. (condition-case nil
  202. (progn
  203. (org-beginning-of-item)
  204. (org-at-item-p)
  205. (if (org-invisible-p) (error "Invisible item"))
  206. t)
  207. (error nil)))
  208. (let* ((bul (match-string 0))
  209. (descp (save-excursion (goto-char (match-beginning 0))
  210. (beginning-of-line 1)
  211. (save-match-data
  212. (and (looking-at "[ \t]*\\(.*?\\) ::")
  213. (match-string 1)))))
  214. (empty-line-p (save-excursion
  215. (goto-char (match-beginning 0))
  216. (and (not (bobp))
  217. (or (beginning-of-line 0) t)
  218. (save-match-data
  219. (looking-at "[ \t]*$")))))
  220. (timerp (and descp
  221. (save-match-data
  222. (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
  223. descp))))
  224. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  225. (match-end 0)))
  226. (blank-a (if org-empty-line-terminates-plain-lists
  227. nil
  228. (cdr (assq 'plain-list-item org-blank-before-new-entry))))
  229. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  230. pos)
  231. (if descp (setq checkbox nil))
  232. (if timerp
  233. (progn (org-timer-item) t)
  234. (cond
  235. ((and (org-at-item-p) (<= (point) eow))
  236. ;; before the bullet
  237. (beginning-of-line 1)
  238. (open-line (if blank 2 1)))
  239. ((<= (point) eow)
  240. (beginning-of-line 1))
  241. (t
  242. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  243. (end-of-line 1)
  244. (delete-horizontal-space))
  245. (newline (if blank 2 1))))
  246. (insert bul
  247. (if checkbox "[ ]" "")
  248. (if descp (concat (if checkbox " " "")
  249. (read-string "Term: ") " :: ") ""))
  250. (just-one-space)
  251. (setq pos (point))
  252. (end-of-line 1)
  253. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  254. (org-maybe-renumber-ordered-list)
  255. (and checkbox (org-update-checkbox-count-maybe))
  256. t)))
  257. ;;; Checkboxes
  258. (defun org-at-item-checkbox-p ()
  259. "Is point at a line starting a plain-list item with a checklet?"
  260. (and (org-at-item-p)
  261. (save-excursion
  262. (goto-char (match-end 0))
  263. (skip-chars-forward " \t")
  264. (looking-at "\\[[- X]\\]"))))
  265. (defun org-toggle-checkbox (&optional toggle-presence)
  266. "Toggle the checkbox in the current line.
  267. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
  268. With double prefix, set checkbox to [-].
  269. When there is an active region, toggle status or presence of the checkbox
  270. in the first line, and make every item in the region have the same
  271. status or presence, respectively.
  272. If the cursor is in a headline, apply this to all checkbox items in the
  273. text below the heading."
  274. (interactive "P")
  275. (catch 'exit
  276. (let (beg end status first-present first-status blocked)
  277. (cond
  278. ((org-region-active-p)
  279. (setq beg (region-beginning) end (region-end)))
  280. ((org-on-heading-p)
  281. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  282. ((org-at-item-checkbox-p)
  283. (save-excursion
  284. (if (equal toggle-presence '(4))
  285. (progn
  286. (replace-match "")
  287. (goto-char (match-beginning 0))
  288. (just-one-space))
  289. (when (setq blocked (org-checkbox-blocked-p))
  290. (error "Checkbox blocked because of unchecked box in line %d"
  291. blocked))
  292. (replace-match
  293. (cond ((equal toggle-presence '(16)) "[-]")
  294. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  295. (t "[ ]"))
  296. t t)))
  297. (throw 'exit t))
  298. ((org-at-item-p)
  299. ;; add a checkbox
  300. (save-excursion
  301. (goto-char (match-end 0))
  302. (insert "[ ] "))
  303. (throw 'exit t))
  304. (t (error "Not at a checkbox or heading, and no active region")))
  305. (setq end (move-marker (make-marker) end))
  306. (save-excursion
  307. (goto-char beg)
  308. (setq first-present (org-at-item-checkbox-p)
  309. first-status
  310. (save-excursion
  311. (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t)
  312. (equal (match-string 1) "[X]"))))
  313. (while (< (point) end)
  314. (if toggle-presence
  315. (cond
  316. ((and first-present (org-at-item-checkbox-p))
  317. (save-excursion
  318. (replace-match "")
  319. (goto-char (match-beginning 0))
  320. (just-one-space)))
  321. ((and (not first-present) (not (org-at-item-checkbox-p))
  322. (org-at-item-p))
  323. (save-excursion
  324. (goto-char (match-end 0))
  325. (insert "[ ] "))))
  326. (when (org-at-item-checkbox-p)
  327. (setq status (equal (match-string 0) "[X]"))
  328. (replace-match
  329. (if first-status "[ ]" "[X]") t t)))
  330. (beginning-of-line 2)))))
  331. (org-update-checkbox-count-maybe))
  332. (defun org-reset-checkbox-state-subtree ()
  333. "Reset all checkboxes in an entry subtree."
  334. (interactive "*")
  335. (save-restriction
  336. (save-excursion
  337. (org-narrow-to-subtree)
  338. (org-show-subtree)
  339. (goto-char (point-min))
  340. (let ((end (point-max)))
  341. (while (< (point) end)
  342. (when (org-at-item-checkbox-p)
  343. (replace-match "[ ]" t t))
  344. (beginning-of-line 2))))
  345. (org-update-checkbox-count-maybe)))
  346. (defun org-checkbox-blocked-p ()
  347. "Is the current checkbox blocked from for being checked now?
  348. A checkbox is blocked if all of the following conditions are fulfilled:
  349. 1. The checkbox is not checked already.
  350. 2. The current entry has the ORDERED property set.
  351. 3. There is an unchecked checkbox in this entry before the current line."
  352. (catch 'exit
  353. (save-match-data
  354. (save-excursion
  355. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  356. (when (equal (match-string 0) "[X]")
  357. ;; the box is already checked!
  358. (throw 'exit nil))
  359. (let ((end (point-at-bol)))
  360. (condition-case nil (org-back-to-heading t)
  361. (error (throw 'exit nil)))
  362. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  363. (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t)
  364. (org-current-line)
  365. nil))))))
  366. (defvar org-checkbox-statistics-hook nil
  367. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  368. This hook runs even if `org-provide-checkbox-statistics' is nil, to it can
  369. be used to implement alternative ways of collecting statistics information.")
  370. (defun org-update-checkbox-count-maybe ()
  371. "Update checkbox statistics unless turned off by user."
  372. (when org-provide-checkbox-statistics
  373. (org-update-checkbox-count))
  374. (run-hooks 'org-checkbox-statistics-hook))
  375. (defun org-update-checkbox-count (&optional all)
  376. "Update the checkbox statistics in the current section.
  377. This will find all statistic cookies like [57%] and [6/12] and update them
  378. with the current numbers. With optional prefix argument ALL, do this for
  379. the whole buffer."
  380. (interactive "P")
  381. (save-excursion
  382. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  383. (beg (condition-case nil
  384. (progn (org-back-to-heading) (point))
  385. (error (point-min))))
  386. (end (move-marker (make-marker)
  387. (progn (outline-next-heading) (point))))
  388. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  389. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  390. (re-find (concat re "\\|" re-box))
  391. beg-cookie end-cookie is-percent c-on c-off lim
  392. eline curr-ind next-ind continue-from startsearch
  393. (recursive
  394. (or (not org-hierarchical-checkbox-statistics)
  395. (string-match "\\<recursive\\>"
  396. (or (org-entry-get nil "COOKIE_DATA") ""))))
  397. (cstat 0)
  398. )
  399. (when all
  400. (goto-char (point-min))
  401. (outline-next-heading)
  402. (setq beg (point) end (point-max)))
  403. (goto-char end)
  404. ;; find each statistics cookie
  405. (while (and (re-search-backward re-find beg t)
  406. (not (save-match-data
  407. (and (org-on-heading-p)
  408. (string-match "\\<todo\\>"
  409. (downcase
  410. (or (org-entry-get
  411. nil "COOKIE_DATA")
  412. "")))))))
  413. (setq beg-cookie (match-beginning 1)
  414. end-cookie (match-end 1)
  415. cstat (+ cstat (if end-cookie 1 0))
  416. startsearch (point-at-eol)
  417. continue-from (match-beginning 0)
  418. is-percent (match-beginning 2)
  419. lim (cond
  420. ((org-on-heading-p) (outline-next-heading) (point))
  421. ((org-at-item-p) (org-end-of-item) (point))
  422. (t nil))
  423. c-on 0
  424. c-off 0)
  425. (when lim
  426. ;; find first checkbox for this cookie and gather
  427. ;; statistics from all that are at this indentation level
  428. (goto-char startsearch)
  429. (if (re-search-forward re-box lim t)
  430. (progn
  431. (org-beginning-of-item)
  432. (setq curr-ind (org-get-indentation))
  433. (setq next-ind curr-ind)
  434. (while (and (bolp) (org-at-item-p)
  435. (if recursive
  436. (<= curr-ind next-ind)
  437. (= curr-ind next-ind)))
  438. (save-excursion (end-of-line) (setq eline (point)))
  439. (if (re-search-forward re-box eline t)
  440. (if (member (match-string 2) '("[ ]" "[-]"))
  441. (setq c-off (1+ c-off))
  442. (setq c-on (1+ c-on))))
  443. (if (not recursive)
  444. (org-end-of-item)
  445. (end-of-line)
  446. (when (re-search-forward org-list-beginning-re lim t)
  447. (beginning-of-line)))
  448. (setq next-ind (org-get-indentation)))))
  449. (goto-char continue-from)
  450. ;; update cookie
  451. (when end-cookie
  452. (delete-region beg-cookie end-cookie)
  453. (goto-char beg-cookie)
  454. (insert
  455. (if is-percent
  456. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  457. (format "[%d/%d]" c-on (+ c-on c-off)))))
  458. ;; update items checkbox if it has one
  459. (when (org-at-item-p)
  460. (org-beginning-of-item)
  461. (when (and (> (+ c-on c-off) 0)
  462. (re-search-forward re-box (point-at-eol) t))
  463. (setq beg-cookie (match-beginning 2)
  464. end-cookie (match-end 2))
  465. (delete-region beg-cookie end-cookie)
  466. (goto-char beg-cookie)
  467. (cond ((= c-off 0) (insert "[X]"))
  468. ((= c-on 0) (insert "[ ]"))
  469. (t (insert "[-]")))
  470. )))
  471. (goto-char continue-from))
  472. (when (interactive-p)
  473. (message "Checkbox statistics updated %s (%d places)"
  474. (if all "in entire file" "in current outline entry") cstat)))))
  475. (defun org-get-checkbox-statistics-face ()
  476. "Select the face for checkbox statistics.
  477. The face will be `org-done' when all relevant boxes are checked. Otherwise
  478. it will be `org-todo'."
  479. (if (match-end 1)
  480. (if (equal (match-string 1) "100%")
  481. 'org-checkbox-statistics-done
  482. 'org-checkbox-statistics-todo)
  483. (if (and (> (match-end 2) (match-beginning 2))
  484. (equal (match-string 2) (match-string 3)))
  485. 'org-checkbox-statistics-done
  486. 'org-checkbox-statistics-todo)))
  487. (defun org-beginning-of-item ()
  488. "Go to the beginning of the current hand-formatted item.
  489. If the cursor is not in an item, throw an error."
  490. (interactive)
  491. (let ((pos (point))
  492. (limit (save-excursion
  493. (condition-case nil
  494. (progn
  495. (org-back-to-heading)
  496. (beginning-of-line 2) (point))
  497. (error (point-min)))))
  498. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  499. ind ind1)
  500. (if (org-at-item-p)
  501. (beginning-of-line 1)
  502. (beginning-of-line 1)
  503. (skip-chars-forward " \t")
  504. (setq ind (current-column))
  505. (if (catch 'exit
  506. (while t
  507. (beginning-of-line 0)
  508. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  509. (if (looking-at "[ \t]*$")
  510. (setq ind1 ind-empty)
  511. (skip-chars-forward " \t")
  512. (setq ind1 (current-column)))
  513. (if (< ind1 ind)
  514. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  515. nil
  516. (goto-char pos)
  517. (error "Not in an item")))))
  518. (defun org-end-of-item ()
  519. "Go to the end of the current hand-formatted item.
  520. If the cursor is not in an item, throw an error."
  521. (interactive)
  522. (let* ((pos (point))
  523. ind1
  524. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  525. (limit (save-excursion (outline-next-heading) (point)))
  526. (ind (save-excursion
  527. (org-beginning-of-item)
  528. (skip-chars-forward " \t")
  529. (current-column)))
  530. (end (catch 'exit
  531. (while t
  532. (beginning-of-line 2)
  533. (if (eobp) (throw 'exit (point)))
  534. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  535. (if (looking-at "[ \t]*$")
  536. (setq ind1 ind-empty)
  537. (skip-chars-forward " \t")
  538. (setq ind1 (current-column)))
  539. (if (<= ind1 ind)
  540. (throw 'exit (point-at-bol)))))))
  541. (if end
  542. (goto-char end)
  543. (goto-char pos)
  544. (error "Not in an item"))))
  545. (defun org-next-item ()
  546. "Move to the beginning of the next item in the current plain list.
  547. Error if not at a plain list, or if this is the last item in the list."
  548. (interactive)
  549. (let (ind ind1 (pos (point)))
  550. (org-beginning-of-item)
  551. (setq ind (org-get-indentation))
  552. (org-end-of-item)
  553. (setq ind1 (org-get-indentation))
  554. (unless (and (org-at-item-p) (= ind ind1))
  555. (goto-char pos)
  556. (error "On last item"))))
  557. (defun org-previous-item ()
  558. "Move to the beginning of the previous item in the current plain list.
  559. Error if not at a plain list, or if this is the first item in the list."
  560. (interactive)
  561. (let (beg ind ind1 (pos (point)))
  562. (org-beginning-of-item)
  563. (setq beg (point))
  564. (setq ind (org-get-indentation))
  565. (goto-char beg)
  566. (catch 'exit
  567. (while t
  568. (beginning-of-line 0)
  569. (if (looking-at "[ \t]*$")
  570. nil
  571. (if (<= (setq ind1 (org-get-indentation)) ind)
  572. (throw 'exit t)))))
  573. (condition-case nil
  574. (if (or (not (org-at-item-p))
  575. (< ind1 (1- ind)))
  576. (error "")
  577. (org-beginning-of-item))
  578. (error (goto-char pos)
  579. (error "On first item")))))
  580. (defun org-first-list-item-p ()
  581. "Is this heading the first item in a plain list?"
  582. (unless (org-at-item-p)
  583. (error "Not at a plain list item"))
  584. (save-excursion
  585. (org-beginning-of-item)
  586. (= (point) (save-excursion (org-beginning-of-item-list)))))
  587. (defun org-move-item-down ()
  588. "Move the plain list item at point down, i.e. swap with following item.
  589. Subitems (items with larger indentation) are considered part of the item,
  590. so this really moves item trees."
  591. (interactive)
  592. (let ((col (current-column))
  593. (pos (point))
  594. beg beg0 end end0 ind ind1 txt ne-end ne-beg)
  595. (org-beginning-of-item)
  596. (setq beg0 (point))
  597. (save-excursion
  598. (setq ne-beg (org-back-over-empty-lines))
  599. (setq beg (point)))
  600. (goto-char beg0)
  601. (setq ind (org-get-indentation))
  602. (org-end-of-item)
  603. (setq end0 (point))
  604. (setq ind1 (org-get-indentation))
  605. (setq ne-end (org-back-over-empty-lines))
  606. (setq end (point))
  607. (goto-char beg0)
  608. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  609. ;; include less whitespace
  610. (save-excursion
  611. (goto-char beg)
  612. (forward-line (- ne-beg ne-end))
  613. (setq beg (point))))
  614. (goto-char end0)
  615. (if (and (org-at-item-p) (= ind ind1))
  616. (progn
  617. (org-end-of-item)
  618. (org-back-over-empty-lines)
  619. (setq txt (buffer-substring beg end))
  620. (save-excursion
  621. (delete-region beg end))
  622. (setq pos (point))
  623. (insert txt)
  624. (goto-char pos) (org-skip-whitespace)
  625. (org-maybe-renumber-ordered-list)
  626. (move-to-column col))
  627. (goto-char pos)
  628. (move-to-column col)
  629. (error "Cannot move this item further down"))))
  630. (defun org-move-item-up (arg)
  631. "Move the plain list item at point up, i.e. swap with previous item.
  632. Subitems (items with larger indentation) are considered part of the item,
  633. so this really moves item trees."
  634. (interactive "p")
  635. (let ((col (current-column)) (pos (point))
  636. beg beg0 end ind ind1 txt
  637. ne-beg ne-ins ins-end)
  638. (org-beginning-of-item)
  639. (setq beg0 (point))
  640. (setq ind (org-get-indentation))
  641. (save-excursion
  642. (setq ne-beg (org-back-over-empty-lines))
  643. (setq beg (point)))
  644. (goto-char beg0)
  645. (org-end-of-item)
  646. (org-back-over-empty-lines)
  647. (setq end (point))
  648. (goto-char beg0)
  649. (catch 'exit
  650. (while t
  651. (beginning-of-line 0)
  652. (if (looking-at "[ \t]*$")
  653. (if org-empty-line-terminates-plain-lists
  654. (progn
  655. (goto-char pos)
  656. (error "Cannot move this item further up"))
  657. nil)
  658. (if (<= (setq ind1 (org-get-indentation)) ind)
  659. (throw 'exit t)))))
  660. (condition-case nil
  661. (org-beginning-of-item)
  662. (error (goto-char beg0)
  663. (move-to-column col)
  664. (error "Cannot move this item further up")))
  665. (setq ind1 (org-get-indentation))
  666. (if (and (org-at-item-p) (= ind ind1))
  667. (progn
  668. (setq ne-ins (org-back-over-empty-lines))
  669. (setq txt (buffer-substring beg end))
  670. (save-excursion
  671. (delete-region beg end))
  672. (setq pos (point))
  673. (insert txt)
  674. (setq ins-end (point))
  675. (goto-char pos) (org-skip-whitespace)
  676. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  677. ;; Move whitespace back to beginning
  678. (save-excursion
  679. (goto-char ins-end)
  680. (let ((kill-whole-line t))
  681. (kill-line (- ne-ins ne-beg)) (point)))
  682. (insert (make-string (- ne-ins ne-beg) ?\n)))
  683. (org-maybe-renumber-ordered-list)
  684. (move-to-column col))
  685. (goto-char pos)
  686. (move-to-column col)
  687. (error "Cannot move this item further up"))))
  688. (defun org-maybe-renumber-ordered-list ()
  689. "Renumber the ordered list at point if setup allows it.
  690. This tests the user option `org-auto-renumber-ordered-lists' before
  691. doing the renumbering."
  692. (interactive)
  693. (when (and org-auto-renumber-ordered-lists
  694. (org-at-item-p))
  695. (if (match-beginning 3)
  696. (org-renumber-ordered-list 1)
  697. (org-fix-bullet-type))))
  698. (defun org-maybe-renumber-ordered-list-safe ()
  699. (condition-case nil
  700. (save-excursion
  701. (org-maybe-renumber-ordered-list))
  702. (error nil)))
  703. (defun org-cycle-list-bullet (&optional which)
  704. "Cycle through the different itemize/enumerate bullets.
  705. This cycle the entire list level through the sequence:
  706. `-' -> `+' -> `*' -> `1.' -> `1)'
  707. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  708. 0 means `-', 1 means `+' etc."
  709. (interactive "P")
  710. (org-preserve-lc
  711. (org-beginning-of-item-list)
  712. (org-at-item-p)
  713. (beginning-of-line 1)
  714. (let ((current (match-string 0))
  715. (prevp (eq which 'previous))
  716. new old)
  717. (setq new (cond
  718. ((and (numberp which)
  719. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  720. ((string-match "-" current) (if prevp "1)" "+"))
  721. ((string-match "\\+" current)
  722. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  723. ((string-match "\\*" current) (if prevp "+" "1."))
  724. ((string-match "\\." current)
  725. (if prevp (if (looking-at "\\S-") "+" "*") "1)"))
  726. ((string-match ")" current) (if prevp "1." "-"))
  727. (t (error "This should not happen"))))
  728. (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
  729. (setq old (match-string 2))
  730. (replace-match (concat "\\1" new)))
  731. (org-shift-item-indentation (- (length new) (length old)))
  732. (org-fix-bullet-type)
  733. (org-maybe-renumber-ordered-list))))
  734. (defun org-get-string-indentation (s)
  735. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  736. (let ((n -1) (i 0) (w tab-width) c)
  737. (catch 'exit
  738. (while (< (setq n (1+ n)) (length s))
  739. (setq c (aref s n))
  740. (cond ((= c ?\ ) (setq i (1+ i)))
  741. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  742. (t (throw 'exit t)))))
  743. i))
  744. (defun org-renumber-ordered-list (arg)
  745. "Renumber an ordered plain list.
  746. Cursor needs to be in the first line of an item, the line that starts
  747. with something like \"1.\" or \"2)\"."
  748. (interactive "p")
  749. (unless (and (org-at-item-p)
  750. (match-beginning 3))
  751. (error "This is not an ordered list"))
  752. (let ((line (org-current-line))
  753. (col (current-column))
  754. (ind (org-get-string-indentation
  755. (buffer-substring (point-at-bol) (match-beginning 3))))
  756. ;; (term (substring (match-string 3) -1))
  757. ind1 (n (1- arg))
  758. fmt bobp old new)
  759. ;; find where this list begins
  760. (org-beginning-of-item-list)
  761. (setq bobp (bobp))
  762. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  763. (setq fmt (concat "%d" (or (match-string 1) ".")))
  764. (beginning-of-line 0)
  765. ;; walk forward and replace these numbers
  766. (catch 'exit
  767. (while t
  768. (catch 'next
  769. (if bobp (setq bobp nil) (beginning-of-line 2))
  770. (if (eobp) (throw 'exit nil))
  771. (if (looking-at "[ \t]*$") (throw 'next nil))
  772. (skip-chars-forward " \t") (setq ind1 (current-column))
  773. (if (> ind1 ind) (throw 'next t))
  774. (if (< ind1 ind) (throw 'exit t))
  775. (if (not (org-at-item-p)) (throw 'exit nil))
  776. (setq old (match-string 2))
  777. (delete-region (match-beginning 2) (match-end 2))
  778. (goto-char (match-beginning 2))
  779. (insert (setq new (format fmt (setq n (1+ n)))))
  780. (org-shift-item-indentation (- (length new) (length old))))))
  781. (goto-line line)
  782. (org-move-to-column col)))
  783. (defun org-fix-bullet-type (&optional force-bullet)
  784. "Make sure all items in this list have the same bullet as the first item.
  785. Also, fix the indentation."
  786. (interactive)
  787. (unless (org-at-item-p) (error "This is not a list"))
  788. (let ((line (org-current-line))
  789. (col (current-column))
  790. (ind (current-indentation))
  791. ind1 bullet oldbullet)
  792. ;; find where this list begins
  793. (org-beginning-of-item-list)
  794. (beginning-of-line 1)
  795. ;; find out what the bullet type is
  796. (looking-at "[ \t]*\\(\\S-+\\)")
  797. (setq bullet (concat (or force-bullet (match-string 1)) " "))
  798. (if (and org-list-two-spaces-after-bullet-regexp
  799. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  800. (setq bullet (concat bullet " ")))
  801. ;; walk forward and replace these numbers
  802. (beginning-of-line 0)
  803. (catch 'exit
  804. (while t
  805. (catch 'next
  806. (beginning-of-line 2)
  807. (if (eobp) (throw 'exit nil))
  808. (if (looking-at "[ \t]*$") (throw 'next nil))
  809. (skip-chars-forward " \t") (setq ind1 (current-column))
  810. (if (> ind1 ind) (throw 'next t))
  811. (if (< ind1 ind) (throw 'exit t))
  812. (if (not (org-at-item-p)) (throw 'exit nil))
  813. (skip-chars-forward " \t")
  814. (looking-at "\\S-+ *")
  815. (setq oldbullet (match-string 0))
  816. (unless (equal bullet oldbullet) (replace-match bullet))
  817. (org-shift-item-indentation (- (length bullet) (length oldbullet))))))
  818. (goto-line line)
  819. (org-move-to-column col)
  820. (if (string-match "[0-9]" bullet)
  821. (org-renumber-ordered-list 1))))
  822. (defun org-shift-item-indentation (delta)
  823. "Shift the indentation in current item by DELTA."
  824. (save-excursion
  825. (let ((beg (point-at-bol))
  826. (end (progn (org-end-of-item) (point)))
  827. i)
  828. (goto-char end)
  829. (beginning-of-line 0)
  830. (while (> (point) beg)
  831. (when (looking-at "[ \t]*\\S-")
  832. ;; this is not an empty line
  833. (setq i (org-get-indentation))
  834. (if (and (> i 0) (> (setq i (+ i delta)) 0))
  835. (indent-line-to i)))
  836. (beginning-of-line 0)))))
  837. (defun org-beginning-of-item-list ()
  838. "Go to the beginning of the current item list.
  839. I.e. to the first item in this list."
  840. (interactive)
  841. (org-beginning-of-item)
  842. (let ((pos (point-at-bol))
  843. (ind (org-get-indentation))
  844. ind1)
  845. ;; find where this list begins
  846. (catch 'exit
  847. (while t
  848. (catch 'next
  849. (beginning-of-line 0)
  850. (if (looking-at "[ \t]*$")
  851. (throw (if (bobp) 'exit 'next) t))
  852. (skip-chars-forward " \t") (setq ind1 (current-column))
  853. (if (or (< ind1 ind)
  854. (and (= ind1 ind)
  855. (not (org-at-item-p)))
  856. (and (= (point-at-bol) (point-min))
  857. (setq pos (point-min))))
  858. (throw 'exit t)
  859. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  860. (goto-char pos)))
  861. (defun org-end-of-item-list ()
  862. "Go to the end of the current item list.
  863. I.e. to the text after the last item."
  864. (interactive)
  865. (org-beginning-of-item)
  866. (let ((pos (point-at-bol))
  867. (ind (org-get-indentation))
  868. ind1)
  869. ;; find where this list begins
  870. (catch 'exit
  871. (while t
  872. (catch 'next
  873. (beginning-of-line 2)
  874. (if (looking-at "[ \t]*$")
  875. (if (eobp)
  876. (progn (setq pos (point)) (throw 'exit t))
  877. (throw 'next t)))
  878. (skip-chars-forward " \t") (setq ind1 (current-column))
  879. (if (or (< ind1 ind)
  880. (and (= ind1 ind)
  881. (not (org-at-item-p)))
  882. (eobp))
  883. (progn
  884. (setq pos (point-at-bol))
  885. (throw 'exit t))))))
  886. (goto-char pos)))
  887. (defvar org-last-indent-begin-marker (make-marker))
  888. (defvar org-last-indent-end-marker (make-marker))
  889. (defun org-outdent-item (arg)
  890. "Outdent a local list item."
  891. (interactive "p")
  892. (org-indent-item (- arg)))
  893. (defun org-indent-item (arg)
  894. "Indent a local list item."
  895. (interactive "p")
  896. (and (org-region-active-p) (org-cursor-to-region-beginning))
  897. (unless (org-at-item-p)
  898. (error "Not on an item"))
  899. (let (beg end ind ind1 ind-bul delta ind-down ind-up firstp)
  900. (setq firstp (org-first-list-item-p))
  901. (save-excursion
  902. (setq end (and (org-region-active-p) (region-end)))
  903. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  904. (setq beg org-last-indent-begin-marker
  905. end org-last-indent-end-marker)
  906. (org-beginning-of-item)
  907. (setq beg (move-marker org-last-indent-begin-marker (point)))
  908. (org-end-of-item)
  909. (setq end (move-marker org-last-indent-end-marker (or end (point)))))
  910. (goto-char beg)
  911. (setq ind-bul (org-item-indent-positions)
  912. ind (caar ind-bul)
  913. ind-down (car (nth 2 ind-bul))
  914. ind-up (car (nth 1 ind-bul))
  915. delta (if (> arg 0)
  916. (if ind-down (- ind-down ind) 2)
  917. (if ind-up (- ind-up ind) -2)))
  918. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  919. (while (< (point) end)
  920. (beginning-of-line 1)
  921. (skip-chars-forward " \t") (setq ind1 (current-column))
  922. (delete-region (point-at-bol) (point))
  923. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  924. (beginning-of-line 2)))
  925. (org-fix-bullet-type
  926. (and (> arg 0)
  927. (not firstp)
  928. (cdr (assoc (cdr (nth 0 ind-bul)) org-list-demote-modify-bullet))))
  929. (org-maybe-renumber-ordered-list-safe)
  930. (save-excursion
  931. (beginning-of-line 0)
  932. (condition-case nil (org-beginning-of-item) (error nil))
  933. (org-maybe-renumber-ordered-list-safe))))
  934. (defun org-item-indent-positions ()
  935. "Return indentation for plain list items.
  936. This returns a list with three values: The current indentation, the
  937. parent indentation and the indentation a child should have.
  938. Assumes cursor in item line."
  939. (let* ((bolpos (point-at-bol))
  940. (ind (org-get-indentation))
  941. (bullet (org-get-bullet))
  942. ind-down ind-up bullet-up bullet-down pos)
  943. (save-excursion
  944. (org-beginning-of-item-list)
  945. (skip-chars-backward "\n\r \t")
  946. (when (org-in-item-p)
  947. (org-beginning-of-item)
  948. (setq ind-up (org-get-indentation))
  949. (setq bullet-up (org-get-bullet))))
  950. (setq pos (point))
  951. (save-excursion
  952. (cond
  953. ((and (condition-case nil (progn (org-previous-item) t)
  954. (error nil))
  955. (or (forward-char 1) t)
  956. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  957. (setq ind-down (org-get-indentation)
  958. bullet-down (org-get-bullet)))
  959. ((and (goto-char pos)
  960. (org-at-item-p))
  961. (goto-char (match-end 0))
  962. (skip-chars-forward " \t")
  963. (setq ind-down (current-column)
  964. bullet-down (org-get-bullet)))))
  965. (if (and bullet-down (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-down))
  966. (setq bullet-down (concat "1" (match-string 1 bullet-down))))
  967. (if (and bullet-up (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-up))
  968. (setq bullet-up (concat "1" (match-string 1 bullet-up))))
  969. (if (and bullet (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet))
  970. (setq bullet (concat "1" (match-string 1 bullet))))
  971. (list (cons ind bullet)
  972. (cons ind-up bullet-up)
  973. (cons ind-down bullet-down))))
  974. (defun org-get-bullet ()
  975. (save-excursion
  976. (goto-char (point-at-bol))
  977. (and (looking-at
  978. "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\(\\*\\)\\)\\( \\|$\\)")
  979. (or (match-string 2) (match-string 4)))))
  980. ;;; Send and receive lists
  981. (defun org-list-parse-list (&optional delete)
  982. "Parse the list at point and maybe DELETE it.
  983. Return a list containing first level items as strings and
  984. sublevels as a list of strings."
  985. (let* ((item-beginning (org-list-item-beginning))
  986. (start (car item-beginning))
  987. (end (org-list-end (cdr item-beginning)))
  988. output itemsep ltype)
  989. (while (re-search-forward org-list-beginning-re end t)
  990. (goto-char (match-beginning 3))
  991. (save-match-data
  992. (cond ((string-match "[0-9]" (match-string 2))
  993. (setq itemsep "[0-9]+\\(?:\\.\\|)\\)"
  994. ltype 'ordered))
  995. ((string-match "^.*::" (match-string 0))
  996. (setq itemsep "[-+]" ltype 'descriptive))
  997. (t (setq itemsep "[-+]" ltype 'unordered))))
  998. (let* ((indent1 (match-string 1))
  999. (nextitem (save-excursion
  1000. (save-match-data
  1001. (or (and (re-search-forward
  1002. (concat "^" indent1 itemsep " *?") end t)
  1003. (match-beginning 0)) end))))
  1004. (item (buffer-substring
  1005. (point)
  1006. (or (and (re-search-forward
  1007. org-list-beginning-re end t)
  1008. (goto-char (match-beginning 0)))
  1009. (goto-char end))))
  1010. (nextindent (match-string 1))
  1011. (item (org-trim item))
  1012. (item (if (string-match "^\\[\\([xX ]\\)\\]" item)
  1013. (replace-match (if (equal (match-string 1 item) " ")
  1014. "[CBOFF]"
  1015. "[CBON]")
  1016. t nil item)
  1017. item)))
  1018. (push item output)
  1019. (when (> (length nextindent)
  1020. (length indent1))
  1021. (narrow-to-region (point) nextitem)
  1022. (push (org-list-parse-list) output)
  1023. (widen))))
  1024. (when delete (delete-region start end))
  1025. (setq output (nreverse output))
  1026. (push ltype output)))
  1027. (defun org-list-item-beginning ()
  1028. "Find the beginning of the list item.
  1029. Return a cons which car is the beginning position of the item and
  1030. cdr is the indentation string."
  1031. (save-excursion
  1032. (if (not (or (looking-at org-list-beginning-re)
  1033. (re-search-backward
  1034. org-list-beginning-re nil t)))
  1035. (progn (goto-char (point-min)) (point))
  1036. (cons (match-beginning 0) (match-string 1)))))
  1037. (defun org-list-goto-true-beginning ()
  1038. "Go to the beginning of the list at point."
  1039. (beginning-of-line 1)
  1040. (while (looking-at org-list-beginning-re)
  1041. (beginning-of-line 0))
  1042. (progn
  1043. (re-search-forward org-list-beginning-re nil t)
  1044. (goto-char (match-beginning 0))))
  1045. (defun org-list-make-subtree ()
  1046. "Convert the plain list at point into a subtree."
  1047. (interactive)
  1048. (org-list-goto-true-beginning)
  1049. (let ((list (org-list-parse-list t)) nstars)
  1050. (save-excursion
  1051. (if (condition-case nil
  1052. (org-back-to-heading)
  1053. (error nil))
  1054. (progn (re-search-forward org-complex-heading-regexp nil t)
  1055. (setq nstars (length (match-string 1))))
  1056. (setq nstars 0)))
  1057. (org-list-make-subtrees list (1+ nstars))))
  1058. (defun org-list-make-subtrees (list level)
  1059. "Convert LIST into subtrees starting at LEVEL."
  1060. (if (symbolp (car list))
  1061. (org-list-make-subtrees (cdr list) level)
  1062. (mapcar (lambda (item)
  1063. (if (stringp item)
  1064. (insert (make-string
  1065. (if org-odd-levels-only
  1066. (1- (* 2 level)) level) ?*) " " item "\n")
  1067. (org-list-make-subtrees item (1+ level))))
  1068. list)))
  1069. (defun org-list-end (indent)
  1070. "Return the position of the end of the list.
  1071. INDENT is the indentation of the list, as a string."
  1072. (save-excursion
  1073. (catch 'exit
  1074. (while (or (looking-at org-list-beginning-re)
  1075. (looking-at (concat "^" indent "[ \t]+\\|^$"))
  1076. (> (or (get-text-property (point) 'original-indentation) -1)
  1077. (length indent)))
  1078. (if (eq (point) (point-max))
  1079. (throw 'exit (point-max)))
  1080. (forward-line 1)))
  1081. (point)))
  1082. (defun org-list-insert-radio-list ()
  1083. "Insert a radio list template appropriate for this major mode."
  1084. (interactive)
  1085. (let* ((e (assq major-mode org-list-radio-list-templates))
  1086. (txt (nth 1 e))
  1087. name pos)
  1088. (unless e (error "No radio list setup defined for %s" major-mode))
  1089. (setq name (read-string "List name: "))
  1090. (while (string-match "%n" txt)
  1091. (setq txt (replace-match name t t txt)))
  1092. (or (bolp) (insert "\n"))
  1093. (setq pos (point))
  1094. (insert txt)
  1095. (goto-char pos)))
  1096. (defun org-list-send-list (&optional maybe)
  1097. "Send a tranformed version of this list to the receiver position.
  1098. With argument MAYBE, fail quietly if no transformation is defined for
  1099. this list."
  1100. (interactive)
  1101. (catch 'exit
  1102. (unless (org-at-item-p) (error "Not at a list"))
  1103. (save-excursion
  1104. (org-list-find-true-beginning)
  1105. (beginning-of-line 0)
  1106. (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  1107. (if maybe
  1108. (throw 'exit nil)
  1109. (error "Don't know how to transform this list"))))
  1110. (let* ((name (match-string 1))
  1111. (item-beginning (org-list-item-beginning))
  1112. (transform (intern (match-string 2)))
  1113. (txt (buffer-substring-no-properties
  1114. (car item-beginning)
  1115. (org-list-end (cdr item-beginning))))
  1116. (list (org-list-parse-list))
  1117. beg)
  1118. (unless (fboundp transform)
  1119. (error "No such transformation function %s" transform))
  1120. (setq txt (funcall transform list))
  1121. ;; Find the insertion place
  1122. (save-excursion
  1123. (goto-char (point-min))
  1124. (unless (re-search-forward
  1125. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  1126. (error "Don't know where to insert translated list"))
  1127. (goto-char (match-beginning 0))
  1128. (beginning-of-line 2)
  1129. (setq beg (point))
  1130. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1131. (error "Cannot find end of insertion region"))
  1132. (beginning-of-line 1)
  1133. (delete-region beg (point))
  1134. (goto-char beg)
  1135. (insert txt "\n"))
  1136. (message "List converted and installed at receiver location"))))
  1137. (defun org-list-to-generic (list params)
  1138. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1139. Valid parameters PARAMS are
  1140. :ustart String to start an unordered list
  1141. :uend String to end an unordered list
  1142. :ostart String to start an ordered list
  1143. :oend String to end an ordered list
  1144. :dstart String to start a descriptive list
  1145. :dend String to end a descriptive list
  1146. :dtstart String to start a descriptive term
  1147. :dtend String to end a descriptive term
  1148. :ddstart String to start a description
  1149. :ddend String to end a description
  1150. :splice When set to t, return only list body lines, don't wrap
  1151. them into :[u/o]start and :[u/o]end. Default is nil.
  1152. :istart String to start a list item
  1153. :iend String to end a list item
  1154. :isep String to separate items
  1155. :lsep String to separate sublists
  1156. :cboff String to insert for an unchecked checkbox
  1157. :cbon String to insert for a checked checkbox"
  1158. (interactive)
  1159. (let* ((p params) sublist
  1160. (splicep (plist-get p :splice))
  1161. (ostart (plist-get p :ostart))
  1162. (oend (plist-get p :oend))
  1163. (ustart (plist-get p :ustart))
  1164. (uend (plist-get p :uend))
  1165. (dstart (plist-get p :dstart))
  1166. (dend (plist-get p :dend))
  1167. (dtstart (plist-get p :dtstart))
  1168. (dtend (plist-get p :dtend))
  1169. (ddstart (plist-get p :ddstart))
  1170. (ddend (plist-get p :ddend))
  1171. (istart (plist-get p :istart))
  1172. (iend (plist-get p :iend))
  1173. (isep (plist-get p :isep))
  1174. (lsep (plist-get p :lsep))
  1175. (cbon (plist-get p :cbon))
  1176. (cboff (plist-get p :cboff)))
  1177. (let ((wrapper
  1178. (cond ((eq (car list) 'ordered)
  1179. (concat ostart "\n%s" oend "\n"))
  1180. ((eq (car list) 'unordered)
  1181. (concat ustart "\n%s" uend "\n"))
  1182. ((eq (car list) 'descriptive)
  1183. (concat dstart "\n%s" dend "\n"))))
  1184. rtn term defstart defend)
  1185. (while (setq sublist (pop list))
  1186. (cond ((symbolp sublist) nil)
  1187. ((stringp sublist)
  1188. (when (string-match "^\\(.*\\) ::" sublist)
  1189. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1190. (match-string 1 sublist))))
  1191. (setq sublist (substring sublist (1+ (length term)))))
  1192. (if (string-match "\\[CBON\\]" sublist)
  1193. (setq sublist (replace-match cbon t t sublist)))
  1194. (if (string-match "\\[CBOFF\\]" sublist)
  1195. (setq sublist (replace-match cboff t t sublist)))
  1196. (setq rtn (concat rtn istart term ddstart
  1197. sublist ddend iend isep)))
  1198. (t (setq rtn (concat rtn ;; previous list
  1199. lsep ;; list separator
  1200. (org-list-to-generic sublist p)
  1201. lsep ;; list separator
  1202. )))))
  1203. (format wrapper rtn))))
  1204. (defun org-list-to-latex (list &optional params)
  1205. "Convert LIST into a LaTeX list.
  1206. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1207. with overruling parameters for `org-list-to-generic'."
  1208. (org-list-to-generic
  1209. list
  1210. (org-combine-plists
  1211. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1212. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1213. :dstart "\\begin{description}" :dend "\\end{description}"
  1214. :dtstart "[" :dtend "]"
  1215. :ddstart "" :ddend ""
  1216. :istart "\\item " :iend ""
  1217. :isep "\n" :lsep "\n"
  1218. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1219. params)))
  1220. (defun org-list-to-html (list &optional params)
  1221. "Convert LIST into a HTML list.
  1222. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1223. with overruling parameters for `org-list-to-generic'."
  1224. (org-list-to-generic
  1225. list
  1226. (org-combine-plists
  1227. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1228. :ustart "<ul>" :uend "</ul>"
  1229. :dstart "<dl>" :dend "</dl>"
  1230. :dtstart "<dt>" :dtend "</dt>"
  1231. :ddstart "<dd>" :ddend "</dd>"
  1232. :istart "<li>" :iend "</li>"
  1233. :isep "\n" :lsep "\n"
  1234. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1235. params)))
  1236. (defun org-list-to-texinfo (list &optional params)
  1237. "Convert LIST into a Texinfo list.
  1238. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1239. with overruling parameters for `org-list-to-generic'."
  1240. (org-list-to-generic
  1241. list
  1242. (org-combine-plists
  1243. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1244. :ustart "@enumerate" :uend "@end enumerate"
  1245. :dstart "@table" :dend "@end table"
  1246. :dtstart "@item " :dtend "\n"
  1247. :ddstart "" :ddend ""
  1248. :istart "@item\n" :iend ""
  1249. :isep "\n" :lsep "\n"
  1250. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  1251. params)))
  1252. (provide 'org-list)
  1253. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1254. ;;; org-list.el ends here