help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is there any existing solution to move list number backwards, forwar


From: Jean Louis
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
User-agent: Mutt/2.2.7+37 (a90f69b) (2022-09-02)

* 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 via email to

[Prev in Thread] Current Thread [Next in Thread]