emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Bastien <bzg@altern.org>
To: Torsten Wagner <torsten.wagner@gmail.com>
Cc: Org Mode Mailing List <emacs-orgmode@gnu.org>
Subject: Re: Problem: Moving rows in a table changes vsum start and end
Date: Fri, 28 Sep 2012 18:56:33 +0200	[thread overview]
Message-ID: <87sja2qe66.fsf@bzg.ath.cx> (raw)
In-Reply-To: <CAPaq-gPsKG=da4PBi8sUuWY8d0M-yUh8FNfuY4oxEfoxMXyeVA@mail.gmail.com> (Torsten Wagner's message of "Sat, 29 Sep 2012 01:17:23 +0900")

Hi Torsten,

Torsten Wagner <torsten.wagner@gmail.com> writes:

> ok,  I think it should be in org-table.el
> and the function in question might be org-table-fix-formulas


> I added
> (defface org-table-formular-change-face
>    '((t (:background "red")))
>    "Used parts of tabe formulars which change by row and column moving
> operations.")

Faces are for visual clues that are here to stay, for transitory
highlighting, you'd better use overlays.  (See "Overlays" in the
Elisp manual.)

> to define a new face for changes in the formular.
> Then I looked around how to do the face changing. By that I noticed,
> org-mode uses its own set of functions (org-table-highlight-rectangle,
> org-table-remove-rectangle-highlight).
> Searching more around I figured out that emacs already comes with a
> function for highlighting.
> http://www.gnu.org/software/emacs/manual/html_node/emacs/Highlight-Interactively.html#Highlight-Interactively
> Which makes me wonder why org-mode don't use that (maybe historical
> reasons, the above function is not that old).

I haven't closely looked at hi-lock.el but I think this is more for
ad-hoc highlighting rather than for mode highlighting.  I guess it's 
faster to set the face text property than to use these functions.

> From this there are mainly two functions of interest
> highlight-regexp
> unhighlight-regexp (btw. a not very emacs conform naming)
> they take a regular expression as input and optional the face to be set.
> It works great in interactive mode.
> So I was looking how to modify the function org-table-fix-formulas
> using the above commands
> I ended up with a very small change
> (defun org-table-fix-formulas (key replace &optional limit delta remove)
>   "Modify the equations after the table structure has been edited.
> KEY is \"@\" or \"$\".  REPLACE is an alist of numbers to replace.
> For all numbers larger than LIMIT, shift them by DELTA."
>   (save-excursion
>     (goto-char (org-table-end))
>     (when (let ((case-fold-search t)) (looking-at "[ \t]*#\\+tblfm:"))
>       (let ((re (concat key "\\([0-9]+\\)"))
> 	    (re2
> 	     (when remove
> 	       (if (or (equal key "$") (equal key "$LR"))
> 		   (format "\\(@[0-9]+\\)?%s%d=.*?\\(::\\|$\\)"
> 			   (regexp-quote key) remove)
> 		 (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
> 	    s n a)
> 	(when remove
> 	  (while (re-search-forward re2 (point-at-eol) t)
> 	    (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
> 	      (if (equal (char-before (match-beginning 0)) ?.)
> 		  (error "Change makes TBLFM term %s invalid.  Use undo to recover."
> 			 (match-string 0))
> 		(replace-match "")))))
> 	(while (re-search-forward re (point-at-eol) t)
> 	  (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
> 	    (setq s (match-string 1) n (string-to-number s))
> 	    (cond
> 	     ((setq a (assoc s replace))
> 	      (replace-match (concat key (cdr a)) t t))
> 	     ((and limit (> n limit))
> 	      (replace-match (concat key (int-to-string (+ n delta)))
> 	      t t)))
> ;Added the single line below
> 	    (highlight-regexp re 'org-table-formular-change-face)
> ; really only the above line
> 	    ))))))

This cannot work because `re' is the regular expression you are
searching for -- thus the string matched by the re will be replaced.

What we want is to add an overlay on the part of the formula that
has been updated, namely the string in (replace-match STRING ...)

> However, it  does not work.
> Defining the face and using the above line works correct in the scratch buffer,
> However, calling the function within org-mode (moving rows in a table)
> I do not get an error message and do not get a highlight...
> I made sure my modified version but no luck yet.

Another thing(y): I'd like the overlay to be transient... 
So maybe overlaying the replacement with a timer to fade it away
is the way to go.  If you didn't already, you can have a look at
how `org-table-edit-formulas' does its job.

> Furthermore, you might like to check about the highlight functions.
> You might be able to simplify some of the org-table.el code by
> relying on those functions instead of defining own functions. If we
> get this started, I dream of
> slightly different face different parts of org-tables:
> * cells base on a formula and not been updated, despite changes were
> made in the table,
> * cells base on a formula but good overwritten by the user (this is
> another dangerous situation I found myself in)
> * highlight the formulas for the cell in which the pointer is located
> (kind of reveres of the current fomular  editor highlighting)
> * highlight changes after recalculation of an table
> As for the last point. I also found the minor mode "highlight-changes-mode"
> This is a great mode and calling it before starting to manipulate a
> org-table, already ticks some of the above points. Please try if you
> are not aware of it (create a table with some forms, call
> highlight-changes-mode, do some operations on the table).

Thanks for the tip!  I'm aware of it but don't use it that much in
tables because a simple M-<right> will highlight the whole table...
but yes, this is one of the great libraries in Emacs.  (Did I already
acknowledge Emacs is great?)

If you want to dig further for the temporary overlay, you may have
a look at this library:




  reply	other threads:[~2012-09-28 16:56 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-28  8:37 Torsten Wagner
2012-09-28  8:46 ` Bastien
2012-09-28 12:32   ` Torsten Wagner
2012-09-28 13:53     ` Bastien
2012-09-28 16:17       ` Torsten Wagner
2012-09-28 16:56         ` Bastien [this message]
2012-09-28 16:00     ` Bastien
2012-09-28 16:18       ` Torsten Wagner
2012-09-29  9:17       ` Carsten Dominik
2012-09-29  9:57         ` Achim Gratz
2012-09-29  9:59           ` Bastien
2012-09-29 10:03             ` Achim Gratz
2012-09-29 10:01           ` Carsten Dominik
2012-09-29 10:04             ` Bastien
2012-09-29 10:11             ` Achim Gratz
2012-09-29 10:58               ` Carsten Dominik
2012-09-29 12:02                 ` Achim Gratz
2012-09-30  7:30         ` Torsten Wagner

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:

  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=87sja2qe66.fsf@bzg.ath.cx \
    --to=bzg@altern.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=torsten.wagner@gmail.com \


* 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


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).