all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jean Louis <bugs@gnu.support>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Is there any existing solution to move list number backwards, forwards in a list?
Date: Fri, 21 Oct 2022 18:54:32 +0300	[thread overview]
Message-ID: <Y1LAuCHM+TosOlql@protected.localdomain> (raw)
In-Reply-To: <jwvwn8txqvu.fsf-monnier+emacs@gnu.org>

* Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2022-10-21 16:34]:
> > Let us say item is number 7 in following list:
> >
> > '(1 2 3 4 5 6 7 8 9)
> >
> > Then I wish to move item number 7 to different number and renumber all
> > items so that number 7 becomes number 6 and number 6 comes in the
> > place of number 7.
> 
> That was clear (ahem) :-)

Thanks. You got it, though my expression is not perfect.

> > Situation is more complex,
> 
> I suspect so.  From where I stand, the normal answer is to change the
> priority of the relevant object and then to pass your list to `sort`
> with the appropriate so it re-sorts based on the new priorities.

I have decided to use hash with ID being the key, and value being the
priority. Then in one SQL set I update it all at once. Now it is
pretty fast.

(defun hyperscope-hyperdocument-set-priority (hyperscope-set id direction)
  (let* ((hyperdocuments (rcd-sql-list (cond (hyperscope-set "SELECT hyobjects_id FROM hyobjects
                                                               WHERE hyobjects_parent = $1
                                                            ORDER BY hyobjects_priorities, hyobjects_id")
					     (t "SELECT hyobjects_id FROM hyobjects
                                                  WHERE hyobjects_parent = NULL
                                               ORDER BY hyobjects_priorities, hyobjects_id"))
                        	       cf-db hyperscope-set))
	 (hash (make-hash-table :test #'equal))
	 (renumbered (let ((count 20))
		       (mapcar (lambda (id)
				 (puthash id (* (setq count (1+ count)) 10) hash))
			       hyperdocuments))))
    (cond ((eq direction 'up) (puthash id (- (gethash id hash) 15) hash))
	  ((eq direction 'down) (puthash id (+ (gethash id hash) 15) hash))
	  (t (error "Verify arguments to `hyperscope-hyperdocument-set-priority'")))
    (let* ((keys (hash-table-keys hash))
	  (sql (with-temp-buffer 
		 (insert "BEGIN;\n")
		 (while keys
		   (let ((key (pop keys)))
		     (insert (format "UPDATE hyobjects set hyobjects_priorities = %s 
                                       WHERE hyobjects_id = %s;\n"
				     (gethash key hash) key))))
		 (insert "COMMIT;\n")
		 (buffer-string))))
      (rcd-sql sql cf-db))))


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



      reply	other threads:[~2022-10-21 15:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-21  7:56 Is there any existing solution to move list number backwards, forwards in a list? Jean Louis
2022-10-21  9:28 ` [SOLVED]: " Jean Louis
2022-10-21 13:13 ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-10-21 15:54   ` Jean Louis [this message]

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y1LAuCHM+TosOlql@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.