emacsorphanage / dart-mode

An Emacs mode for the Dart language
GNU General Public License v3.0
15 stars 2 forks source link

eh, indent wrong when exporting org-mode #119

Closed nesteiner closed 2 years ago

nesteiner commented 2 years ago

2022-02-09 22-59-29 的屏幕截图 2022-02-09 23-14-01 的屏幕截图

recently I found that org-mode can't handle the indent of dart language very well, other language is ok 2022-02-09 23-16-15 的屏幕截图

I don't 2022-02-09 23-16-00 的屏幕截图 know where the error is , can you check it out ??

bradyt commented 2 years ago

I would like a minimal reproduce. That would be a very small init file, with no more than a few expressions, and a very small org file, with steps to reproduce.

nesteiner commented 2 years ago

you need my configuration code ??

(with-eval-after-load 'org
  (use-package org-download
    :ensure t
    :defer t
    :bind (("C-S-y" . org-download-screenshot)
       ("C-S-p" . org-download-clipboard))

    :config
    (setq-default org-download-image-dir "~/ChiniBlogs/src/images/")
    (add-hook 'dired-mode-hook 'org-download-enable))

  (use-package org-bullets
    :ensure t
    :defer t
    :hook (org-mode . org-bullets-mode)
    :config (org-bullets-mode t))

  (require 'ox-publish)
  (require 'ox)

  (setq org-todo-keywords
    (quote ((sequence "TODO(t)" "DOING(i)" "|" "DONE(d)" "CANCEL(c@)")))

    org-todo-keyword-faces
    (quote (("TODO" :foreground "#FF0000" :weight bold)
        ("DOING" :foreground "cyan" :weight bold)
        ("DONE" :foreground "#7fff00" :weight bold)
        ("CANCEL" :foreground "#A020F0" :weight bold))))

  (setq org-startup-indented t
    org-export-preserve-breaks t
    org-publish-project-alist
    '(("blog-notes"
       :base-directory "~/ChiniBlogs/src/"
       :base-extension "org"
       :publishing-directory "~/ChiniBlogs/html/"
       :publishing-function org-html-publish-to-html
       :headline-levels 5
       :section-numbers 4
       :auto-preamble t
       :author "Steiner"
       :email "steiner3044@163.com"
       :html-head "<link rel=\"stylesheet\" type=\"text/css\" href=\"/home/steiner/ChiniBlogs/html/css/org.css\"/>"
       )
      ("blog-static"
       :base-directory "~/ChiniBlogs/src/images/"
       :base-extension "css\\|js\\|png\\|jpg\\|gif"
       :publishing-directory "~/ChiniBlogs/html/images/"
       :publishing-function org-publish-attachment)

      ("current-dir"
       :base-directory "."
       :base-extension "org"
       :publishing-directory "."
       :publishing-function org-html-publish-to-html
       :headline-levels 5
       :section-numbers 2
       :auto-preamble t
       :author "Steiner"
       :email "steiner3044@163.com"
       :html-head "<link rel=\"stylesheet\" type=\"text/css\" href=\"/home/steiner/ChiniBlogs/html/css/org.css\"/>")

      ("current-dir-markdown"
       :base-extension "org"
       :publishing-directory "."
       :publishing-function org-md-export-to-markdown
       :headline-levels 5
       :section-numbers 2
       :auto-preamble t
       :author "Steiner"
       :email "steiner3044@163.com"
       :html-head "<link rel=\"stylesheet\" type=\"text/css\" href=\"/home/steiner/ChiniBlogs/html/css/org.css\"/>")
      )))

(provide 'init-org)

and here is the code example

  #+begin_src dart
    class CounterHomePageState extends State<CounterHomePage> {
      int count = 0;

      void incrementCount() {
    setState(() {
          count += 1;
    });
      }

      @override
      Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(title: Text(widget.title),),
      body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('You have pushed the button this many times:'),
              Text(count.toString(), style: Theme
            .of(context)
            .textTheme
            .headline4),
              TextButton(
            child: Text('Open new route'),
            onPressed: () {
                Navigator.push(context, MaterialPageRoute(builder: (context){
                      return NewRoute();
                }));
              },)
            ]
          ),
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: incrementCount,
          tooltip: 'Increment',
          child: Icon(Icons.add),
      ),
    );
      }
    }
  #+end_src
bradyt commented 2 years ago

I meant something a little more minimal that works by itself with no other configuration, but I guess I wasn't specific enough.

Here's a small example that does not seem to have an issue exporting correct indentation:

> cd /tmp/testing
> HOME=$(pwd)

In /tmp/testing/.emacs, I put the following:

(setq-default indent-tabs-mode nil)

(package-initialize)

(setq package-archives
      '(("gnu" . "https://elpa.gnu.org/packages/")
        ("nongnu" . "https://elpa.nongnu.org/nongnu/")))

(unless package-archive-contents
  (package-refresh-contents))

(package-install 'dart-mode t)

Without myself knowing what minimal .org file reproduces the issue, I copied your Dart example to /tmp/testing/example.org.

I then start emacs in that terminal where HOME has been set to /tmp/testing. Even -nw works here. For example, emacs -nw example.org.

To create the html, I try C-c C-e h o. This opens the output in my default browser. I didn't notice any issues with indentation.

bradyt commented 2 years ago

It occurs to me that my example has the special line, (setq-default indent-tabs-mode nil). Then I noticed your .org example has tabs in it. You can use M-x whitespace-mode RET to see this. Do you want to prefer spaces over tabs in emacs?

If you'd like to convert tabs to space in a file, you can try C-x h to select all, then M-x untabify RET to convert.

I don't know if this is related to your issue.

Even if I keep the tabs in the file and export, the html has only space, no tabs.

bradyt commented 2 years ago

I was wrong. If I remove (setq-default indent-tabs-mode nil) from the init and restart emacs, the html output has tabs.

Also, I can do all the above without dart-mode. I'm not sure how dart-mode is relevant.

Can you check the result with emacs -Q -nw /tmp/testing/example.org, then C-c C-e h o?

You might have only noticed this with Flutter because of its very nested structure.

I would also wonder if you've changed tab width anywhere in Emacs, or what your browser thinks the display width of a tab should be.

bradyt commented 2 years ago

Emacs sets default width of tabs to 8, whereas tab-width is set to 2 in dart-mode, but I haven't convinced myself that difference is related.

If for some reason you need tab-width or indent-tabs-mode to be different than they currently are in dart-mode, one way to do that is on a dart-mode-hook, setting their buffer-local value.

nesteiner commented 2 years ago

2022-02-12 14-01-59 的屏幕截图

here you are, there is no problem

nesteiner commented 2 years ago

ps: the outputs are the same when I don't attach -Q -nw options

bradyt commented 2 years ago

Would I be correct if so far, you only see the problem in your org-publishing usage?

Can you take the following example to research? In my browser, the "1 tab" line lines up with the "8 spaces". Does that work the same for you everywhere? Do you still see the issue in your org publishing website?

#+begin_example
--------
    1 tab
0 spaces
--------
    1 tab
 1 space
--------
    1 tab
  2 spaces
--------
    1 tab
   3 spaces
--------
    1 tab
    4 spaces
--------
    1 tab
     5 spaces
--------
    1 tab
      6 spaces
--------
    1 tab
       7 spaces
--------
    1 tab
        8 spaces
--------
    1 tab
         9 spaces
#+end_example

Can you clarify that you still see the original issue in some few cases? If you still have the same issue, can you find a way to narrow it down so I can understand how to reproduce it?

bradyt commented 2 years ago

And here's how that might look with M-x whitespace-mode RET, ignoring what the width after » actually turns out to be in your Emacs.

#+begin_example$                                                                
--------$                                                                       
»       1·tab$                                                                  
0·spaces$                                                                       
--------$                                                                       
»       1·tab$                                                                  
·1·space$                                                                       
--------$                                                                       
»       1·tab$                                                                  
··2·spaces$                                                                     
--------$                                                                       
»       1·tab$                                                                  
···3·spaces$                                                                    
--------$                                                                       
»       1·tab$                                                                  
····4·spaces$                                                                   
--------$                                                                       
»       1·tab$                                                                  
·····5·spaces$                                                                  
--------$                                                                       
»       1·tab$                                                                  
······6·spaces$                                                                 
--------$                                                                       
»       1·tab$                                                                  
·······7·spaces$                                                                
--------$                                                                       
»       1·tab$                                                                  
········8·spaces$                                                               
--------$                                                                       
»       1·tab$                                                                  
·········9·spaces$                                                              
#+end_example$                                                                  
nesteiner commented 2 years ago

2022-02-12 16-42-49 的屏幕截图

eh, I think it might be the problem of publish style, can you try exporting with style ?

bradyt commented 2 years ago

Do you mean syntax highlighting? (package-install 'htmlize) is enough to achieve that, with the same C-c C-e h o.

Or what do you mean by publish style?

bradyt commented 2 years ago

I haven't used org-publish in years. You need me to construct a minimal usage of org-publish?

bradyt commented 2 years ago

Typo. Edited. I meant org-publish.

bradyt commented 2 years ago

Does the indentation bug still occur if you empty your org.css file?

bradyt commented 2 years ago

Also, did you try putting the example I provided in your org-publish page?

nesteiner commented 2 years ago

can you edit your answer with list :yum:
by the way, the style means css style
and this indent error only happen when using this style https://gongzhitaao.org/orgcss/
but I haven't try other style yet, you can add this #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://gongzhitaao.org/orgcss/org.css"/>

bradyt commented 2 years ago

Can you please produce a complete and minimal example that illustrates the issue? I don't think it should require a lot of elisp, nor a lot of CSS. Please narrow down the issue.

Since I don't understand how to reproduce the problem, I would suggest contacting org-mode via mailing list or IRC, or asking for help from other Emacs users via IRC on #emacs at Libera.chat.

Closing issue.

I will reopen issue when the issue is clear. As it stands, it's not clear to me this is related to dart-mode.

bradyt commented 2 years ago

Please try this:

(add-hook 'dart-mode-hook (defun my-set-dart-tab-width () (setq tab-width 8)))

You might want this for testing: (setq org-publish-use-timestamps-flag nil).

bradyt commented 2 years ago

Here's a minimal reproduce. Error occurs with C-c C-e h o in your org file. Uncommenting the last line is one way to fix it.

(package-initialize)

(setq package-archives
      '(("gnu" . "https://elpa.gnu.org/packages/")
        ("nongnu" . "https://elpa.nongnu.org/nongnu/")))

(let ((my-pkgs '(dart-mode htmlize)))
  (when (seq-remove 'package-installed-p my-pkgs)
    (package-refresh-contents)
    (mapc (lambda (pkg) (package-install pkg t)) my-pkgs)))

;; (add-hook 'dart-mode-hook (defun my-set-dart-tab-width () (setq tab-width 8)))

This seems to occur regardless of whether tabs are used. It doesn't seem to be a matter of tabs versus spaces, but instead, about the difference in tab-width set in org-mode versus dart-mode.

bradyt commented 2 years ago

I think (setq org-src-preserve-indentation t) will fix it too. If you search internet for "org-src-preserve-indentation tab-width export", you might see others have encountered this sort of thing. For example, https://lists.gnu.org/archive/html/emacs-orgmode/2014-02/msg00043.html.

Hmm, no, that might require that you replace tabs with spaces first.

Anyways, the minimal reproduce should be helpful, if you don't like modifying the tab-width in dart-mode, I recommend asking on the org-mode mailing list for their thoughts on the matter.