cute-jumper / org-table-sticky-header

Sticky header for org-mode tables
36 stars 4 forks source link

Added support for moving, inserting and deleting columns #8

Closed swhahn closed 7 years ago

swhahn commented 7 years ago

Hello, I added following functionality to change the sticky line if moving, inserting or deleting columns. Please feel free to use the following code snippet. With kind regards, Stefan

modified   lisp/org-table-sticky-header.el
@@ -79,6 +79,7 @@
 ;;; Code:

 (require 'org)
+(require 'org-table)

 (defvar org-table-sticky-header--last-win-start -1)
 (defvar org-table-sticky-header--old-header-line-format nil)
@@ -171,6 +172,14 @@ org-table-sticky-header--scroll-function
     (save-match-data
       (org-table-sticky-header--fetch-header))))

+(defun org-table-sticky-header--insert-delete-column ()
+  (save-match-data
+    (org-table-sticky-header--fetch-header)))
+
+(defun org-table-sticky-header--table-move-column (&optional left)
+  (save-match-data
+    (org-table-sticky-header--fetch-header)))
+
 ;;;###autoload
 (define-minor-mode org-table-sticky-header-mode
   "Sticky header for org-mode tables."
@@ -181,10 +190,16 @@ org-table-sticky-header-mode
             (setq org-table-sticky-header--old-header-line-format header-line-format)
             (add-hook 'window-scroll-functions
                       'org-table-sticky-header--scroll-function 'append 'local)
+            (advice-add 'org-table-delete-column :after #'org-table-sticky-header--insert-delete-column)
+            (advice-add 'org-table-insert-column :after #'org-table-sticky-header--insert-delete-column)
+            (advice-add 'org-table-move-column :after #'org-table-sticky-header--table-move-column)
             (setq org-table-sticky-header--last-win-start (window-start))
             (org-table-sticky-header--fetch-header))
         (setq org-table-sticky-header-mode nil)
         (error "Not in `org-mode'"))
+    (advice-remove 'org-table-delete-column #'org-table-sticky-header--insert-delete-column)
+    (advice-remove 'org-table-insert-column #'org-table-sticky-header--insert-delete-column)
+    (advice-remove 'org-table-move-column #'org-table-sticky-header--table-move-column)
     (remove-hook 'window-scroll-functions 'org-table-sticky-header--scroll-function 'local)
     (setq header-line-format org-table-sticky-header--old-header-line-format)))
cute-jumper commented 7 years ago

Thanks. The current implementation chooses to update the header only when window scrolls. It makes the header line unaware of the column changes (moving, deleting, adding etc.) so the code above is necessary. Can you make a PR so that I can accept it? 😄

swhahn commented 7 years ago

Done.