From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rasmus Subject: Re: [patch] better(?) indention for cdlatex-environment Date: Tue, 17 Feb 2015 22:19:31 +0100 Message-ID: <877fvg44nw.fsf@gmx.us> References: <87386e0zuy.fsf@gmx.us> <87wq3pwg29.fsf@nicolasgoaziou.fr> <87lhk48z9x.fsf@gmx.us> <87fvacw2jv.fsf@nicolasgoaziou.fr> <87mw4kyq2e.fsf@gmx.us> <87fva9tqdj.fsf@nicolasgoaziou.fr> <873869h0cd.fsf@gmx.us> <87twypgwt5.fsf@gmx.us> <87pp9cnq9w.fsf@nicolasgoaziou.fr> <874mqoc9zf.fsf@gmx.us> <87lhjzo611.fsf@nicolasgoaziou.fr> <87wq3hgyie.fsf@gmx.us> <87egpox6o3.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:57339) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YNpYR-0003bA-DZ for emacs-orgmode@gnu.org; Tue, 17 Feb 2015 16:19:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YNpYM-0006jY-R1 for emacs-orgmode@gnu.org; Tue, 17 Feb 2015 16:19:39 -0500 Received: from mout.gmx.net ([212.227.17.21]:60997) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YNpYM-0006jE-Gm for emacs-orgmode@gnu.org; Tue, 17 Feb 2015 16:19:34 -0500 Received: from W530 ([46.166.186.221]) by mail.gmx.com (mrgmx102) with ESMTPSA (Nemesis) id 0M4kfR-1XeO2i0zvR-00z0WF for ; Tue, 17 Feb 2015 22:19:33 +0100 In-Reply-To: <87egpox6o3.fsf@nicolasgoaziou.fr> (Nicolas Goaziou's message of "Tue, 17 Feb 2015 09:51:08 +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: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Nicolas Goaziou writes: > I don't think you need `org-element-at-point' at all. You already have > BEG and END markers available. I didn't test it, but this should be > enough to indent the environment. Perhaps there are clever ways to figure it out. I say there are too many dynamics and "fixes" in the code to get cdlatex-environment to work already. Just consider this example where | is cursor - foo | bar Midway through, when ENV is reinserted, but before indentation the end-marker will be *after* bar which is a line after \end{ENV}... I think playing the "deterministic" card is non-trivial to get right in all cases and org-cdlatex-environment-indent is already longer than cdlatex-environment. > Also you shouldn't apply `org-indent-to-column' when line is empty. I don't see why not, but OK... Anyway it reminded me that I missed "re-implementing" one feature of cdlatex, namely moving the cursor to the right place. I refind this place do it by inserting a funny string and replacing it. A poor man's marker, I guess... =E2=80=94Rasmus --=20 I hear there's rumors on the, uh, Internets. . . --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-org.el-Change-indention-for-cdlatex-environments.patch >From 7381526412dc6e36e2c5c1b4e92cb102dda8c965 Mon Sep 17 00:00:00 2001 From: rasmus Date: Tue, 10 Feb 2015 12:02:59 +0100 Subject: [PATCH] org.el: Change indention for cdlatex environments * org.el (org-cdlatex-environment-indent): Use different indent algorithm based on content above the new latex-environment. --- lisp/org.el | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 4f047b2..6de53f1 100755 --- a/lisp/org.el +++ b/lisp/org.el @@ -18645,12 +18645,54 @@ Revert to the normal definition outside of these fragments." (call-interactively (key-binding (vector last-input-event)))))) (defun org-cdlatex-environment-indent (&optional environment item) - "Execute `cdlatex-environment' and indent the inserted environment." + "Execute `cdlatex-environment' and indent the inserted environment. + +ENVIRONMENT and ITEM are passed to `cdlatex-environment'. + +The inserted environment is indented to current indentation +unless point is at the beginning of the line, in which the +environment remains unintended." (interactive) - (cdlatex-environment environment item) - (let ((element (org-element-at-point))) - (org-indent-region (org-element-property :begin element) - (org-element-property :end element)))) + ;; cdlatex-environment always return nil. Therefore, capture output + ;; first and determine if an environment was selected. + (let* ((beg (point-marker)) + (end (copy-marker (point) t)) + (env (org-trim + (or (progn (ignore-errors (cdlatex-environment environment item)) + (delete-and-extract-region beg end)) + "")))) + (when (org-string-nw-p env) + ;; Get indentation of next line unless at column 0. + (let ((ind (if (bolp) 0 + (save-excursion + (org-return-indent) + (prog1 (org-get-indentation) + (when (and (skip-chars-forward " \t") (eolp)) + (delete-region beg (point))))))) + (bol (and (skip-chars-backward " \t") (bolp)))) + ;; Insert a newline before environment unless at column zero + ;; to "escape" the current line. Insert a newline if + ;; something is one the same line as \end{ENVIRONMENT}. + (insert (concat (unless bol "\n") + env + (and (skip-chars-forward " \t") (not (eolp)) "\n"))) + (unless (zerop ind) + (let* ((element (org-element-at-point)) + (elm-beg (org-element-property :begin element)) + (elm-end (copy-marker + (save-excursion + (goto-char (org-element-property :end element)) + (skip-chars-backward " \t\n\r") + (point))))) + (save-excursion + (goto-char elm-beg) + (beginning-of-line) + (while (<= (point) elm-end) + (org-indent-to-column ind) + (forward-line))) + (set-marker elm-end nil))))) + (set-marker beg nil) + (set-marker end nil))) ;;;; LaTeX fragments -- 2.3.0 --=-=-=--