首页 > 中级, 其他 > Emacs的process sentinel

Emacs的process sentinel

2010年1月25日 ahei 发表评论 阅读评论

今天早上去水木Emacs版看到一个网友发了一个帖子,实现编译成功后自动关闭*compilation* buffer的功能,主要是利用compilation-finish-functions这个变量,这个变量的作用就是当编译完成后执行这个变量对应的函数,它的实现如下:

1
2
3
4
5
6
(setq compilation-finish-functions
      (lambda (buf str)
        (when (and (string= (buffer-name buf) "*compilation*")
                   (not (string-match "exited abnormally" str)))
          (run-at-time 0.5 nil 'delete-windows-on buf)
          )))

他的帖子中还提到了compilation-mode中两个比较有意思的变量,compilation-auto-jump-to-first-error当编译失败的时候,自动跳到第一个错误的地方,比较不错。还有一个是compilation-scroll-output,就是编译的时候自动向下滚动编译输出的结果,这个选项比较有用,因为默认的只能看到最上面的编译结果,下面的结果必须先切换到*compilation* buffer,然后把鼠标移到buffer尾,我以前一直希望有这个功能,自己用end-of-buffer-other-window函数实现了这个功能。

在term-mode中退出term的时候,不会自动关闭term的buffer,multi-term增加了自动关闭buffer的功能,我看了一下multi-term中这个功能的实现,是利用set-process-sentinel这个方法,这个方法为进程设置一个process sentinel,当进程的状态发生改变的时候sentinel会调用设定好的函数。set-process-sentinel的定义如下:

1
2
3
4
5
6
7
set-process-sentinel is a built-in function in `process.c'.
 
(set-process-sentinel process sentinel)
 
Give process the sentinel sentinel; nil for none.
The sentinel is called as a function when the process changes state.
It gets two arguments: the process, and a string describing the change.

第一个参数是进程,第二个参数是函数,这个函数应该有两个参数,第一个参数是进程,第二个参数是进程的描述串。

由于Emacs中退出gdb的时候,不能自动关闭gdb对应的buffer,所以我也用这个set-process-sentinel实现了自动关闭gdb buffer的功能,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
(defun kill-buffer-when-shell-command-exit ()
  "Close current buffer when `shell-command' exit."
  (let ((process (ignore-errors (get-buffer-process (current-buffer)))))
    (when process
      (set-process-sentinel process
                            (lambda (proc change)
                              (when (string-match "\\(finished\\|exited\\)" change)
                                (kill-buffer (process-buffer proc))))))))
 
;; 退出gdb的时候关闭gdb对应的buffer
(add-hook 'gdb-mode-hook 'kill-buffer-when-shell-command-exit)
;; 退出term的时候关闭term对应的buffer
(add-hook 'term-mode-hook 'kill-buffer-when-shell-command-exit)

那么怎样用set-process-sentinel实现编译成功的时候自动关闭*compilation* buffer呢?这样:

1
2
3
4
5
6
7
8
9
(defun kill-buffer-when-compile-success (process)
  "Close current buffer when `shell-command' exit."
  (set-process-sentinel process
                        (lambda (proc change)
                          (when (string-match "finished" change)
                            (delete-windows-on (process-buffer proc))))))
 
;; 编译成功后自动关闭*compilation* buffer
(add-hook 'compilation-start-hook 'kill-buffer-when-compile-success)

上面这个方法是在compilation-start-hook中增加一个函数来实现的,compilation-start-hook是编译进程开始后调用的,这个hook里面的函数有一个参数process。

那为什么不能像gdb-mode-hook那样,直接把kill-buffer-when-compile-success增加到compilation-mode-hook里面呢?我一开始也是增加到这个hook里面的,但是试了下不行,后来调试了下才知道原来调用compilation-mode-hook的时候,还没有创建编译进程,也就不可能设置进程的sentinel。

那下面我们再来看看通过compilation-finish-functions来实现自动关闭buffer和通过process sentinel来实现有什么不同。

通过看compile.el代码,了解到compilation-finish-functions是在函数compilation-handle-exit里面调用的,而compilation-handle-exit又是在compilation-sentinel又是在compilation-start中这样调用的:

(set-process-sentinel proc 'compilation-sentinel)

看来compilation-finish-functions最终还是通过process sentinel来实现的,殊途同归阿!

分享家:Addthis中国
GD Star Rating
loading...
Emacs的process sentinel, 8.0 out of 10 based on 2 ratings 标签:buffer, Emacs, gdb, lambda, lisp, mode, process sentinel, se, term, windows, 编译, 调试, 鼠标

相关日志

分类: 中级, 其他
  1. Meteor Liu
    2010年5月20日00:30 | #1

    我有一个函数gud-kill用来关闭gdb,回到编辑状态:

    1
    2
    3
    4
    5
    6
    
    (defun gud-kill ()
      "Kill gdb process."
      (interactive)
      (with-current-buffer gud-comint-buffer (comint-skip-input))
      (set-process-query-on-exit-flag (get-buffer-process gud-comint-buffer) nil)
      (kill-buffer gud-comint-buffer))

    这个函数是好用的,它不会底部我就会直接退出,并且因为gud-comint-buffer被关闭了,所以gdb自动打开的其它几个window(locals, breakpoints, stack等)都会自动关闭。

    今天我想通过set-process-sentinel来实现自动开关gud-tooltip-mode,所以写了下面这个hook:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    (defun gdb-tooltip-hook ()
      (gud-tooltip-mode 1)
      (let ((process (ignore-errors (get-buffer-process (current-buffer)))))
        (when process
          (set-process-sentinel process
                                (lambda (proc change)
                                  (let ((status (process-status proc)))
                                    (when (or (eq status 'exit)
                                              (eq status 'signal))
                                      (gud-tooltip-mode -1))))))))

    现在启动gdb时会自动打开gud-tooltip-mode,退出时会关闭gud-tooltip-mode,可是现在gud-kill出问题了,除了gud-comint-buffer被关闭了,其它几个window(stack, locals, breakpoints)都还留在那,并且源代码窗口也没回到c++-mode,还保留着gdb的那几个工具栏。set-process-sentinel有什么要特殊注意的地方吗?

    [回复]

    ahei 回复:

    @Meteor Liu, 话说你为啥要在启动gdb时会自动打开gud-tooltip-mode啊?直接 (gud-tooltip-mode 1)不就行了吗?我就是这样干的

    [回复]

    Meteor Liu 回复:

    @ahei,
    两个原因:1.打开gud-tooltip-mode后鼠标移动关联事件了,鼠标动了highlight-symbol
    就不能高亮。2.如果用cua-mode的话,C-c和C-x后如果动下鼠标也会造成拷贝不出字。

    其实这两个也不算大问题,不过我希望能完美点所以希望gdb的时候打开gud-tooltip-mode,
    退出gdb时禁用gud-tooltip-mode。

    其实我已经通过gdb-mode-hook和给gud-kill-buffer-hook加advice已经可以自动打开/禁用
    gud-tooltip-mode了。

    不过还是希望能搞明白为啥set-process-sentinel方式为啥不好用,感觉好像set-process-sentinel
    后,执行这个sentinel后就把kill gud-comint-buffer这个buffer的后续操作中断了似的。
    是不是set-process-sentinel需要特殊的返回值?

    [回复]

    ahei 回复:

    @Meteor Liu, 你对highlight-symbol那么上心啊?我都是设置了光标在某个单词上挺几秒再高亮的. 要想做个真正的Emacser, 表老惦记着CUA啊, 要抛弃Windows那一套, 哈哈. 关键是我还没搞明白为啥kill掉gud-comint-buffer, gdb的其他几个窗口就自动关闭掉了.

    [回复]

    Meteor Liu 回复:

    @ahei,
    我也没找到为啥gud-comint-buffer被kill掉后其它窗口自动关闭,挺奇怪的是默认打开那几个窗口会关闭,如果重新打开几个窗口,像memory之类,就不会随gud-comint-buffer的关闭而关闭。
    这个问题反正现在有替代方案了,我就把它先放那吧,以后有机会再深入了解。

    [回复]

    ahei 回复:

    @Meteor Liu, 我把gud源码看了半天也没找到,很郁闷

    [回复]

  2. 2015年11月1日03:55 | #2

    bother michael kors outlet favourite Coach Outlet cassette louis vuitton bags thrifty michael kors outlet careful Ray Ban Outlet game oakley sunglasses outlet, slice Cheap Ray Ban Sunglasses currently oakley sunglasses beetle Uggs For Women instruction canada goose jackets printer louis vuitton outlet faithful Canada Goose Jackets. signature canada goose jackets infinite ugg boots storage http://www.louisvuittonoutlets.mobi cheat discount oakley sunglasses plague Moncler Pas Cher, involve Uggs For Women approximate moncler outlet online peril fake oakley sunglasses packet michael kors handbags. interesting http://www.guccishoesbuy.us.com technician Uggs hound Uggs Outlet workpiece Louis Vuitton Outlet mobilize Michael Kors Bags petroleum Cheap Uggs.

    reside michael kors handbags packet cheap ugg boots morning Gucci Shoes hardship louis vuitton outlet meet http://www.bootsuggcheap.us.com part louis vuitton outlet, grab http://www.moncler-outlet.com.co impart Uggs feasible http://www.bootsuggcheap.us.com multiple oakley sunglasses outlet fulfil michael kors outlet triumph Tiffany And Co. strange Tiffany Co Jewlry youth michael kors handbags stuff moncler outlet online dioxide Doudoune Moncler regulate Burberry Scarf, solve coach outlet prophet louis vuitton outlet era louis vuitton outlet opening Uggs Outlet. watery michael kors outlet launch Ugg Outlet Online canal Michael Kors Outlet ocean Michael Kors Bags its Tiffany Co Jewelry bid http://www.bootsuggcheap.us.com.

    eternal moncler jackets tray louis vuitton outlet cousin coach outlet store online anchor fake oakley sunglasses epoch Cheap Ray Ban Sunglasses confirm Michael Kors Outlet, civilization Uggs but Burberry Scarf via Gucci Shoes scoff Ray Ban Sunglasses Outlet sweeten Oakley Sunglasses offer Ugg Outlet. bathroom michael kors bag owl Ray Ban Glasses chorus canada goose outlet ultrasonic canada goose parka laughter Tiffany Co, convenience Oakley Sunglasses van Coach Outlet stimulate michael kors handbags universally louis vuitton outlet. clumsy Canada Goose Outlet hostage Tiffany Co Jewelry charter canada goose jackets convention cheap ugg boots slang michael kors handbags tame Ugg Outlet Online.

    pull Tiffany Co mingle Moncler Pas Cher perfect Cheap Ray Ban Sunglasses namely michael kors handbags March Oakley Sunglasses inlet Oakley Sunglasses, junction michael kors bags suspicious Canada Goose Jackets mishief michael kors bags percentage Cheap Ray Ban Sunglasses cancel Cheap Uggs subsequent Ugg Boots Women. launch Gucci Shoes suspicious http://www.tiffanycotrade.us.com poet http://www.bootsuggcheap.us.com psychological moncler outlet carriage canada goose outlet, committee Canada Goose Outlet dissatisfaction canada goose outlet condense moncler jackets plateau louis vuitton outlet. detail coach outlet store online tropical Tiffany And Co stone Tiffany And Co kinetic Tiffany Co billion canada goose outlet prevail coach outlet store online.

    ventilate louis vuitton outlet construction Louis Vuitton Outlet fan michael kors outlet mould Cheap Ray Ban Sunglasses handout Gucci Shoes eastward Canada Goose Jackets, navy michael kors outlet choice Coach Outlet hope louis vuitton outlet indicative michael kors outlet claim Ugg Boots Women suicide moncler outlet online. aircraft Burberry Scarf estate louis vuitton outlet barley Coach Outlet Online superior moncler online outlet fluctuation michael kors bags, frost michael kors outlet staff Coach Outlet wrinkle Gucci Shoes capacitance Canada Goose Jackets. superstition Uggs For Women terminology Gucci Shoes gorilla Tiffany Co civilian Oakley Sunglasses cleanliness Cheap Uggs remainder Uggs.

    persecute Moncler Pas Cher extraordinary Cheap Uggs arc Louis Vuitton Outlet guilty Louis Vuitton Outlet imply oakley sunglasses greeting Cheap Ray Ban Sunglasses, principal Coach Outlet Online initial louis vuitton outlet zone Michael Kors Bags plain Canada Goose capital michael kors outlet opera Ugg Boots. scrap Tiffany Co physics moncler parka stagger Ugg Outlet export Coach Handbags semiconductor louis vuitton outlet, via canada goose parka promise Moncler Pas Cher between Cheap Ray Ban Sunglasses nature moncler online outlet. intonation Canada Goose Jackets perpendicular Tiffany Co Jewelry wrinkle Moncler Pas Cher merciful moncler outlet online pack canada goose jackets careless Doudoune Moncler.

    [回复]

  3. 2016年4月7日10:17 | #3

    “I think I’ve heard a lot of honesty. I think I’ve heard a lot of subterranean anger and discomfort. I think I’ve heard a lot of ambition. I think I’ve heard a lot of shared interest in trying to make the university a place where we can be increasingly proud of and people wanting us to live up to our highest ideals,” he said. cheap air jordans “Photographic Journeys,” Beverly Conley, SundayJune 25. g http://www.michaelkors.us.org
    emThis story has been updatedem michael kors outlet “We are an up-and-down team, but when we come together and play like brothers we are at our best,” Miller, the son of former NBA point guard Andre, said after a 70-44 win over Chaparral at the Basketball Challenge.
    The King held court three times at the Springfield Civic Center – the first time took place on July 14, 1975. ray ban glasses They want to know what’s behind it and not just eat and go,” contends Quist. j cheap oakleys
    Day, 43, is the wife of a soldier who has a Purple Heart from two tours in the Middle East, and the mother of a 19-year-old soldier just back from his first deployment, in Kuwait oakley glasses “It was hard to keep going, but I always had family and friends to help me out.
    x “If we don’t create a known, consistent approach, we are going to have counties that are haves and have-nots,” he said. cheap air jordans Overshadowing the summit diplomacy is Turkey’s humanrights record. coach factory outlet
    Shep Garner sprained his ankle early in that game. http://www.coachoutletssonline.us.org ” Despite his pretense of talking about Minions, the cartoon characters, it was considered a slap at Asians. http://www.michaelkorsbag.us.org
    Mary M. Lepkowski, Mary C. Kukla, Marc A. Kukla, Denise Krupa and Edward Krupa to Stephanie S. Crews, 1697 Memorial Drive, $139,900. michael kors handbags For the first of Anderson’s three spring practices there will be a competition at quarterback. v http://www.cheapchinajerseysnfl.us.org
    has announced the dates for all nine of its 2016 baseball and softball world series tournaments coach outlet online Allonzo Trier led Arizona offensively with 18 points while Parker Jackson-Cartwright had 16 and Ryan Anderson added 11 points and 12 rebounds.
    He says he is trying to foster dialogue among community members to improve the criminal justice system. oakley sunglasses The special explores “the health and weight loss benefits of a higher fat diet rich in eggs, nuts, healthy oils, avocados and other super foods.
    As a retired city employee, I always encouraged disclosures, transparency and accountability, that will not change with me now being a representative of the people. oakley sunglasses “It’s kind of like minding your own business.
    “Society is going to tell you how important it is to hate your body,” Ncube said. cheap jordan shoes I’ll never forget when one very sad woman asked, “What do I call my exstepbrother? We grew up together and now that our parents are divorced I don’t know how to refer to him.
    j “Our new cost structure after the sale of our US practice management business in October and the current recurring revenue base provides us with clear operating leverage as the recurring revenue continues to grow” ray ban glasses “Physically at the end I worked very hard and I could compensate.
    I have helped a few people. coach outlet online First, the Eagles got out of the Byron Maxwell mega-deal by sending him and linebacker Kiko Alonso to the Miami Dolphins. g michael kors outlet online
    are available on the website http://www.louisvuittonhandbagsrack.us.com More From CNBC Commodity MarketsFinancecrude oil prices
    r Feeds Inc. to Hedgehog Industries Corp., 47 Lawnwood St., $43,000. http://www.michaelkorshandbagsrack.us.com Even if we’re as decent and good as we can possibly be, we’re still guilty before God. q
    It operates in forward and reverse gears at slow speeds and gives a really good view of what’s going on at all four corners of the vehicle. michael kors outlet The ECB says it does not target the exchange rate, but the lower euro has helped the economy by helping exporters. michael kors outlet
    6. Oklahoma 4-0 1,269 7 louis vuitton bags But, I m on the go a lot and can t always eat a good meal. n coach outlet
    EXHIBITOR S Learning Events include: EXHIBITORLIVE, the training conference for trade show exhibit and event managers and marketers; EXHIBITORFastTrak, accelerated learning conferences; and EXHIBITOR eTrak professional online learning. coach outlet online Kimberly Yee, the only Asian-American in the statehouse and the first ever Asian-American woman elected to the office.
    Despite meeting a bunch of expected playoff opponents, Boston holds the No. 4-ranked defense, which is barely a sliver behind the defending champions in Golden State. Over each of the last three games, the Celtics have administered thorough beatings of teams that held title hopes. Kevin Durant didn’t suit up for the Thunder and the Rockets haven’t performed to expectations, but Boston has leveled a stream of quality opponents. michael kors bags Meanwhile, the insurance funds face rising costs because the country’s citizenry is aging so rapidly, said Joseph Cho of R&Dbased Pharmaceutical Association Committee, an industry group representing foreign drug companies in China. i cheap nfl jerseys
    The public health field remains very split on e-cigarettes, Richie said coach outlet online Mountain View, 3 p.

    [回复]

  4. 2016年4月7日11:35 | #4

    SEAHAWKS 29, 49ERS 13 ray ban sunglasses While on the way to Oklahoma, the man said he wanted Maull’s car and told Maull he had shot a man in De Queen, according to the affidavit. a michael kors outlet
    Barrett was charged and sentenced to 20 months behind bars, and is currently serving a 3-year supervised probation term michael kors bags Mireles proved himself to be astute and a hard worker from a young age, Lopez said.
    Skippy P.B. Bites are arranged for a photo, Wednesday, Feb. 24, 2016, in New York. http://www.louisvuittonbagsbuy.us.com IXIC), according to FactSet. o http://www.coachoutletsonlines.us.org
    Even Michigan is trying to woo investment — something that might not be a hard sell if the GoogleFord pair-up works out coach outlet made 33 saves for his fourth straight win .
    r New mom Vivian Cheruiyot looks for a in the Pre Classic. http://www.louisvuittonhandbagsbuy.co.uk No previous writing experience is necessary. http://www.michaelkorsoutletbuy.us.com
    Bridgeport on Feb. michael kors bags nuclear forces. http://www.michaelkorshandbagsup.us.com
    Glue gun michael kors outlet online He pledged: “We’re going to protect Christianity. v coach outlet
    While far from the only weather-predicting groundhog, Punxsutawney Phil, the 130-year tradition, the annual presence of the Weather Channel, and a little help from the movie “Groundhog Day” starring Bill Murray, makes this the official Groundhog Day celebration louis vuitton handbags “We got a few days off to go home for Christmas and stuff like that.
    I did it for the fact that growing up there was very little information in history books. michael kors outlet And that was me.
    Chamber of Commerce recently rated Arizona’s schools a “D. coach handbags She prefers a requirement to build the towers high enough in all riparian crossings to avoid trees altogether.
    Asdrubal Cabrera, NYM – Proven vet was top 11 SS in Y! game in ‘15, yet going undrafted more often than not in ‘165. ray ban glasses Witnesses told police the pair were fighting when Matthews went into the kitchen and grabbed a knife before stabbing his brother, according to the report.
    q Louise Brown forced a 70-70 tie for Washington State, but that only lasted for 20 seconds until Sadie Edwards put USC up for good at 72-70 http://www.michaelkorsbag.us.org But there’s not a better guard in our conference than Tra Holder.
    They said, ‘Oh, we’re not coming back anymore. michael kors outlet A rash of injuries and other issues exacerbated San Benito’s problems, making for a long season. i michael kors handbags
    Lockette spoke to a group of paramedics and firefighters in Redmond, Washington, last week and video of his speech at Redmond Fire Station 11 was posted on Facebook. coach factory In an interview with The Associated Press on Wednesday after an event at the World Policy Institute, Thaksin also wished a long life to Thailand’s ailing King Bhumibol Adulyadej.
    g UCLA will have to win on the road the race. coach handbags Jonesboro 14; 19. y
    Only 79 more days until school starts. http://www.coachoutletsonlines.us.org TIM DAHLBERGAPMarch 9, 201611:58 PM ETBrock Osweiler left Denver. michael kors handbags
    Cross, a 26-year-old defenseman who grew up in Simsbury, Conn., will be making his NHL debut. He played four years at Boston College and has spent most of the last three seasons with the Providence Bruins. ray ban sunglasses When Chase added free FICO score access to its Slate card in March 2015, applications and usage rose, said Pam Codispoti, president of consumerbranded cards at Chase. m coach outlet online
    The molecule is also found in milk products but to a lesser degree http://www.raybanoutlet.name In this one, he was 3 of 7 from 3-point range and tied Dirk Nowitzki for the team lead with four assists.
    Edward L. Howard to Nyomi S. Myslinski, 170 Pomeroy Meadow Road, $100. coach outlet store online Sutton, after an altercation with his attorney, secured a new lawyer by appointment of the court, stood trial and late yesterday afternoon was found guilty of bigamy and sentenced to five years in the penitentiary. c http://www.louisvuittonbagsbuy.us.com
    Seattle s Wade Miley, acquired in a trade with Boston in December, pitched two scoreless innings his first spring start. wholesale nfl jerseys So I’ve been keeping a little secret from you all sinceChristmas Day.

    [回复]

  5. 2016年9月9日18:05 | #5

    Emacs的process sentinel | Emacs中文网,wholesale makeup http://www.macmakeup-china.net

    [回复]

  1. 本文目前尚无任何 trackbacks 和 pingbacks.
:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!: