From: Nicolas Goaziou <n.goaziou@gmail.com>
To: Bastien <bzg@altern.org>
Cc: Org Mode List <emacs-orgmode@gnu.org>,
Carsten Dominik <carsten.dominik@gmail.com>
Subject: Re: [RFC] Make QUOTE an export keyword instead of an element type
Date: Mon, 03 Feb 2014 23:41:14 +0100 [thread overview]
Message-ID: <87vbwv3jlh.fsf@gmail.com> (raw)
In-Reply-To: <87zjm82o9t.fsf@gmail.com> (Nicolas Goaziou's message of "Mon, 03 Feb 2014 16:45:34 +0100")
Nicolas Goaziou <n.goaziou@gmail.com> 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
next prev parent reply other threads:[~2014-02-03 22:41 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-26 13:57 [RFC] Make QUOTE an export keyword instead of an element type Nicolas Goaziou
2014-01-26 15:41 ` Thomas S. Dye
2014-01-26 16:58 ` Rasmus
2014-01-26 21:07 ` Nick Dokos
2014-01-26 21:42 ` Samuel Wales
2014-01-26 22:03 ` Bastien
2014-01-26 23:42 ` Nick Dokos
2014-01-27 1:29 ` Rasmus
2014-01-27 10:06 ` Sebastien Vauban
2014-01-27 10:20 ` Thorsten Jolitz
2014-01-27 5:46 ` Carsten Dominik
2014-01-27 11:12 ` Nicolas Goaziou
2014-01-27 11:21 ` Bastien
2014-01-27 20:45 ` Nicolas Goaziou
2014-01-28 8:52 ` Bastien
2014-01-31 15:44 ` Bastien
2014-02-01 13:24 ` Nicolas Goaziou
2014-02-01 22:43 ` Bastien
2014-02-02 21:40 ` Nicolas Goaziou
2014-02-02 22:20 ` Bastien
2014-02-03 11:20 ` Nicolas Goaziou
2014-02-03 14:59 ` Bastien
2014-02-03 15:08 ` Nicolas Goaziou
2014-02-03 15:36 ` Bastien
2014-02-03 15:45 ` Nicolas Goaziou
2014-02-03 22:41 ` Nicolas Goaziou [this message]
2014-02-05 10:12 ` Bastien
2014-02-06 17:56 ` Nicolas Goaziou
2014-02-06 22:00 ` Bastien
2014-02-07 8:51 ` Nicolas Goaziou
2014-02-07 9:10 ` Bastien
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87vbwv3jlh.fsf@gmail.com \
--to=n.goaziou@gmail.com \
--cc=bzg@altern.org \
--cc=carsten.dominik@gmail.com \
--cc=emacs-orgmode@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).