org-list.el 35 KB

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