From 7739e8b5f7d81319b647043c2a9f6d9ef7eb813b Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Tue, 18 Dec 2012 19:32:40 +0100 Subject: [PATCH 1/4] Small refactoring * lisp/org.el (org-fill-paragraph): Small refactoring. --- lisp/org.el | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 93ec8d6ed..362cb3b72 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -20993,22 +20993,18 @@ width for filling. For convenience, when point is at a plain list, an item or a footnote definition, try to fill the first paragraph within." (interactive) - (cond ;; First ensure filling in correct in message-mode - ((and (derived-mode-p 'message-mode) - (or (not (message-in-body-p)) - (save-excursion (move-beginning-of-line 1) - (looking-at message-cite-prefix-regexp)))) - (let ((fill-paragraph-function - (cadadr (assoc 'fill-paragraph-function org-fb-vars))) - (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars))) - (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars))) - (paragraph-separate - (cadadr (assoc 'paragraph-separate org-fb-vars)))) - (fill-paragraph nil))) - ;; Correct filling in source block - ((org-in-src-block-p) - (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q"))) - (t + (if (and (derived-mode-p 'message-mode) + (or (not (message-in-body-p)) + (save-excursion (move-beginning-of-line 1) + (looking-at message-cite-prefix-regexp)))) + ;; First ensure filling is correct in message-mode. + (let ((fill-paragraph-function + (cadadr (assoc 'fill-paragraph-function org-fb-vars))) + (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars))) + (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars))) + (paragraph-separate + (cadadr (assoc 'paragraph-separate org-fb-vars)))) + (fill-paragraph nil)) (save-excursion ;; Move to end of line in order to get the first paragraph ;; within a plain list or a footnote definition. @@ -21018,6 +21014,8 @@ a footnote definition, try to fill the first paragraph within." ;; the buffer. In that case, ignore filling. (if (< (point) (org-element-property :begin element)) t (case (org-element-type element) + ;; Use major mode filling function is src blocks. + (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q"))) ;; Align Org tables, leave table.el tables as-is. (table-row (org-table-align) t) (table @@ -21094,7 +21092,7 @@ a footnote definition, try to fill the first paragraph within." ;; Fill comments. (comment (fill-comment-paragraph justify)) ;; Ignore every other element. - (otherwise t)))))))) + (otherwise t))))))) (defun org-auto-fill-function () "Auto-fill function." From bc4351ce0d4a4995c83f1af6778fb5dd7bb9ae89 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Tue, 18 Dec 2012 21:52:06 +0100 Subject: [PATCH 2/4] org-element: Fix parsing of links expanded from an abbreviation * lisp/org-element.el (org-element-object-variables): New variable. (org-element-parse-secondary-string): Copy some buffer-local variables to the temporary buffer created to parse the string so links can still be properly expanded. (org-element-link-parser): Link expansion and translation are applied transparently for the parser. --- lisp/org-element.el | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index 8030546be..37ad2aeb4 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -358,6 +358,11 @@ still has an entry since one of its properties (`:title') does.") (footnote-reference . :inline-definition)) "Alist between element types and location of secondary value.") +(defconst org-element-object-variables '(org-link-abbrev-alist-local) + "List of buffer-local variables used when parsing objects. +These variables are copied to the temporary buffer created by +`org-export-secondary-string'.") + ;;; Accessors and Setters @@ -2998,10 +3003,10 @@ Assume point is at the beginning of the link." contents-end (match-end 3) link-end (match-end 0) ;; RAW-LINK is the original link. - raw-link (org-match-string-no-properties 1) - link (org-translate-link - (org-link-expand-abbrev - (org-link-unescape raw-link)))) + raw-link (org-translate-link + (org-link-expand-abbrev + (org-match-string-no-properties 1))) + link (org-link-unescape raw-link)) ;; Determine TYPE of link and set PATH accordingly. (cond ;; File type. @@ -3930,14 +3935,22 @@ looked after. Optional argument PARENT, when non-nil, is the element or object containing the secondary string. It is used to set correctly `:parent' property within the string." - (with-temp-buffer - (insert string) - (let ((secondary (org-element--parse-objects - (point-min) (point-max) nil restriction))) - (when parent - (mapc (lambda (obj) (org-element-put-property obj :parent parent)) - secondary)) - secondary))) + ;; Copy buffer-local variables listed in + ;; `org-element-object-variables' into temporary buffer. This is + ;; required since object parsing is dependent on these variables. + (let ((pairs (delq nil (mapcar (lambda (var) + (when (boundp var) + (cons var (symbol-value var)))) + org-element-object-variables)))) + (with-temp-buffer + (mapc (lambda (pair) (org-set-local (car pair) (cdr pair))) pairs) + (insert string) + (let ((secondary (org-element--parse-objects + (point-min) (point-max) nil restriction))) + (when parent + (mapc (lambda (obj) (org-element-put-property obj :parent parent)) + secondary)) + secondary)))) (defun org-element-map (data types fun &optional info first-match no-recursion with-affiliated) From f1bb17549ba9dadd3160b48017df9cdd81e52a11 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Tue, 18 Dec 2012 22:16:01 +0100 Subject: [PATCH 3/4] org-element: Add tests * testing/lisp/test-org-element.el: Add tests. --- testing/lisp/test-org-element.el | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index c1dd34951..2f6988c05 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -1260,7 +1260,25 @@ e^{i\\pi}+1=0 ;; 4. Angular link. (should (org-test-with-temp-text "A link: " - (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))) + (org-element-map (org-element-parse-buffer) 'link 'identity nil t))) + ;; Link abbreviation. + (should + (equal "http" + (org-test-with-temp-text + "#+LINK: orgmode http://www.orgmode.org/\n[[orgmode:#docs]]" + (progn (org-mode-restart) + (goto-char (point-max)) + (org-element-property :type (org-element-context)))))) + ;; Link abbreviation in a secondary string. + (should + (equal "http" + (org-test-with-temp-text + "#+LINK: orgmode http://www.orgmode.org/\n* H [[orgmode:#docs]]" + (progn (org-mode-restart) + (org-element-map + (org-element-parse-buffer) 'link + (lambda (link) (org-element-property :type link)) + nil t nil t)))))) ;;;; Macro From 27d7d0ea174c3d3975e4ac2e8a0ecfac9401ba61 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Tue, 18 Dec 2012 22:33:02 +0100 Subject: [PATCH 4/4] org-export: Remove NOEXPAND optional argument from `org-export-as' * contrib/lisp/org-export.el (org-export-options-alist): Fix docstring. (org-export-as): Remove NOEXPAND argument. (org-export-to-buffer, org-export-to-file): Apply argument removal. This argument is not needed since a function removing macros, babel code and include keywords can be added to `org-export-before-processing-hook'. --- contrib/lisp/org-export.el | 68 +++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/contrib/lisp/org-export.el b/contrib/lisp/org-export.el index 00eaad0ed..50d8e4407 100644 --- a/contrib/lisp/org-export.el +++ b/contrib/lisp/org-export.el @@ -153,7 +153,7 @@ KEYWORD is a string representing a buffer keyword, or nil. Each property). OPTION is a string that could be found in an #+OPTIONS: line. DEFAULT is the default value for the property. -BEHAVIOUR determine how Org should handle multiple keywords for +BEHAVIOUR determines how Org should handle multiple keywords for the same property. It is a symbol among: nil Keep old value and discard the new one. t Replace old value with the new one. @@ -163,7 +163,8 @@ BEHAVIOUR determine how Org should handle multiple keywords for `split' Split values at white spaces, and cons them to the previous list. -KEYWORD and OPTION have precedence over DEFAULT. +Values set through KEYWORD and OPTION have precedence over +DEFAULT. All these properties should be back-end agnostic. Back-end specific properties are set through `org-export-define-backend'. @@ -2591,7 +2592,7 @@ Return the updated communication channel." ;; associated to the file, that is before parsing. (defun org-export-as - (backend &optional subtreep visible-only body-only ext-plist noexpand) + (backend &optional subtreep visible-only body-only ext-plist) "Transcode current Org buffer into BACKEND code. If narrowing is active in the current buffer, only transcode its @@ -2613,9 +2614,6 @@ Optional argument EXT-PLIST, when provided, is a property list with external parameters overriding Org default settings, but still inferior to file-local settings. -Optional argument NOEXPAND, when non-nil, prevents included files -to be expanded and Babel code to be executed. - Return code as a string." ;; Barf if BACKEND isn't registered. (org-export-barf-if-invalid-backend backend) @@ -2634,20 +2632,19 @@ Return code as a string." ;; Initialize communication channel with original buffer ;; attributes, unavailable in its copy. (let ((info (org-export--get-buffer-attributes)) tree) + ;; Update communication channel and get parse tree. Buffer + ;; isn't parsed directly. Instead, a temporary copy is + ;; created, where include keywords, macros are expanded and + ;; code blocks are evaluated. (org-export-with-buffer-copy ;; Run first hook with current back-end as argument. (run-hook-with-args 'org-export-before-processing-hook backend) - ;; Update communication channel and get parse tree. Buffer - ;; isn't parsed directly. Instead, a temporary copy is - ;; created, where include keywords, macros are expanded and - ;; code blocks are evaluated. - (unless noexpand - (org-export-expand-include-keyword) - ;; Update macro templates since #+INCLUDE keywords might - ;; have added some new ones. - (org-macro-initialize-templates) - (org-macro-replace-all org-macro-templates) - (org-export-execute-babel-code)) + (org-export-expand-include-keyword) + ;; Update macro templates since #+INCLUDE keywords might have + ;; added some new ones. + (org-macro-initialize-templates) + (org-macro-replace-all org-macro-templates) + (org-export-execute-babel-code) ;; Update radio targets since keyword inclusion might have ;; added some more. (org-update-radio-target-regexp) @@ -2664,16 +2661,15 @@ Return code as a string." ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done ;; once regular macros have been expanded, since document ;; keywords may contain one of them. - (unless noexpand - (org-macro-replace-all - (list (cons "author" - (org-element-interpret-data (plist-get info :author))) - (cons "date" - (org-element-interpret-data (plist-get info :date))) - ;; EMAIL is not a parsed keyword: store it as-is. - (cons "email" (or (plist-get info :email) "")) - (cons "title" - (org-element-interpret-data (plist-get info :title)))))) + (org-macro-replace-all + (list (cons "author" + (org-element-interpret-data (plist-get info :author))) + (cons "date" + (org-element-interpret-data (plist-get info :date))) + ;; EMAIL is not a parsed keyword: store it as-is. + (cons "email" (or (plist-get info :email) "")) + (cons "title" + (org-element-interpret-data (plist-get info :title))))) ;; Eventually parse buffer. Call parse-tree filters to get ;; the final tree. (setq tree @@ -2705,7 +2701,7 @@ Return code as a string." output))))) (defun org-export-to-buffer - (backend buffer &optional subtreep visible-only body-only ext-plist noexpand) + (backend buffer &optional subtreep visible-only body-only ext-plist) "Call `org-export-as' with output to a specified buffer. BACKEND is the back-end used for transcoding, as a symbol. @@ -2713,13 +2709,12 @@ BACKEND is the back-end used for transcoding, as a symbol. BUFFER is the output buffer. If it already exists, it will be erased first, otherwise, it will be created. -Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST -and NOEXPAND are similar to those used in `org-export-as', which +Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and +EXT-PLIST are similar to those used in `org-export-as', which see. Return buffer." - (let ((out (org-export-as - backend subtreep visible-only body-only ext-plist noexpand)) + (let ((out (org-export-as backend subtreep visible-only body-only ext-plist)) (buffer (get-buffer-create buffer))) (with-current-buffer buffer (erase-buffer) @@ -2728,14 +2723,14 @@ Return buffer." buffer)) (defun org-export-to-file - (backend file &optional subtreep visible-only body-only ext-plist noexpand) + (backend file &optional subtreep visible-only body-only ext-plist) "Call `org-export-as' with output to a specified file. BACKEND is the back-end used for transcoding, as a symbol. FILE is the name of the output file, as a string. -Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST -and NOEXPAND are similar to those used in `org-export-as', which +Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and +EXT-PLIST are similar to those used in `org-export-as', which see. Return output file's name." @@ -2743,8 +2738,7 @@ Return output file's name." ;; we'd rather avoid needless transcoding of parse tree. (unless (file-writable-p file) (error "Output file not writable")) ;; Insert contents to a temporary buffer and write it to FILE. - (let ((out (org-export-as - backend subtreep visible-only body-only ext-plist noexpand))) + (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))) (with-temp-buffer (insert out) (let ((coding-system-for-write org-export-coding-system))