org-macs.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. ;;; org-macs.el --- Top-level definitions for Org-mode
  2. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file contains macro definitions, defsubst definitions, other
  23. ;; stuff needed for compilation and top-level forms in Org-mode, as well
  24. ;; lots of small functions that are not org-mode specific but simply
  25. ;; generally useful stuff.
  26. ;;; Code:
  27. (eval-and-compile
  28. (unless (fboundp 'declare-function)
  29. (defmacro declare-function (fn file &optional arglist fileonly)))
  30. (if (>= emacs-major-version 23)
  31. (defsubst org-char-to-string(c)
  32. "Defsubst to decode UTF-8 character values in emacs 23 and beyond."
  33. (char-to-string c))
  34. (defsubst org-char-to-string (c)
  35. "Defsubst to decode UTF-8 character values in emacs 22."
  36. (string (decode-char 'ucs c)))))
  37. (declare-function org-add-props "org-compat" (string plist &rest props))
  38. (declare-function org-string-match-p "org-compat" (&rest args))
  39. (defmacro org-with-gensyms (symbols &rest body)
  40. `(let ,(mapcar (lambda (s)
  41. `(,s (make-symbol (concat "--" (symbol-name ',s))))) symbols)
  42. ,@body))
  43. (def-edebug-spec org-with-gensyms (sexp body))
  44. (put 'org-with-gensyms 'lisp-indent-function 1)
  45. (defmacro org-called-interactively-p (&optional kind)
  46. (if (featurep 'xemacs)
  47. `(interactive-p)
  48. (if (or (> emacs-major-version 23)
  49. (and (>= emacs-major-version 23)
  50. (>= emacs-minor-version 2)))
  51. `(with-no-warnings (called-interactively-p ,kind)) ;; defined with no argument in <=23.1
  52. `(interactive-p))))
  53. (def-edebug-spec org-called-interactively-p (&optional ("quote" symbolp)))
  54. (when (and (not (fboundp 'with-silent-modifications))
  55. (or (< emacs-major-version 23)
  56. (and (= emacs-major-version 23)
  57. (< emacs-minor-version 2))))
  58. (defmacro with-silent-modifications (&rest body)
  59. `(org-unmodified ,@body))
  60. (def-edebug-spec with-silent-modifications (body)))
  61. (defmacro org-bound-and-true-p (var)
  62. "Return the value of symbol VAR if it is bound, else nil."
  63. `(and (boundp (quote ,var)) ,var))
  64. (def-edebug-spec org-bound-and-true-p (symbolp))
  65. (defun org-string-nw-p (s)
  66. "Is S a string with a non-white character?"
  67. (and (stringp s)
  68. (org-string-match-p "\\S-" s)
  69. s))
  70. (defun org-not-nil (v)
  71. "If V not nil, and also not the string \"nil\", then return V.
  72. Otherwise return nil."
  73. (and v (not (equal v "nil")) v))
  74. (defmacro org-unmodified (&rest body)
  75. "Execute body without changing `buffer-modified-p'.
  76. Also, do not record undo information."
  77. `(set-buffer-modified-p
  78. (prog1 (buffer-modified-p)
  79. (let ((buffer-undo-list t)
  80. before-change-functions after-change-functions)
  81. ,@body))))
  82. (def-edebug-spec org-unmodified (body))
  83. (defun org-substitute-posix-classes (re)
  84. "Substitute posix classes in regular expression RE."
  85. (let ((ss re))
  86. (save-match-data
  87. (while (string-match "\\[:alnum:\\]" ss)
  88. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  89. (while (string-match "\\[:word:\\]" ss)
  90. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  91. (while (string-match "\\[:alpha:\\]" ss)
  92. (setq ss (replace-match "a-zA-Z" t t ss)))
  93. (while (string-match "\\[:punct:\\]" ss)
  94. (setq ss (replace-match "\001-@[-`{-~" t t ss)))
  95. ss)))
  96. (defmacro org-re (s)
  97. "Replace posix classes in regular expression."
  98. (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
  99. (def-edebug-spec org-re (form))
  100. (defmacro org-preserve-lc (&rest body)
  101. (org-with-gensyms (line col)
  102. `(let ((,line (org-current-line))
  103. (,col (current-column)))
  104. (unwind-protect
  105. (progn ,@body)
  106. (org-goto-line ,line)
  107. (org-move-to-column ,col)))))
  108. (def-edebug-spec org-preserve-lc (body))
  109. (defmacro org-without-partial-completion (&rest body)
  110. `(if (and (boundp 'partial-completion-mode)
  111. partial-completion-mode
  112. (fboundp 'partial-completion-mode))
  113. (unwind-protect
  114. (progn
  115. (partial-completion-mode -1)
  116. ,@body)
  117. (partial-completion-mode 1))
  118. ,@body))
  119. (def-edebug-spec org-without-partial-completion (body))
  120. ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
  121. (defmacro org-maybe-intangible (props)
  122. "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
  123. In Emacs 21, invisible text is not avoided by the command loop, so the
  124. intangible property is needed to make sure point skips this text.
  125. In Emacs 22, this is not necessary. The intangible text property has
  126. led to problems with flyspell. These problems are fixed in flyspell.el,
  127. but we still avoid setting the property in Emacs 22 and later.
  128. We use a macro so that the test can happen at compilation time."
  129. (if (< emacs-major-version 22)
  130. `(append '(intangible t) ,props)
  131. props))
  132. (defmacro org-with-point-at (pom &rest body)
  133. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  134. (org-with-gensyms (mpom)
  135. `(let ((,mpom ,pom))
  136. (save-excursion
  137. (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
  138. (save-excursion
  139. (goto-char (or ,mpom (point)))
  140. ,@body)))))
  141. (def-edebug-spec org-with-point-at (form body))
  142. (put 'org-with-point-at 'lisp-indent-function 1)
  143. (defmacro org-no-warnings (&rest body)
  144. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  145. (def-edebug-spec org-no-warnings (body))
  146. (defmacro org-if-unprotected (&rest body)
  147. "Execute BODY if there is no `org-protected' text property at point."
  148. `(unless (get-text-property (point) 'org-protected)
  149. ,@body))
  150. (def-edebug-spec org-if-unprotected (body))
  151. (defmacro org-if-unprotected-1 (&rest body)
  152. "Execute BODY if there is no `org-protected' text property at point-1."
  153. `(unless (get-text-property (1- (point)) 'org-protected)
  154. ,@body))
  155. (def-edebug-spec org-if-unprotected-1 (body))
  156. (defmacro org-if-unprotected-at (pos &rest body)
  157. "Execute BODY if there is no `org-protected' text property at POS."
  158. `(unless (get-text-property ,pos 'org-protected)
  159. ,@body))
  160. (def-edebug-spec org-if-unprotected-at (form body))
  161. (put 'org-if-unprotected-at 'lisp-indent-function 1)
  162. (defun org-re-search-forward-unprotected (&rest args)
  163. "Like re-search-forward, but stop only in unprotected places."
  164. (catch 'exit
  165. (while t
  166. (unless (apply 're-search-forward args)
  167. (throw 'exit nil))
  168. (unless (get-text-property (match-beginning 0) 'org-protected)
  169. (throw 'exit (point))))))
  170. ;; FIXME: Normalize argument names
  171. (defmacro org-with-remote-undo (_buffer &rest _body)
  172. "Execute BODY while recording undo information in two buffers."
  173. (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
  174. `(let ((,cline (org-current-line))
  175. (,cmd this-command)
  176. (,buf1 (current-buffer))
  177. (,buf2 ,_buffer)
  178. (,undo1 buffer-undo-list)
  179. (,undo2 (with-current-buffer ,_buffer buffer-undo-list))
  180. ,c1 ,c2)
  181. ,@_body
  182. (when org-agenda-allow-remote-undo
  183. (setq ,c1 (org-verify-change-for-undo
  184. ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
  185. ,c2 (org-verify-change-for-undo
  186. ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
  187. (when (or ,c1 ,c2)
  188. ;; make sure there are undo boundaries
  189. (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
  190. (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
  191. ;; remember which buffer to undo
  192. (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
  193. org-agenda-undo-list))))))
  194. (def-edebug-spec org-with-remote-undo (form body))
  195. (put 'org-with-remote-undo 'lisp-indent-function 1)
  196. (defmacro org-no-read-only (&rest body)
  197. "Inhibit read-only for BODY."
  198. `(let ((inhibit-read-only t)) ,@body))
  199. (def-edebug-spec org-no-read-only (body))
  200. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  201. rear-nonsticky t mouse-map t fontified t
  202. org-emphasis t)
  203. "Properties to remove when a string without properties is wanted.")
  204. (defsubst org-match-string-no-properties (num &optional string)
  205. (if (featurep 'xemacs)
  206. (let ((s (match-string num string)))
  207. (and s (remove-text-properties 0 (length s) org-rm-props s))
  208. s)
  209. (match-string-no-properties num string)))
  210. (defsubst org-no-properties (s)
  211. (if (fboundp 'set-text-properties)
  212. (set-text-properties 0 (length s) nil s)
  213. (remove-text-properties 0 (length s) org-rm-props s))
  214. s)
  215. (defsubst org-get-alist-option (option key)
  216. (cond ((eq key t) t)
  217. ((eq option t) t)
  218. ((assoc key option) (cdr (assoc key option)))
  219. (t (cdr (assq 'default option)))))
  220. (defsubst org-check-external-command (cmd &optional use no-error)
  221. "Check if external program CMD for USE exists, error if not.
  222. When the program does exist, return its path.
  223. When it does not exist and NO-ERROR is set, return nil.
  224. Otherwise, throw an error. The optional argument USE can describe what this
  225. program is needed for, so that the error message can be more informative."
  226. (or (executable-find cmd)
  227. (if no-error
  228. nil
  229. (error "Can't find `%s'%s" cmd
  230. (if use (format " (%s)" use) "")))))
  231. (defsubst org-inhibit-invisibility ()
  232. "Modified `buffer-invisibility-spec' for Emacs 21.
  233. Some ops with invisible text do not work correctly on Emacs 21. For these
  234. we turn off invisibility temporarily. Use this in a `let' form."
  235. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  236. (defsubst org-set-local (var value)
  237. "Make VAR local in current buffer and set it to VALUE."
  238. (set (make-local-variable var) value))
  239. (defsubst org-last (list)
  240. "Return the last element of LIST."
  241. (car (last list)))
  242. (defun org-let (list &rest body)
  243. (eval (cons 'let (cons list body))))
  244. (put 'org-let 'lisp-indent-function 1)
  245. (defun org-let2 (list1 list2 &rest body)
  246. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  247. (put 'org-let2 'lisp-indent-function 2)
  248. (defsubst org-call-with-arg (command arg)
  249. "Call COMMAND interactively, but pretend prefix arg was ARG."
  250. (let ((current-prefix-arg arg)) (call-interactively command)))
  251. (defsubst org-current-line (&optional pos)
  252. (save-excursion
  253. (and pos (goto-char pos))
  254. ;; works also in narrowed buffer, because we start at 1, not point-min
  255. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  256. (defsubst org-goto-line (N)
  257. (save-restriction
  258. (widen)
  259. (goto-char (point-min))
  260. (forward-line (1- N))))
  261. (defsubst org-current-line-string (&optional to-here)
  262. (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
  263. (defsubst org-pos-in-match-range (pos n)
  264. (and (match-beginning n)
  265. (<= (match-beginning n) pos)
  266. (>= (match-end n) pos)))
  267. (defun org-autoload (file functions)
  268. "Establish autoload for all FUNCTIONS in FILE, if not bound already."
  269. (let ((d (format "Documentation will be available after `%s.el' is loaded."
  270. file))
  271. f)
  272. (while (setq f (pop functions))
  273. (or (fboundp f) (autoload f file d t)))))
  274. (defun org-match-line (re)
  275. "Looking-at at the beginning of the current line."
  276. (save-excursion
  277. (goto-char (point-at-bol))
  278. (looking-at re)))
  279. (defun org-plist-delete (plist property)
  280. "Delete PROPERTY from PLIST.
  281. This is in contrast to merely setting it to 0."
  282. (let (p)
  283. (while plist
  284. (if (not (eq property (car plist)))
  285. (setq p (plist-put p (car plist) (nth 1 plist))))
  286. (setq plist (cddr plist)))
  287. p))
  288. (defun org-replace-match-keep-properties (newtext &optional fixedcase
  289. literal string)
  290. "Like `replace-match', but add the text properties found original text."
  291. (setq newtext (org-add-props newtext (text-properties-at
  292. (match-beginning 0) string)))
  293. (replace-match newtext fixedcase literal string))
  294. (defmacro org-save-outline-visibility (use-markers &rest body)
  295. "Save and restore outline visibility around BODY.
  296. If USE-MARKERS is non-nil, use markers for the positions.
  297. This means that the buffer may change while running BODY,
  298. but it also means that the buffer should stay alive
  299. during the operation, because otherwise all these markers will
  300. point nowhere."
  301. (declare (indent 1))
  302. (org-with-gensyms (data rtn)
  303. `(let ((,data (org-outline-overlay-data ,use-markers))
  304. ,rtn)
  305. (unwind-protect
  306. (progn
  307. (setq ,rtn (progn ,@body))
  308. (org-set-outline-overlay-data ,data))
  309. (when ,use-markers
  310. (mapc (lambda (c)
  311. (and (markerp (car c)) (move-marker (car c) nil))
  312. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  313. ,data)))
  314. ,rtn)))
  315. (def-edebug-spec org-save-outline-visibility (form body))
  316. (defmacro org-with-wide-buffer (&rest body)
  317. "Execute body while temporarily widening the buffer."
  318. `(save-excursion
  319. (save-restriction
  320. (widen)
  321. ,@body)))
  322. (def-edebug-spec org-with-wide-buffer (body))
  323. (defmacro org-with-limited-levels (&rest body)
  324. "Execute BODY with limited number of outline levels."
  325. `(let* ((org-outline-regexp (org-get-limited-outline-regexp))
  326. (outline-regexp org-outline-regexp)
  327. (org-outline-regexp-at-bol (concat "^" org-outline-regexp)))
  328. ,@body))
  329. (def-edebug-spec org-with-limited-levels (body))
  330. (defvar org-outline-regexp) ; defined in org.el
  331. (defvar org-odd-levels-only) ; defined in org.el
  332. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  333. (defun org-get-limited-outline-regexp ()
  334. "Return outline-regexp with limited number of levels.
  335. The number of levels is controlled by `org-inlinetask-min-level'"
  336. (if (or (not (derived-mode-p 'org-mode)) (not (featurep 'org-inlinetask)))
  337. org-outline-regexp
  338. (let* ((limit-level (1- org-inlinetask-min-level))
  339. (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
  340. (format "\\*\\{1,%d\\} " nstars))))
  341. (defun org-format-seconds (string seconds)
  342. "Compatibility function replacing format-seconds"
  343. (if (fboundp 'format-seconds)
  344. (format-seconds string seconds)
  345. (format-time-string string (seconds-to-time seconds))))
  346. (defmacro org-eval-in-environment (environment form)
  347. `(eval (list 'let ,environment ',form)))
  348. (def-edebug-spec org-eval-in-environment (form form))
  349. (put 'org-eval-in-environment 'lisp-indent-function 1)
  350. (defun org-make-parameter-alist (flat)
  351. "Return alist based on FLAT.
  352. FLAT is a list with alternating symbol names and values. The
  353. returned alist is a list of lists with the symbol name in car and
  354. the value in cdr."
  355. (when flat
  356. (cons (list (car flat) (cadr flat))
  357. (org-make-parameter-alist (cddr flat)))))
  358. (provide 'org-macs)
  359. ;;; org-macs.el ends here