From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: [RFC] Make QUOTE an export keyword instead of an element type Date: Mon, 03 Feb 2014 23:41:14 +0100 Message-ID: <87vbwv3jlh.fsf@gmail.com> References: <87zjmiyhfd.fsf@gmail.com> <87mwiijt9g.fsf@bzg.ath.cx> <6689A23E-E318-4146-8641-BD974CB64C73@gmail.com> <87fvo9y8z7.fsf@gmail.com> <87sis9smaf.fsf@bzg.ath.cx> <87bnyxxig6.fsf@gmail.com> <87lhxw9mwl.fsf@bzg.ath.cx> <87wqhfq83t.fsf@gmail.com> <87ob2qtpwh.fsf@bzg.ath.cx> <87ppn542hj.fsf@gmail.com> <878utt17j1.fsf@bzg.ath.cx> <878uts4f3y.fsf@gmail.com> <87r47kqm2n.fsf@bzg.ath.cx> <874n4g44jy.fsf@gmail.com> <87d2j4qkbs.fsf@bzg.ath.cx> <87zjm82o9t.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:56637) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WASCN-0005je-AU for emacs-orgmode@gnu.org; Mon, 03 Feb 2014 17:41:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WASCE-00065A-T1 for emacs-orgmode@gnu.org; Mon, 03 Feb 2014 17:41:03 -0500 Received: from mail-we0-x22a.google.com ([2a00:1450:400c:c03::22a]:40240) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WASCE-000655-Hr for emacs-orgmode@gnu.org; Mon, 03 Feb 2014 17:40:54 -0500 Received: by mail-we0-f170.google.com with SMTP id w62so3382255wes.29 for ; Mon, 03 Feb 2014 14:40:53 -0800 (PST) In-Reply-To: <87zjm82o9t.fsf@gmail.com> (Nicolas Goaziou's message of "Mon, 03 Feb 2014 16:45:34 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Bastien Cc: Org Mode List , Carsten Dominik Nicolas Goaziou writes: > It is clearer now, thank you. I'll send a patch later on the ML. Here is the function. If it is good enough, I'll add tests and wrap it up in a patch. #+begin_src emacs-lisp (defun org-toggle-fixed-width () "Toggle fixed-width markup. Add or remove fixed-width markup on current line, whenever it makes sense. Return an error otherwise. If a region is active and if it contains only fixed-width areas or blank lines, remove all fixed-width markup in it. If the region contains anything else, convert all non-fixed-width lines to fixed-width ones." (interactive) (if (not (org-region-active-p)) (if (org-at-heading-p) (user-error "Cannot insert a fixed-width line here") (beginning-of-line) (let* ((element (org-element-at-point)) (type (org-element-type element))) (cond ((and (eq type 'fixed-width) (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")) (replace-match "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1))) ((and (eq type 'paragraph) (<= (org-element-property :post-affiliated element) (point))) (skip-chars-forward " \t") (insert ": ")) ((and (org-looking-at-p "[ \t]*$") (or (memq type '(headline inlinetask)) (save-excursion (skip-chars-forward " \r\t\n") (<= (org-element-property :end element) (point))))) (delete-region (point) (line-end-position)) (org-indent-line) (insert ": ")) (t (user-error "Cannot insert a fixed-width line here"))))) (let* ((begin (save-excursion (goto-char (region-beginning)) (line-beginning-position))) (end (copy-marker (save-excursion (goto-char (region-end)) (unless (eolp) (beginning-of-line)) (if (save-excursion (re-search-backward "\\S-" begin t)) (progn (skip-chars-backward " \r\t\n") (point)) (point))))) (all-fixed-width-p (catch 'not-all-p (save-excursion (goto-char begin) (skip-chars-forward " \r\t\n") (when (eobp) (throw 'not-all-p nil)) (while (< (point) end) (let ((element (org-element-at-point))) (if (eq (org-element-type element) 'fixed-width) (goto-char (org-element-property :end element)) (throw 'not-all-p nil)))) t)))) (if all-fixed-width-p (save-excursion (goto-char begin) (while (< (point) end) (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)") (replace-match "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1))) (forward-line))) (let ((min-ind (point-max))) ;; Find minimum indentation across all lines. (save-excursion (goto-char begin) (if (not (save-excursion (re-search-forward "\\S-" end t))) (setq min-ind 0) (catch 'zerop (while (< (point) end) (unless (org-looking-at-p "[ \t]*$") (let ((ind (org-get-indentation))) (setq min-ind (min min-ind ind)) (when (zerop ind) (throw 'zerop t)))) (forward-line))))) ;; Loop over all lines and add fixed-width markup everywhere ;; but in fixed-width lines. (save-excursion (goto-char begin) (while (< (point) end) (cond ((org-at-heading-p) (insert ": ") (forward-line) (while (and (< (point) end) (org-looking-at-p "[ \t]*$")) (insert ":") (forward-line))) ((org-looking-at-p "[ \t]*:\\( \\|$\\)") (let* ((element (org-element-at-point)) (element-end (org-element-property :end element))) (if (eq (org-element-type element) 'fixed-width) (progn (goto-char element-end) (skip-chars-backward " \r\t\n") (forward-line)) (let ((limit (min end element-end))) (while (< (point) limit) (let ((buffer-invisibility-spec nil)) (org-move-to-column min-ind t)) (insert ": ") (forward-line)))))) (t (let ((buffer-invisibility-spec nil)) (org-move-to-column min-ind t)) (insert ": ") (forward-line))))))) (set-marker end nil)))) #+end_src -- Nicolas Goaziou