【问题标题】:Modify emacs indentation behavior for comments in fortran修改 fortran 中注释的 emacs 缩进行为
【发布时间】:2013-12-11 08:45:49
【问题描述】:

我正在编写一个 fortran 代码。源代码以老式 f77 风格编写,但采用 f90 注释风格,带有“!”被授权。我想在使用 cmets 在行上点击选项卡时编辑 emacs 行为。我可以区分 3 种类型的 cmets:

      program helloworld

      integer:: i,j

      do i=1,1
         do j=1,1
* This is a first type of comment
*    another first type comment
            ! second type of comment
            print *,"helloworld" ! third type of comment
         enddo
      enddo ! another third type comment

      end program helloworld

当使用注释类型在每一行点击标签时,我得到了

program helloworld

      integer:: i,j

      do i=1,1
         do j=1,1
*     This is a first type of comment
*     another first type comment
! second type of comment
            print *,"helloworld" ! third type of comment
         enddo
      enddo                     ! another third type comment

      end program helloworld

我想要的行为是,在一行中点击标签:

  • 第一类评论:什么都不做
  • 第二种注释:将行缩进为指令行
  • 第三种注释:将行作为指令行缩进,指令和后面的注释之间不加空格

我试图在我的 init.el 中重写一些来自 fortran.el 的函数,但对 lisp 感到很疯狂。如果一些 lisp/emacs 战士可以帮助我,我会很高兴。

谢谢

【问题讨论】:

    标签: emacs fortran elisp fortran90 fortran77


    【解决方案1】:

    您可以通过以下方式为您的第 2 和第 3 类 cmets 实现您想要的:

    (setq fortran-comment-indent-style 'relative)
    (add-hook 'fortran-mode-hook (lambda () (setq comment-column 0)))
    

    对于第一种类型的 cmets,您需要一个 hack,如下所示:

    (defadvice fortran-indent-line (after custom-indentation activate)
      (save-excursion
        (forward-line 0)
        (when (looking-at "*")
          (forward-char 1)
          (just-one-space))))
    

    编辑

    尊重您的最后一条评论需要更复杂、更丑陋的 hack。 用这个替换之前的defadvice

    (defadvice fortran-indent-line (around custom-indentation activate)
      (let ((type-* (save-excursion
                      (forward-line 0)
                      (looking-at "\s*\\*")))
            (type-! (save-excursion
                      (forward-line 0)
                      (looking-at "\s*!"))))
        (if type-!
            (progn
              (save-excursion
                (forward-line 0)
                (re-search-forward "!")
                (replace-match "__"))
              ad-do-it
              (save-excursion
                (forward-line 0)
                (re-search-forward "__")
                (replace-match "!"))
              )
          (if (not type-*)
              ad-do-it))))
    

    【讨论】:

    • 好的,它适用于第 2 种(除了 '!' 留在第一列)第 3 种类型的 cmets(不添加额外的空格)。谢谢!对第一种类型有任何想法吗?
    • 是的,它几乎是完美的,除了我的第一种类型 cmets 可能在 '*' 和第一个非空格字符之间有超过 1 个空格。如果 '!'可以“关注”第二种类型的评论(而不是留在第 0 列),那绝对是美妙的!
    • 仍然几乎完美。对于第二种类型:1)如果光标位于第一个非空格字符之后作为点击标签,它确实很奇怪! 2)它会擦除“!”之间的每个空格和第一个非空格字符。再次感谢!
    • 有效!力量与你同在!非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多